本地编译树莓派Linux内核

使用Raspbian bookworm aarch64在树莓派上本地编译适配树莓派的64位linux内核。

1. 安装环境

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

配置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 linux
$ KERNEL=kernel8
$ make bcm2711_defconfig

分支2,Raspberry pi 5:

$ cd linux
$ KERNEL=kernel_2712
$ make bcm2712_defconfig

定制化配置(通用):

$ make menuconfig

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

~/rpi/linux/.config

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

--- CONFIG_LOCALVERSION="-v8"
+++ CONFIG_LOCALVERSION="-v8-nihao"

4. 编译

$ cd ~/rpi/linux
$ make -j4 Image.gz modules dtbs

5. 刷机

安装模块:

$ cd ~/rpi/linux
$ sudo make -j4 modules_install

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

$ sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img
$ sudo cp arch/arm64/boot/Image.gz /boot/firmware/$KERNEL.img
$ sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/
$ sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/
$ sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/

原本内核:

$ 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-nihao+ #1 SMP PREEMPT Debian 1:6.12.27 (2025-5-1) aarch64 GNU/Linux

重启:

$ sudo reboot

重启即可使用新内核。

6. 内核头文件

如果需要在raspberry pi开发内核(含驱动),且无内核源码的情况下,可以安装内核头文件:

$ sudo apt install linux-headers-rpi-v8

然后,就可以使用linux内核头文件进行开发了。

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