From: Ard Biesheuvel <ardb@kernel.org>
To: grub-devel@gnu.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
Daniel Kiper <daniel.kiper@oracle.com>,
Leif Lindholm <quic_llindhol@quicinc.com>,
Nikita Ermakov <arei@altlinux.org>,
Atish Patra <atishp@atishpatra.org>,
Huacai Chen <chenhuacai@loongson.cn>,
Heinrich Schuchardt <heinrich.schuchardt@canonical.com>,
dann frazier <dann.frazier@canonical.com>,
Julian Andres Klode <julian.klode@canonical.com>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>
Subject: [PATCH v5 2/6] linux/arm: unify ARM/arm64 vs Xen PE/COFF header handling
Date: Tue, 18 Oct 2022 21:05:03 +0200 [thread overview]
Message-ID: <20221018190508.177568-3-ardb@kernel.org> (raw)
In-Reply-To: <20221018190508.177568-1-ardb@kernel.org>
Xen has its own version of the image header, to account for the
additional PE/COFF header fields. Since we are adding references to
those in the shared EFI loader code, update the common definitions
and drop the Xen specific one which no longer has a purpose.
Since in both cases, the call to grub_arch_efi_linux_check_image() is
preceded by a load of the image header, let's move the load into that
function, and rename it to grub_arch_efi_linux_load_image_header()/
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
grub-core/loader/arm64/linux.c | 12 +++++-----
grub-core/loader/arm64/xen_boot.c | 23 ++++----------------
include/grub/arm/linux.h | 5 +++++
include/grub/arm64/linux.h | 2 ++
include/grub/efi/efi.h | 4 +++-
5 files changed, 21 insertions(+), 25 deletions(-)
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
index b5b559c236e0..3733a69d6e36 100644
--- a/grub-core/loader/arm64/linux.c
+++ b/grub-core/loader/arm64/linux.c
@@ -49,8 +49,13 @@ static grub_addr_t initrd_start;
static grub_addr_t initrd_end;
grub_err_t
-grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
+grub_arch_efi_linux_load_image_header (grub_file_t file,
+ struct linux_arch_kernel_header * lh)
{
+ grub_file_seek (file, 0);
+ if (grub_file_read (file, lh, sizeof (*lh)) < (grub_ssize_t) sizeof (*lh))
+ return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read Linux image header");
+
if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
@@ -301,10 +306,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
kernel_size = grub_file_size (file);
- if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
- return grub_errno;
-
- if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
+ if (grub_arch_efi_linux_load_image_header (file, &lh) != GRUB_ERR_NONE)
goto fail;
grub_loader_unset();
diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c
index 22cc25eccd9d..763d87dcde5b 100644
--- a/grub-core/loader/arm64/xen_boot.c
+++ b/grub-core/loader/arm64/xen_boot.c
@@ -31,7 +31,6 @@
#include <grub/efi/efi.h>
#include <grub/efi/fdtload.h>
#include <grub/efi/memory.h>
-#include <grub/efi/pe32.h> /* required by struct xen_hypervisor_header */
#include <grub/i18n.h>
#include <grub/lib/cmdline.h>
@@ -65,18 +64,6 @@ enum module_type
};
typedef enum module_type module_type_t;
-struct xen_hypervisor_header
-{
- struct linux_arm64_kernel_header efi_head;
-
- /* This is always PE\0\0. */
- grub_uint8_t signature[GRUB_PE32_SIGNATURE_SIZE];
- /* The COFF file header. */
- struct grub_pe32_coff_header coff_header;
- /* The Optional header. */
- struct grub_pe64_optional_header optional_header;
-};
-
struct xen_boot_binary
{
struct xen_boot_binary *next;
@@ -452,7 +439,7 @@ static grub_err_t
grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
- struct xen_hypervisor_header sh;
+ struct linux_arm64_kernel_header lh;
grub_file_t file = NULL;
grub_dl_ref (my_mod);
@@ -467,10 +454,7 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
if (!file)
goto fail;
- if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh))
- goto fail;
- if (grub_arch_efi_linux_check_image
- ((struct linux_arch_kernel_header *) &sh) != GRUB_ERR_NONE)
+ if (grub_arch_efi_linux_load_image_header (file, &lh) != GRUB_ERR_NONE)
goto fail;
grub_file_seek (file, 0);
@@ -484,7 +468,8 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
return grub_errno;
xen_hypervisor->is_hypervisor = 1;
- xen_hypervisor->align = (grub_size_t) sh.optional_header.section_alignment;
+ xen_hypervisor->align
+ = (grub_size_t) lh.pe_image_header.optional_header.section_alignment;
xen_boot_binary_load (xen_hypervisor, file, argc, argv);
if (grub_errno == GRUB_ERR_NONE)
diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h
index bcd5a7eb186e..f38e695b1408 100644
--- a/include/grub/arm/linux.h
+++ b/include/grub/arm/linux.h
@@ -22,6 +22,8 @@
#include "system.h"
+#include <grub/efi/pe32.h>
+
#define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818
struct linux_arm_kernel_header {
@@ -32,6 +34,9 @@ struct linux_arm_kernel_header {
grub_uint32_t end; /* _edata */
grub_uint32_t reserved2[3];
grub_uint32_t hdr_offset;
+#if defined GRUB_MACHINE_EFI
+ struct grub_pe_image_header pe_image_header;
+#endif
};
#if defined(__arm__)
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h
index 7e22b4ab6990..3da71a51256f 100644
--- a/include/grub/arm64/linux.h
+++ b/include/grub/arm64/linux.h
@@ -20,6 +20,7 @@
#define GRUB_ARM64_LINUX_HEADER 1
#include <grub/types.h>
+#include <grub/efi/pe32.h>
#define GRUB_LINUX_ARM64_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */
@@ -36,6 +37,7 @@ struct linux_arm64_kernel_header
grub_uint64_t res4; /* reserved */
grub_uint32_t magic; /* Magic number, little endian, "ARM\x64" */
grub_uint32_t hdr_offset; /* Offset of PE/COFF header */
+ struct grub_pe_image_header pe_image_header;
};
#if defined(__aarch64__)
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index eb2dfdfce9f8..e61272de5330 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -102,7 +102,9 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
#include <grub/cpu/linux.h>
-grub_err_t grub_arch_efi_linux_check_image(struct linux_arch_kernel_header *lh);
+#include <grub/file.h>
+grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file,
+ struct linux_arch_kernel_header *lh);
grub_err_t grub_arch_efi_linux_boot_image(grub_addr_t addr, grub_size_t size,
char *args);
#endif
--
2.35.1
next prev parent reply other threads:[~2022-10-18 19:06 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-18 19:05 [PATCH v5 0/6] linux: implement LoadFile2 initrd loading Ard Biesheuvel
2022-10-18 19:05 ` [PATCH v5 1/6] efi: move MS-DOS stub out of generic PE header definition Ard Biesheuvel
2022-10-18 19:05 ` Ard Biesheuvel [this message]
2022-10-18 19:05 ` [PATCH v5 3/6] linux/arm: account for COFF headers appearing at unexpected offsets Ard Biesheuvel
2022-10-18 19:05 ` [PATCH v5 4/6] efi/efinet: Don't close connections at fini_hw() time Ard Biesheuvel
2022-10-18 19:05 ` [PATCH v5 5/6] efi: implement LoadFile2 initrd loading protocol for Linux Ard Biesheuvel
2022-10-18 19:05 ` [PATCH v5 6/6] linux: ignore FDT unless we need to modify it Ard Biesheuvel
2022-10-21 12:50 ` [PATCH v5 0/6] linux: implement LoadFile2 initrd loading Daniel Kiper
2022-10-21 13:15 ` Ard Biesheuvel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221018190508.177568-3-ardb@kernel.org \
--to=ardb@kernel.org \
--cc=arei@altlinux.org \
--cc=atishp@atishpatra.org \
--cc=chenhuacai@loongson.cn \
--cc=daniel.kiper@oracle.com \
--cc=dann.frazier@canonical.com \
--cc=grub-devel@gnu.org \
--cc=heinrich.schuchardt@canonical.com \
--cc=ilias.apalodimas@linaro.org \
--cc=julian.klode@canonical.com \
--cc=quic_llindhol@quicinc.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.