]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
MdeModulePkg/ConPlatform: Support GOP created as PCI's grandson
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
CommitLineData
fb0b259e 1/** @file\r
dfe687ca 2 Console Platform DXE Driver, install Console Device Guids and update Console\r
9359e53f 3 Environment Variables.\r
95276127 4\r
56dee83a 5Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 6This program and the accompanying materials\r
95276127 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
fb0b259e 14**/\r
95276127 15\r
0ad02bbd 16#include "ConPlatform.h"\r
95276127 17\r
95276127 18\r
19EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {\r
20 ConPlatformTextInDriverBindingSupported,\r
21 ConPlatformTextInDriverBindingStart,\r
22 ConPlatformTextInDriverBindingStop,\r
23 0xa,\r
24 NULL,\r
25 NULL\r
26};\r
27\r
28EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {\r
29 ConPlatformTextOutDriverBindingSupported,\r
30 ConPlatformTextOutDriverBindingStart,\r
31 ConPlatformTextOutDriverBindingStop,\r
32 0xa,\r
33 NULL,\r
34 NULL\r
35};\r
36\r
37/**\r
c5ed97f6 38 Entrypoint of this module.\r
39\r
40 This function is the entrypoint of this module. It installs Driver Binding\r
41 Protocols together with Component Name Protocols.\r
95276127 42\r
7b9ff698 43 @param ImageHandle The firmware allocated handle for the EFI image.\r
44 @param SystemTable A pointer to the EFI System Table.\r
c62a593c 45\r
95276127 46 @retval EFI_SUCCESS The entry point is executed successfully.\r
95276127 47\r
48**/\r
49EFI_STATUS\r
50EFIAPI\r
51InitializeConPlatform(\r
52 IN EFI_HANDLE ImageHandle,\r
53 IN EFI_SYSTEM_TABLE *SystemTable\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57\r
5bca971e 58 Status = EfiLibInstallDriverBindingComponentName2 (\r
95276127 59 ImageHandle,\r
60 SystemTable,\r
61 &gConPlatformTextInDriverBinding,\r
62 ImageHandle,\r
63 &gConPlatformComponentName,\r
5bca971e 64 &gConPlatformComponentName2\r
95276127 65 );\r
66 ASSERT_EFI_ERROR (Status);\r
67\r
5bca971e 68 Status = EfiLibInstallDriverBindingComponentName2 (\r
95276127 69 ImageHandle,\r
70 SystemTable,\r
71 &gConPlatformTextOutDriverBinding,\r
72 NULL,\r
73 &gConPlatformComponentName,\r
5bca971e 74 &gConPlatformComponentName2\r
95276127 75 );\r
76 ASSERT_EFI_ERROR (Status);\r
77\r
c5ed97f6 78 return EFI_SUCCESS;\r
95276127 79}\r
80\r
81\r
9359e53f 82/**\r
c5ed97f6 83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle. \r
9359e53f 84\r
85 @param This Protocol instance pointer.\r
86 @param ControllerHandle Handle of device to test.\r
87 @param RemainingDevicePath Optional parameter use to pick a specific child\r
88 device to start.\r
89\r
7b9ff698 90 @retval EFI_SUCCESS This driver supports this device.\r
91 @retval other This driver does not support this device.\r
9359e53f 92\r
93**/\r
95276127 94EFI_STATUS\r
95EFIAPI\r
96ConPlatformTextInDriverBindingSupported (\r
97 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
98 IN EFI_HANDLE ControllerHandle,\r
c5ed97f6 99 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
95276127 100 )\r
95276127 101{\r
102 return ConPlatformDriverBindingSupported (\r
c5ed97f6 103 This,\r
104 ControllerHandle,\r
105 &gEfiSimpleTextInProtocolGuid\r
106 );\r
95276127 107}\r
108\r
9359e53f 109\r
110/**\r
c5ed97f6 111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle. \r
9359e53f 112\r
113 @param This Protocol instance pointer.\r
114 @param ControllerHandle Handle of device to test.\r
115 @param RemainingDevicePath Optional parameter use to pick a specific child\r
116 device to start.\r
117\r
7b9ff698 118 @retval EFI_SUCCESS This driver supports this device.\r
119 @retval other This driver does not support this device.\r
9359e53f 120\r
121**/\r
95276127 122EFI_STATUS\r
123EFIAPI\r
124ConPlatformTextOutDriverBindingSupported (\r
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
126 IN EFI_HANDLE ControllerHandle,\r
c5ed97f6 127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
95276127 128 )\r
95276127 129{\r
130 return ConPlatformDriverBindingSupported (\r
c5ed97f6 131 This,\r
132 ControllerHandle,\r
133 &gEfiSimpleTextOutProtocolGuid\r
134 );\r
95276127 135}\r
136\r
9359e53f 137\r
138/**\r
c5ed97f6 139 Test to see if the specified protocol is supported on ControllerHandle. \r
9359e53f 140\r
141 @param This Protocol instance pointer.\r
142 @param ControllerHandle Handle of device to test.\r
9359e53f 143 @param ProtocolGuid The specfic protocol.\r
144\r
7b9ff698 145 @retval EFI_SUCCESS This driver supports this device.\r
146 @retval other This driver does not support this device.\r
9359e53f 147\r
148**/\r
95276127 149EFI_STATUS\r
150ConPlatformDriverBindingSupported (\r
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
152 IN EFI_HANDLE ControllerHandle,\r
95276127 153 IN EFI_GUID *ProtocolGuid\r
154 )\r
95276127 155{\r
156 EFI_STATUS Status;\r
157 VOID *Interface;\r
158\r
159 //\r
7b9ff698 160 // Test to see if this is a physical device by checking if\r
161 // it has a Device Path Protocol.\r
95276127 162 //\r
163 Status = gBS->OpenProtocol (\r
164 ControllerHandle,\r
165 &gEfiDevicePathProtocolGuid,\r
166 NULL,\r
167 This->DriverBindingHandle,\r
168 ControllerHandle,\r
169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
170 );\r
171 if (EFI_ERROR (Status)) {\r
172 return Status;\r
173 }\r
174 //\r
7b9ff698 175 // Test to see if this device supports the specified Protocol.\r
95276127 176 //\r
177 Status = gBS->OpenProtocol (\r
178 ControllerHandle,\r
179 ProtocolGuid,\r
180 (VOID **) &Interface,\r
181 This->DriverBindingHandle,\r
182 ControllerHandle,\r
183 EFI_OPEN_PROTOCOL_BY_DRIVER\r
184 );\r
185 if (EFI_ERROR (Status)) {\r
186 return Status;\r
187 }\r
188\r
189 gBS->CloseProtocol (\r
c5ed97f6 190 ControllerHandle,\r
191 ProtocolGuid,\r
192 This->DriverBindingHandle,\r
193 ControllerHandle\r
194 );\r
95276127 195\r
196 return EFI_SUCCESS;\r
197}\r
198\r
9359e53f 199/**\r
c5ed97f6 200 Start this driver on the device for console input.\r
201\r
202 Start this driver on ControllerHandle by opening Simple Text Input Protocol,\r
24248368 203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.\r
9359e53f 204\r
205 If this devcie is not one hot-plug devce, append its device path into the \r
206 console environment variables ConInDev.\r
207 \r
208 @param This Protocol instance pointer.\r
209 @param ControllerHandle Handle of device to bind driver to\r
210 @param RemainingDevicePath Optional parameter use to pick a specific child\r
211 device to start.\r
212\r
213 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
214 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
415df2a3 215 @retval other This driver does not support this device.\r
9359e53f 216\r
217**/\r
95276127 218EFI_STATUS\r
219EFIAPI\r
220ConPlatformTextInDriverBindingStart (\r
221 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
222 IN EFI_HANDLE ControllerHandle,\r
223 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
224 )\r
95276127 225{\r
c5ed97f6 226 EFI_STATUS Status;\r
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
95276127 228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;\r
ca6b86ef 229 BOOLEAN IsInConInVariable;\r
95276127 230\r
231 //\r
232 // Get the Device Path Protocol so the environment variables can be updated\r
233 //\r
234 Status = gBS->OpenProtocol (\r
235 ControllerHandle,\r
236 &gEfiDevicePathProtocolGuid,\r
237 (VOID **) &DevicePath,\r
238 This->DriverBindingHandle,\r
239 ControllerHandle,\r
240 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
241 );\r
242 if (EFI_ERROR (Status)) {\r
243 return Status;\r
244 }\r
245 //\r
c5ed97f6 246 // Open the Simple Text Input Protocol BY_DRIVER\r
95276127 247 //\r
248 Status = gBS->OpenProtocol (\r
249 ControllerHandle,\r
250 &gEfiSimpleTextInProtocolGuid,\r
251 (VOID **) &TextIn,\r
252 This->DriverBindingHandle,\r
253 ControllerHandle,\r
254 EFI_OPEN_PROTOCOL_BY_DRIVER\r
255 );\r
256 if (EFI_ERROR (Status)) {\r
257 return Status;\r
258 }\r
ca6b86ef 259 //\r
260 // Check if the device path is in ConIn Variable\r
261 //\r
262 IsInConInVariable = FALSE;\r
263 Status = ConPlatformUpdateDeviceVariable (\r
264 L"ConIn",\r
265 DevicePath,\r
1ccdbf2a 266 Check\r
ca6b86ef 267 );\r
268 if (!EFI_ERROR (Status)) {\r
269 IsInConInVariable = TRUE;\r
270 }\r
271\r
95276127 272 //\r
f1aec6cc 273 // Check the device path, if it is a hot plug device,\r
95276127 274 // do not put the device path into ConInDev, and install\r
275 // gEfiConsoleInDeviceGuid to the device handle directly.\r
276 // The policy is, make hot plug device plug in and play immediately.\r
277 //\r
f1aec6cc 278 if (IsHotPlugDevice (DevicePath)) {\r
95276127 279 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 280 &ControllerHandle,\r
281 &gEfiConsoleInDeviceGuid,\r
282 NULL,\r
283 NULL\r
284 );\r
ca6b86ef 285 //\r
286 // Append the device path to ConInDev only if it is in ConIn variable.\r
287 //\r
288 if (IsInConInVariable) {\r
289 ConPlatformUpdateDeviceVariable (\r
290 L"ConInDev",\r
291 DevicePath,\r
1ccdbf2a 292 Append\r
ca6b86ef 293 );\r
294 }\r
95276127 295 } else {\r
296 //\r
c5ed97f6 297 // If it is not a hot-plug device, append the device path to the\r
298 // ConInDev environment variable\r
95276127 299 //\r
300 ConPlatformUpdateDeviceVariable (\r
ff37790d 301 L"ConInDev",\r
95276127 302 DevicePath,\r
1ccdbf2a 303 Append\r
95276127 304 );\r
305\r
306 //\r
9fdeb098 307 // If the device path is an instance in the ConIn environment variable,\r
95276127 308 // then install EfiConsoleInDeviceGuid onto ControllerHandle\r
309 //\r
ca6b86ef 310 if (IsInConInVariable) {\r
95276127 311 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 312 &ControllerHandle,\r
313 &gEfiConsoleInDeviceGuid,\r
314 NULL,\r
315 NULL\r
316 );\r
95276127 317 } else {\r
318 gBS->CloseProtocol (\r
c5ed97f6 319 ControllerHandle,\r
320 &gEfiSimpleTextInProtocolGuid,\r
321 This->DriverBindingHandle,\r
322 ControllerHandle\r
323 );\r
95276127 324 }\r
325 }\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
9359e53f 330/**\r
0a18956d 331 Start this driver on the device for console output and standard error output.\r
c5ed97f6 332\r
333 Start this driver on ControllerHandle by opening Simple Text Output Protocol,\r
24248368 334 reading Device Path, and installing Console Out Devcic GUID, Standard Error\r
9359e53f 335 Device GUID on ControllerHandle.\r
336\r
337 If this devcie is not one hot-plug devce, append its device path into the \r
7772b176 338 console environment variables ConOutDev, ErrOutDev.\r
9359e53f 339 \r
340 @param This Protocol instance pointer.\r
341 @param ControllerHandle Handle of device to bind driver to\r
342 @param RemainingDevicePath Optional parameter use to pick a specific child\r
343 device to start.\r
344\r
345 @retval EFI_SUCCESS This driver is added to ControllerHandle\r
346 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r
347 @retval other This driver does not support this device\r
348\r
349**/\r
95276127 350EFI_STATUS\r
351EFIAPI\r
352ConPlatformTextOutDriverBindingStart (\r
353 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
354 IN EFI_HANDLE ControllerHandle,\r
355 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
356 )\r
95276127 357{\r
c5ed97f6 358 EFI_STATUS Status;\r
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
95276127 360 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;\r
c5ed97f6 361 BOOLEAN NeedClose;\r
ca6b86ef 362 BOOLEAN IsInConOutVariable;\r
363 BOOLEAN IsInErrOutVariable;\r
95276127 364\r
365 NeedClose = TRUE;\r
366\r
367 //\r
368 // Get the Device Path Protocol so the environment variables can be updated\r
369 //\r
370 Status = gBS->OpenProtocol (\r
371 ControllerHandle,\r
372 &gEfiDevicePathProtocolGuid,\r
373 (VOID **) &DevicePath,\r
374 This->DriverBindingHandle,\r
375 ControllerHandle,\r
376 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
377 );\r
378 if (EFI_ERROR (Status)) {\r
379 return Status;\r
380 }\r
381 //\r
382 // Open the Simple Text Output Protocol BY_DRIVER\r
383 //\r
384 Status = gBS->OpenProtocol (\r
385 ControllerHandle,\r
386 &gEfiSimpleTextOutProtocolGuid,\r
387 (VOID **) &TextOut,\r
388 This->DriverBindingHandle,\r
389 ControllerHandle,\r
390 EFI_OPEN_PROTOCOL_BY_DRIVER\r
391 );\r
392 if (EFI_ERROR (Status)) {\r
393 return Status;\r
394 }\r
ca6b86ef 395 //\r
396 // Check if the device path is in ConOut & ErrOut Variable\r
397 //\r
398 IsInConOutVariable = FALSE;\r
399 Status = ConPlatformUpdateDeviceVariable (\r
400 L"ConOut",\r
401 DevicePath,\r
1ccdbf2a 402 Check\r
ca6b86ef 403 );\r
404 if (!EFI_ERROR (Status)) {\r
405 IsInConOutVariable = TRUE;\r
406 }\r
407\r
408 IsInErrOutVariable = FALSE;\r
409 Status = ConPlatformUpdateDeviceVariable (\r
410 L"ErrOut",\r
411 DevicePath,\r
1ccdbf2a 412 Check\r
ca6b86ef 413 );\r
414 if (!EFI_ERROR (Status)) {\r
415 IsInErrOutVariable = TRUE;\r
416 }\r
417\r
95276127 418 //\r
f1aec6cc 419 // Check the device path, if it is a hot plug device,\r
7772b176 420 // do not put the device path into ConOutDev and ErrOutDev,\r
95276127 421 // and install gEfiConsoleOutDeviceGuid to the device handle directly.\r
422 // The policy is, make hot plug device plug in and play immediately.\r
423 //\r
f1aec6cc 424 if (IsHotPlugDevice (DevicePath)) {\r
95276127 425 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 426 &ControllerHandle,\r
427 &gEfiConsoleOutDeviceGuid,\r
428 NULL,\r
429 NULL\r
430 );\r
ca6b86ef 431 //\r
432 // Append the device path to ConOutDev only if it is in ConOut variable.\r
433 //\r
434 if (IsInConOutVariable) {\r
435 ConPlatformUpdateDeviceVariable (\r
436 L"ConOutDev",\r
437 DevicePath,\r
1ccdbf2a 438 Append\r
ca6b86ef 439 );\r
440 }\r
441 //\r
442 // Append the device path to ErrOutDev only if it is in ErrOut variable.\r
443 //\r
444 if (IsInErrOutVariable) {\r
445 ConPlatformUpdateDeviceVariable (\r
446 L"ErrOutDev",\r
447 DevicePath,\r
1ccdbf2a 448 Append\r
ca6b86ef 449 );\r
450 }\r
95276127 451 } else {\r
452 //\r
5b7183ef
RN
453 // If it is not a hot-plug device, append the device path to \r
454 // the ConOutDev and ErrOutDev environment variable.\r
455 // For GOP device path, append the sibling device path as well.\r
95276127 456 //\r
5b7183ef
RN
457 if (!ConPlatformUpdateGopCandidate (DevicePath)) {\r
458 ConPlatformUpdateDeviceVariable (\r
459 L"ConOutDev",\r
460 DevicePath,\r
461 Append\r
462 );\r
463 //\r
464 // Then append the device path to the ErrOutDev environment variable\r
465 //\r
466 ConPlatformUpdateDeviceVariable (\r
467 L"ErrOutDev",\r
468 DevicePath,\r
469 Append\r
470 );\r
471 }\r
95276127 472\r
473 //\r
9fdeb098 474 // If the device path is an instance in the ConOut environment variable,\r
95276127 475 // then install EfiConsoleOutDeviceGuid onto ControllerHandle\r
476 //\r
ca6b86ef 477 if (IsInConOutVariable) {\r
95276127 478 NeedClose = FALSE;\r
479 Status = gBS->InstallMultipleProtocolInterfaces (\r
480 &ControllerHandle,\r
481 &gEfiConsoleOutDeviceGuid,\r
482 NULL,\r
483 NULL\r
484 );\r
485 }\r
486 //\r
9fdeb098 487 // If the device path is an instance in the ErrOut environment variable,\r
95276127 488 // then install EfiStandardErrorDeviceGuid onto ControllerHandle\r
489 //\r
ca6b86ef 490 if (IsInErrOutVariable) {\r
95276127 491 NeedClose = FALSE;\r
492 gBS->InstallMultipleProtocolInterfaces (\r
c5ed97f6 493 &ControllerHandle,\r
494 &gEfiStandardErrorDeviceGuid,\r
495 NULL,\r
496 NULL\r
497 );\r
95276127 498 }\r
499\r
500 if (NeedClose) {\r
501 gBS->CloseProtocol (\r
c5ed97f6 502 ControllerHandle,\r
503 &gEfiSimpleTextOutProtocolGuid,\r
504 This->DriverBindingHandle,\r
505 ControllerHandle\r
506 );\r
95276127 507 }\r
508 }\r
509\r
510 return EFI_SUCCESS;\r
511}\r
512\r
9359e53f 513/**\r
24248368 514 Stop this driver on ControllerHandle by removing Console In Devcice GUID \r
c5ed97f6 515 and closing the Simple Text Input protocol on ControllerHandle.\r
9359e53f 516\r
517 @param This Protocol instance pointer.\r
518 @param ControllerHandle Handle of device to stop driver on\r
519 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
520 children is zero stop the entire bus driver.\r
521 @param ChildHandleBuffer List of Child Handles to Stop.\r
522\r
523 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
524 @retval other This driver was not removed from this device\r
525\r
526**/\r
95276127 527EFI_STATUS\r
528EFIAPI\r
529ConPlatformTextInDriverBindingStop (\r
530 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
531 IN EFI_HANDLE ControllerHandle,\r
532 IN UINTN NumberOfChildren,\r
533 IN EFI_HANDLE *ChildHandleBuffer\r
534 )\r
95276127 535{\r
536 EFI_STATUS Status;\r
537 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
538\r
539 //\r
f1aec6cc 540 // Get the Device Path Protocol firstly\r
95276127 541 //\r
f1aec6cc 542 Status = gBS->OpenProtocol (\r
543 ControllerHandle,\r
544 &gEfiDevicePathProtocolGuid,\r
545 (VOID **) &DevicePath,\r
546 This->DriverBindingHandle,\r
547 ControllerHandle,\r
548 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
549 );\r
550 //\r
551 // If there is device path on ControllerHandle\r
552 //\r
553 if (!EFI_ERROR (Status)) {\r
95276127 554 //\r
ca6b86ef 555 // Remove DevicePath from ConInDev if exists.\r
4986c561 556 //\r
ca6b86ef 557 ConPlatformUpdateDeviceVariable (\r
558 L"ConInDev",\r
559 DevicePath,\r
1ccdbf2a 560 Delete\r
ca6b86ef 561 );\r
95276127 562 }\r
f1aec6cc 563\r
95276127 564 //\r
565 // Uninstall the Console Device GUIDs from Controller Handle\r
566 //\r
567 ConPlatformUnInstallProtocol (\r
568 This,\r
569 ControllerHandle,\r
570 &gEfiConsoleInDeviceGuid\r
571 );\r
572\r
573 //\r
c5ed97f6 574 // Close the Simple Text Input Protocol\r
95276127 575 //\r
576 gBS->CloseProtocol (\r
ca6b86ef 577 ControllerHandle,\r
578 &gEfiSimpleTextInProtocolGuid,\r
579 This->DriverBindingHandle,\r
580 ControllerHandle\r
581 );\r
95276127 582\r
583 return EFI_SUCCESS;\r
584}\r
585\r
9359e53f 586\r
587/**\r
24248368 588 Stop this driver on ControllerHandle by removing Console Out Devcice GUID \r
c5ed97f6 589 and closing the Simple Text Output protocol on ControllerHandle.\r
9359e53f 590\r
591 @param This Protocol instance pointer.\r
592 @param ControllerHandle Handle of device to stop driver on\r
593 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
594 children is zero stop the entire bus driver.\r
595 @param ChildHandleBuffer List of Child Handles to Stop.\r
596\r
597 @retval EFI_SUCCESS This driver is removed ControllerHandle\r
598 @retval other This driver was not removed from this device\r
599\r
600**/\r
95276127 601EFI_STATUS\r
602EFIAPI\r
603ConPlatformTextOutDriverBindingStop (\r
604 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
605 IN EFI_HANDLE ControllerHandle,\r
606 IN UINTN NumberOfChildren,\r
607 IN EFI_HANDLE *ChildHandleBuffer\r
608 )\r
95276127 609{\r
610 EFI_STATUS Status;\r
611 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
612\r
613 //\r
f1aec6cc 614 // Get the Device Path Protocol firstly\r
95276127 615 //\r
f1aec6cc 616 Status = gBS->OpenProtocol (\r
617 ControllerHandle,\r
618 &gEfiDevicePathProtocolGuid,\r
619 (VOID **) &DevicePath,\r
620 This->DriverBindingHandle,\r
621 ControllerHandle,\r
622 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
623 );\r
624 if (!EFI_ERROR (Status)) {\r
95276127 625 //\r
ca6b86ef 626 // Remove DevicePath from ConOutDev and ErrOutDev if exists.\r
95276127 627 //\r
ca6b86ef 628 ConPlatformUpdateDeviceVariable (\r
629 L"ConOutDev",\r
630 DevicePath,\r
1ccdbf2a 631 Delete\r
ca6b86ef 632 );\r
633 ConPlatformUpdateDeviceVariable (\r
634 L"ErrOutDev",\r
635 DevicePath,\r
1ccdbf2a 636 Delete\r
ca6b86ef 637 );\r
95276127 638 }\r
f1aec6cc 639\r
95276127 640 //\r
641 // Uninstall the Console Device GUIDs from Controller Handle\r
642 //\r
643 ConPlatformUnInstallProtocol (\r
644 This,\r
645 ControllerHandle,\r
646 &gEfiConsoleOutDeviceGuid\r
647 );\r
648\r
649 ConPlatformUnInstallProtocol (\r
650 This,\r
651 ControllerHandle,\r
652 &gEfiStandardErrorDeviceGuid\r
653 );\r
654\r
655 //\r
656 // Close the Simple Text Output Protocol\r
657 //\r
658 gBS->CloseProtocol (\r
659 ControllerHandle,\r
660 &gEfiSimpleTextOutProtocolGuid,\r
661 This->DriverBindingHandle,\r
662 ControllerHandle\r
663 );\r
664\r
665 return EFI_SUCCESS;\r
666}\r
667\r
668\r
9359e53f 669/**\r
7b9ff698 670 Uninstall the specified protocol.\r
9359e53f 671\r
672 @param This Protocol instance pointer.\r
7b9ff698 673 @param Handle Handle of device to uninstall protocol on.\r
674 @param ProtocolGuid The specified protocol need to be uninstalled.\r
9359e53f 675\r
9359e53f 676**/\r
95276127 677VOID\r
678ConPlatformUnInstallProtocol (\r
679 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
680 IN EFI_HANDLE Handle,\r
681 IN EFI_GUID *ProtocolGuid\r
682 )\r
683{\r
684 EFI_STATUS Status;\r
685\r
686 Status = gBS->OpenProtocol (\r
687 Handle,\r
688 ProtocolGuid,\r
689 NULL,\r
690 This->DriverBindingHandle,\r
691 Handle,\r
692 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
693 );\r
694\r
695 if (!EFI_ERROR (Status)) {\r
696 gBS->UninstallMultipleProtocolInterfaces (\r
c5ed97f6 697 Handle,\r
698 ProtocolGuid,\r
699 NULL,\r
700 NULL\r
701 );\r
95276127 702 }\r
703\r
704 return ;\r
705}\r
706\r
9359e53f 707/**\r
7772b176 708 Get the necessary size of buffer and read the variable.\r
c5ed97f6 709\r
710 First get the necessary size of buffer. Then read the\r
711 EFI variable (Name) and return a dynamically allocated\r
712 buffer. On failure return NULL.\r
95276127 713\r
9359e53f 714 @param Name String part of EFI variable name\r
95276127 715\r
9359e53f 716 @return Dynamically allocated memory that contains a copy of the EFI variable.\r
7b9ff698 717 Caller is repsoncible freeing the buffer. Return NULL means Variable \r
718 was not read.\r
95276127 719\r
9359e53f 720**/\r
721VOID *\r
722ConPlatformGetVariable (\r
723 IN CHAR16 *Name\r
724 )\r
95276127 725{\r
726 EFI_STATUS Status;\r
727 VOID *Buffer;\r
728 UINTN BufferSize;\r
729\r
730 BufferSize = 0;\r
731 Buffer = NULL;\r
732\r
733 //\r
c5ed97f6 734 // Test to see if the variable exists. If it doesn't, return NULL.\r
95276127 735 //\r
736 Status = gRT->GetVariable (\r
737 Name,\r
738 &gEfiGlobalVariableGuid,\r
739 NULL,\r
740 &BufferSize,\r
741 Buffer\r
742 );\r
743\r
744 if (Status == EFI_BUFFER_TOO_SMALL) {\r
745 //\r
746 // Allocate the buffer to return\r
747 //\r
748 Buffer = AllocatePool (BufferSize);\r
749 if (Buffer == NULL) {\r
750 return NULL;\r
751 }\r
752 //\r
753 // Read variable into the allocated buffer.\r
754 //\r
755 Status = gRT->GetVariable (\r
756 Name,\r
757 &gEfiGlobalVariableGuid,\r
758 NULL,\r
759 &BufferSize,\r
760 Buffer\r
761 );\r
762 if (EFI_ERROR (Status)) {\r
763 FreePool (Buffer);\r
7b9ff698 764 //\r
765 // To make sure Buffer is NULL if any error occurs.\r
766 //\r
95276127 767 Buffer = NULL;\r
768 }\r
769 }\r
770\r
771 return Buffer;\r
772}\r
773\r
19f50882
RN
774/**\r
775 Function returns TRUE when the two input device paths point to the two\r
776 GOP child handles that have the same parent.\r
777\r
778 @param Left A pointer to a device path data structure.\r
779 @param Right A pointer to a device path data structure.\r
780\r
781 @retval TRUE Left and Right share the same parent.\r
782 @retval FALSE Left and Right don't share the same parent or either of them is not\r
783 a GOP device path.\r
784**/\r
785BOOLEAN\r
786IsGopSibling (\r
787 IN EFI_DEVICE_PATH_PROTOCOL *Left,\r
788 IN EFI_DEVICE_PATH_PROTOCOL *Right\r
789 )\r
790{\r
791 EFI_DEVICE_PATH_PROTOCOL *NodeLeft;\r
792 EFI_DEVICE_PATH_PROTOCOL *NodeRight;\r
793\r
794 for (NodeLeft = Left; !IsDevicePathEndType (NodeLeft); NodeLeft = NextDevicePathNode (NodeLeft)) {\r
fa9bebd8
RN
795 if ((DevicePathType (NodeLeft) == ACPI_DEVICE_PATH && DevicePathSubType (NodeLeft) == ACPI_ADR_DP) ||\r
796 (DevicePathType (NodeLeft) == HARDWARE_DEVICE_PATH && DevicePathSubType (NodeLeft) == HW_CONTROLLER_DP &&\r
797 DevicePathType (NextDevicePathNode (NodeLeft)) == ACPI_DEVICE_PATH && DevicePathSubType (NextDevicePathNode (NodeLeft)) == ACPI_ADR_DP)) {\r
19f50882
RN
798 break;\r
799 }\r
800 }\r
801\r
802 if (IsDevicePathEndType (NodeLeft)) {\r
803 return FALSE;\r
804 }\r
805\r
806 for (NodeRight = Right; !IsDevicePathEndType (NodeRight); NodeRight = NextDevicePathNode (NodeRight)) {\r
fa9bebd8
RN
807 if ((DevicePathType (NodeRight) == ACPI_DEVICE_PATH && DevicePathSubType (NodeRight) == ACPI_ADR_DP) ||\r
808 (DevicePathType (NodeRight) == HARDWARE_DEVICE_PATH && DevicePathSubType (NodeRight) == HW_CONTROLLER_DP &&\r
809 DevicePathType (NextDevicePathNode (NodeRight)) == ACPI_DEVICE_PATH && DevicePathSubType (NextDevicePathNode (NodeRight)) == ACPI_ADR_DP)) {\r
19f50882
RN
810 break;\r
811 }\r
812 }\r
813\r
814 if (IsDevicePathEndType (NodeRight)) {\r
815 return FALSE;\r
816 }\r
817\r
818 if (((UINTN) NodeLeft - (UINTN) Left) != ((UINTN) NodeRight - (UINTN) Right)) {\r
819 return FALSE;\r
820 }\r
821\r
822 return (BOOLEAN) (CompareMem (Left, Right, (UINTN) NodeLeft - (UINTN) Left) == 0);\r
823}\r
824\r
9359e53f 825/**\r
95276127 826 Function compares a device path data structure to that of all the nodes of a\r
827 second device path instance.\r
828\r
9359e53f 829 @param Multi A pointer to a multi-instance device path data structure.\r
830 @param Single A pointer to a single-instance device path data structure.\r
831 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it\r
832 points to the remaining device path data structure.\r
833 (remaining device path = Multi - Single.)\r
834 @param Delete If TRUE, means removing Single from Multi.\r
835 If FALSE, the routine just check whether Single matches\r
836 with any instance in Multi.\r
95276127 837\r
c5ed97f6 838 @retval EFI_SUCCESS If the Single is contained within Multi.\r
839 @retval EFI_NOT_FOUND If the Single is not contained within Multi.\r
840 @retval EFI_INVALID_PARAMETER Multi is NULL.\r
841 @retval EFI_INVALID_PARAMETER Single is NULL.\r
842 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.\r
95276127 843\r
9359e53f 844**/\r
845EFI_STATUS\r
846ConPlatformMatchDevicePaths (\r
847 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
848 IN EFI_DEVICE_PATH_PROTOCOL *Single,\r
7b9ff698 849 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,\r
9359e53f 850 IN BOOLEAN Delete\r
851 )\r
95276127 852{\r
853 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
7b9ff698 854 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;\r
855 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;\r
95276127 856 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
857 UINTN Size;\r
858\r
859 //\r
860 // The passed in DevicePath should not be NULL\r
861 //\r
9359e53f 862 if ((Multi == NULL) || (Single == NULL)) {\r
c5ed97f6 863 return EFI_INVALID_PARAMETER;\r
95276127 864 }\r
7b9ff698 865\r
95276127 866 //\r
7b9ff698 867 // If performing Delete operation, the NewDevicePath must not be NULL.\r
95276127 868 //\r
7b9ff698 869 if (Delete) {\r
c5ed97f6 870 if (NewDevicePath == NULL) {\r
871 return EFI_INVALID_PARAMETER;\r
872 }\r
7b9ff698 873 }\r
874\r
875 TempDevicePath1 = NULL;\r
95276127 876\r
877 DevicePath = Multi;\r
878 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
879\r
880 //\r
7b9ff698 881 // Search for the match of 'Single' in 'Multi'\r
95276127 882 //\r
9359e53f 883 while (DevicePathInst != NULL) {\r
19f50882 884 if ((CompareMem (Single, DevicePathInst, Size) == 0) || IsGopSibling (Single, DevicePathInst)) {\r
95276127 885 if (!Delete) {\r
7b9ff698 886 //\r
887 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.\r
888 //\r
95276127 889 FreePool (DevicePathInst);\r
890 return EFI_SUCCESS;\r
891 }\r
892 } else {\r
893 if (Delete) {\r
7b9ff698 894 //\r
c5ed97f6 895 // If the node of Multi does not match Single, then added it back to the result.\r
896 // That is, the node matching Single will be dropped and deleted from result.\r
7b9ff698 897 //\r
898 TempDevicePath2 = AppendDevicePathInstance (\r
899 TempDevicePath1,\r
95276127 900 DevicePathInst\r
901 );\r
676df92c 902 if (TempDevicePath1 != NULL) {\r
903 FreePool (TempDevicePath1);\r
904 }\r
7b9ff698 905 TempDevicePath1 = TempDevicePath2;\r
95276127 906 }\r
907 }\r
908\r
909 FreePool (DevicePathInst);\r
910 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
911 }\r
912\r
913 if (Delete) {\r
7b9ff698 914 //\r
c5ed97f6 915 // Return the new device path data structure with specified node deleted.\r
7b9ff698 916 //\r
917 *NewDevicePath = TempDevicePath1;\r
95276127 918 return EFI_SUCCESS;\r
919 }\r
920\r
921 return EFI_NOT_FOUND;\r
922}\r
923\r
9359e53f 924/**\r
7b9ff698 925 Update console environment variables. \r
9359e53f 926\r
927 @param VariableName Console environment variables, ConOutDev, ConInDev\r
7772b176 928 ErrOutDev, ConIn ,ConOut or ErrOut.\r
9359e53f 929 @param DevicePath Console devcie's device path.\r
c5ed97f6 930 @param Operation Variable operations, including APPEND, CHECK and DELETE.\r
9359e53f 931\r
932 @retval EFI_SUCCESS Variable operates successfully.\r
933 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.\r
934 @retval other Variable updating failed.\r
935\r
936**/\r
95276127 937EFI_STATUS\r
938ConPlatformUpdateDeviceVariable (\r
939 IN CHAR16 *VariableName,\r
940 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
941 IN CONPLATFORM_VAR_OPERATION Operation\r
942 )\r
95276127 943{\r
944 EFI_STATUS Status;\r
945 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;\r
946 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;\r
947\r
948 VariableDevicePath = NULL;\r
949 NewVariableDevicePath = NULL;\r
950\r
951 //\r
952 // Get Variable according to variable name.\r
953 // The memory for Variable is allocated within ConPlatformGetVarible(),\r
954 // it is the caller's responsibility to free the memory before return.\r
955 //\r
956 VariableDevicePath = ConPlatformGetVariable (VariableName);\r
957\r
1ccdbf2a 958 if (Operation != Delete) {\r
7b9ff698 959 //\r
960 // Match specified DevicePath in Console Variable.\r
961 // \r
95276127 962 Status = ConPlatformMatchDevicePaths (\r
c5ed97f6 963 VariableDevicePath,\r
964 DevicePath,\r
965 NULL,\r
966 FALSE\r
967 );\r
95276127 968\r
1ccdbf2a 969 if ((Operation == Check) || (!EFI_ERROR (Status))) {\r
95276127 970 //\r
c5ed97f6 971 // Branch here includes 2 cases:\r
972 // 1. Operation is CHECK, simply return Status.\r
973 // 2. Operation is APPEND, and device path already exists in variable, also return.\r
95276127 974 //\r
975 if (VariableDevicePath != NULL) {\r
976 FreePool (VariableDevicePath);\r
977 }\r
978\r
979 return Status;\r
980 }\r
981 //\r
c5ed97f6 982 // We reach here to append a device path that does not exist in variable.\r
95276127 983 //\r
984 Status = EFI_SUCCESS;\r
985 NewVariableDevicePath = AppendDevicePathInstance (\r
986 VariableDevicePath,\r
987 DevicePath\r
988 );\r
989 if (NewVariableDevicePath == NULL) {\r
990 Status = EFI_OUT_OF_RESOURCES;\r
991 }\r
992\r
993 } else {\r
994 //\r
c5ed97f6 995 // We reach here to remove DevicePath from the environment variable that\r
95276127 996 // is a multi-instance device path.\r
997 //\r
998 Status = ConPlatformMatchDevicePaths (\r
c5ed97f6 999 VariableDevicePath,\r
1000 DevicePath,\r
1001 &NewVariableDevicePath,\r
1002 TRUE\r
1003 );\r
95276127 1004 }\r
1005\r
1006 if (VariableDevicePath != NULL) {\r
1007 FreePool (VariableDevicePath);\r
1008 }\r
1009\r
1010 if (EFI_ERROR (Status)) {\r
1011 return Status;\r
1012 }\r
1013\r
c62a593c 1014 if (NewVariableDevicePath != NULL) {\r
7b9ff698 1015 //\r
1016 // Update Console Environment Variable.\r
1017 //\r
c62a593c 1018 Status = gRT->SetVariable (\r
1019 VariableName,\r
1020 &gEfiGlobalVariableGuid,\r
1021 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1022 GetDevicePathSize (NewVariableDevicePath),\r
1023 NewVariableDevicePath\r
1024 );\r
95276127 1025\r
c62a593c 1026 FreePool (NewVariableDevicePath);\r
1027 }\r
95276127 1028\r
1029 return Status;\r
1030}\r
1031\r
9359e53f 1032/**\r
f1aec6cc 1033 Check if the device supports hot-plug through its device path.\r
1034\r
1035 This function could be updated to check more types of Hot Plug devices.\r
1036 Currently, it checks USB and PCCard device.\r
9359e53f 1037\r
f1aec6cc 1038 @param DevicePath Pointer to device's device path.\r
9359e53f 1039\r
1040 @retval TRUE The devcie is a hot-plug device\r
1041 @retval FALSE The devcie is not a hot-plug device.\r
1042\r
1043**/\r
95276127 1044BOOLEAN\r
1045IsHotPlugDevice (\r
f1aec6cc 1046 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
95276127 1047 )\r
1048{\r
f1aec6cc 1049 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;\r
aa8f4f55 1050\r
f1aec6cc 1051 CheckDevicePath = DevicePath;\r
1052 while (!IsDevicePathEnd (CheckDevicePath)) {\r
1053 //\r
1054 // Check device whether is hot plug device or not throught Device Path\r
1055 // \r
1056 if ((DevicePathType (CheckDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1057 (DevicePathSubType (CheckDevicePath) == MSG_USB_DP ||\r
1058 DevicePathSubType (CheckDevicePath) == MSG_USB_CLASS_DP ||\r
1059 DevicePathSubType (CheckDevicePath) == MSG_USB_WWID_DP)) {\r
1060 //\r
1061 // If Device is USB device\r
1062 //\r
1063 return TRUE;\r
1064 }\r
1065 if ((DevicePathType (CheckDevicePath) == HARDWARE_DEVICE_PATH) &&\r
1066 (DevicePathSubType (CheckDevicePath) == HW_PCCARD_DP)) {\r
1067 //\r
1068 // If Device is PCCard\r
1069 //\r
1070 return TRUE;\r
1071 }\r
1072 \r
1073 CheckDevicePath = NextDevicePathNode (CheckDevicePath);\r
95276127 1074 }\r
1075\r
f1aec6cc 1076 return FALSE;\r
95276127 1077}\r
f1aec6cc 1078\r
5b7183ef
RN
1079/**\r
1080 Update ConOutDev and ErrOutDev variables to add the device path of\r
1081 GOP controller itself and the sibling controllers.\r
1082\r
1083 @param DevicePath Pointer to device's device path.\r
1084\r
1085 @retval TRUE The devcie is a GOP device.\r
1086 @retval FALSE The devcie is not a GOP device.\r
1087\r
1088**/\r
1089BOOLEAN\r
1090ConPlatformUpdateGopCandidate (\r
1091 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1092 )\r
1093{\r
1094 EFI_STATUS Status;\r
1095 EFI_HANDLE PciHandle;\r
1096 EFI_HANDLE GopHandle;\r
5b7183ef
RN
1097 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1098\r
1099 //\r
1100 // Check whether it's a GOP device.\r
1101 //\r
1102 TempDevicePath = DevicePath;\r
1103 Status = gBS->LocateDevicePath (&gEfiGraphicsOutputProtocolGuid, &TempDevicePath, &GopHandle);\r
1104 if (EFI_ERROR (Status)) {\r
1105 return FALSE;\r
1106 }\r
1107 //\r
1108 // Get the parent PciIo handle in order to find all the children\r
1109 //\r
1110 Status = gBS->LocateDevicePath (&gEfiPciIoProtocolGuid, &DevicePath, &PciHandle);\r
1111 if (EFI_ERROR (Status)) {\r
1112 return FALSE;\r
1113 }\r
56dee83a
RN
1114 TempDevicePath = EfiBootManagerGetGopDevicePath (PciHandle);\r
1115 if (TempDevicePath != NULL) {\r
1116 ConPlatformUpdateDeviceVariable (L"ConOutDev", TempDevicePath, Append);\r
1117 ConPlatformUpdateDeviceVariable (L"ErrOutDev", TempDevicePath, Append);\r
5b7183ef 1118 }\r
5b7183ef
RN
1119 return TRUE;\r
1120}\r