OpenWRT 增加内核模块及应用方法
进入package目录,创建模块目录
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example
进入example目录,创建Makefile文件和代码路径
cd example
touch Makefile
mkdir src
Makefile具体内容如下:
# Kernel module example
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
DEPENDS:=@TARGET_octeon
TITLE:=Support Module for example
AUTOLOAD:=$(call AutoLoad,81,example)
FILES:=$(PKG_BUILD_DIR)/example/example.$(LINUX_KMOD_SUFFIX)
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) -R ./src/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)/example" \
EXTRA_CFLAGS="-g $(BUILDFLAGS)" \
modules
endef
$(eval $(call KernelPackage,example))
进入src目录,创建代码路径和相关源文件
cd src
mkdir example
cd example
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"
default n
help
This is a example, for debugging kernel model.
If unsure, say N.
Makefile具体内如如下:
obj-m := example.o
回到主路径 mcp/branches/V1.1-beta1/mcp/,编译选项配置保存并编译
make menuconfig
Kernel modules --->
Other modules --->
kmod-example
选项设置为M,保存退出
然后编译该模块:
make package/example/compile
编译出的文件可以在主路径的以下路径找到
./staging_dir/target-mips64_eglibc-2.10.1/root-octeon/lib/modules/2.6.30.9/
./build_dir/linux-octeon/example/ipkg-octeon/kmod-example/lib/modules/2.6.30.9/
./build_dir/linux-octeon/example/example/
文件名为:example.ko
注:我们使用./build_dir/linux-octeon/example/example/example.ko
用户态工具添加方法
进入package目录,创建工具目录
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example1
进入example1目录,创建Makefile文件和代码路径
cd example1
touch Makefile
mkdir src
该Makefile具体内容如下:
#User mode tool example
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=example1
PKG_RELEASE:=1
PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
define Package/example1
SECTION:=utils
CATEGORY:=Base system
TITLE:=Build for example1 commands
endef
define Package/example1/description
This package contains an utility useful to use example1 commands.
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
target=$(firstword $(subst -, ,$(BOARD)))
MAKE_FLAGS += TARGET="$(target)"
TARGET_CFLAGS += -Dtarget_$(target)=1 -Wall
define Build/example1/compile
$(MAKE) -C "$(LINUX_DIR)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)" \
EXTRA_CFLAGS="$(BUILDFLAGS)"
endef
define Package/example1/install
$(INSTALL_DIR) $(1)/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/example1 $(1)/sbin/
endef
$(eval $(call BuildPackage,example1))
进入src目录,创建相关源文件
cd src
touch example1.c Makefile
example1.c 具体内容如下:
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}
Makefile文件具体内容如下:
.NOTPARALLEL:
#OCTEON_ROOT=$(PWD)/src/
CC=~/openwrt/main/staging_dir/toolchain-mips64_gcc-4.4.1_eglibc-2.10.1/usr/bin/mips64-openwrt-linux-gnu-gcc
CFLAGS=-mips64r2 -mabi=64 -march=octeon -mtune=octeon
LFLAGS=
.PHONY: all
all: example1
example1:example1.c
${CC} ${CFLAGS} ${LFLAGS} -W -g -Wall -Wno-unused-parameter -DUSE_RUNTIME_MODEL_CHECKS=1 \
-o $@ example1.c
回到主路径 mcp/branches/V1.1-beta1/mcp/,编译选项配置保存并编译
make menuconfig
Base system --->
example1
选项设置为M,保存退出
然后编译该模块:
make package/example1/compile
编译出的文件可以在主路径的以下路径找到
./staging_dir/target-mips64_eglibc-2.10.1/root-octeon/sbin/
./build_dir/linux-octeon/example1/ipkg-octeon/example1/sbin/
./build_dir/linux-octeon/example1/
文件名为:example1
注:我们使用./build_dir/linux-octeon/example1/example1
根据OpenWrt安装介绍,将内核模块和用户态工具在板子上运行,到这就简单了往下我就不贴了。
本文章由 http://www.wifidog.pro/2015/01/30/openwrt%E6%B7%BB%E5%8A%A0%E6%A8%A1%E5%9D%97.html 整理编辑,转载请注明出处