Skip to content

Commit 5eeb088

Browse files
kraxelmergify[bot]
authored andcommitted
OvmfPkg/QemuBootOrderLib: add StoreQemuBootOrder()
The function reads the boot order from qemu fw_cfg, translates it into device paths and stores them in 'QemuBootOrderNNNN' variables. In case there is no boot ordering configured the function will do nothing. Use case: Allow applications loaded via 'qemu -kernel bootloader.efi' obey the boot order. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
1 parent db463e8 commit 5eeb088

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

OvmfPkg/Include/Library/QemuBootOrderLib.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ ConnectDevicesFromQemu (
4747
VOID
4848
);
4949

50+
/**
51+
Write qemu boot order to uefi variables.
52+
53+
Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate
54+
the OpenFirmware device paths therein to UEFI device path fragments.
55+
56+
On Success store the device path in QemuBootOrderNNNN variables.
57+
**/
58+
VOID
59+
EFIAPI
60+
StoreQemuBootOrder (
61+
VOID
62+
);
63+
5064
/**
5165
5266
Set the boot order based on configuration retrieved from QEMU.

OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,11 @@ PlatformBootManagerAfterConsole (
16941694
//
16951695
PciAcpiInitialization ();
16961696

1697+
//
1698+
// Write qemu bootorder to efi variables
1699+
//
1700+
StoreQemuBootOrder ();
1701+
16971702
//
16981703
// Process QEMU's -kernel command line option
16991704
//

OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,128 @@ ConnectDevicesFromQemu (
16861686
return Status;
16871687
}
16881688

1689+
/**
1690+
Write qemu boot order to uefi variables.
1691+
1692+
Attempt to retrieve the "bootorder" fw_cfg file from QEMU. Translate
1693+
the OpenFirmware device paths therein to UEFI device path fragments.
1694+
1695+
On Success store the device path in QemuBootOrderNNNN variables.
1696+
**/
1697+
VOID
1698+
EFIAPI
1699+
StoreQemuBootOrder (
1700+
VOID
1701+
)
1702+
{
1703+
RETURN_STATUS Status;
1704+
FIRMWARE_CONFIG_ITEM FwCfgItem;
1705+
UINTN FwCfgSize;
1706+
CHAR8 *FwCfg;
1707+
EFI_STATUS EfiStatus;
1708+
EXTRA_ROOT_BUS_MAP *ExtraPciRoots;
1709+
CONST CHAR8 *FwCfgPtr;
1710+
UINTN TranslatedSize;
1711+
CHAR16 Translated[TRANSLATION_OUTPUT_SIZE];
1712+
UINTN VariableIndex = 0;
1713+
CHAR16 VariableName[20];
1714+
1715+
Status = QemuFwCfgFindFile ("bootorder", &FwCfgItem, &FwCfgSize);
1716+
if (RETURN_ERROR (Status)) {
1717+
return;
1718+
}
1719+
1720+
if (FwCfgSize == 0) {
1721+
return;
1722+
}
1723+
1724+
FwCfg = AllocatePool (FwCfgSize);
1725+
if (FwCfg == NULL) {
1726+
return;
1727+
}
1728+
1729+
QemuFwCfgSelectItem (FwCfgItem);
1730+
QemuFwCfgReadBytes (FwCfgSize, FwCfg);
1731+
if (FwCfg[FwCfgSize - 1] != '\0') {
1732+
Status = RETURN_INVALID_PARAMETER;
1733+
goto FreeFwCfg;
1734+
}
1735+
1736+
DEBUG ((DEBUG_VERBOSE, "%a: FwCfg:\n", __FUNCTION__));
1737+
DEBUG ((DEBUG_VERBOSE, "%a\n", FwCfg));
1738+
DEBUG ((DEBUG_VERBOSE, "%a: FwCfg: <end>\n", __FUNCTION__));
1739+
1740+
if (FeaturePcdGet (PcdQemuBootOrderPciTranslation)) {
1741+
EfiStatus = CreateExtraRootBusMap (&ExtraPciRoots);
1742+
if (EFI_ERROR (EfiStatus)) {
1743+
Status = (RETURN_STATUS)EfiStatus;
1744+
goto FreeFwCfg;
1745+
}
1746+
} else {
1747+
ExtraPciRoots = NULL;
1748+
}
1749+
1750+
//
1751+
// Translate each OpenFirmware path to a UEFI devpath prefix.
1752+
//
1753+
FwCfgPtr = FwCfg;
1754+
TranslatedSize = ARRAY_SIZE (Translated);
1755+
Status = TranslateOfwPath (
1756+
&FwCfgPtr,
1757+
ExtraPciRoots,
1758+
Translated,
1759+
&TranslatedSize
1760+
);
1761+
while (!RETURN_ERROR (Status)) {
1762+
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1763+
1764+
//
1765+
// Convert the UEFI devpath prefix to binary representation.
1766+
//
1767+
ASSERT (Translated[TranslatedSize] == L'\0');
1768+
DevicePath = ConvertTextToDevicePath (Translated);
1769+
if (DevicePath == NULL) {
1770+
Status = RETURN_OUT_OF_RESOURCES;
1771+
goto FreeExtraPciRoots;
1772+
}
1773+
1774+
UnicodeSPrint (
1775+
VariableName,
1776+
sizeof (VariableName),
1777+
L"QemuBootOrder%04d",
1778+
VariableIndex++
1779+
);
1780+
DEBUG ((DEBUG_INFO, "%a: %s = %s\n", __FUNCTION__, VariableName, Translated));
1781+
gRT->SetVariable (
1782+
VariableName,
1783+
&gQemuBootOrderGuid,
1784+
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1785+
GetDevicePathSize (DevicePath),
1786+
DevicePath
1787+
);
1788+
FreePool (DevicePath);
1789+
1790+
//
1791+
// Move to the next OFW devpath.
1792+
//
1793+
TranslatedSize = ARRAY_SIZE (Translated);
1794+
Status = TranslateOfwPath (
1795+
&FwCfgPtr,
1796+
ExtraPciRoots,
1797+
Translated,
1798+
&TranslatedSize
1799+
);
1800+
}
1801+
1802+
FreeExtraPciRoots:
1803+
if (ExtraPciRoots != NULL) {
1804+
DestroyExtraRootBusMap (ExtraPciRoots);
1805+
}
1806+
1807+
FreeFwCfg:
1808+
FreePool (FwCfg);
1809+
}
1810+
16891811
/**
16901812
16911813
Convert the UEFI DevicePath to full text representation with DevPathToText,

OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.inf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
[Guids]
5050
gEfiGlobalVariableGuid
5151
gVirtioMmioTransportGuid
52+
gQemuBootOrderGuid
5253

5354
[FeaturePcd]
5455
gUefiOvmfPkgTokenSpaceGuid.PcdQemuBootOrderPciTranslation

OvmfPkg/OvmfPkg.dec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
gConfidentialComputingSecretGuid = {0xadf956ad, 0xe98c, 0x484c, {0xae, 0x11, 0xb5, 0x1c, 0x7d, 0x33, 0x64, 0x47}}
147147
gConfidentialComputingSevSnpBlobGuid = {0x067b1f5f, 0xcf26, 0x44c5, {0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42}}
148148
gUefiOvmfPkgPlatformInfoGuid = {0xdec9b486, 0x1f16, 0x47c7, {0x8f, 0x68, 0xdf, 0x1a, 0x41, 0x88, 0x8b, 0xa5}}
149+
gQemuBootOrderGuid = {0x668f4529, 0x63d0, 0x4bb5, {0xb6, 0x5d, 0x6f, 0xbb, 0x9d, 0x36, 0xa4, 0x4a}}
149150

150151
[Ppis]
151152
# PPI whose presence in the PPI database signals that the TPM base address

0 commit comments

Comments
 (0)