Skip to contentSkip to conversation

Add support for Linux kernel image header

RFC PATCH New Vivian Wang 3 participants 7 messages Mar 15, 2024 – Apr 5, 2024

lore
Share & tools
lore
#0 lore raw

cover Add support for Linux kernel image header

It is very convenient to use M-mode U-Boot during development to load M-mode software for testing. This series makes it easy to load OpenSBI and an S-mode operating system while passing it an initramfs, an FDT, and a command line from U-Boot with the existing "booti" command. Any other bootloader supporting M-mode Linux should work analogously.

To achieve this, a header compatible with that of Linux is added at the beginning of fw_payload and fw_jump. Since the header starts with a jump to the actual start of code, this is not a problem if no such bootloader is used. A config option is also added so users can opt out of this header.

Patch 1 makes the byteorder macros available in assembly code so that patch 2 can use them.

To test this, you will need:

- QEMU (qemu-system-riscv64), tested with 8.1.3
- M-mode U-Boot with config qemu-riscv64_defconfig (u-boot.bin), tested with version 2023.07.02
- Linux for riscv64 (Image)
- An initramfs image for testing (init.cpio)

Some assumptions are made about the memory layout used by QEMU.

Build OpenSBI:

make PLATFORM=generic FW_PAYLOAD_PATH=Image

Firstly, test that the firmware images still work as before:

  qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
    -bios fw_payload.bin

This one should eventually panic at "VFS: Unable to mount root fs on unknown-block(0,0)" because it does not have an initramfs.

  qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
    -bios fw_jump.bin \
    -kernel Image \
    -initrd init.cpio \
    -append "earlycon"

This one should work just fine.

Then, test that the firmwares are now loadable by U-Boot:

  qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
    -bios u-boot.bin \
    -device loader,addr=0x90000000,file=fw_payload.bin \
    -device loader,addr=0xa0000000,file=init.cpio

  qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
    -bios u-boot.bin \
    -device loader,addr=0x80200000,file=Image \
    -device loader,addr=0x90000000,file=fw_jump.bin \
    -device loader,addr=0xa0000000,file=init.cpio

Run these command in U-Boot, making changes as needed:

=> fdt addr -c
Control fdt: bf730af0

=> # Change source address to match control fdt
=> fdt move bf730af0 b0000000
Working FDT set to b0000000

=> setenv bootargs "earlycon"

=> # Change initramfs size to match actual size => booti 0x90000000 0xa0000000:0x12d400 0xb0000000

Once in Linux:

  $ cat /proc/cmdline # Check that the kernel command line is correct
  $ cat /proc/cpuinfo # Check that SMP boot works

Vivian Wang (2):
include: sbi: Support byteorder macros in assembly
firmware: Add support for Linux kernel image header

 firmware/Kconfig                   | 17 +++++++++
 firmware/fw_base.ldS               |  5 +++
 firmware/fw_header_linux.S         | 34 ++++++++++++++++++
 firmware/fw_jump.S                 |  1 +
 firmware/fw_payload.S              |  1 +
 include/sbi/sbi_byteorder.h        | 55 ++++++++++++++++--------------
 platform/generic/configs/defconfig |  1 +
 7 files changed, 89 insertions(+), 25 deletions(-)
 create mode 100644 firmware/fw_header_linux.S

--
2.42.0

#1 lore raw

RFC PATCH 1/2 include: sbi: Support byteorder macros in assembly

Avoid using C types and casts if sbi/sbi_byteorder.h is included in assembly code

---
 include/sbi/sbi_byteorder.h | 55 ++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 25 deletions(-)
include/sbi/sbi_byteorder.h +30 -25
index db6eb2b..2b4981e 100644
@@ -7,7 +7,12 @@
 #ifndef __SBI_BYTEORDER_H__
 #define __SBI_BYTEORDER_H__
 
-#include <sbi/sbi_types.h>
+#ifdef __ASSEMBLER__
+# define _conv_cast(type, val) (val)
+#else
+# include <sbi/sbi_types.h>
+# define _conv_cast(type, val) ((type)(val))
+#endif
 
 #define BSWAP16(x)	((((x) & 0x00ff) << 8) | \
 			 (((x) & 0xff00) >> 8))
