Raspberry Pi kernel configuration

Original address: http://nicekwell.net/blog/20171108/shu-mei-pai-nei-he-kai-fa-shuo-ming-xia-zai-dai-ma-bian-yi-ti-huan-nei -he.html

Raspberry Pi runs Linux system, the kernel code is open source, we can modify the kernel code and write the driver by ourselves.

This article describes how to obtain the linux kernel code, complete the compilation and kernel replacement.

1. Overview
Raspberry Pi's github homepage: https://github.com/raspberrypi, which contains linux source code, cross-compilation tool chain, etc.

There are two warehouses for what we are going to use:

https://github.com/raspberrypi/linux kernel source code

https://github.com/raspberrypi/tools Cross compilation tool chain (only used in cross compilation)

Note:
1. The system image version installed in the Raspberry Pi must correspond to the kernel code. Because the Raspberry Pi system is constantly being developed and upgraded, if your Raspberry Pi uses a system image at a certain time, it is best to use the kernel code at that time.
2. About the kernel compilation method, the official website has a very detailed introduction: https://www.raspberrypi.org/documentation/linux/kernel/building.md, here is a translation and supplement.
3. The following compilation process is tested on Raspberry Pi 1 and Raspberry Pi 3B.

2. Cross compiling in ubuntu
1. Obtain cross compiling tools and source code

Source code: git clone [email protected]:raspberrypi/linux

Cross compilation tool: git clone [email protected]:raspberrypi/tools

2. Configure compilation environment variables

2.1 Manually configure environment variables

After downloading the compilation tool, compile the bin file of the compilation tool we need on 64-bit ubuntu in: tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin directory, add this directory to the environment In the variable PATH, add the method:

PATH=$PATH:/home/nicek/githubProjects/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin

If you are compiling in a 32-bit system, you must choose a 32-bit cross-compiler tool.

After the configuration is complete, you can use the compiler tool command to check the version number:
arm-linux-gnueabihf-gcc -v

After that, all the make commands must specify some environment variables:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7

ARCH=arm indicates that arm is currently to be compiled. Although the Raspberry Pi is 64-bit, arm is still selected here instead of arm64.
CROSS_COMPILE indicates the name of the cross tool chain.
KERNEL indicates the type of kernel. Raspberry Pi 1 is set to kernel, and Raspberry Pi 2 and 3 are set to kernel7.
Every time make needs to specify these environment variables, such as:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make menuconfig
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 zImage

2.2 Automatic configuration of environment variables

It is very troublesome to write the above environment variables every command, you can set it once through export:
export PATH=$PATH:/home/nicek/githubProjects/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf- raspbian-x64/bin ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7
All commands executed in this terminal after kernel7 have these environment variable information.

This export command can be written as a script, and then source this script in the terminal before compiling to set all environment variables. Just source envsetup.sh before compiling Android.

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export PATH="$PATH:$DIR/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/"
export ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7

The path in the above command may be different from your actual one, please pay attention to modification.

After exporting environment variables, the subsequent commands in this terminal can no longer specify these environment variables, such as:
command before configuration: ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make menuconfig
command after configuration: make menuconfig

3. Configure config

There are many projects in the linux source code: the project for
Raspberry Pi 1 is bcmrpi_defconfig; the project for
Raspberry Pi 2 and 3 is bcm2709_defconfig.

3.1 Use the config that comes with the source code

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

The function of this command is to get the configuration of bcm2709_defconfig into .config.
We can directly use the configuration in the project, but in this case, the configuration of the original Raspberry Pi may be lost. Here is a method to obtain the configuration of the Raspberry Pi currently in use.

3.2 Get the current Raspberry Pi config

The Raspberry Pi that has been powered on will have this node: /proc/config.gz, from which the config of this Raspberry Pi can be obtained.
If there is no such node, you need to load the module first: sudo modprobe configs

Copy the content of config.gz to the computer to be compiled:
scp pi@[ip]:/proc/config.gz.

Unzip and save it as a .confg file.
zcat config.gz> .config
Note: It must be decompressed in linux environment, it will be garbled under mac.

Copy this config file to the root directory of the linux source code.

4. Compile

安装必要的库:
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

1. Execute menuconfig
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make menuconfig.
If there is no change, there is no need to perform this step.

2. Compile
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 zImage modules dtbs 2>&1 | tee build.log
to compile with n process. If the number of processes is not specified, it will be compiled as a single process by default.

