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 texlive-lang-french gawk asciidoc libz-dev tex4ht git-core
下面就是下载源码,源码分两种,一种是最新版但不稳定,就是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具体内容如下:
# # Copyright (C) 2008 OpenWrt.org # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=example PKG_RELEASE:=1 include $(INCLUDE_DIR)/package.mk define KernelPackage/example SUBMENU:=Other modules TITLE:=example driver DEPENDS:=@LINUX_2_6 FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX) KCONFIG:= endef define KernelPackage/example/description Kernel module to example endef EXTRA_KCONFIG:= \ CONFIG_EXAMPLE=m EXTRA_CFLAGS:= \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \ MAKE_OPTS:= \ ARCH="$(LINUX_KARCH)" \ CROSS_COMPILE="$(TARGET_CROSS)" \ SUBDIRS="$(PKG_BUILD_DIR)" \ EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \ $(EXTRA_KCONFIG) define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/ endef define Build/Compile $(MAKE) -C "$(LINUX_DIR)" \ $(MAKE_OPTS) \ modules endef $(eval $(call KernelPackage,example))
3.进入src目录,创建代码路径和相关源文件
cd src
touch example.c Kconfig Makefile
example.c具体内容如下:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> /* hello_init ---- 初始化函数,当模块装载时被调用,如果成功装载返回0 否则返回非0值 */ static int __init hello_init(void) { printk("I bear a charmed life.\n"); return 0; } /* hello_exit ---- 退出函数,当模块卸载时被调用 */ static void __exit hello_exit(void) { printk("Out, out, brief candle\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("zhangjiefeng");Kconfig具体内容如下:
config EXAMPLE tristate "Just a example" help This is a example, for debugging kernel model. If unsure, say N.Makefile具体内如如下:
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
参考文档:
http://blog.chinaunix.net/uid-9217288-id-3060464.html
http://downloads.openwrt.org/kamikaze/docs/openwrt.html#x1-470002.1.3