交叉编译树莓派Linux内核

使用Ubuntu 22.04 for x64进行交叉编译树莓派官方的64位linux内核。

1. 安装环境

$ sudo apt install build-essential bison flex gawk texinfo file git ssh libc6-dev libssl-dev libncurses-dev
$ sudo apt install crossbuild-essential-arm64

配置Git:

$ git config --global user.name "<You name>"
$ git config --global user.email "<You email>"

填写格式符合要求的名字和邮箱。

2. 下载源码

$ mkdir -p ~/rpi
$ cd ~/rpi
$ git clone --depth=1 https://github.com/raspberrypi/linux
$ cd linux
$ git branch -vv
* rpi-6.12.y 8d3206ee4 [origin/rpi-6.12.y] arm64: dts: bcm2712-rpi: Add uart0_dma parameter

这里下载的是默认版本,你也可以指定版本下载,格式如下:

$ git clone --depth=1 --branch <branch> https://github.com/raspberrypi/linux

其中branch参考如下:

  • rpi-6.12.y
  • rpi-6.15.y
  • rpi-6.14.y
  • rpi-6.13.y
  • rpi-6.11.y
  • rpi-6.10.y
  • rpi-6.9.y
  • rpi-6.8.y
  • rpi-6.7.y
  • rpi-6.6.y
  • rpi-6.5.y
  • rpi-6.4.y
  • rpi-6.3.y
  • rpi-6.2.y
  • rpi-6.1.y
  • rpi-6.0.y
  • rpi-5.19.y
  • rpi-5.18.y
  • rpi-5.17.y
  • rpi-5.16.y
  • rpi-5.15.y
  • rpi-5.14.y
  • rpi-5.13.y
  • rpi-5.12.y
  • rpi-5.11.y
  • rpi-5.10.y
  • rpi-5.9.y
  • rpi-5.8.y
  • rpi-5.7.y
  • rpi-5.6.y
  • rpi-5.5.y
  • rpi-5.4.y
  • rpi-5.3.y
  • rpi-5.2.y
  • rpi-5.1.y
  • rpi-5.0.y
  • ...

3. 配置

分支1: Raspberry Pi 3、3+、4, Zero 2W, computer 3、3+、4、4s, Pi-400:

$ cd ~/rpi/linux
$ KERNEL=kernel8
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig

分支2: Raspberry pi 5:

$ cd ~/rpi/linux
$ KERNEL=kernel_2712
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2712_defconfig

定制化配置内核功能(通用):

$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

为了区分内核是自己编译的还是其它渠道(如官方)获取的,我们修改'.config':

~/rpi/linux/.config

做如下修改(字符随便写):

--- CONFIG_LOCALVERSION="-v8"
+++ CONFIG_LOCALVERSION="-v8-hello"

4. 编译

$ cd ~/rpi/linux
$ make -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs

5. 刷机

把已经刷好Raspbian的SD卡插入计算机,并识别为USB大容量设备,假设是'/dev/sdb1' (boot), '/dev/sdb2' (root).

$ cd ~/rpi/linux
$ mkdir mnt
$ mkdir mnt/boot
$ mkdir mnt/root
$ sudo mount /dev/sdb1 mnt/boot
$ sudo mount /dev/sdb2 mnt/root

安装模块:

$ cd ~/rpi/linux
$ sudo env PATH=$PATH make -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=mnt/root modules_install

安装内核和设备树等文件:

$ cd ~/rpi/linux
$ sudo cp mnt/boot/$KERNEL.img mnt/boot/$KERNEL-backup.img
$ sudo cp arch/arm64/boot/Image mnt/boot/$KERNEL.img
$ sudo cp arch/arm64/boot/dts/broadcom/*.dtb mnt/boot/
$ sudo cp arch/arm64/boot/dts/overlays/*.dtb* mnt/boot/overlays/
$ sudo cp arch/arm64/boot/dts/overlays/README mnt/boot/overlays/
$ sync
$ sudo umount mnt/boot
$ sudo umount mnt/root
$ sudo rm -rf mnt

原本内核:

$ uname -a
linux raspberrypi 6.12.25+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025-4-30) aarch64 GNU/Linux

编译内核:

$ uname -a
linux raspberrypi 6.12.27-v8-hello+ #1 SMP PREEMPT Mon May 1 12:34:56 UTC 2025 aarch64 GNU/Linux

把SD卡重新插回树莓派,启动即可使用新内核。

posted @ 2025-05-09 17:33  this毛豆  阅读(199)  评论(0)    收藏  举报