Add support for Linux kernel image header
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.binThis 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.cpioRun 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
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
@@ -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
Anup Patel
AppliedRFC 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.his included in assembly codeSigned-off-by Vivian Wang <[email protected]>
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@@ -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
RFC PATCH 2/2 firmware: Add support for Linux kernel image header
In reply to Vivian WangThe 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
@@ -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
@@ -15,6 +15,11 @@ /* Beginning of the code section */ + .header : + { + *(.header) + } + .text : { PROVIDE(_text_start = .);
firmware/fw_header_linux.S new file +34 -0
@@ -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
@@ -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
@@ -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
@@ -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
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_payloadandfw_jumpsupport 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.
Signed-off-by Vivian Wang <[email protected]>
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.Sfirmware/Kconfig +17
-0@@ -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 + +endmenufirmware/fw_base.ldS +5
-0@@ -15,6 +15,11 @@ /* Beginning of the code section */ + .header : + { + *(.header) + } + .text : { PROVIDE(_text_start = .);firmware/fw_header_linux.S new file +34
-0@@ -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@@ -8,6 +8,7 @@ */ #include "fw_base.S" +#include "fw_header_linux.S" .section .entry, "ax", %progbits .align 3firmware/fw_payload.S +1
-0@@ -8,6 +8,7 @@ */ #include "fw_base.S" +#include "fw_header_linux.S" .section .entry, "ax", %progbits .align 3platform/generic/configs/defconfig +1
-1@@ -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
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_payloadandfw_jumpsupport 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.
Signed-off-by Vivian Wang <[email protected]>This won't fly because OpenSBI
FW_DYNAMICexpects three parameters instead of just two expected by Linux flat image:
a0 -> HART ID
a1 - > DTB address
a2 ->FW_DYNAMIC_INFOaddress
Quoted 5 lines
Both QEMU and U-Boot SPL already provide
FW_DYNAMIC_INFOtoFW_DYNAMICin an OpenSBI specific way. Unfortunately, booti or other U-Boot booting commands don't provideFW_DYAMIC_INFOaddress 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,
AnupQuoted 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.Sfirmware/Kconfig +17
-0@@ -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 + +endmenufirmware/fw_base.ldS +5
-0@@ -15,6 +15,11 @@ /* Beginning of the code section */ + .header : + { + *(.header) + } + .text : { PROVIDE(_text_start = .);firmware/fw_header_linux.S new file +34
-0@@ -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@@ -8,6 +8,7 @@ */ #include "fw_base.S" +#include "fw_header_linux.S" .section .entry, "ax", %progbits .align 3firmware/fw_payload.S +1
-0@@ -8,6 +8,7 @@ */ #include "fw_base.S" +#include "fw_header_linux.S" .section .entry, "ax", %progbits .align 3platform/generic/configs/defconfig +1
-1@@ -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
cover Add support for Linux kernel image header
In reply to Vivian WangQuoted 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_payloadandfw_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.binThis 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.cpioRun these command in U-Boot, making changes as needed:
=> fdt addr -c
Control fdt:bf730af0=> # Change source address to match control fdt => fdt move
bf730af0b0000000
Working FDT set tob0000000=> setenv bootargs "earlycon"
=> # Change initramfs size to match actual size => booti
0x900000000xa0000000:0x12d4000xb0000000Once in Linux:
$ cat /proc/cmdline # Check that the kernel command line is correct $ cat /proc/cpuinfo # Check that SMP boot worksVivian Wang (2):
include: sbi: Support byteorder macros in assembly
firmware: Add support for Linux kernel image headerfirmware/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