构建RISC-V工作环境

今天构建了RISC-V的工作环境, 分为如下几步:

一、构建编译器

https://github.com/riscv/riscv-gnu-toolchain下载源代码, 然后编译:

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git submodule update --init --recursive
sudo apt-get 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
./configure --prefix=/opt/riscv
make linux

二、构建QEMU

cd qemu
./configure --target-list=riscv64-softmmu
make
cd ../..

三、构建Linux

首先将RISC-V工具链的bin目录加入PATH环境变量中, 然后下载内核并编译。

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.2.11.tar.xz
tar xvf linux-5.2.11.tar.xz
cd linux-5.2.11
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make vmlinux
cd ..

四、构建riscv-pk

git clone https://github.com/riscv/riscv-pk.git
cd riscv-pk
mkdir -p build
cd build
../configure \
    --disable-fp-emulation \
    --enable-logo \
    --enable-print-device-tree \
    --host=riscv64-unknown-linux-gnu \
    --with-payload=../../linux-5.2.11/vmlinux
make
cp bbl ../../image.bin
cd ../..

五、构建BusyBox

wget https://busybox.net/downloads/busybox-1.31.0.tar.bz2
tar xvf busybox-1.31.0.tar.bz2
cd busybox-1.31.0
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make menuconfig
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make install
cd ..

其中, menuconfig若提示缺少ncurses, 则还需要安装这个包:

sudo apt-get install libncurses5-dev

此外, menuconfig配置BusyBox编译选项时还需开启静态编译(Settings -> Build Options -> Build static binary (no shared libs))。

六、制作根目录磁盘镜像

dd if=/dev/zero of=disk.img bs=1M count=128
mkfs -t ext4 disk.img
mkdir mnt
sudo mount disk.img mnt
sudo rsync -avr busybox-1.31.0/_install/* mnt
sudo chown -R root:root mnt/*
cd mnt
sudo mkdir -p proc sys dev etc/init.d

sudo sh -c 'cat >etc/init.d/rcS <<EOF
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s

/sbin/ip link set lo up
EOF'

sudo chmod +x etc/init.d/rcS
cd ..
sudo umount mnt

七、用QEMU启动内核(bbl)并加载根目录磁盘镜像

qemu-system-riscv64 -machine virt -nographic \
    -kernel image.bin -append "root=/dev/vda ro" \
    -smp 4 \
    -m 1024 \
    -drive file=disk.img,format=raw,id=hd0 \
    -device virtio-blk-device,drive=hd0 \
    -netdev type=user,id=net0 \
    -device virtio-net-device,netdev=net0

此外, 还可以根据需要配置网络, 本文不再赘述。

最终结果如图所示:

八、参考文献

  1. https://coldnew.github.io/6cc46ece/
  2. https://wiki.qemu.org/Documentation/Platforms/RISCV
  3. https://www.cnx-software.com/2018/03/16/how-to-run-linux-on-risc-v-with-qemu-emulator/

发表评论?

2 条评论。

  1. 您这篇文章几乎是我找到唯一能用的。
    感谢!
    另外在第六项中应该把'<‘替换为'<'吧,我在centos7上不替换直接执行会报错。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/13406QrCode