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