// SPDX-License-Identifier: MIT //! The boot ROM: bring your own, found by hash; no replacement ROM is shipped //! (`bootrom.py`). //! //! docs/cartridge.md, "The boot path", and C16. The 512-byte ROM inside Mikey //! answers `$FE00-$FFFF` (bit 2 of MAPCTL replaces `$FE00-$FFF7` with RAM, bit 3 //! the vectors `$FFFA-$FFFF`; `$FFF8` is RAM and `$FFF9` is MAPCTL itself). //! //! Two images circulate, differing only in the byte at `$FFF9`, which a running //! machine cannot read back as ROM: MAME's `lynxa.bin` (everyone else's //! `lynxboot.img`) and MAME's `lynx.bin`. Both behave identically; the first //! is the reference. //! //! Differences forced by Rust: `BootRomError` (an exception) is the `Err` of a `Result`. use std::io::Read; use std::path::{Path, PathBuf}; use sha1::{Digest, Sha1}; use crate::romsets; pub const REFERENCE_SHA1: &str = "e4ed47fae31693e016b081c6bda48da5b70d7ccb"; // lynxboot.img, MAME lynxa.bin pub const MAME_LYNX_BIN_SHA1: &str = "de60f2263851bbe10e5801ef8f6c357a4bc077e6"; // MAME lynx.bin: byte $FFF9 differs pub const BOOT_ROM_SHA1S: [&str; 2] = [REFERENCE_SHA1, MAME_LYNX_BIN_SHA1]; pub const BOOT_ROM_SIZE: usize = 512; /// The user's own copy, then MAME's lynx.zip under $MAME_ROMPATH (by hash); read in place. pub const SEARCH_PATHS: [&str; 1] = ["~/.local/share/lynx-python/lynxboot.img"]; fn sha1_hex(data: &[u8]) -> String { Sha1::digest(data) .iter() .map(|b| format!("{b:02x}")) .collect() } /// Return the image if it is a known Lynx boot ROM, else an error. pub fn check(data: &[u8]) -> Result, String> { if data.len() != BOOT_ROM_SIZE { return Err(format!( "a Lynx boot ROM is {BOOT_ROM_SIZE} bytes, not {}", data.len() )); } let digest = sha1_hex(data); if !BOOT_ROM_SHA1S.contains(&digest.as_str()) { return Err(format!("unknown boot ROM (SHA-1 {digest})")); } Ok(data.to_vec()) } fn expand_user(path: &str) -> PathBuf { match path.strip_prefix("~/") { Some(rest) => crate::cart::eeprom::home().join(rest), None => PathBuf::from(path), } } /// The boot ROM from `path`, `$LYNX_BOOT_ROM`, the search paths or /// `$MAME_ROMPATH/lynx.zip` (the reference image preferred); None if absent. pub fn find(path: Option<&Path>) -> Result>, String> { let mut candidates: Vec = path.map(|p| vec![p.to_path_buf()]).unwrap_or_default(); if let Ok(v) = std::env::var("LYNX_BOOT_ROM") { if !v.is_empty() { candidates.push(PathBuf::from(v)); } } candidates.extend(SEARCH_PATHS.iter().map(|p| PathBuf::from(*p))); for candidate in candidates { let p = expand_user(&candidate.to_string_lossy()); if p.is_file() { let data = std::fs::read(&p).map_err(|e| e.to_string())?; return check(&data).map(Some); } } for directory in romsets::mame_rompaths() { let archive = directory.join("lynx.zip"); if !archive.is_file() { continue; } let file = std::fs::File::open(&archive).map_err(|e| e.to_string())?; let mut z = zip::ZipArchive::new(file).map_err(|e| e.to_string())?; let mut found: Vec<(String, Vec)> = Vec::new(); for i in 0..z.len() { let mut member = z.by_index(i).map_err(|e| e.to_string())?; let mut data = Vec::new(); member.read_to_end(&mut data).map_err(|e| e.to_string())?; if data.len() == BOOT_ROM_SIZE { found.push((sha1_hex(&data), data)); } } for digest in [REFERENCE_SHA1, MAME_LYNX_BIN_SHA1] { if let Some((_, data)) = found.iter().find(|(d, _)| d == digest) { return Ok(Some(data.clone())); } } } Ok(None) }