网上教程很多,有些已经过时。还是推荐官网版本:源代码等可以到gitee下载,速度快。
https://www.raspberrypi.org/documentation/linux/kernel/building.md
这个教程也不错。
https://www.jianshu.com/p/6b81adc3f1b4
1. 下载源码,网址是国内镜像,github不翻墙太慢了。分支我只选了 rpi-4.11.y,总共150MB左右,整个git 几个G,没必要全部下载。第二行的 rpi-4.11.y 可以改成你希望的文件夹名,下载完如果没有代码只有 .git 就 git checkout
git clone https://gitee.com/zhangshengping/raspberrypi-linux.git --branch rpi-4.11.y \ --single-branch --depth 1 rpi-4.11.y
下载编译器 工具链
git clone https://gitee.com/ipcun/raspberrypi_tools.git ~/tools
实际用的只是这个:
2. 将工具链加入环境变量(让系统能找到它)
本次生效:
export PATH=$PATH:$HOME/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin
永久生效: $HOME 就是~ arm-linux-gnueabihf 是一个软链接,实际上指向 arm-rpi-4.9.3-linux-gnueabihf
echo PATH=\$PATH:~/tools/arm-bcm2708/arm-linux-gnueabihf/bin >> ~/.bashrc source ~/.bashrc
3. 编译
切换到源码目录,我的路径是~/rpi-4.11.y
选择芯片:For Pi 2, Pi 3, Pi 3+, or Compute Module 3: bcm2709_defconfig
kernel8.img
:64位的Raspberry Pi 3和Raspberry Pi 4;
kernel7l.img
:32位的Raspberry Pi 4(使用LPAE);
kernel7.img
:32位的Raspberry Pi 4、Raspberry Pi 3和Raspberry Pi 2(未使用LPAE);
kernel.img
:其他版本的树莓派。
cd ~/rpi-4.11.y
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
配置内核:可以一切默认:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
编译: $(nproc) 获取你的cpu最大支持线程,全速编译
make ARCH=arm \ CROSS_COMPILE=arm-linux-gnueabihf- \ -j $(nproc)\ zImage dtbs modules
4. 获取内核镜像和设备树
编译生成的各种最终文件的区别可以参考:
https://www.cnblogs.com/lemaden/p/10438499.html
https://www.cnblogs.com/Oude/articles/12039025.html
zImage:
~/rpi-4.11.y/arch/arm/boot/zImage
dtb:
~/rpi-4.11.y/arch/arm/boot/dts/bcm2710-rpi-2-b.dtb
用zImage 就可以启动qemu。当然解压成 kernel7.img 也可以。
官网教程没说:怎么用zImage制作kernel.img,就是:
~/rpi-4.11.y/scripts/mkknlimg ~/rpi-4.11.y/arch/arm/boot/zImage
~/rpi-4.11.y/scripts/
kernel7.img
注意:驱动安装我没有做,请参考这个 https://www.jianshu.com/p/6b81adc3f1b4
5. qemu配置
windows .bat 脚本
cd C:\Program Files\qemu qemu-system-arm.exe ^ -M raspi2 ^ -cpu cortex-a7 ^ -dtb C:\\store\qemu-rpi-kernel\bcm2710-rpi-2-b.dtb ^ -kernel C:\store\qemu-rpi-kernel\zImage ^ -drive id=hd-root,format=raw,file=C:\store\qemu-rpi-kernel\2019-09-26-raspbian-buster-lite.img ^ -m 1024 ^ -show-cursor ^ -append "rw earlyprintk=ttyAMA0,115200 loglevel=8 console=ttyAMA0,115200 root=PARTUUID=6c586e23-02 rootfstype=ext4 rootwait" ^ -serial mon:stdio