ubuntu下编译内核驱动。

目的:

1. 驱动热身。网上有很多类似的文章可供参考。

2. 在操作系统中, 编写这个设备的驱动。

3. 为写qemu的watchdog驱动练手。

有朋友问make的 watchdog驱动 需要什么准备,所以写这个blog。

 

环境:

ubuntu 12.04.4

热身:

  首先编写一个简单的 hello world驱动。

耗时(基于熟悉linux环境和操作系统的条件下):

  1. 安装操作系统 30-60分钟。

  2. 配置操作系统 30分钟。主要是配置中文书法。无其他特殊的配置。

  3. 代码编写运行 40分钟。

 

查看自己的内核版本:

Search for kernel version (optional)(搜寻内核的版本)

Type the following command:

$ apt-cache search linux-headers-$(uname -r)

Install linux-header package under Debina or Ubuntu Linux(更新内核的版本)

Type the following command:

$ sudo apt-get install linux-headers-$(uname -r)

 

代码:

// hello.c的驱动程序:
 
#include<linux/init.h>  //初始换函数
#include<linux/kernel.h>  //内核头文件
#include<linux/module.h>  //模块的头文件
 
MODULE_LICENSE("shaohef  BSD/GPL");
 
static int __init hello_start(void)
{
    printk(KERN_ALERT "Loading hello module...\n");
    printk(KERN_ALERT "Hello world\n");
    return 0;
}
 
static void __exit hello_end(void)
{
    printk(KERN_ALERT "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);

 

// makefile文件:
ifeq ($(KERNELRELEASE),)
 KVERSION = $(shell uname -r)
all:
 make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) modules
clean:
 make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) clean
modules_install
 $(MAKE)  -C  $(KERNELDIR)M=$(PWD)  modules_install
else
 obj-m :=hello.o
endif
 

安装驱动(可能需要重启):

编译

$ make

挂载驱动

$ sudo insmod hello.ko

卸载驱动

$ sudo rmmod hello

查看驱动, 最好加上管道 |more指令 或者|grep

lsmod |grep hello

 

系统启动的时加载模块

文件 /etc/modules 设置加载的内核,这个文件里包含了系统启动的时候要加载的模块,每一个模块一行。

首先 hello 模块 拷贝到 /lib/modules/$(uname -r)/kernel/drivers.

建议的步骤:

(a)为 hello 模块建立一个目录

$ sudo mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello

(b)拷贝模块

$ sudo cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/

(c)编辑 /etc/modules 文件

$ sudo vim /etc/modules

加上下面一行

hello

(d)重启查看是否加载。

$ sudo cat /proc/modules 

或者

$ sudo lsmod | less

查看程序的输出:

#cat   /var/log/syslog  |grep  world

如果syslog没有输出, 查看 /var/log/message 里面的信息

# tail -f /var/log/message

 注:

insmod的本质就是将ko文件与运行的内核进行链接的过程。类似于编译helloworld的链接过程。

链接必然需要先进行编译,以便确定所需的外部符号(EXPORT_SYMBOLS)是否存在,因为有些符号(函数或全局变量)在内核中。在驱动中如果使用到这些符号,必须预留一个位置,insmod时进一步确定这些符号的具体位置(符号绑定)。

如果内核都没有编译过,怎么知道这些符号有没有编入内核中

 

转载于:https://www.cnblogs.com/shaohef/p/3799117.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值