3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 BDS Lib functions which relate with create or process the boot
24 #include "InternalBdsLib.h"
26 BOOLEAN mEnumBootDevice
= FALSE
;
29 // This GUID is used for an EFI Variable that stores the front device pathes
30 // for a partial device path that starts with the HD node.
32 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
37 Boot the legacy system with the boot option
39 @param Option The legacy boot option which have BBS device path
41 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
43 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
48 IN BDS_COMMON_OPTION
*Option
52 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
54 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
55 if (EFI_ERROR (Status
)) {
57 // If no LegacyBios protocol we do not support legacy boot
59 return EFI_UNSUPPORTED
;
62 // Notes: if we seperate the int 19, then we don't need to refresh BBS
64 BdsRefreshBbsTableForBoot (Option
);
67 // Write boot to OS performance data to a file
70 WriteBootToOsPerformanceData ();
73 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
74 return LegacyBios
->LegacyBoot (
76 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
77 Option
->LoadOptionsSize
,
84 Process the boot option follow the EFI 1.1 specification and
85 special treat the legacy boot option with BBS_DEVICE_PATH.
87 @param Option The boot option need to be processed
88 @param DevicePath The device path which describe where to load the
89 boot image or the legcy BBS device path to boot
91 @param ExitDataSize Returned directly from gBS->StartImage ()
92 @param ExitData Returned directly from gBS->StartImage ()
94 @retval EFI_SUCCESS Status from gBS->StartImage ()
95 @retval EFI_NOT_FOUND If the Device Path is not found in the system
99 BdsLibBootViaBootOption (
100 IN BDS_COMMON_OPTION
* Option
,
101 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
102 OUT UINTN
*ExitDataSize
,
103 OUT CHAR16
**ExitData OPTIONAL
108 EFI_HANDLE ImageHandle
;
109 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
110 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
111 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
112 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
113 LIST_ENTRY TempBootLists
;
116 // Record the performance data for End of BDS
118 PERF_END (0, BDS_TOK
, NULL
, 0);
124 // Notes: put EFI64 ROM Shadow Solution
126 EFI64_SHADOW_ALL_LEGACY_ROM ();
129 // Notes: this code can be remove after the s3 script table
130 // hook on the event EFI_EVENT_SIGNAL_READY_TO_BOOT or
131 // EFI_EVENT_SIGNAL_LEGACY_BOOT
133 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
134 if (!EFI_ERROR (Status
)) {
135 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
138 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
141 WorkingDevicePath
= NULL
;
142 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
143 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
144 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
145 (HARDDRIVE_DEVICE_PATH
*)DevicePath
147 if (WorkingDevicePath
!= NULL
) {
148 DevicePath
= WorkingDevicePath
;
152 // Signal the EFI_EVENT_SIGNAL_READY_TO_BOOT event
154 EfiSignalEventReadyToBoot();
162 &gEfiGlobalVariableGuid
,
163 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
168 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
169 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
172 // Check to see if we should legacy BOOT. If yes then do the legacy boot
174 return BdsLibDoLegacyBoot (Option
);
178 // If the boot option point to Internal FV shell, make sure it is valid
180 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, &gEfiShellFileGuid
);
181 if (!EFI_ERROR(Status
)) {
182 if (Option
->DevicePath
!= NULL
) {
183 SafeFreePool(Option
->DevicePath
);
185 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
186 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
188 // Update the shell boot option
190 InitializeListHead (&TempBootLists
);
191 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
195 // Drop the TPL level from TPL_APPLICATION to TPL_APPLICATION
197 gBS
->RestoreTPL (TPL_APPLICATION
);
199 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting EFI way %S\n", Option
->Description
));
201 Status
= gBS
->LoadImage (
211 // If we didn't find an image directly, we need to try as if it is a removable device boot opotion
212 // and load the image according to the default boot behavior for removable device.
214 if (EFI_ERROR (Status
)) {
216 // check if there is a bootable removable media could be found in this device path ,
217 // and get the bootable media handle
219 Handle
= BdsLibGetBootableHandle(DevicePath
);
220 if (Handle
== NULL
) {
224 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
225 // machinename is ia32, ia64, x64, ...
227 FilePath
= FileDevicePath (Handle
, DEFAULT_REMOVABLE_FILE_NAME
);
229 Status
= gBS
->LoadImage (
237 if (EFI_ERROR (Status
)) {
239 // The DevicePath failed, and it's not a valid
240 // removable media device.
247 if (EFI_ERROR (Status
)) {
249 // It there is any error from the Boot attempt exit now.
254 // Provide the image with it's load options
256 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
257 ASSERT_EFI_ERROR (Status
);
259 if (Option
->LoadOptionsSize
!= 0) {
260 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
261 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
264 // Before calling the image, enable the Watchdog Timer for
265 // the 5 Minute period
267 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
269 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
270 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
273 // Clear the Watchdog Timer after the image returns
275 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
279 // Clear Boot Current
283 &gEfiGlobalVariableGuid
,
284 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
290 // Raise the TPL level back to TPL_APPLICATION
292 gBS
->RaiseTPL (TPL_APPLICATION
);
299 Expand a device path that starts with a hard drive media device path node to be a
300 full device path that includes the full hardware path to the device. We need
301 to do this so it can be booted. As an optimizaiton the front match (the part point
302 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
303 so a connect all is not required on every boot. All successful history device path
304 which point to partition node (the front part) will be saved.
306 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
307 drive media device path.
308 A Pointer to the full device path.
309 @retval NULL Cannot find a valid Hard Drive devic path
312 EFI_DEVICE_PATH_PROTOCOL
*
313 BdsExpandPartitionPartialDevicePathToFull (
314 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
318 UINTN BlockIoHandleCount
;
319 EFI_HANDLE
*BlockIoBuffer
;
320 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
321 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
322 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
325 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
326 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
327 UINTN CachedDevicePathSize
;
330 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
333 FullDevicePath
= NULL
;
335 // Check if there is prestore 'HDDP' variable.
336 // If exist, search the front path which point to partition node in the variable instants.
337 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
339 CachedDevicePath
= BdsLibGetVariableAndSize (
341 &mHdBootVariablePrivateGuid
,
342 &CachedDevicePathSize
344 if (CachedDevicePath
!= NULL
) {
345 TempNewDevicePath
= CachedDevicePath
;
350 // Check every instance of the variable
351 // First, check wheather the instance contain the partition node, which is needed for distinguishing multi
352 // partial partition boot option. Second, check wheather the instance could be connected.
354 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
355 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
357 // Connect the device path instance, the device path point to hard drive media device path node
358 // e.g. ACPI() /PCI()/ATA()/Partition()
360 Status
= BdsLibConnectDevicePath (Instance
);
361 if (!EFI_ERROR (Status
)) {
367 // Come here means the first instance is not matched
370 SafeFreePool(Instance
);
371 } while (TempNewDevicePath
!= NULL
);
375 // Find the matched device path.
376 // Append the file path infomration from the boot option and return the fully expanded device path.
378 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
379 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
382 // Adjust the 'HDDP' instances sequense if the matched one is not first one.
386 // First delete the matched instance.
388 TempNewDevicePath
= CachedDevicePath
;
389 CachedDevicePath
= BdsLibDelPartMatchInstance ( CachedDevicePath
, Instance
);
390 SafeFreePool (TempNewDevicePath
);
392 // Second, append the remaining parth after the matched instance
394 TempNewDevicePath
= CachedDevicePath
;
395 CachedDevicePath
= AppendDevicePathInstance ( Instance
, CachedDevicePath
);
396 SafeFreePool (TempNewDevicePath
);
398 // Save the matching Device Path so we don't need to do a connect all next time
400 Status
= gRT
->SetVariable (
402 &mHdBootVariablePrivateGuid
,
403 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
404 GetDevicePathSize (CachedDevicePath
),
408 SafeFreePool(Instance
);
409 gBS
->FreePool (CachedDevicePath
);
410 return FullDevicePath
;
415 // If we get here we fail to find or 'HDDP' not exist, and now we need
416 // to seach all devices in the system for a matched partition
418 BdsLibConnectAllDriversToAllControllers ();
419 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
420 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0) {
422 // If there was an error or there are no device handles that support
423 // the BLOCK_IO Protocol, then return.
428 // Loop through all the device handles that support the BLOCK_IO Protocol
430 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
432 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
433 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
437 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
439 // Find the matched partition device path
441 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
442 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
445 // Save the matched patition device path in 'HDDP' variable
447 if (CachedDevicePath
!= NULL
) {
449 // Save the matched patition device path as first instance of 'HDDP' variable
451 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
452 TempNewDevicePath
= CachedDevicePath
;
453 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
454 SafeFreePool(TempNewDevicePath
);
456 TempNewDevicePath
= CachedDevicePath
;
457 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
458 SafeFreePool(TempNewDevicePath
);
460 TempNewDevicePath
= CachedDevicePath
;
461 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
462 SafeFreePool(TempNewDevicePath
);
465 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
466 // If the user try to boot many OS in different HDs or partitions, in theary, the 'HDDP' variable maybe become larger and larger.
469 TempNewDevicePath
= CachedDevicePath
;
470 while (!IsDevicePathEnd (TempNewDevicePath
)) {
471 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
473 // Parse one instance
475 while (!IsDevicePathEndType (TempNewDevicePath
)) {
476 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
480 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
482 if (InstanceNum
>= 12) {
483 SetDevicePathEndNode (TempNewDevicePath
);
488 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
492 // Save the matching Device Path so we don't need to do a connect all next time
494 Status
= gRT
->SetVariable (
496 &mHdBootVariablePrivateGuid
,
497 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
498 GetDevicePathSize (CachedDevicePath
),
505 gBS
->FreePool (CachedDevicePath
);
506 gBS
->FreePool (BlockIoBuffer
);
507 return FullDevicePath
;
512 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
513 instances, has the same partition node with HardDriveDevicePath device path
515 @param BlockIoDevicePath Multi device path instances which need to check
516 @param HardDriveDevicePath A device path which starts with a hard drive media
519 @retval TRUE There is a matched device path instance FALSE
520 -There is no matched device path instance
524 MatchPartitionDevicePathNode (
525 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
526 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
529 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
530 HARDDRIVE_DEVICE_PATH
*TempPath
;
531 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
533 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
535 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
539 // Make PreviousDevicePath == the device path node before the end node
541 DevicePath
= BlockIoDevicePath
;
542 BlockIoHdDevicePathNode
= NULL
;
545 // find the partition device path node
547 while (!IsDevicePathEnd (DevicePath
)) {
548 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
549 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
551 BlockIoHdDevicePathNode
= DevicePath
;
555 DevicePath
= NextDevicePathNode (DevicePath
);
558 if (BlockIoHdDevicePathNode
== NULL
) {
562 // See if the harddrive device path in blockio matches the orig Hard Drive Node
564 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
565 TempPath
= (HARDDRIVE_DEVICE_PATH
*) BdsLibUnpackDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
568 // Check for the match
570 if ((TmpHdPath
->MBRType
== TempPath
->MBRType
) &&
571 (TmpHdPath
->SignatureType
== TempPath
->SignatureType
)) {
572 switch (TmpHdPath
->SignatureType
) {
573 case SIGNATURE_TYPE_GUID
:
574 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)TempPath
->Signature
);
576 case SIGNATURE_TYPE_MBR
:
577 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == *(UINT32
*)(&(TempPath
->Signature
[0])));
590 Delete the boot option associated with the handle passed in
592 @param Handle The handle which present the device path to create
595 @retval EFI_SUCCESS Delete the boot option success
596 @retval EFI_NOT_FOUND If the Device Path is not found in the system
597 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
598 @retval Other Error return value from SetVariable()
602 BdsLibDeleteOptionFromHandle (
607 UINT8
*BootOptionVar
;
609 UINTN BootOptionSize
;
612 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
613 UINTN DevicePathSize
;
614 UINTN OptionDevicePathSize
;
615 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
616 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
619 Status
= EFI_SUCCESS
;
623 BootOrder
= BdsLibGetVariableAndSize (
625 &gEfiGlobalVariableGuid
,
628 if (NULL
== BootOrder
) {
629 return EFI_NOT_FOUND
;
632 DevicePath
= DevicePathFromHandle (Handle
);
633 if (DevicePath
== NULL
) {
634 return EFI_NOT_FOUND
;
636 DevicePathSize
= GetDevicePathSize (DevicePath
);
639 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
640 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
641 BootOptionVar
= BdsLibGetVariableAndSize (
643 &gEfiGlobalVariableGuid
,
646 if (NULL
== BootOptionVar
) {
647 gBS
->FreePool (BootOrder
);
648 return EFI_OUT_OF_RESOURCES
;
651 TempPtr
= BootOptionVar
;
652 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
653 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
654 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
655 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
658 // Check whether the device path match
660 if ((OptionDevicePathSize
== DevicePathSize
) &&
661 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
662 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
663 gBS
->FreePool (BootOptionVar
);
667 gBS
->FreePool (BootOptionVar
);
671 Status
= gRT
->SetVariable (
673 &gEfiGlobalVariableGuid
,
674 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
679 gBS
->FreePool (BootOrder
);
686 Delete all invalid EFI boot options. The probable invalid boot option could
687 be Removable media or Network boot device.
691 @retval EFI_SUCCESS Delete all invalid boot option success
692 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
693 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
694 @retval Other Error return value from SetVariable()
698 BdsDeleteAllInvalidEfiBootOption (
703 UINT8
*BootOptionVar
;
705 UINTN BootOptionSize
;
709 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
710 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
713 Status
= EFI_SUCCESS
;
717 BootOrder
= BdsLibGetVariableAndSize (
719 &gEfiGlobalVariableGuid
,
722 if (NULL
== BootOrder
) {
723 return EFI_NOT_FOUND
;
727 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
728 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
729 BootOptionVar
= BdsLibGetVariableAndSize (
731 &gEfiGlobalVariableGuid
,
734 if (NULL
== BootOptionVar
) {
735 gBS
->FreePool (BootOrder
);
736 return EFI_OUT_OF_RESOURCES
;
739 TempPtr
= BootOptionVar
;
740 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
741 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
742 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
745 // Skip legacy boot option (BBS boot device)
747 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
748 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
749 gBS
->FreePool (BootOptionVar
);
754 if (!BdsLibIsValidEFIBootOptDevicePath (OptionDevicePath
, FALSE
)) {
756 // Delete this invalid boot option "Boot####"
758 Status
= gRT
->SetVariable (
760 &gEfiGlobalVariableGuid
,
761 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
766 // Mark this boot option in boot order as deleted
768 BootOrder
[Index
] = 0xffff;
771 gBS
->FreePool (BootOptionVar
);
776 // Adjust boot order array
779 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
780 if (BootOrder
[Index
] != 0xffff) {
781 BootOrder
[Index2
] = BootOrder
[Index
];
785 Status
= gRT
->SetVariable (
787 &gEfiGlobalVariableGuid
,
788 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
789 Index2
* sizeof (UINT16
),
793 gBS
->FreePool (BootOrder
);
800 This function will enumerate all possible boot device in the system,
801 it will only excute once of every boot.
803 @param BdsBootOptionList The header of the link list which indexed all
806 @retval EFI_SUCCESS Finished all the boot device enumerate and create
807 the boot option base on that boot device
811 BdsLibEnumerateAllBootOption (
812 IN OUT LIST_ENTRY
*BdsBootOptionList
820 UINT16 NonBlockNumber
;
821 UINTN NumberBlockIoHandles
;
822 EFI_HANDLE
*BlockIoHandles
;
823 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
825 UINTN NumberSimpleNetworkHandles
;
826 EFI_HANDLE
*SimpleNetworkHandles
;
828 EFI_HANDLE
*FvHandleBuffer
;
829 EFI_FV_FILETYPE Type
;
831 EFI_FV_FILE_ATTRIBUTES Attributes
;
832 UINT32 AuthenticationStatus
;
833 #if (PI_SPECIFICATION_VERSION < 0x00010000)
834 EFI_FIRMWARE_VOLUME_PROTOCOL
*Fv
;
836 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
838 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
839 UINTN DevicePathType
;
841 EFI_HANDLE
*FileSystemHandles
;
842 UINTN NumberFileSystemHandles
;
844 EFI_IMAGE_DOS_HEADER DosHeader
;
845 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
846 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
852 ZeroMem (Buffer
, sizeof (Buffer
));
854 // If the boot device enumerate happened, just get the boot
855 // device from the boot order variable
857 if (mEnumBootDevice
) {
858 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
862 // Notes: this dirty code is to get the legacy boot option from the
863 // BBS table and create to variable as the EFI boot option, it should
864 // be removed after the CSM can provide legacy boot option directly
866 REFRESH_LEGACY_BOOT_OPTIONS
;
869 // Delete invalid boot option
871 BdsDeleteAllInvalidEfiBootOption ();
873 // Parse removable media
875 gBS
->LocateHandleBuffer (
877 &gEfiBlockIoProtocolGuid
,
879 &NumberBlockIoHandles
,
882 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
883 Status
= gBS
->HandleProtocol (
884 BlockIoHandles
[Index
],
885 &gEfiBlockIoProtocolGuid
,
888 if (!EFI_ERROR (Status
)) {
889 if (!BlkIo
->Media
->RemovableMedia
) {
891 // skip the non-removable block devices
896 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
897 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
899 switch (DevicePathType
) {
900 case BDS_EFI_ACPI_FLOPPY_BOOT
:
901 if (FloppyNumber
== 0) {
902 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy");
904 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Floppy %d", FloppyNumber
);
906 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
910 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
911 if (CdromNumber
== 0) {
912 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM");
914 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI DVD/CDROM %d", CdromNumber
);
916 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
920 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
921 if (UsbNumber
== 0) {
922 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device");
924 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI USB Device %d", UsbNumber
);
926 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
930 case BDS_EFI_MESSAGE_SCSI_BOOT
:
931 if (UsbNumber
== 0) {
932 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device");
934 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI SCSI Device %d", UsbNumber
);
936 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
940 case BDS_EFI_MESSAGE_MISC_BOOT
:
941 if (MiscNumber
== 0) {
942 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device");
944 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Misc Device %d", MiscNumber
);
946 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
955 if (NumberBlockIoHandles
) {
956 gBS
->FreePool (BlockIoHandles
);
960 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
963 gBS
->LocateHandleBuffer (
965 &gEfiSimpleFileSystemProtocolGuid
,
967 &NumberFileSystemHandles
,
970 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
971 Status
= gBS
->HandleProtocol (
972 FileSystemHandles
[Index
],
973 &gEfiBlockIoProtocolGuid
,
976 if (!EFI_ERROR (Status
)) {
978 // Skip if the file system handle supports a BlkIo protocol,
984 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
985 // machinename is ia32, ia64, x64, ...
987 Hdr
.Union
= &HdrData
;
989 Status
= BdsLibGetImageHeader (
990 FileSystemHandles
[Index
],
991 DEFAULT_REMOVABLE_FILE_NAME
,
995 if (!EFI_ERROR (Status
) &&
996 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
997 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1003 // No such file or the file is not a EFI application, delete this boot option
1005 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1007 if (NonBlockNumber
== 0) {
1008 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device");
1010 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Non-Block Boot Device %d", NonBlockNumber
);
1012 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1017 if (NumberFileSystemHandles
) {
1018 gBS
->FreePool (FileSystemHandles
);
1022 // Parse Network Boot Device
1024 gBS
->LocateHandleBuffer (
1026 &gEfiSimpleNetworkProtocolGuid
,
1028 &NumberSimpleNetworkHandles
,
1029 &SimpleNetworkHandles
1031 for (Index
= 0; Index
< NumberSimpleNetworkHandles
; Index
++) {
1033 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network");
1035 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"EFI Network %d", Index
);
1037 BdsLibBuildOptionFromHandle (SimpleNetworkHandles
[Index
], BdsBootOptionList
, Buffer
);
1040 if (NumberSimpleNetworkHandles
) {
1041 gBS
->FreePool (SimpleNetworkHandles
);
1045 // Check if we have on flash shell
1047 gBS
->LocateHandleBuffer (
1049 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1050 &gEfiFirmwareVolumeProtocolGuid
,
1052 &gEfiFirmwareVolume2ProtocolGuid
,
1058 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1059 gBS
->HandleProtocol (
1060 FvHandleBuffer
[Index
],
1061 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1062 &gEfiFirmwareVolumeProtocolGuid
,
1064 &gEfiFirmwareVolume2ProtocolGuid
,
1069 Status
= Fv
->ReadFile (
1076 &AuthenticationStatus
1078 if (EFI_ERROR (Status
)) {
1080 // Skip if no shell file in the FV
1085 // Build the shell boot option
1087 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1090 if (FvHandleCount
) {
1091 gBS
->FreePool (FvHandleBuffer
);
1094 // Make sure every boot only have one time
1095 // boot device enumerate
1097 BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1098 mEnumBootDevice
= TRUE
;
1105 Build the boot option with the handle parsed in
1107 @param Handle The handle which present the device path to create
1109 @param BdsBootOptionList The header of the link list which indexed all
1110 current boot options
1116 BdsLibBuildOptionFromHandle (
1117 IN EFI_HANDLE Handle
,
1118 IN LIST_ENTRY
*BdsBootOptionList
,
1122 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1124 DevicePath
= DevicePathFromHandle (Handle
);
1127 // Create and register new boot option
1129 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1134 Build the on flash shell boot option with the handle parsed in
1136 @param Handle The handle which present the device path to create
1137 on flash shell boot option
1138 @param BdsBootOptionList The header of the link list which indexed all
1139 current boot options
1145 BdsLibBuildOptionFromShell (
1146 IN EFI_HANDLE Handle
,
1147 IN OUT LIST_ENTRY
*BdsBootOptionList
1150 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1151 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1153 DevicePath
= DevicePathFromHandle (Handle
);
1156 // Build the shell device path
1158 EfiInitializeFwVolDevicepathNode (&ShellNode
, &gEfiShellFileGuid
);
1160 //ShellNode.Header.Type = MEDIA_DEVICE_PATH;
1161 //ShellNode.Header.SubType = MEDIA_FV_FILEPATH_DP;
1162 //SetDevicePathNodeLength (&ShellNode.Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));
1163 //CopyMem (&ShellNode.NameGuid, &gEfiShellFileGuid, sizeof (EFI_GUID));
1165 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1168 // Create and register the shell boot option
1170 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1176 Boot from the EFI1.1 spec defined "BootNext" variable
1191 BDS_COMMON_OPTION
*BootOption
;
1192 LIST_ENTRY TempList
;
1197 // Init the boot option name buffer and temp link list
1199 InitializeListHead (&TempList
);
1200 ZeroMem (Buffer
, sizeof (Buffer
));
1202 BootNext
= BdsLibGetVariableAndSize (
1204 &gEfiGlobalVariableGuid
,
1209 // Clear the boot next variable first
1211 if (BootNext
!= NULL
) {
1214 &gEfiGlobalVariableGuid
,
1215 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1221 // Start to build the boot option and try to boot
1223 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1224 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1225 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1226 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1234 Return the bootable media handle.
1235 First, check the device is connected
1236 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1237 Third, detect the the default boot file in the Media, and return the removable Media handle.
1239 @param DevicePath Device Path to a bootable device
1241 @retval NULL The device path points to an EFI bootable Media
1242 @retval NULL The media on the DevicePath is not bootable
1246 BdsLibGetBootableHandle (
1247 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1251 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1252 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1254 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1256 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1259 EFI_HANDLE ReturnHandle
;
1260 EFI_HANDLE
*SimpleFileSystemHandles
;
1262 UINTN NumberSimpleFileSystemHandles
;
1264 EFI_IMAGE_DOS_HEADER DosHeader
;
1265 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1266 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1268 UpdatedDevicePath
= DevicePath
;
1270 // Check whether the device is connected
1272 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1273 if (EFI_ERROR (Status
)) {
1275 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1277 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1278 if (EFI_ERROR (Status
)) {
1280 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1282 UpdatedDevicePath
= DevicePath
;
1283 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1284 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1288 // Get BlockIo protocal and check removable attribute
1290 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1292 // Issue a dummy read to the device to check for media change.
1293 // When the removable media is changed, any Block IO read/write will
1294 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1295 // returned. After the Block IO protocol is reinstalled, subsequent
1296 // Block IO read/write will success.
1298 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1299 if (Buffer
!= NULL
) {
1300 BlockIo
->ReadBlocks (
1302 BlockIo
->Media
->MediaId
,
1304 BlockIo
->Media
->BlockSize
,
1307 gBS
->FreePool (Buffer
);
1312 // Detect the the default boot file from removable Media
1316 // 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
1317 // Try to locate the USB node device path first, if fail then use its previour PCI node to search
1319 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1320 UpdatedDevicePath
= DupDevicePath
;
1321 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1323 // 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
1324 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1326 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1327 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1329 // Remove the usb node, let the device path only point to PCI node
1331 SetDevicePathEndNode (UpdatedDevicePath
);
1332 UpdatedDevicePath
= DupDevicePath
;
1334 UpdatedDevicePath
= DevicePath
;
1338 // Get the device path size of boot option
1340 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1341 ReturnHandle
= NULL
;
1342 gBS
->LocateHandleBuffer (
1344 &gEfiSimpleFileSystemProtocolGuid
,
1346 &NumberSimpleFileSystemHandles
,
1347 &SimpleFileSystemHandles
1349 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1351 // Get the device path size of SimpleFileSystem handle
1353 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1354 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1356 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1358 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
1360 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1361 // machinename is ia32, ia64, x64, ...
1363 Hdr
.Union
= &HdrData
;
1364 Status
= BdsLibGetImageHeader (
1365 SimpleFileSystemHandles
[Index
],
1366 DEFAULT_REMOVABLE_FILE_NAME
,
1370 if (!EFI_ERROR (Status
) &&
1371 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1372 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1373 ReturnHandle
= SimpleFileSystemHandles
[Index
];
1379 if (DupDevicePath
!= NULL
) {
1380 SafeFreePool(DupDevicePath
);
1382 if (SimpleFileSystemHandles
!=NULL
) {
1383 gBS
->FreePool (SimpleFileSystemHandles
);
1386 return ReturnHandle
;
1393 Check to see if the network cable is plugged in. If the DevicePath is not
1394 connected it will be connected.
1396 @param DevicePath Device Path to check
1398 @retval TRUE DevicePath points to an Network that is connected
1399 @retval FALSE DevicePath does not point to a bootable network
1403 BdsLibNetworkBootWithMediaPresent (
1404 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1408 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1410 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
1411 BOOLEAN MediaPresent
;
1413 MediaPresent
= FALSE
;
1415 UpdatedDevicePath
= DevicePath
;
1416 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1417 if (EFI_ERROR (Status
)) {
1419 // Device not present so see if we need to connect it
1421 Status
= BdsLibConnectDevicePath (DevicePath
);
1422 if (!EFI_ERROR (Status
)) {
1424 // This one should work after we did the connect
1426 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1430 if (!EFI_ERROR (Status
)) {
1431 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
1432 if (!EFI_ERROR (Status
)) {
1433 if (Snp
->Mode
->MediaPresentSupported
) {
1434 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
1436 // In case some one else is using the SNP check to see if it's connected
1438 MediaPresent
= Snp
->Mode
->MediaPresent
;
1441 // No one is using SNP so we need to Start and Initialize so
1442 // MediaPresent will be valid.
1444 Status
= Snp
->Start (Snp
);
1445 if (!EFI_ERROR (Status
)) {
1446 Status
= Snp
->Initialize (Snp
, 0, 0);
1447 if (!EFI_ERROR (Status
)) {
1448 MediaPresent
= Snp
->Mode
->MediaPresent
;
1449 Snp
->Shutdown (Snp
);
1455 MediaPresent
= TRUE
;
1460 return MediaPresent
;
1466 For a bootable Device path, return its boot type
1468 @param DevicePath The bootable device Path to check
1470 @return UINT32 Boot type :
1472 @return // If the device path contains any media deviec path node, it is media boot type
1473 @return // For the floppy node, handle it as media node
1475 @return BDS_EFI_MEDIA_HD_BOOT
1476 @return BDS_EFI_MEDIA_CDROM_BOOT
1477 @return BDS_EFI_ACPI_FLOPPY_BOOT
1479 @return // If the device path not contains any media deviec path node, and
1480 @return // its last device path node point to a message device path node, it is
1481 @return // a message boot type
1483 @return BDS_EFI_MESSAGE_ATAPI_BOOT
1484 @return BDS_EFI_MESSAGE_SCSI_BOOT
1485 @return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
1486 @return BDS_EFI_MESSAGE_MISC_BOOT
1488 @return // Legacy boot type
1490 @return BDS_LEGACY_BBS_BOOT
1492 @return // If a EFI Removable BlockIO device path not point to a media and message devie,
1493 @return // it is unsupported
1495 @return BDS_EFI_UNSUPPORT
1499 BdsGetBootTypeFromDevicePath (
1500 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1503 ACPI_HID_DEVICE_PATH
*Acpi
;
1504 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1505 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1508 if (NULL
== DevicePath
) {
1509 return BDS_EFI_UNSUPPORT
;
1512 TempDevicePath
= DevicePath
;
1514 while (!IsDevicePathEndType (TempDevicePath
)) {
1515 switch (DevicePathType (TempDevicePath
)) {
1516 case BBS_DEVICE_PATH
:
1517 return BDS_LEGACY_BBS_BOOT
;
1518 case MEDIA_DEVICE_PATH
:
1519 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
1520 return BDS_EFI_MEDIA_HD_BOOT
;
1521 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
1522 return BDS_EFI_MEDIA_CDROM_BOOT
;
1525 case ACPI_DEVICE_PATH
:
1526 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
1527 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
1528 return BDS_EFI_ACPI_FLOPPY_BOOT
;
1531 case MESSAGING_DEVICE_PATH
:
1533 // if the device path not only point to driver device, it is not a messaging device path.
1535 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
1536 if (!IsDevicePathEndType (LastDeviceNode
)) {
1540 if (DevicePathSubType(TempDevicePath
) == MSG_ATAPI_DP
) {
1541 return BDS_EFI_MESSAGE_ATAPI_BOOT
;
1542 } else if (DevicePathSubType(TempDevicePath
) == MSG_USB_DP
) {
1543 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
1544 } else if (DevicePathSubType(TempDevicePath
) == MSG_SCSI_DP
) {
1545 return BDS_EFI_MESSAGE_SCSI_BOOT
;
1547 return BDS_EFI_MESSAGE_MISC_BOOT
;
1551 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1554 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 DevPath -- the Device path in a boot option
1563 CheckMedia -- if true, check the device is ready to boot now.
1565 @return TRUE -- the Device path is valide
1566 @return FALSE -- the Device path is invalide .
1570 BdsLibIsValidEFIBootOptDevicePath (
1571 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
1572 IN BOOLEAN CheckMedia
1577 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1578 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1579 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1581 TempDevicePath
= DevPath
;
1582 LastDeviceNode
= DevPath
;
1584 // Check if it's a valid boot option for network boot device
1585 // Only check if there is SimpleNetworkProtocol installed. If yes, that means
1586 // there is the network card there.
1588 Status
= gBS
->LocateDevicePath (
1589 &gEfiSimpleNetworkProtocolGuid
,
1593 if (EFI_ERROR (Status
)) {
1595 // Device not present so see if we need to connect it
1597 TempDevicePath
= DevPath
;
1598 BdsLibConnectDevicePath (TempDevicePath
);
1599 Status
= gBS
->LocateDevicePath (
1600 &gEfiSimpleNetworkProtocolGuid
,
1605 if (!EFI_ERROR (Status
)) {
1608 // Test if it is ready to boot now
1610 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
1619 // If the boot option point to a file, it is a valid EFI boot option,
1620 // and assume it is ready to boot now
1622 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1623 LastDeviceNode
= TempDevicePath
;
1624 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1626 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
1627 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
1632 // If the boot option point to a internal Shell, it is a valid EFI boot option,
1633 // and assume it is ready to boot now
1635 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
1640 // 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
1642 TempDevicePath
= DevPath
;
1643 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1644 if (EFI_ERROR (Status
)) {
1646 // Device not present so see if we need to connect it
1648 Status
= BdsLibConnectDevicePath (DevPath
);
1649 if (!EFI_ERROR (Status
)) {
1651 // Try again to get the Block Io protocol after we did the connect
1653 TempDevicePath
= DevPath
;
1654 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1657 if (!EFI_ERROR (Status
)) {
1658 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1659 if (!EFI_ERROR (Status
)) {
1662 // Test if it is ready to boot now
1664 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1673 // 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,
1675 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1676 if (!EFI_ERROR (Status
)) {
1679 // Test if it is ready to boot now
1681 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
1695 According to a file guild, check a Fv file device path is valid. If it is invalid,
1696 try to return the valid device path.
1697 FV address maybe changes for memory layout adjust from time to time, use this funciton
1698 could promise the Fv file device path is right.
1700 @param DevicePath on input, the Fv file device path need to check on
1701 output, the updated valid Fv file device path
1702 @param FileGuid the Fv file guild
1704 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1706 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1708 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1710 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1711 and return the updated device path in DevicePath
1716 BdsLibUpdateFvFileDevicePath (
1717 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1718 IN EFI_GUID
*FileGuid
1721 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1722 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1724 EFI_GUID
*GuidPoint
;
1726 UINTN FvHandleCount
;
1727 EFI_HANDLE
*FvHandleBuffer
;
1728 EFI_FV_FILETYPE Type
;
1730 EFI_FV_FILE_ATTRIBUTES Attributes
;
1731 UINT32 AuthenticationStatus
;
1733 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1734 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1735 EFI_FIRMWARE_VOLUME_PROTOCOL
*Fv
;
1737 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1739 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1740 EFI_HANDLE FoundFvHandle
;
1741 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1743 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1744 return EFI_INVALID_PARAMETER
;
1746 if (FileGuid
== NULL
) {
1747 return EFI_INVALID_PARAMETER
;
1750 // Check whether the device path point to the default the input Fv file
1752 TempDevicePath
= *DevicePath
;
1753 LastDeviceNode
= TempDevicePath
;
1754 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1755 LastDeviceNode
= TempDevicePath
;
1756 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1758 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1759 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1761 if (GuidPoint
== NULL
) {
1763 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1765 return EFI_UNSUPPORTED
;
1767 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1769 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1771 return EFI_UNSUPPORTED
;
1775 // Check whether the input Fv file device path is valid
1777 TempDevicePath
= *DevicePath
;
1778 FoundFvHandle
= NULL
;
1779 Status
= gBS
->LocateDevicePath (
1780 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1781 &gEfiFirmwareVolumeProtocolGuid
,
1783 &gEfiFirmwareVolume2ProtocolGuid
,
1788 if (!EFI_ERROR (Status
)) {
1789 Status
= gBS
->HandleProtocol (
1791 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1792 &gEfiFirmwareVolumeProtocolGuid
,
1794 &gEfiFirmwareVolume2ProtocolGuid
,
1798 if (!EFI_ERROR (Status
)) {
1800 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1802 Status
= Fv
->ReadFile (
1809 &AuthenticationStatus
1811 if (!EFI_ERROR (Status
)) {
1812 return EFI_ALREADY_STARTED
;
1818 // Look for the input wanted FV file in current FV
1819 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
1822 FoundFvHandle
= NULL
;
1823 Status
= gBS
->HandleProtocol (
1825 &gEfiLoadedImageProtocolGuid
,
1826 (VOID
**) &LoadedImage
1828 if (!EFI_ERROR (Status
)) {
1829 Status
= gBS
->HandleProtocol (
1830 LoadedImage
->DeviceHandle
,
1831 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1832 &gEfiFirmwareVolumeProtocolGuid
,
1834 &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 gBS
->LocateHandleBuffer (
1860 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1861 &gEfiFirmwareVolumeProtocolGuid
,
1863 &gEfiFirmwareVolume2ProtocolGuid
,
1869 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1870 gBS
->HandleProtocol (
1871 FvHandleBuffer
[Index
],
1872 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1873 &gEfiFirmwareVolumeProtocolGuid
,
1875 &gEfiFirmwareVolume2ProtocolGuid
,
1880 Status
= Fv
->ReadFile (
1887 &AuthenticationStatus
1889 if (EFI_ERROR (Status
)) {
1891 // Skip if input Fv file not in the FV
1896 FoundFvHandle
= FvHandleBuffer
[Index
];
1903 // Build the shell device path
1905 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1906 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1907 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1908 *DevicePath
= NewDevicePath
;
1911 return EFI_NOT_FOUND
;