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 seperate 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
,
75 Process the boot option follow the UEFI specification and
76 special treat the legacy boot option with BBS_DEVICE_PATH.
78 @param Option The boot option need to be processed
79 @param DevicePath The device path which describe where to load the
80 boot image or the legcy BBS device path to boot
82 @param ExitDataSize The size of exit data.
83 @param ExitData Data returned when Boot image failed.
85 @retval EFI_SUCCESS Boot from the input boot option successfully.
86 @retval EFI_NOT_FOUND If the Device Path is not found in the system
91 BdsLibBootViaBootOption (
92 IN BDS_COMMON_OPTION
* Option
,
93 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
94 OUT UINTN
*ExitDataSize
,
95 OUT CHAR16
**ExitData OPTIONAL
100 EFI_HANDLE ImageHandle
;
101 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
102 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
103 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
104 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
105 LIST_ENTRY TempBootLists
;
108 // Record the performance data for End of BDS
110 PERF_END (0, BDS_TOK
, NULL
, 0);
116 // Notes: put EFI64 ROM Shadow Solution
118 EFI64_SHADOW_ALL_LEGACY_ROM ();
121 // Notes: this code can be remove after the s3 script table
122 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
123 // EVT_SIGNAL_LEGACY_BOOT
125 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
126 if (!EFI_ERROR (Status
)) {
127 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
130 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
133 WorkingDevicePath
= NULL
;
134 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
135 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
136 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
137 (HARDDRIVE_DEVICE_PATH
*)DevicePath
139 if (WorkingDevicePath
!= NULL
) {
140 DevicePath
= WorkingDevicePath
;
144 // Signal the EVT_SIGNAL_READY_TO_BOOT event
146 EfiSignalEventReadyToBoot();
154 &gEfiGlobalVariableGuid
,
155 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
160 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
161 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
164 // Check to see if we should legacy BOOT. If yes then do the legacy boot
166 return BdsLibDoLegacyBoot (Option
);
170 // If the boot option point to Internal FV shell, make sure it is valid
172 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
173 if (!EFI_ERROR(Status
)) {
174 if (Option
->DevicePath
!= NULL
) {
175 FreePool(Option
->DevicePath
);
177 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
178 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
180 // Update the shell boot option
182 InitializeListHead (&TempBootLists
);
183 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
186 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
188 FreePool (DevicePath
);
189 DevicePath
= Option
->DevicePath
;
192 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
194 Status
= gBS
->LoadImage (
204 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
205 // and load the image according to the default boot behavior for removable device.
207 if (EFI_ERROR (Status
)) {
209 // check if there is a bootable removable media could be found in this device path ,
210 // and get the bootable media handle
212 Handle
= BdsLibGetBootableHandle(DevicePath
);
213 if (Handle
== NULL
) {
217 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
218 // machinename is ia32, ia64, x64, ...
220 FilePath
= FileDevicePath (Handle
, (CONST CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
));
221 if (FilePath
!= NULL
) {
222 Status
= gBS
->LoadImage (
230 if (EFI_ERROR (Status
)) {
232 // The DevicePath failed, and it's not a valid
233 // removable media device.
240 if (EFI_ERROR (Status
)) {
242 // It there is any error from the Boot attempt exit now.
247 // Provide the image with it's load options
249 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
250 ASSERT_EFI_ERROR (Status
);
252 if (Option
->LoadOptionsSize
!= 0) {
253 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
254 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
257 // Before calling the image, enable the Watchdog Timer for
258 // the 5 Minute period
260 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
262 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
263 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
266 // Clear the Watchdog Timer after the image returns
268 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
272 // Clear Boot Current
276 &gEfiGlobalVariableGuid
,
277 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
287 Expand a device path that starts with a hard drive media device path node to be a
288 full device path that includes the full hardware path to the device. We need
289 to do this so it can be booted. As an optimizaiton the front match (the part point
290 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
291 so a connect all is not required on every boot. All successful history device path
292 which point to partition node (the front part) will be saved.
294 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
295 drive media device path.
296 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
300 EFI_DEVICE_PATH_PROTOCOL
*
302 BdsExpandPartitionPartialDevicePathToFull (
303 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
307 UINTN BlockIoHandleCount
;
308 EFI_HANDLE
*BlockIoBuffer
;
309 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
310 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
311 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
314 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
315 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
316 UINTN CachedDevicePathSize
;
319 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
322 FullDevicePath
= NULL
;
324 // Check if there is prestore 'HDDP' variable.
325 // If exist, search the front path which point to partition node in the variable instants.
326 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
328 CachedDevicePath
= BdsLibGetVariableAndSize (
330 &mHdBootVariablePrivateGuid
,
331 &CachedDevicePathSize
334 if (CachedDevicePath
!= NULL
) {
335 TempNewDevicePath
= CachedDevicePath
;
340 // Check every instance of the variable
341 // First, check wheather the instance contain the partition node, which is needed for distinguishing multi
342 // partial partition boot option. Second, check wheather the instance could be connected.
344 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
345 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
347 // Connect the device path instance, the device path point to hard drive media device path node
348 // e.g. ACPI() /PCI()/ATA()/Partition()
350 Status
= BdsLibConnectDevicePath (Instance
);
351 if (!EFI_ERROR (Status
)) {
357 // Come here means the first instance is not matched
361 } while (TempNewDevicePath
!= NULL
);
365 // Find the matched device path.
366 // Append the file path infomration from the boot option and return the fully expanded device path.
368 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
369 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
372 // Adjust the 'HDDP' instances sequense if the matched one is not first one.
376 // First delete the matched instance.
378 TempNewDevicePath
= CachedDevicePath
;
379 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
380 FreePool (TempNewDevicePath
);
383 // Second, append the remaining parth after the matched instance
385 TempNewDevicePath
= CachedDevicePath
;
386 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
387 FreePool (TempNewDevicePath
);
389 // Save the matching Device Path so we don't need to do a connect all next time
391 Status
= gRT
->SetVariable (
393 &mHdBootVariablePrivateGuid
,
394 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
395 GetDevicePathSize (CachedDevicePath
),
401 FreePool (CachedDevicePath
);
402 return FullDevicePath
;
407 // If we get here we fail to find or 'HDDP' not exist, and now we need
408 // to seach all devices in the system for a matched partition
410 BdsLibConnectAllDriversToAllControllers ();
411 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
412 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0) {
414 // If there was an error or there are no device handles that support
415 // the BLOCK_IO Protocol, then return.
420 // Loop through all the device handles that support the BLOCK_IO Protocol
422 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
424 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
425 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
429 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
431 // Find the matched partition device path
433 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
434 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
437 // Save the matched patition device path in 'HDDP' variable
439 if (CachedDevicePath
!= NULL
) {
441 // Save the matched patition device path as first instance of 'HDDP' variable
443 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
444 TempNewDevicePath
= CachedDevicePath
;
445 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
446 FreePool(TempNewDevicePath
);
448 TempNewDevicePath
= CachedDevicePath
;
449 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
450 FreePool(TempNewDevicePath
);
452 TempNewDevicePath
= CachedDevicePath
;
453 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
454 FreePool(TempNewDevicePath
);
457 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
458 // If the user try to boot many OS in different HDs or partitions, in theary, the 'HDDP' variable maybe become larger and larger.
461 TempNewDevicePath
= CachedDevicePath
;
462 while (!IsDevicePathEnd (TempNewDevicePath
)) {
463 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
465 // Parse one instance
467 while (!IsDevicePathEndType (TempNewDevicePath
)) {
468 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
472 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
474 if (InstanceNum
>= 12) {
475 SetDevicePathEndNode (TempNewDevicePath
);
480 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
484 // Save the matching Device Path so we don't need to do a connect all next time
486 Status
= gRT
->SetVariable (
488 &mHdBootVariablePrivateGuid
,
489 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
490 GetDevicePathSize (CachedDevicePath
),
498 FreePool (CachedDevicePath
);
499 if (BlockIoBuffer
!= NULL
) {
500 FreePool (BlockIoBuffer
);
502 return FullDevicePath
;
506 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
507 instances, has the same partition node with HardDriveDevicePath device path
509 @param BlockIoDevicePath Multi device path instances which need to check
510 @param HardDriveDevicePath A device path which starts with a hard drive media
513 @retval TRUE There is a matched device path instance.
514 @retval FALSE There is no matched device path instance.
519 MatchPartitionDevicePathNode (
520 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
521 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
524 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
525 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
527 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
529 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
534 // Make PreviousDevicePath == the device path node before the end node
536 DevicePath
= BlockIoDevicePath
;
537 BlockIoHdDevicePathNode
= NULL
;
540 // find the partition device path node
542 while (!IsDevicePathEnd (DevicePath
)) {
543 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
544 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
546 BlockIoHdDevicePathNode
= DevicePath
;
550 DevicePath
= NextDevicePathNode (DevicePath
);
553 if (BlockIoHdDevicePathNode
== NULL
) {
557 // See if the harddrive device path in blockio matches the orig Hard Drive Node
559 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
563 // Check for the match
565 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
566 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
567 switch (TmpHdPath
->SignatureType
) {
568 case SIGNATURE_TYPE_GUID
:
569 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
571 case SIGNATURE_TYPE_MBR
:
572 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
584 Delete the boot option associated with the handle passed in.
586 @param Handle The handle which present the device path to create
589 @retval EFI_SUCCESS Delete the boot option success
590 @retval EFI_NOT_FOUND If the Device Path is not found in the system
591 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
592 @retval Other Error return value from SetVariable()
596 BdsLibDeleteOptionFromHandle (
601 UINT8
*BootOptionVar
;
603 UINTN BootOptionSize
;
606 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
607 UINTN DevicePathSize
;
608 UINTN OptionDevicePathSize
;
609 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
610 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
613 Status
= EFI_SUCCESS
;
618 // Check "BootOrder" variable, if no, means there is no any boot order.
620 BootOrder
= BdsLibGetVariableAndSize (
622 &gEfiGlobalVariableGuid
,
625 if (BootOrder
== NULL
) {
626 return EFI_NOT_FOUND
;
630 // Convert device handle to device path protocol instance
632 DevicePath
= DevicePathFromHandle (Handle
);
633 if (DevicePath
== NULL
) {
634 return EFI_NOT_FOUND
;
636 DevicePathSize
= GetDevicePathSize (DevicePath
);
639 // Loop all boot order variable and find the matching device path
642 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
643 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
644 BootOptionVar
= BdsLibGetVariableAndSize (
646 &gEfiGlobalVariableGuid
,
650 if (BootOptionVar
== NULL
) {
651 FreePool (BootOrder
);
652 return EFI_OUT_OF_RESOURCES
;
655 TempPtr
= BootOptionVar
;
656 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
657 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
658 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
659 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
662 // Check whether the device path match
664 if ((OptionDevicePathSize
== DevicePathSize
) &&
665 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
666 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
667 FreePool (BootOptionVar
);
671 FreePool (BootOptionVar
);
676 // Adjust number of boot option for "BootOrder" variable.
678 Status
= gRT
->SetVariable (
680 &gEfiGlobalVariableGuid
,
681 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
686 FreePool (BootOrder
);
693 Delete all invalid EFI boot options. The probable invalid boot option could
694 be Removable media or Network boot device.
696 @retval EFI_SUCCESS Delete all invalid boot option success
697 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
698 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
699 @retval Other Error return value from SetVariable()
703 BdsDeleteAllInvalidEfiBootOption (
708 UINT8
*BootOptionVar
;
710 UINTN BootOptionSize
;
714 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
715 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
718 Status
= EFI_SUCCESS
;
723 // Check "BootOrder" variable firstly, this variable hold the number of boot options
725 BootOrder
= BdsLibGetVariableAndSize (
727 &gEfiGlobalVariableGuid
,
730 if (NULL
== BootOrder
) {
731 return EFI_NOT_FOUND
;
735 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
736 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
737 BootOptionVar
= BdsLibGetVariableAndSize (
739 &gEfiGlobalVariableGuid
,
742 if (NULL
== BootOptionVar
) {
743 FreePool (BootOrder
);
744 return EFI_OUT_OF_RESOURCES
;
747 TempPtr
= BootOptionVar
;
748 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
749 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
750 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
753 // Skip legacy boot option (BBS boot device)
755 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
756 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
757 FreePool (BootOptionVar
);
762 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
764 // Delete this invalid boot option "Boot####"
766 Status
= gRT
->SetVariable (
768 &gEfiGlobalVariableGuid
,
769 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
774 // Mark this boot option in boot order as deleted
776 BootOrder
[Index
] = 0xffff;
779 FreePool (BootOptionVar
);
784 // Adjust boot order array
787 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
788 if (BootOrder
[Index
] != 0xffff) {
789 BootOrder
[Index2
] = BootOrder
[Index
];
793 Status
= gRT
->SetVariable (
795 &gEfiGlobalVariableGuid
,
796 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
797 Index2
* sizeof (UINT16
),
801 FreePool (BootOrder
);
808 This function will enumerate all possible boot device in the system,
809 it will only excute once of every boot.
811 @param BdsBootOptionList The header of the link list which indexed all
814 @retval EFI_SUCCESS Finished all the boot device enumerate and create
815 the boot option base on that boot device
820 BdsLibEnumerateAllBootOption (
821 IN OUT LIST_ENTRY
*BdsBootOptionList
829 UINT16 NonBlockNumber
;
830 UINTN NumberBlockIoHandles
;
831 EFI_HANDLE
*BlockIoHandles
;
832 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
834 UINTN NumberSimpleNetworkHandles
;
835 EFI_HANDLE
*SimpleNetworkHandles
;
837 EFI_HANDLE
*FvHandleBuffer
;
838 EFI_FV_FILETYPE Type
;
840 EFI_FV_FILE_ATTRIBUTES Attributes
;
841 UINT32 AuthenticationStatus
;
842 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
843 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
844 UINTN DevicePathType
;
846 EFI_HANDLE
*FileSystemHandles
;
847 UINTN NumberFileSystemHandles
;
849 EFI_IMAGE_DOS_HEADER DosHeader
;
850 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
851 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
857 ZeroMem (Buffer
, sizeof (Buffer
));
860 // If the boot device enumerate happened, just get the boot
861 // device from the boot order variable
863 if (mEnumBootDevice
) {
864 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
869 // Notes: this dirty code is to get the legacy boot option from the
870 // BBS table and create to variable as the EFI boot option, it should
871 // be removed after the CSM can provide legacy boot option directly
873 REFRESH_LEGACY_BOOT_OPTIONS
;
876 // Delete invalid boot option
878 BdsDeleteAllInvalidEfiBootOption ();
881 // Parse removable media
883 gBS
->LocateHandleBuffer (
885 &gEfiBlockIoProtocolGuid
,
887 &NumberBlockIoHandles
,
891 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
892 Status
= gBS
->HandleProtocol (
893 BlockIoHandles
[Index
],
894 &gEfiBlockIoProtocolGuid
,
897 if (!EFI_ERROR (Status
)) {
898 if (!BlkIo
->Media
->RemovableMedia
) {
900 // skip the non-removable block devices
905 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
906 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
908 switch (DevicePathType
) {
909 case BDS_EFI_ACPI_FLOPPY_BOOT
:
910 if (FloppyNumber
== 0) {
911 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
913 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
915 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
919 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
920 if (CdromNumber
== 0) {
921 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
923 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
925 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
929 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
930 if (UsbNumber
== 0) {
931 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
933 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
935 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
939 case BDS_EFI_MESSAGE_SCSI_BOOT
:
940 if (UsbNumber
== 0) {
941 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
943 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
945 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
949 case BDS_EFI_MESSAGE_MISC_BOOT
:
950 if (MiscNumber
== 0) {
951 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
953 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
955 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
964 if (NumberBlockIoHandles
!= 0) {
965 FreePool (BlockIoHandles
);
969 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
972 gBS
->LocateHandleBuffer (
974 &gEfiSimpleFileSystemProtocolGuid
,
976 &NumberFileSystemHandles
,
979 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
980 Status
= gBS
->HandleProtocol (
981 FileSystemHandles
[Index
],
982 &gEfiBlockIoProtocolGuid
,
985 if (!EFI_ERROR (Status
)) {
987 // Skip if the file system handle supports a BlkIo protocol,
993 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
994 // machinename is ia32, ia64, x64, ...
996 Hdr
.Union
= &HdrData
;
998 Status
= BdsLibGetImageHeader (
999 FileSystemHandles
[Index
],
1000 (CHAR16
*)PcdGetPtr (PcdDefaultBootFileName
),
1004 if (!EFI_ERROR (Status
) &&
1005 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1006 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1012 // No such file or the file is not a EFI application, delete this boot option
1014 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1016 if (NonBlockNumber
== 0) {
1017 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1019 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1021 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1026 if (NumberFileSystemHandles
!= 0) {
1027 FreePool (FileSystemHandles
);
1031 // Parse Network Boot Device
1033 gBS
->LocateHandleBuffer (
1035 &gEfiSimpleNetworkProtocolGuid
,
1037 &NumberSimpleNetworkHandles
,
1038 &SimpleNetworkHandles
1040 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1042 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1044 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1046 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1049 if (NumberSimpleNetworkHandles
!= 0) {
1050 FreePool (SimpleNetworkHandles
);
1054 // Check if we have on flash shell
1056 gBS
->LocateHandleBuffer (
1058 &gEfiFirmwareVolume2ProtocolGuid
,
1063 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1065 // Only care the dispatched FV. If no dispatch protocol on the FV, it is not dispatched, then skip it.
1067 Status
= gBS
->HandleProtocol (
1068 FvHandleBuffer
[Index
],
1069 &gEfiFirmwareVolumeDispatchProtocolGuid
,
1072 if (EFI_ERROR (Status
)) {
1076 gBS
->HandleProtocol (
1077 FvHandleBuffer
[Index
],
1078 &gEfiFirmwareVolume2ProtocolGuid
,
1082 Status
= Fv
->ReadFile (
1089 &AuthenticationStatus
1091 if (EFI_ERROR (Status
)) {
1093 // Skip if no shell file in the FV
1098 // Build the shell boot option
1100 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1103 if (FvHandleCount
!= 0) {
1104 FreePool (FvHandleBuffer
);
1107 // Make sure every boot only have one time
1108 // boot device enumerate
1110 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1111 mEnumBootDevice
= TRUE
;
1117 Build the boot option with the handle parsed in
1119 @param Handle The handle which present the device path to create
1121 @param BdsBootOptionList The header of the link list which indexed all
1122 current boot options
1123 @param String The description of the boot option.
1128 BdsLibBuildOptionFromHandle (
1129 IN EFI_HANDLE Handle
,
1130 IN LIST_ENTRY
*BdsBootOptionList
,
1134 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1136 DevicePath
= DevicePathFromHandle (Handle
);
1139 // Create and register new boot option
1141 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1146 Build the on flash shell boot option with the handle parsed in.
1148 @param Handle The handle which present the device path to create
1149 on flash shell boot option
1150 @param BdsBootOptionList The header of the link list which indexed all
1151 current boot options
1156 BdsLibBuildOptionFromShell (
1157 IN EFI_HANDLE Handle
,
1158 IN OUT LIST_ENTRY
*BdsBootOptionList
1161 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1162 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1164 DevicePath
= DevicePathFromHandle (Handle
);
1167 // Build the shell device path
1169 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1171 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1174 // Create and register the shell boot option
1176 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1181 Boot from the UEFI spec defined "BootNext" variable.
1193 BDS_COMMON_OPTION
*BootOption
;
1194 LIST_ENTRY TempList
;
1199 // Init the boot option name buffer and temp link list
1201 InitializeListHead (&TempList
);
1202 ZeroMem (Buffer
, sizeof (Buffer
));
1204 BootNext
= BdsLibGetVariableAndSize (
1206 &gEfiGlobalVariableGuid
,
1211 // Clear the boot next variable first
1213 if (BootNext
!= NULL
) {
1216 &gEfiGlobalVariableGuid
,
1217 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1223 // Start to build the boot option and try to boot
1225 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1226 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1227 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1228 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1234 Return the bootable media handle.
1235 First, check the device is connected
1236 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1237 Third, detect the the default boot file in the Media, and return the removable Media handle.
1239 @param DevicePath Device Path to a bootable device
1241 @retval NULL The media on the DevicePath is not bootable
1246 BdsLibGetBootableHandle (
1247 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1251 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1252 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1254 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1256 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1259 EFI_HANDLE ReturnHandle
;
1260 EFI_HANDLE
*SimpleFileSystemHandles
;
1262 UINTN NumberSimpleFileSystemHandles
;
1264 EFI_IMAGE_DOS_HEADER DosHeader
;
1265 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1266 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1268 UpdatedDevicePath
= DevicePath
;
1271 // Check whether the device is connected
1273 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1274 if (EFI_ERROR (Status
)) {
1276 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1278 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1279 if (EFI_ERROR (Status
)) {
1281 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1283 UpdatedDevicePath
= DevicePath
;
1284 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1285 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1289 // Get BlockIo protocal and check removable attribute
1291 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1293 // Issue a dummy read to the device to check for media change.
1294 // When the removable media is changed, any Block IO read/write will
1295 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1296 // returned. After the Block IO protocol is reinstalled, subsequent
1297 // Block IO read/write will success.
1299 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1300 if (Buffer
!= NULL
) {
1301 BlockIo
->ReadBlocks (
1303 BlockIo
->Media
->MediaId
,
1305 BlockIo
->Media
->BlockSize
,
1313 // Detect the the default boot file from removable Media
1317 // 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
1318 // Try to locate the USB node device path first, if fail then use its previour PCI node to search
1320 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1321 ASSERT (DupDevicePath
!= NULL
);
1323 UpdatedDevicePath
= DupDevicePath
;
1324 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1326 // 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
1327 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1329 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1330 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1332 // Remove the usb node, let the device path only point to PCI node
1334 SetDevicePathEndNode (UpdatedDevicePath
);
1335 UpdatedDevicePath
= DupDevicePath
;
1337 UpdatedDevicePath
= DevicePath
;
1341 // Get the device path size of boot option
1343 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1344 ReturnHandle
= NULL
;
1345 gBS
->LocateHandleBuffer (
1347 &gEfiSimpleFileSystemProtocolGuid
,
1349 &NumberSimpleFileSystemHandles
,
1350 &SimpleFileSystemHandles
1352 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1354 // Get the device path size of SimpleFileSystem handle
1356 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1357 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1359 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1361 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1363 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1364 // machinename is ia32, ia64, x64, ...
1366 Hdr
.Union
= &HdrData
;
1367 Status
= BdsLibGetImageHeader (
1368 SimpleFileSystemHandles
[Index
],
1369 (CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
),
1373 if (!EFI_ERROR (Status
) &&
1374 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1375 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1376 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1382 FreePool(DupDevicePath
);
1384 if (SimpleFileSystemHandles
!= NULL
) {
1385 FreePool(SimpleFileSystemHandles
);
1388 return ReturnHandle
;
1392 Check to see if the network cable is plugged in. If the DevicePath is not
1393 connected it will be connected.
1395 @param DevicePath Device Path to check
1397 @retval TRUE DevicePath points to an Network that is connected
1398 @retval FALSE DevicePath does not point to a bootable network
1402 BdsLibNetworkBootWithMediaPresent (
1403 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1407 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1409 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1410 BOOLEAN MediaPresent
;
1412 MediaPresent
= FALSE
;
1414 UpdatedDevicePath
= DevicePath
;
1415 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1416 if (EFI_ERROR (Status
)) {
1418 // Device not present so see if we need to connect it
1420 Status
= BdsLibConnectDevicePath (DevicePath
);
1421 if (!EFI_ERROR (Status
)) {
1423 // This one should work after we did the connect
1425 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1429 if (!EFI_ERROR (Status
)) {
1430 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1431 if (!EFI_ERROR (Status
)) {
1432 if (Snp
->Mode
->MediaPresentSupported
) {
1433 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1435 // In case some one else is using the SNP check to see if it's connected
1437 MediaPresent
= Snp
->Mode
->MediaPresent
;
1440 // No one is using SNP so we need to Start and Initialize so
1441 // MediaPresent will be valid.
1443 Status
= Snp
->Start (Snp
);
1444 if (!EFI_ERROR (Status
)) {
1445 Status
= Snp
->Initialize (Snp
, 0, 0);
1446 if (!EFI_ERROR (Status
)) {
1447 MediaPresent
= Snp
->Mode
->MediaPresent
;
1448 Snp
->Shutdown (Snp
);
1454 MediaPresent
= TRUE
;
1459 return MediaPresent
;
1463 For a bootable Device path, return its boot type.
1465 @param DevicePath The bootable device Path to check
1467 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media deviec path node, it is media boot type
1468 For the floppy node, handle it as media node
1469 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media deviec path node, it is media boot type
1470 For the floppy node, handle it as media node
1471 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media deviec path node, it is media boot type
1472 For the floppy node, handle it as media node
1473 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media deviec path node, and
1474 its last device path node point to a message device path node, it is
1476 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media deviec path node, and
1477 its last device path node point to a message device path node, it is
1478 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media deviec path node, and
1479 its last device path node point to a message device path node, it is
1480 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media deviec path node, and
1481 its last device path node point to a message device path node, it is
1482 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1483 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message devie,
1488 BdsGetBootTypeFromDevicePath (
1489 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1492 ACPI_HID_DEVICE_PATH
*Acpi
;
1493 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1494 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1497 if (NULL
== DevicePath
) {
1498 return BDS_EFI_UNSUPPORT
;
1501 TempDevicePath
= DevicePath
;
1503 while (!IsDevicePathEndType (TempDevicePath
)) {
1504 switch (DevicePathType (TempDevicePath
)) {
1505 case BBS_DEVICE_PATH
:
1506 return BDS_LEGACY_BBS_BOOT
;
1507 case MEDIA_DEVICE_PATH
:
1508 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1509 return BDS_EFI_MEDIA_HD_BOOT
;
1510 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1511 return BDS_EFI_MEDIA_CDROM_BOOT
;
1514 case ACPI_DEVICE_PATH
:
1515 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1516 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1517 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1520 case MESSAGING_DEVICE_PATH
:
1522 // Get the last device path node
1524 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1525 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1527 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1530 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1533 // if the device path not only point to driver device, it is not a messaging device path,
1535 if (!IsDevicePathEndType (LastDeviceNode
)) {
1539 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1540 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1541 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1542 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1543 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1544 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1546 return BDS_EFI_MESSAGE_MISC_BOOT
;
1550 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1553 return BDS_EFI_UNSUPPORT
;
1557 Check whether the Device path in a boot option point to a valide bootable device,
1558 And if CheckMedia is true, check the device is ready to boot now.
1560 @param DevPath the Device path in a boot option
1561 @param CheckMedia if true, check the device is ready to boot now.
1563 @retval TRUE the Device path is valide
1564 @retval FALSE the Device path is invalide .
1569 BdsLibIsValidEFIBootOptDevicePath (
1570 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1571 IN BOOLEAN CheckMedia
1576 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1577 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1578 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1580 TempDevicePath
= DevPath
;
1581 LastDeviceNode
= DevPath
;
1584 // Check if it's a valid boot option for network boot device
1585 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1586 // there is the network card there.
1588 Status
= gBS
->LocateDevicePath (
1589 &gEfiSimpleNetworkProtocolGuid
,
1593 if (EFI_ERROR (Status
)) {
1595 // Device not present so see if we need to connect it
1597 TempDevicePath
= DevPath
;
1598 BdsLibConnectDevicePath (TempDevicePath
);
1599 Status
= gBS
->LocateDevicePath (
1600 &gEfiSimpleNetworkProtocolGuid
,
1606 if (!EFI_ERROR (Status
)) {
1609 // Test if it is ready to boot now
1611 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1620 // If the boot option point to a file, it is a valid EFI boot option,
1621 // and assume it is ready to boot now
1623 while (!IsDevicePathEnd (TempDevicePath
)) {
1624 LastDeviceNode
= TempDevicePath
;
1625 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1627 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1628 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1633 // Check if it's a valid boot option for internal Shell
1635 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1637 // If the boot option point to Internal FV shell, make sure it is valid
1639 TempDevicePath
= DevPath
;
1640 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1641 if (Status
== EFI_ALREADY_STARTED
) {
1644 if (Status
== EFI_SUCCESS
) {
1645 FreePool (TempDevicePath
);
1652 // 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
1654 TempDevicePath
= DevPath
;
1655 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1656 if (EFI_ERROR (Status
)) {
1658 // Device not present so see if we need to connect it
1660 Status
= BdsLibConnectDevicePath (DevPath
);
1661 if (!EFI_ERROR (Status
)) {
1663 // Try again to get the Block Io protocol after we did the connect
1665 TempDevicePath
= DevPath
;
1666 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1670 if (!EFI_ERROR (Status
)) {
1671 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1672 if (!EFI_ERROR (Status
)) {
1675 // Test if it is ready to boot now
1677 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1686 // 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,
1688 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1689 if (!EFI_ERROR (Status
)) {
1692 // Test if it is ready to boot now
1694 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1708 According to a file guild, check a Fv file device path is valid. If it is invalid,
1709 try to return the valid device path.
1710 FV address maybe changes for memory layout adjust from time to time, use this funciton
1711 could promise the Fv file device path is right.
1713 @param DevicePath on input, the Fv file device path need to check on
1714 output, the updated valid Fv file device path
1715 @param FileGuid the Fv file guild
1717 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1719 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1721 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1723 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1724 and return the updated device path in DevicePath
1729 BdsLibUpdateFvFileDevicePath (
1730 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1731 IN EFI_GUID
*FileGuid
1734 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1735 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1737 EFI_GUID
*GuidPoint
;
1739 UINTN FvHandleCount
;
1740 EFI_HANDLE
*FvHandleBuffer
;
1741 EFI_FV_FILETYPE Type
;
1743 EFI_FV_FILE_ATTRIBUTES Attributes
;
1744 UINT32 AuthenticationStatus
;
1746 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1747 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1748 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1749 EFI_HANDLE FoundFvHandle
;
1750 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1752 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1753 return EFI_INVALID_PARAMETER
;
1755 if (FileGuid
== NULL
) {
1756 return EFI_INVALID_PARAMETER
;
1760 // Check whether the device path point to the default the input Fv file
1762 TempDevicePath
= *DevicePath
;
1763 LastDeviceNode
= TempDevicePath
;
1764 while (!IsDevicePathEnd (TempDevicePath
)) {
1765 LastDeviceNode
= TempDevicePath
;
1766 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1768 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1769 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1771 if (GuidPoint
== NULL
) {
1773 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1775 return EFI_UNSUPPORTED
;
1777 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1779 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1781 return EFI_UNSUPPORTED
;
1785 // Check whether the input Fv file device path is valid
1787 TempDevicePath
= *DevicePath
;
1788 FoundFvHandle
= NULL
;
1789 Status
= gBS
->LocateDevicePath (
1790 &gEfiFirmwareVolume2ProtocolGuid
,
1794 if (!EFI_ERROR (Status
)) {
1795 Status
= gBS
->HandleProtocol (
1797 &gEfiFirmwareVolume2ProtocolGuid
,
1800 if (!EFI_ERROR (Status
)) {
1802 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1804 Status
= Fv
->ReadFile (
1811 &AuthenticationStatus
1813 if (!EFI_ERROR (Status
)) {
1814 return EFI_ALREADY_STARTED
;
1820 // Look for the input wanted FV file in current FV
1821 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1824 FoundFvHandle
= NULL
;
1825 Status
= gBS
->HandleProtocol (
1827 &gEfiLoadedImageProtocolGuid
,
1828 (VOID
**) &LoadedImage
1830 if (!EFI_ERROR (Status
)) {
1831 Status
= gBS
->HandleProtocol (
1832 LoadedImage
->DeviceHandle
,
1833 &gEfiFirmwareVolume2ProtocolGuid
,
1836 if (!EFI_ERROR (Status
)) {
1837 Status
= Fv
->ReadFile (
1844 &AuthenticationStatus
1846 if (!EFI_ERROR (Status
)) {
1848 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1853 // Second, if fail to find, try to enumerate all FV
1856 FvHandleBuffer
= NULL
;
1857 gBS
->LocateHandleBuffer (
1859 &gEfiFirmwareVolume2ProtocolGuid
,
1864 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1865 gBS
->HandleProtocol (
1866 FvHandleBuffer
[Index
],
1867 &gEfiFirmwareVolume2ProtocolGuid
,
1871 Status
= Fv
->ReadFile (
1878 &AuthenticationStatus
1880 if (EFI_ERROR (Status
)) {
1882 // Skip if input Fv file not in the FV
1887 FoundFvHandle
= FvHandleBuffer
[Index
];
1891 if (FvHandleBuffer
!= NULL
) {
1892 FreePool (FvHandleBuffer
);
1898 // Build the shell device path
1900 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1901 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1902 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1903 *DevicePath
= NewDevicePath
;
1906 return EFI_NOT_FOUND
;