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 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "FrontPage.h"
17 #include "HwErrRecSupport.h"
20 /// BDS arch protocol instance initial value.
22 /// Note: Current BDS not directly get the BootMode, DefaultBoot,
23 /// TimeoutDefault, MemoryTestLevel value from the BDS arch protocol.
24 /// Please refer to the library useage of BdsLibGetBootMode, BdsLibGetTimeout
25 /// and PlatformBdsDiagnostics in BdsPlatform.c
27 EFI_HANDLE gBdsHandle
= NULL
;
29 EFI_BDS_ARCH_PROTOCOL gBds
= {
33 UINT16
*mBootNext
= NULL
;
36 /// The read-only variables defined in UEFI Spec.
38 CHAR16
*mReadOnlyVariables
[] = {
43 L
"OsIndicationsSupported"
48 Install Boot Device Selection Protocol
50 @param ImageHandle The image handle.
51 @param SystemTable The system table.
53 @retval EFI_SUCEESS BDS has finished initializing.
54 Return the dispatcher and recall BDS.Entry
55 @retval Other Return status from AllocatePool() or gBS->InstallProtocolInterface
61 IN EFI_HANDLE ImageHandle
,
62 IN EFI_SYSTEM_TABLE
*SystemTable
68 // Install protocol interface
70 Status
= gBS
->InstallMultipleProtocolInterfaces (
72 &gEfiBdsArchProtocolGuid
, &gBds
,
75 ASSERT_EFI_ERROR (Status
);
82 This function attempts to boot for the boot order specified
93 BDS_COMMON_OPTION
*BootOption
;
99 BOOLEAN BootNextExist
;
100 LIST_ENTRY
*LinkBootNext
;
101 EFI_EVENT ConnectConInEvent
;
104 // Got the latest boot option
106 BootNextExist
= FALSE
;
108 ConnectConInEvent
= NULL
;
109 InitializeListHead (&BootLists
);
112 // First check the boot next option
114 ZeroMem (Buffer
, sizeof (Buffer
));
117 // Create Event to signal ConIn connection request
119 if (PcdGetBool (PcdConInConnectOnDemand
)) {
120 Status
= gBS
->CreateEventEx (
123 EfiEventEmptyFunction
,
125 &gConnectConInEventGuid
,
128 if (EFI_ERROR(Status
)) {
129 ConnectConInEvent
= NULL
;
133 if (mBootNext
!= NULL
) {
135 // Indicate we have the boot next variable, so this time
136 // boot will always have this boot option
138 BootNextExist
= TRUE
;
141 // Clear the this variable so it's only exist in this time boot
143 Status
= gRT
->SetVariable (
145 &gEfiGlobalVariableGuid
,
146 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
151 // Deleting variable with current variable implementation shouldn't fail.
153 ASSERT_EFI_ERROR (Status
);
156 // Add the boot next boot option
158 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *mBootNext
);
159 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
162 // If fail to get boot option from variable, just return and do nothing.
164 if (BootOption
== NULL
) {
168 BootOption
->BootCurrent
= *mBootNext
;
171 // Parse the boot order to get boot option
173 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
176 // When we didn't have chance to build boot option variables in the first
177 // full configuration boot (e.g.: Reset in the first page or in Device Manager),
178 // we have no boot options in the following mini configuration boot.
179 // Give the last chance to enumerate the boot options.
181 if (IsListEmpty (&BootLists
)) {
182 BdsLibEnumerateAllBootOption (&BootLists
);
185 Link
= BootLists
.ForwardLink
;
188 // Parameter check, make sure the loop will be valid
194 // Here we make the boot in a loop, every boot success will
195 // return to the front page
199 // Check the boot option list first
201 if (Link
== &BootLists
) {
203 // When LazyConIn enabled, signal connect ConIn event before enter UI
205 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
206 gBS
->SignalEvent (ConnectConInEvent
);
210 // There are two ways to enter here:
211 // 1. There is no active boot option, give user chance to
212 // add new boot option
213 // 2. All the active boot option processed, and there is no
214 // one is success to boot, then we back here to allow user
215 // add new active boot option
218 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
219 InitializeListHead (&BootLists
);
220 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
221 Link
= BootLists
.ForwardLink
;
225 // Get the boot option from the link list
227 BootOption
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
230 // According to EFI Specification, if a load option is not marked
231 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
234 if (!IS_LOAD_OPTION_TYPE (BootOption
->Attribute
, LOAD_OPTION_ACTIVE
)) {
236 // skip the header of the link list, because it has no boot option
238 Link
= Link
->ForwardLink
;
242 // Make sure the boot option device path connected,
243 // but ignore the BBS device path
245 if (DevicePathType (BootOption
->DevicePath
) != BBS_DEVICE_PATH
) {
247 // Notes: the internal shell can not been connected with device path
248 // so we do not check the status here
250 BdsLibConnectDevicePath (BootOption
->DevicePath
);
254 // Restore to original mode before launching boot option.
256 BdsSetConsoleMode (FALSE
);
259 // All the driver options should have been processed since
260 // now boot will be performed.
262 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
263 if (Status
!= EFI_SUCCESS
) {
265 // Call platform action to indicate the boot fail
267 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
268 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
271 // Check the next boot option
273 Link
= Link
->ForwardLink
;
277 // Call platform action to indicate the boot success
279 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
280 PlatformBdsBootSuccess (BootOption
);
283 // Boot success, then stop process the boot order, and
284 // present the boot manager menu, front page
288 // When LazyConIn enabled, signal connect ConIn Event before enter UI
290 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
291 gBS
->SignalEvent (ConnectConInEvent
);
295 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
298 // Rescan the boot option list, avoid potential risk of the boot
299 // option change in front page
302 LinkBootNext
= BootLists
.ForwardLink
;
305 InitializeListHead (&BootLists
);
306 if (LinkBootNext
!= NULL
) {
308 // Reserve the boot next option
310 InsertTailList (&BootLists
, LinkBootNext
);
313 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
314 Link
= BootLists
.ForwardLink
;
322 Validate input console variable data.
324 If found the device path is not a valid device path, remove the variable.
326 @param VariableName Input console variable name.
330 BdsFormalizeConsoleVariable (
331 IN CHAR16
*VariableName
334 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
338 DevicePath
= BdsLibGetVariableAndSize (
340 &gEfiGlobalVariableGuid
,
343 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
344 Status
= gRT
->SetVariable (
346 &gEfiGlobalVariableGuid
,
347 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
352 // Deleting variable with current variable implementation shouldn't fail.
354 ASSERT_EFI_ERROR (Status
);
360 Formalize Bds global variables.
362 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.
363 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
364 3. Delete OsIndications variable if it is not NV/BS/RT UINT64
365 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
369 BdsFormalizeEfiGlobalVariable (
374 UINT64 OsIndicationSupport
;
380 // Validate Console variable.
382 BdsFormalizeConsoleVariable (L
"ConIn");
383 BdsFormalizeConsoleVariable (L
"ConOut");
384 BdsFormalizeConsoleVariable (L
"ErrOut");
387 // OS indicater support variable
389 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI \
390 | EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED
;
392 BdsDxeSetVariableAndReportStatusCodeOnError (
393 L
"OsIndicationsSupported",
394 &gEfiGlobalVariableGuid
,
395 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
401 // If OsIndications is invalid, remove it.
403 // 1. Data size != UINT64
404 // 2. OsIndication value inconsistence
405 // 3. OsIndication attribute inconsistence
409 DataSize
= sizeof(UINT64
);
410 Status
= gRT
->GetVariable (
412 &gEfiGlobalVariableGuid
,
418 if (!EFI_ERROR(Status
)) {
419 if (DataSize
!= sizeof(UINT64
) ||
420 (OsIndication
& ~OsIndicationSupport
) != 0 ||
421 Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
)){
423 DEBUG ((EFI_D_ERROR
, "Unformalized OsIndications variable exists. Delete it\n"));
424 Status
= gRT
->SetVariable (
426 &gEfiGlobalVariableGuid
,
432 // Deleting variable with current variable implementation shouldn't fail.
434 ASSERT_EFI_ERROR (Status
);
442 Service routine for BdsInstance->Entry(). Devices are connected, the
443 consoles are initialized, and the boot options are tried.
445 @param This Protocol Instance structure.
451 IN EFI_BDS_ARCH_PROTOCOL
*This
454 LIST_ENTRY DriverOptionList
;
455 LIST_ENTRY BootOptionList
;
457 CHAR16
*FirmwareVendor
;
461 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
464 // Insert the performance probe
466 PERF_END (NULL
, "DXE", NULL
, 0);
467 PERF_START (NULL
, "BDS", NULL
, 0);
470 // Initialize the global system boot option and driver option
472 InitializeListHead (&DriverOptionList
);
473 InitializeListHead (&BootOptionList
);
476 // Initialize hotkey service
478 InitializeHotkeyService ();
481 // Fill in FirmwareVendor and FirmwareRevision from PCDs
483 FirmwareVendor
= (CHAR16
*)PcdGetPtr (PcdFirmwareVendor
);
484 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
485 ASSERT (gST
->FirmwareVendor
!= NULL
);
486 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
489 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
492 gBS
->CalculateCrc32 ((VOID
*)gST
, sizeof(EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
495 // Validate Variable.
497 BdsFormalizeEfiGlobalVariable();
500 // Mark the read-only variables if the Variable Lock protocol exists
502 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
503 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
504 if (!EFI_ERROR (Status
)) {
505 for (Index
= 0; Index
< ARRAY_SIZE (mReadOnlyVariables
); Index
++) {
506 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
507 ASSERT_EFI_ERROR (Status
);
512 // Report Status Code to indicate connecting drivers will happen
516 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
519 InitializeHwErrRecSupport();
522 // Initialize L"Timeout" EFI global variable.
524 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
525 if (BootTimeOut
!= 0xFFFF) {
527 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
528 // define same behavior between no value or 0xFFFF value for L"Timeout".
530 BdsDxeSetVariableAndReportStatusCodeOnError (
532 &gEfiGlobalVariableGuid
,
533 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
540 // bugbug: platform specific code
541 // Initialize the platform specific string and language
543 InitializeStringSupport ();
544 InitializeLanguage (TRUE
);
545 InitializeFrontPage (TRUE
);
548 // Do the platform init, can be customized by OEM/IBV
550 PERF_START (NULL
, "PlatformBds", "BDS", 0);
554 // Set up the device list based on EFI 1.1 variables
555 // process Driver#### and Load the driver's in the
556 // driver option list
558 BdsLibBuildOptionFromVar (&DriverOptionList
, L
"DriverOrder");
559 if (!IsListEmpty (&DriverOptionList
)) {
560 BdsLibLoadDrivers (&DriverOptionList
);
563 // Check if we have the boot next option
565 mBootNext
= BdsLibGetVariableAndSize (
567 &gEfiGlobalVariableGuid
,
572 // Setup some platform policy here
574 PlatformBdsPolicyBehavior (&DriverOptionList
, &BootOptionList
, BdsProcessCapsules
, BdsMemoryTest
);
575 PERF_END (NULL
, "PlatformBds", "BDS", 0);
578 // BDS select the boot device to load OS
580 BdsBootDeviceSelect ();
583 // Only assert here since this is the right behavior, we should never
584 // return back to DxeCore.
593 Set the variable and report the error through status code upon failure.
595 @param VariableName A Null-terminated string that is the name of the vendor's variable.
596 Each VariableName is unique for each VendorGuid. VariableName must
597 contain 1 or more characters. If VariableName is an empty string,
598 then EFI_INVALID_PARAMETER is returned.
599 @param VendorGuid A unique identifier for the vendor.
600 @param Attributes Attributes bitmask to set for the variable.
601 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
602 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
603 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
604 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
605 set, then a SetVariable() call with a DataSize of zero will not cause any change to
606 the variable value (the timestamp associated with the variable may be updated however
607 even if no new data value is provided,see the description of the
608 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
609 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
610 @param Data The contents for the variable.
612 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
613 defined by the Attributes.
614 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
615 DataSize exceeds the maximum allowed.
616 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
617 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
618 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
619 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
620 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
621 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
622 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
623 does NOT pass the validation check carried out by the firmware.
625 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
628 BdsDxeSetVariableAndReportStatusCodeOnError (
629 IN CHAR16
*VariableName
,
630 IN EFI_GUID
*VendorGuid
,
631 IN UINT32 Attributes
,
637 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
640 Status
= gRT
->SetVariable (
647 if (EFI_ERROR (Status
)) {
648 NameSize
= StrSize (VariableName
);
649 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
650 if (SetVariableStatus
!= NULL
) {
651 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
652 SetVariableStatus
->NameSize
= NameSize
;
653 SetVariableStatus
->DataSize
= DataSize
;
654 SetVariableStatus
->SetStatus
= Status
;
655 SetVariableStatus
->Attributes
= Attributes
;
656 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
657 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
659 REPORT_STATUS_CODE_EX (
661 PcdGet32 (PcdErrorCodeSetVariable
),
664 &gEdkiiStatusCodeDataTypeVariableGuid
,
666 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
669 FreePool (SetVariableStatus
);