# SPDX-License-Identifier: MIT """Sega's three 32X boot ROMs: bring your own, checked by SHA-1 (Aubrey's decision 3). The board never fetches, bundles or copies them (README, "Boot ROMs"; ``.gitignore``); the user names a directory and this module reads the three files from it and refuses any whose SHA-1 is not the No-Intro / MAME dump's (docs/start-here.md, "Boot ROMs", verified against the user's copies in milestone 0, docs/validation.md). The 68000 one (256 bytes) is the vector table the 32X maps at $000000-$0000FF once ADEN is set (Manual 3.1); the master's (2 KB) and slave's (1 KB) are their SH-2s' (milestone 2). """ from __future__ import annotations import hashlib from dataclasses import dataclass from pathlib import Path #: Name, size and SHA-1 of each, as docs/start-here.md lists them; the file names are the #: ones the user's mirror uses (``32X_G_BIOS.BIN`` ...), matched without regard to case. EXPECTED = { "m68k": ("32X_G_BIOS.BIN", 256, "dbebd76a448447cb6e524ac3cb0fd19fc065d944"), "master": ("32X_M_BIOS.BIN", 2048, "1e5b0b2441a4979b6966d942b20cc76c413b8c5e"), "slave": ("32X_S_BIOS.BIN", 1024, "4103668c1bbd66c5e24558e73d4f3f92061a109a"), } class BootROMError(ValueError): """A boot ROM is missing, or is not the dump the board knows.""" def check(kind: str, image: bytes) -> bytes: """``image`` if it is the expected ``kind`` ("m68k", "master", "slave"), else :class:`BootROMError`.""" name, size, sha1 = EXPECTED[kind] digest = hashlib.sha1(image).hexdigest() if len(image) != size or digest != sha1: raise BootROMError( f"{name}: {len(image)} bytes, SHA-1 {digest}; expected {size} bytes, SHA-1 {sha1}" ) return image def _find(directory: Path, name: str) -> Path: for path in directory.iterdir(): if path.name.upper() == name: return path raise BootROMError(f"{name} not found in {directory}") @dataclass(frozen=True) class BootROMs: """The three images, each checked.""" m68k: bytes master: bytes slave: bytes @classmethod def load(cls, directory: str | Path) -> BootROMs: directory = Path(directory) images = { kind: check(kind, _find(directory, name).read_bytes()) for kind, (name, _size, _sha1) in EXPECTED.items() } return cls(**images)