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();
231 // Adjust the different type memory page number just before booting
232 // and save the updated info into the variable for next boot to use
234 BdsSetMemoryTypeInformationVariable ();
240 if (IsBootOptionValidNVVarialbe (Option
)) {
242 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
243 // In this case, "BootCurrent" is not created.
244 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
248 &gEfiGlobalVariableGuid
,
249 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
255 ASSERT (Option
->DevicePath
!= NULL
);
256 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
257 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
260 // Check to see if we should legacy BOOT. If yes then do the legacy boot
262 return BdsLibDoLegacyBoot (Option
);
266 // If the boot option point to Internal FV shell, make sure it is valid
268 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
269 if (!EFI_ERROR(Status
)) {
270 if (Option
->DevicePath
!= NULL
) {
271 FreePool(Option
->DevicePath
);
273 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
274 ASSERT(Option
->DevicePath
!= NULL
);
275 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
277 // Update the shell boot option
279 InitializeListHead (&TempBootLists
);
280 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
283 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
285 FreePool (DevicePath
);
286 DevicePath
= Option
->DevicePath
;
291 if (Option
->Description
== NULL
) {
292 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
294 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
299 Status
= gBS
->LoadImage (
309 // If we didn't find an image directly, we need to try as if it is a removable device boot option
310 // and load the image according to the default boot behavior for removable device.
312 if (EFI_ERROR (Status
)) {
314 // check if there is a bootable removable media could be found in this device path ,
315 // and get the bootable media handle
317 Handle
= BdsLibGetBootableHandle(DevicePath
);
318 if (Handle
== NULL
) {
322 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
323 // machinename is ia32, ia64, x64, ...
325 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
326 if (FilePath
!= NULL
) {
327 Status
= gBS
->LoadImage (
335 if (EFI_ERROR (Status
)) {
337 // The DevicePath failed, and it's not a valid
338 // removable media device.
345 if (EFI_ERROR (Status
)) {
347 // It there is any error from the Boot attempt exit now.
352 // Provide the image with it's load options
354 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
355 ASSERT_EFI_ERROR (Status
);
357 if (Option
->LoadOptionsSize
!= 0) {
358 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
359 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
362 // Before calling the image, enable the Watchdog Timer for
363 // the 5 Minute period
365 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
368 // Write boot to OS performance data for UEFI boot
371 WriteBootToOsPerformanceData ();
374 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
375 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
378 // Clear the Watchdog Timer after the image returns
380 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
384 // Clear Boot Current
388 &gEfiGlobalVariableGuid
,
389 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
399 Expand a device path that starts with a hard drive media device path node to be a
400 full device path that includes the full hardware path to the device. We need
401 to do this so it can be booted. As an optimization the front match (the part point
402 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
403 so a connect all is not required on every boot. All successful history device path
404 which point to partition node (the front part) will be saved.
406 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
407 drive media device path.
408 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
412 EFI_DEVICE_PATH_PROTOCOL
*
414 BdsExpandPartitionPartialDevicePathToFull (
415 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
419 UINTN BlockIoHandleCount
;
420 EFI_HANDLE
*BlockIoBuffer
;
421 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
422 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
423 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
426 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
427 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
428 UINTN CachedDevicePathSize
;
431 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
434 FullDevicePath
= NULL
;
436 // Check if there is prestore 'HDDP' variable.
437 // If exist, search the front path which point to partition node in the variable instants.
438 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
440 CachedDevicePath
= BdsLibGetVariableAndSize (
442 &mHdBootVariablePrivateGuid
,
443 &CachedDevicePathSize
446 if (CachedDevicePath
!= NULL
) {
447 TempNewDevicePath
= CachedDevicePath
;
452 // Check every instance of the variable
453 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
454 // partial partition boot option. Second, check whether the instance could be connected.
456 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
457 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
459 // Connect the device path instance, the device path point to hard drive media device path node
460 // e.g. ACPI() /PCI()/ATA()/Partition()
462 Status
= BdsLibConnectDevicePath (Instance
);
463 if (!EFI_ERROR (Status
)) {
469 // Come here means the first instance is not matched
473 } while (TempNewDevicePath
!= NULL
);
477 // Find the matched device path.
478 // Append the file path information from the boot option and return the fully expanded device path.
480 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
481 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
484 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
488 // First delete the matched instance.
490 TempNewDevicePath
= CachedDevicePath
;
491 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
492 FreePool (TempNewDevicePath
);
495 // Second, append the remaining path after the matched instance
497 TempNewDevicePath
= CachedDevicePath
;
498 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
499 FreePool (TempNewDevicePath
);
501 // Save the matching Device Path so we don't need to do a connect all next time
503 Status
= gRT
->SetVariable (
505 &mHdBootVariablePrivateGuid
,
506 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
507 GetDevicePathSize (CachedDevicePath
),
513 FreePool (CachedDevicePath
);
514 return FullDevicePath
;
519 // If we get here we fail to find or 'HDDP' not exist, and now we need
520 // to search all devices in the system for a matched partition
522 BdsLibConnectAllDriversToAllControllers ();
523 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
524 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
526 // If there was an error or there are no device handles that support
527 // the BLOCK_IO Protocol, then return.
532 // Loop through all the device handles that support the BLOCK_IO Protocol
534 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
536 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
537 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
541 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
543 // Find the matched partition device path
545 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
546 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
549 // Save the matched partition device path in 'HDDP' variable
551 if (CachedDevicePath
!= NULL
) {
553 // Save the matched partition device path as first instance of 'HDDP' variable
555 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
556 TempNewDevicePath
= CachedDevicePath
;
557 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
558 FreePool(TempNewDevicePath
);
560 TempNewDevicePath
= CachedDevicePath
;
561 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
562 if (TempNewDevicePath
!= NULL
) {
563 FreePool(TempNewDevicePath
);
566 TempNewDevicePath
= CachedDevicePath
;
567 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
568 FreePool(TempNewDevicePath
);
571 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
572 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
575 ASSERT (CachedDevicePath
!= NULL
);
576 TempNewDevicePath
= CachedDevicePath
;
577 while (!IsDevicePathEnd (TempNewDevicePath
)) {
578 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
580 // Parse one instance
582 while (!IsDevicePathEndType (TempNewDevicePath
)) {
583 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
587 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
589 if (InstanceNum
>= 12) {
590 SetDevicePathEndNode (TempNewDevicePath
);
595 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
599 // Save the matching Device Path so we don't need to do a connect all next time
601 Status
= gRT
->SetVariable (
603 &mHdBootVariablePrivateGuid
,
604 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
605 GetDevicePathSize (CachedDevicePath
),
613 if (CachedDevicePath
!= NULL
) {
614 FreePool (CachedDevicePath
);
616 if (BlockIoBuffer
!= NULL
) {
617 FreePool (BlockIoBuffer
);
619 return FullDevicePath
;
623 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
624 instances, has the same partition node with HardDriveDevicePath device path
626 @param BlockIoDevicePath Multi device path instances which need to check
627 @param HardDriveDevicePath A device path which starts with a hard drive media
630 @retval TRUE There is a matched device path instance.
631 @retval FALSE There is no matched device path instance.
636 MatchPartitionDevicePathNode (
637 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
638 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
641 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
642 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
644 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
646 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
651 // Make PreviousDevicePath == the device path node before the end node
653 DevicePath
= BlockIoDevicePath
;
654 BlockIoHdDevicePathNode
= NULL
;
657 // find the partition device path node
659 while (!IsDevicePathEnd (DevicePath
)) {
660 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
661 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
663 BlockIoHdDevicePathNode
= DevicePath
;
667 DevicePath
= NextDevicePathNode (DevicePath
);
670 if (BlockIoHdDevicePathNode
== NULL
) {
674 // See if the harddrive device path in blockio matches the orig Hard Drive Node
676 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
680 // Check for the match
682 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
683 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
684 switch (TmpHdPath
->SignatureType
) {
685 case SIGNATURE_TYPE_GUID
:
686 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
688 case SIGNATURE_TYPE_MBR
:
689 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
701 Delete the boot option associated with the handle passed in.
703 @param Handle The handle which present the device path to create
706 @retval EFI_SUCCESS Delete the boot option success
707 @retval EFI_NOT_FOUND If the Device Path is not found in the system
708 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
709 @retval Other Error return value from SetVariable()
713 BdsLibDeleteOptionFromHandle (
718 UINT8
*BootOptionVar
;
720 UINTN BootOptionSize
;
723 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
724 UINTN DevicePathSize
;
725 UINTN OptionDevicePathSize
;
726 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
727 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
730 Status
= EFI_SUCCESS
;
735 // Check "BootOrder" variable, if no, means there is no any boot order.
737 BootOrder
= BdsLibGetVariableAndSize (
739 &gEfiGlobalVariableGuid
,
742 if (BootOrder
== NULL
) {
743 return EFI_NOT_FOUND
;
747 // Convert device handle to device path protocol instance
749 DevicePath
= DevicePathFromHandle (Handle
);
750 if (DevicePath
== NULL
) {
751 return EFI_NOT_FOUND
;
753 DevicePathSize
= GetDevicePathSize (DevicePath
);
756 // Loop all boot order variable and find the matching device path
759 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
760 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
761 BootOptionVar
= BdsLibGetVariableAndSize (
763 &gEfiGlobalVariableGuid
,
767 if (BootOptionVar
== NULL
) {
768 FreePool (BootOrder
);
769 return EFI_OUT_OF_RESOURCES
;
772 TempPtr
= BootOptionVar
;
773 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
774 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
775 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
776 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
779 // Check whether the device path match
781 if ((OptionDevicePathSize
== DevicePathSize
) &&
782 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
783 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
784 FreePool (BootOptionVar
);
788 FreePool (BootOptionVar
);
793 // Adjust number of boot option for "BootOrder" variable.
795 Status
= gRT
->SetVariable (
797 &gEfiGlobalVariableGuid
,
798 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
803 FreePool (BootOrder
);
810 Delete all invalid EFI boot options.
812 @retval EFI_SUCCESS Delete all invalid boot option success
813 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
814 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
815 @retval Other Error return value from SetVariable()
819 BdsDeleteAllInvalidEfiBootOption (
824 UINT8
*BootOptionVar
;
826 UINTN BootOptionSize
;
830 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
831 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
835 Status
= EFI_SUCCESS
;
840 // Check "BootOrder" variable firstly, this variable hold the number of boot options
842 BootOrder
= BdsLibGetVariableAndSize (
844 &gEfiGlobalVariableGuid
,
847 if (NULL
== BootOrder
) {
848 return EFI_NOT_FOUND
;
852 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
853 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
854 BootOptionVar
= BdsLibGetVariableAndSize (
856 &gEfiGlobalVariableGuid
,
859 if (NULL
== BootOptionVar
) {
860 FreePool (BootOrder
);
861 return EFI_OUT_OF_RESOURCES
;
864 TempPtr
= BootOptionVar
;
865 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
866 Description
= (CHAR16
*) TempPtr
;
867 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
868 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
871 // Skip legacy boot option (BBS boot device)
873 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
874 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
875 FreePool (BootOptionVar
);
880 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
882 // Delete this invalid boot option "Boot####"
884 Status
= gRT
->SetVariable (
886 &gEfiGlobalVariableGuid
,
887 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
892 // Mark this boot option in boot order as deleted
894 BootOrder
[Index
] = 0xffff;
897 FreePool (BootOptionVar
);
902 // Adjust boot order array
905 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
906 if (BootOrder
[Index
] != 0xffff) {
907 BootOrder
[Index2
] = BootOrder
[Index
];
911 Status
= gRT
->SetVariable (
913 &gEfiGlobalVariableGuid
,
914 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
915 Index2
* sizeof (UINT16
),
919 FreePool (BootOrder
);
926 For EFI boot option, BDS separate them as six types:
927 1. Network - The boot option points to the SimpleNetworkProtocol device.
928 Bds will try to automatically create this type boot option when enumerate.
929 2. Shell - The boot option points to internal flash shell.
930 Bds will try to automatically create this type boot option when enumerate.
931 3. Removable BlockIo - The boot option only points to the removable media
932 device, like USB flash disk, DVD, Floppy etc.
933 These device should contain a *removable* blockIo
934 protocol in their device handle.
935 Bds will try to automatically create this type boot option
937 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
939 These device should contain a *fixed* blockIo
940 protocol in their device handle.
941 BDS will skip fixed blockIo devices, and NOT
942 automatically create boot option for them. But BDS
943 will help to delete those fixed blockIo boot option,
944 whose description rule conflict with other auto-created
946 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
947 has SimpleFileSystem Protocol, but has no blockio
948 protocol. These devices do not offer blockIo
949 protocol, but BDS still can get the
950 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
952 6. File - The boot option points to a file. These boot options are usually
953 created by user manually or OS loader. BDS will not delete or modify
956 This function will enumerate all possible boot device in the system, and
957 automatically create boot options for Network, Shell, Removable BlockIo,
958 and Non-BlockIo Simplefile devices.
959 It will only execute once of every boot.
961 @param BdsBootOptionList The header of the link list which indexed all
964 @retval EFI_SUCCESS Finished all the boot device enumerate and create
965 the boot option base on that boot device
967 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
971 BdsLibEnumerateAllBootOption (
972 IN OUT LIST_ENTRY
*BdsBootOptionList
981 UINT16 NonBlockNumber
;
982 UINTN NumberBlockIoHandles
;
983 EFI_HANDLE
*BlockIoHandles
;
984 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
986 UINTN NumOfLoadFileHandles
;
987 EFI_HANDLE
*LoadFileHandles
;
989 EFI_HANDLE
*FvHandleBuffer
;
990 EFI_FV_FILETYPE Type
;
992 EFI_FV_FILE_ATTRIBUTES Attributes
;
993 UINT32 AuthenticationStatus
;
994 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
995 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
996 UINTN DevicePathType
;
998 EFI_HANDLE
*FileSystemHandles
;
999 UINTN NumberFileSystemHandles
;
1001 EFI_IMAGE_DOS_HEADER DosHeader
;
1004 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1005 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1014 ZeroMem (Buffer
, sizeof (Buffer
));
1017 // If the boot device enumerate happened, just get the boot
1018 // device from the boot order variable
1020 if (mEnumBootDevice
) {
1021 LastLang
= GetVariable (L
"LastEnumLang", &mBdsLibLastLangGuid
);
1022 PlatLang
= GetEfiGlobalVariable (L
"PlatformLang");
1023 if (LastLang
== PlatLang
) {
1024 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1027 Status
= gRT
->SetVariable (
1029 &mBdsLibLastLangGuid
,
1030 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1034 ASSERT_EFI_ERROR (Status
);
1039 // Notes: this dirty code is to get the legacy boot option from the
1040 // BBS table and create to variable as the EFI boot option, it should
1041 // be removed after the CSM can provide legacy boot option directly
1043 REFRESH_LEGACY_BOOT_OPTIONS
;
1046 // Delete invalid boot option
1048 BdsDeleteAllInvalidEfiBootOption ();
1051 // Parse removable media
1053 gBS
->LocateHandleBuffer (
1055 &gEfiBlockIoProtocolGuid
,
1057 &NumberBlockIoHandles
,
1061 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1062 Status
= gBS
->HandleProtocol (
1063 BlockIoHandles
[Index
],
1064 &gEfiBlockIoProtocolGuid
,
1067 if (!EFI_ERROR (Status
)) {
1068 if (!BlkIo
->Media
->RemovableMedia
) {
1070 // skip the non-removable block devices
1075 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1076 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1078 switch (DevicePathType
) {
1079 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1080 if (FloppyNumber
!= 0) {
1081 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
1083 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
1085 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1090 // Assume a removable SATA device should be the DVD/CD device
1092 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1093 case BDS_EFI_MESSAGE_SATA_BOOT
:
1094 if (CdromNumber
!= 0) {
1095 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
1097 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
1099 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
1100 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1104 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1105 if (UsbNumber
!= 0) {
1106 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
1108 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
1110 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1114 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1115 if (ScsiNumber
!= 0) {
1116 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
1118 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
1120 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1124 case BDS_EFI_MESSAGE_MISC_BOOT
:
1125 if (MiscNumber
!= 0) {
1126 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
1128 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
1130 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1139 if (NumberBlockIoHandles
!= 0) {
1140 FreePool (BlockIoHandles
);
1144 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1147 gBS
->LocateHandleBuffer (
1149 &gEfiSimpleFileSystemProtocolGuid
,
1151 &NumberFileSystemHandles
,
1154 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1155 Status
= gBS
->HandleProtocol (
1156 FileSystemHandles
[Index
],
1157 &gEfiBlockIoProtocolGuid
,
1160 if (!EFI_ERROR (Status
)) {
1162 // Skip if the file system handle supports a BlkIo protocol,
1168 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1169 // machinename is ia32, ia64, x64, ...
1171 Hdr
.Union
= &HdrData
;
1173 Status
= BdsLibGetImageHeader (
1174 FileSystemHandles
[Index
],
1175 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1179 if (!EFI_ERROR (Status
) &&
1180 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1181 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1187 // No such file or the file is not a EFI application, delete this boot option
1189 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1191 if (NonBlockNumber
!= 0) {
1192 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
1194 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
1196 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1201 if (NumberFileSystemHandles
!= 0) {
1202 FreePool (FileSystemHandles
);
1206 // Parse Network Boot Device
1208 NumOfLoadFileHandles
= 0;
1210 // Search Load File protocol for PXE boot option.
1212 gBS
->LocateHandleBuffer (
1214 &gEfiLoadFileProtocolGuid
,
1216 &NumOfLoadFileHandles
,
1220 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1222 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
1224 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
1226 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1229 if (NumOfLoadFileHandles
!= 0) {
1230 FreePool (LoadFileHandles
);
1234 // Check if we have on flash shell
1236 gBS
->LocateHandleBuffer (
1238 &gEfiFirmwareVolume2ProtocolGuid
,
1243 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1244 gBS
->HandleProtocol (
1245 FvHandleBuffer
[Index
],
1246 &gEfiFirmwareVolume2ProtocolGuid
,
1250 Status
= Fv
->ReadFile (
1252 PcdGetPtr(PcdShellFile
),
1257 &AuthenticationStatus
1259 if (EFI_ERROR (Status
)) {
1261 // Skip if no shell file in the FV
1266 // Build the shell boot option
1268 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1271 if (FvHandleCount
!= 0) {
1272 FreePool (FvHandleBuffer
);
1275 // Make sure every boot only have one time
1276 // boot device enumerate
1278 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1279 mEnumBootDevice
= TRUE
;
1285 Build the boot option with the handle parsed in
1287 @param Handle The handle which present the device path to create
1289 @param BdsBootOptionList The header of the link list which indexed all
1290 current boot options
1291 @param String The description of the boot option.
1296 BdsLibBuildOptionFromHandle (
1297 IN EFI_HANDLE Handle
,
1298 IN LIST_ENTRY
*BdsBootOptionList
,
1302 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1304 DevicePath
= DevicePathFromHandle (Handle
);
1307 // Create and register new boot option
1309 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1314 Build the on flash shell boot option with the handle parsed in.
1316 @param Handle The handle which present the device path to create
1317 on flash shell boot option
1318 @param BdsBootOptionList The header of the link list which indexed all
1319 current boot options
1324 BdsLibBuildOptionFromShell (
1325 IN EFI_HANDLE Handle
,
1326 IN OUT LIST_ENTRY
*BdsBootOptionList
1329 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1330 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1332 DevicePath
= DevicePathFromHandle (Handle
);
1335 // Build the shell device path
1337 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1339 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1342 // Create and register the shell boot option
1344 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1349 Boot from the UEFI spec defined "BootNext" variable.
1361 BDS_COMMON_OPTION
*BootOption
;
1362 LIST_ENTRY TempList
;
1367 // Init the boot option name buffer and temp link list
1369 InitializeListHead (&TempList
);
1370 ZeroMem (Buffer
, sizeof (Buffer
));
1372 BootNext
= BdsLibGetVariableAndSize (
1374 &gEfiGlobalVariableGuid
,
1379 // Clear the boot next variable first
1381 if (BootNext
!= NULL
) {
1384 &gEfiGlobalVariableGuid
,
1385 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1391 // Start to build the boot option and try to boot
1393 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1394 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1395 ASSERT (BootOption
!= NULL
);
1396 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1397 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1403 Return the bootable media handle.
1404 First, check the device is connected
1405 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1406 Third, detect the the default boot file in the Media, and return the removable Media handle.
1408 @param DevicePath Device Path to a bootable device
1410 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1415 BdsLibGetBootableHandle (
1416 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1420 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1421 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1423 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1425 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1428 EFI_HANDLE ReturnHandle
;
1429 EFI_HANDLE
*SimpleFileSystemHandles
;
1431 UINTN NumberSimpleFileSystemHandles
;
1433 EFI_IMAGE_DOS_HEADER DosHeader
;
1434 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1435 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1437 UpdatedDevicePath
= DevicePath
;
1440 // Check whether the device is connected
1442 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1443 if (EFI_ERROR (Status
)) {
1445 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1447 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1448 if (EFI_ERROR (Status
)) {
1450 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1452 UpdatedDevicePath
= DevicePath
;
1453 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1454 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1458 // For removable device boot option, its contained device path only point to the removable device handle,
1459 // should make sure all its children handles (its child partion or media handles) are created and connected.
1461 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1463 // Get BlockIo protocol and check removable attribute
1465 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1467 // Issue a dummy read to the device to check for media change.
1468 // When the removable media is changed, any Block IO read/write will
1469 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1470 // returned. After the Block IO protocol is reinstalled, subsequent
1471 // Block IO read/write will success.
1473 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1474 if (Buffer
!= NULL
) {
1475 BlockIo
->ReadBlocks (
1477 BlockIo
->Media
->MediaId
,
1479 BlockIo
->Media
->BlockSize
,
1487 // Detect the the default boot file from removable Media
1491 // 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
1492 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1494 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1495 ASSERT (DupDevicePath
!= NULL
);
1497 UpdatedDevicePath
= DupDevicePath
;
1498 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1500 // 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
1501 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1503 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1504 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1506 // Remove the usb node, let the device path only point to PCI node
1508 SetDevicePathEndNode (UpdatedDevicePath
);
1509 UpdatedDevicePath
= DupDevicePath
;
1511 UpdatedDevicePath
= DevicePath
;
1515 // Get the device path size of boot option
1517 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1518 ReturnHandle
= NULL
;
1519 gBS
->LocateHandleBuffer (
1521 &gEfiSimpleFileSystemProtocolGuid
,
1523 &NumberSimpleFileSystemHandles
,
1524 &SimpleFileSystemHandles
1526 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1528 // Get the device path size of SimpleFileSystem handle
1530 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1531 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1533 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1535 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1537 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1538 // machinename is ia32, ia64, x64, ...
1540 Hdr
.Union
= &HdrData
;
1541 Status
= BdsLibGetImageHeader (
1542 SimpleFileSystemHandles
[Index
],
1543 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1547 if (!EFI_ERROR (Status
) &&
1548 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1549 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1550 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1556 FreePool(DupDevicePath
);
1558 if (SimpleFileSystemHandles
!= NULL
) {
1559 FreePool(SimpleFileSystemHandles
);
1562 return ReturnHandle
;
1566 Check to see if the network cable is plugged in. If the DevicePath is not
1567 connected it will be connected.
1569 @param DevicePath Device Path to check
1571 @retval TRUE DevicePath points to an Network that is connected
1572 @retval FALSE DevicePath does not point to a bootable network
1576 BdsLibNetworkBootWithMediaPresent (
1577 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1581 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1583 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1584 BOOLEAN MediaPresent
;
1585 UINT32 InterruptStatus
;
1587 MediaPresent
= FALSE
;
1589 UpdatedDevicePath
= DevicePath
;
1591 // Locate Load File Protocol for PXE boot option first
1593 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1594 if (EFI_ERROR (Status
)) {
1596 // Device not present so see if we need to connect it
1598 Status
= BdsLibConnectDevicePath (DevicePath
);
1599 if (!EFI_ERROR (Status
)) {
1601 // This one should work after we did the connect
1603 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1607 if (!EFI_ERROR (Status
)) {
1608 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1609 if (EFI_ERROR (Status
)) {
1611 // Failed to open SNP from this handle, try to get SNP from parent handle
1613 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1614 if (UpdatedDevicePath
!= NULL
) {
1615 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1616 if (!EFI_ERROR (Status
)) {
1618 // SNP handle found, get SNP from it
1620 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1625 if (!EFI_ERROR (Status
)) {
1626 if (Snp
->Mode
->MediaPresentSupported
) {
1627 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1629 // Invoke Snp->GetStatus() to refresh the media status
1631 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
1634 // In case some one else is using the SNP check to see if it's connected
1636 MediaPresent
= Snp
->Mode
->MediaPresent
;
1639 // No one is using SNP so we need to Start and Initialize so
1640 // MediaPresent will be valid.
1642 Status
= Snp
->Start (Snp
);
1643 if (!EFI_ERROR (Status
)) {
1644 Status
= Snp
->Initialize (Snp
, 0, 0);
1645 if (!EFI_ERROR (Status
)) {
1646 MediaPresent
= Snp
->Mode
->MediaPresent
;
1647 Snp
->Shutdown (Snp
);
1653 MediaPresent
= TRUE
;
1658 return MediaPresent
;
1662 For a bootable Device path, return its boot type.
1664 @param DevicePath The bootable device Path to check
1666 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1667 which subtype is MEDIA_HARDDRIVE_DP
1668 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1669 which subtype is MEDIA_CDROM_DP
1670 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1671 which HID is floppy device.
1672 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1673 and its last device path node's subtype is MSG_ATAPI_DP.
1674 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1675 and its last device path node's subtype is MSG_SCSI_DP.
1676 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1677 and its last device path node's subtype is MSG_USB_DP.
1678 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1679 its last device path node point to a message device path node.
1680 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1681 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1686 BdsGetBootTypeFromDevicePath (
1687 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1690 ACPI_HID_DEVICE_PATH
*Acpi
;
1691 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1692 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1695 if (NULL
== DevicePath
) {
1696 return BDS_EFI_UNSUPPORT
;
1699 TempDevicePath
= DevicePath
;
1701 while (!IsDevicePathEndType (TempDevicePath
)) {
1702 switch (DevicePathType (TempDevicePath
)) {
1703 case BBS_DEVICE_PATH
:
1704 return BDS_LEGACY_BBS_BOOT
;
1705 case MEDIA_DEVICE_PATH
:
1706 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1707 return BDS_EFI_MEDIA_HD_BOOT
;
1708 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1709 return BDS_EFI_MEDIA_CDROM_BOOT
;
1712 case ACPI_DEVICE_PATH
:
1713 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1714 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1715 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1718 case MESSAGING_DEVICE_PATH
:
1720 // Get the last device path node
1722 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1723 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1725 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1728 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1731 // if the device path not only point to driver device, it is not a messaging device path,
1733 if (!IsDevicePathEndType (LastDeviceNode
)) {
1737 switch (DevicePathSubType (TempDevicePath
)) {
1739 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1743 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1747 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1751 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1754 case MSG_MAC_ADDR_DP
:
1758 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1762 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1770 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1773 return BDS_EFI_UNSUPPORT
;
1777 Check whether the Device path in a boot option point to a valid bootable device,
1778 And if CheckMedia is true, check the device is ready to boot now.
1780 @param DevPath the Device path in a boot option
1781 @param CheckMedia if true, check the device is ready to boot now.
1783 @retval TRUE the Device path is valid
1784 @retval FALSE the Device path is invalid .
1789 BdsLibIsValidEFIBootOptDevicePath (
1790 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1791 IN BOOLEAN CheckMedia
1794 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1798 Check whether the Device path in a boot option point to a valid bootable device,
1799 And if CheckMedia is true, check the device is ready to boot now.
1800 If Description is not NULL and the device path point to a fixed BlockIo
1801 device, check the description whether conflict with other auto-created
1804 @param DevPath the Device path in a boot option
1805 @param CheckMedia if true, check the device is ready to boot now.
1806 @param Description the description in a boot option
1808 @retval TRUE the Device path is valid
1809 @retval FALSE the Device path is invalid .
1814 BdsLibIsValidEFIBootOptDevicePathExt (
1815 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1816 IN BOOLEAN CheckMedia
,
1817 IN CHAR16
*Description
1822 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1823 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1824 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1826 TempDevicePath
= DevPath
;
1827 LastDeviceNode
= DevPath
;
1830 // Check if it's a valid boot option for network boot device.
1831 // Check if there is EfiLoadFileProtocol installed.
1832 // If yes, that means there is a boot option for network.
1834 Status
= gBS
->LocateDevicePath (
1835 &gEfiLoadFileProtocolGuid
,
1839 if (EFI_ERROR (Status
)) {
1841 // Device not present so see if we need to connect it
1843 TempDevicePath
= DevPath
;
1844 BdsLibConnectDevicePath (TempDevicePath
);
1845 Status
= gBS
->LocateDevicePath (
1846 &gEfiLoadFileProtocolGuid
,
1852 if (!EFI_ERROR (Status
)) {
1853 if (!IsDevicePathEnd (TempDevicePath
)) {
1855 // LoadFile protocol is not installed on handle with exactly the same DevPath
1862 // Test if it is ready to boot now
1864 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1873 // If the boot option point to a file, it is a valid EFI boot option,
1874 // and assume it is ready to boot now
1876 while (!IsDevicePathEnd (TempDevicePath
)) {
1877 LastDeviceNode
= TempDevicePath
;
1878 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1880 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1881 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1886 // Check if it's a valid boot option for internal Shell
1888 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1890 // If the boot option point to Internal FV shell, make sure it is valid
1892 TempDevicePath
= DevPath
;
1893 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1894 if (Status
== EFI_ALREADY_STARTED
) {
1897 if (Status
== EFI_SUCCESS
) {
1898 FreePool (TempDevicePath
);
1905 // If the boot option point to a blockIO device:
1906 // if it is a removable blockIo device, it is valid.
1907 // if it is a fixed blockIo device, check its description confliction.
1909 TempDevicePath
= DevPath
;
1910 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1911 if (EFI_ERROR (Status
)) {
1913 // Device not present so see if we need to connect it
1915 Status
= BdsLibConnectDevicePath (DevPath
);
1916 if (!EFI_ERROR (Status
)) {
1918 // Try again to get the Block Io protocol after we did the connect
1920 TempDevicePath
= DevPath
;
1921 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1925 if (!EFI_ERROR (Status
)) {
1926 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1927 if (!EFI_ERROR (Status
)) {
1930 // Test if it is ready to boot now
1932 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1941 // 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,
1943 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1944 if (!EFI_ERROR (Status
)) {
1947 // Test if it is ready to boot now
1949 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1963 According to a file guild, check a Fv file device path is valid. If it is invalid,
1964 try to return the valid device path.
1965 FV address maybe changes for memory layout adjust from time to time, use this function
1966 could promise the Fv file device path is right.
1968 @param DevicePath on input, the Fv file device path need to check on
1969 output, the updated valid Fv file device path
1970 @param FileGuid the Fv file guild
1972 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1974 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1976 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1978 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1979 and return the updated device path in DevicePath
1984 BdsLibUpdateFvFileDevicePath (
1985 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1986 IN EFI_GUID
*FileGuid
1989 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1990 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1992 EFI_GUID
*GuidPoint
;
1994 UINTN FvHandleCount
;
1995 EFI_HANDLE
*FvHandleBuffer
;
1996 EFI_FV_FILETYPE Type
;
1998 EFI_FV_FILE_ATTRIBUTES Attributes
;
1999 UINT32 AuthenticationStatus
;
2001 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2002 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
2003 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
2004 EFI_HANDLE FoundFvHandle
;
2005 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2007 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2008 return EFI_INVALID_PARAMETER
;
2010 if (FileGuid
== NULL
) {
2011 return EFI_INVALID_PARAMETER
;
2015 // Check whether the device path point to the default the input Fv file
2017 TempDevicePath
= *DevicePath
;
2018 LastDeviceNode
= TempDevicePath
;
2019 while (!IsDevicePathEnd (TempDevicePath
)) {
2020 LastDeviceNode
= TempDevicePath
;
2021 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2023 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2024 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2026 if (GuidPoint
== NULL
) {
2028 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2030 return EFI_UNSUPPORTED
;
2032 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2034 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2036 return EFI_UNSUPPORTED
;
2040 // Check whether the input Fv file device path is valid
2042 TempDevicePath
= *DevicePath
;
2043 FoundFvHandle
= NULL
;
2044 Status
= gBS
->LocateDevicePath (
2045 &gEfiFirmwareVolume2ProtocolGuid
,
2049 if (!EFI_ERROR (Status
)) {
2050 Status
= gBS
->HandleProtocol (
2052 &gEfiFirmwareVolume2ProtocolGuid
,
2055 if (!EFI_ERROR (Status
)) {
2057 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2059 Status
= Fv
->ReadFile (
2066 &AuthenticationStatus
2068 if (!EFI_ERROR (Status
)) {
2069 return EFI_ALREADY_STARTED
;
2075 // Look for the input wanted FV file in current FV
2076 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2079 FoundFvHandle
= NULL
;
2080 Status
= gBS
->HandleProtocol (
2082 &gEfiLoadedImageProtocolGuid
,
2083 (VOID
**) &LoadedImage
2085 if (!EFI_ERROR (Status
)) {
2086 Status
= gBS
->HandleProtocol (
2087 LoadedImage
->DeviceHandle
,
2088 &gEfiFirmwareVolume2ProtocolGuid
,
2091 if (!EFI_ERROR (Status
)) {
2092 Status
= Fv
->ReadFile (
2099 &AuthenticationStatus
2101 if (!EFI_ERROR (Status
)) {
2103 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2108 // Second, if fail to find, try to enumerate all FV
2111 FvHandleBuffer
= NULL
;
2112 gBS
->LocateHandleBuffer (
2114 &gEfiFirmwareVolume2ProtocolGuid
,
2119 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2120 gBS
->HandleProtocol (
2121 FvHandleBuffer
[Index
],
2122 &gEfiFirmwareVolume2ProtocolGuid
,
2126 Status
= Fv
->ReadFile (
2133 &AuthenticationStatus
2135 if (EFI_ERROR (Status
)) {
2137 // Skip if input Fv file not in the FV
2142 FoundFvHandle
= FvHandleBuffer
[Index
];
2146 if (FvHandleBuffer
!= NULL
) {
2147 FreePool (FvHandleBuffer
);
2153 // Build the shell device path
2155 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2156 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2157 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2158 *DevicePath
= NewDevicePath
;
2161 return EFI_NOT_FOUND
;