1. 程式人生 > >Linux下編寫和載入 .ko 檔案(驅動模組檔案)

Linux下編寫和載入 .ko 檔案(驅動模組檔案)

一、.ko 檔案介紹

.ko檔案是kernel object檔案(核心模組),該檔案的意義就是把核心的一些功能移動到核心外邊, 需要的時候插入核心,不需要時解除安裝。
 

二、優點

(1)這樣可以縮小核心體積;

(2)使用方便。

三、.ko檔案一般的用處

(1)作為一個功能模組,需要使用時,直接插入執行就行。如在imx6上連線模擬攝像頭,先執行模擬攝像頭對應的驅動模組 camera.ko檔案,然後對應的工程執行檔案執行就行。

四、使用.ko 檔案

1、載入驅動模組test.ko

(1)方法一 
進入test.ko驅動模組檔案所在的目錄,然後直接   insmod  test.ko 

(2)方法二 
將test.ko檔案拷貝到/lib/module/#uname-r#/目錄下,這裡,#uname -r#意思是,在終端中輸入 
uname -r後顯示的核心版本及名稱,例如mini2440中#uname-r#就是2.6.32.2-FriendlyARM。

然後 depmod(會在/lib/modules/#uname -r#/目錄下生成modules.dep和modules.dep.bb檔案,表明模組的依賴關係) 
最後 modprobe test(注意這裡無需輸入.ko字尾) 即可

注:兩種方法的區別

modprobe和insmod類似,都是用來動態載入驅動模組的,區別在於modprobe可以解決load module時的依賴關係,它是通過/lib/modules/#uname -r/modules.dep(.bb)檔案來查詢依賴關係的;而insmod不能解決依賴問題。也就是說,如果你確定你要載入的驅動模組不依賴其他驅動模組的話,既可以insmod也可以modprobe,當然insmod可以在任何目錄下執行,更方便一些。而如果你要載入的驅動模組還依賴其他ko驅動模組的話,就只能將模組拷貝到上述的特定目錄,depmod後再modprobe。

2、檢視已載入的驅動模組列表

在任何目錄下輸入命令

//
 lsmod 
//

3、解除安裝驅動模組

在任何目錄下, 輸入命令

//
rmmod <module_name>  注:“module_name”是lsmod顯示的模組名稱,而不是對應的ko檔名
//注:“module_name”是lsmod顯示的模組名稱,而不是對應的ko檔名
//

五、編寫生成.ko 檔案

Linux下hello.ko核心模組製作的全過程

1. linux系統用的是Redflag 6.0 SP1 下載地址:ftp://ftp.redflag-linux.com/pub/redflag/dt6sp1/SP1/redflag-6-sp1.iso, 系統安裝很容易,安提示做就好。


所用的核心原始碼目錄樹下載地址:ftp://ftp.redflag-linux.com/pub/redflag/dt6sp1/SP1/redflag-6-tool-sp1-src1.iso,將此iso檔案掛載到/mnt下,安裝其中的核心rpm包。
掛載方法:mount -t iso9660 redflag-6-tool-sp1-src1.iso /mnt/ -o loop
核心目錄樹安裝方法:cd /mnt/RedFlag/SRMPS/

                    rpm -i kernel-2.6.23.1-4.src.rpm

3. 編寫hello模組程式碼,原始碼如下:

hello.c

  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. MODULE_LICENSE("GPL");  
  4. static int hello_init(void)  
  5. {  
  6.   printk(KERN_ALERT "Hello, world\n");  
  7.   return 0;  
  8. }  
  9. static void hello_exit(void)  
  10. {  
  11.   printk(KERN_ALERT "Goodbye, cruel world\n");  
  12. }  
  13. module_init(hello_init);  
  14. module_exit(hello_exit);  

4. 編寫hello模組的Makefile檔案,Makefile內容如下:

Makefile

  1. #Makefile 2.6  
  2. obj-m :=hello.o  
  3. KERNEL :=/usr/src/kernels/$(uname -r)/  
  4. PWD :=$(shell pwd)  
  5. modules :  
  6.     $(MAKE) -C $(KERNEL) M=$(PWD) modules  
  7. .PHONEY:clean  
  8. clean :  
  9.     rm -f *.o *.ko  

5. 編譯模組
在命令列進入hello.c所在的資料夾下執行make命令即可完成hello模組的編譯。用ls命令可以檢視到hello.ko檔案,此檔案就是我們自定義的核心模組。

6. 安裝hello模組

命令列下執行命令:insmod hello.ko 。通過命令:cat /var/log/messages 

可以看到下面這樣的資訊:“Aug  6 13:37:59 localhost kernel: Hello, world”,說明模組載入成功了。

7. 另外一種模組Makefile的編寫方法

Makefile

  1. # If KERNELRELEASE is defined, we've been invoked from the  
  2. # kernel build system and can use its language.  
  3. ifneq ($(KERNELRELEASE),)  
  4.  obj-m := hello.o   
  5. # Otherwise we were called directly from the command  
  6. # line; invoke the kernel build system.  
  7. else  
  8.  KERNELDIR ?= /lib/modules/$(shell uname -r)/build  
  9.  PWD := $(shell pwd)   
  10. default:  
  11.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  12. endif  

7. 解除安裝hello模組

命令列下執行命令:rmmod hello.ko即可。通過命令:cat /var/log/messages.
可以看到下面這樣的資訊:“Aug  6 13:40:36 localhost kernel: Goodbye, cruel world”,說明模組解除安裝成功。

8. 檢視模組資訊

命令列下執行命令:modinfo hello

二 Debian 6下製作hello核心模組的過程

1. 軟體

Debian 6.02 

linux-2.6.32.10.tar.bz2

2. 解壓核心原始碼到一個目錄下, 我解壓到/home/kernel/下來

3. 查詢安裝核心標頭檔案

  1. aptitude search linux-headers-2.6.32*  
  2. aptitude install linux-headers-2.6.32-5-686  

注意:2.6.32-5-686來自命令uname -r的結果

上面這個命令比較麻煩,還可以選擇下面的命令實現同樣的目的

  1. apt-get install linux-headers-`uname -r`  


注意這個命令裡使用的不是單引號,而是反單引號,位於鍵盤的左上角, 一般和數字1是鄰居。

4. 寫個Hello模組測試

5. FAQ

<Q1> 核心程式碼下載後, 要簡單的執行倆命令配置一下,我這裡的命令如下[當然, 您也可以不嘗試這一步, 當你make時, 系統會提示你該怎麼做]

<A1>

  1. cd /home/kernel/linux-2.6.32.10  
  2. make oldconfig && make prepare  

<Q2>WARNING: Symbol version dump /home/kernel/linux-2.6.32.10/Module.symvers

is missing; modules will have no dependencies and modversions.

<A2>

  1. cd /home/kernel/linux-2.6.32.10  
  2. make modules  

[參考內容]

The Module.symvers is (re)generated when you (re)compile modules. Run make modules, and you should get a Module.symvers file at the root of the kernel tree.

Note that if you only ran make and not make modules, you haven't built any modules yet. The symbols from the kernel itself (vmlinux or one of the architecture-dependent image formats) are in System.map.

經測試問題得到很好的解決, make modules要花費好長編譯時間段。

[問題] type defaults to "int' in declaration of module_init

[解答] module_init(hello_init);這句中某個字元弄成漢字編碼導致的

[問題]  XP與虛擬機器裡的debian通訊我用倆辦法一個samba傳輸資料,一個是ssh傳輸命令

[解答] 啟動sshd服務的命令: /etc/init.d/ssh start

6. hello驅動涉及到linux驅動模型的方方面面

hello.h程式碼檔案

  1. #ifndef _HELLO_ANDROID_H  
  2. #define _HELLO_ANDROID_H  
  3. #include <linux/cdev.h>  
  4. #include <linux/semaphore.h>  
  5. #define HELLO_DEVICE_NODE_NAME  "hello"  
  6. #define HELLO_DEVICE_FILE_NAME  "hello"  
  7. #define HELLO_DEVICE_PROC_NAME  "hello"  
  8. #define HELLO_DEVICE_CLASS_NAME "hello"  
  9. struct hello_android_dev {  
  10.     int val;  
  11.     struct semaphore sem;  
  12.     struct cdev dev;  
  13. };  
  14. #define init_MUTEX(sem) sema_init(sem, 1)  
  15. #endif  

hello.c程式碼檔案

  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/fs.h>  
  5. #include <linux/proc_fs.h>  
  6. #include <linux/device.h>  
  7. #include <asm/uaccess.h>  
  8. #include "hello.h"  
  9. static int hello_major = 0;  
  10. static int hello_minor = 0;  
  11. static struct class *hello_class = NULL;  
  12. static struct hello_android_dev *hello_dev = NULL;  
  13. static int hello_open(struct inode *inode, struct file *filp);  
  14. static int hello_release(struct inode *inode, struct file *filp);  
  15. static ssize_t hello_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos);  
  16. static ssize_t hello_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos);  
  17. static struct file_operations hello_fops = {  
  18.     .owner = THIS_MODULE,  
  19.     .open  = hello_open,  
  20.     .release = hello_release,  
  21.     .read  = hello_read,  
  22.     .write = hello_write,  
  23. };  
  24. static ssize_t hello_val_show(struct device *dev, struct device_attribute *attr, char *buf);  
  25. static ssize_t hello_val_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);  
  26. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);  
  27. static int hello_open(struct inode *inode, struct file *filp)  
  28. {  
  29.     struct hello_android_dev *dev;  
  30.     printk(KERN_ALERT"hello_open 1\n");  
  31.     dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
  32.     printk(KERN_ALERT"hello_open 2\n");  
  33.     dev = container_of(inode->i_cdev, struct hello_android_dev, dev);  
  34.     printk(KERN_ALERT"hello_open 3\n");  
  35.     filp->private_data = dev;  
  36.     printk(KERN_ALERT"hello_open 4\n");  
  37.     return 0;  
  38. }  
  39. static int hello_release(struct inode *inode, struct file *filp)  
  40. {  
  41.     return 0;  
  42. }  
  43. static ssize_t hello_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)  
  44. {  
  45.     ssize_t err = 0;  
  46.     struct hello_android_dev *dev = filp->private_data;  
  47.     if (down_interruptible(&(dev->sem))) {  
  48.         return -ERESTARTSYS;  
  49.     }  
  50.     if (count < sizeof(dev->val)) {  
  51.         goto out;  
  52.     }  
  53.     printk(KERN_ALERT"hello_read\n");  
  54.     if (copy_to_user(buf, &(dev->val), sizeof(dev->val))) {  
  55.         err = -EFAULT;  
  56.         goto out;  
  57.     }  
  58.     err = sizeof(dev->val);  
  59. out:  
  60.     up(&(dev->sem));  
  61.     return err;  
  62. }  
  63. static ssize_t hello_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)  
  64. {  
  65.     struct hello_android_dev *dev = filp->private_data;  
  66.     ssize_t err = 0;  
  67.     if (down_interruptible(&(dev->sem))) {  
  68.         return -ERESTARTSYS;  
  69.     }  
  70.     if (count != sizeof(dev->val)) {  
  71.         goto out;  
  72.     }  
  73.     if (copy_from_user(&(dev->val), buf, count)) {  
  74.         err = -EFAULT;  
  75.         goto out;  
  76.     }  
  77.     err = sizeof(dev->val);  
  78. out:  
  79.     up(&(dev->sem));  
  80.     return err;  
  81. }  
  82. /* 
  83.  * dev fs operations  
  84.  */  
  85. static ssize_t __hello_get_val(struct hello_android_dev *dev, char *buf)  
  86. {  
  87.     int val = 0;  
  88.     if (down_interruptible(&(dev->sem))) {  
  89.         return -ERESTARTSYS;  
  90.     }  
  91.     val = dev->val;  
  92.     up(&(dev->sem));  
  93.     return snprintf(buf, PAGE_SIZE, "%d\n", val);  
  94. }  
  95. static ssize_t __hello_set_val(struct hello_android_dev *dev, const char *buf, size_t count)  
  96. {  
  97.     int val = 0;  
  98.     val = simple_strtol(buf, NULL, 10);  
  99.     if (down_interruptible(&(dev->sem))) {  
  100.         return -ERESTARTSYS;  
  101.     }  
  102.     dev->val = val;  
  103.     up(&(dev->sem));  
  104.     return count;  
  105. }  
  106. static ssize_t hello_val_show(struct device *dev, struct device_attribute *attr, char *buf)  
  107. {  
  108.     struct hello_android_dev *hdev = (struct hello_android_dev *)dev_get_drvdata(dev);  
  109.     return __hello_get_val(hdev, buf);  
  110. }  
  111. static ssize_t hello_val_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   
  112. {  
  113.     struct hello_android_dev *hdev = (struct hello_android_dev *)dev_get_drvdata(dev);  
  114.     return __hello_set_val(hdev, buf, count);  
  115. }  
  116. /* 
  117.  * proc fs operations 
  118.  */  
  119. static ssize_t hello_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)  
  120. {  
  121.     if (off > 0) {  
  122.         *eof = 1;  
  123.         return 0;  
  124.     }  
  125.     return __hello_get_val(hello_dev, page);  
  126. }  
  127. static ssize_t hello_proc_write(struct file *filp, const char __user *buf, unsigned long len, void *data)  
  128. {  
  129.     int err = 0;  
  130.     char *page = NULL;  
  131.     if (len > PAGE_SIZE) {  
  132.         printk(KERN_ALERT"The buff is too large: %lu.\n", len);  
  133.         return -EFAULT;  
  134.     }  
  135.     page = (char *)__get_free_page(GFP_KERNEL);  
  136.     if (!page) {  
  137.         printk(KERN_ALERT"Failed to alloc page.\n");  
  138.         return -ENOMEM;  
  139.     }  
  140.     if (copy_from_user(page, buf, len)) {  
  141.         printk(KERN_ALERT"Failed to copy buff from user.\n");  
  142.         err = -EFAULT;  
  143.         goto out;  
  144.     }  
  145.     err = __hello_set_val(hello_dev, page, len);  
  146. out:  
  147.     free_page((unsigned long)page);  
  148.     return err;  
  149. }  
  150. /* 
  151.  * /proc/hello  
  152.  */  
  153. static void hello_create_proc(void)  
  154. {  
  155.     struct proc_dir_entry *entry;  
  156.     entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);  
  157.     if (entry) {  
  158.         /* entry->owner = THIS_MODULE;*/  
  159.         entry->read_proc  = hello_proc_read;  
  160.         entry->write_proc = hello_proc_write;  
  161.     }  
  162. }  
  163. static void hello_remove_proc(void)  
  164. {  
  165.     remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);  
  166. }  
  167. static int __hello_setup_dev(struct hello_android_dev *dev)  
  168. {  
  169.     int err;  
  170.     dev_t devno = MKDEV(hello_major, hello_major);  
  171.     memset(dev, 0, sizeof(struct hello_android_dev));  
  172.     cdev_init(&(dev->dev), &hello_fops);  
  173.     dev->dev.owner = THIS_MODULE;  
  174.     dev->dev.ops   = &hello_fops;  
  175.     err = cdev_add(&(dev->dev), devno, 1);  
  176.     if (err) {  
  177.         return err;  
  178.     }  
  179.     init_MUTEX(&(dev->sem));  
  180.     dev->val = 0;  
  181.     return 0;  
  182. }  
  183. static int __init hello_init(void)  
  184. {  
  185.     int err = -1;  
  186.     dev_t dev = 0;  
  187.     struct device *temp = NULL;  
  188.     printk(KERN_ALERT"Initializing hello device.\n");  
  189.     err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);  
  190.     if (err < 0) {  
  191.         printk(KERN_ALERT"Failed to alloc char dev region.\n");  
  192.         goto fail;  
  193.     }  
  194.     hello_major = MAJOR(dev);  
  195.     hello_minor = MINOR(dev);  
  196.     hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);  
  197.     if (!hello_dev) {  
  198.         err = -ENOMEM;  
  199.         printk(KERN_ALERT"Failed to alloc hello_dev.\n");  
  200.         goto unregister;  
  201.     }  
  202.     err = __hello_setup_dev(hello_dev);  
  203.     if (err) {  
  204.         printk(KERN_ALERT"Failed to setup dev: %d.\n", err);  
  205.         goto cleanup;  
  206.     }  
  207.     hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);  
  208.     if (IS_ERR(hello_class)) {  
  209.         err = PTR_ERR(hello_class);  
  210.         printk(KERN_ALERT"Failed to create hello class.\n");  
  211.         goto destroy_cdev;  
  212.     }  
  213.     /* 
  214.      * create /dev/hello 
  215.      * create /sys/class/hello/hello 
  216.      */  
  217.     temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);  
  218.     if (IS_ERR(hello_class)) {  
  219.         err = PTR_ERR(hello_class);  
  220.         printk(KERN_ALERT"Failed to create hello device.\n");  
  221.         goto destroy_class;  
  222.     }  
  223.     /*  
  224.      * create /sys/class/hello/hello/val  
  225.      */  
  226.     err = device_create_file(temp, &dev_attr_val);  
  227.     if (err < 0) {  
  228.         printk(KERN_ALERT"Failed to create attribute val.\n");  
  229.         goto destroy_device;  
  230.     }  
  231.     dev_set_drvdata(temp, hello_dev);  
  232.     /* 
  233.      * create /proc/hello 
  234.      */  
  235.     hello_create_proc();  
  236.     printk(KERN_ALERT"Succedded to initialize hello device.\n");  
  237.     return 0;  
  238. destroy_device:  
  239.     device_destroy(hello_class, dev);  
  240. destroy_class:  
  241.     class_destroy(hello_class);  
  242. destroy_cdev:  
  243.     cdev_del(&(hello_dev->dev));  
  244. cleanup:  
  245.     kfree(hello_dev);  
  246. unregister:  
  247.     unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);  
  248. fail:  
  249.     return err;  
  250. }  
  251. static void __exit hello_exit(void)  
  252. {  
  253.     dev_t devno = MKDEV(hello_major, hello_minor);  
  254.     printk(KERN_ALERT"Remove hello device.\n");  
  255.     /* 
  256.      * remove /proc/hello 
  257.      */  
  258.     hello_remove_proc();  
  259.     /* 
  260.      * destroy device and class 
  261.      */  
  262.     if (hello_class) {  
  263.         device_destroy(hello_class, MKDEV(hello_major, hello_minor));  
  264.         class_destroy(hello_class);  
  265.     }  
  266.     /* 
  267.      * delete cdev and free malloced mem 
  268.      */  
  269.     if (hello_dev) {  
  270.         cdev_del(&(hello_dev->dev));  
  271.         kfree(hello_dev);  
  272.     }  
  273.     /* 
  274.      * free device ID 
  275.      */  
  276.     unregister_chrdev_region(devno, 1);  
  277. }  
  278. MODULE_LICENSE("GPL");  
  279. MODULE_DESCRIPTION("First Android Driver /dev/hello");  
  280. module_init(hello_init);  
  281. module_exit(hello_exit);  

[問題] Can't find default configuration "arch/x86/configs/

[解答] 預設編譯 x86 配置的config

diff --git a/Makefile b/Makefile

index 39af85f..f7bcdad 100644

--- a/Makefile

+++ b/Makefile

@@ -192,7 +192,7 @@ SUBARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \

# "make" in the configured kernel build directory always uses that.

# Default value for CROSS_COMPILE is not to prefix executables

# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile

-ARCH ?= i$(SUBARCH)

+ARCH ?= mips

CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)

致謝