2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2009, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
17 BOOLEAN mEnumBootDevice
= FALSE
;
20 /// This GUID is used for an EFI Variable that stores the front device pathes
21 /// for a partial device path that starts with the HD node.
23 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 Boot the legacy system with the boot option
30 @param Option The legacy boot option which have BBS device path
32 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
34 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
39 IN BDS_COMMON_OPTION
*Option
43 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
45 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
46 if (EFI_ERROR (Status
)) {
48 // If no LegacyBios protocol we do not support legacy boot
50 return EFI_UNSUPPORTED
;
53 // Notes: if we separate the int 19, then we don't need to refresh BBS
55 BdsRefreshBbsTableForBoot (Option
);
58 // Write boot to OS performance data for legacy boot.
61 WriteBootToOsPerformanceData ();
64 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
65 return LegacyBios
->LegacyBoot (
67 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
68 Option
->LoadOptionsSize
,
74 Internal function to check if the input boot option is a valid EFI NV Boot####.
76 @param OptionToCheck Boot option to be checked.
78 @retval TRUE This boot option matches a valid EFI NV Boot####.
83 IsBootOptionValidNVVarialbe (
84 IN BDS_COMMON_OPTION
*OptionToCheck
88 BDS_COMMON_OPTION
*BootOption
;
90 CHAR16 OptionName
[20];
94 InitializeListHead (&TempList
);
95 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
97 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
98 if (BootOption
== NULL
) {
103 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
104 // valid EFI NV Boot####.
106 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
107 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
112 FreePool (BootOption
);
117 Process the boot option follow the UEFI specification and
118 special treat the legacy boot option with BBS_DEVICE_PATH.
120 @param Option The boot option need to be processed
121 @param DevicePath The device path which describe where to load the
122 boot image or the legacy BBS device path to boot
124 @param ExitDataSize The size of exit data.
125 @param ExitData Data returned when Boot image failed.
127 @retval EFI_SUCCESS Boot from the input boot option successfully.
128 @retval EFI_NOT_FOUND If the Device Path is not found in the system
133 BdsLibBootViaBootOption (
134 IN BDS_COMMON_OPTION
*Option
,
135 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
136 OUT UINTN
*ExitDataSize
,
137 OUT CHAR16
**ExitData OPTIONAL
142 EFI_HANDLE ImageHandle
;
143 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
144 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
145 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
146 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
147 LIST_ENTRY TempBootLists
;
148 EFI_SECURITY_ARCH_PROTOCOL
*SecurityProtocol
;
151 // Record the performance data for End of BDS
153 PERF_END(NULL
, "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
, PcdGetPtr(PcdShellFile
));
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
);
327 // Write boot to OS performance data for UEFI boot
330 WriteBootToOsPerformanceData ();
333 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
334 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
337 // Clear the Watchdog Timer after the image returns
339 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
343 // Clear Boot Current
347 &gEfiGlobalVariableGuid
,
348 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
358 Expand a device path that starts with a hard drive media device path node to be a
359 full device path that includes the full hardware path to the device. We need
360 to do this so it can be booted. As an optimization the front match (the part point
361 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
362 so a connect all is not required on every boot. All successful history device path
363 which point to partition node (the front part) will be saved.
365 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
366 drive media device path.
367 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
371 EFI_DEVICE_PATH_PROTOCOL
*
373 BdsExpandPartitionPartialDevicePathToFull (
374 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
378 UINTN BlockIoHandleCount
;
379 EFI_HANDLE
*BlockIoBuffer
;
380 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
381 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
382 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
385 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
386 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
387 UINTN CachedDevicePathSize
;
390 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
393 FullDevicePath
= NULL
;
395 // Check if there is prestore 'HDDP' variable.
396 // If exist, search the front path which point to partition node in the variable instants.
397 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
399 CachedDevicePath
= BdsLibGetVariableAndSize (
401 &mHdBootVariablePrivateGuid
,
402 &CachedDevicePathSize
405 if (CachedDevicePath
!= NULL
) {
406 TempNewDevicePath
= CachedDevicePath
;
411 // Check every instance of the variable
412 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
413 // partial partition boot option. Second, check whether the instance could be connected.
415 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
416 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
418 // Connect the device path instance, the device path point to hard drive media device path node
419 // e.g. ACPI() /PCI()/ATA()/Partition()
421 Status
= BdsLibConnectDevicePath (Instance
);
422 if (!EFI_ERROR (Status
)) {
428 // Come here means the first instance is not matched
432 } while (TempNewDevicePath
!= NULL
);
436 // Find the matched device path.
437 // Append the file path information from the boot option and return the fully expanded device path.
439 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
440 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
443 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
447 // First delete the matched instance.
449 TempNewDevicePath
= CachedDevicePath
;
450 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
451 FreePool (TempNewDevicePath
);
454 // Second, append the remaining path after the matched instance
456 TempNewDevicePath
= CachedDevicePath
;
457 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
458 FreePool (TempNewDevicePath
);
460 // Save the matching Device Path so we don't need to do a connect all next time
462 Status
= gRT
->SetVariable (
464 &mHdBootVariablePrivateGuid
,
465 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
466 GetDevicePathSize (CachedDevicePath
),
472 FreePool (CachedDevicePath
);
473 return FullDevicePath
;
478 // If we get here we fail to find or 'HDDP' not exist, and now we need
479 // to search all devices in the system for a matched partition
481 BdsLibConnectAllDriversToAllControllers ();
482 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
483 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
485 // If there was an error or there are no device handles that support
486 // the BLOCK_IO Protocol, then return.
491 // Loop through all the device handles that support the BLOCK_IO Protocol
493 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
495 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
496 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
500 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
502 // Find the matched partition device path
504 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
505 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
508 // Save the matched partition device path in 'HDDP' variable
510 if (CachedDevicePath
!= NULL
) {
512 // Save the matched partition device path as first instance of 'HDDP' variable
514 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
515 TempNewDevicePath
= CachedDevicePath
;
516 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
517 FreePool(TempNewDevicePath
);
519 TempNewDevicePath
= CachedDevicePath
;
520 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
521 FreePool(TempNewDevicePath
);
523 TempNewDevicePath
= CachedDevicePath
;
524 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
525 FreePool(TempNewDevicePath
);
528 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
529 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
532 ASSERT (CachedDevicePath
!= NULL
);
533 TempNewDevicePath
= CachedDevicePath
;
534 while (!IsDevicePathEnd (TempNewDevicePath
)) {
535 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
537 // Parse one instance
539 while (!IsDevicePathEndType (TempNewDevicePath
)) {
540 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
544 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
546 if (InstanceNum
>= 12) {
547 SetDevicePathEndNode (TempNewDevicePath
);
552 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
556 // Save the matching Device Path so we don't need to do a connect all next time
558 Status
= gRT
->SetVariable (
560 &mHdBootVariablePrivateGuid
,
561 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
562 GetDevicePathSize (CachedDevicePath
),
570 FreePool (CachedDevicePath
);
571 if (BlockIoBuffer
!= NULL
) {
572 FreePool (BlockIoBuffer
);
574 return FullDevicePath
;
578 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
579 instances, has the same partition node with HardDriveDevicePath device path
581 @param BlockIoDevicePath Multi device path instances which need to check
582 @param HardDriveDevicePath A device path which starts with a hard drive media
585 @retval TRUE There is a matched device path instance.
586 @retval FALSE There is no matched device path instance.
591 MatchPartitionDevicePathNode (
592 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
593 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
596 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
597 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
599 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
601 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
606 // Make PreviousDevicePath == the device path node before the end node
608 DevicePath
= BlockIoDevicePath
;
609 BlockIoHdDevicePathNode
= NULL
;
612 // find the partition device path node
614 while (!IsDevicePathEnd (DevicePath
)) {
615 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
616 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
618 BlockIoHdDevicePathNode
= DevicePath
;
622 DevicePath
= NextDevicePathNode (DevicePath
);
625 if (BlockIoHdDevicePathNode
== NULL
) {
629 // See if the harddrive device path in blockio matches the orig Hard Drive Node
631 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
635 // Check for the match
637 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
638 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
639 switch (TmpHdPath
->SignatureType
) {
640 case SIGNATURE_TYPE_GUID
:
641 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
643 case SIGNATURE_TYPE_MBR
:
644 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
656 Delete the boot option associated with the handle passed in.
658 @param Handle The handle which present the device path to create
661 @retval EFI_SUCCESS Delete the boot option success
662 @retval EFI_NOT_FOUND If the Device Path is not found in the system
663 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
664 @retval Other Error return value from SetVariable()
668 BdsLibDeleteOptionFromHandle (
673 UINT8
*BootOptionVar
;
675 UINTN BootOptionSize
;
678 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
679 UINTN DevicePathSize
;
680 UINTN OptionDevicePathSize
;
681 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
682 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
685 Status
= EFI_SUCCESS
;
690 // Check "BootOrder" variable, if no, means there is no any boot order.
692 BootOrder
= BdsLibGetVariableAndSize (
694 &gEfiGlobalVariableGuid
,
697 if (BootOrder
== NULL
) {
698 return EFI_NOT_FOUND
;
702 // Convert device handle to device path protocol instance
704 DevicePath
= DevicePathFromHandle (Handle
);
705 if (DevicePath
== NULL
) {
706 return EFI_NOT_FOUND
;
708 DevicePathSize
= GetDevicePathSize (DevicePath
);
711 // Loop all boot order variable and find the matching device path
714 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
715 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
716 BootOptionVar
= BdsLibGetVariableAndSize (
718 &gEfiGlobalVariableGuid
,
722 if (BootOptionVar
== NULL
) {
723 FreePool (BootOrder
);
724 return EFI_OUT_OF_RESOURCES
;
727 TempPtr
= BootOptionVar
;
728 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
729 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
730 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
731 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
734 // Check whether the device path match
736 if ((OptionDevicePathSize
== DevicePathSize
) &&
737 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
738 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
739 FreePool (BootOptionVar
);
743 FreePool (BootOptionVar
);
748 // Adjust number of boot option for "BootOrder" variable.
750 Status
= gRT
->SetVariable (
752 &gEfiGlobalVariableGuid
,
753 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
758 FreePool (BootOrder
);
765 Delete all invalid EFI boot options.
767 @retval EFI_SUCCESS Delete all invalid boot option success
768 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
769 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
770 @retval Other Error return value from SetVariable()
774 BdsDeleteAllInvalidEfiBootOption (
779 UINT8
*BootOptionVar
;
781 UINTN BootOptionSize
;
785 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
786 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
790 Status
= EFI_SUCCESS
;
795 // Check "BootOrder" variable firstly, this variable hold the number of boot options
797 BootOrder
= BdsLibGetVariableAndSize (
799 &gEfiGlobalVariableGuid
,
802 if (NULL
== BootOrder
) {
803 return EFI_NOT_FOUND
;
807 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
808 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
809 BootOptionVar
= BdsLibGetVariableAndSize (
811 &gEfiGlobalVariableGuid
,
814 if (NULL
== BootOptionVar
) {
815 FreePool (BootOrder
);
816 return EFI_OUT_OF_RESOURCES
;
819 TempPtr
= BootOptionVar
;
820 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
821 Description
= (CHAR16
*) TempPtr
;
822 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
823 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
826 // Skip legacy boot option (BBS boot device)
828 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
829 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
830 FreePool (BootOptionVar
);
835 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
837 // Delete this invalid boot option "Boot####"
839 Status
= gRT
->SetVariable (
841 &gEfiGlobalVariableGuid
,
842 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
847 // Mark this boot option in boot order as deleted
849 BootOrder
[Index
] = 0xffff;
852 FreePool (BootOptionVar
);
857 // Adjust boot order array
860 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
861 if (BootOrder
[Index
] != 0xffff) {
862 BootOrder
[Index2
] = BootOrder
[Index
];
866 Status
= gRT
->SetVariable (
868 &gEfiGlobalVariableGuid
,
869 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
870 Index2
* sizeof (UINT16
),
874 FreePool (BootOrder
);
881 For EFI boot option, BDS separate them as six types:
882 1. Network - The boot option points to the SimpleNetworkProtocol device.
883 Bds will try to automatically create this type boot option when enumerate.
884 2. Shell - The boot option points to internal flash shell.
885 Bds will try to automatically create this type boot option when enumerate.
886 3. Removable BlockIo - The boot option only points to the removable media
887 device, like USB flash disk, DVD, Floppy etc.
888 These device should contain a *removable* blockIo
889 protocol in their device handle.
890 Bds will try to automatically create this type boot option
892 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
894 These device should contain a *fixed* blockIo
895 protocol in their device handle.
896 BDS will skip fixed blockIo devices, and NOT
897 automatically create boot option for them. But BDS
898 will help to delete those fixed blockIo boot option,
899 whose description rule conflict with other auto-created
901 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
902 has SimpleFileSystem Protocol, but has no blockio
903 protocol. These devices do not offer blockIo
904 protocol, but BDS still can get the
905 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
907 6. File - The boot option points to a file. These boot options are usually
908 created by user manually or OS loader. BDS will not delete or modify
911 This function will enumerate all possible boot device in the system, and
912 automatically create boot options for Network, Shell, Removable BlockIo,
913 and Non-BlockIo Simplefile devices.
914 It will only execute once of every boot.
916 @param BdsBootOptionList The header of the link list which indexed all
919 @retval EFI_SUCCESS Finished all the boot device enumerate and create
920 the boot option base on that boot device
922 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
926 BdsLibEnumerateAllBootOption (
927 IN OUT LIST_ENTRY
*BdsBootOptionList
936 UINT16 NonBlockNumber
;
937 UINTN NumberBlockIoHandles
;
938 EFI_HANDLE
*BlockIoHandles
;
939 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
941 UINTN NumberSimpleNetworkHandles
;
942 EFI_HANDLE
*SimpleNetworkHandles
;
944 EFI_HANDLE
*FvHandleBuffer
;
945 EFI_FV_FILETYPE Type
;
947 EFI_FV_FILE_ATTRIBUTES Attributes
;
948 UINT32 AuthenticationStatus
;
949 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
950 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
951 UINTN DevicePathType
;
953 EFI_HANDLE
*FileSystemHandles
;
954 UINTN NumberFileSystemHandles
;
956 EFI_IMAGE_DOS_HEADER DosHeader
;
957 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
958 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
965 ZeroMem (Buffer
, sizeof (Buffer
));
968 // If the boot device enumerate happened, just get the boot
969 // device from the boot order variable
971 if (mEnumBootDevice
) {
972 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
977 // Notes: this dirty code is to get the legacy boot option from the
978 // BBS table and create to variable as the EFI boot option, it should
979 // be removed after the CSM can provide legacy boot option directly
981 REFRESH_LEGACY_BOOT_OPTIONS
;
984 // Delete invalid boot option
986 BdsDeleteAllInvalidEfiBootOption ();
989 // Parse removable media
991 gBS
->LocateHandleBuffer (
993 &gEfiBlockIoProtocolGuid
,
995 &NumberBlockIoHandles
,
999 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1000 Status
= gBS
->HandleProtocol (
1001 BlockIoHandles
[Index
],
1002 &gEfiBlockIoProtocolGuid
,
1005 if (!EFI_ERROR (Status
)) {
1006 if (!BlkIo
->Media
->RemovableMedia
) {
1008 // skip the non-removable block devices
1013 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1014 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1016 switch (DevicePathType
) {
1017 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1018 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", FloppyNumber
);
1019 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1024 // Assume a removable SATA device should be the DVD/CD device
1026 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1027 case BDS_EFI_MESSAGE_SATA_BOOT
:
1028 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", CdromNumber
);
1029 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1033 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1034 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", UsbNumber
);
1035 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1039 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1040 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", ScsiNumber
);
1041 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1045 case BDS_EFI_MESSAGE_MISC_BOOT
:
1046 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", MiscNumber
);
1047 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1056 if (NumberBlockIoHandles
!= 0) {
1057 FreePool (BlockIoHandles
);
1061 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1064 gBS
->LocateHandleBuffer (
1066 &gEfiSimpleFileSystemProtocolGuid
,
1068 &NumberFileSystemHandles
,
1071 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1072 Status
= gBS
->HandleProtocol (
1073 FileSystemHandles
[Index
],
1074 &gEfiBlockIoProtocolGuid
,
1077 if (!EFI_ERROR (Status
)) {
1079 // Skip if the file system handle supports a BlkIo protocol,
1085 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1086 // machinename is ia32, ia64, x64, ...
1088 Hdr
.Union
= &HdrData
;
1090 Status
= BdsLibGetImageHeader (
1091 FileSystemHandles
[Index
],
1092 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1096 if (!EFI_ERROR (Status
) &&
1097 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1098 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1104 // No such file or the file is not a EFI application, delete this boot option
1106 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1108 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1109 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1114 if (NumberFileSystemHandles
!= 0) {
1115 FreePool (FileSystemHandles
);
1119 // Parse Network Boot Device
1121 gBS
->LocateHandleBuffer (
1123 &gEfiSimpleNetworkProtocolGuid
,
1125 &NumberSimpleNetworkHandles
,
1126 &SimpleNetworkHandles
1129 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1130 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%d", Index
);
1131 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1134 if (NumberSimpleNetworkHandles
!= 0) {
1135 FreePool (SimpleNetworkHandles
);
1139 // Check if we have on flash shell
1141 gBS
->LocateHandleBuffer (
1143 &gEfiFirmwareVolume2ProtocolGuid
,
1148 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1149 gBS
->HandleProtocol (
1150 FvHandleBuffer
[Index
],
1151 &gEfiFirmwareVolume2ProtocolGuid
,
1155 Status
= Fv
->ReadFile (
1157 PcdGetPtr(PcdShellFile
),
1162 &AuthenticationStatus
1164 if (EFI_ERROR (Status
)) {
1166 // Skip if no shell file in the FV
1171 // Build the shell boot option
1173 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1176 if (FvHandleCount
!= 0) {
1177 FreePool (FvHandleBuffer
);
1180 // Make sure every boot only have one time
1181 // boot device enumerate
1183 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1184 mEnumBootDevice
= TRUE
;
1190 Build the boot option with the handle parsed in
1192 @param Handle The handle which present the device path to create
1194 @param BdsBootOptionList The header of the link list which indexed all
1195 current boot options
1196 @param String The description of the boot option.
1201 BdsLibBuildOptionFromHandle (
1202 IN EFI_HANDLE Handle
,
1203 IN LIST_ENTRY
*BdsBootOptionList
,
1207 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1209 DevicePath
= DevicePathFromHandle (Handle
);
1212 // Create and register new boot option
1214 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1219 Build the on flash shell boot option with the handle parsed in.
1221 @param Handle The handle which present the device path to create
1222 on flash shell boot option
1223 @param BdsBootOptionList The header of the link list which indexed all
1224 current boot options
1229 BdsLibBuildOptionFromShell (
1230 IN EFI_HANDLE Handle
,
1231 IN OUT LIST_ENTRY
*BdsBootOptionList
1234 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1235 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1237 DevicePath
= DevicePathFromHandle (Handle
);
1240 // Build the shell device path
1242 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1244 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1247 // Create and register the shell boot option
1249 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1254 Boot from the UEFI spec defined "BootNext" variable.
1266 BDS_COMMON_OPTION
*BootOption
;
1267 LIST_ENTRY TempList
;
1272 // Init the boot option name buffer and temp link list
1274 InitializeListHead (&TempList
);
1275 ZeroMem (Buffer
, sizeof (Buffer
));
1277 BootNext
= BdsLibGetVariableAndSize (
1279 &gEfiGlobalVariableGuid
,
1284 // Clear the boot next variable first
1286 if (BootNext
!= NULL
) {
1289 &gEfiGlobalVariableGuid
,
1290 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1296 // Start to build the boot option and try to boot
1298 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1299 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1300 ASSERT (BootOption
!= NULL
);
1301 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1302 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1308 Return the bootable media handle.
1309 First, check the device is connected
1310 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1311 Third, detect the the default boot file in the Media, and return the removable Media handle.
1313 @param DevicePath Device Path to a bootable device
1315 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1320 BdsLibGetBootableHandle (
1321 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1325 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1326 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1328 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1330 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1333 EFI_HANDLE ReturnHandle
;
1334 EFI_HANDLE
*SimpleFileSystemHandles
;
1336 UINTN NumberSimpleFileSystemHandles
;
1338 EFI_IMAGE_DOS_HEADER DosHeader
;
1339 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1340 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1342 UpdatedDevicePath
= DevicePath
;
1345 // Check whether the device is connected
1347 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1348 if (EFI_ERROR (Status
)) {
1350 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1352 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1353 if (EFI_ERROR (Status
)) {
1355 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1357 UpdatedDevicePath
= DevicePath
;
1358 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1359 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1363 // Get BlockIo protocol and check removable attribute
1365 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1367 // Issue a dummy read to the device to check for media change.
1368 // When the removable media is changed, any Block IO read/write will
1369 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1370 // returned. After the Block IO protocol is reinstalled, subsequent
1371 // Block IO read/write will success.
1373 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1374 if (Buffer
!= NULL
) {
1375 BlockIo
->ReadBlocks (
1377 BlockIo
->Media
->MediaId
,
1379 BlockIo
->Media
->BlockSize
,
1387 // Detect the the default boot file from removable Media
1391 // 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
1392 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1394 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1395 ASSERT (DupDevicePath
!= NULL
);
1397 UpdatedDevicePath
= DupDevicePath
;
1398 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1400 // 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
1401 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1403 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1404 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1406 // Remove the usb node, let the device path only point to PCI node
1408 SetDevicePathEndNode (UpdatedDevicePath
);
1409 UpdatedDevicePath
= DupDevicePath
;
1411 UpdatedDevicePath
= DevicePath
;
1415 // Get the device path size of boot option
1417 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1418 ReturnHandle
= NULL
;
1419 gBS
->LocateHandleBuffer (
1421 &gEfiSimpleFileSystemProtocolGuid
,
1423 &NumberSimpleFileSystemHandles
,
1424 &SimpleFileSystemHandles
1426 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1428 // Get the device path size of SimpleFileSystem handle
1430 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1431 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1433 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1435 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1437 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1438 // machinename is ia32, ia64, x64, ...
1440 Hdr
.Union
= &HdrData
;
1441 Status
= BdsLibGetImageHeader (
1442 SimpleFileSystemHandles
[Index
],
1443 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1447 if (!EFI_ERROR (Status
) &&
1448 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1449 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1450 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1456 FreePool(DupDevicePath
);
1458 if (SimpleFileSystemHandles
!= NULL
) {
1459 FreePool(SimpleFileSystemHandles
);
1462 return ReturnHandle
;
1466 Check to see if the network cable is plugged in. If the DevicePath is not
1467 connected it will be connected.
1469 @param DevicePath Device Path to check
1471 @retval TRUE DevicePath points to an Network that is connected
1472 @retval FALSE DevicePath does not point to a bootable network
1476 BdsLibNetworkBootWithMediaPresent (
1477 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1481 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1483 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1484 BOOLEAN MediaPresent
;
1486 MediaPresent
= FALSE
;
1488 UpdatedDevicePath
= DevicePath
;
1489 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1490 if (EFI_ERROR (Status
)) {
1492 // Device not present so see if we need to connect it
1494 Status
= BdsLibConnectDevicePath (DevicePath
);
1495 if (!EFI_ERROR (Status
)) {
1497 // This one should work after we did the connect
1499 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1503 if (!EFI_ERROR (Status
)) {
1504 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1505 if (!EFI_ERROR (Status
)) {
1506 if (Snp
->Mode
->MediaPresentSupported
) {
1507 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1509 // In case some one else is using the SNP check to see if it's connected
1511 MediaPresent
= Snp
->Mode
->MediaPresent
;
1514 // No one is using SNP so we need to Start and Initialize so
1515 // MediaPresent will be valid.
1517 Status
= Snp
->Start (Snp
);
1518 if (!EFI_ERROR (Status
)) {
1519 Status
= Snp
->Initialize (Snp
, 0, 0);
1520 if (!EFI_ERROR (Status
)) {
1521 MediaPresent
= Snp
->Mode
->MediaPresent
;
1522 Snp
->Shutdown (Snp
);
1528 MediaPresent
= TRUE
;
1533 return MediaPresent
;
1537 For a bootable Device path, return its boot type.
1539 @param DevicePath The bootable device Path to check
1541 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1542 which subtype is MEDIA_HARDDRIVE_DP
1543 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1544 which subtype is MEDIA_CDROM_DP
1545 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1546 which HID is floppy device.
1547 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1548 and its last device path node's subtype is MSG_ATAPI_DP.
1549 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1550 and its last device path node's subtype is MSG_SCSI_DP.
1551 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1552 and its last device path node's subtype is MSG_USB_DP.
1553 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1554 its last device path node point to a message device path node.
1555 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1556 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1561 BdsGetBootTypeFromDevicePath (
1562 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1565 ACPI_HID_DEVICE_PATH
*Acpi
;
1566 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1567 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1570 if (NULL
== DevicePath
) {
1571 return BDS_EFI_UNSUPPORT
;
1574 TempDevicePath
= DevicePath
;
1576 while (!IsDevicePathEndType (TempDevicePath
)) {
1577 switch (DevicePathType (TempDevicePath
)) {
1578 case BBS_DEVICE_PATH
:
1579 return BDS_LEGACY_BBS_BOOT
;
1580 case MEDIA_DEVICE_PATH
:
1581 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1582 return BDS_EFI_MEDIA_HD_BOOT
;
1583 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1584 return BDS_EFI_MEDIA_CDROM_BOOT
;
1587 case ACPI_DEVICE_PATH
:
1588 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1589 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1590 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1593 case MESSAGING_DEVICE_PATH
:
1595 // Get the last device path node
1597 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1598 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1600 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1603 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1606 // if the device path not only point to driver device, it is not a messaging device path,
1608 if (!IsDevicePathEndType (LastDeviceNode
)) {
1612 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1613 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1614 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1615 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1616 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1617 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1618 } else if (DevicePathSubType(TempDevicePath
) == MSG_SATA_DP
) {
1619 return BDS_EFI_MESSAGE_SATA_BOOT
;
1620 } else if (DevicePathSubType(TempDevicePath
) == MSG_MAC_ADDR_DP
) {
1621 return BDS_EFI_MESSAGE_MAC_BOOT
;
1623 return BDS_EFI_MESSAGE_MISC_BOOT
;
1627 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1630 return BDS_EFI_UNSUPPORT
;
1634 Check whether the Device path in a boot option point to a valid bootable device,
1635 And if CheckMedia is true, check the device is ready to boot now.
1637 @param DevPath the Device path in a boot option
1638 @param CheckMedia if true, check the device is ready to boot now.
1640 @retval TRUE the Device path is valid
1641 @retval FALSE the Device path is invalid .
1646 BdsLibIsValidEFIBootOptDevicePath (
1647 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1648 IN BOOLEAN CheckMedia
1651 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
1655 Check whether the Device path in a boot option point to a valid bootable device,
1656 And if CheckMedia is true, check the device is ready to boot now.
1657 If Description is not NULL and the device path point to a fixed BlockIo
1658 device, check the description whether conflict with other auto-created
1661 @param DevPath the Device path in a boot option
1662 @param CheckMedia if true, check the device is ready to boot now.
1663 @param Description the description in a boot option
1665 @retval TRUE the Device path is valid
1666 @retval FALSE the Device path is invalid .
1671 BdsLibIsValidEFIBootOptDevicePathExt (
1672 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1673 IN BOOLEAN CheckMedia
,
1674 IN CHAR16
*Description
1679 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1680 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1681 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1683 TempDevicePath
= DevPath
;
1684 LastDeviceNode
= DevPath
;
1687 // Check if it's a valid boot option for network boot device
1688 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1689 // there is the network card there.
1691 Status
= gBS
->LocateDevicePath (
1692 &gEfiSimpleNetworkProtocolGuid
,
1696 if (EFI_ERROR (Status
)) {
1698 // Device not present so see if we need to connect it
1700 TempDevicePath
= DevPath
;
1701 BdsLibConnectDevicePath (TempDevicePath
);
1702 Status
= gBS
->LocateDevicePath (
1703 &gEfiSimpleNetworkProtocolGuid
,
1709 if (!EFI_ERROR (Status
)) {
1712 // Test if it is ready to boot now
1714 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1723 // If the boot option point to a file, it is a valid EFI boot option,
1724 // and assume it is ready to boot now
1726 while (!IsDevicePathEnd (TempDevicePath
)) {
1727 LastDeviceNode
= TempDevicePath
;
1728 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1730 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1731 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1736 // Check if it's a valid boot option for internal Shell
1738 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1740 // If the boot option point to Internal FV shell, make sure it is valid
1742 TempDevicePath
= DevPath
;
1743 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, PcdGetPtr(PcdShellFile
));
1744 if (Status
== EFI_ALREADY_STARTED
) {
1747 if (Status
== EFI_SUCCESS
) {
1748 FreePool (TempDevicePath
);
1755 // If the boot option point to a blockIO device:
1756 // if it is a removable blockIo device, it is valid.
1757 // if it is a fixed blockIo device, check its description confliction.
1759 TempDevicePath
= DevPath
;
1760 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1761 if (EFI_ERROR (Status
)) {
1763 // Device not present so see if we need to connect it
1765 Status
= BdsLibConnectDevicePath (DevPath
);
1766 if (!EFI_ERROR (Status
)) {
1768 // Try again to get the Block Io protocol after we did the connect
1770 TempDevicePath
= DevPath
;
1771 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1775 if (!EFI_ERROR (Status
)) {
1776 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1777 if (!EFI_ERROR (Status
)) {
1780 // Test if it is ready to boot now
1782 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1791 // 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,
1793 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1794 if (!EFI_ERROR (Status
)) {
1797 // Test if it is ready to boot now
1799 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1813 According to a file guild, check a Fv file device path is valid. If it is invalid,
1814 try to return the valid device path.
1815 FV address maybe changes for memory layout adjust from time to time, use this function
1816 could promise the Fv file device path is right.
1818 @param DevicePath on input, the Fv file device path need to check on
1819 output, the updated valid Fv file device path
1820 @param FileGuid the Fv file guild
1822 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1824 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1826 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1828 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1829 and return the updated device path in DevicePath
1834 BdsLibUpdateFvFileDevicePath (
1835 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1836 IN EFI_GUID
*FileGuid
1839 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1840 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1842 EFI_GUID
*GuidPoint
;
1844 UINTN FvHandleCount
;
1845 EFI_HANDLE
*FvHandleBuffer
;
1846 EFI_FV_FILETYPE Type
;
1848 EFI_FV_FILE_ATTRIBUTES Attributes
;
1849 UINT32 AuthenticationStatus
;
1851 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1852 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1853 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1854 EFI_HANDLE FoundFvHandle
;
1855 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1857 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1858 return EFI_INVALID_PARAMETER
;
1860 if (FileGuid
== NULL
) {
1861 return EFI_INVALID_PARAMETER
;
1865 // Check whether the device path point to the default the input Fv file
1867 TempDevicePath
= *DevicePath
;
1868 LastDeviceNode
= TempDevicePath
;
1869 while (!IsDevicePathEnd (TempDevicePath
)) {
1870 LastDeviceNode
= TempDevicePath
;
1871 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1873 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1874 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1876 if (GuidPoint
== NULL
) {
1878 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1880 return EFI_UNSUPPORTED
;
1882 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1884 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1886 return EFI_UNSUPPORTED
;
1890 // Check whether the input Fv file device path is valid
1892 TempDevicePath
= *DevicePath
;
1893 FoundFvHandle
= NULL
;
1894 Status
= gBS
->LocateDevicePath (
1895 &gEfiFirmwareVolume2ProtocolGuid
,
1899 if (!EFI_ERROR (Status
)) {
1900 Status
= gBS
->HandleProtocol (
1902 &gEfiFirmwareVolume2ProtocolGuid
,
1905 if (!EFI_ERROR (Status
)) {
1907 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1909 Status
= Fv
->ReadFile (
1916 &AuthenticationStatus
1918 if (!EFI_ERROR (Status
)) {
1919 return EFI_ALREADY_STARTED
;
1925 // Look for the input wanted FV file in current FV
1926 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1929 FoundFvHandle
= NULL
;
1930 Status
= gBS
->HandleProtocol (
1932 &gEfiLoadedImageProtocolGuid
,
1933 (VOID
**) &LoadedImage
1935 if (!EFI_ERROR (Status
)) {
1936 Status
= gBS
->HandleProtocol (
1937 LoadedImage
->DeviceHandle
,
1938 &gEfiFirmwareVolume2ProtocolGuid
,
1941 if (!EFI_ERROR (Status
)) {
1942 Status
= Fv
->ReadFile (
1949 &AuthenticationStatus
1951 if (!EFI_ERROR (Status
)) {
1953 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1958 // Second, if fail to find, try to enumerate all FV
1961 FvHandleBuffer
= NULL
;
1962 gBS
->LocateHandleBuffer (
1964 &gEfiFirmwareVolume2ProtocolGuid
,
1969 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1970 gBS
->HandleProtocol (
1971 FvHandleBuffer
[Index
],
1972 &gEfiFirmwareVolume2ProtocolGuid
,
1976 Status
= Fv
->ReadFile (
1983 &AuthenticationStatus
1985 if (EFI_ERROR (Status
)) {
1987 // Skip if input Fv file not in the FV
1992 FoundFvHandle
= FvHandleBuffer
[Index
];
1996 if (FvHandleBuffer
!= NULL
) {
1997 FreePool (FvHandleBuffer
);
2003 // Build the shell device path
2005 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2006 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2007 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2008 *DevicePath
= NewDevicePath
;
2011 return EFI_NOT_FOUND
;