Unpack & Repack Android system.img & data.img
I Get packing software
- Download the https://android.googlesource.com/platform/system/core repository:
cd "$AOSP_ROOT"
[ ! -d "platform/system" ] && mkdir -p platform/system
cd platform/system
git clone https://android.googlesource.com/platform/system/core - Check out a revision of your choice:
cd core
git checkout android-4.2.1_r1 - Compile
simg2img
:
cd libsparse
gcc -o simg2img -Iinclude simg2img.c sparse_crc32.c backed_block.c output_file.c sparse.c sparse_err.c sparse_read.c -lz
II Get unpack software
- Download the https://android.googlesource.com/platform/system/extras repository:
cd "$AOSP_ROOT/platform"
[ ! -d "system" ] && mkdir system
cd system
git clone https://android.googlesource.com/platform/system/extras - Check out a revision of your choice:
cd extras
git checkout android-4.2.1_r1 - Compile
make_ext4fs
:
cd ..
gcc -o make_ext4fs -Icore/libsparse/include extras/ext4_utils/make_ext4fs_main.c extras/ext4_utils/make_ext4fs.c extras/ext4_utils/ext4fixup.c extras/ext4_utils/ext4_utils.c extras/ext4_utils/allocate.c extras/ext4_utils/contents.c extras/ext4_utils/extent.c extras/ext4_utils/indirect.c extras/ext4_utils/uuid.c extras/ext4_utils/sha1.c extras/ext4_utils/wipe.c core/libsparse/backed_block.c core/libsparse/output_file.c core/libsparse/sparse.c core/libsparse/sparse_crc32.c core/libsparse/sparse_err.c core/libsparse/sparse_read.c -lz
III Problem with Android 4.3
Compiling make_ext4fs from Android v4.3 sources
The compilation of
If you try to compile it on linux, the following might be sufficient:
I didn't have the chance to test it (tried to compile on a Mac, but gave up after hitting the SELinux problem), but it seems to be the complete
simg2img
is pretty much the same as for 4.2.*. But the compilation of make_ext4fs
got a lot more complicated. First of all, it seems that this cannot be compiled (at least out-of-the-box) on a Mac anymore because of SELinux dependencies.If you try to compile it on linux, the following might be sufficient:
gcc -o make_ext4fs -Isystem/core/libsparse/include -Isystem/core/include -Iexternal/libselinux/include -lz external/libselinux/src/android.c external/libselinux/src/booleans.c external/libselinux/src/callbacks.c external/libselinux/src/check_context.c external/libselinux/src/freecon.c external/libselinux/src/getenforce.c external/libselinux/src/init.c external/libselinux/src/label.c external/libselinux/src/label_android_property.c external/libselinux/src/label_file.c system/extras/ext4_utils/make_ext4fs_main.c system/extras/ext4_utils/make_ext4fs.c system/extras/ext4_utils/ext4fixup.c system/extras/ext4_utils/ext4_utils.c system/extras/ext4_utils/allocate.c system/extras/ext4_utils/contents.c system/extras/ext4_utils/extent.c system/extras/ext4_utils/indirect.c system/extras/ext4_utils/uuid.c system/extras/ext4_utils/sha1.c system/extras/ext4_utils/wipe.c system/extras/ext4_utils/crc16.c system/core/libsparse/backed_block.c system/core/libsparse/output_file.c system/core/libsparse/sparse.c system/core/libsparse/sparse_crc32.c system/core/libsparse/sparse_err.c system/core/libsparse/sparse_read.c
I didn't have the chance to test it (tried to compile on a Mac, but gave up after hitting the SELinux problem), but it seems to be the complete
gcc
line for make_ext4fs.-Icore/include -DANDROID
IV Unpack system.img and mount for modify
#!/bin/bash
if [ ! -f system.raw.img ]; then
./simg2img system.img system.raw.img
echo "unpack OK"
else
echo "system.raw.img already exist."
fi
./simg2img system.img system.raw.img
echo "unpack OK"
else
echo "system.raw.img already exist."
fi
mount -t ext4 -o loop system.raw.img /mnt/android_system
V Repack system.img
#!/bin/bash
image_size=$(tune2fs -l system.raw.img | awk 'BEGIN{s=1}{if ($1 == "Block") s *= $3;}END{print s}')
echo "image size ${image_size}"
PATH="$PATH:$(pwd)" ./make_ext4fs -s -l ${image_size} -a system new_system.img /mnt/android_system/