Because Android system use linux as it's kernel, you can run your code in kernel mode by make and load a kernel module on Android.
This post simply explain how to build and load a kernel module on Android emulator.
Name of virtual device for default Android emulator is goldfish. So, in this post, I will call the kernel for Android emulator as goldfish kernel.
0. Environment
Development device / software version I used for this post is:
- ubuntu 12.04 desktop 64bit
- git 1.7.9.5
- AOSP master branch synced someday between 4.2.1_r1 and 4.2.2
1. Build & Use goldfish kernel
goldfish kernel doesn't support module in default. So, you should modify the setting to support module, build again with the setting, and use the rebuilt kernel.
1.1. Get kernel source code for Android emulator
$ git clone https://android.googlesource.com/kernel/goldfish
$ cd goldfish
$ git checkout android-goldfish-2.6.29
(You can pick which branch to checkout as you want. You can see which branch is available using $ git branch -r command)
1.2. Get prebuilt project
For cross-compile, you should get AOSP's prebuilt project.
$ git clone https://android.googlesource.com/platform/prebuilt
1.3. kernel build configuration setting
You can set linux kernel build setting as you want. The setting information is saved as .config file in source code directory. But, the source code you got in above step have no .config file.
Make default goldfish kernel build configuration file with the command below:
$ make goldfish_defconfig
Now, you can see .config file. Or, you can get the configuration of already running emulator with command below.
$ adb pull /proc/config.gz
$ gunzip config.gz
$ mv config .config
1.4. Modify configuration to support module
Open .config file you generated and add next lines:
CONFIG_MODULES=y
CONFIG_MODULES_FORCE_LOAD=y
CONFIG_MODULES_UNLOAD=y
CONFIG_MODULES_FORCE_UNLOAD=y
If you don't set CONFIG_MODULE_UNLOAD, you can't unload moudle after loading. Be careful.
1.5. Build goldfish kernel
You can just follow s.android.com's same contents. In summary,
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make -j2
The <path to prebuild we downloaded above> should be alternated with the path to prebuild you got from 1.2 section.
After build process, You can see the kernel image, zImage file.
1.6. Use the built kernel with emulator
You can specify which kernel the emulator will use with next command:
$ emulator -kernel <path of kernel image>
-show-kernel and -verbose option may be helpful in some way.
2. Build Android kernel module
Now, let's code and build kernel module for Android.
2.1. Source code for kernel module
Make simple file like below in name android_module.c
I will not explain about how the code works precisely in this post because it's not this post's object.
Simply, the source code will print "Hello android kernel..." when loaded on, "Goodbye android kernel..." message when it unloaded from kernel on kernel log.
You may change the content of source code as you want.
Simply, the source code will print "Hello android kernel..." when loaded on, "Goodbye android kernel..." message when it unloaded from kernel on kernel log.
You may change the content of source code as you want.
2.2. Write Makefile for the kernel module build process
Write Makefile with content below:
-C option at 4, 7line should point the path to kernel source code.
2.3. Build kernel module
Just set ARCH, CROSS_COMPILE environmental variable and do make. It's essentially same with kernel compile command.
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make
If build success, you can see android_module.ko file generated.
3. Load kernel module
$ adb push android_module.ko /data/
$ adb shell
(Now, you are on Android's shell)
# insmod /data/android_module.ko
You can see the message Hello android kernel... on kernel log by using command like dmesg.
4. Unload kernel module
$ adb shell
# rmmod android_module
You can see the message Goodbye android kernel... on kernel log by using command like dmesg, too.
* You may encounter vermagic problem during module loading on some case.
In the case, article below may be helpful.
http://dry-kiss.blogspot.kr/2013/07/android-linux-module-vermagic.html
References:
http://linuxclues.blogspot.kr/2010/05/build-compile-linux-kernel-android.html
http://stackoverflow.com/questions/6282669/how-do-you-create-a-loadable-kernel-module-for-android
http://s.android.com/
댓글 없음:
댓글 쓰기