Linux-driven infrastructure development

Source: Internet
Author: User

Linux kernel configuration mechanism (make menuconfig, Kconfig, makefile)

before we introduced the module programming, introduced the drive into the kernel in two ways: module and directly compiled into the kernel, and introduced a way to compile the module-in a separate folder through the makefile with the kernel source path completion

So how do you compile the driver directly into the kernel?

What is the kernel clipping that is often heard during the porting of our actual kernel?

We often execute the make Menuconfig command when we are doing the Linux kernel configuration, and the following interface appears on the screen:

How is this interface generated?

What does the kernel configuration and the compilation have to do with what we often say?

Let's take a look at the Linux kernel configuration mechanism and its compilation process.

First, the configuration of the basic structure of the system

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

1, Makefile: Distributed in the Linux kernel source code root directory and each layer directory, define the Linux kernel compilation rules;

2. Configuration file (config.in (2.4 kernel, 2.6 kernel)): Provide the user with the function of configuration selection;

3, configuration tools: including configuration command interpreter (interpretation of configuration commands used in configuration scripts) and configuration user interface (provide a character-based interface, based on the ncurses graphical interface and Xwindows graphical interface based on the user Configuration interface, each corresponding to make config, make Menuconfig and make Xconfig).

These configuration tools are written using scripting languages, such as TCL/TK, Perl (and some code written in C). This article does not analyze the configuration system itself, but rather describes how to use the configuration system. So, unless the maintainer of the configuration system, the general kernel developers do not need to understand their principles, just need to know how to write Makefile and configuration files can be.

Second, makefile Menuconfig process explanation

What exactly did the system do for us when we executed the make Menuconfig command?

There's a couple of papers involved in this, and we're here to explain one by one.

Scripts folder under the Linux kernel root directory

arch/$ARCH/kconfig files, kconfig files in each layer directory

Makefile files in the root directory of the Linux kernel, makefile files under each layer directory

The. config file, arm/$ARCH/config file under the root directory of the Linux kernel

Include/generated/autoconf.h files in the root directory of the Linux kernel

1) Scripts folder is a file that is related to the graphical drawing of Make Menuconfig configuration interface, we as the user need not care about the contents of this folder

2) When we execute the make Menuconfig command to appear above the blue Configuration interface, the system has done the following work for us:

First the system reads the Kconfig file from the arch/$ARCH/directory to generate the entire Configuration Interface option (Kconfig is the core of the entire Linux configuration mechanism), so what is the value of the ARCH environment variable?

It is determined by the makefile file in the root directory of the Linux kernel and is defined under makefile with this environment variable:

or by the Make Arch=arm menuconfig command to generate the configuration interface, the default generated interface is that all parameters are not values

For example, the academic office to carry out examinations, the number of examinations may have foreign languages, languages, mathematics and other subjects, here is equivalent to our choice of arm branch can take the exam, The system will read the Arm/arm/kconfig file generation configuration options (selected arm section of the paper), the system also provides the x86 section, Milps section, such as 10 of subjects, such as exam questions

3) Assuming that the academic department compared "kindness", in order to fear some students do good test, we also prepared a reference answer (default configuration options), stored in arch/$ARCH/configs, for ARM section is Arch/arm/configs folder:

There are many options in this folder, which will be read by the system? The kernel defaults to reading the Linux kernel root directory. config file as the default option for the kernel (the reference answer to the question), we will generally choose from the Development Board type of the closest to our Development Board series to the Linux kernel root (select a closest reference answer)

#cp arch/arm/configs/s3c2410_defconfig. config

4). config

Assuming that the academic office has left simpleton, the reference answer he provided is not entirely correct (the. config file does not exactly match our board), then we can choose to modify the. config file directly and then execute the make Menuconfig command to read the new options

But generally we do not take this approach, we choose to configure the interface through the space, ESC, enter select some options selected or unchecked, and finally save the exit, the Linux kernel will put the new option (the correct parameter?? Answer) to the. config, we can rename the. config to another file (the system will delete the. config file when you execute make Distclean), and we will not need to go back to arch/arm/when we configure the kernel later. Configs to obtain the corresponding files, eliminating the hassle of reconfiguration, directly copy the saved. config file to. config.

