From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by mx.groups.io with SMTP id smtpd.web10.1201.1582746236404584873 for ; Wed, 26 Feb 2020 11:43:56 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=q3Jwa+KS; spf=pass (domain: linaro.org, ip: 209.85.221.65, mailfrom: ard.biesheuvel@linaro.org) Received: by mail-wr1-f65.google.com with SMTP id l5so236398wrx.4 for ; Wed, 26 Feb 2020 11:43:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=v23HRwMjWqjsuWYj6VrCtKV7XgLapYajB+z0lLypTc8=; b=q3Jwa+KSdLcopqroG7UQV/Wi+VEv4pdv/NzWcA74SHewSKb/1RbXbvxoSHD1VM6yd9 dcJEnHo18yVmUavrw817eGPKtRgutxLnh24bJ6Qu1edzCbLbqe35uPN4XRhfdxmoaFF1 fatnjSTQcej6Ki31djEbmDCRrYkv5DPUzafw+kVTaimx5HpFwmv5DgjqdZuppRDnF7dG 9sD/jpxrB7YTZcLligvEnd3m1fh72P2Z4WkOKz/CawWBJB1kAMRrWECZfkD6zC5Ux2um Vdl1SGOZt2sFKzF+1CGxBF7GSuZkob8lA7yiEigE8Q5SI1F6NvDA7VM1Jg1a+7oFTz6e MQmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=v23HRwMjWqjsuWYj6VrCtKV7XgLapYajB+z0lLypTc8=; b=lrqAie3xzxw+mwqaCetMA7euQo79HIEcodP1YrVVLaFhzVOFJy+u5lK2l/QvBq7h/b mlTAeGAOu1j4VxsReY1rsUhctjTgPldZbV8wocQvuJh9ooZJC4pINdQg5XKAUcT7XXq7 4suJ8j90QfH3+a2oicdbf1gETSfcIlC/UKhhc15Q+T4LPU3xht2TGrlqoydJihzl2ff1 8w+13iutzUYQqldfGtVN05ACawoTEqitYUJ5AcxM8w2EWU/x5aMR+mLZNeM6eNDYbpMB SKhsIJD4yDM4nzXrdpVOzAk19Bg+/A5HRix5YENPIT7XiTxBX6fMSwdCnS58NkjSjwRJ MbHw== X-Gm-Message-State: APjAAAWlBEsE0N+qPsEoV81bmKtY2laQMOMHXX4IvQgWfFKnJAdzbMRI ouszeNPCWupt0QKieShTjSzuKBszv36pXA== X-Google-Smtp-Source: APXvYqxauZoX0NQRAzgv2aNIhKyfHtnZHDoLAyXrzNCOURVG5vIC2sC5TlN3Oodv0vDAfIwYp+wTDw== X-Received: by 2002:a5d:534b:: with SMTP id t11mr265967wrv.120.1582746234588; Wed, 26 Feb 2020 11:43:54 -0800 (PST) Return-Path: Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id c9sm4235162wmc.47.2020.02.26.11.43.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Feb 2020 11:43:53 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , lersek@redhat.com, leif@nuviainc.com, michael.d.kinney@intel.com, jian.j.wang@intel.com, hao.a.wu@intel.com, ray.ni@intel.com, zhichao.gao@intel.com Subject: [PATCH v3 6/6] OvmfPkg IA32: add support for loading X64 images Date: Wed, 26 Feb 2020 20:43:43 +0100 Message-Id: <20200226194343.2985-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200226194343.2985-1-ard.biesheuvel@linaro.org> References: <20200226194343.2985-1-ard.biesheuvel@linaro.org> This is the UEFI counterpart to my Linux series which generalizes mixed mode support into a feature that requires very little internal knowledge about the architecture specifics of booting Linux on the part of the bootloader or firmware. Instead, we add a .compat PE/COFF header containing an array of PE_COMPAT nodes containing tuples that describe alternate entrypoints into the image for different native machine types, e.g., IA-32 in a 64-bit image so it can be booted from IA-32 firmware. This patch implements the PE/COFF emulator protocol to take this new section into account, so that such images can simply be loaded via LoadImage/StartImage, e.g., straight from the shell. This feature is based on the EDK2 specific PE/COFF emulator protocol that was introduced in commit 57df17fe26cd ("MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images", 2019-04-14). Signed-off-by: Ard Biesheuvel Acked-by: Laszlo Ersek --- OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.c | 139 ++++++++++++++++++++ OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf | 36 +++++ OvmfPkg/OvmfPkgIa32.dsc | 5 + OvmfPkg/OvmfPkgIa32.fdf | 4 + 4 files changed, 184 insertions(+) diff --git a/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.c b/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.c new file mode 100644 index 000000000000..6dc07f467752 --- /dev/null +++ b/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.c @@ -0,0 +1,139 @@ +/** @file + * PE/COFF emulator protocol implementation to start Linux kernel + * images from non-native firmware + * + * Copyright (c) 2020, ARM Ltd. All rights reserved.
+ * + * SPDX-License-Identifier: BSD-2-Clause-Patent + * + */ + +#include + +#include +#include +#include + +#include + +#pragma pack (1) +typedef struct { + UINT8 Type; + UINT8 Size; + UINT16 MachineType; + UINT32 EntryPoint; +} PE_COMPAT_TYPE1; +#pragma pack () + +STATIC +BOOLEAN +EFIAPI +IsImageSupported ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN UINT16 ImageType, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL + ) +{ + return ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION; +} + +STATIC +EFI_IMAGE_ENTRY_POINT +EFIAPI +GetCompatEntryPoint ( + IN EFI_PHYSICAL_ADDRESS ImageBase + ) +{ + EFI_IMAGE_DOS_HEADER *DosHdr; + UINTN PeCoffHeaderOffset; + EFI_IMAGE_NT_HEADERS32 *Pe32; + EFI_IMAGE_SECTION_HEADER *Section; + UINTN NumberOfSections; + PE_COMPAT_TYPE1 *PeCompat; + + DosHdr = (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageBase; + if (DosHdr->e_magic != EFI_IMAGE_DOS_SIGNATURE) { + return NULL; + } + + PeCoffHeaderOffset = DosHdr->e_lfanew; + Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageBase + PeCoffHeaderOffset); + + Section = (EFI_IMAGE_SECTION_HEADER *)((UINTN)&Pe32->OptionalHeader + + Pe32->FileHeader.SizeOfOptionalHeader); + NumberOfSections = (UINTN)Pe32->FileHeader.NumberOfSections; + + while (NumberOfSections--) { + if (!CompareMem (Section->Name, ".compat", sizeof (Section->Name))) { + // + // Dereference the section contents to find the mixed mode entry point + // + PeCompat = (PE_COMPAT_TYPE1 *)((UINTN)ImageBase + Section->VirtualAddress); + + while (PeCompat->Type != 0) { + if (PeCompat->Type == 1 && + PeCompat->Size >= sizeof (PE_COMPAT_TYPE1) && + EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCompat->MachineType)) { + + return (EFI_IMAGE_ENTRY_POINT)((UINTN)ImageBase + PeCompat->EntryPoint); + } + PeCompat = (PE_COMPAT_TYPE1 *)((UINTN)PeCompat + PeCompat->Size); + } + } + Section++; + } + return NULL; +} + +STATIC +EFI_STATUS +EFIAPI +RegisterImage ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize, + IN OUT EFI_IMAGE_ENTRY_POINT *EntryPoint + ) +{ + EFI_IMAGE_ENTRY_POINT CompatEntryPoint; + + CompatEntryPoint = GetCompatEntryPoint (ImageBase); + if (CompatEntryPoint == NULL) { + return EFI_UNSUPPORTED; + } + + *EntryPoint = CompatEntryPoint; + return EFI_SUCCESS; +} + +STATIC +EFI_STATUS +EFIAPI +UnregisterImage ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS ImageBase + ) +{ + return EFI_SUCCESS; +} + +STATIC EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL mCompatLoaderPeCoffEmuProtocol = { + IsImageSupported, + RegisterImage, + UnregisterImage, + EDKII_PECOFF_IMAGE_EMULATOR_VERSION, + EFI_IMAGE_MACHINE_X64 +}; + +EFI_STATUS +EFIAPI +CompatImageLoaderDxeEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return gBS->InstallProtocolInterface (&ImageHandle, + &gEdkiiPeCoffImageEmulatorProtocolGuid, + EFI_NATIVE_INTERFACE, + &mCompatLoaderPeCoffEmuProtocol); +} diff --git a/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf b/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf new file mode 100644 index 000000000000..82369384fbe6 --- /dev/null +++ b/OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf @@ -0,0 +1,36 @@ +## @file +# PE/COFF emulator protocol implementation to start Linux kernel +# images from non-native firmware +# +# Copyright (c) 2020, ARM Ltd. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 1.27 + BASE_NAME = CompatImageLoaderDxe + FILE_GUID = 1019f54a-2560-41b2-87b0-6750b98f3eff + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = CompatImageLoaderDxeEntryPoint + +[Sources] + CompatImageLoaderDxe.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + BaseMemoryLib + PeCoffLib + UefiBootServicesTableLib + UefiDriverEntryPoint + +[Protocols] + gEdkiiPeCoffImageEmulatorProtocolGuid ## PRODUCES + +[Depex] + TRUE diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 76e52a3de120..8d91903f8b4e 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -33,6 +33,7 @@ [Defines] DEFINE SOURCE_DEBUG_ENABLE = FALSE DEFINE TPM2_ENABLE = FALSE DEFINE TPM2_CONFIG_ENABLE = FALSE + DEFINE LOAD_X64_ON_IA32_ENABLE = FALSE # # Network definition @@ -932,3 +933,7 @@ [Components] SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigDxe.inf !endif !endif + +!if $(LOAD_X64_ON_IA32_ENABLE) == TRUE + OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf +!endif diff --git a/OvmfPkg/OvmfPkgIa32.fdf b/OvmfPkg/OvmfPkgIa32.fdf index b6cd5da4f2b3..ff8d80859fb9 100644 --- a/OvmfPkg/OvmfPkgIa32.fdf +++ b/OvmfPkg/OvmfPkgIa32.fdf @@ -354,6 +354,10 @@ [FV.DXEFV] !endif !endif +!if $(LOAD_X64_ON_IA32_ENABLE) == TRUE +INF OvmfPkg/CompatImageLoaderDxe/CompatImageLoaderDxe.inf +!endif + ################################################################################ [FV.FVMAIN_COMPACT] -- 2.17.1