#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the CD-i firmware MAME knows: system ROMs, slave/servo MCU ROMs, DVC ROMs. Asks the installed MAME binary (`mame -listxml`, a closed box that is only run) which ROMs each CD-i machine needs, then hashes the members of the matching zips under MAME_ROMPATH (SHA-1, in memory) and reports which are present. Nothing is copied or extracted to disk, and no firmware ever enters the repository. MAME_ROMPATH=... python -I scripts/census_bios.py MAME_ROMPATH=... python -I scripts/census_bios.py --markdown """ from __future__ import annotations import argparse import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path MACHINES = ("cdimono1", "cdimono2", "cdi910", "cdi490a", "cdibios", "gpi1200", "quizard", "quizard2", "quizard3", "quizard4") def rompath_dirs() -> list[Path]: value = os.environ.get("MAME_ROMPATH") if not value: sys.exit("set MAME_ROMPATH") return [Path(part) for part in value.split(";") if part] def zip_hashes(path: Path) -> dict[str, str]: hashes: dict[str, str] = {} with zipfile.ZipFile(path) as archive: for member in archive.infolist(): if member.is_dir(): continue digest = hashlib.sha1() with archive.open(member) as handle: for block in iter(lambda: handle.read(1 << 20), b""): digest.update(block) hashes[digest.hexdigest()] = member.filename return hashes def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--mame", default="mame") parser.add_argument("--markdown", action="store_true") args = parser.parse_args() dirs = rompath_dirs() found: dict[str, tuple[str, str]] = {} rows = [] for machine in MACHINES: xml = subprocess.run([args.mame, "-listxml", machine], capture_output=True, text=True, check=False).stdout if not xml: continue root = ET.fromstring(xml) for node in root.findall("machine"): if node.get("name") != machine: continue for rom in node.findall("rom"): rows.append((machine, rom.get("bios") or "", rom.get("region") or "", rom.get("name"), int(rom.get("size") or 0), rom.get("sha1") or "", rom.get("status") or "good")) for zipname in {machine, node.get("romof") or machine}: for folder in dirs: candidate = folder / f"{zipname}.zip" if candidate.exists(): for sha1, member in zip_hashes(candidate).items(): found.setdefault(sha1, (candidate.name, member)) present = 0 if args.markdown: print("| machine | bios | region | ROM | size | SHA-1 | MAME status | present |") print("| --- | --- | --- | --- | ---: | --- | --- | --- |") for machine, bios, region, name, size, sha1, status in rows: where = found.get(sha1) present += bool(where) mark = f"yes ({where[0]})" if where else "NO" if args.markdown: print(f"| {machine} | {bios} | {region} | `{name}` | {size} | `{sha1}` | {status} | {mark} |") else: print(f"{machine:9s} {bios:12s} {region:8s} {size:7d} {sha1} {status:8s} {mark} {name}") print(f"\n{present} of {len(rows)} ROM entries present by SHA-1") return 0 if __name__ == "__main__": sys.exit(main())