OTA (2) - diff and patch

(I) boot.img

boot.img is updated in recovery mode

OTA script to update /boot:

package_extract_file("boot.img", "/dev/block/by-name/boot")

the so-called “boot.img” is raw android boot image.

(II) system.img

system.img is updated in recovery mode

OTA script to update /system

block_image_update("/dev/block/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat");

Tools used for system.img

blockimgdiff is google’s diff tool to handle system image in the OTA package, original sparse system.img will be splitted into 3 files: system.new.dat, system.patch.dat, system.transfer.list. This save some space in OTA package.
source location: build/tools/releasetools/blockimgdiff.py

sdat2img is a 3rd party tool to assemble the files (system.new.dat, system.patch.dat, system.transfer.list) into system.img

(III) recovery.img

recovery.img is updated in normal mode by an auto-generated script: /system/bin/install-recovery.sh

Tools used

bsdiff is a general tool to generate binary diff between 2 files.
imgdiff can generate diff between image files, which means source and target file must have same chunk structures.
applypatch is used for android recovery image OTA, it can be used to verify hashcode of an eMMC partition, or apply a patch to a partition. Full partition hash sum is a realiable way to check its integrity.

source code of applypatch & imgdiff is here:
bootable/recovery/applypatch/applypatch.c
bootable/recovery/applypatch/imgdiff.c

source code of bsdiff:
external/bsdiff

Usage

Generate binary patch file:
bsdiff

Generate script to apply patch:
build/tools/releasetools/common.py

Example:

Complete example for updating recovery.img in normal mode:
./build/tools/releasetools/make_recovery_patch <target_file_dir> <patch_file_and_script_dir>
The above command will generate both patch file and updater script.

More detailed usage can be found in private makefiles