1th Linux driver ___ Compiler shell driver

Source: Internet
Author: User

In the previous blog post, we have learned how to coordinate how each level in the whole system works, and the command issued by the application layer reaches the drive layer under the kernel, thus achieving the purpose of operating the hardware layer.


Let's look at the code in the final molded "shell driver" first_drv.c in the last blog post:

#include <linux/module.h> #include <linux/init.h>static int __init first_drv_init (void) {PRINTK (kern_info    "Hello world!\n"); return 0;}    static int __exit first_drv_exit (void) {PRINTK (kern_info "Goodbye world...\n"); return 0;} Module_init (First_drv_init); Module_exit (First_drv_exit);


As I said in my previous blog post, there is a disadvantage to this driver, and we'll optimize it in the next post. Now first to compile the driver, how to compile it, Source insight can not provide such a compile button, how to do?


We previously logged in to a Linux virtual machine remotely via CuteFTP and dragged/work/my_drivers/first_drv/1th/first_drv.c to D:\work\my_drivers\first_drv\1th\ directory, then use the source Insight on Windows to open first_drv.c and write the above code into FIRST_DRV.C, but how do you compile the code?


In the SCM or embedded development, we often use the "cross-compilation" method to compile the program, such as on the Keil platform for the 51 microcontroller to write the LED.C program, we just need to click the Keil Compile button, the compiler software will automatically run the compiler to compile the LED.C, the generation of binary files Led.bin, you can burn to the microcontroller to run, like this in the Windows environment to run the compilation process Sequence can compile a program that runs on another platform (such as a microcontroller), which is called "cross-compiling."


Our target platform 51 microcontroller running a compiler to compile led.c is obviously not very realistic, then for the embedded development is also the same, we usually use a cross-compilation tool in the virtual machine (such as ARM-LINUX-GCC) to build the bin file for the arm environment, and then burn the bin file to the arm Development Board to transport Yes.


We can use commands such as ARM-LINUX-GCC in the cross-compilation toolchain in the Ubuntu command line to compile programs that have already been written, but for ease of operation, we usually use makefile files to take on the work of compiling.


You better makefile some basic grammar, you can go online to find information learning, remember not to delve into, makefile grammar can speak a book, but we currently only need some of the most basic grammar on it.


Drag first_drv.c to Ubuntu's/work/my_drivers/first_drv/1th directory via CuteFTP, and in SECURECRT terminal (remote login to Ubuntu via SSH2 protocol) we enter/work /my_drivers/first_drv/1th/


Then execute: VI Makefile


Switch to text mode and enter the following:


Kern_dir =/work/kernel/linux-2.6.22.6


All

Make-c $ (kern_dir) m= ' pwd ' modules


Obj-m + = FIRST_DRV.O


Note that the indented line is indented with a TAB key, with a width of four spaces, but not four spaces, and a TAB key must be struck.

After you have a little understanding of Makefile's syntax, let's go through the analysis on a row-by-line basis:


First line:Kern_dir =/work/kernel/linux-2.6.22.6


In fact, the computer language is connected,kern_dir is a variable, the value of this variable is initialized to:/work/kernel/linux-2.6.22.6


So /work/kernel/linux-2.6.22.6 What does this path mean, we use makefile to make it easier to compile the driver, so why does the first sentence in Makefile have to refer to this path? Is there anything special about this path?


In fact, this path is the path of our kernel source tree, in our first download in www.kernel.org (Linux kernel download website) 2.6.22.6 version of the kernel (actually download which version does not matter), after decompression can see in the directory Arch, Block 、...、 USR These many directories, the Linux kernel source files are distributed in these directories.


Now you believe that there are many source files for the Linux kernel in the Ubuntu /work/kernel/linux-2.6.22.6 directory,

The uimage generated after compiling the kernel in the directory is the uimage we burn to the arm Development Board.


That is, the compiler can be run on the Development Board uimage when used by the kernel source code, and we mkefile used in the path of the /work/kernel/linux-2.6.22.6 kernel source is the same thing.


In fact, not only compile uimage, we compile the driver first_drv.c also need to use this kernel source files, only when we compile uimage and compile the driver, all use the same kernel source code, then uimage and driver to match. If we compile the driver first_drv.c Use this version of the kernel source code, and compile Uimage use another version of the kernel source code, then our driver module can not be registered to the kernel, resulting in the driver module can not work in the core of the Development Board.


So we can guess, we will guide makefile to the /work/kernel/linux-2.6.22.6 directory of the kernel source code to the driver first_drv.c to compile. Now, we continue to analyze:


All

Make-c $ (kern_dir) m= ' pwd ' modules

All represents the target, and the colon after all should be followed by a dependency, but we omit it here.

Since this is the first goal of makefile, we simply execute make in the/work/my_drivers/first_drv/1th directory, which is equivalent to calling Make-c $ (kern_dir) m= ' pwd ' in makefile Modules

Two anti-quotes plus pwd ( ' pwd') means: PWD is not a variable, but a command, while the Linux environment pwd is used to print out the current directory, plus the previous analysis of Kern_dir , This long list of commands can be replaced as follows:


Make - C $ (kern_dir) m= ' pwd ' Modules  

Make - c /work/kernel/linux-2.6.22.6 m=/work/my_drivers/first_drv/1th modules


Compare the green section to discover that m= ' pwd ' has become a m=/work/my_drivers/first_drv/1th


We executed a make command in the/work/my_drivers/first_drv/1th directory, so this is the directory where we are currently located.


Don't be intimidated by this long list of colorful commands, and we'll analyze them individually.


The first command make and the last parameter modules should be put together, that is, do modules, which means that we are going to compile a module with this line of command.


