Skip to content

Building a simple Android kernel module

February 17, 2011

Now that I compiled a kernel with loadable module support it’s time to test it out. To do so I created a directory ./drivers/testmod, and inside that I created testmod.c and a Makefile.

testmod.c

#include <linux/kernel.h>
#include <linux/module.h>

static int tm_init(void)
{
     printk(KERN_INFO "testmod module being loaded.\n");
     return 0;
}

static void tm_exit(void)
{
     printk(KERN_INFO "testmod module being unloaded.\n");
}

module_init(tm_init);
module_exit(tm_exit);

MODULE_AUTHOR("tja");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello, Android Kernel!");

Makefile:

ifeq ($(KERNELRELEASE),)

KERNELDIR=~/Research/android/android_kernel
PWD := $(shell pwd)

default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-eabi- EXTRA_CFLAGS=$(EXTRACFLAGS)  modules

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
else

obj-m :=    testmod.o

endif

Note that arm-eabi- is already in my PATH, so it will be valid for CROSS_COMPILE. Now we can install the module to an emulator running the custom kernel.

$ adb push testmod.ko /data
$ adb shell
# cd /data
# insmod testmod.ko

Then, looking back to terminal where the emulator was launched:

testmod module being loaded.

Now for some reason I can’t remove the module.

# rmmod testmod.ko
rmmod: delete_module 'testmod' failed (errno 38)

From → Android, Technology

Leave a Comment

Leave a comment