2 Library functions which relates with booting.
4 Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalBm.h"
18 EFI_RAM_DISK_PROTOCOL
*mRamDisk
= NULL
;
20 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION mBmRefreshLegacyBootOption
= NULL
;
21 EFI_BOOT_MANAGER_LEGACY_BOOT mBmLegacyBoot
= NULL
;
24 /// This GUID is used for an EFI Variable that stores the front device pathes
25 /// for a partial device path that starts with the HD node.
27 EFI_GUID mBmHardDriveBootVariableGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x08, 0xe2, 0x0e, 0x90, 0x6c, 0xb6, 0xde } };
28 EFI_GUID mBmAutoCreateBootOptionGuid
= { 0x8108ac4e, 0x9f11, 0x4d59, { 0x85, 0x0e, 0xe2, 0x1a, 0x52, 0x2c, 0x59, 0xb2 } };
34 @param Event The triggered event.
35 @param Context Context for this event.
46 // Record the performance data for End of BDS
48 PERF_CROSSMODULE_END("BDS");
54 The function registers the legacy boot support capabilities.
56 @param RefreshLegacyBootOption The function pointer to create all the legacy boot options.
57 @param LegacyBoot The function pointer to boot the legacy boot option.
61 EfiBootManagerRegisterLegacyBootSupport (
62 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION RefreshLegacyBootOption
,
63 EFI_BOOT_MANAGER_LEGACY_BOOT LegacyBoot
66 mBmRefreshLegacyBootOption
= RefreshLegacyBootOption
;
67 mBmLegacyBoot
= LegacyBoot
;
71 Return TRUE when the boot option is auto-created instead of manually added.
73 @param BootOption Pointer to the boot option to check.
75 @retval TRUE The boot option is auto-created.
76 @retval FALSE The boot option is manually added.
79 BmIsAutoCreateBootOption (
80 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
83 if ((BootOption
->OptionalDataSize
== sizeof (EFI_GUID
)) &&
84 CompareGuid ((EFI_GUID
*) BootOption
->OptionalData
, &mBmAutoCreateBootOptionGuid
)
93 Find the boot option in the NV storage and return the option number.
95 @param OptionToFind Boot option to be checked.
97 @return The option number of the found boot option.
101 BmFindBootOptionInVariable (
102 IN EFI_BOOT_MANAGER_LOAD_OPTION
*OptionToFind
106 EFI_BOOT_MANAGER_LOAD_OPTION BootOption
;
108 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
109 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
110 UINTN BootOptionCount
;
113 OptionNumber
= LoadOptionNumberUnassigned
;
116 // Try to match the variable exactly if the option number is assigned
118 if (OptionToFind
->OptionNumber
!= LoadOptionNumberUnassigned
) {
120 OptionName
, sizeof (OptionName
), L
"%s%04x",
121 mBmLoadOptionName
[OptionToFind
->OptionType
], OptionToFind
->OptionNumber
123 Status
= EfiBootManagerVariableToLoadOption (OptionName
, &BootOption
);
125 if (!EFI_ERROR (Status
)) {
126 ASSERT (OptionToFind
->OptionNumber
== BootOption
.OptionNumber
);
127 if ((OptionToFind
->Attributes
== BootOption
.Attributes
) &&
128 (StrCmp (OptionToFind
->Description
, BootOption
.Description
) == 0) &&
129 (CompareMem (OptionToFind
->FilePath
, BootOption
.FilePath
, GetDevicePathSize (OptionToFind
->FilePath
)) == 0) &&
130 (OptionToFind
->OptionalDataSize
== BootOption
.OptionalDataSize
) &&
131 (CompareMem (OptionToFind
->OptionalData
, BootOption
.OptionalData
, OptionToFind
->OptionalDataSize
) == 0)
133 OptionNumber
= OptionToFind
->OptionNumber
;
135 EfiBootManagerFreeLoadOption (&BootOption
);
140 // The option number assigned is either incorrect or unassigned.
142 if (OptionNumber
== LoadOptionNumberUnassigned
) {
143 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
145 Index
= EfiBootManagerFindLoadOption (OptionToFind
, BootOptions
, BootOptionCount
);
147 OptionNumber
= BootOptions
[Index
].OptionNumber
;
150 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
157 Return the correct FV file path.
158 FV address may change across reboot. This routine promises the FV file device path is right.
160 @param FilePath The Memory Mapped Device Path to get the file buffer.
162 @return The updated FV Device Path pointint to the file.
164 EFI_DEVICE_PATH_PROTOCOL
*
166 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
171 EFI_DEVICE_PATH_PROTOCOL
*FvFileNode
;
173 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
175 EFI_HANDLE
*FvHandles
;
176 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
177 EFI_DEVICE_PATH_PROTOCOL
*FullPath
;
180 // Get the file buffer by using the exactly FilePath.
182 FvFileNode
= FilePath
;
183 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &FvFileNode
, &FvHandle
);
184 if (!EFI_ERROR (Status
)) {
185 return DuplicateDevicePath (FilePath
);
189 // Only wide match other FVs if it's a memory mapped FV file path.
191 if ((DevicePathType (FilePath
) != HARDWARE_DEVICE_PATH
) || (DevicePathSubType (FilePath
) != HW_MEMMAP_DP
)) {
195 FvFileNode
= NextDevicePathNode (FilePath
);
198 // Firstly find the FV file in current FV
200 gBS
->HandleProtocol (
202 &gEfiLoadedImageProtocolGuid
,
203 (VOID
**) &LoadedImage
205 NewDevicePath
= AppendDevicePathNode (DevicePathFromHandle (LoadedImage
->DeviceHandle
), FvFileNode
);
206 FullPath
= BmAdjustFvFilePath (NewDevicePath
);
207 FreePool (NewDevicePath
);
208 if (FullPath
!= NULL
) {
213 // Secondly find the FV file in all other FVs
215 gBS
->LocateHandleBuffer (
217 &gEfiFirmwareVolume2ProtocolGuid
,
222 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
223 if (FvHandles
[Index
] == LoadedImage
->DeviceHandle
) {
225 // Skip current FV, it was handed in first step.
229 NewDevicePath
= AppendDevicePathNode (DevicePathFromHandle (FvHandles
[Index
]), FvFileNode
);
230 FullPath
= BmAdjustFvFilePath (NewDevicePath
);
231 FreePool (NewDevicePath
);
232 if (FullPath
!= NULL
) {
237 if (FvHandles
!= NULL
) {
238 FreePool (FvHandles
);
244 Check if it's a Device Path pointing to FV file.
246 The function doesn't garentee the device path points to existing FV file.
248 @param DevicePath Input device path.
250 @retval TRUE The device path is a FV File Device Path.
251 @retval FALSE The device path is NOT a FV File Device Path.
255 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
260 EFI_DEVICE_PATH_PROTOCOL
*Node
;
263 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &Node
, &Handle
);
264 if (!EFI_ERROR (Status
)) {
268 if ((DevicePathType (DevicePath
) == HARDWARE_DEVICE_PATH
) && (DevicePathSubType (DevicePath
) == HW_MEMMAP_DP
)) {
269 DevicePath
= NextDevicePathNode (DevicePath
);
270 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) && (DevicePathSubType (DevicePath
) == MEDIA_PIWG_FW_FILE_DP
)) {
271 return IsDevicePathEnd (NextDevicePathNode (DevicePath
));
278 Check whether a USB device match the specified USB Class device path. This
279 function follows "Load Option Processing" behavior in UEFI specification.
281 @param UsbIo USB I/O protocol associated with the USB device.
282 @param UsbClass The USB Class device path to match.
284 @retval TRUE The USB device match the USB Class device path.
285 @retval FALSE The USB device does not match the USB Class device path.
290 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
291 IN USB_CLASS_DEVICE_PATH
*UsbClass
295 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
296 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
298 UINT8 DeviceSubClass
;
299 UINT8 DeviceProtocol
;
301 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
302 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
307 // Check Vendor Id and Product Id.
309 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
310 if (EFI_ERROR (Status
)) {
314 if ((UsbClass
->VendorId
!= 0xffff) &&
315 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
319 if ((UsbClass
->ProductId
!= 0xffff) &&
320 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
324 DeviceClass
= DevDesc
.DeviceClass
;
325 DeviceSubClass
= DevDesc
.DeviceSubClass
;
326 DeviceProtocol
= DevDesc
.DeviceProtocol
;
327 if (DeviceClass
== 0) {
329 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
330 // Protocol in Interface Descriptor instead.
332 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
333 if (EFI_ERROR (Status
)) {
337 DeviceClass
= IfDesc
.InterfaceClass
;
338 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
339 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
343 // Check Class, SubClass and Protocol.
345 if ((UsbClass
->DeviceClass
!= 0xff) &&
346 (UsbClass
->DeviceClass
!= DeviceClass
)) {
350 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
351 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
355 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
356 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
364 Check whether a USB device match the specified USB WWID device path. This
365 function follows "Load Option Processing" behavior in UEFI specification.
367 @param UsbIo USB I/O protocol associated with the USB device.
368 @param UsbWwid The USB WWID device path to match.
370 @retval TRUE The USB device match the USB WWID device path.
371 @retval FALSE The USB device does not match the USB WWID device path.
376 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
377 IN USB_WWID_DEVICE_PATH
*UsbWwid
381 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
382 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
388 CHAR16
*SerialNumberStr
;
391 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
392 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)) {
397 // Check Vendor Id and Product Id.
399 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
400 if (EFI_ERROR (Status
)) {
403 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
404 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
409 // Check Interface Number.
411 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
412 if (EFI_ERROR (Status
)) {
415 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
420 // Check Serial Number.
422 if (DevDesc
.StrSerialNumber
== 0) {
427 // Get all supported languages.
431 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
432 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
437 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
439 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
440 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
441 if (CompareStr
[CompareLen
- 1] == L
'\0') {
446 // Compare serial number in each supported language.
448 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
449 SerialNumberStr
= NULL
;
450 Status
= UsbIo
->UsbGetStringDescriptor (
453 DevDesc
.StrSerialNumber
,
456 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
460 Length
= StrLen (SerialNumberStr
);
461 if ((Length
>= CompareLen
) &&
462 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
463 FreePool (SerialNumberStr
);
467 FreePool (SerialNumberStr
);
474 Find a USB device which match the specified short-form device path start with
475 USB Class or USB WWID device path. If ParentDevicePath is NULL, this function
476 will search in all USB devices of the platform. If ParentDevicePath is not NULL,
477 this function will only search in its child devices.
479 @param DevicePath The device path that contains USB Class or USB WWID device path.
480 @param ParentDevicePathSize The length of the device path before the USB Class or
481 USB WWID device path.
482 @param UsbIoHandleCount A pointer to the count of the returned USB IO handles.
484 @retval NULL The matched USB IO handles cannot be found.
485 @retval other The matched USB IO handles.
490 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
491 IN UINTN ParentDevicePathSize
,
492 OUT UINTN
*UsbIoHandleCount
496 EFI_HANDLE
*UsbIoHandles
;
497 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
498 EFI_USB_IO_PROTOCOL
*UsbIo
;
502 ASSERT (UsbIoHandleCount
!= NULL
);
505 // Get all UsbIo Handles.
507 Status
= gBS
->LocateHandleBuffer (
509 &gEfiUsbIoProtocolGuid
,
514 if (EFI_ERROR (Status
)) {
515 *UsbIoHandleCount
= 0;
519 for (Index
= 0; Index
< *UsbIoHandleCount
; ) {
521 // Get the Usb IO interface.
523 Status
= gBS
->HandleProtocol(
525 &gEfiUsbIoProtocolGuid
,
528 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandles
[Index
]);
530 if (!EFI_ERROR (Status
) && (UsbIoDevicePath
!= NULL
)) {
533 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
535 if (CompareMem (UsbIoDevicePath
, DevicePath
, ParentDevicePathSize
) == 0) {
536 if (BmMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ((UINTN
) DevicePath
+ ParentDevicePathSize
)) ||
537 BmMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ((UINTN
) DevicePath
+ ParentDevicePathSize
))) {
544 (*UsbIoHandleCount
) --;
545 CopyMem (&UsbIoHandles
[Index
], &UsbIoHandles
[Index
+ 1], (*UsbIoHandleCount
- Index
) * sizeof (EFI_HANDLE
));
555 Expand USB Class or USB WWID device path node to be full device path of a USB
558 This function support following 4 cases:
559 1) Boot Option device path starts with a USB Class or USB WWID device path,
560 and there is no Media FilePath device path in the end.
561 In this case, it will follow Removable Media Boot Behavior.
562 2) Boot Option device path starts with a USB Class or USB WWID device path,
563 and ended with Media FilePath device path.
564 3) Boot Option device path starts with a full device path to a USB Host Controller,
565 contains a USB Class or USB WWID device path node, while not ended with Media
566 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
567 4) Boot Option device path starts with a full device path to a USB Host Controller,
568 contains a USB Class or USB WWID device path node, and ended with Media
569 FilePath device path.
571 @param FilePath The device path pointing to a load option.
572 It could be a short-form device path.
573 @param FullPath The full path returned by the routine in last call.
574 Set to NULL in first call.
575 @param ShortformNode Pointer to the USB short-form device path node in the FilePath buffer.
577 @return The next possible full path pointing to the load option.
578 Caller is responsible to free the memory.
580 EFI_DEVICE_PATH_PROTOCOL
*
581 BmExpandUsbDevicePath (
582 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
583 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
,
584 IN EFI_DEVICE_PATH_PROTOCOL
*ShortformNode
587 UINTN ParentDevicePathSize
;
588 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
589 EFI_DEVICE_PATH_PROTOCOL
*NextFullPath
;
596 GetNext
= (BOOLEAN
)(FullPath
== NULL
);
597 ParentDevicePathSize
= (UINTN
) ShortformNode
- (UINTN
) FilePath
;
598 RemainingDevicePath
= NextDevicePathNode (ShortformNode
);
599 Handles
= BmFindUsbDevice (FilePath
, ParentDevicePathSize
, &HandleCount
);
601 for (Index
= 0; Index
< HandleCount
; Index
++) {
602 FilePath
= AppendDevicePath (DevicePathFromHandle (Handles
[Index
]), RemainingDevicePath
);
603 if (FilePath
== NULL
) {
609 NextFullPath
= BmGetNextLoadOptionDevicePath (FilePath
, NULL
);
611 if (NextFullPath
== NULL
) {
613 // No BlockIo or SimpleFileSystem under FilePath.
620 GetNext
= (BOOLEAN
)(CompareMem (NextFullPath
, FullPath
, GetDevicePathSize (NextFullPath
)) == 0);
621 FreePool (NextFullPath
);
626 if (Handles
!= NULL
) {
634 Expand File-path device path node to be full device path in platform.
636 @param FilePath The device path pointing to a load option.
637 It could be a short-form device path.
638 @param FullPath The full path returned by the routine in last call.
639 Set to NULL in first call.
641 @return The next possible full path pointing to the load option.
642 Caller is responsible to free the memory.
644 EFI_DEVICE_PATH_PROTOCOL
*
645 BmExpandFileDevicePath (
646 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
647 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
654 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
656 EFI_DEVICE_PATH_PROTOCOL
*NextFullPath
;
659 EfiBootManagerConnectAll ();
660 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
, NULL
, &HandleCount
, &Handles
);
661 if (EFI_ERROR (Status
)) {
666 GetNext
= (BOOLEAN
)(FullPath
== NULL
);
669 // Enumerate all removable media devices followed by all fixed media devices,
670 // followed by media devices which don't layer on block io.
672 for (MediaType
= 0; MediaType
< 3; MediaType
++) {
673 for (Index
= 0; Index
< HandleCount
; Index
++) {
674 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiBlockIoProtocolGuid
, (VOID
*) &BlockIo
);
675 if (EFI_ERROR (Status
)) {
678 if ((MediaType
== 0 && BlockIo
!= NULL
&& BlockIo
->Media
->RemovableMedia
) ||
679 (MediaType
== 1 && BlockIo
!= NULL
&& !BlockIo
->Media
->RemovableMedia
) ||
680 (MediaType
== 2 && BlockIo
== NULL
)
682 NextFullPath
= AppendDevicePath (DevicePathFromHandle (Handles
[Index
]), FilePath
);
686 GetNext
= (BOOLEAN
)(CompareMem (NextFullPath
, FullPath
, GetDevicePathSize (NextFullPath
)) == 0);
687 FreePool (NextFullPath
);
692 if (NextFullPath
!= NULL
) {
697 if (Handles
!= NULL
) {
705 Expand URI device path node to be full device path in platform.
707 @param FilePath The device path pointing to a load option.
708 It could be a short-form device path.
709 @param FullPath The full path returned by the routine in last call.
710 Set to NULL in first call.
712 @return The next possible full path pointing to the load option.
713 Caller is responsible to free the memory.
715 EFI_DEVICE_PATH_PROTOCOL
*
716 BmExpandUriDevicePath (
717 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
718 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
725 EFI_DEVICE_PATH_PROTOCOL
*NextFullPath
;
726 EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
;
729 EfiBootManagerConnectAll ();
730 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiLoadFileProtocolGuid
, NULL
, &HandleCount
, &Handles
);
731 if (EFI_ERROR (Status
)) {
737 GetNext
= (BOOLEAN
)(FullPath
== NULL
);
738 for (Index
= 0; Index
< HandleCount
; Index
++) {
739 NextFullPath
= BmExpandLoadFile (Handles
[Index
], FilePath
);
741 if (NextFullPath
== NULL
) {
748 GetNext
= (BOOLEAN
)(CompareMem (NextFullPath
, FullPath
, GetDevicePathSize (NextFullPath
)) == 0);
750 // Free the resource occupied by the RAM disk.
752 RamDiskDevicePath
= BmGetRamDiskDevicePath (NextFullPath
);
753 if (RamDiskDevicePath
!= NULL
) {
754 BmDestroyRamDisk (RamDiskDevicePath
);
755 FreePool (RamDiskDevicePath
);
757 FreePool (NextFullPath
);
762 if (Handles
!= NULL
) {
770 Save the partition DevicePath to the CachedDevicePath as the first instance.
772 @param CachedDevicePath The device path cache.
773 @param DevicePath The partition device path to be cached.
776 BmCachePartitionDevicePath (
777 IN OUT EFI_DEVICE_PATH_PROTOCOL
**CachedDevicePath
,
778 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
781 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
784 if (BmMatchDevicePaths (*CachedDevicePath
, DevicePath
)) {
785 TempDevicePath
= *CachedDevicePath
;
786 *CachedDevicePath
= BmDelPartMatchInstance (*CachedDevicePath
, DevicePath
);
787 FreePool (TempDevicePath
);
790 if (*CachedDevicePath
== NULL
) {
791 *CachedDevicePath
= DuplicateDevicePath (DevicePath
);
795 TempDevicePath
= *CachedDevicePath
;
796 *CachedDevicePath
= AppendDevicePathInstance (DevicePath
, *CachedDevicePath
);
797 if (TempDevicePath
!= NULL
) {
798 FreePool (TempDevicePath
);
802 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
803 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
806 TempDevicePath
= *CachedDevicePath
;
807 while (!IsDevicePathEnd (TempDevicePath
)) {
808 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
810 // Parse one instance
812 while (!IsDevicePathEndType (TempDevicePath
)) {
813 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
817 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
820 SetDevicePathEndNode (TempDevicePath
);
827 Expand a device path that starts with a hard drive media device path node to be a
828 full device path that includes the full hardware path to the device. We need
829 to do this so it can be booted. As an optimization the front match (the part point
830 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
831 so a connect all is not required on every boot. All successful history device path
832 which point to partition node (the front part) will be saved.
834 @param FilePath The device path pointing to a load option.
835 It could be a short-form device path.
837 @return The full device path pointing to the load option.
839 EFI_DEVICE_PATH_PROTOCOL
*
840 BmExpandPartitionDevicePath (
841 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
845 UINTN BlockIoHandleCount
;
846 EFI_HANDLE
*BlockIoBuffer
;
847 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
849 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
850 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
851 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
852 EFI_DEVICE_PATH_PROTOCOL
*FullPath
;
853 UINTN CachedDevicePathSize
;
855 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
859 // Check if there is prestore 'HDDP' variable.
860 // If exist, search the front path which point to partition node in the variable instants.
861 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
863 GetVariable2 (L
"HDDP", &mBmHardDriveBootVariableGuid
, (VOID
**) &CachedDevicePath
, &CachedDevicePathSize
);
866 // Delete the invalid 'HDDP' variable.
868 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
869 FreePool (CachedDevicePath
);
870 CachedDevicePath
= NULL
;
871 Status
= gRT
->SetVariable (
873 &mBmHardDriveBootVariableGuid
,
878 ASSERT_EFI_ERROR (Status
);
882 if (CachedDevicePath
!= NULL
) {
883 TempNewDevicePath
= CachedDevicePath
;
887 // Check every instance of the variable
888 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
889 // partial partition boot option. Second, check whether the instance could be connected.
891 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
892 if (BmMatchPartitionDevicePathNode (Instance
, (HARDDRIVE_DEVICE_PATH
*) FilePath
)) {
894 // Connect the device path instance, the device path point to hard drive media device path node
895 // e.g. ACPI() /PCI()/ATA()/Partition()
897 Status
= EfiBootManagerConnectDevicePath (Instance
, NULL
);
898 if (!EFI_ERROR (Status
)) {
899 TempDevicePath
= AppendDevicePath (Instance
, NextDevicePathNode (FilePath
));
901 // TempDevicePath = ACPI()/PCI()/ATA()/Partition()
902 // or = ACPI()/PCI()/ATA()/Partition()/.../A.EFI
904 // When TempDevicePath = ACPI()/PCI()/ATA()/Partition(),
905 // it may expand to two potienal full paths (nested partition, rarely happen):
906 // 1. ACPI()/PCI()/ATA()/Partition()/Partition(A1)/EFI/BootX64.EFI
907 // 2. ACPI()/PCI()/ATA()/Partition()/Partition(A2)/EFI/BootX64.EFI
908 // For simplicity, only #1 is returned.
910 FullPath
= BmGetNextLoadOptionDevicePath (TempDevicePath
, NULL
);
911 FreePool (TempDevicePath
);
913 if (FullPath
!= NULL
) {
915 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
918 BmCachePartitionDevicePath (&CachedDevicePath
, Instance
);
920 // Save the matching Device Path so we don't need to do a connect all next time
921 // Failing to save only impacts performance next time expanding the short-form device path
923 Status
= gRT
->SetVariable (
925 &mBmHardDriveBootVariableGuid
,
926 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
927 GetDevicePathSize (CachedDevicePath
),
933 FreePool (CachedDevicePath
);
939 // Come here means the first instance is not matched
943 } while (TempNewDevicePath
!= NULL
);
947 // If we get here we fail to find or 'HDDP' not exist, and now we need
948 // to search all devices in the system for a matched partition
950 EfiBootManagerConnectAll ();
951 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
952 if (EFI_ERROR (Status
)) {
953 BlockIoHandleCount
= 0;
954 BlockIoBuffer
= NULL
;
957 // Loop through all the device handles that support the BLOCK_IO Protocol
959 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
960 BlockIoDevicePath
= DevicePathFromHandle (BlockIoBuffer
[Index
]);
961 if (BlockIoDevicePath
== NULL
) {
965 if (BmMatchPartitionDevicePathNode (BlockIoDevicePath
, (HARDDRIVE_DEVICE_PATH
*) FilePath
)) {
967 // Find the matched partition device path
969 TempDevicePath
= AppendDevicePath (BlockIoDevicePath
, NextDevicePathNode (FilePath
));
970 FullPath
= BmGetNextLoadOptionDevicePath (TempDevicePath
, NULL
);
971 FreePool (TempDevicePath
);
973 if (FullPath
!= NULL
) {
974 BmCachePartitionDevicePath (&CachedDevicePath
, BlockIoDevicePath
);
977 // Save the matching Device Path so we don't need to do a connect all next time
978 // Failing to save only impacts performance next time expanding the short-form device path
980 Status
= gRT
->SetVariable (
982 &mBmHardDriveBootVariableGuid
,
983 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
984 GetDevicePathSize (CachedDevicePath
),
993 if (CachedDevicePath
!= NULL
) {
994 FreePool (CachedDevicePath
);
996 if (BlockIoBuffer
!= NULL
) {
997 FreePool (BlockIoBuffer
);
1003 Expand the media device path which points to a BlockIo or SimpleFileSystem instance
1004 by appending EFI_REMOVABLE_MEDIA_FILE_NAME.
1006 @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance.
1007 @param FullPath The full path returned by the routine in last call.
1008 Set to NULL in first call.
1010 @return The next possible full path pointing to the load option.
1011 Caller is responsible to free the memory.
1013 EFI_DEVICE_PATH_PROTOCOL
*
1014 BmExpandMediaDevicePath (
1015 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1016 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
1021 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1023 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1024 EFI_DEVICE_PATH_PROTOCOL
*NextFullPath
;
1027 EFI_HANDLE
*SimpleFileSystemHandles
;
1028 UINTN NumberSimpleFileSystemHandles
;
1032 GetNext
= (BOOLEAN
)(FullPath
== NULL
);
1034 // Check whether the device is connected
1036 TempDevicePath
= DevicePath
;
1037 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1038 if (!EFI_ERROR (Status
)) {
1039 ASSERT (IsDevicePathEnd (TempDevicePath
));
1041 NextFullPath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
1043 // For device path pointing to simple file system, it only expands to one full path.
1046 return NextFullPath
;
1048 FreePool (NextFullPath
);
1053 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1054 ASSERT_EFI_ERROR (Status
);
1057 // For device boot option only pointing to the removable device handle,
1058 // should make sure all its children handles (its child partion or media handles)
1059 // are created and connected.
1061 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1064 // Issue a dummy read to the device to check for media change.
1065 // When the removable media is changed, any Block IO read/write will
1066 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1067 // returned. After the Block IO protocol is reinstalled, subsequent
1068 // Block IO read/write will success.
1070 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**) &BlockIo
);
1071 ASSERT_EFI_ERROR (Status
);
1072 if (EFI_ERROR (Status
)) {
1075 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1076 if (Buffer
!= NULL
) {
1077 BlockIo
->ReadBlocks (
1079 BlockIo
->Media
->MediaId
,
1081 BlockIo
->Media
->BlockSize
,
1088 // Detect the the default boot file from removable Media
1090 NextFullPath
= NULL
;
1091 Size
= GetDevicePathSize (DevicePath
) - END_DEVICE_PATH_LENGTH
;
1092 gBS
->LocateHandleBuffer (
1094 &gEfiSimpleFileSystemProtocolGuid
,
1096 &NumberSimpleFileSystemHandles
,
1097 &SimpleFileSystemHandles
1099 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1101 // Get the device path size of SimpleFileSystem handle
1103 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1104 TempSize
= GetDevicePathSize (TempDevicePath
) - END_DEVICE_PATH_LENGTH
;
1106 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1108 if ((Size
<= TempSize
) && (CompareMem (TempDevicePath
, DevicePath
, Size
) == 0)) {
1109 NextFullPath
= FileDevicePath (SimpleFileSystemHandles
[Index
], EFI_REMOVABLE_MEDIA_FILE_NAME
);
1113 GetNext
= (BOOLEAN
)(CompareMem (NextFullPath
, FullPath
, GetDevicePathSize (NextFullPath
)) == 0);
1114 FreePool (NextFullPath
);
1115 NextFullPath
= NULL
;
1120 if (SimpleFileSystemHandles
!= NULL
) {
1121 FreePool (SimpleFileSystemHandles
);
1124 return NextFullPath
;
1128 Check whether Left and Right are the same without matching the specific
1129 device path data in IP device path and URI device path node.
1131 @retval TRUE Left and Right are the same.
1132 @retval FALSE Left and Right are the different.
1135 BmMatchHttpBootDevicePath (
1136 IN EFI_DEVICE_PATH_PROTOCOL
*Left
,
1137 IN EFI_DEVICE_PATH_PROTOCOL
*Right
1140 for (; !IsDevicePathEnd (Left
) && !IsDevicePathEnd (Right
)
1141 ; Left
= NextDevicePathNode (Left
), Right
= NextDevicePathNode (Right
)
1143 if (CompareMem (Left
, Right
, DevicePathNodeLength (Left
)) != 0) {
1144 if ((DevicePathType (Left
) != MESSAGING_DEVICE_PATH
) || (DevicePathType (Right
) != MESSAGING_DEVICE_PATH
)) {
1148 if (DevicePathSubType (Left
) == MSG_DNS_DP
) {
1149 Left
= NextDevicePathNode (Left
);
1152 if (DevicePathSubType (Right
) == MSG_DNS_DP
) {
1153 Right
= NextDevicePathNode (Right
);
1156 if (((DevicePathSubType (Left
) != MSG_IPv4_DP
) || (DevicePathSubType (Right
) != MSG_IPv4_DP
)) &&
1157 ((DevicePathSubType (Left
) != MSG_IPv6_DP
) || (DevicePathSubType (Right
) != MSG_IPv6_DP
)) &&
1158 ((DevicePathSubType (Left
) != MSG_URI_DP
) || (DevicePathSubType (Right
) != MSG_URI_DP
))
1164 return (BOOLEAN
) (IsDevicePathEnd (Left
) && IsDevicePathEnd (Right
));
1168 Get the file buffer from the file system produced by Load File instance.
1170 @param LoadFileHandle The handle of LoadFile instance.
1171 @param RamDiskHandle Return the RAM Disk handle.
1173 @return The next possible full path pointing to the load option.
1174 Caller is responsible to free the memory.
1176 EFI_DEVICE_PATH_PROTOCOL
*
1177 BmExpandNetworkFileSystem (
1178 IN EFI_HANDLE LoadFileHandle
,
1179 OUT EFI_HANDLE
*RamDiskHandle
1184 EFI_HANDLE
*Handles
;
1187 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1189 Status
= gBS
->LocateHandleBuffer (
1191 &gEfiBlockIoProtocolGuid
,
1196 if (EFI_ERROR (Status
)) {
1202 for (Index
= 0; Index
< HandleCount
; Index
++) {
1203 Node
= DevicePathFromHandle (Handles
[Index
]);
1204 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &Node
, &Handle
);
1205 if (!EFI_ERROR (Status
) &&
1206 (Handle
== LoadFileHandle
) &&
1207 (DevicePathType (Node
) == MEDIA_DEVICE_PATH
) && (DevicePathSubType (Node
) == MEDIA_RAM_DISK_DP
)) {
1209 // Find the BlockIo instance populated from the LoadFile.
1211 Handle
= Handles
[Index
];
1216 if (Handles
!= NULL
) {
1220 if (Index
== HandleCount
) {
1224 *RamDiskHandle
= Handle
;
1226 if (Handle
!= NULL
) {
1228 // Re-use BmExpandMediaDevicePath() to get the full device path of load option.
1229 // But assume only one SimpleFileSystem can be found under the BlockIo.
1231 return BmExpandMediaDevicePath (DevicePathFromHandle (Handle
), NULL
);
1238 Return the RAM Disk device path created by LoadFile.
1240 @param FilePath The source file path.
1242 @return Callee-to-free RAM Disk device path
1244 EFI_DEVICE_PATH_PROTOCOL
*
1245 BmGetRamDiskDevicePath (
1246 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1250 EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
;
1251 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1255 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &Node
, &Handle
);
1256 if (!EFI_ERROR (Status
) &&
1257 (DevicePathType (Node
) == MEDIA_DEVICE_PATH
) &&
1258 (DevicePathSubType (Node
) == MEDIA_RAM_DISK_DP
)
1262 // Construct the device path pointing to RAM Disk
1264 Node
= NextDevicePathNode (Node
);
1265 RamDiskDevicePath
= DuplicateDevicePath (FilePath
);
1266 ASSERT (RamDiskDevicePath
!= NULL
);
1267 SetDevicePathEndNode ((VOID
*) ((UINTN
) RamDiskDevicePath
+ ((UINTN
) Node
- (UINTN
) FilePath
)));
1268 return RamDiskDevicePath
;
1275 Return the buffer and buffer size occupied by the RAM Disk.
1277 @param RamDiskDevicePath RAM Disk device path.
1278 @param RamDiskSizeInPages Return RAM Disk size in pages.
1280 @retval RAM Disk buffer.
1283 BmGetRamDiskMemoryInfo (
1284 IN EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
,
1285 OUT UINTN
*RamDiskSizeInPages
1291 UINT64 StartingAddr
;
1294 ASSERT (RamDiskDevicePath
!= NULL
);
1296 *RamDiskSizeInPages
= 0;
1299 // Get the buffer occupied by RAM Disk.
1301 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &RamDiskDevicePath
, &Handle
);
1302 ASSERT_EFI_ERROR (Status
);
1303 ASSERT ((DevicePathType (RamDiskDevicePath
) == MEDIA_DEVICE_PATH
) &&
1304 (DevicePathSubType (RamDiskDevicePath
) == MEDIA_RAM_DISK_DP
));
1305 StartingAddr
= ReadUnaligned64 ((UINT64
*) ((MEDIA_RAM_DISK_DEVICE_PATH
*) RamDiskDevicePath
)->StartingAddr
);
1306 EndingAddr
= ReadUnaligned64 ((UINT64
*) ((MEDIA_RAM_DISK_DEVICE_PATH
*) RamDiskDevicePath
)->EndingAddr
);
1307 *RamDiskSizeInPages
= EFI_SIZE_TO_PAGES ((UINTN
) (EndingAddr
- StartingAddr
+ 1));
1308 return (VOID
*) (UINTN
) StartingAddr
;
1312 Destroy the RAM Disk.
1314 The destroy operation includes to call RamDisk.Unregister to
1315 unregister the RAM DISK from RAM DISK driver, free the memory
1316 allocated for the RAM Disk.
1318 @param RamDiskDevicePath RAM Disk device path.
1322 IN EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
1326 VOID
*RamDiskBuffer
;
1327 UINTN RamDiskSizeInPages
;
1329 ASSERT (RamDiskDevicePath
!= NULL
);
1331 RamDiskBuffer
= BmGetRamDiskMemoryInfo (RamDiskDevicePath
, &RamDiskSizeInPages
);
1334 // Destroy RAM Disk.
1336 if (mRamDisk
== NULL
) {
1337 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
*) &mRamDisk
);
1338 ASSERT_EFI_ERROR (Status
);
1340 Status
= mRamDisk
->Unregister (RamDiskDevicePath
);
1341 ASSERT_EFI_ERROR (Status
);
1342 FreePages (RamDiskBuffer
, RamDiskSizeInPages
);
1346 Get the file buffer from the specified Load File instance.
1348 @param LoadFileHandle The specified Load File instance.
1349 @param FilePath The file path which will pass to LoadFile().
1351 @return The full device path pointing to the load option buffer.
1353 EFI_DEVICE_PATH_PROTOCOL
*
1355 IN EFI_HANDLE LoadFileHandle
,
1356 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1360 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1362 EFI_HANDLE RamDiskHandle
;
1364 EFI_DEVICE_PATH_PROTOCOL
*FullPath
;
1366 Status
= gBS
->OpenProtocol (
1368 &gEfiLoadFileProtocolGuid
,
1369 (VOID
**) &LoadFile
,
1372 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1374 ASSERT_EFI_ERROR (Status
);
1378 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, &BufferSize
, FileBuffer
);
1379 if ((Status
!= EFI_WARN_FILE_SYSTEM
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
1383 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1385 // The load option buffer is directly returned by LoadFile.
1387 return DuplicateDevicePath (DevicePathFromHandle (LoadFileHandle
));
1391 // The load option resides in a RAM disk.
1393 FileBuffer
= AllocateReservedPages (EFI_SIZE_TO_PAGES (BufferSize
));
1394 if (FileBuffer
== NULL
) {
1398 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, &BufferSize
, FileBuffer
);
1399 if (EFI_ERROR (Status
)) {
1400 FreePages (FileBuffer
, EFI_SIZE_TO_PAGES (BufferSize
));
1404 FullPath
= BmExpandNetworkFileSystem (LoadFileHandle
, &RamDiskHandle
);
1405 if (FullPath
== NULL
) {
1407 // Free the memory occupied by the RAM disk if there is no BlockIo or SimpleFileSystem instance.
1409 BmDestroyRamDisk (DevicePathFromHandle (RamDiskHandle
));
1416 Return the full device path pointing to the load option.
1419 1. Exactly matches to a LoadFile instance.
1420 2. Cannot match to any LoadFile instance. Wide match is required.
1421 In either case, the routine may return:
1422 1. A copy of FilePath when FilePath matches to a LoadFile instance and
1423 the LoadFile returns a load option buffer.
1424 2. A new device path with IP and URI information updated when wide match
1426 3. A new device path pointing to a load option in RAM disk.
1427 In either case, only one full device path is returned for a specified
1430 @param FilePath The media device path pointing to a LoadFile instance.
1432 @return The load option buffer.
1434 EFI_DEVICE_PATH_PROTOCOL
*
1436 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1441 EFI_HANDLE
*Handles
;
1444 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1447 // Get file buffer from load file instance.
1450 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &Node
, &Handle
);
1451 if (!EFI_ERROR (Status
) && IsDevicePathEnd (Node
)) {
1453 // When wide match happens, pass full device path to LoadFile (),
1454 // otherwise, pass remaining device path to LoadFile ().
1460 // Use wide match algorithm to find one when
1461 // cannot find a LoadFile instance to exactly match the FilePath
1463 Status
= gBS
->LocateHandleBuffer (
1465 &gEfiLoadFileProtocolGuid
,
1470 if (EFI_ERROR (Status
)) {
1474 for (Index
= 0; Index
< HandleCount
; Index
++) {
1475 if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles
[Index
]), FilePath
)) {
1476 Handle
= Handles
[Index
];
1480 if (Handles
!= NULL
) {
1485 if (Handle
== NULL
) {
1489 return BmExpandLoadFile (Handle
, FilePath
);
1493 Get the load option by its device path.
1495 @param FilePath The device path pointing to a load option.
1496 It could be a short-form device path.
1497 @param FullPath Return the full device path of the load option after
1498 short-form device path expanding.
1499 Caller is responsible to free it.
1500 @param FileSize Return the load option size.
1502 @return The load option buffer. Caller is responsible to free the memory.
1506 EfiBootManagerGetLoadOptionBuffer (
1507 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1508 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1514 EfiBootManagerConnectDevicePath (FilePath
, NULL
);
1515 return BmGetNextLoadOptionBuffer (LoadOptionTypeMax
, FilePath
, FullPath
, FileSize
);
1519 Get the next possible full path pointing to the load option.
1520 The routine doesn't guarantee the returned full path points to an existing
1521 file, and it also doesn't guarantee the existing file is a valid load option.
1522 BmGetNextLoadOptionBuffer() guarantees.
1524 @param FilePath The device path pointing to a load option.
1525 It could be a short-form device path.
1526 @param FullPath The full path returned by the routine in last call.
1527 Set to NULL in first call.
1529 @return The next possible full path pointing to the load option.
1530 Caller is responsible to free the memory.
1532 EFI_DEVICE_PATH_PROTOCOL
*
1533 BmGetNextLoadOptionDevicePath (
1534 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1535 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
1539 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1542 ASSERT (FilePath
!= NULL
);
1545 // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI
1548 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &Node
, &Handle
);
1549 if (EFI_ERROR (Status
)) {
1550 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &Node
, &Handle
);
1553 if (!EFI_ERROR (Status
) && IsDevicePathEnd (Node
)) {
1554 return BmExpandMediaDevicePath (FilePath
, FullPath
);
1558 // Expand the short-form device path to full device path
1560 if ((DevicePathType (FilePath
) == MEDIA_DEVICE_PATH
) &&
1561 (DevicePathSubType (FilePath
) == MEDIA_HARDDRIVE_DP
)) {
1563 // Expand the Harddrive device path
1565 if (FullPath
== NULL
) {
1566 return BmExpandPartitionDevicePath (FilePath
);
1570 } else if ((DevicePathType (FilePath
) == MEDIA_DEVICE_PATH
) &&
1571 (DevicePathSubType (FilePath
) == MEDIA_FILEPATH_DP
)) {
1573 // Expand the File-path device path
1575 return BmExpandFileDevicePath (FilePath
, FullPath
);
1576 } else if ((DevicePathType (FilePath
) == MESSAGING_DEVICE_PATH
) &&
1577 (DevicePathSubType (FilePath
) == MSG_URI_DP
)) {
1579 // Expand the URI device path
1581 return BmExpandUriDevicePath (FilePath
, FullPath
);
1584 Status
= gBS
->LocateDevicePath (&gEfiUsbIoProtocolGuid
, &Node
, &Handle
);
1585 if (EFI_ERROR (Status
)) {
1587 // Only expand the USB WWID/Class device path
1588 // when FilePath doesn't point to a physical UsbIo controller.
1589 // Otherwise, infinite recursion will happen.
1591 for (Node
= FilePath
; !IsDevicePathEnd (Node
); Node
= NextDevicePathNode (Node
)) {
1592 if ((DevicePathType (Node
) == MESSAGING_DEVICE_PATH
) &&
1593 ((DevicePathSubType (Node
) == MSG_USB_CLASS_DP
) || (DevicePathSubType (Node
) == MSG_USB_WWID_DP
))) {
1599 // Expand the USB WWID/Class device path
1601 if (!IsDevicePathEnd (Node
)) {
1602 if (FilePath
== Node
) {
1604 // Boot Option device path starts with USB Class or USB WWID device path.
1605 // For Boot Option device path which doesn't begin with the USB Class or
1606 // USB WWID device path, it's not needed to connect again here.
1608 BmConnectUsbShortFormDevicePath (FilePath
);
1610 return BmExpandUsbDevicePath (FilePath
, FullPath
, Node
);
1616 // For the below cases, FilePath only expands to one Full path.
1617 // So just handle the case when FullPath == NULL.
1619 if (FullPath
!= NULL
) {
1624 // Load option resides in FV.
1626 if (BmIsFvFilePath (FilePath
)) {
1627 return BmAdjustFvFilePath (FilePath
);
1631 // Load option resides in Simple File System.
1634 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &Node
, &Handle
);
1635 if (!EFI_ERROR (Status
)) {
1636 return DuplicateDevicePath (FilePath
);
1640 // Last chance to try: Load option may be loaded through LoadFile.
1642 return BmExpandLoadFiles (FilePath
);
1646 Check if it's a Device Path pointing to BootManagerMenu.
1648 @param DevicePath Input device path.
1650 @retval TRUE The device path is BootManagerMenu File Device Path.
1651 @retval FALSE The device path is NOT BootManagerMenu File Device Path.
1654 BmIsBootManagerMenuFilePath (
1655 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1658 EFI_HANDLE FvHandle
;
1662 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePath
, &FvHandle
);
1663 if (!EFI_ERROR (Status
)) {
1664 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePath
);
1665 if (NameGuid
!= NULL
) {
1666 return CompareGuid (NameGuid
, PcdGetPtr (PcdBootManagerMenuFile
));
1674 Report status code with EFI_RETURN_STATUS_EXTENDED_DATA about LoadImage() or
1675 StartImage() failure.
1677 @param[in] ErrorCode An Error Code in the Software Class, DXE Boot
1678 Service Driver Subclass. ErrorCode will be used to
1679 compose the Value parameter for status code
1680 reporting. Must be one of
1681 EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR and
1682 EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED.
1684 @param[in] FailureStatus The failure status returned by the boot service
1685 that should be reported.
1688 BmReportLoadFailure (
1689 IN UINT32 ErrorCode
,
1690 IN EFI_STATUS FailureStatus
1693 EFI_RETURN_STATUS_EXTENDED_DATA ExtendedData
;
1695 if (!ReportErrorCodeEnabled ()) {
1700 (ErrorCode
== EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
) ||
1701 (ErrorCode
== EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
1704 ZeroMem (&ExtendedData
, sizeof (ExtendedData
));
1705 ExtendedData
.ReturnStatus
= FailureStatus
;
1707 REPORT_STATUS_CODE_EX (
1708 (EFI_ERROR_CODE
| EFI_ERROR_MINOR
),
1709 (EFI_SOFTWARE_DXE_BS_DRIVER
| ErrorCode
),
1713 &ExtendedData
.DataHeader
+ 1,
1714 sizeof (ExtendedData
) - sizeof (ExtendedData
.DataHeader
)
1719 Attempt to boot the EFI boot option. This routine sets L"BootCurent" and
1720 also signals the EFI ready to boot event. If the device path for the option
1721 starts with a BBS device path a legacy boot is attempted via the registered
1722 gLegacyBoot function. Short form device paths are also supported via this
1723 rountine. A device path starting with MEDIA_HARDDRIVE_DP, MSG_USB_WWID_DP,
1724 MSG_USB_CLASS_DP gets expaned out to find the first device that matches.
1725 If the BootOption Device Path fails the removable media boot algorithm
1726 is attempted (\EFI\BOOTIA32.EFI, \EFI\BOOTX64.EFI,... only one file type
1727 is tried per processor type)
1729 @param BootOption Boot Option to try and boot.
1730 On return, BootOption->Status contains the boot status.
1731 EFI_SUCCESS BootOption was booted
1732 EFI_UNSUPPORTED A BBS device path was found with no valid callback
1733 registered via EfiBootManagerInitialize().
1734 EFI_NOT_FOUND The BootOption was not found on the system
1735 !EFI_SUCCESS BootOption failed with this error status
1740 EfiBootManagerBoot (
1741 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
1745 EFI_HANDLE ImageHandle
;
1746 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
1749 UINTN OriginalOptionNumber
;
1750 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
1751 EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
;
1754 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
1755 EFI_EVENT LegacyBootEvent
;
1757 if (BootOption
== NULL
) {
1761 if (BootOption
->FilePath
== NULL
|| BootOption
->OptionType
!= LoadOptionTypeBoot
) {
1762 BootOption
->Status
= EFI_INVALID_PARAMETER
;
1767 // 1. Create Boot#### for a temporary boot if there is no match Boot#### (i.e. a boot by selected a EFI Shell using "Boot From File")
1769 OptionNumber
= BmFindBootOptionInVariable (BootOption
);
1770 if (OptionNumber
== LoadOptionNumberUnassigned
) {
1771 Status
= BmGetFreeOptionNumber (LoadOptionTypeBoot
, &Uint16
);
1772 if (!EFI_ERROR (Status
)) {
1774 // Save the BootOption->OptionNumber to restore later
1776 OptionNumber
= Uint16
;
1777 OriginalOptionNumber
= BootOption
->OptionNumber
;
1778 BootOption
->OptionNumber
= OptionNumber
;
1779 Status
= EfiBootManagerLoadOptionToVariable (BootOption
);
1780 BootOption
->OptionNumber
= OriginalOptionNumber
;
1783 if (EFI_ERROR (Status
)) {
1784 DEBUG ((EFI_D_ERROR
, "[Bds] Failed to create Boot#### for a temporary boot - %r!\n", Status
));
1785 BootOption
->Status
= Status
;
1791 // 2. Set BootCurrent
1793 Uint16
= (UINT16
) OptionNumber
;
1794 BmSetVariableAndReportStatusCodeOnError (
1796 &gEfiGlobalVariableGuid
,
1797 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1803 // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute
1806 if (BmIsBootManagerMenuFilePath (BootOption
->FilePath
)) {
1807 DEBUG ((EFI_D_INFO
, "[Bds] Booting Boot Manager Menu.\n"));
1808 BmStopHotkeyService (NULL
, NULL
);
1810 EfiSignalEventReadyToBoot();
1812 // Report Status Code to indicate ReadyToBoot was signalled
1814 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
1816 // 4. Repair system through DriverHealth protocol
1818 BmRepairAllControllers (0);
1821 PERF_START_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
1824 // 5. Adjust the different type memory page number just before booting
1825 // and save the updated info into the variable for next boot to use
1827 BmSetMemoryTypeInformationVariable (
1828 (BOOLEAN
) ((BootOption
->Attributes
& LOAD_OPTION_CATEGORY
) == LOAD_OPTION_CATEGORY_BOOT
)
1832 // 6. Load EFI boot option to ImageHandle
1834 DEBUG_CODE_BEGIN ();
1835 if (BootOption
->Description
== NULL
) {
1836 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "[Bds]Booting from unknown device path\n"));
1838 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "[Bds]Booting %s\n", BootOption
->Description
));
1843 RamDiskDevicePath
= NULL
;
1844 if (DevicePathType (BootOption
->FilePath
) != BBS_DEVICE_PATH
) {
1845 Status
= EFI_NOT_FOUND
;
1847 EfiBootManagerConnectDevicePath (BootOption
->FilePath
, NULL
);
1848 FileBuffer
= BmGetNextLoadOptionBuffer (LoadOptionTypeBoot
, BootOption
->FilePath
, &FilePath
, &FileSize
);
1849 if (FileBuffer
!= NULL
) {
1850 RamDiskDevicePath
= BmGetRamDiskDevicePath (FilePath
);
1852 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
1853 Status
= gBS
->LoadImage (
1862 if (FileBuffer
!= NULL
) {
1863 FreePool (FileBuffer
);
1865 if (FilePath
!= NULL
) {
1866 FreePool (FilePath
);
1869 if (EFI_ERROR (Status
)) {
1871 // Report Status Code with the failure status to indicate that the failure to load boot option
1873 BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
, Status
);
1874 BootOption
->Status
= Status
;
1876 // Destroy the RAM disk
1878 if (RamDiskDevicePath
!= NULL
) {
1879 BmDestroyRamDisk (RamDiskDevicePath
);
1880 FreePool (RamDiskDevicePath
);
1887 // Check to see if we should legacy BOOT. If yes then do the legacy boot
1888 // Write boot to OS performance data for Legacy boot
1890 if ((DevicePathType (BootOption
->FilePath
) == BBS_DEVICE_PATH
) && (DevicePathSubType (BootOption
->FilePath
) == BBS_BBS_DP
)) {
1891 if (mBmLegacyBoot
!= NULL
) {
1893 // Write boot to OS performance data for legacy boot.
1897 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1899 Status
= EfiCreateEventLegacyBootEx(
1905 ASSERT_EFI_ERROR (Status
);
1908 mBmLegacyBoot (BootOption
);
1910 BootOption
->Status
= EFI_UNSUPPORTED
;
1913 PERF_END_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
1918 // Provide the image with its load options
1920 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
1921 ASSERT_EFI_ERROR (Status
);
1923 if (!BmIsAutoCreateBootOption (BootOption
)) {
1924 ImageInfo
->LoadOptionsSize
= BootOption
->OptionalDataSize
;
1925 ImageInfo
->LoadOptions
= BootOption
->OptionalData
;
1929 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
1931 ImageInfo
->ParentHandle
= NULL
;
1934 // Before calling the image, enable the Watchdog Timer for 5 minutes period
1936 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
1939 // Write boot to OS performance data for UEFI boot
1942 BmEndOfBdsPerfCode (NULL
, NULL
);
1945 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
1947 Status
= gBS
->StartImage (ImageHandle
, &BootOption
->ExitDataSize
, &BootOption
->ExitData
);
1948 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
1949 BootOption
->Status
= Status
;
1950 if (EFI_ERROR (Status
)) {
1952 // Report Status Code with the failure status to indicate that boot failure
1954 BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
, Status
);
1956 PERF_END_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
1959 // Destroy the RAM disk
1961 if (RamDiskDevicePath
!= NULL
) {
1962 BmDestroyRamDisk (RamDiskDevicePath
);
1963 FreePool (RamDiskDevicePath
);
1967 // Clear the Watchdog Timer after the image returns
1969 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
1972 // Set Logo status invalid after trying one boot option
1975 Status
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
1976 if (!EFI_ERROR (Status
) && (BootLogo
!= NULL
)) {
1977 Status
= BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
1978 ASSERT_EFI_ERROR (Status
);
1982 // Clear Boot Current
1984 Status
= gRT
->SetVariable (
1986 &gEfiGlobalVariableGuid
,
1992 // Deleting variable with current variable implementation shouldn't fail.
1993 // When BootXXXX (e.g.: BootManagerMenu) boots BootYYYY, exiting BootYYYY causes BootCurrent deleted,
1994 // exiting BootXXXX causes deleting BootCurrent returns EFI_NOT_FOUND.
1996 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_FOUND
);
2000 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2001 instances, has the same partition node with HardDriveDevicePath device path
2003 @param BlockIoDevicePath Multi device path instances which need to check
2004 @param HardDriveDevicePath A device path which starts with a hard drive media
2007 @retval TRUE There is a matched device path instance.
2008 @retval FALSE There is no matched device path instance.
2012 BmMatchPartitionDevicePathNode (
2013 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2014 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2017 HARDDRIVE_DEVICE_PATH
*Node
;
2019 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2024 // Match all the partition device path nodes including the nested partition nodes
2026 while (!IsDevicePathEnd (BlockIoDevicePath
)) {
2027 if ((DevicePathType (BlockIoDevicePath
) == MEDIA_DEVICE_PATH
) &&
2028 (DevicePathSubType (BlockIoDevicePath
) == MEDIA_HARDDRIVE_DP
)
2031 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2033 Node
= (HARDDRIVE_DEVICE_PATH
*) BlockIoDevicePath
;
2036 // Match Signature and PartitionNumber.
2037 // Unused bytes in Signature are initiaized with zeros.
2039 if ((Node
->PartitionNumber
== HardDriveDevicePath
->PartitionNumber
) &&
2040 (Node
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2041 (Node
->SignatureType
== HardDriveDevicePath
->SignatureType
) &&
2042 (CompareMem (Node
->Signature
, HardDriveDevicePath
->Signature
, sizeof (Node
->Signature
)) == 0)) {
2047 BlockIoDevicePath
= NextDevicePathNode (BlockIoDevicePath
);
2054 Emuerate all possible bootable medias in the following order:
2055 1. Removable BlockIo - The boot option only points to the removable media
2056 device, like USB key, DVD, Floppy etc.
2057 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
2059 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting
2060 SimpleFileSystem Protocol, but not supporting BlockIo
2062 4. LoadFile - The boot option points to the media supporting
2064 Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior
2066 @param BootOptionCount Return the boot option count which has been found.
2068 @retval Pointer to the boot option array.
2070 EFI_BOOT_MANAGER_LOAD_OPTION
*
2071 BmEnumerateBootOptions (
2072 UINTN
*BootOptionCount
2076 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2078 EFI_HANDLE
*Handles
;
2079 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
2082 CHAR16
*Description
;
2084 ASSERT (BootOptionCount
!= NULL
);
2086 *BootOptionCount
= 0;
2090 // Parse removable block io followed by fixed block io
2092 gBS
->LocateHandleBuffer (
2094 &gEfiBlockIoProtocolGuid
,
2100 for (Removable
= 0; Removable
< 2; Removable
++) {
2101 for (Index
= 0; Index
< HandleCount
; Index
++) {
2102 Status
= gBS
->HandleProtocol (
2104 &gEfiBlockIoProtocolGuid
,
2107 if (EFI_ERROR (Status
)) {
2112 // Skip the logical partitions
2114 if (BlkIo
->Media
->LogicalPartition
) {
2119 // Skip the fixed block io then the removable block io
2121 if (BlkIo
->Media
->RemovableMedia
== ((Removable
== 0) ? FALSE
: TRUE
)) {
2125 Description
= BmGetBootDescription (Handles
[Index
]);
2126 BootOptions
= ReallocatePool (
2127 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2128 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2131 ASSERT (BootOptions
!= NULL
);
2133 Status
= EfiBootManagerInitializeLoadOption (
2134 &BootOptions
[(*BootOptionCount
)++],
2135 LoadOptionNumberUnassigned
,
2139 DevicePathFromHandle (Handles
[Index
]),
2143 ASSERT_EFI_ERROR (Status
);
2145 FreePool (Description
);
2149 if (HandleCount
!= 0) {
2154 // Parse simple file system not based on block io
2156 gBS
->LocateHandleBuffer (
2158 &gEfiSimpleFileSystemProtocolGuid
,
2163 for (Index
= 0; Index
< HandleCount
; Index
++) {
2164 Status
= gBS
->HandleProtocol (
2166 &gEfiBlockIoProtocolGuid
,
2169 if (!EFI_ERROR (Status
)) {
2171 // Skip if the file system handle supports a BlkIo protocol, which we've handled in above
2175 Description
= BmGetBootDescription (Handles
[Index
]);
2176 BootOptions
= ReallocatePool (
2177 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2178 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2181 ASSERT (BootOptions
!= NULL
);
2183 Status
= EfiBootManagerInitializeLoadOption (
2184 &BootOptions
[(*BootOptionCount
)++],
2185 LoadOptionNumberUnassigned
,
2189 DevicePathFromHandle (Handles
[Index
]),
2193 ASSERT_EFI_ERROR (Status
);
2194 FreePool (Description
);
2197 if (HandleCount
!= 0) {
2202 // Parse load file protocol
2204 gBS
->LocateHandleBuffer (
2206 &gEfiLoadFileProtocolGuid
,
2211 for (Index
= 0; Index
< HandleCount
; Index
++) {
2213 // Ignore BootManagerMenu. its boot option will be created by EfiBootManagerGetBootManagerMenu().
2215 if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles
[Index
]))) {
2219 Description
= BmGetBootDescription (Handles
[Index
]);
2220 BootOptions
= ReallocatePool (
2221 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2222 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2225 ASSERT (BootOptions
!= NULL
);
2227 Status
= EfiBootManagerInitializeLoadOption (
2228 &BootOptions
[(*BootOptionCount
)++],
2229 LoadOptionNumberUnassigned
,
2233 DevicePathFromHandle (Handles
[Index
]),
2237 ASSERT_EFI_ERROR (Status
);
2238 FreePool (Description
);
2241 if (HandleCount
!= 0) {
2245 BmMakeBootOptionDescriptionUnique (BootOptions
, *BootOptionCount
);
2250 The function enumerates all boot options, creates them and registers them in the BootOrder variable.
2254 EfiBootManagerRefreshAllBootOption (
2259 EFI_BOOT_MANAGER_LOAD_OPTION
*NvBootOptions
;
2260 UINTN NvBootOptionCount
;
2261 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2262 UINTN BootOptionCount
;
2266 // Optionally refresh the legacy boot option
2268 if (mBmRefreshLegacyBootOption
!= NULL
) {
2269 mBmRefreshLegacyBootOption ();
2272 BootOptions
= BmEnumerateBootOptions (&BootOptionCount
);
2273 NvBootOptions
= EfiBootManagerGetLoadOptions (&NvBootOptionCount
, LoadOptionTypeBoot
);
2276 // Mark the boot option as added by BDS by setting OptionalData to a special GUID
2278 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2279 BootOptions
[Index
].OptionalData
= AllocateCopyPool (sizeof (EFI_GUID
), &mBmAutoCreateBootOptionGuid
);
2280 BootOptions
[Index
].OptionalDataSize
= sizeof (EFI_GUID
);
2284 // Remove invalid EFI boot options from NV
2286 for (Index
= 0; Index
< NvBootOptionCount
; Index
++) {
2287 if (((DevicePathType (NvBootOptions
[Index
].FilePath
) != BBS_DEVICE_PATH
) ||
2288 (DevicePathSubType (NvBootOptions
[Index
].FilePath
) != BBS_BBS_DP
)
2289 ) && BmIsAutoCreateBootOption (&NvBootOptions
[Index
])
2292 // Only check those added by BDS
2293 // so that the boot options added by end-user or OS installer won't be deleted
2295 if (EfiBootManagerFindLoadOption (&NvBootOptions
[Index
], BootOptions
, BootOptionCount
) == -1) {
2296 Status
= EfiBootManagerDeleteLoadOptionVariable (NvBootOptions
[Index
].OptionNumber
, LoadOptionTypeBoot
);
2298 // Deleting variable with current variable implementation shouldn't fail.
2300 ASSERT_EFI_ERROR (Status
);
2306 // Add new EFI boot options to NV
2308 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2309 if (EfiBootManagerFindLoadOption (&BootOptions
[Index
], NvBootOptions
, NvBootOptionCount
) == -1) {
2310 EfiBootManagerAddLoadOptionVariable (&BootOptions
[Index
], (UINTN
) -1);
2312 // Try best to add the boot options so continue upon failure.
2317 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2318 EfiBootManagerFreeLoadOptions (NvBootOptions
, NvBootOptionCount
);
2322 This function is called to get or create the boot option for the Boot Manager Menu.
2324 The Boot Manager Menu is shown after successfully booting a boot option.
2325 Assume the BootManagerMenuFile is in the same FV as the module links to this library.
2327 @param BootOption Return the boot option of the Boot Manager Menu
2329 @retval EFI_SUCCESS Successfully register the Boot Manager Menu.
2330 @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.
2331 @retval others Return status of gRT->SetVariable (). BootOption still points
2332 to the Boot Manager Menu even the Status is not EFI_SUCCESS
2336 BmRegisterBootManagerMenu (
2337 OUT EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
2341 CHAR16
*Description
;
2342 UINTN DescriptionLength
;
2343 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2344 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2345 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode
;
2347 EFI_HANDLE
*Handles
;
2355 // Try to find BootManagerMenu from LoadFile protocol
2357 gBS
->LocateHandleBuffer (
2359 &gEfiLoadFileProtocolGuid
,
2364 for (Index
= 0; Index
< HandleCount
; Index
++) {
2365 if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles
[Index
]))) {
2366 DevicePath
= DuplicateDevicePath (DevicePathFromHandle (Handles
[Index
]));
2367 Description
= BmGetBootDescription (Handles
[Index
]);
2371 if (HandleCount
!= 0) {
2375 if (DevicePath
== NULL
) {
2377 Status
= GetSectionFromFv (
2378 PcdGetPtr (PcdBootManagerMenuFile
),
2387 if (EFI_ERROR (Status
)) {
2388 DEBUG ((EFI_D_WARN
, "[Bds]BootManagerMenu FFS section can not be found, skip its boot option registration\n"));
2389 return EFI_NOT_FOUND
;
2393 // Get BootManagerMenu application's description from EFI User Interface Section.
2395 Status
= GetSectionFromFv (
2396 PcdGetPtr (PcdBootManagerMenuFile
),
2397 EFI_SECTION_USER_INTERFACE
,
2399 (VOID
**) &Description
,
2402 if (EFI_ERROR (Status
)) {
2406 EfiInitializeFwVolDevicepathNode (&FileNode
, PcdGetPtr (PcdBootManagerMenuFile
));
2407 Status
= gBS
->HandleProtocol (
2409 &gEfiLoadedImageProtocolGuid
,
2410 (VOID
**) &LoadedImage
2412 ASSERT_EFI_ERROR (Status
);
2413 DevicePath
= AppendDevicePathNode (
2414 DevicePathFromHandle (LoadedImage
->DeviceHandle
),
2415 (EFI_DEVICE_PATH_PROTOCOL
*) &FileNode
2417 ASSERT (DevicePath
!= NULL
);
2420 Status
= EfiBootManagerInitializeLoadOption (
2422 LoadOptionNumberUnassigned
,
2424 LOAD_OPTION_CATEGORY_APP
| LOAD_OPTION_ACTIVE
| LOAD_OPTION_HIDDEN
,
2425 (Description
!= NULL
) ? Description
: L
"Boot Manager Menu",
2430 ASSERT_EFI_ERROR (Status
);
2431 FreePool (DevicePath
);
2432 if (Description
!= NULL
) {
2433 FreePool (Description
);
2437 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2438 UINTN BootOptionCount
;
2440 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
2441 ASSERT (EfiBootManagerFindLoadOption (BootOption
, BootOptions
, BootOptionCount
) == -1);
2442 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2445 return EfiBootManagerAddLoadOptionVariable (BootOption
, 0);
2449 Return the boot option corresponding to the Boot Manager Menu.
2450 It may automatically create one if the boot option hasn't been created yet.
2452 @param BootOption Return the Boot Manager Menu.
2454 @retval EFI_SUCCESS The Boot Manager Menu is successfully returned.
2455 @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.
2456 @retval others Return status of gRT->SetVariable (). BootOption still points
2457 to the Boot Manager Menu even the Status is not EFI_SUCCESS
2462 EfiBootManagerGetBootManagerMenu (
2463 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
2467 UINTN BootOptionCount
;
2468 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2471 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
2473 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2474 if (BmIsBootManagerMenuFilePath (BootOptions
[Index
].FilePath
)) {
2475 Status
= EfiBootManagerInitializeLoadOption (
2477 BootOptions
[Index
].OptionNumber
,
2478 BootOptions
[Index
].OptionType
,
2479 BootOptions
[Index
].Attributes
,
2480 BootOptions
[Index
].Description
,
2481 BootOptions
[Index
].FilePath
,
2482 BootOptions
[Index
].OptionalData
,
2483 BootOptions
[Index
].OptionalDataSize
2485 ASSERT_EFI_ERROR (Status
);
2490 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2493 // Automatically create the Boot#### for Boot Manager Menu when not found.
2495 if (Index
== BootOptionCount
) {
2496 return BmRegisterBootManagerMenu (BootOption
);
2503 Get the next possible full path pointing to the load option.
2504 The routine doesn't guarantee the returned full path points to an existing
2505 file, and it also doesn't guarantee the existing file is a valid load option.
2506 BmGetNextLoadOptionBuffer() guarantees.
2508 @param FilePath The device path pointing to a load option.
2509 It could be a short-form device path.
2510 @param FullPath The full path returned by the routine in last call.
2511 Set to NULL in first call.
2513 @return The next possible full path pointing to the load option.
2514 Caller is responsible to free the memory.
2516 EFI_DEVICE_PATH_PROTOCOL
*
2518 EfiBootManagerGetNextLoadOptionDevicePath (
2519 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
2520 IN EFI_DEVICE_PATH_PROTOCOL
*FullPath
2523 return BmGetNextLoadOptionDevicePath(FilePath
, FullPath
);