Building a RISC-V Linux Boot Environment with buildroot

I want to use buildroot as the build environment for RISC-V Linux, but I couldn't find any material on an environment that builds reliably, so I decided to make one myself.

I work on WSL, which has some annoying aspects such as setting up environment variables, so I decided to build it in a Dockerfile.

https://github.com/msyksphinz-self/riscv-buildroot-linux

https://hub.docker.com/repository/docker/msyksphinz/buildroot

The buildroot setup can be done with the following flow. The key points are downloading buildroot and setting up the Makefile for virt.

FROM ubuntu:22.04

# -----------------------------
# Ubuntu Package Update
# -----------------------------
RUN apt update

RUN apt install -y git python3 make autoconf g++ device-tree-compiler pkg-config

RUN apt install -y libglib2.0-dev
RUN apt install -y libpixman-1-dev
RUN apt install -y ninja-build

WORKDIR /work-buildroot/

# -------------------
# Build QEMU Package
# -------------------

WORKDIR /work-buildroot/
RUN git clone https://github.com/qemu/qemu
WORKDIR /work-buildroot/qemu
RUN ./configure --target-list=riscv64-softmmu
RUN make -j $(nproc)
RUN make install

# ----------------
# Buildroot Build
# ----------------
WORKDIR /work-buildroot/
RUN git clone https://github.com/buildroot/buildroot.git
WORKDIR /work-buildroot/buildroot/
RUN make qemu_riscv64_virt_defconfig
RUN apt install -y file wget cpio unzip rsync bc
RUN make -j 10

This generates the files for building Linux under /work/buildroot/output/, but since the build happens inside Docker, they cannot be run on the host. So I decided to retrieve the build products with the following script.

DOCKER_NAME := $(shell bash -c 'echo $$RANDOM')
DOCKER_ID := $(shell docker ps -aqf "name=$(DOCKER_NAME)")

copy:
        docker cp $(DOCKER_ID):/work-buildroot/buildroot/output .
        docker kill $(DOCKER_NAME)

This lets me retrieve the products in /work-buildroot/buildroot/output/. Now I can run the buildroot Linux on the host machine using QEMU.

run-qemu:
        ./qemu/riscv64-softmmu/qemu-system-riscv64 \
                -bios ./output/images/fw_jump.elf \
                -nographic -machine virt -kernel ./output/images/Image \
                -append "root=/dev/vda ro console=ttyS0" \
                -drive file=./output/images/rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 \
                -netdev user,id=net0 -device virtio-net-device,netdev=net0
$ make run-qemu
OpenSBI v0.9
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : timer,mfdeleg
Platform HART Count       : 1
Firmware Base             : 0x80000000
Firmware Size             : 124 KB
Runtime SBI Version       : 0.2

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01          : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x0000000082200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART ISA             : rv64imafdcsu
Boot HART Features        : scounteren,mcounteren,time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count      : 0
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b109
[    0.000000] Linux version 5.15.43 (root@buildkitsandbox) (riscv64-buildroot-linux-gnu-gcc.br_real (Buildroot 2022.08-353-geedd93f010) 11.3.0, GNU ld (GNU Binutils) 2.38) #1 SMP Sun Sep 25 06:40:33 UTC 2022
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] Machine model: riscv-virtio,qemu
[    0.000000] efi: UEFI not found.
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080200000-0x0000000087ffffff]
...
Starting network: udhcpc: started, v1.35.0
udhcpc: broadcasting discover
udhcpc: broadcasting select for 10.0.2.15, server 10.0.2.2
udhcpc: lease of 10.0.2.15 obtained from 10.0.2.2, lease time 86400
deleting routers
adding dns 10.0.2.3
OK

Welcome to Buildroot
buildroot login:
...

This is an English translation of the original Japanese post: RISC-VのLinuxブート環境をbuildrootで構築する