2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2009, 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 StrCpy (Option
->Description
, HiiString
);
294 if (StrnCmp (BootStringNumber
, L
"0", 1) != 0) {
295 StrCat (Option
->Description
, L
" ");
296 StrCat (Option
->Description
, BootStringNumber
);
299 FreePool (BootStringNumber
);
302 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
306 Status
= gBS
->LoadImage (
316 // If we didn't find an image directly, we need to try as if it is a removable device boot option
317 // and load the image according to the default boot behavior for removable device.
319 if (EFI_ERROR (Status
)) {
321 // check if there is a bootable removable media could be found in this device path ,
322 // and get the bootable media handle
324 Handle
= BdsLibGetBootableHandle(DevicePath
);
325 if (Handle
== NULL
) {
329 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
330 // machinename is ia32, ia64, x64, ...
332 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
333 if (FilePath
!= NULL
) {
334 Status
= gBS
->LoadImage (
342 if (EFI_ERROR (Status
)) {
344 // The DevicePath failed, and it's not a valid
345 // removable media device.
352 if (EFI_ERROR (Status
)) {
354 // It there is any error from the Boot attempt exit now.
359 // Provide the image with it's load options
361 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
362 ASSERT_EFI_ERROR (Status
);
364 if (Option
->LoadOptionsSize
!= 0) {
365 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
366 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
369 // Before calling the image, enable the Watchdog Timer for
370 // the 5 Minute period
372 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
375 // Write boot to OS performance data for UEFI boot
378 WriteBootToOsPerformanceData ();
381 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
382 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
385 // Clear the Watchdog Timer after the image returns
387 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
391 // Clear Boot Current
395 &gEfiGlobalVariableGuid
,
396 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
406 Expand a device path that starts with a hard drive media device path node to be a
407 full device path that includes the full hardware path to the device. We need
408 to do this so it can be booted. As an optimization the front match (the part point
409 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
410 so a connect all is not required on every boot. All successful history device path
411 which point to partition node (the front part) will be saved.
413 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
414 drive media device path.
415 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
419 EFI_DEVICE_PATH_PROTOCOL
*
421 BdsExpandPartitionPartialDevicePathToFull (
422 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
426 UINTN BlockIoHandleCount
;
427 EFI_HANDLE
*BlockIoBuffer
;
428 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
429 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
430 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
433 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
434 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
435 UINTN CachedDevicePathSize
;
438 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
441 FullDevicePath
= NULL
;
443 // Check if there is prestore 'HDDP' variable.
444 // If exist, search the front path which point to partition node in the variable instants.
445 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
447 CachedDevicePath
= BdsLibGetVariableAndSize (
449 &mHdBootVariablePrivateGuid
,
450 &CachedDevicePathSize
453 if (CachedDevicePath
!= NULL
) {
454 TempNewDevicePath
= CachedDevicePath
;
459 // Check every instance of the variable
460 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
461 // partial partition boot option. Second, check whether the instance could be connected.
463 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
464 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
466 // Connect the device path instance, the device path point to hard drive media device path node
467 // e.g. ACPI() /PCI()/ATA()/Partition()
469 Status
= BdsLibConnectDevicePath (Instance
);
470 if (!EFI_ERROR (Status
)) {
476 // Come here means the first instance is not matched
480 } while (TempNewDevicePath
!= NULL
);
484 // Find the matched device path.
485 // Append the file path information from the boot option and return the fully expanded device path.
487 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
488 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
491 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
495 // First delete the matched instance.
497 TempNewDevicePath
= CachedDevicePath
;
498 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
499 FreePool (TempNewDevicePath
);
502 // Second, append the remaining path after the matched instance
504 TempNewDevicePath
= CachedDevicePath
;
505 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
506 FreePool (TempNewDevicePath
);
508 // Save the matching Device Path so we don't need to do a connect all next time
510 Status
= gRT
->SetVariable (
512 &mHdBootVariablePrivateGuid
,
513 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
514 GetDevicePathSize (CachedDevicePath
),
520 FreePool (CachedDevicePath
);
521 return FullDevicePath
;
526 // If we get here we fail to find or 'HDDP' not exist, and now we need
527 // to search all devices in the system for a matched partition
529 BdsLibConnectAllDriversToAllControllers ();
530 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
531 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
533 // If there was an error or there are no device handles that support
534 // the BLOCK_IO Protocol, then return.
539 // Loop through all the device handles that support the BLOCK_IO Protocol
541 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
543 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
544 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
548 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
550 // Find the matched partition device path
552 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
553 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
556 // Save the matched partition device path in 'HDDP' variable
558 if (CachedDevicePath
!= NULL
) {
560 // Save the matched partition device path as first instance of 'HDDP' variable
562 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
563 TempNewDevicePath
= CachedDevicePath
;
564 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
565 FreePool(TempNewDevicePath
);
567 TempNewDevicePath
= CachedDevicePath
;
568 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
569 FreePool(TempNewDevicePath
);
571 TempNewDevicePath
= CachedDevicePath
;
572 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
573 FreePool(TempNewDevicePath
);
576 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
577 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
580 ASSERT (CachedDevicePath
!= NULL
);
581 TempNewDevicePath
= CachedDevicePath
;
582 while (!IsDevicePathEnd (TempNewDevicePath
)) {
583 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
585 // Parse one instance
587 while (!IsDevicePathEndType (TempNewDevicePath
)) {
588 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
592 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
594 if (InstanceNum
>= 12) {
595 SetDevicePathEndNode (TempNewDevicePath
);
600 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
604 // Save the matching Device Path so we don't need to do a connect all next time
606 Status
= gRT
->SetVariable (
608 &mHdBootVariablePrivateGuid
,
609 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
610 GetDevicePathSize (CachedDevicePath
),
618 FreePool (CachedDevicePath
);
619 if (BlockIoBuffer
!= NULL
) {
620 FreePool (BlockIoBuffer
);
622 return FullDevicePath
;
626 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
627 instances, has the same partition node with HardDriveDevicePath device path
629 @param BlockIoDevicePath Multi device path instances which need to check
630 @param HardDriveDevicePath A device path which starts with a hard drive media
633 @retval TRUE There is a matched device path instance.
634 @retval FALSE There is no matched device path instance.
639 MatchPartitionDevicePathNode (
640 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
641 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
644 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
645 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
647 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
649 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
654 // Make PreviousDevicePath == the device path node before the end node
656 DevicePath
= BlockIoDevicePath
;
657 BlockIoHdDevicePathNode
= NULL
;
660 // find the partition device path node
662 while (!IsDevicePathEnd (DevicePath
)) {
663 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
664 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
666 BlockIoHdDevicePathNode
= DevicePath
;
670 DevicePath
= NextDevicePathNode (DevicePath
);
673 if (BlockIoHdDevicePathNode
== NULL
) {
677 // See if the harddrive device path in blockio matches the orig Hard Drive Node
679 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
683 // Check for the match
685 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
686 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
687 switch (TmpHdPath
->SignatureType
) {
688 case SIGNATURE_TYPE_GUID
:
689 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
691 case SIGNATURE_TYPE_MBR
:
692 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
704 Delete the boot option associated with the handle passed in.
706 @param Handle The handle which present the device path to create
709 @retval EFI_SUCCESS Delete the boot option success
710 @retval EFI_NOT_FOUND If the Device Path is not found in the system
711 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
712 @retval Other Error return value from SetVariable()
716 BdsLibDeleteOptionFromHandle (
721 UINT8
*BootOptionVar
;
723 UINTN BootOptionSize
;
726 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
727 UINTN DevicePathSize
;
728 UINTN OptionDevicePathSize
;
729 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
730 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
733 Status
= EFI_SUCCESS
;
738 // Check "BootOrder" variable, if no, means there is no any boot order.
740 BootOrder
= BdsLibGetVariableAndSize (
742 &gEfiGlobalVariableGuid
,
745 if (BootOrder
== NULL
) {
746 return EFI_NOT_FOUND
;
750 // Convert device handle to device path protocol instance
752 DevicePath
= DevicePathFromHandle (Handle
);
753 if (DevicePath
== NULL
) {
754 return EFI_NOT_FOUND
;
756 DevicePathSize
= GetDevicePathSize (DevicePath
);
759 // Loop all boot order variable and find the matching device path
762 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
763 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
764 BootOptionVar
= BdsLibGetVariableAndSize (
766 &gEfiGlobalVariableGuid
,
770 if (BootOptionVar
== NULL
) {
771 FreePool (BootOrder
);
772 return EFI_OUT_OF_RESOURCES
;
775 TempPtr
= BootOptionVar
;
776 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
777 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
778 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
779 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
782 // Check whether the device path match
784 if ((OptionDevicePathSize
== DevicePathSize
) &&
785 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
786 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
787 FreePool (BootOptionVar
);
791 FreePool (BootOptionVar
);
796 // Adjust number of boot option for "BootOrder" variable.
798 Status
= gRT
->SetVariable (
800 &gEfiGlobalVariableGuid
,
801 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
806 FreePool (BootOrder
);
813 Delete all invalid EFI boot options.
815 @retval EFI_SUCCESS Delete all invalid boot option success
816 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
817 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
818 @retval Other Error return value from SetVariable()
822 BdsDeleteAllInvalidEfiBootOption (
827 UINT8
*BootOptionVar
;
829 UINTN BootOptionSize
;
833 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
834 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
838 Status
= EFI_SUCCESS
;
843 // Check "BootOrder" variable firstly, this variable hold the number of boot options
845 BootOrder
= BdsLibGetVariableAndSize (
847 &gEfiGlobalVariableGuid
,
850 if (NULL
== BootOrder
) {
851 return EFI_NOT_FOUND
;
855 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
856 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
857 BootOptionVar
= BdsLibGetVariableAndSize (
859 &gEfiGlobalVariableGuid
,
862 if (NULL
== BootOptionVar
) {
863 FreePool (BootOrder
);
864 return EFI_OUT_OF_RESOURCES
;
867 TempPtr
= BootOptionVar
;
868 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
869 Description
= (CHAR16
*) TempPtr
;
870 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
871 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
874 // Skip legacy boot option (BBS boot device)
876 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
877 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
878 FreePool (BootOptionVar
);
883 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
885 // Delete this invalid boot option "Boot####"
887 Status
= gRT
->SetVariable (
889 &gEfiGlobalVariableGuid
,
890 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
895 // Mark this boot option in boot order as deleted
897 BootOrder
[Index
] = 0xffff;
900 FreePool (BootOptionVar
);
905 // Adjust boot order array
908 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
909 if (BootOrder
[Index
] != 0xffff) {
910 BootOrder
[Index2
] = BootOrder
[Index
];
914 Status
= gRT
->SetVariable (
916 &gEfiGlobalVariableGuid
,
917 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
918 Index2
* sizeof (UINT16
),
922 FreePool (BootOrder
);
929 For EFI boot option, BDS separate them as six types:
930 1. Network - The boot option points to the SimpleNetworkProtocol device.
931 Bds will try to automatically create this type boot option when enumerate.
932 2. Shell - The boot option points to internal flash shell.
933 Bds will try to automatically create this type boot option when enumerate.
934 3. Removable BlockIo - The boot option only points to the removable media
935 device, like USB flash disk, DVD, Floppy etc.
936 These device should contain a *removable* blockIo
937 protocol in their device handle.
938 Bds will try to automatically create this type boot option
940 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
942 These device should contain a *fixed* blockIo
943 protocol in their device handle.
944 BDS will skip fixed blockIo devices, and NOT
945 automatically create boot option for them. But BDS
946 will help to delete those fixed blockIo boot option,
947 whose description rule conflict with other auto-created
949 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
950 has SimpleFileSystem Protocol, but has no blockio
951 protocol. These devices do not offer blockIo
952 protocol, but BDS still can get the
953 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
955 6. File - The boot option points to a file. These boot options are usually
956 created by user manually or OS loader. BDS will not delete or modify
959 This function will enumerate all possible boot device in the system, and
960 automatically create boot options for Network, Shell, Removable BlockIo,
961 and Non-BlockIo Simplefile devices.
962 It will only execute once of every boot.
964 @param BdsBootOptionList The header of the link list which indexed all
967 @retval EFI_SUCCESS Finished all the boot device enumerate and create
968 the boot option base on that boot device
970 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
974 BdsLibEnumerateAllBootOption (
975 IN OUT LIST_ENTRY
*BdsBootOptionList
984 UINT16 NonBlockNumber
;
985 UINTN NumberBlockIoHandles
;
986 EFI_HANDLE
*BlockIoHandles
;
987 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
989 UINTN NumberNetworkHandles
;
990 EFI_HANDLE
*NetworkHandles
;
992 EFI_HANDLE
*FvHandleBuffer
;
993 EFI_FV_FILETYPE Type
;
995 EFI_FV_FILE_ATTRIBUTES Attributes
;
996 UINT32 AuthenticationStatus
;
997 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
998 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
999 UINTN DevicePathType
;
1001 EFI_HANDLE
*FileSystemHandles
;
1002 UINTN NumberFileSystemHandles
;
1004 EFI_IMAGE_DOS_HEADER DosHeader
;
1005 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1006 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 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1025 // Notes: this dirty code is to get the legacy boot option from the
1026 // BBS table and create to variable as the EFI boot option, it should
1027 // be removed after the CSM can provide legacy boot option directly
1029 REFRESH_LEGACY_BOOT_OPTIONS
;
1032 // Delete invalid boot option
1034 BdsDeleteAllInvalidEfiBootOption ();
1037 // Parse removable media
1039 gBS
->LocateHandleBuffer (
1041 &gEfiBlockIoProtocolGuid
,
1043 &NumberBlockIoHandles
,
1047 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1048 Status
= gBS
->HandleProtocol (
1049 BlockIoHandles
[Index
],
1050 &gEfiBlockIoProtocolGuid
,
1053 if (!EFI_ERROR (Status
)) {
1054 if (!BlkIo
->Media
->RemovableMedia
) {
1056 // skip the non-removable block devices
1061 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1062 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1064 switch (DevicePathType
) {
1065 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1066 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", FloppyNumber
);
1067 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1072 // Assume a removable SATA device should be the DVD/CD device
1074 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1075 case BDS_EFI_MESSAGE_SATA_BOOT
:
1076 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", CdromNumber
);
1077 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1081 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1082 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", UsbNumber
);
1083 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1087 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1088 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", ScsiNumber
);
1089 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1093 case BDS_EFI_MESSAGE_MISC_BOOT
:
1094 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", MiscNumber
);
1095 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1104 if (NumberBlockIoHandles
!= 0) {
1105 FreePool (BlockIoHandles
);
1109 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1112 gBS
->LocateHandleBuffer (
1114 &gEfiSimpleFileSystemProtocolGuid
,
1116 &NumberFileSystemHandles
,
1119 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1120 Status
= gBS
->HandleProtocol (
1121 FileSystemHandles
[Index
],
1122 &gEfiBlockIoProtocolGuid
,
1125 if (!EFI_ERROR (Status
)) {
1127 // Skip if the file system handle supports a BlkIo protocol,
1133 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1134 // machinename is ia32, ia64, x64, ...
1136 Hdr
.Union
= &HdrData
;
1138 Status
= BdsLibGetImageHeader (
1139 FileSystemHandles
[Index
],
1140 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1144 if (!EFI_ERROR (Status
) &&
1145 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1146 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1152 // No such file or the file is not a EFI application, delete this boot option
1154 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1156 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1157 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1162 if (NumberFileSystemHandles
!= 0) {
1163 FreePool (FileSystemHandles
);
1167 // Parse Network Boot Device
1169 NumberNetworkHandles
= 0;
1171 // Search MNP Service Binding protocol for UEFI network stack
1173 gBS
->LocateHandleBuffer (
1175 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1177 &NumberNetworkHandles
,
1180 if (NumberNetworkHandles
== 0) {
1182 // MNP Service Binding protocol not found, search SNP for EFI network stack
1184 gBS
->LocateHandleBuffer (
1186 &gEfiSimpleNetworkProtocolGuid
,
1188 &NumberNetworkHandles
,
1193 for (Index
= 0; Index
< NumberNetworkHandles
; Index
++) {
1194 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1195 BdsLibBuildOptionFromHandle (NetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1198 if (NumberNetworkHandles
!= 0) {
1199 FreePool (NetworkHandles
);
1203 // Check if we have on flash shell
1205 gBS
->LocateHandleBuffer (
1207 &gEfiFirmwareVolume2ProtocolGuid
,
1212 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1213 gBS
->HandleProtocol (
1214 FvHandleBuffer
[Index
],
1215 &gEfiFirmwareVolume2ProtocolGuid
,
1219 Status
= Fv
->ReadFile (
1221 PcdGetPtr(PcdShellFile
),
1226 &AuthenticationStatus
1228 if (EFI_ERROR (Status
)) {
1230 // Skip if no shell file in the FV
1235 // Build the shell boot option
1237 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1240 if (FvHandleCount
!= 0) {
1241 FreePool (FvHandleBuffer
);
1244 // Make sure every boot only have one time
1245 // boot device enumerate
1247 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1248 mEnumBootDevice
= TRUE
;
1254 Build the boot option with the handle parsed in
1256 @param Handle The handle which present the device path to create
1258 @param BdsBootOptionList The header of the link list which indexed all
1259 current boot options
1260 @param String The description of the boot option.
1265 BdsLibBuildOptionFromHandle (
1266 IN EFI_HANDLE Handle
,
1267 IN LIST_ENTRY
*BdsBootOptionList
,
1271 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1273 DevicePath
= DevicePathFromHandle (Handle
);
1276 // Create and register new boot option
1278 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1283 Build the on flash shell boot option with the handle parsed in.
1285 @param Handle The handle which present the device path to create
1286 on flash shell boot option
1287 @param BdsBootOptionList The header of the link list which indexed all
1288 current boot options
1293 BdsLibBuildOptionFromShell (
1294 IN EFI_HANDLE Handle
,
1295 IN OUT LIST_ENTRY
*BdsBootOptionList
1298 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1299 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1301 DevicePath
= DevicePathFromHandle (Handle
);
1304 // Build the shell device path
1306 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1308 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1311 // Create and register the shell boot option
1313 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1318 Boot from the UEFI spec defined "BootNext" variable.
1330 BDS_COMMON_OPTION
*BootOption
;
1331 LIST_ENTRY TempList
;
1336 // Init the boot option name buffer and temp link list
1338 InitializeListHead (&TempList
);
1339 ZeroMem (Buffer
, sizeof (Buffer
));
1341 BootNext
= BdsLibGetVariableAndSize (
1343 &gEfiGlobalVariableGuid
,
1348 // Clear the boot next variable first
1350 if (BootNext
!= NULL
) {
1353 &gEfiGlobalVariableGuid
,
1354 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1360 // Start to build the boot option and try to boot
1362 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1363 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1364 ASSERT (BootOption
!= NULL
);
1365 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1366 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1372 Return the bootable media handle.
1373 First, check the device is connected
1374 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1375 Third, detect the the default boot file in the Media, and return the removable Media handle.
1377 @param DevicePath Device Path to a bootable device
1379 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1384 BdsLibGetBootableHandle (
1385 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1389 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1390 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1392 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1394 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1397 EFI_HANDLE ReturnHandle
;
1398 EFI_HANDLE
*SimpleFileSystemHandles
;
1400 UINTN NumberSimpleFileSystemHandles
;
1402 EFI_IMAGE_DOS_HEADER DosHeader
;
1403 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1404 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1406 UpdatedDevicePath
= DevicePath
;
1409 // Check whether the device is connected
1411 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1412 if (EFI_ERROR (Status
)) {
1414 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1416 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1417 if (EFI_ERROR (Status
)) {
1419 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1421 UpdatedDevicePath
= DevicePath
;
1422 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1423 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1427 // Get BlockIo protocol and check removable attribute
1429 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1431 // Issue a dummy read to the device to check for media change.
1432 // When the removable media is changed, any Block IO read/write will
1433 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1434 // returned. After the Block IO protocol is reinstalled, subsequent
1435 // Block IO read/write will success.
1437 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1438 if (Buffer
!= NULL
) {
1439 BlockIo
->ReadBlocks (
1441 BlockIo
->Media
->MediaId
,
1443 BlockIo
->Media
->BlockSize
,
1451 // Detect the the default boot file from removable Media
1455 // 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
1456 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1458 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1459 ASSERT (DupDevicePath
!= NULL
);
1461 UpdatedDevicePath
= DupDevicePath
;
1462 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1464 // 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
1465 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1467 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1468 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1470 // Remove the usb node, let the device path only point to PCI node
1472 SetDevicePathEndNode (UpdatedDevicePath
);
1473 UpdatedDevicePath
= DupDevicePath
;
1475 UpdatedDevicePath
= DevicePath
;
1479 // Get the device path size of boot option
1481 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1482 ReturnHandle
= NULL
;
1483 gBS
->LocateHandleBuffer (
1485 &gEfiSimpleFileSystemProtocolGuid
,
1487 &NumberSimpleFileSystemHandles
,
1488 &SimpleFileSystemHandles
1490 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1492 // Get the device path size of SimpleFileSystem handle
1494 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1495 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1497 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1499 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1501 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1502 // machinename is ia32, ia64, x64, ...
1504 Hdr
.Union
= &HdrData
;
1505 Status
= BdsLibGetImageHeader (
1506 SimpleFileSystemHandles
[Index
],
1507 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1511 if (!EFI_ERROR (Status
) &&
1512 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1513 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1514 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1520 FreePool(DupDevicePath
);
1522 if (SimpleFileSystemHandles
!= NULL
) {
1523 FreePool(SimpleFileSystemHandles
);
1526 return ReturnHandle
;
1530 Check to see if the network cable is plugged in. If the DevicePath is not
1531 connected it will be connected.
1533 @param DevicePath Device Path to check
1535 @retval TRUE DevicePath points to an Network that is connected
1536 @retval FALSE DevicePath does not point to a bootable network
1540 BdsLibNetworkBootWithMediaPresent (
1541 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1545 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1547 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1548 BOOLEAN MediaPresent
;
1550 MediaPresent
= FALSE
;
1552 UpdatedDevicePath
= DevicePath
;
1554 // Locate MNP Service Binding protocol for UEFI network stack first
1556 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1557 if (EFI_ERROR (Status
)) {
1559 // MNP Service Binding protocol not found, search SNP for EFI network stack
1561 UpdatedDevicePath
= DevicePath
;
1562 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1564 if (EFI_ERROR (Status
)) {
1566 // Device not present so see if we need to connect it
1568 Status
= BdsLibConnectDevicePath (DevicePath
);
1569 if (!EFI_ERROR (Status
)) {
1571 // This one should work after we did the connect
1573 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1574 if (EFI_ERROR (Status
)) {
1575 UpdatedDevicePath
= DevicePath
;
1576 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1581 if (!EFI_ERROR (Status
)) {
1582 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1583 if (EFI_ERROR (Status
)) {
1585 // Failed to open SNP from this handle, try to get SNP from parent handle
1587 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1588 if (UpdatedDevicePath
!= NULL
) {
1589 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1590 if (!EFI_ERROR (Status
)) {
1592 // SNP handle found, get SNP from it
1594 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1599 if (!EFI_ERROR (Status
)) {
1600 if (Snp
->Mode
->MediaPresentSupported
) {
1601 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1603 // In case some one else is using the SNP check to see if it's connected
1605 MediaPresent
= Snp
->Mode
->MediaPresent
;
1608 // No one is using SNP so we need to Start and Initialize so
1609 // MediaPresent will be valid.
1611 Status
= Snp
->Start (Snp
);
1612 if (!EFI_ERROR (Status
)) {
1613 Status
= Snp
->Initialize (Snp
, 0, 0);
1614 if (!EFI_ERROR (Status
)) {
1615 MediaPresent
= Snp
->Mode
->MediaPresent
;
1616 Snp
->Shutdown (Snp
);
1622 MediaPresent
= TRUE
;
1627 return MediaPresent
;
1631 For a bootable Device path, return its boot type.
1633 @param DevicePath The bootable device Path to check
1635 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1636 which subtype is MEDIA_HARDDRIVE_DP
1637 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1638 which subtype is MEDIA_CDROM_DP
1639 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1640 which HID is floppy device.
1641 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1642 and its last device path node's subtype is MSG_ATAPI_DP.
1643 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1644 and its last device path node's subtype is MSG_SCSI_DP.
1645 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1646 and its last device path node's subtype is MSG_USB_DP.
1647 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1648 its last device path node point to a message device path node.
1649 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1650 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1655 BdsGetBootTypeFromDevicePath (
1656 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1659 ACPI_HID_DEVICE_PATH
*Acpi
;
1660 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1661 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1664 if (NULL
== DevicePath
) {
1665 return BDS_EFI_UNSUPPORT
;
1668 TempDevicePath
= DevicePath
;
1670 while (!IsDevicePathEndType (TempDevicePath
)) {
1671 switch (DevicePathType (TempDevicePath
)) {
1672 case BBS_DEVICE_PATH
:
1673 return BDS_LEGACY_BBS_BOOT
;
1674 case MEDIA_DEVICE_PATH
:
1675 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1676 return BDS_EFI_MEDIA_HD_BOOT
;
1677 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1678 return BDS_EFI_MEDIA_CDROM_BOOT
;
1681 case ACPI_DEVICE_PATH
:
1682 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1683 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1684 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1687 case MESSAGING_DEVICE_PATH
:
1689 // Get the last device path node
1691 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1692 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1694 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1697 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1700 // if the device path not only point to driver device, it is not a messaging device path,
1702 if (!IsDevicePathEndType (LastDeviceNode
)) {
1706 switch (DevicePathSubType (TempDevicePath
)) {
1708 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1712 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1716 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1720 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1723 case MSG_MAC_ADDR_DP
:
1725 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1729 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1737 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1740 return BDS_EFI_UNSUPPORT
;
1744 Check whether the Device path in a boot option point to a valid bootable device,
1745 And if CheckMedia is true, check the device is ready to boot now.
1747 @param DevPath the Device path in a boot option
1748 @param CheckMedia if true, check the device is ready to boot now.
1750 @retval TRUE the Device path is valid
1751 @retval FALSE the Device path is invalid .
1756 BdsLibIsValidEFIBootOptDevicePath (
1757 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1758 IN BOOLEAN CheckMedia
1761 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1765 Check whether the Device path in a boot option point to a valid bootable device,
1766 And if CheckMedia is true, check the device is ready to boot now.
1767 If Description is not NULL and the device path point to a fixed BlockIo
1768 device, check the description whether conflict with other auto-created
1771 @param DevPath the Device path in a boot option
1772 @param CheckMedia if true, check the device is ready to boot now.
1773 @param Description the description in a boot option
1775 @retval TRUE the Device path is valid
1776 @retval FALSE the Device path is invalid .
1781 BdsLibIsValidEFIBootOptDevicePathExt (
1782 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1783 IN BOOLEAN CheckMedia
,
1784 IN CHAR16
*Description
1789 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1790 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1791 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1792 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1794 TempDevicePath
= DevPath
;
1795 LastDeviceNode
= DevPath
;
1798 // Check if it's a valid boot option for network boot device
1799 // Check if there is MNP Service Binding Protocol or SimpleNetworkProtocol
1800 // installed. If yes, that means there is the network card there.
1802 Status
= gBS
->LocateDevicePath (
1803 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1807 if (EFI_ERROR (Status
)) {
1808 TempDevicePath
= DevPath
;
1809 Status
= gBS
->LocateDevicePath (
1810 &gEfiSimpleNetworkProtocolGuid
,
1815 if (EFI_ERROR (Status
)) {
1817 // Device not present so see if we need to connect it
1819 TempDevicePath
= DevPath
;
1820 BdsLibConnectDevicePath (TempDevicePath
);
1821 Status
= gBS
->LocateDevicePath (
1822 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1826 if (EFI_ERROR (Status
)) {
1827 TempDevicePath
= DevPath
;
1828 Status
= gBS
->LocateDevicePath (
1829 &gEfiSimpleNetworkProtocolGuid
,
1836 if (!EFI_ERROR (Status
)) {
1838 // Check whether LoadFile protocol is installed
1840 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
1841 if (!EFI_ERROR (Status
)) {
1842 if (!IsDevicePathEnd (TempDevicePath
)) {
1844 // LoadFile protocol is not installed on handle with exactly the same DevPath
1851 // Test if it is ready to boot now
1853 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1863 // If the boot option point to a file, it is a valid EFI boot option,
1864 // and assume it is ready to boot now
1866 while (!IsDevicePathEnd (TempDevicePath
)) {
1867 LastDeviceNode
= TempDevicePath
;
1868 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1870 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1871 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1876 // Check if it's a valid boot option for internal Shell
1878 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1880 // If the boot option point to Internal FV shell, make sure it is valid
1882 TempDevicePath
= DevPath
;
1883 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1884 if (Status
== EFI_ALREADY_STARTED
) {
1887 if (Status
== EFI_SUCCESS
) {
1888 FreePool (TempDevicePath
);
1895 // If the boot option point to a blockIO device:
1896 // if it is a removable blockIo device, it is valid.
1897 // if it is a fixed blockIo device, check its description confliction.
1899 TempDevicePath
= DevPath
;
1900 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1901 if (EFI_ERROR (Status
)) {
1903 // Device not present so see if we need to connect it
1905 Status
= BdsLibConnectDevicePath (DevPath
);
1906 if (!EFI_ERROR (Status
)) {
1908 // Try again to get the Block Io protocol after we did the connect
1910 TempDevicePath
= DevPath
;
1911 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1915 if (!EFI_ERROR (Status
)) {
1916 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1917 if (!EFI_ERROR (Status
)) {
1920 // Test if it is ready to boot now
1922 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1931 // 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,
1933 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1934 if (!EFI_ERROR (Status
)) {
1937 // Test if it is ready to boot now
1939 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1953 According to a file guild, check a Fv file device path is valid. If it is invalid,
1954 try to return the valid device path.
1955 FV address maybe changes for memory layout adjust from time to time, use this function
1956 could promise the Fv file device path is right.
1958 @param DevicePath on input, the Fv file device path need to check on
1959 output, the updated valid Fv file device path
1960 @param FileGuid the Fv file guild
1962 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1964 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1966 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1968 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1969 and return the updated device path in DevicePath
1974 BdsLibUpdateFvFileDevicePath (
1975 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1976 IN EFI_GUID
*FileGuid
1979 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1980 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1982 EFI_GUID
*GuidPoint
;
1984 UINTN FvHandleCount
;
1985 EFI_HANDLE
*FvHandleBuffer
;
1986 EFI_FV_FILETYPE Type
;
1988 EFI_FV_FILE_ATTRIBUTES Attributes
;
1989 UINT32 AuthenticationStatus
;
1991 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1992 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1993 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1994 EFI_HANDLE FoundFvHandle
;
1995 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1997 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1998 return EFI_INVALID_PARAMETER
;
2000 if (FileGuid
== NULL
) {
2001 return EFI_INVALID_PARAMETER
;
2005 // Check whether the device path point to the default the input Fv file
2007 TempDevicePath
= *DevicePath
;
2008 LastDeviceNode
= TempDevicePath
;
2009 while (!IsDevicePathEnd (TempDevicePath
)) {
2010 LastDeviceNode
= TempDevicePath
;
2011 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2013 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2014 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2016 if (GuidPoint
== NULL
) {
2018 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2020 return EFI_UNSUPPORTED
;
2022 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2024 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2026 return EFI_UNSUPPORTED
;
2030 // Check whether the input Fv file device path is valid
2032 TempDevicePath
= *DevicePath
;
2033 FoundFvHandle
= NULL
;
2034 Status
= gBS
->LocateDevicePath (
2035 &gEfiFirmwareVolume2ProtocolGuid
,
2039 if (!EFI_ERROR (Status
)) {
2040 Status
= gBS
->HandleProtocol (
2042 &gEfiFirmwareVolume2ProtocolGuid
,
2045 if (!EFI_ERROR (Status
)) {
2047 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2049 Status
= Fv
->ReadFile (
2056 &AuthenticationStatus
2058 if (!EFI_ERROR (Status
)) {
2059 return EFI_ALREADY_STARTED
;
2065 // Look for the input wanted FV file in current FV
2066 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2069 FoundFvHandle
= NULL
;
2070 Status
= gBS
->HandleProtocol (
2072 &gEfiLoadedImageProtocolGuid
,
2073 (VOID
**) &LoadedImage
2075 if (!EFI_ERROR (Status
)) {
2076 Status
= gBS
->HandleProtocol (
2077 LoadedImage
->DeviceHandle
,
2078 &gEfiFirmwareVolume2ProtocolGuid
,
2081 if (!EFI_ERROR (Status
)) {
2082 Status
= Fv
->ReadFile (
2089 &AuthenticationStatus
2091 if (!EFI_ERROR (Status
)) {
2093 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2098 // Second, if fail to find, try to enumerate all FV
2101 FvHandleBuffer
= NULL
;
2102 gBS
->LocateHandleBuffer (
2104 &gEfiFirmwareVolume2ProtocolGuid
,
2109 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2110 gBS
->HandleProtocol (
2111 FvHandleBuffer
[Index
],
2112 &gEfiFirmwareVolume2ProtocolGuid
,
2116 Status
= Fv
->ReadFile (
2123 &AuthenticationStatus
2125 if (EFI_ERROR (Status
)) {
2127 // Skip if input Fv file not in the FV
2132 FoundFvHandle
= FvHandleBuffer
[Index
];
2136 if (FvHandleBuffer
!= NULL
) {
2137 FreePool (FvHandleBuffer
);
2143 // Build the shell device path
2145 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2146 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2147 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2148 *DevicePath
= NewDevicePath
;
2151 return EFI_NOT_FOUND
;