5) After the above two steps, we can correctly read and configure the interface we need

So how do they build a compilation relationship with makefile files?

When you save the Make Menuconfig option, the system will also save all options in the form of a macro, in addition to automatically updating. config.

Under the Linux kernel root directory under the Include/generated/autoconf.h file

The source code in the kernel will contain the above. h file, with the definition of a macro conditional compilation.

When we need to select a file as a whole, such as whether or not to compile, we also need to modify the corresponding makefile file, for example:

We choose whether to compile s3c2410_ts.c this file, makefile will be based on config_touchscreen_s3c2410 to decide is to compile this file, this macro is defined in the Kconfig file, when we configure the completion, will appear in. config and autconf, so we have completed the entire Linux kernel compilation process.

Finally, we will find that throughout the Linux kernel configuration process, the interface left to the user is actually only the layers of kconfig, makefile files and corresponding source files.

For example, if we want to add a function to the kernel and control its claim process through make Menuconfig

The first thing to do is: modify the corresponding directory of the Kconfig file, according to the Kconfig syntax to add the corresponding options;

Next execute make Menuconfig choose to compile into the kernel or not compile into the kernel, or compile into a module, the. config file and the autoconf.h file will be generated automatically;

Finally modify the corresponding directory of the makefile file to complete the compilation options to add;

Finally the final execute make zimage command is compiled.

Third, specific examples

Let's take the previous module experiment as an example and explain how to compile the previously compiled modules into the kernel or compile them into modules with the make menuconfig mechanism.

Suppose I already have such a drive:

Modules.c

  1. #include <linux/module.h>/*module_init () */
  2. #include <linux/kernel.h>/* PRINTK () */
  3. #include <linux/init.h>/* __init __exit * /
  4. #define DEBUG//open Debug Message
  5. #ifdef DEBUG
  6. #define PRINTK (FMT, arg ...) PRINTK (kern_warning fmt, # #arg)
  7. #else
  8. #define PRINTK (FMT, arg ...) PRINTK (Kern_debug fmt, # #arg)
  9. #endif
  10. / * Module Init & Exit function * /
  11. Static int __init mymodule_init (void)
  12. {
  13. / * Module init code * /
  14. PRINTK ("mymodule_init\n");
  15. return 0;
  16. }
  17. Static void __exit mymodule_exit (void)
  18. {
  19. / * Module exit code * /
  20. PRINTK ("mymodule_exit\n");
  21. return;
  22. }
  23. Module_init (Mymodule_init);
  24. Module_exit (Mymodule_exit);
  25. Module_author ("Dengwei"); / * Module author, optional * /
  26. Module_license ("GPL"); / * Module License Certificate, which describes the license permissions of the kernel module, must * /
  27. Module_description ("A simple Hello World Module"); / * Module description, Optional * /

Step1: Copy the MODULES.C to the drivers/char/directory (this folder typically stores common character drivers)   Step2:vi Driver/char/kconfig, in config Devkmem after adding the following information   Config MODULES
TriState "Modules Device Support"
Default Y
Help
Say Y here,the modules'll be build in kernel.
Say M here,the Modules willbe build to modules.
Say N Here,there would be is nothing to being do.
  Step3:make Menuconfig Device driver-character devices [*]modules device Suppor   Step4:vi driver/char/makefile, add after JS-RTC obj-$ (config_modules) + = MODULES.O

Config_modules must be consistent with the above kconfig, the system will automatically add the Config_ prefix

MODULES.O must be with you. c File name is consistent

Final execution: Make zimage modules will be compiled into the kernel

Step Three:

Step3:make Menuconfig Device driver-character devices [m]modules device Suppor

The asterisk is changed to M in the configuration interface, and finally the make modules is executed, and a Modules.ko file is generated in the driver/char/directory.

Just like the individual compilation module effect we were talking about, a module is generated, which is taken into the Development Board to perform Insmod Moudles.ko, and the resulting module is inserted into the kernel for use.

Linux-driven infrastructure development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.