http://blog.csdn.net/ruiyiin/article/details/8850467
注意1:本文中代码直接拷贝可能会有问题,最好手动输入一遍
注意2:example/Makefile中的PKG_BUILD_DIR没有定义,不知道是否有影响,但是我添加了一句PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
注意3:如果需要编译多个文件,可以在src下面的Makefile增加"example-y := example2.o",就可以增加一个example2.c
关于内核模块的自动加载:
1 FILES:=$(PKG_BUILD_DIR)/fnsr-i2c$(PKG_MODULE_EXTENSION) 用于将制定的ko文件拷贝到root-fs/lib/modules目录下
2 AUTOLOAD:=$(call AutoLoad,99,fnsr-i2c) 应该是用于自动装载模块,在linux 2.6上工作正常,在linux 3.10不能生效,仍然需要手动输入insmod
可以在Makefile增加下面代码(在files下创建脚本my_module_start.sh)
define KernelPackage/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) files/my_module_start.sh $(1)/etc/init.d
endef
-
- 进入package目录,创建模块目录
cd mcp/branches/V1.1-beta1/mcp/package
mkdir example - 进入example目录,创建Makefile文件和代码路径
- cd example
- touch Makefile
- mkdir src
- # 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))
- cd src
- mkdir example
- cd example
- touch example.c Kconfig Makefile
- #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");
- 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
5.编译出的文件可以在主路径的以下路径找到
- ./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/
注:我们使用./build_dir/linux-octeon/example/example/example.ko
二、用户态工具添加方法
- cd mcp/branches/V1.1-beta1/mcp/package
- mkdir example1
- cd example1
- touch Makefile
- mkdir src
- #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))
- cd src
- touch example1.c Makefile
- #include <stdio.h>
- int main(void)
- {
- printf("Hello, world\n");
- return 0;
- }
- .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
- make menuconfig
- Base system --->
- example1
- 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安装介绍,将内核模块和用户态工具在板子上运行,到这就简单了往下我就不贴了。