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

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