Today is a memorable day, and I don't know how long it took me to compile successfully! In the process of writing this driver, I really realized that driver programming is not that simple ~, I'm a little excited now. It's really not easy ~ I will repeat the entire process! Many documents on the Internet have been referenced in the process of writing this driver. The final result is that my head is dizzy and everyone writes differently, in fact, I still have some concepts that are not very clear. 1: What is the kernel source code tree? 2: Why do we need to compile the kernel source code tree? 1: kernel source code
Today is a memorable day, and I don't know how long it took me to compile successfully! In the process of writing this driver, I really realized that driver programming is not that simple ~, I'm a little excited now. It's really not easy ~ I will repeat the entire process!
Many documents on the Internet have been referenced in the process of writing this driver. The final result is that my head is dizzy and everyone writes differently, in fact, I still have some concepts that are not very clear.
1: What is the kernel source code tree?
2: Why do we need to compile the kernel source code tree?
1: My current understanding of the kernel source code tree is the entire Linux kernel source code, which is the prerequisite for compiling the driver. Ubuntu does not exist by default,
The kernel source code tree must be downloaded by yourself.
2: The driver is finally generated in the form of *. ko. The essence of insmod is to link the ko file with the running kernel. Similar to the link process for compiling helloworld.
The link must be compiled first to determine whether the required external symbol (EXPORT_SYMBOLS) exists, because some symbols (functions or global variables) are
In the kernel. If these symbols are used in the driver, a location must be reserved, and the specific location of these symbols (symbol binding) must be determined in insmod ).
If the kernel has not been compiled, how do you know whether these symbols are compiled into the kernel?
The whole process of Hello World Driver implementation
1: First, check the kernel version used by your system.
Www.linuxidc.com @ linux :~ $ Uname-r
3.2.0-34-generic-pae // I use ubuntu12.04, which is relatively high.
If the system automatically installs the source code. There will be a corresponding version under the/usr/src directory. I didn't have that version, and I downloaded it myself.
Www.linuxidc.com @ linux:/usr/src $ ls
Linux-headers-3.2.0-34 linux-source-3.2.0 (I downloaded)
Linux-headers-3.2.0-34-generic-pae
If there is no source code, check the source code package that can be downloaded. (do not use this command by a Super User. He will prompt that this command is unavailable)
# Apt-cache search linux-source
Www.linuxidc.com @ linux:/usr/src $ apt-cache search linux-source
Linux-source-Linux kernel source with Ubuntu patches
Linux-source-3.2.0-Linux kernel source for version 3.2.0 with Ubuntu patches
2: then download linux-source-3.2.0
# Sudo apt-get install linux-source-3.2.0
After the download is complete, the file name will be linux-source-3.2.0.tar.bz2 under/usr/src. after decompression, the entire source code will be obtained.
# Sudo tar jxvf linux-source-3.2.0.tar.bz2
After decompression, a new directory/usr/src/linux-source-3.2.0 will be generated, and now all the source code is inside
3: Start to configure the kernel now. There are three options: make oldconfig 2: make menuconfig 3: make xconfig
I chose the fastest way to configure the original configuration.
# Sudo make oldconfig
(If there is a problem during the configuration process, because you have not downloaded the dependent library file of the configuration environment, you can download it by yourself)
Compile the kernel after the configuration is complete.
4: Compile the kernel
# Sudo make
This process is very long. We suggest you do other things ~, It took me more than an hour to get back from dinner.
# Sudo make zImage
Finally, there are several errors in compiling the kernel, but there is no impact on the Hello World.
After the execution is completed, a new file is generated under the current directory: vmlinux
5: Then the compilation Module
# Sudo make modules
6. Installation Module
# Sudo make modules_install
After the execution is complete, a new directory/lib/modules/3.2.31/will be generated under/lib/modules/
The build directory under this path is required for subsequent compilation of module files. No, the kernel is compiled.
Now, the Hello World Program, which has been running for a long time, can be used.
My Hello World is under/hoem/xiongyao/
// Hello. c
# Include // Initial Function Change
# Include // Kernel header file
# Include // Module header file
MODULE_LICENSE ("Xiongyao BSD/GPL ");
Static int hello_init (void)
{
Printk (KERN_ALTER "Hello, world \ n"); // The module runs in the kernel state. You cannot use the printf function in the user State C library function. Instead, you must use the printk function.
// Print debugging information
Return 0;
}
Static int hello_exit (void)
{
Printk ("KERN_ALTER" Goodbye, Hello world \ n ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Makefile
Obj-m: = hello. o // generate the target file
KERNELDIR: =/lib/modules/3.2.31/build
PWD: = $ (shell pwd)
Modules:
(Here we need to use a tab key) $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Modules
(Use a tab key here) $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install
The best key step is to go to the directory where makefile is ready.
Www.linuxidc.com @ linux :~ $ Sudo make
Make-C/lib/modules/3.2.31/build M =/home/xiongyao
Make [1]: In directory '/usr/src/linux-source-3.2.0/linux-source-3.2.0'
LD/home/xiongyao/built-in.o
CC [M]/home/xiongyao/hello. o
Building modules, stage 2.
MODPOST 1 modules
CC/home/xiongyao/hello. mod. o
LD [M]/home/xiongyao/hello. ko
Make [1]: Leaving directory '/usr/src/linux-source-3.2.0/linux-source-3.2.0'
The above proof is successful.
Then load the module
# Sudo insmod./hello. ko
It should have displayed hello and world on the terminal, but nothing is displayed on the terminal (which will be solved later)
View the load Module
# Sudo lsmod
The hello
Hello 2560 0
^ _ ^, Already loaded
Delete A Module
# Sudo rmmod hello
So where is the output of the program? As you can see on the Internet, if the terminal does not appear, it will be in syslog
# Cat/var/log/syslog | grep world
# Hello, world
# Goodbye, linux world
Now all the work is done. I hope this Hello World is the first step for me to enter the linux driver ~, I believe you can also compile it! Successfully compiled.