2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
18 BOOLEAN mEnumBootDevice
= FALSE
;
19 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
22 /// This GUID is used for an EFI Variable that stores the front device pathes
23 /// for a partial device path that starts with the HD node.
25 EFI_GUID mHdBootVariablePrivateGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
28 /// This GUID is used for register UNI string.
30 EFI_GUID mBdsLibStringPackGuid
= { 0x3b4d9b23, 0x95ac, 0x44f6, { 0x9f, 0xcd, 0xe, 0x95, 0x94, 0x58, 0x6c, 0x72 } };
33 /// This GUID is used for Set/Get platform language into/from variable at last time enumeration to ensure the enumeration will
34 /// only execute once.
36 EFI_GUID mBdsLibLastLangGuid
= { 0xe8c545b, 0xa2ee, 0x470d, { 0x8e, 0x26, 0xbd, 0xa1, 0xa1, 0x3c, 0xa, 0xa3 } };
39 The constructor function register UNI strings into imageHandle.
41 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
46 @retval EFI_SUCCESS The constructor successfully added string package.
47 @retval Other value The constructor can't add string package.
52 GenericBdsLibConstructor (
53 IN EFI_HANDLE ImageHandle
,
54 IN EFI_SYSTEM_TABLE
*SystemTable
58 gBdsLibStringPackHandle
= HiiAddPackages (
59 &mBdsLibStringPackGuid
,
65 ASSERT (gBdsLibStringPackHandle
!= NULL
);
73 Boot the legacy system with the boot option
75 @param Option The legacy boot option which have BBS device path
77 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
79 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
84 IN BDS_COMMON_OPTION
*Option
88 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
90 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
91 if (EFI_ERROR (Status
)) {
93 // If no LegacyBios protocol we do not support legacy boot
95 return EFI_UNSUPPORTED
;
98 // Notes: if we separate the int 19, then we don't need to refresh BBS
100 BdsRefreshBbsTableForBoot (Option
);
103 // Write boot to OS performance data for legacy boot.
106 WriteBootToOsPerformanceData ();
109 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
110 return LegacyBios
->LegacyBoot (
112 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
113 Option
->LoadOptionsSize
,
119 Internal function to check if the input boot option is a valid EFI NV Boot####.
121 @param OptionToCheck Boot option to be checked.
123 @retval TRUE This boot option matches a valid EFI NV Boot####.
124 @retval FALSE If not.
128 IsBootOptionValidNVVarialbe (
129 IN BDS_COMMON_OPTION
*OptionToCheck
133 BDS_COMMON_OPTION
*BootOption
;
135 CHAR16 OptionName
[20];
139 InitializeListHead (&TempList
);
140 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
142 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
143 if (BootOption
== NULL
) {
148 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
149 // valid EFI NV Boot####.
151 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
152 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
157 FreePool (BootOption
);
163 Check whether a USB device match the specified USB Class device path. This
164 function follows "Load Option Processing" behavior in UEFI specification.
166 @param UsbIo USB I/O protocol associated with the USB device.
167 @param UsbClass The USB Class device path to match.
169 @retval TRUE The USB device match the USB Class device path.
170 @retval FALSE The USB device does not match the USB Class device path.
175 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
176 IN USB_CLASS_DEVICE_PATH
*UsbClass
180 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
181 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
183 UINT8 DeviceSubClass
;
184 UINT8 DeviceProtocol
;
186 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
187 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
192 // Check Vendor Id and Product Id.
194 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
195 if (EFI_ERROR (Status
)) {
199 if ((UsbClass
->VendorId
!= 0xffff) &&
200 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
204 if ((UsbClass
->ProductId
!= 0xffff) &&
205 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
209 DeviceClass
= DevDesc
.DeviceClass
;
210 DeviceSubClass
= DevDesc
.DeviceSubClass
;
211 DeviceProtocol
= DevDesc
.DeviceProtocol
;
212 if (DeviceClass
== 0) {
214 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
215 // Protocol in Interface Descriptor instead.
217 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
218 if (EFI_ERROR (Status
)) {
222 DeviceClass
= IfDesc
.InterfaceClass
;
223 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
224 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
228 // Check Class, SubClass and Protocol.
230 if ((UsbClass
->DeviceClass
!= 0xff) &&
231 (UsbClass
->DeviceClass
!= DeviceClass
)) {
235 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
236 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
240 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
241 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
249 Check whether a USB device match the specified USB WWID device path. This
250 function follows "Load Option Processing" behavior in UEFI specification.
252 @param UsbIo USB I/O protocol associated with the USB device.
253 @param UsbWwid The USB WWID device path to match.
255 @retval TRUE The USB device match the USB WWID device path.
256 @retval FALSE The USB device does not match the USB WWID device path.
261 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
262 IN USB_WWID_DEVICE_PATH
*UsbWwid
266 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
267 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
273 CHAR16
*SerialNumberStr
;
276 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
277 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
282 // Check Vendor Id and Product Id.
284 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
285 if (EFI_ERROR (Status
)) {
288 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
289 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
294 // Check Interface Number.
296 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
297 if (EFI_ERROR (Status
)) {
300 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
305 // Check Serial Number.
307 if (DevDesc
.StrSerialNumber
== 0) {
312 // Get all supported languages.
316 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
317 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
322 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
324 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
325 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
326 if (CompareStr
[CompareLen
- 1] == L
'\0') {
331 // Compare serial number in each supported language.
333 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
334 SerialNumberStr
= NULL
;
335 Status
= UsbIo
->UsbGetStringDescriptor (
338 DevDesc
.StrSerialNumber
,
341 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
345 Length
= StrLen (SerialNumberStr
);
346 if ((Length
>= CompareLen
) &&
347 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
348 FreePool (SerialNumberStr
);
352 FreePool (SerialNumberStr
);
359 Find a USB device path which match the specified short-form device path start
360 with USB Class or USB WWID device path and load the boot file then return the
361 image handle. If ParentDevicePath is NULL, this function will search in all USB
362 devices of the platform. If ParentDevicePath is not NULL,this function will only
363 search in its child devices.
365 @param ParentDevicePath The device path of the parent.
366 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
368 @return The image Handle if find load file from specified short-form device path
369 or NULL if not found.
374 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
375 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
379 UINTN UsbIoHandleCount
;
380 EFI_HANDLE
*UsbIoHandleBuffer
;
381 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
382 EFI_USB_IO_PROTOCOL
*UsbIo
;
386 EFI_HANDLE ImageHandle
;
388 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
389 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
391 FullDevicePath
= NULL
;
395 // Get all UsbIo Handles.
397 UsbIoHandleCount
= 0;
398 UsbIoHandleBuffer
= NULL
;
399 Status
= gBS
->LocateHandleBuffer (
401 &gEfiUsbIoProtocolGuid
,
406 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
410 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
411 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
413 // Get the Usb IO interface.
415 Status
= gBS
->HandleProtocol(
416 UsbIoHandleBuffer
[Index
],
417 &gEfiUsbIoProtocolGuid
,
420 if (EFI_ERROR (Status
)) {
424 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
425 if (UsbIoDevicePath
== NULL
) {
429 if (ParentDevicePath
!= NULL
) {
431 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
433 Size
= GetDevicePathSize (UsbIoDevicePath
);
434 if ((Size
< ParentSize
) ||
435 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
440 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
441 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
443 // Try to find if there is the boot file in this DevicePath
445 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
446 if (!IsDevicePathEnd (NextDevicePath
)) {
447 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
449 // Connect the full device path, so that Simple File System protocol
450 // could be installed for this USB device.
452 BdsLibConnectDevicePath (FullDevicePath
);
453 Status
= gBS
->LoadImage (
461 FreePool (FullDevicePath
);
463 FullDevicePath
= UsbIoDevicePath
;
464 Status
= EFI_NOT_FOUND
;
468 // If we didn't find an image directly, we need to try as if it is a removable device boot option
469 // and load the image according to the default boot behavior for removable device.
471 if (EFI_ERROR (Status
)) {
473 // check if there is a bootable removable media could be found in this device path ,
474 // and get the bootable media handle
476 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
477 if (Handle
== NULL
) {
481 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
482 // machinename is ia32, ia64, x64, ...
484 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
485 if (FullDevicePath
!= NULL
) {
486 Status
= gBS
->LoadImage (
494 if (EFI_ERROR (Status
)) {
496 // The DevicePath failed, and it's not a valid
497 // removable media device.
509 FreePool (UsbIoHandleBuffer
);
514 Expand USB Class or USB WWID device path node to be full device path of a USB
515 device in platform then load the boot file on this full device path and return the
518 This function support following 4 cases:
519 1) Boot Option device path starts with a USB Class or USB WWID device path,
520 and there is no Media FilePath device path in the end.
521 In this case, it will follow Removable Media Boot Behavior.
522 2) Boot Option device path starts with a USB Class or USB WWID device path,
523 and ended with Media FilePath device path.
524 3) Boot Option device path starts with a full device path to a USB Host Controller,
525 contains a USB Class or USB WWID device path node, while not ended with Media
526 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
527 4) Boot Option device path starts with a full device path to a USB Host Controller,
528 contains a USB Class or USB WWID device path node, and ended with Media
529 FilePath device path.
531 @param DevicePath The Boot Option device path.
533 @return The image handle of boot file, or NULL if there is no boot file found in
534 the specified USB Class or USB WWID device path.
538 BdsExpandUsbShortFormDevicePath (
539 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
542 EFI_HANDLE
*ImageHandle
;
543 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
544 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
547 // Search for USB Class or USB WWID device path node.
549 ShortFormDevicePath
= NULL
;
551 TempDevicePath
= DevicePath
;
552 while (!IsDevicePathEnd (TempDevicePath
)) {
553 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
554 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
555 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
556 ShortFormDevicePath
= TempDevicePath
;
559 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
562 if (ShortFormDevicePath
== NULL
) {
564 // No USB Class or USB WWID device path node found, do nothing.
569 if (ShortFormDevicePath
== DevicePath
) {
571 // Boot Option device path starts with USB Class or USB WWID device path.
573 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
574 if (ImageHandle
== NULL
) {
576 // Failed to find a match in existing devices, connect the short form USB
577 // device path and try again.
579 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
580 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
584 // Boot Option device path contains USB Class or USB WWID device path node.
588 // Prepare the parent device path for search.
590 TempDevicePath
= DuplicateDevicePath (DevicePath
);
591 ASSERT (TempDevicePath
!= NULL
);
592 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
595 // The USB Host Controller device path is already in Boot Option device path
596 // and USB Bus driver already support RemainingDevicePath starts with USB
597 // Class or USB WWID device path, so just search in existing USB devices and
598 // doesn't perform ConnectController here.
600 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
601 FreePool (TempDevicePath
);
608 Process the boot option follow the UEFI specification and
609 special treat the legacy boot option with BBS_DEVICE_PATH.
611 @param Option The boot option need to be processed
612 @param DevicePath The device path which describe where to load the
613 boot image or the legacy BBS device path to boot
615 @param ExitDataSize The size of exit data.
616 @param ExitData Data returned when Boot image failed.
618 @retval EFI_SUCCESS Boot from the input boot option successfully.
619 @retval EFI_NOT_FOUND If the Device Path is not found in the system
624 BdsLibBootViaBootOption (
625 IN BDS_COMMON_OPTION
*Option
,
626 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
627 OUT UINTN
*ExitDataSize
,
628 OUT CHAR16
**ExitData OPTIONAL
633 EFI_HANDLE ImageHandle
;
634 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
635 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
636 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
637 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
638 LIST_ENTRY TempBootLists
;
641 // Record the performance data for End of BDS
643 PERF_END(NULL
, "BDS", NULL
, 0);
649 // Notes: this code can be remove after the s3 script table
650 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
651 // EVT_SIGNAL_LEGACY_BOOT
653 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
654 if (!EFI_ERROR (Status
)) {
655 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
658 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
661 WorkingDevicePath
= NULL
;
662 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
663 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
664 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
665 (HARDDRIVE_DEVICE_PATH
*)DevicePath
667 if (WorkingDevicePath
!= NULL
) {
668 DevicePath
= WorkingDevicePath
;
673 // Expand USB Class or USB WWID drive path node to full device path.
675 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
678 // Signal the EVT_SIGNAL_READY_TO_BOOT event
680 EfiSignalEventReadyToBoot();
683 // Adjust the different type memory page number just before booting
684 // and save the updated info into the variable for next boot to use
686 BdsSetMemoryTypeInformationVariable ();
692 if (IsBootOptionValidNVVarialbe (Option
)) {
694 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
695 // In this case, "BootCurrent" is not created.
696 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
700 &gEfiGlobalVariableGuid
,
701 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
708 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
709 // Here get the ImageHandle for the non USB class or WWID device path.
711 if (ImageHandle
== NULL
) {
712 ASSERT (Option
->DevicePath
!= NULL
);
713 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
714 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
717 // Check to see if we should legacy BOOT. If yes then do the legacy boot
719 return BdsLibDoLegacyBoot (Option
);
723 // If the boot option point to Internal FV shell, make sure it is valid
725 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
726 if (!EFI_ERROR(Status
)) {
727 if (Option
->DevicePath
!= NULL
) {
728 FreePool(Option
->DevicePath
);
730 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
731 ASSERT(Option
->DevicePath
!= NULL
);
732 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
734 // Update the shell boot option
736 InitializeListHead (&TempBootLists
);
737 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
740 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
742 FreePool (DevicePath
);
743 DevicePath
= Option
->DevicePath
;
748 if (Option
->Description
== NULL
) {
749 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
751 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
756 Status
= gBS
->LoadImage (
766 // If we didn't find an image directly, we need to try as if it is a removable device boot option
767 // and load the image according to the default boot behavior for removable device.
769 if (EFI_ERROR (Status
)) {
771 // check if there is a bootable removable media could be found in this device path ,
772 // and get the bootable media handle
774 Handle
= BdsLibGetBootableHandle(DevicePath
);
775 if (Handle
== NULL
) {
779 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
780 // machinename is ia32, ia64, x64, ...
782 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
783 if (FilePath
!= NULL
) {
784 Status
= gBS
->LoadImage (
792 if (EFI_ERROR (Status
)) {
794 // The DevicePath failed, and it's not a valid
795 // removable media device.
802 if (EFI_ERROR (Status
)) {
804 // It there is any error from the Boot attempt exit now.
810 // Provide the image with it's load options
812 if (ImageHandle
== NULL
) {
815 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
816 ASSERT_EFI_ERROR (Status
);
818 if (Option
->LoadOptionsSize
!= 0) {
819 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
820 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
823 // Before calling the image, enable the Watchdog Timer for
824 // the 5 Minute period
826 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
829 // Write boot to OS performance data for UEFI boot
832 WriteBootToOsPerformanceData ();
835 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
836 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
839 // Clear the Watchdog Timer after the image returns
841 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
845 // Clear Boot Current
849 &gEfiGlobalVariableGuid
,
850 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
860 Expand a device path that starts with a hard drive media device path node to be a
861 full device path that includes the full hardware path to the device. We need
862 to do this so it can be booted. As an optimization the front match (the part point
863 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
864 so a connect all is not required on every boot. All successful history device path
865 which point to partition node (the front part) will be saved.
867 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
868 drive media device path.
869 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
873 EFI_DEVICE_PATH_PROTOCOL
*
875 BdsExpandPartitionPartialDevicePathToFull (
876 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
880 UINTN BlockIoHandleCount
;
881 EFI_HANDLE
*BlockIoBuffer
;
882 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
883 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
884 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
887 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
888 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
889 UINTN CachedDevicePathSize
;
892 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
895 FullDevicePath
= NULL
;
897 // Check if there is prestore 'HDDP' variable.
898 // If exist, search the front path which point to partition node in the variable instants.
899 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
901 CachedDevicePath
= BdsLibGetVariableAndSize (
903 &mHdBootVariablePrivateGuid
,
904 &CachedDevicePathSize
907 if (CachedDevicePath
!= NULL
) {
908 TempNewDevicePath
= CachedDevicePath
;
913 // Check every instance of the variable
914 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
915 // partial partition boot option. Second, check whether the instance could be connected.
917 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
918 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
920 // Connect the device path instance, the device path point to hard drive media device path node
921 // e.g. ACPI() /PCI()/ATA()/Partition()
923 Status
= BdsLibConnectDevicePath (Instance
);
924 if (!EFI_ERROR (Status
)) {
930 // Come here means the first instance is not matched
934 } while (TempNewDevicePath
!= NULL
);
938 // Find the matched device path.
939 // Append the file path information from the boot option and return the fully expanded device path.
941 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
942 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
945 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
949 // First delete the matched instance.
951 TempNewDevicePath
= CachedDevicePath
;
952 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
953 FreePool (TempNewDevicePath
);
956 // Second, append the remaining path after the matched instance
958 TempNewDevicePath
= CachedDevicePath
;
959 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
960 FreePool (TempNewDevicePath
);
962 // Save the matching Device Path so we don't need to do a connect all next time
964 Status
= gRT
->SetVariable (
966 &mHdBootVariablePrivateGuid
,
967 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
968 GetDevicePathSize (CachedDevicePath
),
974 FreePool (CachedDevicePath
);
975 return FullDevicePath
;
980 // If we get here we fail to find or 'HDDP' not exist, and now we need
981 // to search all devices in the system for a matched partition
983 BdsLibConnectAllDriversToAllControllers ();
984 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
985 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
987 // If there was an error or there are no device handles that support
988 // the BLOCK_IO Protocol, then return.
993 // Loop through all the device handles that support the BLOCK_IO Protocol
995 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
997 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
998 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
1002 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
1004 // Find the matched partition device path
1006 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
1007 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
1010 // Save the matched partition device path in 'HDDP' variable
1012 if (CachedDevicePath
!= NULL
) {
1014 // Save the matched partition device path as first instance of 'HDDP' variable
1016 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
1017 TempNewDevicePath
= CachedDevicePath
;
1018 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
1019 FreePool(TempNewDevicePath
);
1021 TempNewDevicePath
= CachedDevicePath
;
1022 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
1023 if (TempNewDevicePath
!= NULL
) {
1024 FreePool(TempNewDevicePath
);
1027 TempNewDevicePath
= CachedDevicePath
;
1028 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
1029 FreePool(TempNewDevicePath
);
1032 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
1033 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
1036 ASSERT (CachedDevicePath
!= NULL
);
1037 TempNewDevicePath
= CachedDevicePath
;
1038 while (!IsDevicePathEnd (TempNewDevicePath
)) {
1039 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
1041 // Parse one instance
1043 while (!IsDevicePathEndType (TempNewDevicePath
)) {
1044 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
1048 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
1050 if (InstanceNum
>= 12) {
1051 SetDevicePathEndNode (TempNewDevicePath
);
1056 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
1060 // Save the matching Device Path so we don't need to do a connect all next time
1062 Status
= gRT
->SetVariable (
1064 &mHdBootVariablePrivateGuid
,
1065 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1066 GetDevicePathSize (CachedDevicePath
),
1074 if (CachedDevicePath
!= NULL
) {
1075 FreePool (CachedDevicePath
);
1077 if (BlockIoBuffer
!= NULL
) {
1078 FreePool (BlockIoBuffer
);
1080 return FullDevicePath
;
1084 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
1085 instances, has the same partition node with HardDriveDevicePath device path
1087 @param BlockIoDevicePath Multi device path instances which need to check
1088 @param HardDriveDevicePath A device path which starts with a hard drive media
1091 @retval TRUE There is a matched device path instance.
1092 @retval FALSE There is no matched device path instance.
1097 MatchPartitionDevicePathNode (
1098 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
1099 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
1102 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
1103 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1105 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
1107 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
1112 // Make PreviousDevicePath == the device path node before the end node
1114 DevicePath
= BlockIoDevicePath
;
1115 BlockIoHdDevicePathNode
= NULL
;
1118 // find the partition device path node
1120 while (!IsDevicePathEnd (DevicePath
)) {
1121 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
1122 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
1124 BlockIoHdDevicePathNode
= DevicePath
;
1128 DevicePath
= NextDevicePathNode (DevicePath
);
1131 if (BlockIoHdDevicePathNode
== NULL
) {
1135 // See if the harddrive device path in blockio matches the orig Hard Drive Node
1137 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
1141 // Check for the match
1143 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
1144 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
1145 switch (TmpHdPath
->SignatureType
) {
1146 case SIGNATURE_TYPE_GUID
:
1147 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
1149 case SIGNATURE_TYPE_MBR
:
1150 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
1162 Delete the boot option associated with the handle passed in.
1164 @param Handle The handle which present the device path to create
1167 @retval EFI_SUCCESS Delete the boot option success
1168 @retval EFI_NOT_FOUND If the Device Path is not found in the system
1169 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1170 @retval Other Error return value from SetVariable()
1174 BdsLibDeleteOptionFromHandle (
1175 IN EFI_HANDLE Handle
1179 UINT8
*BootOptionVar
;
1180 UINTN BootOrderSize
;
1181 UINTN BootOptionSize
;
1184 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
1185 UINTN DevicePathSize
;
1186 UINTN OptionDevicePathSize
;
1187 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1188 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
1191 Status
= EFI_SUCCESS
;
1196 // Check "BootOrder" variable, if no, means there is no any boot order.
1198 BootOrder
= BdsLibGetVariableAndSize (
1200 &gEfiGlobalVariableGuid
,
1203 if (BootOrder
== NULL
) {
1204 return EFI_NOT_FOUND
;
1208 // Convert device handle to device path protocol instance
1210 DevicePath
= DevicePathFromHandle (Handle
);
1211 if (DevicePath
== NULL
) {
1212 return EFI_NOT_FOUND
;
1214 DevicePathSize
= GetDevicePathSize (DevicePath
);
1217 // Loop all boot order variable and find the matching device path
1220 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
1221 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1222 BootOptionVar
= BdsLibGetVariableAndSize (
1224 &gEfiGlobalVariableGuid
,
1228 if (BootOptionVar
== NULL
) {
1229 FreePool (BootOrder
);
1230 return EFI_OUT_OF_RESOURCES
;
1233 TempPtr
= BootOptionVar
;
1234 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
1235 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
1236 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
1237 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
1240 // Check whether the device path match
1242 if ((OptionDevicePathSize
== DevicePathSize
) &&
1243 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
1244 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
1245 FreePool (BootOptionVar
);
1249 FreePool (BootOptionVar
);
1254 // Adjust number of boot option for "BootOrder" variable.
1256 Status
= gRT
->SetVariable (
1258 &gEfiGlobalVariableGuid
,
1259 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1264 FreePool (BootOrder
);
1271 Delete all invalid EFI boot options.
1273 @retval EFI_SUCCESS Delete all invalid boot option success
1274 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
1275 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1276 @retval Other Error return value from SetVariable()
1280 BdsDeleteAllInvalidEfiBootOption (
1285 UINT8
*BootOptionVar
;
1286 UINTN BootOrderSize
;
1287 UINTN BootOptionSize
;
1291 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
1292 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
1294 CHAR16
*Description
;
1296 Status
= EFI_SUCCESS
;
1301 // Check "BootOrder" variable firstly, this variable hold the number of boot options
1303 BootOrder
= BdsLibGetVariableAndSize (
1305 &gEfiGlobalVariableGuid
,
1308 if (NULL
== BootOrder
) {
1309 return EFI_NOT_FOUND
;
1313 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
1314 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1315 BootOptionVar
= BdsLibGetVariableAndSize (
1317 &gEfiGlobalVariableGuid
,
1320 if (NULL
== BootOptionVar
) {
1321 FreePool (BootOrder
);
1322 return EFI_OUT_OF_RESOURCES
;
1325 TempPtr
= BootOptionVar
;
1326 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
1327 Description
= (CHAR16
*) TempPtr
;
1328 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
1329 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
1332 // Skip legacy boot option (BBS boot device)
1334 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
1335 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
1336 FreePool (BootOptionVar
);
1341 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
1343 // Delete this invalid boot option "Boot####"
1345 Status
= gRT
->SetVariable (
1347 &gEfiGlobalVariableGuid
,
1348 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1353 // Mark this boot option in boot order as deleted
1355 BootOrder
[Index
] = 0xffff;
1358 FreePool (BootOptionVar
);
1363 // Adjust boot order array
1366 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
1367 if (BootOrder
[Index
] != 0xffff) {
1368 BootOrder
[Index2
] = BootOrder
[Index
];
1372 Status
= gRT
->SetVariable (
1374 &gEfiGlobalVariableGuid
,
1375 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1376 Index2
* sizeof (UINT16
),
1380 FreePool (BootOrder
);
1387 For EFI boot option, BDS separate them as six types:
1388 1. Network - The boot option points to the SimpleNetworkProtocol device.
1389 Bds will try to automatically create this type boot option when enumerate.
1390 2. Shell - The boot option points to internal flash shell.
1391 Bds will try to automatically create this type boot option when enumerate.
1392 3. Removable BlockIo - The boot option only points to the removable media
1393 device, like USB flash disk, DVD, Floppy etc.
1394 These device should contain a *removable* blockIo
1395 protocol in their device handle.
1396 Bds will try to automatically create this type boot option
1398 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
1400 These device should contain a *fixed* blockIo
1401 protocol in their device handle.
1402 BDS will skip fixed blockIo devices, and NOT
1403 automatically create boot option for them. But BDS
1404 will help to delete those fixed blockIo boot option,
1405 whose description rule conflict with other auto-created
1407 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
1408 has SimpleFileSystem Protocol, but has no blockio
1409 protocol. These devices do not offer blockIo
1410 protocol, but BDS still can get the
1411 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
1413 6. File - The boot option points to a file. These boot options are usually
1414 created by user manually or OS loader. BDS will not delete or modify
1417 This function will enumerate all possible boot device in the system, and
1418 automatically create boot options for Network, Shell, Removable BlockIo,
1419 and Non-BlockIo Simplefile devices.
1420 It will only execute once of every boot.
1422 @param BdsBootOptionList The header of the link list which indexed all
1423 current boot options
1425 @retval EFI_SUCCESS Finished all the boot device enumerate and create
1426 the boot option base on that boot device
1428 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
1432 BdsLibEnumerateAllBootOption (
1433 IN OUT LIST_ENTRY
*BdsBootOptionList
1437 UINT16 FloppyNumber
;
1442 UINT16 NonBlockNumber
;
1443 UINTN NumberBlockIoHandles
;
1444 EFI_HANDLE
*BlockIoHandles
;
1445 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
1446 BOOLEAN Removable
[2];
1447 UINTN RemovableIndex
;
1449 UINTN NumOfLoadFileHandles
;
1450 EFI_HANDLE
*LoadFileHandles
;
1451 UINTN FvHandleCount
;
1452 EFI_HANDLE
*FvHandleBuffer
;
1453 EFI_FV_FILETYPE Type
;
1455 EFI_FV_FILE_ATTRIBUTES Attributes
;
1456 UINT32 AuthenticationStatus
;
1457 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1458 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1459 UINTN DevicePathType
;
1461 EFI_HANDLE
*FileSystemHandles
;
1462 UINTN NumberFileSystemHandles
;
1464 EFI_IMAGE_DOS_HEADER DosHeader
;
1467 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1468 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1477 ZeroMem (Buffer
, sizeof (Buffer
));
1480 // If the boot device enumerate happened, just get the boot
1481 // device from the boot order variable
1483 if (mEnumBootDevice
) {
1484 LastLang
= GetVariable (L
"LastEnumLang", &mBdsLibLastLangGuid
);
1485 PlatLang
= GetEfiGlobalVariable (L
"PlatformLang");
1486 ASSERT (PlatLang
!= NULL
);
1487 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
1488 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1489 FreePool (LastLang
);
1490 FreePool (PlatLang
);
1493 Status
= gRT
->SetVariable (
1495 &mBdsLibLastLangGuid
,
1496 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1497 AsciiStrSize (PlatLang
),
1500 ASSERT_EFI_ERROR (Status
);
1502 if (LastLang
!= NULL
) {
1503 FreePool (LastLang
);
1505 FreePool (PlatLang
);
1510 // Notes: this dirty code is to get the legacy boot option from the
1511 // BBS table and create to variable as the EFI boot option, it should
1512 // be removed after the CSM can provide legacy boot option directly
1514 REFRESH_LEGACY_BOOT_OPTIONS
;
1517 // Delete invalid boot option
1519 BdsDeleteAllInvalidEfiBootOption ();
1522 // Parse removable media followed by fixed media.
1523 // The Removable[] array is used by the for-loop below to create removable media boot options
1524 // at first, and then to create fixed media boot options.
1526 Removable
[0] = FALSE
;
1527 Removable
[1] = TRUE
;
1529 gBS
->LocateHandleBuffer (
1531 &gEfiBlockIoProtocolGuid
,
1533 &NumberBlockIoHandles
,
1537 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
1538 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1539 Status
= gBS
->HandleProtocol (
1540 BlockIoHandles
[Index
],
1541 &gEfiBlockIoProtocolGuid
,
1545 // skip the fixed block io then the removable block io
1547 if (EFI_ERROR (Status
) || (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
])) {
1550 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1551 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1553 switch (DevicePathType
) {
1554 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1555 if (FloppyNumber
!= 0) {
1556 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
1558 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
1560 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1565 // Assume a removable SATA device should be the DVD/CD device
1567 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1568 case BDS_EFI_MESSAGE_SATA_BOOT
:
1569 if (CdromNumber
!= 0) {
1570 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
1572 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
1574 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
1575 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1579 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1580 if (UsbNumber
!= 0) {
1581 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
1583 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
1585 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1589 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1590 if (ScsiNumber
!= 0) {
1591 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
1593 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
1595 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1599 case BDS_EFI_MESSAGE_MISC_BOOT
:
1600 if (MiscNumber
!= 0) {
1601 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
1603 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
1605 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1615 if (NumberBlockIoHandles
!= 0) {
1616 FreePool (BlockIoHandles
);
1620 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1623 gBS
->LocateHandleBuffer (
1625 &gEfiSimpleFileSystemProtocolGuid
,
1627 &NumberFileSystemHandles
,
1630 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1631 Status
= gBS
->HandleProtocol (
1632 FileSystemHandles
[Index
],
1633 &gEfiBlockIoProtocolGuid
,
1636 if (!EFI_ERROR (Status
)) {
1638 // Skip if the file system handle supports a BlkIo protocol,
1644 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1645 // machinename is ia32, ia64, x64, ...
1647 Hdr
.Union
= &HdrData
;
1649 Status
= BdsLibGetImageHeader (
1650 FileSystemHandles
[Index
],
1651 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1655 if (!EFI_ERROR (Status
) &&
1656 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1657 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1663 // No such file or the file is not a EFI application, delete this boot option
1665 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1667 if (NonBlockNumber
!= 0) {
1668 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
1670 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
1672 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1677 if (NumberFileSystemHandles
!= 0) {
1678 FreePool (FileSystemHandles
);
1682 // Parse Network Boot Device
1684 NumOfLoadFileHandles
= 0;
1686 // Search Load File protocol for PXE boot option.
1688 gBS
->LocateHandleBuffer (
1690 &gEfiLoadFileProtocolGuid
,
1692 &NumOfLoadFileHandles
,
1696 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1698 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
1700 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
1702 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1705 if (NumOfLoadFileHandles
!= 0) {
1706 FreePool (LoadFileHandles
);
1710 // Check if we have on flash shell
1712 gBS
->LocateHandleBuffer (
1714 &gEfiFirmwareVolume2ProtocolGuid
,
1719 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1720 gBS
->HandleProtocol (
1721 FvHandleBuffer
[Index
],
1722 &gEfiFirmwareVolume2ProtocolGuid
,
1726 Status
= Fv
->ReadFile (
1728 PcdGetPtr(PcdShellFile
),
1733 &AuthenticationStatus
1735 if (EFI_ERROR (Status
)) {
1737 // Skip if no shell file in the FV
1742 // Build the shell boot option
1744 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1747 if (FvHandleCount
!= 0) {
1748 FreePool (FvHandleBuffer
);
1751 // Make sure every boot only have one time
1752 // boot device enumerate
1754 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1755 mEnumBootDevice
= TRUE
;
1761 Build the boot option with the handle parsed in
1763 @param Handle The handle which present the device path to create
1765 @param BdsBootOptionList The header of the link list which indexed all
1766 current boot options
1767 @param String The description of the boot option.
1772 BdsLibBuildOptionFromHandle (
1773 IN EFI_HANDLE Handle
,
1774 IN LIST_ENTRY
*BdsBootOptionList
,
1778 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1780 DevicePath
= DevicePathFromHandle (Handle
);
1783 // Create and register new boot option
1785 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1790 Build the on flash shell boot option with the handle parsed in.
1792 @param Handle The handle which present the device path to create
1793 on flash shell boot option
1794 @param BdsBootOptionList The header of the link list which indexed all
1795 current boot options
1800 BdsLibBuildOptionFromShell (
1801 IN EFI_HANDLE Handle
,
1802 IN OUT LIST_ENTRY
*BdsBootOptionList
1805 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1806 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1808 DevicePath
= DevicePathFromHandle (Handle
);
1811 // Build the shell device path
1813 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1815 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1818 // Create and register the shell boot option
1820 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1825 Boot from the UEFI spec defined "BootNext" variable.
1837 BDS_COMMON_OPTION
*BootOption
;
1838 LIST_ENTRY TempList
;
1843 // Init the boot option name buffer and temp link list
1845 InitializeListHead (&TempList
);
1846 ZeroMem (Buffer
, sizeof (Buffer
));
1848 BootNext
= BdsLibGetVariableAndSize (
1850 &gEfiGlobalVariableGuid
,
1855 // Clear the boot next variable first
1857 if (BootNext
!= NULL
) {
1860 &gEfiGlobalVariableGuid
,
1861 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1867 // Start to build the boot option and try to boot
1869 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1870 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1871 ASSERT (BootOption
!= NULL
);
1872 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1873 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1879 Return the bootable media handle.
1880 First, check the device is connected
1881 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1882 Third, detect the the default boot file in the Media, and return the removable Media handle.
1884 @param DevicePath Device Path to a bootable device
1886 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1891 BdsLibGetBootableHandle (
1892 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1896 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1897 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1899 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1901 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1904 EFI_HANDLE ReturnHandle
;
1905 EFI_HANDLE
*SimpleFileSystemHandles
;
1907 UINTN NumberSimpleFileSystemHandles
;
1909 EFI_IMAGE_DOS_HEADER DosHeader
;
1910 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1911 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1913 UpdatedDevicePath
= DevicePath
;
1916 // Check whether the device is connected
1918 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1919 if (EFI_ERROR (Status
)) {
1921 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1923 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1924 if (EFI_ERROR (Status
)) {
1926 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1928 UpdatedDevicePath
= DevicePath
;
1929 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1930 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1934 // For removable device boot option, its contained device path only point to the removable device handle,
1935 // should make sure all its children handles (its child partion or media handles) are created and connected.
1937 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1939 // Get BlockIo protocol and check removable attribute
1941 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1943 // Issue a dummy read to the device to check for media change.
1944 // When the removable media is changed, any Block IO read/write will
1945 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1946 // returned. After the Block IO protocol is reinstalled, subsequent
1947 // Block IO read/write will success.
1949 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1950 if (Buffer
!= NULL
) {
1951 BlockIo
->ReadBlocks (
1953 BlockIo
->Media
->MediaId
,
1955 BlockIo
->Media
->BlockSize
,
1963 // Detect the the default boot file from removable Media
1967 // 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
1968 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1970 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1971 ASSERT (DupDevicePath
!= NULL
);
1973 UpdatedDevicePath
= DupDevicePath
;
1974 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1976 // 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
1977 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1979 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1980 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
1982 // Remove the usb node, let the device path only point to PCI node
1984 SetDevicePathEndNode (UpdatedDevicePath
);
1985 UpdatedDevicePath
= DupDevicePath
;
1987 UpdatedDevicePath
= DevicePath
;
1991 // Get the device path size of boot option
1993 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
1994 ReturnHandle
= NULL
;
1995 gBS
->LocateHandleBuffer (
1997 &gEfiSimpleFileSystemProtocolGuid
,
1999 &NumberSimpleFileSystemHandles
,
2000 &SimpleFileSystemHandles
2002 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
2004 // Get the device path size of SimpleFileSystem handle
2006 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
2007 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
2009 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
2011 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
2013 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2014 // machinename is ia32, ia64, x64, ...
2016 Hdr
.Union
= &HdrData
;
2017 Status
= BdsLibGetImageHeader (
2018 SimpleFileSystemHandles
[Index
],
2019 EFI_REMOVABLE_MEDIA_FILE_NAME
,
2023 if (!EFI_ERROR (Status
) &&
2024 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
2025 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
2026 ReturnHandle
= SimpleFileSystemHandles
[Index
];
2032 FreePool(DupDevicePath
);
2034 if (SimpleFileSystemHandles
!= NULL
) {
2035 FreePool(SimpleFileSystemHandles
);
2038 return ReturnHandle
;
2042 Check to see if the network cable is plugged in. If the DevicePath is not
2043 connected it will be connected.
2045 @param DevicePath Device Path to check
2047 @retval TRUE DevicePath points to an Network that is connected
2048 @retval FALSE DevicePath does not point to a bootable network
2052 BdsLibNetworkBootWithMediaPresent (
2053 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2057 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
2059 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2060 BOOLEAN MediaPresent
;
2061 UINT32 InterruptStatus
;
2063 MediaPresent
= FALSE
;
2065 UpdatedDevicePath
= DevicePath
;
2067 // Locate Load File Protocol for PXE boot option first
2069 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2070 if (EFI_ERROR (Status
)) {
2072 // Device not present so see if we need to connect it
2074 Status
= BdsLibConnectDevicePath (DevicePath
);
2075 if (!EFI_ERROR (Status
)) {
2077 // This one should work after we did the connect
2079 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2083 if (!EFI_ERROR (Status
)) {
2084 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
2085 if (EFI_ERROR (Status
)) {
2087 // Failed to open SNP from this handle, try to get SNP from parent handle
2089 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
2090 if (UpdatedDevicePath
!= NULL
) {
2091 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2092 if (!EFI_ERROR (Status
)) {
2094 // SNP handle found, get SNP from it
2096 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
2101 if (!EFI_ERROR (Status
)) {
2102 if (Snp
->Mode
->MediaPresentSupported
) {
2103 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
2105 // Invoke Snp->GetStatus() to refresh the media status
2107 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
2110 // In case some one else is using the SNP check to see if it's connected
2112 MediaPresent
= Snp
->Mode
->MediaPresent
;
2115 // No one is using SNP so we need to Start and Initialize so
2116 // MediaPresent will be valid.
2118 Status
= Snp
->Start (Snp
);
2119 if (!EFI_ERROR (Status
)) {
2120 Status
= Snp
->Initialize (Snp
, 0, 0);
2121 if (!EFI_ERROR (Status
)) {
2122 MediaPresent
= Snp
->Mode
->MediaPresent
;
2123 Snp
->Shutdown (Snp
);
2129 MediaPresent
= TRUE
;
2134 return MediaPresent
;
2138 For a bootable Device path, return its boot type.
2140 @param DevicePath The bootable device Path to check
2142 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2143 which subtype is MEDIA_HARDDRIVE_DP
2144 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2145 which subtype is MEDIA_CDROM_DP
2146 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
2147 which HID is floppy device.
2148 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2149 and its last device path node's subtype is MSG_ATAPI_DP.
2150 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2151 and its last device path node's subtype is MSG_SCSI_DP.
2152 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2153 and its last device path node's subtype is MSG_USB_DP.
2154 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
2155 its last device path node point to a message device path node.
2156 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
2157 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
2162 BdsGetBootTypeFromDevicePath (
2163 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2166 ACPI_HID_DEVICE_PATH
*Acpi
;
2167 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2168 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2171 if (NULL
== DevicePath
) {
2172 return BDS_EFI_UNSUPPORT
;
2175 TempDevicePath
= DevicePath
;
2177 while (!IsDevicePathEndType (TempDevicePath
)) {
2178 switch (DevicePathType (TempDevicePath
)) {
2179 case BBS_DEVICE_PATH
:
2180 return BDS_LEGACY_BBS_BOOT
;
2181 case MEDIA_DEVICE_PATH
:
2182 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
2183 return BDS_EFI_MEDIA_HD_BOOT
;
2184 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
2185 return BDS_EFI_MEDIA_CDROM_BOOT
;
2188 case ACPI_DEVICE_PATH
:
2189 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
2190 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
2191 return BDS_EFI_ACPI_FLOPPY_BOOT
;
2194 case MESSAGING_DEVICE_PATH
:
2196 // Get the last device path node
2198 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
2199 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
2201 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
2204 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
2207 // if the device path not only point to driver device, it is not a messaging device path,
2209 if (!IsDevicePathEndType (LastDeviceNode
)) {
2213 switch (DevicePathSubType (TempDevicePath
)) {
2215 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
2219 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
2223 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
2227 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
2230 case MSG_MAC_ADDR_DP
:
2234 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
2238 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
2246 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2249 return BDS_EFI_UNSUPPORT
;
2253 Check whether the Device path in a boot option point to a valid bootable device,
2254 And if CheckMedia is true, check the device is ready to boot now.
2256 @param DevPath the Device path in a boot option
2257 @param CheckMedia if true, check the device is ready to boot now.
2259 @retval TRUE the Device path is valid
2260 @retval FALSE the Device path is invalid .
2265 BdsLibIsValidEFIBootOptDevicePath (
2266 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
2267 IN BOOLEAN CheckMedia
2270 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
2274 Check whether the Device path in a boot option point to a valid bootable device,
2275 And if CheckMedia is true, check the device is ready to boot now.
2276 If Description is not NULL and the device path point to a fixed BlockIo
2277 device, check the description whether conflict with other auto-created
2280 @param DevPath the Device path in a boot option
2281 @param CheckMedia if true, check the device is ready to boot now.
2282 @param Description the description in a boot option
2284 @retval TRUE the Device path is valid
2285 @retval FALSE the Device path is invalid .
2290 BdsLibIsValidEFIBootOptDevicePathExt (
2291 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
2292 IN BOOLEAN CheckMedia
,
2293 IN CHAR16
*Description
2298 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2299 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2300 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
2302 TempDevicePath
= DevPath
;
2303 LastDeviceNode
= DevPath
;
2306 // Check if it's a valid boot option for network boot device.
2307 // Check if there is EfiLoadFileProtocol installed.
2308 // If yes, that means there is a boot option for network.
2310 Status
= gBS
->LocateDevicePath (
2311 &gEfiLoadFileProtocolGuid
,
2315 if (EFI_ERROR (Status
)) {
2317 // Device not present so see if we need to connect it
2319 TempDevicePath
= DevPath
;
2320 BdsLibConnectDevicePath (TempDevicePath
);
2321 Status
= gBS
->LocateDevicePath (
2322 &gEfiLoadFileProtocolGuid
,
2328 if (!EFI_ERROR (Status
)) {
2329 if (!IsDevicePathEnd (TempDevicePath
)) {
2331 // LoadFile protocol is not installed on handle with exactly the same DevPath
2338 // Test if it is ready to boot now
2340 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
2349 // If the boot option point to a file, it is a valid EFI boot option,
2350 // and assume it is ready to boot now
2352 while (!IsDevicePathEnd (TempDevicePath
)) {
2354 // If there is USB Class or USB WWID device path node, treat it as valid EFI
2355 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
2356 // to full device path.
2358 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2359 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2360 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2364 LastDeviceNode
= TempDevicePath
;
2365 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2367 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
2368 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
2373 // Check if it's a valid boot option for internal FV application
2375 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
2377 // If the boot option point to internal FV application, make sure it is valid
2379 TempDevicePath
= DevPath
;
2380 Status
= BdsLibUpdateFvFileDevicePath (
2382 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
2384 if (Status
== EFI_ALREADY_STARTED
) {
2387 if (Status
== EFI_SUCCESS
) {
2388 FreePool (TempDevicePath
);
2395 // If the boot option point to a blockIO device:
2396 // if it is a removable blockIo device, it is valid.
2397 // if it is a fixed blockIo device, check its description confliction.
2399 TempDevicePath
= DevPath
;
2400 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
2401 if (EFI_ERROR (Status
)) {
2403 // Device not present so see if we need to connect it
2405 Status
= BdsLibConnectDevicePath (DevPath
);
2406 if (!EFI_ERROR (Status
)) {
2408 // Try again to get the Block Io protocol after we did the connect
2410 TempDevicePath
= DevPath
;
2411 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
2415 if (!EFI_ERROR (Status
)) {
2416 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
2417 if (!EFI_ERROR (Status
)) {
2420 // Test if it is ready to boot now
2422 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
2431 // 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,
2433 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
2434 if (!EFI_ERROR (Status
)) {
2437 // Test if it is ready to boot now
2439 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
2453 According to a file guild, check a Fv file device path is valid. If it is invalid,
2454 try to return the valid device path.
2455 FV address maybe changes for memory layout adjust from time to time, use this function
2456 could promise the Fv file device path is right.
2458 @param DevicePath on input, the Fv file device path need to check on
2459 output, the updated valid Fv file device path
2460 @param FileGuid the Fv file guild
2462 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
2464 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
2466 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
2468 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
2469 and return the updated device path in DevicePath
2474 BdsLibUpdateFvFileDevicePath (
2475 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
2476 IN EFI_GUID
*FileGuid
2479 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2480 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2482 EFI_GUID
*GuidPoint
;
2484 UINTN FvHandleCount
;
2485 EFI_HANDLE
*FvHandleBuffer
;
2486 EFI_FV_FILETYPE Type
;
2488 EFI_FV_FILE_ATTRIBUTES Attributes
;
2489 UINT32 AuthenticationStatus
;
2491 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2492 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
2493 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
2494 EFI_HANDLE FoundFvHandle
;
2495 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2497 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2498 return EFI_INVALID_PARAMETER
;
2500 if (FileGuid
== NULL
) {
2501 return EFI_INVALID_PARAMETER
;
2505 // Check whether the device path point to the default the input Fv file
2507 TempDevicePath
= *DevicePath
;
2508 LastDeviceNode
= TempDevicePath
;
2509 while (!IsDevicePathEnd (TempDevicePath
)) {
2510 LastDeviceNode
= TempDevicePath
;
2511 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2513 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2514 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2516 if (GuidPoint
== NULL
) {
2518 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2520 return EFI_UNSUPPORTED
;
2522 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2524 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2526 return EFI_UNSUPPORTED
;
2530 // Check whether the input Fv file device path is valid
2532 TempDevicePath
= *DevicePath
;
2533 FoundFvHandle
= NULL
;
2534 Status
= gBS
->LocateDevicePath (
2535 &gEfiFirmwareVolume2ProtocolGuid
,
2539 if (!EFI_ERROR (Status
)) {
2540 Status
= gBS
->HandleProtocol (
2542 &gEfiFirmwareVolume2ProtocolGuid
,
2545 if (!EFI_ERROR (Status
)) {
2547 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2549 Status
= Fv
->ReadFile (
2556 &AuthenticationStatus
2558 if (!EFI_ERROR (Status
)) {
2559 return EFI_ALREADY_STARTED
;
2565 // Look for the input wanted FV file in current FV
2566 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2569 FoundFvHandle
= NULL
;
2570 Status
= gBS
->HandleProtocol (
2572 &gEfiLoadedImageProtocolGuid
,
2573 (VOID
**) &LoadedImage
2575 if (!EFI_ERROR (Status
)) {
2576 Status
= gBS
->HandleProtocol (
2577 LoadedImage
->DeviceHandle
,
2578 &gEfiFirmwareVolume2ProtocolGuid
,
2581 if (!EFI_ERROR (Status
)) {
2582 Status
= Fv
->ReadFile (
2589 &AuthenticationStatus
2591 if (!EFI_ERROR (Status
)) {
2593 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2598 // Second, if fail to find, try to enumerate all FV
2601 FvHandleBuffer
= NULL
;
2602 gBS
->LocateHandleBuffer (
2604 &gEfiFirmwareVolume2ProtocolGuid
,
2609 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2610 gBS
->HandleProtocol (
2611 FvHandleBuffer
[Index
],
2612 &gEfiFirmwareVolume2ProtocolGuid
,
2616 Status
= Fv
->ReadFile (
2623 &AuthenticationStatus
2625 if (EFI_ERROR (Status
)) {
2627 // Skip if input Fv file not in the FV
2632 FoundFvHandle
= FvHandleBuffer
[Index
];
2636 if (FvHandleBuffer
!= NULL
) {
2637 FreePool (FvHandleBuffer
);
2643 // Build the shell device path
2645 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2646 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2647 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2648 *DevicePath
= NewDevicePath
;
2651 return EFI_NOT_FOUND
;