Development Platform:
Ubuntu10.04, kernel version 2.6.32-33-generic,
Cross-compiler: Arm-Linux-GCC 4.3.3,
Kernel tree:/opt/embedsky/linux-2.6.30.4/
Target Platform:
Tianembedded company's tq2440 Development Board ARM9. kernel version 2.6.30.4
1. Release the inner source code package linux-2.6.30.4_20100531.tar.bz2,
tar xvfj linux-2.6.30.4_20100531.tar.bz2 –C /
-C: Specifies the extract directory, followed by/indicates that the archive file is decompressed in the root directory.
The kernel tree will be decompressed to/opt/embedsky/linux-2.6.30.4/
Compile the kernel:
cd /opt/EmbedSky/linux-2.6.30.4cp config_EmbedSky_W35 .configmake zImage
Here, config_embdesky_w35 is the kernel configuration file provided by the daily embedded Company. It can be used temporarily at the beginning.
At this point, the kernel tree is ready, which is a prerequisite for compiling the kernel driver module. If the kernel tree is not prepared in your own file system, you cannot construct a writable module.
2. Install the cross compiler 4.3.3
Decompress the eabi-4.3.3_embedsky_20100610.tar.bz2,
tar xvfj EABI-4.3.3_EmbedSky_20100610.tar.bz2 –C /
Then the cross compiler will be decompressed to/opt/embedsky/4.3.3 /,
Modify the environment variable configuration file/etc/environment,
Path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /opt/embedsky/4.3.3/bin"
Language = "zh_cn: Zh: en_us: en"
Lang = "zh_cn.utf8"
Or
Use commands
export PATH=$PATH:/opt/EmbedSky/4.3.3/bin
Save and restart to take effect. If you do not want to restart, use
source /etc/environment
However, this command only takes effect for this terminal. If there are no other problems, we recommend that you restart it.
3. Write the hello World Driver Module
cd /home/shanks/modulesmkdir hellocd hello
The driver module does not have to be placed in the kernel tree. As long as your makefile specifies the path of the kernel tree, there is no problem, therefore, I have created a modules directory in/home/shanks/to develop driver modules.
Hello. C source code:
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void){ printk(KERN_ALERT "Hello, world\n"); return 0;} static void hello_exit(void){ printk(KERN_ALERT "Goodbye, cruel world\n"); return 0;}module_init(hello_init);module_exit(hello_exit);
Makefile:
KERNELDIR=/opt/EmbedSky/linux-2.6.30.4 PWD:=$(shell pwd) INSTALLDIR=$(PWD) CC=arm-linux-gcc obj-m := hello.o modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: rm -rf *.o *.ko *.mod.c *.markers *.order *.symvers .PHONY:modules clean
It is necessary to explain the makefile compiling method of the driver module. Note the following:
obj-m := hello.o
Extended syntax, which indicates that a module needs to be constructed from the target file hello. O. The module name is hello. Ko.
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
The above command first changes the directory to the location specified by the-C option (that is, the directory source code directory), where the top-level files with the kernel are saved. M = option: Let the makefile at the kernel level be returned to the module source code directory before constructing the modules target. Then, read the makefile in PWD for the second time. The modules target points to the module set in the obj-m variable, and the kernel makefile is responsible for constructing the module.
Then,
Make
Several files are generated. Hello. Ko is the module File we need to load.
Note that you must prepare the kernel tree before making. Otherwise, make will fail.
Connect to the Development Board. Here I mount the PC root directory to/mnt on the board,
cp /mnt/home/shanks/modules/hello/hello.ko/tmpcd /tmp
Okay. Load the module!
[Root @ embedsky/tmp] # insmod hello. Ko
Hello, world
[Root @ embedsky/tmp] # rmmod hello
Goodbye, cruel world
Done!
Possible insmod errors:
I.
Hello: Version magic '2. 6.30.4 mod_unload armv4 'could be '2. 6.30.4-embedsky mod_unload armv4'
Insmod: cannot insert 'hello. Ko ': Invalid module format
Error cause: the module version does not match the kernel version!
Solution: import the preset configuration information and enter menuconfig to continue general config-> () local version-> Add the following content to the brackets: "-embedsky" and save it. config, re-compile the kernel and driver modules, and then load the modules.
II.
Hello: Unknown symbol _ aeabi_unwind_cpp_pr0
Insmod: cannot insert 'hello. Ko ': Unknown symbol in module, or unknown
Parameter
Cause of error: the module and kernel image do not match
Solution: Use the kernel image zimage and zimage that you use to compile the driver at the same time. There is a sentence on ldd3:Although not required, it is best to run the kernel corresponding to the module.
At the beginning, I learned the driver and finally loaded the hello World module!
This is a small step driven by learning, but it is a huge step for me!