Skip to contentSkip to conversation

Unify ARM64 & RISC-V Linux Loader

PATCH Superseded Atish Patra 7 participants 23 messages Nov 4, 2022 – Dec 14, 2022 RISC-V

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 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 is rebased on top of Ard's LoadFile2 series[1].

The unification effort was a little mess and I was to blame :(. I started the effort but couldn't follow up after. Nikita picked it up and rebased all the patches along with LoadFile2.

However, Nikita did not follow up after v3 as well. Ard revised his LoadFile2 series few weeks back. As the ocotober deadline for next grub release is closing, I decided to rebase the patches so that RISC-V support can be officially part of the release. Sorry for the mess/confusion.

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

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

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

Atish Patra (3):
loader: Move arm64 linux loader to common code
RISC-V: Update image header
RISC-V: Use common linux loader

grub-core/Makefile.core.def             |  8 ++--
grub-core/loader/{arm64 => efi}/linux.c |  0
grub-core/loader/riscv/linux.c          | 59 -------------------------
include/grub/riscv32/linux.h            | 19 ++++----
include/grub/riscv64/linux.h            | 19 ++++----
5 files changed, 25 insertions(+), 80 deletions(-)

rename grub-core/loader/{arm64 => efi}/linux.c (100%) delete mode 100644 grub-core/loader/riscv/linux.c

--
2.25.1

#1 lore raw

PATCH v6 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 5212dfab1369..ce95c76eaffa 100644
@@ -1817,9 +1817,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;
   common = loader/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

Re: [v6 PATCH 1/3] loader: Move arm64 linux loader to common code

Quoted 5 lines · Atish Patra

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.

This line should go...

Quoted 1 line

... here...

Daniel

#3 lore raw

PATCH v6 2/3 RISC-V: Update image header

In reply to Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +10 -9
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset;	/* Image load offset, little endian */
+  grub_uint64_t image_size;	/* Effective Image size, little endian */
+  grub_uint64_t flags;		/* kernel flags, little endian */
+  grub_uint32_t version;	/* Version of this header */
+  grub_uint32_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_uint64_t magic;		/* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;		/* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;	/* Offset of PE/COFF header */
+
+  struct grub_pe_image_header pe_image_header;
 };
 
 #define linux_arch_kernel_header linux_riscv_kernel_header
include/grub/riscv64/linux.h +11 -8
index 3630c30fbf1a..a69046de34ef 100644
@@ -19,23 +19,26 @@
 #ifndef GRUB_RISCV64_LINUX_HEADER
 #define GRUB_RISCV64_LINUX_HEADER 1
 
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
+#include <grub/efi/pe32.h>
 
 #define GRUB_EFI_PE_MAGIC	0x5A4D
 
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset;	/* Image load offset, little endian */
+  grub_uint64_t image_size;	/* Effective Image size, little endian */
+  grub_uint64_t flags;		/* kernel flags, little endian */
+  grub_uint32_t version;	/* Version of this header */
+  grub_uint32_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_uint64_t magic;		/* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;		/* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;	/* Offset of PE/COFF header */
+
+  struct grub_pe_image_header pe_image_header;
 };
 
 #define linux_arch_kernel_header linux_riscv_kernel_header
Signature · 1 line
2.25.1
#4 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image-header.rst is 1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Quoted 2 lines

The order of these lines should be reversed...

Quoted 26 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +3 -6
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset;	/* Image load offset, little endian */
+  grub_uint64_t image_size;	/* Effective Image size, little endian */

include/grub/riscv32/linux.h line 35

Quoted 10 lines
include/grub/riscv32/linux.h +5 -3
+  grub_uint64_t flags;		/* kernel flags, little endian */
+  grub_uint32_t version;	/* Version of this header */
+  grub_uint32_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_uint64_t magic;		/* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;		/* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;	/* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

include/grub/riscv32/linux.h line 37

Quoted 2 lines
include/grub/riscv32/linux.h +2 -0
+
+  struct grub_pe_image_header pe_image_header;

This struct should not be part of linux_riscv_kernel_header struct. Please take a look at the commit 6d7bb89ef (efi: Move MS-DOS stub out of generic PE header definition). And right now I can see this comes from ARM headers. I am not sure why we did not spotted and fixed this issue when we worked on LoadFile2 series. Atish, could you fix this too? Of course in separate patch before this one. And if you could align ARM headers structs with current Linux documentation that would be perfect...

Same comments apply below...

Quoted 30 lines
include/grub/riscv32/linux.h +0 -0
 };
 
 #define linux_arch_kernel_header linux_riscv_kernel_header
include/grub/riscv64/linux.h +7 -5
index 3630c30fbf1a..a69046de34ef 100644
@@ -19,23 +19,26 @@
 #ifndef GRUB_RISCV64_LINUX_HEADER
 #define GRUB_RISCV64_LINUX_HEADER 1
 
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
+#include <grub/efi/pe32.h>
 
 #define GRUB_EFI_PE_MAGIC	0x5A4D
 
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset;	/* Image load offset, little endian */
+  grub_uint64_t image_size;	/* Effective Image size, little endian */
+  grub_uint64_t flags;		/* kernel flags, little endian */
+  grub_uint32_t version;	/* Version of this header */
+  grub_uint32_t res1;		/* reserved */

