2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2008, 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 to a file
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####.
84 IsBootOptionValidNVVarialbe (
85 IN BDS_COMMON_OPTION
*OptionToCheck
89 BDS_COMMON_OPTION
*BootOption
;
91 CHAR16 OptionName
[20];
95 InitializeListHead (&TempList
);
96 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
98 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
99 if (BootOption
== NULL
) {
104 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
105 // valid EFI NV Boot####.
107 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
108 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
113 FreePool (BootOption
);
118 Process the boot option follow the UEFI specification and
119 special treat the legacy boot option with BBS_DEVICE_PATH.
121 @param Option The boot option need to be processed
122 @param DevicePath The device path which describe where to load the
123 boot image or the legacy BBS device path to boot
125 @param ExitDataSize The size of exit data.
126 @param ExitData Data returned when Boot image failed.
128 @retval EFI_SUCCESS Boot from the input boot option successfully.
129 @retval EFI_NOT_FOUND If the Device Path is not found in the system
134 BdsLibBootViaBootOption (
135 IN BDS_COMMON_OPTION
*Option
,
136 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
137 OUT UINTN
*ExitDataSize
,
138 OUT CHAR16
**ExitData OPTIONAL
143 EFI_HANDLE ImageHandle
;
144 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
145 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
146 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
147 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
148 LIST_ENTRY TempBootLists
;
151 // Record the performance data for End of BDS
153 PERF_END (0, "BDS", NULL
, 0);
159 // Notes: put EFI64 ROM Shadow Solution
161 EFI64_SHADOW_ALL_LEGACY_ROM ();
164 // Notes: this code can be remove after the s3 script table
165 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
166 // EVT_SIGNAL_LEGACY_BOOT
168 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
169 if (!EFI_ERROR (Status
)) {
170 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
173 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
176 WorkingDevicePath
= NULL
;
177 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
178 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
179 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
180 (HARDDRIVE_DEVICE_PATH
*)DevicePath
182 if (WorkingDevicePath
!= NULL
) {
183 DevicePath
= WorkingDevicePath
;
187 // Signal the EVT_SIGNAL_READY_TO_BOOT event
189 EfiSignalEventReadyToBoot();
195 if (IsBootOptionValidNVVarialbe (Option
)) {
197 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
198 // In this case, "BootCurrent" is not created.
199 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
203 &gEfiGlobalVariableGuid
,
204 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
210 ASSERT (Option
->DevicePath
!= NULL
);
211 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
212 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
215 // Check to see if we should legacy BOOT. If yes then do the legacy boot
217 return BdsLibDoLegacyBoot (Option
);
221 // If the boot option point to Internal FV shell, make sure it is valid
223 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
224 if (!EFI_ERROR(Status
)) {
225 if (Option
->DevicePath
!= NULL
) {
226 FreePool(Option
->DevicePath
);
228 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
229 ASSERT(Option
->DevicePath
!= NULL
);
230 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
232 // Update the shell boot option
234 InitializeListHead (&TempBootLists
);
235 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
238 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
240 FreePool (DevicePath
);
241 DevicePath
= Option
->DevicePath
;
244 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
246 Status
= gBS
->LoadImage (
256 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
257 // and load the image according to the default boot behavior for removable device.
259 if (EFI_ERROR (Status
)) {
261 // check if there is a bootable removable media could be found in this device path ,
262 // and get the bootable media handle
264 Handle
= BdsLibGetBootableHandle(DevicePath
);
265 if (Handle
== NULL
) {
269 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
270 // machinename is ia32, ia64, x64, ...
272 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
273 if (FilePath
!= NULL
) {
274 Status
= gBS
->LoadImage (
282 if (EFI_ERROR (Status
)) {
284 // The DevicePath failed, and it's not a valid
285 // removable media device.
292 if (EFI_ERROR (Status
)) {
294 // It there is any error from the Boot attempt exit now.
299 // Provide the image with it's load options
301 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
302 ASSERT_EFI_ERROR (Status
);
304 if (Option
->LoadOptionsSize
!= 0) {
305 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
306 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
309 // Before calling the image, enable the Watchdog Timer for
310 // the 5 Minute period
312 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
314 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
315 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
318 // Clear the Watchdog Timer after the image returns
320 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
324 // Clear Boot Current
328 &gEfiGlobalVariableGuid
,
329 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
339 Expand a device path that starts with a hard drive media device path node to be a
340 full device path that includes the full hardware path to the device. We need
341 to do this so it can be booted. As an optimization the front match (the part point
342 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
343 so a connect all is not required on every boot. All successful history device path
344 which point to partition node (the front part) will be saved.
346 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
347 drive media device path.
348 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
352 EFI_DEVICE_PATH_PROTOCOL
*
354 BdsExpandPartitionPartialDevicePathToFull (
355 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
359 UINTN BlockIoHandleCount
;
360 EFI_HANDLE
*BlockIoBuffer
;
361 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
362 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
363 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
366 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
367 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
368 UINTN CachedDevicePathSize
;
371 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
374 FullDevicePath
= NULL
;
376 // Check if there is prestore 'HDDP' variable.
377 // If exist, search the front path which point to partition node in the variable instants.
378 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
380 CachedDevicePath
= BdsLibGetVariableAndSize (
382 &mHdBootVariablePrivateGuid
,
383 &CachedDevicePathSize
386 if (CachedDevicePath
!= NULL
) {
387 TempNewDevicePath
= CachedDevicePath
;
392 // Check every instance of the variable
393 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
394 // partial partition boot option. Second, check whether the instance could be connected.
396 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
397 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
399 // Connect the device path instance, the device path point to hard drive media device path node
400 // e.g. ACPI() /PCI()/ATA()/Partition()
402 Status
= BdsLibConnectDevicePath (Instance
);
403 if (!EFI_ERROR (Status
)) {
409 // Come here means the first instance is not matched
413 } while (TempNewDevicePath
!= NULL
);
417 // Find the matched device path.
418 // Append the file path information from the boot option and return the fully expanded device path.
420 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
421 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
424 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
428 // First delete the matched instance.
430 TempNewDevicePath
= CachedDevicePath
;
431 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
432 FreePool (TempNewDevicePath
);
435 // Second, append the remaining path after the matched instance
437 TempNewDevicePath
= CachedDevicePath
;
438 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
439 FreePool (TempNewDevicePath
);
441 // Save the matching Device Path so we don't need to do a connect all next time
443 Status
= gRT
->SetVariable (
445 &mHdBootVariablePrivateGuid
,
446 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
447 GetDevicePathSize (CachedDevicePath
),
453 FreePool (CachedDevicePath
);
454 return FullDevicePath
;
459 // If we get here we fail to find or 'HDDP' not exist, and now we need
460 // to search all devices in the system for a matched partition
462 BdsLibConnectAllDriversToAllControllers ();
463 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
464 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
466 // If there was an error or there are no device handles that support
467 // the BLOCK_IO Protocol, then return.
472 // Loop through all the device handles that support the BLOCK_IO Protocol
474 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
476 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
477 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
481 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
483 // Find the matched partition device path
485 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
486 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
489 // Save the matched partition device path in 'HDDP' variable
491 if (CachedDevicePath
!= NULL
) {
493 // Save the matched partition device path as first instance of 'HDDP' variable
495 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
496 TempNewDevicePath
= CachedDevicePath
;
497 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
498 FreePool(TempNewDevicePath
);
500 TempNewDevicePath
= CachedDevicePath
;
501 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
502 FreePool(TempNewDevicePath
);
504 TempNewDevicePath
= CachedDevicePath
;
505 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
506 FreePool(TempNewDevicePath
);
509 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
510 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
513 ASSERT (CachedDevicePath
!= NULL
);
514 TempNewDevicePath
= CachedDevicePath
;
515 while (!IsDevicePathEnd (TempNewDevicePath
)) {
516 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
518 // Parse one instance
520 while (!IsDevicePathEndType (TempNewDevicePath
)) {
521 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
525 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
527 if (InstanceNum
>= 12) {
528 SetDevicePathEndNode (TempNewDevicePath
);
533 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
537 // Save the matching Device Path so we don't need to do a connect all next time
539 Status
= gRT
->SetVariable (
541 &mHdBootVariablePrivateGuid
,
542 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
543 GetDevicePathSize (CachedDevicePath
),
551 FreePool (CachedDevicePath
);
552 if (BlockIoBuffer
!= NULL
) {
553 FreePool (BlockIoBuffer
);
555 return FullDevicePath
;
559 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
560 instances, has the same partition node with HardDriveDevicePath device path
562 @param BlockIoDevicePath Multi device path instances which need to check
563 @param HardDriveDevicePath A device path which starts with a hard drive media
566 @retval TRUE There is a matched device path instance.
567 @retval FALSE There is no matched device path instance.
572 MatchPartitionDevicePathNode (
573 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
574 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
577 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
578 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
580 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
582 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
587 // Make PreviousDevicePath == the device path node before the end node
589 DevicePath
= BlockIoDevicePath
;
590 BlockIoHdDevicePathNode
= NULL
;
593 // find the partition device path node
595 while (!IsDevicePathEnd (DevicePath
)) {
596 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
597 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
599 BlockIoHdDevicePathNode
= DevicePath
;
603 DevicePath
= NextDevicePathNode (DevicePath
);
606 if (BlockIoHdDevicePathNode
== NULL
) {
610 // See if the harddrive device path in blockio matches the orig Hard Drive Node
612 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
616 // Check for the match
618 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
619 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
620 switch (TmpHdPath
->SignatureType
) {
621 case SIGNATURE_TYPE_GUID
:
622 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
624 case SIGNATURE_TYPE_MBR
:
625 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
637 Delete the boot option associated with the handle passed in.
639 @param Handle The handle which present the device path to create
642 @retval EFI_SUCCESS Delete the boot option success
643 @retval EFI_NOT_FOUND If the Device Path is not found in the system
644 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
645 @retval Other Error return value from SetVariable()
649 BdsLibDeleteOptionFromHandle (
654 UINT8
*BootOptionVar
;
656 UINTN BootOptionSize
;
659 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
660 UINTN DevicePathSize
;
661 UINTN OptionDevicePathSize
;
662 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
663 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
666 Status
= EFI_SUCCESS
;
671 // Check "BootOrder" variable, if no, means there is no any boot order.
673 BootOrder
= BdsLibGetVariableAndSize (
675 &gEfiGlobalVariableGuid
,
678 if (BootOrder
== NULL
) {
679 return EFI_NOT_FOUND
;
683 // Convert device handle to device path protocol instance
685 DevicePath
= DevicePathFromHandle (Handle
);
686 if (DevicePath
== NULL
) {
687 return EFI_NOT_FOUND
;
689 DevicePathSize
= GetDevicePathSize (DevicePath
);
692 // Loop all boot order variable and find the matching device path
695 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
696 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
697 BootOptionVar
= BdsLibGetVariableAndSize (
699 &gEfiGlobalVariableGuid
,
703 if (BootOptionVar
== NULL
) {
704 FreePool (BootOrder
);
705 return EFI_OUT_OF_RESOURCES
;
708 TempPtr
= BootOptionVar
;
709 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
710 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
711 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
712 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
715 // Check whether the device path match
717 if ((OptionDevicePathSize
== DevicePathSize
) &&
718 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
719 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
720 FreePool (BootOptionVar
);
724 FreePool (BootOptionVar
);
729 // Adjust number of boot option for "BootOrder" variable.
731 Status
= gRT
->SetVariable (
733 &gEfiGlobalVariableGuid
,
734 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
739 FreePool (BootOrder
);
746 Delete all invalid EFI boot options. The probable invalid boot option could
747 be Removable media or Network boot device.
749 @retval EFI_SUCCESS Delete all invalid boot option success
750 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
751 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
752 @retval Other Error return value from SetVariable()
756 BdsDeleteAllInvalidEfiBootOption (
761 UINT8
*BootOptionVar
;
763 UINTN BootOptionSize
;
767 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
768 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
771 Status
= EFI_SUCCESS
;
776 // Check "BootOrder" variable firstly, this variable hold the number of boot options
778 BootOrder
= BdsLibGetVariableAndSize (
780 &gEfiGlobalVariableGuid
,
783 if (NULL
== BootOrder
) {
784 return EFI_NOT_FOUND
;
788 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
789 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
790 BootOptionVar
= BdsLibGetVariableAndSize (
792 &gEfiGlobalVariableGuid
,
795 if (NULL
== BootOptionVar
) {
796 FreePool (BootOrder
);
797 return EFI_OUT_OF_RESOURCES
;
800 TempPtr
= BootOptionVar
;
801 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
802 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
803 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
806 // Skip legacy boot option (BBS boot device)
808 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
809 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
810 FreePool (BootOptionVar
);
815 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
817 // Delete this invalid boot option "Boot####"
819 Status
= gRT
->SetVariable (
821 &gEfiGlobalVariableGuid
,
822 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
827 // Mark this boot option in boot order as deleted
829 BootOrder
[Index
] = 0xffff;
832 FreePool (BootOptionVar
);
837 // Adjust boot order array
840 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
841 if (BootOrder
[Index
] != 0xffff) {
842 BootOrder
[Index2
] = BootOrder
[Index
];
846 Status
= gRT
->SetVariable (
848 &gEfiGlobalVariableGuid
,
849 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
850 Index2
* sizeof (UINT16
),
854 FreePool (BootOrder
);
861 This function will enumerate all possible boot device in the system,
862 it will only execute once of every boot.
864 @param BdsBootOptionList The header of the link list which indexed all
867 @retval EFI_SUCCESS Finished all the boot device enumerate and create
868 the boot option base on that boot device
873 BdsLibEnumerateAllBootOption (
874 IN OUT LIST_ENTRY
*BdsBootOptionList
882 UINT16 NonBlockNumber
;
883 UINTN NumberBlockIoHandles
;
884 EFI_HANDLE
*BlockIoHandles
;
885 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
887 UINTN NumberSimpleNetworkHandles
;
888 EFI_HANDLE
*SimpleNetworkHandles
;
890 EFI_HANDLE
*FvHandleBuffer
;
891 EFI_FV_FILETYPE Type
;
893 EFI_FV_FILE_ATTRIBUTES Attributes
;
894 UINT32 AuthenticationStatus
;
895 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
896 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
897 UINTN DevicePathType
;
899 EFI_HANDLE
*FileSystemHandles
;
900 UINTN NumberFileSystemHandles
;
902 EFI_IMAGE_DOS_HEADER DosHeader
;
903 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
904 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
910 ZeroMem (Buffer
, sizeof (Buffer
));
913 // If the boot device enumerate happened, just get the boot
914 // device from the boot order variable
916 if (mEnumBootDevice
) {
917 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
922 // Notes: this dirty code is to get the legacy boot option from the
923 // BBS table and create to variable as the EFI boot option, it should
924 // be removed after the CSM can provide legacy boot option directly
926 REFRESH_LEGACY_BOOT_OPTIONS
;
929 // Delete invalid boot option
931 BdsDeleteAllInvalidEfiBootOption ();
934 // Parse removable media
936 gBS
->LocateHandleBuffer (
938 &gEfiBlockIoProtocolGuid
,
940 &NumberBlockIoHandles
,
944 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
945 Status
= gBS
->HandleProtocol (
946 BlockIoHandles
[Index
],
947 &gEfiBlockIoProtocolGuid
,
950 if (!EFI_ERROR (Status
)) {
951 if (!BlkIo
->Media
->RemovableMedia
) {
953 // skip the non-removable block devices
958 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
959 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
961 switch (DevicePathType
) {
962 case BDS_EFI_ACPI_FLOPPY_BOOT
:
963 if (FloppyNumber
== 0) {
964 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
966 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
968 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
972 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
973 if (CdromNumber
== 0) {
974 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
976 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
978 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
982 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
983 if (UsbNumber
== 0) {
984 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
986 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
988 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
992 case BDS_EFI_MESSAGE_SCSI_BOOT
:
993 if (UsbNumber
== 0) {
994 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
996 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
998 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1002 case BDS_EFI_MESSAGE_MISC_BOOT
:
1003 if (MiscNumber
== 0) {
1004 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
1006 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
1008 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1017 if (NumberBlockIoHandles
!= 0) {
1018 FreePool (BlockIoHandles
);
1022 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1025 gBS
->LocateHandleBuffer (
1027 &gEfiSimpleFileSystemProtocolGuid
,
1029 &NumberFileSystemHandles
,
1032 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1033 Status
= gBS
->HandleProtocol (
1034 FileSystemHandles
[Index
],
1035 &gEfiBlockIoProtocolGuid
,
1038 if (!EFI_ERROR (Status
)) {
1040 // Skip if the file system handle supports a BlkIo protocol,
1046 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1047 // machinename is ia32, ia64, x64, ...
1049 Hdr
.Union
= &HdrData
;
1051 Status
= BdsLibGetImageHeader (
1052 FileSystemHandles
[Index
],
1053 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1057 if (!EFI_ERROR (Status
) &&
1058 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1059 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1065 // No such file or the file is not a EFI application, delete this boot option
1067 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1069 if (NonBlockNumber
== 0) {
1070 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1072 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1074 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1079 if (NumberFileSystemHandles
!= 0) {
1080 FreePool (FileSystemHandles
);
1084 // Parse Network Boot Device
1086 gBS
->LocateHandleBuffer (
1088 &gEfiSimpleNetworkProtocolGuid
,
1090 &NumberSimpleNetworkHandles
,
1091 &SimpleNetworkHandles
1093 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1095 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1097 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1099 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1102 if (NumberSimpleNetworkHandles
!= 0) {
1103 FreePool (SimpleNetworkHandles
);
1107 // Check if we have on flash shell
1109 gBS
->LocateHandleBuffer (
1111 &gEfiFirmwareVolume2ProtocolGuid
,
1116 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1117 gBS
->HandleProtocol (
1118 FvHandleBuffer
[Index
],
1119 &gEfiFirmwareVolume2ProtocolGuid
,
1123 Status
= Fv
->ReadFile (
1130 &AuthenticationStatus
1132 if (EFI_ERROR (Status
)) {
1134 // Skip if no shell file in the FV
1139 // Build the shell boot option
1141 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1144 if (FvHandleCount
!= 0) {
1145 FreePool (FvHandleBuffer
);
1148 // Make sure every boot only have one time
1149 // boot device enumerate
1151 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1152 mEnumBootDevice
= TRUE
;
1158 Build the boot option with the handle parsed in
1160 @param Handle The handle which present the device path to create
1162 @param BdsBootOptionList The header of the link list which indexed all
1163 current boot options
1164 @param String The description of the boot option.
1169 BdsLibBuildOptionFromHandle (
1170 IN EFI_HANDLE Handle
,
1171 IN LIST_ENTRY
*BdsBootOptionList
,
1175 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1177 DevicePath
= DevicePathFromHandle (Handle
);
1180 // Create and register new boot option
1182 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1187 Build the on flash shell boot option with the handle parsed in.
1189 @param Handle The handle which present the device path to create
1190 on flash shell boot option
1191 @param BdsBootOptionList The header of the link list which indexed all
1192 current boot options
1197 BdsLibBuildOptionFromShell (
1198 IN EFI_HANDLE Handle
,
1199 IN OUT LIST_ENTRY
*BdsBootOptionList
1202 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1203 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1205 DevicePath
= DevicePathFromHandle (Handle
);
1208 // Build the shell device path
1210 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1212 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1215 // Create and register the shell boot option
1217 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1222 Boot from the UEFI spec defined "BootNext" variable.
1234 BDS_COMMON_OPTION
*BootOption
;
1235 LIST_ENTRY TempList
;
1240 // Init the boot option name buffer and temp link list
1242 InitializeListHead (&TempList
);
1243 ZeroMem (Buffer
, sizeof (Buffer
));
1245 BootNext
= BdsLibGetVariableAndSize (
1247 &gEfiGlobalVariableGuid
,
1252 // Clear the boot next variable first
1254 if (BootNext
!= NULL
) {
1257 &gEfiGlobalVariableGuid
,
1258 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1264 // Start to build the boot option and try to boot
1266 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1267 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1268 ASSERT (BootOption
!= NULL
);
1269 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1270 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1276 Return the bootable media handle.
1277 First, check the device is connected
1278 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1279 Third, detect the the default boot file in the Media, and return the removable Media handle.
1281 @param DevicePath Device Path to a bootable device
1283 @retval NULL The media on the DevicePath is not bootable
1288 BdsLibGetBootableHandle (
1289 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1293 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1294 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1296 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1298 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1301 EFI_HANDLE ReturnHandle
;
1302 EFI_HANDLE
*SimpleFileSystemHandles
;
1304 UINTN NumberSimpleFileSystemHandles
;
1306 EFI_IMAGE_DOS_HEADER DosHeader
;
1307 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1308 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1310 UpdatedDevicePath
= DevicePath
;
1313 // Check whether the device is connected
1315 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1316 if (EFI_ERROR (Status
)) {
1318 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1320 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1321 if (EFI_ERROR (Status
)) {
1323 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1325 UpdatedDevicePath
= DevicePath
;
1326 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1327 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1331 // Get BlockIo protocol and check removable attribute
1333 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1335 // Issue a dummy read to the device to check for media change.
1336 // When the removable media is changed, any Block IO read/write will
1337 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1338 // returned. After the Block IO protocol is reinstalled, subsequent
1339 // Block IO read/write will success.
1341 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1342 if (Buffer
!= NULL
) {
1343 BlockIo
->ReadBlocks (
1345 BlockIo
->Media
->MediaId
,
1347 BlockIo
->Media
->BlockSize
,
1355 // Detect the the default boot file from removable Media
1359 // 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
1360 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1362 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1363 ASSERT (DupDevicePath
!= NULL
);
1365 UpdatedDevicePath
= DupDevicePath
;
1366 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1368 // 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
1369 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1371 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1372 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1374 // Remove the usb node, let the device path only point to PCI node
1376 SetDevicePathEndNode (UpdatedDevicePath
);
1377 UpdatedDevicePath
= DupDevicePath
;
1379 UpdatedDevicePath
= DevicePath
;
1383 // Get the device path size of boot option
1385 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1386 ReturnHandle
= NULL
;
1387 gBS
->LocateHandleBuffer (
1389 &gEfiSimpleFileSystemProtocolGuid
,
1391 &NumberSimpleFileSystemHandles
,
1392 &SimpleFileSystemHandles
1394 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1396 // Get the device path size of SimpleFileSystem handle
1398 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1399 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1401 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1403 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1405 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1406 // machinename is ia32, ia64, x64, ...
1408 Hdr
.Union
= &HdrData
;
1409 Status
= BdsLibGetImageHeader (
1410 SimpleFileSystemHandles
[Index
],
1411 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1415 if (!EFI_ERROR (Status
) &&
1416 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1417 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1418 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1424 FreePool(DupDevicePath
);
1426 if (SimpleFileSystemHandles
!= NULL
) {
1427 FreePool(SimpleFileSystemHandles
);
1430 return ReturnHandle
;
1434 Check to see if the network cable is plugged in. If the DevicePath is not
1435 connected it will be connected.
1437 @param DevicePath Device Path to check
1439 @retval TRUE DevicePath points to an Network that is connected
1440 @retval FALSE DevicePath does not point to a bootable network
1444 BdsLibNetworkBootWithMediaPresent (
1445 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1449 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1451 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1452 BOOLEAN MediaPresent
;
1454 MediaPresent
= FALSE
;
1456 UpdatedDevicePath
= DevicePath
;
1457 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1458 if (EFI_ERROR (Status
)) {
1460 // Device not present so see if we need to connect it
1462 Status
= BdsLibConnectDevicePath (DevicePath
);
1463 if (!EFI_ERROR (Status
)) {
1465 // This one should work after we did the connect
1467 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1471 if (!EFI_ERROR (Status
)) {
1472 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1473 if (!EFI_ERROR (Status
)) {
1474 if (Snp
->Mode
->MediaPresentSupported
) {
1475 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1477 // In case some one else is using the SNP check to see if it's connected
1479 MediaPresent
= Snp
->Mode
->MediaPresent
;
1482 // No one is using SNP so we need to Start and Initialize so
1483 // MediaPresent will be valid.
1485 Status
= Snp
->Start (Snp
);
1486 if (!EFI_ERROR (Status
)) {
1487 Status
= Snp
->Initialize (Snp
, 0, 0);
1488 if (!EFI_ERROR (Status
)) {
1489 MediaPresent
= Snp
->Mode
->MediaPresent
;
1490 Snp
->Shutdown (Snp
);
1496 MediaPresent
= TRUE
;
1501 return MediaPresent
;
1505 For a bootable Device path, return its boot type.
1507 @param DevicePath The bootable device Path to check
1509 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media device path node, it is media boot type
1510 For the floppy node, handle it as media node
1511 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media device path node, it is media boot type
1512 For the floppy node, handle it as media node
1513 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media device path node, it is media boot type
1514 For the floppy node, handle it as media node
1515 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media device path node, and
1516 its last device path node point to a message device path node, it is
1518 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media device path node, and
1519 its last device path node point to a message device path node, it is
1520 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node, and
1521 its last device path node point to a message device path node, it is
1522 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1523 its last device path node point to a message device path node, it is
1524 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1525 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1530 BdsGetBootTypeFromDevicePath (
1531 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1534 ACPI_HID_DEVICE_PATH
*Acpi
;
1535 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1536 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1539 if (NULL
== DevicePath
) {
1540 return BDS_EFI_UNSUPPORT
;
1543 TempDevicePath
= DevicePath
;
1545 while (!IsDevicePathEndType (TempDevicePath
)) {
1546 switch (DevicePathType (TempDevicePath
)) {
1547 case BBS_DEVICE_PATH
:
1548 return BDS_LEGACY_BBS_BOOT
;
1549 case MEDIA_DEVICE_PATH
:
1550 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1551 return BDS_EFI_MEDIA_HD_BOOT
;
1552 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1553 return BDS_EFI_MEDIA_CDROM_BOOT
;
1556 case ACPI_DEVICE_PATH
:
1557 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1558 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1559 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1562 case MESSAGING_DEVICE_PATH
:
1564 // Get the last device path node
1566 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1567 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1569 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1572 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1575 // if the device path not only point to driver device, it is not a messaging device path,
1577 if (!IsDevicePathEndType (LastDeviceNode
)) {
1581 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1582 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1583 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1584 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1585 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1586 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1588 return BDS_EFI_MESSAGE_MISC_BOOT
;
1592 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1595 return BDS_EFI_UNSUPPORT
;
1599 Check whether the Device path in a boot option point to a valid bootable device,
1600 And if CheckMedia is true, check the device is ready to boot now.
1602 @param DevPath the Device path in a boot option
1603 @param CheckMedia if true, check the device is ready to boot now.
1605 @retval TRUE the Device path is valid
1606 @retval FALSE the Device path is invalid .
1611 BdsLibIsValidEFIBootOptDevicePath (
1612 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1613 IN BOOLEAN CheckMedia
1618 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1619 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1620 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1622 TempDevicePath
= DevPath
;
1623 LastDeviceNode
= DevPath
;
1626 // Check if it's a valid boot option for network boot device
1627 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1628 // there is the network card there.
1630 Status
= gBS
->LocateDevicePath (
1631 &gEfiSimpleNetworkProtocolGuid
,
1635 if (EFI_ERROR (Status
)) {
1637 // Device not present so see if we need to connect it
1639 TempDevicePath
= DevPath
;
1640 BdsLibConnectDevicePath (TempDevicePath
);
1641 Status
= gBS
->LocateDevicePath (
1642 &gEfiSimpleNetworkProtocolGuid
,
1648 if (!EFI_ERROR (Status
)) {
1651 // Test if it is ready to boot now
1653 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1662 // If the boot option point to a file, it is a valid EFI boot option,
1663 // and assume it is ready to boot now
1665 while (!IsDevicePathEnd (TempDevicePath
)) {
1666 LastDeviceNode
= TempDevicePath
;
1667 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1669 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1670 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1675 // Check if it's a valid boot option for internal Shell
1677 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1679 // If the boot option point to Internal FV shell, make sure it is valid
1681 TempDevicePath
= DevPath
;
1682 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1683 if (Status
== EFI_ALREADY_STARTED
) {
1686 if (Status
== EFI_SUCCESS
) {
1687 FreePool (TempDevicePath
);
1694 // If the boot option point to a blockIO device, no matter whether or not it is a removeable device, it is a valid EFI boot option
1696 TempDevicePath
= DevPath
;
1697 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1698 if (EFI_ERROR (Status
)) {
1700 // Device not present so see if we need to connect it
1702 Status
= BdsLibConnectDevicePath (DevPath
);
1703 if (!EFI_ERROR (Status
)) {
1705 // Try again to get the Block Io protocol after we did the connect
1707 TempDevicePath
= DevPath
;
1708 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1712 if (!EFI_ERROR (Status
)) {
1713 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1714 if (!EFI_ERROR (Status
)) {
1717 // Test if it is ready to boot now
1719 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1728 // 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,
1730 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1731 if (!EFI_ERROR (Status
)) {
1734 // Test if it is ready to boot now
1736 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1750 According to a file guild, check a Fv file device path is valid. If it is invalid,
1751 try to return the valid device path.
1752 FV address maybe changes for memory layout adjust from time to time, use this function
1753 could promise the Fv file device path is right.
1755 @param DevicePath on input, the Fv file device path need to check on
1756 output, the updated valid Fv file device path
1757 @param FileGuid the Fv file guild
1759 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1761 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1763 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1765 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1766 and return the updated device path in DevicePath
1771 BdsLibUpdateFvFileDevicePath (
1772 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1773 IN EFI_GUID
*FileGuid
1776 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1777 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1779 EFI_GUID
*GuidPoint
;
1781 UINTN FvHandleCount
;
1782 EFI_HANDLE
*FvHandleBuffer
;
1783 EFI_FV_FILETYPE Type
;
1785 EFI_FV_FILE_ATTRIBUTES Attributes
;
1786 UINT32 AuthenticationStatus
;
1788 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1789 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1790 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1791 EFI_HANDLE FoundFvHandle
;
1792 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1794 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1795 return EFI_INVALID_PARAMETER
;
1797 if (FileGuid
== NULL
) {
1798 return EFI_INVALID_PARAMETER
;
1802 // Check whether the device path point to the default the input Fv file
1804 TempDevicePath
= *DevicePath
;
1805 LastDeviceNode
= TempDevicePath
;
1806 while (!IsDevicePathEnd (TempDevicePath
)) {
1807 LastDeviceNode
= TempDevicePath
;
1808 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1810 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1811 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1813 if (GuidPoint
== NULL
) {
1815 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1817 return EFI_UNSUPPORTED
;
1819 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1821 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1823 return EFI_UNSUPPORTED
;
1827 // Check whether the input Fv file device path is valid
1829 TempDevicePath
= *DevicePath
;
1830 FoundFvHandle
= NULL
;
1831 Status
= gBS
->LocateDevicePath (
1832 &gEfiFirmwareVolume2ProtocolGuid
,
1836 if (!EFI_ERROR (Status
)) {
1837 Status
= gBS
->HandleProtocol (
1839 &gEfiFirmwareVolume2ProtocolGuid
,
1842 if (!EFI_ERROR (Status
)) {
1844 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1846 Status
= Fv
->ReadFile (
1853 &AuthenticationStatus
1855 if (!EFI_ERROR (Status
)) {
1856 return EFI_ALREADY_STARTED
;
1862 // Look for the input wanted FV file in current FV
1863 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1866 FoundFvHandle
= NULL
;
1867 Status
= gBS
->HandleProtocol (
1869 &gEfiLoadedImageProtocolGuid
,
1870 (VOID
**) &LoadedImage
1872 if (!EFI_ERROR (Status
)) {
1873 Status
= gBS
->HandleProtocol (
1874 LoadedImage
->DeviceHandle
,
1875 &gEfiFirmwareVolume2ProtocolGuid
,
1878 if (!EFI_ERROR (Status
)) {
1879 Status
= Fv
->ReadFile (
1886 &AuthenticationStatus
1888 if (!EFI_ERROR (Status
)) {
1890 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1895 // Second, if fail to find, try to enumerate all FV
1898 FvHandleBuffer
= NULL
;
1899 gBS
->LocateHandleBuffer (
1901 &gEfiFirmwareVolume2ProtocolGuid
,
1906 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1907 gBS
->HandleProtocol (
1908 FvHandleBuffer
[Index
],
1909 &gEfiFirmwareVolume2ProtocolGuid
,
1913 Status
= Fv
->ReadFile (
1920 &AuthenticationStatus
1922 if (EFI_ERROR (Status
)) {
1924 // Skip if input Fv file not in the FV
1929 FoundFvHandle
= FvHandleBuffer
[Index
];
1933 if (FvHandleBuffer
!= NULL
) {
1934 FreePool (FvHandleBuffer
);
1940 // Build the shell device path
1942 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1943 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1944 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1945 *DevicePath
= NewDevicePath
;
1948 return EFI_NOT_FOUND
;