A set of scripts to build a working Debian image for RISC-V. This includes usable GDB!
Disclaimer: following recipe is (semi-regularly) tested on Debian Testing (Bullseye at the time of writing). Debian 10 (Buster) is known to work too - but see comment below! If you have some other Debian-based distro, e.g, Ubuntu, this recipe may or may not work!
-
Compile and install RISC-V GNU toolchain. See RISC-V GNU toolchain README on how to do so - in short following should do it:
sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev git clone https://github.com/riscv/riscv-gnu-toolchain cd riscv-gnu-toolchain git submodule update --init --recursive ./configure --prefix=/opt/riscv --enable-linux make linux
This toolchain is needed to compile the kernel. As of 2020-07-27, the Debian-provided RISC-V cross toolchain is not able to link Linux kernel and fails with link error.
-
(Optional) Install recent QEMU:
sudo apt-get install git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build git clone --recursive https://git.qemu.org/git/qemu.git cd qemu ./configure --target-list=riscv64-softmmu --prefix=/opt/riscv make make install
This QEMU is needed to run installed Debian in QEMU. As of 2020-07-27, the Debian-provided QEMU hangs in early initialization.
-
Add Debian Unstable (Sid) repositories to your system:
printf "Package: *\nPin: release a=unstable\nPin-Priority: 10\n" | sudo tee /etc/apt/preferences.d/unstable.pref printf "deb http://ftp.debian.org/debian unstable main\ndeb-src http://ftp.debian.org/debian unstable main\n" | sudo tee /etc/apt/sources.list.d/unstable.list sudo apt-get update
-
Install QEMU and
mmdebstrap
and repository keys (req'd to build root filesystem and run installed system):sudo apt-get install mmdebstrap qemu-user-static qemu-system-misc binfmt-support debian-ports-archive-keyring gcc-riscv64-linux-gnu rsync
For Debian 10 (Buster) users: Debian 10 has old debian-ports repository keys (2018.12.27) which are no longer valid (at the time of writing - 2020-05-14). You need to download and install most recent version of package debian-ports-archive-keyring:
wget http://ftp.debian.org/debian/pool/main/d/debian-ports-archive-keyring/debian-ports-archive-keyring_2019.11.05_all.deb
sudo dpkg -i debian-ports-archive-keyring_2019.11.05_all.deb
git clone https://github.com/janvrany/riscv-debian.git
git -C riscv-debian submodule update --init --recursive
Scripts below do use sudo quite a lot. IF THERE"S A BUG, IT MAY WIPE OUT YOUR SYSTEM. DO NOT RUN THESE SCRIPTS WITHOUT READING THEM CAREFULLY FIRST.
They're provided for convenience. Use at your own risk.
-
Run:
./debian-mk-kernel.mk
by default, PATH to cross-compiler is set in debian-mk-kernel.mk (/opt/riscv/bin/riscv64-unknown-linux-gnu-)
If your cross-compiler is different you may set your configuretion like this:CROSS_COMPILE=/your/path/riscv/toolchain/bin/riscv64-linux-gnu- ./debian-mk-kernel.mk
This will leave QEMU bootable kernel image (BBL + kernel image) in
bbl-q
. The image for HiFive Unleashed isbbl-u
, QEMU image simply won't boot !!!
-
Create a file containing Debian root filesystem. This is optional, you may use directly a device (say
/dev/mmcblk0p2
) or ZFS zvolume (/dev/zvol/...
). You will need at least 4GB of space but for development, use 8G (or more). C++ object files with full debug info can be pretty big.To make plain file image:
truncate -s 8G debian.img /sbin/mkfs.ext3 debian.img
-
Install Debian into that image:
./debian-mk-rootfs.sh debian.img
Please note, that Rebian repository for RISC-V arch is really shaky, at times
apt-get
may fail because unsatisfiable dependencies. In that case, either wait or fiddle about somehow.
You may want to install GDB in order to debug programs. To install GDB that, run
./debian-mk-gdb.sh debian.img
Note, that this may (will) take a lot, lot of time when using QEMU. If you
intend to use Debian on real hardware, e.g., HiFive Unleashed, you may want to
compile GDB manually there. To do so, follow the steps in ./debian-mk-gdb.sh
script.
If you want to run RISC-V Jenkins build slave, run
./debian-mk-jenkins.sh debian.img /path/to/jenkins.id_rsa.pub
You need to provide a path to PUBLIC SSH RSA key that Jenkins master would use to connect to the slave.
On Jenkins master, use SSH to connect to the slave, username is jenkins
and use
the corresponding key.
Following steps assumes the SD card (say /dev/mmcblk0
) is properly partioned.
If not, please follow steps at the bottom of in freedom-u-sdk/Makefile.
In short:
sgdisk --clear \
--new=1:2048:67583 --change-name=1:bootloader --typecode=1:2E54B353-1271-4842-806F-E436D6AF6985 \
--new=2:264192: --change-name=2:root --typecode=2:0FC63DAF-8483-4772-8E79-3D69D8477DE4 \
/dev/mmcblk0
Then:
-
To install kernel on SD card:
./unleashed-install-kernel.sh /dev/mmcblk0p1
-
To install Debian root filesystem on SD card (say
/dev/mmcblk0
)./unleashed-install-rootfs.sh debian.img /dev/mmcblk0p2
Now take your SD card, insert it into Unleashed and hope for the best.
You can connect to Unleashed serial console by using screen
:
sudo screen /dev/ttyUSB1 115200
./qemu-fire.sh
Sometimes it happened to me that /var/lib/dpkg/available
disappeared.
This prevents dpkg
/ apt
from removing packages. Following command
fixed this for me:
sudo dpkg --clear-avail && sudo apt-get update
This code is licensed under MIT license. See LICENSE.txt
.
- https://wiki.debian.org/RISC-V
- https://github.com/jim-wilson/riscv-linux-native-gdb/blob/jimw-riscv-linux-gdb/README.md
- https://groups.google.com/a/groups.riscv.org/forum/#!msg/sw-dev/jTOOXRXyZoY/BibnmSTOAAAJ
- https://wiki.debian.org/InstallingDebianOn/SiFive/HiFiveUnleashed#Building_a_Kernel
- sifive/freedom-u-sdk#44
- https://github.com/rwmjones/fedora-riscv-kernel
- https://github.com/andreas-schwab/linux
- https://forums.sifive.com/t/linux-4-20-on-hifive-unleashed/1955
- SiFive HiFive Unleashed Getting Started Guide