构建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

这会将GCC安装到/opt/riscv

此外, 如果需要编译不带浮点支持的, 可以加上--with-arch=rv64imac --with-abi=lp64参数。

二、构建QEMU

cd qemu
./configure --prefix=/opt/riscv --target-list=riscv64-softmmu,riscv64-linux-user
make
make install
cd ../..

这会将QEMU安装到/opt/riscv

如果提示找不到pkg-config包, 安装一下即可:

sudo apt-get install pkg-config

如果./configure的时候提示缺少什么包, 用如下命令安装即可:

sudo apt install libglib2.0-dev
sudo apt install libpixman-1-dev

三、构建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
mv linux-5.2.11 linux
cd linux
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make
cd ..

另外, 也可以用如下命令来设置各个选项:

ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- make menuconfig

四、(已废弃)构建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/vmlinux
make
cp bbl ../../image.elf
cd ../..

四、构建OpenSBI

git clone https://github.com/riscv-software-src/opensbi.git
cd opensbi
make CROSS_COMPILE=riscv64-unknown-linux-gnu- PLATFORM=generic FW_PAYLOAD_PATH=../linux/arch/riscv/boot/Image
cp build/platform/generic/firmware/fw_payload.elf ../image.elf
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
cd dev
sudo mknod console c 5 1
sudo mknod null c 1 3
sudo mknod random c 1 8
sudo mknod urandom c 1 9
sudo mknod zero c 1 5
cd ..
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或OpenSBI)并加载根目录磁盘镜像

qemu-system-riscv64 -machine virt -nographic \
    -kernel image.elf -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/
  4. https://github.com/riscv-software-src/opensbi/blob/master/docs/firmware/fw_payload.md

发表评论?

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