GraphicsOutput.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /** @file
  2. Implementation for a generic GOP driver.
  3. Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "GraphicsOutput.h"
  7. CONST ACPI_ADR_DEVICE_PATH mGraphicsOutputAdrNode = {
  8. {
  9. ACPI_DEVICE_PATH,
  10. ACPI_ADR_DP,
  11. { sizeof (ACPI_ADR_DEVICE_PATH), 0 },
  12. },
  13. ACPI_DISPLAY_ADR (1, 0, 0, 1, 0, ACPI_ADR_DISPLAY_TYPE_VGA, 0, 0)
  14. };
  15. EFI_PEI_GRAPHICS_DEVICE_INFO_HOB mDefaultGraphicsDeviceInfo = {
  16. MAX_UINT16, MAX_UINT16, MAX_UINT16, MAX_UINT16, MAX_UINT8, MAX_UINT8
  17. };
  18. //
  19. // The driver should only start on one graphics controller.
  20. // So a global flag is used to remember that the driver is already started.
  21. //
  22. BOOLEAN mDriverStarted = FALSE;
  23. /**
  24. Returns information for an available graphics mode that the graphics device
  25. and the set of active video output devices supports.
  26. @param This The EFI_GRAPHICS_OUTPUT_PROTOCOL instance.
  27. @param ModeNumber The mode number to return information on.
  28. @param SizeOfInfo A pointer to the size, in bytes, of the Info buffer.
  29. @param Info A pointer to callee allocated buffer that returns information about ModeNumber.
  30. @retval EFI_SUCCESS Valid mode information was returned.
  31. @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the video mode.
  32. @retval EFI_INVALID_PARAMETER ModeNumber is not valid.
  33. **/
  34. EFI_STATUS
  35. EFIAPI
  36. GraphicsOutputQueryMode (
  37. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  38. IN UINT32 ModeNumber,
  39. OUT UINTN *SizeOfInfo,
  40. OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
  41. )
  42. {
  43. if ((This == NULL) || (Info == NULL) || (SizeOfInfo == NULL) || (ModeNumber >= This->Mode->MaxMode)) {
  44. return EFI_INVALID_PARAMETER;
  45. }
  46. *SizeOfInfo = This->Mode->SizeOfInfo;
  47. *Info = AllocateCopyPool (*SizeOfInfo, This->Mode->Info);
  48. return EFI_SUCCESS;
  49. }
  50. /**
  51. Set the video device into the specified mode and clears the visible portions of
  52. the output display to black.
  53. @param This The EFI_GRAPHICS_OUTPUT_PROTOCOL instance.
  54. @param ModeNumber Abstraction that defines the current video mode.
  55. @retval EFI_SUCCESS The graphics mode specified by ModeNumber was selected.
  56. @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.
  57. @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
  58. **/
  59. EFI_STATUS
  60. EFIAPI
  61. GraphicsOutputSetMode (
  62. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  63. IN UINT32 ModeNumber
  64. )
  65. {
  66. RETURN_STATUS Status;
  67. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Black;
  68. GRAPHICS_OUTPUT_PRIVATE_DATA *Private;
  69. if (ModeNumber >= This->Mode->MaxMode) {
  70. return EFI_UNSUPPORTED;
  71. }
  72. Private = GRAPHICS_OUTPUT_PRIVATE_FROM_THIS (This);
  73. Black.Blue = 0;
  74. Black.Green = 0;
  75. Black.Red = 0;
  76. Black.Reserved = 0;
  77. Status = FrameBufferBlt (
  78. Private->FrameBufferBltLibConfigure,
  79. &Black,
  80. EfiBltVideoFill,
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. This->Mode->Info->HorizontalResolution,
  86. This->Mode->Info->VerticalResolution,
  87. 0
  88. );
  89. return RETURN_ERROR (Status) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
  90. }
  91. /**
  92. Blt a rectangle of pixels on the graphics screen. Blt stands for BLock Transfer.
  93. @param This Protocol instance pointer.
  94. @param BltBuffer The data to transfer to the graphics screen.
  95. Size is at least Width*Height*sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL).
  96. @param BltOperation The operation to perform when copying BltBuffer on to the graphics screen.
  97. @param SourceX The X coordinate of source for the BltOperation.
  98. @param SourceY The Y coordinate of source for the BltOperation.
  99. @param DestinationX The X coordinate of destination for the BltOperation.
  100. @param DestinationY The Y coordinate of destination for the BltOperation.
  101. @param Width The width of a rectangle in the blt rectangle in pixels.
  102. @param Height The height of a rectangle in the blt rectangle in pixels.
  103. @param Delta Not used for EfiBltVideoFill or the EfiBltVideoToVideo operation.
  104. If a Delta of zero is used, the entire BltBuffer is being operated on.
  105. If a subrectangle of the BltBuffer is being used then Delta
  106. represents the number of bytes in a row of the BltBuffer.
  107. @retval EFI_SUCCESS BltBuffer was drawn to the graphics screen.
  108. @retval EFI_INVALID_PARAMETER BltOperation is not valid.
  109. @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.
  110. **/
  111. EFI_STATUS
  112. EFIAPI
  113. GraphicsOutputBlt (
  114. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  115. IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
  116. IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
  117. IN UINTN SourceX,
  118. IN UINTN SourceY,
  119. IN UINTN DestinationX,
  120. IN UINTN DestinationY,
  121. IN UINTN Width,
  122. IN UINTN Height,
  123. IN UINTN Delta OPTIONAL
  124. )
  125. {
  126. RETURN_STATUS Status;
  127. EFI_TPL Tpl;
  128. GRAPHICS_OUTPUT_PRIVATE_DATA *Private;
  129. Private = GRAPHICS_OUTPUT_PRIVATE_FROM_THIS (This);
  130. //
  131. // We have to raise to TPL_NOTIFY, so we make an atomic write to the frame buffer.
  132. // We would not want a timer based event (Cursor, ...) to come in while we are
  133. // doing this operation.
  134. //
  135. Tpl = gBS->RaiseTPL (TPL_NOTIFY);
  136. Status = FrameBufferBlt (
  137. Private->FrameBufferBltLibConfigure,
  138. BltBuffer,
  139. BltOperation,
  140. SourceX,
  141. SourceY,
  142. DestinationX,
  143. DestinationY,
  144. Width,
  145. Height,
  146. Delta
  147. );
  148. gBS->RestoreTPL (Tpl);
  149. return RETURN_ERROR (Status) ? EFI_INVALID_PARAMETER : EFI_SUCCESS;
  150. }
  151. CONST GRAPHICS_OUTPUT_PRIVATE_DATA mGraphicsOutputInstanceTemplate = {
  152. GRAPHICS_OUTPUT_PRIVATE_DATA_SIGNATURE, // Signature
  153. NULL, // GraphicsOutputHandle
  154. {
  155. GraphicsOutputQueryMode,
  156. GraphicsOutputSetMode,
  157. GraphicsOutputBlt,
  158. NULL // Mode
  159. },
  160. {
  161. 1, // MaxMode
  162. 0, // Mode
  163. NULL, // Info
  164. sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION), // SizeOfInfo
  165. 0, // FrameBufferBase
  166. 0 // FrameBufferSize
  167. },
  168. NULL, // DevicePath
  169. NULL, // PciIo
  170. 0, // PciAttributes
  171. NULL, // FrameBufferBltLibConfigure
  172. 0 // FrameBufferBltLibConfigureSize
  173. };
  174. /**
  175. Test whether the Controller can be managed by the driver.
  176. @param This Driver Binding protocol instance pointer.
  177. @param Controller The PCI controller.
  178. @param RemainingDevicePath Optional parameter use to pick a specific child
  179. device to start.
  180. @retval EFI_SUCCESS The driver can manage the video device.
  181. @retval other The driver cannot manage the video device.
  182. **/
  183. EFI_STATUS
  184. EFIAPI
  185. GraphicsOutputDriverBindingSupported (
  186. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  187. IN EFI_HANDLE Controller,
  188. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  189. )
  190. {
  191. EFI_STATUS Status;
  192. EFI_PCI_IO_PROTOCOL *PciIo;
  193. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  194. //
  195. // Since there is only one GraphicsInfo HOB, the driver only manages one video device.
  196. //
  197. if (mDriverStarted) {
  198. return EFI_ALREADY_STARTED;
  199. }
  200. //
  201. // Test the PCI I/O Protocol
  202. //
  203. Status = gBS->OpenProtocol (
  204. Controller,
  205. &gEfiPciIoProtocolGuid,
  206. (VOID **)&PciIo,
  207. This->DriverBindingHandle,
  208. Controller,
  209. EFI_OPEN_PROTOCOL_BY_DRIVER
  210. );
  211. if (Status == EFI_ALREADY_STARTED) {
  212. Status = EFI_SUCCESS;
  213. }
  214. if (EFI_ERROR (Status)) {
  215. return Status;
  216. }
  217. gBS->CloseProtocol (
  218. Controller,
  219. &gEfiPciIoProtocolGuid,
  220. This->DriverBindingHandle,
  221. Controller
  222. );
  223. //
  224. // Test the DevicePath protocol
  225. //
  226. Status = gBS->OpenProtocol (
  227. Controller,
  228. &gEfiDevicePathProtocolGuid,
  229. (VOID **)&DevicePath,
  230. This->DriverBindingHandle,
  231. Controller,
  232. EFI_OPEN_PROTOCOL_BY_DRIVER
  233. );
  234. if (Status == EFI_ALREADY_STARTED) {
  235. Status = EFI_SUCCESS;
  236. }
  237. if (EFI_ERROR (Status)) {
  238. return Status;
  239. }
  240. gBS->CloseProtocol (
  241. Controller,
  242. &gEfiDevicePathProtocolGuid,
  243. This->DriverBindingHandle,
  244. Controller
  245. );
  246. if ((RemainingDevicePath == NULL) ||
  247. IsDevicePathEnd (RemainingDevicePath) ||
  248. (CompareMem (RemainingDevicePath, &mGraphicsOutputAdrNode, sizeof (mGraphicsOutputAdrNode)) == 0))
  249. {
  250. return EFI_SUCCESS;
  251. } else {
  252. return EFI_INVALID_PARAMETER;
  253. }
  254. }
  255. /**
  256. Start the video controller.
  257. @param This Driver Binding protocol instance pointer.
  258. @param ControllerHandle The PCI controller.
  259. @param RemainingDevicePath Optional parameter use to pick a specific child
  260. device to start.
  261. @retval EFI_SUCCESS The driver starts to manage the video device.
  262. @retval other The driver cannot manage the video device.
  263. **/
  264. EFI_STATUS
  265. EFIAPI
  266. GraphicsOutputDriverBindingStart (
  267. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  268. IN EFI_HANDLE Controller,
  269. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  270. )
  271. {
  272. EFI_STATUS Status;
  273. RETURN_STATUS ReturnStatus;
  274. GRAPHICS_OUTPUT_PRIVATE_DATA *Private;
  275. EFI_PCI_IO_PROTOCOL *PciIo;
  276. EFI_DEVICE_PATH *PciDevicePath;
  277. PCI_TYPE00 Pci;
  278. UINT8 Index;
  279. EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Resources;
  280. VOID *HobStart;
  281. EFI_PEI_GRAPHICS_INFO_HOB *GraphicsInfo;
  282. EFI_PEI_GRAPHICS_DEVICE_INFO_HOB *DeviceInfo;
  283. EFI_PHYSICAL_ADDRESS FrameBufferBase;
  284. FrameBufferBase = 0;
  285. HobStart = GetFirstGuidHob (&gEfiGraphicsInfoHobGuid);
  286. ASSERT ((HobStart != NULL) && (GET_GUID_HOB_DATA_SIZE (HobStart) == sizeof (EFI_PEI_GRAPHICS_INFO_HOB)));
  287. GraphicsInfo = (EFI_PEI_GRAPHICS_INFO_HOB *)(GET_GUID_HOB_DATA (HobStart));
  288. HobStart = GetFirstGuidHob (&gEfiGraphicsDeviceInfoHobGuid);
  289. if ((HobStart == NULL) || (GET_GUID_HOB_DATA_SIZE (HobStart) < sizeof (*DeviceInfo))) {
  290. //
  291. // Use default device infomation when the device info HOB doesn't exist
  292. //
  293. DeviceInfo = &mDefaultGraphicsDeviceInfo;
  294. DEBUG ((DEBUG_INFO, "[%a]: GraphicsDeviceInfo HOB doesn't exist!\n", gEfiCallerBaseName));
  295. } else {
  296. DeviceInfo = (EFI_PEI_GRAPHICS_DEVICE_INFO_HOB *)(GET_GUID_HOB_DATA (HobStart));
  297. DEBUG ((
  298. DEBUG_INFO,
  299. "[%a]: GraphicsDeviceInfo HOB:\n"
  300. " VendorId = %04x, DeviceId = %04x,\n"
  301. " RevisionId = %02x, BarIndex = %x,\n"
  302. " SubsystemVendorId = %04x, SubsystemId = %04x\n",
  303. gEfiCallerBaseName,
  304. DeviceInfo->VendorId,
  305. DeviceInfo->DeviceId,
  306. DeviceInfo->RevisionId,
  307. DeviceInfo->BarIndex,
  308. DeviceInfo->SubsystemVendorId,
  309. DeviceInfo->SubsystemId
  310. ));
  311. }
  312. //
  313. // Open the PCI I/O Protocol
  314. //
  315. Status = gBS->OpenProtocol (
  316. Controller,
  317. &gEfiPciIoProtocolGuid,
  318. (VOID **)&PciIo,
  319. This->DriverBindingHandle,
  320. Controller,
  321. EFI_OPEN_PROTOCOL_BY_DRIVER
  322. );
  323. if (Status == EFI_ALREADY_STARTED) {
  324. Status = EFI_SUCCESS;
  325. }
  326. ASSERT_EFI_ERROR (Status);
  327. Status = gBS->OpenProtocol (
  328. Controller,
  329. &gEfiDevicePathProtocolGuid,
  330. (VOID **)&PciDevicePath,
  331. This->DriverBindingHandle,
  332. Controller,
  333. EFI_OPEN_PROTOCOL_BY_DRIVER
  334. );
  335. if (Status == EFI_ALREADY_STARTED) {
  336. Status = EFI_SUCCESS;
  337. }
  338. ASSERT_EFI_ERROR (Status);
  339. //
  340. // Read the PCI Class Code from the PCI Device
  341. //
  342. Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0, sizeof (Pci), &Pci);
  343. if (!EFI_ERROR (Status)) {
  344. if (!IS_PCI_DISPLAY (&Pci) || (
  345. ((DeviceInfo->VendorId != MAX_UINT16) && (DeviceInfo->VendorId != Pci.Hdr.VendorId)) ||
  346. ((DeviceInfo->DeviceId != MAX_UINT16) && (DeviceInfo->DeviceId != Pci.Hdr.DeviceId)) ||
  347. ((DeviceInfo->RevisionId != MAX_UINT8) && (DeviceInfo->RevisionId != Pci.Hdr.RevisionID)) ||
  348. ((DeviceInfo->SubsystemVendorId != MAX_UINT16) && (DeviceInfo->SubsystemVendorId != Pci.Device.SubsystemVendorID)) ||
  349. ((DeviceInfo->SubsystemId != MAX_UINT16) && (DeviceInfo->SubsystemId != Pci.Device.SubsystemID))
  350. )
  351. )
  352. {
  353. //
  354. // It's not a video device, or device infomation doesn't match.
  355. //
  356. Status = EFI_UNSUPPORTED;
  357. } else {
  358. //
  359. // If it's a video device and device information matches, use the BarIndex
  360. // from device information, or any BAR if BarIndex is not specified
  361. // whose size >= the frame buffer size from GraphicsInfo HOB.
  362. // Store the new frame buffer base.
  363. //
  364. for (Index = 0; Index < MAX_PCI_BAR; Index++) {
  365. if ((DeviceInfo->BarIndex != MAX_UINT8) && (DeviceInfo->BarIndex != Index)) {
  366. continue;
  367. }
  368. Status = PciIo->GetBarAttributes (PciIo, Index, NULL, (VOID **)&Resources);
  369. if (!EFI_ERROR (Status)) {
  370. DEBUG ((
  371. DEBUG_INFO,
  372. "[%a]: BAR[%d]: Base = %lx, Length = %lx\n",
  373. gEfiCallerBaseName,
  374. Index,
  375. Resources->AddrRangeMin,
  376. Resources->AddrLen
  377. ));
  378. if ((Resources->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR) &&
  379. (Resources->Len == (UINT16)(sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3)) &&
  380. (Resources->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) &&
  381. (Resources->AddrLen >= GraphicsInfo->FrameBufferSize)
  382. )
  383. {
  384. if (FrameBufferBase == 0) {
  385. FrameBufferBase = Resources->AddrRangeMin;
  386. }
  387. if (DeviceInfo->BarIndex == MAX_UINT8) {
  388. if (Resources->AddrRangeMin == GraphicsInfo->FrameBufferBase) {
  389. FrameBufferBase = Resources->AddrRangeMin;
  390. break;
  391. }
  392. } else {
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. if (Index == MAX_PCI_BAR) {
  399. Status = EFI_UNSUPPORTED;
  400. } else {
  401. DEBUG ((DEBUG_INFO, "[%a]: ... matched!\n", gEfiCallerBaseName));
  402. }
  403. }
  404. }
  405. if (EFI_ERROR (Status)) {
  406. goto CloseProtocols;
  407. }
  408. if ((RemainingDevicePath != NULL) && IsDevicePathEnd (RemainingDevicePath)) {
  409. return EFI_SUCCESS;
  410. }
  411. Private = AllocateCopyPool (sizeof (mGraphicsOutputInstanceTemplate), &mGraphicsOutputInstanceTemplate);
  412. if (Private == NULL) {
  413. Status = EFI_OUT_OF_RESOURCES;
  414. goto CloseProtocols;
  415. }
  416. Private->GraphicsOutputMode.FrameBufferBase = FrameBufferBase;
  417. Private->GraphicsOutputMode.FrameBufferSize = GraphicsInfo->FrameBufferSize;
  418. Private->GraphicsOutputMode.Info = &GraphicsInfo->GraphicsMode;
  419. //
  420. // Fix up Mode pointer in GraphicsOutput
  421. //
  422. Private->GraphicsOutput.Mode = &Private->GraphicsOutputMode;
  423. //
  424. // Set attributes
  425. //
  426. Status = PciIo->Attributes (
  427. PciIo,
  428. EfiPciIoAttributeOperationGet,
  429. 0,
  430. &Private->PciAttributes
  431. );
  432. if (!EFI_ERROR (Status)) {
  433. Status = PciIo->Attributes (
  434. PciIo,
  435. EfiPciIoAttributeOperationEnable,
  436. EFI_PCI_DEVICE_ENABLE,
  437. NULL
  438. );
  439. }
  440. if (EFI_ERROR (Status)) {
  441. goto FreeMemory;
  442. }
  443. //
  444. // Create the FrameBufferBltLib configuration.
  445. //
  446. ReturnStatus = FrameBufferBltConfigure (
  447. (VOID *)(UINTN)Private->GraphicsOutput.Mode->FrameBufferBase,
  448. Private->GraphicsOutput.Mode->Info,
  449. Private->FrameBufferBltLibConfigure,
  450. &Private->FrameBufferBltLibConfigureSize
  451. );
  452. if (ReturnStatus == RETURN_BUFFER_TOO_SMALL) {
  453. Private->FrameBufferBltLibConfigure = AllocatePool (Private->FrameBufferBltLibConfigureSize);
  454. if (Private->FrameBufferBltLibConfigure != NULL) {
  455. ReturnStatus = FrameBufferBltConfigure (
  456. (VOID *)(UINTN)Private->GraphicsOutput.Mode->FrameBufferBase,
  457. Private->GraphicsOutput.Mode->Info,
  458. Private->FrameBufferBltLibConfigure,
  459. &Private->FrameBufferBltLibConfigureSize
  460. );
  461. }
  462. }
  463. if (RETURN_ERROR (ReturnStatus)) {
  464. Status = EFI_OUT_OF_RESOURCES;
  465. goto RestorePciAttributes;
  466. }
  467. Private->DevicePath = AppendDevicePathNode (PciDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&mGraphicsOutputAdrNode);
  468. if (Private->DevicePath == NULL) {
  469. Status = EFI_OUT_OF_RESOURCES;
  470. goto RestorePciAttributes;
  471. }
  472. Status = gBS->InstallMultipleProtocolInterfaces (
  473. &Private->GraphicsOutputHandle,
  474. &gEfiGraphicsOutputProtocolGuid,
  475. &Private->GraphicsOutput,
  476. &gEfiDevicePathProtocolGuid,
  477. Private->DevicePath,
  478. NULL
  479. );
  480. if (!EFI_ERROR (Status)) {
  481. Status = gBS->OpenProtocol (
  482. Controller,
  483. &gEfiPciIoProtocolGuid,
  484. (VOID **)&Private->PciIo,
  485. This->DriverBindingHandle,
  486. Private->GraphicsOutputHandle,
  487. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  488. );
  489. if (!EFI_ERROR (Status)) {
  490. mDriverStarted = TRUE;
  491. } else {
  492. gBS->UninstallMultipleProtocolInterfaces (
  493. Private->GraphicsOutputHandle,
  494. &gEfiGraphicsOutputProtocolGuid,
  495. &Private->GraphicsOutput,
  496. &gEfiDevicePathProtocolGuid,
  497. Private->DevicePath,
  498. NULL
  499. );
  500. }
  501. }
  502. RestorePciAttributes:
  503. if (EFI_ERROR (Status)) {
  504. //
  505. // Restore original PCI attributes
  506. //
  507. PciIo->Attributes (
  508. PciIo,
  509. EfiPciIoAttributeOperationSet,
  510. Private->PciAttributes,
  511. NULL
  512. );
  513. }
  514. FreeMemory:
  515. if (EFI_ERROR (Status)) {
  516. if (Private != NULL) {
  517. if (Private->DevicePath != NULL) {
  518. FreePool (Private->DevicePath);
  519. }
  520. if (Private->FrameBufferBltLibConfigure != NULL) {
  521. FreePool (Private->FrameBufferBltLibConfigure);
  522. }
  523. FreePool (Private);
  524. }
  525. }
  526. CloseProtocols:
  527. if (EFI_ERROR (Status)) {
  528. //
  529. // Close the PCI I/O Protocol
  530. //
  531. gBS->CloseProtocol (
  532. Controller,
  533. &gEfiDevicePathProtocolGuid,
  534. This->DriverBindingHandle,
  535. Controller
  536. );
  537. //
  538. // Close the PCI I/O Protocol
  539. //
  540. gBS->CloseProtocol (
  541. Controller,
  542. &gEfiPciIoProtocolGuid,
  543. This->DriverBindingHandle,
  544. Controller
  545. );
  546. }
  547. return Status;
  548. }
  549. /**
  550. Stop the video controller.
  551. @param This Driver Binding protocol instance pointer.
  552. @param Controller The PCI controller.
  553. @param NumberOfChildren The number of child device handles in ChildHandleBuffer.
  554. @param ChildHandleBuffer An array of child handles to be freed. May be NULL
  555. if NumberOfChildren is 0.
  556. @retval EFI_SUCCESS The device was stopped.
  557. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  558. **/
  559. EFI_STATUS
  560. EFIAPI
  561. GraphicsOutputDriverBindingStop (
  562. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  563. IN EFI_HANDLE Controller,
  564. IN UINTN NumberOfChildren,
  565. IN EFI_HANDLE *ChildHandleBuffer
  566. )
  567. {
  568. EFI_STATUS Status;
  569. EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
  570. GRAPHICS_OUTPUT_PRIVATE_DATA *Private;
  571. if (NumberOfChildren == 0) {
  572. //
  573. // Close the PCI I/O Protocol
  574. //
  575. Status = gBS->CloseProtocol (
  576. Controller,
  577. &gEfiPciIoProtocolGuid,
  578. This->DriverBindingHandle,
  579. Controller
  580. );
  581. ASSERT_EFI_ERROR (Status);
  582. Status = gBS->CloseProtocol (
  583. Controller,
  584. &gEfiDevicePathProtocolGuid,
  585. This->DriverBindingHandle,
  586. Controller
  587. );
  588. ASSERT_EFI_ERROR (Status);
  589. return EFI_SUCCESS;
  590. }
  591. ASSERT (NumberOfChildren == 1);
  592. Status = gBS->OpenProtocol (
  593. ChildHandleBuffer[0],
  594. &gEfiGraphicsOutputProtocolGuid,
  595. (VOID **)&Gop,
  596. This->DriverBindingHandle,
  597. ChildHandleBuffer[0],
  598. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  599. );
  600. if (EFI_ERROR (Status)) {
  601. return Status;
  602. }
  603. Private = GRAPHICS_OUTPUT_PRIVATE_FROM_THIS (Gop);
  604. Status = gBS->CloseProtocol (
  605. Controller,
  606. &gEfiPciIoProtocolGuid,
  607. This->DriverBindingHandle,
  608. Private->GraphicsOutputHandle
  609. );
  610. ASSERT_EFI_ERROR (Status);
  611. //
  612. // Remove the GOP protocol interface from the system
  613. //
  614. Status = gBS->UninstallMultipleProtocolInterfaces (
  615. Private->GraphicsOutputHandle,
  616. &gEfiGraphicsOutputProtocolGuid,
  617. &Private->GraphicsOutput,
  618. &gEfiDevicePathProtocolGuid,
  619. Private->DevicePath,
  620. NULL
  621. );
  622. if (!EFI_ERROR (Status)) {
  623. //
  624. // Restore original PCI attributes
  625. //
  626. Status = Private->PciIo->Attributes (
  627. Private->PciIo,
  628. EfiPciIoAttributeOperationSet,
  629. Private->PciAttributes,
  630. NULL
  631. );
  632. ASSERT_EFI_ERROR (Status);
  633. FreePool (Private->DevicePath);
  634. FreePool (Private->FrameBufferBltLibConfigure);
  635. mDriverStarted = FALSE;
  636. } else {
  637. Status = gBS->OpenProtocol (
  638. Controller,
  639. &gEfiPciIoProtocolGuid,
  640. (VOID **)&Private->PciIo,
  641. This->DriverBindingHandle,
  642. Private->GraphicsOutputHandle,
  643. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  644. );
  645. ASSERT_EFI_ERROR (Status);
  646. }
  647. return Status;
  648. }
  649. EFI_DRIVER_BINDING_PROTOCOL mGraphicsOutputDriverBinding = {
  650. GraphicsOutputDriverBindingSupported,
  651. GraphicsOutputDriverBindingStart,
  652. GraphicsOutputDriverBindingStop,
  653. 0x10,
  654. NULL,
  655. NULL
  656. };
  657. /**
  658. The Entry Point for GraphicsOutput driver.
  659. It installs DriverBinding, ComponentName and ComponentName2 protocol if there is
  660. GraphicsInfo HOB passed from Graphics PEIM.
  661. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  662. @param[in] SystemTable A pointer to the EFI System Table.
  663. @retval EFI_SUCCESS The entry point is executed successfully.
  664. @retval other Some error occurs when executing this entry point.
  665. **/
  666. EFI_STATUS
  667. EFIAPI
  668. InitializeGraphicsOutput (
  669. IN EFI_HANDLE ImageHandle,
  670. IN EFI_SYSTEM_TABLE *SystemTable
  671. )
  672. {
  673. EFI_STATUS Status;
  674. VOID *HobStart;
  675. HobStart = GetFirstGuidHob (&gEfiGraphicsInfoHobGuid);
  676. if ((HobStart == NULL) || (GET_GUID_HOB_DATA_SIZE (HobStart) < sizeof (EFI_PEI_GRAPHICS_INFO_HOB))) {
  677. return EFI_NOT_FOUND;
  678. }
  679. Status = EfiLibInstallDriverBindingComponentName2 (
  680. ImageHandle,
  681. SystemTable,
  682. &mGraphicsOutputDriverBinding,
  683. ImageHandle,
  684. &mGraphicsOutputComponentName,
  685. &mGraphicsOutputComponentName2
  686. );
  687. ASSERT_EFI_ERROR (Status);
  688. return Status;
  689. }