2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2010, Intel Corporation. <BR>
5 All rights reserved. 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"
17 BOOLEAN mEnumBootDevice
= FALSE
;
20 /// This GUID is used for an EFI Variable that stores the front device pathes
21 /// for a partial device path that starts with the HD node.
23 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 Boot the legacy system with the boot option
30 @param Option The legacy boot option which have BBS device path
32 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
34 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
39 IN BDS_COMMON_OPTION
*Option
43 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
45 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
46 if (EFI_ERROR (Status
)) {
48 // If no LegacyBios protocol we do not support legacy boot
50 return EFI_UNSUPPORTED
;
53 // Notes: if we separate the int 19, then we don't need to refresh BBS
55 BdsRefreshBbsTableForBoot (Option
);
58 // Write boot to OS performance data for legacy boot.
61 WriteBootToOsPerformanceData ();
64 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
65 return LegacyBios
->LegacyBoot (
67 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
68 Option
->LoadOptionsSize
,
74 Internal function to check if the input boot option is a valid EFI NV Boot####.
76 @param OptionToCheck Boot option to be checked.
78 @retval TRUE This boot option matches a valid EFI NV Boot####.
83 IsBootOptionValidNVVarialbe (
84 IN BDS_COMMON_OPTION
*OptionToCheck
88 BDS_COMMON_OPTION
*BootOption
;
90 CHAR16 OptionName
[20];
94 InitializeListHead (&TempList
);
95 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
97 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
98 if (BootOption
== NULL
) {
103 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
104 // valid EFI NV Boot####.
106 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
107 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
112 FreePool (BootOption
);
117 Process the boot option follow the UEFI specification and
118 special treat the legacy boot option with BBS_DEVICE_PATH.
120 @param Option The boot option need to be processed
121 @param DevicePath The device path which describe where to load the
122 boot image or the legacy BBS device path to boot
124 @param ExitDataSize The size of exit data.
125 @param ExitData Data returned when Boot image failed.
127 @retval EFI_SUCCESS Boot from the input boot option successfully.
128 @retval EFI_NOT_FOUND If the Device Path is not found in the system
133 BdsLibBootViaBootOption (
134 IN BDS_COMMON_OPTION
*Option
,
135 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
136 OUT UINTN
*ExitDataSize
,
137 OUT CHAR16
**ExitData OPTIONAL
142 EFI_HANDLE ImageHandle
;
143 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
144 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
145 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
146 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
147 LIST_ENTRY TempBootLists
;
150 // Record the performance data for End of BDS
152 PERF_END(NULL
, "BDS", NULL
, 0);
158 // Notes: put EFI64 ROM Shadow Solution
160 EFI64_SHADOW_ALL_LEGACY_ROM ();
163 // Notes: this code can be remove after the s3 script table
164 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
165 // EVT_SIGNAL_LEGACY_BOOT
167 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
168 if (!EFI_ERROR (Status
)) {
169 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
172 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
175 WorkingDevicePath
= NULL
;
176 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
177 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
178 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
179 (HARDDRIVE_DEVICE_PATH
*)DevicePath
181 if (WorkingDevicePath
!= NULL
) {
182 DevicePath
= WorkingDevicePath
;
186 // Signal the EVT_SIGNAL_READY_TO_BOOT event
188 EfiSignalEventReadyToBoot();
194 if (IsBootOptionValidNVVarialbe (Option
)) {
196 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
197 // In this case, "BootCurrent" is not created.
198 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
202 &gEfiGlobalVariableGuid
,
203 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
209 ASSERT (Option
->DevicePath
!= NULL
);
210 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
211 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
214 // Check to see if we should legacy BOOT. If yes then do the legacy boot
216 return BdsLibDoLegacyBoot (Option
);
220 // If the boot option point to Internal FV shell, make sure it is valid
222 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
223 if (!EFI_ERROR(Status
)) {
224 if (Option
->DevicePath
!= NULL
) {
225 FreePool(Option
->DevicePath
);
227 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
228 ASSERT(Option
->DevicePath
!= NULL
);
229 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
231 // Update the shell boot option
233 InitializeListHead (&TempBootLists
);
234 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
237 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
239 FreePool (DevicePath
);
240 DevicePath
= Option
->DevicePath
;
244 UINTN DevicePathTypeValue
;
246 CHAR16
*BootStringNumber
;
249 DevicePathTypeValue
= BdsGetBootTypeFromDevicePath (Option
->DevicePath
);
252 // store number string of boot option temporary.
255 switch (DevicePathTypeValue
) {
256 case BDS_EFI_ACPI_FLOPPY_BOOT
:
257 HiiString
= L
"EFI Floppy";
259 case BDS_EFI_MEDIA_CDROM_BOOT
:
260 case BDS_EFI_MESSAGE_SATA_BOOT
:
261 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
262 HiiString
= L
"EFI DVD/CDROM";
264 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
265 HiiString
= L
"EFI USB Device";
267 case BDS_EFI_MESSAGE_SCSI_BOOT
:
268 HiiString
= L
"EFI SCSI Device";
270 case BDS_EFI_MESSAGE_MISC_BOOT
:
271 HiiString
= L
"EFI Misc Device";
273 case BDS_EFI_MESSAGE_MAC_BOOT
:
274 HiiString
= L
"EFI Network";
276 case BBS_DEVICE_PATH
:
278 // Do nothing for legacy boot option.
282 DEBUG((EFI_D_INFO
, "Can not find HiiString for given device path type 0x%x\n", DevicePathTypeValue
));
286 // If found Hii description string then cat Hii string with original description.
288 if (HiiString
!= NULL
) {
289 BootStringNumber
= Option
->Description
;
290 BufferSize
= StrSize(BootStringNumber
);
291 BufferSize
+= StrSize(HiiString
);
292 Option
->Description
= AllocateZeroPool(BufferSize
);
293 ASSERT (Option
->Description
!= NULL
);
294 StrCpy (Option
->Description
, HiiString
);
295 if (StrnCmp (BootStringNumber
, L
"0", 1) != 0) {
296 StrCat (Option
->Description
, L
" ");
297 StrCat (Option
->Description
, BootStringNumber
);
300 FreePool (BootStringNumber
);
303 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
307 Status
= gBS
->LoadImage (
317 // If we didn't find an image directly, we need to try as if it is a removable device boot option
318 // and load the image according to the default boot behavior for removable device.
320 if (EFI_ERROR (Status
)) {
322 // check if there is a bootable removable media could be found in this device path ,
323 // and get the bootable media handle
325 Handle
= BdsLibGetBootableHandle(DevicePath
);
326 if (Handle
== NULL
) {
330 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
331 // machinename is ia32, ia64, x64, ...
333 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
334 if (FilePath
!= NULL
) {
335 Status
= gBS
->LoadImage (
343 if (EFI_ERROR (Status
)) {
345 // The DevicePath failed, and it's not a valid
346 // removable media device.
353 if (EFI_ERROR (Status
)) {
355 // It there is any error from the Boot attempt exit now.
360 // Provide the image with it's load options
362 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
363 ASSERT_EFI_ERROR (Status
);
365 if (Option
->LoadOptionsSize
!= 0) {
366 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
367 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
370 // Before calling the image, enable the Watchdog Timer for
371 // the 5 Minute period
373 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
376 // Write boot to OS performance data for UEFI boot
379 WriteBootToOsPerformanceData ();
382 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
383 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
386 // Clear the Watchdog Timer after the image returns
388 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
392 // Clear Boot Current
396 &gEfiGlobalVariableGuid
,
397 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
407 Expand a device path that starts with a hard drive media device path node to be a
408 full device path that includes the full hardware path to the device. We need
409 to do this so it can be booted. As an optimization the front match (the part point
410 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
411 so a connect all is not required on every boot. All successful history device path
412 which point to partition node (the front part) will be saved.
414 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
415 drive media device path.
416 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
420 EFI_DEVICE_PATH_PROTOCOL
*
422 BdsExpandPartitionPartialDevicePathToFull (
423 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
427 UINTN BlockIoHandleCount
;
428 EFI_HANDLE
*BlockIoBuffer
;
429 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
430 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
431 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
434 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
435 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
436 UINTN CachedDevicePathSize
;
439 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
442 FullDevicePath
= NULL
;
444 // Check if there is prestore 'HDDP' variable.
445 // If exist, search the front path which point to partition node in the variable instants.
446 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
448 CachedDevicePath
= BdsLibGetVariableAndSize (
450 &mHdBootVariablePrivateGuid
,
451 &CachedDevicePathSize
454 if (CachedDevicePath
!= NULL
) {
455 TempNewDevicePath
= CachedDevicePath
;
460 // Check every instance of the variable
461 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
462 // partial partition boot option. Second, check whether the instance could be connected.
464 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
465 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
467 // Connect the device path instance, the device path point to hard drive media device path node
468 // e.g. ACPI() /PCI()/ATA()/Partition()
470 Status
= BdsLibConnectDevicePath (Instance
);
471 if (!EFI_ERROR (Status
)) {
477 // Come here means the first instance is not matched
481 } while (TempNewDevicePath
!= NULL
);
485 // Find the matched device path.
486 // Append the file path information from the boot option and return the fully expanded device path.
488 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
489 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
492 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
496 // First delete the matched instance.
498 TempNewDevicePath
= CachedDevicePath
;
499 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
500 FreePool (TempNewDevicePath
);
503 // Second, append the remaining path after the matched instance
505 TempNewDevicePath
= CachedDevicePath
;
506 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
507 FreePool (TempNewDevicePath
);
509 // Save the matching Device Path so we don't need to do a connect all next time
511 Status
= gRT
->SetVariable (
513 &mHdBootVariablePrivateGuid
,
514 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
515 GetDevicePathSize (CachedDevicePath
),
521 FreePool (CachedDevicePath
);
522 return FullDevicePath
;
527 // If we get here we fail to find or 'HDDP' not exist, and now we need
528 // to search all devices in the system for a matched partition
530 BdsLibConnectAllDriversToAllControllers ();
531 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
532 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
534 // If there was an error or there are no device handles that support
535 // the BLOCK_IO Protocol, then return.
540 // Loop through all the device handles that support the BLOCK_IO Protocol
542 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
544 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
545 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
549 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
551 // Find the matched partition device path
553 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
554 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
557 // Save the matched partition device path in 'HDDP' variable
559 if (CachedDevicePath
!= NULL
) {
561 // Save the matched partition device path as first instance of 'HDDP' variable
563 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
564 TempNewDevicePath
= CachedDevicePath
;
565 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
566 FreePool(TempNewDevicePath
);
568 TempNewDevicePath
= CachedDevicePath
;
569 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
570 if (TempNewDevicePath
!= NULL
) {
571 FreePool(TempNewDevicePath
);
574 TempNewDevicePath
= CachedDevicePath
;
575 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
576 FreePool(TempNewDevicePath
);
579 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
580 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
583 ASSERT (CachedDevicePath
!= NULL
);
584 TempNewDevicePath
= CachedDevicePath
;
585 while (!IsDevicePathEnd (TempNewDevicePath
)) {
586 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
588 // Parse one instance
590 while (!IsDevicePathEndType (TempNewDevicePath
)) {
591 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
595 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
597 if (InstanceNum
>= 12) {
598 SetDevicePathEndNode (TempNewDevicePath
);
603 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
607 // Save the matching Device Path so we don't need to do a connect all next time
609 Status
= gRT
->SetVariable (
611 &mHdBootVariablePrivateGuid
,
612 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
613 GetDevicePathSize (CachedDevicePath
),
621 if (CachedDevicePath
!= NULL
) {
622 FreePool (CachedDevicePath
);
624 if (BlockIoBuffer
!= NULL
) {
625 FreePool (BlockIoBuffer
);
627 return FullDevicePath
;
631 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
632 instances, has the same partition node with HardDriveDevicePath device path
634 @param BlockIoDevicePath Multi device path instances which need to check
635 @param HardDriveDevicePath A device path which starts with a hard drive media
638 @retval TRUE There is a matched device path instance.
639 @retval FALSE There is no matched device path instance.
644 MatchPartitionDevicePathNode (
645 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
646 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
649 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
650 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
652 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
654 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
659 // Make PreviousDevicePath == the device path node before the end node
661 DevicePath
= BlockIoDevicePath
;
662 BlockIoHdDevicePathNode
= NULL
;
665 // find the partition device path node
667 while (!IsDevicePathEnd (DevicePath
)) {
668 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
669 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
671 BlockIoHdDevicePathNode
= DevicePath
;
675 DevicePath
= NextDevicePathNode (DevicePath
);
678 if (BlockIoHdDevicePathNode
== NULL
) {
682 // See if the harddrive device path in blockio matches the orig Hard Drive Node
684 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
688 // Check for the match
690 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
691 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
692 switch (TmpHdPath
->SignatureType
) {
693 case SIGNATURE_TYPE_GUID
:
694 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
696 case SIGNATURE_TYPE_MBR
:
697 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
709 Delete the boot option associated with the handle passed in.
711 @param Handle The handle which present the device path to create
714 @retval EFI_SUCCESS Delete the boot option success
715 @retval EFI_NOT_FOUND If the Device Path is not found in the system
716 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
717 @retval Other Error return value from SetVariable()
721 BdsLibDeleteOptionFromHandle (
726 UINT8
*BootOptionVar
;
728 UINTN BootOptionSize
;
731 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
732 UINTN DevicePathSize
;
733 UINTN OptionDevicePathSize
;
734 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
735 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
738 Status
= EFI_SUCCESS
;
743 // Check "BootOrder" variable, if no, means there is no any boot order.
745 BootOrder
= BdsLibGetVariableAndSize (
747 &gEfiGlobalVariableGuid
,
750 if (BootOrder
== NULL
) {
751 return EFI_NOT_FOUND
;
755 // Convert device handle to device path protocol instance
757 DevicePath
= DevicePathFromHandle (Handle
);
758 if (DevicePath
== NULL
) {
759 return EFI_NOT_FOUND
;
761 DevicePathSize
= GetDevicePathSize (DevicePath
);
764 // Loop all boot order variable and find the matching device path
767 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
768 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
769 BootOptionVar
= BdsLibGetVariableAndSize (
771 &gEfiGlobalVariableGuid
,
775 if (BootOptionVar
== NULL
) {
776 FreePool (BootOrder
);
777 return EFI_OUT_OF_RESOURCES
;
780 TempPtr
= BootOptionVar
;
781 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
782 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
783 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
784 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
787 // Check whether the device path match
789 if ((OptionDevicePathSize
== DevicePathSize
) &&
790 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
791 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
792 FreePool (BootOptionVar
);
796 FreePool (BootOptionVar
);
801 // Adjust number of boot option for "BootOrder" variable.
803 Status
= gRT
->SetVariable (
805 &gEfiGlobalVariableGuid
,
806 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
811 FreePool (BootOrder
);
818 Delete all invalid EFI boot options.
820 @retval EFI_SUCCESS Delete all invalid boot option success
821 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
822 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
823 @retval Other Error return value from SetVariable()
827 BdsDeleteAllInvalidEfiBootOption (
832 UINT8
*BootOptionVar
;
834 UINTN BootOptionSize
;
838 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
839 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
843 Status
= EFI_SUCCESS
;
848 // Check "BootOrder" variable firstly, this variable hold the number of boot options
850 BootOrder
= BdsLibGetVariableAndSize (
852 &gEfiGlobalVariableGuid
,
855 if (NULL
== BootOrder
) {
856 return EFI_NOT_FOUND
;
860 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
861 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
862 BootOptionVar
= BdsLibGetVariableAndSize (
864 &gEfiGlobalVariableGuid
,
867 if (NULL
== BootOptionVar
) {
868 FreePool (BootOrder
);
869 return EFI_OUT_OF_RESOURCES
;
872 TempPtr
= BootOptionVar
;
873 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
874 Description
= (CHAR16
*) TempPtr
;
875 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
876 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
879 // Skip legacy boot option (BBS boot device)
881 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
882 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
883 FreePool (BootOptionVar
);
888 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
890 // Delete this invalid boot option "Boot####"
892 Status
= gRT
->SetVariable (
894 &gEfiGlobalVariableGuid
,
895 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
900 // Mark this boot option in boot order as deleted
902 BootOrder
[Index
] = 0xffff;
905 FreePool (BootOptionVar
);
910 // Adjust boot order array
913 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
914 if (BootOrder
[Index
] != 0xffff) {
915 BootOrder
[Index2
] = BootOrder
[Index
];
919 Status
= gRT
->SetVariable (
921 &gEfiGlobalVariableGuid
,
922 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
923 Index2
* sizeof (UINT16
),
927 FreePool (BootOrder
);
934 For EFI boot option, BDS separate them as six types:
935 1. Network - The boot option points to the SimpleNetworkProtocol device.
936 Bds will try to automatically create this type boot option when enumerate.
937 2. Shell - The boot option points to internal flash shell.
938 Bds will try to automatically create this type boot option when enumerate.
939 3. Removable BlockIo - The boot option only points to the removable media
940 device, like USB flash disk, DVD, Floppy etc.
941 These device should contain a *removable* blockIo
942 protocol in their device handle.
943 Bds will try to automatically create this type boot option
945 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
947 These device should contain a *fixed* blockIo
948 protocol in their device handle.
949 BDS will skip fixed blockIo devices, and NOT
950 automatically create boot option for them. But BDS
951 will help to delete those fixed blockIo boot option,
952 whose description rule conflict with other auto-created
954 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
955 has SimpleFileSystem Protocol, but has no blockio
956 protocol. These devices do not offer blockIo
957 protocol, but BDS still can get the
958 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
960 6. File - The boot option points to a file. These boot options are usually
961 created by user manually or OS loader. BDS will not delete or modify
964 This function will enumerate all possible boot device in the system, and
965 automatically create boot options for Network, Shell, Removable BlockIo,
966 and Non-BlockIo Simplefile devices.
967 It will only execute once of every boot.
969 @param BdsBootOptionList The header of the link list which indexed all
972 @retval EFI_SUCCESS Finished all the boot device enumerate and create
973 the boot option base on that boot device
975 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
979 BdsLibEnumerateAllBootOption (
980 IN OUT LIST_ENTRY
*BdsBootOptionList
989 UINT16 NonBlockNumber
;
990 UINTN NumberBlockIoHandles
;
991 EFI_HANDLE
*BlockIoHandles
;
992 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
994 UINTN NumOfLoadFileHandles
;
995 EFI_HANDLE
*LoadFileHandles
;
997 EFI_HANDLE
*FvHandleBuffer
;
998 EFI_FV_FILETYPE Type
;
1000 EFI_FV_FILE_ATTRIBUTES Attributes
;
1001 UINT32 AuthenticationStatus
;
1002 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1003 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1004 UINTN DevicePathType
;
1006 EFI_HANDLE
*FileSystemHandles
;
1007 UINTN NumberFileSystemHandles
;
1009 EFI_IMAGE_DOS_HEADER DosHeader
;
1010 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1011 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1018 ZeroMem (Buffer
, sizeof (Buffer
));
1021 // If the boot device enumerate happened, just get the boot
1022 // device from the boot order variable
1024 if (mEnumBootDevice
) {
1025 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1030 // Notes: this dirty code is to get the legacy boot option from the
1031 // BBS table and create to variable as the EFI boot option, it should
1032 // be removed after the CSM can provide legacy boot option directly
1034 REFRESH_LEGACY_BOOT_OPTIONS
;
1037 // Delete invalid boot option
1039 BdsDeleteAllInvalidEfiBootOption ();
1042 // Parse removable media
1044 gBS
->LocateHandleBuffer (
1046 &gEfiBlockIoProtocolGuid
,
1048 &NumberBlockIoHandles
,
1052 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1053 Status
= gBS
->HandleProtocol (
1054 BlockIoHandles
[Index
],
1055 &gEfiBlockIoProtocolGuid
,
1058 if (!EFI_ERROR (Status
)) {
1059 if (!BlkIo
->Media
->RemovableMedia
) {
1061 // skip the non-removable block devices
1066 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1067 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1069 switch (DevicePathType
) {
1070 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1071 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", FloppyNumber
);
1072 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1077 // Assume a removable SATA device should be the DVD/CD device
1079 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1080 case BDS_EFI_MESSAGE_SATA_BOOT
:
1081 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", CdromNumber
);
1082 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1086 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1087 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", UsbNumber
);
1088 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1092 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1093 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", ScsiNumber
);
1094 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1098 case BDS_EFI_MESSAGE_MISC_BOOT
:
1099 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", MiscNumber
);
1100 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1109 if (NumberBlockIoHandles
!= 0) {
1110 FreePool (BlockIoHandles
);
1114 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1117 gBS
->LocateHandleBuffer (
1119 &gEfiSimpleFileSystemProtocolGuid
,
1121 &NumberFileSystemHandles
,
1124 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1125 Status
= gBS
->HandleProtocol (
1126 FileSystemHandles
[Index
],
1127 &gEfiBlockIoProtocolGuid
,
1130 if (!EFI_ERROR (Status
)) {
1132 // Skip if the file system handle supports a BlkIo protocol,
1138 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1139 // machinename is ia32, ia64, x64, ...
1141 Hdr
.Union
= &HdrData
;
1143 Status
= BdsLibGetImageHeader (
1144 FileSystemHandles
[Index
],
1145 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1149 if (!EFI_ERROR (Status
) &&
1150 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1151 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1157 // No such file or the file is not a EFI application, delete this boot option
1159 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1161 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1162 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1167 if (NumberFileSystemHandles
!= 0) {
1168 FreePool (FileSystemHandles
);
1172 // Parse Network Boot Device
1174 NumOfLoadFileHandles
= 0;
1176 // Search Load File protocol for PXE boot option.
1178 gBS
->LocateHandleBuffer (
1180 &gEfiLoadFileProtocolGuid
,
1182 &NumOfLoadFileHandles
,
1186 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1187 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1188 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1191 if (NumOfLoadFileHandles
!= 0) {
1192 FreePool (LoadFileHandles
);
1196 // Check if we have on flash shell
1198 gBS
->LocateHandleBuffer (
1200 &gEfiFirmwareVolume2ProtocolGuid
,
1205 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1206 gBS
->HandleProtocol (
1207 FvHandleBuffer
[Index
],
1208 &gEfiFirmwareVolume2ProtocolGuid
,
1212 Status
= Fv
->ReadFile (
1214 PcdGetPtr(PcdShellFile
),
1219 &AuthenticationStatus
1221 if (EFI_ERROR (Status
)) {
1223 // Skip if no shell file in the FV
1228 // Build the shell boot option
1230 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1233 if (FvHandleCount
!= 0) {
1234 FreePool (FvHandleBuffer
);
1237 // Make sure every boot only have one time
1238 // boot device enumerate
1240 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1241 mEnumBootDevice
= TRUE
;
1247 Build the boot option with the handle parsed in
1249 @param Handle The handle which present the device path to create
1251 @param BdsBootOptionList The header of the link list which indexed all
1252 current boot options
1253 @param String The description of the boot option.
1258 BdsLibBuildOptionFromHandle (
1259 IN EFI_HANDLE Handle
,
1260 IN LIST_ENTRY
*BdsBootOptionList
,
1264 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1266 DevicePath
= DevicePathFromHandle (Handle
);
1269 // Create and register new boot option
1271 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1276 Build the on flash shell boot option with the handle parsed in.
1278 @param Handle The handle which present the device path to create
1279 on flash shell boot option
1280 @param BdsBootOptionList The header of the link list which indexed all
1281 current boot options
1286 BdsLibBuildOptionFromShell (
1287 IN EFI_HANDLE Handle
,
1288 IN OUT LIST_ENTRY
*BdsBootOptionList
1291 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1292 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1294 DevicePath
= DevicePathFromHandle (Handle
);
1297 // Build the shell device path
1299 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1301 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1304 // Create and register the shell boot option
1306 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1311 Boot from the UEFI spec defined "BootNext" variable.
1323 BDS_COMMON_OPTION
*BootOption
;
1324 LIST_ENTRY TempList
;
1329 // Init the boot option name buffer and temp link list
1331 InitializeListHead (&TempList
);
1332 ZeroMem (Buffer
, sizeof (Buffer
));
1334 BootNext
= BdsLibGetVariableAndSize (
1336 &gEfiGlobalVariableGuid
,
1341 // Clear the boot next variable first
1343 if (BootNext
!= NULL
) {
1346 &gEfiGlobalVariableGuid
,
1347 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1353 // Start to build the boot option and try to boot
1355 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1356 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1357 ASSERT (BootOption
!= NULL
);
1358 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1359 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1365 Return the bootable media handle.
1366 First, check the device is connected
1367 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1368 Third, detect the the default boot file in the Media, and return the removable Media handle.
1370 @param DevicePath Device Path to a bootable device
1372 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1377 BdsLibGetBootableHandle (
1378 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1382 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1383 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1385 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1387 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1390 EFI_HANDLE ReturnHandle
;
1391 EFI_HANDLE
*SimpleFileSystemHandles
;
1393 UINTN NumberSimpleFileSystemHandles
;
1395 EFI_IMAGE_DOS_HEADER DosHeader
;
1396 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1397 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1399 UpdatedDevicePath
= DevicePath
;
1402 // Check whether the device is connected
1404 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1405 if (EFI_ERROR (Status
)) {
1407 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1409 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1410 if (EFI_ERROR (Status
)) {
1412 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1414 UpdatedDevicePath
= DevicePath
;
1415 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1416 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1420 // Get BlockIo protocol and check removable attribute
1422 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1424 // Issue a dummy read to the device to check for media change.
1425 // When the removable media is changed, any Block IO read/write will
1426 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1427 // returned. After the Block IO protocol is reinstalled, subsequent
1428 // Block IO read/write will success.
1430 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1431 if (Buffer
!= NULL
) {
1432 BlockIo
->ReadBlocks (
1434 BlockIo
->Media
->MediaId
,
1436 BlockIo
->Media
->BlockSize
,
1444 // Detect the the default boot file from removable Media
1448 // 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
1449 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1451 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1452 ASSERT (DupDevicePath
!= NULL
);
1454 UpdatedDevicePath
= DupDevicePath
;
1455 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1457 // 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
1458 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1460 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1461 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1463 // Remove the usb node, let the device path only point to PCI node
1465 SetDevicePathEndNode (UpdatedDevicePath
);
1466 UpdatedDevicePath
= DupDevicePath
;
1468 UpdatedDevicePath
= DevicePath
;
1472 // Get the device path size of boot option
1474 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1475 ReturnHandle
= NULL
;
1476 gBS
->LocateHandleBuffer (
1478 &gEfiSimpleFileSystemProtocolGuid
,
1480 &NumberSimpleFileSystemHandles
,
1481 &SimpleFileSystemHandles
1483 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1485 // Get the device path size of SimpleFileSystem handle
1487 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1488 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1490 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1492 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1494 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1495 // machinename is ia32, ia64, x64, ...
1497 Hdr
.Union
= &HdrData
;
1498 Status
= BdsLibGetImageHeader (
1499 SimpleFileSystemHandles
[Index
],
1500 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1504 if (!EFI_ERROR (Status
) &&
1505 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1506 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1507 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1513 FreePool(DupDevicePath
);
1515 if (SimpleFileSystemHandles
!= NULL
) {
1516 FreePool(SimpleFileSystemHandles
);
1519 return ReturnHandle
;
1523 Check to see if the network cable is plugged in. If the DevicePath is not
1524 connected it will be connected.
1526 @param DevicePath Device Path to check
1528 @retval TRUE DevicePath points to an Network that is connected
1529 @retval FALSE DevicePath does not point to a bootable network
1533 BdsLibNetworkBootWithMediaPresent (
1534 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1538 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1540 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1541 BOOLEAN MediaPresent
;
1543 MediaPresent
= FALSE
;
1545 UpdatedDevicePath
= DevicePath
;
1547 // Locate Load File Protocol for PXE boot option first
1549 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1550 if (EFI_ERROR (Status
)) {
1552 // Device not present so see if we need to connect it
1554 Status
= BdsLibConnectDevicePath (DevicePath
);
1555 if (!EFI_ERROR (Status
)) {
1557 // This one should work after we did the connect
1559 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1563 if (!EFI_ERROR (Status
)) {
1564 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1565 if (EFI_ERROR (Status
)) {
1567 // Failed to open SNP from this handle, try to get SNP from parent handle
1569 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1570 if (UpdatedDevicePath
!= NULL
) {
1571 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1572 if (!EFI_ERROR (Status
)) {
1574 // SNP handle found, get SNP from it
1576 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1581 if (!EFI_ERROR (Status
)) {
1582 if (Snp
->Mode
->MediaPresentSupported
) {
1583 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1585 // In case some one else is using the SNP check to see if it's connected
1587 MediaPresent
= Snp
->Mode
->MediaPresent
;
1590 // No one is using SNP so we need to Start and Initialize so
1591 // MediaPresent will be valid.
1593 Status
= Snp
->Start (Snp
);
1594 if (!EFI_ERROR (Status
)) {
1595 Status
= Snp
->Initialize (Snp
, 0, 0);
1596 if (!EFI_ERROR (Status
)) {
1597 MediaPresent
= Snp
->Mode
->MediaPresent
;
1598 Snp
->Shutdown (Snp
);
1604 MediaPresent
= TRUE
;
1609 return MediaPresent
;
1613 For a bootable Device path, return its boot type.
1615 @param DevicePath The bootable device Path to check
1617 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1618 which subtype is MEDIA_HARDDRIVE_DP
1619 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1620 which subtype is MEDIA_CDROM_DP
1621 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1622 which HID is floppy device.
1623 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1624 and its last device path node's subtype is MSG_ATAPI_DP.
1625 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1626 and its last device path node's subtype is MSG_SCSI_DP.
1627 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1628 and its last device path node's subtype is MSG_USB_DP.
1629 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1630 its last device path node point to a message device path node.
1631 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1632 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1637 BdsGetBootTypeFromDevicePath (
1638 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1641 ACPI_HID_DEVICE_PATH
*Acpi
;
1642 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1643 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1646 if (NULL
== DevicePath
) {
1647 return BDS_EFI_UNSUPPORT
;
1650 TempDevicePath
= DevicePath
;
1652 while (!IsDevicePathEndType (TempDevicePath
)) {
1653 switch (DevicePathType (TempDevicePath
)) {
1654 case BBS_DEVICE_PATH
:
1655 return BDS_LEGACY_BBS_BOOT
;
1656 case MEDIA_DEVICE_PATH
:
1657 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1658 return BDS_EFI_MEDIA_HD_BOOT
;
1659 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1660 return BDS_EFI_MEDIA_CDROM_BOOT
;
1663 case ACPI_DEVICE_PATH
:
1664 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1665 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1666 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1669 case MESSAGING_DEVICE_PATH
:
1671 // Get the last device path node
1673 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1674 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1676 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1679 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1682 // if the device path not only point to driver device, it is not a messaging device path,
1684 if (!IsDevicePathEndType (LastDeviceNode
)) {
1688 switch (DevicePathSubType (TempDevicePath
)) {
1690 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1694 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1698 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1702 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1705 case MSG_MAC_ADDR_DP
:
1709 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1713 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1721 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1724 return BDS_EFI_UNSUPPORT
;
1728 Check whether the Device path in a boot option point to a valid bootable device,
1729 And if CheckMedia is true, check the device is ready to boot now.
1731 @param DevPath the Device path in a boot option
1732 @param CheckMedia if true, check the device is ready to boot now.
1734 @retval TRUE the Device path is valid
1735 @retval FALSE the Device path is invalid .
1740 BdsLibIsValidEFIBootOptDevicePath (
1741 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1742 IN BOOLEAN CheckMedia
1745 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1749 Check whether the Device path in a boot option point to a valid bootable device,
1750 And if CheckMedia is true, check the device is ready to boot now.
1751 If Description is not NULL and the device path point to a fixed BlockIo
1752 device, check the description whether conflict with other auto-created
1755 @param DevPath the Device path in a boot option
1756 @param CheckMedia if true, check the device is ready to boot now.
1757 @param Description the description in a boot option
1759 @retval TRUE the Device path is valid
1760 @retval FALSE the Device path is invalid .
1765 BdsLibIsValidEFIBootOptDevicePathExt (
1766 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1767 IN BOOLEAN CheckMedia
,
1768 IN CHAR16
*Description
1773 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1774 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1775 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1777 TempDevicePath
= DevPath
;
1778 LastDeviceNode
= DevPath
;
1781 // Check if it's a valid boot option for network boot device.
1782 // Check if there is EfiLoadFileProtocol installed.
1783 // If yes, that means there is a boot option for network.
1785 Status
= gBS
->LocateDevicePath (
1786 &gEfiLoadFileProtocolGuid
,
1790 if (EFI_ERROR (Status
)) {
1792 // Device not present so see if we need to connect it
1794 TempDevicePath
= DevPath
;
1795 BdsLibConnectDevicePath (TempDevicePath
);
1796 Status
= gBS
->LocateDevicePath (
1797 &gEfiLoadFileProtocolGuid
,
1803 if (!EFI_ERROR (Status
)) {
1804 if (!IsDevicePathEnd (TempDevicePath
)) {
1806 // LoadFile protocol is not installed on handle with exactly the same DevPath
1813 // Test if it is ready to boot now
1815 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1824 // If the boot option point to a file, it is a valid EFI boot option,
1825 // and assume it is ready to boot now
1827 while (!IsDevicePathEnd (TempDevicePath
)) {
1828 LastDeviceNode
= TempDevicePath
;
1829 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1831 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1832 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1837 // Check if it's a valid boot option for internal Shell
1839 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1841 // If the boot option point to Internal FV shell, make sure it is valid
1843 TempDevicePath
= DevPath
;
1844 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1845 if (Status
== EFI_ALREADY_STARTED
) {
1848 if (Status
== EFI_SUCCESS
) {
1849 FreePool (TempDevicePath
);
1856 // If the boot option point to a blockIO device:
1857 // if it is a removable blockIo device, it is valid.
1858 // if it is a fixed blockIo device, check its description confliction.
1860 TempDevicePath
= DevPath
;
1861 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1862 if (EFI_ERROR (Status
)) {
1864 // Device not present so see if we need to connect it
1866 Status
= BdsLibConnectDevicePath (DevPath
);
1867 if (!EFI_ERROR (Status
)) {
1869 // Try again to get the Block Io protocol after we did the connect
1871 TempDevicePath
= DevPath
;
1872 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1876 if (!EFI_ERROR (Status
)) {
1877 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1878 if (!EFI_ERROR (Status
)) {
1881 // Test if it is ready to boot now
1883 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1892 // 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,
1894 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1895 if (!EFI_ERROR (Status
)) {
1898 // Test if it is ready to boot now
1900 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1914 According to a file guild, check a Fv file device path is valid. If it is invalid,
1915 try to return the valid device path.
1916 FV address maybe changes for memory layout adjust from time to time, use this function
1917 could promise the Fv file device path is right.
1919 @param DevicePath on input, the Fv file device path need to check on
1920 output, the updated valid Fv file device path
1921 @param FileGuid the Fv file guild
1923 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1925 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1927 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1929 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1930 and return the updated device path in DevicePath
1935 BdsLibUpdateFvFileDevicePath (
1936 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1937 IN EFI_GUID
*FileGuid
1940 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1941 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1943 EFI_GUID
*GuidPoint
;
1945 UINTN FvHandleCount
;
1946 EFI_HANDLE
*FvHandleBuffer
;
1947 EFI_FV_FILETYPE Type
;
1949 EFI_FV_FILE_ATTRIBUTES Attributes
;
1950 UINT32 AuthenticationStatus
;
1952 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1953 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1954 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1955 EFI_HANDLE FoundFvHandle
;
1956 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1958 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1959 return EFI_INVALID_PARAMETER
;
1961 if (FileGuid
== NULL
) {
1962 return EFI_INVALID_PARAMETER
;
1966 // Check whether the device path point to the default the input Fv file
1968 TempDevicePath
= *DevicePath
;
1969 LastDeviceNode
= TempDevicePath
;
1970 while (!IsDevicePathEnd (TempDevicePath
)) {
1971 LastDeviceNode
= TempDevicePath
;
1972 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1974 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1975 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1977 if (GuidPoint
== NULL
) {
1979 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1981 return EFI_UNSUPPORTED
;
1983 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1985 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1987 return EFI_UNSUPPORTED
;
1991 // Check whether the input Fv file device path is valid
1993 TempDevicePath
= *DevicePath
;
1994 FoundFvHandle
= NULL
;
1995 Status
= gBS
->LocateDevicePath (
1996 &gEfiFirmwareVolume2ProtocolGuid
,
2000 if (!EFI_ERROR (Status
)) {
2001 Status
= gBS
->HandleProtocol (
2003 &gEfiFirmwareVolume2ProtocolGuid
,
2006 if (!EFI_ERROR (Status
)) {
2008 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2010 Status
= Fv
->ReadFile (
2017 &AuthenticationStatus
2019 if (!EFI_ERROR (Status
)) {
2020 return EFI_ALREADY_STARTED
;
2026 // Look for the input wanted FV file in current FV
2027 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2030 FoundFvHandle
= NULL
;
2031 Status
= gBS
->HandleProtocol (
2033 &gEfiLoadedImageProtocolGuid
,
2034 (VOID
**) &LoadedImage
2036 if (!EFI_ERROR (Status
)) {
2037 Status
= gBS
->HandleProtocol (
2038 LoadedImage
->DeviceHandle
,
2039 &gEfiFirmwareVolume2ProtocolGuid
,
2042 if (!EFI_ERROR (Status
)) {
2043 Status
= Fv
->ReadFile (
2050 &AuthenticationStatus
2052 if (!EFI_ERROR (Status
)) {
2054 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2059 // Second, if fail to find, try to enumerate all FV
2062 FvHandleBuffer
= NULL
;
2063 gBS
->LocateHandleBuffer (
2065 &gEfiFirmwareVolume2ProtocolGuid
,
2070 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2071 gBS
->HandleProtocol (
2072 FvHandleBuffer
[Index
],
2073 &gEfiFirmwareVolume2ProtocolGuid
,
2077 Status
= Fv
->ReadFile (
2084 &AuthenticationStatus
2086 if (EFI_ERROR (Status
)) {
2088 // Skip if input Fv file not in the FV
2093 FoundFvHandle
= FvHandleBuffer
[Index
];
2097 if (FvHandleBuffer
!= NULL
) {
2098 FreePool (FvHandleBuffer
);
2104 // Build the shell device path
2106 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2107 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2108 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2109 *DevicePath
= NewDevicePath
;
2112 return EFI_NOT_FOUND
;