Skip to contentSkip to conversation

Unify ARM64 & RISC-V Linux Loader

PATCH In review Atish Patra 2 participants 5 messages Mar 10, 2023 – Mar 21, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

v8 cover Unify ARM64 & RISC-V Linux Loader

This series unifies the linux loader for ARM64 & RISC-V. The linux loader for ARM64 is pretty much arch independent. Thus, this series just moves it to the common directory and update the make files.

This series also removes the arch specific kernel image haders for ARM64, RISC-V, ARM as suggested on v6[1].I have verified with coreboot, efi platform options for ARM. I am not that well versed with ARM options. If it causes any more build failure, please scream at me again :).

This series has been tested with OpenSuse image in Qemu for RISC-V. For ARM64,ARM it has been compile tested only.
It would be good to get more testing on ARM64 and real RISC-V boards as well.

[1] https://lists.gnu.org/archive/html/grub-devel/2022-11/msg00042.html

@Daniel:
I have not added your RB tag in PATCH 2 as it is changed a bit after the last round of discussion around ARM headers.

Changes from v7->v8:
1. Removed the arch specific header for ARM as well.
2. Fixed ARM64 load image header with random COFF header placement.
3. Added few grub distro folks for more visibility. Sorry for spam :).

Changes from v6->v7:
1. Rebased on top of the latest upstream.
2. Addressed all the comments on v6.
3. Removed arch specific image header files for ARM64 & RISC-V.

Changes from v5->v6:
1. Rebased on top of Ard's latest LoadFile2 series.

Changes from v4->v5:
1. Removed redundant macros from header file and updated the rv32 kernel header structure.

Changes from v3->V4:
1. Added all the comments on v3.
2. Dropped LoadFile2 patches as Ard's series[1] updated those patches

Atish Patra (3):
loader: Move arm64 linux loader to common code
efi: Remove arch specific image headers for RISC-V, ARM64, ARM
RISC-V: Use common linux loader

grub-core/Makefile.core.def             |  8 ++--
grub-core/commands/file.c               | 23 +++++++---
grub-core/loader/arm/linux.c            |  3 +-
grub-core/loader/arm64/xen_boot.c       |  3 +-
grub-core/loader/{arm64 => efi}/linux.c |  1 -
grub-core/loader/riscv/linux.c          | 59 -------------------------
include/grub/arm/linux.h                | 20 ---------
include/grub/arm64/linux.h              | 48 --------------------
include/grub/efi/efi.h                  | 13 +++++-
include/grub/riscv32/linux.h            | 41 -----------------
include/grub/riscv64/linux.h            | 43 ------------------
11 files changed, 37 insertions(+), 225 deletions(-)

rename grub-core/loader/{arm64 => efi}/linux.c (99%) delete mode 100644 grub-core/loader/riscv/linux.c delete mode 100644 include/grub/arm64/linux.h delete mode 100644 include/grub/riscv32/linux.h delete mode 100644 include/grub/riscv64/linux.h

--
2.25.1

#1 lore raw

PATCH v8 1/3 loader: Move arm64 linux loader to common code

ARM64 linux loader code is written in such a way that it can be reused across different architectures without much change. Move it to common code so that RISC-V doesn't have to define a separate loader.

