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