@@ -25,37 +30,37 @@
 			 (((x) & 0xff00000000000000ULL) >> 56))
 
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
-#define cpu_to_be16(x)		((uint16_t)BSWAP16(x))
-#define cpu_to_be32(x)		((uint32_t)BSWAP32(x))
-#define cpu_to_be64(x)		((uint64_t)BSWAP64(x))
+#define cpu_to_be16(x)		_conv_cast(uint16_t, BSWAP16(x))
+#define cpu_to_be32(x)		_conv_cast(uint32_t, BSWAP32(x))
+#define cpu_to_be64(x)		_conv_cast(uint64_t, BSWAP64(x))
 
-#define be16_to_cpu(x)		((uint16_t)BSWAP16(x))
-#define be32_to_cpu(x)		((uint32_t)BSWAP32(x))
-#define be64_to_cpu(x)		((uint64_t)BSWAP64(x))
+#define be16_to_cpu(x)		_conv_cast(uint16_t, BSWAP16(x))
+#define be32_to_cpu(x)		_conv_cast(uint32_t, BSWAP32(x))
+#define be64_to_cpu(x)		_conv_cast(uint64_t, BSWAP64(x))
 
-#define cpu_to_le16(x)		((uint16_t)(x))
-#define cpu_to_le32(x)		((uint32_t)(x))
-#define cpu_to_le64(x)		((uint64_t)(x))
+#define cpu_to_le16(x)		_conv_cast(uint16_t, (x))
+#define cpu_to_le32(x)		_conv_cast(uint32_t, (x))
+#define cpu_to_le64(x)		_conv_cast(uint64_t, (x))
 
-#define le16_to_cpu(x)		((uint16_t)(x))
-#define le32_to_cpu(x)		((uint32_t)(x))
-#define le64_to_cpu(x)		((uint64_t)(x))
+#define le16_to_cpu(x)		_conv_cast(uint16_t, (x))
+#define le32_to_cpu(x)		_conv_cast(uint32_t, (x))
+#define le64_to_cpu(x)		_conv_cast(uint64_t, (x))
 #else /* CPU(big-endian) */
-#define cpu_to_be16(x)		((uint16_t)(x))
-#define cpu_to_be32(x)		((uint32_t)(x))
-#define cpu_to_be64(x)		((uint64_t)(x))
+#define cpu_to_be16(x)		_conv_cast(uint16_t, (x))
+#define cpu_to_be32(x)		_conv_cast(uint32_t, (x))
+#define cpu_to_be64(x)		_conv_cast(uint64_t, (x))
 
-#define be16_to_cpu(x)		((uint16_t)(x))
-#define be32_to_cpu(x)		((uint32_t)(x))
-#define be64_to_cpu(x)		((uint64_t)(x))
+#define be16_to_cpu(x)		_conv_cast(uint16_t, (x))
+#define be32_to_cpu(x)		_conv_cast(uint32_t, (x))
+#define be64_to_cpu(x)		_conv_cast(uint64_t, (x))
 
-#define cpu_to_le16(x)		((uint16_t)BSWAP16(x))
-#define cpu_to_le32(x)		((uint32_t)BSWAP32(x))
-#define cpu_to_le64(x)		((uint64_t)BSWAP64(x))
+#define cpu_to_le16(x)		_conv_cast(uint16_t, BSWAP16(x))
+#define cpu_to_le32(x)		_conv_cast(uint32_t, BSWAP32(x))
+#define cpu_to_le64(x)		_conv_cast(uint64_t, BSWAP64(x))
 
-#define le16_to_cpu(x)		((uint16_t)BSWAP16(x))
-#define le32_to_cpu(x)		((uint32_t)BSWAP32(x))
-#define le64_to_cpu(x)		((uint64_t)BSWAP64(x))
+#define le16_to_cpu(x)		_conv_cast(uint16_t, BSWAP16(x))
+#define le32_to_cpu(x)		_conv_cast(uint32_t, BSWAP32(x))
+#define le64_to_cpu(x)		_conv_cast(uint64_t, BSWAP64(x))
 #endif
 
 #if __riscv_xlen == 64
Signature · 1 line
2.42.0
Applied
#2 lore raw

