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 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 This function attempts to boot for the boot order specified
99 BDS_COMMON_OPTION
*BootOption
;
103 LIST_ENTRY BootLists
;
105 BOOLEAN BootNextExist
;
106 LIST_ENTRY
*LinkBootNext
;
107 EFI_EVENT ConnectConInEvent
;
110 // Got the latest boot option
112 BootNextExist
= FALSE
;
114 ConnectConInEvent
= NULL
;
115 InitializeListHead (&BootLists
);
118 // First check the boot next option
120 ZeroMem (Buffer
, sizeof (Buffer
));
123 // Create Event to signal ConIn connection request
125 if (PcdGetBool (PcdConInConnectOnDemand
)) {
126 Status
= gBS
->CreateEventEx (
129 EfiEventEmptyFunction
,
131 &gConnectConInEventGuid
,
134 if (EFI_ERROR(Status
)) {
135 ConnectConInEvent
= NULL
;
139 if (mBootNext
!= NULL
) {
141 // Indicate we have the boot next variable, so this time
142 // boot will always have this boot option
144 BootNextExist
= TRUE
;
147 // Clear the this variable so it's only exist in this time boot
149 Status
= gRT
->SetVariable (
151 &gEfiGlobalVariableGuid
,
152 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
157 // Deleting variable with current variable implementation shouldn't fail.
159 ASSERT_EFI_ERROR (Status
);
162 // Add the boot next boot option
164 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *mBootNext
);
165 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
168 // If fail to get boot option from variable, just return and do nothing.
170 if (BootOption
== NULL
) {
174 BootOption
->BootCurrent
= *mBootNext
;
177 // Parse the boot order to get boot option
179 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
182 // When we didn't have chance to build boot option variables in the first
183 // full configuration boot (e.g.: Reset in the first page or in Device Manager),
184 // we have no boot options in the following mini configuration boot.
185 // Give the last chance to enumerate the boot options.
187 if (IsListEmpty (&BootLists
)) {
188 BdsLibEnumerateAllBootOption (&BootLists
);
191 Link
= BootLists
.ForwardLink
;
194 // Parameter check, make sure the loop will be valid
200 // Here we make the boot in a loop, every boot success will
201 // return to the front page
205 // Check the boot option list first
207 if (Link
== &BootLists
) {
209 // When LazyConIn enabled, signal connect ConIn event before enter UI
211 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
212 gBS
->SignalEvent (ConnectConInEvent
);
216 // There are two ways to enter here:
217 // 1. There is no active boot option, give user chance to
218 // add new boot option
219 // 2. All the active boot option processed, and there is no
220 // one is success to boot, then we back here to allow user
221 // add new active boot option
224 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
225 InitializeListHead (&BootLists
);
226 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
227 Link
= BootLists
.ForwardLink
;
231 // Get the boot option from the link list
233 BootOption
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
236 // According to EFI Specification, if a load option is not marked
237 // as LOAD_OPTION_ACTIVE, the boot manager will not automatically
240 if (!IS_LOAD_OPTION_TYPE (BootOption
->Attribute
, LOAD_OPTION_ACTIVE
)) {
242 // skip the header of the link list, because it has no boot option
244 Link
= Link
->ForwardLink
;
248 // Make sure the boot option device path connected,
249 // but ignore the BBS device path
251 if (DevicePathType (BootOption
->DevicePath
) != BBS_DEVICE_PATH
) {
253 // Notes: the internal shell can not been connected with device path
254 // so we do not check the status here
256 BdsLibConnectDevicePath (BootOption
->DevicePath
);
260 // Restore to original mode before launching boot option.
262 BdsSetConsoleMode (FALSE
);
265 // All the driver options should have been processed since
266 // now boot will be performed.
268 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
269 if (Status
!= EFI_SUCCESS
) {
271 // Call platform action to indicate the boot fail
273 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
274 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
277 // Check the next boot option
279 Link
= Link
->ForwardLink
;
283 // Call platform action to indicate the boot success
285 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
286 PlatformBdsBootSuccess (BootOption
);
289 // Boot success, then stop process the boot order, and
290 // present the boot manager menu, front page
294 // When LazyConIn enabled, signal connect ConIn Event before enter UI
296 if (PcdGetBool (PcdConInConnectOnDemand
) && ConnectConInEvent
!= NULL
) {
297 gBS
->SignalEvent (ConnectConInEvent
);
301 PlatformBdsEnterFrontPage (Timeout
, FALSE
);
304 // Rescan the boot option list, avoid potential risk of the boot
305 // option change in front page
308 LinkBootNext
= BootLists
.ForwardLink
;
311 InitializeListHead (&BootLists
);
312 if (LinkBootNext
!= NULL
) {
314 // Reserve the boot next option
316 InsertTailList (&BootLists
, LinkBootNext
);
319 BdsLibBuildOptionFromVar (&BootLists
, L
"BootOrder");
320 Link
= BootLists
.ForwardLink
;
328 Validate input console variable data.
330 If found the device path is not a valid device path, remove the variable.
332 @param VariableName Input console variable name.
336 BdsFormalizeConsoleVariable (
337 IN CHAR16
*VariableName
340 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
344 DevicePath
= BdsLibGetVariableAndSize (
346 &gEfiGlobalVariableGuid
,
349 if ((DevicePath
!= NULL
) && !IsDevicePathValid (DevicePath
, VariableSize
)) {
350 Status
= gRT
->SetVariable (
352 &gEfiGlobalVariableGuid
,
353 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
358 // Deleting variable with current variable implementation shouldn't fail.
360 ASSERT_EFI_ERROR (Status
);
366 Formalize Bds global variables.
368 1. For ConIn/ConOut/ConErr, if found the device path is not a valid device path, remove the variable.
369 2. For OsIndicationsSupported, Create a BS/RT/UINT64 variable to report caps
370 3. Delete OsIndications variable if it is not NV/BS/RT UINT64
371 Item 3 is used to solve case when OS corrupts OsIndications. Here simply delete this NV variable.
375 BdsFormalizeEfiGlobalVariable (
380 UINT64 OsIndicationSupport
;
386 // Validate Console variable.
388 BdsFormalizeConsoleVariable (L
"ConIn");
389 BdsFormalizeConsoleVariable (L
"ConOut");
390 BdsFormalizeConsoleVariable (L
"ErrOut");
393 // OS indicater support variable
395 OsIndicationSupport
= EFI_OS_INDICATIONS_BOOT_TO_FW_UI \
396 | EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED
;
398 BdsDxeSetVariableAndReportStatusCodeOnError (
399 L
"OsIndicationsSupported",
400 &gEfiGlobalVariableGuid
,
401 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
407 // If OsIndications is invalid, remove it.
409 // 1. Data size != UINT64
410 // 2. OsIndication value inconsistence
411 // 3. OsIndication attribute inconsistence
415 DataSize
= sizeof(UINT64
);
416 Status
= gRT
->GetVariable (
418 &gEfiGlobalVariableGuid
,
424 if (!EFI_ERROR(Status
)) {
425 if (DataSize
!= sizeof(UINT64
) ||
426 (OsIndication
& ~OsIndicationSupport
) != 0 ||
427 Attributes
!= (EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
)){
429 DEBUG ((EFI_D_ERROR
, "Unformalized OsIndications variable exists. Delete it\n"));
430 Status
= gRT
->SetVariable (
432 &gEfiGlobalVariableGuid
,
438 // Deleting variable with current variable implementation shouldn't fail.
440 ASSERT_EFI_ERROR (Status
);
448 Service routine for BdsInstance->Entry(). Devices are connected, the
449 consoles are initialized, and the boot options are tried.
451 @param This Protocol Instance structure.
457 IN EFI_BDS_ARCH_PROTOCOL
*This
460 LIST_ENTRY DriverOptionList
;
461 LIST_ENTRY BootOptionList
;
463 CHAR16
*FirmwareVendor
;
467 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
470 // Insert the performance probe
472 PERF_END (NULL
, "DXE", NULL
, 0);
473 PERF_START (NULL
, "BDS", NULL
, 0);
476 // Initialize the global system boot option and driver option
478 InitializeListHead (&DriverOptionList
);
479 InitializeListHead (&BootOptionList
);
482 // Initialize hotkey service
484 InitializeHotkeyService ();
487 // Fill in FirmwareVendor and FirmwareRevision from PCDs
489 FirmwareVendor
= (CHAR16
*)PcdGetPtr (PcdFirmwareVendor
);
490 gST
->FirmwareVendor
= AllocateRuntimeCopyPool (StrSize (FirmwareVendor
), FirmwareVendor
);
491 ASSERT (gST
->FirmwareVendor
!= NULL
);
492 gST
->FirmwareRevision
= PcdGet32 (PcdFirmwareRevision
);
495 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
498 gBS
->CalculateCrc32 ((VOID
*)gST
, sizeof(EFI_SYSTEM_TABLE
), &gST
->Hdr
.CRC32
);
501 // Validate Variable.
503 BdsFormalizeEfiGlobalVariable();
506 // Mark the read-only variables if the Variable Lock protocol exists
508 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
509 DEBUG ((EFI_D_INFO
, "[BdsDxe] Locate Variable Lock protocol - %r\n", Status
));
510 if (!EFI_ERROR (Status
)) {
511 for (Index
= 0; Index
< ARRAY_SIZE (mReadOnlyVariables
); Index
++) {
512 Status
= VariableLock
->RequestToLock (VariableLock
, mReadOnlyVariables
[Index
], &gEfiGlobalVariableGuid
);
513 ASSERT_EFI_ERROR (Status
);
518 // Report Status Code to indicate connecting drivers will happen
522 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_BEGIN_CONNECTING_DRIVERS
)
525 InitializeHwErrRecSupport();
528 // Initialize L"Timeout" EFI global variable.
530 BootTimeOut
= PcdGet16 (PcdPlatformBootTimeOut
);
531 if (BootTimeOut
!= 0xFFFF) {
533 // If time out value equal 0xFFFF, no need set to 0xFFFF to variable area because UEFI specification
534 // define same behavior between no value or 0xFFFF value for L"Timeout".
536 BdsDxeSetVariableAndReportStatusCodeOnError (
538 &gEfiGlobalVariableGuid
,
539 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
546 // bugbug: platform specific code
547 // Initialize the platform specific string and language
549 InitializeStringSupport ();
550 InitializeLanguage (TRUE
);
551 InitializeFrontPage (TRUE
);
554 // Do the platform init, can be customized by OEM/IBV
556 PERF_START (NULL
, "PlatformBds", "BDS", 0);
560 // Set up the device list based on EFI 1.1 variables
561 // process Driver#### and Load the driver's in the
562 // driver option list
564 BdsLibBuildOptionFromVar (&DriverOptionList
, L
"DriverOrder");
565 if (!IsListEmpty (&DriverOptionList
)) {
566 BdsLibLoadDrivers (&DriverOptionList
);
569 // Check if we have the boot next option
571 mBootNext
= BdsLibGetVariableAndSize (
573 &gEfiGlobalVariableGuid
,
578 // Setup some platform policy here
580 PlatformBdsPolicyBehavior (&DriverOptionList
, &BootOptionList
, BdsProcessCapsules
, BdsMemoryTest
);
581 PERF_END (NULL
, "PlatformBds", "BDS", 0);
584 // BDS select the boot device to load OS
586 BdsBootDeviceSelect ();
589 // Only assert here since this is the right behavior, we should never
590 // return back to DxeCore.
599 Set the variable and report the error through status code upon failure.
601 @param VariableName A Null-terminated string that is the name of the vendor's variable.
602 Each VariableName is unique for each VendorGuid. VariableName must
603 contain 1 or more characters. If VariableName is an empty string,
604 then EFI_INVALID_PARAMETER is returned.
605 @param VendorGuid A unique identifier for the vendor.
606 @param Attributes Attributes bitmask to set for the variable.
607 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
608 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
609 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
610 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
611 set, then a SetVariable() call with a DataSize of zero will not cause any change to
612 the variable value (the timestamp associated with the variable may be updated however
613 even if no new data value is provided,see the description of the
614 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
615 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
616 @param Data The contents for the variable.
618 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
619 defined by the Attributes.
620 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
621 DataSize exceeds the maximum allowed.
622 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
623 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
624 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
625 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
626 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
627 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
628 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
629 does NOT pass the validation check carried out by the firmware.
631 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
634 BdsDxeSetVariableAndReportStatusCodeOnError (
635 IN CHAR16
*VariableName
,
636 IN EFI_GUID
*VendorGuid
,
637 IN UINT32 Attributes
,
643 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
646 Status
= gRT
->SetVariable (
653 if (EFI_ERROR (Status
)) {
654 NameSize
= StrSize (VariableName
);
655 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
656 if (SetVariableStatus
!= NULL
) {
657 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
658 SetVariableStatus
->NameSize
= NameSize
;
659 SetVariableStatus
->DataSize
= DataSize
;
660 SetVariableStatus
->SetStatus
= Status
;
661 SetVariableStatus
->Attributes
= Attributes
;
662 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
663 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
665 REPORT_STATUS_CODE_EX (
667 PcdGet32 (PcdErrorCodeSetVariable
),
670 &gEdkiiStatusCodeDataTypeVariableGuid
,
672 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
675 FreePool (SetVariableStatus
);