I'm learning how to develop a kernel module in Openwrt. I make a hello world try. The package directory tree is:
khelloworld/
Makefile
src/
khelloworld.c
Makefile
The Openwrt Makefile:
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=khelloworld
PKG_RELEASE:=1
PKG_VERSION:=1.0
include $(INCLUDE_DIR)/package.mk
define KernelPackage/khelloworld
SUBMENU:=HELLO WORLD MODULES
TITLE:=khelloworld
MAINTAINER:=Nobody
MENU:=1
FILES:=$(PKG_BUILD_DIR)/$(PKG_NAME).$(LINUX_KMOD_SUFFIX)
endef
EXTRA_KCONFIG:= \
CONFIG_HELLO_MOD=m
EXTRA_CFLAGS:= \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \
#MAKE_OPTS:= \
# EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
# $(EXTRA_KCONFIG)
define Build/Prepare
# Copy sources
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)" \
EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
$(EXTRA_KCONFIG) \
modules
endef
$(eval $(call KernelPackage,khelloworld))
The source Makefile:
obj-m += khelloworld.o
all:
$(MAKE) -C "$(LINUX_DIR)" \
$(MAKE_OPTS) \
modules
The C helloworld source:
#include <linux/module.h>
#include <linux/kernel.h>
int init_module( void ) {
printk( KERN_INFO "Hello World KERNEL!!!\n" );
return 0;
}
void cleanup_module( void ) {
printk( KERN_INFO "Goodbye World KERNEL!!!\n" );
}
This module compiles well with this command: make package/khelloworld/compile
Then I installed it in my openwrt using this command: opkg install kmod-khelloworld-xxxxxx.ipk
the install is done without errors. but the problem dmesg doesn't display the expected message of the init function.
I thought that insmod is missing. So I added the following install macro to the Makefile just before "$(eval $(call KernelPackage,khelloworld))"
define Build/install
insmod $(PKG_BUILD_DIR)/khelloworld.ko
endef
but that didn't solve the problem. Has anyone an idea about this problem and how I can correct it?
(Last edited by OmarKallel on 8 Oct 2017, 04:43)