public inbox for linux-efi@vger.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb@kernel.org>
To: grub-devel@nongnu.org
Cc: linux-efi@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	grub-devel@gnu.org, Daniel Kiper <daniel.kiper@oracle.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>
Subject: [PATCH resend 2/9] efi: move MS-DOS stub out of generic PE header definition
Date: Thu, 18 Aug 2022 10:55:32 +0200	[thread overview]
Message-ID: <20220818085540.2075028-3-ardb@kernel.org> (raw)
In-Reply-To: <20220818085540.2075028-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,
use the GRUB_PE32_MSDOS_STUB_SIZE macro explicitly to reference the
COFF header in the only place in the code where we rely on this.

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 | 5 +++--
 include/grub/efi/pe32.h  | 5 +----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index e8a976a22f15..8bef81663853 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -302,7 +302,7 @@ grub_addr_t
 grub_efi_modules_addr (void)
 {
   grub_efi_loaded_image_t *image;
-  struct grub_pe32_header *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 +313,8 @@ grub_efi_modules_addr (void)
   if (! image)
     return 0;
 
-  header = image->image_base;
+  header = (struct grub_coff_image_header *) ((char *) image->image_base
+                                             + GRUB_PE32_MSDOS_STUB_SIZE);
   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..a2da4b318c85 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -254,11 +254,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


  parent reply	other threads:[~2022-08-18  8:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-18  8:55 [PATCH resend 0/9] linux: implement LoadFile2 initrd loading Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 1/9] loader: drop argv[] argument in grub_initrd_load() Ard Biesheuvel
2022-08-18  8:55 ` Ard Biesheuvel [this message]
2022-08-18  8:55 ` [PATCH resend 3/9] arm64/linux: Remove magic number header field check Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 4/9] linux/arm: unify ARM/arm64 vs Xen PE/COFF header handling Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 5/9] linux/arm: account for COFF headers appearing at unexpected offsets Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 6/9] efi: add definition of LoadFile2 protocol Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 7/9] efi/efinet: Don't close connections at fini_hw() time Ard Biesheuvel
2022-08-18  8:55 ` [PATCH resend 8/9] efi: implement LoadFile2 initrd loading protocol for Linux Ard Biesheuvel
2022-09-08  7:29   ` Ilias Apalodimas
2022-08-18  8:55 ` [PATCH resend 9/9] linux: ignore FDT unless we need to modify it Ard Biesheuvel
2022-08-18  8:58 ` [PATCH resend 0/9] linux: implement LoadFile2 initrd loading 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=20220818085540.2075028-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=grub-devel@nongnu.org \
    --cc=heinrich.schuchardt@canonical.com \
    --cc=julian.klode@canonical.com \
    --cc=linux-efi@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox