[Tutorial] How to compile a kernel module outside the kernel

19 views
Skip to first unread message

한영빈

unread,
Feb 18, 2012, 11:51:08 PM2/18/12
to IAD developers
xda 에서 퍼온 커널 모듈 컴파일 튜토리얼 강좌입니다^^
http://forum.xda-developers.com/showthread.php?t=1236576
참고하세요 ㅎㅎ


I've decided to make a short tutorial and present the way I compile
kernel modules (outside the kernel sources).

I've built few kernel modules (governors - ineractive and smartass,
cifs, nls, etc) and I started receiving private messages asking how I
did it.

For kernel modules that come with the kernel itself - cifs / tun for
example - they just work if you compile the kernel and activate
correct config parameters.
Some other modules (such as the smartass governor that doesn't come
with the kernel) you compile outside the kernel source. However they
require changes since kernel does not export the symbols the module
needs to use - so you have to know what k_all_syms are needed, grab
them from the phone and update the kernel module.

So there will be changes there. However, the main steps are:

a) follow tutorials to get the kernel / android ndk to compile. People
seem able to do this.
b) then take the module you want (For example cpufreq_smartass.c from
here: http://pastebin.com/rR4QUCrk ) and copy it in a new folder on
the disk.
c) create a Makefile like the one below, but with your paths of
course:

Code:
KERNEL_DIR=/home/viulian/android_platform/kernel-2.1.A.0.435/kernel

obj-m := cpufreq_smartass.o
PWD := $(shell pwd)
default:
$(MAKE) ARCH=arm CROSS_COMPILE=/home/viulian/android_platform/
prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- -C $
(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean


d) execute make

Of course, the module source needs to be adjusted as you need to put
in the frequencies, and also update the k_all_syms pointers .. But you
can retrieve them from /proc/kallsyms on the device itself - just look
for the method name, and use the address you see in the log.

If you still can't get it to compile, try to compile a very basic
hello_world kernel module. I used the code below when testing:

Code:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */

MODULE_LICENSE("GPL");
MODULE_AUTHOR("viulian, 2011");
MODULE_DESCRIPTION("Demo module for X10i");

int init_module(void)
{
printk("<1>Hello world\n");

// A non 0 return means init_module failed; module can't be loaded.
return 0;
}


void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}

It is not perfect, but if you manage to insmod-it and check dmesg, you
will see "Hello world" written there.

One more thing, linux kernel is fussy about the module versions. Even
if nothing is changed between two kernel versions related to what a
module needs, is enough a small difference in module's modinfo value
to make the kernel to refuse the module.

For this, you need to trick your local kernel and adjust EXTRAVERSION
value in kernel's main Makefile to have the exact version of the one
on the device:

In X10 stock kernel (GB 2.3.3 release), the kernel version is
2.6.29-00054-g5f01537 visible in phone settings.

This means that the kernel on the phone will only accept modules that
are compiled for that exact version. But the kernel version is just a
string in the module .ko, so is a string comparison - the module might
work perfectly, but is not loaded.
There is luck though, the string value comes from a define in kernel's
Makefile, which you can change before you compile!

The Makefile in the kernel you are going to use to build the module
will have to include these lines at the top:

Code:
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 29
EXTRAVERSION = -00054-g5f01537

Other than that, it should work .. Expect phone reboots and difficulty
to debug if stuff goes wrong. Android kernel doesn't come with syslog
functionality, kernel prints are found in /proc/kmsg. Dmesg works, but
you can't execute if if phone reboots.
I usually had to keep another adb shell opening with 'cat /proc/kmsg'
which showed as much as possible from the module's outputs.

Happy compiling on your risk!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages