首先,你需要从树莓派基金会官方github仓库上克隆源码。

git clone --depth=1 -b rpi-4.19.y https://github.com/raspberrypi/linux

如果你说你想用最新内核,就把rpi-4.19.y换成最新内核分支,例如rpi-5.5.y,代码完整拉取难度较大,所以这里只拉取第一层代码。

你是在x86主机上编译的话需要安装交叉编译工具链和一些软件包

apt-get install -y bc build-essential gcc-aarch64-linux-gnu flex libncurses5-dev bison

等代码克隆完成后,应用内核默认配置。

cd linux

树莓派3b,3b+

make ARCH=arm64 bcmrpi3_defconfig

树莓派4b

make ARCH=arm64 bcm2711_defconfig

事实证明,使用树莓派4b配置文件编译出来的内核3b和3b+也可以用。

要支持4b也是改一行命令的事,第一个适配树莓派64位系统和真正全系适配64位树莓派系统的opensuse并没有说什么,在opensuse的更新日志中提都没提,在他们看来这也只是应该的事,这里感谢opensuse的开源精神,向opensuse伟大的开源精神致敬。

其实我们还可以对内核进行自定义,比如说增加一些无线网卡的支持(可选)

make ARCH=arm64 menucnfig

下一步就是编译内核

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j1

-j1表示单核编译,如果你是在树莓派上编译的话可以把CROSS_COMPILE=aarch64-linux-gnu-删掉,-j1换成-j4,这个可能会等很久。

然后我们获取树莓派的bootloader

cd ..
git clone --depth=1 -b master https://github.com/raspberrypi/firmware

我们只需要里面的boot文件夹下的一些文件,所以把boot文件夹移动出来然后删掉不用的东西

mv firmware/boot . && rm -rf firmware
rm boot/*dtb && rm boot/kernel*

然后把linux/arch/arm64/boot/Image复制到boot文件夹里命名为kernel8.img,把 linux/arch/arm64/boot/dts/broadcom/ 里的dtb复制到boot文件夹里。

cp linux/arch/arm64/boot/Image boot/kernel8.img
cp linux/arch/arm64/boot/dts/broadcom/*dtb boot

然后再添加config.txt,默认如下。

# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
#dtoverlay=vc4-fkms-v3d

然后添加cmdline.txt(如果是采用efi启动则不需要,cmdline写在grub.cfg里的)

console=serial0,115200 console=tty1 root=/dev/mmcblk0p3 rootfstype=f2fs elevator=deadline fsck.repair=yes rootwait

当然,你说这好复杂,那么你可以看下面。

树莓派基金会也默认对pi4增加了64位内核,同样的,树莓派3也可以用,接下来我们就用树莓派官方提供的软件包解包来获取这些东西。

mkdir kernel

分别获取firmware及一些库文件并解包到rootfs文件夹

wget -O kernel/firmware-bin.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/libraspberrypi-bin_1.20190925-2_armhf.deb
sudo dpkg -x kernel/firmware-bin.deb tmp
cp -rfp tmp/* rootfs
rm -rf tmp/*

wget -O kernel/firmware-dev.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/libraspberrypi-dev_1.20190925-2_armhf.deb
sudo dpkg -x kernel/firmware-dev.deb tmp
cp -rfp tmp/* rootfs
rm -rf tmp/*

wget -O kernel/libraspberrypi0.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/libraspberrypi0_1.20190925-2_armhf.deb
sudo dpkg -x kernel/libraspberrypi0.deb tmp
cp -rfp tmp/lib/* rootfs/lib
rm -rf tmp/lib
cp -rfp tmp/* rootfs
rm -rf tmp/*

然后是获取bootloader并解包

wget -O kernel/bootloader.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/raspberrypi-bootloader_1.20190925-2_armhf.deb
sudo dpkg -x kernel/bootloader.deb tmp
cp -rfp tmp/* $ROOTFS
rm -rf tmp/*

接着是内核

wget -O kernel/kernel.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/raspberrypi-kernel_1.20190925-2_armhf.deb
sudo dpkg -x kernel/kernel.deb tmp
cp -rfp tmp/lib/* $ROOTFS/lib
rm -rf tmp/lib
cp -rfp tmp/* $ROOTFS
rm -rf tmp/*

然后是内核头文件,如果你后期不需要额外编译一些内核模块的话,这一步可以不要。

mkdir kernel-headers
wget -O kernel-headers/headers.deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/pool/main/r/raspberrypi-firmware/raspberrypi-kernel-headers_1.20190925-2_armhf.deb
sudo dpkg -x kernel-headers/headers.deb tmp
cp -rfp tmp/lib/* $ROOTFS/lib
rm -rf tmp/lib
cp -rfp tmp/* $ROOTFS
rm -rf tmp/*

你可能发现了,上面代码中复制操作为什么弄那么复杂,因为用dpkg-deb命令在解包时会出现清空文件夹的情况,导致你前面解包的文件被清空。。。。

最后是删除我们创建的一些文件夹和32位的内核等

rm -rf $ROOTFS/lib/modules/*7+
rm -rf $ROOTFS/lib/modules/*7l+
rm -rf kernel
rm -rf kernel-headers
rm $ROOTFS/boot/kernel.img
rm $ROOTFS/boot/kernel7.img
rm $ROOTFS/boot/kernel7l.img
rm -rf tmp

然后,内核就准备好了。