table of Contents:
The writing of the driver code requires a pre-compiled kernel. To compile the kernel, the kernel must be configured. The final goal of the configuration is to generate a .config file, which instructs the Makefile to organize useful things into the kernel.
1. Obtain the source code needed for compilation:
Click to enter the official website .
Required files:
linux: kernel source code, you can choose the version under branches.
tools: tools needed to compile the kernel and other source code-cross compiler, etc.
linux:
tools:
2. Configure environment variables:
(1) Manually configure environment variables:
①Get the value of current environment variables: echo $PATH
②Get the path of the cross-compilation tool chain: pwd
③Manually configure environment variables:export PATH=
echo $PATH+pwd
tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin // 将该文件配置入环境变量
arm-linux-gnueabihf-gcc -> arm-linux-gnueabihf-gcc-4.8.3 // 使用该交叉编译工具
(2) Automatic configuration of environment variables:
①Enter the working directory: cd
②Enter the hidden .bashrc file in the working directory: vi .bashrc
③Modify the content of the hidden .bashrc file, and add the corresponding content to the last line of the file:export PATH=
echo $PATH+pwd
④Save the file
3. Configure config:
There are many projects in the linux source code:
- The project of Raspberry Pi 1 is
bcmrpi_defconfig
- The project of Raspberry Pi 2 and 3 is
bcm2709_defconfig
(1) Use the config that comes with the source code:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make bcm2709_defconfig
// 获取 bcm2709_defconfig 的配置到 .config 里,此命令在 linux-rpi-4.14.y 目录下执行
Explanation:
ARCH=arm
: Specify ARM architectureCROSS_COMPILE=arm-linux-gnueabihf-
: Specify the compilerKERNEL=kernel7
:raspberry piemake bcm2709_defconfig
: The main core instructions
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.
(2) Get the config of the current Raspberry Pi:
①There will be this node on /proc/config.gz
the Raspberry Pi that has been powered on:, from this node, you can get the config of this Raspberry Pi.
If there is no such node, you need to load the module first:sudo modprobe configs
②Copy the config.gz
content to the computer to be compiled
③Unzip and save as a .config file:zcat config.gz > .config
! ! Note: It must be decompressed in the linux environment, otherwise it will be garbled
④Copy this config file to the root directory of the linux source code
4. Compile:
(1) Install the necessary libraries:
! ! Install the 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
(2) Execute menuconfig:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make menuconfig
(3) Compile:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 zImage modules dtbs
// 此过程持续时间较长
Explanation:
-j4
: Specify how many computer resources to call for compilationzImage
: Generate a kernel imagemodules
: Generate drive moduledtbs
: Generate configuration file
! ! There are more vmlinux
files in the kernel source tree directory , indicating that the compilation is successful and the target zImage
image is in the arch/arm/boot
path
(4) Pack the zImage file:
Use the tools in the linux source package directly:
./scripts/mkknlimg arch/arm/boot/zImage ./kernel_new.img
Generate a kernel_new.img
file in this directory , this file is the file to be placed in the SD card
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 virtual machine of the ubuntu system. The sd card of the Raspberry Pi has two partitions:
one is the fat partition, which is related to boot, and the kernel img file is placed in this partition; the
other is The ext4 partition is the root partition of the system.
(1) Create two folders:
mkdir data1
mkdir data2
(2) Mount:
sudo mount /dev/sdb1 data1
sudo mount /dev/sdb2 data2
/dev/sdb1
(Fat partition) mount to data1
/dev/sdb2
(ext4 partition) mount to data2
(3) Install modules
(device driver file) to data2 (ext4 partition ):
This operation must be done in the kernel source linux-rpi-4.14.y
path
sudo ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make INSTALL_MOD_PATH=/home/xxx/data2 modules_install
! ! Note the path of data2 (ext4 partition)
(4) Update the kernel.img file:
! ! Note: The name of the image is kernel7.img
, it is recommended to backup and then update
①Backup kernel7.img
(just back up in the original directory, just give the name) (this file is in the fat partition)
② kernel_new.img
Copy the newly generated image cp
todata1 (fat partition)The kernel7.img
file
③Check the file code to check whether the copy is complete:
md5sum kernel_new.img // 查看 kernel_new.img 文件编码
md5sum kernel7.img // 查看 kernel7.img 文件编码
④ Copy the configuration file (arch
The file is in the kernel source linux-rpi-4.14.y
path):
cp arch/arm/boot/dts/.*dtb* /home/xxx/data1
cp arch/arm/boot/dts/overlays/.*dtb* /home/xxx/data1/overlays
cp arch/arm/boot/dts/overlays/README /home/xxx/data1/overlays
! ! Pay attention to the path
After the update is complete, insert the SD card back into the Raspberry Pi to boot. After booting, you can use the uname -a
command to view the kernel information has changed