2 Library functions which relates with booting.
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 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 #define VENDOR_IDENTIFICATION_OFFSET 3
19 #define VENDOR_IDENTIFICATION_LENGTH 8
20 #define PRODUCT_IDENTIFICATION_OFFSET 11
21 #define PRODUCT_IDENTIFICATION_LENGTH 16
23 CONST UINT16 mBmUsbLangId
= 0x0409; // English
24 CHAR16 mBmUefiPrefix
[] = L
"UEFI ";
26 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION mBmRefreshLegacyBootOption
= NULL
;
27 EFI_BOOT_MANAGER_LEGACY_BOOT mBmLegacyBoot
= NULL
;
29 LIST_ENTRY mPlatformBootDescriptionHandlers
= INITIALIZE_LIST_HEAD_VARIABLE (mPlatformBootDescriptionHandlers
);
32 /// This GUID is used for an EFI Variable that stores the front device pathes
33 /// for a partial device path that starts with the HD node.
35 EFI_GUID mBmHardDriveBootVariableGuid
= { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x08, 0xe2, 0x0e, 0x90, 0x6c, 0xb6, 0xde } };
36 EFI_GUID mBmAutoCreateBootOptionGuid
= { 0x8108ac4e, 0x9f11, 0x4d59, { 0x85, 0x0e, 0xe2, 0x1a, 0x52, 0x2c, 0x59, 0xb2 } };
39 The function registers the legacy boot support capabilities.
41 @param RefreshLegacyBootOption The function pointer to create all the legacy boot options.
42 @param LegacyBoot The function pointer to boot the legacy boot option.
46 EfiBootManagerRegisterLegacyBootSupport (
47 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION RefreshLegacyBootOption
,
48 EFI_BOOT_MANAGER_LEGACY_BOOT LegacyBoot
51 mBmRefreshLegacyBootOption
= RefreshLegacyBootOption
;
52 mBmLegacyBoot
= LegacyBoot
;
56 Return TRUE when the boot option is auto-created instead of manually added.
58 @param BootOption Pointer to the boot option to check.
60 @retval TRUE The boot option is auto-created.
61 @retval FALSE The boot option is manually added.
64 BmIsAutoCreateBootOption (
65 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
68 if ((BootOption
->OptionalDataSize
== sizeof (EFI_GUID
)) &&
69 CompareGuid ((EFI_GUID
*) BootOption
->OptionalData
, &mBmAutoCreateBootOptionGuid
)
78 For a bootable Device path, return its boot type.
80 @param DevicePath The bootable device Path to check
82 @retval AcpiFloppyBoot If given device path contains ACPI_DEVICE_PATH type device path node
83 which HID is floppy device.
84 @retval MessageAtapiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
85 and its last device path node's subtype is MSG_ATAPI_DP.
86 @retval MessageSataBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
87 and its last device path node's subtype is MSG_SATA_DP.
88 @retval MessageScsiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
89 and its last device path node's subtype is MSG_SCSI_DP.
90 @retval MessageUsbBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
91 and its last device path node's subtype is MSG_USB_DP.
92 @retval MessageNetworkBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
93 and its last device path node's subtype is MSG_MAC_ADDR_DP, MSG_VLAN_DP,
94 MSG_IPv4_DP or MSG_IPv6_DP.
95 @retval MessageHttpBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
96 and its last device path node's subtype is MSG_URI_DP.
97 @retval UnsupportedBoot If tiven device path doesn't match the above condition, it's not supported.
102 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
105 EFI_DEVICE_PATH_PROTOCOL
*Node
;
106 EFI_DEVICE_PATH_PROTOCOL
*NextNode
;
108 ASSERT (DevicePath
!= NULL
);
110 for (Node
= DevicePath
; !IsDevicePathEndType (Node
); Node
= NextDevicePathNode (Node
)) {
111 switch (DevicePathType (Node
)) {
113 case ACPI_DEVICE_PATH
:
114 if (EISA_ID_TO_NUM (((ACPI_HID_DEVICE_PATH
*) Node
)->HID
) == 0x0604) {
115 return BmAcpiFloppyBoot
;
119 case HARDWARE_DEVICE_PATH
:
120 if (DevicePathSubType (Node
) == HW_CONTROLLER_DP
) {
121 return BmHardwareDeviceBoot
;
125 case MESSAGING_DEVICE_PATH
:
127 // Skip LUN device node
131 NextNode
= NextDevicePathNode (NextNode
);
133 (DevicePathType (NextNode
) == MESSAGING_DEVICE_PATH
) &&
134 (DevicePathSubType(NextNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
)
138 // If the device path not only point to driver device, it is not a messaging device path,
140 if (!IsDevicePathEndType (NextNode
)) {
144 switch (DevicePathSubType (Node
)) {
146 return BmMessageAtapiBoot
;
150 return BmMessageSataBoot
;
154 return BmMessageUsbBoot
;
158 return BmMessageScsiBoot
;
161 case MSG_MAC_ADDR_DP
:
165 return BmMessageNetworkBoot
;
169 return BmMessageHttpBoot
;
179 Find the boot option in the NV storage and return the option number.
181 @param OptionToFind Boot option to be checked.
183 @return The option number of the found boot option.
187 BmFindBootOptionInVariable (
188 IN EFI_BOOT_MANAGER_LOAD_OPTION
*OptionToFind
192 EFI_BOOT_MANAGER_LOAD_OPTION BootOption
;
194 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
195 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
196 UINTN BootOptionCount
;
199 OptionNumber
= LoadOptionNumberUnassigned
;
202 // Try to match the variable exactly if the option number is assigned
204 if (OptionToFind
->OptionNumber
!= LoadOptionNumberUnassigned
) {
206 OptionName
, sizeof (OptionName
), L
"%s%04x",
207 mBmLoadOptionName
[OptionToFind
->OptionType
], OptionToFind
->OptionNumber
209 Status
= EfiBootManagerVariableToLoadOption (OptionName
, &BootOption
);
211 if (!EFI_ERROR (Status
)) {
212 ASSERT (OptionToFind
->OptionNumber
== BootOption
.OptionNumber
);
213 if ((OptionToFind
->Attributes
== BootOption
.Attributes
) &&
214 (StrCmp (OptionToFind
->Description
, BootOption
.Description
) == 0) &&
215 (CompareMem (OptionToFind
->FilePath
, BootOption
.FilePath
, GetDevicePathSize (OptionToFind
->FilePath
)) == 0) &&
216 (OptionToFind
->OptionalDataSize
== BootOption
.OptionalDataSize
) &&
217 (CompareMem (OptionToFind
->OptionalData
, BootOption
.OptionalData
, OptionToFind
->OptionalDataSize
) == 0)
219 OptionNumber
= OptionToFind
->OptionNumber
;
221 EfiBootManagerFreeLoadOption (&BootOption
);
226 // The option number assigned is either incorrect or unassigned.
228 if (OptionNumber
== LoadOptionNumberUnassigned
) {
229 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
231 Index
= EfiBootManagerFindLoadOption (OptionToFind
, BootOptions
, BootOptionCount
);
233 OptionNumber
= BootOptions
[Index
].OptionNumber
;
236 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
243 Get the file buffer using a Memory Mapped Device Path.
245 FV address may change across reboot. This routine promises the FV file device path is right.
247 @param FilePath The Memory Mapped Device Path to get the file buffer.
248 @param FullPath Receive the updated FV Device Path pointint to the file.
249 @param FileSize Receive the file buffer size.
251 @return The file buffer.
254 BmGetFileBufferByFvFilePath (
255 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
256 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
262 EFI_DEVICE_PATH_PROTOCOL
*FvFileNode
;
264 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
265 UINT32 AuthenticationStatus
;
267 EFI_HANDLE
*FvHandles
;
268 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
272 // Get the file buffer by using the exactly FilePath.
274 FvFileNode
= FilePath
;
275 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &FvFileNode
, &FvHandle
);
276 if (!EFI_ERROR (Status
)) {
277 FileBuffer
= GetFileBufferByFilePath (TRUE
, FilePath
, FileSize
, &AuthenticationStatus
);
278 if (FileBuffer
!= NULL
) {
279 *FullPath
= DuplicateDevicePath (FilePath
);
285 // Only wide match other FVs if it's a memory mapped FV file path.
287 if ((DevicePathType (FilePath
) != HARDWARE_DEVICE_PATH
) || (DevicePathSubType (FilePath
) != HW_MEMMAP_DP
)) {
291 FvFileNode
= NextDevicePathNode (FilePath
);
294 // Firstly find the FV file in current FV
296 gBS
->HandleProtocol (
298 &gEfiLoadedImageProtocolGuid
,
299 (VOID
**) &LoadedImage
301 NewDevicePath
= AppendDevicePathNode (DevicePathFromHandle (LoadedImage
->DeviceHandle
), FvFileNode
);
302 FileBuffer
= BmGetFileBufferByFvFilePath (NewDevicePath
, FullPath
, FileSize
);
303 FreePool (NewDevicePath
);
305 if (FileBuffer
!= NULL
) {
310 // Secondly find the FV file in all other FVs
312 gBS
->LocateHandleBuffer (
314 &gEfiFirmwareVolume2ProtocolGuid
,
319 for (Index
= 0; (Index
< FvHandleCount
) && (FileBuffer
== NULL
); Index
++) {
320 if (FvHandles
[Index
] == LoadedImage
->DeviceHandle
) {
326 NewDevicePath
= AppendDevicePathNode (DevicePathFromHandle (FvHandles
[Index
]), FvFileNode
);
327 FileBuffer
= BmGetFileBufferByFvFilePath (NewDevicePath
, FullPath
, FileSize
);
328 FreePool (NewDevicePath
);
331 if (FvHandles
!= NULL
) {
332 FreePool (FvHandles
);
338 Check if it's a Device Path pointing to FV file.
340 The function doesn't garentee the device path points to existing FV file.
342 @param DevicePath Input device path.
344 @retval TRUE The device path is a FV File Device Path.
345 @retval FALSE The device path is NOT a FV File Device Path.
349 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
354 EFI_DEVICE_PATH_PROTOCOL
*Node
;
357 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &Node
, &Handle
);
358 if (!EFI_ERROR (Status
)) {
362 if ((DevicePathType (DevicePath
) == HARDWARE_DEVICE_PATH
) && (DevicePathSubType (DevicePath
) == HW_MEMMAP_DP
)) {
363 DevicePath
= NextDevicePathNode (DevicePath
);
364 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) && (DevicePathSubType (DevicePath
) == MEDIA_PIWG_FW_FILE_DP
)) {
365 return IsDevicePathEnd (NextDevicePathNode (DevicePath
));
372 Check whether a USB device match the specified USB Class device path. This
373 function follows "Load Option Processing" behavior in UEFI specification.
375 @param UsbIo USB I/O protocol associated with the USB device.
376 @param UsbClass The USB Class device path to match.
378 @retval TRUE The USB device match the USB Class device path.
379 @retval FALSE The USB device does not match the USB Class device path.
384 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
385 IN USB_CLASS_DEVICE_PATH
*UsbClass
389 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
390 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
392 UINT8 DeviceSubClass
;
393 UINT8 DeviceProtocol
;
395 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
396 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
401 // Check Vendor Id and Product Id.
403 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
404 if (EFI_ERROR (Status
)) {
408 if ((UsbClass
->VendorId
!= 0xffff) &&
409 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
413 if ((UsbClass
->ProductId
!= 0xffff) &&
414 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
418 DeviceClass
= DevDesc
.DeviceClass
;
419 DeviceSubClass
= DevDesc
.DeviceSubClass
;
420 DeviceProtocol
= DevDesc
.DeviceProtocol
;
421 if (DeviceClass
== 0) {
423 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
424 // Protocol in Interface Descriptor instead.
426 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
427 if (EFI_ERROR (Status
)) {
431 DeviceClass
= IfDesc
.InterfaceClass
;
432 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
433 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
437 // Check Class, SubClass and Protocol.
439 if ((UsbClass
->DeviceClass
!= 0xff) &&
440 (UsbClass
->DeviceClass
!= DeviceClass
)) {
444 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
445 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
449 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
450 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
458 Eliminate the extra spaces in the Str to one space.
460 @param Str Input string info.
463 BmEliminateExtraSpaces (
470 for (Index
= 0, ActualIndex
= 0; Str
[Index
] != L
'\0'; Index
++) {
471 if ((Str
[Index
] != L
' ') || ((ActualIndex
> 0) && (Str
[ActualIndex
- 1] != L
' '))) {
472 Str
[ActualIndex
++] = Str
[Index
];
475 Str
[ActualIndex
] = L
'\0';
479 Try to get the controller's ATA/ATAPI description.
481 @param Handle Controller handle.
483 @return The description string.
486 BmGetDescriptionFromDiskInfo (
492 EFI_DISK_INFO_PROTOCOL
*DiskInfo
;
494 EFI_ATAPI_IDENTIFY_DATA IdentifyData
;
495 EFI_SCSI_INQUIRY_DATA InquiryData
;
498 CONST UINTN ModelNameLength
= 40;
499 CONST UINTN SerialNumberLength
= 20;
505 Status
= gBS
->HandleProtocol (
507 &gEfiDiskInfoProtocolGuid
,
510 if (EFI_ERROR (Status
)) {
514 if (CompareGuid (&DiskInfo
->Interface
, &gEfiDiskInfoAhciInterfaceGuid
) ||
515 CompareGuid (&DiskInfo
->Interface
, &gEfiDiskInfoIdeInterfaceGuid
)) {
516 BufferSize
= sizeof (EFI_ATAPI_IDENTIFY_DATA
);
517 Status
= DiskInfo
->Identify (
522 if (!EFI_ERROR (Status
)) {
523 Description
= AllocateZeroPool ((ModelNameLength
+ SerialNumberLength
+ 2) * sizeof (CHAR16
));
524 ASSERT (Description
!= NULL
);
525 for (Index
= 0; Index
+ 1 < ModelNameLength
; Index
+= 2) {
526 Description
[Index
] = (CHAR16
) IdentifyData
.ModelName
[Index
+ 1];
527 Description
[Index
+ 1] = (CHAR16
) IdentifyData
.ModelName
[Index
];
531 Description
[Length
++] = L
' ';
533 for (Index
= 0; Index
+ 1 < SerialNumberLength
; Index
+= 2) {
534 Description
[Length
+ Index
] = (CHAR16
) IdentifyData
.SerialNo
[Index
+ 1];
535 Description
[Length
+ Index
+ 1] = (CHAR16
) IdentifyData
.SerialNo
[Index
];
538 Description
[Length
++] = L
'\0';
539 ASSERT (Length
== ModelNameLength
+ SerialNumberLength
+ 2);
541 BmEliminateExtraSpaces (Description
);
543 } else if (CompareGuid (&DiskInfo
->Interface
, &gEfiDiskInfoScsiInterfaceGuid
)) {
544 BufferSize
= sizeof (EFI_SCSI_INQUIRY_DATA
);
545 Status
= DiskInfo
->Inquiry (
550 if (!EFI_ERROR (Status
)) {
551 Description
= AllocateZeroPool ((VENDOR_IDENTIFICATION_LENGTH
+ PRODUCT_IDENTIFICATION_LENGTH
+ 2) * sizeof (CHAR16
));
552 ASSERT (Description
!= NULL
);
555 // Per SCSI spec, EFI_SCSI_INQUIRY_DATA.Reserved_5_95[3 - 10] save the Verdor identification
556 // EFI_SCSI_INQUIRY_DATA.Reserved_5_95[11 - 26] save the product identification,
557 // Here combine the vendor identification and product identification to the description.
559 StrPtr
= (CHAR8
*) (&InquiryData
.Reserved_5_95
[VENDOR_IDENTIFICATION_OFFSET
]);
560 Temp
= StrPtr
[VENDOR_IDENTIFICATION_LENGTH
];
561 StrPtr
[VENDOR_IDENTIFICATION_LENGTH
] = '\0';
562 AsciiStrToUnicodeStr (StrPtr
, Description
);
563 StrPtr
[VENDOR_IDENTIFICATION_LENGTH
] = Temp
;
566 // Add one space at the middle of vendor information and product information.
568 Description
[VENDOR_IDENTIFICATION_LENGTH
] = L
' ';
570 StrPtr
= (CHAR8
*) (&InquiryData
.Reserved_5_95
[PRODUCT_IDENTIFICATION_OFFSET
]);
571 StrPtr
[PRODUCT_IDENTIFICATION_LENGTH
] = '\0';
572 AsciiStrToUnicodeStr (StrPtr
, Description
+ VENDOR_IDENTIFICATION_LENGTH
+ 1);
574 BmEliminateExtraSpaces (Description
);
582 Try to get the controller's USB description.
584 @param Handle Controller handle.
586 @return The description string.
589 BmGetUsbDescription (
594 EFI_USB_IO_PROTOCOL
*UsbIo
;
596 CHAR16
*Manufacturer
;
598 CHAR16
*SerialNumber
;
600 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
603 Status
= gBS
->HandleProtocol (
605 &gEfiUsbIoProtocolGuid
,
608 if (EFI_ERROR (Status
)) {
614 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
615 if (EFI_ERROR (Status
)) {
619 Status
= UsbIo
->UsbGetStringDescriptor (
622 DevDesc
.StrManufacturer
,
625 if (EFI_ERROR (Status
)) {
626 Manufacturer
= &NullChar
;
629 Status
= UsbIo
->UsbGetStringDescriptor (
635 if (EFI_ERROR (Status
)) {
639 Status
= UsbIo
->UsbGetStringDescriptor (
642 DevDesc
.StrSerialNumber
,
645 if (EFI_ERROR (Status
)) {
646 SerialNumber
= &NullChar
;
649 if ((Manufacturer
== &NullChar
) &&
650 (Product
== &NullChar
) &&
651 (SerialNumber
== &NullChar
)
656 DescMaxSize
= StrSize (Manufacturer
) + StrSize (Product
) + StrSize (SerialNumber
);
657 Description
= AllocateZeroPool (DescMaxSize
);
658 ASSERT (Description
!= NULL
);
659 StrCatS (Description
, DescMaxSize
/sizeof(CHAR16
), Manufacturer
);
660 StrCatS (Description
, DescMaxSize
/sizeof(CHAR16
), L
" ");
662 StrCatS (Description
, DescMaxSize
/sizeof(CHAR16
), Product
);
663 StrCatS (Description
, DescMaxSize
/sizeof(CHAR16
), L
" ");
665 StrCatS (Description
, DescMaxSize
/sizeof(CHAR16
), SerialNumber
);
667 if (Manufacturer
!= &NullChar
) {
668 FreePool (Manufacturer
);
670 if (Product
!= &NullChar
) {
673 if (SerialNumber
!= &NullChar
) {
674 FreePool (SerialNumber
);
677 BmEliminateExtraSpaces (Description
);
683 Return the boot description for the controller based on the type.
685 @param Handle Controller handle.
687 @return The description string.
690 BmGetMiscDescription (
696 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
697 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Fs
;
699 switch (BmDevicePathType (DevicePathFromHandle (Handle
))) {
700 case BmAcpiFloppyBoot
:
701 Description
= L
"Floppy";
704 case BmMessageAtapiBoot
:
705 case BmMessageSataBoot
:
706 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**) &BlockIo
);
707 ASSERT_EFI_ERROR (Status
);
709 // Assume a removable SATA device should be the DVD/CD device
711 Description
= BlockIo
->Media
->RemovableMedia
? L
"DVD/CDROM" : L
"Hard Drive";
714 case BmMessageUsbBoot
:
715 Description
= L
"USB Device";
718 case BmMessageScsiBoot
:
719 Description
= L
"SCSI Device";
722 case BmHardwareDeviceBoot
:
723 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**) &BlockIo
);
724 if (!EFI_ERROR (Status
)) {
725 Description
= BlockIo
->Media
->RemovableMedia
? L
"Removable Disk" : L
"Hard Drive";
727 Description
= L
"Misc Device";
731 case BmMessageNetworkBoot
:
732 Description
= L
"Network";
735 case BmMessageHttpBoot
:
736 Description
= L
"Http";
740 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**) &Fs
);
741 if (!EFI_ERROR (Status
)) {
742 Description
= L
"Non-Block Boot Device";
744 Description
= L
"Misc Device";
749 return AllocateCopyPool (StrSize (Description
), Description
);
753 Register the platform provided boot description handler.
755 @param Handler The platform provided boot description handler
757 @retval EFI_SUCCESS The handler was registered successfully.
758 @retval EFI_ALREADY_STARTED The handler was already registered.
759 @retval EFI_OUT_OF_RESOURCES There is not enough resource to perform the registration.
763 EfiBootManagerRegisterBootDescriptionHandler (
764 IN EFI_BOOT_MANAGER_BOOT_DESCRIPTION_HANDLER Handler
768 BM_BOOT_DESCRIPTION_ENTRY
*Entry
;
770 for ( Link
= GetFirstNode (&mPlatformBootDescriptionHandlers
)
771 ; !IsNull (&mPlatformBootDescriptionHandlers
, Link
)
772 ; Link
= GetNextNode (&mPlatformBootDescriptionHandlers
, Link
)
774 Entry
= CR (Link
, BM_BOOT_DESCRIPTION_ENTRY
, Link
, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE
);
775 if (Entry
->Handler
== Handler
) {
776 return EFI_ALREADY_STARTED
;
780 Entry
= AllocatePool (sizeof (BM_BOOT_DESCRIPTION_ENTRY
));
782 return EFI_OUT_OF_RESOURCES
;
785 Entry
->Signature
= BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE
;
786 Entry
->Handler
= Handler
;
787 InsertTailList (&mPlatformBootDescriptionHandlers
, &Entry
->Link
);
791 BM_GET_BOOT_DESCRIPTION mBmBootDescriptionHandlers
[] = {
793 BmGetDescriptionFromDiskInfo
,
798 Return the boot description for the controller.
800 @param Handle Controller handle.
802 @return The description string.
805 BmGetBootDescription (
810 BM_BOOT_DESCRIPTION_ENTRY
*Entry
;
812 CHAR16
*DefaultDescription
;
817 // Firstly get the default boot description
819 DefaultDescription
= NULL
;
820 for (Index
= 0; Index
< sizeof (mBmBootDescriptionHandlers
) / sizeof (mBmBootDescriptionHandlers
[0]); Index
++) {
821 DefaultDescription
= mBmBootDescriptionHandlers
[Index
] (Handle
);
822 if (DefaultDescription
!= NULL
) {
824 // Avoid description confusion between UEFI & Legacy boot option by adding "UEFI " prefix
825 // ONLY for core provided boot description handler.
827 Temp
= AllocatePool (StrSize (DefaultDescription
) + sizeof (mBmUefiPrefix
));
828 ASSERT (Temp
!= NULL
);
830 (StrSize (DefaultDescription
) + sizeof (mBmUefiPrefix
))/sizeof(CHAR16
),
834 (StrSize (DefaultDescription
) + sizeof (mBmUefiPrefix
))/sizeof(CHAR16
),
837 FreePool (DefaultDescription
);
838 DefaultDescription
= Temp
;
842 ASSERT (DefaultDescription
!= NULL
);
845 // Secondly query platform for the better boot description
847 for ( Link
= GetFirstNode (&mPlatformBootDescriptionHandlers
)
848 ; !IsNull (&mPlatformBootDescriptionHandlers
, Link
)
849 ; Link
= GetNextNode (&mPlatformBootDescriptionHandlers
, Link
)
851 Entry
= CR (Link
, BM_BOOT_DESCRIPTION_ENTRY
, Link
, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE
);
852 Description
= Entry
->Handler (Handle
, DefaultDescription
);
853 if (Description
!= NULL
) {
854 FreePool (DefaultDescription
);
859 return DefaultDescription
;
863 Check whether a USB device match the specified USB WWID device path. This
864 function follows "Load Option Processing" behavior in UEFI specification.
866 @param UsbIo USB I/O protocol associated with the USB device.
867 @param UsbWwid The USB WWID device path to match.
869 @retval TRUE The USB device match the USB WWID device path.
870 @retval FALSE The USB device does not match the USB WWID device path.
875 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
876 IN USB_WWID_DEVICE_PATH
*UsbWwid
880 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
881 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
887 CHAR16
*SerialNumberStr
;
890 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
891 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)) {
896 // Check Vendor Id and Product Id.
898 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
899 if (EFI_ERROR (Status
)) {
902 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
903 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
908 // Check Interface Number.
910 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
911 if (EFI_ERROR (Status
)) {
914 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
919 // Check Serial Number.
921 if (DevDesc
.StrSerialNumber
== 0) {
926 // Get all supported languages.
930 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
931 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
936 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
938 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
939 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
940 if (CompareStr
[CompareLen
- 1] == L
'\0') {
945 // Compare serial number in each supported language.
947 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
948 SerialNumberStr
= NULL
;
949 Status
= UsbIo
->UsbGetStringDescriptor (
952 DevDesc
.StrSerialNumber
,
955 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
959 Length
= StrLen (SerialNumberStr
);
960 if ((Length
>= CompareLen
) &&
961 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
962 FreePool (SerialNumberStr
);
966 FreePool (SerialNumberStr
);
973 Find a USB device which match the specified short-form device path start with
974 USB Class or USB WWID device path. If ParentDevicePath is NULL, this function
975 will search in all USB devices of the platform. If ParentDevicePath is not NULL,
976 this function will only search in its child devices.
978 @param DevicePath The device path that contains USB Class or USB WWID device path.
979 @param ParentDevicePathSize The length of the device path before the USB Class or
980 USB WWID device path.
981 @param UsbIoHandleCount A pointer to the count of the returned USB IO handles.
983 @retval NULL The matched USB IO handles cannot be found.
984 @retval other The matched USB IO handles.
989 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
990 IN UINTN ParentDevicePathSize
,
991 OUT UINTN
*UsbIoHandleCount
995 EFI_HANDLE
*UsbIoHandles
;
996 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
997 EFI_USB_IO_PROTOCOL
*UsbIo
;
1001 ASSERT (UsbIoHandleCount
!= NULL
);
1004 // Get all UsbIo Handles.
1006 Status
= gBS
->LocateHandleBuffer (
1008 &gEfiUsbIoProtocolGuid
,
1013 if (EFI_ERROR (Status
)) {
1014 *UsbIoHandleCount
= 0;
1015 UsbIoHandles
= NULL
;
1018 for (Index
= 0; Index
< *UsbIoHandleCount
; ) {
1020 // Get the Usb IO interface.
1022 Status
= gBS
->HandleProtocol(
1023 UsbIoHandles
[Index
],
1024 &gEfiUsbIoProtocolGuid
,
1027 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandles
[Index
]);
1029 if (!EFI_ERROR (Status
) && (UsbIoDevicePath
!= NULL
)) {
1032 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
1034 if (CompareMem (UsbIoDevicePath
, DevicePath
, ParentDevicePathSize
) == 0) {
1035 if (BmMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ((UINTN
) DevicePath
+ ParentDevicePathSize
)) ||
1036 BmMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ((UINTN
) DevicePath
+ ParentDevicePathSize
))) {
1043 (*UsbIoHandleCount
) --;
1044 CopyMem (&UsbIoHandles
[Index
], &UsbIoHandles
[Index
+ 1], (*UsbIoHandleCount
- Index
) * sizeof (EFI_HANDLE
));
1050 return UsbIoHandles
;
1054 Expand USB Class or USB WWID device path node to be full device path of a USB
1057 This function support following 4 cases:
1058 1) Boot Option device path starts with a USB Class or USB WWID device path,
1059 and there is no Media FilePath device path in the end.
1060 In this case, it will follow Removable Media Boot Behavior.
1061 2) Boot Option device path starts with a USB Class or USB WWID device path,
1062 and ended with Media FilePath device path.
1063 3) Boot Option device path starts with a full device path to a USB Host Controller,
1064 contains a USB Class or USB WWID device path node, while not ended with Media
1065 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
1066 4) Boot Option device path starts with a full device path to a USB Host Controller,
1067 contains a USB Class or USB WWID device path node, and ended with Media
1068 FilePath device path.
1070 @param FilePath The device path pointing to a load option.
1071 It could be a short-form device path.
1072 @param FullPath Return the full device path of the load option after
1073 short-form device path expanding.
1074 Caller is responsible to free it.
1075 @param FileSize Return the load option size.
1076 @param ShortformNode Pointer to the USB short-form device path node in the FilePath buffer.
1078 @return The load option buffer. Caller is responsible to free the memory.
1081 BmExpandUsbDevicePath (
1082 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1083 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1084 OUT UINTN
*FileSize
,
1085 IN EFI_DEVICE_PATH_PROTOCOL
*ShortformNode
1088 UINTN ParentDevicePathSize
;
1089 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1090 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1091 EFI_HANDLE
*Handles
;
1096 ParentDevicePathSize
= (UINTN
) ShortformNode
- (UINTN
) FilePath
;
1097 RemainingDevicePath
= NextDevicePathNode (ShortformNode
);
1099 Handles
= BmFindUsbDevice (FilePath
, ParentDevicePathSize
, &HandleCount
);
1101 for (Index
= 0; (Index
< HandleCount
) && (FileBuffer
== NULL
); Index
++) {
1102 FullDevicePath
= AppendDevicePath (DevicePathFromHandle (Handles
[Index
]), RemainingDevicePath
);
1103 FileBuffer
= BmGetLoadOptionBuffer (FullDevicePath
, FullPath
, FileSize
);
1104 FreePool (FullDevicePath
);
1107 if (Handles
!= NULL
) {
1115 Expand File-path device path node to be full device path in platform.
1117 @param FilePath The device path pointing to a load option.
1118 It could be a short-form device path.
1119 @param FullPath Return the full device path of the load option after
1120 short-form device path expanding.
1121 Caller is responsible to free it.
1122 @param FileSize Return the load option size.
1124 @return The load option buffer. Caller is responsible to free the memory.
1127 BmExpandFileDevicePath (
1128 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1129 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1136 EFI_HANDLE
*Handles
;
1137 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1139 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1141 UINT32 AuthenticationStatus
;
1143 EfiBootManagerConnectAll ();
1144 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
, NULL
, &HandleCount
, &Handles
);
1145 if (EFI_ERROR (Status
)) {
1151 // Enumerate all removable media devices followed by all fixed media devices,
1152 // followed by media devices which don't layer on block io.
1154 for (MediaType
= 0; MediaType
< 3; MediaType
++) {
1155 for (Index
= 0; Index
< HandleCount
; Index
++) {
1156 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiBlockIoProtocolGuid
, (VOID
*) &BlockIo
);
1157 if (EFI_ERROR (Status
)) {
1160 if ((MediaType
== 0 && BlockIo
!= NULL
&& BlockIo
->Media
->RemovableMedia
) ||
1161 (MediaType
== 1 && BlockIo
!= NULL
&& !BlockIo
->Media
->RemovableMedia
) ||
1162 (MediaType
== 2 && BlockIo
== NULL
)
1164 FullDevicePath
= AppendDevicePath (DevicePathFromHandle (Handles
[Index
]), FilePath
);
1165 FileBuffer
= GetFileBufferByFilePath (TRUE
, FullDevicePath
, FileSize
, &AuthenticationStatus
);
1166 if (FileBuffer
!= NULL
) {
1167 *FullPath
= FullDevicePath
;
1171 FreePool (FullDevicePath
);
1176 if (Handles
!= NULL
) {
1185 Expand URI device path node to be full device path in platform.
1187 @param FilePath The device path pointing to a load option.
1188 It could be a short-form device path.
1189 @param FullPath Return the full device path of the load option after
1190 short-form device path expanding.
1191 Caller is responsible to free it.
1192 @param FileSize Return the load option size.
1194 @return The load option buffer. Caller is responsible to free the memory.
1197 BmExpandUriDevicePath (
1198 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1199 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1206 EFI_HANDLE
*Handles
;
1207 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1210 EfiBootManagerConnectAll ();
1211 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiLoadFileProtocolGuid
, NULL
, &HandleCount
, &Handles
);
1212 if (EFI_ERROR (Status
)) {
1219 for (Index
= 0; Index
< HandleCount
; Index
++) {
1220 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiLoadFileProtocolGuid
, (VOID
*) &LoadFile
);
1221 ASSERT_EFI_ERROR (Status
);
1224 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, FileSize
, FileBuffer
);
1225 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1226 FileBuffer
= AllocatePool (*FileSize
);
1227 if (FileBuffer
== NULL
) {
1230 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, FileSize
, FileBuffer
);
1233 if (!EFI_ERROR (Status
)) {
1235 // LoadFile() returns a file buffer mapping to a file system.
1237 if (Status
== EFI_WARN_FILE_SYSTEM
) {
1238 return BmGetFileBufferFromLoadFileFileSystem (Handles
[Index
], FullPath
, FileSize
);
1241 ASSERT (Status
== EFI_SUCCESS
);
1242 *FullPath
= DuplicateDevicePath (DevicePathFromHandle (Handles
[Index
]));
1246 if (FileBuffer
!= NULL
) {
1247 FreePool (FileBuffer
);
1251 if (Handles
!= NULL
) {
1259 Save the partition DevicePath to the CachedDevicePath as the first instance.
1261 @param CachedDevicePath The device path cache.
1262 @param DevicePath The partition device path to be cached.
1265 BmCachePartitionDevicePath (
1266 IN OUT EFI_DEVICE_PATH_PROTOCOL
**CachedDevicePath
,
1267 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1270 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1273 if (BmMatchDevicePaths (*CachedDevicePath
, DevicePath
)) {
1274 TempDevicePath
= *CachedDevicePath
;
1275 *CachedDevicePath
= BmDelPartMatchInstance (*CachedDevicePath
, DevicePath
);
1276 FreePool (TempDevicePath
);
1279 if (*CachedDevicePath
== NULL
) {
1280 *CachedDevicePath
= DuplicateDevicePath (DevicePath
);
1284 TempDevicePath
= *CachedDevicePath
;
1285 *CachedDevicePath
= AppendDevicePathInstance (DevicePath
, *CachedDevicePath
);
1286 if (TempDevicePath
!= NULL
) {
1287 FreePool (TempDevicePath
);
1291 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
1292 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
1295 TempDevicePath
= *CachedDevicePath
;
1296 while (!IsDevicePathEnd (TempDevicePath
)) {
1297 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1299 // Parse one instance
1301 while (!IsDevicePathEndType (TempDevicePath
)) {
1302 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1306 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
1309 SetDevicePathEndNode (TempDevicePath
);
1316 Expand a device path that starts with a hard drive media device path node to be a
1317 full device path that includes the full hardware path to the device. We need
1318 to do this so it can be booted. As an optimization the front match (the part point
1319 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
1320 so a connect all is not required on every boot. All successful history device path
1321 which point to partition node (the front part) will be saved.
1323 @param FilePath The device path pointing to a load option.
1324 It could be a short-form device path.
1325 @param FullPath Return the full device path of the load option after
1326 short-form device path expanding.
1327 Caller is responsible to free it.
1328 @param FileSize Return the load option size.
1330 @return The load option buffer. Caller is responsible to free the memory.
1333 BmExpandPartitionDevicePath (
1334 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1335 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1340 UINTN BlockIoHandleCount
;
1341 EFI_HANDLE
*BlockIoBuffer
;
1343 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
1345 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
1346 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
1347 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1348 UINTN CachedDevicePathSize
;
1350 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1355 // Check if there is prestore 'HDDP' variable.
1356 // If exist, search the front path which point to partition node in the variable instants.
1357 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
1359 GetVariable2 (L
"HDDP", &mBmHardDriveBootVariableGuid
, (VOID
**) &CachedDevicePath
, &CachedDevicePathSize
);
1362 // Delete the invalid 'HDDP' variable.
1364 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
1365 FreePool (CachedDevicePath
);
1366 CachedDevicePath
= NULL
;
1367 Status
= gRT
->SetVariable (
1369 &mBmHardDriveBootVariableGuid
,
1374 ASSERT_EFI_ERROR (Status
);
1377 if (CachedDevicePath
!= NULL
) {
1378 TempNewDevicePath
= CachedDevicePath
;
1382 // Check every instance of the variable
1383 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
1384 // partial partition boot option. Second, check whether the instance could be connected.
1386 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
1387 if (BmMatchPartitionDevicePathNode (Instance
, (HARDDRIVE_DEVICE_PATH
*) FilePath
)) {
1389 // Connect the device path instance, the device path point to hard drive media device path node
1390 // e.g. ACPI() /PCI()/ATA()/Partition()
1392 Status
= EfiBootManagerConnectDevicePath (Instance
, NULL
);
1393 if (!EFI_ERROR (Status
)) {
1394 TempDevicePath
= AppendDevicePath (Instance
, NextDevicePathNode (FilePath
));
1395 FileBuffer
= BmGetLoadOptionBuffer (TempDevicePath
, FullPath
, FileSize
);
1396 FreePool (TempDevicePath
);
1398 if (FileBuffer
!= NULL
) {
1400 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
1403 BmCachePartitionDevicePath (&CachedDevicePath
, Instance
);
1405 // Save the matching Device Path so we don't need to do a connect all next time
1406 // Failing to save only impacts performance next time expanding the short-form device path
1408 Status
= gRT
->SetVariable (
1410 &mBmHardDriveBootVariableGuid
,
1411 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1412 GetDevicePathSize (CachedDevicePath
),
1417 FreePool (Instance
);
1418 FreePool (CachedDevicePath
);
1424 // Come here means the first instance is not matched
1428 } while (TempNewDevicePath
!= NULL
);
1432 // If we get here we fail to find or 'HDDP' not exist, and now we need
1433 // to search all devices in the system for a matched partition
1435 EfiBootManagerConnectAll ();
1436 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
1437 if (EFI_ERROR (Status
)) {
1438 BlockIoHandleCount
= 0;
1439 BlockIoBuffer
= NULL
;
1442 // Loop through all the device handles that support the BLOCK_IO Protocol
1444 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
1445 BlockIoDevicePath
= DevicePathFromHandle (BlockIoBuffer
[Index
]);
1446 if (BlockIoDevicePath
== NULL
) {
1450 if (BmMatchPartitionDevicePathNode (BlockIoDevicePath
, (HARDDRIVE_DEVICE_PATH
*) FilePath
)) {
1452 // Find the matched partition device path
1454 TempDevicePath
= AppendDevicePath (BlockIoDevicePath
, NextDevicePathNode (FilePath
));
1455 FileBuffer
= BmGetLoadOptionBuffer (TempDevicePath
, FullPath
, FileSize
);
1456 FreePool (TempDevicePath
);
1458 if (FileBuffer
!= NULL
) {
1459 BmCachePartitionDevicePath (&CachedDevicePath
, BlockIoDevicePath
);
1462 // Save the matching Device Path so we don't need to do a connect all next time
1463 // Failing to save only impacts performance next time expanding the short-form device path
1465 Status
= gRT
->SetVariable (
1467 &mBmHardDriveBootVariableGuid
,
1468 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1469 GetDevicePathSize (CachedDevicePath
),
1478 if (CachedDevicePath
!= NULL
) {
1479 FreePool (CachedDevicePath
);
1481 if (BlockIoBuffer
!= NULL
) {
1482 FreePool (BlockIoBuffer
);
1488 Expand the media device path which points to a BlockIo or SimpleFileSystem instance
1489 by appending EFI_REMOVABLE_MEDIA_FILE_NAME.
1491 @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance.
1492 @param FullPath Return the full device path pointing to the load option.
1493 @param FileSize Return the size of the load option.
1495 @return The load option buffer.
1498 BmExpandMediaDevicePath (
1499 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1500 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1506 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1508 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1511 EFI_HANDLE
*SimpleFileSystemHandles
;
1512 UINTN NumberSimpleFileSystemHandles
;
1515 UINT32 AuthenticationStatus
;
1518 // Check whether the device is connected
1520 TempDevicePath
= DevicePath
;
1521 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
1522 if (!EFI_ERROR (Status
)) {
1523 ASSERT (IsDevicePathEnd (TempDevicePath
));
1525 TempDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
1526 FileBuffer
= GetFileBufferByFilePath (TRUE
, TempDevicePath
, FileSize
, &AuthenticationStatus
);
1527 if (FileBuffer
== NULL
) {
1528 FreePool (TempDevicePath
);
1529 TempDevicePath
= NULL
;
1531 *FullPath
= TempDevicePath
;
1536 // For device boot option only pointing to the removable device handle,
1537 // should make sure all its children handles (its child partion or media handles) are created and connected.
1539 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
1542 // Issue a dummy read to the device to check for media change.
1543 // When the removable media is changed, any Block IO read/write will
1544 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1545 // returned. After the Block IO protocol is reinstalled, subsequent
1546 // Block IO read/write will success.
1548 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
1549 ASSERT_EFI_ERROR (Status
);
1550 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**) &BlockIo
);
1551 ASSERT_EFI_ERROR (Status
);
1552 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
1553 if (Buffer
!= NULL
) {
1554 BlockIo
->ReadBlocks (
1556 BlockIo
->Media
->MediaId
,
1558 BlockIo
->Media
->BlockSize
,
1565 // Detect the the default boot file from removable Media
1569 Size
= GetDevicePathSize (DevicePath
) - END_DEVICE_PATH_LENGTH
;
1570 gBS
->LocateHandleBuffer (
1572 &gEfiSimpleFileSystemProtocolGuid
,
1574 &NumberSimpleFileSystemHandles
,
1575 &SimpleFileSystemHandles
1577 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
1579 // Get the device path size of SimpleFileSystem handle
1581 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
1582 TempSize
= GetDevicePathSize (TempDevicePath
) - END_DEVICE_PATH_LENGTH
;
1584 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1586 if ((Size
<= TempSize
) && (CompareMem (TempDevicePath
, DevicePath
, Size
) == 0)) {
1587 TempDevicePath
= FileDevicePath (SimpleFileSystemHandles
[Index
], EFI_REMOVABLE_MEDIA_FILE_NAME
);
1588 FileBuffer
= GetFileBufferByFilePath (TRUE
, TempDevicePath
, FileSize
, &AuthenticationStatus
);
1589 if (FileBuffer
!= NULL
) {
1590 *FullPath
= TempDevicePath
;
1593 FreePool (TempDevicePath
);
1597 if (SimpleFileSystemHandles
!= NULL
) {
1598 FreePool (SimpleFileSystemHandles
);
1605 Check whether Left and Right are the same without matching the specific
1606 device path data in IP device path and URI device path node.
1608 @retval TRUE Left and Right are the same.
1609 @retval FALSE Left and Right are the different.
1612 BmMatchHttpBootDevicePath (
1613 IN EFI_DEVICE_PATH_PROTOCOL
*Left
,
1614 IN EFI_DEVICE_PATH_PROTOCOL
*Right
1617 for (; !IsDevicePathEnd (Left
) && !IsDevicePathEnd (Right
)
1618 ; Left
= NextDevicePathNode (Left
), Right
= NextDevicePathNode (Right
)
1620 if (CompareMem (Left
, Right
, DevicePathNodeLength (Left
)) != 0) {
1621 if ((DevicePathType (Left
) != MESSAGING_DEVICE_PATH
) || (DevicePathType (Right
) != MESSAGING_DEVICE_PATH
)) {
1625 if (((DevicePathSubType (Left
) != MSG_IPv4_DP
) || (DevicePathSubType (Right
) != MSG_IPv4_DP
)) &&
1626 ((DevicePathSubType (Left
) != MSG_IPv6_DP
) || (DevicePathSubType (Right
) != MSG_IPv6_DP
)) &&
1627 ((DevicePathSubType (Left
) != MSG_URI_DP
) || (DevicePathSubType (Right
) != MSG_URI_DP
))
1633 return (BOOLEAN
) (IsDevicePathEnd (Left
) && IsDevicePathEnd (Right
));
1637 Get the file buffer from the file system produced by Load File instance.
1639 @param LoadFileHandle The handle of LoadFile instance.
1640 @param FullPath Return the full device path pointing to the load option.
1641 @param FileSize Return the size of the load option.
1643 @return The load option buffer.
1646 BmGetFileBufferFromLoadFileFileSystem (
1647 IN EFI_HANDLE LoadFileHandle
,
1648 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1654 EFI_HANDLE
*Handles
;
1657 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1659 Status
= gBS
->LocateHandleBuffer (
1661 &gEfiBlockIoProtocolGuid
,
1666 if (EFI_ERROR (Status
)) {
1670 for (Index
= 0; Index
< HandleCount
; Index
++) {
1671 Node
= DevicePathFromHandle (Handles
[Index
]);
1672 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &Node
, &Handle
);
1673 if (!EFI_ERROR (Status
) &&
1674 (Handle
== LoadFileHandle
) &&
1675 (DevicePathType (Node
) == MEDIA_DEVICE_PATH
) && (DevicePathSubType (Node
) == MEDIA_RAM_DISK_DP
)) {
1676 Handle
= Handles
[Index
];
1681 if (Handles
!= NULL
) {
1685 if (Index
!= HandleCount
) {
1686 return BmExpandMediaDevicePath (DevicePathFromHandle (Handle
), FullPath
, FileSize
);
1693 Get the file buffer from Load File instance.
1695 @param FilePath The media device path pointing to a LoadFile instance.
1696 @param FullPath Return the full device path pointing to the load option.
1697 @param FileSize Return the size of the load option.
1699 @return The load option buffer.
1702 BmGetFileBufferFromLoadFile (
1703 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1704 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1711 EFI_HANDLE
*Handles
;
1714 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1715 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
1719 // Get file buffer from load file instance.
1722 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &Node
, &Handle
);
1723 if (!EFI_ERROR (Status
) && IsDevicePathEnd (Node
)) {
1725 // When wide match happens, pass full device path to LoadFile (),
1726 // otherwise, pass remaining device path to LoadFile ().
1732 // Use wide match algorithm to find one when
1733 // cannot find a LoadFile instance to exactly match the FilePath
1735 Status
= gBS
->LocateHandleBuffer (
1737 &gEfiLoadFileProtocolGuid
,
1742 if (EFI_ERROR (Status
)) {
1746 for (Index
= 0; Index
< HandleCount
; Index
++) {
1747 if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles
[Index
]), FilePath
)) {
1748 Handle
= Handles
[Index
];
1752 if (Handles
!= NULL
) {
1757 if (Handle
== NULL
) {
1761 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**) &LoadFile
);
1762 ASSERT_EFI_ERROR (Status
);
1766 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, &BufferSize
, FileBuffer
);
1767 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1768 FileBuffer
= AllocatePool (BufferSize
);
1769 if (FileBuffer
!= NULL
) {
1770 Status
= EFI_SUCCESS
;
1774 if (!EFI_ERROR (Status
)) {
1775 Status
= LoadFile
->LoadFile (LoadFile
, FilePath
, TRUE
, &BufferSize
, FileBuffer
);
1778 if (!EFI_ERROR (Status
)) {
1780 // LoadFile() returns a file buffer mapping to a file system.
1782 if (Status
== EFI_WARN_FILE_SYSTEM
) {
1783 return BmGetFileBufferFromLoadFileFileSystem (Handle
, FullPath
, FileSize
);
1786 ASSERT (Status
== EFI_SUCCESS
);
1788 // LoadFile () may cause the device path of the Handle be updated.
1790 *FullPath
= DuplicateDevicePath (DevicePathFromHandle (Handle
));
1791 *FileSize
= BufferSize
;
1799 Get the load option by its device path.
1801 @param FilePath The device path pointing to a load option.
1802 It could be a short-form device path.
1803 @param FullPath Return the full device path of the load option after
1804 short-form device path expanding.
1805 Caller is responsible to free it.
1806 @param FileSize Return the load option size.
1808 @return The load option buffer. Caller is responsible to free the memory.
1811 BmGetLoadOptionBuffer (
1812 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1813 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1819 UINT32 AuthenticationStatus
;
1820 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1823 ASSERT ((FilePath
!= NULL
) && (FullPath
!= NULL
) && (FileSize
!= NULL
));
1825 EfiBootManagerConnectDevicePath (FilePath
, NULL
);
1832 // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI
1835 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &Node
, &Handle
);
1836 if (EFI_ERROR (Status
)) {
1837 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &Node
, &Handle
);
1840 if (!EFI_ERROR (Status
) && IsDevicePathEnd (Node
)) {
1841 return BmExpandMediaDevicePath (FilePath
, FullPath
, FileSize
);
1845 // Expand the short-form device path to full device path
1847 if ((DevicePathType (FilePath
) == MEDIA_DEVICE_PATH
) &&
1848 (DevicePathSubType (FilePath
) == MEDIA_HARDDRIVE_DP
)) {
1850 // Expand the Harddrive device path
1852 return BmExpandPartitionDevicePath (FilePath
, FullPath
, FileSize
);
1853 } else if ((DevicePathType (FilePath
) == MEDIA_DEVICE_PATH
) &&
1854 (DevicePathSubType (FilePath
) == MEDIA_FILEPATH_DP
)) {
1856 // Expand the File-path device path
1858 return BmExpandFileDevicePath (FilePath
, FullPath
, FileSize
);
1859 } else if ((DevicePathType (FilePath
) == MESSAGING_DEVICE_PATH
) &&
1860 (DevicePathSubType (FilePath
) == MSG_URI_DP
)) {
1862 // Expand the URI device path
1864 return BmExpandUriDevicePath (FilePath
, FullPath
, FileSize
);
1866 for (Node
= FilePath
; !IsDevicePathEnd (Node
); Node
= NextDevicePathNode (Node
)) {
1867 if ((DevicePathType (Node
) == MESSAGING_DEVICE_PATH
) &&
1868 ((DevicePathSubType (Node
) == MSG_USB_CLASS_DP
) || (DevicePathSubType (Node
) == MSG_USB_WWID_DP
))) {
1873 if (!IsDevicePathEnd (Node
)) {
1875 // Expand the USB WWID/Class device path
1877 FileBuffer
= BmExpandUsbDevicePath (FilePath
, FullPath
, FileSize
, Node
);
1878 if ((FileBuffer
== NULL
) && (FilePath
== Node
)) {
1880 // Boot Option device path starts with USB Class or USB WWID device path.
1881 // For Boot Option device path which doesn't begin with the USB Class or
1882 // USB WWID device path, it's not needed to connect again here.
1884 BmConnectUsbShortFormDevicePath (FilePath
);
1885 FileBuffer
= BmExpandUsbDevicePath (FilePath
, FullPath
, FileSize
, Node
);
1892 // Get file buffer from FV file path.
1894 if (BmIsFvFilePath (FilePath
)) {
1895 return BmGetFileBufferByFvFilePath (FilePath
, FullPath
, FileSize
);
1899 // Get file buffer from simple file system.
1902 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &Node
, &Handle
);
1903 if (!EFI_ERROR (Status
)) {
1904 FileBuffer
= GetFileBufferByFilePath (TRUE
, FilePath
, FileSize
, &AuthenticationStatus
);
1905 if (FileBuffer
!= NULL
) {
1906 *FullPath
= DuplicateDevicePath (FilePath
);
1911 return BmGetFileBufferFromLoadFile (FilePath
, FullPath
, FileSize
);
1915 Attempt to boot the EFI boot option. This routine sets L"BootCurent" and
1916 also signals the EFI ready to boot event. If the device path for the option
1917 starts with a BBS device path a legacy boot is attempted via the registered
1918 gLegacyBoot function. Short form device paths are also supported via this
1919 rountine. A device path starting with MEDIA_HARDDRIVE_DP, MSG_USB_WWID_DP,
1920 MSG_USB_CLASS_DP gets expaned out to find the first device that matches.
1921 If the BootOption Device Path fails the removable media boot algorithm
1922 is attempted (\EFI\BOOTIA32.EFI, \EFI\BOOTX64.EFI,... only one file type
1923 is tried per processor type)
1925 @param BootOption Boot Option to try and boot.
1926 On return, BootOption->Status contains the boot status.
1927 EFI_SUCCESS BootOption was booted
1928 EFI_UNSUPPORTED A BBS device path was found with no valid callback
1929 registered via EfiBootManagerInitialize().
1930 EFI_NOT_FOUND The BootOption was not found on the system
1931 !EFI_SUCCESS BootOption failed with this error status
1936 EfiBootManagerBoot (
1937 IN EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
1941 EFI_HANDLE ImageHandle
;
1942 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
1945 UINTN OriginalOptionNumber
;
1946 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
1947 EFI_DEVICE_PATH_PROTOCOL
*Node
;
1948 EFI_HANDLE FvHandle
;
1951 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
1952 EFI_EVENT LegacyBootEvent
;
1954 if (BootOption
== NULL
) {
1958 if (BootOption
->FilePath
== NULL
|| BootOption
->OptionType
!= LoadOptionTypeBoot
) {
1959 BootOption
->Status
= EFI_INVALID_PARAMETER
;
1964 // 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")
1966 OptionNumber
= BmFindBootOptionInVariable (BootOption
);
1967 if (OptionNumber
== LoadOptionNumberUnassigned
) {
1968 Status
= BmGetFreeOptionNumber (LoadOptionTypeBoot
, &Uint16
);
1969 if (!EFI_ERROR (Status
)) {
1971 // Save the BootOption->OptionNumber to restore later
1973 OptionNumber
= Uint16
;
1974 OriginalOptionNumber
= BootOption
->OptionNumber
;
1975 BootOption
->OptionNumber
= OptionNumber
;
1976 Status
= EfiBootManagerLoadOptionToVariable (BootOption
);
1977 BootOption
->OptionNumber
= OriginalOptionNumber
;
1980 if (EFI_ERROR (Status
)) {
1981 DEBUG ((EFI_D_ERROR
, "[Bds] Failed to create Boot#### for a temporary boot - %r!\n", Status
));
1982 BootOption
->Status
= Status
;
1988 // 2. Set BootCurrent
1990 Uint16
= (UINT16
) OptionNumber
;
1991 BmSetVariableAndReportStatusCodeOnError (
1993 &gEfiGlobalVariableGuid
,
1994 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2000 // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute
2003 Node
= BootOption
->FilePath
;
2004 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &Node
, &FvHandle
);
2005 if (!EFI_ERROR (Status
) && CompareGuid (
2006 EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) Node
),
2007 PcdGetPtr (PcdBootManagerMenuFile
)
2009 DEBUG ((EFI_D_INFO
, "[Bds] Booting Boot Manager Menu.\n"));
2010 BmStopHotkeyService (NULL
, NULL
);
2012 EfiSignalEventReadyToBoot();
2014 // Report Status Code to indicate ReadyToBoot was signalled
2016 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2018 // 4. Repair system through DriverHealth protocol
2020 BmRepairAllControllers ();
2023 PERF_START_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
2026 // 5. Load EFI boot option to ImageHandle
2029 if (DevicePathType (BootOption
->FilePath
) != BBS_DEVICE_PATH
) {
2030 Status
= EFI_NOT_FOUND
;
2031 FileBuffer
= BmGetLoadOptionBuffer (BootOption
->FilePath
, &FilePath
, &FileSize
);
2033 if (FileBuffer
!= NULL
&& CompareMem (BootOption
->FilePath
, FilePath
, GetDevicePathSize (FilePath
)) != 0) {
2034 DEBUG ((EFI_D_INFO
, "[Bds] DevicePath expand: "));
2035 BmPrintDp (BootOption
->FilePath
);
2036 DEBUG ((EFI_D_INFO
, " -> "));
2037 BmPrintDp (FilePath
);
2038 DEBUG ((EFI_D_INFO
, "\n"));
2041 if (BmIsLoadOptionPeHeaderValid (BootOption
->OptionType
, FileBuffer
, FileSize
)) {
2042 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2043 Status
= gBS
->LoadImage (
2052 if (FileBuffer
!= NULL
) {
2053 FreePool (FileBuffer
);
2055 if (FilePath
!= NULL
) {
2056 FreePool (FilePath
);
2059 if (EFI_ERROR (Status
)) {
2061 // Report Status Code to indicate that the failure to load boot option
2063 REPORT_STATUS_CODE (
2064 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2065 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2067 BootOption
->Status
= Status
;
2073 // 6. Adjust the different type memory page number just before booting
2074 // and save the updated info into the variable for next boot to use
2076 BmSetMemoryTypeInformationVariable (
2077 (BOOLEAN
) ((BootOption
->Attributes
& LOAD_OPTION_CATEGORY
) == LOAD_OPTION_CATEGORY_BOOT
)
2081 if (BootOption
->Description
== NULL
) {
2082 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "[Bds]Booting from unknown device path\n"));
2084 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "[Bds]Booting %s\n", BootOption
->Description
));
2089 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2090 // Write boot to OS performance data for Legacy boot
2092 if ((DevicePathType (BootOption
->FilePath
) == BBS_DEVICE_PATH
) && (DevicePathSubType (BootOption
->FilePath
) == BBS_BBS_DP
)) {
2093 if (mBmLegacyBoot
!= NULL
) {
2095 // Write boot to OS performance data for legacy boot.
2099 // Create an event to be signalled when Legacy Boot occurs to write performance data.
2101 Status
= EfiCreateEventLegacyBootEx(
2103 BmWriteBootToOsPerformanceData
,
2107 ASSERT_EFI_ERROR (Status
);
2110 mBmLegacyBoot (BootOption
);
2112 BootOption
->Status
= EFI_UNSUPPORTED
;
2115 PERF_END_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
2120 // Provide the image with its load options
2122 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2123 ASSERT_EFI_ERROR (Status
);
2125 if (!BmIsAutoCreateBootOption (BootOption
)) {
2126 ImageInfo
->LoadOptionsSize
= BootOption
->OptionalDataSize
;
2127 ImageInfo
->LoadOptions
= BootOption
->OptionalData
;
2131 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2133 ImageInfo
->ParentHandle
= NULL
;
2136 // Before calling the image, enable the Watchdog Timer for 5 minutes period
2138 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2141 // Write boot to OS performance data for UEFI boot
2144 BmWriteBootToOsPerformanceData (NULL
, NULL
);
2147 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2149 Status
= gBS
->StartImage (ImageHandle
, &BootOption
->ExitDataSize
, &BootOption
->ExitData
);
2150 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2151 BootOption
->Status
= Status
;
2152 if (EFI_ERROR (Status
)) {
2154 // Report Status Code to indicate that boot failure
2156 REPORT_STATUS_CODE (
2157 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2158 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2161 PERF_END_EX (gImageHandle
, "BdsAttempt", NULL
, 0, (UINT32
) OptionNumber
);
2164 // Clear the Watchdog Timer after the image returns
2166 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2169 // Set Logo status invalid after trying one boot option
2172 Status
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2173 if (!EFI_ERROR (Status
) && (BootLogo
!= NULL
)) {
2174 Status
= BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2175 ASSERT_EFI_ERROR (Status
);
2179 // Clear Boot Current
2181 Status
= gRT
->SetVariable (
2183 &gEfiGlobalVariableGuid
,
2189 // Deleting variable with current variable implementation shouldn't fail.
2190 // When BootXXXX (e.g.: BootManagerMenu) boots BootYYYY, exiting BootYYYY causes BootCurrent deleted,
2191 // exiting BootXXXX causes deleting BootCurrent returns EFI_NOT_FOUND.
2193 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_FOUND
);
2197 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2198 instances, has the same partition node with HardDriveDevicePath device path
2200 @param BlockIoDevicePath Multi device path instances which need to check
2201 @param HardDriveDevicePath A device path which starts with a hard drive media
2204 @retval TRUE There is a matched device path instance.
2205 @retval FALSE There is no matched device path instance.
2209 BmMatchPartitionDevicePathNode (
2210 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2211 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2214 HARDDRIVE_DEVICE_PATH
*Node
;
2216 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2221 // find the partition device path node
2223 while (!IsDevicePathEnd (BlockIoDevicePath
)) {
2224 if ((DevicePathType (BlockIoDevicePath
) == MEDIA_DEVICE_PATH
) &&
2225 (DevicePathSubType (BlockIoDevicePath
) == MEDIA_HARDDRIVE_DP
)
2230 BlockIoDevicePath
= NextDevicePathNode (BlockIoDevicePath
);
2233 if (IsDevicePathEnd (BlockIoDevicePath
)) {
2238 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2240 Node
= (HARDDRIVE_DEVICE_PATH
*) BlockIoDevicePath
;
2243 // Match Signature and PartitionNumber.
2244 // Unused bytes in Signature are initiaized with zeros.
2247 (Node
->PartitionNumber
== HardDriveDevicePath
->PartitionNumber
) &&
2248 (Node
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2249 (Node
->SignatureType
== HardDriveDevicePath
->SignatureType
) &&
2250 (CompareMem (Node
->Signature
, HardDriveDevicePath
->Signature
, sizeof (Node
->Signature
)) == 0)
2255 Enumerate all boot option descriptions and append " 2"/" 3"/... to make
2258 @param BootOptions Array of boot options.
2259 @param BootOptionCount Count of boot options.
2262 BmMakeBootOptionDescriptionUnique (
2263 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
,
2264 UINTN BootOptionCount
2269 UINTN DescriptionSize
;
2270 UINTN MaxSuffixSize
;
2274 if (BootOptionCount
== 0) {
2279 // Calculate the maximum buffer size for the number suffix.
2280 // The initial sizeof (CHAR16) is for the blank space before the number.
2282 MaxSuffixSize
= sizeof (CHAR16
);
2283 for (Index
= BootOptionCount
; Index
!= 0; Index
= Index
/ 10) {
2284 MaxSuffixSize
+= sizeof (CHAR16
);
2287 Visited
= AllocateZeroPool (sizeof (BOOLEAN
) * BootOptionCount
);
2288 ASSERT (Visited
!= NULL
);
2290 for (Base
= 0; Base
< BootOptionCount
; Base
++) {
2291 if (!Visited
[Base
]) {
2293 Visited
[Base
] = TRUE
;
2294 DescriptionSize
= StrSize (BootOptions
[Base
].Description
);
2295 for (Index
= Base
+ 1; Index
< BootOptionCount
; Index
++) {
2296 if (!Visited
[Index
] && StrCmp (BootOptions
[Base
].Description
, BootOptions
[Index
].Description
) == 0) {
2297 Visited
[Index
] = TRUE
;
2299 FreePool (BootOptions
[Index
].Description
);
2300 BootOptions
[Index
].Description
= AllocatePool (DescriptionSize
+ MaxSuffixSize
);
2302 BootOptions
[Index
].Description
, DescriptionSize
+ MaxSuffixSize
,
2304 BootOptions
[Base
].Description
, MatchCount
2315 Emuerate all possible bootable medias in the following order:
2316 1. Removable BlockIo - The boot option only points to the removable media
2317 device, like USB key, DVD, Floppy etc.
2318 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
2320 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting
2321 SimpleFileSystem Protocol, but not supporting BlockIo
2323 4. LoadFile - The boot option points to the media supporting
2325 Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior
2327 @param BootOptionCount Return the boot option count which has been found.
2329 @retval Pointer to the boot option array.
2331 EFI_BOOT_MANAGER_LOAD_OPTION
*
2332 BmEnumerateBootOptions (
2333 UINTN
*BootOptionCount
2337 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2339 EFI_HANDLE
*Handles
;
2340 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
2343 CHAR16
*Description
;
2345 ASSERT (BootOptionCount
!= NULL
);
2347 *BootOptionCount
= 0;
2351 // Parse removable block io followed by fixed block io
2353 gBS
->LocateHandleBuffer (
2355 &gEfiBlockIoProtocolGuid
,
2361 for (Removable
= 0; Removable
< 2; Removable
++) {
2362 for (Index
= 0; Index
< HandleCount
; Index
++) {
2363 Status
= gBS
->HandleProtocol (
2365 &gEfiBlockIoProtocolGuid
,
2368 if (EFI_ERROR (Status
)) {
2373 // Skip the logical partitions
2375 if (BlkIo
->Media
->LogicalPartition
) {
2380 // Skip the fixed block io then the removable block io
2382 if (BlkIo
->Media
->RemovableMedia
== ((Removable
== 0) ? FALSE
: TRUE
)) {
2386 Description
= BmGetBootDescription (Handles
[Index
]);
2387 BootOptions
= ReallocatePool (
2388 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2389 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2392 ASSERT (BootOptions
!= NULL
);
2394 Status
= EfiBootManagerInitializeLoadOption (
2395 &BootOptions
[(*BootOptionCount
)++],
2396 LoadOptionNumberUnassigned
,
2400 DevicePathFromHandle (Handles
[Index
]),
2404 ASSERT_EFI_ERROR (Status
);
2406 FreePool (Description
);
2410 if (HandleCount
!= 0) {
2415 // Parse simple file system not based on block io
2417 gBS
->LocateHandleBuffer (
2419 &gEfiSimpleFileSystemProtocolGuid
,
2424 for (Index
= 0; Index
< HandleCount
; Index
++) {
2425 Status
= gBS
->HandleProtocol (
2427 &gEfiBlockIoProtocolGuid
,
2430 if (!EFI_ERROR (Status
)) {
2432 // Skip if the file system handle supports a BlkIo protocol, which we've handled in above
2436 Description
= BmGetBootDescription (Handles
[Index
]);
2437 BootOptions
= ReallocatePool (
2438 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2439 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2442 ASSERT (BootOptions
!= NULL
);
2444 Status
= EfiBootManagerInitializeLoadOption (
2445 &BootOptions
[(*BootOptionCount
)++],
2446 LoadOptionNumberUnassigned
,
2450 DevicePathFromHandle (Handles
[Index
]),
2454 ASSERT_EFI_ERROR (Status
);
2455 FreePool (Description
);
2458 if (HandleCount
!= 0) {
2463 // Parse load file, assuming UEFI Network boot option
2465 gBS
->LocateHandleBuffer (
2467 &gEfiLoadFileProtocolGuid
,
2472 for (Index
= 0; Index
< HandleCount
; Index
++) {
2474 Description
= BmGetBootDescription (Handles
[Index
]);
2475 BootOptions
= ReallocatePool (
2476 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
),
2477 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
) * (*BootOptionCount
+ 1),
2480 ASSERT (BootOptions
!= NULL
);
2482 Status
= EfiBootManagerInitializeLoadOption (
2483 &BootOptions
[(*BootOptionCount
)++],
2484 LoadOptionNumberUnassigned
,
2488 DevicePathFromHandle (Handles
[Index
]),
2492 ASSERT_EFI_ERROR (Status
);
2493 FreePool (Description
);
2496 if (HandleCount
!= 0) {
2500 BmMakeBootOptionDescriptionUnique (BootOptions
, *BootOptionCount
);
2505 The function enumerates all boot options, creates them and registers them in the BootOrder variable.
2509 EfiBootManagerRefreshAllBootOption (
2514 EFI_BOOT_MANAGER_LOAD_OPTION
*NvBootOptions
;
2515 UINTN NvBootOptionCount
;
2516 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2517 UINTN BootOptionCount
;
2521 // Optionally refresh the legacy boot option
2523 if (mBmRefreshLegacyBootOption
!= NULL
) {
2524 mBmRefreshLegacyBootOption ();
2527 BootOptions
= BmEnumerateBootOptions (&BootOptionCount
);
2528 NvBootOptions
= EfiBootManagerGetLoadOptions (&NvBootOptionCount
, LoadOptionTypeBoot
);
2531 // Mark the boot option as added by BDS by setting OptionalData to a special GUID
2533 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2534 BootOptions
[Index
].OptionalData
= AllocateCopyPool (sizeof (EFI_GUID
), &mBmAutoCreateBootOptionGuid
);
2535 BootOptions
[Index
].OptionalDataSize
= sizeof (EFI_GUID
);
2539 // Remove invalid EFI boot options from NV
2541 for (Index
= 0; Index
< NvBootOptionCount
; Index
++) {
2542 if (((DevicePathType (NvBootOptions
[Index
].FilePath
) != BBS_DEVICE_PATH
) ||
2543 (DevicePathSubType (NvBootOptions
[Index
].FilePath
) != BBS_BBS_DP
)
2544 ) && BmIsAutoCreateBootOption (&NvBootOptions
[Index
])
2547 // Only check those added by BDS
2548 // so that the boot options added by end-user or OS installer won't be deleted
2550 if (EfiBootManagerFindLoadOption (&NvBootOptions
[Index
], BootOptions
, BootOptionCount
) == (UINTN
) -1) {
2551 Status
= EfiBootManagerDeleteLoadOptionVariable (NvBootOptions
[Index
].OptionNumber
, LoadOptionTypeBoot
);
2553 // Deleting variable with current variable implementation shouldn't fail.
2555 ASSERT_EFI_ERROR (Status
);
2561 // Add new EFI boot options to NV
2563 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2564 if (EfiBootManagerFindLoadOption (&BootOptions
[Index
], NvBootOptions
, NvBootOptionCount
) == (UINTN
) -1) {
2565 EfiBootManagerAddLoadOptionVariable (&BootOptions
[Index
], (UINTN
) -1);
2567 // Try best to add the boot options so continue upon failure.
2572 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2573 EfiBootManagerFreeLoadOptions (NvBootOptions
, NvBootOptionCount
);
2577 This function is called to create the boot option for the Boot Manager Menu.
2579 The Boot Manager Menu is shown after successfully booting a boot option.
2580 Assume the BootManagerMenuFile is in the same FV as the module links to this library.
2582 @param BootOption Return the boot option of the Boot Manager Menu
2584 @retval EFI_SUCCESS Successfully register the Boot Manager Menu.
2585 @retval Status Return status of gRT->SetVariable (). BootOption still points
2586 to the Boot Manager Menu even the Status is not EFI_SUCCESS.
2589 BmRegisterBootManagerMenu (
2590 OUT EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
2594 CHAR16
*Description
;
2595 UINTN DescriptionLength
;
2596 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2597 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
2598 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode
;
2600 Status
= GetSectionFromFv (
2601 PcdGetPtr (PcdBootManagerMenuFile
),
2602 EFI_SECTION_USER_INTERFACE
,
2604 (VOID
**) &Description
,
2607 if (EFI_ERROR (Status
)) {
2611 EfiInitializeFwVolDevicepathNode (&FileNode
, PcdGetPtr (PcdBootManagerMenuFile
));
2612 Status
= gBS
->HandleProtocol (
2614 &gEfiLoadedImageProtocolGuid
,
2615 (VOID
**) &LoadedImage
2617 ASSERT_EFI_ERROR (Status
);
2618 DevicePath
= AppendDevicePathNode (
2619 DevicePathFromHandle (LoadedImage
->DeviceHandle
),
2620 (EFI_DEVICE_PATH_PROTOCOL
*) &FileNode
2622 ASSERT (DevicePath
!= NULL
);
2624 Status
= EfiBootManagerInitializeLoadOption (
2626 LoadOptionNumberUnassigned
,
2628 LOAD_OPTION_CATEGORY_APP
| LOAD_OPTION_ACTIVE
| LOAD_OPTION_HIDDEN
,
2629 (Description
!= NULL
) ? Description
: L
"Boot Manager Menu",
2634 ASSERT_EFI_ERROR (Status
);
2635 FreePool (DevicePath
);
2636 if (Description
!= NULL
) {
2637 FreePool (Description
);
2641 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2642 UINTN BootOptionCount
;
2644 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
2645 ASSERT (EfiBootManagerFindLoadOption (BootOption
, BootOptions
, BootOptionCount
) == -1);
2646 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2649 return EfiBootManagerAddLoadOptionVariable (BootOption
, 0);
2653 Return the boot option corresponding to the Boot Manager Menu.
2654 It may automatically create one if the boot option hasn't been created yet.
2656 @param BootOption Return the Boot Manager Menu.
2658 @retval EFI_SUCCESS The Boot Manager Menu is successfully returned.
2659 @retval Status Return status of gRT->SetVariable (). BootOption still points
2660 to the Boot Manager Menu even the Status is not EFI_SUCCESS.
2664 EfiBootManagerGetBootManagerMenu (
2665 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOption
2669 UINTN BootOptionCount
;
2670 EFI_BOOT_MANAGER_LOAD_OPTION
*BootOptions
;
2672 EFI_DEVICE_PATH_PROTOCOL
*Node
;
2673 EFI_HANDLE FvHandle
;
2675 BootOptions
= EfiBootManagerGetLoadOptions (&BootOptionCount
, LoadOptionTypeBoot
);
2677 for (Index
= 0; Index
< BootOptionCount
; Index
++) {
2678 Node
= BootOptions
[Index
].FilePath
;
2679 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &Node
, &FvHandle
);
2680 if (!EFI_ERROR (Status
)) {
2682 EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) Node
),
2683 PcdGetPtr (PcdBootManagerMenuFile
)
2686 Status
= EfiBootManagerInitializeLoadOption (
2688 BootOptions
[Index
].OptionNumber
,
2689 BootOptions
[Index
].OptionType
,
2690 BootOptions
[Index
].Attributes
,
2691 BootOptions
[Index
].Description
,
2692 BootOptions
[Index
].FilePath
,
2693 BootOptions
[Index
].OptionalData
,
2694 BootOptions
[Index
].OptionalDataSize
2696 ASSERT_EFI_ERROR (Status
);
2702 EfiBootManagerFreeLoadOptions (BootOptions
, BootOptionCount
);
2705 // Automatically create the Boot#### for Boot Manager Menu when not found.
2707 if (Index
== BootOptionCount
) {
2708 return BmRegisterBootManagerMenu (BootOption
);