RFC PATCH 1/2 include: sbi: Support byteorder macros in assembly

Quoted 5 lines · On Fri, Mar 15, 2024 at 11:07?PM Vivian Wang <[email protected]> wrote:

Avoid using C types and casts if sbi/sbi_byteorder.h is included in assembly code

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 92 lines
---
 include/sbi/sbi_byteorder.h | 55 ++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 25 deletions(-)
include/sbi/sbi_byteorder.h +30 -26
index db6eb2b..2b4981e 100644
@@ -7,7 +7,12 @@
 #ifndef __SBI_BYTEORDER_H__
 #define __SBI_BYTEORDER_H__
 
-#include <sbi/sbi_types.h>
+#ifdef __ASSEMBLER__
+# define _conv_cast(type, val) (val)
+#else
+# include <sbi/sbi_types.h>
+# define _conv_cast(type, val) ((type)(val))
+#endif
 
 #define BSWAP16(x)     ((((x) & 0x00ff) << 8) | \
                         (((x) & 0xff00) >> 8))
@@ -25,37 +30,37 @@
                         (((x) & 0xff00000000000000ULL) >> 56))
 
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  /* CPU(little-endian) */
-#define cpu_to_be16(x)         ((uint16_t)BSWAP16(x))
-#define cpu_to_be32(x)         ((uint32_t)BSWAP32(x))
-#define cpu_to_be64(x)         ((uint64_t)BSWAP64(x))
+#define cpu_to_be16(x)         _conv_cast(uint16_t, BSWAP16(x))
+#define cpu_to_be32(x)         _conv_cast(uint32_t, BSWAP32(x))
+#define cpu_to_be64(x)         _conv_cast(uint64_t, BSWAP64(x))
 
-#define be16_to_cpu(x)         ((uint16_t)BSWAP16(x))
-#define be32_to_cpu(x)         ((uint32_t)BSWAP32(x))
-#define be64_to_cpu(x)         ((uint64_t)BSWAP64(x))
+#define be16_to_cpu(x)         _conv_cast(uint16_t, BSWAP16(x))
+#define be32_to_cpu(x)         _conv_cast(uint32_t, BSWAP32(x))
+#define be64_to_cpu(x)         _conv_cast(uint64_t, BSWAP64(x))
 
-#define cpu_to_le16(x)         ((uint16_t)(x))
-#define cpu_to_le32(x)         ((uint32_t)(x))
-#define cpu_to_le64(x)         ((uint64_t)(x))
+#define cpu_to_le16(x)         _conv_cast(uint16_t, (x))
+#define cpu_to_le32(x)         _conv_cast(uint32_t, (x))
+#define cpu_to_le64(x)         _conv_cast(uint64_t, (x))
 
-#define le16_to_cpu(x)         ((uint16_t)(x))
-#define le32_to_cpu(x)         ((uint32_t)(x))
-#define le64_to_cpu(x)         ((uint64_t)(x))
+#define le16_to_cpu(x)         _conv_cast(uint16_t, (x))
+#define le32_to_cpu(x)         _conv_cast(uint32_t, (x))
+#define le64_to_cpu(x)         _conv_cast(uint64_t, (x))
 #else /* CPU(big-endian) */
-#define cpu_to_be16(x)         ((uint16_t)(x))
-#define cpu_to_be32(x)         ((uint32_t)(x))
-#define cpu_to_be64(x)         ((uint64_t)(x))
+#define cpu_to_be16(x)         _conv_cast(uint16_t, (x))
+#define cpu_to_be32(x)         _conv_cast(uint32_t, (x))
+#define cpu_to_be64(x)         _conv_cast(uint64_t, (x))
 
-#define be16_to_cpu(x)         ((uint16_t)(x))
-#define be32_to_cpu(x)         ((uint32_t)(x))
-#define be64_to_cpu(x)         ((uint64_t)(x))
+#define be16_to_cpu(x)         _conv_cast(uint16_t, (x))
+#define be32_to_cpu(x)         _conv_cast(uint32_t, (x))
+#define be64_to_cpu(x)         _conv_cast(uint64_t, (x))
 
