2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2012, 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 The constructor function register UNI strings into imageHandle.
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
26 @param ImageHandle The firmware allocated handle for the EFI image.
27 @param SystemTable A pointer to the EFI System Table.
29 @retval EFI_SUCCESS The constructor successfully added string package.
30 @retval Other value The constructor can't add string package.
35 GenericBdsLibConstructor (
36 IN EFI_HANDLE ImageHandle
,
37 IN EFI_SYSTEM_TABLE
*SystemTable
41 gBdsLibStringPackHandle
= HiiAddPackages (
42 &gBdsLibStringPackageGuid
,
48 ASSERT (gBdsLibStringPackHandle
!= NULL
);
56 Boot the legacy system with the boot option
58 @param Option The legacy boot option which have BBS device path
60 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
62 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
67 IN BDS_COMMON_OPTION
*Option
71 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
73 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
74 if (EFI_ERROR (Status
)) {
76 // If no LegacyBios protocol we do not support legacy boot
78 return EFI_UNSUPPORTED
;
81 // Notes: if we separate the int 19, then we don't need to refresh BBS
83 BdsRefreshBbsTableForBoot (Option
);
86 // Write boot to OS performance data for legacy boot.
89 WriteBootToOsPerformanceData ();
92 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
93 return LegacyBios
->LegacyBoot (
95 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
96 Option
->LoadOptionsSize
,
102 Internal function to check if the input boot option is a valid EFI NV Boot####.
104 @param OptionToCheck Boot option to be checked.
106 @retval TRUE This boot option matches a valid EFI NV Boot####.
107 @retval FALSE If not.
111 IsBootOptionValidNVVarialbe (
112 IN BDS_COMMON_OPTION
*OptionToCheck
116 BDS_COMMON_OPTION
*BootOption
;
118 CHAR16 OptionName
[20];
122 InitializeListHead (&TempList
);
123 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
125 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
126 if (BootOption
== NULL
) {
131 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
132 // valid EFI NV Boot####.
134 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
135 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
140 FreePool (BootOption
);
146 Check whether a USB device match the specified USB Class device path. This
147 function follows "Load Option Processing" behavior in UEFI specification.
149 @param UsbIo USB I/O protocol associated with the USB device.
150 @param UsbClass The USB Class device path to match.
152 @retval TRUE The USB device match the USB Class device path.
153 @retval FALSE The USB device does not match the USB Class device path.
158 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
159 IN USB_CLASS_DEVICE_PATH
*UsbClass
163 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
164 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
166 UINT8 DeviceSubClass
;
167 UINT8 DeviceProtocol
;
169 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
170 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
175 // Check Vendor Id and Product Id.
177 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
178 if (EFI_ERROR (Status
)) {
182 if ((UsbClass
->VendorId
!= 0xffff) &&
183 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
187 if ((UsbClass
->ProductId
!= 0xffff) &&
188 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
192 DeviceClass
= DevDesc
.DeviceClass
;
193 DeviceSubClass
= DevDesc
.DeviceSubClass
;
194 DeviceProtocol
= DevDesc
.DeviceProtocol
;
195 if (DeviceClass
== 0) {
197 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
198 // Protocol in Interface Descriptor instead.
200 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
201 if (EFI_ERROR (Status
)) {
205 DeviceClass
= IfDesc
.InterfaceClass
;
206 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
207 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
211 // Check Class, SubClass and Protocol.
213 if ((UsbClass
->DeviceClass
!= 0xff) &&
214 (UsbClass
->DeviceClass
!= DeviceClass
)) {
218 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
219 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
223 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
224 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
232 Check whether a USB device match the specified USB WWID device path. This
233 function follows "Load Option Processing" behavior in UEFI specification.
235 @param UsbIo USB I/O protocol associated with the USB device.
236 @param UsbWwid The USB WWID device path to match.
238 @retval TRUE The USB device match the USB WWID device path.
239 @retval FALSE The USB device does not match the USB WWID device path.
244 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
245 IN USB_WWID_DEVICE_PATH
*UsbWwid
249 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
250 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
256 CHAR16
*SerialNumberStr
;
259 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
260 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
265 // Check Vendor Id and Product Id.
267 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
268 if (EFI_ERROR (Status
)) {
271 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
272 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
277 // Check Interface Number.
279 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
280 if (EFI_ERROR (Status
)) {
283 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
288 // Check Serial Number.
290 if (DevDesc
.StrSerialNumber
== 0) {
295 // Get all supported languages.
299 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
300 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
305 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
307 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
308 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
309 if (CompareStr
[CompareLen
- 1] == L
'\0') {
314 // Compare serial number in each supported language.
316 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
317 SerialNumberStr
= NULL
;
318 Status
= UsbIo
->UsbGetStringDescriptor (
321 DevDesc
.StrSerialNumber
,
324 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
328 Length
= StrLen (SerialNumberStr
);
329 if ((Length
>= CompareLen
) &&
330 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
331 FreePool (SerialNumberStr
);
335 FreePool (SerialNumberStr
);
342 Find a USB device path which match the specified short-form device path start
343 with USB Class or USB WWID device path and load the boot file then return the
344 image handle. If ParentDevicePath is NULL, this function will search in all USB
345 devices of the platform. If ParentDevicePath is not NULL,this function will only
346 search in its child devices.
348 @param ParentDevicePath The device path of the parent.
349 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
351 @return The image Handle if find load file from specified short-form device path
352 or NULL if not found.
357 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
358 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
362 UINTN UsbIoHandleCount
;
363 EFI_HANDLE
*UsbIoHandleBuffer
;
364 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
365 EFI_USB_IO_PROTOCOL
*UsbIo
;
369 EFI_HANDLE ImageHandle
;
371 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
372 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
374 FullDevicePath
= NULL
;
378 // Get all UsbIo Handles.
380 UsbIoHandleCount
= 0;
381 UsbIoHandleBuffer
= NULL
;
382 Status
= gBS
->LocateHandleBuffer (
384 &gEfiUsbIoProtocolGuid
,
389 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
393 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
394 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
396 // Get the Usb IO interface.
398 Status
= gBS
->HandleProtocol(
399 UsbIoHandleBuffer
[Index
],
400 &gEfiUsbIoProtocolGuid
,
403 if (EFI_ERROR (Status
)) {
407 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
408 if (UsbIoDevicePath
== NULL
) {
412 if (ParentDevicePath
!= NULL
) {
414 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
416 Size
= GetDevicePathSize (UsbIoDevicePath
);
417 if ((Size
< ParentSize
) ||
418 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
423 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
424 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
426 // Try to find if there is the boot file in this DevicePath
428 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
429 if (!IsDevicePathEnd (NextDevicePath
)) {
430 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
432 // Connect the full device path, so that Simple File System protocol
433 // could be installed for this USB device.
435 BdsLibConnectDevicePath (FullDevicePath
);
436 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
437 Status
= gBS
->LoadImage (
445 FreePool (FullDevicePath
);
447 FullDevicePath
= UsbIoDevicePath
;
448 Status
= EFI_NOT_FOUND
;
452 // If we didn't find an image directly, we need to try as if it is a removable device boot option
453 // and load the image according to the default boot behavior for removable device.
455 if (EFI_ERROR (Status
)) {
457 // check if there is a bootable removable media could be found in this device path ,
458 // and get the bootable media handle
460 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
461 if (Handle
== NULL
) {
465 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
466 // machinename is ia32, ia64, x64, ...
468 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
469 if (FullDevicePath
!= NULL
) {
470 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
471 Status
= gBS
->LoadImage (
479 if (EFI_ERROR (Status
)) {
481 // The DevicePath failed, and it's not a valid
482 // removable media device.
494 FreePool (UsbIoHandleBuffer
);
499 Expand USB Class or USB WWID device path node to be full device path of a USB
500 device in platform then load the boot file on this full device path and return the
503 This function support following 4 cases:
504 1) Boot Option device path starts with a USB Class or USB WWID device path,
505 and there is no Media FilePath device path in the end.
506 In this case, it will follow Removable Media Boot Behavior.
507 2) Boot Option device path starts with a USB Class or USB WWID device path,
508 and ended with Media FilePath device path.
509 3) Boot Option device path starts with a full device path to a USB Host Controller,
510 contains a USB Class or USB WWID device path node, while not ended with Media
511 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
512 4) Boot Option device path starts with a full device path to a USB Host Controller,
513 contains a USB Class or USB WWID device path node, and ended with Media
514 FilePath device path.
516 @param DevicePath The Boot Option device path.
518 @return The image handle of boot file, or NULL if there is no boot file found in
519 the specified USB Class or USB WWID device path.
523 BdsExpandUsbShortFormDevicePath (
524 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
527 EFI_HANDLE
*ImageHandle
;
528 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
529 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
532 // Search for USB Class or USB WWID device path node.
534 ShortFormDevicePath
= NULL
;
536 TempDevicePath
= DevicePath
;
537 while (!IsDevicePathEnd (TempDevicePath
)) {
538 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
539 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
540 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
541 ShortFormDevicePath
= TempDevicePath
;
544 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
547 if (ShortFormDevicePath
== NULL
) {
549 // No USB Class or USB WWID device path node found, do nothing.
554 if (ShortFormDevicePath
== DevicePath
) {
556 // Boot Option device path starts with USB Class or USB WWID device path.
558 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
559 if (ImageHandle
== NULL
) {
561 // Failed to find a match in existing devices, connect the short form USB
562 // device path and try again.
564 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
565 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
569 // Boot Option device path contains USB Class or USB WWID device path node.
573 // Prepare the parent device path for search.
575 TempDevicePath
= DuplicateDevicePath (DevicePath
);
576 ASSERT (TempDevicePath
!= NULL
);
577 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
580 // The USB Host Controller device path is already in Boot Option device path
581 // and USB Bus driver already support RemainingDevicePath starts with USB
582 // Class or USB WWID device path, so just search in existing USB devices and
583 // doesn't perform ConnectController here.
585 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
586 FreePool (TempDevicePath
);
593 Process the boot option follow the UEFI specification and
594 special treat the legacy boot option with BBS_DEVICE_PATH.
596 @param Option The boot option need to be processed
597 @param DevicePath The device path which describe where to load the
598 boot image or the legacy BBS device path to boot
600 @param ExitDataSize The size of exit data.
601 @param ExitData Data returned when Boot image failed.
603 @retval EFI_SUCCESS Boot from the input boot option successfully.
604 @retval EFI_NOT_FOUND If the Device Path is not found in the system
609 BdsLibBootViaBootOption (
610 IN BDS_COMMON_OPTION
*Option
,
611 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
612 OUT UINTN
*ExitDataSize
,
613 OUT CHAR16
**ExitData OPTIONAL
617 EFI_STATUS StatusLogo
;
619 EFI_HANDLE ImageHandle
;
620 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
621 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
622 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
623 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
624 LIST_ENTRY TempBootLists
;
625 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
628 // Record the performance data for End of BDS
630 PERF_END(NULL
, "BDS", NULL
, 0);
636 // Notes: this code can be remove after the s3 script table
637 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
638 // EVT_SIGNAL_LEGACY_BOOT
640 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
641 if (!EFI_ERROR (Status
)) {
642 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
645 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
648 WorkingDevicePath
= NULL
;
649 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
650 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
651 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
652 (HARDDRIVE_DEVICE_PATH
*)DevicePath
654 if (WorkingDevicePath
!= NULL
) {
655 DevicePath
= WorkingDevicePath
;
662 if (IsBootOptionValidNVVarialbe (Option
)) {
664 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
665 // In this case, "BootCurrent" is not created.
666 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
670 &gEfiGlobalVariableGuid
,
671 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
678 // Signal the EVT_SIGNAL_READY_TO_BOOT event
680 EfiSignalEventReadyToBoot();
683 // Expand USB Class or USB WWID device path node to be full device path of a USB
684 // device in platform then load the boot file on this full device path and get the
687 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
690 // Adjust the different type memory page number just before booting
691 // and save the updated info into the variable for next boot to use
693 BdsSetMemoryTypeInformationVariable ();
696 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
697 // Here get the ImageHandle for the non USB class or WWID device path.
699 if (ImageHandle
== NULL
) {
700 ASSERT (Option
->DevicePath
!= NULL
);
701 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
702 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
705 // Check to see if we should legacy BOOT. If yes then do the legacy boot
707 return BdsLibDoLegacyBoot (Option
);
711 // If the boot option point to Internal FV shell, make sure it is valid
713 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
714 if (!EFI_ERROR(Status
)) {
715 if (Option
->DevicePath
!= NULL
) {
716 FreePool(Option
->DevicePath
);
718 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
719 ASSERT(Option
->DevicePath
!= NULL
);
720 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
722 // Update the shell boot option
724 InitializeListHead (&TempBootLists
);
725 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
728 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
730 FreePool (DevicePath
);
731 DevicePath
= Option
->DevicePath
;
736 if (Option
->Description
== NULL
) {
737 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
739 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
745 // Report status code for OS Loader LoadImage.
747 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
748 Status
= gBS
->LoadImage (
758 // If we didn't find an image directly, we need to try as if it is a removable device boot option
759 // and load the image according to the default boot behavior for removable device.
761 if (EFI_ERROR (Status
)) {
763 // check if there is a bootable removable media could be found in this device path ,
764 // and get the bootable media handle
766 Handle
= BdsLibGetBootableHandle(DevicePath
);
767 if (Handle
== NULL
) {
771 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
772 // machinename is ia32, ia64, x64, ...
774 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
775 if (FilePath
!= NULL
) {
776 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
777 Status
= gBS
->LoadImage (
785 if (EFI_ERROR (Status
)) {
787 // The DevicePath failed, and it's not a valid
788 // removable media device.
795 if (EFI_ERROR (Status
)) {
797 // It there is any error from the Boot attempt exit now.
803 // Provide the image with it's load options
805 if (ImageHandle
== NULL
) {
808 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
809 ASSERT_EFI_ERROR (Status
);
811 if (Option
->LoadOptionsSize
!= 0) {
812 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
813 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
816 // Before calling the image, enable the Watchdog Timer for
817 // the 5 Minute period
819 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
822 // Write boot to OS performance data for UEFI boot
825 WriteBootToOsPerformanceData ();
829 // Report status code for OS Loader StartImage.
831 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
833 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
834 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
837 // Clear the Watchdog Timer after the image returns
839 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
843 // Set Logo status invalid after trying one boot option
846 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
847 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
848 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
852 // Clear Boot Current
856 &gEfiGlobalVariableGuid
,
857 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
867 Expand a device path that starts with a hard drive media device path node to be a
868 full device path that includes the full hardware path to the device. We need
869 to do this so it can be booted. As an optimization the front match (the part point
870 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
871 so a connect all is not required on every boot. All successful history device path
872 which point to partition node (the front part) will be saved.
874 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
875 drive media device path.
876 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
880 EFI_DEVICE_PATH_PROTOCOL
*
882 BdsExpandPartitionPartialDevicePathToFull (
883 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
887 UINTN BlockIoHandleCount
;
888 EFI_HANDLE
*BlockIoBuffer
;
889 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
890 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
891 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
894 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
895 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
896 UINTN CachedDevicePathSize
;
899 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
902 FullDevicePath
= NULL
;
904 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
905 // If exist, search the front path which point to partition node in the variable instants.
906 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
908 CachedDevicePath
= BdsLibGetVariableAndSize (
909 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
910 &gHdBootDevicePathVariablGuid
,
911 &CachedDevicePathSize
914 if (CachedDevicePath
!= NULL
) {
915 TempNewDevicePath
= CachedDevicePath
;
920 // Check every instance of the variable
921 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
922 // partial partition boot option. Second, check whether the instance could be connected.
924 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
925 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
927 // Connect the device path instance, the device path point to hard drive media device path node
928 // e.g. ACPI() /PCI()/ATA()/Partition()
930 Status
= BdsLibConnectDevicePath (Instance
);
931 if (!EFI_ERROR (Status
)) {
937 // Come here means the first instance is not matched
941 } while (TempNewDevicePath
!= NULL
);
945 // Find the matched device path.
946 // Append the file path information from the boot option and return the fully expanded device path.
948 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
949 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
952 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
956 // First delete the matched instance.
958 TempNewDevicePath
= CachedDevicePath
;
959 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
960 FreePool (TempNewDevicePath
);
963 // Second, append the remaining path after the matched instance
965 TempNewDevicePath
= CachedDevicePath
;
966 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
967 FreePool (TempNewDevicePath
);
969 // Save the matching Device Path so we don't need to do a connect all next time
971 Status
= gRT
->SetVariable (
972 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
973 &gHdBootDevicePathVariablGuid
,
974 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
975 GetDevicePathSize (CachedDevicePath
),
981 FreePool (CachedDevicePath
);
982 return FullDevicePath
;
987 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
988 // to search all devices in the system for a matched partition
990 BdsLibConnectAllDriversToAllControllers ();
991 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
992 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
994 // If there was an error or there are no device handles that support
995 // the BLOCK_IO Protocol, then return.
1000 // Loop through all the device handles that support the BLOCK_IO Protocol
1002 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
1004 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
1005 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
1009 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
1011 // Find the matched partition device path
1013 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
1014 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
1017 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
1019 if (CachedDevicePath
!= NULL
) {
1021 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
1023 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
1024 TempNewDevicePath
= CachedDevicePath
;
1025 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
1026 FreePool(TempNewDevicePath
);
1028 TempNewDevicePath
= CachedDevicePath
;
1029 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
1030 if (TempNewDevicePath
!= NULL
) {
1031 FreePool(TempNewDevicePath
);
1034 TempNewDevicePath
= CachedDevicePath
;
1035 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
1036 FreePool(TempNewDevicePath
);
1039 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
1040 // If the user try to boot many OS in different HDs or partitions, in theory,
1041 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
1044 ASSERT (CachedDevicePath
!= NULL
);
1045 TempNewDevicePath
= CachedDevicePath
;
1046 while (!IsDevicePathEnd (TempNewDevicePath
)) {
1047 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
1049 // Parse one instance
1051 while (!IsDevicePathEndType (TempNewDevicePath
)) {
1052 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
1056 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
1058 if (InstanceNum
>= 12) {
1059 SetDevicePathEndNode (TempNewDevicePath
);
1064 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
1068 // Save the matching Device Path so we don't need to do a connect all next time
1070 Status
= gRT
->SetVariable (
1071 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
1072 &gHdBootDevicePathVariablGuid
,
1073 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1074 GetDevicePathSize (CachedDevicePath
),
1082 if (CachedDevicePath
!= NULL
) {
1083 FreePool (CachedDevicePath
);
1085 if (BlockIoBuffer
!= NULL
) {
1086 FreePool (BlockIoBuffer
);
1088 return FullDevicePath
;
1092 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
1093 instances, has the same partition node with HardDriveDevicePath device path
1095 @param BlockIoDevicePath Multi device path instances which need to check
1096 @param HardDriveDevicePath A device path which starts with a hard drive media
1099 @retval TRUE There is a matched device path instance.
1100 @retval FALSE There is no matched device path instance.
1105 MatchPartitionDevicePathNode (
1106 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
1107 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
1110 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
1111 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1113 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
1115 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
1120 // Make PreviousDevicePath == the device path node before the end node
1122 DevicePath
= BlockIoDevicePath
;
1123 BlockIoHdDevicePathNode
= NULL
;
1126 // find the partition device path node
1128 while (!IsDevicePathEnd (DevicePath
)) {
1129 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
1130 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
1132 BlockIoHdDevicePathNode
= DevicePath
;
1136 DevicePath
= NextDevicePathNode (DevicePath
);
1139 if (BlockIoHdDevicePathNode
== NULL
) {
1143 // See if the harddrive device path in blockio matches the orig Hard Drive Node
1145 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
1149 // Check for the match
1151 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
1152 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
1153 switch (TmpHdPath
->SignatureType
) {
1154 case SIGNATURE_TYPE_GUID
:
1155 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
1157 case SIGNATURE_TYPE_MBR
:
1158 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
1170 Delete the boot option associated with the handle passed in.
1172 @param Handle The handle which present the device path to create
1175 @retval EFI_SUCCESS Delete the boot option success
1176 @retval EFI_NOT_FOUND If the Device Path is not found in the system
1177 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1178 @retval Other Error return value from SetVariable()
1182 BdsLibDeleteOptionFromHandle (
1183 IN EFI_HANDLE Handle
1187 UINT8
*BootOptionVar
;
1188 UINTN BootOrderSize
;
1189 UINTN BootOptionSize
;
1192 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
1193 UINTN DevicePathSize
;
1194 UINTN OptionDevicePathSize
;
1195 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1196 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
1199 Status
= EFI_SUCCESS
;
1204 // Check "BootOrder" variable, if no, means there is no any boot order.
1206 BootOrder
= BdsLibGetVariableAndSize (
1208 &gEfiGlobalVariableGuid
,
1211 if (BootOrder
== NULL
) {
1212 return EFI_NOT_FOUND
;
1216 // Convert device handle to device path protocol instance
1218 DevicePath
= DevicePathFromHandle (Handle
);
1219 if (DevicePath
== NULL
) {
1220 return EFI_NOT_FOUND
;
1222 DevicePathSize
= GetDevicePathSize (DevicePath
);
1225 // Loop all boot order variable and find the matching device path
1228 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
1229 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1230 BootOptionVar
= BdsLibGetVariableAndSize (
1232 &gEfiGlobalVariableGuid
,
1236 if (BootOptionVar
== NULL
) {
1237 FreePool (BootOrder
);
1238 return EFI_OUT_OF_RESOURCES
;
1241 TempPtr
= BootOptionVar
;
1242 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
1243 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
1244 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
1245 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
1248 // Check whether the device path match
1250 if ((OptionDevicePathSize
== DevicePathSize
) &&
1251 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
1252 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
1253 FreePool (BootOptionVar
);
1257 FreePool (BootOptionVar
);
1262 // Adjust number of boot option for "BootOrder" variable.
1264 Status
= gRT
->SetVariable (
1266 &gEfiGlobalVariableGuid
,
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1272 FreePool (BootOrder
);
1279 Delete all invalid EFI boot options.
1281 @retval EFI_SUCCESS Delete all invalid boot option success
1282 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
1283 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1284 @retval Other Error return value from SetVariable()
1288 BdsDeleteAllInvalidEfiBootOption (
1293 UINT8
*BootOptionVar
;
1294 UINTN BootOrderSize
;
1295 UINTN BootOptionSize
;
1299 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
1300 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
1302 CHAR16
*Description
;
1304 Status
= EFI_SUCCESS
;
1309 // Check "BootOrder" variable firstly, this variable hold the number of boot options
1311 BootOrder
= BdsLibGetVariableAndSize (
1313 &gEfiGlobalVariableGuid
,
1316 if (NULL
== BootOrder
) {
1317 return EFI_NOT_FOUND
;
1321 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
1322 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1323 BootOptionVar
= BdsLibGetVariableAndSize (
1325 &gEfiGlobalVariableGuid
,
1328 if (NULL
== BootOptionVar
) {
1329 FreePool (BootOrder
);
1330 return EFI_OUT_OF_RESOURCES
;
1333 TempPtr
= BootOptionVar
;
1334 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
1335 Description
= (CHAR16
*) TempPtr
;
1336 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
1337 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
1340 // Skip legacy boot option (BBS boot device)
1342 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
1343 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
1344 FreePool (BootOptionVar
);
1349 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
1351 // Delete this invalid boot option "Boot####"
1353 Status
= gRT
->SetVariable (
1355 &gEfiGlobalVariableGuid
,
1356 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1361 // Mark this boot option in boot order as deleted
1363 BootOrder
[Index
] = 0xffff;
1366 FreePool (BootOptionVar
);
1371 // Adjust boot order array
1374 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
1375 if (BootOrder
[Index
] != 0xffff) {
1376 BootOrder
[Index2
] = BootOrder
[Index
];
1380 Status
= gRT
->SetVariable (
1382 &gEfiGlobalVariableGuid
,
1383 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1384 Index2
* sizeof (UINT16
),
1388 FreePool (BootOrder
);
1395 For EFI boot option, BDS separate them as six types:
1396 1. Network - The boot option points to the SimpleNetworkProtocol device.
1397 Bds will try to automatically create this type boot option when enumerate.
1398 2. Shell - The boot option points to internal flash shell.
1399 Bds will try to automatically create this type boot option when enumerate.
1400 3. Removable BlockIo - The boot option only points to the removable media
1401 device, like USB flash disk, DVD, Floppy etc.
1402 These device should contain a *removable* blockIo
1403 protocol in their device handle.
1404 Bds will try to automatically create this type boot option
1406 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
1408 These device should contain a *fixed* blockIo
1409 protocol in their device handle.
1410 BDS will skip fixed blockIo devices, and NOT
1411 automatically create boot option for them. But BDS
1412 will help to delete those fixed blockIo boot option,
1413 whose description rule conflict with other auto-created
1415 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
1416 has SimpleFileSystem Protocol, but has no blockio
1417 protocol. These devices do not offer blockIo
1418 protocol, but BDS still can get the
1419 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
1421 6. File - The boot option points to a file. These boot options are usually
1422 created by user manually or OS loader. BDS will not delete or modify
1425 This function will enumerate all possible boot device in the system, and
1426 automatically create boot options for Network, Shell, Removable BlockIo,
1427 and Non-BlockIo Simplefile devices.
1428 It will only execute once of every boot.
1430 @param BdsBootOptionList The header of the link list which indexed all
1431 current boot options
1433 @retval EFI_SUCCESS Finished all the boot device enumerate and create
1434 the boot option base on that boot device
1436 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
1440 BdsLibEnumerateAllBootOption (
1441 IN OUT LIST_ENTRY
*BdsBootOptionList
1445 UINT16 FloppyNumber
;
1446 UINT16 HarddriveNumber
;
1451 UINT16 NonBlockNumber
;
1452 UINTN NumberBlockIoHandles
;
1453 EFI_HANDLE
*BlockIoHandles
;
1454 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
1455 BOOLEAN Removable
[2];
1456 UINTN RemovableIndex
;
1458 UINTN NumOfLoadFileHandles
;
1459 EFI_HANDLE
*LoadFileHandles
;
1460 UINTN FvHandleCount
;
1461 EFI_HANDLE
*FvHandleBuffer
;
1462 EFI_FV_FILETYPE Type
;
1464 EFI_FV_FILE_ATTRIBUTES Attributes
;
1465 UINT32 AuthenticationStatus
;
1466 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1467 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1468 UINTN DevicePathType
;
1470 EFI_HANDLE
*FileSystemHandles
;
1471 UINTN NumberFileSystemHandles
;
1473 EFI_IMAGE_DOS_HEADER DosHeader
;
1476 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1477 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1480 HarddriveNumber
= 0;
1487 ZeroMem (Buffer
, sizeof (Buffer
));
1490 // If the boot device enumerate happened, just get the boot
1491 // device from the boot order variable
1493 if (mEnumBootDevice
) {
1494 LastLang
= GetVariable (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
);
1495 PlatLang
= GetEfiGlobalVariable (L
"PlatformLang");
1496 ASSERT (PlatLang
!= NULL
);
1497 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
1498 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1499 FreePool (LastLang
);
1500 FreePool (PlatLang
);
1503 Status
= gRT
->SetVariable (
1504 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
1506 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1507 AsciiStrSize (PlatLang
),
1510 ASSERT_EFI_ERROR (Status
);
1512 if (LastLang
!= NULL
) {
1513 FreePool (LastLang
);
1515 FreePool (PlatLang
);
1520 // Notes: this dirty code is to get the legacy boot option from the
1521 // BBS table and create to variable as the EFI boot option, it should
1522 // be removed after the CSM can provide legacy boot option directly
1524 REFRESH_LEGACY_BOOT_OPTIONS
;
1527 // Delete invalid boot option
1529 BdsDeleteAllInvalidEfiBootOption ();
1532 // Parse removable media followed by fixed media.
1533 // The Removable[] array is used by the for-loop below to create removable media boot options
1534 // at first, and then to create fixed media boot options.
1536 Removable
[0] = FALSE
;
1537 Removable
[1] = TRUE
;
1539 gBS
->LocateHandleBuffer (
1541 &gEfiBlockIoProtocolGuid
,
1543 &NumberBlockIoHandles
,
1547 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
1548 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
1549 Status
= gBS
->HandleProtocol (
1550 BlockIoHandles
[Index
],
1551 &gEfiBlockIoProtocolGuid
,
1555 // skip the fixed block io then the removable block io
1557 if (EFI_ERROR (Status
) || (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
])) {
1560 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
1561 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
1563 switch (DevicePathType
) {
1564 case BDS_EFI_ACPI_FLOPPY_BOOT
:
1565 if (FloppyNumber
!= 0) {
1566 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
1568 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
1570 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1575 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
1577 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
1578 case BDS_EFI_MESSAGE_SATA_BOOT
:
1579 if (BlkIo
->Media
->RemovableMedia
) {
1580 if (CdromNumber
!= 0) {
1581 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
1583 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
1587 if (HarddriveNumber
!= 0) {
1588 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
1590 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
1594 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
1595 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1598 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
1599 if (UsbNumber
!= 0) {
1600 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
1602 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
1604 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1608 case BDS_EFI_MESSAGE_SCSI_BOOT
:
1609 if (ScsiNumber
!= 0) {
1610 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
1612 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
1614 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1618 case BDS_EFI_MESSAGE_MISC_BOOT
:
1619 if (MiscNumber
!= 0) {
1620 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
1622 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
1624 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
1634 if (NumberBlockIoHandles
!= 0) {
1635 FreePool (BlockIoHandles
);
1639 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1642 gBS
->LocateHandleBuffer (
1644 &gEfiSimpleFileSystemProtocolGuid
,
1646 &NumberFileSystemHandles
,
1649 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
1650 Status
= gBS
->HandleProtocol (
1651 FileSystemHandles
[Index
],
1652 &gEfiBlockIoProtocolGuid
,
1655 if (!EFI_ERROR (Status
)) {
1657 // Skip if the file system handle supports a BlkIo protocol,
1663 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1664 // machinename is ia32, ia64, x64, ...
1666 Hdr
.Union
= &HdrData
;
1668 Status
= BdsLibGetImageHeader (
1669 FileSystemHandles
[Index
],
1670 EFI_REMOVABLE_MEDIA_FILE_NAME
,
1674 if (!EFI_ERROR (Status
) &&
1675 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
1676 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
1682 // No such file or the file is not a EFI application, delete this boot option
1684 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
1686 if (NonBlockNumber
!= 0) {
1687 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
1689 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
1691 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
1696 if (NumberFileSystemHandles
!= 0) {
1697 FreePool (FileSystemHandles
);
1701 // Parse Network Boot Device
1703 NumOfLoadFileHandles
= 0;
1705 // Search Load File protocol for PXE boot option.
1707 gBS
->LocateHandleBuffer (
1709 &gEfiLoadFileProtocolGuid
,
1711 &NumOfLoadFileHandles
,
1715 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
1717 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
1719 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
1721 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
1724 if (NumOfLoadFileHandles
!= 0) {
1725 FreePool (LoadFileHandles
);
1729 // Check if we have on flash shell
1731 gBS
->LocateHandleBuffer (
1733 &gEfiFirmwareVolume2ProtocolGuid
,
1738 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1739 gBS
->HandleProtocol (
1740 FvHandleBuffer
[Index
],
1741 &gEfiFirmwareVolume2ProtocolGuid
,
1745 Status
= Fv
->ReadFile (
1747 PcdGetPtr(PcdShellFile
),
1752 &AuthenticationStatus
1754 if (EFI_ERROR (Status
)) {
1756 // Skip if no shell file in the FV
1761 // Build the shell boot option
1763 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
1766 if (FvHandleCount
!= 0) {
1767 FreePool (FvHandleBuffer
);
1770 // Make sure every boot only have one time
1771 // boot device enumerate
1773 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
1774 mEnumBootDevice
= TRUE
;
1780 Build the boot option with the handle parsed in
1782 @param Handle The handle which present the device path to create
1784 @param BdsBootOptionList The header of the link list which indexed all
1785 current boot options
1786 @param String The description of the boot option.
1791 BdsLibBuildOptionFromHandle (
1792 IN EFI_HANDLE Handle
,
1793 IN LIST_ENTRY
*BdsBootOptionList
,
1797 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1799 DevicePath
= DevicePathFromHandle (Handle
);
1802 // Create and register new boot option
1804 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
1809 Build the on flash shell boot option with the handle parsed in.
1811 @param Handle The handle which present the device path to create
1812 on flash shell boot option
1813 @param BdsBootOptionList The header of the link list which indexed all
1814 current boot options
1819 BdsLibBuildOptionFromShell (
1820 IN EFI_HANDLE Handle
,
1821 IN OUT LIST_ENTRY
*BdsBootOptionList
1824 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1825 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
1827 DevicePath
= DevicePathFromHandle (Handle
);
1830 // Build the shell device path
1832 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
1834 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
1837 // Create and register the shell boot option
1839 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
1844 Boot from the UEFI spec defined "BootNext" variable.
1856 BDS_COMMON_OPTION
*BootOption
;
1857 LIST_ENTRY TempList
;
1862 // Init the boot option name buffer and temp link list
1864 InitializeListHead (&TempList
);
1865 ZeroMem (Buffer
, sizeof (Buffer
));
1867 BootNext
= BdsLibGetVariableAndSize (
1869 &gEfiGlobalVariableGuid
,
1874 // Clear the boot next variable first
1876 if (BootNext
!= NULL
) {
1879 &gEfiGlobalVariableGuid
,
1880 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1886 // Start to build the boot option and try to boot
1888 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
1889 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
1890 ASSERT (BootOption
!= NULL
);
1891 BdsLibConnectDevicePath (BootOption
->DevicePath
);
1892 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
1898 Return the bootable media handle.
1899 First, check the device is connected
1900 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1901 Third, detect the the default boot file in the Media, and return the removable Media handle.
1903 @param DevicePath Device Path to a bootable device
1905 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1910 BdsLibGetBootableHandle (
1911 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1916 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
1917 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
1919 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1921 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1924 EFI_HANDLE ReturnHandle
;
1925 EFI_HANDLE
*SimpleFileSystemHandles
;
1927 UINTN NumberSimpleFileSystemHandles
;
1929 EFI_IMAGE_DOS_HEADER DosHeader
;
1930 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
1931 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1933 UpdatedDevicePath
= DevicePath
;
1936 // Enter to critical section to protect the acquired BlockIo instance
1937 // from getting released due to the USB mass storage hotplug event
1939 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1942 // Check whether the device is connected
1944 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1945 if (EFI_ERROR (Status
)) {
1947 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1949 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1950 if (EFI_ERROR (Status
)) {
1952 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1954 UpdatedDevicePath
= DevicePath
;
1955 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
1956 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1960 // For removable device boot option, its contained device path only point to the removable device handle,
1961 // should make sure all its children handles (its child partion or media handles) are created and connected.
1963 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1965 // Get BlockIo protocol and check removable attribute
1967 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
1968 ASSERT_EFI_ERROR (Status
);
1971 // Issue a dummy read to the device to check for media change.
1972 // When the removable media is changed, any Block IO read/write will
1973 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1974 // returned. After the Block IO protocol is reinstalled, subsequent
1975 // Block IO read/write will success.
1977 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1978 if (Buffer
!= NULL
) {
1979 BlockIo
->ReadBlocks (
1981 BlockIo
->Media
->MediaId
,
1983 BlockIo
->Media
->BlockSize
,
1991 // Detect the the default boot file from removable Media
1995 // 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
1996 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1998 DupDevicePath
= DuplicateDevicePath (DevicePath
);
1999 ASSERT (DupDevicePath
!= NULL
);
2001 UpdatedDevicePath
= DupDevicePath
;
2002 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2004 // 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
2005 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
2007 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2008 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
2010 // Remove the usb node, let the device path only point to PCI node
2012 SetDevicePathEndNode (UpdatedDevicePath
);
2013 UpdatedDevicePath
= DupDevicePath
;
2015 UpdatedDevicePath
= DevicePath
;
2019 // Get the device path size of boot option
2021 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
2022 ReturnHandle
= NULL
;
2023 gBS
->LocateHandleBuffer (
2025 &gEfiSimpleFileSystemProtocolGuid
,
2027 &NumberSimpleFileSystemHandles
,
2028 &SimpleFileSystemHandles
2030 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
2032 // Get the device path size of SimpleFileSystem handle
2034 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
2035 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
2037 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
2039 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
2041 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2042 // machinename is ia32, ia64, x64, ...
2044 Hdr
.Union
= &HdrData
;
2045 Status
= BdsLibGetImageHeader (
2046 SimpleFileSystemHandles
[Index
],
2047 EFI_REMOVABLE_MEDIA_FILE_NAME
,
2051 if (!EFI_ERROR (Status
) &&
2052 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
2053 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
2054 ReturnHandle
= SimpleFileSystemHandles
[Index
];
2060 FreePool(DupDevicePath
);
2062 if (SimpleFileSystemHandles
!= NULL
) {
2063 FreePool(SimpleFileSystemHandles
);
2066 gBS
->RestoreTPL (OldTpl
);
2068 return ReturnHandle
;
2072 Check to see if the network cable is plugged in. If the DevicePath is not
2073 connected it will be connected.
2075 @param DevicePath Device Path to check
2077 @retval TRUE DevicePath points to an Network that is connected
2078 @retval FALSE DevicePath does not point to a bootable network
2082 BdsLibNetworkBootWithMediaPresent (
2083 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2087 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
2089 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
2090 BOOLEAN MediaPresent
;
2091 UINT32 InterruptStatus
;
2093 MediaPresent
= FALSE
;
2095 UpdatedDevicePath
= DevicePath
;
2097 // Locate Load File Protocol for PXE boot option first
2099 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2100 if (EFI_ERROR (Status
)) {
2102 // Device not present so see if we need to connect it
2104 Status
= BdsLibConnectDevicePath (DevicePath
);
2105 if (!EFI_ERROR (Status
)) {
2107 // This one should work after we did the connect
2109 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2113 if (!EFI_ERROR (Status
)) {
2114 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
2115 if (EFI_ERROR (Status
)) {
2117 // Failed to open SNP from this handle, try to get SNP from parent handle
2119 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
2120 if (UpdatedDevicePath
!= NULL
) {
2121 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
2122 if (!EFI_ERROR (Status
)) {
2124 // SNP handle found, get SNP from it
2126 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
2131 if (!EFI_ERROR (Status
)) {
2132 if (Snp
->Mode
->MediaPresentSupported
) {
2133 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
2135 // Invoke Snp->GetStatus() to refresh the media status
2137 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
2140 // In case some one else is using the SNP check to see if it's connected
2142 MediaPresent
= Snp
->Mode
->MediaPresent
;
2145 // No one is using SNP so we need to Start and Initialize so
2146 // MediaPresent will be valid.
2148 Status
= Snp
->Start (Snp
);
2149 if (!EFI_ERROR (Status
)) {
2150 Status
= Snp
->Initialize (Snp
, 0, 0);
2151 if (!EFI_ERROR (Status
)) {
2152 MediaPresent
= Snp
->Mode
->MediaPresent
;
2153 Snp
->Shutdown (Snp
);
2159 MediaPresent
= TRUE
;
2164 return MediaPresent
;
2168 For a bootable Device path, return its boot type.
2170 @param DevicePath The bootable device Path to check
2172 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2173 which subtype is MEDIA_HARDDRIVE_DP
2174 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2175 which subtype is MEDIA_CDROM_DP
2176 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
2177 which HID is floppy device.
2178 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2179 and its last device path node's subtype is MSG_ATAPI_DP.
2180 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2181 and its last device path node's subtype is MSG_SCSI_DP.
2182 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2183 and its last device path node's subtype is MSG_USB_DP.
2184 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
2185 its last device path node point to a message device path node.
2186 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
2187 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
2192 BdsGetBootTypeFromDevicePath (
2193 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2196 ACPI_HID_DEVICE_PATH
*Acpi
;
2197 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2198 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2201 if (NULL
== DevicePath
) {
2202 return BDS_EFI_UNSUPPORT
;
2205 TempDevicePath
= DevicePath
;
2207 while (!IsDevicePathEndType (TempDevicePath
)) {
2208 switch (DevicePathType (TempDevicePath
)) {
2209 case BBS_DEVICE_PATH
:
2210 return BDS_LEGACY_BBS_BOOT
;
2211 case MEDIA_DEVICE_PATH
:
2212 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
2213 return BDS_EFI_MEDIA_HD_BOOT
;
2214 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
2215 return BDS_EFI_MEDIA_CDROM_BOOT
;
2218 case ACPI_DEVICE_PATH
:
2219 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
2220 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
2221 return BDS_EFI_ACPI_FLOPPY_BOOT
;
2224 case MESSAGING_DEVICE_PATH
:
2226 // Get the last device path node
2228 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
2229 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
2231 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
2234 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
2237 // if the device path not only point to driver device, it is not a messaging device path,
2239 if (!IsDevicePathEndType (LastDeviceNode
)) {
2243 switch (DevicePathSubType (TempDevicePath
)) {
2245 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
2249 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
2253 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
2257 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
2260 case MSG_MAC_ADDR_DP
:
2264 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
2268 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
2276 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2279 return BDS_EFI_UNSUPPORT
;
2283 Check whether the Device path in a boot option point to a valid bootable device,
2284 And if CheckMedia is true, check the device is ready to boot now.
2286 @param DevPath the Device path in a boot option
2287 @param CheckMedia if true, check the device is ready to boot now.
2289 @retval TRUE the Device path is valid
2290 @retval FALSE the Device path is invalid .
2295 BdsLibIsValidEFIBootOptDevicePath (
2296 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
2297 IN BOOLEAN CheckMedia
2300 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
2304 Check whether the Device path in a boot option point to a valid bootable device,
2305 And if CheckMedia is true, check the device is ready to boot now.
2306 If Description is not NULL and the device path point to a fixed BlockIo
2307 device, check the description whether conflict with other auto-created
2310 @param DevPath the Device path in a boot option
2311 @param CheckMedia if true, check the device is ready to boot now.
2312 @param Description the description in a boot option
2314 @retval TRUE the Device path is valid
2315 @retval FALSE the Device path is invalid .
2320 BdsLibIsValidEFIBootOptDevicePathExt (
2321 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
2322 IN BOOLEAN CheckMedia
,
2323 IN CHAR16
*Description
2328 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2329 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2330 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
2332 TempDevicePath
= DevPath
;
2333 LastDeviceNode
= DevPath
;
2336 // Check if it's a valid boot option for network boot device.
2337 // Check if there is EfiLoadFileProtocol installed.
2338 // If yes, that means there is a boot option for network.
2340 Status
= gBS
->LocateDevicePath (
2341 &gEfiLoadFileProtocolGuid
,
2345 if (EFI_ERROR (Status
)) {
2347 // Device not present so see if we need to connect it
2349 TempDevicePath
= DevPath
;
2350 BdsLibConnectDevicePath (TempDevicePath
);
2351 Status
= gBS
->LocateDevicePath (
2352 &gEfiLoadFileProtocolGuid
,
2358 if (!EFI_ERROR (Status
)) {
2359 if (!IsDevicePathEnd (TempDevicePath
)) {
2361 // LoadFile protocol is not installed on handle with exactly the same DevPath
2368 // Test if it is ready to boot now
2370 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
2379 // If the boot option point to a file, it is a valid EFI boot option,
2380 // and assume it is ready to boot now
2382 while (!IsDevicePathEnd (TempDevicePath
)) {
2384 // If there is USB Class or USB WWID device path node, treat it as valid EFI
2385 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
2386 // to full device path.
2388 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2389 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2390 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2394 LastDeviceNode
= TempDevicePath
;
2395 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2397 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
2398 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
2403 // Check if it's a valid boot option for internal FV application
2405 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
2407 // If the boot option point to internal FV application, make sure it is valid
2409 TempDevicePath
= DevPath
;
2410 Status
= BdsLibUpdateFvFileDevicePath (
2412 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
2414 if (Status
== EFI_ALREADY_STARTED
) {
2417 if (Status
== EFI_SUCCESS
) {
2418 FreePool (TempDevicePath
);
2425 // If the boot option point to a blockIO device:
2426 // if it is a removable blockIo device, it is valid.
2427 // if it is a fixed blockIo device, check its description confliction.
2429 TempDevicePath
= DevPath
;
2430 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
2431 if (EFI_ERROR (Status
)) {
2433 // Device not present so see if we need to connect it
2435 Status
= BdsLibConnectDevicePath (DevPath
);
2436 if (!EFI_ERROR (Status
)) {
2438 // Try again to get the Block Io protocol after we did the connect
2440 TempDevicePath
= DevPath
;
2441 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
2445 if (!EFI_ERROR (Status
)) {
2446 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
2447 if (!EFI_ERROR (Status
)) {
2450 // Test if it is ready to boot now
2452 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
2461 // 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,
2463 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
2464 if (!EFI_ERROR (Status
)) {
2467 // Test if it is ready to boot now
2469 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
2483 According to a file guild, check a Fv file device path is valid. If it is invalid,
2484 try to return the valid device path.
2485 FV address maybe changes for memory layout adjust from time to time, use this function
2486 could promise the Fv file device path is right.
2488 @param DevicePath on input, the Fv file device path need to check on
2489 output, the updated valid Fv file device path
2490 @param FileGuid the Fv file guild
2492 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
2494 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
2496 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
2498 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
2499 and return the updated device path in DevicePath
2504 BdsLibUpdateFvFileDevicePath (
2505 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
2506 IN EFI_GUID
*FileGuid
2509 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2510 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
2512 EFI_GUID
*GuidPoint
;
2514 UINTN FvHandleCount
;
2515 EFI_HANDLE
*FvHandleBuffer
;
2516 EFI_FV_FILETYPE Type
;
2518 EFI_FV_FILE_ATTRIBUTES Attributes
;
2519 UINT32 AuthenticationStatus
;
2521 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2522 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
2523 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
2524 EFI_HANDLE FoundFvHandle
;
2525 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
2527 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
2528 return EFI_INVALID_PARAMETER
;
2530 if (FileGuid
== NULL
) {
2531 return EFI_INVALID_PARAMETER
;
2535 // Check whether the device path point to the default the input Fv file
2537 TempDevicePath
= *DevicePath
;
2538 LastDeviceNode
= TempDevicePath
;
2539 while (!IsDevicePathEnd (TempDevicePath
)) {
2540 LastDeviceNode
= TempDevicePath
;
2541 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2543 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
2544 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
2546 if (GuidPoint
== NULL
) {
2548 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2550 return EFI_UNSUPPORTED
;
2552 if (!CompareGuid (GuidPoint
, FileGuid
)) {
2554 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2556 return EFI_UNSUPPORTED
;
2560 // Check whether the input Fv file device path is valid
2562 TempDevicePath
= *DevicePath
;
2563 FoundFvHandle
= NULL
;
2564 Status
= gBS
->LocateDevicePath (
2565 &gEfiFirmwareVolume2ProtocolGuid
,
2569 if (!EFI_ERROR (Status
)) {
2570 Status
= gBS
->HandleProtocol (
2572 &gEfiFirmwareVolume2ProtocolGuid
,
2575 if (!EFI_ERROR (Status
)) {
2577 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2579 Status
= Fv
->ReadFile (
2586 &AuthenticationStatus
2588 if (!EFI_ERROR (Status
)) {
2589 return EFI_ALREADY_STARTED
;
2595 // Look for the input wanted FV file in current FV
2596 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2599 FoundFvHandle
= NULL
;
2600 Status
= gBS
->HandleProtocol (
2602 &gEfiLoadedImageProtocolGuid
,
2603 (VOID
**) &LoadedImage
2605 if (!EFI_ERROR (Status
)) {
2606 Status
= gBS
->HandleProtocol (
2607 LoadedImage
->DeviceHandle
,
2608 &gEfiFirmwareVolume2ProtocolGuid
,
2611 if (!EFI_ERROR (Status
)) {
2612 Status
= Fv
->ReadFile (
2619 &AuthenticationStatus
2621 if (!EFI_ERROR (Status
)) {
2623 FoundFvHandle
= LoadedImage
->DeviceHandle
;
2628 // Second, if fail to find, try to enumerate all FV
2631 FvHandleBuffer
= NULL
;
2632 gBS
->LocateHandleBuffer (
2634 &gEfiFirmwareVolume2ProtocolGuid
,
2639 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
2640 gBS
->HandleProtocol (
2641 FvHandleBuffer
[Index
],
2642 &gEfiFirmwareVolume2ProtocolGuid
,
2646 Status
= Fv
->ReadFile (
2653 &AuthenticationStatus
2655 if (EFI_ERROR (Status
)) {
2657 // Skip if input Fv file not in the FV
2662 FoundFvHandle
= FvHandleBuffer
[Index
];
2666 if (FvHandleBuffer
!= NULL
) {
2667 FreePool (FvHandleBuffer
);
2673 // Build the shell device path
2675 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
2676 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
2677 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
2678 *DevicePath
= NewDevicePath
;
2681 return EFI_NOT_FOUND
;