OpenWRT 增加内核模块及应用方法

  1. 进入package目录,创建模块目录
    cd mcp/branches/V1.1-beta1/mcp/package
    mkdir example
  2. 进入example目录,创建Makefile文件和代码路径
    1. cd example
    2. touch Makefile
    3. mkdir src
     Makefile具体内容如下:
    1. # Kernel module example
    2. include $(TOPDIR)/rules.mk
    3. include $(INCLUDE_DIR)/kernel.mk
    4. PKG_NAME:=example
    5. PKG_RELEASE:=1
    6. include $(INCLUDE_DIR)/package.mk
    7. define KernelPackage/example
    8.   SUBMENU:=Other modules
    9.   DEPENDS:=@TARGET_octeon
    10.   TITLE:=Support Module for example
    11.   AUTOLOAD:=$(call AutoLoad,81,example)
    12.   FILES:=$(PKG_BUILD_DIR)/example/example.$(LINUX_KMOD_SUFFIX)
    13. endef
    14. define Build/Prepare
    15.   mkdir -p $(PKG_BUILD_DIR)
    16.   $(CP) -R ./src/* $(PKG_BUILD_DIR)/
    17. endef
    18. define Build/Compile
    19.   $(MAKE) -C "$(LINUX_DIR)" \
    20.     CROSS_COMPILE="$(TARGET_CROSS)" \
    21.     ARCH="$(LINUX_KARCH)" \
    22.     SUBDIRS="$(PKG_BUILD_DIR)/example" \
    23.     EXTRA_CFLAGS="-g $(BUILDFLAGS)" \
    24.     modules
    25. endef
    26. $(eval $(call KernelPackage,example))
3.进入src目录,创建代码路径和相关源文件
  1. cd src
  2. mkdir example
  3. cd example
  4. 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.   default n
  4.   help
  5.    This is a example, for debugging kernel model.
  6.    If unsure, say N.

  Makefile具体内如如下:

  1. obj-m := example.o

  回到主路径 mcp/branches/V1.1-beta1/mcp/,编译选项配置保存并编译

  1. make menuconfig
  2.   Kernel modules --->
  3.     Other modules --->
  4.       kmod-example

  选项设置为M,保存退出

  然后编译该模块:

  1. make package/example/compile

5.编译出的文件可以在主路径的以下路径找到

  1. ./staging_dir/target-mips64_eglibc-2.10.1/root-octeon/lib/modules/2.6.30.9/
  2. ./build_dir/linux-octeon/example/ipkg-octeon/kmod-example/lib/modules/2.6.30.9/
  3. ./build_dir/linux-octeon/example/example/
  文件名为:example.ko
  注:我们使用./build_dir/linux-octeon/example/example/example.ko
二、用户态工具添加方法
 1.进入package目录,创建工具目录
  1. cd mcp/branches/V1.1-beta1/mcp/package
  2. mkdir example1
 2.进入example1目录,创建Makefile文件和代码路径
  1. cd example1
  2. touch Makefile
  3. mkdir src
 该Makefile具体内容如下:
  1. #User mode tool example
  2. include $(TOPDIR)/rules.mk
  3. include $(INCLUDE_DIR)/kernel.mk
  4. PKG_NAME:=example1
  5. PKG_RELEASE:=1
  6. PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
  7. include $(INCLUDE_DIR)/package.mk
  8. define Package/example1
  9.  SECTION:=utils
  10.  CATEGORY:=Base system
  11.  TITLE:=Build for example1 commands
  12. endef
  13. define Package/example1/description
  14.  This package contains an utility useful to use example1 commands.
  15. endef
  16. define Build/Prepare
  17.   mkdir -p $(PKG_BUILD_DIR)
  18.   $(CP) ./src/* $(PKG_BUILD_DIR)/
  19. endef
  20. target=$(firstword $(subst -, ,$(BOARD)))
  21. MAKE_FLAGS += TARGET="$(target)"
  22. TARGET_CFLAGS += -Dtarget_$(target)=1 -Wall
  23. define Build/example1/compile
  24.   $(MAKE) -C "$(LINUX_DIR)" \
  25.    CROSS_COMPILE="$(TARGET_CROSS)" \
  26.    ARCH="$(LINUX_KARCH)" \
  27.    SUBDIRS="$(PKG_BUILD_DIR)" \
  28.    EXTRA_CFLAGS="$(BUILDFLAGS)"
  29. endef
  30. define Package/example1/install
  31.   $(INSTALL_DIR) $(1)/sbin
  32.   $(INSTALL_BIN) $(PKG_BUILD_DIR)/example1 $(1)/sbin/
  33. endef
  34. $(eval $(call BuildPackage,example1))
3.进入src目录,创建相关源文件
  1. cd src
  2. touch example1.c Makefile
  example1.c 具体内容如下:
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.   printf("Hello, world\n");
  5.   return 0;
  6. }
  Makefile文件具体内容如下:
  1. .NOTPARALLEL:
  2. #OCTEON_ROOT=$(PWD)/src/
  3. CC=~/openwrt/main/staging_dir/toolchain-mips64_gcc-4.4.1_eglibc-2.10.1/usr/bin/mips64-openwrt-linux-gnu-gcc
  4. CFLAGS=-mips64r2 -mabi=64 -march=octeon -mtune=octeon
  5. LFLAGS=
  6. .PHONY: all
  7. all: example1
  8. example1:example1.c
  9.   ${CC} ${CFLAGS} ${LFLAGS} -W -g -Wall -Wno-unused-parameter -DUSE_RUNTIME_MODEL_CHECKS=1 \
  10.     -o $@ example1.c
4.回到主路径 mcp/branches/V1.1-beta1/mcp/,编译选项配置保存并编译
  1. make menuconfig
  2.   Base system --->
  3.    example1
 选项设置为M,保存退出
 然后编译该模块:
  1. make package/example1/compile
5.编译出的文件可以在主路径的以下路径找到
  1. ./staging_dir/target-mips64_eglibc-2.10.1/root-octeon/sbin/
  2. ./build_dir/linux-octeon/example1/ipkg-octeon/example1/sbin/
  3. ./build_dir/linux-octeon/example1/

文件名为:example1
注:我们使用./build_dir/linux-octeon/example1/example1

根据OpenWrt安装介绍,将内核模块和用户态工具在板子上运行,到这就简单了往下我就不贴了。


你可能感兴趣的:(OpenWRT 增加内核模块及应用方法)