Wednesday, October 31, 2012

Rebuild a new kernel for ZTE-V9


I have bought this bargain device for a while but have not done something until this month.

So start from building a kernel on Mac OS X and flash it.

Build the kernel:

1. Get the kernel source code from ZTE website - google it.
2. Unzip the source
3. Install Android SDK and NDK.
4. Run the following command from platform-tools folder of Android SDK after connecting the device to the Mac:
./adb pull /proc/config.gz ~
This will copy the config file from the device to the Mac.
5. gunzip ~/config.gz
This command unzips the config file to config.
6. Copy config to .config in the unzip kernel source code folder (root folder).
7. Make a file called elf.h and put the content on the bottom of this post to it. Then put elf.h in the kernel source code folder (root folder).
8. Make a bash file called build.sh in kernel source code folder (root folder) with the following string:
make ARCH=arm SUBARCH=arm CROSS_COMPILE=~/android-ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi- HOSTCFLAGS="-I/opt/local/include/ -I."
Here, it assumes the NDK is installed in the home folder.
If there is an error msg saying: the symlink include/asm points to asm-x86 but asm-arm was expected, delete include/asm folder.
9. Run ./build.sh. If there is an error saying: ERROR: the symlink include/asm points to asm-x86 but asm-arm was expected, just delete linux/asm which points to asm-x86 folder.
10. Wait for the new kernel image

If everything goes well, there will be arch/arm/boot/zImage which is the compressed kernel.

Repackage the new kenrel:

Now build two tools for making an kernel image - mkbootimg and unpackbootimg. There maybe some pre-built versions available on web but it is quite easy to build them from the source code which is available from the master repository of Google Android source code - or google for an alternative.

Then get the current running kernel:

1. Run this command on V9: cat /dev/mtd/mtd2 > /mnt/sdcard/boot.img
2. Run this on Mac: ./adb pull /mnt/sdcard/boot.img .
3. Then: ./unpackbootimg -i boot.img

There will be couple files:
-rwxr-xr-x  1 Ray  staff  5242880 17 Oct 21:47 boot.img
-rw-r--r--  1 Ray  staff        9 23 Oct 14:32 boot.img-base
-rw-r--r--  1 Ray  staff        9 23 Oct 14:34 boot.img-base.txt
-rw-r--r--  1 Ray  staff        1 23 Oct 14:32 boot.img-cmdline
-rw-r--r--  1 Ray  staff        1 23 Oct 14:36 boot.img-cmdline.txt
-rw-r--r--  1 Ray  staff        5 23 Oct 14:32 boot.img-pagesize
-rw-r--r--  1 Ray  staff        5 23 Oct 14:35 boot.img-pagesize.txt
-rw-r--r--  1 Ray  staff   232299 23 Oct 14:32 boot.img-ramdisk.gz
-rw-r--r--  1 Ray  staff  3171056 23 Oct 14:32 boot.img-zImage

Copy arch/arm/boot/zImage to current folder.

Finally run:
mkbootimg –kernel zImage –ramdisk boot.img-ramdisk.gz –cmdline “(boot.img-cmdline.txt)” –base (boot.img-base.txt) –pagesize (boot.img-pagesize.txt) -o ./newboot.img

So newboot.img is the new kernel image.

Flash the new kernel:
1. ./adb push ./newboot.img /mnt/sdcard/newboot.img
2. ./adb shell
3. cat /dev/zero > /dev/mtd/mtd2
4. flash_image boot /mnt/sdcard/newboot.img
5. reboot

That's all.

If everything is fine, V9 should be back in normal.

Btw, to confirm the new image is installed, change CONFIG_LOCALVERSION in .config to something different before running ./build.sh so the kernel version is different from Settings page on V9.

=====================

#ifndef _LINUX_ELF_H
#define _LINUX_ELF_H

#include

#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_ARM_NONE        0
#define R_ARM_PC24        1
#define R_ARM_ABS32       2
#define R_MIPS_NONE       0
#define R_MIPS_16         1
#define R_MIPS_32         2
#define R_MIPS_REL32      3
#define R_MIPS_26         4
#define R_MIPS_HI16       5
#define R_MIPS_LO16       6

#endif /* _LINUX_ELF_H */