]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
efi: Remove arch specific image headers for RISC-V, ARM64 and ARM
authorAtish Patra <atishp@rivosinc.com>
Thu, 9 Mar 2023 23:59:52 +0000 (15:59 -0800)
committerDaniel Kiper <daniel.kiper@oracle.com>
Wed, 29 Mar 2023 18:31:57 +0000 (20:31 +0200)
The arch specific image header details are not very useful as most of
the GRUB just looks at the PE/COFF spec parameters (PE32 magic and
header offset).

Remove the arch specific images headers and define a generic arch
headers that provide enough PE/COFF fields for the GRUB to parse
kernel images correctly.

Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/commands/file.c
grub-core/loader/arm/linux.c
grub-core/loader/arm64/xen_boot.c
grub-core/loader/efi/linux.c
include/grub/arm/linux.h
include/grub/arm64/linux.h [deleted file]
include/grub/efi/efi.h
include/grub/riscv32/linux.h [deleted file]
include/grub/riscv64/linux.h [deleted file]

index 9de00061eb5414ae38d8bd5b696221df303aa642..7c13e976b505b3bf7982dcbd553a77666c1797e4 100644 (file)
 #include <grub/i18n.h>
 #include <grub/file.h>
 #include <grub/elf.h>
+#include <grub/efi/efi.h>
 #include <grub/xen_file.h>
 #include <grub/efi/pe32.h>
 #include <grub/arm/linux.h>
-#include <grub/arm64/linux.h>
 #include <grub/i386/linux.h>
 #include <grub/xnu.h>
 #include <grub/machoload.h>
