openWRT内核模块加载

来源:互联网 发布:linux用dd制作启动盘 编辑:程序博客网 时间:2024/05/20 22:01

转自:openwrt增加内核模块的方法

开发环境为ubuntu.首先搭建编译环境。
sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev gawk asciidoc libz-dev git-core build-essential libssl-dev
下面就是下载源码,源码分两种,一种是最新版但不稳定,就是trunk版,一种是相对稳定版,backfire版
trunk版下载命令:svn co svn://svn.openwrt.org/openwrt/trunk/
backfire版下载命令:svn co svn://svn.openwrt.org/openwrt/branches/backfire/
如果不是最新下载,最好定期更新代码,命令为
./scripts/feeds update –a
./scripts/feeds install –a
接着就是编译了。编译方法如下:
make defconfig
make menuconfig进入定制界面,选择自己的设备类型。
make V=99


下面就是增加内核模块的方法了

进入package目录,创建模块目录
cd backfire/package
mkdir example
进入example目录,创建Makefile文件和代码路径
cd example
touch Makefile
mkdir src
 Makefile具体内容如下:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #  
  2. # Copyright (C) 2008 OpenWrt.org  
  3. #  
  4. # This is free software, licensed under the GNU General Public License v2.  
  5. # See /LICENSE for more information.  
  6. #  
  7.   
  8. include $(TOPDIR)/rules.mk  
  9. include $(INCLUDE_DIR)/kernel.mk  
  10.   
  11. PKG_NAME:=example  
  12. PKG_RELEASE:=1  
  13.   
  14. include $(INCLUDE_DIR)/package.mk  
  15.   
  16. define KernelPackage/example  
  17.   SUBMENU:=Other modules  
  18.   TITLE:=example driver  
  19.   DEPENDS:=@LINUX_2_6  
  20.   FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX)  
  21.   KCONFIG:=  
  22. endef  
  23.   
  24. define KernelPackage/example/description  
  25.   Kernel module to example  
  26. endef  
  27.   
  28. EXTRA_KCONFIG:= \  
  29.     CONFIG_EXAMPLE=m  
  30.   
  31. EXTRA_CFLAGS:= \  
  32.     $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \  
  33.     $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \  
  34.   
  35. MAKE_OPTS:= \  
  36.     ARCH="$(LINUX_KARCH)" \  
  37.     CROSS_COMPILE="$(TARGET_CROSS)" \  
  38.     SUBDIRS="$(PKG_BUILD_DIR)" \  
  39.     EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \  
  40.     $(EXTRA_KCONFIG)  
  41.   
  42. define Build/Prepare  
  43.     mkdir -p $(PKG_BUILD_DIR)  
  44.     $(CP) ./src/* $(PKG_BUILD_DIR)/  
  45. endef  
  46.   
  47. define Build/Compile  
  48.     $(MAKE) -C "$(LINUX_DIR)" \  
  49.         $(MAKE_OPTS) \  
  50.         modules  
  51. endef  
  52.   
  53. $(eval $(call KernelPackage,example))  


3.进入src目录,创建代码路径和相关源文件
cd src
touch example.c Kconfig Makefile
  example.c具体内容如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/kernel.h>  
  4. /* hello_init ---- 初始化函数,当模块装载时被调用,如果成功装载返回0 否则返回非0值 */  
  5. static int __init hello_init(void)  
  6. {  
  7.     printk("I bear a charmed life.\n");  
  8.     return 0;  
  9. }  
  10. /* hello_exit ---- 退出函数,当模块卸载时被调用 */  
  11. static void __exit hello_exit(void)  
  12. {  
  13.     printk("Out, out, brief candle\n");  
  14. }  
  15. module_init(hello_init);  
  16. module_exit(hello_exit);  
  17. MODULE_LICENSE("GPL");  
  18. MODULE_AUTHOR("zhangjiefeng");  
  Kconfig具体内容如下:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. config EXAMPLE  
  2.   tristate "Just a example"  
  3.   help  
  4.    This is a example, for debugging kernel model.  
  5.    If unsure, say N.  
  Makefile具体内如如下:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. obj-$(CONFIG_EXAMPLE)   += example.o  
  回到主路径 backfire/,编译选项配置保存并编译
make menuconfig
  Kernel modules --->
    Other modules --->
      kmod-example
  选项设置为M,保存退出
  然后编译该模块:
make package/example/compile
5.编译出的文件可以在主路径的以下路径找到
./staging_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko
./build_dir/linux-lantiq_ar9/example/ipkg-lantiq/kmod-example/lib/modules/2.6.32.33/example.ko
./build_dir/linux-lantiq_ar9/example/example.ko
./build_dir/target-mips_r2_uClibc-0.9.30.1/OpenWrt-SDK-lantiq-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1/staging_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko
./build_dir/target-mips_r2_uClibc-0.9.30.1/root-lantiq/lib/modules/2.6.32.33/example.ko

  注:我们使用./build_dir/linux-lantiq_ar9/example/example.ko

0 0
原创粉丝点击