#!/bin/bash
echo =========================================
echo 树莓派3内核下载及编译替换 raspberry3 kernel 4.19
echo =========================================
echo ======如果在windows下编译执行出现‘损坏的编译器’ sed -i 's/\r$//' filename.sh即可恢复正常======
echo ======.sh文件在哪个目录下执行,树莓派内核下载也在该目录下进行======
echo 1.下载树莓派linux 内核
git clone https://github.com/raspberrypi/linux.git
echo 2.配置编译环境
sudo apt-get install bc
sudo apt-get arm-bcm2708hardfp-linux-gnueabi
sudo apt-get arm-bcm2708-linux-gnueabi
sudo apt-get arm-rpi-4.9.3-linux-gnueabihf
sudo apt-get gcc-linaro-arm-linux-gnueabihf-raspbian
sudo apt-get gcc-linaro-arm-linux-gnueabihf-raspbian-x64
echo 3.安装工具链(可能和步骤2有重复)
git clone https://github.com/raspberrypi/tools
cp -a tools /
echo 4.将交叉编译工具加入临时环境变量(32bit/64bit),只在当前窗口有效,如果想设定永久变量 vim /etc/profile 进行设定
export PATH=$PATH:/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
export PATH=$PATH:/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin
export PATH=$PATH:/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin
export PATH=$PATH:/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
export PATH=$PATH:/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin
echo 5.在Makefile 中配置ARCH 以及编译工具
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
echo 6.Kernel Configuration 生成.config 文件(这个步骤需要手动配置,不能直接通过.sh 执行)
make menuconfig
echo 7.编译内核镜像
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 zImage modules dtbs
echo 8.创建两个挂载文件夹
mkdir /mnt/fat32
mkdir /mnt/ext4
echo 9.将能够正常启动的,含有树莓派img 的SD 插入主机,df -lk 获取挂载分区(假设挂载在sda1 sda2)
sudo mount /dev/sda1 /mnt/fat32
sudo mount /dev/sda2 /mnt/ext4
echo 10.安装内核模块
sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=/mnt/ext4 modules_install
echo 11.将编译好的内核和设备树文件替换到SD 卡中(先将原本SD 卡中的img 复制保存一份)
sudo cp /mnt/fat32/kernel7.img ./
sudo scripts/mkknlimg arch/arm/boot/zImage /mnt/fat32/kernel7.img
sudo cp arch/arm/boot/dts/*.dtb /mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README /mnt/fat32/overlays/
echo 12.卸载SD卡的挂载
sudo umount /mnt/fat32
sudo umount /mnt/ext4
将SD 卡插入树莓派,上电启动,进入控制终端:$uname -a 查看内核是否已经替换成4.19