2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
18 BOOLEAN mEnumBootDevice
= FALSE
;
19 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
22 /// This GUID is used for an EFI Variable that stores the front device pathes
23 /// for a partial device path that starts with the HD node.
25 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 /// This GUID is used for register UNI string.
30 EFI_GUID mBdsLibStringPackGuid
= { 0x3b4d9b23, 0x95ac, 0x44f6, { 0x9f, 0xcd, 0xe, 0x95, 0x94, 0x58, 0x6c, 0x72 } };
33 /// This GUID is used for Set/Get platform language into/from variable at last time enumeration to ensure the enumeration will
34 /// only execute once.
36 EFI_GUID mBdsLibLastLangGuid
= { 0xe8c545b, 0xa2ee, 0x470d, { 0x8e, 0x26, 0xbd, 0xa1, 0xa1, 0x3c, 0xa, 0xa3 } };
39 The constructor function register UNI strings into imageHandle.
41 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
46 @retval EFI_SUCCESS The constructor successfully added string package.
47 @retval Other value The constructor can't add string package.
52 GenericBdsLibConstructor (
53 IN EFI_HANDLE ImageHandle
,
54 IN EFI_SYSTEM_TABLE
*SystemTable
58 gBdsLibStringPackHandle
= HiiAddPackages (
59 &mBdsLibStringPackGuid
,
65 ASSERT (gBdsLibStringPackHandle
!= NULL
);
73 Boot the legacy system with the boot option
75 @param Option The legacy boot option which have BBS device path
77 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
79 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
84 IN BDS_COMMON_OPTION
*Option
88 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
90 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
91 if (EFI_ERROR (Status
)) {
93 // If no LegacyBios protocol we do not support legacy boot
95 return EFI_UNSUPPORTED
;
98 // Notes: if we separate the int 19, then we don't need to refresh BBS
100 BdsRefreshBbsTableForBoot (Option
);
103 // Write boot to OS performance data for legacy boot.
106 WriteBootToOsPerformanceData ();
109 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
110 return LegacyBios
->LegacyBoot (
112 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
113 Option
->LoadOptionsSize
,
119 Internal function to check if the input boot option is a valid EFI NV Boot####.
121 @param OptionToCheck Boot option to be checked.
123 @retval TRUE This boot option matches a valid EFI NV Boot####.
124 @retval FALSE If not.
128 IsBootOptionValidNVVarialbe (
129 IN BDS_COMMON_OPTION
*OptionToCheck
133 BDS_COMMON_OPTION
*BootOption
;
135 CHAR16 OptionName
[20];
139 InitializeListHead (&TempList
);
140 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
142 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
143 if (BootOption
== NULL
) {
148 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
149 // valid EFI NV Boot####.
151 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
152 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
157 FreePool (BootOption
);
162 Process the boot option follow the UEFI specification and
163 special treat the legacy boot option with BBS_DEVICE_PATH.
165 @param Option The boot option need to be processed
166 @param DevicePath The device path which describe where to load the
167 boot image or the legacy BBS device path to boot
169 @param ExitDataSize The size of exit data.
170 @param ExitData Data returned when Boot image failed.
172 @retval EFI_SUCCESS Boot from the input boot option successfully.
173 @retval EFI_NOT_FOUND If the Device Path is not found in the system
178 BdsLibBootViaBootOption (
179 IN BDS_COMMON_OPTION
*Option
,
180 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
181 OUT UINTN
*ExitDataSize
,
182 OUT CHAR16
**ExitData OPTIONAL
187 EFI_HANDLE ImageHandle
;
188 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
189 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
190 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
191 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
192 LIST_ENTRY TempBootLists
;
195 // Record the performance data for End of BDS
197 PERF_END(NULL
, "BDS", NULL
, 0);
203 // Notes: this code can be remove after the s3 script table
204 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
205 // EVT_SIGNAL_LEGACY_BOOT
207 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
208 if (!EFI_ERROR (Status
)) {
209 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
212 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
215 WorkingDevicePath
= NULL
;
216 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
217 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
218 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
219 (HARDDRIVE_DEVICE_PATH
*)DevicePath
221 if (WorkingDevicePath
!= NULL
) {
222 DevicePath
= WorkingDevicePath
;
226 // Signal the EVT_SIGNAL_READY_TO_BOOT event
228 EfiSignalEventReadyToBoot();
234 if (IsBootOptionValidNVVarialbe (Option
)) {
236 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
237 // In this case, "BootCurrent" is not created.
238 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
242 &gEfiGlobalVariableGuid
,
243 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
249 ASSERT (Option
->DevicePath
!= NULL
);
250 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
251 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
254 // Check to see if we should legacy BOOT. If yes then do the legacy boot
256 return BdsLibDoLegacyBoot (Option
);
260 // If the boot option point to Internal FV shell, make sure it is valid
262 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
263 if (!EFI_ERROR(Status
)) {
264 if (Option
->DevicePath
!= NULL
) {
265 FreePool(Option
->DevicePath
);
267 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
268 ASSERT(Option
->DevicePath
!= NULL
);
269 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
271 // Update the shell boot option
273 InitializeListHead (&TempBootLists
);
274 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
277 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
279 FreePool (DevicePath
);
280 DevicePath
= Option
->DevicePath
;
285 if (Option
->Description
== NULL
) {
286 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
288 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
293 Status
= gBS
->LoadImage (
303 // If we didn't find an image directly, we need to try as if it is a removable device boot option
304 // and load the image according to the default boot behavior for removable device.
306 if (EFI_ERROR (Status
)) {
308 // check if there is a bootable removable media could be found in this device path ,
309 // and get the bootable media handle
311 Handle
= BdsLibGetBootableHandle(DevicePath
);
312 if (Handle
== NULL
) {
316 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
317 // machinename is ia32, ia64, x64, ...
319 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
320 if (FilePath
!= NULL
) {
321 Status
= gBS
->LoadImage (
329 if (EFI_ERROR (Status
)) {
331 // The DevicePath failed, and it's not a valid
332 // removable media device.
339 if (EFI_ERROR (Status
)) {
341 // It there is any error from the Boot attempt exit now.
346 // Provide the image with it's load options
348 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
349 ASSERT_EFI_ERROR (Status
);
351 if (Option
->LoadOptionsSize
!= 0) {
352 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
353 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
356 // Before calling the image, enable the Watchdog Timer for
357 // the 5 Minute period
359 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
362 // Write boot to OS performance data for UEFI boot
365 WriteBootToOsPerformanceData ();
368 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
369 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
372 // Clear the Watchdog Timer after the image returns
374 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
378 // Clear Boot Current
382 &gEfiGlobalVariableGuid
,
383 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
393 Expand a device path that starts with a hard drive media device path node to be a
394 full device path that includes the full hardware path to the device. We need
395 to do this so it can be booted. As an optimization the front match (the part point
396 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
397 so a connect all is not required on every boot. All successful history device path
398 which point to partition node (the front part) will be saved.
400 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
401 drive media device path.
402 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
406 EFI_DEVICE_PATH_PROTOCOL
*
408 BdsExpandPartitionPartialDevicePathToFull (
409 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
413 UINTN BlockIoHandleCount
;
414 EFI_HANDLE
*BlockIoBuffer
;
415 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
416 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
417 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
420 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
421 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
422 UINTN CachedDevicePathSize
;
425 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
428 FullDevicePath
= NULL
;
430 // Check if there is prestore 'HDDP' variable.
431 // If exist, search the front path which point to partition node in the variable instants.
432 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
434 CachedDevicePath
= BdsLibGetVariableAndSize (
436 &mHdBootVariablePrivateGuid
,
437 &CachedDevicePathSize
440 if (CachedDevicePath
!= NULL
) {
441 TempNewDevicePath
= CachedDevicePath
;
446 // Check every instance of the variable
447 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
448 // partial partition boot option. Second, check whether the instance could be connected.
450 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
451 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
453 // Connect the device path instance, the device path point to hard drive media device path node
454 // e.g. ACPI() /PCI()/ATA()/Partition()
456 Status
= BdsLibConnectDevicePath (Instance
);
457 if (!EFI_ERROR (Status
)) {
463 // Come here means the first instance is not matched
467 } while (TempNewDevicePath
!= NULL
);
471 // Find the matched device path.
472 // Append the file path information from the boot option and return the fully expanded device path.
474 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
475 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
478 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
482 // First delete the matched instance.
484 TempNewDevicePath
= CachedDevicePath
;
485 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
486 FreePool (TempNewDevicePath
);
489 // Second, append the remaining path after the matched instance
491 TempNewDevicePath
= CachedDevicePath
;
492 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
493 FreePool (TempNewDevicePath
);
495 // Save the matching Device Path so we don't need to do a connect all next time
497 Status
= gRT
->SetVariable (
499 &mHdBootVariablePrivateGuid
,
500 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
501 GetDevicePathSize (CachedDevicePath
),
507 FreePool (CachedDevicePath
);
508 return FullDevicePath
;
513 // If we get here we fail to find or 'HDDP' not exist, and now we need
514 // to search all devices in the system for a matched partition
516 BdsLibConnectAllDriversToAllControllers ();
517 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
518 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
520 // If there was an error or there are no device handles that support
521 // the BLOCK_IO Protocol, then return.
526 // Loop through all the device handles that support the BLOCK_IO Protocol
528 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
530 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
531 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
535 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
537 // Find the matched partition device path
539 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
540 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
543 // Save the matched partition device path in 'HDDP' variable
545 if (CachedDevicePath
!= NULL
) {
547 // Save the matched partition device path as first instance of 'HDDP' variable
549 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
550 TempNewDevicePath
= CachedDevicePath
;
551 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
552 FreePool(TempNewDevicePath
);
554 TempNewDevicePath
= CachedDevicePath
;
555 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
556 if (TempNewDevicePath
!= NULL
) {
557 FreePool(TempNewDevicePath
);
560 TempNewDevicePath
= CachedDevicePath
;
561 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
562 FreePool(TempNewDevicePath
);
565 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
566 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
569 ASSERT (CachedDevicePath
!= NULL
);
570 TempNewDevicePath
= CachedDevicePath
;
571 while (!IsDevicePathEnd (TempNewDevicePath
)) {
572 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
574 // Parse one instance
576 while (!IsDevicePathEndType (TempNewDevicePath
)) {
577 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
581 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
583 if (InstanceNum
>= 12) {
584 SetDevicePathEndNode (TempNewDevicePath
);
589 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
593 // Save the matching Device Path so we don't need to do a connect all next time
595 Status
= gRT
->SetVariable (
597 &mHdBootVariablePrivateGuid
,
598 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
599 GetDevicePathSize (CachedDevicePath
),
607 if (CachedDevicePath
!= NULL
) {
608 FreePool (CachedDevicePath
);
610 if (BlockIoBuffer
!= NULL
) {
611 FreePool (BlockIoBuffer
);
613 return FullDevicePath
;
617 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
618 instances, has the same partition node with HardDriveDevicePath device path
620 @param BlockIoDevicePath Multi device path instances which need to check
621 @param HardDriveDevicePath A device path which starts with a hard drive media
624 @retval TRUE There is a matched device path instance.
625 @retval FALSE There is no matched device path instance.
630 MatchPartitionDevicePathNode (
631 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
632 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
635 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
636 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
638 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
640 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
645 // Make PreviousDevicePath == the device path node before the end node
647 DevicePath
= BlockIoDevicePath
;
648 BlockIoHdDevicePathNode
= NULL
;
651 // find the partition device path node
653 while (!IsDevicePathEnd (DevicePath
)) {
654 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
655 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
657 BlockIoHdDevicePathNode
= DevicePath
;
661 DevicePath
= NextDevicePathNode (DevicePath
);
664 if (BlockIoHdDevicePathNode
== NULL
) {
668 // See if the harddrive device path in blockio matches the orig Hard Drive Node
670 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
674 // Check for the match
676 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
677 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
678 switch (TmpHdPath
->SignatureType
) {
679 case SIGNATURE_TYPE_GUID
:
680 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
682 case SIGNATURE_TYPE_MBR
:
683 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
695 Delete the boot option associated with the handle passed in.
697 @param Handle The handle which present the device path to create
700 @retval EFI_SUCCESS Delete the boot option success
701 @retval EFI_NOT_FOUND If the Device Path is not found in the system
702 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
703 @retval Other Error return value from SetVariable()
707 BdsLibDeleteOptionFromHandle (
712 UINT8
*BootOptionVar
;
714 UINTN BootOptionSize
;
717 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
718 UINTN DevicePathSize
;
719 UINTN OptionDevicePathSize
;
720 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
721 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
724 Status
= EFI_SUCCESS
;
729 // Check "BootOrder" variable, if no, means there is no any boot order.
731 BootOrder
= BdsLibGetVariableAndSize (
733 &gEfiGlobalVariableGuid
,
736 if (BootOrder
== NULL
) {
737 return EFI_NOT_FOUND
;
741 // Convert device handle to device path protocol instance
743 DevicePath
= DevicePathFromHandle (Handle
);
744 if (DevicePath
== NULL
) {
745 return EFI_NOT_FOUND
;
747 DevicePathSize
= GetDevicePathSize (DevicePath
);
750 // Loop all boot order variable and find the matching device path
753 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
754 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
755 BootOptionVar
= BdsLibGetVariableAndSize (
757 &gEfiGlobalVariableGuid
,
761 if (BootOptionVar
== NULL
) {
762 FreePool (BootOrder
);
763 return EFI_OUT_OF_RESOURCES
;
766 TempPtr
= BootOptionVar
;
767 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
768 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
769 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
770 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
773 // Check whether the device path match
775 if ((OptionDevicePathSize
== DevicePathSize
) &&
776 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
777 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
778 FreePool (BootOptionVar
);
782 FreePool (BootOptionVar
);
787 // Adjust number of boot option for "BootOrder" variable.
789 Status
= gRT
->SetVariable (
791 &gEfiGlobalVariableGuid
,
792 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
797 FreePool (BootOrder
);
804 Delete all invalid EFI boot options.
806 @retval EFI_SUCCESS Delete all invalid boot option success
807 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
808 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
809 @retval Other Error return value from SetVariable()
813 BdsDeleteAllInvalidEfiBootOption (
818 UINT8
*BootOptionVar
;
820 UINTN BootOptionSize
;
824 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
825 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
829 Status
= EFI_SUCCESS
;
834 // Check "BootOrder" variable firstly, this variable hold the number of boot options
836 BootOrder
= BdsLibGetVariableAndSize (
838 &gEfiGlobalVariableGuid
,
841 if (NULL
== BootOrder
) {
842 return EFI_NOT_FOUND
;
846 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
847 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
848 BootOptionVar
= BdsLibGetVariableAndSize (
850 &gEfiGlobalVariableGuid
,
853 if (NULL
== BootOptionVar
) {
854 FreePool (BootOrder
);
855 return EFI_OUT_OF_RESOURCES
;
858 TempPtr
= BootOptionVar
;
859 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
860 Description
= (CHAR16
*) TempPtr
;
861 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
862 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
865 // Skip legacy boot option (BBS boot device)
867 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
868 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
869 FreePool (BootOptionVar
);
874 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
876 // Delete this invalid boot option "Boot####"
878 Status
= gRT
->SetVariable (
880 &gEfiGlobalVariableGuid
,
881 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
886 // Mark this boot option in boot order as deleted
888 BootOrder
[Index
] = 0xffff;
891 FreePool (BootOptionVar
);
896 // Adjust boot order array
899 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
900 if (BootOrder
[Index
] != 0xffff) {
901 BootOrder
[Index2
] = BootOrder
[Index
];
905 Status
= gRT
->SetVariable (
907 &gEfiGlobalVariableGuid
,
908 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
909 Index2
* sizeof (UINT16
),
913 FreePool (BootOrder
);
920 For EFI boot option, BDS separate them as six types:
921 1. Network - The boot option points to the SimpleNetworkProtocol device.
922 Bds will try to automatically create this type boot option when enumerate.
923 2. Shell - The boot option points to internal flash shell.
924 Bds will try to automatically create this type boot option when enumerate.
925 3. Removable BlockIo - The boot option only points to the removable media
926 device, like USB flash disk, DVD, Floppy etc.
927 These device should contain a *removable* blockIo
928 protocol in their device handle.
929 Bds will try to automatically create this type boot option
931 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
933 These device should contain a *fixed* blockIo
934 protocol in their device handle.
935 BDS will skip fixed blockIo devices, and NOT
936 automatically create boot option for them. But BDS
937 will help to delete those fixed blockIo boot option,
938 whose description rule conflict with other auto-created
940 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
941 has SimpleFileSystem Protocol, but has no blockio
942 protocol. These devices do not offer blockIo
943 protocol, but BDS still can get the
944 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
946 6. File - The boot option points to a file. These boot options are usually
947 created by user manually or OS loader. BDS will not delete or modify
950 This function will enumerate all possible boot device in the system, and
951 automatically create boot options for Network, Shell, Removable BlockIo,
952 and Non-BlockIo Simplefile devices.
953 It will only execute once of every boot.
955 @param BdsBootOptionList The header of the link list which indexed all
958 @retval EFI_SUCCESS Finished all the boot device enumerate and create
959 the boot option base on that boot device
961 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
965 BdsLibEnumerateAllBootOption (
966 IN OUT LIST_ENTRY
*BdsBootOptionList
975 UINT16 NonBlockNumber
;
976 UINTN NumberBlockIoHandles
;
977 EFI_HANDLE
*BlockIoHandles
;
978 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
980 UINTN NumOfLoadFileHandles
;
981 EFI_HANDLE
*LoadFileHandles
;
983 EFI_HANDLE
*FvHandleBuffer
;
984 EFI_FV_FILETYPE Type
;
986 EFI_FV_FILE_ATTRIBUTES Attributes
;
987 UINT32 AuthenticationStatus
;
988 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
989 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
990 UINTN DevicePathType
;
992 EFI_HANDLE
*FileSystemHandles
;
993 UINTN NumberFileSystemHandles
;
995 EFI_IMAGE_DOS_HEADER DosHeader
;
998 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
999 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1008 ZeroMem (Buffer
, sizeof (Buffer
));
1011 // If the boot device enumerate happened, just get the boot
1012 // device from the boot order variable
1014 if (mEnumBootDevice
) {
1015 LastLang
= GetVariable (L
"LastEnumLang", &mBdsLibLastLangGuid
);
1016 PlatLang
= GetEfiGlobalVariable (L
"PlatformLang");
1017 if (LastLang
== PlatLang
) {
1018 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1021 Status
= gRT
->SetVariable (
1023 &mBdsLibLastLangGuid
,
1024 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1028 ASSERT_EFI_ERROR (Status
);
1033 // Notes: this dirty code is to get the legacy boot option from the
1034 // BBS table and create to variable as the EFI boot option, it should
1035 // be removed after the CSM can provide legacy boot option directly
1037 REFRESH_LEGACY_BOOT_OPTIONS
;
1040 // Delete invalid boot option
1042 BdsDeleteAllInvalidEfiBootOption ();
1045 // Parse removable media
1047 gBS
->LocateHandleBuffer (
1049 &gEfiBlockIoProtocolGuid
,
1051 &NumberBlockIoHandles
,
1055 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1056 Status
= gBS
->HandleProtocol (
1057 BlockIoHandles
[Index
],
1058 &gEfiBlockIoProtocolGuid
,
1061 if (!EFI_ERROR (Status
)) {
1062 if (!BlkIo
->Media
->RemovableMedia
) {
1064 // skip the non-removable block devices
1069 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1070 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1072 switch (DevicePathType
) {
1073 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1074 if (FloppyNumber
!= 0) {
1075 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
1077 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
1079 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1084 // Assume a removable SATA device should be the DVD/CD device
1086 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1087 case BDS_EFI_MESSAGE_SATA_BOOT
:
1088 if (CdromNumber
!= 0) {
1089 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
1091 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
1093 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
1094 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1098 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1099 if (UsbNumber
!= 0) {
1100 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
1102 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
1104 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1108 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1109 if (ScsiNumber
!= 0) {
1110 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
1112 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
1114 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1118 case BDS_EFI_MESSAGE_MISC_BOOT
:
1119 if (MiscNumber
!= 0) {
1120 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
1122 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
1124 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1133 if (NumberBlockIoHandles
!= 0) {
1134 FreePool (BlockIoHandles
);
1138 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1141 gBS
->LocateHandleBuffer (
1143 &gEfiSimpleFileSystemProtocolGuid
,
1145 &NumberFileSystemHandles
,
1148 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1149 Status
= gBS
->HandleProtocol (
1150 FileSystemHandles
[Index
],
1151 &gEfiBlockIoProtocolGuid
,
1154 if (!EFI_ERROR (Status
)) {
1156 // Skip if the file system handle supports a BlkIo protocol,
1162 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1163 // machinename is ia32, ia64, x64, ...
1165 Hdr
.Union
= &HdrData
;
1167 Status
= BdsLibGetImageHeader (
1168 FileSystemHandles
[Index
],
1169 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1173 if (!EFI_ERROR (Status
) &&
1174 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1175 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1181 // No such file or the file is not a EFI application, delete this boot option
1183 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1185 if (NonBlockNumber
!= 0) {
1186 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
1188 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
1190 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1195 if (NumberFileSystemHandles
!= 0) {
1196 FreePool (FileSystemHandles
);
1200 // Parse Network Boot Device
1202 NumOfLoadFileHandles
= 0;
1204 // Search Load File protocol for PXE boot option.
1206 gBS
->LocateHandleBuffer (
1208 &gEfiLoadFileProtocolGuid
,
1210 &NumOfLoadFileHandles
,
1214 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1216 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
1218 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
1220 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1223 if (NumOfLoadFileHandles
!= 0) {
1224 FreePool (LoadFileHandles
);
1228 // Check if we have on flash shell
1230 gBS
->LocateHandleBuffer (
1232 &gEfiFirmwareVolume2ProtocolGuid
,
1237 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1238 gBS
->HandleProtocol (
1239 FvHandleBuffer
[Index
],
1240 &gEfiFirmwareVolume2ProtocolGuid
,
1244 Status
= Fv
->ReadFile (
1246 PcdGetPtr(PcdShellFile
),
1251 &AuthenticationStatus
1253 if (EFI_ERROR (Status
)) {
1255 // Skip if no shell file in the FV
1260 // Build the shell boot option
1262 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1265 if (FvHandleCount
!= 0) {
1266 FreePool (FvHandleBuffer
);
1269 // Make sure every boot only have one time
1270 // boot device enumerate
1272 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1273 mEnumBootDevice
= TRUE
;
1279 Build the boot option with the handle parsed in
1281 @param Handle The handle which present the device path to create
1283 @param BdsBootOptionList The header of the link list which indexed all
1284 current boot options
1285 @param String The description of the boot option.
1290 BdsLibBuildOptionFromHandle (
1291 IN EFI_HANDLE Handle
,
1292 IN LIST_ENTRY
*BdsBootOptionList
,
1296 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1298 DevicePath
= DevicePathFromHandle (Handle
);
1301 // Create and register new boot option
1303 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1308 Build the on flash shell boot option with the handle parsed in.
1310 @param Handle The handle which present the device path to create
1311 on flash shell boot option
1312 @param BdsBootOptionList The header of the link list which indexed all
1313 current boot options
1318 BdsLibBuildOptionFromShell (
1319 IN EFI_HANDLE Handle
,
1320 IN OUT LIST_ENTRY
*BdsBootOptionList
1323 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1324 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1326 DevicePath
= DevicePathFromHandle (Handle
);
1329 // Build the shell device path
1331 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1333 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1336 // Create and register the shell boot option
1338 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1343 Boot from the UEFI spec defined "BootNext" variable.
1355 BDS_COMMON_OPTION
*BootOption
;
1356 LIST_ENTRY TempList
;
1361 // Init the boot option name buffer and temp link list
1363 InitializeListHead (&TempList
);
1364 ZeroMem (Buffer
, sizeof (Buffer
));
1366 BootNext
= BdsLibGetVariableAndSize (
1368 &gEfiGlobalVariableGuid
,
1373 // Clear the boot next variable first
1375 if (BootNext
!= NULL
) {
1378 &gEfiGlobalVariableGuid
,
1379 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1385 // Start to build the boot option and try to boot
1387 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1388 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1389 ASSERT (BootOption
!= NULL
);
1390 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1391 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1397 Return the bootable media handle.
1398 First, check the device is connected
1399 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1400 Third, detect the the default boot file in the Media, and return the removable Media handle.
1402 @param DevicePath Device Path to a bootable device
1404 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1409 BdsLibGetBootableHandle (
1410 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1414 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1415 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1417 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1419 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1422 EFI_HANDLE ReturnHandle
;
1423 EFI_HANDLE
*SimpleFileSystemHandles
;
1425 UINTN NumberSimpleFileSystemHandles
;
1427 EFI_IMAGE_DOS_HEADER DosHeader
;
1428 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1429 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1431 UpdatedDevicePath
= DevicePath
;
1434 // Check whether the device is connected
1436 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1437 if (EFI_ERROR (Status
)) {
1439 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1441 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1442 if (EFI_ERROR (Status
)) {
1444 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1446 UpdatedDevicePath
= DevicePath
;
1447 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1448 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1452 // For removable device boot option, its contained device path only point to the removable device handle,
1453 // should make sure all its children handles (its child partion or media handles) are created and connected.
1455 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1457 // Get BlockIo protocol and check removable attribute
1459 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1461 // Issue a dummy read to the device to check for media change.
1462 // When the removable media is changed, any Block IO read/write will
1463 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1464 // returned. After the Block IO protocol is reinstalled, subsequent
1465 // Block IO read/write will success.
1467 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1468 if (Buffer
!= NULL
) {
1469 BlockIo
->ReadBlocks (
1471 BlockIo
->Media
->MediaId
,
1473 BlockIo
->Media
->BlockSize
,
1481 // Detect the the default boot file from removable Media
1485 // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus
1486 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1488 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1489 ASSERT (DupDevicePath
!= NULL
);
1491 UpdatedDevicePath
= DupDevicePath
;
1492 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1494 // if the resulting device path point to a usb node, and the usb node is a dummy node, should only let device path only point to the previous Pci node
1495 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1497 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1498 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1500 // Remove the usb node, let the device path only point to PCI node
1502 SetDevicePathEndNode (UpdatedDevicePath
);
1503 UpdatedDevicePath
= DupDevicePath
;
1505 UpdatedDevicePath
= DevicePath
;
1509 // Get the device path size of boot option
1511 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1512 ReturnHandle
= NULL
;
1513 gBS
->LocateHandleBuffer (
1515 &gEfiSimpleFileSystemProtocolGuid
,
1517 &NumberSimpleFileSystemHandles
,
1518 &SimpleFileSystemHandles
1520 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1522 // Get the device path size of SimpleFileSystem handle
1524 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1525 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1527 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1529 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1531 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1532 // machinename is ia32, ia64, x64, ...
1534 Hdr
.Union
= &HdrData
;
1535 Status
= BdsLibGetImageHeader (
1536 SimpleFileSystemHandles
[Index
],
1537 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1541 if (!EFI_ERROR (Status
) &&
1542 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1543 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1544 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1550 FreePool(DupDevicePath
);
1552 if (SimpleFileSystemHandles
!= NULL
) {
1553 FreePool(SimpleFileSystemHandles
);
1556 return ReturnHandle
;
1560 Check to see if the network cable is plugged in. If the DevicePath is not
1561 connected it will be connected.
1563 @param DevicePath Device Path to check
1565 @retval TRUE DevicePath points to an Network that is connected
1566 @retval FALSE DevicePath does not point to a bootable network
1570 BdsLibNetworkBootWithMediaPresent (
1571 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1575 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1577 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1578 BOOLEAN MediaPresent
;
1579 UINT32 InterruptStatus
;
1581 MediaPresent
= FALSE
;
1583 UpdatedDevicePath
= DevicePath
;
1585 // Locate Load File Protocol for PXE boot option first
1587 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1588 if (EFI_ERROR (Status
)) {
1590 // Device not present so see if we need to connect it
1592 Status
= BdsLibConnectDevicePath (DevicePath
);
1593 if (!EFI_ERROR (Status
)) {
1595 // This one should work after we did the connect
1597 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1601 if (!EFI_ERROR (Status
)) {
1602 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1603 if (EFI_ERROR (Status
)) {
1605 // Failed to open SNP from this handle, try to get SNP from parent handle
1607 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1608 if (UpdatedDevicePath
!= NULL
) {
1609 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1610 if (!EFI_ERROR (Status
)) {
1612 // SNP handle found, get SNP from it
1614 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1619 if (!EFI_ERROR (Status
)) {
1620 if (Snp
->Mode
->MediaPresentSupported
) {
1621 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1623 // Invoke Snp->GetStatus() to refresh the media status
1625 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
1628 // In case some one else is using the SNP check to see if it's connected
1630 MediaPresent
= Snp
->Mode
->MediaPresent
;
1633 // No one is using SNP so we need to Start and Initialize so
1634 // MediaPresent will be valid.
1636 Status
= Snp
->Start (Snp
);
1637 if (!EFI_ERROR (Status
)) {
1638 Status
= Snp
->Initialize (Snp
, 0, 0);
1639 if (!EFI_ERROR (Status
)) {
1640 MediaPresent
= Snp
->Mode
->MediaPresent
;
1641 Snp
->Shutdown (Snp
);
1647 MediaPresent
= TRUE
;
1652 return MediaPresent
;
1656 For a bootable Device path, return its boot type.
1658 @param DevicePath The bootable device Path to check
1660 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1661 which subtype is MEDIA_HARDDRIVE_DP
1662 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1663 which subtype is MEDIA_CDROM_DP
1664 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1665 which HID is floppy device.
1666 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1667 and its last device path node's subtype is MSG_ATAPI_DP.
1668 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1669 and its last device path node's subtype is MSG_SCSI_DP.
1670 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1671 and its last device path node's subtype is MSG_USB_DP.
1672 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1673 its last device path node point to a message device path node.
1674 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1675 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1680 BdsGetBootTypeFromDevicePath (
1681 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1684 ACPI_HID_DEVICE_PATH
*Acpi
;
1685 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1686 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1689 if (NULL
== DevicePath
) {
1690 return BDS_EFI_UNSUPPORT
;
1693 TempDevicePath
= DevicePath
;
1695 while (!IsDevicePathEndType (TempDevicePath
)) {
1696 switch (DevicePathType (TempDevicePath
)) {
1697 case BBS_DEVICE_PATH
:
1698 return BDS_LEGACY_BBS_BOOT
;
1699 case MEDIA_DEVICE_PATH
:
1700 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1701 return BDS_EFI_MEDIA_HD_BOOT
;
1702 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1703 return BDS_EFI_MEDIA_CDROM_BOOT
;
1706 case ACPI_DEVICE_PATH
:
1707 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1708 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1709 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1712 case MESSAGING_DEVICE_PATH
:
1714 // Get the last device path node
1716 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1717 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1719 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1722 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1725 // if the device path not only point to driver device, it is not a messaging device path,
1727 if (!IsDevicePathEndType (LastDeviceNode
)) {
1731 switch (DevicePathSubType (TempDevicePath
)) {
1733 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1737 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1741 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1745 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1748 case MSG_MAC_ADDR_DP
:
1752 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1756 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1764 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1767 return BDS_EFI_UNSUPPORT
;
1771 Check whether the Device path in a boot option point to a valid bootable device,
1772 And if CheckMedia is true, check the device is ready to boot now.
1774 @param DevPath the Device path in a boot option
1775 @param CheckMedia if true, check the device is ready to boot now.
1777 @retval TRUE the Device path is valid
1778 @retval FALSE the Device path is invalid .
1783 BdsLibIsValidEFIBootOptDevicePath (
1784 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1785 IN BOOLEAN CheckMedia
1788 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1792 Check whether the Device path in a boot option point to a valid bootable device,
1793 And if CheckMedia is true, check the device is ready to boot now.
1794 If Description is not NULL and the device path point to a fixed BlockIo
1795 device, check the description whether conflict with other auto-created
1798 @param DevPath the Device path in a boot option
1799 @param CheckMedia if true, check the device is ready to boot now.
1800 @param Description the description in a boot option
1802 @retval TRUE the Device path is valid
1803 @retval FALSE the Device path is invalid .
1808 BdsLibIsValidEFIBootOptDevicePathExt (
1809 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1810 IN BOOLEAN CheckMedia
,
1811 IN CHAR16
*Description
1816 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1817 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1818 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1820 TempDevicePath
= DevPath
;
1821 LastDeviceNode
= DevPath
;
1824 // Check if it's a valid boot option for network boot device.
1825 // Check if there is EfiLoadFileProtocol installed.
1826 // If yes, that means there is a boot option for network.
1828 Status
= gBS
->LocateDevicePath (
1829 &gEfiLoadFileProtocolGuid
,
1833 if (EFI_ERROR (Status
)) {
1835 // Device not present so see if we need to connect it
1837 TempDevicePath
= DevPath
;
1838 BdsLibConnectDevicePath (TempDevicePath
);
1839 Status
= gBS
->LocateDevicePath (
1840 &gEfiLoadFileProtocolGuid
,
1846 if (!EFI_ERROR (Status
)) {
1847 if (!IsDevicePathEnd (TempDevicePath
)) {
1849 // LoadFile protocol is not installed on handle with exactly the same DevPath
1856 // Test if it is ready to boot now
1858 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1867 // If the boot option point to a file, it is a valid EFI boot option,
1868 // and assume it is ready to boot now
1870 while (!IsDevicePathEnd (TempDevicePath
)) {
1871 LastDeviceNode
= TempDevicePath
;
1872 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1874 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1875 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1880 // Check if it's a valid boot option for internal Shell
1882 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1884 // If the boot option point to Internal FV shell, make sure it is valid
1886 TempDevicePath
= DevPath
;
1887 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1888 if (Status
== EFI_ALREADY_STARTED
) {
1891 if (Status
== EFI_SUCCESS
) {
1892 FreePool (TempDevicePath
);
1899 // If the boot option point to a blockIO device:
1900 // if it is a removable blockIo device, it is valid.
1901 // if it is a fixed blockIo device, check its description confliction.
1903 TempDevicePath
= DevPath
;
1904 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1905 if (EFI_ERROR (Status
)) {
1907 // Device not present so see if we need to connect it
1909 Status
= BdsLibConnectDevicePath (DevPath
);
1910 if (!EFI_ERROR (Status
)) {
1912 // Try again to get the Block Io protocol after we did the connect
1914 TempDevicePath
= DevPath
;
1915 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1919 if (!EFI_ERROR (Status
)) {
1920 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1921 if (!EFI_ERROR (Status
)) {
1924 // Test if it is ready to boot now
1926 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1935 // if the boot option point to a simple file protocol which does not consume block Io protocol, it is also a valid EFI boot option,
1937 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1938 if (!EFI_ERROR (Status
)) {
1941 // Test if it is ready to boot now
1943 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1957 According to a file guild, check a Fv file device path is valid. If it is invalid,
1958 try to return the valid device path.
1959 FV address maybe changes for memory layout adjust from time to time, use this function
1960 could promise the Fv file device path is right.
1962 @param DevicePath on input, the Fv file device path need to check on
1963 output, the updated valid Fv file device path
1964 @param FileGuid the Fv file guild
1966 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1968 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1970 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1972 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1973 and return the updated device path in DevicePath
1978 BdsLibUpdateFvFileDevicePath (
1979 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1980 IN EFI_GUID
*FileGuid
1983 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1984 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1986 EFI_GUID
*GuidPoint
;
1988 UINTN FvHandleCount
;
1989 EFI_HANDLE
*FvHandleBuffer
;
1990 EFI_FV_FILETYPE Type
;
1992 EFI_FV_FILE_ATTRIBUTES Attributes
;
1993 UINT32 AuthenticationStatus
;
1995 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1996 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1997 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1998 EFI_HANDLE FoundFvHandle
;
1999 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2001 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2002 return EFI_INVALID_PARAMETER
;
2004 if (FileGuid
== NULL
) {
2005 return EFI_INVALID_PARAMETER
;
2009 // Check whether the device path point to the default the input Fv file
2011 TempDevicePath
= *DevicePath
;
2012 LastDeviceNode
= TempDevicePath
;
2013 while (!IsDevicePathEnd (TempDevicePath
)) {
2014 LastDeviceNode
= TempDevicePath
;
2015 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2017 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2018 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2020 if (GuidPoint
== NULL
) {
2022 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2024 return EFI_UNSUPPORTED
;
2026 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2028 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2030 return EFI_UNSUPPORTED
;
2034 // Check whether the input Fv file device path is valid
2036 TempDevicePath
= *DevicePath
;
2037 FoundFvHandle
= NULL
;
2038 Status
= gBS
->LocateDevicePath (
2039 &gEfiFirmwareVolume2ProtocolGuid
,
2043 if (!EFI_ERROR (Status
)) {
2044 Status
= gBS
->HandleProtocol (
2046 &gEfiFirmwareVolume2ProtocolGuid
,
2049 if (!EFI_ERROR (Status
)) {
2051 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2053 Status
= Fv
->ReadFile (
2060 &AuthenticationStatus
2062 if (!EFI_ERROR (Status
)) {
2063 return EFI_ALREADY_STARTED
;
2069 // Look for the input wanted FV file in current FV
2070 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2073 FoundFvHandle
= NULL
;
2074 Status
= gBS
->HandleProtocol (
2076 &gEfiLoadedImageProtocolGuid
,
2077 (VOID
**) &LoadedImage
2079 if (!EFI_ERROR (Status
)) {
2080 Status
= gBS
->HandleProtocol (
2081 LoadedImage
->DeviceHandle
,
2082 &gEfiFirmwareVolume2ProtocolGuid
,
2085 if (!EFI_ERROR (Status
)) {
2086 Status
= Fv
->ReadFile (
2093 &AuthenticationStatus
2095 if (!EFI_ERROR (Status
)) {
2097 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2102 // Second, if fail to find, try to enumerate all FV
2105 FvHandleBuffer
= NULL
;
2106 gBS
->LocateHandleBuffer (
2108 &gEfiFirmwareVolume2ProtocolGuid
,
2113 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2114 gBS
->HandleProtocol (
2115 FvHandleBuffer
[Index
],
2116 &gEfiFirmwareVolume2ProtocolGuid
,
2120 Status
= Fv
->ReadFile (
2127 &AuthenticationStatus
2129 if (EFI_ERROR (Status
)) {
2131 // Skip if input Fv file not in the FV
2136 FoundFvHandle
= FvHandleBuffer
[Index
];
2140 if (FvHandleBuffer
!= NULL
) {
2141 FreePool (FvHandleBuffer
);
2147 // Build the shell device path
2149 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2150 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2151 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2152 *DevicePath
= NewDevicePath
;
2155 return EFI_NOT_FOUND
;