- rust-for-linux
- Debian GNU/Linux 12 (bookworm) aarch64 in Parallel Desktop Virtual Machine
- QEMU qemu-system-aarch64 version 7.2.5
- rust-for-linux arm64 6.6.0-rc4
- BusyBox v1.35.0 (Debian 1:1.35.0-4+b3) multi-call binary.
使用fujita的linux分支
git clone https://github.com/fujita/linux -b rust-dev --depth=1
按照指导安装依赖,
- 但是注意cargo install bindgen,没有后面的cli,因为fujiya使用的是比较早的linux kernel版本
make ARCH=arm64 LLVM=1 O=build defconfig
make ARCH=arm64 LLVM=1 O=build menuconfig
#set the following config to yes
General setup
---> [*] Rust support
make ARCH=arm64 LLVM=1 -j8
sudo apt install qemu-user qemu-system-arm # 安装qemu 7.x.x
qemu 8.x.x以上运行DQIB kernel会报错 => "...net user not compiled into kernel..."
- Debian Quick Image Baker(DQIB) 另一个选择,但是不是很会用
make menuconfig ARCH=arm64
Settings --->
--- Build Options
[*] Build static binary (no shared libs)
make -j20
make install
cat qemu-init.sh
#!/bin/sh
busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox poweroff -f
cat qemu-initramfs.desc
dir /bin 0755 0 0
file /bin/busybox busybox 0755 0 0
slink /bin/sh /bin/busybox 0755 0 0
file /init qemu-init.sh 0755 0 0
# 制作img
# 每次修改完qemu-init.sh & qemu-initramfs.desc要记得重新制作
../linux-fujita/build/usr/gen_init_cpio qemu-initramfs.desc > qemu-initramfs.img
按照指导添加代码
Kernel hacking
---> Sample Kernel code
---> Rust samples
---> <M>Print Helloworld in Rust (NEW) # 其它也要勾选
cat qemu-init.sh
#!/bin/sh
busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox insmod rust_helloworld.ko
busybox poweroff -f
cat qemu-initramfs.desc
dir /bin 0755 0 0
file /bin/busybox busybox 0755 0 0
slink /bin/sh /bin/busybox 0755 0 0
file /init qemu-init.sh 0755 0 0
file /rust_helloworld.ko ../linux-fujita/build/samples/rust/rust_helloworld.ko 0755 0 0
- QEMU启动参数
#!/bin/sh
qemu-system-aarch64 \
-machine 'virt' \
-cpu 'cortex-a57' \
-m 1G \
-netdev user,id=net,hostfwd=tcp::2222-:22 \
-kernel /home/junzhe/Dev/Software/linux-fujita/build/arch/arm64/boot/Image \
-initrd ../busybox/qemu-initramfs.img \
-nographic \
-append "root=LABEL=rootfs console=ttyAMA0" \
git clone https://github.com/fujita/rust-e1000
make KDIR=/home/junzhe/Dev/Software/linux-fujita/build/ LLVM=1
cat qemu-initramfs.desc
dir /bin 0755 0 0
file /bin/busybox busybox 0755 0 0
slink /bin/sh /bin/busybox 0755 0 0
file /init qemu-init.sh 0755 0 0
file /rust_helloworld.ko ../linux-fujita/build/samples/rust/rust_helloworld.ko 0755 0 0
file /rust_e1000.ko ../rust-e1000/rust_e1000.ko 0755 0 0 # 新增
cat qemu-init.sh
#!/bin/sh
busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox echo "[INFO] Modules Loaded"
busybox insmod rust_helloworld.ko
busybox insmod rust_e1000.ko
busybox echo "============================"
busybox echo "[INFO] Network configured"
busybox echo "IP set to 192.168.100.224"
busybox ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
busybox ifconfig eth0 192.168.100.224 netmask 255.255.255.0 broadcast 192.168.100.255 up
busybox ip addr
busybox echo "============================"
export 'PS1=(kernel) >'
busybox sh
busybox poweroff -f
qemu-system-aarch64 \
-machine 'virt' \
-cpu 'cortex-a57' \
-m 1G \
-kernel /home/junzhe/Dev/Software/linux-fujita/build/arch/arm64/boot/Image \
-initrd ../busybox/qemu-initramfs.img \
-netdev tap,ifname=tap0,id=tap0,script=no,downscript=no -device e1000,netdev=tap0 \
-nographic \
-append "root=LABEL=rootfs console=ttyAMA0" \
- ping通
-
增加自定义函数并编译成功
在menuconfig中搜索e1000,并停用
编译后对e1000模块进行加载
busybox insmod e1000_for_linux.ko
busybox ifconfig eth0 up
busybox ifconfig eth0 10.0.2.20
busybox ip route add default via 10.0.2.2 dev eth0
- ping通外网
在树莓派模拟器上,适配Rust for Linux内核,并实现Rust Uart串口驱动
- 下载Raspberry Pi OS image: https://www.raspberrypi.com/software/operating-systems/
- 由于较新版的Raspberry Pi OS不再支持默认用户名密码登陆,所以需要在镜像根目录添加userconf.txt,手动添加用户,利用openssl生成密码密文
echo 'raspberry' | openssl passwd -6 -stdin
- 复制出内核vmlinuz(后面会替换成rust-for-linux内核)和文件系统initrd
- 适配rust-for-linux 内核
# 目前最新的
git clone https://github.com/Rust-for-Linux/linux -b rust-dev --depth=1
make ARCH=arm64 LLVM=1 O=build defconfig
make ARCH=arm64 LLVM=1 O=build menuconfig
#set the following config to yes
General setup
---> [*] Rust support
make ARCH=arm64 LLVM=1 -j8
- QEMU 运行
qemu-system-aarch64 \
-cpu cortex-a57 \
-M virt \
-m 1G \
-kernel ../linux/build/arch/arm64/boot/Image \
-initrd ../raspberry-pi/initrd.img \
-drive file=../raspberry-pi/2023-10-10-raspios-bookworm-arm64-lite.img,if=none,id=drive0,cache=writeback -device virtio-blk,drive=drive0,bootindex=0 \
-append 'root=/dev/vda2 noresume rw' \
-nographic \
-no-reboot \
Linux 中的 UART 驱动通常作为内核的一部分提供,而不是独立于内核的用户空间程序。UART 驱动的源代码位于 Linux 内核源代码的 drivers/tty/serial/ 目录下。在这个目录下,你可以找到多个 UART 驱动,每个驱动通常对应于支持的特定硬件。
TODO: