#!/usr/bin/env python3 """Replace the appended DTB in a 2720 Flip Android boot image. The stock aboot finds the appended DTB through the 32-bit value at kernel+0x2c. Keeping that pointer and rebuilding the page-aligned kernel/ramdisk layout lets us ship a corrected DTB without rebuilding the whole 1.6 GiB rootfs image. This tool ONLY touches the standalone Android boot.img. The raw image written to `userdata` is an MBR disk image whose first partition is an ext2 pmOS_boot filesystem; never write an Android boot.img at raw offset 1 MiB. To update the boot.img/DTB/extlinux.conf inside that filesystem use sync-beatles-bootfs.py. """ import argparse import os import struct import sys FDT_MAGIC = b"\xd0\x0d\xfe\xed" BOOT_MAGIC = b"ANDROID!" DTB_OFFSET_FIELD = 0x2c ID_OFFSET = 576 ID_SIZE = 32 def align(value, page): return (value + page - 1) // page * page def parse_header(data): if len(data) < 48 or data[:8] != BOOT_MAGIC: raise ValueError("not an Android boot image") fields = struct.unpack_from("<8s8I", data, 0) return { "kernel_size": fields[1], "kernel_addr": fields[2], "ramdisk_size": fields[3], "ramdisk_addr": fields[4], "second_size": fields[5], "second_addr": fields[6], "tags_addr": fields[7], "page_size": fields[8], } def repack(old, dtb): hdr = parse_header(old) page = hdr["page_size"] kernel_off = page dtb_rel = struct.unpack_from(" %d bytes" % (len(old), len(new))) return 0 if __name__ == "__main__": sys.exit(main())