Openwrt添加内核模块


转载有道,先给出原文路径:http://blog.csdn.net/l0605020112/article/details/13168261


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

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

  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具体内容如下:
  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具体内容如下:
  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具体内如如下:
  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


对出现的错误和解决进行一些总结。

看似一个很简单的流程,做起来也是出现了一些问题。希望自己以后不要再犯错。

1,出现错误   *** No rule to make target ` '.  Stop. 

      在makefile 行连接符“\” 后面多了空格。如果其他类似错误,都是由于复制文本的时候出现的空格,或者前面不是tab开头的

2 出现错误 *** Recursive variable `ARCH' references itself (eventually).  Stop.

make官网是这么说的。

Recursive variable `xxx' references itself (eventually). Stop.

This means you’ve defined a normal (recursive) make variablexxx that, when it’s expanded, will refer to itself (xxx).This is not allowed; either use simply-expanded variables (‘:=’or ‘::=’) or use the append operator (‘+=’).

以后要多加注意。

    

发布了26 篇原创文章 · 获赞 39 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章