Cross-compile Linux kernel source code with Ubuntu and deploy it to Raspberry Pi 4B

Reference article
1. Configuring the cross-compilation environment.
It has been configured on ubuntu before, so skip it directly.

2. Obtain the Linux kernel source code

Linux kernel source code link

  • Go to the link to select the appropriate version of the kernel source code, download it, and then transfer it to Ubuntu for decompression.
    Insert image description here

3. Configuration of Linux kernel source code

Reference article

			厂家配linux内核源码,比如说买了树莓派,树莓派linux内核源码
		第一种方式:
			cp 厂家.config .config
		第二种方式:
			make menuconfig 一项项配置,通常是基于厂家的config来配置
			
		第三种方式:
			完全自己来

I use the first method to configure according to the .config file provided by the manufacturer.

1) Kernel configuration:
First, enter the directory of the Linux kernel source code and
obtain the bcm2711_defconfig configuration into .config.

Raspberry Pi 4b is configured with bcm2711_defconfig
and Raspberry Pi 3b is configured with bcm2709_defconfig.

implement

ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make bcm2711_defconfig

Insert image description here

2) Driver configuration

  • Install necessary libraries:
sudo apt-get install bc
sudo apt-get install libncurses5-dev libncursesw5-dev
sudo apt-get install zlib1g:i386
sudo apt-get install libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5
  • The second way:
    make menuconfig configures item by item, usually based on the manufacturer's config to execute menuconfig.
 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make menuconfig

Then a block diagram like this will pop up

  • *Compiled into the kernel zImage includes the driver
  • Generate the driver file xxx.ko in M ​​module mode. After the system is started, load it through the command inmosd xxx.ko

Insert image description here
You can perform some related configurations of the kernel driver.
If there is nothing to change, you do not need to perform this step.

4. Compile and package the kernel source code
1) Compile

ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 zImage modules dtbs

Insert image description here
Then the compilation error occurs. The tool is not working. Install the latest tool directly.

sudo apt install libssl-dev

Then the compilation is successful
* Note: After the compilation is successful, you will see that there is vmlinux in the source tree directory. If it fails, there will be no such file.
After success, the target zImage image will be under arch/arm/boot
*
Insert image description here
Insert image description here

2) Package zImage into xxx.img reference article available for Raspberry Pi

./scripts/mkknlimg arch/arm/boot/zImage ./kernel_new.img

There is a bug here

Tip: ash: ./scripts/mkknlimg: No such file or directory
There is no packaging tool mkknlimg. I tried many methods but it didn’t work. Later I found that the packaging tool mkknlimg was eliminated.
Method 1: Re-download an old version of the source code Linux-rpi-4.14 .y.zip, unzip, find the packaging tool mkknlimg in the scripts folder, and copy the packaging tool to the scripts directory of the current source code.
Insert image description here

Method 2: Download the tool_master tool package and find imagetool-uncompressed.py

sudo ./imagetool-uncompressed.py 内核源码目录的绝对路径/arch/arm/boot/zImage

After successful packaging, go to the source code directory to find the file kernel_new.img
Insert image description here

5. Mount the Raspberry Pi SD card and install the compiled kernel source code to the SD card

1) First create two empty folders to facilitate mounting the sd card

mkdir data1 data2

Use dmesg to check the kernel print information. If sdb1 sdb2 appears, continue the following operations:
Mount the USB disk

sudo mount /dev/sdb1 data1   一个fat分区,是boot相关的内容,kernel的img
sudo mount /dev/sdb2 data2   一个是ext4分区,也就是系统的根目录分区。

The following uses [fat] to indicate the boot mounting path, and [ext4] to indicate the ext4 mounting path.

2) Install modules and device driver files

sudo ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make INSTALL_MOD_PATH=[ext4] modules_install

To operate an ext4 partition, root privileges are required.

3) Install and update the kernel.img file. Note that the image name is kernel7.img.
Back it up before updating to prevent the Raspberry Pi from failing to start after changing the kernel and unable to find the original kernel.

```bash
cd [fat]
cp kernel7.img kernel7old.img

The kernel_new.img file has been packaged with the mkknlimg tool before. Copy it to the boot partition and configure it for use.

cp kernel_new.img [fat]/

Then edit the [fat]/config.txt file and add a line at the end:

kernel=kernel_new.img

You can also copy the newly generated compilation to [fat]/ and name it kernel7.img

cp kernel_new.img [fat]/kernel7.img

In this case, there is no need to modify the [fat]/config.txt file.

4) Copy other related files

sudo cp arch/arm/boot/dts/*.dtb [fat]/
sudo cp arch/arm/boot/dts/overlays/*.dtb* [fat]/overlays/
sudo cp arch/arm/boot/dts/overlays/README [fat]/overlays/

6. Insert the SD card back into the Raspberry Pi and log in through the serial port to check whether the kernel output log of the Raspberry Pi is normal and whether you can log in normally.
Insert image description here

My kernel version was 5.15.32 before
and then changed to 5.15.0
Insert image description here

Guess you like

Origin blog.csdn.net/m0_68038554/article/details/132011324