include/grub/riscv64/linux.h line 42

Quoted 10 lines
include/grub/riscv64/linux.h +4 -3
   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_uint64_t magic;		/* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;		/* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;	/* Offset of PE/COFF header */
+
+  struct grub_pe_image_header pe_image_header;
 };

Daniel

#5 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 54 lines · Daniel Kiper
Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image-header.rst is 1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Quoted 2 lines

The order of these lines should be reversed...

Quoted 26 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +3 -6
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */

include/grub/riscv32/linux.h line 35

Quoted 10 lines
include/grub/riscv32/linux.h +5 -3
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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_uint64_t magic;               /* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;              /* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */
Quoted 8 lines

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

#6 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 70 lines · Ard Biesheuvel
Quoted 8 lines · Daniel Kiper
Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve

compatibility with the ARM64 header")

Quoted 2 lines

The latest commit which updates

Documentation/riscv/boot-image-header.rst is

Quoted 5 lines

1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Quoted 2 lines
Quoted 8 lines

The order of these lines should be reversed...

Review

Quoted 6 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
(quoted diff) +0 -0

b/include/grub/riscv32/linux.h

Quoted 19 lines
Quoted 19 lines

index 512b777c8a41..a884d4f4760c 100644

include/grub/riscv32/linux.h +2 -6
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset; /* Image load offset, little endian */
Quoted 8 lines

include/grub/riscv32/linux.h

Quoted 8 lines
include/grub/riscv32/linux.h +4 -3
+  grub_uint64_t image_size;  /* Effective Image size, little endian */
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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" */

Quoted 1 line

include/grub/riscv32/linux.h line 33

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint64_t magic;               /* magic (RISC-V specifc,

deprecated)*/

Quoted 1 line

include/grub/riscv32/linux.h line 34

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint32_t magic2;              /* Magic number 2 (to match the

ARM64 'magic' field pos) */

Quoted 10 lines

include/grub/riscv32/linux.h line 35

Quoted 1 line
include/grub/riscv32/linux.h +0 -0
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Quoted 7 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

If I understand you correctly, you are suggesting to remove the linux_arm64_kernel_header/linux_riscv_kernel_header
and just define a generic linux_arch_kernel_header which
has pe_image_header at the correct offset.
grub_cmd_file() can just compare the machine type instead of the magic number for the IS_ARM64_LINUX case.
The other places using linux_arm64_kernel_header will just use the generic structure to compute the section_alignment field from the PE header.

#7 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 77 lines · Atish Kumar Patra
Quoted 65 lines · Ard Biesheuvel
Quoted 54 lines · Daniel Kiper
Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image-header.rst is 1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Quoted 2 lines

The order of these lines should be reversed...

Quoted 26 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +3 -6
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */

include/grub/riscv32/linux.h line 35

Quoted 10 lines
include/grub/riscv32/linux.h +5 -3
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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_uint64_t magic;               /* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;              /* Magic number 2 (to match the ARM64 'magic' field pos) */
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */
Quoted 8 lines

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Quoted 6 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

Quoted 5 lines

If I understand you correctly, you are suggesting to remove the linux_arm64_kernel_header/linux_riscv_kernel_header
and just define a generic linux_arch_kernel_header which has pe_image_header at the correct offset.
grub_cmd_file() can just compare the machine type instead of the magic number for the IS_ARM64_LINUX case.
The other places using linux_arm64_kernel_header will just use the generic structure to compute the section_alignment field from the PE header.

Exactly.

#8 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 91 lines · Ard Biesheuvel
Quoted 9 lines · Atish Kumar Patra
Quoted 5 lines · Ard Biesheuvel
Quoted 3 lines · Daniel Kiper
Quoted 1 line · Atish Patra

Update the RISC-V Linux kernel image headers as per the current

header.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

