開發環境為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具體內容如下:
## 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.mkinclude $(INCLUDE_DIR)/kernel.mkPKG_NAME:=examplePKG_RELEASE:=1include $(INCLUDE_DIR)/package.mkdefine KernelPackage/example SUBMENU:=Other modules TITLE:=example driver DEPENDS:=@LINUX_2_6 FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX) KCONFIG:=endefdefine KernelPackage/example/description Kernel module to exampleendefEXTRA_KCONFIG:= \CONFIG_EXAMPLE=mEXTRA_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/Preparemkdir -p $(PKG_BUILD_DIR)$(CP) ./src/* $(PKG_BUILD_DIR)/endefdefine Build/Compile$(MAKE) -C "$(LINUX_DIR)" \$(MAKE_OPTS) \modulesendef$(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
http://blog.csdn.net/gubenpeiyuan/article/details/8024247