#!/usr/bin/env python3 """Turns a built g1v0 image into what the app ships: app/g1/firmware/g1v0-image.ts and g1v0-own-bytes.ts. A built image embeds two of Even's files (scouter_kit g1_firmware/README.md, "Build the image"), which must not be published: its network-core image and its TouchBar table. Those bytes are cut out here, and the phone puts them back from Even's own download (app/g1/firmware-builder.ts). The result has the same SHA-256 as the build, so the MCUboot signature made at build time stays valid. tools/g1_firmware_bundle.py WORK/build/g1v0-v0563/zephyr/app_update.bin --firmware-dir ~/src/scouter_kit/g1_firmware --firmware-dir holds stock_images.txt and, under work/firmware/1.6.6/, Even's unpacked files (scripts/fetch_stock.sh). scripts/update_g1_firmware.sh builds the image in ScouterKit and runs this on it. """ import argparse import base64 import hashlib import json import struct import subprocess import zlib from pathlib import Path ROOT = Path(__file__).resolve().parent.parent OUT = ROOT / "app" / "g1" / "firmware" STOCK_VERSION = "1.6.6" TABLE = "aw9320x_table" def sha(data): return hashlib.sha256(data).hexdigest() def stock_pins(firmware_dir): rows = [line.split() for line in (firmware_dir / "stock_images.txt").read_text().splitlines() if line.strip() and not line.startswith("#")] return {row[1]: row[2:] for row in rows if row[0] == STOCK_VERSION} def only_place(image, part, label): at = image.find(part) if at < 0: raise SystemExit(f"{label} is not in the image: was it built with Even's {STOCK_VERSION}?") if image.find(part, at + 1) >= 0: raise SystemExit(f"{label} is in the image twice") return at def image_version(image): if image[:4] != bytes.fromhex("3db8f396"): raise SystemExit("not an MCUboot image") major, minor, revision = struct.unpack_from("