编译Android内核模块

Android内核模块的编译与普通Linux内核的编译稍有不同
1.下载android内核

#如果git需要使用代理可以使用如下命令
export https_proxy="http://192.168.1.105:8087"

git clone https://android.googlesource.com/kernel/goldfish.git

上面得到的是主线上的内核源代码,现在我们需要适用于模拟器用的内核,因此,我们需要checkout goldfish版本:
cd  goldfish
git branch -a   //查看branch

git checkout remotes/origin/android-goldfish-3.4

2.下载交叉编译工具
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6
设置环境变量
export PATH=$PATH:/home/alan/workspace/arm_toolchain/arm-eabi-4.6/bin
export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-

3、现在是要得到android的内核编译参数的配置文件的时候了,该文件需要从已经安装好的android的模拟器
配置文件在模拟器的"/proc/config.gz"目录,pull出来,解压,将config文件改名为.config,拷贝到内核代码goldfish目录下。

这是在goldfish目录下make,可编译内核

http://source.android.com/source/building-kernels.html

下面讲解编译内核模块

1.在内核源码drives目录下添加hello目录,内含hello.c Kconfig Makefile文件
hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init (void)
{
	printk("hello linux kernel\n");
	return 0;
}

static void hello_exit (void) 
{
    printk("bye..lwave from the kernel\n");
}

module_init(hello_init);
module_exit(hello_exit);


Kconfig
config HELLO
tristate "Helllo module test created by lz"  
default m
help
	test for adding driver to menuconfig


Makefile
KERNELDIR :=/home/alan/workspace/android_kernel/goldfish
PWD :=$(shell pwd)
ARCH=arm
CROSS_COMPILE=arm-eabi-
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
obj-$(CONFIG_HELLO) +=hello.o


1.通过给下面的Kconfig文件再加上下面的代码,可使hello项出现在配置菜单中:

(1)在arch/arm/Kconfig文件中 menu "Device Drivers" endmenu之间添加  source "drivers/hello/Kconfig"(这个文件好像没有menu "Device Drivers" endmenu,要自己写)

menu "Device Drivers"
source "drivers/hello/Kconfig"
endmenu


(2)在drivers/Kconfig文件中 menu "Device Drivers" endmenu之间添加 source "drivers/hello/Kconfig"

2.修改Drivers目录下的Makefile文件,添加如下行:
obj-$(CONFIG_HELLO) += hello/ 
当CONFIG_HELLO为y或m时,使系统能找到hello驱动的makefile

以上过程完成便将hello模块添加至内核menuconfig菜单, 注意格式与上下文保持一致

这是在goldfish目录下make,则模块在
drivers/hello/目录的hello.ko

猜你喜欢

转载自asdf314159265.iteye.com/blog/2017766