2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
17 BOOLEAN mEnumBootDevice
= FALSE
;
20 /// This GUID is used for an EFI Variable that stores the front device pathes
21 /// for a partial device path that starts with the HD node.
23 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 Boot the legacy system with the boot option
30 @param Option The legacy boot option which have BBS device path
32 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
34 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
39 IN BDS_COMMON_OPTION
*Option
43 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
45 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
46 if (EFI_ERROR (Status
)) {
48 // If no LegacyBios protocol we do not support legacy boot
50 return EFI_UNSUPPORTED
;
53 // Notes: if we separate the int 19, then we don't need to refresh BBS
55 BdsRefreshBbsTableForBoot (Option
);
58 // Write boot to OS performance data to a file
61 WriteBootToOsPerformanceData ();
64 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
65 return LegacyBios
->LegacyBoot (
67 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
68 Option
->LoadOptionsSize
,
74 Internal function to check if the input boot option is a valid EFI NV Boot####.
76 @param OptionToCheck Boot option to be checked.
78 @retval TRUE This boot option matches a valid EFI NV Boot####.
84 IsBootOptionValidNVVarialbe (
85 IN BDS_COMMON_OPTION
*OptionToCheck
89 BDS_COMMON_OPTION
*BootOption
;
91 CHAR16 OptionName
[20];
95 InitializeListHead (&TempList
);
96 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
98 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
99 if (BootOption
== NULL
) {
104 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
105 // valid EFI NV Boot####.
107 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
108 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
113 FreePool (BootOption
);
118 Process the boot option follow the UEFI specification and
119 special treat the legacy boot option with BBS_DEVICE_PATH.
121 @param Option The boot option need to be processed
122 @param DevicePath The device path which describe where to load the
123 boot image or the legacy BBS device path to boot
125 @param ExitDataSize The size of exit data.
126 @param ExitData Data returned when Boot image failed.
128 @retval EFI_SUCCESS Boot from the input boot option successfully.
129 @retval EFI_NOT_FOUND If the Device Path is not found in the system
134 BdsLibBootViaBootOption (
135 IN BDS_COMMON_OPTION
*Option
,
136 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
137 OUT UINTN
*ExitDataSize
,
138 OUT CHAR16
**ExitData OPTIONAL
143 EFI_HANDLE ImageHandle
;
144 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
145 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
146 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
147 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
148 LIST_ENTRY TempBootLists
;
149 EFI_SECURITY_ARCH_PROTOCOL
*SecurityProtocol
;
152 // Record the performance data for End of BDS
154 PERF_END (0, "BDS", NULL
, 0);
160 // Notes: put EFI64 ROM Shadow Solution
162 EFI64_SHADOW_ALL_LEGACY_ROM ();
165 // Notes: this code can be remove after the s3 script table
166 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
167 // EVT_SIGNAL_LEGACY_BOOT
169 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
170 if (!EFI_ERROR (Status
)) {
171 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
174 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
177 WorkingDevicePath
= NULL
;
178 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
179 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
180 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
181 (HARDDRIVE_DEVICE_PATH
*)DevicePath
183 if (WorkingDevicePath
!= NULL
) {
184 DevicePath
= WorkingDevicePath
;
188 // Signal the EVT_SIGNAL_READY_TO_BOOT event
190 EfiSignalEventReadyToBoot();
196 if (IsBootOptionValidNVVarialbe (Option
)) {
198 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
199 // In this case, "BootCurrent" is not created.
200 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
204 &gEfiGlobalVariableGuid
,
205 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
211 ASSERT (Option
->DevicePath
!= NULL
);
212 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
213 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
216 // Check to see if we should legacy BOOT. If yes then do the legacy boot
218 return BdsLibDoLegacyBoot (Option
);
222 // If the boot option point to Internal FV shell, make sure it is valid
224 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
225 if (!EFI_ERROR(Status
)) {
226 if (Option
->DevicePath
!= NULL
) {
227 FreePool(Option
->DevicePath
);
229 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
230 ASSERT(Option
->DevicePath
!= NULL
);
231 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
233 // Update the shell boot option
235 InitializeListHead (&TempBootLists
);
236 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
239 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
241 FreePool (DevicePath
);
242 DevicePath
= Option
->DevicePath
;
246 // Measure GPT Table by SAP protocol.
248 Status
= gBS
->LocateProtocol (
249 &gEfiSecurityArchProtocolGuid
,
251 (VOID
**) &SecurityProtocol
253 if (!EFI_ERROR (Status
)) {
254 Status
= SecurityProtocol
->FileAuthenticationState (SecurityProtocol
, 0, DevicePath
);
257 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
259 Status
= gBS
->LoadImage (
269 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
270 // and load the image according to the default boot behavior for removable device.
272 if (EFI_ERROR (Status
)) {
274 // check if there is a bootable removable media could be found in this device path ,
275 // and get the bootable media handle
277 Handle
= BdsLibGetBootableHandle(DevicePath
);
278 if (Handle
== NULL
) {
282 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
283 // machinename is ia32, ia64, x64, ...
285 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
286 if (FilePath
!= NULL
) {
287 Status
= gBS
->LoadImage (
295 if (EFI_ERROR (Status
)) {
297 // The DevicePath failed, and it's not a valid
298 // removable media device.
305 if (EFI_ERROR (Status
)) {
307 // It there is any error from the Boot attempt exit now.
312 // Provide the image with it's load options
314 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
315 ASSERT_EFI_ERROR (Status
);
317 if (Option
->LoadOptionsSize
!= 0) {
318 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
319 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
322 // Before calling the image, enable the Watchdog Timer for
323 // the 5 Minute period
325 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
327 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
328 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
331 // Clear the Watchdog Timer after the image returns
333 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
337 // Clear Boot Current
341 &gEfiGlobalVariableGuid
,
342 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
352 Expand a device path that starts with a hard drive media device path node to be a
353 full device path that includes the full hardware path to the device. We need
354 to do this so it can be booted. As an optimization the front match (the part point
355 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
356 so a connect all is not required on every boot. All successful history device path
357 which point to partition node (the front part) will be saved.
359 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
360 drive media device path.
361 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
365 EFI_DEVICE_PATH_PROTOCOL
*
367 BdsExpandPartitionPartialDevicePathToFull (
368 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
372 UINTN BlockIoHandleCount
;
373 EFI_HANDLE
*BlockIoBuffer
;
374 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
375 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
376 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
379 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
380 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
381 UINTN CachedDevicePathSize
;
384 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
387 FullDevicePath
= NULL
;
389 // Check if there is prestore 'HDDP' variable.
390 // If exist, search the front path which point to partition node in the variable instants.
391 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
393 CachedDevicePath
= BdsLibGetVariableAndSize (
395 &mHdBootVariablePrivateGuid
,
396 &CachedDevicePathSize
399 if (CachedDevicePath
!= NULL
) {
400 TempNewDevicePath
= CachedDevicePath
;
405 // Check every instance of the variable
406 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
407 // partial partition boot option. Second, check whether the instance could be connected.
409 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
410 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
412 // Connect the device path instance, the device path point to hard drive media device path node
413 // e.g. ACPI() /PCI()/ATA()/Partition()
415 Status
= BdsLibConnectDevicePath (Instance
);
416 if (!EFI_ERROR (Status
)) {
422 // Come here means the first instance is not matched
426 } while (TempNewDevicePath
!= NULL
);
430 // Find the matched device path.
431 // Append the file path information from the boot option and return the fully expanded device path.
433 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
434 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
437 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
441 // First delete the matched instance.
443 TempNewDevicePath
= CachedDevicePath
;
444 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
445 FreePool (TempNewDevicePath
);
448 // Second, append the remaining path after the matched instance
450 TempNewDevicePath
= CachedDevicePath
;
451 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
452 FreePool (TempNewDevicePath
);
454 // Save the matching Device Path so we don't need to do a connect all next time
456 Status
= gRT
->SetVariable (
458 &mHdBootVariablePrivateGuid
,
459 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
460 GetDevicePathSize (CachedDevicePath
),
466 FreePool (CachedDevicePath
);
467 return FullDevicePath
;
472 // If we get here we fail to find or 'HDDP' not exist, and now we need
473 // to search all devices in the system for a matched partition
475 BdsLibConnectAllDriversToAllControllers ();
476 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
477 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
479 // If there was an error or there are no device handles that support
480 // the BLOCK_IO Protocol, then return.
485 // Loop through all the device handles that support the BLOCK_IO Protocol
487 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
489 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
490 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
494 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
496 // Find the matched partition device path
498 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
499 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
502 // Save the matched partition device path in 'HDDP' variable
504 if (CachedDevicePath
!= NULL
) {
506 // Save the matched partition device path as first instance of 'HDDP' variable
508 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
509 TempNewDevicePath
= CachedDevicePath
;
510 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
511 FreePool(TempNewDevicePath
);
513 TempNewDevicePath
= CachedDevicePath
;
514 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
515 FreePool(TempNewDevicePath
);
517 TempNewDevicePath
= CachedDevicePath
;
518 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
519 FreePool(TempNewDevicePath
);
522 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
523 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
526 ASSERT (CachedDevicePath
!= NULL
);
527 TempNewDevicePath
= CachedDevicePath
;
528 while (!IsDevicePathEnd (TempNewDevicePath
)) {
529 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
531 // Parse one instance
533 while (!IsDevicePathEndType (TempNewDevicePath
)) {
534 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
538 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
540 if (InstanceNum
>= 12) {
541 SetDevicePathEndNode (TempNewDevicePath
);
546 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
550 // Save the matching Device Path so we don't need to do a connect all next time
552 Status
= gRT
->SetVariable (
554 &mHdBootVariablePrivateGuid
,
555 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
556 GetDevicePathSize (CachedDevicePath
),
564 FreePool (CachedDevicePath
);
565 if (BlockIoBuffer
!= NULL
) {
566 FreePool (BlockIoBuffer
);
568 return FullDevicePath
;
572 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
573 instances, has the same partition node with HardDriveDevicePath device path
575 @param BlockIoDevicePath Multi device path instances which need to check
576 @param HardDriveDevicePath A device path which starts with a hard drive media
579 @retval TRUE There is a matched device path instance.
580 @retval FALSE There is no matched device path instance.
585 MatchPartitionDevicePathNode (
586 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
587 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
590 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
591 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
593 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
595 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
600 // Make PreviousDevicePath == the device path node before the end node
602 DevicePath
= BlockIoDevicePath
;
603 BlockIoHdDevicePathNode
= NULL
;
606 // find the partition device path node
608 while (!IsDevicePathEnd (DevicePath
)) {
609 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
610 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
612 BlockIoHdDevicePathNode
= DevicePath
;
616 DevicePath
= NextDevicePathNode (DevicePath
);
619 if (BlockIoHdDevicePathNode
== NULL
) {
623 // See if the harddrive device path in blockio matches the orig Hard Drive Node
625 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
629 // Check for the match
631 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
632 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
633 switch (TmpHdPath
->SignatureType
) {
634 case SIGNATURE_TYPE_GUID
:
635 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
637 case SIGNATURE_TYPE_MBR
:
638 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
650 Delete the boot option associated with the handle passed in.
652 @param Handle The handle which present the device path to create
655 @retval EFI_SUCCESS Delete the boot option success
656 @retval EFI_NOT_FOUND If the Device Path is not found in the system
657 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
658 @retval Other Error return value from SetVariable()
662 BdsLibDeleteOptionFromHandle (
667 UINT8
*BootOptionVar
;
669 UINTN BootOptionSize
;
672 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
673 UINTN DevicePathSize
;
674 UINTN OptionDevicePathSize
;
675 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
676 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
679 Status
= EFI_SUCCESS
;
684 // Check "BootOrder" variable, if no, means there is no any boot order.
686 BootOrder
= BdsLibGetVariableAndSize (
688 &gEfiGlobalVariableGuid
,
691 if (BootOrder
== NULL
) {
692 return EFI_NOT_FOUND
;
696 // Convert device handle to device path protocol instance
698 DevicePath
= DevicePathFromHandle (Handle
);
699 if (DevicePath
== NULL
) {
700 return EFI_NOT_FOUND
;
702 DevicePathSize
= GetDevicePathSize (DevicePath
);
705 // Loop all boot order variable and find the matching device path
708 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
709 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
710 BootOptionVar
= BdsLibGetVariableAndSize (
712 &gEfiGlobalVariableGuid
,
716 if (BootOptionVar
== NULL
) {
717 FreePool (BootOrder
);
718 return EFI_OUT_OF_RESOURCES
;
721 TempPtr
= BootOptionVar
;
722 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
723 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
724 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
725 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
728 // Check whether the device path match
730 if ((OptionDevicePathSize
== DevicePathSize
) &&
731 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
732 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
733 FreePool (BootOptionVar
);
737 FreePool (BootOptionVar
);
742 // Adjust number of boot option for "BootOrder" variable.
744 Status
= gRT
->SetVariable (
746 &gEfiGlobalVariableGuid
,
747 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
752 FreePool (BootOrder
);
759 Delete all invalid EFI boot options. The probable invalid boot option could
760 be Removable media or Network boot device.
762 @retval EFI_SUCCESS Delete all invalid boot option success
763 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
764 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
765 @retval Other Error return value from SetVariable()
769 BdsDeleteAllInvalidEfiBootOption (
774 UINT8
*BootOptionVar
;
776 UINTN BootOptionSize
;
780 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
781 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
784 Status
= EFI_SUCCESS
;
789 // Check "BootOrder" variable firstly, this variable hold the number of boot options
791 BootOrder
= BdsLibGetVariableAndSize (
793 &gEfiGlobalVariableGuid
,
796 if (NULL
== BootOrder
) {
797 return EFI_NOT_FOUND
;
801 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
802 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
803 BootOptionVar
= BdsLibGetVariableAndSize (
805 &gEfiGlobalVariableGuid
,
808 if (NULL
== BootOptionVar
) {
809 FreePool (BootOrder
);
810 return EFI_OUT_OF_RESOURCES
;
813 TempPtr
= BootOptionVar
;
814 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
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 (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
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 This function will enumerate all possible boot device in the system,
875 it will only execute once of every boot.
877 @param BdsBootOptionList The header of the link list which indexed all
880 @retval EFI_SUCCESS Finished all the boot device enumerate and create
881 the boot option base on that boot device
886 BdsLibEnumerateAllBootOption (
887 IN OUT LIST_ENTRY
*BdsBootOptionList
895 UINT16 NonBlockNumber
;
896 UINTN NumberBlockIoHandles
;
897 EFI_HANDLE
*BlockIoHandles
;
898 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
900 UINTN NumberSimpleNetworkHandles
;
901 EFI_HANDLE
*SimpleNetworkHandles
;
903 EFI_HANDLE
*FvHandleBuffer
;
904 EFI_FV_FILETYPE Type
;
906 EFI_FV_FILE_ATTRIBUTES Attributes
;
907 UINT32 AuthenticationStatus
;
908 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
909 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
910 UINTN DevicePathType
;
912 EFI_HANDLE
*FileSystemHandles
;
913 UINTN NumberFileSystemHandles
;
915 EFI_IMAGE_DOS_HEADER DosHeader
;
916 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
917 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
923 ZeroMem (Buffer
, sizeof (Buffer
));
926 // If the boot device enumerate happened, just get the boot
927 // device from the boot order variable
929 if (mEnumBootDevice
) {
930 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
935 // Notes: this dirty code is to get the legacy boot option from the
936 // BBS table and create to variable as the EFI boot option, it should
937 // be removed after the CSM can provide legacy boot option directly
939 REFRESH_LEGACY_BOOT_OPTIONS
;
942 // Delete invalid boot option
944 BdsDeleteAllInvalidEfiBootOption ();
947 // Parse removable media
949 gBS
->LocateHandleBuffer (
951 &gEfiBlockIoProtocolGuid
,
953 &NumberBlockIoHandles
,
957 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
958 Status
= gBS
->HandleProtocol (
959 BlockIoHandles
[Index
],
960 &gEfiBlockIoProtocolGuid
,
963 if (!EFI_ERROR (Status
)) {
964 if (!BlkIo
->Media
->RemovableMedia
) {
966 // skip the non-removable block devices
971 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
972 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
974 switch (DevicePathType
) {
975 case BDS_EFI_ACPI_FLOPPY_BOOT
:
976 if (FloppyNumber
== 0) {
977 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
979 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
981 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
985 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
986 if (CdromNumber
== 0) {
987 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
989 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
991 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
995 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
996 if (UsbNumber
== 0) {
997 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
999 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
1001 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1005 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1006 if (UsbNumber
== 0) {
1007 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
1009 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
1011 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1015 case BDS_EFI_MESSAGE_MISC_BOOT
:
1016 if (MiscNumber
== 0) {
1017 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
1019 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
1021 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1030 if (NumberBlockIoHandles
!= 0) {
1031 FreePool (BlockIoHandles
);
1035 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1038 gBS
->LocateHandleBuffer (
1040 &gEfiSimpleFileSystemProtocolGuid
,
1042 &NumberFileSystemHandles
,
1045 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1046 Status
= gBS
->HandleProtocol (
1047 FileSystemHandles
[Index
],
1048 &gEfiBlockIoProtocolGuid
,
1051 if (!EFI_ERROR (Status
)) {
1053 // Skip if the file system handle supports a BlkIo protocol,
1059 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1060 // machinename is ia32, ia64, x64, ...
1062 Hdr
.Union
= &HdrData
;
1064 Status
= BdsLibGetImageHeader (
1065 FileSystemHandles
[Index
],
1066 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1070 if (!EFI_ERROR (Status
) &&
1071 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1072 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1078 // No such file or the file is not a EFI application, delete this boot option
1080 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1082 if (NonBlockNumber
== 0) {
1083 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1085 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1087 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1092 if (NumberFileSystemHandles
!= 0) {
1093 FreePool (FileSystemHandles
);
1097 // Parse Network Boot Device
1099 gBS
->LocateHandleBuffer (
1101 &gEfiSimpleNetworkProtocolGuid
,
1103 &NumberSimpleNetworkHandles
,
1104 &SimpleNetworkHandles
1106 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1108 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1110 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1112 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1115 if (NumberSimpleNetworkHandles
!= 0) {
1116 FreePool (SimpleNetworkHandles
);
1120 // Check if we have on flash shell
1122 gBS
->LocateHandleBuffer (
1124 &gEfiFirmwareVolume2ProtocolGuid
,
1129 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1130 gBS
->HandleProtocol (
1131 FvHandleBuffer
[Index
],
1132 &gEfiFirmwareVolume2ProtocolGuid
,
1136 Status
= Fv
->ReadFile (
1143 &AuthenticationStatus
1145 if (EFI_ERROR (Status
)) {
1147 // Skip if no shell file in the FV
1152 // Build the shell boot option
1154 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1157 if (FvHandleCount
!= 0) {
1158 FreePool (FvHandleBuffer
);
1161 // Make sure every boot only have one time
1162 // boot device enumerate
1164 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1165 mEnumBootDevice
= TRUE
;
1171 Build the boot option with the handle parsed in
1173 @param Handle The handle which present the device path to create
1175 @param BdsBootOptionList The header of the link list which indexed all
1176 current boot options
1177 @param String The description of the boot option.
1182 BdsLibBuildOptionFromHandle (
1183 IN EFI_HANDLE Handle
,
1184 IN LIST_ENTRY
*BdsBootOptionList
,
1188 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1190 DevicePath
= DevicePathFromHandle (Handle
);
1193 // Create and register new boot option
1195 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1200 Build the on flash shell boot option with the handle parsed in.
1202 @param Handle The handle which present the device path to create
1203 on flash shell boot option
1204 @param BdsBootOptionList The header of the link list which indexed all
1205 current boot options
1210 BdsLibBuildOptionFromShell (
1211 IN EFI_HANDLE Handle
,
1212 IN OUT LIST_ENTRY
*BdsBootOptionList
1215 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1216 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1218 DevicePath
= DevicePathFromHandle (Handle
);
1221 // Build the shell device path
1223 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1225 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1228 // Create and register the shell boot option
1230 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1235 Boot from the UEFI spec defined "BootNext" variable.
1247 BDS_COMMON_OPTION
*BootOption
;
1248 LIST_ENTRY TempList
;
1253 // Init the boot option name buffer and temp link list
1255 InitializeListHead (&TempList
);
1256 ZeroMem (Buffer
, sizeof (Buffer
));
1258 BootNext
= BdsLibGetVariableAndSize (
1260 &gEfiGlobalVariableGuid
,
1265 // Clear the boot next variable first
1267 if (BootNext
!= NULL
) {
1270 &gEfiGlobalVariableGuid
,
1271 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1277 // Start to build the boot option and try to boot
1279 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1280 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1281 ASSERT (BootOption
!= NULL
);
1282 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1283 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1289 Return the bootable media handle.
1290 First, check the device is connected
1291 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1292 Third, detect the the default boot file in the Media, and return the removable Media handle.
1294 @param DevicePath Device Path to a bootable device
1296 @retval NULL The media on the DevicePath is not bootable
1301 BdsLibGetBootableHandle (
1302 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1306 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1307 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1309 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1311 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1314 EFI_HANDLE ReturnHandle
;
1315 EFI_HANDLE
*SimpleFileSystemHandles
;
1317 UINTN NumberSimpleFileSystemHandles
;
1319 EFI_IMAGE_DOS_HEADER DosHeader
;
1320 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1321 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1323 UpdatedDevicePath
= DevicePath
;
1326 // Check whether the device is connected
1328 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1329 if (EFI_ERROR (Status
)) {
1331 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1333 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1334 if (EFI_ERROR (Status
)) {
1336 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1338 UpdatedDevicePath
= DevicePath
;
1339 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1340 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1344 // Get BlockIo protocol and check removable attribute
1346 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1348 // Issue a dummy read to the device to check for media change.
1349 // When the removable media is changed, any Block IO read/write will
1350 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1351 // returned. After the Block IO protocol is reinstalled, subsequent
1352 // Block IO read/write will success.
1354 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1355 if (Buffer
!= NULL
) {
1356 BlockIo
->ReadBlocks (
1358 BlockIo
->Media
->MediaId
,
1360 BlockIo
->Media
->BlockSize
,
1368 // Detect the the default boot file from removable Media
1372 // 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
1373 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1375 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1376 ASSERT (DupDevicePath
!= NULL
);
1378 UpdatedDevicePath
= DupDevicePath
;
1379 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1381 // 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
1382 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1384 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1385 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1387 // Remove the usb node, let the device path only point to PCI node
1389 SetDevicePathEndNode (UpdatedDevicePath
);
1390 UpdatedDevicePath
= DupDevicePath
;
1392 UpdatedDevicePath
= DevicePath
;
1396 // Get the device path size of boot option
1398 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1399 ReturnHandle
= NULL
;
1400 gBS
->LocateHandleBuffer (
1402 &gEfiSimpleFileSystemProtocolGuid
,
1404 &NumberSimpleFileSystemHandles
,
1405 &SimpleFileSystemHandles
1407 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1409 // Get the device path size of SimpleFileSystem handle
1411 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1412 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1414 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1416 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1418 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1419 // machinename is ia32, ia64, x64, ...
1421 Hdr
.Union
= &HdrData
;
1422 Status
= BdsLibGetImageHeader (
1423 SimpleFileSystemHandles
[Index
],
1424 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1428 if (!EFI_ERROR (Status
) &&
1429 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1430 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1431 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1437 FreePool(DupDevicePath
);
1439 if (SimpleFileSystemHandles
!= NULL
) {
1440 FreePool(SimpleFileSystemHandles
);
1443 return ReturnHandle
;
1447 Check to see if the network cable is plugged in. If the DevicePath is not
1448 connected it will be connected.
1450 @param DevicePath Device Path to check
1452 @retval TRUE DevicePath points to an Network that is connected
1453 @retval FALSE DevicePath does not point to a bootable network
1457 BdsLibNetworkBootWithMediaPresent (
1458 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1462 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1464 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1465 BOOLEAN MediaPresent
;
1467 MediaPresent
= FALSE
;
1469 UpdatedDevicePath
= DevicePath
;
1470 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1471 if (EFI_ERROR (Status
)) {
1473 // Device not present so see if we need to connect it
1475 Status
= BdsLibConnectDevicePath (DevicePath
);
1476 if (!EFI_ERROR (Status
)) {
1478 // This one should work after we did the connect
1480 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1484 if (!EFI_ERROR (Status
)) {
1485 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1486 if (!EFI_ERROR (Status
)) {
1487 if (Snp
->Mode
->MediaPresentSupported
) {
1488 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1490 // In case some one else is using the SNP check to see if it's connected
1492 MediaPresent
= Snp
->Mode
->MediaPresent
;
1495 // No one is using SNP so we need to Start and Initialize so
1496 // MediaPresent will be valid.
1498 Status
= Snp
->Start (Snp
);
1499 if (!EFI_ERROR (Status
)) {
1500 Status
= Snp
->Initialize (Snp
, 0, 0);
1501 if (!EFI_ERROR (Status
)) {
1502 MediaPresent
= Snp
->Mode
->MediaPresent
;
1503 Snp
->Shutdown (Snp
);
1509 MediaPresent
= TRUE
;
1514 return MediaPresent
;
1518 For a bootable Device path, return its boot type.
1520 @param DevicePath The bootable device Path to check
1522 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media device path node, it is media boot type
1523 For the floppy node, handle it as media node
1524 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media device path node, it is media boot type
1525 For the floppy node, handle it as media node
1526 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media device path node, it is media boot type
1527 For the floppy node, handle it as media node
1528 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media device path node, and
1529 its last device path node point to a message device path node, it is
1531 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media device path node, and
1532 its last device path node point to a message device path node, it is
1533 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node, and
1534 its last device path node point to a message device path node, it is
1535 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1536 its last device path node point to a message device path node, it is
1537 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1538 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1543 BdsGetBootTypeFromDevicePath (
1544 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1547 ACPI_HID_DEVICE_PATH
*Acpi
;
1548 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1549 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1552 if (NULL
== DevicePath
) {
1553 return BDS_EFI_UNSUPPORT
;
1556 TempDevicePath
= DevicePath
;
1558 while (!IsDevicePathEndType (TempDevicePath
)) {
1559 switch (DevicePathType (TempDevicePath
)) {
1560 case BBS_DEVICE_PATH
:
1561 return BDS_LEGACY_BBS_BOOT
;
1562 case MEDIA_DEVICE_PATH
:
1563 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1564 return BDS_EFI_MEDIA_HD_BOOT
;
1565 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1566 return BDS_EFI_MEDIA_CDROM_BOOT
;
1569 case ACPI_DEVICE_PATH
:
1570 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1571 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1572 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1575 case MESSAGING_DEVICE_PATH
:
1577 // Get the last device path node
1579 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1580 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1582 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1585 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1588 // if the device path not only point to driver device, it is not a messaging device path,
1590 if (!IsDevicePathEndType (LastDeviceNode
)) {
1594 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1595 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1596 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1597 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1598 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1599 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1601 return BDS_EFI_MESSAGE_MISC_BOOT
;
1605 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1608 return BDS_EFI_UNSUPPORT
;
1612 Check whether the Device path in a boot option point to a valid bootable device,
1613 And if CheckMedia is true, check the device is ready to boot now.
1615 @param DevPath the Device path in a boot option
1616 @param CheckMedia if true, check the device is ready to boot now.
1618 @retval TRUE the Device path is valid
1619 @retval FALSE the Device path is invalid .
1624 BdsLibIsValidEFIBootOptDevicePath (
1625 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1626 IN BOOLEAN CheckMedia
1631 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1632 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1633 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1635 TempDevicePath
= DevPath
;
1636 LastDeviceNode
= DevPath
;
1639 // Check if it's a valid boot option for network boot device
1640 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1641 // there is the network card there.
1643 Status
= gBS
->LocateDevicePath (
1644 &gEfiSimpleNetworkProtocolGuid
,
1648 if (EFI_ERROR (Status
)) {
1650 // Device not present so see if we need to connect it
1652 TempDevicePath
= DevPath
;
1653 BdsLibConnectDevicePath (TempDevicePath
);
1654 Status
= gBS
->LocateDevicePath (
1655 &gEfiSimpleNetworkProtocolGuid
,
1661 if (!EFI_ERROR (Status
)) {
1664 // Test if it is ready to boot now
1666 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1675 // If the boot option point to a file, it is a valid EFI boot option,
1676 // and assume it is ready to boot now
1678 while (!IsDevicePathEnd (TempDevicePath
)) {
1679 LastDeviceNode
= TempDevicePath
;
1680 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1682 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1683 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1688 // Check if it's a valid boot option for internal Shell
1690 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1692 // If the boot option point to Internal FV shell, make sure it is valid
1694 TempDevicePath
= DevPath
;
1695 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1696 if (Status
== EFI_ALREADY_STARTED
) {
1699 if (Status
== EFI_SUCCESS
) {
1700 FreePool (TempDevicePath
);
1707 // If the boot option point to a blockIO device, no matter whether or not it is a removeable device, it is a valid EFI boot option
1709 TempDevicePath
= DevPath
;
1710 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1711 if (EFI_ERROR (Status
)) {
1713 // Device not present so see if we need to connect it
1715 Status
= BdsLibConnectDevicePath (DevPath
);
1716 if (!EFI_ERROR (Status
)) {
1718 // Try again to get the Block Io protocol after we did the connect
1720 TempDevicePath
= DevPath
;
1721 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1725 if (!EFI_ERROR (Status
)) {
1726 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1727 if (!EFI_ERROR (Status
)) {
1730 // Test if it is ready to boot now
1732 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1741 // 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,
1743 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1744 if (!EFI_ERROR (Status
)) {
1747 // Test if it is ready to boot now
1749 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1763 According to a file guild, check a Fv file device path is valid. If it is invalid,
1764 try to return the valid device path.
1765 FV address maybe changes for memory layout adjust from time to time, use this function
1766 could promise the Fv file device path is right.
1768 @param DevicePath on input, the Fv file device path need to check on
1769 output, the updated valid Fv file device path
1770 @param FileGuid the Fv file guild
1772 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1774 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1776 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1778 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1779 and return the updated device path in DevicePath
1784 BdsLibUpdateFvFileDevicePath (
1785 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1786 IN EFI_GUID
*FileGuid
1789 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1790 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1792 EFI_GUID
*GuidPoint
;
1794 UINTN FvHandleCount
;
1795 EFI_HANDLE
*FvHandleBuffer
;
1796 EFI_FV_FILETYPE Type
;
1798 EFI_FV_FILE_ATTRIBUTES Attributes
;
1799 UINT32 AuthenticationStatus
;
1801 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1802 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1803 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1804 EFI_HANDLE FoundFvHandle
;
1805 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1807 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1808 return EFI_INVALID_PARAMETER
;
1810 if (FileGuid
== NULL
) {
1811 return EFI_INVALID_PARAMETER
;
1815 // Check whether the device path point to the default the input Fv file
1817 TempDevicePath
= *DevicePath
;
1818 LastDeviceNode
= TempDevicePath
;
1819 while (!IsDevicePathEnd (TempDevicePath
)) {
1820 LastDeviceNode
= TempDevicePath
;
1821 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1823 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1824 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1826 if (GuidPoint
== NULL
) {
1828 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1830 return EFI_UNSUPPORTED
;
1832 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1834 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1836 return EFI_UNSUPPORTED
;
1840 // Check whether the input Fv file device path is valid
1842 TempDevicePath
= *DevicePath
;
1843 FoundFvHandle
= NULL
;
1844 Status
= gBS
->LocateDevicePath (
1845 &gEfiFirmwareVolume2ProtocolGuid
,
1849 if (!EFI_ERROR (Status
)) {
1850 Status
= gBS
->HandleProtocol (
1852 &gEfiFirmwareVolume2ProtocolGuid
,
1855 if (!EFI_ERROR (Status
)) {
1857 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1859 Status
= Fv
->ReadFile (
1866 &AuthenticationStatus
1868 if (!EFI_ERROR (Status
)) {
1869 return EFI_ALREADY_STARTED
;
1875 // Look for the input wanted FV file in current FV
1876 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1879 FoundFvHandle
= NULL
;
1880 Status
= gBS
->HandleProtocol (
1882 &gEfiLoadedImageProtocolGuid
,
1883 (VOID
**) &LoadedImage
1885 if (!EFI_ERROR (Status
)) {
1886 Status
= gBS
->HandleProtocol (
1887 LoadedImage
->DeviceHandle
,
1888 &gEfiFirmwareVolume2ProtocolGuid
,
1891 if (!EFI_ERROR (Status
)) {
1892 Status
= Fv
->ReadFile (
1899 &AuthenticationStatus
1901 if (!EFI_ERROR (Status
)) {
1903 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1908 // Second, if fail to find, try to enumerate all FV
1911 FvHandleBuffer
= NULL
;
1912 gBS
->LocateHandleBuffer (
1914 &gEfiFirmwareVolume2ProtocolGuid
,
1919 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1920 gBS
->HandleProtocol (
1921 FvHandleBuffer
[Index
],
1922 &gEfiFirmwareVolume2ProtocolGuid
,
1926 Status
= Fv
->ReadFile (
1933 &AuthenticationStatus
1935 if (EFI_ERROR (Status
)) {
1937 // Skip if input Fv file not in the FV
1942 FoundFvHandle
= FvHandleBuffer
[Index
];
1946 if (FvHandleBuffer
!= NULL
) {
1947 FreePool (FvHandleBuffer
);
1953 // Build the shell device path
1955 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1956 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1957 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1958 *DevicePath
= NewDevicePath
;
1961 return EFI_NOT_FOUND
;