Raspberry Pi Advanced Course 3: Linux kernel source directory tree literacy analysis, configuration of Linux kernel suitable for Raspberry Pi related operations and kernel compilation, transplantation

Literacy Analysis of Linux Kernel Source Directory Tree

1. Linux kernel source directory tree literacy analysis:

sudo apt-get install tree
tree   //查看目录树

About 1.3w C files: 1100w lines of code
Linux is open source and free, and Linux open source community workers jointly maintain
Linux as an open source, support multi-architecture and multi-platform code, and have very high portability

But the Linux kernel is usually compiled into a few M ---- 4M

Because it supports multi-platform and multi-architecture, it is necessary to configure before compiling and configure it to a suitable target platform.
Platform: ARM (HiSilicon, Friendly Arm, Raspberry Pi, nanopi) X86 MIPS

Reference article:
Linux kernel source code directory tree structure

Understand the structure of the source directory tree

2. Configure Linux kernel suitable for Raspberry Pi related operations:

When we work, we need to write the driver code, and then compile the driver code, but the compilation of the driver code requires aPre-compiled kernel, Compile the kernelMust be configured

The final goal of the configuration will generate a .config file, which instructs the Makefile to organize useful things into the kernel.

The manufacturer is equipped with Linux kernel source code: For example, if you buy a Raspberry Pi, the manufacturer will give you the Raspberry Pi Linux kernel source
code. After you get the source code, it contains the manufacturer's source code

The first way:

cp 厂家.config .config
find . -name *_defconfig
找到:
./arch/arm/configs/bcm2709_defconfig

The second way:

make menuconfig   //一项项配置,通常基于厂家的config来配置

The third way:

Manufacturer's own family: do it all by yourself

How to configure the kernel of Linux Raspberry Pi?

method one:

ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make bcm2709_defconfig
//用来配置,指定编译器

The function of this command isGet 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.
Insert picture description here
Realize to change manufacturer.config into, config

Method 2:
Add a driver to
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

After installation:

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

Insert picture description here
Drive two loading methods:

*: Compile into the kernel ----- zImage contains the driver.
M: The driver file xxx.ko is generated in the module mode, after the system is started, through the command
inmosd xxx.ko load

**Method 3: **Ignore temporarily

3. Linux kernel compilation:

编译:

ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make -j4 zImage modules dtbs 2>&1 | tee build.log
//arm架构,内核7,j4:指定用多少电脑资源编译,
//zImage:生成内核镜像
//modules:生成驱动模块
//dtbs:生成配置文件

Compile in n process. If the number of processes is not specified, it will be compiled as a single process by default.
Insert picture description hereAfter the kernel is compiled successfully, you can see that there are more vmlinux in the source tree. If it fails, there is no such file.
After success, zImage is the target image we need

cd arch/arm
cd boot
ls

Insert picture description here

3. Copy the Linux kernel image to the Raspberry Pi and start the new kernel

Pack the zImage file:

Use the tools in the linux source code package directly:

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

Generate one in this directorykernel_new.imgFile, this file is the file to be placed in the SD card.
Note: Many places on the Internet say that using tools/mkimage/imagetool-uncompressd.py does not work! !

Insert picture description here
Copy the image:
1. Power off the Raspberry Pi, pull out the SD card, use a card reader to insert the computer and connect to the virtual machine.

dmesg    //显示可移动设备信息

Insert picture description here

cd  //回到根目录
mkdir data1
mkdir data2
//数据拷贝;创建两个文件夹,挂载U盘
sudo mount /dev/sdb1 data1//一个fat分区,是boot相关的内容,kernel的img文件就放在这个分区里;
sudo mount /dev/sdb2 data2//一个是ext4分区,也就是系统的根目录分区
//  sudo umount /dev/sdb1   //取消挂载
//data1:U盘数据   data2:根目录数据

Insert picture description here
data2:Insert picture description here

Install modules:

sudo ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7 make INSTALL_MOD_PATH=/home/dazai/data2 modules_install

Install device driver files: hdmi interface usb WiFi io, etc.;
Insert picture description here

Update the kernel.img file:

note: Mirror name: kernel7.img

cd data1
ls
//可以看到kernel7.img 文件
cp  kernel7.img  kernel7OLD.img//做一个备份

Insert picture description here
Then copy the newly generated compilation to data1 and name it:kernel7.img

cp kernel_new.img  /home/dazai/data1/kernel7.img

md5sum kernel_new.img//检查cp是否成功
md5sum /home/dazai/data1/kernel7.img

Insert picture description here
Insert picture description here
The copy was successful.

Copy other related files:

cp arch/arm/boot/dts/.*dtb* /home/dazai/data1/
cp arch/arm/boot/dts/overlays/.*dtb* /home/dazai/data1/overlays/
cp arch/arm/boot/dts/overlays/README /home/dazai/data1/overlays/

After that, Ubuntu disconnects the processing of the SD card and sends it to windows to capture.
Restore:
Insert picture description here
After: insert the SD card back into the Raspberry Pi and
log in through the serial port to watch the startup process.
Enter the Raspberry Pi terminal:

 uname -r

Insert picture description here
The kernel version has changed.
The configuration, compilation and transplantation of the Raspberry Pi kernel was successful.
These operations stem from:
The compilation of driver code requires a pre-compiled kernel

Reference article:
Raspberry Pi-Kernel Development-Instructions to download the code, compile and replace the kernel

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108730597