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 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
9 Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include "HwErrRecSupport.h"
24 #define SET_BOOT_OPTION_SUPPORT_KEY_COUNT(a, c) { \
25 (a) = ((a) & ~EFI_BOOT_OPTION_SUPPORT_COUNT) | (((c) << LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT)) & EFI_BOOT_OPTION_SUPPORT_COUNT); \
29 /// BDS arch protocol instance initial value.
31 EFI_BDS_ARCH_PROTOCOL gBds
= {
36 // gConnectConInEvent - Event which is signaled when ConIn connection is required
38 EFI_EVENT gConnectConInEvent
= NULL
;
41 /// The read-only variables defined in UEFI Spec.
43 CHAR16
*mReadOnlyVariables
[] = {
44 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
,
45 EFI_LANG_CODES_VARIABLE_NAME
,
46 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
47 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME
,
48 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
51 CHAR16
*mBdsLoadOptionName
[] = {
57 CHAR16 mRecoveryBoot
[] = L
"Recovery Boot";
59 Event to Connect ConIn.
61 @param Event Event whose notification function is being invoked.
62 @param Context Pointer to the notification function's context,
63 which is implementation-dependent.
68 BdsDxeOnConnectConInCallBack (
76 // When Osloader call ReadKeyStroke to signal this event
77 // no driver dependency is assumed existing. So use a non-dispatch version
79 Status
= EfiBootManagerConnectConsoleVariable (ConIn
);
80 if (EFI_ERROR (Status
)) {
82 // Should not enter this case, if enter, the keyboard will not work.
83 // May need platfrom policy to connect keyboard.
85 DEBUG ((EFI_D_WARN
, "[Bds] Connect ConIn failed - %r!!!\n", Status
));
91 Install Boot Device Selection Protocol
93 @param ImageHandle The image handle.
94 @param SystemTable The system table.
96 @retval EFI_SUCEESS BDS has finished initializing.
97 Return the dispatcher and recall BDS.Entry
98 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
104 IN EFI_HANDLE ImageHandle
,
105 IN EFI_SYSTEM_TABLE
*SystemTable
111 // Install protocol interface
114 Status
= gBS
->InstallMultipleProtocolInterfaces (
116 &gEfiBdsArchProtocolGuid
, &gBds
,
119 ASSERT_EFI_ERROR (Status
);
125 Emuerate all possible bootable medias in the following order:
126 1. Removable BlockIo - The boot option only points to the removable media
127 device, like USB key, DVD, Floppy etc.
128 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
130 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting
131 SimpleFileSystem Protocol, but not supporting BlockIo
133 4. LoadFile - The boot option points to the media supporting
135 Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior
137 @param BootOptionCount Return the boot option count which has been found.
139 @retval Pointer to the boot option array.
141 EFI_BOOT_MANAGER_LOAD_OPTION
*
142 BdsEnumerateBootOptions (
143 UINTN
*BootOptionCount
147 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
150 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
154 ASSERT (BootOptionCount
!= NULL
);
156 *BootOptionCount
= 0;
160 // Parse removable block io followed by fixed block io
162 gBS
->LocateHandleBuffer (
164 &gEfiBlockIoProtocolGuid
,
170 for (Removable
= 0; Removable
< 2; Removable
++) {
171 for (Index
= 0; Index
< HandleCount
; Index
++) {
172 Status
= gBS
->HandleProtocol (
174 &gEfiBlockIoProtocolGuid
,
177 if (EFI_ERROR (Status
)) {
182 // Skip the logical partitions
184 if (BlkIo
->Media
->LogicalPartition
) {
189 // Skip the fixed block io then the removable block io
191 if (BlkIo
->Media
->RemovableMedia
== ((Removable
== 0) ? FALSE
: TRUE
)) {
195 BootOptions
= ReallocatePool (
196 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
197 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
200 ASSERT (BootOptions
!= NULL
);
202 Status
= EfiBootManagerInitializeLoadOption (
203 &BootOptions
[(*BootOptionCount
)++],
204 LoadOptionNumberUnassigned
,
208 DevicePathFromHandle (Handles
[Index
]),
212 ASSERT_EFI_ERROR (Status
);
216 if (HandleCount
!= 0) {
221 // Parse simple file system not based on block io
223 gBS
->LocateHandleBuffer (
225 &gEfiSimpleFileSystemProtocolGuid
,
230 for (Index
= 0; Index
< HandleCount
; Index
++) {
231 Status
= gBS
->HandleProtocol (
233 &gEfiBlockIoProtocolGuid
,
236 if (!EFI_ERROR (Status
)) {
238 // Skip if the file system handle supports a BlkIo protocol, which we've handled in above
242 BootOptions
= ReallocatePool (
243 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
244 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
247 ASSERT (BootOptions
!= NULL
);
249 Status
= EfiBootManagerInitializeLoadOption (
250 &BootOptions
[(*BootOptionCount
)++],
251 LoadOptionNumberUnassigned
,
255 DevicePathFromHandle (Handles
[Index
]),
259 ASSERT_EFI_ERROR (Status
);
262 if (HandleCount
!= 0) {
267 // Parse load file, assuming UEFI Network boot option
269 gBS
->LocateHandleBuffer (
271 &gEfiLoadFileProtocolGuid
,
276 for (Index
= 0; Index
< HandleCount
; Index
++) {
278 BootOptions
= ReallocatePool (
279 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
280 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
283 ASSERT (BootOptions
!= NULL
);
285 Status
= EfiBootManagerInitializeLoadOption (
286 &BootOptions
[(*BootOptionCount
)++],
287 LoadOptionNumberUnassigned
,
291 DevicePathFromHandle (Handles
[Index
]),
295 ASSERT_EFI_ERROR (Status
);
298 if (HandleCount
!= 0) {
306 Function waits for a given event to fire, or for an optional timeout to expire.
308 @param Event The event to wait for
309 @param Timeout An optional timeout value in 100 ns units.
311 @retval EFI_SUCCESS Event fired before Timeout expired.
312 @retval EFI_TIME_OUT Timout expired before Event fired..
316 BdsWaitForSingleEvent (
318 IN UINT64 Timeout OPTIONAL
323 EFI_EVENT TimerEvent
;
324 EFI_EVENT WaitList
[2];
328 // Create a timer event
330 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
331 if (!EFI_ERROR (Status
)) {
333 // Set the timer event
342 // Wait for the original event or the timer
345 WaitList
[1] = TimerEvent
;
346 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
347 ASSERT_EFI_ERROR (Status
);
348 gBS
->CloseEvent (TimerEvent
);
351 // If the timer expired, change the return to timed out
354 Status
= EFI_TIMEOUT
;
359 // No timeout... just wait on the event
361 Status
= gBS
->WaitForEvent (1, &Event
, &Index
);
362 ASSERT (!EFI_ERROR (Status
));
370 The function reads user inputs.
381 if (PcdGetBool (PcdConInConnectOnDemand
)) {
385 while (gST
->ConIn
!= NULL
) {
387 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
389 if (EFI_ERROR (Status
)) {
399 The function waits for the boot manager timeout expires or hotkey is pressed.
401 It calls PlatformBootManagerWaitCallback each second.
403 @param HotkeyTriggered Input hotkey event.
407 IN EFI_EVENT HotkeyTriggered
411 UINT16 TimeoutRemain
;
413 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait ...Zzzzzzzzzzzz...\n"));
415 TimeoutRemain
= PcdGet16 (PcdPlatformBootTimeOut
);
416 while (TimeoutRemain
!= 0) {
417 DEBUG ((EFI_D_INFO
, "[Bds]BdsWait(%d)..Zzzz...\n", (UINTN
) TimeoutRemain
));
418 PlatformBootManagerWaitCallback (TimeoutRemain
);
420 BdsReadKeys (); // BUGBUG: Only reading can signal HotkeyTriggered
421 // Can be removed after all keyboard drivers invoke callback in timer callback.
423 if (HotkeyTriggered
!= NULL
) {
424 Status
= BdsWaitForSingleEvent (HotkeyTriggered
, EFI_TIMER_PERIOD_SECONDS (1));
425 if (!EFI_ERROR (Status
)) {
429 gBS
->Stall (1000000);
433 // 0xffff means waiting forever
434 // BDS with no hotkey provided and 0xffff as timeout will "hang" in the loop
436 if (TimeoutRemain
!= 0xffff) {
440 DEBUG ((EFI_D_INFO
, "[Bds]Exit the waiting!\n"));
444 Attempt to boot each boot option in the BootOptions array.
446 @param BootOptions Input boot option array.
447 @param BootOptionCount Input boot option count.
449 @retval TRUE Successfully boot one of the boot options.
450 @retval FALSE Failed boot any of the boot options.
454 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
,
455 IN UINTN BootOptionCount
461 // Attempt boot each boot option
463 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
465 // According to EFI Specification, if a load option is not marked
466 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
469 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_ACTIVE
) == 0) {
474 // Boot#### load options with LOAD_OPTION_CATEGORY_APP are executables which are not
475 // part of the normal boot processing. Boot options with reserved category values will be
476 // ignored by the boot manager.
478 if ((BootOptions
[Index
].Attributes
& LOAD_OPTION_CATEGORY
) != LOAD_OPTION_CATEGORY_BOOT
) {
483 // All the driver options should have been processed since
484 // now boot will be performed.
486 EfiBootManagerBoot (&BootOptions
[Index
]);
489 // Successful boot breaks the loop, otherwise tries next boot option
491 if (BootOptions
[Index
].Status
== EFI_SUCCESS
) {
496 return (BOOLEAN
) (Index
< BootOptionCount
);
500 This function attempts to boot per the boot order specified by platform policy.
502 If the boot via Boot#### returns with a status of EFI_SUCCESS the boot manager will stop
503 processing the BootOrder variable and present a boot manager menu to the user. If a boot via
504 Boot#### returns a status other than EFI_SUCCESS, the boot has failed and the next Boot####
505 in the BootOrder variable will be tried until all possibilities are exhausted.
506 -- Chapter 3.1.1 Boot Manager Programming, the 4th paragraph
509 DefaultBootBehavior (
513 UINTN BootOptionCount
;
514 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
515 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
517 EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
519 // BootManagerMenu always contains the correct information even the above function returns failure.
522 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
524 if (BootAllBootOptions (BootOptions
, BootOptionCount
)) {
526 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI
528 if (PcdGetBool (PcdConInConnectOnDemand
)) {
529 BdsDxeOnConnectConInCallBack (NULL
, NULL
);
533 // Show the Boot Manager Menu after successful boot
535 EfiBootManagerBoot (&BootManagerMenu
);
537 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
539 // Re-scan all EFI boot options in case all the boot#### are deleted or failed to boot
541 // If no valid boot options exist, the boot manager will enumerate all removable media
542 // devices followed by all fixed media devices. The order within each group is undefined.
543 // These new default boot options are not saved to non volatile storage.The boot manger
544 // will then attempt toboot from each boot option.
545 // -- Chapter 3.3 Boot Manager Programming, the 2nd paragraph
547 EfiBootManagerConnectAll ();
548 BootOptions
= BdsEnumerateBootOptions (&BootOptionCount
);
550 if (!BootAllBootOptions (BootOptions
, BootOptionCount
)) {
551 DEBUG ((EFI_D_ERROR
, "[Bds]No bootable device!\n"));
552 EfiBootManagerBoot (&BootManagerMenu
);
556 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
557 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
561 The function will load and start every Driver####/SysPrep####.
563 @param LoadOptions Load option array.
564 @param LoadOptionCount Load option count.
569 IN EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
,
570 IN UINTN LoadOptionCount
575 BOOLEAN ReconnectAll
;
576 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
578 ReconnectAll
= FALSE
;
579 LoadOptionType
= LoadOptionTypeMax
;
582 // Process the driver option
584 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
586 // All the load options in the array should be of the same type.
588 if (LoadOptionType
== LoadOptionTypeMax
) {
589 LoadOptionType
= LoadOptions
[Index
].OptionType
;
591 ASSERT (LoadOptionType
== LoadOptions
[Index
].OptionType
);
592 ASSERT (LoadOptionType
== LoadOptionTypeDriver
|| LoadOptionType
== LoadOptionTypeSysPrep
);
594 Status
= EfiBootManagerProcessLoadOption (&LoadOptions
[Index
]);
596 if (!EFI_ERROR (Status
) && ((LoadOptions
[Index
].Attributes
& LOAD_OPTION_FORCE_RECONNECT
) != 0)) {
602 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
603 // then all of the EFI drivers in the system will be disconnected and
604 // reconnected after the last driver load option is processed.
606 if (ReconnectAll
&& LoadOptionType
== LoadOptionTypeDriver
) {
607 EfiBootManagerDisconnectAll ();
608 EfiBootManagerConnectAll ();
614 Validate input console variable data.
616 If found the device path is not a valid device path, remove the variable.
618 @param VariableName Input console variable name.
622 BdsFormalizeConsoleVariable (
623 IN CHAR16
*VariableName
626 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
630 GetEfiGlobalVariable2 (VariableName
, (VOID
**) &DevicePath
, &VariableSize
);
631 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
632 Status
= gRT
->SetVariable (
634 &gEfiGlobalVariableGuid
,
635 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
640 // Deleting variable with current variable implementation shouldn't fail.
642 ASSERT_EFI_ERROR (Status
);
645 if (DevicePath
!= NULL
) {
646 FreePool (DevicePath
);
651 Formalize OsIndication related variables.
653 For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
654 Delete OsIndications variable if it is not NV/BS/RT UINT64.
656 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
660 BdsFormalizeOSIndicationVariable (
665 UINT64 OsIndicationSupport
;
671 // OS indicater support variable
673 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
674 Status
= gRT
->SetVariable (
675 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
,
676 &gEfiGlobalVariableGuid
,
677 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
682 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
684 ASSERT_EFI_ERROR (Status
);
687 // If OsIndications is invalid, remove it.
689 // 1. Data size != UINT64
690 // 2. OsIndication value inconsistence
691 // 3. OsIndication attribute inconsistence
695 DataSize
= sizeof(UINT64
);
696 Status
= gRT
->GetVariable (
697 EFI_OS_INDICATIONS_VARIABLE_NAME
,
698 &gEfiGlobalVariableGuid
,
703 if (Status
== EFI_NOT_FOUND
) {
707 if ((DataSize
!= sizeof (OsIndication
)) ||
708 ((OsIndication
& ~OsIndicationSupport
) != 0) ||
709 (Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
))
712 DEBUG ((EFI_D_ERROR
, "[Bds] Unformalized OsIndications variable exists. Delete it\n"));
713 Status
= gRT
->SetVariable (
714 EFI_OS_INDICATIONS_VARIABLE_NAME
,
715 &gEfiGlobalVariableGuid
,
721 // Deleting variable with current variable implementation shouldn't fail.
723 ASSERT_EFI_ERROR(Status
);
733 BdsFormalizeEfiGlobalVariable (
738 // Validate Console variable.
740 BdsFormalizeConsoleVariable (EFI_CON_IN_VARIABLE_NAME
);
741 BdsFormalizeConsoleVariable (EFI_CON_OUT_VARIABLE_NAME
);
742 BdsFormalizeConsoleVariable (EFI_ERR_OUT_VARIABLE_NAME
);
745 // Validate OSIndication related variable.
747 BdsFormalizeOSIndicationVariable ();
752 Allocate a block of memory that will contain performance data to OS.
756 BdsAllocateMemoryForPerformanceData (
761 EFI_PHYSICAL_ADDRESS AcpiLowMemoryBase
;
762 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
764 AcpiLowMemoryBase
= 0x0FFFFFFFFULL
;
767 // Allocate a block of memory that will contain performance data to OS.
769 Status
= gBS
->AllocatePages (
771 EfiReservedMemoryType
,
772 EFI_SIZE_TO_PAGES (PERF_DATA_MAX_LENGTH
),
775 if (!EFI_ERROR (Status
)) {
777 // Save the pointer to variable for use in S3 resume.
779 Status
= BdsDxeSetVariableAndReportStatusCodeOnError (
781 &gPerformanceProtocolGuid
,
782 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
783 sizeof (EFI_PHYSICAL_ADDRESS
),
786 if (EFI_ERROR (Status
)) {
787 DEBUG ((EFI_D_ERROR
, "[Bds] PerfDataMemAddr (%08x) cannot be saved to NV storage.\n", AcpiLowMemoryBase
));
790 // Mark L"PerfDataMemAddr" variable to read-only if the Variable Lock protocol exists
791 // Still lock it even the variable cannot be saved to prevent it's set by 3rd party code.
793 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
794 if (!EFI_ERROR (Status
)) {
795 Status
= VariableLock
->RequestToLock (VariableLock
, L
"PerfDataMemAddr", &gPerformanceProtocolGuid
);
796 ASSERT_EFI_ERROR (Status
);
803 Service routine for BdsInstance->Entry(). Devices are connected, the
804 consoles are initialized, and the boot options are tried.
806 @param This Protocol Instance structure.
812 IN EFI_BDS_ARCH_PROTOCOL
*This
815 EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOptions
;
816 UINTN LoadOptionCount
;
817 CHAR16
*FirmwareVendor
;
818 EFI_EVENT HotkeyTriggered
;
822 UINT32 BootOptionSupport
;
824 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
826 EFI_BOOT_MANAGER_LOAD_OPTION BootOption
;
828 CHAR16 BootNextVariableName
[sizeof ("Boot####")];
829 EFI_BOOT_MANAGER_LOAD_OPTION BootManagerMenu
;
832 HotkeyTriggered
= NULL
;
833 Status
= EFI_SUCCESS
;
836 // Insert the performance probe
838 PERF_END (NULL
, "DXE", NULL
, 0);
839 PERF_START (NULL
, "BDS", NULL
, 0);
840 DEBUG ((EFI_D_INFO
, "[Bds] Entry...\n"));
843 BdsAllocateMemoryForPerformanceData ();
847 // Fill in FirmwareVendor and FirmwareRevision from PCDs
849 FirmwareVendor
= (CHAR16
*) PcdGetPtr (PcdFirmwareVendor
);
850 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
851 ASSERT (gST
->FirmwareVendor
!= NULL
);
852 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
855 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
858 gBS
->CalculateCrc32 ((VOID
*) gST
, sizeof (EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
861 // Validate Variable.
863 BdsFormalizeEfiGlobalVariable ();
866 // Mark the read-only variables if the Variable Lock protocol exists
868 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
869 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
870 if (!EFI_ERROR (Status
)) {
871 for (Index
= 0; Index
< sizeof (mReadOnlyVariables
) / sizeof (mReadOnlyVariables
[0]); Index
++) {
872 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
873 ASSERT_EFI_ERROR (Status
);
877 InitializeHwErrRecSupport ();
880 // Initialize L"Timeout" EFI global variable.
882 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
883 if (BootTimeOut
!= 0xFFFF) {
885 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
886 // define same behavior between no value or 0xFFFF value for L"Timeout".
888 BdsDxeSetVariableAndReportStatusCodeOnError (
889 EFI_TIME_OUT_VARIABLE_NAME
,
890 &gEfiGlobalVariableGuid
,
891 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
898 // Initialize L"BootOptionSupport" EFI global variable.
899 // Lazy-ConIn implictly disables BDS hotkey.
901 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_APP
| EFI_BOOT_OPTION_SUPPORT_SYSPREP
;
902 if (!PcdGetBool (PcdConInConnectOnDemand
)) {
903 BootOptionSupport
|= EFI_BOOT_OPTION_SUPPORT_KEY
;
904 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport
, 3);
906 Status
= gRT
->SetVariable (
907 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
,
908 &gEfiGlobalVariableGuid
,
909 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
910 sizeof (BootOptionSupport
),
914 // Platform needs to make sure setting volatile variable before calling 3rd party code shouldn't fail.
916 ASSERT_EFI_ERROR (Status
);
919 // Cache and remove the "BootNext" NV variable.
921 GetEfiGlobalVariable2 (EFI_BOOT_NEXT_VARIABLE_NAME
, (VOID
**) &BootNext
, &DataSize
);
922 if (DataSize
!= sizeof (UINT16
)) {
923 if (BootNext
!= NULL
) {
928 Status
= gRT
->SetVariable (
929 EFI_BOOT_NEXT_VARIABLE_NAME
,
930 &gEfiGlobalVariableGuid
,
936 // Deleting NV variable shouldn't fail unless it doesn't exist.
938 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_FOUND
);
941 // Initialize the platform language variables
943 InitializeLanguage (TRUE
);
946 // Report Status Code to indicate connecting drivers will happen
950 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
954 // Do the platform init, can be customized by OEM/IBV
955 // Possible things that can be done in PlatformBootManagerBeforeConsole:
956 // > Update console variable: 1. include hot-plug devices; 2. Clear ConIn and add SOL for AMT
957 // > Register new Driver#### or Boot####
958 // > Register new Key####: e.g.: F12
959 // > Signal ReadyToLock event
960 // > Authentication action: 1. connect Auth devices; 2. Identify auto logon user.
962 PERF_START (NULL
, "PlatformBootManagerBeforeConsole", "BDS", 0);
963 PlatformBootManagerBeforeConsole ();
964 PERF_END (NULL
, "PlatformBootManagerBeforeConsole", "BDS", 0);
967 // Initialize hotkey service
969 EfiBootManagerStartHotkeyService (&HotkeyTriggered
);
972 // Execute Driver Options
974 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeDriver
);
975 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
976 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
981 PERF_START (NULL
, "EfiBootManagerConnectAllDefaultConsoles", "BDS", 0);
982 if (PcdGetBool (PcdConInConnectOnDemand
)) {
983 EfiBootManagerConnectConsoleVariable (ConOut
);
984 EfiBootManagerConnectConsoleVariable (ErrOut
);
987 // Initialize ConnectConIn event
989 Status
= gBS
->CreateEventEx (
992 BdsDxeOnConnectConInCallBack
,
994 &gConnectConInEventGuid
,
997 if (EFI_ERROR (Status
)) {
998 gConnectConInEvent
= NULL
;
1001 EfiBootManagerConnectAllDefaultConsoles ();
1003 PERF_END (NULL
, "EfiBootManagerConnectAllDefaultConsoles", "BDS", 0);
1006 // Do the platform specific action after the console is ready
1007 // Possible things that can be done in PlatformBootManagerAfterConsole:
1008 // > Console post action:
1009 // > Dynamically switch output mode from 100x31 to 80x25 for certain senarino
1010 // > Signal console ready platform customized event
1011 // > Run diagnostics like memory testing
1012 // > Connect certain devices
1013 // > Dispatch aditional option roms
1014 // > Special boot: e.g.: USB boot, enter UI
1016 PERF_START (NULL
, "PlatformBootManagerAfterConsole", "BDS", 0);
1017 PlatformBootManagerAfterConsole ();
1018 PERF_END (NULL
, "PlatformBootManagerAfterConsole", "BDS", 0);
1021 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
;
1022 DEBUG ((EFI_D_INFO
, "[Bds]=============Begin Load Options Dumping ...=============\n"));
1023 for (LoadOptionType
= 0; LoadOptionType
< LoadOptionTypeMax
; LoadOptionType
++) {
1025 EFI_D_INFO
, " %s Options:\n",
1026 mBdsLoadOptionName
[LoadOptionType
]
1028 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionType
);
1029 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
1031 EFI_D_INFO
, " %s%04x: %s \t\t 0x%04x\n",
1032 mBdsLoadOptionName
[LoadOptionType
],
1033 LoadOptions
[Index
].OptionNumber
,
1034 LoadOptions
[Index
].Description
,
1035 LoadOptions
[Index
].Attributes
1038 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1040 DEBUG ((EFI_D_INFO
, "[Bds]=============End Load Options Dumping=============\n"));
1044 // Boot to Boot Manager Menu when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
1046 DataSize
= sizeof (UINT64
);
1047 Status
= gRT
->GetVariable (
1048 EFI_OS_INDICATIONS_VARIABLE_NAME
,
1049 &gEfiGlobalVariableGuid
,
1054 if (EFI_ERROR (Status
)) {
1058 BootFwUi
= (BOOLEAN
) ((OsIndication
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
) != 0);
1060 // Clear EFI_OS_INDICATIONS_BOOT_TO_FW_UI to acknowledge OS
1063 OsIndication
&= ~((UINT64
) EFI_OS_INDICATIONS_BOOT_TO_FW_UI
);
1064 Status
= gRT
->SetVariable (
1065 EFI_OS_INDICATIONS_VARIABLE_NAME
,
1066 &gEfiGlobalVariableGuid
,
1067 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1072 // Changing the content without increasing its size with current variable implementation shouldn't fail.
1074 ASSERT_EFI_ERROR (Status
);
1078 // Launch Boot Manager Menu directly when EFI_OS_INDICATIONS_BOOT_TO_FW_UI is set. Skip HotkeyBoot
1082 // Follow generic rule, Call BdsDxeOnConnectConInCallBack to connect ConIn before enter UI
1084 if (PcdGetBool (PcdConInConnectOnDemand
)) {
1085 BdsDxeOnConnectConInCallBack (NULL
, NULL
);
1089 // Directly enter the setup page.
1090 // BootManagerMenu always contains the correct information even call fails.
1092 EfiBootManagerGetBootManagerMenu (&BootManagerMenu
);
1093 EfiBootManagerBoot (&BootManagerMenu
);
1094 EfiBootManagerFreeLoadOption (&BootManagerMenu
);
1098 // Execute SysPrep####
1100 LoadOptions
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, LoadOptionTypeSysPrep
);
1101 ProcessLoadOptions (LoadOptions
, LoadOptionCount
);
1102 EfiBootManagerFreeLoadOptions (LoadOptions
, LoadOptionCount
);
1107 PERF_START (NULL
, "BdsWait", "BDS", 0);
1108 BdsWait (HotkeyTriggered
);
1109 PERF_END (NULL
, "BdsWait", "BDS", 0);
1112 // BdsReadKeys() be removed after all keyboard drivers invoke callback in timer callback.
1116 EfiBootManagerHotkeyBoot ();
1119 // Boot to "BootNext"
1121 if (BootNext
!= NULL
) {
1122 UnicodeSPrint (BootNextVariableName
, sizeof (BootNextVariableName
), L
"Boot%04x", *BootNext
);
1123 Status
= EfiBootManagerVariableToLoadOption (BootNextVariableName
, &BootOption
);
1124 if (!EFI_ERROR (Status
)) {
1125 EfiBootManagerBoot (&BootOption
);
1126 EfiBootManagerFreeLoadOption (&BootOption
);
1127 if (BootOption
.Status
== EFI_SUCCESS
) {
1129 // Boot to Boot Manager Menu upon EFI_SUCCESS
1131 EfiBootManagerGetBootManagerMenu (&BootOption
);
1132 EfiBootManagerBoot (&BootOption
);
1133 EfiBootManagerFreeLoadOption (&BootOption
);
1140 // BDS select the boot device to load OS
1141 // Try next upon boot failure
1142 // Show Boot Manager Menu upon boot success
1144 DefaultBootBehavior ();
1149 Set the variable and report the error through status code upon failure.
1151 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1152 Each VariableName is unique for each VendorGuid. VariableName must
1153 contain 1 or more characters. If VariableName is an empty string,
1154 then EFI_INVALID_PARAMETER is returned.
1155 @param VendorGuid A unique identifier for the vendor.
1156 @param Attributes Attributes bitmask to set for the variable.
1157 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1158 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
1159 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1160 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1161 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1162 the variable value (the timestamp associated with the variable may be updated however
1163 even if no new data value is provided,see the description of the
1164 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1165 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1166 @param Data The contents for the variable.
1168 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1169 defined by the Attributes.
1170 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1171 DataSize exceeds the maximum allowed.
1172 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1173 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1174 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1175 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1176 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1177 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1178 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
1179 does NOT pass the validation check carried out by the firmware.
1181 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1184 BdsDxeSetVariableAndReportStatusCodeOnError (
1185 IN CHAR16
*VariableName
,
1186 IN EFI_GUID
*VendorGuid
,
1187 IN UINT32 Attributes
,
1193 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1196 Status
= gRT
->SetVariable (
1203 if (EFI_ERROR (Status
)) {
1204 NameSize
= StrSize (VariableName
);
1205 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1206 if (SetVariableStatus
!= NULL
) {
1207 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1208 SetVariableStatus
->NameSize
= NameSize
;
1209 SetVariableStatus
->DataSize
= DataSize
;
1210 SetVariableStatus
->SetStatus
= Status
;
1211 SetVariableStatus
->Attributes
= Attributes
;
1212 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1213 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1215 REPORT_STATUS_CODE_EX (
1217 PcdGet32 (PcdErrorCodeSetVariable
),
1220 &gEdkiiStatusCodeDataTypeVariableGuid
,
1222 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1225 FreePool (SetVariableStatus
);