1. 要先具配 android kernel code base, 而且要至少build過一次
這樣才會產生 out底下的資料
2. 任意找個位置新增目錄
寫個Makefile與hello.c
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
### MODIFY THESE VARIABLES TO FIT YOUR ENVIRONMENT ### obj-m = hello.o ANDROID_CODE_BASE = /home/codebase/xxxx ### DO NOT MODIFY THE FOLLOWING PART UNLESS YOU KNOW WHAT YOU'RE DOING ### KERNEL_PATH = $(ANDROID_CODE_BASE)/out/target/product/ventana/obj/KERNEL TARGET_ARCH = arm define kernel-make make -C $(KERNEL_PATH) ARCH=$(TARGET_ARCH) \ CROSS_COMPILE=$(ANDROID_CODE_BASE)/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- \ M=$(PWD) endef all: $(kernel-make) modules clean: $(kernel-make) clean |
hello.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* hello-2.c - Demonstrating the module_init() and module_exit() macros. This is the * preferred over using init_module() and cleanup_module(). */ #include <linux/module.h> // Needed by all modules #include <linux/kernel.h> // Needed for KERN_ALERT #include <linux/init.h> // Needed for the macros static int hello_2_init(void) { printk(KERN_ALERT "Hello, world 2\n"); return 0; } static void hello_2_exit(void) { printk(KERN_ALERT "Goodbye, world 2\n"); } module_init(hello_2_init); module_exit(hello_2_exit); |
3. 然後在妳開啟的目錄底下
make
4. 就會開啟編譯了, 編譯完成後會產生
hello.mod.c
hello.mod.o
hello.ko
hello.o
5. 如果要傳入DUT的話, 就要配合adb tool囉
- 傳入DUT
adb push hello.ko /sdcard - Insert Kernel module
adb shell insmod /sdcard/hello.ko - Remove kernel module
adb shell rmmod hello.ko