@@ -391,7 +391,7 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
       }
     case IS_ARM_LINUX:
       {
-       struct linux_arm_kernel_header lh;
+       struct linux_arch_kernel_header lh;
 
        if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
          break;
@@ -412,13 +412,24 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
       }
     case IS_ARM64_LINUX:
       {
-       struct linux_arm64_kernel_header lh;
+       struct linux_arch_kernel_header lh;
 
        if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
          break;
 
-       if (lh.magic ==
-           grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM64_MAGIC_SIGNATURE))
+       /*
+        * The PE/COFF header can be anywhere in the file. Load it from the correct
+        * offset if it is not where it is expected.
+        */
+        if ((grub_uint8_t *) &lh + lh.hdr_offset != (grub_uint8_t *) &lh.pe_image_header)
+        {
+          if (grub_file_seek (file, lh.hdr_offset) == (grub_off_t) -1
+              || grub_file_read (file, &lh.pe_image_header, sizeof (struct grub_pe_image_header))
+                 != sizeof (struct grub_pe_image_header))
+            return grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header");
+        }
+
+       if (lh.pe_image_header.coff_header.machine == grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64))
          {
            ret = 1;
            break;
index f00b538ebad0274aeb2ed14075c0e68542724b8a..19ddedbc2ef9a6bfe197c2f1d6210eef45bef1c8 100644 (file)
@@ -26,6 +26,7 @@
 #include <grub/command.h>
 #include <grub/cache.h>
 #include <grub/cpu/linux.h>
+#include <grub/efi/efi.h>
 #include <grub/lib/cmdline.h>
 #include <grub/linux.h>
 #include <grub/verify.h>
@@ -304,7 +305,7 @@ linux_boot (void)
 static grub_err_t
 linux_load (const char *filename, grub_file_t file)
 {
-  struct linux_arm_kernel_header *lh;
+  struct linux_arch_kernel_header *lh;
   int size;
 
   size = grub_file_size (file);
index 763d87dcde5b28cd6131f0513fc5e5418490f441..26e1472c9d24d18b2eb71f963efb45546a7edbf9 100644 (file)
@@ -27,7 +27,6 @@
 #include <grub/misc.h>
 #include <grub/mm.h>
 #include <grub/types.h>
-#include <grub/cpu/linux.h>
 #include <grub/efi/efi.h>
 #include <grub/efi/fdtload.h>
 #include <grub/efi/memory.h>
@@ -439,7 +438,7 @@ static grub_err_t
 grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
                         int argc, char *argv[])
 {
-  struct linux_arm64_kernel_header lh;
+  struct linux_arch_kernel_header lh;
   grub_file_t file = NULL;
 
   grub_dl_ref (my_mod);
index 48ab34a2565be4c9b0570a1a2d91337b6bf12fe4..15e0686549d7ecca2d718cf6612c8e26aa3ea895 100644 (file)
@@ -25,7 +25,6 @@
 #include <grub/loader.h>
 #include <grub/mm.h>
 #include <grub/types.h>
-#include <grub/cpu/linux.h>
 #include <grub/efi/efi.h>
 #include <grub/efi/fdtload.h>
 #include <grub/efi/memory.h>
index f38e695b1408435265da4434c764062978aebc5a..5b8fb14e08c6b6f46c4d6a12560e28d3ce49a19a 100644 (file)
 
 #include <grub/efi/pe32.h>
 
-#define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818
-
-struct linux_arm_kernel_header {
-  grub_uint32_t code0;
-  grub_uint32_t reserved1[8];
-  grub_uint32_t magic;
-  grub_uint32_t start; /* _start */
-  grub_uint32_t end;   /* _edata */
-  grub_uint32_t reserved2[3];
-  grub_uint32_t hdr_offset;
-#if defined GRUB_MACHINE_EFI
-  struct grub_pe_image_header pe_image_header;
-#endif
-};
-
-#if defined(__arm__)
-# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE
-# define linux_arch_kernel_header linux_arm_kernel_header
-#endif
-
 #if defined GRUB_MACHINE_UBOOT
 # include <grub/uboot/uboot.h>
 # define LINUX_ADDRESS        (start_of_ram + 0x8000)
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h
deleted file mode 100644 (file)
index 3da71a5..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2013  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef GRUB_ARM64_LINUX_HEADER
-#define GRUB_ARM64_LINUX_HEADER 1
-
-#include <grub/types.h>
-#include <grub/efi/pe32.h>
-
-#define GRUB_LINUX_ARM64_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */
-
-/* From linux/Documentation/arm64/booting.txt */
-struct linux_arm64_kernel_header
-{
-  grub_uint32_t code0;         /* Executable code */
-  grub_uint32_t code1;         /* Executable code */
-  grub_uint64_t text_offset;    /* Image load offset */
-  grub_uint64_t res0;          /* reserved */
-  grub_uint64_t res1;          /* reserved */
-  grub_uint64_t res2;          /* reserved */
-  grub_uint64_t res3;          /* reserved */
-  grub_uint64_t res4;          /* reserved */
-  grub_uint32_t magic;         /* Magic number, little endian, "ARM\x64" */
-  grub_uint32_t hdr_offset;    /* Offset of PE/COFF header */
-  struct grub_pe_image_header pe_image_header;
-};
-
-#if defined(__aarch64__)
-# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE
-# define linux_arch_kernel_header linux_arm64_kernel_header
-#endif
-
-#endif /* ! GRUB_ARM64_LINUX_HEADER */
index e61272de533014c28a3fbe6dc6e8bb5a2b195535..444bf5b0b53e31fed6d71e1a5558035d8e503ef0 100644 (file)
 #include <grub/types.h>
 #include <grub/dl.h>
 #include <grub/efi/api.h>
+#include <grub/efi/pe32.h>
+
+#define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818
+
+struct linux_arch_kernel_header {
+  grub_uint32_t code0;
+  grub_uint32_t code1;
+  grub_uint64_t reserved[6];
+  grub_uint32_t magic;
+  grub_uint32_t hdr_offset; /* Offset of PE/COFF header. */
+  struct grub_pe_image_header pe_image_header;
+};
 
 /* Functions.  */
 void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_efi_guid_t *protocol,
@@ -101,7 +113,6 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
 #if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
-#include <grub/cpu/linux.h>
 #include <grub/file.h>
 grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file,
                                                 struct linux_arch_kernel_header *lh);
diff --git a/include/grub/riscv32/linux.h b/include/grub/riscv32/linux.h
deleted file mode 100644 (file)
index 512b777..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2018  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef GRUB_RISCV32_LINUX_HEADER
-#define GRUB_RISCV32_LINUX_HEADER 1
-
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
-
-/* From linux/Documentation/riscv/booting.txt */
-struct linux_riscv_kernel_header
-{
-  grub_uint32_t code0;         /* Executable code */
-  grub_uint32_t code1;         /* Executable code */
-  grub_uint64_t text_offset;   /* Image load offset */
-  grub_uint64_t res0;          /* reserved */
-  grub_uint64_t res1;          /* reserved */
-  grub_uint64_t res2;          /* reserved */
-  grub_uint64_t res3;          /* reserved */
-  grub_uint64_t res4;          /* reserved */
-  grub_uint32_t magic;         /* Magic number, little endian, "RSCV" */
-  grub_uint32_t hdr_offset;    /* Offset of PE/COFF header */
-};
-
-#define linux_arch_kernel_header linux_riscv_kernel_header
-
-#endif /* ! GRUB_RISCV32_LINUX_HEADER */
diff --git a/include/grub/riscv64/linux.h b/include/grub/riscv64/linux.h
deleted file mode 100644 (file)
index 3630c30..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2018  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef GRUB_RISCV64_LINUX_HEADER
-#define GRUB_RISCV64_LINUX_HEADER 1
-
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
-
-#define GRUB_EFI_PE_MAGIC      0x5A4D
-
-/* From linux/Documentation/riscv/booting.txt */
-struct linux_riscv_kernel_header
-{
-  grub_uint32_t code0;         /* Executable code */
-  grub_uint32_t code1;         /* Executable code */
-  grub_uint64_t text_offset;   /* Image load offset */
-  grub_uint64_t res0;          /* reserved */
-  grub_uint64_t res1;          /* reserved */
-  grub_uint64_t res2;          /* reserved */
-  grub_uint64_t res3;          /* reserved */
-  grub_uint64_t res4;          /* reserved */
-  grub_uint32_t magic;         /* Magic number, little endian, "RSCV" */
-  grub_uint32_t hdr_offset;    /* Offset of PE/COFF header */
-};
-
-#define linux_arch_kernel_header linux_riscv_kernel_header
-
-#endif /* ! GRUB_RISCV64_LINUX_HEADER */