编译修改安卓内核烧录到nexus4上,并从app层到hal层再到kernel层调用新的内核驱动

用的ubuntu14.04  android 4.4(KRT16S)  nexus4

1.download kernel code
git config --global http.sslVerify false
git clone https://aosp.tuna.tsinghua.edu.cn/kernel/msm.git //not very big
git checkout 3.4.0-perf-g6aa1c72

2.
myenvsetup.sh
export CC=/home/aaa/Desktop/kitkat/mydroid/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/arm-eabi-
export CROSS_COMPILE=/home/aaa/Desktop/kitkat/mydroid/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/arm-eabi-

export ARCH=arm
export SUBARCH=arm

cd msm
make clean
make help | grep mako
make mako_defconfig
make menuconfig //not necessary
make -j4
for this case zImage (not with name dtb) will be created. it located at msm/arch/arm/boot/zImage

copyImage.sh
cp -f ./msm/arch/arm/boot/zImage ./../device/lge/mako-kernel/zImage
cp -f ./msm/arch/arm/boot/zImage ./../device/lge/mako-kernel/kernel //back up it before operation

exit this terminal because env variables

3.
cd kitkat/mydroid
source ./build/envsetup.sh
lunch
13
make bootimage //a new boot.img will be created
reboot phone to fastboot and connect to pc
fastboot flash boot ./out/target/product/mako/boot.img
fastboot reboot
enter phone's setting will see new kernel

4.
create file word_count.c in any directory

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "wordcount"

int forBack;
unsigned char tmp[4];
int justhere;


static ssize_t word_count_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){
forBack = 123456;
tmp[0] = forBack >> 24;
tmp[1] = forBack >> 16;
tmp[2] = forBack >> 8;
tmp[3] = forBack;
justhere = copy_to_user(buf, (void*) tmp, 4);
printk("my kmodule read");
return count;
}

static struct file_operations dev_fops = {
.read = word_count_read
};

static struct miscdevice misc = {
.name = DEVICE_NAME,
.fops = &dev_fops
};

static int word_count_init(void){
int ret;
ret = misc_register(&misc);
printk("my kmodule init");
return ret;
}

static void word_count_exit(void){
misc_deregister(&misc);
printk("my kmodule exit");
}

module_init(word_count_init);
module_exit(word_count_exit);
MODULE_AUTHOR("my");
MODULE_LICENSE("GPL");


create Makefile (this is it's file name) in the same directory
obj-m := word_count.o
KERNELDIR = /usr/src/linux-headers-3.13.0-170-generic/
PWD = $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD)
    rm -rf *.o *.order *.mod.* *.symvers

execute make command in this directory and ko file will be created
we can test it on linux

5.
copy the word_count.c to msm/drivers/char

modify msm/drivers/Makefile
add this
obj-$(CONFIG_WORD_COUNT)    += word_count.o

modify msm/drivers/Kconfig
add this
config WORD_COUNT
    bool "word_count driver"
    help
        This is my driver path is /dev/wordcount

source the myenvsetup.sh
cd msm
make clean //clean won't change .config file so no need to make mako_defconfig every time after clean
make mako_defconfig
make menuconfig //path is ...device...->...char...->word...
make -j4 //if make stop don't clean, just make again and again until logs come out

copy the zImage

now the kernel has module, we need to modify hal and then flash them all

6.
modify hardware/libhardware/modules/helloworld/helloworld.c
add these
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int fd;
int readRet;
unsigned char buf[4];
int retNum;
int closeRet;

and chengfa changed to this, no change on it's params

static int chengfa(struct helloworld_device_t* dev, int a ,int b, int* c){
    fd = open("/dev/wordcount",O_RDWR);
    if(fd == -1){
        *c = 99;
        return 0;
    }
    readRet = read(fd,buf,4);
    retNum = (((int)(buf[0])) << 24) |
        (((int)(buf[1])) << 16) |
        (((int)(buf[2])) << 8) |
        ((int)(buf[3]));

    //*c = a * b;
    *c = retNum;
    closeRet = close(fd);
    return 0;
}
end of modify


cd kitkat/mydroid
source ./build/envsetup.sh
lunch
13
mmm hardware/libhardware/modules/helloworld/
make snod

reboot phone to fastboot and connect to pc
fastboot -w flashall

7.
when phone started up
adb shell
su
ls -l /dev/wordcount
chmod 666 /dev/wordcount

now use testhal
tap button 2 and we will see 123456
we did it!

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值