In the previous section, we introduced two methods for driver entry into the kernel: module and direct compilation into the kernel, and introduced the module'sOne way to compile: In an independent folder, use makefile with the kernel source code path.
So how to directly compile the driver into the kernel?
What is kernel pruning that we often hear about during kernel porting configuration?
We often execute the make menuconfig command during Linux Kernel configuration, and the following interface will appear on the screen:
How is this interface generated?
What is the relationship between Kernel configuration and compilation that we often talk about?
The following describes the Linux Kernel configuration mechanism and compilation process.
1. Configure the basic structure of the system
The Linux Kernel configuration system consists of three parts:
1. makefile: it is distributed in the Linux kernel source code root directory and the directories at different layers, and defines the compiling rules of the Linux kernel;
2. configuration file (config. In (2.4 kernel, 2.6 kernel): provides users with the configuration selection function;
3. Configuration tool: includes the configuration command interpreter (explains the configuration commands used in the configuration script) and configure the user interface (the user configuration interface based on the Character interface, the ncurses-based GUI, And the xwindows-based GUI, corresponding to make config, make menuconfig, and make xconfig ).
These configuration tools are all written in scripting languages, such as Tcl/TK and Perl (including some code written in C ). This document does not analyze the Configuration System, but describes how to use the configuration system. Therefore, unless it is the maintainer of the Configuration System, General kernel developers do not need to understand their principles, just need to know how to write
Makefile and configuration file.
Ii. makefile menuconfig
What does the system do for us when we execute the make menuconfig command?
Here we will explain a total of several files.
Scripts folder in the root directory of Linux kernel
ARCH/$ ARCH/kconfig file, the kconfig file under each directory
Makefile files under the root directory of Linux kernel and makefile files under different directories
The. config file under the Linux kernel root directory and the config file under arm/$ ARCH/
Include/generated/Autoconf. h file under the root directory of Linux kernel
1) the scripts folder stores files related to drawing images on the make menuconfig configuration interface. as users, we do not need to care about the contents of this folder.
2) When we execute the make menuconfig command and the above blue configuration interface appears, the system helps us do the following:
First, the system will read the kconfig file in the arch/$ ARCH/directory to generate the entire configuration interface option (kconfig is the core of the Linux configuration mechanism). What is the value of the arch environment variable equal?
It is determined by the MAKEFILE file in the root directory of the Linux kernel. The makefile contains the definition of this environment variable:
You can also run the Make arch = arm menuconfig command to generate the configuration interface. The default Interface is that all parameters have no value.
For example, if the Academic Affairs Office takes an exam, the exam subjects may include foreign languages, Chinese languages, and mathematics. Here, we chose arm for the exam,The system will read the ARM/kconfig file to generate configuration options (select the arm section of the paper), the system also provides x86 section, milps Section and other more than 10 course questions
3) assume that the Office of Academic Affairs is "benevolent", and we have prepared a reference answer (default configuration options) for some students to make good questions ), stored in arch/$ ARCH/configs. For arm, the arch/ARM/configs folder is used:
There are many options in this folder. Which one will the system read? By default, the kernel reads data from the root directory of the Linux kernel. the config file is used as the default kernel option (answers to the questions ), we usually select a series that is the closest to our development board to the Linux kernel root directory based on the type of the Development Board (select the nearest reference answer)
# Cp ARCH/ARM/configs/s3c2410_defconfig. config
4). config
Assuming that the academic affairs office has been focused, his reference answers are not completely correct (. the config file does not exactly match our board. run the make menuconfig command to read the new options.
However, we generally do not adopt this scheme. In the configuration interface, we choose to use spaces, ESC, and press enter to select some options or do not select, and finally save and exit, the Linux Kernel updates the new options (correct answers). config. config is renamed to another file and saved (when you execute make distclean, the system will. delete the config file). We will not need to go to arch/ARM/configs to obtain the corresponding file when we configure the kernel later, saving the trouble of reconfiguration and saving it directly. copy the config file. config.
5) after the above two steps, we can correctly read and configure the interface we need.
So how do they establish a compilation relationship with makefile files?
When you save the make menuconfig option, the system will not only automatically update. config, but also save all options as Macros in
Include/generated/Autoconf. h file under the root directory of Linux kernel
The source code in the kernel will contain the above. h file and implement Conditional compilation with the macro definition.
When we need to select whether to compile a file as a whole, we also need to modify the corresponding MAKEFILE file, for example:
When we choose whether to compile the s3c2410_ts.c file, makefile will decide to compile this file according to config_touchscreen_s3c2410. This macro is defined in the kconfig file. After the configuration is complete, it will appear in. in config and autconf, we have completed the entire Linux kernel compilation process.
Finally, we will find that throughout the Linux Kernel configuration process, only the kconfig, makefile files, and corresponding source files of each layer are left for the user interface.
For example, if we want to add a function to the kernel and use make menuconfig to control its claim process
First, you need to modify the kconfig file in the corresponding directory and add corresponding options according to the kconfig syntax;
Execute make menuconfig to select whether to compile the file into the kernel or not, or compile the file into a module. The. config file and Autoconf. h file are automatically generated;
Finally, modify the MAKEFILE file in the corresponding directory to add the compilation option;
Finally, execute the make zimage command for compilation.
Iii. Specific instances
The following is an example of a previous module experiment. It explains how to use the make menuconfig mechanism to compile the previously compiled modules into the kernel or compile them into modules.
Suppose I already have such a driver:
Modules. c
# Include <Linux/module. h>/* module_init () */# include <Linux/kernel. h>/* printk () */# include <Linux/init. h>/* _ init _ exit */# define debug // open debug message # ifdef debug # define printk (FMT, Arg ...) printk (kern_warning FMT, ## Arg) # else # define printk (FMT, Arg ...) printk (kern_debug FMT, ## Arg) # endif/* module init & exit function */static int _ init mymodule_init (void) {/* module init Code */printk ("mymodule_init \ n"); Return 0;} static void _ exit mymodule_exit (void) {/* module exit code */printk ("mymodule_exit \ n"); return;} module_init (mymodule_init); module_exit (mymodule_exit); module_author ("dengwei "); /* module author, optional */module_license ("GPL");/* module license, which describes the permission of the kernel module, required */module_description ("a simple hello World module");/* module description, optional */
Step 1: Copy modules. C to the drivers/Char/directory (this folder generally stores common character drivers)
Step 2: VI driver/Char/kconfig, in
Add the following information after config devkmem
Config modules
Tristate "modules device support"
Default y
Help
Say y here, the modules will be built in kernel.
Say m here, the modules willbe build to modules.
Say n here, there will be nothing to be do.
Step 3: Make menuconfig
Device Driver-character Devices
[*] Modules device suppor
Step 4: VI driver/Char/makefile, after js-RTC, add
OBJ-$ (config_modules) + = modules. o
Config_modules must be consistent with the preceding kconfig. The system automatically adds the config _ prefix.
Modules. O must be the same as the. c file name you added.
Finally, execute: Make zimage modules to compile it into the kernel.
Step 3:
Step 3: Make menuconfig
Device Driver-character Devices
[M] modules device suppor
Change the asterisk to m in the configuration interface, and then execute make modules. A modules. Ko file is generated in the driver/Char/directory.
Similar to the effect of compiling a module separately described above, a module is generated and tested into the Development Board and run insmod moudles. Ko to insert the generated module into the kernel.