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 FreePool(TempNewDevicePath
);
572 TempNewDevicePath
= CachedDevicePath
;
573 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
574 FreePool(TempNewDevicePath
);
577 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
578 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
581 ASSERT (CachedDevicePath
!= NULL
);
582 TempNewDevicePath
= CachedDevicePath
;
583 while (!IsDevicePathEnd (TempNewDevicePath
)) {
584 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
586 // Parse one instance
588 while (!IsDevicePathEndType (TempNewDevicePath
)) {
589 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
593 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
595 if (InstanceNum
>= 12) {
596 SetDevicePathEndNode (TempNewDevicePath
);
601 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
605 // Save the matching Device Path so we don't need to do a connect all next time
607 Status
= gRT
->SetVariable (
609 &mHdBootVariablePrivateGuid
,
610 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
611 GetDevicePathSize (CachedDevicePath
),
619 FreePool (CachedDevicePath
);
620 if (BlockIoBuffer
!= NULL
) {
621 FreePool (BlockIoBuffer
);
623 return FullDevicePath
;
627 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
628 instances, has the same partition node with HardDriveDevicePath device path
630 @param BlockIoDevicePath Multi device path instances which need to check
631 @param HardDriveDevicePath A device path which starts with a hard drive media
634 @retval TRUE There is a matched device path instance.
635 @retval FALSE There is no matched device path instance.
640 MatchPartitionDevicePathNode (
641 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
642 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
645 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
646 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
648 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
650 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
655 // Make PreviousDevicePath == the device path node before the end node
657 DevicePath
= BlockIoDevicePath
;
658 BlockIoHdDevicePathNode
= NULL
;
661 // find the partition device path node
663 while (!IsDevicePathEnd (DevicePath
)) {
664 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
665 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
667 BlockIoHdDevicePathNode
= DevicePath
;
671 DevicePath
= NextDevicePathNode (DevicePath
);
674 if (BlockIoHdDevicePathNode
== NULL
) {
678 // See if the harddrive device path in blockio matches the orig Hard Drive Node
680 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
684 // Check for the match
686 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
687 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
688 switch (TmpHdPath
->SignatureType
) {
689 case SIGNATURE_TYPE_GUID
:
690 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
692 case SIGNATURE_TYPE_MBR
:
693 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
705 Delete the boot option associated with the handle passed in.
707 @param Handle The handle which present the device path to create
710 @retval EFI_SUCCESS Delete the boot option success
711 @retval EFI_NOT_FOUND If the Device Path is not found in the system
712 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
713 @retval Other Error return value from SetVariable()
717 BdsLibDeleteOptionFromHandle (
722 UINT8
*BootOptionVar
;
724 UINTN BootOptionSize
;
727 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
728 UINTN DevicePathSize
;
729 UINTN OptionDevicePathSize
;
730 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
731 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
734 Status
= EFI_SUCCESS
;
739 // Check "BootOrder" variable, if no, means there is no any boot order.
741 BootOrder
= BdsLibGetVariableAndSize (
743 &gEfiGlobalVariableGuid
,
746 if (BootOrder
== NULL
) {
747 return EFI_NOT_FOUND
;
751 // Convert device handle to device path protocol instance
753 DevicePath
= DevicePathFromHandle (Handle
);
754 if (DevicePath
== NULL
) {
755 return EFI_NOT_FOUND
;
757 DevicePathSize
= GetDevicePathSize (DevicePath
);
760 // Loop all boot order variable and find the matching device path
763 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
764 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
765 BootOptionVar
= BdsLibGetVariableAndSize (
767 &gEfiGlobalVariableGuid
,
771 if (BootOptionVar
== NULL
) {
772 FreePool (BootOrder
);
773 return EFI_OUT_OF_RESOURCES
;
776 TempPtr
= BootOptionVar
;
777 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
778 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
779 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
780 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
783 // Check whether the device path match
785 if ((OptionDevicePathSize
== DevicePathSize
) &&
786 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
787 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
788 FreePool (BootOptionVar
);
792 FreePool (BootOptionVar
);
797 // Adjust number of boot option for "BootOrder" variable.
799 Status
= gRT
->SetVariable (
801 &gEfiGlobalVariableGuid
,
802 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
807 FreePool (BootOrder
);
814 Delete all invalid EFI boot options.
816 @retval EFI_SUCCESS Delete all invalid boot option success
817 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
818 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
819 @retval Other Error return value from SetVariable()
823 BdsDeleteAllInvalidEfiBootOption (
828 UINT8
*BootOptionVar
;
830 UINTN BootOptionSize
;
834 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
835 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
839 Status
= EFI_SUCCESS
;
844 // Check "BootOrder" variable firstly, this variable hold the number of boot options
846 BootOrder
= BdsLibGetVariableAndSize (
848 &gEfiGlobalVariableGuid
,
851 if (NULL
== BootOrder
) {
852 return EFI_NOT_FOUND
;
856 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
857 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
858 BootOptionVar
= BdsLibGetVariableAndSize (
860 &gEfiGlobalVariableGuid
,
863 if (NULL
== BootOptionVar
) {
864 FreePool (BootOrder
);
865 return EFI_OUT_OF_RESOURCES
;
868 TempPtr
= BootOptionVar
;
869 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
870 Description
= (CHAR16
*) TempPtr
;
871 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
872 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
875 // Skip legacy boot option (BBS boot device)
877 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
878 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
879 FreePool (BootOptionVar
);
884 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
886 // Delete this invalid boot option "Boot####"
888 Status
= gRT
->SetVariable (
890 &gEfiGlobalVariableGuid
,
891 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
896 // Mark this boot option in boot order as deleted
898 BootOrder
[Index
] = 0xffff;
901 FreePool (BootOptionVar
);
906 // Adjust boot order array
909 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
910 if (BootOrder
[Index
] != 0xffff) {
911 BootOrder
[Index2
] = BootOrder
[Index
];
915 Status
= gRT
->SetVariable (
917 &gEfiGlobalVariableGuid
,
918 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
919 Index2
* sizeof (UINT16
),
923 FreePool (BootOrder
);
930 For EFI boot option, BDS separate them as six types:
931 1. Network - The boot option points to the SimpleNetworkProtocol device.
932 Bds will try to automatically create this type boot option when enumerate.
933 2. Shell - The boot option points to internal flash shell.
934 Bds will try to automatically create this type boot option when enumerate.
935 3. Removable BlockIo - The boot option only points to the removable media
936 device, like USB flash disk, DVD, Floppy etc.
937 These device should contain a *removable* blockIo
938 protocol in their device handle.
939 Bds will try to automatically create this type boot option
941 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
943 These device should contain a *fixed* blockIo
944 protocol in their device handle.
945 BDS will skip fixed blockIo devices, and NOT
946 automatically create boot option for them. But BDS
947 will help to delete those fixed blockIo boot option,
948 whose description rule conflict with other auto-created
950 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
951 has SimpleFileSystem Protocol, but has no blockio
952 protocol. These devices do not offer blockIo
953 protocol, but BDS still can get the
954 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
956 6. File - The boot option points to a file. These boot options are usually
957 created by user manually or OS loader. BDS will not delete or modify
960 This function will enumerate all possible boot device in the system, and
961 automatically create boot options for Network, Shell, Removable BlockIo,
962 and Non-BlockIo Simplefile devices.
963 It will only execute once of every boot.
965 @param BdsBootOptionList The header of the link list which indexed all
968 @retval EFI_SUCCESS Finished all the boot device enumerate and create
969 the boot option base on that boot device
971 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
975 BdsLibEnumerateAllBootOption (
976 IN OUT LIST_ENTRY
*BdsBootOptionList
985 UINT16 NonBlockNumber
;
986 UINTN NumberBlockIoHandles
;
987 EFI_HANDLE
*BlockIoHandles
;
988 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
990 UINTN NumberNetworkHandles
;
991 EFI_HANDLE
*NetworkHandles
;
993 EFI_HANDLE
*FvHandleBuffer
;
994 EFI_FV_FILETYPE Type
;
996 EFI_FV_FILE_ATTRIBUTES Attributes
;
997 UINT32 AuthenticationStatus
;
998 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
999 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1000 UINTN DevicePathType
;
1002 EFI_HANDLE
*FileSystemHandles
;
1003 UINTN NumberFileSystemHandles
;
1005 EFI_IMAGE_DOS_HEADER DosHeader
;
1006 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1007 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1014 ZeroMem (Buffer
, sizeof (Buffer
));
1017 // If the boot device enumerate happened, just get the boot
1018 // device from the boot order variable
1020 if (mEnumBootDevice
) {
1021 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1026 // Notes: this dirty code is to get the legacy boot option from the
1027 // BBS table and create to variable as the EFI boot option, it should
1028 // be removed after the CSM can provide legacy boot option directly
1030 REFRESH_LEGACY_BOOT_OPTIONS
;
1033 // Delete invalid boot option
1035 BdsDeleteAllInvalidEfiBootOption ();
1038 // Parse removable media
1040 gBS
->LocateHandleBuffer (
1042 &gEfiBlockIoProtocolGuid
,
1044 &NumberBlockIoHandles
,
1048 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1049 Status
= gBS
->HandleProtocol (
1050 BlockIoHandles
[Index
],
1051 &gEfiBlockIoProtocolGuid
,
1054 if (!EFI_ERROR (Status
)) {
1055 if (!BlkIo
->Media
->RemovableMedia
) {
1057 // skip the non-removable block devices
1062 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1063 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1065 switch (DevicePathType
) {
1066 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1067 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", FloppyNumber
);
1068 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1073 // Assume a removable SATA device should be the DVD/CD device
1075 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1076 case BDS_EFI_MESSAGE_SATA_BOOT
:
1077 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", CdromNumber
);
1078 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1082 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1083 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", UsbNumber
);
1084 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1088 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1089 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", ScsiNumber
);
1090 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1094 case BDS_EFI_MESSAGE_MISC_BOOT
:
1095 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", MiscNumber
);
1096 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1105 if (NumberBlockIoHandles
!= 0) {
1106 FreePool (BlockIoHandles
);
1110 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1113 gBS
->LocateHandleBuffer (
1115 &gEfiSimpleFileSystemProtocolGuid
,
1117 &NumberFileSystemHandles
,
1120 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1121 Status
= gBS
->HandleProtocol (
1122 FileSystemHandles
[Index
],
1123 &gEfiBlockIoProtocolGuid
,
1126 if (!EFI_ERROR (Status
)) {
1128 // Skip if the file system handle supports a BlkIo protocol,
1134 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1135 // machinename is ia32, ia64, x64, ...
1137 Hdr
.Union
= &HdrData
;
1139 Status
= BdsLibGetImageHeader (
1140 FileSystemHandles
[Index
],
1141 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1145 if (!EFI_ERROR (Status
) &&
1146 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1147 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1153 // No such file or the file is not a EFI application, delete this boot option
1155 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1157 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1158 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1163 if (NumberFileSystemHandles
!= 0) {
1164 FreePool (FileSystemHandles
);
1168 // Parse Network Boot Device
1170 NumberNetworkHandles
= 0;
1172 // Search MNP Service Binding protocol for UEFI network stack
1174 gBS
->LocateHandleBuffer (
1176 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1178 &NumberNetworkHandles
,
1181 if (NumberNetworkHandles
== 0) {
1183 // MNP Service Binding protocol not found, search SNP for EFI network stack
1185 gBS
->LocateHandleBuffer (
1187 &gEfiSimpleNetworkProtocolGuid
,
1189 &NumberNetworkHandles
,
1194 for (Index
= 0; Index
< NumberNetworkHandles
; Index
++) {
1195 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1196 BdsLibBuildOptionFromHandle (NetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1199 if (NumberNetworkHandles
!= 0) {
1200 FreePool (NetworkHandles
);
1204 // Check if we have on flash shell
1206 gBS
->LocateHandleBuffer (
1208 &gEfiFirmwareVolume2ProtocolGuid
,
1213 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1214 gBS
->HandleProtocol (
1215 FvHandleBuffer
[Index
],
1216 &gEfiFirmwareVolume2ProtocolGuid
,
1220 Status
= Fv
->ReadFile (
1222 PcdGetPtr(PcdShellFile
),
1227 &AuthenticationStatus
1229 if (EFI_ERROR (Status
)) {
1231 // Skip if no shell file in the FV
1236 // Build the shell boot option
1238 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1241 if (FvHandleCount
!= 0) {
1242 FreePool (FvHandleBuffer
);
1245 // Make sure every boot only have one time
1246 // boot device enumerate
1248 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1249 mEnumBootDevice
= TRUE
;
1255 Build the boot option with the handle parsed in
1257 @param Handle The handle which present the device path to create
1259 @param BdsBootOptionList The header of the link list which indexed all
1260 current boot options
1261 @param String The description of the boot option.
1266 BdsLibBuildOptionFromHandle (
1267 IN EFI_HANDLE Handle
,
1268 IN LIST_ENTRY
*BdsBootOptionList
,
1272 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1274 DevicePath
= DevicePathFromHandle (Handle
);
1277 // Create and register new boot option
1279 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1284 Build the on flash shell boot option with the handle parsed in.
1286 @param Handle The handle which present the device path to create
1287 on flash shell boot option
1288 @param BdsBootOptionList The header of the link list which indexed all
1289 current boot options
1294 BdsLibBuildOptionFromShell (
1295 IN EFI_HANDLE Handle
,
1296 IN OUT LIST_ENTRY
*BdsBootOptionList
1299 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1300 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1302 DevicePath
= DevicePathFromHandle (Handle
);
1305 // Build the shell device path
1307 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1309 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1312 // Create and register the shell boot option
1314 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1319 Boot from the UEFI spec defined "BootNext" variable.
1331 BDS_COMMON_OPTION
*BootOption
;
1332 LIST_ENTRY TempList
;
1337 // Init the boot option name buffer and temp link list
1339 InitializeListHead (&TempList
);
1340 ZeroMem (Buffer
, sizeof (Buffer
));
1342 BootNext
= BdsLibGetVariableAndSize (
1344 &gEfiGlobalVariableGuid
,
1349 // Clear the boot next variable first
1351 if (BootNext
!= NULL
) {
1354 &gEfiGlobalVariableGuid
,
1355 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1361 // Start to build the boot option and try to boot
1363 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1364 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1365 ASSERT (BootOption
!= NULL
);
1366 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1367 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1373 Return the bootable media handle.
1374 First, check the device is connected
1375 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1376 Third, detect the the default boot file in the Media, and return the removable Media handle.
1378 @param DevicePath Device Path to a bootable device
1380 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1385 BdsLibGetBootableHandle (
1386 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1390 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1391 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1393 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1395 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1398 EFI_HANDLE ReturnHandle
;
1399 EFI_HANDLE
*SimpleFileSystemHandles
;
1401 UINTN NumberSimpleFileSystemHandles
;
1403 EFI_IMAGE_DOS_HEADER DosHeader
;
1404 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1405 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1407 UpdatedDevicePath
= DevicePath
;
1410 // Check whether the device is connected
1412 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1413 if (EFI_ERROR (Status
)) {
1415 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1417 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1418 if (EFI_ERROR (Status
)) {
1420 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1422 UpdatedDevicePath
= DevicePath
;
1423 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1424 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1428 // Get BlockIo protocol and check removable attribute
1430 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1432 // Issue a dummy read to the device to check for media change.
1433 // When the removable media is changed, any Block IO read/write will
1434 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1435 // returned. After the Block IO protocol is reinstalled, subsequent
1436 // Block IO read/write will success.
1438 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1439 if (Buffer
!= NULL
) {
1440 BlockIo
->ReadBlocks (
1442 BlockIo
->Media
->MediaId
,
1444 BlockIo
->Media
->BlockSize
,
1452 // Detect the the default boot file from removable Media
1456 // 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
1457 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1459 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1460 ASSERT (DupDevicePath
!= NULL
);
1462 UpdatedDevicePath
= DupDevicePath
;
1463 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1465 // 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
1466 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1468 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1469 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1471 // Remove the usb node, let the device path only point to PCI node
1473 SetDevicePathEndNode (UpdatedDevicePath
);
1474 UpdatedDevicePath
= DupDevicePath
;
1476 UpdatedDevicePath
= DevicePath
;
1480 // Get the device path size of boot option
1482 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1483 ReturnHandle
= NULL
;
1484 gBS
->LocateHandleBuffer (
1486 &gEfiSimpleFileSystemProtocolGuid
,
1488 &NumberSimpleFileSystemHandles
,
1489 &SimpleFileSystemHandles
1491 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1493 // Get the device path size of SimpleFileSystem handle
1495 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1496 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1498 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1500 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1502 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1503 // machinename is ia32, ia64, x64, ...
1505 Hdr
.Union
= &HdrData
;
1506 Status
= BdsLibGetImageHeader (
1507 SimpleFileSystemHandles
[Index
],
1508 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1512 if (!EFI_ERROR (Status
) &&
1513 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1514 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1515 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1521 FreePool(DupDevicePath
);
1523 if (SimpleFileSystemHandles
!= NULL
) {
1524 FreePool(SimpleFileSystemHandles
);
1527 return ReturnHandle
;
1531 Check to see if the network cable is plugged in. If the DevicePath is not
1532 connected it will be connected.
1534 @param DevicePath Device Path to check
1536 @retval TRUE DevicePath points to an Network that is connected
1537 @retval FALSE DevicePath does not point to a bootable network
1541 BdsLibNetworkBootWithMediaPresent (
1542 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1546 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1548 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1549 BOOLEAN MediaPresent
;
1551 MediaPresent
= FALSE
;
1553 UpdatedDevicePath
= DevicePath
;
1555 // Locate MNP Service Binding protocol for UEFI network stack first
1557 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1558 if (EFI_ERROR (Status
)) {
1560 // MNP Service Binding protocol not found, search SNP for EFI network stack
1562 UpdatedDevicePath
= DevicePath
;
1563 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1565 if (EFI_ERROR (Status
)) {
1567 // Device not present so see if we need to connect it
1569 Status
= BdsLibConnectDevicePath (DevicePath
);
1570 if (!EFI_ERROR (Status
)) {
1572 // This one should work after we did the connect
1574 Status
= gBS
->LocateDevicePath (&gEfiManagedNetworkServiceBindingProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1575 if (EFI_ERROR (Status
)) {
1576 UpdatedDevicePath
= DevicePath
;
1577 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1582 if (!EFI_ERROR (Status
)) {
1583 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1584 if (EFI_ERROR (Status
)) {
1586 // Failed to open SNP from this handle, try to get SNP from parent handle
1588 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
1589 if (UpdatedDevicePath
!= NULL
) {
1590 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1591 if (!EFI_ERROR (Status
)) {
1593 // SNP handle found, get SNP from it
1595 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
1600 if (!EFI_ERROR (Status
)) {
1601 if (Snp
->Mode
->MediaPresentSupported
) {
1602 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1604 // In case some one else is using the SNP check to see if it's connected
1606 MediaPresent
= Snp
->Mode
->MediaPresent
;
1609 // No one is using SNP so we need to Start and Initialize so
1610 // MediaPresent will be valid.
1612 Status
= Snp
->Start (Snp
);
1613 if (!EFI_ERROR (Status
)) {
1614 Status
= Snp
->Initialize (Snp
, 0, 0);
1615 if (!EFI_ERROR (Status
)) {
1616 MediaPresent
= Snp
->Mode
->MediaPresent
;
1617 Snp
->Shutdown (Snp
);
1623 MediaPresent
= TRUE
;
1628 return MediaPresent
;
1632 For a bootable Device path, return its boot type.
1634 @param DevicePath The bootable device Path to check
1636 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1637 which subtype is MEDIA_HARDDRIVE_DP
1638 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1639 which subtype is MEDIA_CDROM_DP
1640 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1641 which HID is floppy device.
1642 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1643 and its last device path node's subtype is MSG_ATAPI_DP.
1644 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1645 and its last device path node's subtype is MSG_SCSI_DP.
1646 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1647 and its last device path node's subtype is MSG_USB_DP.
1648 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1649 its last device path node point to a message device path node.
1650 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1651 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1656 BdsGetBootTypeFromDevicePath (
1657 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1660 ACPI_HID_DEVICE_PATH
*Acpi
;
1661 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1662 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1665 if (NULL
== DevicePath
) {
1666 return BDS_EFI_UNSUPPORT
;
1669 TempDevicePath
= DevicePath
;
1671 while (!IsDevicePathEndType (TempDevicePath
)) {
1672 switch (DevicePathType (TempDevicePath
)) {
1673 case BBS_DEVICE_PATH
:
1674 return BDS_LEGACY_BBS_BOOT
;
1675 case MEDIA_DEVICE_PATH
:
1676 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1677 return BDS_EFI_MEDIA_HD_BOOT
;
1678 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1679 return BDS_EFI_MEDIA_CDROM_BOOT
;
1682 case ACPI_DEVICE_PATH
:
1683 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1684 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1685 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1688 case MESSAGING_DEVICE_PATH
:
1690 // Get the last device path node
1692 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1693 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1695 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1698 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1701 // if the device path not only point to driver device, it is not a messaging device path,
1703 if (!IsDevicePathEndType (LastDeviceNode
)) {
1707 switch (DevicePathSubType (TempDevicePath
)) {
1709 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
1713 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1717 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
1721 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
1724 case MSG_MAC_ADDR_DP
:
1726 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
1730 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
1738 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1741 return BDS_EFI_UNSUPPORT
;
1745 Check whether the Device path in a boot option point to a valid bootable device,
1746 And if CheckMedia is true, check the device is ready to boot now.
1748 @param DevPath the Device path in a boot option
1749 @param CheckMedia if true, check the device is ready to boot now.
1751 @retval TRUE the Device path is valid
1752 @retval FALSE the Device path is invalid .
1757 BdsLibIsValidEFIBootOptDevicePath (
1758 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1759 IN BOOLEAN CheckMedia
1762 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1766 Check whether the Device path in a boot option point to a valid bootable device,
1767 And if CheckMedia is true, check the device is ready to boot now.
1768 If Description is not NULL and the device path point to a fixed BlockIo
1769 device, check the description whether conflict with other auto-created
1772 @param DevPath the Device path in a boot option
1773 @param CheckMedia if true, check the device is ready to boot now.
1774 @param Description the description in a boot option
1776 @retval TRUE the Device path is valid
1777 @retval FALSE the Device path is invalid .
1782 BdsLibIsValidEFIBootOptDevicePathExt (
1783 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1784 IN BOOLEAN CheckMedia
,
1785 IN CHAR16
*Description
1790 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1791 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1792 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1793 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1795 TempDevicePath
= DevPath
;
1796 LastDeviceNode
= DevPath
;
1799 // Check if it's a valid boot option for network boot device
1800 // Check if there is MNP Service Binding Protocol or SimpleNetworkProtocol
1801 // installed. If yes, that means there is the network card there.
1803 Status
= gBS
->LocateDevicePath (
1804 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1808 if (EFI_ERROR (Status
)) {
1809 TempDevicePath
= DevPath
;
1810 Status
= gBS
->LocateDevicePath (
1811 &gEfiSimpleNetworkProtocolGuid
,
1816 if (EFI_ERROR (Status
)) {
1818 // Device not present so see if we need to connect it
1820 TempDevicePath
= DevPath
;
1821 BdsLibConnectDevicePath (TempDevicePath
);
1822 Status
= gBS
->LocateDevicePath (
1823 &gEfiManagedNetworkServiceBindingProtocolGuid
,
1827 if (EFI_ERROR (Status
)) {
1828 TempDevicePath
= DevPath
;
1829 Status
= gBS
->LocateDevicePath (
1830 &gEfiSimpleNetworkProtocolGuid
,
1837 if (!EFI_ERROR (Status
)) {
1839 // Check whether LoadFile protocol is installed
1841 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
1842 if (!EFI_ERROR (Status
)) {
1843 if (!IsDevicePathEnd (TempDevicePath
)) {
1845 // LoadFile protocol is not installed on handle with exactly the same DevPath
1852 // Test if it is ready to boot now
1854 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1864 // If the boot option point to a file, it is a valid EFI boot option,
1865 // and assume it is ready to boot now
1867 while (!IsDevicePathEnd (TempDevicePath
)) {
1868 LastDeviceNode
= TempDevicePath
;
1869 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1871 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1872 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1877 // Check if it's a valid boot option for internal Shell
1879 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1881 // If the boot option point to Internal FV shell, make sure it is valid
1883 TempDevicePath
= DevPath
;
1884 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1885 if (Status
== EFI_ALREADY_STARTED
) {
1888 if (Status
== EFI_SUCCESS
) {
1889 FreePool (TempDevicePath
);
1896 // If the boot option point to a blockIO device:
1897 // if it is a removable blockIo device, it is valid.
1898 // if it is a fixed blockIo device, check its description confliction.
1900 TempDevicePath
= DevPath
;
1901 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1902 if (EFI_ERROR (Status
)) {
1904 // Device not present so see if we need to connect it
1906 Status
= BdsLibConnectDevicePath (DevPath
);
1907 if (!EFI_ERROR (Status
)) {
1909 // Try again to get the Block Io protocol after we did the connect
1911 TempDevicePath
= DevPath
;
1912 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1916 if (!EFI_ERROR (Status
)) {
1917 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1918 if (!EFI_ERROR (Status
)) {
1921 // Test if it is ready to boot now
1923 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1932 // 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,
1934 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1935 if (!EFI_ERROR (Status
)) {
1938 // Test if it is ready to boot now
1940 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1954 According to a file guild, check a Fv file device path is valid. If it is invalid,
1955 try to return the valid device path.
1956 FV address maybe changes for memory layout adjust from time to time, use this function
1957 could promise the Fv file device path is right.
1959 @param DevicePath on input, the Fv file device path need to check on
1960 output, the updated valid Fv file device path
1961 @param FileGuid the Fv file guild
1963 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1965 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1967 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1969 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1970 and return the updated device path in DevicePath
1975 BdsLibUpdateFvFileDevicePath (
1976 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1977 IN EFI_GUID
*FileGuid
1980 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1981 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1983 EFI_GUID
*GuidPoint
;
1985 UINTN FvHandleCount
;
1986 EFI_HANDLE
*FvHandleBuffer
;
1987 EFI_FV_FILETYPE Type
;
1989 EFI_FV_FILE_ATTRIBUTES Attributes
;
1990 UINT32 AuthenticationStatus
;
1992 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1993 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1994 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1995 EFI_HANDLE FoundFvHandle
;
1996 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1998 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1999 return EFI_INVALID_PARAMETER
;
2001 if (FileGuid
== NULL
) {
2002 return EFI_INVALID_PARAMETER
;
2006 // Check whether the device path point to the default the input Fv file
2008 TempDevicePath
= *DevicePath
;
2009 LastDeviceNode
= TempDevicePath
;
2010 while (!IsDevicePathEnd (TempDevicePath
)) {
2011 LastDeviceNode
= TempDevicePath
;
2012 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2014 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2015 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2017 if (GuidPoint
== NULL
) {
2019 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2021 return EFI_UNSUPPORTED
;
2023 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2025 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2027 return EFI_UNSUPPORTED
;
2031 // Check whether the input Fv file device path is valid
2033 TempDevicePath
= *DevicePath
;
2034 FoundFvHandle
= NULL
;
2035 Status
= gBS
->LocateDevicePath (
2036 &gEfiFirmwareVolume2ProtocolGuid
,
2040 if (!EFI_ERROR (Status
)) {
2041 Status
= gBS
->HandleProtocol (
2043 &gEfiFirmwareVolume2ProtocolGuid
,
2046 if (!EFI_ERROR (Status
)) {
2048 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2050 Status
= Fv
->ReadFile (
2057 &AuthenticationStatus
2059 if (!EFI_ERROR (Status
)) {
2060 return EFI_ALREADY_STARTED
;
2066 // Look for the input wanted FV file in current FV
2067 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2070 FoundFvHandle
= NULL
;
2071 Status
= gBS
->HandleProtocol (
2073 &gEfiLoadedImageProtocolGuid
,
2074 (VOID
**) &LoadedImage
2076 if (!EFI_ERROR (Status
)) {
2077 Status
= gBS
->HandleProtocol (
2078 LoadedImage
->DeviceHandle
,
2079 &gEfiFirmwareVolume2ProtocolGuid
,
2082 if (!EFI_ERROR (Status
)) {
2083 Status
= Fv
->ReadFile (
2090 &AuthenticationStatus
2092 if (!EFI_ERROR (Status
)) {
2094 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2099 // Second, if fail to find, try to enumerate all FV
2102 FvHandleBuffer
= NULL
;
2103 gBS
->LocateHandleBuffer (
2105 &gEfiFirmwareVolume2ProtocolGuid
,
2110 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2111 gBS
->HandleProtocol (
2112 FvHandleBuffer
[Index
],
2113 &gEfiFirmwareVolume2ProtocolGuid
,
2117 Status
= Fv
->ReadFile (
2124 &AuthenticationStatus
2126 if (EFI_ERROR (Status
)) {
2128 // Skip if input Fv file not in the FV
2133 FoundFvHandle
= FvHandleBuffer
[Index
];
2137 if (FvHandleBuffer
!= NULL
) {
2138 FreePool (FvHandleBuffer
);
2144 // Build the shell device path
2146 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2147 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2148 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2149 *DevicePath
= NewDevicePath
;
2152 return EFI_NOT_FOUND
;