Get the qemu, compiler tool-chain and below software package
- qemu-system-riscv64
- riscv64-linux-gnu-
- busybox
- linux
- opensbi (optional)
- u-boot (optional)
Reference:
https://risc-v-machines.readthedocs.io/en/latest/linux/simple/#pull
https://www.qemu.org/docs/master/system/riscv/virt.html
https://gitlab.inf.ethz.ch/project-openenzian/applications/risc-v/riscv-gnu-toolchain
Build the Linux
export CROSS_COMPILE=riscv64-linux-gnu-
make ARCH=riscv defconfig
make ARCH=riscv -j 8
the image will be generated at arch/riscv/boot/Image
Build the Busybox
export CROSS_COMPILE=riscv64-linux-gnu-
make ARCH=riscv menuconfig
make it as static library
make ARCH=riscv
After fix the compile error (set CONFIG_SHA1_HWACCEL=0), busybox will be generated
Build the initramfs
mkdir initramfs
cd initramfs
mkdir -p {bin,sbin,dev,etc,home,mnt,proc,sys,usr,tmp}
mkdir -p usr/{bin,sbin}
mkdir -p proc/sys/kernel
cd dev
sudo mknod sda b 8 0
sudo mknod console c 5 1
cd ..
cp ../busybox/busybox ./bin/
vi init #Change the init file as:
#!/bin/busybox sh
# Make symlinks
/bin/busybox --install -s
# Mount system
mount -t devtmpfs devtmpfs /dev
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t tmpfs tmpfs /tmp
# Busybox TTY fix
setsid cttyhack sh
# https://git.busybox.net/busybox/tree/docs/mdev.txt?h=1_32_stable
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
sh
Quit the vi, add the +x permission
chmod +x init
Pack as cpio.gz format
find . -print0 | cpio --null -ov --format=newc | gzip -9 > initramfs.cpio.gz
Boot the Linux kernel
qemu-system-riscv64 -machine virt -m 2G -nographic -kernel linux/arch/riscv/boot/Image -initrd initramfs/initramfs.cpio.gz
Compile u-boot
riscv64_smode_defconfig
Boot the FreeBSD
Download the FreeBSD, choose zfs.raw.xz format
Uncompress the file by using xz command and convert to qcow2 format
qemu-system-riscv64 -machine virt -m 2G -nographic -kernel u-boot.bin -drive file=freebsd-zfs.qcow2,format=qcow2,id=hd0 -device virtio-blk-device,
drive=hd0 -netdev user,id=net0 -device virtio-net-device,netdev=net0