2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2010, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
17 BOOLEAN mEnumBootDevice
= FALSE
;
20 /// This GUID is used for an EFI Variable that stores the front device pathes
21 /// for a partial device path that starts with the HD node.
23 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 Boot the legacy system with the boot option
30 @param Option The legacy boot option which have BBS device path
32 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
34 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
39 IN BDS_COMMON_OPTION
*Option
43 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
45 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
46 if (EFI_ERROR (Status
)) {
48 // If no LegacyBios protocol we do not support legacy boot
50 return EFI_UNSUPPORTED
;
53 // Notes: if we separate the int 19, then we don't need to refresh BBS
55 BdsRefreshBbsTableForBoot (Option
);
58 // Write boot to OS performance data for legacy boot.
61 WriteBootToOsPerformanceData ();
64 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
65 return LegacyBios
->LegacyBoot (
67 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
68 Option
->LoadOptionsSize
,
74 Internal function to check if the input boot option is a valid EFI NV Boot####.
76 @param OptionToCheck Boot option to be checked.
78 @retval TRUE This boot option matches a valid EFI NV Boot####.
83 IsBootOptionValidNVVarialbe (
84 IN BDS_COMMON_OPTION
*OptionToCheck
88 BDS_COMMON_OPTION
*BootOption
;
90 CHAR16 OptionName
[20];
94 InitializeListHead (&TempList
);
95 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
97 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
98 if (BootOption
== NULL
) {
103 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
104 // valid EFI NV Boot####.
106 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
107 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
112 FreePool (BootOption
);
117 Process the boot option follow the UEFI specification and
118 special treat the legacy boot option with BBS_DEVICE_PATH.
120 @param Option The boot option need to be processed
121 @param DevicePath The device path which describe where to load the
122 boot image or the legacy BBS device path to boot
124 @param ExitDataSize The size of exit data.
125 @param ExitData Data returned when Boot image failed.
127 @retval EFI_SUCCESS Boot from the input boot option successfully.
128 @retval EFI_NOT_FOUND If the Device Path is not found in the system
133 BdsLibBootViaBootOption (
134 IN BDS_COMMON_OPTION
*Option
,
135 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
136 OUT UINTN
*ExitDataSize
,
137 OUT CHAR16
**ExitData OPTIONAL
142 EFI_HANDLE ImageHandle
;
143 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
144 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
145 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
146 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
147 LIST_ENTRY TempBootLists
;
150 // Record the performance data for End of BDS
152 PERF_END(NULL
, "BDS", NULL
, 0);
158 // Notes: put EFI64 ROM Shadow Solution
160 EFI64_SHADOW_ALL_LEGACY_ROM ();
163 // Notes: this code can be remove after the s3 script table
164 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
165 // EVT_SIGNAL_LEGACY_BOOT
167 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
168 if (!EFI_ERROR (Status
)) {
169 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
172 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
175 WorkingDevicePath
= NULL
;
176 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
177 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
178 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
179 (HARDDRIVE_DEVICE_PATH
*)DevicePath
181 if (WorkingDevicePath
!= NULL
) {
182 DevicePath
= WorkingDevicePath
;
186 // Signal the EVT_SIGNAL_READY_TO_BOOT event
188 EfiSignalEventReadyToBoot();
194 if (IsBootOptionValidNVVarialbe (Option
)) {
196 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
197 // In this case, "BootCurrent" is not created.
198 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
202 &gEfiGlobalVariableGuid
,
203 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
209 ASSERT (Option
->DevicePath
!= NULL
);
210 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
211 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
214 // Check to see if we should legacy BOOT. If yes then do the legacy boot
216 return BdsLibDoLegacyBoot (Option
);
220 // If the boot option point to Internal FV shell, make sure it is valid
222 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
223 if (!EFI_ERROR(Status
)) {
224 if (Option
->DevicePath
!= NULL
) {
225 FreePool(Option
->DevicePath
);
227 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
228 ASSERT(Option
->DevicePath
!= NULL
);
229 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
231 // Update the shell boot option
233 InitializeListHead (&TempBootLists
);
234 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
237 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
239 FreePool (DevicePath
);
240 DevicePath
= Option
->DevicePath
;
244 UINTN DevicePathTypeValue
;
246 CHAR16
*BootStringNumber
;
249 DevicePathTypeValue
= BdsGetBootTypeFromDevicePath (Option
->DevicePath
);
252 // store number string of boot option temporary.
255 switch (DevicePathTypeValue
) {
256 case BDS_EFI_ACPI_FLOPPY_BOOT
:
257 HiiString
= L
"EFI Floppy";
259 case BDS_EFI_MEDIA_CDROM_BOOT
:
260 case BDS_EFI_MESSAGE_SATA_BOOT
:
261 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
262 HiiString
= L
"EFI DVD/CDROM";
264 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
265 HiiString
= L
"EFI USB Device";
267 case BDS_EFI_MESSAGE_SCSI_BOOT
:
268 HiiString
= L
"EFI SCSI Device";
270 case BDS_EFI_MESSAGE_MISC_BOOT
:
271 HiiString
= L
"EFI Misc Device";
273 case BDS_EFI_MESSAGE_MAC_BOOT
:
274 HiiString
= L
"EFI Network";
276 case BBS_DEVICE_PATH
:
278 // Do nothing for legacy boot option.
282 DEBUG((EFI_D_INFO
, "Can not find HiiString for given device path type 0x%x\n", DevicePathTypeValue
));
286 // If found Hii description string then cat Hii string with original description.
288 if (HiiString
!= NULL
) {
289 BootStringNumber
= Option
->Description
;
290 BufferSize
= StrSize(BootStringNumber
);
291 BufferSize
+= StrSize(HiiString
);
292 Option
->Description
= AllocateZeroPool(BufferSize
);
293 ASSERT (Option
->Description
!= NULL
);
294 StrCpy (Option
->Description
, HiiString
);
295 if (StrnCmp (BootStringNumber
, L
"0", 1) != 0) {
296 StrCat (Option
->Description
, L
" ");
297 StrCat (Option
->Description
, BootStringNumber
);
300 FreePool (BootStringNumber
);
303 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
307 Status
= gBS
->LoadImage (
317 // If we didn't find an image directly, we need to try as if it is a removable device boot option
318 // and load the image according to the default boot behavior for removable device.
320 if (EFI_ERROR (Status
)) {
322 // check if there is a bootable removable media could be found in this device path ,
323 // and get the bootable media handle
325 Handle
= BdsLibGetBootableHandle(DevicePath
);
326 if (Handle
== NULL
) {
330 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
331 // machinename is ia32, ia64, x64, ...
333 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
334 if (FilePath
!= NULL
) {
335 Status
= gBS
->LoadImage (
343 if (EFI_ERROR (Status
)) {
345 // The DevicePath failed, and it's not a valid
346 // removable media device.
353 if (EFI_ERROR (Status
)) {
355 // It there is any error from the Boot attempt exit now.
360 // Provide the image with it's load options
362 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
363 ASSERT_EFI_ERROR (Status
);
365 if (Option
->LoadOptionsSize
!= 0) {
366 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
367 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
370 // Before calling the image, enable the Watchdog Timer for
371 // the 5 Minute period
373 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
376 // Write boot to OS performance data for UEFI boot
379 WriteBootToOsPerformanceData ();
382 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
383 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
386 // Clear the Watchdog Timer after the image returns
388 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
392 // Clear Boot Current
396 &gEfiGlobalVariableGuid
,
397 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
407 Expand a device path that starts with a hard drive media device path node to be a
408 full device path that includes the full hardware path to the device. We need
409 to do this so it can be booted. As an optimization the front match (the part point
410 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
411 so a connect all is not required on every boot. All successful history device path
412 which point to partition node (the front part) will be saved.
414 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
415 drive media device path.
416 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
420 EFI_DEVICE_PATH_PROTOCOL
*
422 BdsExpandPartitionPartialDevicePathToFull (
423 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
427 UINTN BlockIoHandleCount
;
428 EFI_HANDLE
*BlockIoBuffer
;
429 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
430 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
431 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
434 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
435 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
436 UINTN CachedDevicePathSize
;
439 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
442 FullDevicePath
= NULL
;
444 // Check if there is prestore 'HDDP' variable.
445 // If exist, search the front path which point to partition node in the variable instants.
446 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
448 CachedDevicePath
= BdsLibGetVariableAndSize (
450 &mHdBootVariablePrivateGuid
,
451 &CachedDevicePathSize
454 if (CachedDevicePath
!= NULL
) {
455 TempNewDevicePath
= CachedDevicePath
;
460 // Check every instance of the variable
461 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
462 // partial partition boot option. Second, check whether the instance could be connected.
464 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
465 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
467 // Connect the device path instance, the device path point to hard drive media device path node
468 // e.g. ACPI() /PCI()/ATA()/Partition()
470 Status
= BdsLibConnectDevicePath (Instance
);
471 if (!EFI_ERROR (Status
)) {
477 // Come here means the first instance is not matched
481 } while (TempNewDevicePath
!= NULL
);
485 // Find the matched device path.
486 // Append the file path information from the boot option and return the fully expanded device path.
488 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
489 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
492 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
496 // First delete the matched instance.
498 TempNewDevicePath
= CachedDevicePath
;
499 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
500 FreePool (TempNewDevicePath
);
503 // Second, append the remaining path after the matched instance
505 TempNewDevicePath
= CachedDevicePath
;
506 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
507 FreePool (TempNewDevicePath
);
509 // Save the matching Device Path so we don't need to do a connect all next time
511 Status
= gRT
->SetVariable (
513 &mHdBootVariablePrivateGuid
,
514 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
515 GetDevicePathSize (CachedDevicePath
),
521 FreePool (CachedDevicePath
);
522 return FullDevicePath
;
527 // If we get here we fail to find or 'HDDP' not exist, and now we need
528 // to search all devices in the system for a matched partition
530 BdsLibConnectAllDriversToAllControllers ();
531 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
532 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
534 // If there was an error or there are no device handles that support
535 // the BLOCK_IO Protocol, then return.
540 // Loop through all the device handles that support the BLOCK_IO Protocol
542 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
544 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
545 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
549 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
551 // Find the matched partition device path
553 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
554 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
557 // Save the matched partition device path in 'HDDP' variable
559 if (CachedDevicePath
!= NULL
) {
561 // Save the matched partition device path as first instance of 'HDDP' variable
563 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
564 TempNewDevicePath
= CachedDevicePath
;
565 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
566 FreePool(TempNewDevicePath
);
568 TempNewDevicePath
= CachedDevicePath
;
569 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
570 if (TempNewDevicePath
!= NULL
) {
571 FreePool(TempNewDevicePath
);
574 TempNewDevicePath
= CachedDevicePath
;
575 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
576 FreePool(TempNewDevicePath
);
579 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
580 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
583 ASSERT (CachedDevicePath
!= NULL
);
584 TempNewDevicePath
= CachedDevicePath
;
585 while (!IsDevicePathEnd (TempNewDevicePath
)) {
586 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
588 // Parse one instance
590 while (!IsDevicePathEndType (TempNewDevicePath
)) {
591 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
595 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
597 if (InstanceNum
>= 12) {
598 SetDevicePathEndNode (TempNewDevicePath
);
603 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
607 // Save the matching Device Path so we don't need to do a connect all next time
609 Status
= gRT
->SetVariable (
611 &mHdBootVariablePrivateGuid
,
612 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
613 GetDevicePathSize (CachedDevicePath
),
621 if (CachedDevicePath
!= NULL
) {
622 FreePool (CachedDevicePath
);
624 if (BlockIoBuffer
!= NULL
) {
625 FreePool (BlockIoBuffer
);
627 return FullDevicePath
;
631 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
632 instances, has the same partition node with HardDriveDevicePath device path
634 @param BlockIoDevicePath Multi device path instances which need to check
635 @param HardDriveDevicePath A device path which starts with a hard drive media
638 @retval TRUE There is a matched device path instance.
639 @retval FALSE There is no matched device path instance.
644 MatchPartitionDevicePathNode (
645 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
646 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
649 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
650 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
652 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
654 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
659 // Make PreviousDevicePath == the device path node before the end node
661 DevicePath
= BlockIoDevicePath
;
662 BlockIoHdDevicePathNode
= NULL
;
665 // find the partition device path node
667 while (!IsDevicePathEnd (DevicePath
)) {
668 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
669 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
671 BlockIoHdDevicePathNode
= DevicePath
;
675 DevicePath
= NextDevicePathNode (DevicePath
);
678 if (BlockIoHdDevicePathNode
== NULL
) {
682 // See if the harddrive device path in blockio matches the orig Hard Drive Node
684 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
688 // Check for the match
690 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
691 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
692 switch (TmpHdPath
->SignatureType
) {
693 case SIGNATURE_TYPE_GUID
:
694 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
696 case SIGNATURE_TYPE_MBR
:
697 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
709 Delete the boot option associated with the handle passed in.
711 @param Handle The handle which present the device path to create
714 @retval EFI_SUCCESS Delete the boot option success
715 @retval EFI_NOT_FOUND If the Device Path is not found in the system
716 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
717 @retval Other Error return value from SetVariable()
721 BdsLibDeleteOptionFromHandle (
726 UINT8
*BootOptionVar
;
728 UINTN BootOptionSize
;
731 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
732 UINTN DevicePathSize
;
733 UINTN OptionDevicePathSize
;
734 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
735 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
738 Status
= EFI_SUCCESS
;
743 // Check "BootOrder" variable, if no, means there is no any boot order.
745 BootOrder
= BdsLibGetVariableAndSize (
747 &gEfiGlobalVariableGuid
,
750 if (BootOrder
== NULL
) {
751 return EFI_NOT_FOUND
;
755 // Convert device handle to device path protocol instance
757 DevicePath
= DevicePathFromHandle (Handle
);
758 if (DevicePath
== NULL
) {
759 return EFI_NOT_FOUND
;
761 DevicePathSize
= GetDevicePathSize (DevicePath
);
764 // Loop all boot order variable and find the matching device path
767 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
768 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
769 BootOptionVar
= BdsLibGetVariableAndSize (
771 &gEfiGlobalVariableGuid
,
775 if (BootOptionVar
== NULL
) {
776 FreePool (BootOrder
);
777 return EFI_OUT_OF_RESOURCES
;
780 TempPtr
= BootOptionVar
;
781 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
782 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
783 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
784 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
787 // Check whether the device path match
789 if ((OptionDevicePathSize
== DevicePathSize
) &&
790 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
791 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
792 FreePool (BootOptionVar
);
796 FreePool (BootOptionVar
);
801 // Adjust number of boot option for "BootOrder" variable.
803 Status
= gRT
->SetVariable (
805 &gEfiGlobalVariableGuid
,
806 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
811 FreePool (BootOrder
);
818 Delete all invalid EFI boot options.
820 @retval EFI_SUCCESS Delete all invalid boot option success
821 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
822 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
823 @retval Other Error return value from SetVariable()
827 BdsDeleteAllInvalidEfiBootOption (
832 UINT8
*BootOptionVar
;
834 UINTN BootOptionSize
;
838 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
839 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
843 Status
= EFI_SUCCESS
;
848 // Check "BootOrder" variable firstly, this variable hold the number of boot options
850 BootOrder
= BdsLibGetVariableAndSize (
852 &gEfiGlobalVariableGuid
,
855 if (NULL
== BootOrder
) {
856 return EFI_NOT_FOUND
;
860 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
861 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
862 BootOptionVar
= BdsLibGetVariableAndSize (
864 &gEfiGlobalVariableGuid
,
867 if (NULL
== BootOptionVar
) {
868 FreePool (BootOrder
);
869 return EFI_OUT_OF_RESOURCES
;
872 TempPtr
= BootOptionVar
;
873 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
874 Description
= (CHAR16
*) TempPtr
;
875 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
876 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
879 // Skip legacy boot option (BBS boot device)
881 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
882 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
883 FreePool (BootOptionVar
);
888 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
890 // Delete this invalid boot option "Boot####"
892 Status
= gRT
->SetVariable (
894 &gEfiGlobalVariableGuid
,
895 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
900 // Mark this boot option in boot order as deleted
902 BootOrder
[Index
] = 0xffff;
905 FreePool (BootOptionVar
);
910 // Adjust boot order array
913 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
914 if (BootOrder
[Index
] != 0xffff) {
915 BootOrder
[Index2
] = BootOrder
[Index
];
919 Status
= gRT
->SetVariable (
921 &gEfiGlobalVariableGuid
,
922 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
923 Index2
* sizeof (UINT16
),
927 FreePool (BootOrder
);
934 For EFI boot option, BDS separate them as six types:
935 1. Network - The boot option points to the SimpleNetworkProtocol device.
936 Bds will try to automatically create this type boot option when enumerate.
937 2. Shell - The boot option points to internal flash shell.
938 Bds will try to automatically create this type boot option when enumerate.
939 3. Removable BlockIo - The boot option only points to the removable media
940 device, like USB flash disk, DVD, Floppy etc.
941 These device should contain a *removable* blockIo
942 protocol in their device handle.
943 Bds will try to automatically create this type boot option
945 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
947 These device should contain a *fixed* blockIo
948 protocol in their device handle.
949 BDS will skip fixed blockIo devices, and NOT
950 automatically create boot option for them. But BDS
951 will help to delete those fixed blockIo boot option,
952 whose description rule conflict with other auto-created
954 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
955 has SimpleFileSystem Protocol, but has no blockio
956 protocol. These devices do not offer blockIo
957 protocol, but BDS still can get the
958 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
960 6. File - The boot option points to a file. These boot options are usually
961 created by user manually or OS loader. BDS will not delete or modify
964 This function will enumerate all possible boot device in the system, and
965 automatically create boot options for Network, Shell, Removable BlockIo,
966 and Non-BlockIo Simplefile devices.
967 It will only execute once of every boot.
969 @param BdsBootOptionList The header of the link list which indexed all
972 @retval EFI_SUCCESS Finished all the boot device enumerate and create
973 the boot option base on that boot device
975 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
979 BdsLibEnumerateAllBootOption (
980 IN OUT LIST_ENTRY
*BdsBootOptionList
989 UINT16 NonBlockNumber
;
990 UINTN NumberBlockIoHandles
;
991 EFI_HANDLE
*BlockIoHandles
;
992 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
994 UINTN NumberNetworkHandles
;
995 EFI_HANDLE
*NetworkHandles
;
997 EFI_HANDLE
*FvHandleBuffer
;
998 EFI_FV_FILETYPE Type
;
1000 EFI_FV_FILE_ATTRIBUTES Attributes
;
1001 UINT32 AuthenticationStatus
;
1002 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1003 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1004 UINTN DevicePathType
;
1006 EFI_HANDLE
*FileSystemHandles
;
1007 UINTN NumberFileSystemHandles
;
1009 EFI_IMAGE_DOS_HEADER DosHeader
;
1010 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1011 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1018 ZeroMem (Buffer
, sizeof (Buffer
));
1021 // If the boot device enumerate happened, just get the boot
1022 // device from the boot order variable
1024 if (mEnumBootDevice
) {
1025 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1030 // Notes: this dirty code is to get the legacy boot option from the
1031 // BBS table and create to variable as the EFI boot option, it should
1032 // be removed after the CSM can provide legacy boot option directly
1034 REFRESH_LEGACY_BOOT_OPTIONS
;
1037 // Delete invalid boot option
1039 BdsDeleteAllInvalidEfiBootOption ();
1042 // Parse removable media
1044 gBS
->LocateHandleBuffer (
1046 &gEfiBlockIoProtocolGuid
,
1048 &NumberBlockIoHandles
,
1052 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1053 Status
= gBS
->HandleProtocol (
1054 BlockIoHandles
[Index
],
1055 &gEfiBlockIoProtocolGuid
,
1058 if (!EFI_ERROR (Status
)) {
1059 if (!BlkIo
->Media
->RemovableMedia
) {
1061 // skip the non-removable block devices
1066 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1067 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1069 switch (DevicePathType
) {
1070 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1071 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", FloppyNumber
);
1072 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1077 // Assume a removable SATA device should be the DVD/CD device
1079 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1080 case BDS_EFI_MESSAGE_SATA_BOOT
:
1081 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", CdromNumber
);
1082 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1086 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1087 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", UsbNumber
);
1088 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1092 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1093 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", ScsiNumber
);
1094 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1098 case BDS_EFI_MESSAGE_MISC_BOOT
:
1099 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", MiscNumber
);
1100 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1109 if (NumberBlockIoHandles
!= 0) {
1110 FreePool (BlockIoHandles
);
1114 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1117 gBS
->LocateHandleBuffer (
1119 &gEfiSimpleFileSystemProtocolGuid
,
1121 &NumberFileSystemHandles
,
1124 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1125 Status
= gBS
->HandleProtocol (
1126 FileSystemHandles
[Index
],
1127 &gEfiBlockIoProtocolGuid
,
1130 if (!EFI_ERROR (Status
)) {
1132 // Skip if the file system handle supports a BlkIo protocol,
1138 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1139 // machinename is ia32, ia64, x64, ...
1141 Hdr
.Union
= &HdrData
;
1143 Status
= BdsLibGetImageHeader (
1144 FileSystemHandles
[Index
],
1145 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1149 if (!EFI_ERROR (Status
) &&
1150 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1151 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1157 // No such file or the file is not a EFI application, delete this boot option
1159 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1161 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1162 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1167 if (NumberFileSystemHandles
!= 0) {
1168 FreePool (FileSystemHandles
);
1172 // Parse Network Boot Device
1174 NumberNetworkHandles
= 0;
1176 // Search MNP Service Binding protocol for UEFI network stack
1178 gBS
->LocateHandleBuffer (
1180 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1182 &NumberNetworkHandles
,
1185 if (NumberNetworkHandles
== 0) {
1187 // MNP Service Binding protocol not found, search SNP for EFI network stack
1189 gBS
->LocateHandleBuffer (
1191 &gEfiSimpleNetworkProtocolGuid
,
1193 &NumberNetworkHandles
,
1198 for (Index
= 0; Index
< NumberNetworkHandles
; Index
++) {
1199 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1200 BdsLibBuildOptionFromHandle (NetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1203 if (NumberNetworkHandles
!= 0) {
1204 FreePool (NetworkHandles
);
1208 // Check if we have on flash shell
1210 gBS
->LocateHandleBuffer (
1212 &gEfiFirmwareVolume2ProtocolGuid
,
1217 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1218 gBS
->HandleProtocol (
1219 FvHandleBuffer
[Index
],
1220 &gEfiFirmwareVolume2ProtocolGuid
,
1224 Status
= Fv
->ReadFile (
1226 PcdGetPtr(PcdShellFile
),
1231 &AuthenticationStatus
1233 if (EFI_ERROR (Status
)) {
1235 // Skip if no shell file in the FV
1240 // Build the shell boot option
1242 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1245 if (FvHandleCount
!= 0) {
1246 FreePool (FvHandleBuffer
);
1249 // Make sure every boot only have one time
1250 // boot device enumerate
1252 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1253 mEnumBootDevice
= TRUE
;
1259 Build the boot option with the handle parsed in
1261 @param Handle The handle which present the device path to create
1263 @param BdsBootOptionList The header of the link list which indexed all
1264 current boot options
1265 @param String The description of the boot option.
1270 BdsLibBuildOptionFromHandle (
1271 IN EFI_HANDLE Handle
,
1272 IN LIST_ENTRY
*BdsBootOptionList
,
1276 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1278 DevicePath
= DevicePathFromHandle (Handle
);
1281 // Create and register new boot option
1283 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1288 Build the on flash shell boot option with the handle parsed in.
1290 @param Handle The handle which present the device path to create
1291 on flash shell boot option
1292 @param BdsBootOptionList The header of the link list which indexed all
1293 current boot options
1298 BdsLibBuildOptionFromShell (
1299 IN EFI_HANDLE Handle
,
1300 IN OUT LIST_ENTRY
*BdsBootOptionList
1303 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1304 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1306 DevicePath
= DevicePathFromHandle (Handle
);
1309 // Build the shell device path
1311 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1313 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1316 // Create and register the shell boot option
1318 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1323 Boot from the UEFI spec defined "BootNext" variable.
1335 BDS_COMMON_OPTION
*BootOption
;
1336 LIST_ENTRY TempList
;
1341 // Init the boot option name buffer and temp link list
1343 InitializeListHead (&TempList
);
1344 ZeroMem (Buffer
, sizeof (Buffer
));
1346 BootNext
= BdsLibGetVariableAndSize (
1348 &gEfiGlobalVariableGuid
,
1353 // Clear the boot next variable first
1355 if (BootNext
!= NULL
) {
1358 &gEfiGlobalVariableGuid
,
1359 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1365 // Start to build the boot option and try to boot
1367 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1368 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1369 ASSERT (BootOption
!= NULL
);
1370 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1371 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1377 Return the bootable media handle.
1378 First, check the device is connected
1379 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1380 Third, detect the the default boot file in the Media, and return the removable Media handle.
1382 @param DevicePath Device Path to a bootable device
1384 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1389 BdsLibGetBootableHandle (
1390 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1394 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1395 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1397 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1399 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1402 EFI_HANDLE ReturnHandle
;
1403 EFI_HANDLE
*SimpleFileSystemHandles
;
1405 UINTN NumberSimpleFileSystemHandles
;
1407 EFI_IMAGE_DOS_HEADER DosHeader
;
1408 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1409 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1411 UpdatedDevicePath
= DevicePath
;
1414 // Check whether the device is connected
1416 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1417 if (EFI_ERROR (Status
)) {
1419 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1421 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1422 if (EFI_ERROR (Status
)) {
1424 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1426 UpdatedDevicePath
= DevicePath
;
1427 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1428 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1432 // Get BlockIo protocol and check removable attribute
1434 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1436 // Issue a dummy read to the device to check for media change.
1437 // When the removable media is changed, any Block IO read/write will
1438 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1439 // returned. After the Block IO protocol is reinstalled, subsequent
1440 // Block IO read/write will success.
1442 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1443 if (Buffer
!= NULL
) {
1444 BlockIo
->ReadBlocks (
1446 BlockIo
->Media
->MediaId
,
1448 BlockIo
->Media
->BlockSize
,
1456 // Detect the the default boot file from removable Media
1460 // 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
1461 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1463 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1464 ASSERT (DupDevicePath
!= NULL
);
1466 UpdatedDevicePath
= DupDevicePath
;
1467 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1469 // 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
1470 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1472 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1473 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1475 // Remove the usb node, let the device path only point to PCI node
1477 SetDevicePathEndNode (UpdatedDevicePath
);
1478 UpdatedDevicePath
= DupDevicePath
;
1480 UpdatedDevicePath
= DevicePath
;
1484 // Get the device path size of boot option
1486 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1487 ReturnHandle
= NULL
;
1488 gBS
->LocateHandleBuffer (
1490 &gEfiSimpleFileSystemProtocolGuid
,
1492 &NumberSimpleFileSystemHandles
,
1493 &SimpleFileSystemHandles
1495 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1497 // Get the device path size of SimpleFileSystem handle
1499 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1500 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1502 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1504 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1506 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1507 // machinename is ia32, ia64, x64, ...
1509 Hdr
.Union
= &HdrData
;
1510 Status
= BdsLibGetImageHeader (
1511 SimpleFileSystemHandles
[Index
],
1512 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1516 if (!EFI_ERROR (Status
) &&
1517 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1518 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1519 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1525 FreePool(DupDevicePath
);
1527 if (SimpleFileSystemHandles
!= NULL
) {
1528 FreePool(SimpleFileSystemHandles
);
1531 return ReturnHandle
;
1535 Check to see if the network cable is plugged in. If the DevicePath is not
1536 connected it will be connected.
1538 @param DevicePath Device Path to check
1540 @retval TRUE DevicePath points to an Network that is connected
1541 @retval FALSE DevicePath does not point to a bootable network
1545 BdsLibNetworkBootWithMediaPresent (
1546 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1550 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1552 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1553 BOOLEAN MediaPresent
;
1555 MediaPresent
= FALSE
;
1557 UpdatedDevicePath
= DevicePath
;
1559 // Locate MNP Service Binding protocol for UEFI network stack first
1561 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1562 if (EFI_ERROR (Status
)) {
1564 // MNP Service Binding protocol not found, search SNP for EFI network stack
1566 UpdatedDevicePath
= DevicePath
;
1567 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1569 if (EFI_ERROR (Status
)) {
1571 // Device not present so see if we need to connect it
1573 Status
= BdsLibConnectDevicePath (DevicePath
);
1574 if (!EFI_ERROR (Status
)) {
1576 // This one should work after we did the connect
1578 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1579 if (EFI_ERROR (Status
)) {
1580 UpdatedDevicePath
= DevicePath
;
1581 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1586 if (!EFI_ERROR (Status
)) {
1587 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1588 if (EFI_ERROR (Status
)) {
1590 // Failed to open SNP from this handle, try to get SNP from parent handle
1592 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1593 if (UpdatedDevicePath
!= NULL
) {
1594 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1595 if (!EFI_ERROR (Status
)) {
1597 // SNP handle found, get SNP from it
1599 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1604 if (!EFI_ERROR (Status
)) {
1605 if (Snp
->Mode
->MediaPresentSupported
) {
1606 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1608 // In case some one else is using the SNP check to see if it's connected
1610 MediaPresent
= Snp
->Mode
->MediaPresent
;
1613 // No one is using SNP so we need to Start and Initialize so
1614 // MediaPresent will be valid.
1616 Status
= Snp
->Start (Snp
);
1617 if (!EFI_ERROR (Status
)) {
1618 Status
= Snp
->Initialize (Snp
, 0, 0);
1619 if (!EFI_ERROR (Status
)) {
1620 MediaPresent
= Snp
->Mode
->MediaPresent
;
1621 Snp
->Shutdown (Snp
);
1627 MediaPresent
= TRUE
;
1632 return MediaPresent
;
1636 For a bootable Device path, return its boot type.
1638 @param DevicePath The bootable device Path to check
1640 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1641 which subtype is MEDIA_HARDDRIVE_DP
1642 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1643 which subtype is MEDIA_CDROM_DP
1644 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1645 which HID is floppy device.
1646 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1647 and its last device path node's subtype is MSG_ATAPI_DP.
1648 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1649 and its last device path node's subtype is MSG_SCSI_DP.
1650 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1651 and its last device path node's subtype is MSG_USB_DP.
1652 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1653 its last device path node point to a message device path node.
1654 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1655 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1660 BdsGetBootTypeFromDevicePath (
1661 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1664 ACPI_HID_DEVICE_PATH
*Acpi
;
1665 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1666 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1669 if (NULL
== DevicePath
) {
1670 return BDS_EFI_UNSUPPORT
;
1673 TempDevicePath
= DevicePath
;
1675 while (!IsDevicePathEndType (TempDevicePath
)) {
1676 switch (DevicePathType (TempDevicePath
)) {
1677 case BBS_DEVICE_PATH
:
1678 return BDS_LEGACY_BBS_BOOT
;
1679 case MEDIA_DEVICE_PATH
:
1680 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1681 return BDS_EFI_MEDIA_HD_BOOT
;
1682 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1683 return BDS_EFI_MEDIA_CDROM_BOOT
;
1686 case ACPI_DEVICE_PATH
:
1687 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1688 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1689 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1692 case MESSAGING_DEVICE_PATH
:
1694 // Get the last device path node
1696 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1697 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1699 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1702 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1705 // if the device path not only point to driver device, it is not a messaging device path,
1707 if (!IsDevicePathEndType (LastDeviceNode
)) {
1711 switch (DevicePathSubType (TempDevicePath
)) {
1713 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1717 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1721 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1725 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1728 case MSG_MAC_ADDR_DP
:
1730 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1734 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1742 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1745 return BDS_EFI_UNSUPPORT
;
1749 Check whether the Device path in a boot option point to a valid bootable device,
1750 And if CheckMedia is true, check the device is ready to boot now.
1752 @param DevPath the Device path in a boot option
1753 @param CheckMedia if true, check the device is ready to boot now.
1755 @retval TRUE the Device path is valid
1756 @retval FALSE the Device path is invalid .
1761 BdsLibIsValidEFIBootOptDevicePath (
1762 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1763 IN BOOLEAN CheckMedia
1766 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1770 Check whether the Device path in a boot option point to a valid bootable device,
1771 And if CheckMedia is true, check the device is ready to boot now.
1772 If Description is not NULL and the device path point to a fixed BlockIo
1773 device, check the description whether conflict with other auto-created
1776 @param DevPath the Device path in a boot option
1777 @param CheckMedia if true, check the device is ready to boot now.
1778 @param Description the description in a boot option
1780 @retval TRUE the Device path is valid
1781 @retval FALSE the Device path is invalid .
1786 BdsLibIsValidEFIBootOptDevicePathExt (
1787 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1788 IN BOOLEAN CheckMedia
,
1789 IN CHAR16
*Description
1794 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1795 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1796 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1797 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1799 TempDevicePath
= DevPath
;
1800 LastDeviceNode
= DevPath
;
1803 // Check if it's a valid boot option for network boot device
1804 // Check if there is MNP Service Binding Protocol or SimpleNetworkProtocol
1805 // installed. If yes, that means there is the network card there.
1807 Status
= gBS
->LocateDevicePath (
1808 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1812 if (EFI_ERROR (Status
)) {
1813 TempDevicePath
= DevPath
;
1814 Status
= gBS
->LocateDevicePath (
1815 &gEfiSimpleNetworkProtocolGuid
,
1820 if (EFI_ERROR (Status
)) {
1822 // Device not present so see if we need to connect it
1824 TempDevicePath
= DevPath
;
1825 BdsLibConnectDevicePath (TempDevicePath
);
1826 Status
= gBS
->LocateDevicePath (
1827 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1831 if (EFI_ERROR (Status
)) {
1832 TempDevicePath
= DevPath
;
1833 Status
= gBS
->LocateDevicePath (
1834 &gEfiSimpleNetworkProtocolGuid
,
1841 if (!EFI_ERROR (Status
)) {
1843 // Check whether LoadFile protocol is installed
1845 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
1846 if (!EFI_ERROR (Status
)) {
1847 if (!IsDevicePathEnd (TempDevicePath
)) {
1849 // LoadFile protocol is not installed on handle with exactly the same DevPath
1856 // Test if it is ready to boot now
1858 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1868 // If the boot option point to a file, it is a valid EFI boot option,
1869 // and assume it is ready to boot now
1871 while (!IsDevicePathEnd (TempDevicePath
)) {
1872 LastDeviceNode
= TempDevicePath
;
1873 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1875 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1876 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1881 // Check if it's a valid boot option for internal Shell
1883 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1885 // If the boot option point to Internal FV shell, make sure it is valid
1887 TempDevicePath
= DevPath
;
1888 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1889 if (Status
== EFI_ALREADY_STARTED
) {
1892 if (Status
== EFI_SUCCESS
) {
1893 FreePool (TempDevicePath
);
1900 // If the boot option point to a blockIO device:
1901 // if it is a removable blockIo device, it is valid.
1902 // if it is a fixed blockIo device, check its description confliction.
1904 TempDevicePath
= DevPath
;
1905 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1906 if (EFI_ERROR (Status
)) {
1908 // Device not present so see if we need to connect it
1910 Status
= BdsLibConnectDevicePath (DevPath
);
1911 if (!EFI_ERROR (Status
)) {
1913 // Try again to get the Block Io protocol after we did the connect
1915 TempDevicePath
= DevPath
;
1916 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1920 if (!EFI_ERROR (Status
)) {
1921 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1922 if (!EFI_ERROR (Status
)) {
1925 // Test if it is ready to boot now
1927 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1936 // 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,
1938 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1939 if (!EFI_ERROR (Status
)) {
1942 // Test if it is ready to boot now
1944 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1958 According to a file guild, check a Fv file device path is valid. If it is invalid,
1959 try to return the valid device path.
1960 FV address maybe changes for memory layout adjust from time to time, use this function
1961 could promise the Fv file device path is right.
1963 @param DevicePath on input, the Fv file device path need to check on
1964 output, the updated valid Fv file device path
1965 @param FileGuid the Fv file guild
1967 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1969 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1971 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1973 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1974 and return the updated device path in DevicePath
1979 BdsLibUpdateFvFileDevicePath (
1980 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1981 IN EFI_GUID
*FileGuid
1984 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1985 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1987 EFI_GUID
*GuidPoint
;
1989 UINTN FvHandleCount
;
1990 EFI_HANDLE
*FvHandleBuffer
;
1991 EFI_FV_FILETYPE Type
;
1993 EFI_FV_FILE_ATTRIBUTES Attributes
;
1994 UINT32 AuthenticationStatus
;
1996 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1997 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1998 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1999 EFI_HANDLE FoundFvHandle
;
2000 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2002 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2003 return EFI_INVALID_PARAMETER
;
2005 if (FileGuid
== NULL
) {
2006 return EFI_INVALID_PARAMETER
;
2010 // Check whether the device path point to the default the input Fv file
2012 TempDevicePath
= *DevicePath
;
2013 LastDeviceNode
= TempDevicePath
;
2014 while (!IsDevicePathEnd (TempDevicePath
)) {
2015 LastDeviceNode
= TempDevicePath
;
2016 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2018 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2019 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2021 if (GuidPoint
== NULL
) {
2023 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2025 return EFI_UNSUPPORTED
;
2027 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2029 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2031 return EFI_UNSUPPORTED
;
2035 // Check whether the input Fv file device path is valid
2037 TempDevicePath
= *DevicePath
;
2038 FoundFvHandle
= NULL
;
2039 Status
= gBS
->LocateDevicePath (
2040 &gEfiFirmwareVolume2ProtocolGuid
,
2044 if (!EFI_ERROR (Status
)) {
2045 Status
= gBS
->HandleProtocol (
2047 &gEfiFirmwareVolume2ProtocolGuid
,
2050 if (!EFI_ERROR (Status
)) {
2052 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2054 Status
= Fv
->ReadFile (
2061 &AuthenticationStatus
2063 if (!EFI_ERROR (Status
)) {
2064 return EFI_ALREADY_STARTED
;
2070 // Look for the input wanted FV file in current FV
2071 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2074 FoundFvHandle
= NULL
;
2075 Status
= gBS
->HandleProtocol (
2077 &gEfiLoadedImageProtocolGuid
,
2078 (VOID
**) &LoadedImage
2080 if (!EFI_ERROR (Status
)) {
2081 Status
= gBS
->HandleProtocol (
2082 LoadedImage
->DeviceHandle
,
2083 &gEfiFirmwareVolume2ProtocolGuid
,
2086 if (!EFI_ERROR (Status
)) {
2087 Status
= Fv
->ReadFile (
2094 &AuthenticationStatus
2096 if (!EFI_ERROR (Status
)) {
2098 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2103 // Second, if fail to find, try to enumerate all FV
2106 FvHandleBuffer
= NULL
;
2107 gBS
->LocateHandleBuffer (
2109 &gEfiFirmwareVolume2ProtocolGuid
,
2114 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2115 gBS
->HandleProtocol (
2116 FvHandleBuffer
[Index
],
2117 &gEfiFirmwareVolume2ProtocolGuid
,
2121 Status
= Fv
->ReadFile (
2128 &AuthenticationStatus
2130 if (EFI_ERROR (Status
)) {
2132 // Skip if input Fv file not in the FV
2137 FoundFvHandle
= FvHandleBuffer
[Index
];
2141 if (FvHandleBuffer
!= NULL
) {
2142 FreePool (FvHandleBuffer
);
2148 // Build the shell device path
2150 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2151 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2152 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2153 *DevicePath
= NewDevicePath
;
2156 return EFI_NOT_FOUND
;