474efecb65dc: ("riscv: modify the Image header to improve

compatibility with the ARM64 header")

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The latest commit which updates

Documentation/riscv/boot-image-header.rst is

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Quoted 2 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

The order of these lines should be reversed...

Review

Quoted 6 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
(quoted diff) +0 -0

b/include/grub/riscv32/linux.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index 512b777c8a41..a884d4f4760c 100644

include/grub/riscv32/linux.h +0 -0
@@ -19,21 +19,22 @@
 #ifndef GRUB_RISCV32_LINUX_HEADER
 #define GRUB_RISCV32_LINUX_HEADER 1
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
include/grub/riscv32/linux.h +1 -3
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
-
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 struct linux_riscv_kernel_header
Quoted 8 lines
Quoted 8 lines

include/grub/riscv32/linux.h

Quoted 8 lines
include/grub/riscv32/linux.h +2 -3
 {
   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 text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little

endian */

Quoted 1 line
Quoted 1 line
Quoted 1 line

include/grub/riscv32/linux.h line 29

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint64_t flags;               /* kernel flags, little

endian */

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

include/grub/riscv32/linux.h

Quoted 6 lines
include/grub/riscv32/linux.h +2 -3
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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" */

Quoted 1 line
Quoted 1 line
Quoted 1 line

include/grub/riscv32/linux.h line 33

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint64_t magic;               /* magic (RISC-V specifc,

deprecated)*/

Quoted 1 line
Quoted 1 line
Quoted 1 line

include/grub/riscv32/linux.h line 34

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint32_t magic2;              /* Magic number 2 (to match

the ARM64 'magic' field pos) */

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

include/grub/riscv32/linux.h line 35

Quoted 1 line
include/grub/riscv32/linux.h +0 -0
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Can we get rid of these header definitions entirely?

Quoted 7 lines
Quoted 4 lines

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

If I understand you correctly, you are suggesting to remove the

linux_arm64_kernel_header/linux_riscv_kernel_header

Quoted 1 line

and just define a generic linux_arch_kernel_header which has

Quoted 9 lines

pe_image_header at the correct offset.

Quoted 1 line

grub_cmd_file() can just compare the machine type instead of the magic

number for the IS_ARM64_LINUX case.

Quoted 1 line

The other places using linux_arm64_kernel_header will just use the

generic structure to compute the section_alignment field from the PE header.

Quoted 1 line

Exactly.

Thanks for the prompt response. I will take a stab at it and revise the series.

#9 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

In reply to Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel
Quoted 7 lines

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Quoted 6 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

Right, so your patch from August dropped that check in the loader itself. I missed that one.

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

/
Leif

#10 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 17 lines · Leif Lindholm
Quoted 8 lines · Ard Biesheuvel
Quoted 7 lines

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Quoted 6 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

Quoted 8 lines

Right, so your patch from August dropped that check in the loader itself. I missed that one.

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case, and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

#11 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 15 lines · Ard Biesheuvel
Quoted 7 lines
Quoted 6 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

Quoted 7 lines

Right, so your patch from August dropped that check in the loader itself. I missed that one.

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

Quoted 6 lines

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Is that true for arm32 as well?

Certainly for arm64, *a* change was needed.

Quoted 3 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The change may have been the appropriate compromise, but it wasn't treated as one.

/
Leif

#12 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 17 lines · Leif Lindholm
Quoted 15 lines · Ard Biesheuvel
Quoted 14 lines
Quoted 6 lines

Can we get rid of these header definitions entirely?

The only GRUB code that seems to care about the fields that are not defined in the PE/COFF spec is grub_cmd_file(), which currently parses the magic field, but given that we only support EFI boot anyway, that code should just parse the PE machine type instead.

Right, so your patch from August dropped that check in the loader itself. I missed that one.

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

Quoted 9 lines
Quoted 6 lines

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Is that true for arm32 as well?

Is what true?

Quoted 2 lines

Certainly for arm64, *a* change was needed.

RISC-V, arm64 and LoongArch now all use the same compressed format, which can decompress itself when executed as a EFI binary, or be decompressed by a non-EFI loader using the metadata in the header.

So it might make sense for GRUB to be able to identify this image type specifically as well, but only if that information is useful in some way.

Quoted 7 lines
Quoted 3 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

I am not following you here.

Quoted 3 lines

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The sanity check had to be dropped because not all EFI Linux images carry the magic number.

Whether or not the chainloader and the linux loader are the same thing is a different matter.

Quoted 3 lines

The change may have been the appropriate compromise, but it wasn't treated as one.

It is not a compromise. GRUB should not reason about whether or not a Linux EFI image is also bootable via the bare metal boot protocol which it does not implement.

#13 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 8 lines · Ard Biesheuvel
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Is that true for arm32 as well?

Is what true?

That arm32 linux images containing an EFI stub do not always contain the magic field?

Quoted 9 lines
Quoted 1 line

Certainly for arm64, *a* change was needed.

RISC-V, arm64 and LoongArch now all use the same compressed format, which can decompress itself when executed as a EFI binary, or be decompressed by a non-EFI loader using the metadata in the header.

So it might make sense for GRUB to be able to identify this image type specifically as well, but only if that information is useful in some way.

In order to be able to indicate to a user, who may *not* be a kernel developer, or even be aware of the concept of different types of kernel images, that they picked the wrong image some other way than the boot failing.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

Quoted 7 lines

I am not following you here.

Quoted 2 lines

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The sanity check had to be dropped because not all EFI Linux images carry the magic number.

No, the sanity check had to be *changed* because not all EFI Linux images carry the magic number.

If there really is no way to tell the EFI xboot kernel from any other EFI executable for the same architecture, then:
- that's going to be reasonably annoying also from within the OS when using the file command.
- we have lost the ability to warn users they (or their cross-upgrade) picked the wrong file.

Quoted 2 lines

Whether or not the chainloader and the linux loader are the same thing is a different matter.

You brought that point up, not me.
I agree the chainloader should not go looking for further magic.

Quoted 6 lines
Quoted 2 lines

The change may have been the appropriate compromise, but it wasn't treated as one.

It is not a compromise. GRUB should not reason about whether or not a Linux EFI image is also bootable via the bare metal boot protocol which it does not implement.

GRUB should, where reasonable, provide helpful messages to end users.

That means a loader for a specific operating system should do basic sanity checks to verify that the image is for that operating system where at all possible. Otherwise, it should not be using an os-specific loader.

Of course, if the operating system decides that's an antifeature, it can always choose to not provide identifiable images.

/
Leif

#14 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 14 lines · Leif Lindholm
Quoted 12 lines · Ard Biesheuvel
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Is that true for arm32 as well?

Is what true?

That arm32 linux images containing an EFI stub do not always contain the magic field?

No, but I'm not sure how that matters. We will have a generic EFI loader for arm64, ARM and RISC-V, and shortly also for LoongArch. Those images may not have magic numbers because they do not support bare metal boot only EFI boot.

The fact that none of the images in the latter category happen to be arm32 seems hardly relevant to me.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Certainly for arm64, *a* change was needed.

RISC-V, arm64 and LoongArch now all use the same compressed format, which can decompress itself when executed as a EFI binary, or be decompressed by a non-EFI loader using the metadata in the header.

Quoted 9 lines
Quoted 3 lines

So it might make sense for GRUB to be able to identify this image type specifically as well, but only if that information is useful in some way.

In order to be able to indicate to a user, who may *not* be a kernel developer, or even be aware of the concept of different types of kernel images, that they picked the wrong image some other way than the boot failing.

OK, so that information *is* useful in some way. Fair enough.

Quoted 18 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

Quoted 7 lines

I am not following you here.

Quoted 2 lines

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The sanity check had to be dropped because not all EFI Linux images carry the magic number.

No, the sanity check had to be *changed* because not all EFI Linux images carry the magic number.

Quoted 7 lines

If there really is no way to tell the EFI xboot kernel from any other EFI executable for the same architecture, then:
- that's going to be reasonably annoying also from within the OS when using the file command.
- we have lost the ability to warn users they (or their cross-upgrade) picked the wrong file.

There is. The zboot image header clearly spells out 'zimg', the compression type and the start and size of the compressed payload inside the EFI binary.

Quoted 6 lines
Quoted 2 lines

Whether or not the chainloader and the linux loader are the same thing is a different matter.

You brought that point up, not me.
I agree the chainloader should not go looking for further magic.

Ok, at least we agree on something :-)

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

The change may have been the appropriate compromise, but it wasn't treated as one.

It is not a compromise. GRUB should not reason about whether or not a Linux EFI image is also bootable via the bare metal boot protocol which it does not implement.

GRUB should, where reasonable, provide helpful messages to end users.

Quoted 8 lines

That means a loader for a specific operating system should do basic sanity checks to verify that the image is for that operating system where at all possible. Otherwise, it should not be using an os-specific loader.

Of course, if the operating system decides that's an antifeature, it can always choose to not provide identifiable images.

In that case, we should add something to the PE headers of Linux kernels built for any architecture to identify it as such. And we should add the same thing to the zboot images, so they are identifiable both as a zboot image and as a linux kernel.

#15 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 41 lines · Ard Biesheuvel
Quoted 14 lines · Leif Lindholm
Quoted 12 lines · Ard Biesheuvel
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Is that true for arm32 as well?

Is what true?

That arm32 linux images containing an EFI stub do not always contain the magic field?

No, but I'm not sure how that matters. We will have a generic EFI loader for arm64, ARM and RISC-V, and shortly also for LoongArch. Those images may not have magic numbers because they do not support bare metal boot only EFI boot.

The fact that none of the images in the latter category happen to be arm32 seems hardly relevant to me.

Quoted 10 lines
Quoted 9 lines
Quoted 1 line

Certainly for arm64, *a* change was needed.

RISC-V, arm64 and LoongArch now all use the same compressed format, which can decompress itself when executed as a EFI binary, or be decompressed by a non-EFI loader using the metadata in the header.

So it might make sense for GRUB to be able to identify this image type specifically as well, but only if that information is useful in some way.

Quoted 7 lines
Quoted 5 lines

In order to be able to indicate to a user, who may *not* be a kernel developer, or even be aware of the concept of different types of kernel images, that they picked the wrong image some other way than the boot failing.

OK, so that information *is* useful in some way. Fair enough.

In the U-Boot repository we had to add kernel signatures to the EFI header to be able to run test applications initrddump.efi and dtbdump.efi from GRUB. Running these via chainloader does not make sense as this would not expose device trees or intirds provided by GRUB.

Quoted 54 lines
Quoted 19 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

Quoted 7 lines

I am not following you here.

Quoted 2 lines

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The sanity check had to be dropped because not all EFI Linux images carry the magic number.

No, the sanity check had to be *changed* because not all EFI Linux images carry the magic number.

Quoted 7 lines

If there really is no way to tell the EFI xboot kernel from any other EFI executable for the same architecture, then:
- that's going to be reasonably annoying also from within the OS when using the file command.
- we have lost the ability to warn users they (or their cross-upgrade) picked the wrong file.

There is. The zboot image header clearly spells out 'zimg', the compression type and the start and size of the compressed payload inside the EFI binary.

Quoted 6 lines
Quoted 2 lines

Whether or not the chainloader and the linux loader are the same thing is a different matter.

You brought that point up, not me.
I agree the chainloader should not go looking for further magic.

Ok, at least we agree on something :-)

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

