Skip to content

Commit

Permalink
OvmfPkg: register BootManagerMnuApp
Browse files Browse the repository at this point in the history
Ported from EmulatorPkg

Signed-off-by: Maciej Pijanowski <[email protected]>
  • Loading branch information
macpijan committed Oct 10, 2023
1 parent f40aced commit 115aa9e
Showing 1 changed file with 148 additions and 1 deletion.
149 changes: 148 additions & 1 deletion OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <Library/Tcg2PhysicalPresenceLib.h>
#include <Library/XenPlatformLib.h>

EFI_GUID mBootMenuFile = {
0xEEC25BDC, 0x67F2, 0x4D95, { 0xB1, 0xD5, 0xF8, 0x1B, 0x20, 0x39, 0xD1, 0x1D }
};

//
// Global data
Expand Down Expand Up @@ -79,6 +82,148 @@ InstallDevicePathCallback (
VOID
);

EFI_DEVICE_PATH *
FvFilePath (
EFI_GUID *FileGuid
)
{

EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;

EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);

Status = gBS->HandleProtocol (
gImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &LoadedImage
);
ASSERT_EFI_ERROR (Status);
return AppendDevicePathNode (
DevicePathFromHandle (LoadedImage->DeviceHandle),
(EFI_DEVICE_PATH_PROTOCOL *) &FileNode
);
}

/**
Create one boot option for BootManagerMenuApp.
@param FileGuid Input file guid for the BootManagerMenuApp.
@param Description Description of the BootManagerMenuApp boot option.
@param Position Position of the new load option to put in the ****Order variable.
@param IsBootCategory Whether this is a boot category.
@retval OptionNumber Return the option number info.
**/
UINTN
RegisterBootManagerMenuAppBootOption (
EFI_GUID *FileGuid,
CHAR16 *Description,
UINTN Position,
BOOLEAN IsBootCategory
)
{
EFI_STATUS Status;
EFI_BOOT_MANAGER_LOAD_OPTION NewOption;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
UINTN OptionNumber;

DevicePath = FvFilePath (FileGuid);
Status = EfiBootManagerInitializeLoadOption (
&NewOption,
LoadOptionNumberUnassigned,
LoadOptionTypeBoot,
IsBootCategory ? LOAD_OPTION_ACTIVE : LOAD_OPTION_CATEGORY_APP,
Description,
DevicePath,
NULL,
0
);
ASSERT_EFI_ERROR (Status);
FreePool (DevicePath);

Status = EfiBootManagerAddLoadOptionVariable (&NewOption, Position);
ASSERT_EFI_ERROR (Status);

OptionNumber = NewOption.OptionNumber;

EfiBootManagerFreeLoadOption (&NewOption);

return OptionNumber;
}

/**
Check if it's a Device Path pointing to BootManagerMenuApp.
@param DevicePath Input device path.
@retval TRUE The device path is BootManagerMenuApp File Device Path.
@retval FALSE The device path is NOT BootManagerMenuApp File Device Path.
**/
BOOLEAN
IsBootManagerMenuAppFilePath (
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, &mBootMenuFile);
}
}

return FALSE;
}

/**
Return the boot option number to the BootManagerMenuApp.
If not found it in the current boot option, create a new one.
@retval OptionNumber Return the boot option number to the BootManagerMenuApp.
**/
UINTN
GetBootManagerMenuAppOption (
VOID
)
{
UINTN BootOptionCount;
EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
UINTN Index;
UINTN OptionNumber;

OptionNumber = 0;

BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);

for (Index = 0; Index < BootOptionCount; Index++) {
if (IsBootManagerMenuAppFilePath (BootOptions[Index].FilePath)) {
OptionNumber = BootOptions[Index].OptionNumber;
break;
}
}

EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);

if (Index >= BootOptionCount) {
//
// If not found the BootManagerMenuApp, create it.
//
OptionNumber = (UINT16) RegisterBootManagerMenuAppBootOption (&mBootMenuFile, L"UEFI BootManagerMenuApp", (UINTN) -1, FALSE);
}

return OptionNumber;
}

VOID
PlatformRegisterFvBootOption (
EFI_GUID *FileGuid,
Expand Down Expand Up @@ -278,6 +423,7 @@ PlatformRegisterOptionsAndKeys (
EFI_INPUT_KEY F2;
EFI_INPUT_KEY Esc;
EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
UINTN OptionNumber;

//
// Register ENTER as CONTINUE key
Expand All @@ -296,8 +442,9 @@ PlatformRegisterOptionsAndKeys (
Esc.UnicodeChar = CHAR_NULL;
Status = EfiBootManagerGetBootManagerMenu (&BootOption);
ASSERT_EFI_ERROR (Status);
OptionNumber = GetBootManagerMenuAppOption ();
Status = EfiBootManagerAddKeyOptionVariable (
NULL, (UINT16) BootOption.OptionNumber, 0, &F2, NULL
NULL, (UINT16) OptionNumber, 0, &F2, NULL
);
ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);
Status = EfiBootManagerAddKeyOptionVariable (
Expand Down

0 comments on commit 115aa9e

Please sign in to comment.