Skip to content
systemcoding.dev
Kernels

Boot Linux on RISC-V in QEMU With OpenSBI and U-Boot

Build OpenSBI, U-Boot and a Linux 6.12 Image for riscv64, boot them on QEMU virt in the real firmware chain and load the kernel from an ext4 disk with booti.

8 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, RISC-V and QEMU logos.

Most RISC-V boards boot Linux through three stages: firmware in machine mode, a bootloader in supervisor mode, and the kernel. QEMU can reproduce that exact chain on your laptop, so you can learn it, change it and break it without hardware. This post builds each piece for riscv64, boots them in QEMU's virt machine, and then loads the kernel from a disk with U-Boot's own commands, which is what a real board does. It ends with the one-line shortcut that skips U-Boot, and a table of what each stage hands to the next.

The sources are OpenSBI, U-Boot 2024.10 and Linux 6.12, built with the riscv64-linux-gnu- cross toolchain from your distribution. The boot requirements are those in the kernel's Documentation/arch/riscv/boot.rst.

The chain and who does what

Boot proceeds in stages, and each has a narrow job.

OpenSBI runs first, in machine mode. QEMU loads it as the BIOS at 0x80000000. It sets up the machine, protects its own memory, and hands control to the next stage in supervisor mode, passing the hart ID in a0 and a device tree pointer in a1.

U-Boot is the second stage. It runs in supervisor mode at 0x80200000, initializes devices, reads a disk or the network, finds a kernel and a device tree, and jumps to the kernel with the same register convention.

Linux is the third. It expects exactly what the firmware gave U-Boot: a0 is the boot hart's ID, a1 is the physical address of the device tree, and the MMU is off.

On QEMU you may skip the middle stage and let -kernel hand Linux straight to OpenSBI. That is faster and hides the interesting part, so build the full chain first.

Build OpenSBI

QEMU ships a copy, but building your own makes the version a variable you control. The generic platform covers every machine that describes itself with a device tree, including virt.

Shell
git clone https://github.com/riscv-software-src/opensbi.git
cd opensbi
make CROSS_COMPILE=riscv64-linux-gnu- PLATFORM=generic -j$(nproc)
ls build/platform/generic/firmware/

The firmware comes in three flavors, and the difference is how it finds the next stage:

Text
fw_dynamic.bin  fw_dynamic.elf  fw_jump.bin  fw_jump.elf  fw_payload.bin  fw_payload.elf

fw_jump jumps to a fixed address. fw_payload has a payload, such as a kernel, linked into the image. fw_dynamic receives the next stage's address and mode from the previous stage in registers, which is what a bootloader that loads OpenSBI uses. For QEMU's -bios, fw_jump.elf is the usual choice and the default OpenSBI QEMU carries is equivalent.

Build U-Boot for QEMU

U-Boot has a configuration for QEMU's 64-bit RISC-V virt machine that runs in supervisor mode behind OpenSBI:

Shell
git clone https://source.denx.de/u-boot/u-boot.git
cd u-boot
git checkout v2024.10
make CROSS_COMPILE=riscv64-linux-gnu- qemu-riscv64_smode_defconfig
make CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc)
ls -l u-boot.bin

The smode in the name is what you want: it assumes OpenSBI already ran and builds U-Boot to load at 0x80200000 as an S-mode payload. A different defconfig, qemu-riscv64_defconfig, builds U-Boot for machine mode, which is for the case with no OpenSBI.

Build the Linux kernel

Linux builds from the same tree for every board. For QEMU, the default configuration includes the virt machine's drivers: the 16550 UART, the PLIC, virtio block and network.

Shell
cd linux-6.12
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- defconfig
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc) Image
ls -l arch/riscv/boot/Image

The result is Image, an uncompressed binary with a small header that bootloaders recognize, as the next section describes. A compressed Image.gz also exists, and U-Boot's booti command accepts the plain one.

A root filesystem and a disk

Linux needs a root filesystem to run init. A small BusyBox rootfs on an ext4 image is enough. Build or download one, then place the kernel in a boot directory on the same disk, so U-Boot can load it:

Shell
dd if=/dev/zero of=disk.img bs=1M count=256
mkfs.ext4 -F disk.img
mkdir -p mnt && sudo mount -o loop disk.img mnt
sudo mkdir -p mnt/boot mnt/extlinux
sudo cp linux-6.12/arch/riscv/boot/Image mnt/boot/Image
## populate mnt/ with a root filesystem (BusyBox, /sbin/init, /dev, /proc and so on)

Add a boot menu that U-Boot's generic distro boot reads. It is a small text file at /extlinux/extlinux.conf:

extlinux/extlinux.confText
default linux
timeout 1

label linux
    kernel /boot/Image
    append root=/dev/vda rw console=ttyS0 earlycon=sbi

Then unmount with sudo umount mnt. The earlycon=sbi option makes the very first kernel messages come out through the SBI console, before the UART driver is ready, which is the first thing you want when a boot fails early.

Run the whole chain

QEMU gets OpenSBI as its BIOS, U-Boot as the kernel it boots, and the disk as a virtio block device:

Shell
qemu-system-riscv64 -machine virt -cpu rv64 -smp 2 -m 1G -nographic \
    -bios opensbi/build/platform/generic/firmware/fw_jump.elf \
    -kernel u-boot/u-boot.bin \
    -drive file=disk.img,format=raw,id=hd0,if=none \
    -device virtio-blk-device,drive=hd0 \
    -device virtio-net-device,netdev=net0 -netdev user,id=net0