3. Pack the zImage file
Directly use the tools in the linux source package:
./scripts/mkknlimg arch/arm/boot/zImage ./kernel_new.img
to generate a kernel_new.img file in this directory, this file is to be placed in the sd card document.
Note: Many places on the Internet say that using tools/mkimage/imagetool-uncompressd.py does not work! !

5. Mount the Raspberry Pi SD card, and install the compiled DIRECTLY to the SD card

Insert the sd card of the Raspberry Pi into the ubuntu system computer. The sd card of the Raspberry Pi has two partitions:
a fat partition, which is related to the boot content, and the kernel img file is placed in this partition; the
other is an ext4 partition. It is the root directory partition of the system.

The file we generated involves the contents of these two partitions. Generally, it will be automatically mounted after being inserted into ubuntu. The fat partition can be operated without root privileges, and the ext4 partition requires root privilege operations.

You can decide where to mount the two partitions. In the following, [fat] is used to indicate the path where boot is mounted, and [ext4] is the path where ext4 is mounted.

1. Install modules
sudo ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make INSTALL_MOD_PATH=[ext4] modules_install to
operate ext4 partition, requires root privileges.

2. Update
the kernel.img file. 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]/
edit [fat]/config.txt file, Add a line at the end:
kernel=kernel_new.img

3、复制其他相关文件
cp arch/arm/boot/dts/.dtb [fat]/
cp arch/arm/boot/dts/overlays/.dtb* [fat]/overlays/
cp arch/arm/boot/dts/overlays/README [fat]/overlays/

After the update is complete, plug in the Raspberry Pi to boot. After booting, you can use the uname -a command to check that the kernel information has changed.

3. Local compilation on Raspberry Pi The
principle of local compilation on Raspberry Pi is basically the same as the above cross-compilation principle. Because it is local compilation, the configuration of compilation tools and environment variables is simpler.
It took nearly 2 hours to compile a kernel on the Raspberry Pi.

1. Get the source code

git clone [email protected]:raspberrypi/linux

2. Configure the compilation environment

The environment variables that need to be configured when cross-compiling in ubuntu are:

PATH: Add the directory of the cross tool chain
ARCH: Configure it as arm
CROSS_COMPILE: Configure it as the cross tool chain used on ubuntu arm-linux-gnueabihf- KERNEL=kernel7
KERNEL: Configure it as kernel7
and compile locally on the Raspberry Pi:
About cross tool chain , Your own compilation tool can be compiled for your own use, so there is no need to configure;
just configure KERNEL=kernel7.

Same as above, you can use export KERNEL=kernel7. After one setting, all commands in this terminal have this environment variable.
It can be further written as a script, but this line of command is very simple, so you don’t need to write a script.

3. Configure config

As above,
Raspberry Pi 1 uses bcmrpi_defconfig, and
Raspberry Pi 2 and 3 use bcm2709_defconfig.
Example: KERNEL=kernel7 make bcm2709_defconfig

If you want to use the config that comes with the Raspberry Pi:
sudo modprobe configs # Load the module
zcat config.gz> .config # Get the configuration

4. Compile

Install the necessary libraries:
sudo apt-get install bc
sudo apt-get install libncurses5-dev libncursesw5-dev
sudo apt-get install zlib1g
sudo apt-get install libc6

1. Execute menuconfig
KERNEL=kernel7 make menuconfig. If there is
nothing to change, you don't need to execute this step.

2. Compile
KERNEL=kernel7 make -j4 zImage modules dtbs 2>&1 | tee build.log
to compile with n process. If the number of processes is not specified, it will be compiled as a single process by default.

3. Pack the zImage file
Directly use the tools in the linux source package:
./scripts/mkknlimg arch/arm/boot/zImage ./kernel_new.img
to generate a kernel_new.img file in this directory, this file is to be placed in the sd card document.

5. Update the system

1. Install the module
sudo make modules_install

2、复制dtb文件
sudo cp arch/arm/boot/dts/.dtb /boot/ sudo cp arch/arm/boot/dts/overlays/.dtb* /boot/overlays/ sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/

3. Update the kernel.img file
sudo cp arch/arm/boot/zImage /boot/$KERNEL.img

Guess you like

Origin blog.csdn.net/hhltaishuai/article/details/108007979