]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/EmuGopDxe/GopDriver.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 EFI_STATUS
13 FreeNotifyList (
14 IN OUT LIST_ENTRY *ListHead
15 )
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
38 while (!IsListEmpty (ListHead)) {
39 NotifyNode = CR (
40 ListHead->ForwardLink,
41 EMU_GOP_SIMPLE_TEXTIN_EX_NOTIFY,
42 NotifyEntry,
43 EMU_GOP_SIMPLE_TEXTIN_EX_NOTIFY_SIGNATURE
44 );
45 RemoveEntryList (ListHead->ForwardLink);
46 gBS->FreePool (NotifyNode);
47 }
48
49 return EFI_SUCCESS;
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 Starts a device controller or a bus controller.
137
138 The Start() function is designed to be invoked from the EFI boot service ConnectController().
139 As a result, much of the error checking on the parameters to Start() has been moved into this
140 common boot service. It is legal to call Start() from other locations,
141 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
142 1. ControllerHandle must be a valid EFI_HANDLE.
143 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
144 EFI_DEVICE_PATH_PROTOCOL.
145 3. Prior to calling Start(), the Supported() function for the driver specified by This must
146 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
147
148 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
149 @param[in] ControllerHandle The handle of the controller to start. This handle
150 must support a protocol interface that supplies
151 an I/O abstraction to the driver.
152 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
153 parameter is ignored by device drivers, and is optional for bus
154 drivers. For a bus driver, if this parameter is NULL, then handles
155 for all the children of Controller are created by this driver.
156 If this parameter is not NULL and the first Device Path Node is
157 not the End of Device Path Node, then only the handle for the
158 child device specified by the first Device Path Node of
159 RemainingDevicePath is created by this driver.
160 If the first Device Path Node of RemainingDevicePath is
161 the End of Device Path Node, no child handle is created by this
162 driver.
163
164 @retval EFI_SUCCESS The device was started.
165 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
166 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
167 @retval Others The driver failded to start the device.
168
169 **/
170 EFI_STATUS
171 EFIAPI
172 EmuGopDriverBindingStart (
173 IN EFI_DRIVER_BINDING_PROTOCOL *This,
174 IN EFI_HANDLE Handle,
175 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
176 )
177 {
178 EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
179 EFI_STATUS Status;
180 GOP_PRIVATE_DATA *Private;
181
182 //
183 // Grab the protocols we need
184 //
185 Status = gBS->OpenProtocol (
186 Handle,
187 &gEmuIoThunkProtocolGuid,
188 (VOID **)&EmuIoThunk,
189 This->DriverBindingHandle,
190 Handle,
191 EFI_OPEN_PROTOCOL_BY_DRIVER
192 );
193 if (EFI_ERROR (Status)) {
194 return EFI_UNSUPPORTED;
195 }
196
197 //
198 // Allocate Private context data for SGO inteface.
199 //
200 Private = NULL;
201 Status = gBS->AllocatePool (
202 EfiBootServicesData,
203 sizeof (GOP_PRIVATE_DATA),
204 (VOID **)&Private
205 );
206 if (EFI_ERROR (Status)) {
207 goto Done;
208 }
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 //
239 // Publish the Gop interface to the world
240 //
241 Status = gBS->InstallMultipleProtocolInterfaces (
242 &Private->Handle,
243 &gEfiGraphicsOutputProtocolGuid,
244 &Private->GraphicsOutput,
245 &gEfiSimpleTextInProtocolGuid,
246 &Private->SimpleTextIn,
247 &gEfiSimplePointerProtocolGuid,
248 &Private->SimplePointer,
249 &gEfiSimpleTextInputExProtocolGuid,
250 &Private->SimpleTextInEx,
251 NULL
252 );
253
254 Done:
255 if (EFI_ERROR (Status)) {
256 gBS->CloseProtocol (
257 Handle,
258 &gEmuIoThunkProtocolGuid,
259 This->DriverBindingHandle,
260 Handle
261 );
262
263 if (Private != NULL) {
264 //
265 // On Error Free back private data
266 //
267 if (Private->ControllerNameTable != NULL) {
268 FreeUnicodeStringTable (Private->ControllerNameTable);
269 }
270
271 if (Private->SimpleTextIn.WaitForKey != NULL) {
272 gBS->CloseEvent (Private->SimpleTextIn.WaitForKey);
273 }
274
275 if (Private->SimpleTextInEx.WaitForKeyEx != NULL) {
276 gBS->CloseEvent (Private->SimpleTextInEx.WaitForKeyEx);
277 }
278
279 FreeNotifyList (&Private->NotifyList);
280
281 gBS->FreePool (Private);
282 }
283 }
284
285 return Status;
286 }
287
288 /**
289 Stops a device controller or a bus controller.
290
291 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
292 As a result, much of the error checking on the parameters to Stop() has been moved
293 into this common boot service. It is legal to call Stop() from other locations,
294 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
295 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
296 same driver's Start() function.
297 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
298 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
299 Start() function, and the Start() function must have called OpenProtocol() on
300 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
301
302 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
303 @param[in] ControllerHandle A handle to the device being stopped. The handle must
304 support a bus specific I/O protocol for the driver
305 to use to stop the device.
306 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
307 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
308 if NumberOfChildren is 0.
309
310 @retval EFI_SUCCESS The device was stopped.
311 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
312
313 **/
314 EFI_STATUS
315 EFIAPI
316 EmuGopDriverBindingStop (
317 IN EFI_DRIVER_BINDING_PROTOCOL *This,
318 IN EFI_HANDLE Handle,
319 IN UINTN NumberOfChildren,
320 IN EFI_HANDLE *ChildHandleBuffer
321 )
322 {
323 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
324 EFI_STATUS Status;
325 GOP_PRIVATE_DATA *Private;
326
327 Status = gBS->OpenProtocol (
328 Handle,
329 &gEfiGraphicsOutputProtocolGuid,
330 (VOID **)&GraphicsOutput,
331 This->DriverBindingHandle,
332 Handle,
333 EFI_OPEN_PROTOCOL_GET_PROTOCOL
334 );
335 if (EFI_ERROR (Status)) {
336 //
337 // If the GOP interface does not exist the driver is not started
338 //
339 return EFI_NOT_STARTED;
340 }
341
342 //
343 // Get our private context information
344 //
345 Private = GOP_PRIVATE_DATA_FROM_THIS (GraphicsOutput);
346
347 //
348 // Remove the SGO interface from the system
349 //
350 Status = gBS->UninstallMultipleProtocolInterfaces (
351 Private->Handle,
352 &gEfiGraphicsOutputProtocolGuid,
353 &Private->GraphicsOutput,
354 &gEfiSimpleTextInProtocolGuid,
355 &Private->SimpleTextIn,
356 &gEfiSimplePointerProtocolGuid,
357 &Private->SimplePointer,
358 &gEfiSimpleTextInputExProtocolGuid,
359 &Private->SimpleTextInEx,
360 NULL
361 );
362 if (!EFI_ERROR (Status)) {
363 //
364 // Shutdown the hardware
365 //
366 Status = EmuGopDestructor (Private);
367 if (EFI_ERROR (Status)) {
368 return EFI_DEVICE_ERROR;
369 }
370
371 gBS->CloseProtocol (
372 Handle,
373 &gEmuIoThunkProtocolGuid,
374 This->DriverBindingHandle,
375 Handle
376 );
377
378 //
379 // Free our instance data
380 //
381 FreeUnicodeStringTable (Private->ControllerNameTable);
382
383 Status = gBS->CloseEvent (Private->SimpleTextIn.WaitForKey);
384 ASSERT_EFI_ERROR (Status);
385
386 Status = gBS->CloseEvent (Private->SimpleTextInEx.WaitForKeyEx);
387 ASSERT_EFI_ERROR (Status);
388
389 FreeNotifyList (&Private->NotifyList);
390
391 gBS->FreePool (Private);
392 }
393
394 return Status;
395 }
396
397 ///
398 /// This protocol provides the services required to determine if a driver supports a given controller.
399 /// If a controller is supported, then it also provides routines to start and stop the controller.
400 ///
401 EFI_DRIVER_BINDING_PROTOCOL gEmuGopDriverBinding = {
402 EmuGopDriverBindingSupported,
403 EmuGopDriverBindingStart,
404 EmuGopDriverBindingStop,
405 0xa,
406 NULL,
407 NULL
408 };
409
410 /**
411 The user Entry Point for module EmuGop. The user code starts with this function.
412
413 @param[in] ImageHandle The firmware allocated handle for the EFI image.
414 @param[in] SystemTable A pointer to the EFI System Table.
415
416 @retval EFI_SUCCESS The entry point is executed successfully.
417 @retval other Some error occurs when executing this entry point.
418
419 **/
420 EFI_STATUS
421 EFIAPI
422 InitializeEmuGop (
423 IN EFI_HANDLE ImageHandle,
424 IN EFI_SYSTEM_TABLE *SystemTable
425 )
426 {
427 EFI_STATUS Status;
428
429 Status = EfiLibInstallDriverBindingComponentName2 (
430 ImageHandle,
431 SystemTable,
432 &gEmuGopDriverBinding,
433 ImageHandle,
434 &gEmuGopComponentName,
435 &gEmuGopComponentName2
436 );
437 ASSERT_EFI_ERROR (Status);
438
439 return Status;
440 }