環(huán)境
Linux
內(nèi)核升級(jí)
- 下載標(biāo)準(zhǔn)內(nèi)核源碼
- 鏈接選擇合適的版本秉沼,我選擇的是4.3.1。
- 創(chuàng)建一個(gè)文件夾 放入其中矿酵,解壓唬复。進(jìn)入第二層目錄。
- 執(zhí)行make menuconfig,進(jìn)入 Processor type and feature選項(xiàng)全肮,回車進(jìn)入Processor family選項(xiàng)敞咧,選擇 Generic-x86-64保存退出。
- make
- make modules
- make modules_install
- make install
- reboot
- 重啟后uname -r查看新內(nèi)核版本辜腺。
開(kāi)始編寫Hello World驅(qū)動(dòng)程序
hello.c
#include <linux/init.h> /* 定義了一些相關(guān)的宏 */
#include <linux/module.h> /* 定義了模塊需要的*/
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n"); /* 打印hello World */
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, world\n"); /* 打印Goodbye,world */
}
module_init(hello_init); /* 指定模塊加載函數(shù) */
module_exit(hello_exit); /* 指定模塊卸載函數(shù) */
MODULE_LICENSE("Dual BSD/GPL");
Makefile
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /linux-2.6.29.4/linux-2.6.29.4
# The current directory is passed to sub-makes as argument
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
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
修改下KERNELDIR 路徑就可以了休建。
insmod加載模塊
- insmod hello.ko
以上就是全部過(guò)程了=_=