Location>code7788 >text

Raspberry Pi CM4 (4): Raspberry Pi image replacement kernel

Popularity:93 ℃/2024-08-27 15:18:13

Raspberry Pi Image Replacement Kernel

1. Why replace the kernel

  • The official image provided by Raspberry Pi comes with a kernel version of6.6.31

  • neverthelessgithubThe kernel source code provided on6.6.40There's a slight difference.

  • In addition, it is likely that subsequent work such as kernel trimming and customization will be done, and replacing the kernel is an inextricable task

2. Getting the kernel source code

  • githubAddress:

    /raspberrypi/linux

  • Selection of use6.version of the kernel

  • Copy it to theubuntucenter

  • decompression (in digital technology)

    $ unzip linux-rpi-6.
    
  • Go to the kernel source directory

    $ cd linux-rpi-6.
    

3. Access to cross-compilation toolchain

  • The Raspberry Pi image I chose for thegccVersion 12.2.0

  • In order to avoid the appearance ofglibcversion inconsistencies, again using the12.2.0version of the cross-compilation toolchain

  • Download Address:

    /downloads/-/arm-gnu-toolchain-downloads

4. Kernel compilation

  • Modify the top levelMakefile, Designation of target platformsARCHand cross-compilation toolchainCROSS_COMPILE

  • The Raspberry Pi CM4 uses the method of the Broadcom BCM2711 and therefore uses thebcm2711_defconfigGenerate default configuration file

    linux-rpi-6.$ make bcm2711_defconfig
    
  • menuconfigconfigure

    linux-rpi-6.$ make menuconfig
    
  • Here I need to putLAN78XXThe driver is compiled directly into the kernel, not as an external module

  • Start compiling, kernel images/driver modules/device trees need to be compiled, using the-j$(nproc)Full-core compilation for faster compilation

    linux-rpi-6.$ make Image modules dtbs -j$(nproc)
    

5. Mount the Raspberry Pi image to Ubuntu

  • Choose to use the latest image that uses the Raspberry PiRaspberry Pi OS LiteMirror Download Address

    /raspios_lite_arm64/images/raspios_lite_arm64-2024-07-04/

  • Unzip it and get*.imgimage file of the

  • Mounting an image to Ubuntu

    # .img image
    jun@ubuntu:$ ls
    
    
    # View the first unused loopback device
    jun@ubuntu:$ losetup -f
    /dev/loop0
    
    # Associate the .img image to the loopback device
    jun@ubuntu:$ sudo losetup /dev/loop0
    [sudo] password for jun.
    
    # View the partitions, two are detected, the smaller is the system partition and the larger is the root filesystem
    jun@ubuntu:$ sudo kpartx -av /dev/loop0
    add map loop0p1 (253:0): 0 1048576 linear 7:0 8192
    add map loop0p2 (253:1): 0 4481024 linear 7:0 1056768
    
    # Create a system partition mount directory
    jun@ubuntu:$ mkdir boot
    
    # Create a root filesystem mount directory
    jun@ubuntu:$ mkdir rootfs
    
    # Mount the system partition
    jun@ubuntu:$ sudo mount /dev/mapper/loop0p1 . /boot/
    
    # Mount the root filesystem
    jun@ubuntu:$ sudo mount /dev/mapper/loop0p2 . /rootfs/
    
    # View system partitions
    jun@ubuntu:$ ls . /boot/
                        fixup_x.dat start_x.elf
                              initramfs_2712
                         fixup_cd.dat initramfs8 overlays start_cd.elf
                                         fixup_db.dat initramfs8 overlays start_cd.elf
                  fixup_db.dat kernel_2712.img
    
    # View root filesystem
    jun@ubuntu:$ ls . /rootfs/
    bin boot dev etc home lib lost+found media mnt opt proc root run sbin srv sys tmp usr var
    

6. Installation of the new kernel

  • Go to the kernel source directory

  • Installing kernel modules to the root filesystem

    linux-rpi-6.$ sudo env PATH=$PATH make INSTALL_MOD_PATH=../rootfs modules_install
    

  • Install header files to the root filesystem of theusrcatalogs

    linux-rpi-6.$ sudo make headers_install INSTALL_HDR_PATH=../rootfs/usr/
    
  • mountingImageto the mountedbootallocated area (for housing, industry etc)

    linux-rpi-6.$ sudo cp arch/arm64/boot/Image ../boot/
    
  • Installation of device tree files

    linux-rpi-6.$ sudo cp arch/arm64/boot/dts/broadcom/*.dtb ../boot/
    linux-rpi-6.$ sudo cp arch/arm64/boot/dts/overlays/*.dtb* ../boot/overlays/
    
  • interchangeability

    linux-rpi-6.$ sudo cp include/generated/uapi/linux/ ../rootfs/usr/include/linux/
    

7. Solve the problem that the kernel module cannot be loaded

  • go intorootfsThe directory in the root file system where the driver is stored

    linux-rpi-6.$ cd ./rootfs/lib/modules/6.6.40-v8/
    
  • Looking for the .ko file and finding no kernel module

    6.6.40-v8$ find . -name *ko
    
  • Inspection reveals that the directory is full of.file for the reason thatmake module_installWhen the command is executed, the .ko file is automatically compressed, but this will result in the kernel not being able to load the module when booting, so you need to decompress the compressed .ko file.

  • Writing scriptsmodules_install.shIt is used to generate, when the system is first powered on, thefile, store the script in therootfs/lib/modules/6.6.40-v8/directory, the script reads as follows

    #!/bin/bash
    
    # modules path
    MODULES_PATH=/lib/modules/$(uname -r)
    
    # decompression (in digital technology)*.file
    module_decompress()
    {
            MODULES_XZ_FILES=`find ${MODULES_PATH} -name *`
    
            for MODULE in ${MODULES_XZ_FILES}
            do
                    set -x
                    xz -dk ${MODULE}
                    set +x
            done
            
            return 0
    }
    
    RET=$(cat ${MODULES_PATH}/)
    if [ -z "${RET}" ]; then
            
        # modules decompress
        module_decompress
        
        # generate
        depmod
            
        # make sure this script executed only once
        SCRIPT_NAME=$(basename $0)
        sed -i "/$SCRIPT_NAME/d" /etc/
    		
        # reboot
        reboot
    else
            echo "modules already installed!"
    fi
    
    exit 0
    
  • Give scripts executable permissions

    6.6.40-v8$ sudo chmod 777 modules_install.sh
    
  • under the root filesystemetc/Add the following tomodule_install.shScripted boot-up

    # modules_install
    /bin/bash /lib/modules/$(uname -r)/modules_install.sh &
    

8. Selection of the kernel

  • modificationsbootpartition under the,
    $ sudo vi ./boot/
    
  • Add the following to the end of the file to indicate that you are booting with the new kernel
    kernel=
    

9. Unmount the image

  • abolish5. Mount the Raspberry Pi image to Ubuntuimage mounting of the
    jun@ubuntu:$ ls
      boot  rootfs
    jun@ubuntu:$ sudo umount ./boot 
    jun@ubuntu:$ sudo umount ./rootfs
    jun@ubuntu:$ sudo losetup -d /dev/loop0
    

10. Mirror burning

  • Refer to previous documentation;Raspberry Pi CM4 (I): Mirror Burn-in

11. Booting

  • It is normal that the system will automatically restart twice when it is turned on for the first time.
  • Check the kernel version, which is6.6.40, Kernel Replacement Successful
    # uname -a
    Linux IG-210 6.6.40-v8 #2 SMP PREEMPT Tue Aug 27 14:04:24 CST 2024 aarch64 GNU/Linux