Note: This has been tested on Ubuntu 18.04.
Prerequisites
sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
gawk build-essential bison flex texinfo gperf libtool patchutils bc \
zlib1g-dev libexpat-dev git
Getting the sources
首先,創建一個工作文件夾,將所有資源全部下載到該文件夾下;
mkdir riscv64-linux
cd riscv64-linux
然後就開始下載所需要的資源,分別包含了下面5部分:
- RISC-V newlib and Linux toolchain
- QEMU
- Linux
- BBL (Berkeley Boot Loader)
- Busybear Linux (RISC-V root filesystem image)
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
git clone https://github.com/qemu/qemu
git clone https://github.com/torvalds/linux
git clone https://github.com/riscv/riscv-pk
git clone https://github.com/michaeljclark/busybear-linux
Note: 本人試過,最難的是第一個riscv-gnu-toolchain,各種submodules. 嘗試過用Gitee,但是有的模塊沒有成功,主要可能因爲跟網速有關(有興趣可以嘗試一下,說不定你就成功了,哈哈)。我最後是在公司服務器上用上述源地址clone成功的。
Building
build toolchain
cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv64 // pick an install path, e.g. /opt/riscv64
make newlib -j $(nproc)
make linux -j $(nproc)
//導出環境變量(也可以在/etc/profile末尾添加下面兩行,這樣重啓後不會丟失)
export PATH="$PATH:/opt/riscv64/bin"
export RISCV="/opt/riscv64"
build QEMU with the RISC-V target
cd qemu
git checkout v3.0.0
./configure --target-list=riscv64-softmmu
make -j $(nproc)
sudo make install
Note:
- configure時可能會報錯,錯誤原因如果是: glib-2.40 gthread-2.0 is required to compile QEMU,可以參考此文解決configure QEMU時遇到缺少庫和工具的問題。
- make如果遇到卡到一個config_all_devices的地方,可以選擇耐心等待(由於它還是在clone,失敗後會Retry),也可中止後重新make,直到make沒有error.
build Linux for the RISC-V target
cd linux
git checkout v4.19-rc3
cp ../busybear-linux/conf/linux.config .config
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- olddefconfig
# enter kernel configuration
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- nconfig
確認下面三個選項被選上。
- ARCH_RV64I (Platform type->Base ISA)
- CMODEL_MEDANY (Platform type->Kernel Code Model)
- CONFIG_SIFIVE_PLIC (Device Drivers)
# After accepting changes in the configuration, compile the kernel
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- vmlinux -j $(nproc)
build BBL
cd riscv-pk
mkdir build && cd build
../configure --enable-logo --host=riscv64-unknown-elf --with-payload=../../linux/vmlinux
make -j $(nproc)
build Busybear Linux
cd busybear-linux
make -j $(nproc)
Note:
- make時會先Download些資源,在自己電腦上一直download失敗,最後還是在公司服務器上成功。download完之後會自動配置。如果最後出現下圖,即生成busybear.bin意味着成功。
2. 自己雖然貌似從服務器download成功,但配置最後會報錯,問題還是缺少資源。發現busybear下的src文件夾下的riscv-pk是空文件夾。**解決方法是:**將之前編譯過的riscv-pk文件夾(與busybear-linux同級目錄)複製到src文件夾下,然後再重新編譯,成功生成busybear.bin文件。
Running
Go back to your main working directory(riscv64-linux) and run:
sudo qemu-system-riscv64 -nographic -machine virt \
-kernel riscv-pk/build/bbl -append "root=/dev/vda ro console=ttyS0" \
-drive file=busybear-linux/busybear.bin,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0
執行後,出現下面景象(kernel起來之後會要求輸入用戶名和密碼),恭喜你,基本成功了。
username------root
password------busybear
補充: busybear起來之後,如果遇到ntpd的問題,可暫時將/etc/inittab中下面這一行注掉:
::respawn:/usr/sbin/ntpd -n