【Kernel driver】Kernel compilation

00. Table of Contents

01. Install compilation tools and dependencies

deng@local:~/A72/x3399_linux_new/kernel$ sudo apt install git  lib32z1

02. Kernel source code download

There are three ways to obtain the Kernel source code:

  • One is Kernel official kernel source code

  • One is the official NXP kernel source code

  • One is the kernel source code that we modified to adapt to our board.

We use the source code we provide as an example here. Friends who are interested in the official source code can also download it to learn configuration. Our kernel is customized based on the kernel officially provided by the manufacturer, and the manufacturer's kernel is adapted to the chip based on a certain official version of the kernel. As embedded developers, we generally only need to use the kernel adapted by the chip manufacturer for development. It is the responsibility of the chip manufacturer to download the new version from the official kernel to configure it. Interested partners can also follow the official version provided by NXP. The chip manual is adapted to the new version of the kernel.

Kernel official kernel source code: https://git.kernel.org/

NXP kernel source code: https://github.com/Freescale/linux-fslc

EBF kernel source code: https://github.com/Embedfire/ebf_linux_kernel.git

git clone https://github.com/Embedfire/ebf_linux_kernel.git

03. Kernel project structure analysis

To learn a software, especially open source software, you should first start by analyzing the engineering structure of the software. A good software has a good engineering structure, which is very helpful for readers to learn and understand the software's architecture and workflow.

The kernel source code directory is as follows

arch         COPYING        drivers   init     kernel       make_deb.sh  README    sound
block        CREDITS        firmware  ipc      lib          Makefile     samples   tools
build_image  crypto         fs        Kbuild   LICENSES     mm           scripts   usr
certs        Documentation  include   Kconfig  MAINTAINERS  net          security  virt

We can see that there are many directories in the Linux kernel source code directory, and there are also many files in the directories. Let's briefly analyze the main functions of common directories.

arch : Mainly contains code related to hardware architecture, such as arm, x86, MIPS, PPC. Each CPU platform occupies a corresponding directory. For example, the imx series CPU we use is in the directory. The Linux kernel currently supports 30 types arch/arm/mach-imx. left and right CPU architecture. The directory in arch stores the support for Linux kernel process scheduling, memory management, interrupts, etc. of each platform and chip on each platform, as well as the board-level support code for each specific SoC and circuit board.

block : In Linux, block represents a block device (accessed as a whole in units of blocks (a whole composed of multiple bytes, similar to sectors)). For example, SD cards, Nands, hard disks, etc. are all block devices, and the block directory is decentralized. It is some code about block device management in the Linux storage system.

crypto : This directory stores commonly used encryption and hashing algorithms (such as md5, AES, SHA, etc.), as well as some compression and CRC verification algorithms.

Documentation : Document description of each part of the kernel.

drivers : Device driver, which lists the driver source codes of all hardware devices supported by the Linux kernel. Each different driver occupies a subdirectory, such as char, block, net, mtd, i2c, etc.

fs : fs is file system, which contains various file systems supported by Linux, such as EXT, FAT, NTFS, JFFS2, etc.

include : The directory includes most of the header files needed to compile the core. For example, platform-independent header files are in the include/linuxsubdirectory, and header files related to the CPU architecture are in the corresponding subdirectory of the include directory.

init : Kernel initialization code. The code in this directory is the code that initializes the kernel when the Linux kernel starts.

ipc : ipc is inter process commuicationinter-process communication. This directory contains codes for Linux inter-process communication.

Kernel : The kernel is the Linux kernel, which is the core part of Linux, including process scheduling, timers, etc., and some code related to the platform is placed in the arch/ * /kernel directory .

lib : lib means library. The lib directory stores some common and useful library functions. Note that the library functions here are different from the C language library functions, because the C language standard library cannot be used in kernel programming. Function, so you need to use the library functions in lib. In addition, the library function codes related to the processor structure are placed arch/*/lib/in the directory.

mm : The directory contains all memory management code that is independent of the CPU architecture, such as page storage management memory allocation and release, etc., while the memory management code related to the specific hardware architecture is located in the directory, for arch/*/mmexample arch/arm/mm/fault.c.

net : Network protocol stack related code, various common network protocols are implemented in the net directory.

scripts : All script files in this directory are not used when the Linux kernel works, but are used to configure and compile the Linux kernel.

security : Code related to the kernel security model, such as the most famous SELINUX.

sound : The driver core code and common device drivers of ALSA and OSS audio equipment.

usr : implements cpio for packaging and compression, etc.

04. Kernel configuration options

The Linux kernel configuration system consists of three parts, namely:

  • Makefile: distributed in the Linux kernel source code root directory and directories at each level, defining the compilation rules of the Linux kernel;
  • Configuration file: Provides users with the function of configuration selection. For example, the Kconfig file defines configuration items. When compiling with the make_deb.sh script, use the arch/arm/configs/npi_v7_defconfigfile to assign values ​​to the configuration items;
  • Configuration tools: including configuration command interpreter (interprets the configuration commands used in the configuration script) and configuration user interface (Linux provides user configuration interface based on character interface, Ncurses graphical interface and Xwindows graphical interface, each corresponding to make config , make menuconfig and make xconfig).

make menuconfig is a configuration interface based on text selection. It is recommended to be used in a character terminal. You can use the keyboard's "up", "down", "left", "right", "enter", "space", "?" , "ESC" and other keys to select configuration.

#使用图形界面配置需要额外安装 libncurses-dev
sudo apt install libncurses-dev

Insert image description here

For example, we choose to configure the ds18b20 driver of our development board: ds18b20If the reader cannot find where this configuration option is, you can use make menuconfigthe search function in the English input method and press "/" to search, enter "ds18b20" Find the location where you changed the configuration option. When you make a mistake in input, you can use Ctrl+Backspace to delete the input.

When you can use y、n、mthe key to change the configuration of the ds18b20 driver, y means compiling into the kernel, m means compiling into a module, and n means not compiling. You can also use spaces to select configuration options for the ds18b20 driver.

05. Kernel compilation

deng@local:~/A72/x3399_linux_new$ ./mk.sh -k

06. Discussion

07. Appendix

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/132796263