How do you create a loadable kernel module for Android?

ε祈祈猫儿з 提交于 2019-12-02 15:58:42
Peter Teoh

Sorry for being late, but hope it helps:

http://tthtlc.wordpress.com/2011/12/29/how-to-write-a-kernel-module-on-android-sony-ericsson-xperia-arc-s/

I wrote and tested the code on my Sony Ericsson Xperia Arc S and it works (in general in should work for any Android phone).

Connecting via adb and USB, and “su” to root, the “lsmod” listed all the kernel module (in general, the article at http://developer.sonyericsson.com/wp/2011/05/06/how-to-build-a-linux-kernel/ gave a very good coverage of what to do in Linux kernel compilation for SonyEricsson phone):

lsmod
android_module 654 0 - Live 0x7f007000 (P)
sdio 16532 0 - Live 0x7f000000

“android_module” was the one I had inserted via insmod android_module.ko.

Here’s how to do it:

First the original program was copied from:

http://rechtzeit.wordpress.com/2011/03/21/77/

Or reproduced as follows:

android_module.c:

#include"linux/module.h"
#include"linux/kernel.h"
//replace the "" with angular brackets
int init_module(void)
{
  printk(KERN_INFO "Hello android kernel...\n");
  return 0;
}

void cleanup_module(void)
{
  printk(KERN_INFO "Goodbye android kernel...\n");
}

Makefile:

obj-m += android_module.o

all:
make -C /home/tteikhua/android/sony_ericsson_src/58/kernel/ M=$(PWD) modules
clean:
make -C /home/tteikhua/android/sony_ericsson_src/58/kernel/ M=$(PWD) clean

The directory where the kernel is located (“-C” above) is where I had downloaded the SonyEricsson kernel image from here:

http://developer.sonyericsson.com/wportal/devworld/downloads/download/30a2181182tarbz2?cc=gb&lc=en

And the following command will use the Makefile from above:

ARCH=arm CROSS_COMPILE=/opt/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi- make

As shown above, the cross compiler I had used is from CodeSourcery.

And after insmod android_module.ko you can see the debugging message in dmesg output:

 <6>[11184.063507] Hello android kernel...
 <7>[11619.209655] msmrtc_timeremote_set_time: 11/29/2011 10:09:36 (04)
 <6>[11619.210418] RPC CALL -- TOD TIME UPDATE: ttick = 404244221
 <6>[11619.210418] stamp=52910543933046785, freq = 0
 <7>[11619.211578] msmrtc_tod_proc_result: 12/29/2011 10:09:36 (03)
 <6>[11619.211578] rs30000048 rs30000048.262144: setting system clock to 2011-12-29 10:09:36 UTC (1325153376)
 <6>[11662.112365] device rmnet0 left promiscuous mode
 <6>[11662.112579] device rmnet0 entered promiscuous mode
 <6>[11669.958221] device rmnet0 left promiscuous mode
 <6>[11669.958435] device rmnet0 entered promiscuous mode
 <7>[11698.181060] msmrtc_timeremote_set_time: 11/29/2011 10:10:55 (04)
 <6>[11698.187622] RPC CALL -- TOD TIME UPDATE: ttick = 406832008
 <6>[11698.187652] stamp=52910548228014081, freq = 0
 <7>[11698.193939] msmrtc_tod_proc_result: 12/29/2011 10:10:55 (03)
 <6>[11698.194030] rs30000048 rs30000048.262144: setting system clock to 2011-12-29 10:10:55 UTC (1325153455)
 <6>[11814.442901] bq27520 0-0055: bq27520_handle_soc_worker() capacity=97 (100) flags=0x229 ctrl_status=0x28b soh_state=0x3, valid=1
 <6>[11984.057373] Goodbye android kernel...

And the “Goodbye” is when rmmod android_module is executed. In between are debugging message from other components in the kernel.

I think I've red that emulator kernel does not support module, so you must compile also the kernel with modules support.

I hope that this link will help you

alkalinity

There are two things you'll need to do. Firstly, create an emulator kernel that supports loadable kernel modules.

There's a comprehensive post here covering the process of setting up the emulator kernel.

There are 2 parts of the instructions to alter:

  1. I find it easiest to keep the kernel within the repo area (in this case, ~/WORKING_DIRECTORY), like so:

    cd ~/WORKING_DIRECTORY
    mkdir kernel
    cd kernel
    git clone git://android.git.kernel.org/kernel/common.git
    
  2. The branch information is no longer correct, however, so where it says:

    git checkout -t origin/android-goldfish-2.6.29 -b goldfish
    

use this instead:

    git checkout -t archive/android-gldfish-2.6.29 -b goldfish

As you've found out, the kernel by default is not configured with loadable module support. Enable before compiling. After, this step:

make ARCH=arm goldfish_defconfig

do this:

make ARCH=arm menuconfig

A text-based menu will show up. Use the Up/Down arrow keys to navigate, Space to select an option, Enter to expand a menu (menus have --->) at the end of the line, and Esc-Esc to go back a menu.

  1. Select "Enable loadable module support" by scrolling down and hitting Space
  2. Hit Esc-Esc to exit, and hit Enter to save the new configuration

Now build. Using your setup, it would be:

make ARCH=arm CROSS_COMPILE=/home/<myuser>/WORKING_DIRECTORY/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-

Now that that's done you can do the second part, which is making a loadable kernel module. You were almost there, but because you didn't have the kernel source for the emulator, it didn't work. Just take the makefile you have and change KERNEL_DIR to:

KERNEL_DIR ?= /home/<myuser>/WORKING_DIRECTORY/kernel/common
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!