This article was reproduced from: http://blog.csdn.net/fengyuwuzu0519/article/details/73772109
To understand the structure of the kernel, let's implement the following simple example.
First, increase the kernel boot Hello World
Task:
The kernel starts by loading the Hello driver and prints out Hello World
Steps:
(1) Create a new Hello folder in the drivers directory and implement the corresponding hello.c, Makefile, Kconfig
(2) Modify the makefile, Kconfig of the previous level (Linux-3.4.2/drivers)
(3) Make Menuconfig configuration
(4) Compile, burn write, run
Realize:
(1) Implement driver hello.c
The simplest driver
[CPP]View PlainCopy print?
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- static int first_drv_init (void)
- {
- PRINTK ("------------------Hello World!--------------------");
- return 0;
- }
- static void First_drv_exit (void)
- {
- PRINTK ("------------------exit Hello World!--------------------");
- }
- Module_init (First_drv_init);
- Module_exit (First_drv_exit);
- Module_license ("GPL");
(2) Realizing Hello/makefile
[CPP]View PlainCopyprint?
- # LED Core
- obj-$ (Config_hello) + = hello.o
Note that the Config_hello here is defined in. config, and if the configuration is compiled into the kernel, then the config_hello=y is present in the. config and the hello.o is compiled into the kernel.
(3) Realizing Hello/kconfig
[CPP]View PlainCopyprint?
- Config HELLO
- TriState "Hello World for Fengyuwuzu"
- Help
- Hello for Fengyuwuzu
Config Hello determines the name of Config_hello.
Hello World for Fengyuwuzu: determines the GUI name at make Menuconfig
(4) Modify Drivers/makefile, add:
[CPP]View PlainCopyprint?
- Obj-y + = myled/
(5) Modify Drivers/kconfig, add:
[CPP]View PlainCopyprint?
- SOURCE "Drivers/myled/kconfig"
(6) Make menuconfig configure the appropriate entry into the kernel
(7) Make Uimage
(8) Burn write new kernel boot
See "------------------Hello World!--------------------" is printed, and the corresponding driver is loaded.
Ii. relationship of Makefile, Kconfig,. config in the Linux kernel
(1) The role of the three
Simply put: Kconfig is the menu, makefile is the practice,. config is the dish you ordered.
Makefile: A text-form file that compiles a method of the source file.
Kconfig: A text form of the file, the kernel configuration menu.
. config: The configuration on which the compilation is based.
(2) The grammar of the three
1, Makefile
Goal definition: The target definition is used to define what is to be compiled as a module and which to compile the link into the kernel.
Direct compilation:
Obj-y + = hello.o
Indicates to be compiled by hello.c or Hello.s file hello.o and linked into the kernel
A more common practice is to determine how files are compiled based on the config_ variable of the. config file:
Conditional compilation:
obj-$ (Config_hello) + = hello.o
Obj-m indicates that the file is to be compiled as a module.
Objects other than y,m are not compiled in obj-x form.
2, Kconfig
Type definition:
Each config menu item must have a type definition: bool Boolean type, TriState tri-State (built-in, module, remove), string string, hex 16 binary, Integer integer.
For example:
Config Hello_module
BOOL "Hello Test module"
The bool type can only be selected or unchecked and displayed as [];
TriState types of menu items are compiled into kernel module options, shown as < >, if you choose to compile into a kernel module, you will generate a config_hello_module=m configuration in. CONFIG, if the choice of built-in, is directly compiled into the kernel effect, A config_hello_module=y configuration is generated in. CONFIG. Hex hexadecimal type is displayed as ().
Directory hierarchy iterations
There is a similar statement in Kconfig: source "Drivers/usb/kconfig"
Used to include (or nest) new kconfig files so that each directory can manage its own configuration so that it is not necessary to write those configurations in the same file for easy modification and management.
To configure dependencies between options:
Depend on: An option relies on another option to generate
Select: Reverse dependency, when selected, selects the item defined after the Select
Requie
Default value: Default (Defaults y/n/m equivalent)
Input hint: prompt
Helpful Information: Help
3,. config
The kernel compiles the reference file.
How to modify:
(1) Make Menuconfig
(2) Make Xxx_defconfig
(3) Direct modification
! Note If you modify directly, it does not necessarily take effect, because some configurations may have dependencies, make will be based on dependencies, the rules are checked, not recommended directly in the. config to modify.
"Summarize" the relationship of Makefile, Kconfig,. config in the embedded Linux kernel and add boot Hello World "turn"