Blame SOURCES/0547-pe-add-the-DOS-header-struct-and-fix-some-bad-naming.patch

bf0270
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bf0270
From: Peter Jones <pjones@redhat.com>
bf0270
Date: Fri, 25 Mar 2022 15:40:12 -0400
bf0270
Subject: [PATCH] pe: add the DOS header struct and fix some bad naming.
bf0270
bf0270
In order to properly validate a loaded kernel's support for being loaded
bf0270
without a writable stack or executable, we need to be able to properly
bf0270
parse arbitrary PE headers.
bf0270
bf0270
Currently, pe32.h is written in such a way that the MS-DOS header that
bf0270
tells us where to find the PE header in the binary can't be accessed.
bf0270
Further, for some reason it calls the DOS MZ magic "GRUB_PE32_MAGIC".
bf0270
bf0270
This patch adds the structure for the DOS header, renames the DOS magic
bf0270
define, and adds defines for the actual PE magic.
bf0270
bf0270
Signed-off-by: Peter Jones <pjones@redhat.com>
bf0270
(cherry picked from commit 955f47aa8300387eecf18b0866d21dde7720593d)
bf0270
(cherry picked from commit 662744c2e986cb770fe49e71e019aaf33a66272d)
bf0270
---
bf0270
 grub-core/loader/arm64/linux.c |  2 +-
bf0270
 include/grub/efi/pe32.h        | 28 ++++++++++++++++++++++++++--
bf0270
 2 files changed, 27 insertions(+), 3 deletions(-)
bf0270
bf0270
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
bf0270
index e1923cf725..24ab0f0074 100644
bf0270
--- a/grub-core/loader/arm64/linux.c
bf0270
+++ b/grub-core/loader/arm64/linux.c
bf0270
@@ -57,7 +57,7 @@ grub_armxx_efi_linux_check_image (struct linux_armxx_kernel_header * lh)
bf0270
   if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
bf0270
     return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
bf0270
 
bf0270
-  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
bf0270
+  if ((lh->code0 & 0xffff) != GRUB_DOS_MAGIC)
bf0270
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
bf0270
 		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
bf0270
 
bf0270
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
bf0270
index c03cc599f6..2241f6317b 100644
bf0270
--- a/include/grub/efi/pe32.h
bf0270
+++ b/include/grub/efi/pe32.h
bf0270
@@ -45,7 +45,30 @@
bf0270
 
bf0270
 #define GRUB_PE32_MSDOS_STUB_SIZE	0x80
bf0270
 
bf0270
-#define GRUB_PE32_MAGIC			0x5a4d
bf0270
+#define GRUB_DOS_MAGIC			0x5a4d
bf0270
+
bf0270
+struct grub_dos_header
bf0270
+{
bf0270
+  grub_uint16_t magic;
bf0270
+  grub_uint16_t cblp;
bf0270
+  grub_uint16_t cp;
bf0270
+  grub_uint16_t crlc;
bf0270
+  grub_uint16_t cparhdr;
bf0270
+  grub_uint16_t minalloc;
bf0270
+  grub_uint16_t maxalloc;
bf0270
+  grub_uint16_t ss;
bf0270
+  grub_uint16_t sp;
bf0270
+  grub_uint16_t csum;
bf0270
+  grub_uint16_t ip;
bf0270
+  grub_uint16_t cs;
bf0270
+  grub_uint16_t lfarlc;
bf0270
+  grub_uint16_t ovno;
bf0270
+  grub_uint16_t res0[4];
bf0270
+  grub_uint16_t oemid;
bf0270
+  grub_uint16_t oeminfo;
bf0270
+  grub_uint16_t res1[10];
bf0270
+  grub_uint32_t lfanew;
bf0270
+};
bf0270
 
bf0270
 /* According to the spec, the minimal alignment is 512 bytes...
bf0270
    But some examples (such as EFI drivers in the Intel
bf0270
@@ -271,7 +294,8 @@ struct grub_pe32_section_table
bf0270
 
bf0270
 
bf0270
 
bf0270
-#define GRUB_PE32_SIGNATURE_SIZE 4
bf0270
+#define GRUB_PE32_SIGNATURE_SIZE		4
bf0270
+#define GRUB_PE32_SIGNATURE			"PE\0\0"
bf0270
 
bf0270
 struct grub_pe32_header
bf0270
 {