The change may have been the appropriate compromise, but it wasn't treated as one.

It is not a compromise. GRUB should not reason about whether or not a Linux EFI image is also bootable via the bare metal boot protocol which it does not implement.

GRUB should, where reasonable, provide helpful messages to end users.

That means a loader for a specific operating system should do basic sanity checks to verify that the image is for that operating system where at all possible. Otherwise, it should not be using an os-specific loader.

Quoted 8 lines
Quoted 3 lines

Of course, if the operating system decides that's an antifeature, it can always choose to not provide identifiable images.

In that case, we should add something to the PE headers of Linux kernels built for any architecture to identify it as such. And we should add the same thing to the zboot images, so they are identifiable both as a zboot image and as a linux kernel.

We should not require that the linux command only works for EFI binaries that are Linux kernels. Being able to load a device-tree via the devicetree command and then executing an EFI binary is useful for other operating systems too.

Putting a devicetree loader in front of GRUB like the AArch64 Laptop project did is not very attractive.

Best regards

Heinrich

#16 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

In reply to Ard Biesheuvel
Quoted 103 lines · Ard Biesheuvel
Quoted 14 lines · Leif Lindholm
Quoted 12 lines · Ard Biesheuvel
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines

The drawback to that is that not all EFI executables are destined for the Linux loader. So while trying to boot them using the linux loader is definitely user error, that change removed a potentially useful user-visible error message.

