2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
17 BOOLEAN mEnumBootDevice
= FALSE
;
20 /// This GUID is used for an EFI Variable that stores the front device pathes
21 /// for a partial device path that starts with the HD node.
23 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 Boot the legacy system with the boot option
30 @param Option The legacy boot option which have BBS device path
32 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
34 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
39 IN BDS_COMMON_OPTION
*Option
43 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
45 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
46 if (EFI_ERROR (Status
)) {
48 // If no LegacyBios protocol we do not support legacy boot
50 return EFI_UNSUPPORTED
;
53 // Notes: if we separate the int 19, then we don't need to refresh BBS
55 BdsRefreshBbsTableForBoot (Option
);
58 // Write boot to OS performance data to a file
61 WriteBootToOsPerformanceData ();
64 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
65 return LegacyBios
->LegacyBoot (
67 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
68 Option
->LoadOptionsSize
,
74 Internal function to check if the input boot option is a valid EFI NV Boot####.
76 @param OptionToCheck Boot option to be checked.
78 @retval TRUE This boot option matches a valid EFI NV Boot####.
84 IsBootOptionValidNVVarialbe (
85 IN BDS_COMMON_OPTION
*OptionToCheck
89 BDS_COMMON_OPTION
*BootOption
;
91 CHAR16 OptionName
[20];
95 InitializeListHead (&TempList
);
96 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
98 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
99 if (BootOption
== NULL
) {
104 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
105 // valid EFI NV Boot####.
107 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
108 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
113 FreePool (BootOption
);
118 Process the boot option follow the UEFI specification and
119 special treat the legacy boot option with BBS_DEVICE_PATH.
121 @param Option The boot option need to be processed
122 @param DevicePath The device path which describe where to load the
123 boot image or the legacy BBS device path to boot
125 @param ExitDataSize The size of exit data.
126 @param ExitData Data returned when Boot image failed.
128 @retval EFI_SUCCESS Boot from the input boot option successfully.
129 @retval EFI_NOT_FOUND If the Device Path is not found in the system
134 BdsLibBootViaBootOption (
135 IN BDS_COMMON_OPTION
*Option
,
136 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
137 OUT UINTN
*ExitDataSize
,
138 OUT CHAR16
**ExitData OPTIONAL
143 EFI_HANDLE ImageHandle
;
144 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
145 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
146 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
147 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
148 LIST_ENTRY TempBootLists
;
151 // Record the performance data for End of BDS
153 PERF_END (0, "BDS", NULL
, 0);
159 // Notes: put EFI64 ROM Shadow Solution
161 EFI64_SHADOW_ALL_LEGACY_ROM ();
164 // Notes: this code can be remove after the s3 script table
165 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
166 // EVT_SIGNAL_LEGACY_BOOT
168 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
169 if (!EFI_ERROR (Status
)) {
170 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
173 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
176 WorkingDevicePath
= NULL
;
177 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
178 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
179 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
180 (HARDDRIVE_DEVICE_PATH
*)DevicePath
182 if (WorkingDevicePath
!= NULL
) {
183 DevicePath
= WorkingDevicePath
;
187 // Signal the EVT_SIGNAL_READY_TO_BOOT event
189 EfiSignalEventReadyToBoot();
195 if (IsBootOptionValidNVVarialbe (Option
)) {
197 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
198 // In this case, "BootCurrent" is not created.
199 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
203 &gEfiGlobalVariableGuid
,
204 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
210 ASSERT (Option
->DevicePath
!= NULL
);
211 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
212 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
215 // Check to see if we should legacy BOOT. If yes then do the legacy boot
217 return BdsLibDoLegacyBoot (Option
);
221 // If the boot option point to Internal FV shell, make sure it is valid
223 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
224 if (!EFI_ERROR(Status
)) {
225 if (Option
->DevicePath
!= NULL
) {
226 FreePool(Option
->DevicePath
);
228 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
229 ASSERT(Option
->DevicePath
!= NULL
);
230 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
232 // Update the shell boot option
234 InitializeListHead (&TempBootLists
);
235 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
238 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
240 FreePool (DevicePath
);
241 DevicePath
= Option
->DevicePath
;
244 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
246 Status
= gBS
->LoadImage (
256 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
257 // and load the image according to the default boot behavior for removable device.
259 if (EFI_ERROR (Status
)) {
261 // check if there is a bootable removable media could be found in this device path ,
262 // and get the bootable media handle
264 Handle
= BdsLibGetBootableHandle(DevicePath
);
265 if (Handle
== NULL
) {
269 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
270 // machinename is ia32, ia64, x64, ...
272 FilePath
= FileDevicePath (Handle
, (CONST CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
));
273 if (FilePath
!= NULL
) {
274 Status
= gBS
->LoadImage (
282 if (EFI_ERROR (Status
)) {
284 // The DevicePath failed, and it's not a valid
285 // removable media device.
292 if (EFI_ERROR (Status
)) {
294 // It there is any error from the Boot attempt exit now.
299 // Provide the image with it's load options
301 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
302 ASSERT_EFI_ERROR (Status
);
304 if (Option
->LoadOptionsSize
!= 0) {
305 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
306 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
309 // Before calling the image, enable the Watchdog Timer for
310 // the 5 Minute period
312 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
314 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
315 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
318 // Clear the Watchdog Timer after the image returns
320 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
324 // Clear Boot Current
328 &gEfiGlobalVariableGuid
,
329 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
339 Expand a device path that starts with a hard drive media device path node to be a
340 full device path that includes the full hardware path to the device. We need
341 to do this so it can be booted. As an optimization the front match (the part point
342 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
343 so a connect all is not required on every boot. All successful history device path
344 which point to partition node (the front part) will be saved.
346 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
347 drive media device path.
348 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
352 EFI_DEVICE_PATH_PROTOCOL
*
354 BdsExpandPartitionPartialDevicePathToFull (
355 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
359 UINTN BlockIoHandleCount
;
360 EFI_HANDLE
*BlockIoBuffer
;
361 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
362 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
363 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
366 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
367 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
368 UINTN CachedDevicePathSize
;
371 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
374 FullDevicePath
= NULL
;
376 // Check if there is prestore 'HDDP' variable.
377 // If exist, search the front path which point to partition node in the variable instants.
378 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
380 CachedDevicePath
= BdsLibGetVariableAndSize (
382 &mHdBootVariablePrivateGuid
,
383 &CachedDevicePathSize
386 if (CachedDevicePath
!= NULL
) {
387 TempNewDevicePath
= CachedDevicePath
;
392 // Check every instance of the variable
393 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
394 // partial partition boot option. Second, check whether the instance could be connected.
396 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
397 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
399 // Connect the device path instance, the device path point to hard drive media device path node
400 // e.g. ACPI() /PCI()/ATA()/Partition()
402 Status
= BdsLibConnectDevicePath (Instance
);
403 if (!EFI_ERROR (Status
)) {
409 // Come here means the first instance is not matched
413 } while (TempNewDevicePath
!= NULL
);
417 // Find the matched device path.
418 // Append the file path information from the boot option and return the fully expanded device path.
420 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
421 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
424 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
428 // First delete the matched instance.
430 TempNewDevicePath
= CachedDevicePath
;
431 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
432 FreePool (TempNewDevicePath
);
435 // Second, append the remaining path after the matched instance
437 TempNewDevicePath
= CachedDevicePath
;
438 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
439 FreePool (TempNewDevicePath
);
441 // Save the matching Device Path so we don't need to do a connect all next time
443 Status
= gRT
->SetVariable (
445 &mHdBootVariablePrivateGuid
,
446 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
447 GetDevicePathSize (CachedDevicePath
),
453 FreePool (CachedDevicePath
);
454 return FullDevicePath
;
459 // If we get here we fail to find or 'HDDP' not exist, and now we need
460 // to search all devices in the system for a matched partition
462 BdsLibConnectAllDriversToAllControllers ();
463 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
464 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
466 // If there was an error or there are no device handles that support
467 // the BLOCK_IO Protocol, then return.
472 // Loop through all the device handles that support the BLOCK_IO Protocol
474 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
476 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
477 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
481 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
483 // Find the matched partition device path
485 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
486 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
489 // Save the matched partition device path in 'HDDP' variable
491 if (CachedDevicePath
!= NULL
) {
493 // Save the matched partition device path as first instance of 'HDDP' variable
495 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
496 TempNewDevicePath
= CachedDevicePath
;
497 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
498 FreePool(TempNewDevicePath
);
500 TempNewDevicePath
= CachedDevicePath
;
501 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
502 FreePool(TempNewDevicePath
);
504 TempNewDevicePath
= CachedDevicePath
;
505 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
506 FreePool(TempNewDevicePath
);
509 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
510 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
513 ASSERT (CachedDevicePath
!= NULL
);
514 TempNewDevicePath
= CachedDevicePath
;
515 while (!IsDevicePathEnd (TempNewDevicePath
)) {
516 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
518 // Parse one instance
520 while (!IsDevicePathEndType (TempNewDevicePath
)) {
521 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
525 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
527 if (InstanceNum
>= 12) {
528 SetDevicePathEndNode (TempNewDevicePath
);
533 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
537 // Save the matching Device Path so we don't need to do a connect all next time
539 Status
= gRT
->SetVariable (
541 &mHdBootVariablePrivateGuid
,
542 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
543 GetDevicePathSize (CachedDevicePath
),
551 FreePool (CachedDevicePath
);
552 if (BlockIoBuffer
!= NULL
) {
553 FreePool (BlockIoBuffer
);
555 return FullDevicePath
;
559 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
560 instances, has the same partition node with HardDriveDevicePath device path
562 @param BlockIoDevicePath Multi device path instances which need to check
563 @param HardDriveDevicePath A device path which starts with a hard drive media
566 @retval TRUE There is a matched device path instance.
567 @retval FALSE There is no matched device path instance.
572 MatchPartitionDevicePathNode (
573 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
574 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
577 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
578 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
580 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
582 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
587 // Make PreviousDevicePath == the device path node before the end node
589 DevicePath
= BlockIoDevicePath
;
590 BlockIoHdDevicePathNode
= NULL
;
593 // find the partition device path node
595 while (!IsDevicePathEnd (DevicePath
)) {
596 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
597 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
599 BlockIoHdDevicePathNode
= DevicePath
;
603 DevicePath
= NextDevicePathNode (DevicePath
);
606 if (BlockIoHdDevicePathNode
== NULL
) {
610 // See if the harddrive device path in blockio matches the orig Hard Drive Node
612 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
616 // Check for the match
618 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
619 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
620 switch (TmpHdPath
->SignatureType
) {
621 case SIGNATURE_TYPE_GUID
:
622 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
624 case SIGNATURE_TYPE_MBR
:
625 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
637 Delete the boot option associated with the handle passed in.
639 @param Handle The handle which present the device path to create
642 @retval EFI_SUCCESS Delete the boot option success
643 @retval EFI_NOT_FOUND If the Device Path is not found in the system
644 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
645 @retval Other Error return value from SetVariable()
649 BdsLibDeleteOptionFromHandle (
654 UINT8
*BootOptionVar
;
656 UINTN BootOptionSize
;
659 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
660 UINTN DevicePathSize
;
661 UINTN OptionDevicePathSize
;
662 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
663 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
666 Status
= EFI_SUCCESS
;
671 // Check "BootOrder" variable, if no, means there is no any boot order.
673 BootOrder
= BdsLibGetVariableAndSize (
675 &gEfiGlobalVariableGuid
,
678 if (BootOrder
== NULL
) {
679 return EFI_NOT_FOUND
;
683 // Convert device handle to device path protocol instance
685 DevicePath
= DevicePathFromHandle (Handle
);
686 if (DevicePath
== NULL
) {
687 return EFI_NOT_FOUND
;
689 DevicePathSize
= GetDevicePathSize (DevicePath
);
692 // Loop all boot order variable and find the matching device path
695 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
696 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
697 BootOptionVar
= BdsLibGetVariableAndSize (
699 &gEfiGlobalVariableGuid
,
703 if (BootOptionVar
== NULL
) {
704 FreePool (BootOrder
);
705 return EFI_OUT_OF_RESOURCES
;
708 TempPtr
= BootOptionVar
;
709 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
710 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
711 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
712 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
715 // Check whether the device path match
717 if ((OptionDevicePathSize
== DevicePathSize
) &&
718 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
719 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
720 FreePool (BootOptionVar
);
724 FreePool (BootOptionVar
);
729 // Adjust number of boot option for "BootOrder" variable.
731 Status
= gRT
->SetVariable (
733 &gEfiGlobalVariableGuid
,
734 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
739 FreePool (BootOrder
);
746 Delete all invalid EFI boot options. The probable invalid boot option could
747 be Removable media or Network boot device.
749 @retval EFI_SUCCESS Delete all invalid boot option success
750 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
751 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
752 @retval Other Error return value from SetVariable()
756 BdsDeleteAllInvalidEfiBootOption (
761 UINT8
*BootOptionVar
;
763 UINTN BootOptionSize
;
767 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
768 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
771 Status
= EFI_SUCCESS
;
776 // Check "BootOrder" variable firstly, this variable hold the number of boot options
778 BootOrder
= BdsLibGetVariableAndSize (
780 &gEfiGlobalVariableGuid
,
783 if (NULL
== BootOrder
) {
784 return EFI_NOT_FOUND
;
788 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
789 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
790 BootOptionVar
= BdsLibGetVariableAndSize (
792 &gEfiGlobalVariableGuid
,
795 if (NULL
== BootOptionVar
) {
796 FreePool (BootOrder
);
797 return EFI_OUT_OF_RESOURCES
;
800 TempPtr
= BootOptionVar
;
801 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
802 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
803 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
806 // Skip legacy boot option (BBS boot device)
808 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
809 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
810 FreePool (BootOptionVar
);
815 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
817 // Delete this invalid boot option "Boot####"
819 Status
= gRT
->SetVariable (
821 &gEfiGlobalVariableGuid
,
822 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
827 // Mark this boot option in boot order as deleted
829 BootOrder
[Index
] = 0xffff;
832 FreePool (BootOptionVar
);
837 // Adjust boot order array
840 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
841 if (BootOrder
[Index
] != 0xffff) {
842 BootOrder
[Index2
] = BootOrder
[Index
];
846 Status
= gRT
->SetVariable (
848 &gEfiGlobalVariableGuid
,
849 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
850 Index2
* sizeof (UINT16
),
854 FreePool (BootOrder
);
861 This function will enumerate all possible boot device in the system,
862 it will only execute once of every boot.
864 @param BdsBootOptionList The header of the link list which indexed all
867 @retval EFI_SUCCESS Finished all the boot device enumerate and create
868 the boot option base on that boot device
873 BdsLibEnumerateAllBootOption (
874 IN OUT LIST_ENTRY
*BdsBootOptionList
882 UINT16 NonBlockNumber
;
883 UINTN NumberBlockIoHandles
;
884 EFI_HANDLE
*BlockIoHandles
;
885 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
887 UINTN NumberSimpleNetworkHandles
;
888 EFI_HANDLE
*SimpleNetworkHandles
;
890 EFI_HANDLE
*FvHandleBuffer
;
891 EFI_FV_FILETYPE Type
;
893 EFI_FV_FILE_ATTRIBUTES Attributes
;
894 UINT32 AuthenticationStatus
;
895 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
896 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
897 UINTN DevicePathType
;
899 EFI_HANDLE
*FileSystemHandles
;
900 UINTN NumberFileSystemHandles
;
902 EFI_IMAGE_DOS_HEADER DosHeader
;
903 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
904 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
910 ZeroMem (Buffer
, sizeof (Buffer
));
913 // If the boot device enumerate happened, just get the boot
914 // device from the boot order variable
916 if (mEnumBootDevice
) {
917 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
922 // Notes: this dirty code is to get the legacy boot option from the
923 // BBS table and create to variable as the EFI boot option, it should
924 // be removed after the CSM can provide legacy boot option directly
926 REFRESH_LEGACY_BOOT_OPTIONS
;
929 // Delete invalid boot option
931 BdsDeleteAllInvalidEfiBootOption ();
934 // Parse removable media
936 gBS
->LocateHandleBuffer (
938 &gEfiBlockIoProtocolGuid
,
940 &NumberBlockIoHandles
,
944 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
945 Status
= gBS
->HandleProtocol (
946 BlockIoHandles
[Index
],
947 &gEfiBlockIoProtocolGuid
,
950 if (!EFI_ERROR (Status
)) {
951 if (!BlkIo
->Media
->RemovableMedia
) {
953 // skip the non-removable block devices
958 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
959 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
961 switch (DevicePathType
) {
962 case BDS_EFI_ACPI_FLOPPY_BOOT
:
963 if (FloppyNumber
== 0) {
964 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
966 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
968 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
972 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
973 if (CdromNumber
== 0) {
974 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
976 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
978 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
982 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
983 if (UsbNumber
== 0) {
984 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
986 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
988 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
992 case BDS_EFI_MESSAGE_SCSI_BOOT
:
993 if (UsbNumber
== 0) {
994 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
996 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
998 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1002 case BDS_EFI_MESSAGE_MISC_BOOT
:
1003 if (MiscNumber
== 0) {
1004 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
1006 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
1008 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1017 if (NumberBlockIoHandles
!= 0) {
1018 FreePool (BlockIoHandles
);
1022 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1025 gBS
->LocateHandleBuffer (
1027 &gEfiSimpleFileSystemProtocolGuid
,
1029 &NumberFileSystemHandles
,
1032 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1033 Status
= gBS
->HandleProtocol (
1034 FileSystemHandles
[Index
],
1035 &gEfiBlockIoProtocolGuid
,
1038 if (!EFI_ERROR (Status
)) {
1040 // Skip if the file system handle supports a BlkIo protocol,
1046 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1047 // machinename is ia32, ia64, x64, ...
1049 Hdr
.Union
= &HdrData
;
1051 Status
= BdsLibGetImageHeader (
1052 FileSystemHandles
[Index
],
1053 (CHAR16
*)PcdGetPtr (PcdDefaultBootFileName
),
1057 if (!EFI_ERROR (Status
) &&
1058 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1059 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1065 // No such file or the file is not a EFI application, delete this boot option
1067 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1069 if (NonBlockNumber
== 0) {
1070 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1072 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1074 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1079 if (NumberFileSystemHandles
!= 0) {
1080 FreePool (FileSystemHandles
);
1084 // Parse Network Boot Device
1086 gBS
->LocateHandleBuffer (
1088 &gEfiSimpleNetworkProtocolGuid
,
1090 &NumberSimpleNetworkHandles
,
1091 &SimpleNetworkHandles
1093 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1095 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1097 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1099 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1102 if (NumberSimpleNetworkHandles
!= 0) {
1103 FreePool (SimpleNetworkHandles
);
1107 // Check if we have on flash shell
1109 gBS
->LocateHandleBuffer (
1111 &gEfiFirmwareVolume2ProtocolGuid
,
1116 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1118 // Only care the dispatched FV. If no dispatch protocol on the FV, it is not dispatched, then skip it.
1120 Status
= gBS
->HandleProtocol (
1121 FvHandleBuffer
[Index
],
1122 &gEfiFirmwareVolumeDispatchProtocolGuid
,
1125 if (EFI_ERROR (Status
)) {
1129 gBS
->HandleProtocol (
1130 FvHandleBuffer
[Index
],
1131 &gEfiFirmwareVolume2ProtocolGuid
,
1135 Status
= Fv
->ReadFile (
1142 &AuthenticationStatus
1144 if (EFI_ERROR (Status
)) {
1146 // Skip if no shell file in the FV
1151 // Build the shell boot option
1153 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1156 if (FvHandleCount
!= 0) {
1157 FreePool (FvHandleBuffer
);
1160 // Make sure every boot only have one time
1161 // boot device enumerate
1163 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1164 mEnumBootDevice
= TRUE
;
1170 Build the boot option with the handle parsed in
1172 @param Handle The handle which present the device path to create
1174 @param BdsBootOptionList The header of the link list which indexed all
1175 current boot options
1176 @param String The description of the boot option.
1181 BdsLibBuildOptionFromHandle (
1182 IN EFI_HANDLE Handle
,
1183 IN LIST_ENTRY
*BdsBootOptionList
,
1187 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1189 DevicePath
= DevicePathFromHandle (Handle
);
1192 // Create and register new boot option
1194 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1199 Build the on flash shell boot option with the handle parsed in.
1201 @param Handle The handle which present the device path to create
1202 on flash shell boot option
1203 @param BdsBootOptionList The header of the link list which indexed all
1204 current boot options
1209 BdsLibBuildOptionFromShell (
1210 IN EFI_HANDLE Handle
,
1211 IN OUT LIST_ENTRY
*BdsBootOptionList
1214 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1215 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1217 DevicePath
= DevicePathFromHandle (Handle
);
1220 // Build the shell device path
1222 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1224 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1227 // Create and register the shell boot option
1229 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1234 Boot from the UEFI spec defined "BootNext" variable.
1246 BDS_COMMON_OPTION
*BootOption
;
1247 LIST_ENTRY TempList
;
1252 // Init the boot option name buffer and temp link list
1254 InitializeListHead (&TempList
);
1255 ZeroMem (Buffer
, sizeof (Buffer
));
1257 BootNext
= BdsLibGetVariableAndSize (
1259 &gEfiGlobalVariableGuid
,
1264 // Clear the boot next variable first
1266 if (BootNext
!= NULL
) {
1269 &gEfiGlobalVariableGuid
,
1270 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1276 // Start to build the boot option and try to boot
1278 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1279 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1280 ASSERT (BootOption
!= NULL
);
1281 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1282 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1288 Return the bootable media handle.
1289 First, check the device is connected
1290 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1291 Third, detect the the default boot file in the Media, and return the removable Media handle.
1293 @param DevicePath Device Path to a bootable device
1295 @retval NULL The media on the DevicePath is not bootable
1300 BdsLibGetBootableHandle (
1301 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1305 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1306 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1308 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1310 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1313 EFI_HANDLE ReturnHandle
;
1314 EFI_HANDLE
*SimpleFileSystemHandles
;
1316 UINTN NumberSimpleFileSystemHandles
;
1318 EFI_IMAGE_DOS_HEADER DosHeader
;
1319 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1320 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1322 UpdatedDevicePath
= DevicePath
;
1325 // Check whether the device is connected
1327 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1328 if (EFI_ERROR (Status
)) {
1330 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1332 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1333 if (EFI_ERROR (Status
)) {
1335 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1337 UpdatedDevicePath
= DevicePath
;
1338 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1339 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1343 // Get BlockIo protocol and check removable attribute
1345 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1347 // Issue a dummy read to the device to check for media change.
1348 // When the removable media is changed, any Block IO read/write will
1349 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1350 // returned. After the Block IO protocol is reinstalled, subsequent
1351 // Block IO read/write will success.
1353 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1354 if (Buffer
!= NULL
) {
1355 BlockIo
->ReadBlocks (
1357 BlockIo
->Media
->MediaId
,
1359 BlockIo
->Media
->BlockSize
,
1367 // Detect the the default boot file from removable Media
1371 // 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
1372 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1374 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1375 ASSERT (DupDevicePath
!= NULL
);
1377 UpdatedDevicePath
= DupDevicePath
;
1378 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1380 // 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
1381 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1383 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1384 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1386 // Remove the usb node, let the device path only point to PCI node
1388 SetDevicePathEndNode (UpdatedDevicePath
);
1389 UpdatedDevicePath
= DupDevicePath
;
1391 UpdatedDevicePath
= DevicePath
;
1395 // Get the device path size of boot option
1397 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1398 ReturnHandle
= NULL
;
1399 gBS
->LocateHandleBuffer (
1401 &gEfiSimpleFileSystemProtocolGuid
,
1403 &NumberSimpleFileSystemHandles
,
1404 &SimpleFileSystemHandles
1406 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1408 // Get the device path size of SimpleFileSystem handle
1410 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1411 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1413 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1415 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1417 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1418 // machinename is ia32, ia64, x64, ...
1420 Hdr
.Union
= &HdrData
;
1421 Status
= BdsLibGetImageHeader (
1422 SimpleFileSystemHandles
[Index
],
1423 (CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
),
1427 if (!EFI_ERROR (Status
) &&
1428 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1429 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1430 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1436 FreePool(DupDevicePath
);
1438 if (SimpleFileSystemHandles
!= NULL
) {
1439 FreePool(SimpleFileSystemHandles
);
1442 return ReturnHandle
;
1446 Check to see if the network cable is plugged in. If the DevicePath is not
1447 connected it will be connected.
1449 @param DevicePath Device Path to check
1451 @retval TRUE DevicePath points to an Network that is connected
1452 @retval FALSE DevicePath does not point to a bootable network
1456 BdsLibNetworkBootWithMediaPresent (
1457 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1461 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1463 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1464 BOOLEAN MediaPresent
;
1466 MediaPresent
= FALSE
;
1468 UpdatedDevicePath
= DevicePath
;
1469 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1470 if (EFI_ERROR (Status
)) {
1472 // Device not present so see if we need to connect it
1474 Status
= BdsLibConnectDevicePath (DevicePath
);
1475 if (!EFI_ERROR (Status
)) {
1477 // This one should work after we did the connect
1479 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1483 if (!EFI_ERROR (Status
)) {
1484 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1485 if (!EFI_ERROR (Status
)) {
1486 if (Snp
->Mode
->MediaPresentSupported
) {
1487 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1489 // In case some one else is using the SNP check to see if it's connected
1491 MediaPresent
= Snp
->Mode
->MediaPresent
;
1494 // No one is using SNP so we need to Start and Initialize so
1495 // MediaPresent will be valid.
1497 Status
= Snp
->Start (Snp
);
1498 if (!EFI_ERROR (Status
)) {
1499 Status
= Snp
->Initialize (Snp
, 0, 0);
1500 if (!EFI_ERROR (Status
)) {
1501 MediaPresent
= Snp
->Mode
->MediaPresent
;
1502 Snp
->Shutdown (Snp
);
1508 MediaPresent
= TRUE
;
1513 return MediaPresent
;
1517 For a bootable Device path, return its boot type.
1519 @param DevicePath The bootable device Path to check
1521 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media device path node, it is media boot type
1522 For the floppy node, handle it as media node
1523 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media device path node, it is media boot type
1524 For the floppy node, handle it as media node
1525 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media device path node, it is media boot type
1526 For the floppy node, handle it as media node
1527 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media device path node, and
1528 its last device path node point to a message device path node, it is
1530 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media device path node, and
1531 its last device path node point to a message device path node, it is
1532 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node, and
1533 its last device path node point to a message device path node, it is
1534 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1535 its last device path node point to a message device path node, it is
1536 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1537 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1542 BdsGetBootTypeFromDevicePath (
1543 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1546 ACPI_HID_DEVICE_PATH
*Acpi
;
1547 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1548 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1551 if (NULL
== DevicePath
) {
1552 return BDS_EFI_UNSUPPORT
;
1555 TempDevicePath
= DevicePath
;
1557 while (!IsDevicePathEndType (TempDevicePath
)) {
1558 switch (DevicePathType (TempDevicePath
)) {
1559 case BBS_DEVICE_PATH
:
1560 return BDS_LEGACY_BBS_BOOT
;
1561 case MEDIA_DEVICE_PATH
:
1562 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1563 return BDS_EFI_MEDIA_HD_BOOT
;
1564 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1565 return BDS_EFI_MEDIA_CDROM_BOOT
;
1568 case ACPI_DEVICE_PATH
:
1569 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1570 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1571 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1574 case MESSAGING_DEVICE_PATH
:
1576 // Get the last device path node
1578 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1579 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1581 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1584 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1587 // if the device path not only point to driver device, it is not a messaging device path,
1589 if (!IsDevicePathEndType (LastDeviceNode
)) {
1593 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1594 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1595 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1596 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1597 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1598 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1600 return BDS_EFI_MESSAGE_MISC_BOOT
;
1604 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1607 return BDS_EFI_UNSUPPORT
;
1611 Check whether the Device path in a boot option point to a valid bootable device,
1612 And if CheckMedia is true, check the device is ready to boot now.
1614 @param DevPath the Device path in a boot option
1615 @param CheckMedia if true, check the device is ready to boot now.
1617 @retval TRUE the Device path is valid
1618 @retval FALSE the Device path is invalid .
1623 BdsLibIsValidEFIBootOptDevicePath (
1624 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1625 IN BOOLEAN CheckMedia
1630 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1631 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1632 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1634 TempDevicePath
= DevPath
;
1635 LastDeviceNode
= DevPath
;
1638 // Check if it's a valid boot option for network boot device
1639 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1640 // there is the network card there.
1642 Status
= gBS
->LocateDevicePath (
1643 &gEfiSimpleNetworkProtocolGuid
,
1647 if (EFI_ERROR (Status
)) {
1649 // Device not present so see if we need to connect it
1651 TempDevicePath
= DevPath
;
1652 BdsLibConnectDevicePath (TempDevicePath
);
1653 Status
= gBS
->LocateDevicePath (
1654 &gEfiSimpleNetworkProtocolGuid
,
1660 if (!EFI_ERROR (Status
)) {
1663 // Test if it is ready to boot now
1665 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1674 // If the boot option point to a file, it is a valid EFI boot option,
1675 // and assume it is ready to boot now
1677 while (!IsDevicePathEnd (TempDevicePath
)) {
1678 LastDeviceNode
= TempDevicePath
;
1679 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1681 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1682 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1687 // Check if it's a valid boot option for internal Shell
1689 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1691 // If the boot option point to Internal FV shell, make sure it is valid
1693 TempDevicePath
= DevPath
;
1694 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1695 if (Status
== EFI_ALREADY_STARTED
) {
1698 if (Status
== EFI_SUCCESS
) {
1699 FreePool (TempDevicePath
);
1706 // 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
1708 TempDevicePath
= DevPath
;
1709 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1710 if (EFI_ERROR (Status
)) {
1712 // Device not present so see if we need to connect it
1714 Status
= BdsLibConnectDevicePath (DevPath
);
1715 if (!EFI_ERROR (Status
)) {
1717 // Try again to get the Block Io protocol after we did the connect
1719 TempDevicePath
= DevPath
;
1720 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1724 if (!EFI_ERROR (Status
)) {
1725 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1726 if (!EFI_ERROR (Status
)) {
1729 // Test if it is ready to boot now
1731 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1740 // 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,
1742 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1743 if (!EFI_ERROR (Status
)) {
1746 // Test if it is ready to boot now
1748 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1762 According to a file guild, check a Fv file device path is valid. If it is invalid,
1763 try to return the valid device path.
1764 FV address maybe changes for memory layout adjust from time to time, use this function
1765 could promise the Fv file device path is right.
1767 @param DevicePath on input, the Fv file device path need to check on
1768 output, the updated valid Fv file device path
1769 @param FileGuid the Fv file guild
1771 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1773 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1775 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1777 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1778 and return the updated device path in DevicePath
1783 BdsLibUpdateFvFileDevicePath (
1784 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1785 IN EFI_GUID
*FileGuid
1788 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1789 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1791 EFI_GUID
*GuidPoint
;
1793 UINTN FvHandleCount
;
1794 EFI_HANDLE
*FvHandleBuffer
;
1795 EFI_FV_FILETYPE Type
;
1797 EFI_FV_FILE_ATTRIBUTES Attributes
;
1798 UINT32 AuthenticationStatus
;
1800 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1801 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1802 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1803 EFI_HANDLE FoundFvHandle
;
1804 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1806 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1807 return EFI_INVALID_PARAMETER
;
1809 if (FileGuid
== NULL
) {
1810 return EFI_INVALID_PARAMETER
;
1814 // Check whether the device path point to the default the input Fv file
1816 TempDevicePath
= *DevicePath
;
1817 LastDeviceNode
= TempDevicePath
;
1818 while (!IsDevicePathEnd (TempDevicePath
)) {
1819 LastDeviceNode
= TempDevicePath
;
1820 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1822 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1823 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1825 if (GuidPoint
== NULL
) {
1827 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1829 return EFI_UNSUPPORTED
;
1831 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1833 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1835 return EFI_UNSUPPORTED
;
1839 // Check whether the input Fv file device path is valid
1841 TempDevicePath
= *DevicePath
;
1842 FoundFvHandle
= NULL
;
1843 Status
= gBS
->LocateDevicePath (
1844 &gEfiFirmwareVolume2ProtocolGuid
,
1848 if (!EFI_ERROR (Status
)) {
1849 Status
= gBS
->HandleProtocol (
1851 &gEfiFirmwareVolume2ProtocolGuid
,
1854 if (!EFI_ERROR (Status
)) {
1856 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1858 Status
= Fv
->ReadFile (
1865 &AuthenticationStatus
1867 if (!EFI_ERROR (Status
)) {
1868 return EFI_ALREADY_STARTED
;
1874 // Look for the input wanted FV file in current FV
1875 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1878 FoundFvHandle
= NULL
;
1879 Status
= gBS
->HandleProtocol (
1881 &gEfiLoadedImageProtocolGuid
,
1882 (VOID
**) &LoadedImage
1884 if (!EFI_ERROR (Status
)) {
1885 Status
= gBS
->HandleProtocol (
1886 LoadedImage
->DeviceHandle
,
1887 &gEfiFirmwareVolume2ProtocolGuid
,
1890 if (!EFI_ERROR (Status
)) {
1891 Status
= Fv
->ReadFile (
1898 &AuthenticationStatus
1900 if (!EFI_ERROR (Status
)) {
1902 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1907 // Second, if fail to find, try to enumerate all FV
1910 FvHandleBuffer
= NULL
;
1911 gBS
->LocateHandleBuffer (
1913 &gEfiFirmwareVolume2ProtocolGuid
,
1918 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1919 gBS
->HandleProtocol (
1920 FvHandleBuffer
[Index
],
1921 &gEfiFirmwareVolume2ProtocolGuid
,
1925 Status
= Fv
->ReadFile (
1932 &AuthenticationStatus
1934 if (EFI_ERROR (Status
)) {
1936 // Skip if input Fv file not in the FV
1941 FoundFvHandle
= FvHandleBuffer
[Index
];
1945 if (FvHandleBuffer
!= NULL
) {
1946 FreePool (FvHandleBuffer
);
1952 // Build the shell device path
1954 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1955 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1956 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1957 *DevicePath
= NewDevicePath
;
1960 return EFI_NOT_FOUND
;