How to compile drivers into linux kernel
The working principle of kernel compiling:
There are files need to change in the relevant directory - Kconfig and Makefile.
- All these configures and makefiles are based on the great program "make".
- When we enter "make menuconfig", the "make" program will build a visual configure interface according to Kconfig files existing in different directories.
- After finish configuring the system, "make" will produce a configure file ".config" which contains the information of your configure options.
- Then compile the system using "make", gcc will compile the system according to the Makefiles which will choose to compile drivers as kernel, or module, or not according to the configure file ".config".
The format of Kconfig file:
menu "Name of menu"
config SYMBOL1
tristate (or bool) "The function of this option"
default M (or Y)
---help---
Here we can illustrate what is the configure option used for
config SYMBOL2
tristate (or bool) "The function of this option"
default M (or Y)
---help---
Here we can illustrate what is the configure option used for
config SYMBOL3
tristate (or bool) "The function of this option"
default M (or Y)
---help---
Here we can illustrate what is the configure option used for
...
endmenu
"config SYMBOL" here config is a keyword which illustrates the following symbol is a configure option, and symbol is used to identity the configure which will be changed into CONFIG_SYMBOL and be valued as Y, M, or empty in .config file. Then further, Makefile can use these CONFIG_SYMBOLs to choose to compile drivers as kernel, module or not compile.
There is another useful keyword of Kconfig - source.
This keyword is used in the parent directory which usually is used to enter the Kconfig file of subdirectory.
Its usage is shown below:
menu "Name of the menu"
source "currentdirectoty/subdirectory1/Kconfig"
source "currentdirectoty/subdirectory2/Kconfig"
source "currentdirectoty/subdirectory3/Kconfig"
source "currentdirectoty/subdirectory4/Kconfig"
source "currentdirectoty/subdirectory5/Kconfig"
...
endmenu
The root of Kconfig structure:
Kconfig files make a configure tree of kernel. So there must be a root which can be used as the entry of kernel configure system. You must modify the root Kconfig file to add driver or modify driver, otherwise it cannot display when you do "make menuconfig". So the root Kconfig file is : /arch/xxx(processor architecture)/Kconfig. This is the entry (main menu) of the whole configure interface of kernel.
How to write Makefile according to Kconfig:
After writing Kconfig file, we need to write Makefile to response to Kconfig.
We already knew that the Makefile need to use the configure file .config to compile the source code.
So we need to use the SYMBOLs we talked above to choose to compile drivers as kernel (Y), or module (M) or not compile (empty).
It is written as following:
obj - $(CONFIG_SYMBOL1) (tab tab ...) += xxxx1.o
obj - $(CONFIG_SYMBOL2) (tab tab ...) += xxxx2.o
obj - $(CONFIG_SYMBOL3) (tab tab ...) += xxxx3.o
After change the Makefile in the current directory, you have to modify the Makefile in the parent directory to add the path of your new drivers, otherwise when compiling the kernel, it cannot find your new driver. This sentence should be like following:
obj - $(CONFIG_SYMBOL1) (tab tab ...) += mydrivers/
obj - $(CONFIG_SYMBOL2) (tab tab ...) += mydrivers/
obj - $(CONFIG_SYMBOL3) (tab tab ...) += mydrivers/
References:
[1] Linux 内核源码树学习: Kconfig 和 Makefile http://www.linuxidc.com/Linux/2009-11/23091.htm
[2] Linux source code.
[3] Linux设备驱动开发详解, P61.
Thanks for the post. It was quite interesting to read.
ReplyDeleteLinux course in Pune
very informative blog about java, thanks for this amazing post. linux classes in pune
ReplyDelete