2 Library functions which contain all the code to connect console device.
4 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<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
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.
16 #include "InternalBm.h"
18 CHAR16
*mConVarName
[] = {
28 Search out the video controller.
30 @return PCI device path of the video controller.
33 BmGetVideoController (
38 UINTN RootBridgeHandleCount
;
39 EFI_HANDLE
*RootBridgeHandleBuffer
;
41 EFI_HANDLE
*HandleBuffer
;
42 UINTN RootBridgeIndex
;
44 EFI_HANDLE VideoController
;
45 EFI_PCI_IO_PROTOCOL
*PciIo
;
49 // Make all the PCI_IO protocols show up
51 Status
= gBS
->LocateHandleBuffer (
53 &gEfiPciRootBridgeIoProtocolGuid
,
55 &RootBridgeHandleCount
,
56 &RootBridgeHandleBuffer
58 if (EFI_ERROR (Status
) || (RootBridgeHandleCount
== 0)) {
62 VideoController
= NULL
;
63 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
64 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
67 // Start to check all the pci io to find the first video controller
69 Status
= gBS
->LocateHandleBuffer (
71 &gEfiPciIoProtocolGuid
,
76 if (EFI_ERROR (Status
)) {
80 for (Index
= 0; Index
< HandleCount
; Index
++) {
81 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiPciIoProtocolGuid
, (VOID
**) &PciIo
);
82 if (!EFI_ERROR (Status
)) {
84 // Check for all video controller
86 Status
= PciIo
->Pci
.Read (
90 sizeof (Pci
) / sizeof (UINT32
),
93 if (!EFI_ERROR (Status
) && IS_PCI_VGA (&Pci
)) {
94 // TODO: use IS_PCI_DISPLAY??
95 VideoController
= HandleBuffer
[Index
];
100 FreePool (HandleBuffer
);
102 if (VideoController
!= NULL
) {
106 FreePool (RootBridgeHandleBuffer
);
108 return VideoController
;
112 Query all the children of VideoController and return the device paths of all the
113 children that support GraphicsOutput protocol.
115 @param VideoController PCI handle of video controller.
117 @return Device paths of all the children that support GraphicsOutput protocol.
119 EFI_DEVICE_PATH_PROTOCOL
*
121 EfiBootManagerGetGopDevicePath (
122 IN EFI_HANDLE VideoController
127 EFI_GUID
**ProtocolBuffer
;
128 UINTN ProtocolBufferCount
;
130 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
132 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
133 EFI_DEVICE_PATH_PROTOCOL
*Next
;
134 EFI_DEVICE_PATH_PROTOCOL
*Previous
;
135 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
136 EFI_DEVICE_PATH_PROTOCOL
*GopPool
;
137 EFI_DEVICE_PATH_PROTOCOL
*ReturnDevicePath
;
140 Status
= gBS
->ProtocolsPerHandle (
145 if (EFI_ERROR (Status
)) {
151 for (ProtocolIndex
= 0; ProtocolIndex
< ProtocolBufferCount
; ProtocolIndex
++) {
152 Status
= gBS
->OpenProtocolInformation (
154 ProtocolBuffer
[ProtocolIndex
],
158 if (EFI_ERROR (Status
)) {
162 for (Index
= 0; Index
< EntryCount
; Index
++) {
164 // Query all the children
166 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
167 Status
= gBS
->OpenProtocol (
168 OpenInfoBuffer
[Index
].ControllerHandle
,
169 &gEfiDevicePathProtocolGuid
,
170 (VOID
**) &DevicePath
,
173 EFI_OPEN_PROTOCOL_GET_PROTOCOL
175 if (EFI_ERROR (Status
)) {
180 for (Next
= DevicePath
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
183 ASSERT (Previous
!= NULL
);
185 if (DevicePathType (Previous
) == ACPI_DEVICE_PATH
&& DevicePathSubType (Previous
) == ACPI_ADR_DP
) {
186 Status
= gBS
->OpenProtocol (
187 OpenInfoBuffer
[Index
].ControllerHandle
,
188 &gEfiGraphicsOutputProtocolGuid
,
192 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
194 if (!EFI_ERROR (Status
)) {
196 // Append the device path to GOP pool when there is GOP protocol installed.
198 TempDevicePath
= GopPool
;
199 GopPool
= AppendDevicePathInstance (GopPool
, DevicePath
);
200 gBS
->FreePool (TempDevicePath
);
204 if (DevicePathType (Previous
) == HARDWARE_DEVICE_PATH
&& DevicePathSubType (Previous
) == HW_CONTROLLER_DP
) {
206 // Recursively look for GOP child in this frame buffer handle
208 DEBUG ((EFI_D_INFO
, "[Bds] Looking for GOP child deeper ... \n"));
209 TempDevicePath
= GopPool
;
210 ReturnDevicePath
= EfiBootManagerGetGopDevicePath (OpenInfoBuffer
[Index
].ControllerHandle
);
211 GopPool
= AppendDevicePathInstance (GopPool
, ReturnDevicePath
);
212 gBS
->FreePool (ReturnDevicePath
);
213 gBS
->FreePool (TempDevicePath
);
218 FreePool (OpenInfoBuffer
);
221 FreePool (ProtocolBuffer
);
227 Connect the platform active active video controller.
229 @param VideoController PCI handle of video controller.
231 @retval EFI_NOT_FOUND There is no active video controller.
232 @retval EFI_SUCCESS The video controller is connected.
236 EfiBootManagerConnectVideoController (
237 EFI_HANDLE VideoController OPTIONAL
240 EFI_DEVICE_PATH_PROTOCOL
*Gop
;
242 if (VideoController
== NULL
) {
244 // Get the platform vga device
246 VideoController
= BmGetVideoController ();
249 if (VideoController
== NULL
) {
250 return EFI_NOT_FOUND
;
254 // Try to connect the PCI device path, so that GOP dirver could start on this
255 // device and create child handles with GraphicsOutput Protocol installed
256 // on them, then we get device paths of these child handles and select
257 // them as possible console device.
259 gBS
->ConnectController (VideoController
, NULL
, NULL
, FALSE
);
261 Gop
= EfiBootManagerGetGopDevicePath (VideoController
);
263 return EFI_NOT_FOUND
;
266 EfiBootManagerUpdateConsoleVariable (ConOut
, Gop
, NULL
);
270 // Necessary for ConPlatform and ConSplitter driver to start up again after ConOut is updated.
272 return gBS
->ConnectController (VideoController
, NULL
, NULL
, TRUE
);
276 Fill console handle in System Table if there are no valid console handle in.
278 Firstly, check the validation of console handle in System Table. If it is invalid,
279 update it by the first console device handle from EFI console variable.
281 @param VarName The name of the EFI console variable.
282 @param ConsoleGuid Specified Console protocol GUID.
283 @param ConsoleHandle On IN, console handle in System Table to be checked.
284 On OUT, new console handle in system table.
285 @param ProtocolInterface On IN, console protocol on console handle in System Table to be checked.
286 On OUT, new console protocol on new console handle in system table.
288 @retval TRUE System Table has been updated.
289 @retval FALSE System Table hasn't been updated.
293 BmUpdateSystemTableConsole (
295 IN EFI_GUID
*ConsoleGuid
,
296 IN OUT EFI_HANDLE
*ConsoleHandle
,
297 IN OUT VOID
**ProtocolInterface
301 UINTN DevicePathSize
;
302 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
303 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
304 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
305 EFI_DEVICE_PATH_PROTOCOL
*FullInstance
;
307 EFI_HANDLE NewHandle
;
308 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
310 ASSERT (VarName
!= NULL
);
311 ASSERT (ConsoleHandle
!= NULL
);
312 ASSERT (ConsoleGuid
!= NULL
);
313 ASSERT (ProtocolInterface
!= NULL
);
315 if (*ConsoleHandle
!= NULL
) {
316 Status
= gBS
->HandleProtocol (
321 if (Status
== EFI_SUCCESS
&& Interface
== *ProtocolInterface
) {
323 // If ConsoleHandle is valid and console protocol on this handle also
324 // also matched, just return.
331 // Get all possible consoles device path from EFI variable
333 GetEfiGlobalVariable2 (VarName
, (VOID
**) &VarConsole
, NULL
);
334 if (VarConsole
== NULL
) {
336 // If there is no any console device, just return.
341 FullDevicePath
= VarConsole
;
345 // Check every instance of the console variable
347 Instance
= GetNextDevicePathInstance (&VarConsole
, &DevicePathSize
);
348 if (Instance
== NULL
) {
349 DEBUG ((EFI_D_ERROR
, "[Bds] No valid console instance is found for %s!\n", VarName
));
350 // We should not ASSERT when all the console devices are removed.
351 // ASSERT_EFI_ERROR (EFI_NOT_FOUND);
352 FreePool (FullDevicePath
);
357 // Find console device handle by device path instance
359 FullInstance
= Instance
;
360 Status
= gBS
->LocateDevicePath (
365 FreePool (FullInstance
);
366 if (!EFI_ERROR (Status
)) {
368 // Get the console protocol on this console device handle
370 Status
= gBS
->HandleProtocol (
375 if (!EFI_ERROR (Status
)) {
377 // Update new console handle in System Table.
379 *ConsoleHandle
= NewHandle
;
380 *ProtocolInterface
= Interface
;
381 if (CompareGuid (ConsoleGuid
, &gEfiSimpleTextOutProtocolGuid
)) {
383 // If it is console out device, set console mode 80x25 if current mode is invalid.
385 TextOut
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*) Interface
;
386 if (TextOut
->Mode
->Mode
== -1) {
387 TextOut
->SetMode (TextOut
, 0);
390 FreePool (FullDevicePath
);
395 } while (Instance
!= NULL
);
398 // No any available console devcie found.
400 FreePool (FullDevicePath
);
405 This function updates the console variable based on ConVarName. It can
406 add or remove one specific console device path from the variable
408 @param ConsoleType ConIn, ConOut, ErrOut, ConInDev, ConOutDev or ErrOutDev.
409 @param CustomizedConDevicePath The console device path to be added to
410 the console variable. Cannot be multi-instance.
411 @param ExclusiveDevicePath The console device path to be removed
412 from the console variable. Cannot be multi-instance.
414 @retval EFI_UNSUPPORTED The added device path is the same as a removed one.
415 @retval EFI_SUCCESS Successfully added or removed the device path from the
417 @retval others Return status of RT->SetVariable().
422 EfiBootManagerUpdateConsoleVariable (
423 IN CONSOLE_TYPE ConsoleType
,
424 IN EFI_DEVICE_PATH_PROTOCOL
*CustomizedConDevicePath
,
425 IN EFI_DEVICE_PATH_PROTOCOL
*ExclusiveDevicePath
429 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
430 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
431 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
433 if (ConsoleType
>= sizeof (mConVarName
) / sizeof (mConVarName
[0])) {
434 return EFI_INVALID_PARAMETER
;
438 // Notes: check the device path point, here should check
439 // with compare memory
441 if (CustomizedConDevicePath
== ExclusiveDevicePath
) {
442 return EFI_UNSUPPORTED
;
445 // Delete the ExclusiveDevicePath from current default console
447 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**) &VarConsole
, NULL
);
449 // Initialize NewDevicePath
451 NewDevicePath
= VarConsole
;
454 // If ExclusiveDevicePath is even the part of the instance in VarConsole, delete it.
455 // In the end, NewDevicePath is the final device path.
457 if (ExclusiveDevicePath
!= NULL
&& VarConsole
!= NULL
) {
458 NewDevicePath
= BmDelPartMatchInstance (VarConsole
, ExclusiveDevicePath
);
461 // Try to append customized device path to NewDevicePath.
463 if (CustomizedConDevicePath
!= NULL
) {
464 if (!BmMatchDevicePaths (NewDevicePath
, CustomizedConDevicePath
)) {
466 // Check if there is part of CustomizedConDevicePath in NewDevicePath, delete it.
468 NewDevicePath
= BmDelPartMatchInstance (NewDevicePath
, CustomizedConDevicePath
);
470 // In the first check, the default console variable will be _ModuleEntryPoint,
471 // just append current customized device path
473 TempNewDevicePath
= NewDevicePath
;
474 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, CustomizedConDevicePath
);
475 if (TempNewDevicePath
!= NULL
) {
476 FreePool(TempNewDevicePath
);
482 // Finally, Update the variable of the default console by NewDevicePath
484 Status
= gRT
->SetVariable (
485 mConVarName
[ConsoleType
],
486 &gEfiGlobalVariableGuid
,
487 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
488 | ((ConsoleType
< ConInDev
) ? EFI_VARIABLE_NON_VOLATILE
: 0),
489 GetDevicePathSize (NewDevicePath
),
493 if (VarConsole
== NewDevicePath
) {
494 if (VarConsole
!= NULL
) {
495 FreePool(VarConsole
);
498 if (VarConsole
!= NULL
) {
499 FreePool(VarConsole
);
501 if (NewDevicePath
!= NULL
) {
502 FreePool(NewDevicePath
);
511 Connect the console device base on the variable ConsoleType.
513 @param ConsoleType ConIn, ConOut or ErrOut.
515 @retval EFI_NOT_FOUND There is not any console devices connected
517 @retval EFI_SUCCESS Success connect any one instance of the console
518 device path base on the variable ConVarName.
523 EfiBootManagerConnectConsoleVariable (
524 IN CONSOLE_TYPE ConsoleType
528 EFI_DEVICE_PATH_PROTOCOL
*StartDevicePath
;
529 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
530 EFI_DEVICE_PATH_PROTOCOL
*Next
;
531 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
536 if ((ConsoleType
!= ConIn
) && (ConsoleType
!= ConOut
) && (ConsoleType
!= ErrOut
)) {
537 return EFI_INVALID_PARAMETER
;
540 Status
= EFI_SUCCESS
;
545 // Check if the console variable exist
547 GetEfiGlobalVariable2 (mConVarName
[ConsoleType
], (VOID
**) &StartDevicePath
, NULL
);
548 if (StartDevicePath
== NULL
) {
549 return EFI_UNSUPPORTED
;
552 CopyOfDevicePath
= StartDevicePath
;
555 // Check every instance of the console variable
557 Instance
= GetNextDevicePathInstance (&CopyOfDevicePath
, &Size
);
558 if (Instance
== NULL
) {
559 FreePool (StartDevicePath
);
560 return EFI_UNSUPPORTED
;
564 while (!IsDevicePathEndType (Next
)) {
565 Next
= NextDevicePathNode (Next
);
568 SetDevicePathEndNode (Next
);
570 // Connect the USB console
571 // USB console device path is a short-form device path that
572 // starts with the first element being a USB WWID
573 // or a USB Class device path
575 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
576 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
) || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
))
578 Status
= BmConnectUsbShortFormDevicePath (Instance
);
579 if (!EFI_ERROR (Status
)) {
583 for (Next
= Instance
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
584 if (DevicePathType (Next
) == ACPI_DEVICE_PATH
&& DevicePathSubType (Next
) == ACPI_ADR_DP
) {
586 } else if (DevicePathType (Next
) == HARDWARE_DEVICE_PATH
&&
587 DevicePathSubType (Next
) == HW_CONTROLLER_DP
&&
588 DevicePathType (NextDevicePathNode (Next
)) == ACPI_DEVICE_PATH
&&
589 DevicePathSubType (NextDevicePathNode (Next
)) == ACPI_ADR_DP
594 if (!IsDevicePathEnd (Next
)) {
596 // For GOP device path, start the video driver with NULL remaining device path
598 SetDevicePathEndNode (Next
);
599 Status
= EfiBootManagerConnectDevicePath (Instance
, &Handle
);
600 if (!EFI_ERROR (Status
)) {
601 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
604 Status
= EfiBootManagerConnectDevicePath (Instance
, NULL
);
606 if (EFI_ERROR (Status
)) {
608 // Delete the instance from the console varialbe
610 EfiBootManagerUpdateConsoleVariable (ConsoleType
, NULL
, Instance
);
616 } while (CopyOfDevicePath
!= NULL
);
618 FreePool (StartDevicePath
);
621 return EFI_NOT_FOUND
;
629 This function will search every input/output device in current system,
630 and make every input/output device as potential console device.
634 EfiBootManagerConnectAllConsoles (
639 EFI_DEVICE_PATH_PROTOCOL
*ConDevicePath
;
641 EFI_HANDLE
*HandleBuffer
;
646 ConDevicePath
= NULL
;
649 // Update all the console variables
651 gBS
->LocateHandleBuffer (
653 &gEfiSimpleTextInProtocolGuid
,
659 for (Index
= 0; Index
< HandleCount
; Index
++) {
660 gBS
->HandleProtocol (
662 &gEfiDevicePathProtocolGuid
,
663 (VOID
**) &ConDevicePath
665 EfiBootManagerUpdateConsoleVariable (ConIn
, ConDevicePath
, NULL
);
668 if (HandleBuffer
!= NULL
) {
669 FreePool(HandleBuffer
);
673 gBS
->LocateHandleBuffer (
675 &gEfiSimpleTextOutProtocolGuid
,
680 for (Index
= 0; Index
< HandleCount
; Index
++) {
681 gBS
->HandleProtocol (
683 &gEfiDevicePathProtocolGuid
,
684 (VOID
**) &ConDevicePath
686 EfiBootManagerUpdateConsoleVariable (ConOut
, ConDevicePath
, NULL
);
687 EfiBootManagerUpdateConsoleVariable (ErrOut
, ConDevicePath
, NULL
);
690 if (HandleBuffer
!= NULL
) {
691 FreePool(HandleBuffer
);
695 // Connect all console variables
697 EfiBootManagerConnectAllDefaultConsoles ();
702 This function will connect all the console devices base on the console
703 device variable ConIn, ConOut and ErrOut.
705 @retval EFI_DEVICE_ERROR All the consoles were not connected due to an error.
706 @retval EFI_SUCCESS Success connect any one instance of the console
707 device path base on the variable ConVarName.
711 EfiBootManagerConnectAllDefaultConsoles (
716 BOOLEAN OneConnected
;
717 BOOLEAN SystemTableUpdated
;
719 OneConnected
= FALSE
;
721 Status
= EfiBootManagerConnectConsoleVariable (ConOut
);
722 if (!EFI_ERROR (Status
)) {
725 PERF_START (NULL
, "ConOutReady", "BDS", 1);
726 PERF_END (NULL
, "ConOutReady", "BDS", 0);
729 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
730 if (!EFI_ERROR (Status
)) {
733 PERF_START (NULL
, "ConInReady", "BDS", 1);
734 PERF_END (NULL
, "ConInReady", "BDS", 0);
736 Status
= EfiBootManagerConnectConsoleVariable (ErrOut
);
737 if (!EFI_ERROR (Status
)) {
740 PERF_START (NULL
, "ErrOutReady", "BDS", 1);
741 PERF_END (NULL
, "ErrOutReady", "BDS", 0);
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
;
750 if (BmUpdateSystemTableConsole (L
"ConOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**) &gST
->ConOut
)) {
751 SystemTableUpdated
= TRUE
;
753 if (BmUpdateSystemTableConsole (L
"ErrOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**) &gST
->StdErr
)) {
754 SystemTableUpdated
= TRUE
;
757 if (SystemTableUpdated
) {
759 // Update the CRC32 in the EFI System Table header
762 gBS
->CalculateCrc32 (
769 return OneConnected
? EFI_SUCCESS
: EFI_DEVICE_ERROR
;