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 NumberSimpleNetworkHandles
;
990 EFI_HANDLE
*SimpleNetworkHandles
;
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 gBS
->LocateHandleBuffer (
1171 &gEfiSimpleNetworkProtocolGuid
,
1173 &NumberSimpleNetworkHandles
,
1174 &SimpleNetworkHandles
1177 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1178 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1179 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1182 if (NumberSimpleNetworkHandles
!= 0) {
1183 FreePool (SimpleNetworkHandles
);
1187 // Check if we have on flash shell
1189 gBS
->LocateHandleBuffer (
1191 &gEfiFirmwareVolume2ProtocolGuid
,
1196 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1197 gBS
->HandleProtocol (
1198 FvHandleBuffer
[Index
],
1199 &gEfiFirmwareVolume2ProtocolGuid
,
1203 Status
= Fv
->ReadFile (
1205 PcdGetPtr(PcdShellFile
),
1210 &AuthenticationStatus
1212 if (EFI_ERROR (Status
)) {
1214 // Skip if no shell file in the FV
1219 // Build the shell boot option
1221 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1224 if (FvHandleCount
!= 0) {
1225 FreePool (FvHandleBuffer
);
1228 // Make sure every boot only have one time
1229 // boot device enumerate
1231 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1232 mEnumBootDevice
= TRUE
;
1238 Build the boot option with the handle parsed in
1240 @param Handle The handle which present the device path to create
1242 @param BdsBootOptionList The header of the link list which indexed all
1243 current boot options
1244 @param String The description of the boot option.
1249 BdsLibBuildOptionFromHandle (
1250 IN EFI_HANDLE Handle
,
1251 IN LIST_ENTRY
*BdsBootOptionList
,
1255 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1257 DevicePath
= DevicePathFromHandle (Handle
);
1260 // Create and register new boot option
1262 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1267 Build the on flash shell boot option with the handle parsed in.
1269 @param Handle The handle which present the device path to create
1270 on flash shell boot option
1271 @param BdsBootOptionList The header of the link list which indexed all
1272 current boot options
1277 BdsLibBuildOptionFromShell (
1278 IN EFI_HANDLE Handle
,
1279 IN OUT LIST_ENTRY
*BdsBootOptionList
1282 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1283 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1285 DevicePath
= DevicePathFromHandle (Handle
);
1288 // Build the shell device path
1290 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1292 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1295 // Create and register the shell boot option
1297 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1302 Boot from the UEFI spec defined "BootNext" variable.
1314 BDS_COMMON_OPTION
*BootOption
;
1315 LIST_ENTRY TempList
;
1320 // Init the boot option name buffer and temp link list
1322 InitializeListHead (&TempList
);
1323 ZeroMem (Buffer
, sizeof (Buffer
));
1325 BootNext
= BdsLibGetVariableAndSize (
1327 &gEfiGlobalVariableGuid
,
1332 // Clear the boot next variable first
1334 if (BootNext
!= NULL
) {
1337 &gEfiGlobalVariableGuid
,
1338 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1344 // Start to build the boot option and try to boot
1346 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1347 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1348 ASSERT (BootOption
!= NULL
);
1349 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1350 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1356 Return the bootable media handle.
1357 First, check the device is connected
1358 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1359 Third, detect the the default boot file in the Media, and return the removable Media handle.
1361 @param DevicePath Device Path to a bootable device
1363 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1368 BdsLibGetBootableHandle (
1369 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1373 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1374 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1376 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1378 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1381 EFI_HANDLE ReturnHandle
;
1382 EFI_HANDLE
*SimpleFileSystemHandles
;
1384 UINTN NumberSimpleFileSystemHandles
;
1386 EFI_IMAGE_DOS_HEADER DosHeader
;
1387 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1388 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1390 UpdatedDevicePath
= DevicePath
;
1393 // Check whether the device is connected
1395 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1396 if (EFI_ERROR (Status
)) {
1398 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1400 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1401 if (EFI_ERROR (Status
)) {
1403 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1405 UpdatedDevicePath
= DevicePath
;
1406 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1407 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1411 // Get BlockIo protocol and check removable attribute
1413 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1415 // Issue a dummy read to the device to check for media change.
1416 // When the removable media is changed, any Block IO read/write will
1417 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1418 // returned. After the Block IO protocol is reinstalled, subsequent
1419 // Block IO read/write will success.
1421 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1422 if (Buffer
!= NULL
) {
1423 BlockIo
->ReadBlocks (
1425 BlockIo
->Media
->MediaId
,
1427 BlockIo
->Media
->BlockSize
,
1435 // Detect the the default boot file from removable Media
1439 // 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
1440 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1442 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1443 ASSERT (DupDevicePath
!= NULL
);
1445 UpdatedDevicePath
= DupDevicePath
;
1446 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1448 // 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
1449 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1451 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1452 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1454 // Remove the usb node, let the device path only point to PCI node
1456 SetDevicePathEndNode (UpdatedDevicePath
);
1457 UpdatedDevicePath
= DupDevicePath
;
1459 UpdatedDevicePath
= DevicePath
;
1463 // Get the device path size of boot option
1465 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1466 ReturnHandle
= NULL
;
1467 gBS
->LocateHandleBuffer (
1469 &gEfiSimpleFileSystemProtocolGuid
,
1471 &NumberSimpleFileSystemHandles
,
1472 &SimpleFileSystemHandles
1474 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1476 // Get the device path size of SimpleFileSystem handle
1478 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1479 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1481 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1483 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1485 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1486 // machinename is ia32, ia64, x64, ...
1488 Hdr
.Union
= &HdrData
;
1489 Status
= BdsLibGetImageHeader (
1490 SimpleFileSystemHandles
[Index
],
1491 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1495 if (!EFI_ERROR (Status
) &&
1496 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1497 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1498 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1504 FreePool(DupDevicePath
);
1506 if (SimpleFileSystemHandles
!= NULL
) {
1507 FreePool(SimpleFileSystemHandles
);
1510 return ReturnHandle
;
1514 Check to see if the network cable is plugged in. If the DevicePath is not
1515 connected it will be connected.
1517 @param DevicePath Device Path to check
1519 @retval TRUE DevicePath points to an Network that is connected
1520 @retval FALSE DevicePath does not point to a bootable network
1524 BdsLibNetworkBootWithMediaPresent (
1525 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1529 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1531 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1532 BOOLEAN MediaPresent
;
1534 MediaPresent
= FALSE
;
1536 UpdatedDevicePath
= DevicePath
;
1537 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1538 if (EFI_ERROR (Status
)) {
1540 // Device not present so see if we need to connect it
1542 Status
= BdsLibConnectDevicePath (DevicePath
);
1543 if (!EFI_ERROR (Status
)) {
1545 // This one should work after we did the connect
1547 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1551 if (!EFI_ERROR (Status
)) {
1552 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1553 if (!EFI_ERROR (Status
)) {
1554 if (Snp
->Mode
->MediaPresentSupported
) {
1555 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1557 // In case some one else is using the SNP check to see if it's connected
1559 MediaPresent
= Snp
->Mode
->MediaPresent
;
1562 // No one is using SNP so we need to Start and Initialize so
1563 // MediaPresent will be valid.
1565 Status
= Snp
->Start (Snp
);
1566 if (!EFI_ERROR (Status
)) {
1567 Status
= Snp
->Initialize (Snp
, 0, 0);
1568 if (!EFI_ERROR (Status
)) {
1569 MediaPresent
= Snp
->Mode
->MediaPresent
;
1570 Snp
->Shutdown (Snp
);
1576 MediaPresent
= TRUE
;
1581 return MediaPresent
;
1585 For a bootable Device path, return its boot type.
1587 @param DevicePath The bootable device Path to check
1589 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1590 which subtype is MEDIA_HARDDRIVE_DP
1591 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1592 which subtype is MEDIA_CDROM_DP
1593 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1594 which HID is floppy device.
1595 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1596 and its last device path node's subtype is MSG_ATAPI_DP.
1597 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1598 and its last device path node's subtype is MSG_SCSI_DP.
1599 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1600 and its last device path node's subtype is MSG_USB_DP.
1601 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1602 its last device path node point to a message device path node.
1603 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1604 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1609 BdsGetBootTypeFromDevicePath (
1610 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1613 ACPI_HID_DEVICE_PATH
*Acpi
;
1614 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1615 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1618 if (NULL
== DevicePath
) {
1619 return BDS_EFI_UNSUPPORT
;
1622 TempDevicePath
= DevicePath
;
1624 while (!IsDevicePathEndType (TempDevicePath
)) {
1625 switch (DevicePathType (TempDevicePath
)) {
1626 case BBS_DEVICE_PATH
:
1627 return BDS_LEGACY_BBS_BOOT
;
1628 case MEDIA_DEVICE_PATH
:
1629 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1630 return BDS_EFI_MEDIA_HD_BOOT
;
1631 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1632 return BDS_EFI_MEDIA_CDROM_BOOT
;
1635 case ACPI_DEVICE_PATH
:
1636 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1637 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1638 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1641 case MESSAGING_DEVICE_PATH
:
1643 // Get the last device path node
1645 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1646 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1648 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1651 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1654 // if the device path not only point to driver device, it is not a messaging device path,
1656 if (!IsDevicePathEndType (LastDeviceNode
)) {
1660 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1661 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1662 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1663 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1664 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1665 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1666 } else if (DevicePathSubType(TempDevicePath
) == MSG_SATA_DP
) {
1667 return BDS_EFI_MESSAGE_SATA_BOOT
;
1668 } else if (DevicePathSubType(TempDevicePath
) == MSG_MAC_ADDR_DP
) {
1669 return BDS_EFI_MESSAGE_MAC_BOOT
;
1671 return BDS_EFI_MESSAGE_MISC_BOOT
;
1675 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1678 return BDS_EFI_UNSUPPORT
;
1682 Check whether the Device path in a boot option point to a valid bootable device,
1683 And if CheckMedia is true, check the device is ready to boot now.
1685 @param DevPath the Device path in a boot option
1686 @param CheckMedia if true, check the device is ready to boot now.
1688 @retval TRUE the Device path is valid
1689 @retval FALSE the Device path is invalid .
1694 BdsLibIsValidEFIBootOptDevicePath (
1695 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1696 IN BOOLEAN CheckMedia
1699 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1703 Check whether the Device path in a boot option point to a valid bootable device,
1704 And if CheckMedia is true, check the device is ready to boot now.
1705 If Description is not NULL and the device path point to a fixed BlockIo
1706 device, check the description whether conflict with other auto-created
1709 @param DevPath the Device path in a boot option
1710 @param CheckMedia if true, check the device is ready to boot now.
1711 @param Description the description in a boot option
1713 @retval TRUE the Device path is valid
1714 @retval FALSE the Device path is invalid .
1719 BdsLibIsValidEFIBootOptDevicePathExt (
1720 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1721 IN BOOLEAN CheckMedia
,
1722 IN CHAR16
*Description
1727 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1728 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1729 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1731 TempDevicePath
= DevPath
;
1732 LastDeviceNode
= DevPath
;
1735 // Check if it's a valid boot option for network boot device
1736 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1737 // there is the network card there.
1739 Status
= gBS
->LocateDevicePath (
1740 &gEfiSimpleNetworkProtocolGuid
,
1744 if (EFI_ERROR (Status
)) {
1746 // Device not present so see if we need to connect it
1748 TempDevicePath
= DevPath
;
1749 BdsLibConnectDevicePath (TempDevicePath
);
1750 Status
= gBS
->LocateDevicePath (
1751 &gEfiSimpleNetworkProtocolGuid
,
1757 if (!EFI_ERROR (Status
)) {
1760 // Test if it is ready to boot now
1762 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1771 // If the boot option point to a file, it is a valid EFI boot option,
1772 // and assume it is ready to boot now
1774 while (!IsDevicePathEnd (TempDevicePath
)) {
1775 LastDeviceNode
= TempDevicePath
;
1776 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1778 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1779 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1784 // Check if it's a valid boot option for internal Shell
1786 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1788 // If the boot option point to Internal FV shell, make sure it is valid
1790 TempDevicePath
= DevPath
;
1791 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1792 if (Status
== EFI_ALREADY_STARTED
) {
1795 if (Status
== EFI_SUCCESS
) {
1796 FreePool (TempDevicePath
);
1803 // If the boot option point to a blockIO device:
1804 // if it is a removable blockIo device, it is valid.
1805 // if it is a fixed blockIo device, check its description confliction.
1807 TempDevicePath
= DevPath
;
1808 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1809 if (EFI_ERROR (Status
)) {
1811 // Device not present so see if we need to connect it
1813 Status
= BdsLibConnectDevicePath (DevPath
);
1814 if (!EFI_ERROR (Status
)) {
1816 // Try again to get the Block Io protocol after we did the connect
1818 TempDevicePath
= DevPath
;
1819 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1823 if (!EFI_ERROR (Status
)) {
1824 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1825 if (!EFI_ERROR (Status
)) {
1828 // Test if it is ready to boot now
1830 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1839 // 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,
1841 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1842 if (!EFI_ERROR (Status
)) {
1845 // Test if it is ready to boot now
1847 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1861 According to a file guild, check a Fv file device path is valid. If it is invalid,
1862 try to return the valid device path.
1863 FV address maybe changes for memory layout adjust from time to time, use this function
1864 could promise the Fv file device path is right.
1866 @param DevicePath on input, the Fv file device path need to check on
1867 output, the updated valid Fv file device path
1868 @param FileGuid the Fv file guild
1870 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1872 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1874 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1876 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1877 and return the updated device path in DevicePath
1882 BdsLibUpdateFvFileDevicePath (
1883 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1884 IN EFI_GUID
*FileGuid
1887 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1888 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1890 EFI_GUID
*GuidPoint
;
1892 UINTN FvHandleCount
;
1893 EFI_HANDLE
*FvHandleBuffer
;
1894 EFI_FV_FILETYPE Type
;
1896 EFI_FV_FILE_ATTRIBUTES Attributes
;
1897 UINT32 AuthenticationStatus
;
1899 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1900 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1901 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1902 EFI_HANDLE FoundFvHandle
;
1903 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1905 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1906 return EFI_INVALID_PARAMETER
;
1908 if (FileGuid
== NULL
) {
1909 return EFI_INVALID_PARAMETER
;
1913 // Check whether the device path point to the default the input Fv file
1915 TempDevicePath
= *DevicePath
;
1916 LastDeviceNode
= TempDevicePath
;
1917 while (!IsDevicePathEnd (TempDevicePath
)) {
1918 LastDeviceNode
= TempDevicePath
;
1919 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1921 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1922 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1924 if (GuidPoint
== NULL
) {
1926 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1928 return EFI_UNSUPPORTED
;
1930 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1932 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1934 return EFI_UNSUPPORTED
;
1938 // Check whether the input Fv file device path is valid
1940 TempDevicePath
= *DevicePath
;
1941 FoundFvHandle
= NULL
;
1942 Status
= gBS
->LocateDevicePath (
1943 &gEfiFirmwareVolume2ProtocolGuid
,
1947 if (!EFI_ERROR (Status
)) {
1948 Status
= gBS
->HandleProtocol (
1950 &gEfiFirmwareVolume2ProtocolGuid
,
1953 if (!EFI_ERROR (Status
)) {
1955 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1957 Status
= Fv
->ReadFile (
1964 &AuthenticationStatus
1966 if (!EFI_ERROR (Status
)) {
1967 return EFI_ALREADY_STARTED
;
1973 // Look for the input wanted FV file in current FV
1974 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1977 FoundFvHandle
= NULL
;
1978 Status
= gBS
->HandleProtocol (
1980 &gEfiLoadedImageProtocolGuid
,
1981 (VOID
**) &LoadedImage
1983 if (!EFI_ERROR (Status
)) {
1984 Status
= gBS
->HandleProtocol (
1985 LoadedImage
->DeviceHandle
,
1986 &gEfiFirmwareVolume2ProtocolGuid
,
1989 if (!EFI_ERROR (Status
)) {
1990 Status
= Fv
->ReadFile (
1997 &AuthenticationStatus
1999 if (!EFI_ERROR (Status
)) {
2001 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2006 // Second, if fail to find, try to enumerate all FV
2009 FvHandleBuffer
= NULL
;
2010 gBS
->LocateHandleBuffer (
2012 &gEfiFirmwareVolume2ProtocolGuid
,
2017 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2018 gBS
->HandleProtocol (
2019 FvHandleBuffer
[Index
],
2020 &gEfiFirmwareVolume2ProtocolGuid
,
2024 Status
= Fv
->ReadFile (
2031 &AuthenticationStatus
2033 if (EFI_ERROR (Status
)) {
2035 // Skip if input Fv file not in the FV
2040 FoundFvHandle
= FvHandleBuffer
[Index
];
2044 if (FvHandleBuffer
!= NULL
) {
2045 FreePool (FvHandleBuffer
);
2051 // Build the shell device path
2053 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2054 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2055 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2056 *DevicePath
= NewDevicePath
;
2059 return EFI_NOT_FOUND
;