-#define cpu_to_le16(x)         ((uint16_t)BSWAP16(x))
-#define cpu_to_le32(x)         ((uint32_t)BSWAP32(x))
-#define cpu_to_le64(x)         ((uint64_t)BSWAP64(x))
+#define cpu_to_le16(x)         _conv_cast(uint16_t, BSWAP16(x))
+#define cpu_to_le32(x)         _conv_cast(uint32_t, BSWAP32(x))
+#define cpu_to_le64(x)         _conv_cast(uint64_t, BSWAP64(x))
 
-#define le16_to_cpu(x)         ((uint16_t)BSWAP16(x))
-#define le32_to_cpu(x)         ((uint32_t)BSWAP32(x))
-#define le64_to_cpu(x)         ((uint64_t)BSWAP64(x))
+#define le16_to_cpu(x)         _conv_cast(uint16_t, BSWAP16(x))
+#define le32_to_cpu(x)         _conv_cast(uint32_t, BSWAP32(x))
+#define le64_to_cpu(x)         _conv_cast(uint64_t, BSWAP64(x))
 #endif
 
 #if __riscv_xlen == 64
--

2.42.0

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

RFC PATCH 2/2 firmware: Add support for Linux kernel image header

In reply to Vivian Wang

The firmwares fw_payload and fw_jump support the same entrypoint ABI as M-mode Linux. To support booting theses firmware images from bootloaders already supporting M-mode Linux, add a compatible header to the firmware image.

The header starts with a jump to skip to the actual code, so including it does not affect operation without a bootloader.

---
 firmware/Kconfig                   | 17 +++++++++++++++
 firmware/fw_base.ldS               |  5 +++++
 firmware/fw_header_linux.S         | 34 ++++++++++++++++++++++++++++++
 firmware/fw_jump.S                 |  1 +
 firmware/fw_payload.S              |  1 +
 platform/generic/configs/defconfig |  1 +
 6 files changed, 59 insertions(+)
 create mode 100644 firmware/fw_header_linux.S
firmware/Kconfig +17 -0
index d6e0506..50ec340 100644
@@ -1 +1,18 @@
 # SPDX-License-Identifier: BSD-2-Clause
+
+menu "Firmware Configuration"
+
+choice FW_HEADER
+	bool "Include Firmware Header"
+	optional
+
+config FW_HEADER_LINUX
+	bool "Linux Kernel Image"
+	help
+	  Compatibility with Linux kernel image header, for fw_jump and fw_payload
+
+	  See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+
+endchoice
+
+endmenu
firmware/fw_base.ldS +5 -0
index fb47984..6a3b21b 100644
@@ -15,6 +15,11 @@
 
 	/* Beginning of the code section */
 
