// 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). //! //! The reference's `bootrom.py`. What Rust forced: `BootROMError` is an error string. use std::path::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. pub const EXPECTED: [(&str, &str, usize, &str); 3] = [ ("m68k", "32X_G_BIOS.BIN", 256, "dbebd76a448447cb6e524ac3cb0fd19fc065d944"), ("master", "32X_M_BIOS.BIN", 2048, "1e5b0b2441a4979b6966d942b20cc76c413b8c5e"), ("slave", "32X_S_BIOS.BIN", 1024, "4103668c1bbd66c5e24558e73d4f3f92061a109a"), ]; /// A boot ROM is missing, or is not the dump the board knows (the reference's /// `BootROMError`). pub type BootROMError = String; /// `image` if it is the expected `kind` ("m68k", "master", "slave"), else the error. pub fn check(kind: &str, image: Vec) -> Result, BootROMError> { let (_, name, size, sha1) = EXPECTED.iter().find(|e| e.0 == kind).expect("a kind of boot ROM"); let digest = sha1_smol::Sha1::from(&image).digest().to_string(); if image.len() != *size || digest != *sha1 { return Err(format!("{name}: {} bytes, SHA-1 {digest}; expected {size} bytes, SHA-1 {sha1}", image.len())); } Ok(image) } fn find(directory: &Path, name: &str) -> Result { let entries = std::fs::read_dir(directory).map_err(|e| format!("{}: {e}", directory.display()))?; for entry in entries.flatten() { if entry.file_name().to_string_lossy().to_uppercase() == name { return Ok(entry.path()); } } Err(format!("{name} not found in {}", directory.display())) } /// The three images, each checked. #[derive(Clone, Debug, PartialEq, Eq)] pub struct BootROMs { pub m68k: Vec, pub master: Vec, pub slave: Vec, } impl BootROMs { pub fn load(directory: impl AsRef) -> Result { let directory = directory.as_ref(); let mut images = Vec::new(); for (kind, name, _size, _sha1) in EXPECTED { let path = find(directory, name)?; let bytes = std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?; images.push(check(kind, bytes)?); } let slave = images.pop().expect("three"); let master = images.pop().expect("three"); let m68k = images.pop().expect("three"); Ok(BootROMs { m68k, master, slave }) } }