The new EFI zboot images don't have the arch specific magic numbers either, and those are Linux images too.

So pretending that Linux EFI PE/COFF images are always hybrid images that could also boot in bare metal mode is no longer appropriate in any case,

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Is that true for arm32 as well?

Is what true?

That arm32 linux images containing an EFI stub do not always contain the magic field?

No, but I'm not sure how that matters. We will have a generic EFI loader for arm64, ARM and RISC-V, and shortly also for LoongArch. Those images may not have magic numbers because they do not support bare metal boot only EFI boot.

The fact that none of the images in the latter category happen to be arm32 seems hardly relevant to me.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Certainly for arm64, *a* change was needed.

RISC-V, arm64 and LoongArch now all use the same compressed format, which can decompress itself when executed as a EFI binary, or be decompressed by a non-EFI loader using the metadata in the header.

Quoted 9 lines
Quoted 3 lines

So it might make sense for GRUB to be able to identify this image type specifically as well, but only if that information is useful in some way.

In order to be able to indicate to a user, who may *not* be a kernel developer, or even be aware of the concept of different types of kernel images, that they picked the wrong image some other way than the boot failing.

OK, so that information *is* useful in some way. Fair enough.

Quoted 18 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

and we should really just deal with the fact that the linux loader and the chainloader are mostly the same thing on EFI-only architectures.

Architectures that only support *linux* booting via EFI. Which arm32 isn't.

Quoted 7 lines

I am not following you here.

Quoted 2 lines

*Dealing* with that would mean merging the linux- and chain- loaders. Not dropping sanity checks and keeping both.

The sanity check had to be dropped because not all EFI Linux images carry the magic number.

No, the sanity check had to be *changed* because not all EFI Linux images carry the magic number.

Quoted 7 lines

If there really is no way to tell the EFI xboot kernel from any other EFI executable for the same architecture, then:
- that's going to be reasonably annoying also from within the OS when using the file command.
- we have lost the ability to warn users they (or their cross-upgrade) picked the wrong file.