+	.header :
+	{
+		*(.header)
+	}
+
 	.text :
  	{
 		PROVIDE(_text_start = .);
firmware/fw_header_linux.S new file +34 -0
new file mode 100644
index 0000000..fbd0cf9
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+#include <sbi/sbi_byteorder.h>
+
+#ifdef CONFIG_FW_HEADER_LINUX
+
+	/*
+	 * Compatibility with Linux kernel image header, for fw_jump and fw_payload
+	 *
+	 * See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+	 */
+
+	.section .header, "a", %progbits
+
+	/* 8 bytes at beginning reserved for code */
+	j _start
+	.align 3
+
+	/* We want to be loaded at start of RAM, so text_offset is 0 */
+	.dword 0					/* text_offset */
+	.dword cpu_to_le64(_fw_reloc_end - _fw_start)	/* image_size */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+	.dword 0					/* flags: Not big-endian */
+#else
+	.dword 1					/* flags: Big-endian */
+#endif
+	.word cpu_to_le16(0x0002)			/* version (0.2) */
+	.word 0						/* res1 */
+	.dword 0					/* res2 */
+	.ascii "RISCV\0\0\0"				/* magic */
+	.ascii "RSC\x05"				/* magic2 */
+	.word 0						/* res3 */
+
+#endif /* CONFIG_FW_HEADER_LINUX */
firmware/fw_jump.S +1 -0
index ebf297f..9b2ddda 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
 	.section .entry, "ax", %progbits
 	.align 3
firmware/fw_payload.S +1 -0
index 3c8433e..e324f6d 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
 	.section .entry, "ax", %progbits
 	.align 3
platform/generic/configs/defconfig +1 -0
index 1ce6a12..4f7217b 100644
@@ -42,3 +42,4 @@ CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
+CONFIG_FW_HEADER_LINUX=y
Signature · 1 line
2.42.0
#4 lore raw

RFC PATCH 2/2 firmware: Add support for Linux kernel image header

Quoted 10 lines · On Fri, Mar 15, 2024 at 11:07?PM Vivian Wang <[email protected]> wrote:

The firmwares fw_payload and fw_jump support the same entrypoint ABI as M-mode Linux. To support booting theses firmware images from bootloaders already supporting M-mode Linux, add a compatible header to the firmware image.

The header starts with a jump to skip to the actual code, so including it does not affect operation without a bootloader.

This won't fly because OpenSBI FW_DYNAMIC expects three parameters instead of just two expected by Linux flat image:
a0 -> HART ID
a1 - > DTB address
a2 -> FW_DYNAMIC_INFO address

Both QEMU and U-Boot SPL already provide FW_DYNAMIC_INFO to FW_DYNAMIC in an OpenSBI specific way. Unfortunately, booti or other U-Boot booting commands don't provide FW_DYAMIC_INFO address hence no point in making OpenSBI binary look like Linux binary for using booti from U-Boot M-mode.

Regards,
Anup

Quoted 130 lines
---
 firmware/Kconfig                   | 17 +++++++++++++++
 firmware/fw_base.ldS               |  5 +++++
 firmware/fw_header_linux.S         | 34 ++++++++++++++++++++++++++++++
 firmware/fw_jump.S                 |  1 +
 firmware/fw_payload.S              |  1 +
 platform/generic/configs/defconfig |  1 +
 6 files changed, 59 insertions(+)
 create mode 100644 firmware/fw_header_linux.S
firmware/Kconfig +17 -0
index d6e0506..50ec340 100644
@@ -1 +1,18 @@
 # SPDX-License-Identifier: BSD-2-Clause
+
+menu "Firmware Configuration"
+
+choice FW_HEADER
+       bool "Include Firmware Header"
+       optional
+
+config FW_HEADER_LINUX
+       bool "Linux Kernel Image"
+       help
+         Compatibility with Linux kernel image header, for fw_jump and fw_payload
+
+         See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+
+endchoice
+
+endmenu
firmware/fw_base.ldS +5 -0
index fb47984..6a3b21b 100644
@@ -15,6 +15,11 @@
 
        /* Beginning of the code section */
 
+       .header :
+       {
+               *(.header)
+       }
+
        .text :
        {
                PROVIDE(_text_start = .);
firmware/fw_header_linux.S new file +34 -0
new file mode 100644
index 0000000..fbd0cf9
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+#include <sbi/sbi_byteorder.h>
+
+#ifdef CONFIG_FW_HEADER_LINUX
+
+       /*
+        * Compatibility with Linux kernel image header, for fw_jump and fw_payload
+        *
+        * See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+        */
+
+       .section .header, "a", %progbits
+
+       /* 8 bytes at beginning reserved for code */
+       j _start
+       .align 3
+
+       /* We want to be loaded at start of RAM, so text_offset is 0 */
+       .dword 0                                        /* text_offset */
+       .dword cpu_to_le64(_fw_reloc_end - _fw_start)   /* image_size */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       .dword 0                                        /* flags: Not big-endian */
+#else
+       .dword 1                                        /* flags: Big-endian */
+#endif
+       .word cpu_to_le16(0x0002)                       /* version (0.2) */
+       .word 0                                         /* res1 */
+       .dword 0                                        /* res2 */
+       .ascii "RISCV\0\0\0"                            /* magic */
+       .ascii "RSC\x05"                                /* magic2 */
+       .word 0                                         /* res3 */
+
+#endif /* CONFIG_FW_HEADER_LINUX */
firmware/fw_jump.S +1 -0
index ebf297f..9b2ddda 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
        .section .entry, "ax", %progbits
        .align 3
firmware/fw_payload.S +1 -0
index 3c8433e..e324f6d 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
        .section .entry, "ax", %progbits
        .align 3
platform/generic/configs/defconfig +1 -1
index 1ce6a12..4f7217b 100644
@@ -42,3 +42,4 @@ CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
+CONFIG_FW_HEADER_LINUX=y
--

2.42.0

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

RFC PATCH 2/2 firmware: Add support for Linux kernel image header

Quoted 16 lines · Anup Patel
Quoted 9 lines · On Fri, Mar 15, 2024 at 11:07?PM Vivian Wang <[email protected]> wrote:

The firmwares fw_payload and fw_jump support the same entrypoint ABI as M-mode Linux. To support booting theses firmware images from bootloaders already supporting M-mode Linux, add a compatible header to the firmware image.

The header starts with a jump to skip to the actual code, so including it does not affect operation without a bootloader.

This won't fly because OpenSBI FW_DYNAMIC expects three parameters instead of just two expected by Linux flat image:
a0 -> HART ID
a1 - > DTB address
a2 -> FW_DYNAMIC_INFO address

Quoted 5 lines

Both QEMU and U-Boot SPL already provide FW_DYNAMIC_INFO to FW_DYNAMIC in an OpenSBI specific way. Unfortunately, booti or other U-Boot booting commands don't provide FW_DYAMIC_INFO address hence no point in making OpenSBI binary look like Linux binary for using booti from U-Boot M-mode.

That's fair, we'll do it the "clean" way and make FIT images for U-Boot. This patch is abandoned.

Vivian "dram"

Quoted 133 lines

Regards,
Anup

Quoted 130 lines
---
 firmware/Kconfig                   | 17 +++++++++++++++
 firmware/fw_base.ldS               |  5 +++++
 firmware/fw_header_linux.S         | 34 ++++++++++++++++++++++++++++++
 firmware/fw_jump.S                 |  1 +
 firmware/fw_payload.S              |  1 +
 platform/generic/configs/defconfig |  1 +
 6 files changed, 59 insertions(+)
 create mode 100644 firmware/fw_header_linux.S
firmware/Kconfig +17 -0
index d6e0506..50ec340 100644
@@ -1 +1,18 @@
 # SPDX-License-Identifier: BSD-2-Clause
+
+menu "Firmware Configuration"
+
+choice FW_HEADER
+       bool "Include Firmware Header"
+       optional
+
+config FW_HEADER_LINUX
+       bool "Linux Kernel Image"
+       help
+         Compatibility with Linux kernel image header, for fw_jump and fw_payload
+
+         See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+
+endchoice
+
+endmenu
firmware/fw_base.ldS +5 -0
index fb47984..6a3b21b 100644
@@ -15,6 +15,11 @@
 
        /* Beginning of the code section */
 
+       .header :
+       {
+               *(.header)
+       }
+
        .text :
        {
                PROVIDE(_text_start = .);
firmware/fw_header_linux.S new file +34 -0
new file mode 100644
index 0000000..fbd0cf9
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+#include <sbi/sbi_byteorder.h>
+
+#ifdef CONFIG_FW_HEADER_LINUX
+
+       /*
+        * Compatibility with Linux kernel image header, for fw_jump and fw_payload
+        *
+        * See: https://www.kernel.org/doc/html/v6.8/arch/riscv/boot-image-header.html
+        */
+
+       .section .header, "a", %progbits
+
+       /* 8 bytes at beginning reserved for code */
+       j _start
+       .align 3
+
+       /* We want to be loaded at start of RAM, so text_offset is 0 */
+       .dword 0                                        /* text_offset */
+       .dword cpu_to_le64(_fw_reloc_end - _fw_start)   /* image_size */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       .dword 0                                        /* flags: Not big-endian */
+#else
+       .dword 1                                        /* flags: Big-endian */
+#endif
+       .word cpu_to_le16(0x0002)                       /* version (0.2) */
+       .word 0                                         /* res1 */
+       .dword 0                                        /* res2 */
+       .ascii "RISCV\0\0\0"                            /* magic */
+       .ascii "RSC\x05"                                /* magic2 */
+       .word 0                                         /* res3 */
+
+#endif /* CONFIG_FW_HEADER_LINUX */
firmware/fw_jump.S +1 -0
index ebf297f..9b2ddda 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
        .section .entry, "ax", %progbits
        .align 3
firmware/fw_payload.S +1 -0
index 3c8433e..e324f6d 100644
@@ -8,6 +8,7 @@
  */
 
 #include "fw_base.S"
+#include "fw_header_linux.S"
 
        .section .entry, "ax", %progbits
        .align 3
platform/generic/configs/defconfig +1 -1
index 1ce6a12..4f7217b 100644
@@ -42,3 +42,4 @@ CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
+CONFIG_FW_HEADER_LINUX=y
--

2.42.0

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#6 lore raw

cover Add support for Linux kernel image header

In reply to Vivian Wang
Quoted 11 lines · Vivian Wang

It is very convenient to use M-mode U-Boot during development to load M-mode software for testing. This series makes it easy to load OpenSBI and an S-mode operating system while passing it an initramfs, an FDT, and a command line from U-Boot with the existing "booti" command. Any other bootloader supporting M-mode Linux should work analogously.

To achieve this, a header compatible with that of Linux is added at the beginning of fw_payload and fw_jump. Since the header starts with a jump to the actual start of code, this is not a problem if no such bootloader is used. A config option is also added so users can opt out of this header.

It looks like this patch is specifically tailored for the "booti" command, because it expects a Linux header to load. I don't think this is necessary and openSBI should not care about or carry OS level stuff. Instead of "faking" openSBI as a Linux Image, why not use the more flexible FIT image in U-Boot, where you can specify all needed pieces, openSBI, Linux, initrd, dtb and so on, then do "bootm"?

Quoted 77 lines

Patch 1 makes the byteorder macros available in assembly code so that patch 2 can use them.

To test this, you will need:

- QEMU (qemu-system-riscv64), tested with 8.1.3
- M-mode U-Boot with config qemu-riscv64_defconfig (u-boot.bin), tested with version 2023.07.02
- Linux for riscv64 (Image)
- An initramfs image for testing (init.cpio)

Some assumptions are made about the memory layout used by QEMU.

Build OpenSBI:

make PLATFORM=generic FW_PAYLOAD_PATH=Image

Firstly, test that the firmware images still work as before:

   qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
     -bios fw_payload.bin

This one should eventually panic at "VFS: Unable to mount root fs on unknown-block(0,0)" because it does not have an initramfs.

   qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
     -bios fw_jump.bin \
     -kernel Image \
     -initrd init.cpio \
     -append "earlycon"

This one should work just fine.

Then, test that the firmwares are now loadable by U-Boot:

   qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
     -bios u-boot.bin \
     -device loader,addr=0x90000000,file=fw_payload.bin \
     -device loader,addr=0xa0000000,file=init.cpio

   qemu-system-riscv64 -M virt -smp 2 -m 1G -nographic \
     -bios u-boot.bin \
     -device loader,addr=0x80200000,file=Image \
     -device loader,addr=0x90000000,file=fw_jump.bin \
     -device loader,addr=0xa0000000,file=init.cpio

Run these command in U-Boot, making changes as needed:

=> fdt addr -c
Control fdt: bf730af0

=> # Change source address to match control fdt => fdt move bf730af0 b0000000
Working FDT set to b0000000

=> setenv bootargs "earlycon"

=> # Change initramfs size to match actual size => booti 0x90000000 0xa0000000:0x12d400 0xb0000000

Once in Linux:

   $ cat /proc/cmdline # Check that the kernel command line is correct
   $ cat /proc/cpuinfo # Check that SMP boot works

Vivian Wang (2):
include: sbi: Support byteorder macros in assembly
firmware: Add support for Linux kernel image header

  firmware/Kconfig                   | 17 +++++++++
  firmware/fw_base.ldS               |  5 +++
  firmware/fw_header_linux.S         | 34 ++++++++++++++++++
  firmware/fw_jump.S                 |  1 +
  firmware/fw_payload.S              |  1 +
  include/sbi/sbi_byteorder.h        | 55 ++++++++++++++++--------------
  platform/generic/configs/defconfig |  1 +
  7 files changed, 89 insertions(+), 25 deletions(-)
Quoted 6 lines

create mode 100644 firmware/fw_header_linux.S

--
2.42.0

Bo