// SPDX-License-Identifier: MIT //! The BIOS socket: bring your own Atari ROM, found by SHA-1 and read in place. //! //! Open decision 5: Atari's own BIOS only (the 4-port CO19156 or revision A //! CO19156A). Nothing is ever copied or written: zip archives are read through //! memory. Search order: an explicit path, `$A5200_BIOS`, files and zips under //! `~/.local/share/a5200-python/bios/`, then the ROM sets named by //! `$NO_INTRO_DIR` and `$MAME_ROMPATH` (docs/start-here.md "The BIOS"). //! //! A transcription of a5200-python's `bios.py` (at cf82d44). The user folder is the //! reference's (`a5200-python`), so both boards find the same files. Rust-forced //! differences: `find_bios` returns `Result<(Vec, String), BiosNotFound>`; candidates are //! collected into a `Vec` (Python's generator). use std::io::{Cursor, Read}; use std::path::{Path, PathBuf}; use sha1::{Digest, Sha1}; use crate::machine::bios_name; pub fn user_place() -> PathBuf { let home = std::env::var_os("HOME") .map(PathBuf::from) .unwrap_or_default(); home.join(".local/share/a5200-python/bios") } /// The user folder, then the romsets named by the family's environment /// variables (`NO_INTRO_DIR`, `MAME_ROMPATH`), each skipped when unset. pub fn default_places() -> Vec { let mut places = vec![user_place()]; if let Some(dir) = std::env::var_os("NO_INTRO_DIR").filter(|v| !v.is_empty()) { places.push( PathBuf::from(dir) .join("Atari - Atari 5200") .join("[BIOS] Atari 5200 (USA).zip"), ); } if let Some(dir) = std::env::var_os("MAME_ROMPATH").filter(|v| !v.is_empty()) { let rompath = PathBuf::from(dir); places.push(rompath.join("a5200.zip")); // merged or non-merged places.push(rompath.join("a5200a.zip")); } places } #[derive(Debug, Clone)] pub struct BiosNotFound(pub String); impl std::fmt::Display for BiosNotFound { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) } } impl std::error::Error for BiosNotFound {} fn _candidates(place: &Path, out: &mut Vec<(String, Vec)>) { if place.is_dir() { let mut children: Vec = match std::fs::read_dir(place) { Ok(entries) => entries.filter_map(|e| e.ok().map(|e| e.path())).collect(), Err(_) => return, }; children.sort(); for child in children { _candidates(&child, out); } return; } let Ok(meta) = std::fs::metadata(place) else { return; }; if !meta.is_file() || meta.len() > 4 * 1024 * 1024 { return; } let Ok(data) = std::fs::read(place) else { return; }; if data.starts_with(b"PK\x03\x04") { let Ok(mut archive) = zip::ZipArchive::new(Cursor::new(data)) else { return; }; for index in 0..archive.len() { let Ok(mut file) = archive.by_index(index) else { continue; }; if file.size() == 2048 { let mut bytes = Vec::new(); if file.read_to_end(&mut bytes).is_ok() { out.push((format!("{} :: {}", place.display(), file.name()), bytes)); } } } } else if data.len() == 2048 { out.push((place.display().to_string(), data)); } } fn sha1_hex(data: &[u8]) -> String { let mut hasher = Sha1::new(); hasher.update(data); hasher .finalize() .iter() .map(|b| format!("{b:02x}")) .collect() } /// (the 2K image, where it was found) for the BIOS with this SHA-1. pub fn find_bios(sha1: &str, path: Option<&Path>) -> Result<(Vec, String), BiosNotFound> { let mut places = Vec::new(); if let Some(path) = path { places.push(path.to_path_buf()); } if let Some(bios) = std::env::var_os("A5200_BIOS").filter(|v| !v.is_empty()) { places.push(PathBuf::from(bios)); } places.extend(default_places()); for place in &places { let mut found = Vec::new(); _candidates(place, &mut found); for (where_, data) in found { if sha1_hex(&data) == sha1 { return Ok((data, where_)); } } } let name = bios_name(sha1).unwrap_or(sha1); Err(BiosNotFound(format!( "BIOS {name} (SHA-1 {sha1}) not found; put the 2K ROM (or a zip holding it) \ in ~/.local/share/a5200-python/bios/, set A5200_BIOS, or set NO_INTRO_DIR / \ MAME_ROMPATH to sets that hold it" ))) }