There is. The zboot image header clearly spells out 'zimg', the compression type and the start and size of the compressed payload inside the EFI binary.

Quoted 6 lines
Quoted 2 lines

Whether or not the chainloader and the linux loader are the same thing is a different matter.

You brought that point up, not me.
I agree the chainloader should not go looking for further magic.

Ok, at least we agree on something :-)

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

The change may have been the appropriate compromise, but it wasn't treated as one.

It is not a compromise. GRUB should not reason about whether or not a Linux EFI image is also bootable via the bare metal boot protocol which it does not implement.

GRUB should, where reasonable, provide helpful messages to end users.

That means a loader for a specific operating system should do basic sanity checks to verify that the image is for that operating system where at all possible. Otherwise, it should not be using an os-specific loader.

Quoted 8 lines
Quoted 3 lines

Of course, if the operating system decides that's an antifeature, it can always choose to not provide identifiable images.

In that case, we should add something to the PE headers of Linux kernels built for any architecture to identify it as such. And we should add the same thing to the zboot images, so they are identifiable both as a zboot image and as a linux kernel.

OK, with the exception of arm32 (which should not hold us back imho), we could use offset 0x30 in the DOS header of all architectures as well as the zboot image to carry a magic number that conveys 'linux'

RISC-V and arm64 already put their own magic numbers there. I propose to introduce a single new magic number value meaning 'generic linux, go and look at the PE machine type' that we can use for loongarch and for the zboot image, and for x86 in the future as well, once we merge the EFI loader support with x86 too.

To Heinrich's point, I agree that rejecting a PE/COFF image that does not carry the expected magic number does not make sense, and so dropping the check in the loader patch was the right thing to do imo.

But for identifying Linux images as such, I agree it makes sense to retain some of that logic.

So the generic linux arch efi image header will have the magic at offset 0x30, and only arm32 will need to provide its own definition

#17 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

In reply to Daniel Kiper
Quoted 6 lines · Daniel Kiper
Quoted 5 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

Quoted 8 lines
Quoted 1 line

474efecb65dc: ("riscv: modify the Image header to improve compatibility

with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image-header.rst is
1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Yes. I was pointing out the commit the image header was actually modified. I will modify the commit text to reflect the latest commit for the documentation as you suggested.

Quoted 6 lines
Quoted 2 lines

The order of these lines should be reversed...

Sure. Will do.

Quoted 109 lines
Quoted 23 lines
---
 include/grub/riscv32/linux.h | 19 ++++++++++---------
 include/grub/riscv64/linux.h | 19 +++++++++++--------
 2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +1 -5
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
 #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 */

include/grub/riscv32/linux.h line 35

Quoted 10 lines
include/grub/riscv32/linux.h +5 -4
-  grub_uint64_t res1;                /* reserved */
+  grub_uint64_t text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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" */

include/grub/riscv32/linux.h line 33

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint64_t magic;               /* magic (RISC-V specifc,

deprecated)*/

include/grub/riscv32/linux.h line 34

Quoted 1 line
include/grub/riscv32/linux.h +1 -0
+  grub_uint32_t magic2;              /* Magic number 2 (to match the

ARM64 'magic' field pos) */

include/grub/riscv32/linux.h line 35

Quoted 1 line
include/grub/riscv32/linux.h +0 -0
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3. So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

include/grub/riscv32/linux.h line 37

Quoted 2 lines
include/grub/riscv32/linux.h +2 -0
+
+  struct grub_pe_image_header pe_image_header;

This struct should not be part of linux_riscv_kernel_header struct. Please take a look at the commit 6d7bb89ef (efi: Move MS-DOS stub out of generic PE header definition). And right now I can see this comes from ARM headers. I am not sure why we did not spotted and fixed this issue when we worked on LoadFile2 series. Atish, could you fix this too? Of course in separate patch before this one. And if you could align ARM headers structs with current Linux documentation that would be perfect...

Same comments apply below...

Quoted 24 lines
include/grub/riscv32/linux.h +0 -0
 };
 
 #define linux_arch_kernel_header linux_riscv_kernel_header
include/grub/riscv64/linux.h +2 -4
index 3630c30fbf1a..a69046de34ef 100644
@@ -19,23 +19,26 @@
 #ifndef GRUB_RISCV64_LINUX_HEADER
 #define GRUB_RISCV64_LINUX_HEADER 1
 
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
+#include <grub/efi/pe32.h>
 
 #define GRUB_EFI_PE_MAGIC    0x5A4D
 
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */
 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 */

include/grub/riscv64/linux.h line 37

Quoted 10 lines
include/grub/riscv64/linux.h +5 -4
-  grub_uint64_t res1;                /* reserved */
+  grub_uint64_t text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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" */

include/grub/riscv64/linux.h line 37

