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: put EFI64 ROM Shadow Solution
205 EFI64_SHADOW_ALL_LEGACY_ROM ();
208 // Notes: this code can be remove after the s3 script table
209 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
210 // EVT_SIGNAL_LEGACY_BOOT
212 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
213 if (!EFI_ERROR (Status
)) {
214 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
217 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
220 WorkingDevicePath
= NULL
;
221 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
222 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
223 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
224 (HARDDRIVE_DEVICE_PATH
*)DevicePath
226 if (WorkingDevicePath
!= NULL
) {
227 DevicePath
= WorkingDevicePath
;
231 // Signal the EVT_SIGNAL_READY_TO_BOOT event
233 EfiSignalEventReadyToBoot();
239 if (IsBootOptionValidNVVarialbe (Option
)) {
241 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
242 // In this case, "BootCurrent" is not created.
243 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
247 &gEfiGlobalVariableGuid
,
248 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
254 ASSERT (Option
->DevicePath
!= NULL
);
255 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
256 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
259 // Check to see if we should legacy BOOT. If yes then do the legacy boot
261 return BdsLibDoLegacyBoot (Option
);
265 // If the boot option point to Internal FV shell, make sure it is valid
267 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
268 if (!EFI_ERROR(Status
)) {
269 if (Option
->DevicePath
!= NULL
) {
270 FreePool(Option
->DevicePath
);
272 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
273 ASSERT(Option
->DevicePath
!= NULL
);
274 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
276 // Update the shell boot option
278 InitializeListHead (&TempBootLists
);
279 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
282 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
284 FreePool (DevicePath
);
285 DevicePath
= Option
->DevicePath
;
290 if (Option
->Description
== NULL
) {
291 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
293 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
298 Status
= gBS
->LoadImage (
308 // If we didn't find an image directly, we need to try as if it is a removable device boot option
309 // and load the image according to the default boot behavior for removable device.
311 if (EFI_ERROR (Status
)) {
313 // check if there is a bootable removable media could be found in this device path ,
314 // and get the bootable media handle
316 Handle
= BdsLibGetBootableHandle(DevicePath
);
317 if (Handle
== NULL
) {
321 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
322 // machinename is ia32, ia64, x64, ...
324 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
325 if (FilePath
!= NULL
) {
326 Status
= gBS
->LoadImage (
334 if (EFI_ERROR (Status
)) {
336 // The DevicePath failed, and it's not a valid
337 // removable media device.
344 if (EFI_ERROR (Status
)) {
346 // It there is any error from the Boot attempt exit now.
351 // Provide the image with it's load options
353 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
354 ASSERT_EFI_ERROR (Status
);
356 if (Option
->LoadOptionsSize
!= 0) {
357 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
358 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
361 // Before calling the image, enable the Watchdog Timer for
362 // the 5 Minute period
364 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
367 // Write boot to OS performance data for UEFI boot
370 WriteBootToOsPerformanceData ();
373 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
374 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
377 // Clear the Watchdog Timer after the image returns
379 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
383 // Clear Boot Current
387 &gEfiGlobalVariableGuid
,
388 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
398 Expand a device path that starts with a hard drive media device path node to be a
399 full device path that includes the full hardware path to the device. We need
400 to do this so it can be booted. As an optimization the front match (the part point
401 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
402 so a connect all is not required on every boot. All successful history device path
403 which point to partition node (the front part) will be saved.
405 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
406 drive media device path.
407 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
411 EFI_DEVICE_PATH_PROTOCOL
*
413 BdsExpandPartitionPartialDevicePathToFull (
414 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
418 UINTN BlockIoHandleCount
;
419 EFI_HANDLE
*BlockIoBuffer
;
420 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
421 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
422 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
425 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
426 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
427 UINTN CachedDevicePathSize
;
430 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
433 FullDevicePath
= NULL
;
435 // Check if there is prestore 'HDDP' variable.
436 // If exist, search the front path which point to partition node in the variable instants.
437 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
439 CachedDevicePath
= BdsLibGetVariableAndSize (
441 &mHdBootVariablePrivateGuid
,
442 &CachedDevicePathSize
445 if (CachedDevicePath
!= NULL
) {
446 TempNewDevicePath
= CachedDevicePath
;
451 // Check every instance of the variable
452 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
453 // partial partition boot option. Second, check whether the instance could be connected.
455 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
456 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
458 // Connect the device path instance, the device path point to hard drive media device path node
459 // e.g. ACPI() /PCI()/ATA()/Partition()
461 Status
= BdsLibConnectDevicePath (Instance
);
462 if (!EFI_ERROR (Status
)) {
468 // Come here means the first instance is not matched
472 } while (TempNewDevicePath
!= NULL
);
476 // Find the matched device path.
477 // Append the file path information from the boot option and return the fully expanded device path.
479 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
480 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
483 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
487 // First delete the matched instance.
489 TempNewDevicePath
= CachedDevicePath
;
490 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
491 FreePool (TempNewDevicePath
);
494 // Second, append the remaining path after the matched instance
496 TempNewDevicePath
= CachedDevicePath
;
497 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
498 FreePool (TempNewDevicePath
);
500 // Save the matching Device Path so we don't need to do a connect all next time
502 Status
= gRT
->SetVariable (
504 &mHdBootVariablePrivateGuid
,
505 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
506 GetDevicePathSize (CachedDevicePath
),
512 FreePool (CachedDevicePath
);
513 return FullDevicePath
;
518 // If we get here we fail to find or 'HDDP' not exist, and now we need
519 // to search all devices in the system for a matched partition
521 BdsLibConnectAllDriversToAllControllers ();
522 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
523 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
525 // If there was an error or there are no device handles that support
526 // the BLOCK_IO Protocol, then return.
531 // Loop through all the device handles that support the BLOCK_IO Protocol
533 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
535 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
536 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
540 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
542 // Find the matched partition device path
544 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
545 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
548 // Save the matched partition device path in 'HDDP' variable
550 if (CachedDevicePath
!= NULL
) {
552 // Save the matched partition device path as first instance of 'HDDP' variable
554 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
555 TempNewDevicePath
= CachedDevicePath
;
556 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
557 FreePool(TempNewDevicePath
);
559 TempNewDevicePath
= CachedDevicePath
;
560 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
561 if (TempNewDevicePath
!= NULL
) {
562 FreePool(TempNewDevicePath
);
565 TempNewDevicePath
= CachedDevicePath
;
566 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
567 FreePool(TempNewDevicePath
);
570 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
571 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
574 ASSERT (CachedDevicePath
!= NULL
);
575 TempNewDevicePath
= CachedDevicePath
;
576 while (!IsDevicePathEnd (TempNewDevicePath
)) {
577 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
579 // Parse one instance
581 while (!IsDevicePathEndType (TempNewDevicePath
)) {
582 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
586 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
588 if (InstanceNum
>= 12) {
589 SetDevicePathEndNode (TempNewDevicePath
);
594 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
598 // Save the matching Device Path so we don't need to do a connect all next time
600 Status
= gRT
->SetVariable (
602 &mHdBootVariablePrivateGuid
,
603 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
604 GetDevicePathSize (CachedDevicePath
),
612 if (CachedDevicePath
!= NULL
) {
613 FreePool (CachedDevicePath
);
615 if (BlockIoBuffer
!= NULL
) {
616 FreePool (BlockIoBuffer
);
618 return FullDevicePath
;
622 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
623 instances, has the same partition node with HardDriveDevicePath device path
625 @param BlockIoDevicePath Multi device path instances which need to check
626 @param HardDriveDevicePath A device path which starts with a hard drive media
629 @retval TRUE There is a matched device path instance.
630 @retval FALSE There is no matched device path instance.
635 MatchPartitionDevicePathNode (
636 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
637 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
640 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
641 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
643 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
645 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
650 // Make PreviousDevicePath == the device path node before the end node
652 DevicePath
= BlockIoDevicePath
;
653 BlockIoHdDevicePathNode
= NULL
;
656 // find the partition device path node
658 while (!IsDevicePathEnd (DevicePath
)) {
659 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
660 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
662 BlockIoHdDevicePathNode
= DevicePath
;
666 DevicePath
= NextDevicePathNode (DevicePath
);
669 if (BlockIoHdDevicePathNode
== NULL
) {
673 // See if the harddrive device path in blockio matches the orig Hard Drive Node
675 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
679 // Check for the match
681 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
682 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
683 switch (TmpHdPath
->SignatureType
) {
684 case SIGNATURE_TYPE_GUID
:
685 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
687 case SIGNATURE_TYPE_MBR
:
688 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
700 Delete the boot option associated with the handle passed in.
702 @param Handle The handle which present the device path to create
705 @retval EFI_SUCCESS Delete the boot option success
706 @retval EFI_NOT_FOUND If the Device Path is not found in the system
707 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
708 @retval Other Error return value from SetVariable()
712 BdsLibDeleteOptionFromHandle (
717 UINT8
*BootOptionVar
;
719 UINTN BootOptionSize
;
722 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
723 UINTN DevicePathSize
;
724 UINTN OptionDevicePathSize
;
725 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
726 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
729 Status
= EFI_SUCCESS
;
734 // Check "BootOrder" variable, if no, means there is no any boot order.
736 BootOrder
= BdsLibGetVariableAndSize (
738 &gEfiGlobalVariableGuid
,
741 if (BootOrder
== NULL
) {
742 return EFI_NOT_FOUND
;
746 // Convert device handle to device path protocol instance
748 DevicePath
= DevicePathFromHandle (Handle
);
749 if (DevicePath
== NULL
) {
750 return EFI_NOT_FOUND
;
752 DevicePathSize
= GetDevicePathSize (DevicePath
);
755 // Loop all boot order variable and find the matching device path
758 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
759 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
760 BootOptionVar
= BdsLibGetVariableAndSize (
762 &gEfiGlobalVariableGuid
,
766 if (BootOptionVar
== NULL
) {
767 FreePool (BootOrder
);
768 return EFI_OUT_OF_RESOURCES
;
771 TempPtr
= BootOptionVar
;
772 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
773 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
774 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
775 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
778 // Check whether the device path match
780 if ((OptionDevicePathSize
== DevicePathSize
) &&
781 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
782 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
783 FreePool (BootOptionVar
);
787 FreePool (BootOptionVar
);
792 // Adjust number of boot option for "BootOrder" variable.
794 Status
= gRT
->SetVariable (
796 &gEfiGlobalVariableGuid
,
797 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
802 FreePool (BootOrder
);
809 Delete all invalid EFI boot options.
811 @retval EFI_SUCCESS Delete all invalid boot option success
812 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
813 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
814 @retval Other Error return value from SetVariable()
818 BdsDeleteAllInvalidEfiBootOption (
823 UINT8
*BootOptionVar
;
825 UINTN BootOptionSize
;
829 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
830 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
834 Status
= EFI_SUCCESS
;
839 // Check "BootOrder" variable firstly, this variable hold the number of boot options
841 BootOrder
= BdsLibGetVariableAndSize (
843 &gEfiGlobalVariableGuid
,
846 if (NULL
== BootOrder
) {
847 return EFI_NOT_FOUND
;
851 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
852 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
853 BootOptionVar
= BdsLibGetVariableAndSize (
855 &gEfiGlobalVariableGuid
,
858 if (NULL
== BootOptionVar
) {
859 FreePool (BootOrder
);
860 return EFI_OUT_OF_RESOURCES
;
863 TempPtr
= BootOptionVar
;
864 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
865 Description
= (CHAR16
*) TempPtr
;
866 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
867 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
870 // Skip legacy boot option (BBS boot device)
872 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
873 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
874 FreePool (BootOptionVar
);
879 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
881 // Delete this invalid boot option "Boot####"
883 Status
= gRT
->SetVariable (
885 &gEfiGlobalVariableGuid
,
886 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
891 // Mark this boot option in boot order as deleted
893 BootOrder
[Index
] = 0xffff;
896 FreePool (BootOptionVar
);
901 // Adjust boot order array
904 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
905 if (BootOrder
[Index
] != 0xffff) {
906 BootOrder
[Index2
] = BootOrder
[Index
];
910 Status
= gRT
->SetVariable (
912 &gEfiGlobalVariableGuid
,
913 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
914 Index2
* sizeof (UINT16
),
918 FreePool (BootOrder
);
925 For EFI boot option, BDS separate them as six types:
926 1. Network - The boot option points to the SimpleNetworkProtocol device.
927 Bds will try to automatically create this type boot option when enumerate.
928 2. Shell - The boot option points to internal flash shell.
929 Bds will try to automatically create this type boot option when enumerate.
930 3. Removable BlockIo - The boot option only points to the removable media
931 device, like USB flash disk, DVD, Floppy etc.
932 These device should contain a *removable* blockIo
933 protocol in their device handle.
934 Bds will try to automatically create this type boot option
936 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
938 These device should contain a *fixed* blockIo
939 protocol in their device handle.
940 BDS will skip fixed blockIo devices, and NOT
941 automatically create boot option for them. But BDS
942 will help to delete those fixed blockIo boot option,
943 whose description rule conflict with other auto-created
945 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
946 has SimpleFileSystem Protocol, but has no blockio
947 protocol. These devices do not offer blockIo
948 protocol, but BDS still can get the
949 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
951 6. File - The boot option points to a file. These boot options are usually
952 created by user manually or OS loader. BDS will not delete or modify
955 This function will enumerate all possible boot device in the system, and
956 automatically create boot options for Network, Shell, Removable BlockIo,
957 and Non-BlockIo Simplefile devices.
958 It will only execute once of every boot.
960 @param BdsBootOptionList The header of the link list which indexed all
963 @retval EFI_SUCCESS Finished all the boot device enumerate and create
964 the boot option base on that boot device
966 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
970 BdsLibEnumerateAllBootOption (
971 IN OUT LIST_ENTRY
*BdsBootOptionList
980 UINT16 NonBlockNumber
;
981 UINTN NumberBlockIoHandles
;
982 EFI_HANDLE
*BlockIoHandles
;
983 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
985 UINTN NumOfLoadFileHandles
;
986 EFI_HANDLE
*LoadFileHandles
;
988 EFI_HANDLE
*FvHandleBuffer
;
989 EFI_FV_FILETYPE Type
;
991 EFI_FV_FILE_ATTRIBUTES Attributes
;
992 UINT32 AuthenticationStatus
;
993 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
994 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
995 UINTN DevicePathType
;
997 EFI_HANDLE
*FileSystemHandles
;
998 UINTN NumberFileSystemHandles
;
1000 EFI_IMAGE_DOS_HEADER DosHeader
;
1003 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1004 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1013 ZeroMem (Buffer
, sizeof (Buffer
));
1016 // If the boot device enumerate happened, just get the boot
1017 // device from the boot order variable
1019 if (mEnumBootDevice
) {
1020 LastLang
= GetVariable (L
"LastEnumLang", &mBdsLibLastLangGuid
);
1021 PlatLang
= GetEfiGlobalVariable (L
"PlatformLang");
1022 if (LastLang
== PlatLang
) {
1023 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1026 Status
= gRT
->SetVariable (
1028 &mBdsLibLastLangGuid
,
1029 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1033 ASSERT_EFI_ERROR (Status
);
1038 // Notes: this dirty code is to get the legacy boot option from the
1039 // BBS table and create to variable as the EFI boot option, it should
1040 // be removed after the CSM can provide legacy boot option directly
1042 REFRESH_LEGACY_BOOT_OPTIONS
;
1045 // Delete invalid boot option
1047 BdsDeleteAllInvalidEfiBootOption ();
1050 // Parse removable media
1052 gBS
->LocateHandleBuffer (
1054 &gEfiBlockIoProtocolGuid
,
1056 &NumberBlockIoHandles
,
1060 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1061 Status
= gBS
->HandleProtocol (
1062 BlockIoHandles
[Index
],
1063 &gEfiBlockIoProtocolGuid
,
1066 if (!EFI_ERROR (Status
)) {
1067 if (!BlkIo
->Media
->RemovableMedia
) {
1069 // skip the non-removable block devices
1074 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1075 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1077 switch (DevicePathType
) {
1078 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1079 if (FloppyNumber
!= 0) {
1080 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
1082 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
1084 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1089 // Assume a removable SATA device should be the DVD/CD device
1091 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1092 case BDS_EFI_MESSAGE_SATA_BOOT
:
1093 if (CdromNumber
!= 0) {
1094 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
1096 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
1098 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
1099 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1103 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1104 if (UsbNumber
!= 0) {
1105 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
1107 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
1109 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1113 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1114 if (ScsiNumber
!= 0) {
1115 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
1117 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
1119 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1123 case BDS_EFI_MESSAGE_MISC_BOOT
:
1124 if (MiscNumber
!= 0) {
1125 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
1127 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
1129 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1138 if (NumberBlockIoHandles
!= 0) {
1139 FreePool (BlockIoHandles
);
1143 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1146 gBS
->LocateHandleBuffer (
1148 &gEfiSimpleFileSystemProtocolGuid
,
1150 &NumberFileSystemHandles
,
1153 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1154 Status
= gBS
->HandleProtocol (
1155 FileSystemHandles
[Index
],
1156 &gEfiBlockIoProtocolGuid
,
1159 if (!EFI_ERROR (Status
)) {
1161 // Skip if the file system handle supports a BlkIo protocol,
1167 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1168 // machinename is ia32, ia64, x64, ...
1170 Hdr
.Union
= &HdrData
;
1172 Status
= BdsLibGetImageHeader (
1173 FileSystemHandles
[Index
],
1174 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1178 if (!EFI_ERROR (Status
) &&
1179 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1180 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1186 // No such file or the file is not a EFI application, delete this boot option
1188 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1190 if (NonBlockNumber
!= 0) {
1191 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
1193 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
1195 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1200 if (NumberFileSystemHandles
!= 0) {
1201 FreePool (FileSystemHandles
);
1205 // Parse Network Boot Device
1207 NumOfLoadFileHandles
= 0;
1209 // Search Load File protocol for PXE boot option.
1211 gBS
->LocateHandleBuffer (
1213 &gEfiLoadFileProtocolGuid
,
1215 &NumOfLoadFileHandles
,
1219 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1221 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
1223 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
1225 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1228 if (NumOfLoadFileHandles
!= 0) {
1229 FreePool (LoadFileHandles
);
1233 // Check if we have on flash shell
1235 gBS
->LocateHandleBuffer (
1237 &gEfiFirmwareVolume2ProtocolGuid
,
1242 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1243 gBS
->HandleProtocol (
1244 FvHandleBuffer
[Index
],
1245 &gEfiFirmwareVolume2ProtocolGuid
,
1249 Status
= Fv
->ReadFile (
1251 PcdGetPtr(PcdShellFile
),
1256 &AuthenticationStatus
1258 if (EFI_ERROR (Status
)) {
1260 // Skip if no shell file in the FV
1265 // Build the shell boot option
1267 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1270 if (FvHandleCount
!= 0) {
1271 FreePool (FvHandleBuffer
);
1274 // Make sure every boot only have one time
1275 // boot device enumerate
1277 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1278 mEnumBootDevice
= TRUE
;
1284 Build the boot option with the handle parsed in
1286 @param Handle The handle which present the device path to create
1288 @param BdsBootOptionList The header of the link list which indexed all
1289 current boot options
1290 @param String The description of the boot option.
1295 BdsLibBuildOptionFromHandle (
1296 IN EFI_HANDLE Handle
,
1297 IN LIST_ENTRY
*BdsBootOptionList
,
1301 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1303 DevicePath
= DevicePathFromHandle (Handle
);
1306 // Create and register new boot option
1308 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1313 Build the on flash shell boot option with the handle parsed in.
1315 @param Handle The handle which present the device path to create
1316 on flash shell boot option
1317 @param BdsBootOptionList The header of the link list which indexed all
1318 current boot options
1323 BdsLibBuildOptionFromShell (
1324 IN EFI_HANDLE Handle
,
1325 IN OUT LIST_ENTRY
*BdsBootOptionList
1328 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1329 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1331 DevicePath
= DevicePathFromHandle (Handle
);
1334 // Build the shell device path
1336 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1338 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1341 // Create and register the shell boot option
1343 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1348 Boot from the UEFI spec defined "BootNext" variable.
1360 BDS_COMMON_OPTION
*BootOption
;
1361 LIST_ENTRY TempList
;
1366 // Init the boot option name buffer and temp link list
1368 InitializeListHead (&TempList
);
1369 ZeroMem (Buffer
, sizeof (Buffer
));
1371 BootNext
= BdsLibGetVariableAndSize (
1373 &gEfiGlobalVariableGuid
,
1378 // Clear the boot next variable first
1380 if (BootNext
!= NULL
) {
1383 &gEfiGlobalVariableGuid
,
1384 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1390 // Start to build the boot option and try to boot
1392 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1393 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1394 ASSERT (BootOption
!= NULL
);
1395 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1396 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1402 Return the bootable media handle.
1403 First, check the device is connected
1404 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1405 Third, detect the the default boot file in the Media, and return the removable Media handle.
1407 @param DevicePath Device Path to a bootable device
1409 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1414 BdsLibGetBootableHandle (
1415 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1419 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1420 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1422 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1424 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1427 EFI_HANDLE ReturnHandle
;
1428 EFI_HANDLE
*SimpleFileSystemHandles
;
1430 UINTN NumberSimpleFileSystemHandles
;
1432 EFI_IMAGE_DOS_HEADER DosHeader
;
1433 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1434 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1436 UpdatedDevicePath
= DevicePath
;
1439 // Check whether the device is connected
1441 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1442 if (EFI_ERROR (Status
)) {
1444 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1446 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1447 if (EFI_ERROR (Status
)) {
1449 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1451 UpdatedDevicePath
= DevicePath
;
1452 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1453 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1457 // For removable device boot option, its contained device path only point to the removable device handle,
1458 // should make sure all its children handles (its child partion or media handles) are created and connected.
1460 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1462 // Get BlockIo protocol and check removable attribute
1464 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1466 // Issue a dummy read to the device to check for media change.
1467 // When the removable media is changed, any Block IO read/write will
1468 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1469 // returned. After the Block IO protocol is reinstalled, subsequent
1470 // Block IO read/write will success.
1472 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1473 if (Buffer
!= NULL
) {
1474 BlockIo
->ReadBlocks (
1476 BlockIo
->Media
->MediaId
,
1478 BlockIo
->Media
->BlockSize
,
1486 // Detect the the default boot file from removable Media
1490 // 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
1491 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1493 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1494 ASSERT (DupDevicePath
!= NULL
);
1496 UpdatedDevicePath
= DupDevicePath
;
1497 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1499 // 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
1500 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1502 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1503 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1505 // Remove the usb node, let the device path only point to PCI node
1507 SetDevicePathEndNode (UpdatedDevicePath
);
1508 UpdatedDevicePath
= DupDevicePath
;
1510 UpdatedDevicePath
= DevicePath
;
1514 // Get the device path size of boot option
1516 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1517 ReturnHandle
= NULL
;
1518 gBS
->LocateHandleBuffer (
1520 &gEfiSimpleFileSystemProtocolGuid
,
1522 &NumberSimpleFileSystemHandles
,
1523 &SimpleFileSystemHandles
1525 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1527 // Get the device path size of SimpleFileSystem handle
1529 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1530 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1532 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1534 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1536 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1537 // machinename is ia32, ia64, x64, ...
1539 Hdr
.Union
= &HdrData
;
1540 Status
= BdsLibGetImageHeader (
1541 SimpleFileSystemHandles
[Index
],
1542 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1546 if (!EFI_ERROR (Status
) &&
1547 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1548 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1549 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1555 FreePool(DupDevicePath
);
1557 if (SimpleFileSystemHandles
!= NULL
) {
1558 FreePool(SimpleFileSystemHandles
);
1561 return ReturnHandle
;
1565 Check to see if the network cable is plugged in. If the DevicePath is not
1566 connected it will be connected.
1568 @param DevicePath Device Path to check
1570 @retval TRUE DevicePath points to an Network that is connected
1571 @retval FALSE DevicePath does not point to a bootable network
1575 BdsLibNetworkBootWithMediaPresent (
1576 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1580 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1582 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1583 BOOLEAN MediaPresent
;
1584 UINT32 InterruptStatus
;
1586 MediaPresent
= FALSE
;
1588 UpdatedDevicePath
= DevicePath
;
1590 // Locate Load File Protocol for PXE boot option first
1592 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1593 if (EFI_ERROR (Status
)) {
1595 // Device not present so see if we need to connect it
1597 Status
= BdsLibConnectDevicePath (DevicePath
);
1598 if (!EFI_ERROR (Status
)) {
1600 // This one should work after we did the connect
1602 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1606 if (!EFI_ERROR (Status
)) {
1607 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1608 if (EFI_ERROR (Status
)) {
1610 // Failed to open SNP from this handle, try to get SNP from parent handle
1612 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1613 if (UpdatedDevicePath
!= NULL
) {
1614 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1615 if (!EFI_ERROR (Status
)) {
1617 // SNP handle found, get SNP from it
1619 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1624 if (!EFI_ERROR (Status
)) {
1625 if (Snp
->Mode
->MediaPresentSupported
) {
1626 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1628 // Invoke Snp->GetStatus() to refresh the media status
1630 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
1633 // In case some one else is using the SNP check to see if it's connected
1635 MediaPresent
= Snp
->Mode
->MediaPresent
;
1638 // No one is using SNP so we need to Start and Initialize so
1639 // MediaPresent will be valid.
1641 Status
= Snp
->Start (Snp
);
1642 if (!EFI_ERROR (Status
)) {
1643 Status
= Snp
->Initialize (Snp
, 0, 0);
1644 if (!EFI_ERROR (Status
)) {
1645 MediaPresent
= Snp
->Mode
->MediaPresent
;
1646 Snp
->Shutdown (Snp
);
1652 MediaPresent
= TRUE
;
1657 return MediaPresent
;
1661 For a bootable Device path, return its boot type.
1663 @param DevicePath The bootable device Path to check
1665 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1666 which subtype is MEDIA_HARDDRIVE_DP
1667 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1668 which subtype is MEDIA_CDROM_DP
1669 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1670 which HID is floppy device.
1671 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1672 and its last device path node's subtype is MSG_ATAPI_DP.
1673 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1674 and its last device path node's subtype is MSG_SCSI_DP.
1675 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1676 and its last device path node's subtype is MSG_USB_DP.
1677 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1678 its last device path node point to a message device path node.
1679 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1680 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1685 BdsGetBootTypeFromDevicePath (
1686 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1689 ACPI_HID_DEVICE_PATH
*Acpi
;
1690 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1691 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1694 if (NULL
== DevicePath
) {
1695 return BDS_EFI_UNSUPPORT
;
1698 TempDevicePath
= DevicePath
;
1700 while (!IsDevicePathEndType (TempDevicePath
)) {
1701 switch (DevicePathType (TempDevicePath
)) {
1702 case BBS_DEVICE_PATH
:
1703 return BDS_LEGACY_BBS_BOOT
;
1704 case MEDIA_DEVICE_PATH
:
1705 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1706 return BDS_EFI_MEDIA_HD_BOOT
;
1707 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1708 return BDS_EFI_MEDIA_CDROM_BOOT
;
1711 case ACPI_DEVICE_PATH
:
1712 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1713 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1714 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1717 case MESSAGING_DEVICE_PATH
:
1719 // Get the last device path node
1721 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1722 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1724 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1727 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1730 // if the device path not only point to driver device, it is not a messaging device path,
1732 if (!IsDevicePathEndType (LastDeviceNode
)) {
1736 switch (DevicePathSubType (TempDevicePath
)) {
1738 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1742 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1746 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1750 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1753 case MSG_MAC_ADDR_DP
:
1757 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1761 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1769 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1772 return BDS_EFI_UNSUPPORT
;
1776 Check whether the Device path in a boot option point to a valid bootable device,
1777 And if CheckMedia is true, check the device is ready to boot now.
1779 @param DevPath the Device path in a boot option
1780 @param CheckMedia if true, check the device is ready to boot now.
1782 @retval TRUE the Device path is valid
1783 @retval FALSE the Device path is invalid .
1788 BdsLibIsValidEFIBootOptDevicePath (
1789 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1790 IN BOOLEAN CheckMedia
1793 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1797 Check whether the Device path in a boot option point to a valid bootable device,
1798 And if CheckMedia is true, check the device is ready to boot now.
1799 If Description is not NULL and the device path point to a fixed BlockIo
1800 device, check the description whether conflict with other auto-created
1803 @param DevPath the Device path in a boot option
1804 @param CheckMedia if true, check the device is ready to boot now.
1805 @param Description the description in a boot option
1807 @retval TRUE the Device path is valid
1808 @retval FALSE the Device path is invalid .
1813 BdsLibIsValidEFIBootOptDevicePathExt (
1814 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1815 IN BOOLEAN CheckMedia
,
1816 IN CHAR16
*Description
1821 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1822 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1823 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1825 TempDevicePath
= DevPath
;
1826 LastDeviceNode
= DevPath
;
1829 // Check if it's a valid boot option for network boot device.
1830 // Check if there is EfiLoadFileProtocol installed.
1831 // If yes, that means there is a boot option for network.
1833 Status
= gBS
->LocateDevicePath (
1834 &gEfiLoadFileProtocolGuid
,
1838 if (EFI_ERROR (Status
)) {
1840 // Device not present so see if we need to connect it
1842 TempDevicePath
= DevPath
;
1843 BdsLibConnectDevicePath (TempDevicePath
);
1844 Status
= gBS
->LocateDevicePath (
1845 &gEfiLoadFileProtocolGuid
,
1851 if (!EFI_ERROR (Status
)) {
1852 if (!IsDevicePathEnd (TempDevicePath
)) {
1854 // LoadFile protocol is not installed on handle with exactly the same DevPath
1861 // Test if it is ready to boot now
1863 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1872 // If the boot option point to a file, it is a valid EFI boot option,
1873 // and assume it is ready to boot now
1875 while (!IsDevicePathEnd (TempDevicePath
)) {
1876 LastDeviceNode
= TempDevicePath
;
1877 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1879 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1880 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1885 // Check if it's a valid boot option for internal Shell
1887 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1889 // If the boot option point to Internal FV shell, make sure it is valid
1891 TempDevicePath
= DevPath
;
1892 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1893 if (Status
== EFI_ALREADY_STARTED
) {
1896 if (Status
== EFI_SUCCESS
) {
1897 FreePool (TempDevicePath
);
1904 // If the boot option point to a blockIO device:
1905 // if it is a removable blockIo device, it is valid.
1906 // if it is a fixed blockIo device, check its description confliction.
1908 TempDevicePath
= DevPath
;
1909 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1910 if (EFI_ERROR (Status
)) {
1912 // Device not present so see if we need to connect it
1914 Status
= BdsLibConnectDevicePath (DevPath
);
1915 if (!EFI_ERROR (Status
)) {
1917 // Try again to get the Block Io protocol after we did the connect
1919 TempDevicePath
= DevPath
;
1920 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1924 if (!EFI_ERROR (Status
)) {
1925 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1926 if (!EFI_ERROR (Status
)) {
1929 // Test if it is ready to boot now
1931 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1940 // 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,
1942 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1943 if (!EFI_ERROR (Status
)) {
1946 // Test if it is ready to boot now
1948 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1962 According to a file guild, check a Fv file device path is valid. If it is invalid,
1963 try to return the valid device path.
1964 FV address maybe changes for memory layout adjust from time to time, use this function
1965 could promise the Fv file device path is right.
1967 @param DevicePath on input, the Fv file device path need to check on
1968 output, the updated valid Fv file device path
1969 @param FileGuid the Fv file guild
1971 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1973 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1975 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1977 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1978 and return the updated device path in DevicePath
1983 BdsLibUpdateFvFileDevicePath (
1984 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1985 IN EFI_GUID
*FileGuid
1988 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1989 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1991 EFI_GUID
*GuidPoint
;
1993 UINTN FvHandleCount
;
1994 EFI_HANDLE
*FvHandleBuffer
;
1995 EFI_FV_FILETYPE Type
;
1997 EFI_FV_FILE_ATTRIBUTES Attributes
;
1998 UINT32 AuthenticationStatus
;
2000 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2001 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
2002 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
2003 EFI_HANDLE FoundFvHandle
;
2004 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2006 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2007 return EFI_INVALID_PARAMETER
;
2009 if (FileGuid
== NULL
) {
2010 return EFI_INVALID_PARAMETER
;
2014 // Check whether the device path point to the default the input Fv file
2016 TempDevicePath
= *DevicePath
;
2017 LastDeviceNode
= TempDevicePath
;
2018 while (!IsDevicePathEnd (TempDevicePath
)) {
2019 LastDeviceNode
= TempDevicePath
;
2020 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2022 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2023 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2025 if (GuidPoint
== NULL
) {
2027 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2029 return EFI_UNSUPPORTED
;
2031 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2033 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2035 return EFI_UNSUPPORTED
;
2039 // Check whether the input Fv file device path is valid
2041 TempDevicePath
= *DevicePath
;
2042 FoundFvHandle
= NULL
;
2043 Status
= gBS
->LocateDevicePath (
2044 &gEfiFirmwareVolume2ProtocolGuid
,
2048 if (!EFI_ERROR (Status
)) {
2049 Status
= gBS
->HandleProtocol (
2051 &gEfiFirmwareVolume2ProtocolGuid
,
2054 if (!EFI_ERROR (Status
)) {
2056 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2058 Status
= Fv
->ReadFile (
2065 &AuthenticationStatus
2067 if (!EFI_ERROR (Status
)) {
2068 return EFI_ALREADY_STARTED
;
2074 // Look for the input wanted FV file in current FV
2075 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2078 FoundFvHandle
= NULL
;
2079 Status
= gBS
->HandleProtocol (
2081 &gEfiLoadedImageProtocolGuid
,
2082 (VOID
**) &LoadedImage
2084 if (!EFI_ERROR (Status
)) {
2085 Status
= gBS
->HandleProtocol (
2086 LoadedImage
->DeviceHandle
,
2087 &gEfiFirmwareVolume2ProtocolGuid
,
2090 if (!EFI_ERROR (Status
)) {
2091 Status
= Fv
->ReadFile (
2098 &AuthenticationStatus
2100 if (!EFI_ERROR (Status
)) {
2102 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2107 // Second, if fail to find, try to enumerate all FV
2110 FvHandleBuffer
= NULL
;
2111 gBS
->LocateHandleBuffer (
2113 &gEfiFirmwareVolume2ProtocolGuid
,
2118 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2119 gBS
->HandleProtocol (
2120 FvHandleBuffer
[Index
],
2121 &gEfiFirmwareVolume2ProtocolGuid
,
2125 Status
= Fv
->ReadFile (
2132 &AuthenticationStatus
2134 if (EFI_ERROR (Status
)) {
2136 // Skip if input Fv file not in the FV
2141 FoundFvHandle
= FvHandleBuffer
[Index
];
2145 if (FvHandleBuffer
!= NULL
) {
2146 FreePool (FvHandleBuffer
);
2152 // Build the shell device path
2154 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2155 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2156 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2157 *DevicePath
= NewDevicePath
;
2160 return EFI_NOT_FOUND
;