Pack Unpack Images
Table of Contents

boot.img

1) to extract

$ abootimg -x ../boot.img 
writing boot image config in bootimg.cfg
extracting kernel in zImage
extracting ramdisk in initrd.img
$ ls -l
total 4332
-rw-rw-r-- 1 master master     151 Nov 11 20:45 bootimg.cfg
-rw-rw-r-- 1 master master  351311 Nov 11 20:45 initrd.img
-rw-rw-r-- 1 master master 4077132 Nov 11 20:45 zImage

2) to further expand ramdisk:

mkdir ramdisk
cd ramdisk
gunzip -c ../initrd.img | cpio -i

// note the initrd.img in fact is a initrd.cpio.gz file. that can unzipped, then cpio.

-a) $file ./initrd.img
./initrd.img: gzip compressed data, from Unix
-b) mv ./initrd.img to ./initrd.gz 
-c) gzip -d ./initrd.gz will generate a single file initrd
-d) $file ./initrd
./initrd: ASCII cpio archive (SVR4 with no CRC)
-e) cpio -i -F ./initrd
will finally generate all files.

3) make changes

4) you need to delete bootsize property line in bootimg.cfg as our kernel image can be larger than the one from the factory image.

cd ..
sed -i '/bootsize/d' ./bootimg.cfg

5) compress back the files into a ramdisk
approach 1)

mkbootfs ramdisk | gzip > ramdisk-new.img

approach2)

find . | cpio -o -H newc | gzip > ../newramdisk.cpio.gz

note: if you need to add new binary files, the file mod permission needs a special handling, see special image handling

6) create the boot image:
- approach 1)

abootimg --create myboot.img -f bootimg.cfg -k zImage -r ramdisk-new.img

- approach 2)

mkbootimg --kernel zImage --ramdisk newramdisk.cpio.gz --base 0x40000000 --cmdline 'console=ttyS0,115200 rw init=/init loglevel=8' -o new_boot.img

how do you know the base address? from bootimg.cfg file, if kerneladdr = 0x80008000, that means base should be 0x80000000

- http://www.imajeenyus.com/computer/20130301_android_tablet/android/unpack_repack_recovery_image.html
- http://boundarydevices.com/hacking-ram-disks/
- http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

system.img

http://forum.xda-developers.com/showthread.php?t=1588461

place you system.img and the 2 binaries in one directory, and make sure the binaries have exec permission.
Part 1 - mount the filesystem

1. mkdir sys
2. ./simg2img system.img sys.raw
3. sudo mount -t ext4 -o loop sys.raw sys/

Then you have all your system partition mouned in 'sys' and you can modify whatever you want in 'sys'. For example de-odex apks and framework jars.
Part 2 - create a new flashable system image.

1. sudo ./make_ext4fs -s -l 512M -a system new.img sys/
2. sudo umount sys
3. rm -fr sys

Now you can simply type:

fastboot flash system new.img
$ tune2fs -l ../sys.raw | grep "Block size"
Block size:               4096
$ tune2fs -l ../sys.raw | grep "Block count"
Block count:              393216
$ echo $((393216 *4096))
1610612736

$ sudo ./make_ext4fs -s -l 1610612736 -a system new.img ../mnt/

http://blog.djodjo.org/?p=98
http://muzso.hu/2012/08/10/how-to-pack-and-unpack-system.img-and-userdata.img-from-an-android-factory-image

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License