#!/usr/bin/env python3 """Assemble an N810 MBR image while proving partition 1 is unchanged.""" import argparse, hashlib, os, struct SECTOR = 512 P1_START, P1_SECTORS = 16, 1_966_064 P2_START, P2_SECTORS = 1_966_080, 1_966_080 IMAGE_SECTORS = 3_932_160 def digest(path): h = hashlib.sha256() with open(path, "rb") as f: for block in iter(lambda: f.read(1024 * 1024), b""): h.update(block) return h.hexdigest() def main(): p = argparse.ArgumentParser() p.add_argument("--partition1", required=True) p.add_argument("--partition2", required=True) p.add_argument("--output", required=True) a = p.parse_args() if a.output.startswith("/dev/"): p.error("device output is forbidden") expected = {a.partition1: P1_SECTORS * SECTOR, a.partition2: P2_SECTORS * SECTOR} for path, size in expected.items(): if os.path.getsize(path) != size: p.error(f"{path}: expected {size} bytes") if os.path.exists(a.output): p.error(f"refusing to overwrite {a.output}") p1_hash = digest(a.partition1) mbr = bytearray(SECTOR) mbr[440:444] = bytes.fromhex("be23308f") def entry(offset, ptype, start, sectors): mbr[offset:offset+16] = struct.pack("