]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
MdeModulePkg/Library: Fix typos in comments and variables
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
... / ...
CommitLineData
1/** @file\r
2 Console Platform DXE Driver, install Console Device Guids and update Console\r
3 Environment Variables.\r
4\r
5Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
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
14**/\r
15\r
16#include "ConPlatform.h"\r
17\r
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
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
42\r
43 @param ImageHandle The firmware allocated handle for the EFI image.\r
44 @param SystemTable A pointer to the EFI System Table.\r
45\r
46 @retval EFI_SUCCESS The entry point is executed successfully.\r
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
58 Status = EfiLibInstallDriverBindingComponentName2 (\r
59 ImageHandle,\r
60 SystemTable,\r
61 &gConPlatformTextInDriverBinding,\r
62 ImageHandle,\r
63 &gConPlatformComponentName,\r
64 &gConPlatformComponentName2\r
65 );\r
66 ASSERT_EFI_ERROR (Status);\r
67\r
68 Status = EfiLibInstallDriverBindingComponentName2 (\r
69 ImageHandle,\r
70 SystemTable,\r
71 &gConPlatformTextOutDriverBinding,\r
72 NULL,\r
73 &gConPlatformComponentName,\r
74 &gConPlatformComponentName2\r
75 );\r
76 ASSERT_EFI_ERROR (Status);\r
77\r
78 return EFI_SUCCESS;\r
79}\r
80\r
81\r
82/**\r
83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle. \r
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
90 @retval EFI_SUCCESS This driver supports this device.\r
91 @retval other This driver does not support this device.\r
92\r
93**/\r
94EFI_STATUS\r
95EFIAPI\r
96ConPlatformTextInDriverBindingSupported (\r
97 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
98 IN EFI_HANDLE ControllerHandle,\r
99 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
100 )\r
101{\r
102 return ConPlatformDriverBindingSupported (\r
103 This,\r
104 ControllerHandle,\r
105 &gEfiSimpleTextInProtocolGuid\r
106 );\r
107}\r
108\r
109\r
110/**\r
111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle. \r
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
118 @retval EFI_SUCCESS This driver supports this device.\r
119 @retval other This driver does not support this device.\r
120\r
121**/\r
122EFI_STATUS\r
123EFIAPI\r
124ConPlatformTextOutDriverBindingSupported (\r
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
126 IN EFI_HANDLE ControllerHandle,\r
127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
128 )\r
129{\r
130 return ConPlatformDriverBindingSupported (\r
131 This,\r
132 ControllerHandle,\r
133 &gEfiSimpleTextOutProtocolGuid\r
134 );\r
135}\r
136\r
137\r
138/**\r
139 Test to see if the specified protocol is supported on ControllerHandle. \r
140\r
141 @param This Protocol instance pointer.\r
142 @param ControllerHandle Handle of device to test.\r
143 @param ProtocolGuid The specfic protocol.\r
144\r
145 @retval EFI_SUCCESS This driver supports this device.\r
146 @retval other This driver does not support this device.\r
147\r
148**/\r
149EFI_STATUS\r
150ConPlatformDriverBindingSupported (\r
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
152 IN EFI_HANDLE ControllerHandle,\r
153 IN EFI_GUID *ProtocolGuid\r
154 )\r
155{\r
156 EFI_STATUS Status;\r
157 VOID *Interface;\r
158\r
159 //\r
160 // Test to see if this is a physical device by checking if\r
161 // it has a Device Path Protocol.\r
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
175 // Test to see if this device supports the specified Protocol.\r
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
190 ControllerHandle,\r
191 ProtocolGuid,\r
192 This->DriverBindingHandle,\r
193 ControllerHandle\r
194 );\r
195\r
196 return EFI_SUCCESS;\r
197}\r
198\r
199/**\r
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
203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.\r
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
215 @retval other This driver does not support this device.\r
216\r
217**/\r
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
225{\r
226 EFI_STATUS Status;\r
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;\r
229 BOOLEAN IsInConInVariable;\r
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
246 // Open the Simple Text Input Protocol BY_DRIVER\r
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
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
266 Check\r
267 );\r
268 if (!EFI_ERROR (Status)) {\r
269 IsInConInVariable = TRUE;\r
270 }\r
271\r
272 //\r
273 // Check the device path, if it is a hot plug device,\r
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
278 if (IsHotPlugDevice (DevicePath)) {\r
279 gBS->InstallMultipleProtocolInterfaces (\r
280 &ControllerHandle,\r
281 &gEfiConsoleInDeviceGuid,\r
282 NULL,\r
283 NULL\r
284 );\r
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
292 Append\r
293 );\r
294 }\r
295 } else {\r
296 //\r
297 // If it is not a hot-plug device, append the device path to the\r
298 // ConInDev environment variable\r
299 //\r
300 ConPlatformUpdateDeviceVariable (\r
301 L"ConInDev",\r
302 DevicePath,\r
303 Append\r
304 );\r
305\r
306 //\r
307 // If the device path is an instance in the ConIn environment variable,\r
308 // then install EfiConsoleInDeviceGuid onto ControllerHandle\r
309 //\r
310 if (IsInConInVariable) {\r
311 gBS->InstallMultipleProtocolInterfaces (\r
312 &ControllerHandle,\r
313 &gEfiConsoleInDeviceGuid,\r
314 NULL,\r
315 NULL\r
316 );\r
317 } else {\r
318 gBS->CloseProtocol (\r
319 ControllerHandle,\r
320 &gEfiSimpleTextInProtocolGuid,\r
321 This->DriverBindingHandle,\r
322 ControllerHandle\r
323 );\r
324 }\r
325 }\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
330/**\r
331 Start this driver on the device for console output and stardard error output.\r
332\r
333 Start this driver on ControllerHandle by opening Simple Text Output Protocol,\r
334 reading Device Path, and installing Console Out Devcic GUID, Standard Error\r
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
338 console environment variables ConOutDev, ErrOutDev.\r
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
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
357{\r
358 EFI_STATUS Status;\r
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
360 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;\r
361 BOOLEAN NeedClose;\r
362 BOOLEAN IsInConOutVariable;\r
363 BOOLEAN IsInErrOutVariable;\r
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
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
402 Check\r
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
412 Check\r
413 );\r
414 if (!EFI_ERROR (Status)) {\r
415 IsInErrOutVariable = TRUE;\r
416 }\r
417\r
418 //\r
419 // Check the device path, if it is a hot plug device,\r
420 // do not put the device path into ConOutDev and ErrOutDev,\r
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
424 if (IsHotPlugDevice (DevicePath)) {\r
425 gBS->InstallMultipleProtocolInterfaces (\r
426 &ControllerHandle,\r
427 &gEfiConsoleOutDeviceGuid,\r
428 NULL,\r
429 NULL\r
430 );\r
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
438 Append\r
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
448 Append\r
449 );\r
450 }\r
451 } else {\r
452 //\r
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
456 //\r
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
472\r
473 //\r
474 // If the device path is an instance in the ConOut environment variable,\r
475 // then install EfiConsoleOutDeviceGuid onto ControllerHandle\r
476 //\r
477 if (IsInConOutVariable) {\r
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
487 // If the device path is an instance in the ErrOut environment variable,\r
488 // then install EfiStandardErrorDeviceGuid onto ControllerHandle\r
489 //\r
490 if (IsInErrOutVariable) {\r
491 NeedClose = FALSE;\r
492 gBS->InstallMultipleProtocolInterfaces (\r
493 &ControllerHandle,\r
494 &gEfiStandardErrorDeviceGuid,\r
495 NULL,\r
496 NULL\r
497 );\r
498 }\r
499\r
500 if (NeedClose) {\r
501 gBS->CloseProtocol (\r
502 ControllerHandle,\r
503 &gEfiSimpleTextOutProtocolGuid,\r
504 This->DriverBindingHandle,\r
505 ControllerHandle\r
506 );\r
507 }\r
508 }\r
509\r
510 return EFI_SUCCESS;\r
511}\r
512\r
513/**\r
514 Stop this driver on ControllerHandle by removing Console In Devcice GUID \r
515 and closing the Simple Text Input protocol on ControllerHandle.\r
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
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
535{\r
536 EFI_STATUS Status;\r
537 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
538\r
539 //\r
540 // Get the Device Path Protocol firstly\r
541 //\r
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
554 //\r
555 // Remove DevicePath from ConInDev if exists.\r
556 //\r
557 ConPlatformUpdateDeviceVariable (\r
558 L"ConInDev",\r
559 DevicePath,\r
560 Delete\r
561 );\r
562 }\r
563\r
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
574 // Close the Simple Text Input Protocol\r
575 //\r
576 gBS->CloseProtocol (\r
577 ControllerHandle,\r
578 &gEfiSimpleTextInProtocolGuid,\r
579 This->DriverBindingHandle,\r
580 ControllerHandle\r
581 );\r
582\r
583 return EFI_SUCCESS;\r
584}\r
585\r
586\r
587/**\r
588 Stop this driver on ControllerHandle by removing Console Out Devcice GUID \r
589 and closing the Simple Text Output protocol on ControllerHandle.\r
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
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
609{\r
610 EFI_STATUS Status;\r
611 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
612\r
613 //\r
614 // Get the Device Path Protocol firstly\r
615 //\r
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
625 //\r
626 // Remove DevicePath from ConOutDev and ErrOutDev if exists.\r
627 //\r
628 ConPlatformUpdateDeviceVariable (\r
629 L"ConOutDev",\r
630 DevicePath,\r
631 Delete\r
632 );\r
633 ConPlatformUpdateDeviceVariable (\r
634 L"ErrOutDev",\r
635 DevicePath,\r
636 Delete\r
637 );\r
638 }\r
639\r
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
669/**\r
670 Uninstall the specified protocol.\r
671\r
672 @param This Protocol instance pointer.\r
673 @param Handle Handle of device to uninstall protocol on.\r
674 @param ProtocolGuid The specified protocol need to be uninstalled.\r
675\r
676**/\r
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
697 Handle,\r
698 ProtocolGuid,\r
699 NULL,\r
700 NULL\r
701 );\r
702 }\r
703\r
704 return ;\r
705}\r
706\r
707/**\r
708 Get the necessary size of buffer and read the variable.\r
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
713\r
714 @param Name String part of EFI variable name\r
715\r
716 @return Dynamically allocated memory that contains a copy of the EFI variable.\r
717 Caller is repsoncible freeing the buffer. Return NULL means Variable \r
718 was not read.\r
719\r
720**/\r
721VOID *\r
722ConPlatformGetVariable (\r
723 IN CHAR16 *Name\r
724 )\r
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
734 // Test to see if the variable exists. If it doesn't, return NULL.\r
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
764 //\r
765 // To make sure Buffer is NULL if any error occurs.\r
766 //\r
767 Buffer = NULL;\r
768 }\r
769 }\r
770\r
771 return Buffer;\r
772}\r
773\r
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
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
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
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
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
825/**\r
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
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
837\r
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
843\r
844**/\r
845EFI_STATUS\r
846ConPlatformMatchDevicePaths (\r
847 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
848 IN EFI_DEVICE_PATH_PROTOCOL *Single,\r
849 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,\r
850 IN BOOLEAN Delete\r
851 )\r
852{\r
853 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
854 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;\r
855 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;\r
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
862 if ((Multi == NULL) || (Single == NULL)) {\r
863 return EFI_INVALID_PARAMETER;\r
864 }\r
865\r
866 //\r
867 // If performing Delete operation, the NewDevicePath must not be NULL.\r
868 //\r
869 if (Delete) {\r
870 if (NewDevicePath == NULL) {\r
871 return EFI_INVALID_PARAMETER;\r
872 }\r
873 }\r
874\r
875 TempDevicePath1 = NULL;\r
876\r
877 DevicePath = Multi;\r
878 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
879\r
880 //\r
881 // Search for the match of 'Single' in 'Multi'\r
882 //\r
883 while (DevicePathInst != NULL) {\r
884 if ((CompareMem (Single, DevicePathInst, Size) == 0) || IsGopSibling (Single, DevicePathInst)) {\r
885 if (!Delete) {\r
886 //\r
887 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.\r
888 //\r
889 FreePool (DevicePathInst);\r
890 return EFI_SUCCESS;\r
891 }\r
892 } else {\r
893 if (Delete) {\r
894 //\r
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
897 //\r
898 TempDevicePath2 = AppendDevicePathInstance (\r
899 TempDevicePath1,\r
900 DevicePathInst\r
901 );\r
902 if (TempDevicePath1 != NULL) {\r
903 FreePool (TempDevicePath1);\r
904 }\r
905 TempDevicePath1 = TempDevicePath2;\r
906 }\r
907 }\r
908\r
909 FreePool (DevicePathInst);\r
910 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
911 }\r
912\r
913 if (Delete) {\r
914 //\r
915 // Return the new device path data structure with specified node deleted.\r
916 //\r
917 *NewDevicePath = TempDevicePath1;\r
918 return EFI_SUCCESS;\r
919 }\r
920\r
921 return EFI_NOT_FOUND;\r
922}\r
923\r
924/**\r
925 Update console environment variables. \r
926\r
927 @param VariableName Console environment variables, ConOutDev, ConInDev\r
928 ErrOutDev, ConIn ,ConOut or ErrOut.\r
929 @param DevicePath Console devcie's device path.\r
930 @param Operation Variable operations, including APPEND, CHECK and DELETE.\r
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
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
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
958 if (Operation != Delete) {\r
959 //\r
960 // Match specified DevicePath in Console Variable.\r
961 // \r
962 Status = ConPlatformMatchDevicePaths (\r
963 VariableDevicePath,\r
964 DevicePath,\r
965 NULL,\r
966 FALSE\r
967 );\r
968\r
969 if ((Operation == Check) || (!EFI_ERROR (Status))) {\r
970 //\r
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
974 //\r
975 if (VariableDevicePath != NULL) {\r
976 FreePool (VariableDevicePath);\r
977 }\r
978\r
979 return Status;\r
980 }\r
981 //\r
982 // We reach here to append a device path that does not exist in variable.\r
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
995 // We reach here to remove DevicePath from the environment variable that\r
996 // is a multi-instance device path.\r
997 //\r
998 Status = ConPlatformMatchDevicePaths (\r
999 VariableDevicePath,\r
1000 DevicePath,\r
1001 &NewVariableDevicePath,\r
1002 TRUE\r
1003 );\r
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
1014 if (NewVariableDevicePath != NULL) {\r
1015 //\r
1016 // Update Console Environment Variable.\r
1017 //\r
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
1025\r
1026 FreePool (NewVariableDevicePath);\r
1027 }\r
1028\r
1029 return Status;\r
1030}\r
1031\r
1032/**\r
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
1037\r
1038 @param DevicePath Pointer to device's device path.\r
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
1044BOOLEAN\r
1045IsHotPlugDevice (\r
1046 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1047 )\r
1048{\r
1049 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;\r
1050\r
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
1074 }\r
1075\r
1076 return FALSE;\r
1077}\r
1078\r
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
1097 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
1098 UINTN EntryCount;\r
1099 UINTN Index;\r
1100 EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath;\r
1101 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1102\r
1103 //\r
1104 // Check whether it's a GOP device.\r
1105 //\r
1106 TempDevicePath = DevicePath;\r
1107 Status = gBS->LocateDevicePath (&gEfiGraphicsOutputProtocolGuid, &TempDevicePath, &GopHandle);\r
1108 if (EFI_ERROR (Status)) {\r
1109 return FALSE;\r
1110 }\r
1111 //\r
1112 // Get the parent PciIo handle in order to find all the children\r
1113 //\r
1114 Status = gBS->LocateDevicePath (&gEfiPciIoProtocolGuid, &DevicePath, &PciHandle);\r
1115 if (EFI_ERROR (Status)) {\r
1116 return FALSE;\r
1117 }\r
1118\r
1119 Status = gBS->OpenProtocolInformation (\r
1120 PciHandle,\r
1121 &gEfiPciIoProtocolGuid,\r
1122 &OpenInfoBuffer,\r
1123 &EntryCount\r
1124 );\r
1125 if (EFI_ERROR (Status)) {\r
1126 return FALSE;\r
1127 }\r
1128\r
1129 for (Index = 0; Index < EntryCount; Index++) {\r
1130 //\r
1131 // Query all the children created by the GOP driver\r
1132 //\r
1133 if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
1134 Status = gBS->OpenProtocol (\r
1135 OpenInfoBuffer[Index].ControllerHandle,\r
1136 &gEfiDevicePathProtocolGuid,\r
1137 (VOID **) &ChildDevicePath,\r
1138 NULL,\r
1139 NULL,\r
1140 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1141 );\r
1142 if (!EFI_ERROR (Status)) {\r
1143 //\r
1144 // Append the device path to ConOutDev and ErrOutDev\r
1145 //\r
1146 ConPlatformUpdateDeviceVariable (L"ConOutDev", ChildDevicePath, Append);\r
1147 ConPlatformUpdateDeviceVariable (L"ErrOutDev", ChildDevicePath, Append);\r
1148 }\r
1149 }\r
1150 }\r
1151 FreePool (OpenInfoBuffer);\r
1152\r
1153 return TRUE;\r
1154}\r