2 Library functions which contain all the code to connect console device.
4 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBm.h"
17 CHAR16
*mConVarName
[] = {
27 Search out the video controller.
29 @return PCI device path of the video controller.
32 BmGetVideoController (
37 UINTN RootBridgeHandleCount
;
38 EFI_HANDLE
*RootBridgeHandleBuffer
;
40 EFI_HANDLE
*HandleBuffer
;
41 UINTN RootBridgeIndex
;
43 EFI_HANDLE VideoController
;
44 EFI_PCI_IO_PROTOCOL
*PciIo
;
48 // Make all the PCI_IO protocols show up
50 Status
= gBS
->LocateHandleBuffer (
52 &gEfiPciRootBridgeIoProtocolGuid
,
54 &RootBridgeHandleCount
,
55 &RootBridgeHandleBuffer
57 if (EFI_ERROR (Status
) || (RootBridgeHandleCount
== 0)) {
61 VideoController
= NULL
;
62 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
63 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
66 // Start to check all the pci io to find the first video controller
68 Status
= gBS
->LocateHandleBuffer (
70 &gEfiPciIoProtocolGuid
,
75 if (EFI_ERROR (Status
)) {
79 for (Index
= 0; Index
< HandleCount
; Index
++) {
80 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiPciIoProtocolGuid
, (VOID
**) &PciIo
);
81 if (!EFI_ERROR (Status
)) {
83 // Check for all video controller
85 Status
= PciIo
->Pci
.Read (
89 sizeof (Pci
) / sizeof (UINT32
),
92 if (!EFI_ERROR (Status
) && IS_PCI_VGA (&Pci
)) {
93 // TODO: use IS_PCI_DISPLAY??
94 VideoController
= HandleBuffer
[Index
];
99 FreePool (HandleBuffer
);
101 if (VideoController
!= NULL
) {
105 FreePool (RootBridgeHandleBuffer
);
107 return VideoController
;
111 Query all the children of VideoController and return the device paths of all the
112 children that support GraphicsOutput protocol.
114 @param VideoController PCI handle of video controller.
116 @return Device paths of all the children that support GraphicsOutput protocol.
118 EFI_DEVICE_PATH_PROTOCOL
*
120 EfiBootManagerGetGopDevicePath (
121 IN EFI_HANDLE VideoController
126 EFI_GUID
**ProtocolBuffer
;
127 UINTN ProtocolBufferCount
;
129 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
131 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
132 EFI_DEVICE_PATH_PROTOCOL
*Next
;
133 EFI_DEVICE_PATH_PROTOCOL
*Previous
;
134 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
135 EFI_DEVICE_PATH_PROTOCOL
*GopPool
;
136 EFI_DEVICE_PATH_PROTOCOL
*ReturnDevicePath
;
139 Status
= gBS
->ProtocolsPerHandle (
144 if (EFI_ERROR (Status
)) {
150 for (ProtocolIndex
= 0; ProtocolIndex
< ProtocolBufferCount
; ProtocolIndex
++) {
151 Status
= gBS
->OpenProtocolInformation (
153 ProtocolBuffer
[ProtocolIndex
],
157 if (EFI_ERROR (Status
)) {
161 for (Index
= 0; Index
< EntryCount
; Index
++) {
163 // Query all the children
165 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
166 Status
= gBS
->OpenProtocol (
167 OpenInfoBuffer
[Index
].ControllerHandle
,
168 &gEfiDevicePathProtocolGuid
,
169 (VOID
**) &DevicePath
,
172 EFI_OPEN_PROTOCOL_GET_PROTOCOL
174 if (EFI_ERROR (Status
)) {
179 for (Next
= DevicePath
; !IsDevicePathEnd (Next
); Next
= NextDevicePathNode (Next
)) {
182 ASSERT (Previous
!= NULL
);
184 if (DevicePathType (Previous
) == ACPI_DEVICE_PATH
&& DevicePathSubType (Previous
) == ACPI_ADR_DP
) {
185 Status
= gBS
->OpenProtocol (
186 OpenInfoBuffer
[Index
].ControllerHandle
,
187 &gEfiGraphicsOutputProtocolGuid
,
191 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
193 if (!EFI_ERROR (Status
)) {
195 // Append the device path to GOP pool when there is GOP protocol installed.
197 TempDevicePath
= GopPool
;
198 GopPool
= AppendDevicePathInstance (GopPool
, DevicePath
);
199 gBS
->FreePool (TempDevicePath
);
203 if (DevicePathType (Previous
) == HARDWARE_DEVICE_PATH
&& DevicePathSubType (Previous
) == HW_CONTROLLER_DP
) {
205 // Recursively look for GOP child in this frame buffer handle
207 DEBUG ((EFI_D_INFO
, "[Bds] Looking for GOP child deeper ... \n"));
208 TempDevicePath
= GopPool
;
209 ReturnDevicePath
= EfiBootManagerGetGopDevicePath (OpenInfoBuffer
[Index
].ControllerHandle
);
210 GopPool
= AppendDevicePathInstance (GopPool
, ReturnDevicePath
);
211 gBS
->FreePool (ReturnDevicePath
);
212 gBS
->FreePool (TempDevicePath
);
217 FreePool (OpenInfoBuffer
);
220 FreePool (ProtocolBuffer
);
226 Connect the platform active active video controller.
228 @param VideoController PCI handle of video controller.
230 @retval EFI_NOT_FOUND There is no active video controller.
231 @retval EFI_SUCCESS The video controller is connected.
235 EfiBootManagerConnectVideoController (
236 EFI_HANDLE VideoController OPTIONAL
239 EFI_DEVICE_PATH_PROTOCOL
*Gop
;
241 if (VideoController
== NULL
) {
243 // Get the platform vga device
245 VideoController
= BmGetVideoController ();
248 if (VideoController
== NULL
) {
249 return EFI_NOT_FOUND
;
253 // Try to connect the PCI device path, so that GOP dirver could start on this
254 // device and create child handles with GraphicsOutput Protocol installed
255 // on them, then we get device paths of these child handles and select
256 // them as possible console device.
258 gBS
->ConnectController (VideoController
, NULL
, NULL
, FALSE
);
260 Gop
= EfiBootManagerGetGopDevicePath (VideoController
);
262 return EFI_NOT_FOUND
;
265 EfiBootManagerUpdateConsoleVariable (ConOut
, Gop
, NULL
);
269 // Necessary for ConPlatform and ConSplitter driver to start up again after ConOut is updated.
271 return gBS
->ConnectController (VideoController
, NULL
, NULL
, TRUE
);
275 Fill console handle in System Table if there are no valid console handle in.
277 Firstly, check the validation of console handle in System Table. If it is invalid,
278 update it by the first console device handle from EFI console variable.
280 @param VarName The name of the EFI console variable.
281 @param ConsoleGuid Specified Console protocol GUID.
282 @param ConsoleHandle On IN, console handle in System Table to be checked.
283 On OUT, new console handle in system table.
284 @param ProtocolInterface On IN, console protocol on console handle in System Table to be checked.
285 On OUT, new console protocol on new console handle in system table.
287 @retval TRUE System Table has been updated.
288 @retval FALSE System Table hasn't been updated.
292 BmUpdateSystemTableConsole (
294 IN EFI_GUID
*ConsoleGuid
,
295 IN OUT EFI_HANDLE
*ConsoleHandle
,
296 IN OUT VOID
**ProtocolInterface
300 UINTN DevicePathSize
;
301 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
302 EFI_DEVICE_PATH_PROTOCOL
*VarConsole
;
303 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
305 EFI_HANDLE NewHandle
;
306 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
308 ASSERT (VarName
!= NULL
);
309 ASSERT (ConsoleHandle
!= NULL
);
310 ASSERT (ConsoleGuid
!= NULL
);
311 ASSERT (ProtocolInterface
!= NULL
);
313 if (*ConsoleHandle
!= NULL
) {
314 Status
= gBS
->HandleProtocol (
319 if (Status
== EFI_SUCCESS
&& Interface
== *ProtocolInterface
) {
321 // If ConsoleHandle is valid and console protocol on this handle also
322 // also matched, just return.
329 // Get all possible consoles device path from EFI variable
331 GetEfiGlobalVariable2 (VarName
, (VOID
**) &VarConsole
, NULL
);
332 if (VarConsole
== NULL
) {
334 // If there is no any console device, just return.
339 FullDevicePath
= VarConsole
;
343 // Check every instance of the console variable
345 Instance
= GetNextDevicePathInstance (&VarConsole
, &DevicePathSize
);
346 if (Instance
== NULL
) {
347 DEBUG ((EFI_D_ERROR
, "[Bds] No valid console instance is found for %s!\n", VarName
));
348 // We should not ASSERT when all the console devices are removed.
349 // ASSERT_EFI_ERROR (EFI_NOT_FOUND);
350 FreePool (FullDevicePath
);
355 // Find console device handle by device path instance
357 Status
= gBS
->LocateDevicePath (
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);
390 } while (Instance
!= NULL
);
393 // No any available console devcie found.
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
>= sizeof (mConVarName
) / sizeof (mConVarName
[0])) {
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.
701 EfiBootManagerConnectAllDefaultConsoles (
705 BOOLEAN SystemTableUpdated
;
707 EfiBootManagerConnectConsoleVariable (ConOut
);
708 PERF_START (NULL
, "ConOutReady", "BDS", 1);
709 PERF_END (NULL
, "ConOutReady", "BDS", 0);
712 EfiBootManagerConnectConsoleVariable (ConIn
);
713 PERF_START (NULL
, "ConInReady", "BDS", 1);
714 PERF_END (NULL
, "ConInReady", "BDS", 0);
717 // The _ModuleEntryPoint err out var is legal.
719 EfiBootManagerConnectConsoleVariable (ErrOut
);
720 PERF_START (NULL
, "ErrOutReady", "BDS", 1);
721 PERF_END (NULL
, "ErrOutReady", "BDS", 0);
723 SystemTableUpdated
= FALSE
;
725 // Fill console handles in System Table if no console device assignd.
727 if (BmUpdateSystemTableConsole (L
"ConIn", &gEfiSimpleTextInProtocolGuid
, &gST
->ConsoleInHandle
, (VOID
**) &gST
->ConIn
)) {
728 SystemTableUpdated
= TRUE
;
730 if (BmUpdateSystemTableConsole (L
"ConOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->ConsoleOutHandle
, (VOID
**) &gST
->ConOut
)) {
731 SystemTableUpdated
= TRUE
;
733 if (BmUpdateSystemTableConsole (L
"ErrOut", &gEfiSimpleTextOutProtocolGuid
, &gST
->StandardErrorHandle
, (VOID
**) &gST
->StdErr
)) {
734 SystemTableUpdated
= TRUE
;
737 if (SystemTableUpdated
) {
739 // Update the CRC32 in the EFI System Table header
742 gBS
->CalculateCrc32 (