2 This module produce main entry for BDS phase - BdsEntry.
3 When this module was dispatched by DxeCore, gEfiBdsArchProtocolGuid will be installed
4 which contains interface of BdsEntry.
5 After DxeCore finish DXE phase, gEfiBdsArchProtocolGuid->BdsEntry will be invoked
8 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
9 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
10 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
17 #include "HwErrRecSupport.h"
19 #define SET_BOOT_OPTION_SUPPORT_KEY_COUNT(a, c) { \
20 (a) = ((a) & ~EFI_BOOT_OPTION_SUPPORT_COUNT) | (((c) << LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT)) & EFI_BOOT_OPTION_SUPPORT_COUNT); \
24 /// BDS arch protocol instance initial value.
26 EFI_BDS_ARCH_PROTOCOL gBds
= {
31 // gConnectConInEvent - Event which is signaled when ConIn connection is required
33 EFI_EVENT gConnectConInEvent
= NULL
;
36 /// The read-only variables defined in UEFI Spec.
38 CHAR16
*mReadOnlyVariables
[] = {
39 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
,
40 EFI_LANG_CODES_VARIABLE_NAME
,
41 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
42 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME
,
43 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
46 CHAR16
*mBdsLoadOptionName
[] = {
54 Event to Connect ConIn.
56 @param Event Event whose notification function is being invoked.
57 @param Context Pointer to the notification function's context,
58 which is implementation-dependent.
63 BdsDxeOnConnectConInCallBack (
71 // When Osloader call ReadKeyStroke to signal this event
72 // no driver dependency is assumed existing. So use a non-dispatch version
74 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
75 if (EFI_ERROR (Status
)) {
77 // Should not enter this case, if enter, the keyboard will not work.
78 // May need platfrom policy to connect keyboard.
80 DEBUG ((EFI_D_WARN
, "[Bds] Connect ConIn failed - %r!!!\n", Status
));
84 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
85 check whether there is remaining deferred load images.
87 @param[in] Event The Event that is being processed.
88 @param[in] Context The Event Context.
93 CheckDeferredLoadImageOnReadyToBoot (
99 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL
*DeferredImage
;
104 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
108 CHAR16
*DevicePathStr
;
111 // Find all the deferred image load protocols.
115 Status
= gBS
->LocateHandleBuffer (
117 &gEfiDeferredImageLoadProtocolGuid
,
122 if (EFI_ERROR (Status
)) {
126 for (Index
= 0; Index
< HandleCount
; Index
++) {
127 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiDeferredImageLoadProtocolGuid
, (VOID
**) &DeferredImage
);
128 if (EFI_ERROR (Status
)) {
132 for (ImageIndex
= 0; ; ImageIndex
++) {
134 // Load all the deferred images in this protocol instance.
136 Status
= DeferredImage
->GetImageInfo (
144 if (EFI_ERROR (Status
)) {
147 DevicePathStr
= ConvertDevicePathToText (ImageDevicePath
, FALSE
, FALSE
);
148 DEBUG ((DEBUG_LOAD
, "[Bds] Image was deferred but not loaded: %s.\n", DevicePathStr
));
149 if (DevicePathStr
!= NULL
) {
150 FreePool (DevicePathStr
);
154 if (Handles
!= NULL
) {
161 Install Boot Device Selection Protocol
163 @param ImageHandle The image handle.
164 @param SystemTable The system table.
166 @retval EFI_SUCEESS BDS has finished initializing.
167 Return the dispatcher and recall BDS.Entry
168 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
174 IN EFI_HANDLE ImageHandle
,
175 IN EFI_SYSTEM_TABLE
*SystemTable
181 // Install protocol interface
184 Status
= gBS
->InstallMultipleProtocolInterfaces (
186 &gEfiBdsArchProtocolGuid
, &gBds
,
189 ASSERT_EFI_ERROR (Status
);
194 // Register notify function to check deferred images on ReadyToBoot Event.
196 Status
= gBS
->CreateEventEx (
199 CheckDeferredLoadImageOnReadyToBoot
,
201 &gEfiEventReadyToBootGuid
,
204 ASSERT_EFI_ERROR (Status
);
210 Function waits for a given event to fire, or for an optional timeout to expire.
212 @param Event The event to wait for
213 @param Timeout An optional timeout value in 100 ns units.
215 @retval EFI_SUCCESS Event fired before Timeout expired.
216 @retval EFI_TIME_OUT Timout expired before Event fired..
220 BdsWaitForSingleEvent (
222 IN UINT64 Timeout OPTIONAL
227 EFI_EVENT TimerEvent
;
228 EFI_EVENT WaitList
[2];
232 // Create a timer event
234 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
235 if (!EFI_ERROR (Status
)) {
237 // Set the timer event
246 // Wait for the original event or the timer
249 WaitList
[1] = TimerEvent
;
250 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
251 ASSERT_EFI_ERROR (Status
);
252 gBS
->CloseEvent (TimerEvent
);
255 // If the timer expired, change the return to timed out
258 Status
= EFI_TIMEOUT
;
263 // No timeout... just wait on the event
265 Status
= gBS
->WaitForEvent (1, &Event
, &Index
);
266 ASSERT (!EFI_ERROR (Status
));
274 The function reads user inputs.
285 if (PcdGetBool (PcdConInConnectOnDemand
)) {
289 while (gST
->ConIn
!= NULL
) {
291 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
293 if (EFI_ERROR (Status
)) {
303 The function waits for the boot manager timeout expires or hotkey is pressed.
305 It calls PlatformBootManagerWaitCallback each second.
307 @param HotkeyTriggered Input hotkey event.
311 IN EFI_EVENT HotkeyTriggered
315 UINT16 TimeoutRemain
;
317 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait ...Zzzzzzzzzzzz...\n"));
319 TimeoutRemain
= PcdGet16 (PcdPlatformBootTimeOut
);
320 while (TimeoutRemain
!= 0) {
321 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait(%d)..Zzzz...\n", (UINTN
) TimeoutRemain
));
322 PlatformBootManagerWaitCallback (TimeoutRemain
);
324 BdsReadKeys (); // BUGBUG: Only reading can signal HotkeyTriggered
325 // Can be removed after all keyboard drivers invoke callback in timer callback.
327 if (HotkeyTriggered
!= NULL
) {
328 Status
= BdsWaitForSingleEvent (HotkeyTriggered
, EFI_TIMER_PERIOD_SECONDS (1));
329 if (!EFI_ERROR (Status
)) {
333 gBS
->Stall (1000000);
337 // 0xffff means waiting forever
338 // BDS with no hotkey provided and 0xffff as timeout will "hang" in the loop
340 if (TimeoutRemain
!= 0xffff) {
344 DEBUG ((EFI_D_INFO
, "[Bds]Exit the waiting!\n"));
348 Attempt to boot each boot option in the BootOptions array.
350 @param BootOptions Input boot option array.
351 @param BootOptionCount Input boot option count.
352 @param BootManagerMenu Input boot manager menu.
354 @retval TRUE Successfully boot one of the boot options.
355 @retval FALSE Failed boot any of the boot options.
359 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
,
360 IN UINTN BootOptionCount
,
361 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootManagerMenu OPTIONAL
367 // Report Status Code to indicate BDS starts attempting booting from the UEFI BootOrder list.
369 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_ATTEMPT_BOOT_ORDER_EVENT
));
372 // Attempt boot each boot option
374 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
376 // According to EFI Specification, if a load option is not marked
377 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
380 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_ACTIVE
) == 0) {
385 // Boot#### load options with LOAD_OPTION_CATEGORY_APP are executables which are not
386 // part of the normal boot processing. Boot options with reserved category values will be
387 // ignored by the boot manager.
389 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_CATEGORY
) != LOAD_OPTION_CATEGORY_BOOT
) {
394 // All the driver options should have been processed since
395 // now boot will be performed.
397 EfiBootManagerBoot (&BootOptions
[Index
]);
400 // If the boot via Boot#### returns with a status of EFI_SUCCESS, platform firmware
401 // supports boot manager menu, and if firmware is configured to boot in an
402 // interactive mode, the boot manager will stop processing the BootOrder variable and
403 // present a boot manager menu to the user.
405 if ((BootManagerMenu
!= NULL
) && (BootOptions
[Index
].Status
== EFI_SUCCESS
)) {
406 EfiBootManagerBoot (BootManagerMenu
);
411 return (BOOLEAN
) (Index
< BootOptionCount
);
415 The function will load and start every Driver####, SysPrep#### or PlatformRecovery####.
417 @param LoadOptions Load option array.
418 @param LoadOptionCount Load option count.
422 IN EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
,
423 IN UINTN LoadOptionCount
428 BOOLEAN ReconnectAll
;
429 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
431 ReconnectAll
= FALSE
;
432 LoadOptionType
= LoadOptionTypeMax
;
435 // Process the driver option
437 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
439 // All the load options in the array should be of the same type.
442 LoadOptionType
= LoadOptions
[Index
].OptionType
;
444 ASSERT (LoadOptionType
== LoadOptions
[Index
].OptionType
);
445 ASSERT (LoadOptionType
!= LoadOptionTypeBoot
);
447 Status
= EfiBootManagerProcessLoadOption (&LoadOptions
[Index
]);
450 // Status indicates whether the load option is loaded and executed
451 // LoadOptions[Index].Status is what the load option returns
453 if (!EFI_ERROR (Status
)) {
455 // Stop processing if any PlatformRecovery#### returns success.
457 if ((LoadOptions
[Index
].Status
== EFI_SUCCESS
) &&
458 (LoadOptionType
== LoadOptionTypePlatformRecovery
)) {
463 // Only set ReconnectAll flag when the load option executes successfully.
465 if (!EFI_ERROR (LoadOptions
[Index
].Status
) &&
466 (LoadOptions
[Index
].Attributes
& LOAD_OPTION_FORCE_RECONNECT
) != 0) {
473 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
474 // then all of the EFI drivers in the system will be disconnected and
475 // reconnected after the last driver load option is processed.
477 if (ReconnectAll
&& LoadOptionType
== LoadOptionTypeDriver
) {
478 EfiBootManagerDisconnectAll ();
479 EfiBootManagerConnectAll ();
485 Validate input console variable data.
487 If found the device path is not a valid device path, remove the variable.
489 @param VariableName Input console variable name.
493 BdsFormalizeConsoleVariable (
494 IN CHAR16
*VariableName
497 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
501 GetEfiGlobalVariable2 (VariableName
, (VOID
**) &DevicePath
, &VariableSize
);
502 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
503 Status
= gRT
->SetVariable (
505 &gEfiGlobalVariableGuid
,
506 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
511 // Deleting variable with current variable implementation shouldn't fail.
513 ASSERT_EFI_ERROR (Status
);
516 if (DevicePath
!= NULL
) {
517 FreePool (DevicePath
);
522 Formalize OsIndication related variables.
524 For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
525 Delete OsIndications variable if it is not NV/BS/RT UINT64.
527 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
529 Create a boot option for BootManagerMenu if it hasn't been created yet
533 BdsFormalizeOSIndicationVariable (
538 UINT64 OsIndicationSupport
;
542 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
545 // OS indicater support variable
547 Status
= EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
548 if (Status
!= EFI_NOT_FOUND
) {
549 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
| EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
;
550 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
552 OsIndicationSupport
= EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
;
555 Status
= gRT
->SetVariable (
556 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
,
557 &gEfiGlobalVariableGuid
,
558 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
563 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
565 ASSERT_EFI_ERROR (Status
);
568 // If OsIndications is invalid, remove it.
570 // 1. Data size != UINT64
571 // 2. OsIndication value inconsistence
572 // 3. OsIndication attribute inconsistence
576 DataSize
= sizeof(UINT64
);
577 Status
= gRT
->GetVariable (
578 EFI_OS_INDICATIONS_VARIABLE_NAME
,
579 &gEfiGlobalVariableGuid
,
584 if (Status
== EFI_NOT_FOUND
) {
588 if ((DataSize
!= sizeof (OsIndication
)) ||
589 ((OsIndication
& ~OsIndicationSupport
) != 0) ||
590 (Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
))
593 DEBUG ((EFI_D_ERROR
, "[Bds] Unformalized OsIndications variable exists. Delete it\n"));
594 Status
= gRT
->SetVariable (
595 EFI_OS_INDICATIONS_VARIABLE_NAME
,
596 &gEfiGlobalVariableGuid
,
602 // Deleting variable with current variable implementation shouldn't fail.
604 ASSERT_EFI_ERROR(Status
);
614 BdsFormalizeEfiGlobalVariable (
619 // Validate Console variable.
621 BdsFormalizeConsoleVariable (EFI_CON_IN_VARIABLE_NAME
);
622 BdsFormalizeConsoleVariable (EFI_CON_OUT_VARIABLE_NAME
);
623 BdsFormalizeConsoleVariable (EFI_ERR_OUT_VARIABLE_NAME
);
626 // Validate OSIndication related variable.
628 BdsFormalizeOSIndicationVariable ();
633 Service routine for BdsInstance->Entry(). Devices are connected, the
634 consoles are initialized, and the boot options are tried.
636 @param This Protocol Instance structure.
642 IN EFI_BDS_ARCH_PROTOCOL
*This
645 EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
;
646 UINTN LoadOptionCount
;
647 CHAR16
*FirmwareVendor
;
648 EFI_EVENT HotkeyTriggered
;
652 UINT32 BootOptionSupport
;
654 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
656 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption
;
658 CHAR16 BootNextVariableName
[sizeof ("Boot####")];
659 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
661 BOOLEAN PlatformRecovery
;
663 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
664 EFI_STATUS BootManagerMenuStatus
;
666 HotkeyTriggered
= NULL
;
667 Status
= EFI_SUCCESS
;
671 // Insert the performance probe
673 PERF_CROSSMODULE_END("DXE");
674 PERF_CROSSMODULE_BEGIN("BDS");
675 DEBUG ((EFI_D_INFO
, "[Bds] Entry...\n"));
678 // Fill in FirmwareVendor and FirmwareRevision from PCDs
680 FirmwareVendor
= (CHAR16
*) PcdGetPtr (PcdFirmwareVendor
);
681 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
682 ASSERT (gST
->FirmwareVendor
!= NULL
);
683 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
686 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
689 gBS
->CalculateCrc32 ((VOID
*) gST
, sizeof (EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
692 // Validate Variable.
694 BdsFormalizeEfiGlobalVariable ();
697 // Mark the read-only variables if the Variable Lock protocol exists
699 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
700 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
701 if (!EFI_ERROR (Status
)) {
702 for (Index
= 0; Index
< ARRAY_SIZE (mReadOnlyVariables
); Index
++) {
703 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
704 ASSERT_EFI_ERROR (Status
);
708 InitializeHwErrRecSupport ();
711 // Initialize L"Timeout" EFI global variable.
713 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
714 if (BootTimeOut
!= 0xFFFF) {
716 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
717 // define same behavior between no value or 0xFFFF value for L"Timeout".
719 BdsDxeSetVariableAndReportStatusCodeOnError (
720 EFI_TIME_OUT_VARIABLE_NAME
,
721 &gEfiGlobalVariableGuid
,
722 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
729 // Initialize L"BootOptionSupport" EFI global variable.
730 // Lazy-ConIn implictly disables BDS hotkey.
732 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_APP
| EFI_BOOT_OPTION_SUPPORT_SYSPREP
;
733 if (!PcdGetBool (PcdConInConnectOnDemand
)) {
734 BootOptionSupport
|= EFI_BOOT_OPTION_SUPPORT_KEY
;
735 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport
, 3);
737 Status
= gRT
->SetVariable (
738 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
739 &gEfiGlobalVariableGuid
,
740 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
741 sizeof (BootOptionSupport
),
745 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
747 ASSERT_EFI_ERROR (Status
);
750 // Cache the "BootNext" NV variable before calling any PlatformBootManagerLib APIs
751 // This could avoid the "BootNext" set by PlatformBootManagerLib be consumed in this boot.
753 GetEfiGlobalVariable2 (EFI_BOOT_NEXT_VARIABLE_NAME
, (VOID
**) &BootNext
, &DataSize
);
754 if (DataSize
!= sizeof (UINT16
)) {
755 if (BootNext
!= NULL
) {
762 // Initialize the platform language variables
764 InitializeLanguage (TRUE
);
767 // System firmware must include a PlatformRecovery#### variable specifying
768 // a short-form File Path Media Device Path containing the platform default
769 // file path for removable media
771 FilePath
= FileDevicePath (NULL
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
772 Status
= EfiBootManagerInitializeLoadOption (
774 LoadOptionNumberUnassigned
,
775 LoadOptionTypePlatformRecovery
,
777 L
"Default PlatformRecovery",
782 ASSERT_EFI_ERROR (Status
);
783 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypePlatformRecovery
);
784 if (EfiBootManagerFindLoadOption (&LoadOption
, LoadOptions
, LoadOptionCount
) == -1) {
785 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
787 // The PlatformRecovery#### options are sorted by OptionNumber.
788 // Find the the smallest unused number as the new OptionNumber.
790 if (LoadOptions
[Index
].OptionNumber
!= Index
) {
794 LoadOption
.OptionNumber
= Index
;
795 Status
= EfiBootManagerLoadOptionToVariable (&LoadOption
);
796 ASSERT_EFI_ERROR (Status
);
798 EfiBootManagerFreeLoadOption (&LoadOption
);
800 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
803 // Report Status Code to indicate connecting drivers will happen
807 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
811 // Initialize ConnectConIn event before calling platform code.
813 if (PcdGetBool (PcdConInConnectOnDemand
)) {
814 Status
= gBS
->CreateEventEx (
817 BdsDxeOnConnectConInCallBack
,
819 &gConnectConInEventGuid
,
822 if (EFI_ERROR (Status
)) {
823 gConnectConInEvent
= NULL
;
828 // Do the platform init, can be customized by OEM/IBV
829 // Possible things that can be done in PlatformBootManagerBeforeConsole:
830 // > Update console variable: 1. include hot-plug devices; 2. Clear ConIn and add SOL for AMT
831 // > Register new Driver#### or Boot####
832 // > Register new Key####: e.g.: F12
833 // > Signal ReadyToLock event
834 // > Authentication action: 1. connect Auth devices; 2. Identify auto logon user.
836 PERF_INMODULE_BEGIN("PlatformBootManagerBeforeConsole");
837 PlatformBootManagerBeforeConsole ();
838 PERF_INMODULE_END("PlatformBootManagerBeforeConsole");
841 // Initialize hotkey service
843 EfiBootManagerStartHotkeyService (&HotkeyTriggered
);
846 // Execute Driver Options
848 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeDriver
);
849 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
850 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
855 PERF_INMODULE_BEGIN("EfiBootManagerConnectAllDefaultConsoles");
856 if (PcdGetBool (PcdConInConnectOnDemand
)) {
857 EfiBootManagerConnectConsoleVariable (ConOut
);
858 EfiBootManagerConnectConsoleVariable (ErrOut
);
860 // Do not connect ConIn devices when lazy ConIn feature is ON.
863 EfiBootManagerConnectAllDefaultConsoles ();
865 PERF_INMODULE_END("EfiBootManagerConnectAllDefaultConsoles");
868 // Do the platform specific action after the console is ready
869 // Possible things that can be done in PlatformBootManagerAfterConsole:
870 // > Console post action:
871 // > Dynamically switch output mode from 100x31 to 80x25 for certain senarino
872 // > Signal console ready platform customized event
873 // > Run diagnostics like memory testing
874 // > Connect certain devices
875 // > Dispatch aditional option roms
876 // > Special boot: e.g.: USB boot, enter UI
878 PERF_INMODULE_BEGIN("PlatformBootManagerAfterConsole");
879 PlatformBootManagerAfterConsole ();
880 PERF_INMODULE_END("PlatformBootManagerAfterConsole");
883 // If any component set PcdTestKeyUsed to TRUE because use of a test key
884 // was detected, then display a warning message on the debug log and the console
886 if (PcdGetBool (PcdTestKeyUsed
)) {
887 DEBUG ((DEBUG_ERROR
, "**********************************\n"));
888 DEBUG ((DEBUG_ERROR
, "** WARNING: Test Key is used. **\n"));
889 DEBUG ((DEBUG_ERROR
, "**********************************\n"));
890 Print (L
"** WARNING: Test Key is used. **\n");
894 // Boot to Boot Manager Menu when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
896 DataSize
= sizeof (UINT64
);
897 Status
= gRT
->GetVariable (
898 EFI_OS_INDICATIONS_VARIABLE_NAME
,
899 &gEfiGlobalVariableGuid
,
904 if (EFI_ERROR (Status
)) {
909 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
910 DEBUG ((EFI_D_INFO
, "[Bds]OsIndication: %016x\n", OsIndication
));
911 DEBUG ((EFI_D_INFO
, "[Bds]=============Begin Load Options Dumping ...=============\n"));
912 for (LoadOptionType
= 0; LoadOptionType
< LoadOptionTypeMax
; LoadOptionType
++) {
914 EFI_D_INFO
, " %s Options:\n",
915 mBdsLoadOptionName
[LoadOptionType
]
917 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionType
);
918 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
920 EFI_D_INFO
, " %s%04x: %s \t\t 0x%04x\n",
921 mBdsLoadOptionName
[LoadOptionType
],
922 LoadOptions
[Index
].OptionNumber
,
923 LoadOptions
[Index
].Description
,
924 LoadOptions
[Index
].Attributes
927 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
929 DEBUG ((EFI_D_INFO
, "[Bds]=============End Load Options Dumping=============\n"));
933 // BootManagerMenu doesn't contain the correct information when return status is EFI_NOT_FOUND.
935 BootManagerMenuStatus
= EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
937 BootFwUi
= (BOOLEAN
) ((OsIndication
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
) != 0);
938 PlatformRecovery
= (BOOLEAN
) ((OsIndication
& EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
) != 0);
940 // Clear EFI_OS_INDICATIONS_BOOT_TO_FW_UI to acknowledge OS
942 if (BootFwUi
|| PlatformRecovery
) {
943 OsIndication
&= ~((UINT64
) (EFI_OS_INDICATIONS_BOOT_TO_FW_UI
| EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
));
944 Status
= gRT
->SetVariable (
945 EFI_OS_INDICATIONS_VARIABLE_NAME
,
946 &gEfiGlobalVariableGuid
,
947 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
952 // Changing the content without increasing its size with current variable implementation shouldn't fail.
954 ASSERT_EFI_ERROR (Status
);
958 // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
960 if (BootFwUi
&& (BootManagerMenuStatus
!= EFI_NOT_FOUND
)) {
962 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI
964 if (PcdGetBool (PcdConInConnectOnDemand
)) {
965 BdsDxeOnConnectConInCallBack (NULL
, NULL
);
969 // Directly enter the setup page.
971 EfiBootManagerBoot (&BootManagerMenu
);
974 if (!PlatformRecovery
) {
976 // Execute SysPrep####
978 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeSysPrep
);
979 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
980 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
985 PERF_INMODULE_BEGIN ("BdsWait");
986 BdsWait (HotkeyTriggered
);
987 PERF_INMODULE_END ("BdsWait");
989 // BdsReadKeys() can be removed after all keyboard drivers invoke callback in timer callback.
993 EfiBootManagerHotkeyBoot ();
995 if (BootNext
!= NULL
) {
997 // Delete "BootNext" NV variable before transferring control to it to prevent loops.
999 Status
= gRT
->SetVariable (
1000 EFI_BOOT_NEXT_VARIABLE_NAME
,
1001 &gEfiGlobalVariableGuid
,
1007 // Deleting NV variable shouldn't fail unless it doesn't exist.
1009 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_FOUND
);
1012 // Boot to "BootNext"
1014 UnicodeSPrint (BootNextVariableName
, sizeof (BootNextVariableName
), L
"Boot%04x", *BootNext
);
1015 Status
= EfiBootManagerVariableToLoadOption (BootNextVariableName
, &LoadOption
);
1016 if (!EFI_ERROR (Status
)) {
1017 EfiBootManagerBoot (&LoadOption
);
1018 EfiBootManagerFreeLoadOption (&LoadOption
);
1019 if ((LoadOption
.Status
== EFI_SUCCESS
) &&
1020 (BootManagerMenuStatus
!= EFI_NOT_FOUND
) &&
1021 (LoadOption
.OptionNumber
!= BootManagerMenu
.OptionNumber
)) {
1023 // Boot to Boot Manager Menu upon EFI_SUCCESS
1024 // Exception: Do not boot again when the BootNext points to Boot Manager Menu.
1026 EfiBootManagerBoot (&BootManagerMenu
);
1033 // Retry to boot if any of the boot succeeds
1035 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeBoot
);
1036 BootSuccess
= BootBootOptions (LoadOptions
, LoadOptionCount
, (BootManagerMenuStatus
!= EFI_NOT_FOUND
) ? &BootManagerMenu
: NULL
);
1037 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1038 } while (BootSuccess
);
1041 if (BootManagerMenuStatus
!= EFI_NOT_FOUND
) {
1042 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
1046 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypePlatformRecovery
);
1047 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
1048 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1051 DEBUG ((EFI_D_ERROR
, "[Bds] Unable to boot!\n"));
1052 PlatformBootManagerUnableToBoot ();
1057 Set the variable and report the error through status code upon failure.
1059 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1060 Each VariableName is unique for each VendorGuid. VariableName must
1061 contain 1 or more characters. If VariableName is an empty string,
1062 then EFI_INVALID_PARAMETER is returned.
1063 @param VendorGuid A unique identifier for the vendor.
1064 @param Attributes Attributes bitmask to set for the variable.
1065 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1066 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1067 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1068 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1069 the variable value (the timestamp associated with the variable may be updated however
1070 even if no new data value is provided,see the description of the
1071 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1072 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1073 @param Data The contents for the variable.
1075 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1076 defined by the Attributes.
1077 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1078 DataSize exceeds the maximum allowed.
1079 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1080 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1081 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1082 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1083 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1084 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
1085 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.
1087 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1090 BdsDxeSetVariableAndReportStatusCodeOnError (
1091 IN CHAR16
*VariableName
,
1092 IN EFI_GUID
*VendorGuid
,
1093 IN UINT32 Attributes
,
1099 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1102 Status
= gRT
->SetVariable (
1109 if (EFI_ERROR (Status
)) {
1110 NameSize
= StrSize (VariableName
);
1111 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1112 if (SetVariableStatus
!= NULL
) {
1113 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1114 SetVariableStatus
->NameSize
= NameSize
;
1115 SetVariableStatus
->DataSize
= DataSize
;
1116 SetVariableStatus
->SetStatus
= Status
;
1117 SetVariableStatus
->Attributes
= Attributes
;
1118 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1119 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1121 REPORT_STATUS_CODE_EX (
1123 PcdGet32 (PcdErrorCodeSetVariable
),
1126 &gEdkiiStatusCodeDataTypeVariableGuid
,
1128 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1131 FreePool (SetVariableStatus
);