openwrt 添加自定义内核模块

openwrt添加内核模块步奏:

1、创建需要的文件及文件夹:

CUR_DIR:openwrt根目录
cd package
mkdir demo
cd demo
mkdir src
touch Makefile
cd src
touch demo.c
touch Kconfig
touch Makefile

2、编写demo文件夹第一级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.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=demo
PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define KernelPackage/demo
	SUBMENU:=Other modules			#此模块在make menuconfig中的位置
	TITLE:=Support Module for demo		
#	DEPENDS:=@TARGET_octeon			此模块依赖
	AUTOLOAD:=$(call AutoLoad,81,demo)	#内核自动挂载
	FILES:=$(PKG_BUILD_DIR)/demo.ko
	KCONFIG:=
endef


EXTRA_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/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Compile
	$(MAKE) -C "$(LINUX_DIR)" \
		$(MAKE_OPTS) \
		modules
endef

	
$(eval $(call KernelPackage,demo))

注释:AUTOLOAD:定义了内核模块开机自动挂载的动作,$(call AutoLoad,81,example),表示当系统启动时名叫“example”的内核模块会在顺序为第81的位置加载到系统中,不必每次启动系统后手动的去insmod加载模块。如果正常,本模块ipk包制作好并安装到目标系统中后,会在目标系统的/etc/modules.d/目录下创建一份名叫81-example的加载序列文件,其内容为example。而本ipk安装后会在/lib/moudules/$(KERNEL-VERSION)/目录下放置example.ko文件。而如果本软件包包含了多个内核模块文件,比如多个“.ko”文件,那么在这个位置就要加入多个模块名,模块名之间以“空格”隔开,如下例: 
  SUBMENU:=Other modules
  TITLE:=exfat driver
  FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX)
  AUTOLOAD:=$(call AutoLoad,82,exfat_core exfat_fs)
  KCONFIG:=
3、进入src目录,编写demo.c、Kconfig、Makefile文件

demo.c:

#include 
#include 

static int __init hello_init(void)
{
	printk("Hello Word\n");
	return 0;
}

static void __exit hello_exit(void)
{
	printk("Exit\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangLiMao");
Kconfig:

config DEMO
  tristate "This is a Module_DEMO"
  default n
  help
   This is a DEMO, for debugging kernel model.
   If unsure, say N.
Makefile:

obj-(CONFIG_DEMO) := demo.o

4、命令make menuconfig 进入,选中模块

$make menuconfig
  Kernel modules —>
    Other modules —>
      kmod-example

5、编译make package/demo/compile















你可能感兴趣的:(openwrt 添加自定义内核模块)