How to compile according to what,-c/lib/modules/2.6.31-23-generic/build, means enter /lib/modules/2.6.31-23-generic /build directory down to read the kernel source of makefile, using the kernel source code makefile rules to compile our driver first_drv.c.


And the green part:m=/work/my_drivers/first_drv/1th means that after the compilation is complete, it returns to the /work/my_drivers/first_drv/1th directory. Continue with the remainder of the makefile.


In the actual compilation process, FIRST_DRV.C can be compiled into many formats of the file, in the end, you want to generate which format file? We are going to compile the C file into a driver module that can be installed into the kernel, we write the last line of the makefile in obj-m + = first_drv.o means to compile a module according to the FIRST_DRV.O file (first_ Drv.ko).


But for the shell driver we wrote FIRST_DRV.C, its task is to print out Hello World, which is obviously not suitable for running on the development Board, we want to be able to see in the Ubuntu command line driver print out Hello world, then please think about it, Ubuntu is a Linux distribution, that is, a Linux operating system, it also has its own kernel, the kernel also has a certain version number, so if we want to allow the driver to run in Ubuntu and print out Hello world, then Compile the driver program used by the kernel source must be and Ubuntu kernel source "good match"!


Or, to make our compiled driver module can be recognized by the Ubuntu kernel, we'd better use Ubuntu's own kernel source file to compile the driver first_drv.c, so as to get the ability to run in Ubuntu environment First_ Drv.ko, print out the desired Hello world.


So where is Ubuntu's own kernel source file?


In the Ubuntu command line, enter: ls/lib/modules/


Print out in my Ubuntu system: 2.6.31-14-generic 2.6.31-23-generic


This is two folders, loaded with different versions of the kernel source files, one is 2.6.31-14 version, the other is the 2.6.31-23 version, which version of Ubuntu itself is based on?


"Uname" is a Linux command, you can perform "man uname" learned that "uname" role is "print the kernel release", that is, the print kernel version number.


Execute uname-r print out: 2.6.31-23-generic


So 2.6.31-23 is our Ubuntu kernel version number.


Execution: CD 2.6.31-23-generic/build/


You can see the same files as those in the/work/kernel/linux-2.6.22.6 directory.


In Mkaefile # indicates that the line content is commented, we will already have the kernel source tree kern_dir =/work/kernel/linux-2.6.22.6 comment as follows:


#KERN_DIR =/work/kernel/linux-2.6.22.6


Then add the following two lines:


Kern_ver = $ (Shell uname-r)

Kern_dir =/lib/modules/$ (kern_ver)/build


What do these two lines mean, this involves makefile's syntax, but you should be able to guess roughly that the two lines above are equivalent:


Kern_dir =/lib/modules/$ (Shell uname-r)/build


I have learned that after executing uname-r, we will get: 2.6.31-23-generic


Then replace it with the following assignment:


Kern_dir =/lib/modules/2.6.31-23-generic/build


This is exactly the absolute path to the Ubuntu kernel source tree I'm using.


Execute LS /lib/modules/2.6.31-23-generic/build


Can see arch, block 、...、 usr these many kernel source directory, and /work/kernel/linux-2.6.22.6 path under the kernel source directory is the same format, but their kernel version number is different


After we save the makefile, the final content of the makefile is:


Kern_ver = $ (Shell uname-r)

Kern_dir =/lib/modules/$ (kern_ver)/build


#KERN_DIR =/work/kernel/linux-2.6.22.6


All

Make-c $ (kern_dir) m= ' pwd ' modules


Obj-m + = FIRST_DRV.O


By executing make under the/work/my_drivers/first_drv/1th/path, you can run the commands in the makefile, which will print the following compilation information on the command line:


Make-c/lib/modules/2.6.31-23-generic/build m= ' pwd ' modules

MAKE[1]: Entering directory '/usr/src/linux-headers-2.6.31-23-generic '

CC [M]/work/my_drivers/first_drv/1th/first_drv.o

Building modules, Stage 2.

Modpost 1 Modules

cc/work/my_drivers/first_drv/1th/first_drv.mod.o

LD [M]/work/my_drivers/first_drv/1th/first_drv.ko

MAKE[1]: Leaving directory '/usr/src/linux-headers-2.6.31-23-generic '


Execute LS to display all the files in the current/work/my_drivers/first_drv/1th/directory:


FIRST_DRV.C First_drv.ko first_drv.mod.c FIRST_DRV.MOD.O first_drv.o

Makefile module.markers Modules.order module.symvers


You can see that there are only first_drv.c and makefile two files in this path before you execute make, and now we have a lot of files, which is the product we generated during our makefile execution, we only care about First_ Drv.ko, because this is our driver module file,


In fact, we can change the makefile to the following:


Kern_ver = $ (Shell uname-r)

Kern_dir =/lib/modules/$ (kern_ver)/build


#KERN_DIR =/work/kernel/linux-2.6.22.6


All

Make-c $ (kern_dir) m= ' pwd ' modules


Clean

Make-c $ (kern_dir) m= ' pwd ' modules clean

RM-RF Modules.order


Obj-m + = FIRST_DRV.O


In this way, we have added the clean target to delete the files generated by the make command, to execute the delete command, to execute the input makes clean on that path, clean cannot be omitted, because the clean target is not the first goal in our makefile, all is, Executing make is the equivalent of doing all.


After executing the make clean LS, it is found that only first_drv.c and makefile two files are left in the directory.





This article is from the "12253782" blog, please be sure to keep this source http://12263782.blog.51cto.com/12253782/1872875

1th Linux Driver ___ compiling the shell driver

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.