He,YuanHui —— 业精于勤荒于嬉,行成于思毁于随

如果你喜欢一个事,又有这样的才干,那就把整个人都投入进去,就要象一把刀直扎下去直到刀柄一样,不要问为什么,也不要管会碰到什么。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

这篇文章写得相当不错,推荐阅读,同时在此感谢一下原作者 微笑

                                                                                           ——hyh

 

飛帆網  http://www.pyoix.com  2010年09月17日 09:02

本文參考了很多網上的文章,在這裏先感謝網上的朋友們。編譯了一天終於可以導入Ubuntu 10.04 設備驅動程序最簡單的 Hello World。

其實我也是一個初學者,但衹要堅持學習,一定會理解的。

現在我講述在Ubuntu 10.04下安裝的過程:

1.安裝編譯內核所需要的軟件

build-essential、autoconf、automake、cvs、subversion

apt-get install build-essential kernel-package libncurses5-dev

libncurses5這個軟件包在使用menuconfig配置內核的時候會用到。

2.下載內核源碼

使用uname -r 命令查看當前的內核版本號,我的是2.6.32-25-generic,使用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

我選擇linux-source-2.6.32 - Linux kernel source for version 2.6.32 with Ubuntu patches

sudo apt-get install linux-source-2.6.32

下載好後cd /usr/src 目錄下就可以看見linux-source-2.6.32.tar.bz2,然後解壓到當前的目錄
sudo tar xjvf linux-source-2.6.32.tar.bz2

解壓完畢,會生成linux-source-2.6.32目錄

3.編譯內核源碼

在編譯之前我們需要 Ubuntu原來內核的一個配置文件

這是我/usr/src目錄下的文件預覽:

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 kernel configuration...
save a kernel 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目錄下產生一個目錄。

4.測試驅動模塊

這裏就抄個程序測試了,初學者顧不了那麽多了。

我在 /home/shana/linux_q/ 目錄下創建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
KERNELDIR := /lib/modules/2.6.20/build
PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

需要註意的是makefile的格式$(MAKE)前面要加個tab.
make 編譯,產生如下文件:
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers

5.加載模塊到內核去

sudo insmod ./hello.ko 這個命令把hello.ko加載到內核

sudo rmmod hello 這個命令是把hello這個模塊移除掉

lsmod 這個命令可以查看當前所有的驅動模塊

程序的輸出結果可以在
/var/log/syslog文件中查看

Sep 16 21:50:10 wuyangyu-desktop kernel: [10428.551271] Hello,World
Sep 16 21:55:45 wuyangyu-desktop kernel: [10763.644605] Goodbye,cruel world

這是程序的輸出。

 

本文来自:http://www.pyoix.com/a/735.html

收藏备查

posted on 2011-02-25 18:06  He,YuanHui  阅读(591)  评论(0编辑  收藏  举报

Add to Google