2 BDS Lib functions which relate with create or process the boot
5 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalBdsLib.h"
18 BOOLEAN mEnumBootDevice
= FALSE
;
21 /// This GUID is used for an EFI Variable that stores the front device pathes
22 /// for a partial device path that starts with the HD node.
24 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
29 Boot the legacy system with the boot option
31 @param Option The legacy boot option which have BBS device path
33 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
35 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
40 IN BDS_COMMON_OPTION
*Option
44 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
46 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
47 if (EFI_ERROR (Status
)) {
49 // If no LegacyBios protocol we do not support legacy boot
51 return EFI_UNSUPPORTED
;
54 // Notes: if we seperate the int 19, then we don't need to refresh BBS
56 BdsRefreshBbsTableForBoot (Option
);
59 // Write boot to OS performance data to a file
62 WriteBootToOsPerformanceData ();
65 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
66 return LegacyBios
->LegacyBoot (
68 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
69 Option
->LoadOptionsSize
,
76 Process the boot option follow the EFI 1.1 specification and
77 special treat the legacy boot option with BBS_DEVICE_PATH.
79 @param Option The boot option need to be processed
80 @param DevicePath The device path which describe where to load the
81 boot image or the legcy BBS device path to boot
83 @param ExitDataSize Returned directly from gBS->StartImage ()
84 @param ExitData Returned directly from gBS->StartImage ()
86 @retval EFI_SUCCESS Status from gBS->StartImage ()
87 @retval EFI_NOT_FOUND If the Device Path is not found in the system
92 BdsLibBootViaBootOption (
93 IN BDS_COMMON_OPTION
* Option
,
94 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
95 OUT UINTN
*ExitDataSize
,
96 OUT CHAR16
**ExitData OPTIONAL
101 EFI_HANDLE ImageHandle
;
102 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
103 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
104 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
105 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
106 LIST_ENTRY TempBootLists
;
109 // Record the performance data for End of BDS
111 PERF_END (0, BDS_TOK
, NULL
, 0);
117 // Notes: put EFI64 ROM Shadow Solution
119 EFI64_SHADOW_ALL_LEGACY_ROM ();
122 // Notes: this code can be remove after the s3 script table
123 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
124 // EVT_SIGNAL_LEGACY_BOOT
126 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
127 if (!EFI_ERROR (Status
)) {
128 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
131 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
134 WorkingDevicePath
= NULL
;
135 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
136 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
137 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
138 (HARDDRIVE_DEVICE_PATH
*)DevicePath
140 if (WorkingDevicePath
!= NULL
) {
141 DevicePath
= WorkingDevicePath
;
145 // Signal the EVT_SIGNAL_READY_TO_BOOT event
147 EfiSignalEventReadyToBoot();
155 &gEfiGlobalVariableGuid
,
156 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
161 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
162 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
165 // Check to see if we should legacy BOOT. If yes then do the legacy boot
167 return BdsLibDoLegacyBoot (Option
);
171 // If the boot option point to Internal FV shell, make sure it is valid
173 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
174 if (!EFI_ERROR(Status
)) {
175 if (Option
->DevicePath
!= NULL
) {
176 FreePool(Option
->DevicePath
);
178 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
179 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
181 // Update the shell boot option
183 InitializeListHead (&TempBootLists
);
184 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
187 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
189 FreePool (DevicePath
);
190 DevicePath
= Option
->DevicePath
;
193 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
195 Status
= gBS
->LoadImage (
205 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
206 // and load the image according to the default boot behavior for removable device.
208 if (EFI_ERROR (Status
)) {
210 // check if there is a bootable removable media could be found in this device path ,
211 // and get the bootable media handle
213 Handle
= BdsLibGetBootableHandle(DevicePath
);
214 if (Handle
== NULL
) {
218 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
219 // machinename is ia32, ia64, x64, ...
221 FilePath
= FileDevicePath (Handle
, (CONST CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
));
222 if (FilePath
!= NULL
) {
223 Status
= gBS
->LoadImage (
231 if (EFI_ERROR (Status
)) {
233 // The DevicePath failed, and it's not a valid
234 // removable media device.
241 if (EFI_ERROR (Status
)) {
243 // It there is any error from the Boot attempt exit now.
248 // Provide the image with it's load options
250 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
251 ASSERT_EFI_ERROR (Status
);
253 if (Option
->LoadOptionsSize
!= 0) {
254 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
255 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
258 // Before calling the image, enable the Watchdog Timer for
259 // the 5 Minute period
261 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
263 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
264 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
267 // Clear the Watchdog Timer after the image returns
269 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
273 // Clear Boot Current
277 &gEfiGlobalVariableGuid
,
278 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
288 Expand a device path that starts with a hard drive media device path node to be a
289 full device path that includes the full hardware path to the device. We need
290 to do this so it can be booted. As an optimizaiton the front match (the part point
291 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
292 so a connect all is not required on every boot. All successful history device path
293 which point to partition node (the front part) will be saved.
295 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
296 drive media device path.
297 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
301 EFI_DEVICE_PATH_PROTOCOL
*
303 BdsExpandPartitionPartialDevicePathToFull (
304 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
308 UINTN BlockIoHandleCount
;
309 EFI_HANDLE
*BlockIoBuffer
;
310 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
311 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
312 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
315 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
316 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
317 UINTN CachedDevicePathSize
;
320 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
323 FullDevicePath
= NULL
;
325 // Check if there is prestore 'HDDP' variable.
326 // If exist, search the front path which point to partition node in the variable instants.
327 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
329 CachedDevicePath
= BdsLibGetVariableAndSize (
331 &mHdBootVariablePrivateGuid
,
332 &CachedDevicePathSize
335 if (CachedDevicePath
!= NULL
) {
336 TempNewDevicePath
= CachedDevicePath
;
341 // Check every instance of the variable
342 // First, check wheather the instance contain the partition node, which is needed for distinguishing multi
343 // partial partition boot option. Second, check wheather the instance could be connected.
345 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
346 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
348 // Connect the device path instance, the device path point to hard drive media device path node
349 // e.g. ACPI() /PCI()/ATA()/Partition()
351 Status
= BdsLibConnectDevicePath (Instance
);
352 if (!EFI_ERROR (Status
)) {
358 // Come here means the first instance is not matched
362 } while (TempNewDevicePath
!= NULL
);
366 // Find the matched device path.
367 // Append the file path infomration from the boot option and return the fully expanded device path.
369 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
370 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
373 // Adjust the 'HDDP' instances sequense if the matched one is not first one.
377 // First delete the matched instance.
379 TempNewDevicePath
= CachedDevicePath
;
380 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
381 FreePool (TempNewDevicePath
);
384 // Second, append the remaining parth after the matched instance
386 TempNewDevicePath
= CachedDevicePath
;
387 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
388 FreePool (TempNewDevicePath
);
390 // Save the matching Device Path so we don't need to do a connect all next time
392 Status
= gRT
->SetVariable (
394 &mHdBootVariablePrivateGuid
,
395 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
396 GetDevicePathSize (CachedDevicePath
),
402 FreePool (CachedDevicePath
);
403 return FullDevicePath
;
408 // If we get here we fail to find or 'HDDP' not exist, and now we need
409 // to seach all devices in the system for a matched partition
411 BdsLibConnectAllDriversToAllControllers ();
412 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
413 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0) {
415 // If there was an error or there are no device handles that support
416 // the BLOCK_IO Protocol, then return.
421 // Loop through all the device handles that support the BLOCK_IO Protocol
423 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
425 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
426 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
430 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
432 // Find the matched partition device path
434 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
435 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
438 // Save the matched patition device path in 'HDDP' variable
440 if (CachedDevicePath
!= NULL
) {
442 // Save the matched patition device path as first instance of 'HDDP' variable
444 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
445 TempNewDevicePath
= CachedDevicePath
;
446 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
447 FreePool(TempNewDevicePath
);
449 TempNewDevicePath
= CachedDevicePath
;
450 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
451 FreePool(TempNewDevicePath
);
453 TempNewDevicePath
= CachedDevicePath
;
454 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
455 FreePool(TempNewDevicePath
);
458 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
459 // If the user try to boot many OS in different HDs or partitions, in theary, the 'HDDP' variable maybe become larger and larger.
462 TempNewDevicePath
= CachedDevicePath
;
463 while (!IsDevicePathEnd (TempNewDevicePath
)) {
464 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
466 // Parse one instance
468 while (!IsDevicePathEndType (TempNewDevicePath
)) {
469 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
473 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
475 if (InstanceNum
>= 12) {
476 SetDevicePathEndNode (TempNewDevicePath
);
481 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
485 // Save the matching Device Path so we don't need to do a connect all next time
487 Status
= gRT
->SetVariable (
489 &mHdBootVariablePrivateGuid
,
490 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
491 GetDevicePathSize (CachedDevicePath
),
499 FreePool (CachedDevicePath
);
500 if (BlockIoBuffer
!= NULL
) {
501 FreePool (BlockIoBuffer
);
503 return FullDevicePath
;
507 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
508 instances, has the same partition node with HardDriveDevicePath device path
510 @param BlockIoDevicePath Multi device path instances which need to check
511 @param HardDriveDevicePath A device path which starts with a hard drive media
514 @retval TRUE There is a matched device path instance FALSE
515 @retval FALSE There is no matched device path instance
520 MatchPartitionDevicePathNode (
521 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
522 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
525 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
526 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
528 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
530 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
535 // Make PreviousDevicePath == the device path node before the end node
537 DevicePath
= BlockIoDevicePath
;
538 BlockIoHdDevicePathNode
= NULL
;
541 // find the partition device path node
543 while (!IsDevicePathEnd (DevicePath
)) {
544 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
545 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
547 BlockIoHdDevicePathNode
= DevicePath
;
551 DevicePath
= NextDevicePathNode (DevicePath
);
554 if (BlockIoHdDevicePathNode
== NULL
) {
558 // See if the harddrive device path in blockio matches the orig Hard Drive Node
560 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
564 // Check for the match
566 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
567 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
568 switch (TmpHdPath
->SignatureType
) {
569 case SIGNATURE_TYPE_GUID
:
570 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
572 case SIGNATURE_TYPE_MBR
:
573 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
585 Delete the boot option associated with the handle passed in.
587 @param Handle The handle which present the device path to create
590 @retval EFI_SUCCESS Delete the boot option success
591 @retval EFI_NOT_FOUND If the Device Path is not found in the system
592 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
593 @retval Other Error return value from SetVariable()
597 BdsLibDeleteOptionFromHandle (
602 UINT8
*BootOptionVar
;
604 UINTN BootOptionSize
;
607 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
608 UINTN DevicePathSize
;
609 UINTN OptionDevicePathSize
;
610 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
611 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
614 Status
= EFI_SUCCESS
;
619 // Check "BootOrder" variable, if no, means there is no any boot order.
621 BootOrder
= BdsLibGetVariableAndSize (
623 &gEfiGlobalVariableGuid
,
626 if (BootOrder
== NULL
) {
627 return EFI_NOT_FOUND
;
631 // Convert device handle to device path protocol instance
633 DevicePath
= DevicePathFromHandle (Handle
);
634 if (DevicePath
== NULL
) {
635 return EFI_NOT_FOUND
;
637 DevicePathSize
= GetDevicePathSize (DevicePath
);
640 // Loop all boot order variable and find the matching device path
643 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
644 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
645 BootOptionVar
= BdsLibGetVariableAndSize (
647 &gEfiGlobalVariableGuid
,
651 if (BootOptionVar
== NULL
) {
652 FreePool (BootOrder
);
653 return EFI_OUT_OF_RESOURCES
;
656 TempPtr
= BootOptionVar
;
657 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
658 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
659 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
660 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
663 // Check whether the device path match
665 if ((OptionDevicePathSize
== DevicePathSize
) &&
666 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
667 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
668 FreePool (BootOptionVar
);
672 FreePool (BootOptionVar
);
677 // Adjust number of boot option for "BootOrder" variable.
679 Status
= gRT
->SetVariable (
681 &gEfiGlobalVariableGuid
,
682 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
687 FreePool (BootOrder
);
694 Delete all invalid EFI boot options. The probable invalid boot option could
695 be Removable media or Network boot device.
697 @retval EFI_SUCCESS Delete all invalid boot option success
698 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
699 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
700 @retval Other Error return value from SetVariable()
704 BdsDeleteAllInvalidEfiBootOption (
709 UINT8
*BootOptionVar
;
711 UINTN BootOptionSize
;
715 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
716 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
719 Status
= EFI_SUCCESS
;
724 // Check "BootOrder" variable firstly, this variable hold the number of boot options
726 BootOrder
= BdsLibGetVariableAndSize (
728 &gEfiGlobalVariableGuid
,
731 if (NULL
== BootOrder
) {
732 return EFI_NOT_FOUND
;
736 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
737 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
738 BootOptionVar
= BdsLibGetVariableAndSize (
740 &gEfiGlobalVariableGuid
,
743 if (NULL
== BootOptionVar
) {
744 FreePool (BootOrder
);
745 return EFI_OUT_OF_RESOURCES
;
748 TempPtr
= BootOptionVar
;
749 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
750 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
751 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
754 // Skip legacy boot option (BBS boot device)
756 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
757 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
758 FreePool (BootOptionVar
);
763 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
765 // Delete this invalid boot option "Boot####"
767 Status
= gRT
->SetVariable (
769 &gEfiGlobalVariableGuid
,
770 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
775 // Mark this boot option in boot order as deleted
777 BootOrder
[Index
] = 0xffff;
780 FreePool (BootOptionVar
);
785 // Adjust boot order array
788 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
789 if (BootOrder
[Index
] != 0xffff) {
790 BootOrder
[Index2
] = BootOrder
[Index
];
794 Status
= gRT
->SetVariable (
796 &gEfiGlobalVariableGuid
,
797 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
798 Index2
* sizeof (UINT16
),
802 FreePool (BootOrder
);
809 This function will enumerate all possible boot device in the system,
810 it will only excute once of every boot.
812 @param BdsBootOptionList The header of the link list which indexed all
815 @retval EFI_SUCCESS Finished all the boot device enumerate and create
816 the boot option base on that boot device
821 BdsLibEnumerateAllBootOption (
822 IN OUT LIST_ENTRY
*BdsBootOptionList
830 UINT16 NonBlockNumber
;
831 UINTN NumberBlockIoHandles
;
832 EFI_HANDLE
*BlockIoHandles
;
833 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
835 UINTN NumberSimpleNetworkHandles
;
836 EFI_HANDLE
*SimpleNetworkHandles
;
838 EFI_HANDLE
*FvHandleBuffer
;
839 EFI_FV_FILETYPE Type
;
841 EFI_FV_FILE_ATTRIBUTES Attributes
;
842 UINT32 AuthenticationStatus
;
843 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
844 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
845 UINTN DevicePathType
;
847 EFI_HANDLE
*FileSystemHandles
;
848 UINTN NumberFileSystemHandles
;
850 EFI_IMAGE_DOS_HEADER DosHeader
;
851 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
852 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
858 ZeroMem (Buffer
, sizeof (Buffer
));
861 // If the boot device enumerate happened, just get the boot
862 // device from the boot order variable
864 if (mEnumBootDevice
) {
865 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
870 // Notes: this dirty code is to get the legacy boot option from the
871 // BBS table and create to variable as the EFI boot option, it should
872 // be removed after the CSM can provide legacy boot option directly
874 REFRESH_LEGACY_BOOT_OPTIONS
;
877 // Delete invalid boot option
879 BdsDeleteAllInvalidEfiBootOption ();
882 // Parse removable media
884 gBS
->LocateHandleBuffer (
886 &gEfiBlockIoProtocolGuid
,
888 &NumberBlockIoHandles
,
892 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
893 Status
= gBS
->HandleProtocol (
894 BlockIoHandles
[Index
],
895 &gEfiBlockIoProtocolGuid
,
898 if (!EFI_ERROR (Status
)) {
899 if (!BlkIo
->Media
->RemovableMedia
) {
901 // skip the non-removable block devices
906 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
907 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
909 switch (DevicePathType
) {
910 case BDS_EFI_ACPI_FLOPPY_BOOT
:
911 if (FloppyNumber
== 0) {
912 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
914 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
916 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
920 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
921 if (CdromNumber
== 0) {
922 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
924 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
926 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
930 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
931 if (UsbNumber
== 0) {
932 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
934 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
936 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
940 case BDS_EFI_MESSAGE_SCSI_BOOT
:
941 if (UsbNumber
== 0) {
942 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
944 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
946 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
950 case BDS_EFI_MESSAGE_MISC_BOOT
:
951 if (MiscNumber
== 0) {
952 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
954 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
956 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
965 if (NumberBlockIoHandles
!= 0) {
966 FreePool (BlockIoHandles
);
970 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
973 gBS
->LocateHandleBuffer (
975 &gEfiSimpleFileSystemProtocolGuid
,
977 &NumberFileSystemHandles
,
980 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
981 Status
= gBS
->HandleProtocol (
982 FileSystemHandles
[Index
],
983 &gEfiBlockIoProtocolGuid
,
986 if (!EFI_ERROR (Status
)) {
988 // Skip if the file system handle supports a BlkIo protocol,
994 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
995 // machinename is ia32, ia64, x64, ...
997 Hdr
.Union
= &HdrData
;
999 Status
= BdsLibGetImageHeader (
1000 FileSystemHandles
[Index
],
1001 (CHAR16
*)PcdGetPtr (PcdDefaultBootFileName
),
1005 if (!EFI_ERROR (Status
) &&
1006 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1007 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1013 // No such file or the file is not a EFI application, delete this boot option
1015 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1017 if (NonBlockNumber
== 0) {
1018 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1020 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1022 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1027 if (NumberFileSystemHandles
!= 0) {
1028 FreePool (FileSystemHandles
);
1032 // Parse Network Boot Device
1034 gBS
->LocateHandleBuffer (
1036 &gEfiSimpleNetworkProtocolGuid
,
1038 &NumberSimpleNetworkHandles
,
1039 &SimpleNetworkHandles
1041 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1043 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1045 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1047 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1050 if (NumberSimpleNetworkHandles
!= 0) {
1051 FreePool (SimpleNetworkHandles
);
1055 // Check if we have on flash shell
1057 gBS
->LocateHandleBuffer (
1059 &gEfiFirmwareVolume2ProtocolGuid
,
1064 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1066 // Only care the dispatched FV. If no dispatch protocol on the FV, it is not dispatched, then skip it.
1068 Status
= gBS
->HandleProtocol (
1069 FvHandleBuffer
[Index
],
1070 &gEfiFirmwareVolumeDispatchProtocolGuid
,
1073 if (EFI_ERROR (Status
)) {
1077 gBS
->HandleProtocol (
1078 FvHandleBuffer
[Index
],
1079 &gEfiFirmwareVolume2ProtocolGuid
,
1083 Status
= Fv
->ReadFile (
1090 &AuthenticationStatus
1092 if (EFI_ERROR (Status
)) {
1094 // Skip if no shell file in the FV
1099 // Build the shell boot option
1101 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1104 if (FvHandleCount
!= 0) {
1105 FreePool (FvHandleBuffer
);
1108 // Make sure every boot only have one time
1109 // boot device enumerate
1111 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1112 mEnumBootDevice
= TRUE
;
1118 Build the boot option with the handle parsed in.
1120 @param Handle The handle which present the device path to create
1122 @param BdsBootOptionList The header of the link list which indexed all
1123 current boot options
1124 @param String Boot option name.
1129 BdsLibBuildOptionFromHandle (
1130 IN EFI_HANDLE Handle
,
1131 IN LIST_ENTRY
*BdsBootOptionList
,
1135 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1137 DevicePath
= DevicePathFromHandle (Handle
);
1140 // Create and register new boot option
1142 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1147 Build the on flash shell boot option with the handle parsed in.
1149 @param Handle The handle which present the device path to create
1150 on flash shell boot option
1151 @param BdsBootOptionList The header of the link list which indexed all
1152 current boot options
1157 BdsLibBuildOptionFromShell (
1158 IN EFI_HANDLE Handle
,
1159 IN OUT LIST_ENTRY
*BdsBootOptionList
1162 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1163 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1165 DevicePath
= DevicePathFromHandle (Handle
);
1168 // Build the shell device path
1170 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1172 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1175 // Create and register the shell boot option
1177 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1182 Boot from the EFI1.1 spec defined "BootNext" variable
1194 BDS_COMMON_OPTION
*BootOption
;
1195 LIST_ENTRY TempList
;
1200 // Init the boot option name buffer and temp link list
1202 InitializeListHead (&TempList
);
1203 ZeroMem (Buffer
, sizeof (Buffer
));
1205 BootNext
= BdsLibGetVariableAndSize (
1207 &gEfiGlobalVariableGuid
,
1212 // Clear the boot next variable first
1214 if (BootNext
!= NULL
) {
1217 &gEfiGlobalVariableGuid
,
1218 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1224 // Start to build the boot option and try to boot
1226 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1227 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1228 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1229 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1235 Return the bootable media handle.
1236 First, check the device is connected
1237 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1238 Third, detect the the default boot file in the Media, and return the removable Media handle.
1240 @param DevicePath Device Path to a bootable device
1242 @retval NULL The device path points to an EFI bootable Media
1243 @retval NULL The media on the DevicePath is not bootable
1248 BdsLibGetBootableHandle (
1249 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1253 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1254 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1256 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1258 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1261 EFI_HANDLE ReturnHandle
;
1262 EFI_HANDLE
*SimpleFileSystemHandles
;
1264 UINTN NumberSimpleFileSystemHandles
;
1266 EFI_IMAGE_DOS_HEADER DosHeader
;
1267 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1268 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1270 UpdatedDevicePath
= DevicePath
;
1273 // Check whether the device is connected
1275 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1276 if (EFI_ERROR (Status
)) {
1278 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1280 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1281 if (EFI_ERROR (Status
)) {
1283 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1285 UpdatedDevicePath
= DevicePath
;
1286 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1287 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1291 // Get BlockIo protocal and check removable attribute
1293 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1295 // Issue a dummy read to the device to check for media change.
1296 // When the removable media is changed, any Block IO read/write will
1297 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1298 // returned. After the Block IO protocol is reinstalled, subsequent
1299 // Block IO read/write will success.
1301 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1302 if (Buffer
!= NULL
) {
1303 BlockIo
->ReadBlocks (
1305 BlockIo
->Media
->MediaId
,
1307 BlockIo
->Media
->BlockSize
,
1315 // Detect the the default boot file from removable Media
1319 // 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
1320 // Try to locate the USB node device path first, if fail then use its previour PCI node to search
1322 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1323 ASSERT (DupDevicePath
!= NULL
);
1325 UpdatedDevicePath
= DupDevicePath
;
1326 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1328 // 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
1329 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1331 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1332 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1334 // Remove the usb node, let the device path only point to PCI node
1336 SetDevicePathEndNode (UpdatedDevicePath
);
1337 UpdatedDevicePath
= DupDevicePath
;
1339 UpdatedDevicePath
= DevicePath
;
1343 // Get the device path size of boot option
1345 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1346 ReturnHandle
= NULL
;
1347 gBS
->LocateHandleBuffer (
1349 &gEfiSimpleFileSystemProtocolGuid
,
1351 &NumberSimpleFileSystemHandles
,
1352 &SimpleFileSystemHandles
1354 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1356 // Get the device path size of SimpleFileSystem handle
1358 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1359 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1361 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1363 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1365 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1366 // machinename is ia32, ia64, x64, ...
1368 Hdr
.Union
= &HdrData
;
1369 Status
= BdsLibGetImageHeader (
1370 SimpleFileSystemHandles
[Index
],
1371 (CHAR16
*)PcdGetPtr(PcdDefaultBootFileName
),
1375 if (!EFI_ERROR (Status
) &&
1376 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1377 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1378 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1384 FreePool(DupDevicePath
);
1386 if (SimpleFileSystemHandles
!= NULL
) {
1387 FreePool(SimpleFileSystemHandles
);
1390 return ReturnHandle
;
1394 Check to see if the network cable is plugged in. If the DevicePath is not
1395 connected it will be connected.
1397 @param DevicePath Device Path to check
1399 @retval TRUE DevicePath points to an Network that is connected
1400 @retval FALSE DevicePath does not point to a bootable network
1404 BdsLibNetworkBootWithMediaPresent (
1405 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1409 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1411 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1412 BOOLEAN MediaPresent
;
1414 MediaPresent
= FALSE
;
1416 UpdatedDevicePath
= DevicePath
;
1417 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1418 if (EFI_ERROR (Status
)) {
1420 // Device not present so see if we need to connect it
1422 Status
= BdsLibConnectDevicePath (DevicePath
);
1423 if (!EFI_ERROR (Status
)) {
1425 // This one should work after we did the connect
1427 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1431 if (!EFI_ERROR (Status
)) {
1432 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1433 if (!EFI_ERROR (Status
)) {
1434 if (Snp
->Mode
->MediaPresentSupported
) {
1435 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1437 // In case some one else is using the SNP check to see if it's connected
1439 MediaPresent
= Snp
->Mode
->MediaPresent
;
1442 // No one is using SNP so we need to Start and Initialize so
1443 // MediaPresent will be valid.
1445 Status
= Snp
->Start (Snp
);
1446 if (!EFI_ERROR (Status
)) {
1447 Status
= Snp
->Initialize (Snp
, 0, 0);
1448 if (!EFI_ERROR (Status
)) {
1449 MediaPresent
= Snp
->Mode
->MediaPresent
;
1450 Snp
->Shutdown (Snp
);
1456 MediaPresent
= TRUE
;
1461 return MediaPresent
;
1465 For a bootable Device path, return its boot type.
1467 @param DevicePath The bootable device Path to check
1469 @retval BDS_EFI_MEDIA_HD_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_MEDIA_CDROM_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_ACPI_FLOPPY_BOOT If the device path contains any media deviec path node, it is media boot type
1474 For the floppy node, handle it as media node
1475 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media deviec path node, and
1476 its last device path node point to a message device path node, it is
1478 @retval BDS_EFI_MESSAGE_SCSI_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_USB_DEVICE_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_EFI_MESSAGE_MISC_BOOT If the device path not contains any media deviec path node, and
1483 its last device path node point to a message device path node, it is
1484 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1485 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message devie,
1490 BdsGetBootTypeFromDevicePath (
1491 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1494 ACPI_HID_DEVICE_PATH
*Acpi
;
1495 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1496 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1499 if (NULL
== DevicePath
) {
1500 return BDS_EFI_UNSUPPORT
;
1503 TempDevicePath
= DevicePath
;
1505 while (!IsDevicePathEndType (TempDevicePath
)) {
1506 switch (DevicePathType (TempDevicePath
)) {
1507 case BBS_DEVICE_PATH
:
1508 return BDS_LEGACY_BBS_BOOT
;
1509 case MEDIA_DEVICE_PATH
:
1510 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1511 return BDS_EFI_MEDIA_HD_BOOT
;
1512 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1513 return BDS_EFI_MEDIA_CDROM_BOOT
;
1516 case ACPI_DEVICE_PATH
:
1517 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1518 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1519 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1522 case MESSAGING_DEVICE_PATH
:
1524 // Get the last device path node
1526 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1527 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
1529 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1532 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
1535 // if the device path not only point to driver device, it is not a messaging device path,
1537 if (!IsDevicePathEndType (LastDeviceNode
)) {
1541 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1542 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1543 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1544 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1545 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1546 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1548 return BDS_EFI_MESSAGE_MISC_BOOT
;
1552 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1555 return BDS_EFI_UNSUPPORT
;
1559 Check whether the Device path in a boot option point to a valide bootable device,
1560 And if CheckMedia is true, check the device is ready to boot now.
1562 @param DevPath the Device path in a boot option
1563 @param CheckMedia if true, check the device is ready to boot now.
1565 @retval TRUE the Device path is valide
1566 @retval FALSE the Device path is invalide .
1571 BdsLibIsValidEFIBootOptDevicePath (
1572 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1573 IN BOOLEAN CheckMedia
1578 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1579 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1580 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1582 TempDevicePath
= DevPath
;
1583 LastDeviceNode
= DevPath
;
1586 // Check if it's a valid boot option for network boot device
1587 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1588 // there is the network card there.
1590 Status
= gBS
->LocateDevicePath (
1591 &gEfiSimpleNetworkProtocolGuid
,
1595 if (EFI_ERROR (Status
)) {
1597 // Device not present so see if we need to connect it
1599 TempDevicePath
= DevPath
;
1600 BdsLibConnectDevicePath (TempDevicePath
);
1601 Status
= gBS
->LocateDevicePath (
1602 &gEfiSimpleNetworkProtocolGuid
,
1608 if (!EFI_ERROR (Status
)) {
1611 // Test if it is ready to boot now
1613 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1622 // If the boot option point to a file, it is a valid EFI boot option,
1623 // and assume it is ready to boot now
1625 while (!IsDevicePathEnd (TempDevicePath
)) {
1626 LastDeviceNode
= TempDevicePath
;
1627 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1629 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1630 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1635 // Check if it's a valid boot option for internal Shell
1637 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1639 // If the boot option point to Internal FV shell, make sure it is valid
1641 TempDevicePath
= DevPath
;
1642 Status
= BdsLibUpdateFvFileDevicePath (&TempDevicePath
, &gEfiShellFileGuid
);
1643 if (Status
== EFI_ALREADY_STARTED
) {
1646 if (Status
== EFI_SUCCESS
) {
1647 FreePool (TempDevicePath
);
1654 // 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
1656 TempDevicePath
= DevPath
;
1657 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1658 if (EFI_ERROR (Status
)) {
1660 // Device not present so see if we need to connect it
1662 Status
= BdsLibConnectDevicePath (DevPath
);
1663 if (!EFI_ERROR (Status
)) {
1665 // Try again to get the Block Io protocol after we did the connect
1667 TempDevicePath
= DevPath
;
1668 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1672 if (!EFI_ERROR (Status
)) {
1673 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1674 if (!EFI_ERROR (Status
)) {
1677 // Test if it is ready to boot now
1679 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1688 // 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,
1690 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1691 if (!EFI_ERROR (Status
)) {
1694 // Test if it is ready to boot now
1696 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1710 According to a file guild, check a Fv file device path is valid. If it is invalid,
1711 try to return the valid device path.
1712 FV address maybe changes for memory layout adjust from time to time, use this funciton
1713 could promise the Fv file device path is right.
1715 @param DevicePath on input, the Fv file device path need to check on
1716 output, the updated valid Fv file device path
1717 @param FileGuid the Fv file guild
1719 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1721 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1723 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1725 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1726 and return the updated device path in DevicePath
1731 BdsLibUpdateFvFileDevicePath (
1732 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1733 IN EFI_GUID
*FileGuid
1736 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1737 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1739 EFI_GUID
*GuidPoint
;
1741 UINTN FvHandleCount
;
1742 EFI_HANDLE
*FvHandleBuffer
;
1743 EFI_FV_FILETYPE Type
;
1745 EFI_FV_FILE_ATTRIBUTES Attributes
;
1746 UINT32 AuthenticationStatus
;
1748 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1749 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1750 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1751 EFI_HANDLE FoundFvHandle
;
1752 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1754 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1755 return EFI_INVALID_PARAMETER
;
1757 if (FileGuid
== NULL
) {
1758 return EFI_INVALID_PARAMETER
;
1762 // Check whether the device path point to the default the input Fv file
1764 TempDevicePath
= *DevicePath
;
1765 LastDeviceNode
= TempDevicePath
;
1766 while (!IsDevicePathEnd (TempDevicePath
)) {
1767 LastDeviceNode
= TempDevicePath
;
1768 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1770 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1771 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1773 if (GuidPoint
== NULL
) {
1775 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1777 return EFI_UNSUPPORTED
;
1779 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1781 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1783 return EFI_UNSUPPORTED
;
1787 // Check whether the input Fv file device path is valid
1789 TempDevicePath
= *DevicePath
;
1790 FoundFvHandle
= NULL
;
1791 Status
= gBS
->LocateDevicePath (
1792 &gEfiFirmwareVolume2ProtocolGuid
,
1796 if (!EFI_ERROR (Status
)) {
1797 Status
= gBS
->HandleProtocol (
1799 &gEfiFirmwareVolume2ProtocolGuid
,
1802 if (!EFI_ERROR (Status
)) {
1804 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1806 Status
= Fv
->ReadFile (
1813 &AuthenticationStatus
1815 if (!EFI_ERROR (Status
)) {
1816 return EFI_ALREADY_STARTED
;
1822 // Look for the input wanted FV file in current FV
1823 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1826 FoundFvHandle
= NULL
;
1827 Status
= gBS
->HandleProtocol (
1829 &gEfiLoadedImageProtocolGuid
,
1830 (VOID
**) &LoadedImage
1832 if (!EFI_ERROR (Status
)) {
1833 Status
= gBS
->HandleProtocol (
1834 LoadedImage
->DeviceHandle
,
1835 &gEfiFirmwareVolume2ProtocolGuid
,
1838 if (!EFI_ERROR (Status
)) {
1839 Status
= Fv
->ReadFile (
1846 &AuthenticationStatus
1848 if (!EFI_ERROR (Status
)) {
1850 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1855 // Second, if fail to find, try to enumerate all FV
1858 FvHandleBuffer
= NULL
;
1859 gBS
->LocateHandleBuffer (
1861 &gEfiFirmwareVolume2ProtocolGuid
,
1866 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1867 gBS
->HandleProtocol (
1868 FvHandleBuffer
[Index
],
1869 &gEfiFirmwareVolume2ProtocolGuid
,
1873 Status
= Fv
->ReadFile (
1880 &AuthenticationStatus
1882 if (EFI_ERROR (Status
)) {
1884 // Skip if input Fv file not in the FV
1889 FoundFvHandle
= FvHandleBuffer
[Index
];
1893 if (FvHandleBuffer
!= NULL
) {
1894 FreePool (FvHandleBuffer
);
1900 // Build the shell device path
1902 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1903 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1904 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1905 *DevicePath
= NewDevicePath
;
1908 return EFI_NOT_FOUND
;