2 Library functions which contain all the code to connect console device.
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "InternalBm.h"
12 CHAR16
*mConVarName
[] = {
22 Search out the video controller.
24 @return PCI device path of the video controller.
27 BmGetVideoController (
32 UINTN RootBridgeHandleCount
;
33 EFI_HANDLE
*RootBridgeHandleBuffer
;
35 EFI_HANDLE
*HandleBuffer
;
36 UINTN RootBridgeIndex
;
38 EFI_HANDLE VideoController
;
39 EFI_PCI_IO_PROTOCOL
*PciIo
;
43 // Make all the PCI_IO protocols show up
45 Status
= gBS
->LocateHandleBuffer (
47 &gEfiPciRootBridgeIoProtocolGuid
,
49 &RootBridgeHandleCount
,
50 &RootBridgeHandleBuffer
52 if (EFI_ERROR (Status
) || (RootBridgeHandleCount
== 0)) {
56 VideoController
= NULL
;
57 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
58 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
61 // Start to check all the pci io to find the first video controller
63 Status
= gBS
->LocateHandleBuffer (
65 &gEfiPciIoProtocolGuid
,
70 if (EFI_ERROR (Status
)) {
74 for (Index
= 0; Index
< HandleCount
; Index
++) {
75 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiPciIoProtocolGuid
, (VOID
**) &PciIo
);
76 if (!EFI_ERROR (Status
)) {
78 // Check for all video controller
80 Status
= PciIo
->Pci
.Read (
84 sizeof (Pci
) / sizeof (UINT32
),
87 if (!EFI_ERROR (Status
) && IS_PCI_VGA (&Pci
)) {
88 // TODO: use IS_PCI_DISPLAY??
89 VideoController
= HandleBuffer
[Index
];
94 FreePool (HandleBuffer
);
96 if (VideoController
!= NULL
) {
100 FreePool (RootBridgeHandleBuffer
);
102 return VideoController
;
106 Query all the children of VideoController and return the device paths of all the
107 children that support GraphicsOutput protocol.
109 @param VideoController PCI handle of video controller.
111 @return Device paths of all the children that support GraphicsOutput protocol.
113 EFI_DEVICE_PATH_PROTOCOL
*
115 EfiBootManagerGetGopDevicePath (
116 IN EFI_HANDLE VideoController
121 EFI_GUID
**ProtocolBuffer
;
122 UINTN ProtocolBufferCount
;
124 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
126 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
127 EFI_DEVICE_PATH_PROTOCOL
*Next
;
128 EFI_DEVICE_PATH_PROTOCOL
*Previous
;
129 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
130 EFI_DEVICE_PATH_PROTOCOL
*GopPool
;
131 EFI_DEVICE_PATH_PROTOCOL
*ReturnDevicePath
;
134 Status
= gBS
->ProtocolsPerHandle (
139 if (EFI_ERROR (Status
)) {
145 for (ProtocolIndex
= 0; ProtocolIndex
< ProtocolBufferCount
; ProtocolIndex
++) {
146 Status
= gBS
->OpenProtocolInformation (
148 ProtocolBuffer
[ProtocolIndex
],
152 if (EFI_ERROR (Status
)) {
156 for (Index
= 0; Index
< EntryCount
; Index
++) {
158 // Query all the children
160 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
161 Status
= gBS
->OpenProtocol (
162 OpenInfoBuffer
[Index
].ControllerHandle
,
163 &gEfiDevicePathProtocolGuid
,
164 (VOID
**) &DevicePath
,
167 EFI_OPEN_PROTOCOL_GET_PROTOCOL
169 if (EFI_ERROR (Status
)) {
174 for (Next
= DevicePath
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
177 ASSERT (Previous
!= NULL
);
179 if (DevicePathType (Previous
) == ACPI_DEVICE_PATH
&& DevicePathSubType (Previous
) == ACPI_ADR_DP
) {
180 Status
= gBS
->OpenProtocol (
181 OpenInfoBuffer
[Index
].ControllerHandle
,
182 &gEfiGraphicsOutputProtocolGuid
,
186 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
188 if (!EFI_ERROR (Status
)) {
190 // Append the device path to GOP pool when there is GOP protocol installed.
192 TempDevicePath
= GopPool
;
193 GopPool
= AppendDevicePathInstance (GopPool
, DevicePath
);
194 gBS
->FreePool (TempDevicePath
);
198 if (DevicePathType (Previous
) == HARDWARE_DEVICE_PATH
&& DevicePathSubType (Previous
) == HW_CONTROLLER_DP
) {
200 // Recursively look for GOP child in this frame buffer handle
202 DEBUG ((EFI_D_INFO
, "[Bds] Looking for GOP child deeper ... \n"));
203 TempDevicePath
= GopPool
;
204 ReturnDevicePath
= EfiBootManagerGetGopDevicePath (OpenInfoBuffer
[Index
].ControllerHandle
);
205 GopPool
= AppendDevicePathInstance (GopPool
, ReturnDevicePath
);
206 gBS
->FreePool (ReturnDevicePath
);
207 gBS
->FreePool (TempDevicePath
);
212 FreePool (OpenInfoBuffer
);
215 FreePool (ProtocolBuffer
);
221 Connect the platform active active video controller.
223 @param VideoController PCI handle of video controller.
225 @retval EFI_NOT_FOUND There is no active video controller.
226 @retval EFI_SUCCESS The video controller is connected.
230 EfiBootManagerConnectVideoController (
231 EFI_HANDLE VideoController OPTIONAL
234 EFI_DEVICE_PATH_PROTOCOL
*Gop
;
236 if (VideoController
== NULL
) {
238 // Get the platform vga device
240 VideoController
= BmGetVideoController ();
243 if (VideoController
== NULL
) {
244 return EFI_NOT_FOUND
;
248 // Try to connect the PCI device path, so that GOP driver could start on this
249 // device and create child handles with GraphicsOutput Protocol installed
250 // on them, then we get device paths of these child handles and select
251 // them as possible console device.
253 gBS
->ConnectController (VideoController
, NULL
, NULL
, FALSE
);
255 Gop
= EfiBootManagerGetGopDevicePath (VideoController
);
257 return EFI_NOT_FOUND
;
260 EfiBootManagerUpdateConsoleVariable (ConOut
, Gop
, NULL
);
264 // Necessary for ConPlatform and ConSplitter driver to start up again after ConOut is updated.
266 return gBS
->ConnectController (VideoController
, NULL
, NULL
, TRUE
);
270 Fill console handle in System Table if there are no valid console handle in.
272 Firstly, check the validation of console handle in System Table. If it is invalid,
273 update it by the first console device handle from EFI console variable.
275 @param VarName The name of the EFI console variable.
276 @param ConsoleGuid Specified Console protocol GUID.
277 @param ConsoleHandle On IN, console handle in System Table to be checked.
278 On OUT, new console handle in system table.
279 @param ProtocolInterface On IN, console protocol on console handle in System Table to be checked.
280 On OUT, new console protocol on new console handle in system table.
282 @retval TRUE System Table has been updated.
283 @retval FALSE System Table hasn't been updated.
287 BmUpdateSystemTableConsole (
289 IN EFI_GUID
*ConsoleGuid
,
290 IN OUT EFI_HANDLE
*ConsoleHandle
,
291 IN OUT VOID
**ProtocolInterface
295 UINTN DevicePathSize
;
296 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
297 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
298 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
299 EFI_DEVICE_PATH_PROTOCOL
*FullInstance
;
301 EFI_HANDLE NewHandle
;
302 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
304 ASSERT (VarName
!= NULL
);
305 ASSERT (ConsoleHandle
!= NULL
);
306 ASSERT (ConsoleGuid
!= NULL
);
307 ASSERT (ProtocolInterface
!= NULL
);
309 if (*ConsoleHandle
!= NULL
) {
310 Status
= gBS
->HandleProtocol (
315 if (Status
== EFI_SUCCESS
&& Interface
== *ProtocolInterface
) {
317 // If ConsoleHandle is valid and console protocol on this handle also
318 // also matched, just return.
325 // Get all possible consoles device path from EFI variable
327 GetEfiGlobalVariable2 (VarName
, (VOID
**) &VarConsole
, NULL
);
328 if (VarConsole
== NULL
) {
330 // If there is no any console device, just return.
335 FullDevicePath
= VarConsole
;
339 // Check every instance of the console variable
341 Instance
= GetNextDevicePathInstance (&VarConsole
, &DevicePathSize
);
342 if (Instance
== NULL
) {
343 DEBUG ((EFI_D_ERROR
, "[Bds] No valid console instance is found for %s!\n", VarName
));
344 // We should not ASSERT when all the console devices are removed.
345 // ASSERT_EFI_ERROR (EFI_NOT_FOUND);
346 FreePool (FullDevicePath
);
351 // Find console device handle by device path instance
353 FullInstance
= Instance
;
354 Status
= gBS
->LocateDevicePath (
359 FreePool (FullInstance
);
360 if (!EFI_ERROR (Status
)) {
362 // Get the console protocol on this console device handle
364 Status
= gBS
->HandleProtocol (
369 if (!EFI_ERROR (Status
)) {
371 // Update new console handle in System Table.
373 *ConsoleHandle
= NewHandle
;
374 *ProtocolInterface
= Interface
;
375 if (CompareGuid (ConsoleGuid
, &gEfiSimpleTextOutProtocolGuid
)) {
377 // If it is console out device, set console mode 80x25 if current mode is invalid.
379 TextOut
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*) Interface
;
380 if (TextOut
->Mode
->Mode
== -1) {
381 TextOut
->SetMode (TextOut
, 0);
384 FreePool (FullDevicePath
);
389 } while (Instance
!= NULL
);
392 // No any available console devcie found.
394 FreePool (FullDevicePath
);
399 This function updates the console variable based on ConVarName. It can
400 add or remove one specific console device path from the variable
402 @param ConsoleType ConIn, ConOut, ErrOut, ConInDev, ConOutDev or ErrOutDev.
403 @param CustomizedConDevicePath The console device path to be added to
404 the console variable. Cannot be multi-instance.
405 @param ExclusiveDevicePath The console device path to be removed
406 from the console variable. Cannot be multi-instance.
408 @retval EFI_UNSUPPORTED The added device path is the same as a removed one.
409 @retval EFI_SUCCESS Successfully added or removed the device path from the
411 @retval others Return status of RT->SetVariable().
416 EfiBootManagerUpdateConsoleVariable (
417 IN CONSOLE_TYPE ConsoleType
,
418 IN EFI_DEVICE_PATH_PROTOCOL
*CustomizedConDevicePath
,
419 IN EFI_DEVICE_PATH_PROTOCOL
*ExclusiveDevicePath
423 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
424 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
425 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
427 if (ConsoleType
>= ARRAY_SIZE (mConVarName
)) {
428 return EFI_INVALID_PARAMETER
;
432 // Notes: check the device path point, here should check
433 // with compare memory
435 if (CustomizedConDevicePath
== ExclusiveDevicePath
) {
436 return EFI_UNSUPPORTED
;
439 // Delete the ExclusiveDevicePath from current default console
441 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**) &VarConsole
, NULL
);
443 // Initialize NewDevicePath
445 NewDevicePath
= VarConsole
;
448 // If ExclusiveDevicePath is even the part of the instance in VarConsole, delete it.
449 // In the end, NewDevicePath is the final device path.
451 if (ExclusiveDevicePath
!= NULL
&& VarConsole
!= NULL
) {
452 NewDevicePath
= BmDelPartMatchInstance (VarConsole
, ExclusiveDevicePath
);
455 // Try to append customized device path to NewDevicePath.
457 if (CustomizedConDevicePath
!= NULL
) {
458 if (!BmMatchDevicePaths (NewDevicePath
, CustomizedConDevicePath
)) {
460 // Check if there is part of CustomizedConDevicePath in NewDevicePath, delete it.
462 NewDevicePath
= BmDelPartMatchInstance (NewDevicePath
, CustomizedConDevicePath
);
464 // In the first check, the default console variable will be _ModuleEntryPoint,
465 // just append current customized device path
467 TempNewDevicePath
= NewDevicePath
;
468 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, CustomizedConDevicePath
);
469 if (TempNewDevicePath
!= NULL
) {
470 FreePool(TempNewDevicePath
);
476 // Finally, Update the variable of the default console by NewDevicePath
478 Status
= gRT
->SetVariable (
479 mConVarName
[ConsoleType
],
480 &gEfiGlobalVariableGuid
,
481 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
482 | ((ConsoleType
< ConInDev
) ? EFI_VARIABLE_NON_VOLATILE
: 0),
483 GetDevicePathSize (NewDevicePath
),
487 if (VarConsole
== NewDevicePath
) {
488 if (VarConsole
!= NULL
) {
489 FreePool(VarConsole
);
492 if (VarConsole
!= NULL
) {
493 FreePool(VarConsole
);
495 if (NewDevicePath
!= NULL
) {
496 FreePool(NewDevicePath
);
505 Connect the console device base on the variable ConsoleType.
507 @param ConsoleType ConIn, ConOut or ErrOut.
509 @retval EFI_NOT_FOUND There is not any console devices connected
511 @retval EFI_SUCCESS Success connect any one instance of the console
512 device path base on the variable ConVarName.
517 EfiBootManagerConnectConsoleVariable (
518 IN CONSOLE_TYPE ConsoleType
522 EFI_DEVICE_PATH_PROTOCOL
*StartDevicePath
;
523 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
524 EFI_DEVICE_PATH_PROTOCOL
*Next
;
525 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
530 if ((ConsoleType
!= ConIn
) && (ConsoleType
!= ConOut
) && (ConsoleType
!= ErrOut
)) {
531 return EFI_INVALID_PARAMETER
;
534 Status
= EFI_SUCCESS
;
539 // Check if the console variable exist
541 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**) &StartDevicePath
, NULL
);
542 if (StartDevicePath
== NULL
) {
543 return EFI_UNSUPPORTED
;
546 CopyOfDevicePath
= StartDevicePath
;
549 // Check every instance of the console variable
551 Instance
= GetNextDevicePathInstance (&CopyOfDevicePath
, &Size
);
552 if (Instance
== NULL
) {
553 FreePool (StartDevicePath
);
554 return EFI_UNSUPPORTED
;
558 while (!IsDevicePathEndType (Next
)) {
559 Next
= NextDevicePathNode (Next
);
562 SetDevicePathEndNode (Next
);
564 // Connect the USB console
565 // USB console device path is a short-form device path that
566 // starts with the first element being a USB WWID
567 // or a USB Class device path
569 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
570 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
) || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
))
572 Status
= BmConnectUsbShortFormDevicePath (Instance
);
573 if (!EFI_ERROR (Status
)) {
577 for (Next
= Instance
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
578 if (DevicePathType (Next
) == ACPI_DEVICE_PATH
&& DevicePathSubType (Next
) == ACPI_ADR_DP
) {
580 } else if (DevicePathType (Next
) == HARDWARE_DEVICE_PATH
&&
581 DevicePathSubType (Next
) == HW_CONTROLLER_DP
&&
582 DevicePathType (NextDevicePathNode (Next
)) == ACPI_DEVICE_PATH
&&
583 DevicePathSubType (NextDevicePathNode (Next
)) == ACPI_ADR_DP
588 if (!IsDevicePathEnd (Next
)) {
590 // For GOP device path, start the video driver with NULL remaining device path
592 SetDevicePathEndNode (Next
);
593 Status
= EfiBootManagerConnectDevicePath (Instance
, &Handle
);
594 if (!EFI_ERROR (Status
)) {
595 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
598 Status
= EfiBootManagerConnectDevicePath (Instance
, NULL
);
600 if (EFI_ERROR (Status
)) {
602 // Delete the instance from the console varialbe
604 EfiBootManagerUpdateConsoleVariable (ConsoleType
, NULL
, Instance
);
610 } while (CopyOfDevicePath
!= NULL
);
612 FreePool (StartDevicePath
);
615 return EFI_NOT_FOUND
;
623 This function will search every input/output device in current system,
624 and make every input/output device as potential console device.
628 EfiBootManagerConnectAllConsoles (
633 EFI_DEVICE_PATH_PROTOCOL
*ConDevicePath
;
635 EFI_HANDLE
*HandleBuffer
;
640 ConDevicePath
= NULL
;
643 // Update all the console variables
645 gBS
->LocateHandleBuffer (
647 &gEfiSimpleTextInProtocolGuid
,
653 for (Index
= 0; Index
< HandleCount
; Index
++) {
654 gBS
->HandleProtocol (
656 &gEfiDevicePathProtocolGuid
,
657 (VOID
**) &ConDevicePath
659 EfiBootManagerUpdateConsoleVariable (ConIn
, ConDevicePath
, NULL
);
662 if (HandleBuffer
!= NULL
) {
663 FreePool(HandleBuffer
);
667 gBS
->LocateHandleBuffer (
669 &gEfiSimpleTextOutProtocolGuid
,
674 for (Index
= 0; Index
< HandleCount
; Index
++) {
675 gBS
->HandleProtocol (
677 &gEfiDevicePathProtocolGuid
,
678 (VOID
**) &ConDevicePath
680 EfiBootManagerUpdateConsoleVariable (ConOut
, ConDevicePath
, NULL
);
681 EfiBootManagerUpdateConsoleVariable (ErrOut
, ConDevicePath
, NULL
);
684 if (HandleBuffer
!= NULL
) {
685 FreePool(HandleBuffer
);
689 // Connect all console variables
691 EfiBootManagerConnectAllDefaultConsoles ();
696 This function will connect all the console devices base on the console
697 device variable ConIn, ConOut and ErrOut.
699 @retval EFI_DEVICE_ERROR All the consoles were not connected due to an error.
700 @retval EFI_SUCCESS Success connect any one instance of the console
701 device path base on the variable ConVarName.
705 EfiBootManagerConnectAllDefaultConsoles (
710 BOOLEAN OneConnected
;
711 BOOLEAN SystemTableUpdated
;
713 OneConnected
= FALSE
;
715 Status
= EfiBootManagerConnectConsoleVariable (ConOut
);
716 if (!EFI_ERROR (Status
)) {
719 PERF_EVENT ("ConOutReady");
722 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
723 if (!EFI_ERROR (Status
)) {
726 PERF_EVENT ("ConInReady");
728 Status
= EfiBootManagerConnectConsoleVariable (ErrOut
);
729 if (!EFI_ERROR (Status
)) {
732 PERF_EVENT ("ErrOutReady");
734 SystemTableUpdated
= FALSE
;
736 // Fill console handles in System Table if no console device assignd.
738 if (BmUpdateSystemTableConsole (L
"ConIn", &gEfiSimpleTextInProtocolGuid
, &gST
->ConsoleInHandle
, (VOID
**) &gST
->ConIn
)) {
739 SystemTableUpdated
= TRUE
;
741 if (BmUpdateSystemTableConsole (L
"ConOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**) &gST
->ConOut
)) {
742 SystemTableUpdated
= TRUE
;
744 if (BmUpdateSystemTableConsole (L
"ErrOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**) &gST
->StdErr
)) {
745 SystemTableUpdated
= TRUE
;
748 if (SystemTableUpdated
) {
750 // Update the CRC32 in the EFI System Table header
753 gBS
->CalculateCrc32 (
760 return OneConnected
? EFI_SUCCESS
: EFI_DEVICE_ERROR
;