openwrt增加核心模組的方法__Openwrt

來源:互聯網
上載者:User

開發環境為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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.