Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading kernel module in Android kernel

I am listing my problem here.

I have a Google Nexus one a.k.a. "passion" phone with me. Fastboot and adb tools are installed in the phone. And the boot loader is unlocked.

My task: I have to add a linux kernel module to the Android kernel.

What I have done:

I followed the steps in http://source.android.com/source/initializing.html and downloaded the kernel for android-2.3.6_r1 (passion) and have built it. I am also able to flash it on the phone and the new android kernel also works fine. Now what I want is to modify the kernel and add my own kernel module and then flash it on the phone, so that the kernel on the phone is my modified kernel.

Now I have come across two approaches to do this.

1)

Cross Compile my kernel module with the android kernel and push it on the device with adb command. The Makefile I use in the kernel is as follows.

VERSION = 2
PATCHLEVEL = 3
SUBLEVEL = 6
EXTRAVERSION = -00054-g5f01537
obj-m += hello-1.o
KDIR=/home/apurva/android_dir
PWD := $(shell pwd)
all:
        make -C $(KDIR) ARCH=arm CROSS_COMPILE=/home/apurva/android_dir/prebuilt/linux-    x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- SUBDIRS=$(PWD) modules

clean:
        make -C $(KDIR) ARCH=arm CROSS_COMPILE=/home/apurva/android_dir/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- SUBDIRS=$(PWD) clean

Now this is not able to generate new hello-1.ko. I do not know why, I guess there is some problem with the VERSION, PATCHLEVEL, SUBLEVEL and EXTRAVERSION values. Are these necessary? I tried these value from android-2.3.6_r1 also but still it does not work. I am not sure what is this EXTRAVERSION value?

I even tried with the hello-1.ko generated from the compiler in my ubuntu. I pushed this hello-1.ko into the emulator with the following adb command.

/root/bin/src/out/host/linux-x86/bin/adb shell mount
/root/bin/src/out/host/linux-x86/bin/adb push hello-1.ko /data
/root/bin/src/out/host/linux-x86/bin/adb insmod /data/hello-1.ko

But that hello-1.ko is not able to insmod and I get the following error. insmod : Error in init_module() hello-1.ko function not implemented

Whereas the hello-1.c is quite simple:

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

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");
        return 0;
}

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

2)

The second approach of doing this can be placing my source files of the kernel module in the kernel directory of android. May be in the system directory or somewhere else and ask the make to build these source files also along with the other source. But I am not sure where to ask the make process to do so. I tried to do so in main.mk and created a Android.mk file in the source directory of my source files but it did not work. May be this is a better solution but I could not find any help on this.

After doing this, my kernel modules should be able to control the wnic (Wireless Network Interface device) of the android phone. It should be able to put the wnic in sleep mode and then wake it up after receiving command from my kernel module. If you have some pointers on how to do this, that will be a help. I have found that on Android it is controlled through wpa_supplicant private driver. Commands, like:

wpa_cli driver powermode 0 - auto 
wpa_cli driver powermode 1 - active

can do my task, but I am not sure since I have not tried. I have not reached that stage.

Please look into this and provide some help/guidance.

Thanks,

Apurva

like image 535
user1219784 Avatar asked Feb 24 '12 13:02

user1219784


People also ask

Where are kernel modules located Android?

Kernel modules from the SoC vendor that are required for full Android or Charger modes should be located in /vendor/lib/modules . If an ODM partition exists, kernel modules from the ODM that are required for full Android or Charger modes should be located in /odm/lib/modules .

Where kernel modules are stored?

They are located in /lib/modules or /usr/lib/modules and have had the extension . ko ("kernel object") since version 2.6 (previous versions used the .o extension). The lsmod command lists the loaded kernel modules.

What command do you use to load a kernel module?

To load a kernel module, we can use the insmod (insert module) command.


1 Answers

Kernel modules (KO's) are much easier to work with than a static kernel - as long as the kernel has enabled them. The easiest way to tell is do an "adb shell lsmod". Second is to see if the kernel .config has enabled CONFIG_MODULES=y and CONFIG_MODULE_UNLOAD=y. Lots of info on the web about linux KO development.

Hummm, you're close but it looks like the makefile is screwy. First try to build the hello KO on your host for unit test, then build on your target. Here's a sample makefile I use on an OMAP36xx running gingerbread:

# Makefile for trivial android kernel module

obj-m += mod_hello.o

CROSS_COMPILE=/opt/distros/ARM/bin/arm-none-linux-gnueabi-
TARG_KDIR ?= /opt/android/dal/nook_kernel

HOST_KDIR=/lib/modules/$(shell uname -r)/build

# target creates:
#  .<obj>.o: CC command line for the .o, including dependencies
#  .<obj>.mod.o.cmd: CC command line for the mod.o, including dependencies
#  .<obj>.ko.cmd: LD command line which links the .o and .mod.o to create the .ko
target:
    @echo "Make module for target arm"
    make -C $(TARG_KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules

host:
    @echo "Make module for host"
    make -C $(HOST_KDIR) M=$(PWD) modules

clean:
    @echo "cleaning target"
    make -C $(TARG_KDIR) M=$(PWD) clean
    @echo "cleaning host"
    make -C $(HOST_KDIR) M=$(PWD) clean
like image 181
dturvene Avatar answered Sep 21 '22 23:09

dturvene