From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Thu, 9 Sep 2021 10:59:28 -0400
Subject: [PATCH] Arm: check for the PE magic for the compiled arch

In "arm64: Fix EFI loader kernel image allocation", Ben fixed the kernel
alignment to match the alignment given in the PE header.  In doing so, a
check for valid PE magic was added, which was hard-coded to the value
seen on Aarch64 (GRUB_PE32_PE64_MAGIC).

Unfortunately, this code is shared between 64-bit and 32-bit, and so
that value broke 32-bit Arm systems.

This patch adds a constant definition for GRUB_PE32_PEXX_MAGIC, which is
either GRUB_PE32_PE64_MAGIC or GRUB_PE32_PE32_MAGIC, depending on which
platform is being built, and uses it in the header magic check.

Resolves: rhbz#2000756

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/loader/arm64/linux.c | 2 +-
 include/grub/arm/linux.h       | 1 +
 include/grub/arm64/linux.h     | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
index 4a252d5e7e9..f18d90bd749 100644
--- a/grub-core/loader/arm64/linux.c
+++ b/grub-core/loader/arm64/linux.c
@@ -322,7 +322,7 @@ parse_pe_header (void *kernel, grub_uint64_t *total_size,
 
   pe = (void *)((unsigned long)kernel + lh->hdr_offset);
 
-  if (pe->opt.magic != GRUB_PE32_PE64_MAGIC)
+  if (pe->opt.magic != GRUB_PE32_PEXX_MAGIC)
     return grub_error(GRUB_ERR_BAD_OS, "Invalid PE optional header magic");
 
   *total_size   = pe->opt.image_size;
diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h
index b582f67f661..966a5074f53 100644
--- a/include/grub/arm/linux.h
+++ b/include/grub/arm/linux.h
@@ -44,6 +44,7 @@ struct grub_arm_linux_pe_header
 
 #if defined(__arm__)
 # define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE
+# define GRUB_PE32_PEXX_MAGIC GRUB_PE32_PE32_MAGIC
 # define linux_arch_kernel_header linux_arm_kernel_header
 # define grub_armxx_linux_pe_header grub_arm_linux_pe_header
 #endif
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h
index ea030312df3..422bf2bf24b 100644
--- a/include/grub/arm64/linux.h
+++ b/include/grub/arm64/linux.h
@@ -48,6 +48,7 @@ struct grub_arm64_linux_pe_header
 
 #if defined(__aarch64__)
 # define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE
+# define GRUB_PE32_PEXX_MAGIC GRUB_PE32_PE64_MAGIC
 # define linux_arch_kernel_header linux_arm64_kernel_header
 # define grub_armxx_linux_pe_header grub_arm64_linux_pe_header
 #endif