---
 grub-core/Makefile.core.def             | 4 ++--
 grub-core/loader/{arm64 => efi}/linux.c | 0
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename grub-core/loader/{arm64 => efi}/linux.c (100%)
grub-core/Makefile.core.def +2 -2
index 71093a1..3ec0e7b 100644
@@ -1826,9 +1826,9 @@ module = {
   sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c;
   ia64_efi = loader/ia64/efi/linux.c;
   arm_coreboot = loader/arm/linux.c;
-  arm_efi = loader/arm64/linux.c;
+  arm_efi = loader/efi/linux.c;
   arm_uboot = loader/arm/linux.c;
-  arm64 = loader/arm64/linux.c;
+  arm64 = loader/efi/linux.c;
   riscv32 = loader/riscv/linux.c;
   riscv64 = loader/riscv/linux.c;
   emu = loader/emu/linux.c;
grub-core/loader/efi/linux.c renamed from grub-core/loader/arm64/linux.c +0 -0
similarity index 100%
rename from grub-core/loader/arm64/linux.c
rename to grub-core/loader/efi/linux.c
Signature · 1 line
2.25.1
#2 lore raw

PATCH v8 2/3 efi: Remove arch specific image headers for RISC-V, ARM64, ARM

In reply to Atish Patra

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 grub to parse kernel images correctly.

---
 grub-core/commands/file.c         | 23 +++++++++++----
 grub-core/loader/arm/linux.c      |  3 +-
 grub-core/loader/arm64/xen_boot.c |  3 +-
 grub-core/loader/efi/linux.c      |  1 -
 include/grub/arm/linux.h          | 20 -------------
 include/grub/arm64/linux.h        | 48 -------------------------------
 include/grub/efi/efi.h            | 13 ++++++++-
 include/grub/riscv32/linux.h      | 41 --------------------------
 include/grub/riscv64/linux.h      | 43 ---------------------------
 9 files changed, 33 insertions(+), 162 deletions(-)
 delete mode 100644 include/grub/arm64/linux.h
 delete mode 100644 include/grub/riscv32/linux.h
 delete mode 100644 include/grub/riscv64/linux.h
grub-core/commands/file.c +18 -5
index 9de0006..62ddec4 100644
@@ -25,10 +25,10 @@
 #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,26 @@ 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_le32_compile_time (GRUB_PE32_MACHINE_ARM64))
 	  {
 	    ret = 1;
 	    break;
grub-core/loader/arm/linux.c +2 -1
index f00b538..19ddedb 100644
@@ -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);
grub-core/loader/arm64/xen_boot.c +1 -2
index 763d87d..26e1472 100644
@@ -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);
grub-core/loader/efi/linux.c +0 -1
index 48ab34a..15e0686 100644
@@ -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>
include/grub/arm/linux.h +0 -20
index f38e695..5b8fb14 100644
@@ -24,26 +24,6 @@
 
 #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)
include/grub/arm64/linux.h deleted +0 -48

48 lines not on this page · raw message

include/grub/efi/efi.h +12 -1
index e61272d..329c4f9 100644
@@ -23,6 +23,18 @@
 #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);
include/grub/riscv32/linux.h deleted +0 -41

41 lines not on this page · raw message

include/grub/riscv64/linux.h deleted +0 -43

43 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v8 2/3
#3 lore raw

Re: [v8 PATCH 2/3] efi: Remove arch specific image headers for RISC-V, ARM64, ARM

Quoted 9 lines · Atish Patra

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 grub to parse kernel images correctly.

Now everything builds without any problems...

Thank you for fixing all the issues!

Daniel

#4 lore raw

PATCH v8 3/3 RISC-V: Use common linux loader

In reply to Atish Patra

RISC-V doesn't have to do anything very different from other architectures to loader EFI stub linux kernel. As a result, just use the common linux loader instead of defining a RISC-V specific linux loader.

---
 grub-core/Makefile.core.def    |  4 +--
 grub-core/loader/riscv/linux.c | 59 ----------------------------------
 2 files changed, 2 insertions(+), 61 deletions(-)
 delete mode 100644 grub-core/loader/riscv/linux.c
grub-core/Makefile.core.def +2 -2
index 3ec0e7b..5446099 100644
@@ -1829,8 +1829,8 @@ module = {
   arm_efi = loader/efi/linux.c;
   arm_uboot = loader/arm/linux.c;
   arm64 = loader/efi/linux.c;
-  riscv32 = loader/riscv/linux.c;
-  riscv64 = loader/riscv/linux.c;
+  riscv32 = loader/efi/linux.c;
+  riscv64 = loader/efi/linux.c;
   emu = loader/emu/linux.c;
   common = loader/linux.c;
   common = lib/cmdline.c;
grub-core/loader/riscv/linux.c deleted +0 -59

59 lines not on this page · raw message

Signature · 1 line
2.25.1