Quoted 1 line
include/grub/riscv64/linux.h +1 -0
+  grub_uint64_t magic;               /* magic (RISC-V specifc,

deprecated)*/

include/grub/riscv64/linux.h line 38

Quoted 1 line
include/grub/riscv64/linux.h +1 -0
+  grub_uint32_t magic2;              /* Magic number 2 (to match the

ARM64 'magic' field pos) */

include/grub/riscv64/linux.h line 42

Quoted 4 lines
include/grub/riscv64/linux.h +2 -0
   grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */
+
+  struct grub_pe_image_header pe_image_header;
 };

Daniel

#18 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Is there a new patch?

Quoted 164 lines · 在 2022-11-08星期二的 23:59 -0800,Atish Kumar Patra写道:
Quoted 7 lines · Daniel Kiper
Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

Quoted 9 lines
Quoted 2 lines

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image- header.rst is
1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Yes. I was pointing out the commit the image header was actually modified.
I will modify the commit text to reflect the latest commit for the documentation as you suggested.

Quoted 6 lines
Quoted 2 lines

The order of these lines should be reversed...

Sure. Will do.

Quoted 56 lines
Quoted 35 lines

---
  include/grub/riscv32/linux.h | 19 ++++++++++--------- include/grub/riscv64/linux.h | 19 +++++++++++--------

  2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +1 -3
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
   #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */

  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 text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */ +  grub_uint32_t res1;                /* reserved */ grub_uint64_t res2;                /* reserved */
-  grub_uint64_t res3;                /* reserved */

Quoted 8 lines

-  grub_uint64_t res4;                /* reserved */
-  grub_uint32_t magic;               /* Magic number, little endian, "RSCV" */
+  grub_uint64_t magic;               /* magic (RISC-V specifc, deprecated)*/
+  grub_uint32_t magic2;              /* Magic number 2 (to match the ARM64 'magic' field pos) */
    grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3.
So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

Quoted 2 lines

+
+  struct grub_pe_image_header pe_image_header;

Quoted 14 lines

This struct should not be part of linux_riscv_kernel_header struct. Please take a look at the commit 6d7bb89ef (efi: Move MS-DOS stub out of
generic PE header definition). And right now I can see this comes from
ARM headers. I am not sure why we did not spotted and fixed this issue
when we worked on LoadFile2 series. Atish, could you fix this too? Of
course in separate patch before this one. And if you could align ARM
headers structs with current Linux documentation that would be perfect...

Quoted 43 lines

Same comments apply below...

Quoted 41 lines

  };

  #define linux_arch_kernel_header linux_riscv_kernel_header

include/grub/riscv64/linux.h +2 -2
index 3630c30fbf1a..a69046de34ef 100644
@@ -19,23 +19,26 @@
   #ifndef GRUB_RISCV64_LINUX_HEADER
   #define GRUB_RISCV64_LINUX_HEADER 1
 
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
+#include <grub/efi/pe32.h>
 
   #define GRUB_EFI_PE_MAGIC    0x5A4D
 
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */

  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 text_offset; /* Image load offset, little endian */
+  grub_uint64_t image_size;  /* Effective Image size, little endian */
+  grub_uint64_t flags;               /* kernel flags, little endian */
+  grub_uint32_t version;     /* Version of this header */ +  grub_uint32_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_uint64_t magic;               /* magic (RISC-V specifc, deprecated)*/

Quoted 8 lines
Quoted 6 lines

+  grub_uint32_t magic2;              /* Magic number 2 (to match the ARM64 'magic' field pos) */
    grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */ +
+  struct grub_pe_image_header pe_image_header;
  };

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Best Regards
Xiaotian Wu
#19 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 3 lines · Xiaotian Wu

Is there a new patch?

Not sure if you are asking about this series or Ard's series [1]. I got busy with other day jobs and did not get time to revise this series as that requires a bit of work
to make sure that it doesn't break ARM64 (by removing the arch specific header files).
I am planning to look into this during the holidays. If anybody else has some free cycles to revise
the series, please let me know. Apologies for the delay.

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

Quoted 170 lines · 在 2022-11-08星期二的 23:59 -0800,Atish Kumar Patra写道:
Quoted 160 lines
Quoted 7 lines · Daniel Kiper
Quoted 6 lines · Atish Patra

Update the RISC-V Linux kernel image headers as per the current header.

Reference:
<Linux kernel source>/Documentation/riscv/boot-image-header.rst

Quoted 9 lines
Quoted 2 lines

474efecb65dc: ("riscv: modify the Image header to improve compatibility with the ARM64 header")

The latest commit which updates Documentation/riscv/boot-image- header.rst is
1d5c17e47028 (RISC-V: Typo fixes in image header and documentation).

Yes. I was pointing out the commit the image header was actually modified.
I will modify the commit text to reflect the latest commit for the documentation as you suggested.

