在之前博文中(ubuntu 16.04 下 Android 4.4.4 源码编译到刷机 ) 提到如何编译android 源码。本篇介绍 如何编译 android 内核源码 并刷进系统。 两个是不同的源码。 android 源码中没有包含内核源码,可能是不同开源协议原因吧。分开不同项目。
android内核是linux内核。 android 源码包里面含有编译好的内核文件,所以即使不包含内核源码,一样可以刷机启动。 这篇文章要做的事情就是编译内核源代码,并替换掉自带的内核。
本篇文章大部分参考
我的环境是 ubuntu 16.04 64 位 桌面版。 android 手机为nexus 5 android 4.4.4 版本。
一、确定内核版本
要确定系统映像中使用的内核版本,请对内核文件运行以下命令:
-
dd if=kernel bs=1 skip=$(LC_ALL=C grep -a -b -o $'\x1f\x8b\x08\x00\x00\x00\x00\x00' kernel | cut -d ':' -f 1) | zgrep -a 'Linux version
对于 Nexus 5 (hammerhead),请运行以下命令:
-
dd if=zImage-dtb bs=1 skip=$(LC_ALL=C od -Ad -x -w2 zImage-dtb | grep 8b1f | cut -d ' ' -f1 | head -1) | zgrep -a 'Linux version
***执行命令要先进入到android源码 目录 cd device/lge/hammerhead-kernel
-
stevenrao@aosp-4.4.4_r1$ cd device/lge/hammerhead-kernel/
-
stevenrao@hammerhead-kernel$ dd if=zImage-dtb bs=1 skip=$(LC_ALL=C od -Ad -x -w2 zImage-dtb | grep 8b1f | cut -d ' ' -f1 | head -1) | zgrep -a 'Linux version'
-
Linux version 3.4.0-gd59db4e (android-build@vpbs1.mtv.corp.google.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Mon Mar 17 15:16:36 PDT 2014
-
Linux version CIFS VFS Client for Linux<7>%s: sess setup type %d
-
8386668+0 records in
-
8386668+0 records out
-
8386668 bytes (8.4 MB, 8.0 MiB) copied, 9.69736 s, 865 kB/s
或者直接登录到真机上查看
-
cat /proc/version
-
Linux version 3.4.0-gd59db4e (android-build@vpbs1.mtv.corp.google.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Mon Mar 17 15:16:36 PDT 2014
二、下载源代码
1、先要确定下载那个源码包
我的机器是nexus 5.对应的系统代号是 hammerhead ktu84p ;
从 中可以查看 各种版本
2、再到上查看
ok, 说明我的设备是msg代码分支
3、下载代码
先在android源码目录建立一个内核代码目录
使用git下载
-
git clone https://android.googlesource.com/kernel/msm
查看下载branch
-
git branch -a | 'grep 3.4
因为我的内核是3.4, 分支太多,过滤一下
-
remotes/origin/android-msm-flo-3.4-jb-mr2
-
remotes/origin/android-msm-flo-3.4-kitkat-mr0
-
remotes/origin/android-msm-flo-3.4-kitkat-mr1
-
remotes/origin/android-msm-flo-3.4-kitkat-mr2
-
remotes/origin/android-msm-flo-3.4-l-preview
-
remotes/origin/android-msm-flo-3.4-lollipop-mr1
-
remotes/origin/android-msm-flo-3.4-lollipop-mr1.1
-
remotes/origin/android-msm-flo-3.4-lollipop-mr1.2
-
remotes/origin/android-msm-flo-3.4-lollipop-release
-
remotes/origin/android-msm-flo-3.4-marshmallow
-
remotes/origin/android-msm-flo-3.4-marshmallow-mr1
-
remotes/origin/android-msm-flo-3.4-marshmallow-mr2
-
remotes/origin/android-msm-hammerhead-3.4-kitkat-mr1
-
remotes/origin/android-msm-hammerhead-3.4-kitkat-mr2
-
remotes/origin/android-msm-hammerhead-3.4-kk-fr1
-
remotes/origin/android-msm-hammerhead-3.4-kk-fr2
-
remotes/origin/android-msm-hammerhead-3.4-kk-r1
-
remotes/origin/android-msm-hammerhead-3.4-l-preview
-
remotes/origin/android-msm-hammerhead-3.4-lollipop-mr1
-
remotes/origin/android-msm-hammerhead-3.4-lollipop-mr1.1
-
remotes/origin/android-msm-hammerhead-3.4-lollipop-release
-
remotes/origin/android-msm-hammerhead-3.4-m-preview
-
remotes/origin/android-msm-hammerhead-3.4-marshmallow
-
remotes/origin/android-msm-hammerhead-3.4-marshmallow-mr1
-
remotes/origin/android-msm-hammerhead-3.4-marshmallow-mr2
-
remotes/origin/android-msm-hammerhead-3.4-marshmallow-mr3
-
remotes/origin/android-msm-mako-3.4-jb-mr1
-
remotes/origin/android-msm-mako-3.4-jb-mr1-fr
-
remotes/origin/android-msm-mako-3.4-jb-mr1-kgsl
-
remotes/origin/android-msm-mako-3.4-jb-mr1.1
-
remotes/origin/android-msm-mako-3.4-jb-mr2
-
remotes/origin/android-msm-mako-3.4-kitkat-mr0
-
remotes/origin/android-msm-mako-3.4-kitkat-mr1
-
remotes/origin/android-msm-mako-3.4-kitkat-mr2
-
remotes/origin/android-msm-mako-3.4-lollipop-mr1
-
remotes/origin/android-msm-mako-3.4-lollipop-mr1.1
-
remotes/origin/android-msm-mako-3.4-lollipop-release
好多3.4的内核。那我到底选择哪个呢,对照下面选择器代号,再看看前面,内核版本是 3.4.0-gd59db4e
so
三、编译内核
1、首先需要确认你的系统是32位还是64位。 我们知道,linux系统直接看可执行文件的信息即可,但是android上没有file 命令。因此随便copy一个/system/bin 下面一个可执行程序 下来查看一下因此的的系统是 32位系统。
-
stevenrao@msm$stevenrao@msm$ adb pull /system/bin/cat cat
-
/system/bin/cat: 1 file pulled. 1.5 MB/s (139040 bytes in 0.089s)
-
-
stevenrao@msm$ file cat
-
cat: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, stripped
2、编译
-
进入android源码目录(注意是android源码,不是内核源码)
-
$ cd /home/stevenrao/work/aosp-4.4.4_r1
-
$ source build/envsetup.sh
-
$ lunch #选择 aosp_hammerhead-userdebug 因为我的设备是 hammerhead
-
$ export ANDROID_SRC_PATH=/home/stevenrao/work/aosp-4.4.4_r1 #这里设置你自己的android 源码目录
-
$ export PATH=$ANDROID_SRC_PATH/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin:$PATH
-
$ export ARCH=arm
-
$ export SUBARCH=arm
-
$ export CROSS_COMPILE=arm-eabi-
-
$ cd kernel/msm/ #到内核源码目录中
-
$ make hammerhead_defconfig
-
$ make
如果编译遇到 Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at kernel/timeconst.pl line 373.
修改一下这个文件 的 373 行代码
-
if (!defined(@val)) {
-
-
改成
-
if (!@val) {
编译结果
-
OBJCOPY arch/arm/boot/zImage
-
Kernel: arch/arm/boot/zImage is ready
-
DTC arch/arm/boot/msm8974-hammerhead-rev-11.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-11j.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-10.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-c.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-b.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-bn.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-a.dtb
-
DTC arch/arm/boot/msm8974-hammerhead-rev-f.dtb
-
CAT arch/arm/boot/zImage-dtb
-
Kernel: arch/arm/boot/zImage-dtb is ready
-
make[1]: Nothing to be done for 'arch/arm/boot/dtbs'.
四、编译bootimage
前面只是编译完内核文件,但是还不能直接刷到真机上去,真机的内核文件为 bootimage 。 包含zImage-dtb
1、再次进入到 android 源码目录。注意是android 源码,不是内核源码
2、设置环境变量,指定前面编译 zImage-dtb 路径
-
export TARGET_PREBUILT_KERNEL=/home/stevenrao/work/aosp-4.4.4_r1/kernel/msm/arch/arm/boot/zImage-dtb
3、编译
成功输出
-
PRODUCT_COPY_FILES device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml ignored.
-
Copy: out/target/product/hammerhead/kernel
-
Target boot image: out/target/product/hammerhead/boot.img
-
out/target/product/hammerhead/boot.img maxsize=23519232 blocksize=135168 total=8912896 reserve=270336
五、刷机
刷机就比较简单,进入编译后的 bootimage目录
-
$ cd /home/stevenrao/work/aosp-4.4.4_r1/out/target/product/hammerhead
$ adb reboot bootloader
# fastboot flash boot boot.img
target reported max download size of 1073741824 bytes
sending 'boot' (8704 KB)...
OKAY [ 0.490s]
writing 'boot'...
OKAY [ 0.741s]
finished. total time: 1.231s
阅读(5189) | 评论(0) | 转发(0) |