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