支付宝能绑定信用社卡:内核源码书编译及简单驱动验证

来源:百度文库 编辑:中财网 时间:2024/05/04 22:15:41

内核源码书编译及简单驱动验证

分类: Linux 相关 2011-04-06 13:19 19人阅读 评论(0) 收藏 举报

这几天看了一些linux驱动的资料,对其结构和原理大致有了些了解,然后就开始动手实践了。
首先是要重新编译内核,构建linux内核树。我完全是小白一个,网上搜了很多方案,尝试了好些次,终于成功。步骤如下:
1.安装编译内核所需要的软件
有:build-essential、autoconf、automake、cvs、subversion
$apt-get install build-essential kernel-package libncurses5-dev
libncurses5这个软件包在使用menuconfig配置内核的时候会用到。
2.下载内核源码
先查看linux内核版本:$uname -r
然后用apt-cache search linux-source命令, 会列出一些可选源码包,对准你的内核版本号,选择“with Ubuntu patche”的那个
最后用apt-get install linux-source-2.6.32下载之。解压缩源码包,进入解压后的源码目录。
在编译之前我们需要Ubuntu原来内核的一个配置文件,这是我/usr/src目录下的文件预览:ls -al
drwxr-xr-x  4 root root     4096 2010-09-04 21:31 fglrx-8.723.1
drwxr-xr-x 24 root root     4096 2010-09-04 20:35 linux-headers-2.6.32-25
drwxr-xr-x  7 root root     4096 2010-09-04 20:35 linux-headers-2.6.32-25-generic
drwxr-xr-x 25 root root     4096 2010-09-16 21:39 linux-source-2.6.32
-rw-r--r--  1 root root 65846876 2010-09-01 22:41 linux-source-2.6.32.tar.bz2
现在我们需要linux-headers-2.6.32-25-generic目录下的.config文件,我们把它拷贝到我们刚下好解压的目录,也就是linux-source-2.6.32
sudo cp /usr/src/linux-headers-2.6.32-25-generic/.config /usr/src/linux-2.6.32
接下来切换到root用户
sudo -i
cd /usr/src/linux-2.6.32
make menuconfig
终端会弹出一个配置界面
最后有两项:load a alternative kernel configuration...
save a alternative configuration...
选择load a kernel configuration保存,然后在选择save akernel configuration再保存退出,并退出配置环境。
接下来我们就要开始编译了。
#cd /usr/src/linux-2.6.32
#make
记住一定要是管理员帐号运行,这个过程很久,如果你的cpu是双核的可以在make后面加个参数,make -j4.
#make bzImage 执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。
#make modules /* 编译 模块 */
#make modules_install  这条命令能在/lib/modules目录下产生一个目录

如果一切顺利,编译过程中不出现什么错误的话,接下来我们就可以开始linux模块的helloworld了。
我在 /home/xxxx/linux_modules/ 目录下创建2个文本文件 hello.c Makefile
 //hello.c
#include
#include
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
KERNELDIR := /lib/modules/2.6.32-21-generic/build
PWD := $(shell pwd)
modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
需要注意的是makefile的格式$(MAKE)前面要加个tab.
make 编译,不出现错误的话,用ls -al查看linux_modules目录下产生了如下文件:
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers
其中hello.ko就是可加载的模块文件
现在我们就可以将编译好的模块helloworld加载到内核中去了
#insmod ./hello.ko   //这个命令把hello.ko加载到内核
#lsmod|grep hello   //lsmod 这个命令可以查看当前所有的驱动模块,结果应该显示hello 680 0
#rmmod hello         //这个命令是把hello这个模块移除掉

程序的输出结果可以在
/var/log/syslog文件中查看
Hello,World
Goodbye,cruel world
这是程序的输出。

csdn上的fudan_abc大师的著作很搞很实用,正在跟着学:《Linux内核修炼之道》《Linux那些事之我是USB_core》。希望在他的引领下,通过我的努力,能拔去Linux下USB驱动的一点皮毛。跟着他,有肉吃!

原文地址:http://hi.baidu.com/bsbgong/blog/item/e2a3da38830955e7b311c704.htmls

分享到: