進(jìn)入package目錄刁憋,創(chuàng)建模塊目錄
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example
進(jìn)入example目錄滥嘴,創(chuàng)建Makefile文件和代碼路徑
cd example
touch Makefile
mkdir src
Makefile具體內(nèi)容如下:
# 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))
進(jìn)入src目錄至耻,創(chuàng)建代碼路徑和相關(guān)源文件
cd src
mkdir example
cd example
touch example.c Kconfig Makefile
example.c具體內(nèi)容如下:
#include
#include
#include
/* hello_init ---- 初始化函數(shù)若皱,當(dāng)模塊裝載時(shí)被調(diào)用,如果成功裝載返回0 否則返回非0值 */
static int __init hello_init(void)
{
printk("I bear a charmed life.\n");
return 0;
}
/ * hello_exit ---- 退出函數(shù)尘颓,當(dāng)模塊卸載時(shí)被調(diào)用 */
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具體內(nèi)容如下:
config EXAMPLE
tristate "Just a example"
default n
help
This is a example, for debugging kernel model.
If unsure, say N.
Makefile具體內(nèi)如如下:
obj-m := example.o
回到主路徑 mcp/branches/V1.1-beta1/mcp/走触,編譯選項(xiàng)配置保存并編譯
make menuconfig
Kernel modules --->
Other modules --->
kmod-example
選項(xiàng)設(shè)置為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
用戶態(tài)工具添加方法
進(jìn)入package目錄疤苹,創(chuàng)建工具目錄
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example1
進(jìn)入example1目錄互广,創(chuàng)建Makefile文件和代碼路徑
cd example1
touch Makefile
mkdir src
該Makefile具體內(nèi)容如下:
#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))
進(jìn)入src目錄,創(chuàng)建相關(guān)源文件
cd src
touch example1.c Makefile
example1.c 具體內(nèi)容如下:
#include
int main(void)
{
printf("Hello, world\n");
return 0;
}
Makefile文件具體內(nèi)容如下:
.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/卧土,編譯選項(xiàng)配置保存并編譯
make menuconfig
Base system --->
example1
選項(xiàng)設(shè)置為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
根據(jù)OpenWrt安裝介紹,將內(nèi)核模塊和用戶態(tài)工具在板子上運(yùn)行尤莺,到這就簡(jiǎn)單了往下我就不貼了旅敷。
本文由http://www.wifidog.pro/2015/01/30/openwrt%E6%B7%BB%E5%8A%A0%E6%A8%A1%E5%9D%97.html 整理編輯,轉(zhuǎn)載請(qǐng)注明出處