#!/usr/bin/env python3 """Pack a static ARM64 BusyBox and init.sh into a gzip newc initramfs. With --os-dir the archive also carries everything needed to assemble a custom Android from pinned images on userdata: the extent maps, the partition table and the qkx-* helper scripts. """ import argparse import gzip import stat import struct from pathlib import Path def archive(entries): data = bytearray() for ino, (name, mode, contents, major, minor) in enumerate(entries, 1): name = name.encode() + b"\0" fields = (ino, mode, 0, 0, 1, 0, len(contents), 0, 0, major, minor, len(name), 0) data += b"070701" + "".join(f"{x:08x}" for x in fields).encode() + name data += b"\0" * (-len(data) % 4) data += contents data += b"\0" * (-len(data) % 4) data += b"\0" * (-len(data) % 512) return bytes(data) def check_static_arm64(path): b = path.read_bytes() if b[:6] != b"\x7fELF\x02\x01" or struct.unpack_from(" a.max_size: raise SystemExit(f"initramfs is {len(data)} bytes; the staging layout " f"allows {a.max_size}") a.output.parent.mkdir(parents=True, exist_ok=True) a.output.write_bytes(data) print(f"{a.output}: {len(data)} bytes") if __name__ == "__main__": main()