My first HelloWorld driver in ubuntu is a memorable day. I don't know how long it took me to compile it! 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 ~ My first Hello World driver in ubuntu is a memorable day. I don't know how long it took me to compile it! 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! Www.2cto.com has referenced many documents on the Internet during the preparation of 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. The ubuntu system does not exist by default. the kernel source code tree needs to be downloaded by itself. 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 have been compiled into the Hello World driver in the kernel? 1. First, check the kernel version xiongyao @ xiongyao-Lenovo :~ $ Uname-r3.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 don't have that version. I downloaded xiongyao @ xiongyao-Lenovo: /usr/src $ lslinux-headers-3.2.0-34 linux-source-3.2.0 linux-headers-3.2.0-34-generic-pae (I downloaded) if there is no source code, check the source code package that can be downloaded (don't use this command by a super user, he will prompt that this command is not available) # apt-cache search linux-source xiongyao @ xiongyao-Lenovo: /usr/src $ apt-cache search linux-sourcelinux-source-Linux kernel source with Ubuntu patcheslinux-source-3.2.0-Linux kernel source for ve Rsion 3.2.0 with Ubuntu patches 2: and then download the linux-source-3.2.0 # sudo apt-get install linux-source-3.2.0 after the download is complete, under/usr/src, the file name is linux-source-3.2.0.tar.bz2 this compressed package, after decompression, you can get the entire source code # sudo tar jxvf linux-source-3.2.0.tar.bz2 unzip will generate a new directory/usr/src/linux-source-3.2.0, 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, the reason is that you did not download the configuration environment The dependent library file can be downloaded by yourself.) after the configuration is complete, the kernel needs to be compiled. 4: The process of compiling the kernel # sudo make is long, we suggest you do other things ~, I spent more than an hour, and it was okay after dinner. # Several errors finally occurred in the result of sudo make zImage kernel compilation, but after the completion of the Hello World compilation, A new file will be generated under the current directory: vmlinux 5: then the compilation module # sudo make modules 6: installation module # After sudo make modules_install is executed, A new directory/lib/modules/3.2.31/will be generated under/lib/modules/3.2.31/. the build directory under this path will be used in subsequent compilation module files. No, the kernel has compiled www.2cto.com, after a long time, the Hello World program can finally 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 and cannot use the printf function in the user State C library function. Instead, use the printk function // Print the 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 file obj-m: = hello. o // Generate the target file KERNELDIR: =/lib/modules/3.2.31/buildPWD: = $ (shell pwd) modules :( use one here Tab keys) $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules (a tab key is used here) $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules_install. the best key step is to go to the makefile directory xiongyao @ xiongyao-Lenovo :~ $ Sudo makemake-C/lib/modules/3.2.31/build M =/home/xiongyao make [1]: entering 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. komake [1]: is leaving the directory '/usr/src/linux-source-3.2.0/linux-source-3.2.0' above to prove successful and then load module # sudo insmod. /hello. ko should have been in the final stage The terminal displays hello, world, but nothing is displayed on the terminal (which will be solved later). view the loading Module # hello 2560 0 ^ _ ^ has been generated in sudo lsmod, the deletion module has been loaded. # Where is the output of the sudo rmmod hello 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 www.2cto.com
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