在 Emulator 里面用 insmod 安装 LKM 时,会报告错误,例如:
# insmod hello.ko
insmod: init_module 'hello.ko' failed (Function not implemented) 这是因为 Android SDK 里面自带的 Emulator 所用的 kernel 关闭了加载 LKM 的功能。要在 Emulator 里面开发和调试 LKM,必须自己重新编译 kernel. 编译方法可以参考 .NOTE: 如果是在 Mac OS X 里面编译,make 的时候可能会遇到以下错误:
HOSTCC scripts/mod/mk_elfconfig
scripts/mod/mk_elfconfig.c:4:17: error: elf.h: No such file or directory
这是因为 Mac 的 include 文件少了一个 elf.h
从网上(例如:http://www.rockbox.org/tracker/9006?getfile=16683)下载一个放到 scripts/mod 目录,并且修改 mod 目录里面引用了 elf.h 的两个文件就可以了。
编译好的新 kernel 假定是 zImage, 建议启动 emulator 的时候加上 -show-kernel 开关,这样可以把 LKM 用 printk() 输出的信息输出到 console 上,便于调试。例:emulator -kernel zImage -show-kernel -avd <AVD名字>
写一个简单的 Hello World 来测试一下:
- #include <linux/init.h>
- #include <linux/module.h>
- MODULE_LICENSE("Dual BSD/GPL");
- static int hello_init(void)
- {
- printk(KERN_INFO "Hello, world\n");
- return 0;
- }
- static void hello_exit(void)
- {
- printk(KERN_INFO "Goodbye, cruel world\n");
- }
- module_init(hello_init);
- module_exit(hello_exit);
交叉编译的 Makefile:
- KERNELDIR := /Users/quaful/Documents/Projects/360/kernel/
- PWD :=$(shell pwd)
- ARCH=arm
- CROSS_COMPILE=/Developer/android-ndk-r4b/build/prebuilt/darwin-x86/arm-eabi-4.4.0/bin/arm-eabi-
- CC=$(CROSS_COMPILE)gcc
- LD=$(CROSS_COMPILE)ld
- obj-m := hello.o
- modules:
- $(MAKE) -C $(KERNELDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules
- clean:
- rm *.o *.ko *.mod.c *.markers *.order *.symvers
把编译生成的 hello.ko 传到手机上,然后执行:
insmod hello.ko
在 kernel 的 console 输出中就可以看到 printk 的结果了。
Howto BUILD the LINUX KERNEL for the Android EMULATOR (Eclair version)
DOWNLOAD THE KERNEL SOURCE CODEFirst we download the kernel source code from
Within that page there are kernels for other platforms too. We choose to download kernel/common project from there.
$git clone git://android.git.kernel.org/kernel/common
We check which branch we have downloaded:
$git branch it shows * android-2.6.27, not the one we are searching for:To list all remote available branches:
$git branch -r origin/HEAD -> origin/android-2.6.27 origin/android-2.6.25 origin/android-2.6.27 origin/android-2.6.29 origin/android-2.6.32 origin/android-goldfish-2.6.27 origin/android-goldfish-2.6.29What does goldfish mean? (from android-kernel mail list)
Goldfish is the kernel hacked branch that supports the qemu based arm emulator for android, so it is the one we need.Download GOLDFISH kernel version
$git checkout --track -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29 $git branchandroid-2.6.27 * android-goldfish-2.6.29
RUNNING THE EMULATORWithin this link we will find how to get the android emulator, and launch it.
Building Android in Debian SidShowing the kernel version running in the emulator
$adb shell #cat /proc/versionLinux version 2.6.29-00261-g0097074 () (gcc version 4.4.0 (GCC) ) #14 Tue Feb 2 15:49:02 PST 2010
OBTAINING KERNEL CONFIGURATIONWe are going to obtain the kernel configuration .config file from within our running emulator.
$cd common # we enter in the kernel source directory.
$adb pull /proc/config.gz . # get compressed .config file from the emulator.$gunzip config.gz # uncompress it.
$cp config .config # rename it into .configNow you can edit .config file the way it suits you the most.
BUILDING AND COMPILING THE KERNELCROSS_COMPILE environment variable stores the path to the arm cross compiling toolchain. I use the one which comes with android source code.
$ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make
Executing make will build the kernel.
Last lines will show:
Kernel: arch/arm/boot/Image is ready Kernel: arch/arm/boot/zImage is ready
So we have obtained Image and zImage kernel binary files.
RUN THE EMULATOR USING THE NEW COMPILED KERNEL IMAGEWe need -kernel option:
$emulator -kernel /path/to/common/arch/arm/boot/zImage -show-kernel -verboseWe can check now the kernel version:
$adb shell # cat /proc/versionLinux version 2.6.29-00262-gb0d93fb () (gcc version 4.4.0 (GCC) ) #1 Sun May 2 14:27:31 CEST 2010
If we do not specify kernel option it usually uses the prebuilt one:
$emulator -show-kernel -verboseemulator: argv[01] = "-kernel"emulator: argv[02] = "/path/to/mydroid/prebuilt/android-arm/kernel/kernel-qemu"
ACTIVATING MODULE LOADING SUPPORT IN THE KERNELModule loading support is previously disabled in the kernel, if we want to load modules in the kernel we have to enable it:
edit .config file and set:
CONFIG_MODULES=y$ ARCH=arm CROSS_COMPILE=/path/to/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- make
I am asked about some options when executing make. I ask yes for module related options.
After compiling I see several modules have been built.
MODPOST 6 modules CC drivers/video/fb_sys_fops.mod.o LD [M] drivers/video/fb_sys_fops.ko CC drivers/video/syscopyarea.mod.o LD [M] drivers/video/syscopyarea.ko CC drivers/video/sysfillrect.mod.o LD [M] drivers/video/sysfillrect.ko CC drivers/video/sysimgblt.mod.o LD [M] drivers/video/sysimgblt.ko CC drivers/video/vfb.mod.o LD [M] drivers/video/vfb.koWe can upload the modules in the emulator and install them:
$adb push drivers/video/fb_sys_fops.ko /data $adb shell #insmod /data/fb_sys_fops.ko