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 - 2014, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include "FrontPage.h"
23 #include "HwErrRecSupport.h"
26 /// BDS arch protocol instance initial value.
28 /// Note: Current BDS not directly get the BootMode, DefaultBoot,
29 /// TimeoutDefault, MemoryTestLevel value from the BDS arch protocol.
30 /// Please refer to the library useage of BdsLibGetBootMode, BdsLibGetTimeout
31 /// and PlatformBdsDiagnostics in BdsPlatform.c
33 EFI_HANDLE gBdsHandle
= NULL
;
35 EFI_BDS_ARCH_PROTOCOL gBds
= {
39 UINT16
*mBootNext
= NULL
;
42 /// The read-only variables defined in UEFI Spec.
44 CHAR16
*mReadOnlyVariables
[] = {
49 L
"OsIndicationsSupported"
54 Install Boot Device Selection Protocol
56 @param ImageHandle The image handle.
57 @param SystemTable The system table.
59 @retval EFI_SUCEESS BDS has finished initializing.
60 Return the dispatcher and recall BDS.Entry
61 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
67 IN EFI_HANDLE ImageHandle
,
68 IN EFI_SYSTEM_TABLE
*SystemTable
74 // Install protocol interface
76 Status
= gBS
->InstallMultipleProtocolInterfaces (
78 &gEfiBdsArchProtocolGuid
, &gBds
,
81 ASSERT_EFI_ERROR (Status
);
88 An empty function to pass error checking of CreateEventEx ().
90 @param Event Event whose notification function is being invoked.
91 @param Context Pointer to the notification function's context,
92 which is implementation-dependent.
97 BdsEmptyCallbackFunction (
106 This function attempts to boot for the boot order specified
111 BdsBootDeviceSelect (
117 BDS_COMMON_OPTION
*BootOption
;
121 LIST_ENTRY BootLists
;
123 BOOLEAN BootNextExist
;
124 LIST_ENTRY
*LinkBootNext
;
125 EFI_EVENT ConnectConInEvent
;
128 // Got the latest boot option
130 BootNextExist
= FALSE
;
132 ConnectConInEvent
= NULL
;
133 InitializeListHead (&BootLists
);
136 // First check the boot next option
138 ZeroMem (Buffer
, sizeof (Buffer
));
141 // Create Event to signal ConIn connection request
143 if (PcdGetBool (PcdConInConnectOnDemand
)) {
144 Status
= gBS
->CreateEventEx (
147 BdsEmptyCallbackFunction
,
149 &gConnectConInEventGuid
,
152 if (EFI_ERROR(Status
)) {
153 ConnectConInEvent
= NULL
;
157 if (mBootNext
!= NULL
) {
159 // Indicate we have the boot next variable, so this time
160 // boot will always have this boot option
162 BootNextExist
= TRUE
;
165 // Clear the this variable so it's only exist in this time boot
167 Status
= gRT
->SetVariable (
169 &gEfiGlobalVariableGuid
,
170 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
175 // Deleting variable with current variable implementation shouldn't fail.
177 ASSERT_EFI_ERROR (Status
);
180 // Add the boot next boot option
182 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *mBootNext
);
183 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
186 // If fail to get boot option from variable, just return and do nothing.
188 if (BootOption
== NULL
) {
192 BootOption
->BootCurrent
= *mBootNext
;
195 // Parse the boot order to get boot option
197 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
200 // When we didn't have chance to build boot option variables in the first
201 // full configuration boot (e.g.: Reset in the first page or in Device Manager),
202 // we have no boot options in the following mini configuration boot.
203 // Give the last chance to enumerate the boot options.
205 if (IsListEmpty (&BootLists
)) {
206 BdsLibEnumerateAllBootOption (&BootLists
);
209 Link
= BootLists
.ForwardLink
;
212 // Parameter check, make sure the loop will be valid
218 // Here we make the boot in a loop, every boot success will
219 // return to the front page
223 // Check the boot option list first
225 if (Link
== &BootLists
) {
227 // When LazyConIn enabled, signal connect ConIn event before enter UI
229 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
230 gBS
->SignalEvent (ConnectConInEvent
);
234 // There are two ways to enter here:
235 // 1. There is no active boot option, give user chance to
236 // add new boot option
237 // 2. All the active boot option processed, and there is no
238 // one is success to boot, then we back here to allow user
239 // add new active boot option
242 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
243 InitializeListHead (&BootLists
);
244 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
245 Link
= BootLists
.ForwardLink
;
249 // Get the boot option from the link list
251 BootOption
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
254 // According to EFI Specification, if a load option is not marked
255 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
258 if (!IS_LOAD_OPTION_TYPE (BootOption
->Attribute
, LOAD_OPTION_ACTIVE
)) {
260 // skip the header of the link list, because it has no boot option
262 Link
= Link
->ForwardLink
;
266 // Make sure the boot option device path connected,
267 // but ignore the BBS device path
269 if (DevicePathType (BootOption
->DevicePath
) != BBS_DEVICE_PATH
) {
271 // Notes: the internal shell can not been connected with device path
272 // so we do not check the status here
274 BdsLibConnectDevicePath (BootOption
->DevicePath
);
278 // Restore to original mode before launching boot option.
280 BdsSetConsoleMode (FALSE
);
283 // All the driver options should have been processed since
284 // now boot will be performed.
286 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
287 if (Status
!= EFI_SUCCESS
) {
289 // Call platform action to indicate the boot fail
291 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
292 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
295 // Check the next boot option
297 Link
= Link
->ForwardLink
;
301 // Call platform action to indicate the boot success
303 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
304 PlatformBdsBootSuccess (BootOption
);
307 // Boot success, then stop process the boot order, and
308 // present the boot manager menu, front page
312 // When LazyConIn enabled, signal connect ConIn Event before enter UI
314 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
315 gBS
->SignalEvent (ConnectConInEvent
);
319 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
322 // Rescan the boot option list, avoid potential risk of the boot
323 // option change in front page
326 LinkBootNext
= BootLists
.ForwardLink
;
329 InitializeListHead (&BootLists
);
330 if (LinkBootNext
!= NULL
) {
332 // Reserve the boot next option
334 InsertTailList (&BootLists
, LinkBootNext
);
337 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
338 Link
= BootLists
.ForwardLink
;
346 Validate input console variable data.
348 If found the device path is not a valid device path, remove the variable.
350 @param VariableName Input console variable name.
354 BdsFormalizeConsoleVariable (
355 IN CHAR16
*VariableName
358 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
362 DevicePath
= BdsLibGetVariableAndSize (
364 &gEfiGlobalVariableGuid
,
367 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
368 Status
= gRT
->SetVariable (
370 &gEfiGlobalVariableGuid
,
371 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
376 // Deleting variable with current variable implementation shouldn't fail.
378 ASSERT_EFI_ERROR (Status
);
384 Formalize Bds global variables.
386 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.
387 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
388 3. Delete OsIndications variable if it is not NV/BS/RT UINT64
389 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
393 BdsFormalizeEfiGlobalVariable (
398 UINT64 OsIndicationSupport
;
404 // Validate Console variable.
406 BdsFormalizeConsoleVariable (L
"ConIn");
407 BdsFormalizeConsoleVariable (L
"ConOut");
408 BdsFormalizeConsoleVariable (L
"ErrOut");
411 // OS indicater support variable
413 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI \
414 | EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED
;
416 BdsDxeSetVariableAndReportStatusCodeOnError (
417 L
"OsIndicationsSupported",
418 &gEfiGlobalVariableGuid
,
419 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
425 // If OsIndications is invalid, remove it.
427 // 1. Data size != UINT64
428 // 2. OsIndication value inconsistence
429 // 3. OsIndication attribute inconsistence
433 DataSize
= sizeof(UINT64
);
434 Status
= gRT
->GetVariable (
436 &gEfiGlobalVariableGuid
,
442 if (!EFI_ERROR(Status
)) {
443 if (DataSize
!= sizeof(UINT64
) ||
444 (OsIndication
& ~OsIndicationSupport
) != 0 ||
445 Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
)){
447 DEBUG ((EFI_D_ERROR
, "Unformalized OsIndications variable exists. Delete it\n"));
448 Status
= gRT
->SetVariable (
450 &gEfiGlobalVariableGuid
,
456 // Deleting variable with current variable implementation shouldn't fail.
458 ASSERT_EFI_ERROR (Status
);
466 Allocate a block of memory that will contain performance data to OS.
470 BdsAllocateMemoryForPerformanceData (
475 EFI_PHYSICAL_ADDRESS AcpiLowMemoryBase
;
476 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
478 AcpiLowMemoryBase
= 0x0FFFFFFFFULL
;
481 // Allocate a block of memory that will contain performance data to OS.
483 Status
= gBS
->AllocatePages (
485 EfiReservedMemoryType
,
486 EFI_SIZE_TO_PAGES (PERF_DATA_MAX_LENGTH
),
489 if (!EFI_ERROR (Status
)) {
491 // Save the pointer to variable for use in S3 resume.
493 BdsDxeSetVariableAndReportStatusCodeOnError (
495 &gPerformanceProtocolGuid
,
496 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
497 sizeof (EFI_PHYSICAL_ADDRESS
),
500 if (EFI_ERROR (Status
)) {
501 DEBUG ((EFI_D_ERROR
, "[Bds] PerfDataMemAddr (%08x) cannot be saved to NV storage.\n", AcpiLowMemoryBase
));
504 // Mark L"PerfDataMemAddr" variable to read-only if the Variable Lock protocol exists
505 // Still lock it even the variable cannot be saved to prevent it's set by 3rd party code.
507 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
508 if (!EFI_ERROR (Status
)) {
509 Status
= VariableLock
->RequestToLock (VariableLock
, L
"PerfDataMemAddr", &gPerformanceProtocolGuid
);
510 ASSERT_EFI_ERROR (Status
);
517 Service routine for BdsInstance->Entry(). Devices are connected, the
518 consoles are initialized, and the boot options are tried.
520 @param This Protocol Instance structure.
526 IN EFI_BDS_ARCH_PROTOCOL
*This
529 LIST_ENTRY DriverOptionList
;
530 LIST_ENTRY BootOptionList
;
532 CHAR16
*FirmwareVendor
;
536 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
539 // Insert the performance probe
541 PERF_END (NULL
, "DXE", NULL
, 0);
542 PERF_START (NULL
, "BDS", NULL
, 0);
545 BdsAllocateMemoryForPerformanceData ();
549 // Initialize the global system boot option and driver option
551 InitializeListHead (&DriverOptionList
);
552 InitializeListHead (&BootOptionList
);
555 // Initialize hotkey service
557 InitializeHotkeyService ();
560 // Fill in FirmwareVendor and FirmwareRevision from PCDs
562 FirmwareVendor
= (CHAR16
*)PcdGetPtr (PcdFirmwareVendor
);
563 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
564 ASSERT (gST
->FirmwareVendor
!= NULL
);
565 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
568 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
571 gBS
->CalculateCrc32 ((VOID
*)gST
, sizeof(EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
574 // Validate Variable.
576 BdsFormalizeEfiGlobalVariable();
579 // Mark the read-only variables if the Variable Lock protocol exists
581 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
582 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
583 if (!EFI_ERROR (Status
)) {
584 for (Index
= 0; Index
< sizeof (mReadOnlyVariables
) / sizeof (mReadOnlyVariables
[0]); Index
++) {
585 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
586 ASSERT_EFI_ERROR (Status
);
591 // Report Status Code to indicate connecting drivers will happen
595 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
598 InitializeHwErrRecSupport();
601 // Initialize L"Timeout" EFI global variable.
603 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
604 if (BootTimeOut
!= 0xFFFF) {
606 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
607 // define same behavior between no value or 0xFFFF value for L"Timeout".
609 BdsDxeSetVariableAndReportStatusCodeOnError (
611 &gEfiGlobalVariableGuid
,
612 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
619 // bugbug: platform specific code
620 // Initialize the platform specific string and language
622 InitializeStringSupport ();
623 InitializeLanguage (TRUE
);
624 InitializeFrontPage (TRUE
);
627 // Do the platform init, can be customized by OEM/IBV
629 PERF_START (NULL
, "PlatformBds", "BDS", 0);
633 // Set up the device list based on EFI 1.1 variables
634 // process Driver#### and Load the driver's in the
635 // driver option list
637 BdsLibBuildOptionFromVar (&DriverOptionList
, L
"DriverOrder");
638 if (!IsListEmpty (&DriverOptionList
)) {
639 BdsLibLoadDrivers (&DriverOptionList
);
642 // Check if we have the boot next option
644 mBootNext
= BdsLibGetVariableAndSize (
646 &gEfiGlobalVariableGuid
,
651 // Setup some platform policy here
653 PlatformBdsPolicyBehavior (&DriverOptionList
, &BootOptionList
, BdsProcessCapsules
, BdsMemoryTest
);
654 PERF_END (NULL
, "PlatformBds", "BDS", 0);
657 // BDS select the boot device to load OS
659 BdsBootDeviceSelect ();
662 // Only assert here since this is the right behavior, we should never
663 // return back to DxeCore.
672 Set the variable and report the error through status code upon failure.
674 @param VariableName A Null-terminated string that is the name of the vendor's variable.
675 Each VariableName is unique for each VendorGuid. VariableName must
676 contain 1 or more characters. If VariableName is an empty string,
677 then EFI_INVALID_PARAMETER is returned.
678 @param VendorGuid A unique identifier for the vendor.
679 @param Attributes Attributes bitmask to set for the variable.
680 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
681 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
682 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
683 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
684 set, then a SetVariable() call with a DataSize of zero will not cause any change to
685 the variable value (the timestamp associated with the variable may be updated however
686 even if no new data value is provided,see the description of the
687 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
688 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
689 @param Data The contents for the variable.
691 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
692 defined by the Attributes.
693 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
694 DataSize exceeds the maximum allowed.
695 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
696 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
697 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
698 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
699 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
700 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
701 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
702 does NOT pass the validation check carried out by the firmware.
704 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
707 BdsDxeSetVariableAndReportStatusCodeOnError (
708 IN CHAR16
*VariableName
,
709 IN EFI_GUID
*VendorGuid
,
710 IN UINT32 Attributes
,
716 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
719 Status
= gRT
->SetVariable (
726 if (EFI_ERROR (Status
)) {
727 NameSize
= StrSize (VariableName
);
728 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
729 if (SetVariableStatus
!= NULL
) {
730 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
731 SetVariableStatus
->NameSize
= NameSize
;
732 SetVariableStatus
->DataSize
= DataSize
;
733 SetVariableStatus
->SetStatus
= Status
;
734 SetVariableStatus
->Attributes
= Attributes
;
735 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
736 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
738 REPORT_STATUS_CODE_EX (
740 PcdGet32 (PcdErrorCodeSetVariable
),
743 &gEdkiiStatusCodeDataTypeVariableGuid
,
745 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
748 FreePool (SetVariableStatus
);