X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FUefiBootManagerLib%2FBmBoot.c;h=d6844823aa5519c72a182d033000ebd07fe744d4;hb=5e6e2dcc380dcd841f6f979fea8c302c80a87ec3;hp=8f14cf6d3f8116a6c7b44206757b4363e7a33231;hpb=4982ea57d973e25ded82841fe0fe351f173eac65;p=mirror_edk2.git diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c index 8f14cf6d3f..d6844823aa 100644 --- a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c @@ -1,8 +1,8 @@ /** @file Library functions which relates with booting. -(C) Copyright 2015 Hewlett-Packard Development Company, L.P.
-Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.
+(C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -15,19 +15,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "InternalBm.h" -#define VENDOR_IDENTIFICATION_OFFSET 3 -#define VENDOR_IDENTIFICATION_LENGTH 8 -#define PRODUCT_IDENTIFICATION_OFFSET 11 -#define PRODUCT_IDENTIFICATION_LENGTH 16 - -CONST UINT16 mBmUsbLangId = 0x0409; // English -CHAR16 mBmUefiPrefix[] = L"UEFI "; +EFI_RAM_DISK_PROTOCOL *mRamDisk = NULL; EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION mBmRefreshLegacyBootOption = NULL; EFI_BOOT_MANAGER_LEGACY_BOOT mBmLegacyBoot = NULL; -LIST_ENTRY mPlatformBootDescriptionHandlers = INITIALIZE_LIST_HEAD_VARIABLE (mPlatformBootDescriptionHandlers); - /// /// This GUID is used for an EFI Variable that stores the front device pathes /// for a partial device path that starts with the HD node. @@ -53,104 +45,25 @@ EfiBootManagerRegisterLegacyBootSupport ( } /** - For a bootable Device path, return its boot type. - - @param DevicePath The bootable device Path to check - - @retval AcpiFloppyBoot If given device path contains ACPI_DEVICE_PATH type device path node - which HID is floppy device. - @retval MessageAtapiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_ATAPI_DP. - @retval MessageSataBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_SATA_DP. - @retval MessageScsiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_SCSI_DP. - @retval MessageUsbBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_USB_DP. - @retval MessageNetworkBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_MAC_ADDR_DP, MSG_VLAN_DP, - MSG_IPv4_DP or MSG_IPv6_DP. - @retval MessageHttpBoot If given device path contains MESSAGING_DEVICE_PATH type device path node - and its last device path node's subtype is MSG_URI_DP. - @retval UnsupportedBoot If tiven device path doesn't match the above condition, it's not supported. + Return TRUE when the boot option is auto-created instead of manually added. + @param BootOption Pointer to the boot option to check. + + @retval TRUE The boot option is auto-created. + @retval FALSE The boot option is manually added. **/ -BM_BOOT_TYPE -BmDevicePathType ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath +BOOLEAN +BmIsAutoCreateBootOption ( + EFI_BOOT_MANAGER_LOAD_OPTION *BootOption ) { - EFI_DEVICE_PATH_PROTOCOL *Node; - EFI_DEVICE_PATH_PROTOCOL *NextNode; - - ASSERT (DevicePath != NULL); - - for (Node = DevicePath; !IsDevicePathEndType (Node); Node = NextDevicePathNode (Node)) { - switch (DevicePathType (Node)) { - - case ACPI_DEVICE_PATH: - if (EISA_ID_TO_NUM (((ACPI_HID_DEVICE_PATH *) Node)->HID) == 0x0604) { - return BmAcpiFloppyBoot; - } - break; - - case HARDWARE_DEVICE_PATH: - if (DevicePathSubType (Node) == HW_CONTROLLER_DP) { - return BmHardwareDeviceBoot; - } - break; - - case MESSAGING_DEVICE_PATH: - // - // Skip LUN device node - // - NextNode = Node; - do { - NextNode = NextDevicePathNode (NextNode); - } while ( - (DevicePathType (NextNode) == MESSAGING_DEVICE_PATH) && - (DevicePathSubType(NextNode) == MSG_DEVICE_LOGICAL_UNIT_DP) - ); - - // - // If the device path not only point to driver device, it is not a messaging device path, - // - if (!IsDevicePathEndType (NextNode)) { - continue; - } - - switch (DevicePathSubType (Node)) { - case MSG_ATAPI_DP: - return BmMessageAtapiBoot; - break; - - case MSG_SATA_DP: - return BmMessageSataBoot; - break; - - case MSG_USB_DP: - return BmMessageUsbBoot; - break; - - case MSG_SCSI_DP: - return BmMessageScsiBoot; - break; - - case MSG_MAC_ADDR_DP: - case MSG_VLAN_DP: - case MSG_IPv4_DP: - case MSG_IPv6_DP: - return BmMessageNetworkBoot; - break; - - case MSG_URI_DP: - return BmMessageHttpBoot; - break; - } - } + if ((BootOption->OptionalDataSize == sizeof (EFI_GUID)) && + CompareGuid ((EFI_GUID *) BootOption->OptionalData, &mBmAutoCreateBootOptionGuid) + ) { + return TRUE; + } else { + return FALSE; } - - return BmMiscBoot; } /** @@ -206,7 +119,7 @@ BmFindBootOptionInVariable ( if (OptionNumber == LoadOptionNumberUnassigned) { BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot); - Index = BmFindLoadOption (OptionToFind, BootOptions, BootOptionCount); + Index = EfiBootManagerFindLoadOption (OptionToFind, BootOptions, BootOptionCount); if (Index != -1) { OptionNumber = BootOptions[Index].OptionNumber; } @@ -218,21 +131,16 @@ BmFindBootOptionInVariable ( } /** - Get the file buffer using a Memory Mapped Device Path. - + Return the correct FV file path. FV address may change across reboot. This routine promises the FV file device path is right. - @param DevicePath The Memory Mapped Device Path to get the file buffer. - @param FullPath Receive the updated FV Device Path pointint to the file. - @param FileSize Receive the file buffer size. + @param FilePath The Memory Mapped Device Path to get the file buffer. - @return The file buffer. + @return The updated FV Device Path pointint to the file. **/ -VOID * -BmGetFileBufferByMemmapFv ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, - OUT EFI_DEVICE_PATH_PROTOCOL **FullPath, - OUT UINTN *FileSize +EFI_DEVICE_PATH_PROTOCOL * +BmAdjustFvFilePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath ) { EFI_STATUS Status; @@ -240,23 +148,28 @@ BmGetFileBufferByMemmapFv ( EFI_DEVICE_PATH_PROTOCOL *FvFileNode; EFI_HANDLE FvHandle; EFI_LOADED_IMAGE_PROTOCOL *LoadedImage; - UINT32 AuthenticationStatus; UINTN FvHandleCount; EFI_HANDLE *FvHandles; EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; - VOID *FileBuffer; - - FvFileNode = DevicePath; + EFI_DEVICE_PATH_PROTOCOL *FullPath; + + // + // Get the file buffer by using the exactly FilePath. + // + FvFileNode = FilePath; Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &FvFileNode, &FvHandle); if (!EFI_ERROR (Status)) { - FileBuffer = GetFileBufferByFilePath (TRUE, DevicePath, FileSize, &AuthenticationStatus); - if (FileBuffer != NULL) { - *FullPath = DuplicateDevicePath (DevicePath); - } - return FileBuffer; + return DuplicateDevicePath (FilePath); + } + + // + // Only wide match other FVs if it's a memory mapped FV file path. + // + if ((DevicePathType (FilePath) != HARDWARE_DEVICE_PATH) || (DevicePathSubType (FilePath) != HW_MEMMAP_DP)) { + return NULL; } - FvFileNode = NextDevicePathNode (DevicePath); + FvFileNode = NextDevicePathNode (FilePath); // // Firstly find the FV file in current FV @@ -267,11 +180,10 @@ BmGetFileBufferByMemmapFv ( (VOID **) &LoadedImage ); NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (LoadedImage->DeviceHandle), FvFileNode); - FileBuffer = BmGetFileBufferByMemmapFv (NewDevicePath, FullPath, FileSize); + FullPath = BmAdjustFvFilePath (NewDevicePath); FreePool (NewDevicePath); - - if (FileBuffer != NULL) { - return FileBuffer; + if (FullPath != NULL) { + return FullPath; } // @@ -284,48 +196,58 @@ BmGetFileBufferByMemmapFv ( &FvHandleCount, &FvHandles ); - for (Index = 0; (Index < FvHandleCount) && (FileBuffer == NULL); Index++) { + for (Index = 0; Index < FvHandleCount; Index++) { if (FvHandles[Index] == LoadedImage->DeviceHandle) { // - // Skip current FV + // Skip current FV, it was handed in first step. // continue; } NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (FvHandles[Index]), FvFileNode); - FileBuffer = BmGetFileBufferByMemmapFv (NewDevicePath, FullPath, FileSize); + FullPath = BmAdjustFvFilePath (NewDevicePath); FreePool (NewDevicePath); + if (FullPath != NULL) { + break; + } } if (FvHandles != NULL) { FreePool (FvHandles); } - return FileBuffer; + return FullPath; } /** - Check if it's a Memory Mapped FV Device Path. + Check if it's a Device Path pointing to FV file. The function doesn't garentee the device path points to existing FV file. @param DevicePath Input device path. - @retval TRUE The device path is a Memory Mapped FV Device Path. - @retval FALSE The device path is NOT a Memory Mapped FV Device Path. + @retval TRUE The device path is a FV File Device Path. + @retval FALSE The device path is NOT a FV File Device Path. **/ BOOLEAN -BmIsMemmapFvFilePath ( +BmIsFvFilePath ( IN EFI_DEVICE_PATH_PROTOCOL *DevicePath ) { - EFI_DEVICE_PATH_PROTOCOL *FileNode; + EFI_STATUS Status; + EFI_HANDLE Handle; + EFI_DEVICE_PATH_PROTOCOL *Node; + + Node = DevicePath; + Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &Handle); + if (!EFI_ERROR (Status)) { + return TRUE; + } if ((DevicePathType (DevicePath) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (DevicePath) == HW_MEMMAP_DP)) { - FileNode = NextDevicePathNode (DevicePath); - if ((DevicePathType (FileNode) == MEDIA_DEVICE_PATH) && (DevicePathSubType (FileNode) == MEDIA_PIWG_FW_FILE_DP)) { - return IsDevicePathEnd (NextDevicePathNode (FileNode)); + DevicePath = NextDevicePathNode (DevicePath); + if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType (DevicePath) == MEDIA_PIWG_FW_FILE_DP)) { + return IsDevicePathEnd (NextDevicePathNode (DevicePath)); } } - return FALSE; } @@ -415,411 +337,6 @@ BmMatchUsbClass ( return TRUE; } -/** - Eliminate the extra spaces in the Str to one space. - - @param Str Input string info. -**/ -VOID -BmEliminateExtraSpaces ( - IN CHAR16 *Str - ) -{ - UINTN Index; - UINTN ActualIndex; - - for (Index = 0, ActualIndex = 0; Str[Index] != L'\0'; Index++) { - if ((Str[Index] != L' ') || ((ActualIndex > 0) && (Str[ActualIndex - 1] != L' '))) { - Str[ActualIndex++] = Str[Index]; - } - } - Str[ActualIndex] = L'\0'; -} - -/** - Try to get the controller's ATA/ATAPI description. - - @param Handle Controller handle. - - @return The description string. -**/ -CHAR16 * -BmGetDescriptionFromDiskInfo ( - IN EFI_HANDLE Handle - ) -{ - UINTN Index; - EFI_STATUS Status; - EFI_DISK_INFO_PROTOCOL *DiskInfo; - UINT32 BufferSize; - EFI_ATAPI_IDENTIFY_DATA IdentifyData; - EFI_SCSI_INQUIRY_DATA InquiryData; - CHAR16 *Description; - UINTN Length; - CONST UINTN ModelNameLength = 40; - CONST UINTN SerialNumberLength = 20; - CHAR8 *StrPtr; - UINT8 Temp; - - Description = NULL; - - Status = gBS->HandleProtocol ( - Handle, - &gEfiDiskInfoProtocolGuid, - (VOID **) &DiskInfo - ); - if (EFI_ERROR (Status)) { - return NULL; - } - - if (CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoAhciInterfaceGuid) || - CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoIdeInterfaceGuid)) { - BufferSize = sizeof (EFI_ATAPI_IDENTIFY_DATA); - Status = DiskInfo->Identify ( - DiskInfo, - &IdentifyData, - &BufferSize - ); - if (!EFI_ERROR (Status)) { - Description = AllocateZeroPool ((ModelNameLength + SerialNumberLength + 2) * sizeof (CHAR16)); - ASSERT (Description != NULL); - for (Index = 0; Index + 1 < ModelNameLength; Index += 2) { - Description[Index] = (CHAR16) IdentifyData.ModelName[Index + 1]; - Description[Index + 1] = (CHAR16) IdentifyData.ModelName[Index]; - } - - Length = Index; - Description[Length++] = L' '; - - for (Index = 0; Index + 1 < SerialNumberLength; Index += 2) { - Description[Length + Index] = (CHAR16) IdentifyData.SerialNo[Index + 1]; - Description[Length + Index + 1] = (CHAR16) IdentifyData.SerialNo[Index]; - } - Length += Index; - Description[Length++] = L'\0'; - ASSERT (Length == ModelNameLength + SerialNumberLength + 2); - - BmEliminateExtraSpaces (Description); - } - } else if (CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoScsiInterfaceGuid)) { - BufferSize = sizeof (EFI_SCSI_INQUIRY_DATA); - Status = DiskInfo->Inquiry ( - DiskInfo, - &InquiryData, - &BufferSize - ); - if (!EFI_ERROR (Status)) { - Description = AllocateZeroPool ((VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2) * sizeof (CHAR16)); - ASSERT (Description != NULL); - - // - // Per SCSI spec, EFI_SCSI_INQUIRY_DATA.Reserved_5_95[3 - 10] save the Verdor identification - // EFI_SCSI_INQUIRY_DATA.Reserved_5_95[11 - 26] save the product identification, - // Here combine the vendor identification and product identification to the description. - // - StrPtr = (CHAR8 *) (&InquiryData.Reserved_5_95[VENDOR_IDENTIFICATION_OFFSET]); - Temp = StrPtr[VENDOR_IDENTIFICATION_LENGTH]; - StrPtr[VENDOR_IDENTIFICATION_LENGTH] = '\0'; - AsciiStrToUnicodeStr (StrPtr, Description); - StrPtr[VENDOR_IDENTIFICATION_LENGTH] = Temp; - - // - // Add one space at the middle of vendor information and product information. - // - Description[VENDOR_IDENTIFICATION_LENGTH] = L' '; - - StrPtr = (CHAR8 *) (&InquiryData.Reserved_5_95[PRODUCT_IDENTIFICATION_OFFSET]); - StrPtr[PRODUCT_IDENTIFICATION_LENGTH] = '\0'; - AsciiStrToUnicodeStr (StrPtr, Description + VENDOR_IDENTIFICATION_LENGTH + 1); - - BmEliminateExtraSpaces (Description); - } - } - - return Description; -} - -/** - Try to get the controller's USB description. - - @param Handle Controller handle. - - @return The description string. -**/ -CHAR16 * -BmGetUsbDescription ( - IN EFI_HANDLE Handle - ) -{ - EFI_STATUS Status; - EFI_USB_IO_PROTOCOL *UsbIo; - CHAR16 NullChar; - CHAR16 *Manufacturer; - CHAR16 *Product; - CHAR16 *SerialNumber; - CHAR16 *Description; - EFI_USB_DEVICE_DESCRIPTOR DevDesc; - UINTN DescMaxSize; - - Status = gBS->HandleProtocol ( - Handle, - &gEfiUsbIoProtocolGuid, - (VOID **) &UsbIo - ); - if (EFI_ERROR (Status)) { - return NULL; - } - - NullChar = L'\0'; - - Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc); - if (EFI_ERROR (Status)) { - return NULL; - } - - Status = UsbIo->UsbGetStringDescriptor ( - UsbIo, - mBmUsbLangId, - DevDesc.StrManufacturer, - &Manufacturer - ); - if (EFI_ERROR (Status)) { - Manufacturer = &NullChar; - } - - Status = UsbIo->UsbGetStringDescriptor ( - UsbIo, - mBmUsbLangId, - DevDesc.StrProduct, - &Product - ); - if (EFI_ERROR (Status)) { - Product = &NullChar; - } - - Status = UsbIo->UsbGetStringDescriptor ( - UsbIo, - mBmUsbLangId, - DevDesc.StrSerialNumber, - &SerialNumber - ); - if (EFI_ERROR (Status)) { - SerialNumber = &NullChar; - } - - if ((Manufacturer == &NullChar) && - (Product == &NullChar) && - (SerialNumber == &NullChar) - ) { - return NULL; - } - - DescMaxSize = StrSize (Manufacturer) + StrSize (Product) + StrSize (SerialNumber); - Description = AllocateZeroPool (DescMaxSize); - ASSERT (Description != NULL); - StrCatS (Description, DescMaxSize/sizeof(CHAR16), Manufacturer); - StrCatS (Description, DescMaxSize/sizeof(CHAR16), L" "); - - StrCatS (Description, DescMaxSize/sizeof(CHAR16), Product); - StrCatS (Description, DescMaxSize/sizeof(CHAR16), L" "); - - StrCatS (Description, DescMaxSize/sizeof(CHAR16), SerialNumber); - - if (Manufacturer != &NullChar) { - FreePool (Manufacturer); - } - if (Product != &NullChar) { - FreePool (Product); - } - if (SerialNumber != &NullChar) { - FreePool (SerialNumber); - } - - BmEliminateExtraSpaces (Description); - - return Description; -} - -/** - Return the boot description for the controller based on the type. - - @param Handle Controller handle. - - @return The description string. -**/ -CHAR16 * -BmGetMiscDescription ( - IN EFI_HANDLE Handle - ) -{ - EFI_STATUS Status; - CHAR16 *Description; - EFI_BLOCK_IO_PROTOCOL *BlockIo; - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs; - - switch (BmDevicePathType (DevicePathFromHandle (Handle))) { - case BmAcpiFloppyBoot: - Description = L"Floppy"; - break; - - case BmMessageAtapiBoot: - case BmMessageSataBoot: - Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo); - ASSERT_EFI_ERROR (Status); - // - // Assume a removable SATA device should be the DVD/CD device - // - Description = BlockIo->Media->RemovableMedia ? L"DVD/CDROM" : L"Hard Drive"; - break; - - case BmMessageUsbBoot: - Description = L"USB Device"; - break; - - case BmMessageScsiBoot: - Description = L"SCSI Device"; - break; - - case BmHardwareDeviceBoot: - Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo); - if (!EFI_ERROR (Status)) { - Description = BlockIo->Media->RemovableMedia ? L"Removable Disk" : L"Hard Drive"; - } else { - Description = L"Misc Device"; - } - break; - - case BmMessageNetworkBoot: - Description = L"Network"; - break; - - case BmMessageHttpBoot: - Description = L"Http"; - break; - - default: - Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **) &Fs); - if (!EFI_ERROR (Status)) { - Description = L"Non-Block Boot Device"; - } else { - Description = L"Misc Device"; - } - break; - } - - return AllocateCopyPool (StrSize (Description), Description); -} - -/** - Register the platform provided boot description handler. - - @param Handler The platform provided boot description handler - - @retval EFI_SUCCESS The handler was registered successfully. - @retval EFI_ALREADY_STARTED The handler was already registered. - @retval EFI_OUT_OF_RESOURCES There is not enough resource to perform the registration. -**/ -EFI_STATUS -EFIAPI -EfiBootManagerRegisterBootDescriptionHandler ( - IN EFI_BOOT_MANAGER_BOOT_DESCRIPTION_HANDLER Handler - ) -{ - LIST_ENTRY *Link; - BM_BOOT_DESCRIPTION_ENTRY *Entry; - - for ( Link = GetFirstNode (&mPlatformBootDescriptionHandlers) - ; !IsNull (&mPlatformBootDescriptionHandlers, Link) - ; Link = GetNextNode (&mPlatformBootDescriptionHandlers, Link) - ) { - Entry = CR (Link, BM_BOOT_DESCRIPTION_ENTRY, Link, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE); - if (Entry->Handler == Handler) { - return EFI_ALREADY_STARTED; - } - } - - Entry = AllocatePool (sizeof (BM_BOOT_DESCRIPTION_ENTRY)); - if (Entry == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - Entry->Signature = BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE; - Entry->Handler = Handler; - InsertTailList (&mPlatformBootDescriptionHandlers, &Entry->Link); - return EFI_SUCCESS; -} - -BM_GET_BOOT_DESCRIPTION mBmBootDescriptionHandlers[] = { - BmGetUsbDescription, - BmGetDescriptionFromDiskInfo, - BmGetMiscDescription -}; - -/** - Return the boot description for the controller. - - @param Handle Controller handle. - - @return The description string. -**/ -CHAR16 * -BmGetBootDescription ( - IN EFI_HANDLE Handle - ) -{ - LIST_ENTRY *Link; - BM_BOOT_DESCRIPTION_ENTRY *Entry; - CHAR16 *Description; - CHAR16 *DefaultDescription; - CHAR16 *Temp; - UINTN Index; - - // - // Firstly get the default boot description - // - DefaultDescription = NULL; - for (Index = 0; Index < sizeof (mBmBootDescriptionHandlers) / sizeof (mBmBootDescriptionHandlers[0]); Index++) { - DefaultDescription = mBmBootDescriptionHandlers[Index] (Handle); - if (DefaultDescription != NULL) { - // - // Avoid description confusion between UEFI & Legacy boot option by adding "UEFI " prefix - // ONLY for core provided boot description handler. - // - Temp = AllocatePool (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix)); - ASSERT (Temp != NULL); - StrCpyS ( Temp, - (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix))/sizeof(CHAR16), - mBmUefiPrefix - ); - StrCatS ( Temp, - (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix))/sizeof(CHAR16), - DefaultDescription - ); - FreePool (DefaultDescription); - DefaultDescription = Temp; - break; - } - } - ASSERT (DefaultDescription != NULL); - - // - // Secondly query platform for the better boot description - // - for ( Link = GetFirstNode (&mPlatformBootDescriptionHandlers) - ; !IsNull (&mPlatformBootDescriptionHandlers, Link) - ; Link = GetNextNode (&mPlatformBootDescriptionHandlers, Link) - ) { - Entry = CR (Link, BM_BOOT_DESCRIPTION_ENTRY, Link, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE); - Description = Entry->Handler (Handle, DefaultDescription); - if (Description != NULL) { - FreePool (DefaultDescription); - return Description; - } - } - - return DefaultDescription; -} - /** Check whether a USB device match the specified USB WWID device path. This function follows "Load Option Processing" behavior in UEFI specification. @@ -1030,65 +547,219 @@ BmFindUsbDevice ( @param FilePath The device path pointing to a load option. It could be a short-form device path. - @param FullPath Return the full device path of the load option after - short-form device path expanding. - Caller is responsible to free it. - @param FileSize Return the load option size. + @param FullPath The full path returned by the routine in last call. + Set to NULL in first call. @param ShortformNode Pointer to the USB short-form device path node in the FilePath buffer. - @return The load option buffer. Caller is responsible to free the memory. + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. **/ -VOID * +EFI_DEVICE_PATH_PROTOCOL * BmExpandUsbDevicePath ( IN EFI_DEVICE_PATH_PROTOCOL *FilePath, - OUT EFI_DEVICE_PATH_PROTOCOL **FullPath, - OUT UINTN *FileSize, - IN EFI_DEVICE_PATH_PROTOCOL *ShortformNode + IN EFI_DEVICE_PATH_PROTOCOL *FullPath, + IN EFI_DEVICE_PATH_PROTOCOL *ShortformNode ) { UINTN ParentDevicePathSize; EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath; - EFI_DEVICE_PATH_PROTOCOL *FullDevicePath; + EFI_DEVICE_PATH_PROTOCOL *NextFullPath; EFI_HANDLE *Handles; UINTN HandleCount; UINTN Index; - VOID *FileBuffer; + BOOLEAN GetNext; + NextFullPath = NULL; + GetNext = (BOOLEAN)(FullPath == NULL); ParentDevicePathSize = (UINTN) ShortformNode - (UINTN) FilePath; RemainingDevicePath = NextDevicePathNode (ShortformNode); - FileBuffer = NULL; Handles = BmFindUsbDevice (FilePath, ParentDevicePathSize, &HandleCount); - for (Index = 0; (Index < HandleCount) && (FileBuffer == NULL); Index++) { - FullDevicePath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), RemainingDevicePath); - FileBuffer = BmGetLoadOptionBuffer (FullDevicePath, FullPath, FileSize); - FreePool (FullDevicePath); + for (Index = 0; Index < HandleCount; Index++) { + FilePath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), RemainingDevicePath); + if (FilePath == NULL) { + // + // Out of memory. + // + continue; + } + NextFullPath = BmGetNextLoadOptionDevicePath (FilePath, NULL); + FreePool (FilePath); + if (NextFullPath == NULL) { + // + // No BlockIo or SimpleFileSystem under FilePath. + // + continue; + } + if (GetNext) { + break; + } else { + GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0); + FreePool (NextFullPath); + NextFullPath = NULL; + } } if (Handles != NULL) { FreePool (Handles); } - return FileBuffer; + return NextFullPath; } /** - Save the partition DevicePath to the CachedDevicePath as the first instance. + Expand File-path device path node to be full device path in platform. - @param CachedDevicePath The device path cache. - @param DevicePath The partition device path to be cached. + @param FilePath The device path pointing to a load option. + It could be a short-form device path. + @param FullPath The full path returned by the routine in last call. + Set to NULL in first call. + + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. **/ -VOID -BmCachePartitionDevicePath ( - IN OUT EFI_DEVICE_PATH_PROTOCOL **CachedDevicePath, - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath +EFI_DEVICE_PATH_PROTOCOL * +BmExpandFileDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN EFI_DEVICE_PATH_PROTOCOL *FullPath ) { - EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; - UINTN Count; + EFI_STATUS Status; + UINTN Index; + UINTN HandleCount; + EFI_HANDLE *Handles; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + UINTN MediaType; + EFI_DEVICE_PATH_PROTOCOL *NextFullPath; + BOOLEAN GetNext; - if (BmMatchDevicePaths (*CachedDevicePath, DevicePath)) { - TempDevicePath = *CachedDevicePath; + EfiBootManagerConnectAll (); + Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &HandleCount, &Handles); + if (EFI_ERROR (Status)) { + HandleCount = 0; + Handles = NULL; + } + + GetNext = (BOOLEAN)(FullPath == NULL); + NextFullPath = NULL; + // + // Enumerate all removable media devices followed by all fixed media devices, + // followed by media devices which don't layer on block io. + // + for (MediaType = 0; MediaType < 3; MediaType++) { + for (Index = 0; Index < HandleCount; Index++) { + Status = gBS->HandleProtocol (Handles[Index], &gEfiBlockIoProtocolGuid, (VOID *) &BlockIo); + if (EFI_ERROR (Status)) { + BlockIo = NULL; + } + if ((MediaType == 0 && BlockIo != NULL && BlockIo->Media->RemovableMedia) || + (MediaType == 1 && BlockIo != NULL && !BlockIo->Media->RemovableMedia) || + (MediaType == 2 && BlockIo == NULL) + ) { + NextFullPath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), FilePath); + if (GetNext) { + break; + } else { + GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0); + FreePool (NextFullPath); + NextFullPath = NULL; + } + } + } + if (NextFullPath != NULL) { + break; + } + } + + if (Handles != NULL) { + FreePool (Handles); + } + + return NextFullPath; +} + +/** + Expand URI device path node to be full device path in platform. + + @param FilePath The device path pointing to a load option. + It could be a short-form device path. + @param FullPath The full path returned by the routine in last call. + Set to NULL in first call. + + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmExpandUriDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN EFI_DEVICE_PATH_PROTOCOL *FullPath + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN HandleCount; + EFI_HANDLE *Handles; + EFI_DEVICE_PATH_PROTOCOL *NextFullPath; + EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath; + BOOLEAN GetNext; + + EfiBootManagerConnectAll (); + Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiLoadFileProtocolGuid, NULL, &HandleCount, &Handles); + if (EFI_ERROR (Status)) { + HandleCount = 0; + Handles = NULL; + } + + NextFullPath = NULL; + GetNext = (BOOLEAN)(FullPath == NULL); + for (Index = 0; Index < HandleCount; Index++) { + NextFullPath = BmExpandLoadFile (Handles[Index], FilePath); + + if (NextFullPath == NULL) { + continue; + } + + if (GetNext) { + break; + } else { + GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0); + // + // Free the resource occupied by the RAM disk. + // + RamDiskDevicePath = BmGetRamDiskDevicePath (NextFullPath); + if (RamDiskDevicePath != NULL) { + BmDestroyRamDisk (RamDiskDevicePath); + FreePool (RamDiskDevicePath); + } + FreePool (NextFullPath); + NextFullPath = NULL; + } + } + + if (Handles != NULL) { + FreePool (Handles); + } + + return NextFullPath; +} + +/** + Save the partition DevicePath to the CachedDevicePath as the first instance. + + @param CachedDevicePath The device path cache. + @param DevicePath The partition device path to be cached. +**/ +VOID +BmCachePartitionDevicePath ( + IN OUT EFI_DEVICE_PATH_PROTOCOL **CachedDevicePath, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath + ) +{ + EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; + UINTN Count; + + if (BmMatchDevicePaths (*CachedDevicePath, DevicePath)) { + TempDevicePath = *CachedDevicePath; *CachedDevicePath = BmDelPartMatchInstance (*CachedDevicePath, DevicePath); FreePool (TempDevicePath); } @@ -1139,35 +810,28 @@ BmCachePartitionDevicePath ( @param FilePath The device path pointing to a load option. It could be a short-form device path. - @param FullPath Return the full device path of the load option after - short-form device path expanding. - Caller is responsible to free it. - @param FileSize Return the load option size. - @return The load option buffer. Caller is responsible to free the memory. + @return The full device path pointing to the load option. **/ -VOID * +EFI_DEVICE_PATH_PROTOCOL * BmExpandPartitionDevicePath ( - IN EFI_DEVICE_PATH_PROTOCOL *FilePath, - OUT EFI_DEVICE_PATH_PROTOCOL **FullPath, - OUT UINTN *FileSize + IN EFI_DEVICE_PATH_PROTOCOL *FilePath ) { EFI_STATUS Status; UINTN BlockIoHandleCount; EFI_HANDLE *BlockIoBuffer; - VOID *FileBuffer; EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath; UINTN Index; EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath; EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath; EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; + EFI_DEVICE_PATH_PROTOCOL *FullPath; UINTN CachedDevicePathSize; BOOLEAN NeedAdjust; EFI_DEVICE_PATH_PROTOCOL *Instance; UINTN Size; - FileBuffer = NULL; // // Check if there is prestore 'HDDP' variable. // If exist, search the front path which point to partition node in the variable instants. @@ -1191,6 +855,7 @@ BmExpandPartitionDevicePath ( ASSERT_EFI_ERROR (Status); } + FullPath = NULL; if (CachedDevicePath != NULL) { TempNewDevicePath = CachedDevicePath; NeedAdjust = FALSE; @@ -1209,10 +874,20 @@ BmExpandPartitionDevicePath ( Status = EfiBootManagerConnectDevicePath (Instance, NULL); if (!EFI_ERROR (Status)) { TempDevicePath = AppendDevicePath (Instance, NextDevicePathNode (FilePath)); - FileBuffer = BmGetLoadOptionBuffer (TempDevicePath, FullPath, FileSize); + // + // TempDevicePath = ACPI()/PCI()/ATA()/Partition() + // or = ACPI()/PCI()/ATA()/Partition()/.../A.EFI + // + // When TempDevicePath = ACPI()/PCI()/ATA()/Partition(), + // it may expand to two potienal full paths (nested partition, rarely happen): + // 1. ACPI()/PCI()/ATA()/Partition()/Partition(A1)/EFI/BootX64.EFI + // 2. ACPI()/PCI()/ATA()/Partition()/Partition(A2)/EFI/BootX64.EFI + // For simplicity, only #1 is returned. + // + FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL); FreePool (TempDevicePath); - if (FileBuffer != NULL) { + if (FullPath != NULL) { // // Adjust the 'HDDP' instances sequence if the matched one is not first one. // @@ -1233,7 +908,7 @@ BmExpandPartitionDevicePath ( FreePool (Instance); FreePool (CachedDevicePath); - return FileBuffer; + return FullPath; } } } @@ -1269,10 +944,10 @@ BmExpandPartitionDevicePath ( // Find the matched partition device path // TempDevicePath = AppendDevicePath (BlockIoDevicePath, NextDevicePathNode (FilePath)); - FileBuffer = BmGetLoadOptionBuffer (TempDevicePath, FullPath, FileSize); + FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL); FreePool (TempDevicePath); - if (FileBuffer != NULL) { + if (FullPath != NULL) { BmCachePartitionDevicePath (&CachedDevicePath, BlockIoDevicePath); // @@ -1295,127 +970,489 @@ BmExpandPartitionDevicePath ( if (CachedDevicePath != NULL) { FreePool (CachedDevicePath); } - if (BlockIoBuffer != NULL) { - FreePool (BlockIoBuffer); + if (BlockIoBuffer != NULL) { + FreePool (BlockIoBuffer); + } + return FullPath; +} + +/** + Expand the media device path which points to a BlockIo or SimpleFileSystem instance + by appending EFI_REMOVABLE_MEDIA_FILE_NAME. + + @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance. + @param FullPath The full path returned by the routine in last call. + Set to NULL in first call. + + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmExpandMediaDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN EFI_DEVICE_PATH_PROTOCOL *FullPath + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + VOID *Buffer; + EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; + EFI_DEVICE_PATH_PROTOCOL *NextFullPath; + UINTN Size; + UINTN TempSize; + EFI_HANDLE *SimpleFileSystemHandles; + UINTN NumberSimpleFileSystemHandles; + UINTN Index; + BOOLEAN GetNext; + + GetNext = (BOOLEAN)(FullPath == NULL); + // + // Check whether the device is connected + // + TempDevicePath = DevicePath; + Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle); + if (!EFI_ERROR (Status)) { + ASSERT (IsDevicePathEnd (TempDevicePath)); + + NextFullPath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME); + // + // For device path pointing to simple file system, it only expands to one full path. + // + if (GetNext) { + return NextFullPath; + } else { + FreePool (NextFullPath); + return NULL; + } + } + + Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle); + ASSERT_EFI_ERROR (Status); + + // + // For device boot option only pointing to the removable device handle, + // should make sure all its children handles (its child partion or media handles) + // are created and connected. + // + gBS->ConnectController (Handle, NULL, NULL, TRUE); + + // + // Issue a dummy read to the device to check for media change. + // When the removable media is changed, any Block IO read/write will + // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is + // returned. After the Block IO protocol is reinstalled, subsequent + // Block IO read/write will success. + // + Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo); + ASSERT_EFI_ERROR (Status); + Buffer = AllocatePool (BlockIo->Media->BlockSize); + if (Buffer != NULL) { + BlockIo->ReadBlocks ( + BlockIo, + BlockIo->Media->MediaId, + 0, + BlockIo->Media->BlockSize, + Buffer + ); + FreePool (Buffer); + } + + // + // Detect the the default boot file from removable Media + // + NextFullPath = NULL; + Size = GetDevicePathSize (DevicePath) - END_DEVICE_PATH_LENGTH; + gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiSimpleFileSystemProtocolGuid, + NULL, + &NumberSimpleFileSystemHandles, + &SimpleFileSystemHandles + ); + for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) { + // + // Get the device path size of SimpleFileSystem handle + // + TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]); + TempSize = GetDevicePathSize (TempDevicePath) - END_DEVICE_PATH_LENGTH; + // + // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path + // + if ((Size <= TempSize) && (CompareMem (TempDevicePath, DevicePath, Size) == 0)) { + NextFullPath = FileDevicePath (SimpleFileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME); + if (GetNext) { + break; + } else { + GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0); + FreePool (NextFullPath); + NextFullPath = NULL; + } + } + } + + if (SimpleFileSystemHandles != NULL) { + FreePool (SimpleFileSystemHandles); + } + + return NextFullPath; +} + +/** + Check whether Left and Right are the same without matching the specific + device path data in IP device path and URI device path node. + + @retval TRUE Left and Right are the same. + @retval FALSE Left and Right are the different. +**/ +BOOLEAN +BmMatchHttpBootDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *Left, + IN EFI_DEVICE_PATH_PROTOCOL *Right + ) +{ + for (; !IsDevicePathEnd (Left) && !IsDevicePathEnd (Right) + ; Left = NextDevicePathNode (Left), Right = NextDevicePathNode (Right) + ) { + if (CompareMem (Left, Right, DevicePathNodeLength (Left)) != 0) { + if ((DevicePathType (Left) != MESSAGING_DEVICE_PATH) || (DevicePathType (Right) != MESSAGING_DEVICE_PATH)) { + return FALSE; + } + + if (((DevicePathSubType (Left) != MSG_IPv4_DP) || (DevicePathSubType (Right) != MSG_IPv4_DP)) && + ((DevicePathSubType (Left) != MSG_IPv6_DP) || (DevicePathSubType (Right) != MSG_IPv6_DP)) && + ((DevicePathSubType (Left) != MSG_URI_DP) || (DevicePathSubType (Right) != MSG_URI_DP)) + ) { + return FALSE; + } + } + } + return (BOOLEAN) (IsDevicePathEnd (Left) && IsDevicePathEnd (Right)); +} + +/** + Get the file buffer from the file system produced by Load File instance. + + @param LoadFileHandle The handle of LoadFile instance. + @param RamDiskHandle Return the RAM Disk handle. + + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmExpandNetworkFileSystem ( + IN EFI_HANDLE LoadFileHandle, + OUT EFI_HANDLE *RamDiskHandle + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + EFI_HANDLE *Handles; + UINTN HandleCount; + UINTN Index; + EFI_DEVICE_PATH_PROTOCOL *Node; + + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiBlockIoProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + if (EFI_ERROR (Status)) { + Handles = NULL; + HandleCount = 0; + } + + Handle = NULL; + for (Index = 0; Index < HandleCount; Index++) { + Node = DevicePathFromHandle (Handles[Index]); + Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle); + if (!EFI_ERROR (Status) && + (Handle == LoadFileHandle) && + (DevicePathType (Node) == MEDIA_DEVICE_PATH) && (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP)) { + // + // Find the BlockIo instance populated from the LoadFile. + // + Handle = Handles[Index]; + break; + } + } + + if (Handles != NULL) { + FreePool (Handles); + } + + if (Index == HandleCount) { + Handle = NULL; + } + + *RamDiskHandle = Handle; + + if (Handle != NULL) { + // + // Re-use BmExpandMediaDevicePath() to get the full device path of load option. + // But assume only one SimpleFileSystem can be found under the BlockIo. + // + return BmExpandMediaDevicePath (DevicePathFromHandle (Handle), NULL); + } else { + return NULL; + } +} + +/** + Return the RAM Disk device path created by LoadFile. + + @param FilePath The source file path. + + @return Callee-to-free RAM Disk device path +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmGetRamDiskDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath; + EFI_DEVICE_PATH_PROTOCOL *Node; + EFI_HANDLE Handle; + + Node = FilePath; + Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle); + if (!EFI_ERROR (Status) && + (DevicePathType (Node) == MEDIA_DEVICE_PATH) && + (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP) + ) { + + // + // Construct the device path pointing to RAM Disk + // + Node = NextDevicePathNode (Node); + RamDiskDevicePath = DuplicateDevicePath (FilePath); + ASSERT (RamDiskDevicePath != NULL); + SetDevicePathEndNode ((VOID *) ((UINTN) RamDiskDevicePath + ((UINTN) Node - (UINTN) FilePath))); + return RamDiskDevicePath; + } + + return NULL; +} + +/** + Return the buffer and buffer size occupied by the RAM Disk. + + @param RamDiskDevicePath RAM Disk device path. + @param RamDiskSizeInPages Return RAM Disk size in pages. + + @retval RAM Disk buffer. +**/ +VOID * +BmGetRamDiskMemoryInfo ( + IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath, + OUT UINTN *RamDiskSizeInPages + ) +{ + + EFI_STATUS Status; + EFI_HANDLE Handle; + UINT64 StartingAddr; + UINT64 EndingAddr; + + ASSERT (RamDiskDevicePath != NULL); + + *RamDiskSizeInPages = 0; + + // + // Get the buffer occupied by RAM Disk. + // + Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &RamDiskDevicePath, &Handle); + ASSERT_EFI_ERROR (Status); + ASSERT ((DevicePathType (RamDiskDevicePath) == MEDIA_DEVICE_PATH) && + (DevicePathSubType (RamDiskDevicePath) == MEDIA_RAM_DISK_DP)); + StartingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->StartingAddr); + EndingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->EndingAddr); + *RamDiskSizeInPages = EFI_SIZE_TO_PAGES ((UINTN) (EndingAddr - StartingAddr + 1)); + return (VOID *) (UINTN) StartingAddr; +} + +/** + Destroy the RAM Disk. + + The destroy operation includes to call RamDisk.Unregister to + unregister the RAM DISK from RAM DISK driver, free the memory + allocated for the RAM Disk. + + @param RamDiskDevicePath RAM Disk device path. +**/ +VOID +BmDestroyRamDisk ( + IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath + ) +{ + EFI_STATUS Status; + VOID *RamDiskBuffer; + UINTN RamDiskSizeInPages; + + ASSERT (RamDiskDevicePath != NULL); + + RamDiskBuffer = BmGetRamDiskMemoryInfo (RamDiskDevicePath, &RamDiskSizeInPages); + + // + // Destroy RAM Disk. + // + if (mRamDisk == NULL) { + Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID *) &mRamDisk); + ASSERT_EFI_ERROR (Status); + } + Status = mRamDisk->Unregister (RamDiskDevicePath); + ASSERT_EFI_ERROR (Status); + FreePages (RamDiskBuffer, RamDiskSizeInPages); +} + +/** + Get the file buffer from the specified Load File instance. + + @param LoadFileHandle The specified Load File instance. + @param FilePath The file path which will pass to LoadFile(). + + @return The full device path pointing to the load option buffer. +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmExpandLoadFile ( + IN EFI_HANDLE LoadFileHandle, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath + ) +{ + EFI_STATUS Status; + EFI_LOAD_FILE_PROTOCOL *LoadFile; + VOID *FileBuffer; + EFI_HANDLE RamDiskHandle; + UINTN BufferSize; + EFI_DEVICE_PATH_PROTOCOL *FullPath; + + Status = gBS->OpenProtocol ( + LoadFileHandle, + &gEfiLoadFileProtocolGuid, + (VOID **) &LoadFile, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + ASSERT_EFI_ERROR (Status); + + FileBuffer = NULL; + BufferSize = 0; + Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer); + if ((Status != EFI_WARN_FILE_SYSTEM) && (Status != EFI_BUFFER_TOO_SMALL)) { + return NULL; + } + + if (Status == EFI_BUFFER_TOO_SMALL) { + // + // The load option buffer is directly returned by LoadFile. + // + return DuplicateDevicePath (DevicePathFromHandle (LoadFileHandle)); + } + + // + // The load option resides in a RAM disk. + // + FileBuffer = AllocateReservedPages (EFI_SIZE_TO_PAGES (BufferSize)); + if (FileBuffer == NULL) { + return NULL; + } + + Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer); + if (EFI_ERROR (Status)) { + FreePages (FileBuffer, EFI_SIZE_TO_PAGES (BufferSize)); + return NULL; + } + + FullPath = BmExpandNetworkFileSystem (LoadFileHandle, &RamDiskHandle); + if (FullPath == NULL) { + // + // Free the memory occupied by the RAM disk if there is no BlockIo or SimpleFileSystem instance. + // + BmDestroyRamDisk (DevicePathFromHandle (RamDiskHandle)); } - return FileBuffer; + + return FullPath; } /** - Expand the media device path which points to a BlockIo or SimpleFileSystem instance - by appending EFI_REMOVABLE_MEDIA_FILE_NAME. - - @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance. - @param FullPath Return the full device path pointing to the load option. - @param FileSize Return the size of the load option. + Return the full device path pointing to the load option. + + FilePath may: + 1. Exactly matches to a LoadFile instance. + 2. Cannot match to any LoadFile instance. Wide match is required. + In either case, the routine may return: + 1. A copy of FilePath when FilePath matches to a LoadFile instance and + the LoadFile returns a load option buffer. + 2. A new device path with IP and URI information updated when wide match + happens. + 3. A new device path pointing to a load option in RAM disk. + In either case, only one full device path is returned for a specified + FilePath. + + @param FilePath The media device path pointing to a LoadFile instance. @return The load option buffer. **/ -VOID * -BmExpandMediaDevicePath ( - IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, - OUT EFI_DEVICE_PATH_PROTOCOL **FullPath, - OUT UINTN *FileSize +EFI_DEVICE_PATH_PROTOCOL * +BmExpandLoadFiles ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath ) { - EFI_STATUS Status; - EFI_HANDLE Handle; - EFI_BLOCK_IO_PROTOCOL *BlockIo; - VOID *Buffer; - EFI_DEVICE_PATH_PROTOCOL *TempDevicePath; - UINTN Size; - UINTN TempSize; - EFI_HANDLE *SimpleFileSystemHandles; - UINTN NumberSimpleFileSystemHandles; - UINTN Index; - VOID *FileBuffer; - UINT32 AuthenticationStatus; - - // - // Check whether the device is connected - // - TempDevicePath = DevicePath; - Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle); - if (!EFI_ERROR (Status)) { - ASSERT (IsDevicePathEnd (TempDevicePath)); - - TempDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME); - FileBuffer = GetFileBufferByFilePath (TRUE, TempDevicePath, FileSize, &AuthenticationStatus); - if (FileBuffer == NULL) { - FreePool (TempDevicePath); - TempDevicePath = NULL; - } - *FullPath = TempDevicePath; - return FileBuffer; - } - - // - // For device boot option only pointing to the removable device handle, - // should make sure all its children handles (its child partion or media handles) are created and connected. - // - gBS->ConnectController (Handle, NULL, NULL, TRUE); - - // - // Issue a dummy read to the device to check for media change. - // When the removable media is changed, any Block IO read/write will - // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is - // returned. After the Block IO protocol is reinstalled, subsequent - // Block IO read/write will success. - // - Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle); - ASSERT_EFI_ERROR (Status); - Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo); - ASSERT_EFI_ERROR (Status); - Buffer = AllocatePool (BlockIo->Media->BlockSize); - if (Buffer != NULL) { - BlockIo->ReadBlocks ( - BlockIo, - BlockIo->Media->MediaId, - 0, - BlockIo->Media->BlockSize, - Buffer - ); - FreePool (Buffer); - } + EFI_STATUS Status; + EFI_HANDLE Handle; + EFI_HANDLE *Handles; + UINTN HandleCount; + UINTN Index; + EFI_DEVICE_PATH_PROTOCOL *Node; // - // Detect the the default boot file from removable Media + // Get file buffer from load file instance. // - FileBuffer = NULL; - *FullPath = NULL; - Size = GetDevicePathSize (DevicePath) - END_DEVICE_PATH_LENGTH; - gBS->LocateHandleBuffer ( - ByProtocol, - &gEfiSimpleFileSystemProtocolGuid, - NULL, - &NumberSimpleFileSystemHandles, - &SimpleFileSystemHandles - ); - for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) { + Node = FilePath; + Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle); + if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) { // - // Get the device path size of SimpleFileSystem handle + // When wide match happens, pass full device path to LoadFile (), + // otherwise, pass remaining device path to LoadFile (). // - TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]); - TempSize = GetDevicePathSize (TempDevicePath) - END_DEVICE_PATH_LENGTH; + FilePath = Node; + } else { + Handle = NULL; // - // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path + // Use wide match algorithm to find one when + // cannot find a LoadFile instance to exactly match the FilePath // - if ((Size <= TempSize) && (CompareMem (TempDevicePath, DevicePath, Size) == 0)) { - TempDevicePath = FileDevicePath (SimpleFileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME); - FileBuffer = GetFileBufferByFilePath (TRUE, TempDevicePath, FileSize, &AuthenticationStatus); - if (FileBuffer != NULL) { - *FullPath = TempDevicePath; + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiLoadFileProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + if (EFI_ERROR (Status)) { + Handles = NULL; + HandleCount = 0; + } + for (Index = 0; Index < HandleCount; Index++) { + if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles[Index]), FilePath)) { + Handle = Handles[Index]; break; } - FreePool (TempDevicePath); + } + if (Handles != NULL) { + FreePool (Handles); } } - if (SimpleFileSystemHandles != NULL) { - FreePool (SimpleFileSystemHandles); + if (Handle == NULL) { + return NULL; } - return FileBuffer; + return BmExpandLoadFile (Handle, FilePath); } /** @@ -1431,25 +1468,44 @@ BmExpandMediaDevicePath ( @return The load option buffer. Caller is responsible to free the memory. **/ VOID * -BmGetLoadOptionBuffer ( +EFIAPI +EfiBootManagerGetLoadOptionBuffer ( IN EFI_DEVICE_PATH_PROTOCOL *FilePath, OUT EFI_DEVICE_PATH_PROTOCOL **FullPath, OUT UINTN *FileSize ) +{ + *FullPath = NULL; + + EfiBootManagerConnectDevicePath (FilePath, NULL); + return BmGetNextLoadOptionBuffer (LoadOptionTypeMax, FilePath, FullPath, FileSize); +} + +/** + Get the next possible full path pointing to the load option. + The routine doesn't guarantee the returned full path points to an existing + file, and it also doesn't guarantee the existing file is a valid load option. + BmGetNextLoadOptionBuffer() guarantees. + + @param FilePath The device path pointing to a load option. + It could be a short-form device path. + @param FullPath The full path returned by the routine in last call. + Set to NULL in first call. + + @return The next possible full path pointing to the load option. + Caller is responsible to free the memory. +**/ +EFI_DEVICE_PATH_PROTOCOL * +BmGetNextLoadOptionDevicePath ( + IN EFI_DEVICE_PATH_PROTOCOL *FilePath, + IN EFI_DEVICE_PATH_PROTOCOL *FullPath + ) { EFI_HANDLE Handle; - VOID *FileBuffer; - UINT32 AuthenticationStatus; EFI_DEVICE_PATH_PROTOCOL *Node; EFI_STATUS Status; - ASSERT ((FilePath != NULL) && (FullPath != NULL) && (FileSize != NULL)); - - EfiBootManagerConnectDevicePath (FilePath, NULL); - - *FullPath = NULL; - *FileSize = 0; - FileBuffer = NULL; + ASSERT (FilePath != NULL); // // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI @@ -1461,7 +1517,7 @@ BmGetLoadOptionBuffer ( } if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) { - return BmExpandMediaDevicePath (FilePath, FullPath, FileSize); + return BmExpandMediaDevicePath (FilePath, FullPath); } // @@ -1472,59 +1528,112 @@ BmGetLoadOptionBuffer ( // // Expand the Harddrive device path // - return BmExpandPartitionDevicePath (FilePath, FullPath, FileSize); + if (FullPath == NULL) { + return BmExpandPartitionDevicePath (FilePath); + } else { + return NULL; + } + } else if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) && + (DevicePathSubType (FilePath) == MEDIA_FILEPATH_DP)) { + // + // Expand the File-path device path + // + return BmExpandFileDevicePath (FilePath, FullPath); + } else if ((DevicePathType (FilePath) == MESSAGING_DEVICE_PATH) && + (DevicePathSubType (FilePath) == MSG_URI_DP)) { + // + // Expand the URI device path + // + return BmExpandUriDevicePath (FilePath, FullPath); } else { - for (Node = FilePath; !IsDevicePathEnd (Node); Node = NextDevicePathNode (Node)) { - if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) && - ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) || (DevicePathSubType (Node) == MSG_USB_WWID_DP))) { - break; + Node = FilePath; + Status = gBS->LocateDevicePath (&gEfiUsbIoProtocolGuid, &Node, &Handle); + if (EFI_ERROR (Status)) { + // + // Only expand the USB WWID/Class device path + // when FilePath doesn't point to a physical UsbIo controller. + // Otherwise, infinite recursion will happen. + // + for (Node = FilePath; !IsDevicePathEnd (Node); Node = NextDevicePathNode (Node)) { + if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) && + ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) || (DevicePathSubType (Node) == MSG_USB_WWID_DP))) { + break; + } } - } - if (!IsDevicePathEnd (Node)) { // // Expand the USB WWID/Class device path // - FileBuffer = BmExpandUsbDevicePath (FilePath, FullPath, FileSize, Node); - if ((FileBuffer == NULL) && (FilePath == Node)) { - // - // Boot Option device path starts with USB Class or USB WWID device path. - // For Boot Option device path which doesn't begin with the USB Class or - // USB WWID device path, it's not needed to connect again here. - // - BmConnectUsbShortFormDevicePath (FilePath); - FileBuffer = BmExpandUsbDevicePath (FilePath, FullPath, FileSize, Node); + if (!IsDevicePathEnd (Node)) { + if (FilePath == Node) { + // + // Boot Option device path starts with USB Class or USB WWID device path. + // For Boot Option device path which doesn't begin with the USB Class or + // USB WWID device path, it's not needed to connect again here. + // + BmConnectUsbShortFormDevicePath (FilePath); + } + return BmExpandUsbDevicePath (FilePath, FullPath, Node); } - return FileBuffer; } } // - // Fix up the boot option path if it points to a FV in memory map style of device path + // For the below cases, FilePath only expands to one Full path. + // So just handle the case when FullPath == NULL. + // + if (FullPath != NULL) { + return NULL; + } + + // + // Load option resides in FV. // - if (BmIsMemmapFvFilePath (FilePath)) { - return BmGetFileBufferByMemmapFv (FilePath, FullPath, FileSize); + if (BmIsFvFilePath (FilePath)) { + return BmAdjustFvFilePath (FilePath); } // - // Directly reads the load option when it doesn't reside in simple file system instance (LoadFile/LoadFile2), - // or it directly points to a file in simple file system instance. + // Load option resides in Simple File System. // Node = FilePath; - Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle); - FileBuffer = GetFileBufferByFilePath (TRUE, FilePath, FileSize, &AuthenticationStatus); - if (FileBuffer != NULL) { - if (EFI_ERROR (Status)) { - *FullPath = DuplicateDevicePath (FilePath); - } else { - // - // LoadFile () may cause the device path of the Handle be updated. - // - *FullPath = AppendDevicePath (DevicePathFromHandle (Handle), Node); + Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle); + if (!EFI_ERROR (Status)) { + return DuplicateDevicePath (FilePath); + } + + // + // Last chance to try: Load option may be loaded through LoadFile. + // + return BmExpandLoadFiles (FilePath); +} + +/** + Check if it's a Device Path pointing to BootManagerMenu. + + @param DevicePath Input device path. + + @retval TRUE The device path is BootManagerMenu File Device Path. + @retval FALSE The device path is NOT BootManagerMenu File Device Path. +**/ +BOOLEAN +BmIsBootManagerMenuFilePath ( + EFI_DEVICE_PATH_PROTOCOL *DevicePath +) +{ + EFI_HANDLE FvHandle; + VOID *NameGuid; + EFI_STATUS Status; + + Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePath, &FvHandle); + if (!EFI_ERROR (Status)) { + NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath); + if (NameGuid != NULL) { + return CompareGuid (NameGuid, PcdGetPtr (PcdBootManagerMenuFile)); } } - return FileBuffer; + return FALSE; } /** @@ -1560,8 +1669,7 @@ EfiBootManagerBoot ( UINTN OptionNumber; UINTN OriginalOptionNumber; EFI_DEVICE_PATH_PROTOCOL *FilePath; - EFI_DEVICE_PATH_PROTOCOL *Node; - EFI_HANDLE FvHandle; + EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath; VOID *FileBuffer; UINTN FileSize; EFI_BOOT_LOGO_PROTOCOL *BootLogo; @@ -1616,12 +1724,7 @@ EfiBootManagerBoot ( // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute // the boot option. // - Node = BootOption->FilePath; - Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &FvHandle); - if (!EFI_ERROR (Status) && CompareGuid ( - EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) Node), - PcdGetPtr (PcdBootManagerMenuFile) - )) { + if (BmIsBootManagerMenuFilePath (BootOption->FilePath)) { DEBUG ((EFI_D_INFO, "[Bds] Booting Boot Manager Menu.\n")); BmStopHotkeyService (NULL, NULL); } else { @@ -1639,22 +1742,34 @@ EfiBootManagerBoot ( PERF_START_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber); // - // 5. Load EFI boot option to ImageHandle + // 5. Adjust the different type memory page number just before booting + // and save the updated info into the variable for next boot to use + // + BmSetMemoryTypeInformationVariable ( + (BOOLEAN) ((BootOption->Attributes & LOAD_OPTION_CATEGORY) == LOAD_OPTION_CATEGORY_BOOT) + ); + // - ImageHandle = NULL; + // 6. Load EFI boot option to ImageHandle + // + DEBUG_CODE_BEGIN (); + if (BootOption->Description == NULL) { + DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting from unknown device path\n")); + } else { + DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting %s\n", BootOption->Description)); + } + DEBUG_CODE_END (); + + ImageHandle = NULL; + RamDiskDevicePath = NULL; if (DevicePathType (BootOption->FilePath) != BBS_DEVICE_PATH) { - Status = EFI_NOT_FOUND; - FileBuffer = BmGetLoadOptionBuffer (BootOption->FilePath, &FilePath, &FileSize); - DEBUG_CODE ( - if (FileBuffer != NULL && CompareMem (BootOption->FilePath, FilePath, GetDevicePathSize (FilePath)) != 0) { - DEBUG ((EFI_D_INFO, "[Bds] DevicePath expand: ")); - BmPrintDp (BootOption->FilePath); - DEBUG ((EFI_D_INFO, " -> ")); - BmPrintDp (FilePath); - DEBUG ((EFI_D_INFO, "\n")); - } - ); - if (BmIsLoadOptionPeHeaderValid (BootOption->OptionType, FileBuffer, FileSize)) { + Status = EFI_NOT_FOUND; + FilePath = NULL; + EfiBootManagerConnectDevicePath (BootOption->FilePath, NULL); + FileBuffer = BmGetNextLoadOptionBuffer (LoadOptionTypeBoot, BootOption->FilePath, &FilePath, &FileSize); + if (FileBuffer != NULL) { + RamDiskDevicePath = BmGetRamDiskDevicePath (FilePath); + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad)); Status = gBS->LoadImage ( TRUE, @@ -1681,28 +1796,17 @@ EfiBootManagerBoot ( (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR) ); BootOption->Status = Status; + // + // Destroy the RAM disk + // + if (RamDiskDevicePath != NULL) { + BmDestroyRamDisk (RamDiskDevicePath); + FreePool (RamDiskDevicePath); + } return; } } - // - // 6. Adjust the different type memory page number just before booting - // and save the updated info into the variable for next boot to use - // - if ((BootOption->Attributes & LOAD_OPTION_CATEGORY) == LOAD_OPTION_CATEGORY_BOOT) { - if (PcdGetBool (PcdResetOnMemoryTypeInformationChange)) { - BmSetMemoryTypeInformationVariable (); - } - } - - DEBUG_CODE_BEGIN(); - if (BootOption->Description == NULL) { - DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting from unknown device path\n")); - } else { - DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting %s\n", BootOption->Description)); - } - DEBUG_CODE_END(); - // // Check to see if we should legacy BOOT. If yes then do the legacy boot // Write boot to OS performance data for Legacy boot @@ -1740,8 +1844,10 @@ EfiBootManagerBoot ( Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo); ASSERT_EFI_ERROR (Status); - ImageInfo->LoadOptionsSize = BootOption->OptionalDataSize; - ImageInfo->LoadOptions = BootOption->OptionalData; + if (!BmIsAutoCreateBootOption (BootOption)) { + ImageInfo->LoadOptionsSize = BootOption->OptionalDataSize; + ImageInfo->LoadOptions = BootOption->OptionalData; + } // // Clean to NULL because the image is loaded directly from the firmwares boot manager. @@ -1776,6 +1882,14 @@ EfiBootManagerBoot ( } PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber); + // + // Destroy the RAM disk + // + if (RamDiskDevicePath != NULL) { + BmDestroyRamDisk (RamDiskDevicePath); + FreePool (RamDiskDevicePath); + } + // // Clear the Watchdog Timer after the image returns // @@ -1867,66 +1981,6 @@ BmMatchPartitionDevicePathNode ( ); } -/** - Enumerate all boot option descriptions and append " 2"/" 3"/... to make - unique description. - - @param BootOptions Array of boot options. - @param BootOptionCount Count of boot options. -**/ -VOID -BmMakeBootOptionDescriptionUnique ( - EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions, - UINTN BootOptionCount - ) -{ - UINTN Base; - UINTN Index; - UINTN DescriptionSize; - UINTN MaxSuffixSize; - BOOLEAN *Visited; - UINTN MatchCount; - - if (BootOptionCount == 0) { - return; - } - - // - // Calculate the maximum buffer size for the number suffix. - // The initial sizeof (CHAR16) is for the blank space before the number. - // - MaxSuffixSize = sizeof (CHAR16); - for (Index = BootOptionCount; Index != 0; Index = Index / 10) { - MaxSuffixSize += sizeof (CHAR16); - } - - Visited = AllocateZeroPool (sizeof (BOOLEAN) * BootOptionCount); - ASSERT (Visited != NULL); - - for (Base = 0; Base < BootOptionCount; Base++) { - if (!Visited[Base]) { - MatchCount = 1; - Visited[Base] = TRUE; - DescriptionSize = StrSize (BootOptions[Base].Description); - for (Index = Base + 1; Index < BootOptionCount; Index++) { - if (!Visited[Index] && StrCmp (BootOptions[Base].Description, BootOptions[Index].Description) == 0) { - Visited[Index] = TRUE; - MatchCount++; - FreePool (BootOptions[Index].Description); - BootOptions[Index].Description = AllocatePool (DescriptionSize + MaxSuffixSize); - UnicodeSPrint ( - BootOptions[Index].Description, DescriptionSize + MaxSuffixSize, - L"%s %d", - BootOptions[Base].Description, MatchCount - ); - } - } - } - } - - FreePool (Visited); -} - /** Emuerate all possible bootable medias in the following order: 1. Removable BlockIo - The boot option only points to the removable media @@ -2076,7 +2130,7 @@ BmEnumerateBootOptions ( } // - // Parse load file, assuming UEFI Network boot option + // Parse load file protocol // gBS->LocateHandleBuffer ( ByProtocol, @@ -2086,6 +2140,12 @@ BmEnumerateBootOptions ( &Handles ); for (Index = 0; Index < HandleCount; Index++) { + // + // Ignore BootManagerMenu. its boot option will be created by EfiBootManagerGetBootManagerMenu(). + // + if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) { + continue; + } Description = BmGetBootDescription (Handles[Index]); BootOptions = ReallocatePool ( @@ -2157,15 +2217,13 @@ EfiBootManagerRefreshAllBootOption ( for (Index = 0; Index < NvBootOptionCount; Index++) { if (((DevicePathType (NvBootOptions[Index].FilePath) != BBS_DEVICE_PATH) || (DevicePathSubType (NvBootOptions[Index].FilePath) != BBS_BBS_DP) - ) && - (NvBootOptions[Index].OptionalDataSize == sizeof (EFI_GUID)) && - CompareGuid ((EFI_GUID *) NvBootOptions[Index].OptionalData, &mBmAutoCreateBootOptionGuid) + ) && BmIsAutoCreateBootOption (&NvBootOptions[Index]) ) { // // Only check those added by BDS // so that the boot options added by end-user or OS installer won't be deleted // - if (BmFindLoadOption (&NvBootOptions[Index], BootOptions, BootOptionCount) == (UINTN) -1) { + if (EfiBootManagerFindLoadOption (&NvBootOptions[Index], BootOptions, BootOptionCount) == -1) { Status = EfiBootManagerDeleteLoadOptionVariable (NvBootOptions[Index].OptionNumber, LoadOptionTypeBoot); // // Deleting variable with current variable implementation shouldn't fail. @@ -2179,7 +2237,7 @@ EfiBootManagerRefreshAllBootOption ( // Add new EFI boot options to NV // for (Index = 0; Index < BootOptionCount; Index++) { - if (BmFindLoadOption (&BootOptions[Index], NvBootOptions, NvBootOptionCount) == (UINTN) -1) { + if (EfiBootManagerFindLoadOption (&BootOptions[Index], NvBootOptions, NvBootOptionCount) == -1) { EfiBootManagerAddLoadOptionVariable (&BootOptions[Index], (UINTN) -1); // // Try best to add the boot options so continue upon failure. @@ -2192,7 +2250,7 @@ EfiBootManagerRefreshAllBootOption ( } /** - This function is called to create the boot option for the Boot Manager Menu. + This function is called to get or create the boot option for the Boot Manager Menu. The Boot Manager Menu is shown after successfully booting a boot option. Assume the BootManagerMenuFile is in the same FV as the module links to this library. @@ -2200,8 +2258,10 @@ EfiBootManagerRefreshAllBootOption ( @param BootOption Return the boot option of the Boot Manager Menu @retval EFI_SUCCESS Successfully register the Boot Manager Menu. - @retval Status Return status of gRT->SetVariable (). BootOption still points - to the Boot Manager Menu even the Status is not EFI_SUCCESS. + @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found. + @retval others Return status of gRT->SetVariable (). BootOption still points + to the Boot Manager Menu even the Status is not EFI_SUCCESS + and EFI_NOT_FOUND. **/ EFI_STATUS BmRegisterBootManagerMenu ( @@ -2214,30 +2274,79 @@ BmRegisterBootManagerMenu ( EFI_DEVICE_PATH_PROTOCOL *DevicePath; EFI_LOADED_IMAGE_PROTOCOL *LoadedImage; MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode; + UINTN HandleCount; + EFI_HANDLE *Handles; + UINTN Index; + VOID *Data; + UINTN DataSize; - Status = GetSectionFromFv ( - PcdGetPtr (PcdBootManagerMenuFile), - EFI_SECTION_USER_INTERFACE, - 0, - (VOID **) &Description, - &DescriptionLength - ); - if (EFI_ERROR (Status)) { - Description = NULL; + DevicePath = NULL; + Description = NULL; + // + // Try to find BootManagerMenu from LoadFile protocol + // + gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiLoadFileProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + for (Index = 0; Index < HandleCount; Index++) { + if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) { + DevicePath = DuplicateDevicePath (DevicePathFromHandle (Handles[Index])); + Description = BmGetBootDescription (Handles[Index]); + break; + } + } + if (HandleCount != 0) { + FreePool (Handles); } - EfiInitializeFwVolDevicepathNode (&FileNode, PcdGetPtr (PcdBootManagerMenuFile)); - Status = gBS->HandleProtocol ( - gImageHandle, - &gEfiLoadedImageProtocolGuid, - (VOID **) &LoadedImage - ); - ASSERT_EFI_ERROR (Status); - DevicePath = AppendDevicePathNode ( - DevicePathFromHandle (LoadedImage->DeviceHandle), - (EFI_DEVICE_PATH_PROTOCOL *) &FileNode - ); - ASSERT (DevicePath != NULL); + if (DevicePath == NULL) { + Data = NULL; + Status = GetSectionFromFv ( + PcdGetPtr (PcdBootManagerMenuFile), + EFI_SECTION_PE32, + 0, + (VOID **) &Data, + &DataSize + ); + if (Data != NULL) { + FreePool (Data); + } + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_WARN, "[Bds]BootManagerMenu FFS section can not be found, skip its boot option registration\n")); + return EFI_NOT_FOUND; + } + + // + // Get BootManagerMenu application's description from EFI User Interface Section. + // + Status = GetSectionFromFv ( + PcdGetPtr (PcdBootManagerMenuFile), + EFI_SECTION_USER_INTERFACE, + 0, + (VOID **) &Description, + &DescriptionLength + ); + if (EFI_ERROR (Status)) { + Description = NULL; + } + + EfiInitializeFwVolDevicepathNode (&FileNode, PcdGetPtr (PcdBootManagerMenuFile)); + Status = gBS->HandleProtocol ( + gImageHandle, + &gEfiLoadedImageProtocolGuid, + (VOID **) &LoadedImage + ); + ASSERT_EFI_ERROR (Status); + DevicePath = AppendDevicePathNode ( + DevicePathFromHandle (LoadedImage->DeviceHandle), + (EFI_DEVICE_PATH_PROTOCOL *) &FileNode + ); + ASSERT (DevicePath != NULL); + } Status = EfiBootManagerInitializeLoadOption ( BootOption, @@ -2260,7 +2369,7 @@ BmRegisterBootManagerMenu ( UINTN BootOptionCount; BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot); - ASSERT (BmFindLoadOption (BootOption, BootOptions, BootOptionCount) == -1); + ASSERT (EfiBootManagerFindLoadOption (BootOption, BootOptions, BootOptionCount) == -1); EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount); ); @@ -2270,12 +2379,14 @@ BmRegisterBootManagerMenu ( /** Return the boot option corresponding to the Boot Manager Menu. It may automatically create one if the boot option hasn't been created yet. - + @param BootOption Return the Boot Manager Menu. @retval EFI_SUCCESS The Boot Manager Menu is successfully returned. - @retval Status Return status of gRT->SetVariable (). BootOption still points - to the Boot Manager Menu even the Status is not EFI_SUCCESS. + @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found. + @retval others Return status of gRT->SetVariable (). BootOption still points + to the Boot Manager Menu even the Status is not EFI_SUCCESS + and EFI_NOT_FOUND. **/ EFI_STATUS EFIAPI @@ -2287,20 +2398,11 @@ EfiBootManagerGetBootManagerMenu ( UINTN BootOptionCount; EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions; UINTN Index; - EFI_DEVICE_PATH_PROTOCOL *Node; - EFI_HANDLE FvHandle; BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot); for (Index = 0; Index < BootOptionCount; Index++) { - Node = BootOptions[Index].FilePath; - Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &FvHandle); - if (!EFI_ERROR (Status)) { - if (CompareGuid ( - EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) Node), - PcdGetPtr (PcdBootManagerMenuFile) - ) - ) { + if (BmIsBootManagerMenuFilePath (BootOptions[Index].FilePath)) { Status = EfiBootManagerInitializeLoadOption ( BootOption, BootOptions[Index].OptionNumber, @@ -2313,7 +2415,6 @@ EfiBootManagerGetBootManagerMenu ( ); ASSERT_EFI_ERROR (Status); break; - } } }