Quoted 6 lines
Quoted 2 lines

The order of these lines should be reversed...

Sure. Will do.

Quoted 115 lines
Quoted 37 lines
---
  include/grub/riscv32/linux.h | 19 ++++++++++---------
  include/grub/riscv64/linux.h | 19 +++++++++++--------
  2 files changed, 21 insertions(+), 17 deletions(-)
include/grub/riscv32/linux.h +3 -6
index 512b777c8a41..a884d4f4760c 100644
@@ -19,21 +19,22 @@
  #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 */
+/* From linux/Documentation/riscv/boot-image-header.rst */
  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 text_offset; /* Image load offset, little endian
 */
+  grub_uint64_t image_size;  /* Effective Image size, little

endian */
+ grub_uint64_t flags; /* kernel flags, little
endian */

include/grub/riscv32/linux.h +2 -3
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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
Quoted 6 lines

endian, "RSCV" */
+ grub_uint64_t magic; /* magic (RISC-V specifc,
deprecated)*/
+ grub_uint32_t magic2; /* Magic number 2 (to match
the ARM64 'magic' field pos) */
grub_uint32_t hdr_offset; /* Offset of PE/COFF header */

I can agree that hdr_offset makes more sense but Documentation/riscv/boot-image-header.rst names this member as res3.
So, I would rename hdr_offset to res3 too. Or fix Documentation/riscv/boot-image-header.rst in the kernel. Or, least preferred, explain in the commit message why you do not rename this member and add to the comment that this member is named res3 in the documentation.

include/grub/riscv32/linux.h

Quoted 2 lines
include/grub/riscv32/linux.h +2 -0
+
+  struct grub_pe_image_header pe_image_header;

This struct should not be part of linux_riscv_kernel_header struct. Please take a look at the commit 6d7bb89ef (efi: Move MS-DOS stub out of
generic PE header definition). And right now I can see this comes from
ARM headers. I am not sure why we did not spotted and fixed this issue
when we worked on LoadFile2 series. Atish, could you fix this too? Of
course in separate patch before this one. And if you could align ARM
headers structs with current Linux documentation that would be perfect...

Same comments apply below...

Quoted 41 lines
  };

  #define linux_arch_kernel_header linux_riscv_kernel_header
include/grub/riscv64/linux.h +4 -5
index 3630c30fbf1a..a69046de34ef 100644
@@ -19,23 +19,26 @@
  #ifndef GRUB_RISCV64_LINUX_HEADER
  #define GRUB_RISCV64_LINUX_HEADER 1
 
-#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
+#include <grub/efi/pe32.h>
 
  #define GRUB_EFI_PE_MAGIC    0x5A4D
 
-/* From linux/Documentation/riscv/booting.txt */
+/* From linux/Documentation/riscv/boot-image-header.rst */
  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 text_offset; /* Image load offset, little endian
 */
+  grub_uint64_t image_size;  /* Effective Image size, little

endian */
+ grub_uint64_t flags; /* kernel flags, little
endian */

include/grub/riscv64/linux.h +2 -3
+  grub_uint32_t version;     /* Version of this header */
+  grub_uint32_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_uint64_t magic; /* magic (RISC-V specifc,
deprecated)*/

Quoted 8 lines

include/grub/riscv64/linux.h

Quoted 6 lines

+ grub_uint32_t magic2; /* Magic number 2 (to match
the ARM64 'magic' field pos) */
grub_uint32_t hdr_offset; /* Offset of PE/COFF header */

include/grub/riscv64/linux.h +2 -0
+
+  struct grub_pe_image_header pe_image_header;
  };

Daniel

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

--
Best Regards
Xiaotian Wu

Signature · 2 lines
Regards,
Atish
#20 lore raw

Re: [v6 PATCH 2/3] RISC-V: Update image header

Quoted 4 lines · Atish Patra
Quoted 2 lines · Xiaotian Wu

Is there a new patch?

Quoted 8 lines

Not sure if you are asking about this series or Ard's series [1]. I got busy with other day jobs and did not get time to revise this series as that requires a bit of work
to make sure that it doesn't break ARM64 (by removing the arch specific header files).
I am planning to look into this during the holidays. If anybody else has some free cycles to revise
the series, please let me know. Apologies for the delay.

No worries. If nobody jumps in we will wait for you. Though it will be perfect if you post the patches after holidays.

Daniel

#21 lore raw

PATCH v6 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 ce95c76eaffa..d6cb8a673e1b 100644
@@ -1820,8 +1820,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;
   common = loader/linux.c;
   common = lib/cmdline.c;
   enable = noemu;
grub-core/loader/riscv/linux.c deleted +0 -59

59 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v6 3/3
#22 lore raw

Re: [v6 PATCH 3/3] RISC-V: Use common linux loader

Quoted 5 lines · 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.

Daniel