您的位置:首页 > 其它

在ubuntu10.04上驱动环境配置

2011-05-20 20:29 387 查看
各位同学:

在我看完实验3时我就知道,你们这次实验不容易,果真在几天后我宿舍的大刘同学就问我找不到头文件怎么回事儿,

在今天我遇到李老师的时候我们交流了一下,我说了这次实验的难度,便觉得会有很多同学在这卡壳滴,所以李老师叫我把我的思路写下来给你们借鉴借鉴,鄙人不才,还请你们多多指教,好吧现在就听我给你们慢慢道来!

刚看 O'REILLY 写的《LINUX 设备驱动程序》时。作者一再强调在编写驱动程序时必须 建立内核树。所谓内核树,我的理解和网上资料说的一致就是内核源码的一种逻辑形式。那怎么建立呢?为此上网“翻云覆雨”起来而结果却是“惨败而归“。

先查看自己OS使用的内核版本

lmh@lmh:~$ uname -r

2.6.32-24-generic /* 这是我显示的结果 */

如果安装系统时,自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。例如下(我是自己下的)

lmh@lmh:/usr/src$ ls

linux-headers-2.6.32-24

linux-headers-2.6.32-24-generic

linux-source-2.6.32 /*这个就是解压后的源码目录 */

linux-source-2.6.32.tar.bz2 /* 这是我下的源码 包 */

lmh@lmh:/usr/src$

如果没有源码。(一般ubuntu 都没有吧)

查看一下可一下载的源码包(切记不要使用超级用户使用此命令否则……会提示没有此命令)

lmh@lmh:/usr/src$ apt-cache search linux-source

linux-source - Linux kernel source with Ubuntu patches

linux-source-2.6.32 - Linux kernel source for version 2.6.32 with Ubuntu patches

lmh@lmh:/usr/src$

我选择了 linux-source-2.6.32 - Linux kernel source for version 2.6.32 with Ubuntu patches 这个~

然后 install 之

lmh@lmh:/usr/src$ sudo apt-get install linux-source-2.6.32(注意:由于学校限网,可在windows下下载linux-source-2.6.32版本的内核源码将其copy到ubuntu目录/usr/src下)

下载完成后,在/usr/src下,文件名为:linux-source-2.6.32.tar.bz2,是一个压缩包,解压缩既可以得到整个内核的源代码:

注意 已经切换到超级用户模式

root@lmh:/usr/src#tar jxvf linux-source-2.6.32.tar.bz2

解压后生成一个新的目录/usr/src/linux-source-2.6.32,所有的源代码都在该目录下。

进入该目录

开始配置内核 选择最快的原版的配置(默认)方式 (我是如此)

root@lmh:/usr/src/linux-source-2.6.32# make oldconfig

当然你也可以使用 自己喜欢的配置方式 如 menuconfig , xconfig(必须有GTK环境吧)。反正不用剪裁什么,所以不管那种方式能配置它就行了。

完成后,开始make 吧 这儿比较久 一般有1一个小时吧。(保证空间足够 我编译完成后 使用了1.8G)

root@lmh:/usr/src/linux-source-2.6.32$ make

root@lmh:/usr/src/linux-source-2.6.32$ make bzImage

当然,第一个make也可以不执行,直接make bzImage。执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。

然后 :

root@lmh:/usr/src/linux-source-2.6.32#make modules /* 编译 模块 */

root@lmh:/usr/src/linux-source-2.6.32#make modules_install /* 安装 模块 */

执行结束之后,会在/lib/modules下生成新的目录/lib/modules/2.6.32-24-generic/

。 在随后的编译模块文件时,要用到这个路径下的build目录。至此,内核编译完成。可以重启一下系统。

至此 内核树就建立啦 原来不是很难.....

写一个 最简单 最没用的驱动吧

我在 /home/lmh/program/ 目录下创建2个文本文件 hello.c Makefile

//hello.c

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)

{

printk(KERN_ALERT "Hello, world/n");

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT"Goodbye, cruel world/n");

}

module_init(hello_init);

module_exit(hello_exit);

程序我就不解释了……

Makefile 文件

obj-m = hello.o

K_DIR = $(shell uname -r)

PWD = $(shell pwd)

all:

make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules

clean:

make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean

# end #

如果以上你都完成了在 make 时出现这样的错误

lmh@lmh:~/linux_驱动开发$ make

make: 没有什么可以做的为 `modules'。

原因很简单 你肯定是从我这直接复制的吧~~~呵呵,Makefile格式错误啦~

解决办法就是 你把如 make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules 移动到行首 然后按Tab 键自动对齐

这时里边的变量都变成绿色了~然后在 make 吧

lmh@lmh:~/linux_驱动开发$ make

make -C /lib/modules/2.6.32-24-generic/build M=/home/lmh/linux_驱动开发 modules

make[1]: Entering directory `/usr/src/linux-headers-2.6.32-24-generic'

CC [M] /home/lmh/linux_驱动开发/hello.o

Building modules, stage 2.

MODPOST 1 modules

CC /home/lmh/linux_驱动开发/hello.mod.o

LD [M] /home/lmh/linux_驱动开发/hello.ko

make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-24-generic'

lmh@lmh:~/linux_驱动开发$

lmh@lmh:~/linux_驱动开发$ ls

总用量 124

hello.c

hello.ko

hello.mod.c

hello.mod.o

hello.o

linux_qudong_qu.txt

Makefile

Module.symvers

lmh@lmh:~/linux_驱动开发$

然后加载模块 (超级用户)

root@lmh:/home/lmh/linux_驱动开发# insmod ./hello.ko

按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现 (原因不解)

root@lmh:/home/lmh/linux_驱动开发# insmod ./hello.ko

root@lmh:/home/lmh/linux_驱动开发#

查看加载模块

root@lmh:/home/lmh/linux_驱动开发# lsmod

Module Size Used by

hello 2560 0

已经加载上咯~~

删除模块

root@lmh:/home/lmh/linux_驱动开发# rmmod hello

root@lmh:/home/lmh/linux_驱动开发#

那程序的输出在那呢?书中说明 如果不出现在终端 则会写进 syslog 文件中

lmh@lmh:~/linux_驱动开发$ cat /var/log/syslog |grep world

Mar 16 12:14:53 lmh kernel: [ 5937.529297] Hello, world

Mar 16 12:16:05 lmh kernel: [ 6009.439036] Goodbye, cruel world

lmh@lmh:~/linux_驱动开发$

至此 全部工作都完成了。是否对你有用呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: