GopDriver.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*++ @file
  2. Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2010,Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Gop.h"
  7. EFI_STATUS
  8. FreeNotifyList (
  9. IN OUT LIST_ENTRY *ListHead
  10. )
  11. /*++
  12. Routine Description:
  13. Arguments:
  14. ListHead - The list head
  15. Returns:
  16. EFI_SUCCESS - Free the notify list successfully
  17. EFI_INVALID_PARAMETER - ListHead is invalid.
  18. **/
  19. {
  20. EMU_GOP_SIMPLE_TEXTIN_EX_NOTIFY *NotifyNode;
  21. if (ListHead == NULL) {
  22. return EFI_INVALID_PARAMETER;
  23. }
  24. while (!IsListEmpty (ListHead)) {
  25. NotifyNode = CR (
  26. ListHead->ForwardLink,
  27. EMU_GOP_SIMPLE_TEXTIN_EX_NOTIFY,
  28. NotifyEntry,
  29. EMU_GOP_SIMPLE_TEXTIN_EX_NOTIFY_SIGNATURE
  30. );
  31. RemoveEntryList (ListHead->ForwardLink);
  32. gBS->FreePool (NotifyNode);
  33. }
  34. return EFI_SUCCESS;
  35. }
  36. /**
  37. Tests to see if this driver supports a given controller. If a child device is provided,
  38. it further tests to see if this driver supports creating a handle for the specified child device.
  39. This function checks to see if the driver specified by This supports the device specified by
  40. ControllerHandle. Drivers will typically use the device path attached to
  41. ControllerHandle and/or the services from the bus I/O abstraction attached to
  42. ControllerHandle to determine if the driver supports ControllerHandle. This function
  43. may be called many times during platform initialization. In order to reduce boot times, the tests
  44. performed by this function must be very small, and take as little time as possible to execute. This
  45. function must not change the state of any hardware devices, and this function must be aware that the
  46. device specified by ControllerHandle may already be managed by the same driver or a
  47. different driver. This function must match its calls to AllocatePages() with FreePages(),
  48. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  49. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  50. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  51. to guarantee the state of ControllerHandle is not modified by this function.
  52. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  53. @param[in] ControllerHandle The handle of the controller to test. This handle
  54. must support a protocol interface that supplies
  55. an I/O abstraction to the driver.
  56. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  57. parameter is ignored by device drivers, and is optional for bus
  58. drivers. For bus drivers, if this parameter is not NULL, then
  59. the bus driver must determine if the bus controller specified
  60. by ControllerHandle and the child controller specified
  61. by RemainingDevicePath are both supported by this
  62. bus driver.
  63. @retval EFI_SUCCESS The device specified by ControllerHandle and
  64. RemainingDevicePath is supported by the driver specified by This.
  65. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  66. RemainingDevicePath is already being managed by the driver
  67. specified by This.
  68. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  69. RemainingDevicePath is already being managed by a different
  70. driver or an application that requires exclusive access.
  71. Currently not implemented.
  72. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  73. RemainingDevicePath is not supported by the driver specified by This.
  74. **/
  75. EFI_STATUS
  76. EFIAPI
  77. EmuGopDriverBindingSupported (
  78. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  79. IN EFI_HANDLE Handle,
  80. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  81. )
  82. {
  83. EFI_STATUS Status;
  84. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  85. //
  86. // Open the IO Abstraction(s) needed to perform the supported test
  87. //
  88. Status = gBS->OpenProtocol (
  89. Handle,
  90. &gEmuIoThunkProtocolGuid,
  91. (VOID **)&EmuIoThunk,
  92. This->DriverBindingHandle,
  93. Handle,
  94. EFI_OPEN_PROTOCOL_BY_DRIVER
  95. );
  96. if (EFI_ERROR (Status)) {
  97. return Status;
  98. }
  99. Status = EmuGopSupported (EmuIoThunk);
  100. //
  101. // Close the I/O Abstraction(s) used to perform the supported test
  102. //
  103. gBS->CloseProtocol (
  104. Handle,
  105. &gEmuIoThunkProtocolGuid,
  106. This->DriverBindingHandle,
  107. Handle
  108. );
  109. return Status;
  110. }
  111. /**
  112. Starts a device controller or a bus controller.
  113. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  114. As a result, much of the error checking on the parameters to Start() has been moved into this
  115. common boot service. It is legal to call Start() from other locations,
  116. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  117. 1. ControllerHandle must be a valid EFI_HANDLE.
  118. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  119. EFI_DEVICE_PATH_PROTOCOL.
  120. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  121. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  122. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  123. @param[in] ControllerHandle The handle of the controller to start. This handle
  124. must support a protocol interface that supplies
  125. an I/O abstraction to the driver.
  126. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  127. parameter is ignored by device drivers, and is optional for bus
  128. drivers. For a bus driver, if this parameter is NULL, then handles
  129. for all the children of Controller are created by this driver.
  130. If this parameter is not NULL and the first Device Path Node is
  131. not the End of Device Path Node, then only the handle for the
  132. child device specified by the first Device Path Node of
  133. RemainingDevicePath is created by this driver.
  134. If the first Device Path Node of RemainingDevicePath is
  135. the End of Device Path Node, no child handle is created by this
  136. driver.
  137. @retval EFI_SUCCESS The device was started.
  138. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  139. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  140. @retval Others The driver failded to start the device.
  141. **/
  142. EFI_STATUS
  143. EFIAPI
  144. EmuGopDriverBindingStart (
  145. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  146. IN EFI_HANDLE Handle,
  147. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  148. )
  149. {
  150. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  151. EFI_STATUS Status;
  152. GOP_PRIVATE_DATA *Private;
  153. //
  154. // Grab the protocols we need
  155. //
  156. Status = gBS->OpenProtocol (
  157. Handle,
  158. &gEmuIoThunkProtocolGuid,
  159. (VOID **)&EmuIoThunk,
  160. This->DriverBindingHandle,
  161. Handle,
  162. EFI_OPEN_PROTOCOL_BY_DRIVER
  163. );
  164. if (EFI_ERROR (Status)) {
  165. return EFI_UNSUPPORTED;
  166. }
  167. //
  168. // Allocate Private context data for SGO inteface.
  169. //
  170. Private = NULL;
  171. Status = gBS->AllocatePool (
  172. EfiBootServicesData,
  173. sizeof (GOP_PRIVATE_DATA),
  174. (VOID **)&Private
  175. );
  176. if (EFI_ERROR (Status)) {
  177. goto Done;
  178. }
  179. //
  180. // Set up context record
  181. //
  182. Private->Signature = GOP_PRIVATE_DATA_SIGNATURE;
  183. Private->Handle = Handle;
  184. Private->EmuIoThunk = EmuIoThunk;
  185. Private->WindowName = EmuIoThunk->ConfigString;
  186. Private->ControllerNameTable = NULL;
  187. AddUnicodeString (
  188. "eng",
  189. gEmuGopComponentName.SupportedLanguages,
  190. &Private->ControllerNameTable,
  191. EmuIoThunk->ConfigString
  192. );
  193. AddUnicodeString2 (
  194. "en",
  195. gEmuGopComponentName2.SupportedLanguages,
  196. &Private->ControllerNameTable,
  197. EmuIoThunk->ConfigString,
  198. FALSE
  199. );
  200. Status = EmuGopConstructor (Private);
  201. if (EFI_ERROR (Status)) {
  202. goto Done;
  203. }
  204. //
  205. // Publish the Gop interface to the world
  206. //
  207. Status = gBS->InstallMultipleProtocolInterfaces (
  208. &Private->Handle,
  209. &gEfiGraphicsOutputProtocolGuid,
  210. &Private->GraphicsOutput,
  211. &gEfiSimpleTextInProtocolGuid,
  212. &Private->SimpleTextIn,
  213. &gEfiSimplePointerProtocolGuid,
  214. &Private->SimplePointer,
  215. &gEfiSimpleTextInputExProtocolGuid,
  216. &Private->SimpleTextInEx,
  217. NULL
  218. );
  219. Done:
  220. if (EFI_ERROR (Status)) {
  221. gBS->CloseProtocol (
  222. Handle,
  223. &gEmuIoThunkProtocolGuid,
  224. This->DriverBindingHandle,
  225. Handle
  226. );
  227. if (Private != NULL) {
  228. //
  229. // On Error Free back private data
  230. //
  231. if (Private->ControllerNameTable != NULL) {
  232. FreeUnicodeStringTable (Private->ControllerNameTable);
  233. }
  234. if (Private->SimpleTextIn.WaitForKey != NULL) {
  235. gBS->CloseEvent (Private->SimpleTextIn.WaitForKey);
  236. }
  237. if (Private->SimpleTextInEx.WaitForKeyEx != NULL) {
  238. gBS->CloseEvent (Private->SimpleTextInEx.WaitForKeyEx);
  239. }
  240. FreeNotifyList (&Private->NotifyList);
  241. gBS->FreePool (Private);
  242. }
  243. }
  244. return Status;
  245. }
  246. /**
  247. Stops a device controller or a bus controller.
  248. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  249. As a result, much of the error checking on the parameters to Stop() has been moved
  250. into this common boot service. It is legal to call Stop() from other locations,
  251. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  252. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  253. same driver's Start() function.
  254. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  255. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  256. Start() function, and the Start() function must have called OpenProtocol() on
  257. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  258. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  259. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  260. support a bus specific I/O protocol for the driver
  261. to use to stop the device.
  262. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  263. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  264. if NumberOfChildren is 0.
  265. @retval EFI_SUCCESS The device was stopped.
  266. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  267. **/
  268. EFI_STATUS
  269. EFIAPI
  270. EmuGopDriverBindingStop (
  271. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  272. IN EFI_HANDLE Handle,
  273. IN UINTN NumberOfChildren,
  274. IN EFI_HANDLE *ChildHandleBuffer
  275. )
  276. {
  277. EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
  278. EFI_STATUS Status;
  279. GOP_PRIVATE_DATA *Private;
  280. Status = gBS->OpenProtocol (
  281. Handle,
  282. &gEfiGraphicsOutputProtocolGuid,
  283. (VOID **)&GraphicsOutput,
  284. This->DriverBindingHandle,
  285. Handle,
  286. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  287. );
  288. if (EFI_ERROR (Status)) {
  289. //
  290. // If the GOP interface does not exist the driver is not started
  291. //
  292. return EFI_NOT_STARTED;
  293. }
  294. //
  295. // Get our private context information
  296. //
  297. Private = GOP_PRIVATE_DATA_FROM_THIS (GraphicsOutput);
  298. //
  299. // Remove the SGO interface from the system
  300. //
  301. Status = gBS->UninstallMultipleProtocolInterfaces (
  302. Private->Handle,
  303. &gEfiGraphicsOutputProtocolGuid,
  304. &Private->GraphicsOutput,
  305. &gEfiSimpleTextInProtocolGuid,
  306. &Private->SimpleTextIn,
  307. &gEfiSimplePointerProtocolGuid,
  308. &Private->SimplePointer,
  309. &gEfiSimpleTextInputExProtocolGuid,
  310. &Private->SimpleTextInEx,
  311. NULL
  312. );
  313. if (!EFI_ERROR (Status)) {
  314. //
  315. // Shutdown the hardware
  316. //
  317. Status = EmuGopDestructor (Private);
  318. if (EFI_ERROR (Status)) {
  319. return EFI_DEVICE_ERROR;
  320. }
  321. gBS->CloseProtocol (
  322. Handle,
  323. &gEmuIoThunkProtocolGuid,
  324. This->DriverBindingHandle,
  325. Handle
  326. );
  327. //
  328. // Free our instance data
  329. //
  330. FreeUnicodeStringTable (Private->ControllerNameTable);
  331. Status = gBS->CloseEvent (Private->SimpleTextIn.WaitForKey);
  332. ASSERT_EFI_ERROR (Status);
  333. Status = gBS->CloseEvent (Private->SimpleTextInEx.WaitForKeyEx);
  334. ASSERT_EFI_ERROR (Status);
  335. FreeNotifyList (&Private->NotifyList);
  336. gBS->FreePool (Private);
  337. }
  338. return Status;
  339. }
  340. ///
  341. /// This protocol provides the services required to determine if a driver supports a given controller.
  342. /// If a controller is supported, then it also provides routines to start and stop the controller.
  343. ///
  344. EFI_DRIVER_BINDING_PROTOCOL gEmuGopDriverBinding = {
  345. EmuGopDriverBindingSupported,
  346. EmuGopDriverBindingStart,
  347. EmuGopDriverBindingStop,
  348. 0xa,
  349. NULL,
  350. NULL
  351. };
  352. /**
  353. The user Entry Point for module EmuGop. The user code starts with this function.
  354. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  355. @param[in] SystemTable A pointer to the EFI System Table.
  356. @retval EFI_SUCCESS The entry point is executed successfully.
  357. @retval other Some error occurs when executing this entry point.
  358. **/
  359. EFI_STATUS
  360. EFIAPI
  361. InitializeEmuGop (
  362. IN EFI_HANDLE ImageHandle,
  363. IN EFI_SYSTEM_TABLE *SystemTable
  364. )
  365. {
  366. EFI_STATUS Status;
  367. Status = EfiLibInstallDriverBindingComponentName2 (
  368. ImageHandle,
  369. SystemTable,
  370. &gEmuGopDriverBinding,
  371. ImageHandle,
  372. &gEmuGopComponentName,
  373. &gEmuGopComponentName2
  374. );
  375. ASSERT_EFI_ERROR (Status);
  376. return Status;
  377. }