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
];
95 FreePool (HandleBuffer
);
97 if (VideoController
!= NULL
) {
102 FreePool (RootBridgeHandleBuffer
);
104 return VideoController
;
108 Query all the children of VideoController and return the device paths of all the
109 children that support GraphicsOutput protocol.
111 @param VideoController PCI handle of video controller.
113 @return Device paths of all the children that support GraphicsOutput protocol.
115 EFI_DEVICE_PATH_PROTOCOL
*
117 EfiBootManagerGetGopDevicePath (
118 IN EFI_HANDLE VideoController
123 EFI_GUID
**ProtocolBuffer
;
124 UINTN ProtocolBufferCount
;
126 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
128 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
129 EFI_DEVICE_PATH_PROTOCOL
*Next
;
130 EFI_DEVICE_PATH_PROTOCOL
*Previous
;
131 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
132 EFI_DEVICE_PATH_PROTOCOL
*GopPool
;
133 EFI_DEVICE_PATH_PROTOCOL
*ReturnDevicePath
;
135 Status
= gBS
->ProtocolsPerHandle (
140 if (EFI_ERROR (Status
)) {
146 for (ProtocolIndex
= 0; ProtocolIndex
< ProtocolBufferCount
; ProtocolIndex
++) {
147 Status
= gBS
->OpenProtocolInformation (
149 ProtocolBuffer
[ProtocolIndex
],
153 if (EFI_ERROR (Status
)) {
157 for (Index
= 0; Index
< EntryCount
; Index
++) {
159 // Query all the children
161 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
162 Status
= gBS
->OpenProtocol (
163 OpenInfoBuffer
[Index
].ControllerHandle
,
164 &gEfiDevicePathProtocolGuid
,
165 (VOID
**)&DevicePath
,
168 EFI_OPEN_PROTOCOL_GET_PROTOCOL
170 if (EFI_ERROR (Status
)) {
175 for (Next
= DevicePath
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
179 ASSERT (Previous
!= NULL
);
181 if ((DevicePathType (Previous
) == ACPI_DEVICE_PATH
) && (DevicePathSubType (Previous
) == ACPI_ADR_DP
)) {
182 Status
= gBS
->OpenProtocol (
183 OpenInfoBuffer
[Index
].ControllerHandle
,
184 &gEfiGraphicsOutputProtocolGuid
,
188 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
190 if (!EFI_ERROR (Status
)) {
192 // Append the device path to GOP pool when there is GOP protocol installed.
194 TempDevicePath
= GopPool
;
195 GopPool
= AppendDevicePathInstance (GopPool
, DevicePath
);
196 gBS
->FreePool (TempDevicePath
);
200 if ((DevicePathType (Previous
) == HARDWARE_DEVICE_PATH
) && (DevicePathSubType (Previous
) == HW_CONTROLLER_DP
)) {
202 // Recursively look for GOP child in this frame buffer handle
204 DEBUG ((DEBUG_INFO
, "[Bds] Looking for GOP child deeper ... \n"));
205 TempDevicePath
= GopPool
;
206 ReturnDevicePath
= EfiBootManagerGetGopDevicePath (OpenInfoBuffer
[Index
].ControllerHandle
);
207 GopPool
= AppendDevicePathInstance (GopPool
, ReturnDevicePath
);
208 gBS
->FreePool (ReturnDevicePath
);
209 gBS
->FreePool (TempDevicePath
);
214 FreePool (OpenInfoBuffer
);
217 FreePool (ProtocolBuffer
);
223 Connect the platform active active video controller.
225 @param VideoController PCI handle of video controller.
227 @retval EFI_NOT_FOUND There is no active video controller.
228 @retval EFI_SUCCESS The video controller is connected.
232 EfiBootManagerConnectVideoController (
233 EFI_HANDLE VideoController OPTIONAL
236 EFI_DEVICE_PATH_PROTOCOL
*Gop
;
238 if (VideoController
== NULL
) {
240 // Get the platform vga device
242 VideoController
= BmGetVideoController ();
245 if (VideoController
== NULL
) {
246 return EFI_NOT_FOUND
;
250 // Try to connect the PCI device path, so that GOP driver could start on this
251 // device and create child handles with GraphicsOutput Protocol installed
252 // on them, then we get device paths of these child handles and select
253 // them as possible console device.
255 gBS
->ConnectController (VideoController
, NULL
, NULL
, FALSE
);
257 Gop
= EfiBootManagerGetGopDevicePath (VideoController
);
259 return EFI_NOT_FOUND
;
262 EfiBootManagerUpdateConsoleVariable (ConOut
, Gop
, NULL
);
266 // Necessary for ConPlatform and ConSplitter driver to start up again after ConOut is updated.
268 return gBS
->ConnectController (VideoController
, NULL
, NULL
, TRUE
);
272 Fill console handle in System Table if there are no valid console handle in.
274 Firstly, check the validation of console handle in System Table. If it is invalid,
275 update it by the first console device handle from EFI console variable.
277 @param VarName The name of the EFI console variable.
278 @param ConsoleGuid Specified Console protocol GUID.
279 @param ConsoleHandle On IN, console handle in System Table to be checked.
280 On OUT, new console handle in system table.
281 @param ProtocolInterface On IN, console protocol on console handle in System Table to be checked.
282 On OUT, new console protocol on new console handle in system table.
284 @retval TRUE System Table has been updated.
285 @retval FALSE System Table hasn't been updated.
289 BmUpdateSystemTableConsole (
291 IN EFI_GUID
*ConsoleGuid
,
292 IN OUT EFI_HANDLE
*ConsoleHandle
,
293 IN OUT VOID
**ProtocolInterface
297 UINTN DevicePathSize
;
298 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
299 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
300 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
301 EFI_DEVICE_PATH_PROTOCOL
*FullInstance
;
303 EFI_HANDLE NewHandle
;
304 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
306 ASSERT (VarName
!= NULL
);
307 ASSERT (ConsoleHandle
!= NULL
);
308 ASSERT (ConsoleGuid
!= NULL
);
309 ASSERT (ProtocolInterface
!= NULL
);
311 if (*ConsoleHandle
!= NULL
) {
312 Status
= gBS
->HandleProtocol (
317 if ((Status
== EFI_SUCCESS
) && (Interface
== *ProtocolInterface
)) {
319 // If ConsoleHandle is valid and console protocol on this handle also
320 // also matched, just return.
327 // Get all possible consoles device path from EFI variable
329 GetEfiGlobalVariable2 (VarName
, (VOID
**)&VarConsole
, NULL
);
330 if (VarConsole
== NULL
) {
332 // If there is no any console device, just return.
337 FullDevicePath
= VarConsole
;
341 // Check every instance of the console variable
343 Instance
= GetNextDevicePathInstance (&VarConsole
, &DevicePathSize
);
344 if (Instance
== NULL
) {
345 DEBUG ((DEBUG_ERROR
, "[Bds] No valid console instance is found for %s!\n", VarName
));
346 // We should not ASSERT when all the console devices are removed.
347 // ASSERT_EFI_ERROR (EFI_NOT_FOUND);
348 FreePool (FullDevicePath
);
353 // Find console device handle by device path instance
355 FullInstance
= Instance
;
356 Status
= gBS
->LocateDevicePath (
361 FreePool (FullInstance
);
362 if (!EFI_ERROR (Status
)) {
364 // Get the console protocol on this console device handle
366 Status
= gBS
->HandleProtocol (
371 if (!EFI_ERROR (Status
)) {
373 // Update new console handle in System Table.
375 *ConsoleHandle
= NewHandle
;
376 *ProtocolInterface
= Interface
;
377 if (CompareGuid (ConsoleGuid
, &gEfiSimpleTextOutProtocolGuid
)) {
379 // If it is console out device, set console mode 80x25 if current mode is invalid.
381 TextOut
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)Interface
;
382 if (TextOut
->Mode
->Mode
== -1) {
383 TextOut
->SetMode (TextOut
, 0);
387 FreePool (FullDevicePath
);
391 } while (Instance
!= NULL
);
394 // No any available console devcie found.
396 FreePool (FullDevicePath
);
401 This function updates the console variable based on ConVarName. It can
402 add or remove one specific console device path from the variable
404 @param ConsoleType ConIn, ConOut, ErrOut, ConInDev, ConOutDev or ErrOutDev.
405 @param CustomizedConDevicePath The console device path to be added to
406 the console variable. Cannot be multi-instance.
407 @param ExclusiveDevicePath The console device path to be removed
408 from the console variable. Cannot be multi-instance.
410 @retval EFI_UNSUPPORTED The added device path is the same as a removed one.
411 @retval EFI_SUCCESS Successfully added or removed the device path from the
413 @retval others Return status of RT->SetVariable().
418 EfiBootManagerUpdateConsoleVariable (
419 IN CONSOLE_TYPE ConsoleType
,
420 IN EFI_DEVICE_PATH_PROTOCOL
*CustomizedConDevicePath
,
421 IN EFI_DEVICE_PATH_PROTOCOL
*ExclusiveDevicePath
425 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
426 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
427 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
429 if (ConsoleType
>= ARRAY_SIZE (mConVarName
)) {
430 return EFI_INVALID_PARAMETER
;
434 // Notes: check the device path point, here should check
435 // with compare memory
437 if (CustomizedConDevicePath
== ExclusiveDevicePath
) {
438 return EFI_UNSUPPORTED
;
442 // Delete the ExclusiveDevicePath from current default console
444 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**)&VarConsole
, NULL
);
446 // Initialize NewDevicePath
448 NewDevicePath
= VarConsole
;
451 // If ExclusiveDevicePath is even the part of the instance in VarConsole, delete it.
452 // In the end, NewDevicePath is the final device path.
454 if ((ExclusiveDevicePath
!= NULL
) && (VarConsole
!= NULL
)) {
455 NewDevicePath
= BmDelPartMatchInstance (VarConsole
, ExclusiveDevicePath
);
459 // Try to append customized device path to NewDevicePath.
461 if (CustomizedConDevicePath
!= NULL
) {
462 if (!BmMatchDevicePaths (NewDevicePath
, CustomizedConDevicePath
)) {
464 // Check if there is part of CustomizedConDevicePath in NewDevicePath, delete it.
466 NewDevicePath
= BmDelPartMatchInstance (NewDevicePath
, CustomizedConDevicePath
);
468 // In the first check, the default console variable will be _ModuleEntryPoint,
469 // just append current customized device path
471 TempNewDevicePath
= NewDevicePath
;
472 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, CustomizedConDevicePath
);
473 if (TempNewDevicePath
!= NULL
) {
474 FreePool (TempNewDevicePath
);
480 // Finally, Update the variable of the default console by NewDevicePath
482 Status
= gRT
->SetVariable (
483 mConVarName
[ConsoleType
],
484 &gEfiGlobalVariableGuid
,
485 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
486 | ((ConsoleType
< ConInDev
) ? EFI_VARIABLE_NON_VOLATILE
: 0),
487 GetDevicePathSize (NewDevicePath
),
491 if (VarConsole
== NewDevicePath
) {
492 if (VarConsole
!= NULL
) {
493 FreePool (VarConsole
);
496 if (VarConsole
!= NULL
) {
497 FreePool (VarConsole
);
500 if (NewDevicePath
!= NULL
) {
501 FreePool (NewDevicePath
);
509 Connect the console device base on the variable ConsoleType.
511 @param ConsoleType ConIn, ConOut or ErrOut.
513 @retval EFI_NOT_FOUND There is not any console devices connected
515 @retval EFI_SUCCESS Success connect any one instance of the console
516 device path base on the variable ConVarName.
521 EfiBootManagerConnectConsoleVariable (
522 IN CONSOLE_TYPE ConsoleType
526 EFI_DEVICE_PATH_PROTOCOL
*StartDevicePath
;
527 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
528 EFI_DEVICE_PATH_PROTOCOL
*Next
;
529 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
534 if ((ConsoleType
!= ConIn
) && (ConsoleType
!= ConOut
) && (ConsoleType
!= ErrOut
)) {
535 return EFI_INVALID_PARAMETER
;
538 Status
= EFI_SUCCESS
;
543 // Check if the console variable exist
545 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**)&StartDevicePath
, NULL
);
546 if (StartDevicePath
== NULL
) {
547 return EFI_UNSUPPORTED
;
550 CopyOfDevicePath
= StartDevicePath
;
553 // Check every instance of the console variable
555 Instance
= GetNextDevicePathInstance (&CopyOfDevicePath
, &Size
);
556 if (Instance
== NULL
) {
557 FreePool (StartDevicePath
);
558 return EFI_UNSUPPORTED
;
562 while (!IsDevicePathEndType (Next
)) {
563 Next
= NextDevicePathNode (Next
);
566 SetDevicePathEndNode (Next
);
568 // Connect the USB console
569 // USB console device path is a short-form device path that
570 // starts with the first element being a USB WWID
571 // or a USB Class device path
573 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
574 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
) || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
))
577 Status
= BmConnectUsbShortFormDevicePath (Instance
);
578 if (!EFI_ERROR (Status
)) {
582 for (Next
= Instance
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
583 if ((DevicePathType (Next
) == ACPI_DEVICE_PATH
) && (DevicePathSubType (Next
) == ACPI_ADR_DP
)) {
585 } else if ((DevicePathType (Next
) == HARDWARE_DEVICE_PATH
) &&
586 (DevicePathSubType (Next
) == HW_CONTROLLER_DP
) &&
587 (DevicePathType (NextDevicePathNode (Next
)) == ACPI_DEVICE_PATH
) &&
588 (DevicePathSubType (NextDevicePathNode (Next
)) == ACPI_ADR_DP
)
595 if (!IsDevicePathEnd (Next
)) {
597 // For GOP device path, start the video driver with NULL remaining device path
599 SetDevicePathEndNode (Next
);
600 Status
= EfiBootManagerConnectDevicePath (Instance
, &Handle
);
601 if (!EFI_ERROR (Status
)) {
602 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
605 Status
= EfiBootManagerConnectDevicePath (Instance
, NULL
);
608 if (EFI_ERROR (Status
)) {
610 // Delete the instance from the console varialbe
612 EfiBootManagerUpdateConsoleVariable (ConsoleType
, NULL
, Instance
);
619 } while (CopyOfDevicePath
!= NULL
);
621 FreePool (StartDevicePath
);
624 return EFI_NOT_FOUND
;
631 This function will search every input/output device in current system,
632 and make every input/output device as potential console device.
636 EfiBootManagerConnectAllConsoles (
641 EFI_DEVICE_PATH_PROTOCOL
*ConDevicePath
;
643 EFI_HANDLE
*HandleBuffer
;
648 ConDevicePath
= NULL
;
651 // Update all the console variables
653 gBS
->LocateHandleBuffer (
655 &gEfiSimpleTextInProtocolGuid
,
661 for (Index
= 0; Index
< HandleCount
; Index
++) {
662 gBS
->HandleProtocol (
664 &gEfiDevicePathProtocolGuid
,
665 (VOID
**)&ConDevicePath
667 EfiBootManagerUpdateConsoleVariable (ConIn
, ConDevicePath
, NULL
);
670 if (HandleBuffer
!= NULL
) {
671 FreePool (HandleBuffer
);
675 gBS
->LocateHandleBuffer (
677 &gEfiSimpleTextOutProtocolGuid
,
682 for (Index
= 0; Index
< HandleCount
; Index
++) {
683 gBS
->HandleProtocol (
685 &gEfiDevicePathProtocolGuid
,
686 (VOID
**)&ConDevicePath
688 EfiBootManagerUpdateConsoleVariable (ConOut
, ConDevicePath
, NULL
);
689 EfiBootManagerUpdateConsoleVariable (ErrOut
, ConDevicePath
, NULL
);
692 if (HandleBuffer
!= NULL
) {
693 FreePool (HandleBuffer
);
697 // Connect all console variables
699 EfiBootManagerConnectAllDefaultConsoles ();
703 This function will connect all the console devices base on the console
704 device variable ConIn, ConOut and ErrOut.
706 @retval EFI_DEVICE_ERROR All the consoles were not connected due to an error.
707 @retval EFI_SUCCESS Success connect any one instance of the console
708 device path base on the variable ConVarName.
712 EfiBootManagerConnectAllDefaultConsoles (
717 BOOLEAN OneConnected
;
718 BOOLEAN SystemTableUpdated
;
720 OneConnected
= FALSE
;
722 Status
= EfiBootManagerConnectConsoleVariable (ConOut
);
723 if (!EFI_ERROR (Status
)) {
727 PERF_EVENT ("ConOutReady");
729 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
730 if (!EFI_ERROR (Status
)) {
734 PERF_EVENT ("ConInReady");
736 Status
= EfiBootManagerConnectConsoleVariable (ErrOut
);
737 if (!EFI_ERROR (Status
)) {
741 PERF_EVENT ("ErrOutReady");
743 SystemTableUpdated
= FALSE
;
745 // Fill console handles in System Table if no console device assignd.
747 if (BmUpdateSystemTableConsole (L
"ConIn", &gEfiSimpleTextInProtocolGuid
, &gST
->ConsoleInHandle
, (VOID
**)&gST
->ConIn
)) {
748 SystemTableUpdated
= TRUE
;
751 if (BmUpdateSystemTableConsole (L
"ConOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**)&gST
->ConOut
)) {
752 SystemTableUpdated
= TRUE
;
755 if (BmUpdateSystemTableConsole (L
"ErrOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**)&gST
->StdErr
)) {
756 SystemTableUpdated
= TRUE
;
759 if (SystemTableUpdated
) {
761 // Update the CRC32 in the EFI System Table header
764 gBS
->CalculateCrc32 (
771 return OneConnected
? EFI_SUCCESS
: EFI_DEVICE_ERROR
;