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####.
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
;
148 EFI_SECURITY_ARCH_PROTOCOL
*SecurityProtocol
;
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
;
245 // Measure GPT Table by SAP protocol.
247 Status
= gBS
->LocateProtocol (
248 &gEfiSecurityArchProtocolGuid
,
250 (VOID
**) &SecurityProtocol
252 if (!EFI_ERROR (Status
)) {
253 Status
= SecurityProtocol
->FileAuthenticationState (SecurityProtocol
, 0, DevicePath
);
256 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
258 Status
= gBS
->LoadImage (
268 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
269 // and load the image according to the default boot behavior for removable device.
271 if (EFI_ERROR (Status
)) {
273 // check if there is a bootable removable media could be found in this device path ,
274 // and get the bootable media handle
276 Handle
= BdsLibGetBootableHandle(DevicePath
);
277 if (Handle
== NULL
) {
281 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
282 // machinename is ia32, ia64, x64, ...
284 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
285 if (FilePath
!= NULL
) {
286 Status
= gBS
->LoadImage (
294 if (EFI_ERROR (Status
)) {
296 // The DevicePath failed, and it's not a valid
297 // removable media device.
304 if (EFI_ERROR (Status
)) {
306 // It there is any error from the Boot attempt exit now.
311 // Provide the image with it's load options
313 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
314 ASSERT_EFI_ERROR (Status
);
316 if (Option
->LoadOptionsSize
!= 0) {
317 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
318 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
321 // Before calling the image, enable the Watchdog Timer for
322 // the 5 Minute period
324 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
326 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
327 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
330 // Clear the Watchdog Timer after the image returns
332 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
336 // Clear Boot Current
340 &gEfiGlobalVariableGuid
,
341 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
351 Expand a device path that starts with a hard drive media device path node to be a
352 full device path that includes the full hardware path to the device. We need
353 to do this so it can be booted. As an optimization the front match (the part point
354 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
355 so a connect all is not required on every boot. All successful history device path
356 which point to partition node (the front part) will be saved.
358 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
359 drive media device path.
360 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
364 EFI_DEVICE_PATH_PROTOCOL
*
366 BdsExpandPartitionPartialDevicePathToFull (
367 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
371 UINTN BlockIoHandleCount
;
372 EFI_HANDLE
*BlockIoBuffer
;
373 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
374 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
375 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
378 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
379 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
380 UINTN CachedDevicePathSize
;
383 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
386 FullDevicePath
= NULL
;
388 // Check if there is prestore 'HDDP' variable.
389 // If exist, search the front path which point to partition node in the variable instants.
390 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
392 CachedDevicePath
= BdsLibGetVariableAndSize (
394 &mHdBootVariablePrivateGuid
,
395 &CachedDevicePathSize
398 if (CachedDevicePath
!= NULL
) {
399 TempNewDevicePath
= CachedDevicePath
;
404 // Check every instance of the variable
405 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
406 // partial partition boot option. Second, check whether the instance could be connected.
408 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
409 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
411 // Connect the device path instance, the device path point to hard drive media device path node
412 // e.g. ACPI() /PCI()/ATA()/Partition()
414 Status
= BdsLibConnectDevicePath (Instance
);
415 if (!EFI_ERROR (Status
)) {
421 // Come here means the first instance is not matched
425 } while (TempNewDevicePath
!= NULL
);
429 // Find the matched device path.
430 // Append the file path information from the boot option and return the fully expanded device path.
432 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
433 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
436 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
440 // First delete the matched instance.
442 TempNewDevicePath
= CachedDevicePath
;
443 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
444 FreePool (TempNewDevicePath
);
447 // Second, append the remaining path after the matched instance
449 TempNewDevicePath
= CachedDevicePath
;
450 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
451 FreePool (TempNewDevicePath
);
453 // Save the matching Device Path so we don't need to do a connect all next time
455 Status
= gRT
->SetVariable (
457 &mHdBootVariablePrivateGuid
,
458 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
459 GetDevicePathSize (CachedDevicePath
),
465 FreePool (CachedDevicePath
);
466 return FullDevicePath
;
471 // If we get here we fail to find or 'HDDP' not exist, and now we need
472 // to search all devices in the system for a matched partition
474 BdsLibConnectAllDriversToAllControllers ();
475 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
476 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
478 // If there was an error or there are no device handles that support
479 // the BLOCK_IO Protocol, then return.
484 // Loop through all the device handles that support the BLOCK_IO Protocol
486 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
488 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
489 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
493 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
495 // Find the matched partition device path
497 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
498 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
501 // Save the matched partition device path in 'HDDP' variable
503 if (CachedDevicePath
!= NULL
) {
505 // Save the matched partition device path as first instance of 'HDDP' variable
507 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
508 TempNewDevicePath
= CachedDevicePath
;
509 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
510 FreePool(TempNewDevicePath
);
512 TempNewDevicePath
= CachedDevicePath
;
513 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
514 FreePool(TempNewDevicePath
);
516 TempNewDevicePath
= CachedDevicePath
;
517 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
518 FreePool(TempNewDevicePath
);
521 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
522 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
525 ASSERT (CachedDevicePath
!= NULL
);
526 TempNewDevicePath
= CachedDevicePath
;
527 while (!IsDevicePathEnd (TempNewDevicePath
)) {
528 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
530 // Parse one instance
532 while (!IsDevicePathEndType (TempNewDevicePath
)) {
533 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
537 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
539 if (InstanceNum
>= 12) {
540 SetDevicePathEndNode (TempNewDevicePath
);
545 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
549 // Save the matching Device Path so we don't need to do a connect all next time
551 Status
= gRT
->SetVariable (
553 &mHdBootVariablePrivateGuid
,
554 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
555 GetDevicePathSize (CachedDevicePath
),
563 FreePool (CachedDevicePath
);
564 if (BlockIoBuffer
!= NULL
) {
565 FreePool (BlockIoBuffer
);
567 return FullDevicePath
;
571 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
572 instances, has the same partition node with HardDriveDevicePath device path
574 @param BlockIoDevicePath Multi device path instances which need to check
575 @param HardDriveDevicePath A device path which starts with a hard drive media
578 @retval TRUE There is a matched device path instance.
579 @retval FALSE There is no matched device path instance.
584 MatchPartitionDevicePathNode (
585 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
586 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
589 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
590 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
592 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
594 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
599 // Make PreviousDevicePath == the device path node before the end node
601 DevicePath
= BlockIoDevicePath
;
602 BlockIoHdDevicePathNode
= NULL
;
605 // find the partition device path node
607 while (!IsDevicePathEnd (DevicePath
)) {
608 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
609 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
611 BlockIoHdDevicePathNode
= DevicePath
;
615 DevicePath
= NextDevicePathNode (DevicePath
);
618 if (BlockIoHdDevicePathNode
== NULL
) {
622 // See if the harddrive device path in blockio matches the orig Hard Drive Node
624 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
628 // Check for the match
630 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
631 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
632 switch (TmpHdPath
->SignatureType
) {
633 case SIGNATURE_TYPE_GUID
:
634 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
636 case SIGNATURE_TYPE_MBR
:
637 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
649 Delete the boot option associated with the handle passed in.
651 @param Handle The handle which present the device path to create
654 @retval EFI_SUCCESS Delete the boot option success
655 @retval EFI_NOT_FOUND If the Device Path is not found in the system
656 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
657 @retval Other Error return value from SetVariable()
661 BdsLibDeleteOptionFromHandle (
666 UINT8
*BootOptionVar
;
668 UINTN BootOptionSize
;
671 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
672 UINTN DevicePathSize
;
673 UINTN OptionDevicePathSize
;
674 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
675 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
678 Status
= EFI_SUCCESS
;
683 // Check "BootOrder" variable, if no, means there is no any boot order.
685 BootOrder
= BdsLibGetVariableAndSize (
687 &gEfiGlobalVariableGuid
,
690 if (BootOrder
== NULL
) {
691 return EFI_NOT_FOUND
;
695 // Convert device handle to device path protocol instance
697 DevicePath
= DevicePathFromHandle (Handle
);
698 if (DevicePath
== NULL
) {
699 return EFI_NOT_FOUND
;
701 DevicePathSize
= GetDevicePathSize (DevicePath
);
704 // Loop all boot order variable and find the matching device path
707 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
708 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
709 BootOptionVar
= BdsLibGetVariableAndSize (
711 &gEfiGlobalVariableGuid
,
715 if (BootOptionVar
== NULL
) {
716 FreePool (BootOrder
);
717 return EFI_OUT_OF_RESOURCES
;
720 TempPtr
= BootOptionVar
;
721 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
722 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
723 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
724 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
727 // Check whether the device path match
729 if ((OptionDevicePathSize
== DevicePathSize
) &&
730 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
731 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
732 FreePool (BootOptionVar
);
736 FreePool (BootOptionVar
);
741 // Adjust number of boot option for "BootOrder" variable.
743 Status
= gRT
->SetVariable (
745 &gEfiGlobalVariableGuid
,
746 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
751 FreePool (BootOrder
);
758 Delete all invalid EFI boot options.
760 @retval EFI_SUCCESS Delete all invalid boot option success
761 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
762 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
763 @retval Other Error return value from SetVariable()
767 BdsDeleteAllInvalidEfiBootOption (
772 UINT8
*BootOptionVar
;
774 UINTN BootOptionSize
;
778 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
779 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
783 Status
= EFI_SUCCESS
;
788 // Check "BootOrder" variable firstly, this variable hold the number of boot options
790 BootOrder
= BdsLibGetVariableAndSize (
792 &gEfiGlobalVariableGuid
,
795 if (NULL
== BootOrder
) {
796 return EFI_NOT_FOUND
;
800 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
801 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
802 BootOptionVar
= BdsLibGetVariableAndSize (
804 &gEfiGlobalVariableGuid
,
807 if (NULL
== BootOptionVar
) {
808 FreePool (BootOrder
);
809 return EFI_OUT_OF_RESOURCES
;
812 TempPtr
= BootOptionVar
;
813 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
814 Description
= (CHAR16
*) TempPtr
;
815 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
816 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
819 // Skip legacy boot option (BBS boot device)
821 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
822 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
823 FreePool (BootOptionVar
);
828 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
830 // Delete this invalid boot option "Boot####"
832 Status
= gRT
->SetVariable (
834 &gEfiGlobalVariableGuid
,
835 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
840 // Mark this boot option in boot order as deleted
842 BootOrder
[Index
] = 0xffff;
845 FreePool (BootOptionVar
);
850 // Adjust boot order array
853 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
854 if (BootOrder
[Index
] != 0xffff) {
855 BootOrder
[Index2
] = BootOrder
[Index
];
859 Status
= gRT
->SetVariable (
861 &gEfiGlobalVariableGuid
,
862 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
863 Index2
* sizeof (UINT16
),
867 FreePool (BootOrder
);
874 For EFI boot option, BDS separate them as six types:
875 1. Network - The boot option points to the SimpleNetworkProtocol device.
876 Bds will try to automatically create this type boot option when enumerate.
877 2. Shell - The boot option points to internal flash shell.
878 Bds will try to automatically create this type boot option when enumerate.
879 3. Removable BlockIo - The boot option only points to the removable media
880 device, like USB flash disk, DVD, Floppy etc.
881 These device should contain a *removable* blockIo
882 protocol in their device handle.
883 Bds will try to automatically create this type boot option
885 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
887 These device should contain a *fixed* blockIo
888 protocol in their device handle.
889 BDS will skip fixed blockIo devices, and NOT
890 automatically create boot option for them. But BDS
891 will help to delete those fixed blockIo boot option,
892 whose description rule conflict with other auto-created
894 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
895 has SimpleFileSystem Protocol, but has no blockio
896 protocol. These devices do not offer blockIo
897 protocol, but BDS still can get the
898 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
900 6. File - The boot option points to a file. These boot options are usually
901 created by user manually or OS loader. BDS will not delete or modify
904 This function will enumerate all possible boot device in the system, and
905 automatically create boot options for Network, Shell, Removable BlockIo,
906 and Non-BlockIo Simplefile devices.
907 It will only execute once of every boot.
909 @param BdsBootOptionList The header of the link list which indexed all
912 @retval EFI_SUCCESS Finished all the boot device enumerate and create
913 the boot option base on that boot device
918 BdsLibEnumerateAllBootOption (
919 IN OUT LIST_ENTRY
*BdsBootOptionList
928 UINT16 NonBlockNumber
;
929 UINTN NumberBlockIoHandles
;
930 EFI_HANDLE
*BlockIoHandles
;
931 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
933 UINTN NumberSimpleNetworkHandles
;
934 EFI_HANDLE
*SimpleNetworkHandles
;
936 EFI_HANDLE
*FvHandleBuffer
;
937 EFI_FV_FILETYPE Type
;
939 EFI_FV_FILE_ATTRIBUTES Attributes
;
940 UINT32 AuthenticationStatus
;
941 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
942 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
943 UINTN DevicePathType
;
945 EFI_HANDLE
*FileSystemHandles
;
946 UINTN NumberFileSystemHandles
;
948 EFI_IMAGE_DOS_HEADER DosHeader
;
949 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
950 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
957 ZeroMem (Buffer
, sizeof (Buffer
));
960 // If the boot device enumerate happened, just get the boot
961 // device from the boot order variable
963 if (mEnumBootDevice
) {
964 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
969 // Notes: this dirty code is to get the legacy boot option from the
970 // BBS table and create to variable as the EFI boot option, it should
971 // be removed after the CSM can provide legacy boot option directly
973 REFRESH_LEGACY_BOOT_OPTIONS
;
976 // Delete invalid boot option
978 BdsDeleteAllInvalidEfiBootOption ();
981 // Parse removable media
983 gBS
->LocateHandleBuffer (
985 &gEfiBlockIoProtocolGuid
,
987 &NumberBlockIoHandles
,
991 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
992 Status
= gBS
->HandleProtocol (
993 BlockIoHandles
[Index
],
994 &gEfiBlockIoProtocolGuid
,
997 if (!EFI_ERROR (Status
)) {
998 if (!BlkIo
->Media
->RemovableMedia
) {
1000 // skip the non-removable block devices
1005 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1006 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1008 switch (DevicePathType
) {
1009 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1010 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_FLOPPY_NUM
, FloppyNumber
);
1011 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1016 // Assume a removable SATA device should be the DVD/CD device
1018 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1019 case BDS_EFI_MESSAGE_SATA_BOOT
:
1020 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_DVD_NUM
, CdromNumber
);
1021 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1025 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1026 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_USB_NUM
, UsbNumber
);
1027 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1031 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1032 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_SCSI_NUM
, ScsiNumber
);
1033 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1037 case BDS_EFI_MESSAGE_MISC_BOOT
:
1038 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_MISC_NUM
, MiscNumber
);
1039 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1048 if (NumberBlockIoHandles
!= 0) {
1049 FreePool (BlockIoHandles
);
1053 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1056 gBS
->LocateHandleBuffer (
1058 &gEfiSimpleFileSystemProtocolGuid
,
1060 &NumberFileSystemHandles
,
1063 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1064 Status
= gBS
->HandleProtocol (
1065 FileSystemHandles
[Index
],
1066 &gEfiBlockIoProtocolGuid
,
1069 if (!EFI_ERROR (Status
)) {
1071 // Skip if the file system handle supports a BlkIo protocol,
1077 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1078 // machinename is ia32, ia64, x64, ...
1080 Hdr
.Union
= &HdrData
;
1082 Status
= BdsLibGetImageHeader (
1083 FileSystemHandles
[Index
],
1084 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1088 if (!EFI_ERROR (Status
) &&
1089 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1090 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1096 // No such file or the file is not a EFI application, delete this boot option
1098 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1100 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_NON_BLOCK_NUM
, NonBlockNumber
);
1101 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1106 if (NumberFileSystemHandles
!= 0) {
1107 FreePool (FileSystemHandles
);
1111 // Parse Network Boot Device
1113 gBS
->LocateHandleBuffer (
1115 &gEfiSimpleNetworkProtocolGuid
,
1117 &NumberSimpleNetworkHandles
,
1118 &SimpleNetworkHandles
1121 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1122 UnicodeSPrint (Buffer
, sizeof (Buffer
), DESCRIPTION_NETWORK_NUM
, Index
);
1123 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1126 if (NumberSimpleNetworkHandles
!= 0) {
1127 FreePool (SimpleNetworkHandles
);
1131 // Check if we have on flash shell
1133 gBS
->LocateHandleBuffer (
1135 &gEfiFirmwareVolume2ProtocolGuid
,
1140 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1141 gBS
->HandleProtocol (
1142 FvHandleBuffer
[Index
],
1143 &gEfiFirmwareVolume2ProtocolGuid
,
1147 Status
= Fv
->ReadFile (
1154 &AuthenticationStatus
1156 if (EFI_ERROR (Status
)) {
1158 // Skip if no shell file in the FV
1163 // Build the shell boot option
1165 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1168 if (FvHandleCount
!= 0) {
1169 FreePool (FvHandleBuffer
);
1172 // Make sure every boot only have one time
1173 // boot device enumerate
1175 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1176 mEnumBootDevice
= TRUE
;
1182 Build the boot option with the handle parsed in
1184 @param Handle The handle which present the device path to create
1186 @param BdsBootOptionList The header of the link list which indexed all
1187 current boot options
1188 @param String The description of the boot option.
1193 BdsLibBuildOptionFromHandle (
1194 IN EFI_HANDLE Handle
,
1195 IN LIST_ENTRY
*BdsBootOptionList
,
1199 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1201 DevicePath
= DevicePathFromHandle (Handle
);
1204 // Create and register new boot option
1206 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1211 Build the on flash shell boot option with the handle parsed in.
1213 @param Handle The handle which present the device path to create
1214 on flash shell boot option
1215 @param BdsBootOptionList The header of the link list which indexed all
1216 current boot options
1221 BdsLibBuildOptionFromShell (
1222 IN EFI_HANDLE Handle
,
1223 IN OUT LIST_ENTRY
*BdsBootOptionList
1226 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1227 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1229 DevicePath
= DevicePathFromHandle (Handle
);
1232 // Build the shell device path
1234 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1236 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1239 // Create and register the shell boot option
1241 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1246 Boot from the UEFI spec defined "BootNext" variable.
1258 BDS_COMMON_OPTION
*BootOption
;
1259 LIST_ENTRY TempList
;
1264 // Init the boot option name buffer and temp link list
1266 InitializeListHead (&TempList
);
1267 ZeroMem (Buffer
, sizeof (Buffer
));
1269 BootNext
= BdsLibGetVariableAndSize (
1271 &gEfiGlobalVariableGuid
,
1276 // Clear the boot next variable first
1278 if (BootNext
!= NULL
) {
1281 &gEfiGlobalVariableGuid
,
1282 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1288 // Start to build the boot option and try to boot
1290 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1291 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1292 ASSERT (BootOption
!= NULL
);
1293 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1294 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1300 Return the bootable media handle.
1301 First, check the device is connected
1302 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1303 Third, detect the the default boot file in the Media, and return the removable Media handle.
1305 @param DevicePath Device Path to a bootable device
1307 @retval NULL The media on the DevicePath is not bootable
1312 BdsLibGetBootableHandle (
1313 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1317 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1318 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1320 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1322 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1325 EFI_HANDLE ReturnHandle
;
1326 EFI_HANDLE
*SimpleFileSystemHandles
;
1328 UINTN NumberSimpleFileSystemHandles
;
1330 EFI_IMAGE_DOS_HEADER DosHeader
;
1331 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1332 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1334 UpdatedDevicePath
= DevicePath
;
1337 // Check whether the device is connected
1339 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1340 if (EFI_ERROR (Status
)) {
1342 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1344 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1345 if (EFI_ERROR (Status
)) {
1347 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1349 UpdatedDevicePath
= DevicePath
;
1350 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1351 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1355 // Get BlockIo protocol and check removable attribute
1357 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1359 // Issue a dummy read to the device to check for media change.
1360 // When the removable media is changed, any Block IO read/write will
1361 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1362 // returned. After the Block IO protocol is reinstalled, subsequent
1363 // Block IO read/write will success.
1365 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1366 if (Buffer
!= NULL
) {
1367 BlockIo
->ReadBlocks (
1369 BlockIo
->Media
->MediaId
,
1371 BlockIo
->Media
->BlockSize
,
1379 // Detect the the default boot file from removable Media
1383 // 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
1384 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1386 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1387 ASSERT (DupDevicePath
!= NULL
);
1389 UpdatedDevicePath
= DupDevicePath
;
1390 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1392 // 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
1393 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1395 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1396 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1398 // Remove the usb node, let the device path only point to PCI node
1400 SetDevicePathEndNode (UpdatedDevicePath
);
1401 UpdatedDevicePath
= DupDevicePath
;
1403 UpdatedDevicePath
= DevicePath
;
1407 // Get the device path size of boot option
1409 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1410 ReturnHandle
= NULL
;
1411 gBS
->LocateHandleBuffer (
1413 &gEfiSimpleFileSystemProtocolGuid
,
1415 &NumberSimpleFileSystemHandles
,
1416 &SimpleFileSystemHandles
1418 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1420 // Get the device path size of SimpleFileSystem handle
1422 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1423 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1425 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1427 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1429 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1430 // machinename is ia32, ia64, x64, ...
1432 Hdr
.Union
= &HdrData
;
1433 Status
= BdsLibGetImageHeader (
1434 SimpleFileSystemHandles
[Index
],
1435 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1439 if (!EFI_ERROR (Status
) &&
1440 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1441 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1442 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1448 FreePool(DupDevicePath
);
1450 if (SimpleFileSystemHandles
!= NULL
) {
1451 FreePool(SimpleFileSystemHandles
);
1454 return ReturnHandle
;
1458 Check to see if the network cable is plugged in. If the DevicePath is not
1459 connected it will be connected.
1461 @param DevicePath Device Path to check
1463 @retval TRUE DevicePath points to an Network that is connected
1464 @retval FALSE DevicePath does not point to a bootable network
1468 BdsLibNetworkBootWithMediaPresent (
1469 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1473 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1475 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1476 BOOLEAN MediaPresent
;
1478 MediaPresent
= FALSE
;
1480 UpdatedDevicePath
= DevicePath
;
1481 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1482 if (EFI_ERROR (Status
)) {
1484 // Device not present so see if we need to connect it
1486 Status
= BdsLibConnectDevicePath (DevicePath
);
1487 if (!EFI_ERROR (Status
)) {
1489 // This one should work after we did the connect
1491 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1495 if (!EFI_ERROR (Status
)) {
1496 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1497 if (!EFI_ERROR (Status
)) {
1498 if (Snp
->Mode
->MediaPresentSupported
) {
1499 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1501 // In case some one else is using the SNP check to see if it's connected
1503 MediaPresent
= Snp
->Mode
->MediaPresent
;
1506 // No one is using SNP so we need to Start and Initialize so
1507 // MediaPresent will be valid.
1509 Status
= Snp
->Start (Snp
);
1510 if (!EFI_ERROR (Status
)) {
1511 Status
= Snp
->Initialize (Snp
, 0, 0);
1512 if (!EFI_ERROR (Status
)) {
1513 MediaPresent
= Snp
->Mode
->MediaPresent
;
1514 Snp
->Shutdown (Snp
);
1520 MediaPresent
= TRUE
;
1525 return MediaPresent
;
1529 For a bootable Device path, return its boot type.
1531 @param DevicePath The bootable device Path to check
1533 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media device path node, it is media boot type
1534 For the floppy node, handle it as media node
1535 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media device path node, it is media boot type
1536 For the floppy node, handle it as media node
1537 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media device path node, it is media boot type
1538 For the floppy node, handle it as media node
1539 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media device path node, and
1540 its last device path node point to a message device path node, it is
1542 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media device path node, and
1543 its last device path node point to a message device path node, it is
1544 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node, and
1545 its last device path node point to a message device path node, it is
1546 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1547 its last device path node point to a message device path node, it is
1548 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1549 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1554 BdsGetBootTypeFromDevicePath (
1555 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1558 ACPI_HID_DEVICE_PATH
*Acpi
;
1559 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1560 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1563 if (NULL
== DevicePath
) {
1564 return BDS_EFI_UNSUPPORT
;
1567 TempDevicePath
= DevicePath
;
1569 while (!IsDevicePathEndType (TempDevicePath
)) {
1570 switch (DevicePathType (TempDevicePath
)) {
1571 case BBS_DEVICE_PATH
:
1572 return BDS_LEGACY_BBS_BOOT
;
1573 case MEDIA_DEVICE_PATH
:
1574 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1575 return BDS_EFI_MEDIA_HD_BOOT
;
1576 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1577 return BDS_EFI_MEDIA_CDROM_BOOT
;
1580 case ACPI_DEVICE_PATH
:
1581 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1582 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1583 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1586 case MESSAGING_DEVICE_PATH
:
1588 // Get the last device path node
1590 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1591 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1593 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1596 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1599 // if the device path not only point to driver device, it is not a messaging device path,
1601 if (!IsDevicePathEndType (LastDeviceNode
)) {
1605 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1606 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1607 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1608 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1609 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1610 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1611 } else if (DevicePathSubType(TempDevicePath
) == MSG_SATA_DP
) {
1612 return BDS_EFI_MESSAGE_SATA_BOOT
;
1614 return BDS_EFI_MESSAGE_MISC_BOOT
;
1618 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1621 return BDS_EFI_UNSUPPORT
;
1626 Check whether the descriptionis is conflict with the description reserved for
1627 auto-created boot options.
1629 @param Description The Description in a boot option
1631 @retval TRUE The description is conflict with the description reserved for
1632 auto-created boot options.
1633 @retval FALSE The description is not conflict with the description reserved.
1638 CheckDescritptionConflict (
1639 IN CHAR16
*Description
1642 if (Description
== NULL
) {
1645 if ((CompareMem (Description
, DESCRIPTION_FLOPPY
, StrLen (DESCRIPTION_FLOPPY
) * sizeof (CHAR16
)) == 0) ||
1646 (CompareMem (Description
, DESCRIPTION_DVD
, StrLen (DESCRIPTION_DVD
) * sizeof (CHAR16
)) == 0) ||
1647 (CompareMem (Description
, DESCRIPTION_USB
, StrLen (DESCRIPTION_USB
) * sizeof (CHAR16
)) == 0) ||
1648 (CompareMem (Description
, DESCRIPTION_SCSI
, StrLen (DESCRIPTION_SCSI
) * sizeof (CHAR16
)) == 0) ||
1649 (CompareMem (Description
, DESCRIPTION_MISC
, StrLen (DESCRIPTION_MISC
) * sizeof (CHAR16
)) == 0) ||
1650 (CompareMem (Description
, DESCRIPTION_NETWORK
, StrLen (DESCRIPTION_NETWORK
) * sizeof (CHAR16
)) == 0)||
1651 (CompareMem (Description
, DESCRIPTION_NON_BLOCK
, StrLen (DESCRIPTION_NON_BLOCK
) * sizeof (CHAR16
)) == 0)) {
1660 Check whether the Device path in a boot option point to a valid bootable device,
1661 And if CheckMedia is true, check the device is ready to boot now.
1663 @param DevPath the Device path in a boot option
1664 @param CheckMedia if true, check the device is ready to boot now.
1666 @retval TRUE the Device path is valid
1667 @retval FALSE the Device path is invalid .
1672 BdsLibIsValidEFIBootOptDevicePath (
1673 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1674 IN BOOLEAN CheckMedia
1677 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1681 Check whether the Device path in a boot option point to a valid bootable device,
1682 And if CheckMedia is true, check the device is ready to boot now.
1683 If Description is not NULL and the device path point to a fixed BlockIo
1684 device, check the description whether conflict with other auto-created
1687 @param DevPath the Device path in a boot option
1688 @param CheckMedia if true, check the device is ready to boot now.
1689 @param Description the description in a boot option
1691 @retval TRUE the Device path is valid
1692 @retval FALSE the Device path is invalid .
1697 BdsLibIsValidEFIBootOptDevicePathExt (
1698 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1699 IN BOOLEAN CheckMedia
,
1700 IN CHAR16
*Description
1705 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1706 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1707 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1709 TempDevicePath
= DevPath
;
1710 LastDeviceNode
= DevPath
;
1713 // Check if it's a valid boot option for network boot device
1714 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1715 // there is the network card there.
1717 Status
= gBS
->LocateDevicePath (
1718 &gEfiSimpleNetworkProtocolGuid
,
1722 if (EFI_ERROR (Status
)) {
1724 // Device not present so see if we need to connect it
1726 TempDevicePath
= DevPath
;
1727 BdsLibConnectDevicePath (TempDevicePath
);
1728 Status
= gBS
->LocateDevicePath (
1729 &gEfiSimpleNetworkProtocolGuid
,
1735 if (!EFI_ERROR (Status
)) {
1738 // Test if it is ready to boot now
1740 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1749 // If the boot option point to a file, it is a valid EFI boot option,
1750 // and assume it is ready to boot now
1752 while (!IsDevicePathEnd (TempDevicePath
)) {
1753 LastDeviceNode
= TempDevicePath
;
1754 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1756 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1757 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1762 // Check if it's a valid boot option for internal Shell
1764 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1766 // If the boot option point to Internal FV shell, make sure it is valid
1768 TempDevicePath
= DevPath
;
1769 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1770 if (Status
== EFI_ALREADY_STARTED
) {
1773 if (Status
== EFI_SUCCESS
) {
1774 FreePool (TempDevicePath
);
1781 // If the boot option point to a blockIO device:
1782 // if it is a removable blockIo device, it is valid.
1783 // if it is a fixed blockIo device, check its description confliction.
1785 TempDevicePath
= DevPath
;
1786 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1787 if (EFI_ERROR (Status
)) {
1789 // Device not present so see if we need to connect it
1791 Status
= BdsLibConnectDevicePath (DevPath
);
1792 if (!EFI_ERROR (Status
)) {
1794 // Try again to get the Block Io protocol after we did the connect
1796 TempDevicePath
= DevPath
;
1797 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1801 if (!EFI_ERROR (Status
)) {
1802 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1803 if (!EFI_ERROR (Status
)) {
1804 if (!BlockIo
->Media
->RemovableMedia
) {
1806 // For the Fixed block devices, check its description whether conflict
1807 // with other auto-created boot options. BDS permit a boot option point to
1808 // Fixed block device, but not permit it use the description reserved for
1809 // auto-created boot options.
1810 // The check is to cover the bug, that replace a removable BlockIo device
1811 // with a fixed BlockIo device at the same port, but the removable device's
1812 // boot option can not be automatically deleted.
1814 if (CheckDescritptionConflict (Description
)) {
1820 // Test if it is ready to boot now
1822 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1831 // 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,
1833 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1834 if (!EFI_ERROR (Status
)) {
1837 // Test if it is ready to boot now
1839 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1853 According to a file guild, check a Fv file device path is valid. If it is invalid,
1854 try to return the valid device path.
1855 FV address maybe changes for memory layout adjust from time to time, use this function
1856 could promise the Fv file device path is right.
1858 @param DevicePath on input, the Fv file device path need to check on
1859 output, the updated valid Fv file device path
1860 @param FileGuid the Fv file guild
1862 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1864 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1866 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1868 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1869 and return the updated device path in DevicePath
1874 BdsLibUpdateFvFileDevicePath (
1875 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1876 IN EFI_GUID
*FileGuid
1879 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1880 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1882 EFI_GUID
*GuidPoint
;
1884 UINTN FvHandleCount
;
1885 EFI_HANDLE
*FvHandleBuffer
;
1886 EFI_FV_FILETYPE Type
;
1888 EFI_FV_FILE_ATTRIBUTES Attributes
;
1889 UINT32 AuthenticationStatus
;
1891 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1892 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1893 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1894 EFI_HANDLE FoundFvHandle
;
1895 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1897 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1898 return EFI_INVALID_PARAMETER
;
1900 if (FileGuid
== NULL
) {
1901 return EFI_INVALID_PARAMETER
;
1905 // Check whether the device path point to the default the input Fv file
1907 TempDevicePath
= *DevicePath
;
1908 LastDeviceNode
= TempDevicePath
;
1909 while (!IsDevicePathEnd (TempDevicePath
)) {
1910 LastDeviceNode
= TempDevicePath
;
1911 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1913 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1914 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1916 if (GuidPoint
== NULL
) {
1918 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1920 return EFI_UNSUPPORTED
;
1922 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1924 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1926 return EFI_UNSUPPORTED
;
1930 // Check whether the input Fv file device path is valid
1932 TempDevicePath
= *DevicePath
;
1933 FoundFvHandle
= NULL
;
1934 Status
= gBS
->LocateDevicePath (
1935 &gEfiFirmwareVolume2ProtocolGuid
,
1939 if (!EFI_ERROR (Status
)) {
1940 Status
= gBS
->HandleProtocol (
1942 &gEfiFirmwareVolume2ProtocolGuid
,
1945 if (!EFI_ERROR (Status
)) {
1947 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1949 Status
= Fv
->ReadFile (
1956 &AuthenticationStatus
1958 if (!EFI_ERROR (Status
)) {
1959 return EFI_ALREADY_STARTED
;
1965 // Look for the input wanted FV file in current FV
1966 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1969 FoundFvHandle
= NULL
;
1970 Status
= gBS
->HandleProtocol (
1972 &gEfiLoadedImageProtocolGuid
,
1973 (VOID
**) &LoadedImage
1975 if (!EFI_ERROR (Status
)) {
1976 Status
= gBS
->HandleProtocol (
1977 LoadedImage
->DeviceHandle
,
1978 &gEfiFirmwareVolume2ProtocolGuid
,
1981 if (!EFI_ERROR (Status
)) {
1982 Status
= Fv
->ReadFile (
1989 &AuthenticationStatus
1991 if (!EFI_ERROR (Status
)) {
1993 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1998 // Second, if fail to find, try to enumerate all FV
2001 FvHandleBuffer
= NULL
;
2002 gBS
->LocateHandleBuffer (
2004 &gEfiFirmwareVolume2ProtocolGuid
,
2009 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2010 gBS
->HandleProtocol (
2011 FvHandleBuffer
[Index
],
2012 &gEfiFirmwareVolume2ProtocolGuid
,
2016 Status
= Fv
->ReadFile (
2023 &AuthenticationStatus
2025 if (EFI_ERROR (Status
)) {
2027 // Skip if input Fv file not in the FV
2032 FoundFvHandle
= FvHandleBuffer
[Index
];
2036 if (FvHandleBuffer
!= NULL
) {
2037 FreePool (FvHandleBuffer
);
2043 // Build the shell device path
2045 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2046 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2047 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2048 *DevicePath
= NewDevicePath
;
2051 return EFI_NOT_FOUND
;