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 EFI_EVENT_SIGNAL_READY_TO_BOOT or
124 // EFI_EVENT_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 EFI_EVENT_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 SafeFreePool(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");
188 // Drop the TPL level from TPL_APPLICATION to TPL_APPLICATION
190 gBS
->RestoreTPL (TPL_APPLICATION
);
192 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting EFI way %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
, DEFAULT_REMOVABLE_FILE_NAME
);
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
,
283 // Raise the TPL level back to TPL_APPLICATION
285 gBS
->RaiseTPL (TPL_APPLICATION
);
292 Expand a device path that starts with a hard drive media device path node to be a
293 full device path that includes the full hardware path to the device. We need
294 to do this so it can be booted. As an optimizaiton the front match (the part point
295 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
296 so a connect all is not required on every boot. All successful history device path
297 which point to partition node (the front part) will be saved.
299 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
300 drive media device path.
301 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
305 EFI_DEVICE_PATH_PROTOCOL
*
307 BdsExpandPartitionPartialDevicePathToFull (
308 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
312 UINTN BlockIoHandleCount
;
313 EFI_HANDLE
*BlockIoBuffer
;
314 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
315 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
316 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
319 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
320 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
321 UINTN CachedDevicePathSize
;
324 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
327 FullDevicePath
= NULL
;
329 // Check if there is prestore 'HDDP' variable.
330 // If exist, search the front path which point to partition node in the variable instants.
331 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
333 CachedDevicePath
= BdsLibGetVariableAndSize (
335 &mHdBootVariablePrivateGuid
,
336 &CachedDevicePathSize
338 if (CachedDevicePath
!= NULL
) {
339 TempNewDevicePath
= CachedDevicePath
;
344 // Check every instance of the variable
345 // First, check wheather the instance contain the partition node, which is needed for distinguishing multi
346 // partial partition boot option. Second, check wheather the instance could be connected.
348 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
349 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
351 // Connect the device path instance, the device path point to hard drive media device path node
352 // e.g. ACPI() /PCI()/ATA()/Partition()
354 Status
= BdsLibConnectDevicePath (Instance
);
355 if (!EFI_ERROR (Status
)) {
361 // Come here means the first instance is not matched
364 SafeFreePool(Instance
);
365 } while (TempNewDevicePath
!= NULL
);
369 // Find the matched device path.
370 // Append the file path infomration from the boot option and return the fully expanded device path.
372 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
373 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
376 // Adjust the 'HDDP' instances sequense if the matched one is not first one.
380 // First delete the matched instance.
382 TempNewDevicePath
= CachedDevicePath
;
383 CachedDevicePath
= BdsLibDelPartMatchInstance ( CachedDevicePath
, Instance
);
384 SafeFreePool (TempNewDevicePath
);
386 // Second, append the remaining parth after the matched instance
388 TempNewDevicePath
= CachedDevicePath
;
389 CachedDevicePath
= AppendDevicePathInstance ( Instance
, CachedDevicePath
);
390 SafeFreePool (TempNewDevicePath
);
392 // Save the matching Device Path so we don't need to do a connect all next time
394 Status
= gRT
->SetVariable (
396 &mHdBootVariablePrivateGuid
,
397 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
398 GetDevicePathSize (CachedDevicePath
),
402 SafeFreePool(Instance
);
403 gBS
->FreePool (CachedDevicePath
);
404 return FullDevicePath
;
409 // If we get here we fail to find or 'HDDP' not exist, and now we need
410 // to seach all devices in the system for a matched partition
412 BdsLibConnectAllDriversToAllControllers ();
413 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
414 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0) {
416 // If there was an error or there are no device handles that support
417 // the BLOCK_IO Protocol, then return.
422 // Loop through all the device handles that support the BLOCK_IO Protocol
424 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
426 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
427 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
431 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
433 // Find the matched partition device path
435 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
436 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
439 // Save the matched patition device path in 'HDDP' variable
441 if (CachedDevicePath
!= NULL
) {
443 // Save the matched patition device path as first instance of 'HDDP' variable
445 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
446 TempNewDevicePath
= CachedDevicePath
;
447 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
448 SafeFreePool(TempNewDevicePath
);
450 TempNewDevicePath
= CachedDevicePath
;
451 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
452 SafeFreePool(TempNewDevicePath
);
454 TempNewDevicePath
= CachedDevicePath
;
455 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
456 SafeFreePool(TempNewDevicePath
);
459 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
460 // If the user try to boot many OS in different HDs or partitions, in theary, the 'HDDP' variable maybe become larger and larger.
463 TempNewDevicePath
= CachedDevicePath
;
464 while (!IsDevicePathEnd (TempNewDevicePath
)) {
465 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
467 // Parse one instance
469 while (!IsDevicePathEndType (TempNewDevicePath
)) {
470 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
474 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
476 if (InstanceNum
>= 12) {
477 SetDevicePathEndNode (TempNewDevicePath
);
482 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
486 // Save the matching Device Path so we don't need to do a connect all next time
488 Status
= gRT
->SetVariable (
490 &mHdBootVariablePrivateGuid
,
491 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
492 GetDevicePathSize (CachedDevicePath
),
499 gBS
->FreePool (CachedDevicePath
);
500 gBS
->FreePool (BlockIoBuffer
);
501 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 FALSE
514 -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 HARDDRIVE_DEVICE_PATH
*TempPath
;
526 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
528 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
530 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
;
560 TempPath
= (HARDDRIVE_DEVICE_PATH
*) BdsLibUnpackDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
563 // Check for the match
565 if ((TmpHdPath
->MBRType
== TempPath
->MBRType
) &&
566 (TmpHdPath
->SignatureType
== TempPath
->SignatureType
)) {
567 switch (TmpHdPath
->SignatureType
) {
568 case SIGNATURE_TYPE_GUID
:
569 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)TempPath
->Signature
);
571 case SIGNATURE_TYPE_MBR
:
572 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == *(UINT32
*)(&(TempPath
->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
;
618 BootOrder
= BdsLibGetVariableAndSize (
620 &gEfiGlobalVariableGuid
,
623 if (NULL
== BootOrder
) {
624 return EFI_NOT_FOUND
;
627 DevicePath
= DevicePathFromHandle (Handle
);
628 if (DevicePath
== NULL
) {
629 return EFI_NOT_FOUND
;
631 DevicePathSize
= GetDevicePathSize (DevicePath
);
634 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
635 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
636 BootOptionVar
= BdsLibGetVariableAndSize (
638 &gEfiGlobalVariableGuid
,
641 if (NULL
== BootOptionVar
) {
642 gBS
->FreePool (BootOrder
);
643 return EFI_OUT_OF_RESOURCES
;
646 TempPtr
= BootOptionVar
;
647 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
648 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
649 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
650 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
653 // Check whether the device path match
655 if ((OptionDevicePathSize
== DevicePathSize
) &&
656 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
657 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
658 gBS
->FreePool (BootOptionVar
);
662 gBS
->FreePool (BootOptionVar
);
666 Status
= gRT
->SetVariable (
668 &gEfiGlobalVariableGuid
,
669 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
674 gBS
->FreePool (BootOrder
);
681 Delete all invalid EFI boot options. The probable invalid boot option could
682 be Removable media or Network boot device.
686 @retval EFI_SUCCESS Delete all invalid boot option success
687 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
688 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
689 @retval Other Error return value from SetVariable()
693 BdsDeleteAllInvalidEfiBootOption (
698 UINT8
*BootOptionVar
;
700 UINTN BootOptionSize
;
704 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
705 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
708 Status
= EFI_SUCCESS
;
712 BootOrder
= BdsLibGetVariableAndSize (
714 &gEfiGlobalVariableGuid
,
717 if (NULL
== BootOrder
) {
718 return EFI_NOT_FOUND
;
722 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
723 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
724 BootOptionVar
= BdsLibGetVariableAndSize (
726 &gEfiGlobalVariableGuid
,
729 if (NULL
== BootOptionVar
) {
730 gBS
->FreePool (BootOrder
);
731 return EFI_OUT_OF_RESOURCES
;
734 TempPtr
= BootOptionVar
;
735 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
736 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
737 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
740 // Skip legacy boot option (BBS boot device)
742 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
743 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
744 gBS
->FreePool (BootOptionVar
);
749 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
751 // Delete this invalid boot option "Boot####"
753 Status
= gRT
->SetVariable (
755 &gEfiGlobalVariableGuid
,
756 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
761 // Mark this boot option in boot order as deleted
763 BootOrder
[Index
] = 0xffff;
766 gBS
->FreePool (BootOptionVar
);
771 // Adjust boot order array
774 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
775 if (BootOrder
[Index
] != 0xffff) {
776 BootOrder
[Index2
] = BootOrder
[Index
];
780 Status
= gRT
->SetVariable (
782 &gEfiGlobalVariableGuid
,
783 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
784 Index2
* sizeof (UINT16
),
788 gBS
->FreePool (BootOrder
);
795 This function will enumerate all possible boot device in the system,
796 it will only excute once of every boot.
798 @param BdsBootOptionList The header of the link list which indexed all
801 @retval EFI_SUCCESS Finished all the boot device enumerate and create
802 the boot option base on that boot device
807 BdsLibEnumerateAllBootOption (
808 IN OUT LIST_ENTRY
*BdsBootOptionList
816 UINT16 NonBlockNumber
;
817 UINTN NumberBlockIoHandles
;
818 EFI_HANDLE
*BlockIoHandles
;
819 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
821 UINTN NumberSimpleNetworkHandles
;
822 EFI_HANDLE
*SimpleNetworkHandles
;
824 EFI_HANDLE
*FvHandleBuffer
;
825 EFI_FV_FILETYPE Type
;
827 EFI_FV_FILE_ATTRIBUTES Attributes
;
828 UINT32 AuthenticationStatus
;
829 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
830 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
831 UINTN DevicePathType
;
833 EFI_HANDLE
*FileSystemHandles
;
834 UINTN NumberFileSystemHandles
;
836 EFI_IMAGE_DOS_HEADER DosHeader
;
837 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
838 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
844 ZeroMem (Buffer
, sizeof (Buffer
));
846 // If the boot device enumerate happened, just get the boot
847 // device from the boot order variable
849 if (mEnumBootDevice
) {
850 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
854 // Notes: this dirty code is to get the legacy boot option from the
855 // BBS table and create to variable as the EFI boot option, it should
856 // be removed after the CSM can provide legacy boot option directly
858 REFRESH_LEGACY_BOOT_OPTIONS
;
861 // Delete invalid boot option
863 BdsDeleteAllInvalidEfiBootOption ();
865 // Parse removable media
867 gBS
->LocateHandleBuffer (
869 &gEfiBlockIoProtocolGuid
,
871 &NumberBlockIoHandles
,
874 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
875 Status
= gBS
->HandleProtocol (
876 BlockIoHandles
[Index
],
877 &gEfiBlockIoProtocolGuid
,
880 if (!EFI_ERROR (Status
)) {
881 if (!BlkIo
->Media
->RemovableMedia
) {
883 // skip the non-removable block devices
888 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
889 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
891 switch (DevicePathType
) {
892 case BDS_EFI_ACPI_FLOPPY_BOOT
:
893 if (FloppyNumber
== 0) {
894 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
896 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
898 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
902 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
903 if (CdromNumber
== 0) {
904 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
906 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
908 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
912 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
913 if (UsbNumber
== 0) {
914 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
916 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
918 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
922 case BDS_EFI_MESSAGE_SCSI_BOOT
:
923 if (UsbNumber
== 0) {
924 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
926 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
928 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
932 case BDS_EFI_MESSAGE_MISC_BOOT
:
933 if (MiscNumber
== 0) {
934 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
936 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
938 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
947 if (NumberBlockIoHandles
) {
948 gBS
->FreePool (BlockIoHandles
);
952 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
955 gBS
->LocateHandleBuffer (
957 &gEfiSimpleFileSystemProtocolGuid
,
959 &NumberFileSystemHandles
,
962 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
963 Status
= gBS
->HandleProtocol (
964 FileSystemHandles
[Index
],
965 &gEfiBlockIoProtocolGuid
,
968 if (!EFI_ERROR (Status
)) {
970 // Skip if the file system handle supports a BlkIo protocol,
976 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
977 // machinename is ia32, ia64, x64, ...
979 Hdr
.Union
= &HdrData
;
981 Status
= BdsLibGetImageHeader (
982 FileSystemHandles
[Index
],
983 DEFAULT_REMOVABLE_FILE_NAME
,
987 if (!EFI_ERROR (Status
) &&
988 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
989 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
995 // No such file or the file is not a EFI application, delete this boot option
997 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
999 if (NonBlockNumber
== 0) {
1000 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1002 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1004 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1009 if (NumberFileSystemHandles
) {
1010 gBS
->FreePool (FileSystemHandles
);
1014 // Parse Network Boot Device
1016 gBS
->LocateHandleBuffer (
1018 &gEfiSimpleNetworkProtocolGuid
,
1020 &NumberSimpleNetworkHandles
,
1021 &SimpleNetworkHandles
1023 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1025 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1027 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1029 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1032 if (NumberSimpleNetworkHandles
) {
1033 gBS
->FreePool (SimpleNetworkHandles
);
1037 // Check if we have on flash shell
1039 gBS
->LocateHandleBuffer (
1041 &gEfiFirmwareVolume2ProtocolGuid
,
1046 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1048 // Only care the dispatched FV. If no dispatch protocol on the FV, it is not dispatched, then skip it.
1050 Status
= gBS
->HandleProtocol (
1051 FvHandleBuffer
[Index
],
1052 &gEfiFirmwareVolumeDispatchProtocolGuid
,
1055 if (EFI_ERROR (Status
)) {
1059 gBS
->HandleProtocol (
1060 FvHandleBuffer
[Index
],
1061 &gEfiFirmwareVolume2ProtocolGuid
,
1065 Status
= Fv
->ReadFile (
1072 &AuthenticationStatus
1074 if (EFI_ERROR (Status
)) {
1076 // Skip if no shell file in the FV
1081 // Build the shell boot option
1083 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1086 if (FvHandleCount
) {
1087 gBS
->FreePool (FvHandleBuffer
);
1090 // Make sure every boot only have one time
1091 // boot device enumerate
1093 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1094 mEnumBootDevice
= TRUE
;
1101 Build the boot option with the handle parsed in
1103 @param Handle The handle which present the device path to create
1105 @param BdsBootOptionList The header of the link list which indexed all
1106 current boot options
1113 BdsLibBuildOptionFromHandle (
1114 IN EFI_HANDLE Handle
,
1115 IN LIST_ENTRY
*BdsBootOptionList
,
1119 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1121 DevicePath
= DevicePathFromHandle (Handle
);
1124 // Create and register new boot option
1126 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1131 Build the on flash shell boot option with the handle parsed in
1133 @param Handle The handle which present the device path to create
1134 on flash shell boot option
1135 @param BdsBootOptionList The header of the link list which indexed all
1136 current boot options
1143 BdsLibBuildOptionFromShell (
1144 IN EFI_HANDLE Handle
,
1145 IN OUT LIST_ENTRY
*BdsBootOptionList
1148 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1149 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1151 DevicePath
= DevicePathFromHandle (Handle
);
1154 // Build the shell device path
1156 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1158 //ShellNode.Header.Type = MEDIA_DEVICE_PATH;
1159 //ShellNode.Header.SubType = MEDIA_FV_FILEPATH_DP;
1160 //SetDevicePathNodeLength (&ShellNode.Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));
1161 //CopyMem (&ShellNode.NameGuid, &gEfiShellFileGuid, sizeof (EFI_GUID));
1163 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1166 // Create and register the shell boot option
1168 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1174 Boot from the EFI1.1 spec defined "BootNext" variable
1186 BDS_COMMON_OPTION
*BootOption
;
1187 LIST_ENTRY TempList
;
1192 // Init the boot option name buffer and temp link list
1194 InitializeListHead (&TempList
);
1195 ZeroMem (Buffer
, sizeof (Buffer
));
1197 BootNext
= BdsLibGetVariableAndSize (
1199 &gEfiGlobalVariableGuid
,
1204 // Clear the boot next variable first
1206 if (BootNext
!= NULL
) {
1209 &gEfiGlobalVariableGuid
,
1210 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1216 // Start to build the boot option and try to boot
1218 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1219 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1220 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1221 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1229 Return the bootable media handle.
1230 First, check the device is connected
1231 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1232 Third, detect the the default boot file in the Media, and return the removable Media handle.
1234 @param DevicePath Device Path to a bootable device
1236 @retval NULL The device path points to an EFI bootable Media
1237 @retval NULL The media on the DevicePath is not bootable
1242 BdsLibGetBootableHandle (
1243 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1247 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1248 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1250 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1252 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1255 EFI_HANDLE ReturnHandle
;
1256 EFI_HANDLE
*SimpleFileSystemHandles
;
1258 UINTN NumberSimpleFileSystemHandles
;
1260 EFI_IMAGE_DOS_HEADER DosHeader
;
1261 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1262 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1264 UpdatedDevicePath
= DevicePath
;
1266 // Check whether the device is connected
1268 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1269 if (EFI_ERROR (Status
)) {
1271 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1273 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1274 if (EFI_ERROR (Status
)) {
1276 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1278 UpdatedDevicePath
= DevicePath
;
1279 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1280 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1284 // Get BlockIo protocal and check removable attribute
1286 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1288 // Issue a dummy read to the device to check for media change.
1289 // When the removable media is changed, any Block IO read/write will
1290 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1291 // returned. After the Block IO protocol is reinstalled, subsequent
1292 // Block IO read/write will success.
1294 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1295 if (Buffer
!= NULL
) {
1296 BlockIo
->ReadBlocks (
1298 BlockIo
->Media
->MediaId
,
1300 BlockIo
->Media
->BlockSize
,
1303 gBS
->FreePool (Buffer
);
1308 // Detect the the default boot file from removable Media
1312 // 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
1313 // Try to locate the USB node device path first, if fail then use its previour PCI node to search
1315 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1316 UpdatedDevicePath
= DupDevicePath
;
1317 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1319 // 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
1320 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1322 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1323 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1325 // Remove the usb node, let the device path only point to PCI node
1327 SetDevicePathEndNode (UpdatedDevicePath
);
1328 UpdatedDevicePath
= DupDevicePath
;
1330 UpdatedDevicePath
= DevicePath
;
1334 // Get the device path size of boot option
1336 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1337 ReturnHandle
= NULL
;
1338 gBS
->LocateHandleBuffer (
1340 &gEfiSimpleFileSystemProtocolGuid
,
1342 &NumberSimpleFileSystemHandles
,
1343 &SimpleFileSystemHandles
1345 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1347 // Get the device path size of SimpleFileSystem handle
1349 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1350 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1352 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1354 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1356 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1357 // machinename is ia32, ia64, x64, ...
1359 Hdr
.Union
= &HdrData
;
1360 Status
= BdsLibGetImageHeader (
1361 SimpleFileSystemHandles
[Index
],
1362 DEFAULT_REMOVABLE_FILE_NAME
,
1366 if (!EFI_ERROR (Status
) &&
1367 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1368 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1369 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1375 if (DupDevicePath
!= NULL
) {
1376 SafeFreePool(DupDevicePath
);
1378 if (SimpleFileSystemHandles
!=NULL
) {
1379 gBS
->FreePool (SimpleFileSystemHandles
);
1382 return ReturnHandle
;
1389 Check to see if the network cable is plugged in. If the DevicePath is not
1390 connected it will be connected.
1392 @param DevicePath Device Path to check
1394 @retval TRUE DevicePath points to an Network that is connected
1395 @retval FALSE DevicePath does not point to a bootable network
1399 BdsLibNetworkBootWithMediaPresent (
1400 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1404 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1406 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1407 BOOLEAN MediaPresent
;
1409 MediaPresent
= FALSE
;
1411 UpdatedDevicePath
= DevicePath
;
1412 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1413 if (EFI_ERROR (Status
)) {
1415 // Device not present so see if we need to connect it
1417 Status
= BdsLibConnectDevicePath (DevicePath
);
1418 if (!EFI_ERROR (Status
)) {
1420 // This one should work after we did the connect
1422 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1426 if (!EFI_ERROR (Status
)) {
1427 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1428 if (!EFI_ERROR (Status
)) {
1429 if (Snp
->Mode
->MediaPresentSupported
) {
1430 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1432 // In case some one else is using the SNP check to see if it's connected
1434 MediaPresent
= Snp
->Mode
->MediaPresent
;
1437 // No one is using SNP so we need to Start and Initialize so
1438 // MediaPresent will be valid.
1440 Status
= Snp
->Start (Snp
);
1441 if (!EFI_ERROR (Status
)) {
1442 Status
= Snp
->Initialize (Snp
, 0, 0);
1443 if (!EFI_ERROR (Status
)) {
1444 MediaPresent
= Snp
->Mode
->MediaPresent
;
1445 Snp
->Shutdown (Snp
);
1451 MediaPresent
= TRUE
;
1456 return MediaPresent
;
1462 For a bootable Device path, return its boot type
1464 @param DevicePath The bootable device Path to check
1466 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media deviec path node, it is media boot type
1467 For the floppy node, handle it as media node
1468 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media deviec path node, it is media boot type
1469 For the floppy node, handle it as media node
1470 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media deviec path node, it is media boot type
1471 For the floppy node, handle it as media node
1472 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media deviec path node, and
1473 its last device path node point to a message device path node, it is
1475 @retval BDS_EFI_MESSAGE_SCSI_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
1477 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media deviec path node, and
1478 its last device path node point to a message device path node, it is
1479 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media deviec path node, and
1480 its last device path node point to a message device path node, it is
1481 @retval BDS_LEGACY_BBS_BOOT Legacy boot type
1482 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message devie,
1487 BdsGetBootTypeFromDevicePath (
1488 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1491 ACPI_HID_DEVICE_PATH
*Acpi
;
1492 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1493 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1496 if (NULL
== DevicePath
) {
1497 return BDS_EFI_UNSUPPORT
;
1500 TempDevicePath
= DevicePath
;
1502 while (!IsDevicePathEndType (TempDevicePath
)) {
1503 switch (DevicePathType (TempDevicePath
)) {
1504 case BBS_DEVICE_PATH
:
1505 return BDS_LEGACY_BBS_BOOT
;
1506 case MEDIA_DEVICE_PATH
:
1507 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1508 return BDS_EFI_MEDIA_HD_BOOT
;
1509 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1510 return BDS_EFI_MEDIA_CDROM_BOOT
;
1513 case ACPI_DEVICE_PATH
:
1514 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1515 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1516 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1519 case MESSAGING_DEVICE_PATH
:
1521 // if the device path not only point to driver device, it is not a messaging device path.
1523 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1524 if (!IsDevicePathEndType (LastDeviceNode
)) {
1528 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1529 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1530 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1531 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1532 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1533 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1535 return BDS_EFI_MESSAGE_MISC_BOOT
;
1539 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1542 return BDS_EFI_UNSUPPORT
;
1547 Check whether the Device path in a boot option point to a valide bootable device,
1548 And if CheckMedia is true, check the device is ready to boot now.
1550 DevPath -- the Device path in a boot option
1551 CheckMedia -- if true, check the device is ready to boot now.
1553 @return TRUE -- the Device path is valide
1554 @return FALSE -- the Device path is invalide .
1559 BdsLibIsValidEFIBootOptDevicePath (
1560 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1561 IN BOOLEAN CheckMedia
1566 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1567 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1568 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1570 TempDevicePath
= DevPath
;
1571 LastDeviceNode
= DevPath
;
1573 // Check if it's a valid boot option for network boot device
1574 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1575 // there is the network card there.
1577 Status
= gBS
->LocateDevicePath (
1578 &gEfiSimpleNetworkProtocolGuid
,
1582 if (EFI_ERROR (Status
)) {
1584 // Device not present so see if we need to connect it
1586 TempDevicePath
= DevPath
;
1587 BdsLibConnectDevicePath (TempDevicePath
);
1588 Status
= gBS
->LocateDevicePath (
1589 &gEfiSimpleNetworkProtocolGuid
,
1594 if (!EFI_ERROR (Status
)) {
1597 // Test if it is ready to boot now
1599 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1608 // If the boot option point to a file, it is a valid EFI boot option,
1609 // and assume it is ready to boot now
1611 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1612 LastDeviceNode
= TempDevicePath
;
1613 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1615 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1616 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1621 // If the boot option point to a internal Shell, it is a valid EFI boot option,
1622 // and assume it is ready to boot now
1624 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1629 // 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
1631 TempDevicePath
= DevPath
;
1632 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1633 if (EFI_ERROR (Status
)) {
1635 // Device not present so see if we need to connect it
1637 Status
= BdsLibConnectDevicePath (DevPath
);
1638 if (!EFI_ERROR (Status
)) {
1640 // Try again to get the Block Io protocol after we did the connect
1642 TempDevicePath
= DevPath
;
1643 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1646 if (!EFI_ERROR (Status
)) {
1647 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1648 if (!EFI_ERROR (Status
)) {
1651 // Test if it is ready to boot now
1653 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1662 // 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,
1664 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1665 if (!EFI_ERROR (Status
)) {
1668 // Test if it is ready to boot now
1670 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1684 According to a file guild, check a Fv file device path is valid. If it is invalid,
1685 try to return the valid device path.
1686 FV address maybe changes for memory layout adjust from time to time, use this funciton
1687 could promise the Fv file device path is right.
1689 @param DevicePath on input, the Fv file device path need to check on
1690 output, the updated valid Fv file device path
1691 @param FileGuid the Fv file guild
1693 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1695 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1697 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1699 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1700 and return the updated device path in DevicePath
1705 BdsLibUpdateFvFileDevicePath (
1706 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1707 IN EFI_GUID
*FileGuid
1710 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1711 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1713 EFI_GUID
*GuidPoint
;
1715 UINTN FvHandleCount
;
1716 EFI_HANDLE
*FvHandleBuffer
;
1717 EFI_FV_FILETYPE Type
;
1719 EFI_FV_FILE_ATTRIBUTES Attributes
;
1720 UINT32 AuthenticationStatus
;
1722 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1723 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1724 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1725 EFI_HANDLE FoundFvHandle
;
1726 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1728 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1729 return EFI_INVALID_PARAMETER
;
1731 if (FileGuid
== NULL
) {
1732 return EFI_INVALID_PARAMETER
;
1735 // Check whether the device path point to the default the input Fv file
1737 TempDevicePath
= *DevicePath
;
1738 LastDeviceNode
= TempDevicePath
;
1739 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1740 LastDeviceNode
= TempDevicePath
;
1741 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1743 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1744 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1746 if (GuidPoint
== NULL
) {
1748 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1750 return EFI_UNSUPPORTED
;
1752 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1754 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1756 return EFI_UNSUPPORTED
;
1760 // Check whether the input Fv file device path is valid
1762 TempDevicePath
= *DevicePath
;
1763 FoundFvHandle
= NULL
;
1764 Status
= gBS
->LocateDevicePath (
1765 &gEfiFirmwareVolume2ProtocolGuid
,
1769 if (!EFI_ERROR (Status
)) {
1770 Status
= gBS
->HandleProtocol (
1772 &gEfiFirmwareVolume2ProtocolGuid
,
1775 if (!EFI_ERROR (Status
)) {
1777 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1779 Status
= Fv
->ReadFile (
1786 &AuthenticationStatus
1788 if (!EFI_ERROR (Status
)) {
1789 return EFI_ALREADY_STARTED
;
1795 // Look for the input wanted FV file in current FV
1796 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1799 FoundFvHandle
= NULL
;
1800 Status
= gBS
->HandleProtocol (
1802 &gEfiLoadedImageProtocolGuid
,
1803 (VOID
**) &LoadedImage
1805 if (!EFI_ERROR (Status
)) {
1806 Status
= gBS
->HandleProtocol (
1807 LoadedImage
->DeviceHandle
,
1808 &gEfiFirmwareVolume2ProtocolGuid
,
1811 if (!EFI_ERROR (Status
)) {
1812 Status
= Fv
->ReadFile (
1819 &AuthenticationStatus
1821 if (!EFI_ERROR (Status
)) {
1823 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1828 // Second, if fail to find, try to enumerate all FV
1831 gBS
->LocateHandleBuffer (
1833 &gEfiFirmwareVolume2ProtocolGuid
,
1838 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1839 gBS
->HandleProtocol (
1840 FvHandleBuffer
[Index
],
1841 &gEfiFirmwareVolume2ProtocolGuid
,
1845 Status
= Fv
->ReadFile (
1852 &AuthenticationStatus
1854 if (EFI_ERROR (Status
)) {
1856 // Skip if input Fv file not in the FV
1861 FoundFvHandle
= FvHandleBuffer
[Index
];
1868 // Build the shell device path
1870 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1871 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1872 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1873 *DevicePath
= NewDevicePath
;
1876 return EFI_NOT_FOUND
;