Chinaunix首页 | 论坛 | 博客
  • 博客访问: 380773
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 560
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-10 08:38
文章分类
文章存档

2016年(32)

2015年(48)

我的朋友

分类: LINUX

2015-06-12 11:01:33

转载地址: http://blog.csdn.net/jiankangshiye/article/details/6665179

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   
  2. #include   
  3.   
  4. MODULE_LICENSE("GPL");  
  5. static int hello_init(void)  
  6. {  
  7.   printk(KERN_ALERT "Hello, world\n");  
  8.   return 0;  
  9. }  
  10. static void hello_exit(void)  
  11. {  
  12.   printk(KERN_ALERT "Goodbye, cruel world\n");  
  13. }  
  14.   
  15. module_init(hello_init);  
  16. module_exit(hello_exit);  




4. 编写hello模块的Makefile文件,Makefile内容如下:

Makefile

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #Makefile 2.6  
  2.   
  3. obj-m :=hello.o  
  4. KERNEL :=/usr/src/kernels/2.6.23.1-4-i686/  
  5. PWD :=$(shell pwd)  
  6. modules :  
  7.     $(MAKE) -C $(KERNEL) M=$(PWD) modules  
  8. .PHONEY:clean  
  9. clean :  
  10.     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


[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. # If KERNELRELEASE is defined, we've been invoked from the  
  2. # kernel build system and can use its language.  
  3. ifneq ($(KERNELRELEASE),)  
  4.   
  5.  obj-m := hello.o   
  6. # Otherwise we were called directly from the command  
  7. # line; invoke the kernel build system.  
  8. else  
  9.   
  10.  KERNELDIR ?= /lib/modules/$(shell uname -r)/build  
  11.  PWD := $(shell pwd)   
  12. default:  
  13.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  14.   
  15. 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. 查询安装内核头文件


[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  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的结果

上面这个命令比较麻烦,还可以选择下面的命令实现同样的目的


[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. apt-get install linux-headers-`uname -r`  


注意这个命令里使用的不是单引号,而是反单引号,位于键盘的左上角, 一般和数字1是邻居。



4. 写个Hello模块测试

5. FAQ

内核代码下载后, 要简单的运行俩命令配置一下,我这里的命令如下[当然, 您也可以不尝试这一步, 当你make时, 系统会提示你该怎么做]


[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. cd /home/kernel/linux-2.6.32.10  
  2. make oldconfig && make prepare  



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

is missing; modules will have no dependencies and modversions.


[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  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传输命令

debian中安装sshd的命令:  apt-get install openssh-server

启动sshd服务的命令: /etc/init.d/ssh start

参考:


6. hello驱动涉及到linux驱动模型的方方面面

hello.h代码文件


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



hello.c代码文件


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