It is mentioned in the Openwrt package Makefile chapter, to add a chapter to add a custom module, here are two simple examples to look at, in fact, we look at the existing examples can probably be imitated.
1. Add openwrt application module
As follows, we add a hello module under the package, with the following files
linye@ubuntu:~/14.07/package/hello$ tree
.
├── Makefile
└── src
├── hello.c
└── Makefile
1 directory, 3 files
The outermost Makefile is Openwrt's compiled Makefile. The specific meaning of each definition can be viewed in the Openwrt package Makefile chapter.
include $(TOPDIR)/rules.mk
PKG_NAME:=hello
PKG_VERSION:=1.01
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)_$(PKG_VERSION)
include $(INCLUDE_DIR)/package.mk
define Package/$(PKG_NAME)
SUBMENU:=utils
CATEGORY:=Base system
TITLE:=hello test
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
TARGET_CFLAGS += -D_GNU_SOURCE
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/sbin
endef
$(eval $(call BuildPackage,$(PKG_NAME)))
Makefile inside src is used to compile C function
CFLAGS += -std=gnu99 -fstrict-aliasing -Iinclude -Wall -Werror
CFLAGS += -DRSA_VERIFY
all: hello
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
zcheck: hello.o
$(CC) $(LDFLAGS) -o $@ $^
.PHONY: clean
clean:
rm -rf *.o hello
hello.c is the main function
#include <stdio.h>
int main()
{
printf("hello world\r\n");
}
After the code is written, use make menuconfig to find the hello option, check it
Base system --->
utils --->
<*> hello...
Then use the package compilation method make package/hello/compile V=99
to compile the module
After compiling, you can build_
see an extra hello_1.01
module below
linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/hello_1.01$ tree
.
├── hello
├── hello.c
├── ipkg-mtk_1004kc
│ └── hello
│ ├── CONTROL
│ │ └── control
│ └── usr
│ └── sbin
│ └── hello
└── Makefile
5 directories, 5 files
Copy hello to the board to run
root@openwrt:/tmp# chmod +x hello
root@openwrt:/tmp# ./hello
hello world
If we first add scripts / configuration files to this module, the general approach is to create a new files directory, and then add correspondingly according to our rootfs structure.
as follows
linye@ubuntu:~/14.07/package/hello$ tree
.
├── files
│ ├── etc
│ │ ├── config
│ │ │ └── hello
│ │ └── init.d
│ │ └── autohello.sh
│ └── sbin
├── Makefile
└── src
├── hello.c
└── Makefile
6 directories, 5 files
Then copy these files to the corresponding folders in the Makefile
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/sbin
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_DATA) ./files/etc/config/hello $(1)/etc/config/hello
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/etc/init.d/autohello.sh $(1)/etc/init.d/hello
endef
Then use make V = 99 to recompile. After the compilation is complete, you can see in the rootfs that the files we need have run to their respective directories, and the process can start automatically
linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/rootfs$ find ./ -name hello
./usr/sbin/hello
./etc/config/hello
./etc/init.d/hello
2. Add the kernel module
The framework of adding a kernel module is similar to that of adding an application. The direct internal implementation functions are different.
linye@ubuntu:~/14.07/package/hello_kernel$ tree
.
├── Makefile
└── src
├── hello_kernel.c
└── Makefile
1 directory, 3 files
The content of the outermost Makefile is as follows, the difference lies in the bottom$(eval $(call KernelPackage,$(PKG_NAME)))
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=hello_kernel
PKG_VERSION:=1.01
include $(INCLUDE_DIR)/package.mk
define KernelPackage/$(PKG_NAME)
SUBMENU:=Kernel Modules
CATEGORY:=Base system
TITLE:=netfilter debug
DEPENDS:=kmod-ipt-conntrack
FILES:=$(PKG_BUILD_DIR)/hello_kernel.ko
AUTOLOAD:=$(call AutoLoad,66,hello_kernel)
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
ARCH="$(LINUX_KARCH)" \
SUBDIRS="$(PKG_BUILD_DIR)" \
EXTRA_CFLAGS="$(BUILDFLAGS)" \
modules
endef
$(eval $(call KernelPackage,$(PKG_NAME)))
Makefile in src
obj-m += hello_kernel.o
hello_kernel.c in src
#include <linux/init.h> /* __init and __exit macroses */
#include <linux/kernel.h> /* KERN_INFO macros */
#include <linux/module.h> /* required for all kernel modules */
static int __init hello_init(void)
{
printk("hello install\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("hello uninstall\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("hello Debug Module");
After adding the above information, you can find the following options in make menuconfig
Base system --->
Kernel Modules --->
<*> kmod-hello_kernel.......
After compiling, under linux, you can see more hello_kernel module content
linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/linux-mtk_mt7621/hello_kernel-1.01$ tree
.
├── hello_kernel.c
├── hello_kernel.ko
├── hello_kernel.mod.c
├── hello_kernel.mod.o
├── hello_kernel.o
├── ipkg-mtk_1004kc
│ └── kmod-hello_kernel
│ ├── CONTROL
│ │ ├── control
│ │ └── postinst
│ ├── etc
│ │ └── modules.d
│ │ └── 66-hello_kernel
│ └── lib
│ └── modules
│ └── 3.10.49
│ └── hello_kernel.ko
├── Makefile
├── modules.order
└── Module.symvers
8 directories, 12 files
Copy the hello_kernel.ko file directly to the board for insmod installation
root@openwrt:/tmp# insmod hello_kernel.ko
root@openwrt:/tmp# lsmod | grep hello
hello_kernel 1888 0
root@openwrt:/tmp# rmmod hello_kernel
dmesg can see the kernel print information
[67961.350000] hello install
[67969.390000] hello uninstall