The output is the three stages one after another. The version strings follow the sources you built:

Text
OpenSBI v1.5
...
Boot HART ID              : 0

U-Boot 2024.10 (Nov 23 2026 - 10:00:00 +0000)

CPU:   rv64imafdch_zicsr_zifencei_zicntr_zihpm
Model: riscv-virtio,qemu
DRAM:  1 GiB
...
Hit any key to stop autoboot:  0
...
Scanning for bootflows in all bootdevs
...
1:	linux
Retrieving file: /boot/Image
append: root=/dev/vda rw console=ttyS0 earlycon=sbi
Moving Image from 0x84000000 to 0x80200000
...
[    0.000000] Linux version 6.12.0 (user@host) (riscv64-linux-gnu-gcc ...)
[    0.000000] Machine model: riscv-virtio,qemu
[    0.000000] SBI specification v2.0 detected
[    0.000000] SBI implementation ID=0x1 Version=0x10005
...
[    1.2] Run /sbin/init as init process

The line Moving Image from 0x84000000 to 0x80200000 is the detail worth noticing. U-Boot loaded the file to a scratch address, then moved it to the place the kernel's header says it wants to run.

Doing it by hand at the U-Boot prompt

Press a key at the autoboot countdown to get a prompt. Typing the boot steps yourself shows what the distro scripts automate:

Text
=> virtio scan
=> ext4ls virtio 0:0 /boot
=> ext4load virtio 0:0 ${kernel_addr_r} /boot/Image
=> setenv bootargs root=/dev/vda rw console=ttyS0 earlycon=sbi
=> booti ${kernel_addr_r} - ${fdtcontroladdr}

virtio scan enumerates the virtio block device. ext4load reads the file into memory at kernel_addr_r, an environment variable with a safe default address. The bootargs variable becomes the kernel command line, which U-Boot writes into the device tree's /chosen node. booti is the command for the Linux ARM64 and RISC-V Image format: it validates the header, moves the image to its link address and jumps. The - means no initial RAM disk, and ${fdtcontroladdr} is the device tree that QEMU gave U-Boot, reused for the kernel.

What the Image header gives the bootloader

The first 64 bytes of Image are a header, defined in the kernel's Documentation/arch/riscv/boot-image-header.rst. U-Boot reads it to know how big the image is and where it expects to be loaded.

image_header.hc
#include <stdint.h>

struct riscv_image_header {
    uint32_t code0;          // executable code: a jump past the header
    uint32_t code1;
    uint64_t text_offset;    // load offset from the start of RAM
    uint64_t image_size;     // size of the image, in bytes
    uint64_t flags;
    uint32_t version;
    uint32_t res1;
    uint64_t res2;
    uint64_t magic;          // 0x5643534952: "RISCV" followed by three zero bytes
    uint32_t magic2;         // 0x05435352: "RSC\x05"
    uint32_t res3;
};

The magic at offset 0x30 is how booti knows a file is a RISC-V Image and not garbage. text_offset is added to the start of RAM to get the load address, which is why the kernel ends up at 0x80200000 on a machine whose RAM begins at 0x80000000. The first two words are real instructions, a jump over the header, so that a loader that knows nothing about the format can jump to the first byte.

The shortcut and when to use it

Skipping U-Boot is one flag. QEMU's -kernel option accepts a Linux Image directly, and passes the device tree on to it through OpenSBI's fw_dynamic handoff:

Shell
qemu-system-riscv64 -machine virt -smp 2 -m 1G -nographic \
    -kernel linux-6.12/arch/riscv/boot/Image \
    -append "root=/dev/vda rw console=ttyS0 earlycon=sbi" \
    -drive file=disk.img,format=raw,id=hd0,if=none \
    -device virtio-blk-device,drive=hd0

That starts Linux in a second or two. Use it for kernel work, where you rebuild and boot hundreds of times. Use the full chain when you are working on a bootloader, a board port or anything that depends on what U-Boot passes: the environment, the device tree overlays, secure boot or network boot.

What each stage hands the next

Failures in this chain nearly always come from a broken handoff, so keep the contract in view:

StageRuns inStarts atReceivesHands over
OpenSBIM mode0x80000000Reset statea0 hart ID, a1 device tree, S mode
U-BootS mode0x80200000a0, a1 from OpenSBIa0 hart ID, a1 device tree, MMU off
LinuxS modeRAM base plus text_offseta0, a1 from U-BootRuns init

If the screen goes silent after U-Boot prints the kernel's address, suspect the device tree pointer or the command line: add earlycon=sbi and look for the first kernel message. If Linux panics on mounting root with unknown-block(0,0), the virtio block driver did not see the disk, so check root= and the -device line. If booti rejects the file with a bad magic number, you loaded a compressed image or the wrong file.

Boundaries

This QEMU setup teaches the chain and does not model real boards in two ways. Real hardware has a first stage before OpenSBI, a mask ROM and often a secondary program loader, that QEMU skips. And real boards need the vendor's device tree and drivers, which are rarely in the mainline defconfig, so a kernel that boots in QEMU may not boot on a board without board-specific configuration. The method carries over, and the details are in the board's documentation.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.