Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Thursday, July 7, 2011

Compiling custom kernel modules for HTC Evo 2.3.3 Gingerbread

Some of my current research on Android forensics and security requires the loading of custom kernel modules.  In my previous post I detailed the steps required to compile and load custom modules on a rooted phone.  I recently updated my rooted Evo to Android 2.3.3 Gingerbread and was frustrated to see that HTC has yet to release the updated kernel source code.

Using an adb shell I can see that the phone is running linux kernel 2.6.35.10.
joe@zuul:~$ adb shell
# uname -a
Linux localhost 2.6.35.10-gc0a661b #1 PREEMPT Tue Jun 14 23:58:32 CST 2011 armv7l GNU/Linux
Luckily, HTC has released the Gingerbread kernel source for another phone, the Desire HD, which runs the same kernel version.  Assuming that the the kernels from the two phones would be very similar, I tried my hand at compiling the module for my Evo with the Desire HD source.  It worked!

To compile a kernel module for the Evo with the Desire HD kernel source requires a few extra steps:

  • Download and unpack the Desire HD source code.
  • Modify Makefile at the root of the source.  Change line 4 to read: EXTRAVERSION = .10-gc0a661b
  • Follow the steps outlined in my previous post to export the config from the Evo, prepare the kernel sources, and compile the custom module
These steps worked for my phone and my module, but your mileage may vary.  It is unlikely that the kernels running on the two phones are identical, but hopefully the sources are similar enough to allow the functionality of your custom modules to work.  Of course HTC will eventually release the official kernel sources for the Evo 4G, but it's anyone's guess when that will be.

UPDATE (8/9/11):// HTC has released the EVO 2.3.3 GB Kernel sources.  See this post for details.

Wednesday, January 5, 2011

Guide to Compiling Custom Kernel Modules in Android

I've spent the better part of today trying to figure out how to compile and load a custom kernel modules in android to aid me in my research.  It has been in entirely frustrating experience, as there is almost no documentation on the topic that I can find.  Below you will find my attempt at a guide.  Hopefully this will help save someone else the hassle.

PREREQUISITES

Disclaimer:  This list may be incomplete, since I've not tried it on a fresh install. Please let me know if I've missed anything.
  • Install the general android prereqs found here. 
  • Download and un(zip|tar) the android NDK found here.
  • Download and un(zip|tar) the android SDK found here.
  • Download and untar the kernel source for your device.  This can usually be found on the website of your device manufacturer or by a quick Google search.
  • Root your phone.  In order to run custom kernel modules, you must have a rooted phone.
  • Plug your phone into your computer.

PREPARING YOUR KERNEL SOURCE

First we must retrieve and copy the kernel config from our device.
$ cd /path/to/android-sdk/tools
$ ./adk pull /proc/config.gz
$ gunzip ./config.gz
$ cp config /path/to/kernel/.config
Next we have to prepare our kernel source for our module.
$ cd /path/to/kernel
$ make ARCH=arm CROSS_COMPILE=/path/to/android-ndk/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin/arm-eabi- modules_prepare
PREPARING YOUR MODULE FOR COMPILATION

We need to create a Makefile to cross-compile our kernel module.  The contents of your Makefile should be similar to the following:
obj-m := modulename.o
KDIR := /path/to/kernel
PWD := $(shell pwd)
CCPATH := /path/to/android-ndk/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin
default:
$(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-eabi- -C $(KDIR) M=$(PWD) modules
COMPILING AND INSTALLING YOUR MODULE
$ cd /path/to/module/src
$ make
$ cd /path/to/android-sdk/tools/
$ ./adb push /path/to/module/src/modulename.ko /sdcard/modulename.ko
RUNNING YOUR MODULE
$ cd /path/to/android-sdk/
$ ./adb shell
$ su
# insmod /sdcard/modulename.ko
CONCLUSION

Ineviditibly you'll run into some errors that you will need to sort out, as I did with both my droid 2 and evo devices.  Feel free to post in the comments if you run into any major problems, and I'll try my best to help you out.