All of lore.kernel.org
 help / color / mirror / Atom feed
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 v4 1/6] efi: move MS-DOS stub out of generic PE header definition
Date: Thu,  8 Sep 2022 15:30:12 +0200	[thread overview]
Message-ID: <20220908133017.1464494-2-ardb@kernel.org> (raw)
In-Reply-To: <20220908133017.1464494-1-ardb@kernel.org>

The PE/COFF spec permits the COFF signature and file header to appear
anywhere in the file, and the actual offset is recorded in 4 byte
little endian field at offset 0x3c of the image.

When GRUB is emitted as a PE/COFF binary, we reuse the 128 byte MS-DOS
stub (even for non-x86 architectures), putting the COFF signature and
file header at offset 0x80. However, other PE/COFF images may use
different values, and non-x86 Linux kernels use an offset of 0x40
instead.

So let's get rid of the grub_pe32_header struct from pe32.h, given that
it does not represent anything defined by the PE/COFF spec. Instead,
introduce a minimal struct grub_msdos_image_header type based on the
PE/COFF spec's description of the image header, and use the offset
recorded at file position 0x3c to discover the actual location of the PE
signature and the COFF image header.

The remaining fields are moved into a struct grub_coff_image_header,
which we will use later to access COFF header fields of arbitrary
images (and which may therefore appear at different offsets)

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 grub-core/kern/efi/efi.c |  8 ++++++--
 include/grub/efi/pe32.h  | 16 ++++++++++++----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index e8a976a22f15..f85587d66635 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -302,7 +302,8 @@ grub_addr_t
 grub_efi_modules_addr (void)
 {
   grub_efi_loaded_image_t *image;
-  struct grub_pe32_header *header;
+  struct grub_msdos_image_header *dos_header;
+  struct grub_coff_image_header *header;
   struct grub_pe32_coff_header *coff_header;
   struct grub_pe32_section_table *sections;
   struct grub_pe32_section_table *section;
@@ -313,7 +314,10 @@ grub_efi_modules_addr (void)
   if (! image)
     return 0;
 
-  header = image->image_base;
+  dos_header = (struct grub_msdos_image_header *)image->image_base;
+
+  header = (struct grub_coff_image_header *) ((char *) dos_header
+                                              + dos_header->pe_signature_offset);
   coff_header = &(header->coff_header);
   sections
     = (struct grub_pe32_section_table *) ((char *) coff_header
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
index 0ed8781f0376..6688d96c0046 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -48,6 +48,17 @@
 
 #define GRUB_PE32_MAGIC			0x5a4d
 
+struct grub_msdos_image_header
+{
+  /* This is always 'MZ'. (GRUB_PE32_MAGIC)  */
+  grub_uint16_t msdos_magic;
+
+  grub_uint16_t reserved[29];
+
+  /* The file offset of the PE signature and COFF image header. */
+  grub_uint32_t pe_signature_offset;
+};
+
 /* According to the spec, the minimal alignment is 512 bytes...
    But some examples (such as EFI drivers in the Intel
    Sample Implementation) use 32 bytes (0x20) instead, and it seems
@@ -254,11 +265,8 @@ struct grub_pe32_section_table
 
 #define GRUB_PE32_SIGNATURE_SIZE 4
 
-struct grub_pe32_header
+struct grub_coff_image_header
 {
-  /* This should be filled in with GRUB_PE32_MSDOS_STUB.  */
-  grub_uint8_t msdos_stub[GRUB_PE32_MSDOS_STUB_SIZE];
-
   /* This is always PE\0\0.  */
   char signature[GRUB_PE32_SIGNATURE_SIZE];
 
-- 
2.35.1



  reply	other threads:[~2022-09-08 13:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08 13:30 [PATCH v4 0/6] linux: implement LoadFile2 initrd loading Ard Biesheuvel
2022-09-08 13:30 ` Ard Biesheuvel [this message]
2022-09-15 12:18   ` [PATCH v4 1/6] efi: move MS-DOS stub out of generic PE header definition Leif Lindholm
2022-09-15 14:03     ` Ard Biesheuvel
2022-10-14 12:52       ` Daniel Kiper
2022-09-08 13:30 ` [PATCH v4 2/6] linux/arm: unify ARM/arm64 vs Xen PE/COFF header handling Ard Biesheuvel
2022-10-14 13:07   ` Daniel Kiper
2022-09-08 13:30 ` [PATCH v4 3/6] linux/arm: account for COFF headers appearing at unexpected offsets Ard Biesheuvel
2022-10-14 13:25   ` Daniel Kiper
2022-09-08 13:30 ` [PATCH v4 4/6] efi/efinet: Don't close connections at fini_hw() time Ard Biesheuvel
2022-10-14 13:53   ` Daniel Kiper
2022-09-08 13:30 ` [PATCH v4 5/6] efi: implement LoadFile2 initrd loading protocol for Linux Ard Biesheuvel
2022-10-17 15:07   ` Daniel Kiper
2022-09-08 13:30 ` [PATCH v4 6/6] linux: ignore FDT unless we need to modify it Ard Biesheuvel
2022-10-17 15:25   ` Daniel Kiper

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=20220908133017.1464494-2-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.