efi: Move MS-DOS stub out of generic PE header definition

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_pe_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>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
This commit is contained in:
Wentao Guan
2024-08-25 18:22:55 +08:00
parent 018b8e8ec0
commit 280867088c
5 changed files with 31 additions and 133 deletions

View File

@@ -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 image header. */
grub_uint32_t pe_image_header_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
@@ -257,11 +268,8 @@ struct grub_pe32_section_table
#define GRUB_PE32_SIGNATURE_SIZE 4
struct grub_pe32_header
struct grub_pe_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];