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 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include "HwErrRecSupport.h"
25 #define SET_BOOT_OPTION_SUPPORT_KEY_COUNT(a, c) { \
26 (a) = ((a) & ~EFI_BOOT_OPTION_SUPPORT_COUNT) | (((c) << LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT)) & EFI_BOOT_OPTION_SUPPORT_COUNT); \
30 /// BDS arch protocol instance initial value.
32 EFI_BDS_ARCH_PROTOCOL gBds
= {
37 // gConnectConInEvent - Event which is signaled when ConIn connection is required
39 EFI_EVENT gConnectConInEvent
= NULL
;
42 /// The read-only variables defined in UEFI Spec.
44 CHAR16
*mReadOnlyVariables
[] = {
45 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
,
46 EFI_LANG_CODES_VARIABLE_NAME
,
47 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
48 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME
,
49 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
52 CHAR16
*mBdsLoadOptionName
[] = {
60 Event to Connect ConIn.
62 @param Event Event whose notification function is being invoked.
63 @param Context Pointer to the notification function's context,
64 which is implementation-dependent.
69 BdsDxeOnConnectConInCallBack (
77 // When Osloader call ReadKeyStroke to signal this event
78 // no driver dependency is assumed existing. So use a non-dispatch version
80 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
81 if (EFI_ERROR (Status
)) {
83 // Should not enter this case, if enter, the keyboard will not work.
84 // May need platfrom policy to connect keyboard.
86 DEBUG ((EFI_D_WARN
, "[Bds] Connect ConIn failed - %r!!!\n", Status
));
90 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
91 check whether there is remaining deferred load images.
93 @param[in] Event The Event that is being processed.
94 @param[in] Context The Event Context.
99 CheckDeferredLoadImageOnReadyToBoot (
105 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL
*DeferredImage
;
110 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
114 CHAR16
*DevicePathStr
;
117 // Find all the deferred image load protocols.
121 Status
= gBS
->LocateHandleBuffer (
123 &gEfiDeferredImageLoadProtocolGuid
,
128 if (EFI_ERROR (Status
)) {
132 for (Index
= 0; Index
< HandleCount
; Index
++) {
133 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiDeferredImageLoadProtocolGuid
, (VOID
**) &DeferredImage
);
134 if (EFI_ERROR (Status
)) {
138 for (ImageIndex
= 0; ; ImageIndex
++) {
140 // Load all the deferred images in this protocol instance.
142 Status
= DeferredImage
->GetImageInfo (
150 if (EFI_ERROR (Status
)) {
153 DevicePathStr
= ConvertDevicePathToText (ImageDevicePath
, FALSE
, FALSE
);
154 DEBUG ((DEBUG_LOAD
, "[Bds] Image was deferred but not loaded: %s.\n", DevicePathStr
));
155 if (DevicePathStr
!= NULL
) {
156 FreePool (DevicePathStr
);
160 if (Handles
!= NULL
) {
167 Install Boot Device Selection Protocol
169 @param ImageHandle The image handle.
170 @param SystemTable The system table.
172 @retval EFI_SUCEESS BDS has finished initializing.
173 Return the dispatcher and recall BDS.Entry
174 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
180 IN EFI_HANDLE ImageHandle
,
181 IN EFI_SYSTEM_TABLE
*SystemTable
187 // Install protocol interface
190 Status
= gBS
->InstallMultipleProtocolInterfaces (
192 &gEfiBdsArchProtocolGuid
, &gBds
,
195 ASSERT_EFI_ERROR (Status
);
200 // Register notify function to check deferred images on ReadyToBoot Event.
202 Status
= gBS
->CreateEventEx (
205 CheckDeferredLoadImageOnReadyToBoot
,
207 &gEfiEventReadyToBootGuid
,
210 ASSERT_EFI_ERROR (Status
);
216 Function waits for a given event to fire, or for an optional timeout to expire.
218 @param Event The event to wait for
219 @param Timeout An optional timeout value in 100 ns units.
221 @retval EFI_SUCCESS Event fired before Timeout expired.
222 @retval EFI_TIME_OUT Timout expired before Event fired..
226 BdsWaitForSingleEvent (
228 IN UINT64 Timeout OPTIONAL
233 EFI_EVENT TimerEvent
;
234 EFI_EVENT WaitList
[2];
238 // Create a timer event
240 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
241 if (!EFI_ERROR (Status
)) {
243 // Set the timer event
252 // Wait for the original event or the timer
255 WaitList
[1] = TimerEvent
;
256 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
257 ASSERT_EFI_ERROR (Status
);
258 gBS
->CloseEvent (TimerEvent
);
261 // If the timer expired, change the return to timed out
264 Status
= EFI_TIMEOUT
;
269 // No timeout... just wait on the event
271 Status
= gBS
->WaitForEvent (1, &Event
, &Index
);
272 ASSERT (!EFI_ERROR (Status
));
280 The function reads user inputs.
291 if (PcdGetBool (PcdConInConnectOnDemand
)) {
295 while (gST
->ConIn
!= NULL
) {
297 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
299 if (EFI_ERROR (Status
)) {
309 The function waits for the boot manager timeout expires or hotkey is pressed.
311 It calls PlatformBootManagerWaitCallback each second.
313 @param HotkeyTriggered Input hotkey event.
317 IN EFI_EVENT HotkeyTriggered
321 UINT16 TimeoutRemain
;
323 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait ...Zzzzzzzzzzzz...\n"));
325 TimeoutRemain
= PcdGet16 (PcdPlatformBootTimeOut
);
326 while (TimeoutRemain
!= 0) {
327 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait(%d)..Zzzz...\n", (UINTN
) TimeoutRemain
));
328 PlatformBootManagerWaitCallback (TimeoutRemain
);
330 BdsReadKeys (); // BUGBUG: Only reading can signal HotkeyTriggered
331 // Can be removed after all keyboard drivers invoke callback in timer callback.
333 if (HotkeyTriggered
!= NULL
) {
334 Status
= BdsWaitForSingleEvent (HotkeyTriggered
, EFI_TIMER_PERIOD_SECONDS (1));
335 if (!EFI_ERROR (Status
)) {
339 gBS
->Stall (1000000);
343 // 0xffff means waiting forever
344 // BDS with no hotkey provided and 0xffff as timeout will "hang" in the loop
346 if (TimeoutRemain
!= 0xffff) {
350 DEBUG ((EFI_D_INFO
, "[Bds]Exit the waiting!\n"));
354 Attempt to boot each boot option in the BootOptions array.
356 @param BootOptions Input boot option array.
357 @param BootOptionCount Input boot option count.
358 @param BootManagerMenu Input boot manager menu.
360 @retval TRUE Successfully boot one of the boot options.
361 @retval FALSE Failed boot any of the boot options.
365 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
,
366 IN UINTN BootOptionCount
,
367 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootManagerMenu OPTIONAL
373 // Report Status Code to indicate BDS starts attempting booting from the UEFI BootOrder list.
375 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_ATTEMPT_BOOT_ORDER_EVENT
));
378 // Attempt boot each boot option
380 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
382 // According to EFI Specification, if a load option is not marked
383 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
386 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_ACTIVE
) == 0) {
391 // Boot#### load options with LOAD_OPTION_CATEGORY_APP are executables which are not
392 // part of the normal boot processing. Boot options with reserved category values will be
393 // ignored by the boot manager.
395 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_CATEGORY
) != LOAD_OPTION_CATEGORY_BOOT
) {
400 // All the driver options should have been processed since
401 // now boot will be performed.
403 EfiBootManagerBoot (&BootOptions
[Index
]);
406 // If the boot via Boot#### returns with a status of EFI_SUCCESS, platform firmware
407 // supports boot manager menu, and if firmware is configured to boot in an
408 // interactive mode, the boot manager will stop processing the BootOrder variable and
409 // present a boot manager menu to the user.
411 if ((BootManagerMenu
!= NULL
) && (BootOptions
[Index
].Status
== EFI_SUCCESS
)) {
412 EfiBootManagerBoot (BootManagerMenu
);
417 return (BOOLEAN
) (Index
< BootOptionCount
);
421 The function will load and start every Driver####, SysPrep#### or PlatformRecovery####.
423 @param LoadOptions Load option array.
424 @param LoadOptionCount Load option count.
428 IN EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
,
429 IN UINTN LoadOptionCount
434 BOOLEAN ReconnectAll
;
435 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
437 ReconnectAll
= FALSE
;
438 LoadOptionType
= LoadOptionTypeMax
;
441 // Process the driver option
443 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
445 // All the load options in the array should be of the same type.
448 LoadOptionType
= LoadOptions
[Index
].OptionType
;
450 ASSERT (LoadOptionType
== LoadOptions
[Index
].OptionType
);
451 ASSERT (LoadOptionType
!= LoadOptionTypeBoot
);
453 Status
= EfiBootManagerProcessLoadOption (&LoadOptions
[Index
]);
456 // Status indicates whether the load option is loaded and executed
457 // LoadOptions[Index].Status is what the load option returns
459 if (!EFI_ERROR (Status
)) {
461 // Stop processing if any PlatformRecovery#### returns success.
463 if ((LoadOptions
[Index
].Status
== EFI_SUCCESS
) &&
464 (LoadOptionType
== LoadOptionTypePlatformRecovery
)) {
469 // Only set ReconnectAll flag when the load option executes successfully.
471 if (!EFI_ERROR (LoadOptions
[Index
].Status
) &&
472 (LoadOptions
[Index
].Attributes
& LOAD_OPTION_FORCE_RECONNECT
) != 0) {
479 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
480 // then all of the EFI drivers in the system will be disconnected and
481 // reconnected after the last driver load option is processed.
483 if (ReconnectAll
&& LoadOptionType
== LoadOptionTypeDriver
) {
484 EfiBootManagerDisconnectAll ();
485 EfiBootManagerConnectAll ();
491 Validate input console variable data.
493 If found the device path is not a valid device path, remove the variable.
495 @param VariableName Input console variable name.
499 BdsFormalizeConsoleVariable (
500 IN CHAR16
*VariableName
503 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
507 GetEfiGlobalVariable2 (VariableName
, (VOID
**) &DevicePath
, &VariableSize
);
508 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
509 Status
= gRT
->SetVariable (
511 &gEfiGlobalVariableGuid
,
512 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
517 // Deleting variable with current variable implementation shouldn't fail.
519 ASSERT_EFI_ERROR (Status
);
522 if (DevicePath
!= NULL
) {
523 FreePool (DevicePath
);
528 Formalize OsIndication related variables.
530 For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
531 Delete OsIndications variable if it is not NV/BS/RT UINT64.
533 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
535 Create a boot option for BootManagerMenu if it hasn't been created yet
539 BdsFormalizeOSIndicationVariable (
544 UINT64 OsIndicationSupport
;
548 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
551 // OS indicater support variable
553 Status
= EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
554 if (Status
!= EFI_NOT_FOUND
) {
555 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
| EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
;
556 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
558 OsIndicationSupport
= EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
;
561 Status
= gRT
->SetVariable (
562 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
,
563 &gEfiGlobalVariableGuid
,
564 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
569 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
571 ASSERT_EFI_ERROR (Status
);
574 // If OsIndications is invalid, remove it.
576 // 1. Data size != UINT64
577 // 2. OsIndication value inconsistence
578 // 3. OsIndication attribute inconsistence
582 DataSize
= sizeof(UINT64
);
583 Status
= gRT
->GetVariable (
584 EFI_OS_INDICATIONS_VARIABLE_NAME
,
585 &gEfiGlobalVariableGuid
,
590 if (Status
== EFI_NOT_FOUND
) {
594 if ((DataSize
!= sizeof (OsIndication
)) ||
595 ((OsIndication
& ~OsIndicationSupport
) != 0) ||
596 (Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
))
599 DEBUG ((EFI_D_ERROR
, "[Bds] Unformalized OsIndications variable exists. Delete it\n"));
600 Status
= gRT
->SetVariable (
601 EFI_OS_INDICATIONS_VARIABLE_NAME
,
602 &gEfiGlobalVariableGuid
,
608 // Deleting variable with current variable implementation shouldn't fail.
610 ASSERT_EFI_ERROR(Status
);
620 BdsFormalizeEfiGlobalVariable (
625 // Validate Console variable.
627 BdsFormalizeConsoleVariable (EFI_CON_IN_VARIABLE_NAME
);
628 BdsFormalizeConsoleVariable (EFI_CON_OUT_VARIABLE_NAME
);
629 BdsFormalizeConsoleVariable (EFI_ERR_OUT_VARIABLE_NAME
);
632 // Validate OSIndication related variable.
634 BdsFormalizeOSIndicationVariable ();
639 Service routine for BdsInstance->Entry(). Devices are connected, the
640 consoles are initialized, and the boot options are tried.
642 @param This Protocol Instance structure.
648 IN EFI_BDS_ARCH_PROTOCOL
*This
651 EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
;
652 UINTN LoadOptionCount
;
653 CHAR16
*FirmwareVendor
;
654 EFI_EVENT HotkeyTriggered
;
658 UINT32 BootOptionSupport
;
660 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
662 EFI_BOOT_MANAGER_LOAD_OPTION LoadOption
;
664 CHAR16 BootNextVariableName
[sizeof ("Boot####")];
665 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
667 BOOLEAN PlatformRecovery
;
669 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
670 EFI_STATUS BootManagerMenuStatus
;
672 HotkeyTriggered
= NULL
;
673 Status
= EFI_SUCCESS
;
677 // Insert the performance probe
679 PERF_CROSSMODULE_END("DXE");
680 PERF_CROSSMODULE_BEGIN("BDS");
681 DEBUG ((EFI_D_INFO
, "[Bds] Entry...\n"));
684 // Fill in FirmwareVendor and FirmwareRevision from PCDs
686 FirmwareVendor
= (CHAR16
*) PcdGetPtr (PcdFirmwareVendor
);
687 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
688 ASSERT (gST
->FirmwareVendor
!= NULL
);
689 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
692 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
695 gBS
->CalculateCrc32 ((VOID
*) gST
, sizeof (EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
698 // Validate Variable.
700 BdsFormalizeEfiGlobalVariable ();
703 // Mark the read-only variables if the Variable Lock protocol exists
705 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
706 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
707 if (!EFI_ERROR (Status
)) {
708 for (Index
= 0; Index
< ARRAY_SIZE (mReadOnlyVariables
); Index
++) {
709 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
710 ASSERT_EFI_ERROR (Status
);
714 InitializeHwErrRecSupport ();
717 // Initialize L"Timeout" EFI global variable.
719 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
720 if (BootTimeOut
!= 0xFFFF) {
722 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
723 // define same behavior between no value or 0xFFFF value for L"Timeout".
725 BdsDxeSetVariableAndReportStatusCodeOnError (
726 EFI_TIME_OUT_VARIABLE_NAME
,
727 &gEfiGlobalVariableGuid
,
728 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
735 // Initialize L"BootOptionSupport" EFI global variable.
736 // Lazy-ConIn implictly disables BDS hotkey.
738 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_APP
| EFI_BOOT_OPTION_SUPPORT_SYSPREP
;
739 if (!PcdGetBool (PcdConInConnectOnDemand
)) {
740 BootOptionSupport
|= EFI_BOOT_OPTION_SUPPORT_KEY
;
741 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport
, 3);
743 Status
= gRT
->SetVariable (
744 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
745 &gEfiGlobalVariableGuid
,
746 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
747 sizeof (BootOptionSupport
),
751 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
753 ASSERT_EFI_ERROR (Status
);
756 // Cache the "BootNext" NV variable before calling any PlatformBootManagerLib APIs
757 // This could avoid the "BootNext" set by PlatformBootManagerLib be consumed in this boot.
759 GetEfiGlobalVariable2 (EFI_BOOT_NEXT_VARIABLE_NAME
, (VOID
**) &BootNext
, &DataSize
);
760 if (DataSize
!= sizeof (UINT16
)) {
761 if (BootNext
!= NULL
) {
768 // Initialize the platform language variables
770 InitializeLanguage (TRUE
);
773 // System firmware must include a PlatformRecovery#### variable specifying
774 // a short-form File Path Media Device Path containing the platform default
775 // file path for removable media
777 FilePath
= FileDevicePath (NULL
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
778 Status
= EfiBootManagerInitializeLoadOption (
780 LoadOptionNumberUnassigned
,
781 LoadOptionTypePlatformRecovery
,
783 L
"Default PlatformRecovery",
788 ASSERT_EFI_ERROR (Status
);
789 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypePlatformRecovery
);
790 if (EfiBootManagerFindLoadOption (&LoadOption
, LoadOptions
, LoadOptionCount
) == -1) {
791 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
793 // The PlatformRecovery#### options are sorted by OptionNumber.
794 // Find the the smallest unused number as the new OptionNumber.
796 if (LoadOptions
[Index
].OptionNumber
!= Index
) {
800 LoadOption
.OptionNumber
= Index
;
801 Status
= EfiBootManagerLoadOptionToVariable (&LoadOption
);
802 ASSERT_EFI_ERROR (Status
);
804 EfiBootManagerFreeLoadOption (&LoadOption
);
806 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
809 // Report Status Code to indicate connecting drivers will happen
813 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
817 // Initialize ConnectConIn event before calling platform code.
819 if (PcdGetBool (PcdConInConnectOnDemand
)) {
820 Status
= gBS
->CreateEventEx (
823 BdsDxeOnConnectConInCallBack
,
825 &gConnectConInEventGuid
,
828 if (EFI_ERROR (Status
)) {
829 gConnectConInEvent
= NULL
;
834 // Do the platform init, can be customized by OEM/IBV
835 // Possible things that can be done in PlatformBootManagerBeforeConsole:
836 // > Update console variable: 1. include hot-plug devices; 2. Clear ConIn and add SOL for AMT
837 // > Register new Driver#### or Boot####
838 // > Register new Key####: e.g.: F12
839 // > Signal ReadyToLock event
840 // > Authentication action: 1. connect Auth devices; 2. Identify auto logon user.
842 PERF_INMODULE_BEGIN("PlatformBootManagerBeforeConsole");
843 PlatformBootManagerBeforeConsole ();
844 PERF_INMODULE_END("PlatformBootManagerBeforeConsole");
847 // Initialize hotkey service
849 EfiBootManagerStartHotkeyService (&HotkeyTriggered
);
852 // Execute Driver Options
854 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeDriver
);
855 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
856 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
861 PERF_INMODULE_BEGIN("EfiBootManagerConnectAllDefaultConsoles");
862 if (PcdGetBool (PcdConInConnectOnDemand
)) {
863 EfiBootManagerConnectConsoleVariable (ConOut
);
864 EfiBootManagerConnectConsoleVariable (ErrOut
);
866 // Do not connect ConIn devices when lazy ConIn feature is ON.
869 EfiBootManagerConnectAllDefaultConsoles ();
871 PERF_INMODULE_END("EfiBootManagerConnectAllDefaultConsoles");
874 // Do the platform specific action after the console is ready
875 // Possible things that can be done in PlatformBootManagerAfterConsole:
876 // > Console post action:
877 // > Dynamically switch output mode from 100x31 to 80x25 for certain senarino
878 // > Signal console ready platform customized event
879 // > Run diagnostics like memory testing
880 // > Connect certain devices
881 // > Dispatch aditional option roms
882 // > Special boot: e.g.: USB boot, enter UI
884 PERF_INMODULE_BEGIN("PlatformBootManagerAfterConsole");
885 PlatformBootManagerAfterConsole ();
886 PERF_INMODULE_END("PlatformBootManagerAfterConsole");
888 // Boot to Boot Manager Menu when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
890 DataSize
= sizeof (UINT64
);
891 Status
= gRT
->GetVariable (
892 EFI_OS_INDICATIONS_VARIABLE_NAME
,
893 &gEfiGlobalVariableGuid
,
898 if (EFI_ERROR (Status
)) {
903 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
904 DEBUG ((EFI_D_INFO
, "[Bds]OsIndication: %016x\n", OsIndication
));
905 DEBUG ((EFI_D_INFO
, "[Bds]=============Begin Load Options Dumping ...=============\n"));
906 for (LoadOptionType
= 0; LoadOptionType
< LoadOptionTypeMax
; LoadOptionType
++) {
908 EFI_D_INFO
, " %s Options:\n",
909 mBdsLoadOptionName
[LoadOptionType
]
911 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionType
);
912 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
914 EFI_D_INFO
, " %s%04x: %s \t\t 0x%04x\n",
915 mBdsLoadOptionName
[LoadOptionType
],
916 LoadOptions
[Index
].OptionNumber
,
917 LoadOptions
[Index
].Description
,
918 LoadOptions
[Index
].Attributes
921 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
923 DEBUG ((EFI_D_INFO
, "[Bds]=============End Load Options Dumping=============\n"));
927 // BootManagerMenu doesn't contain the correct information when return status is EFI_NOT_FOUND.
929 BootManagerMenuStatus
= EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
931 BootFwUi
= (BOOLEAN
) ((OsIndication
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
) != 0);
932 PlatformRecovery
= (BOOLEAN
) ((OsIndication
& EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
) != 0);
934 // Clear EFI_OS_INDICATIONS_BOOT_TO_FW_UI to acknowledge OS
936 if (BootFwUi
|| PlatformRecovery
) {
937 OsIndication
&= ~((UINT64
) (EFI_OS_INDICATIONS_BOOT_TO_FW_UI
| EFI_OS_INDICATIONS_START_PLATFORM_RECOVERY
));
938 Status
= gRT
->SetVariable (
939 EFI_OS_INDICATIONS_VARIABLE_NAME
,
940 &gEfiGlobalVariableGuid
,
941 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
946 // Changing the content without increasing its size with current variable implementation shouldn't fail.
948 ASSERT_EFI_ERROR (Status
);
952 // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
954 if (BootFwUi
&& (BootManagerMenuStatus
!= EFI_NOT_FOUND
)) {
956 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI
958 if (PcdGetBool (PcdConInConnectOnDemand
)) {
959 BdsDxeOnConnectConInCallBack (NULL
, NULL
);
963 // Directly enter the setup page.
965 EfiBootManagerBoot (&BootManagerMenu
);
968 if (!PlatformRecovery
) {
970 // Execute SysPrep####
972 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeSysPrep
);
973 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
974 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
979 PERF_INMODULE_BEGIN ("BdsWait");
980 BdsWait (HotkeyTriggered
);
981 PERF_INMODULE_END ("BdsWait");
983 // BdsReadKeys() can be removed after all keyboard drivers invoke callback in timer callback.
987 EfiBootManagerHotkeyBoot ();
989 if (BootNext
!= NULL
) {
991 // Delete "BootNext" NV variable before transferring control to it to prevent loops.
993 Status
= gRT
->SetVariable (
994 EFI_BOOT_NEXT_VARIABLE_NAME
,
995 &gEfiGlobalVariableGuid
,
1001 // Deleting NV variable shouldn't fail unless it doesn't exist.
1003 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_FOUND
);
1006 // Boot to "BootNext"
1008 UnicodeSPrint (BootNextVariableName
, sizeof (BootNextVariableName
), L
"Boot%04x", *BootNext
);
1009 Status
= EfiBootManagerVariableToLoadOption (BootNextVariableName
, &LoadOption
);
1010 if (!EFI_ERROR (Status
)) {
1011 EfiBootManagerBoot (&LoadOption
);
1012 EfiBootManagerFreeLoadOption (&LoadOption
);
1013 if ((LoadOption
.Status
== EFI_SUCCESS
) &&
1014 (BootManagerMenuStatus
!= EFI_NOT_FOUND
) &&
1015 (LoadOption
.OptionNumber
!= BootManagerMenu
.OptionNumber
)) {
1017 // Boot to Boot Manager Menu upon EFI_SUCCESS
1018 // Exception: Do not boot again when the BootNext points to Boot Manager Menu.
1020 EfiBootManagerBoot (&BootManagerMenu
);
1027 // Retry to boot if any of the boot succeeds
1029 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeBoot
);
1030 BootSuccess
= BootBootOptions (LoadOptions
, LoadOptionCount
, (BootManagerMenuStatus
!= EFI_NOT_FOUND
) ? &BootManagerMenu
: NULL
);
1031 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1032 } while (BootSuccess
);
1035 if (BootManagerMenuStatus
!= EFI_NOT_FOUND
) {
1036 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
1040 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypePlatformRecovery
);
1041 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
1042 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1045 DEBUG ((EFI_D_ERROR
, "[Bds] Unable to boot!\n"));
1050 Set the variable and report the error through status code upon failure.
1052 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1053 Each VariableName is unique for each VendorGuid. VariableName must
1054 contain 1 or more characters. If VariableName is an empty string,
1055 then EFI_INVALID_PARAMETER is returned.
1056 @param VendorGuid A unique identifier for the vendor.
1057 @param Attributes Attributes bitmask to set for the variable.
1058 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1059 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1060 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1061 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1062 the variable value (the timestamp associated with the variable may be updated however
1063 even if no new data value is provided,see the description of the
1064 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1065 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1066 @param Data The contents for the variable.
1068 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1069 defined by the Attributes.
1070 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1071 DataSize exceeds the maximum allowed.
1072 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1073 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1074 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1075 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1076 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1077 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
1078 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.
1080 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1083 BdsDxeSetVariableAndReportStatusCodeOnError (
1084 IN CHAR16
*VariableName
,
1085 IN EFI_GUID
*VendorGuid
,
1086 IN UINT32 Attributes
,
1092 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1095 Status
= gRT
->SetVariable (
1102 if (EFI_ERROR (Status
)) {
1103 NameSize
= StrSize (VariableName
);
1104 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1105 if (SetVariableStatus
!= NULL
) {
1106 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1107 SetVariableStatus
->NameSize
= NameSize
;
1108 SetVariableStatus
->DataSize
= DataSize
;
1109 SetVariableStatus
->SetStatus
= Status
;
1110 SetVariableStatus
->Attributes
= Attributes
;
1111 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1112 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1114 REPORT_STATUS_CODE_EX (
1116 PcdGet32 (PcdErrorCodeSetVariable
),
1119 &gEdkiiStatusCodeDataTypeVariableGuid
,
1121 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1124 FreePool (SetVariableStatus
);