#!/usr/bin/env python3 """ sparse_split.py — split a raw partition image into multiple Android *sparse* images, each small enough for one fastboot `download` (i.e. under the device's max-download-size). Why: partitions like `super` are multiple GB, but a device only accepts max-download-size bytes per download (often a few hundred MB). fastboot normally re-chunks large images into several sparse files and flashes each to the same partition; the MAOS web installer flashes pre-split chunks instead of implementing sparse in the browser. Each output file is a full sparse image for the WHOLE partition, but contains real data only for its slice and marks the rest DONT_CARE, so flashing them in order reconstructs the partition. Sparse format ref: AOSP system/core/libsparse (sparse_format.h). USAGE: sparse_split.py [--max-bytes N] [--block-size 4096] Emits .0.simg, .1.simg, ... and prints each filename on stdout. NOTE: authored against the documented format; validate a real flash on a device before relying on it. """ import argparse import os import struct import sys SPARSE_MAGIC = 0xED26FF3A CHUNK_RAW = 0xCAC1 CHUNK_FILL = 0xCAC2 CHUNK_DONT_CARE = 0xCAC3 FILE_HDR_SZ = 28 CHUNK_HDR_SZ = 12 def write_header(f, block_size, total_blocks, total_chunks): f.write( struct.pack( " 0 else 0 chunks += 1 # the RAW slice chunks += 1 if end < total_blocks else 0 write_header(out, bs, total_blocks, chunks) if start > 0: write_dont_care(out, start) src.seek(start * bs) to_read = (end - start) * bs data = src.read(min(to_read, size - start * bs)) # Zero-pad the final block if the image isn't block-aligned. if len(data) % bs != 0: data += b"\x00" * (bs - (len(data) % bs)) # If this slice runs past EOF (last file), pad remaining blocks with zeros. expected = (end - start) * bs if len(data) < expected: data += b"\x00" * (expected - len(data)) write_raw(out, data, bs) if end < total_blocks: write_dont_care(out, total_blocks - end) outputs.append(out_path) print(out_path) idx += 1 start = end if not outputs: print("no output produced", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()