From b69de9f37fe6b16c0bf8d3d6bfcec7fe05a3ee03 Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Tue, 29 Jul 2025 17:00:36 +0800 Subject: [PATCH] libconfig: wrap FS in OnceLock --- Cargo.lock | 1 + libconfig/Cargo.toml | 1 + libconfig/src/lib.rs | 24 +++++++++--------------- szl/src/main.rs | 17 ++++++++++------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dab31fa..d36cc80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,7 @@ dependencies = [ "core_io", "fatfs", "libboard_zynq", + "libcortex_a9", "log", ] diff --git a/libconfig/Cargo.toml b/libconfig/Cargo.toml index 3dd0456..ab278cc 100644 --- a/libconfig/Cargo.toml +++ b/libconfig/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] libboard_zynq = { path = "../libboard_zynq" } log = "0.4" +libcortex_a9 = { path = "../libcortex_a9" } [dependencies.core_io] git = "https://git.m-labs.hk/M-Labs/rs-core_io.git" diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 1e8e03e..9c94ba1 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -7,6 +7,7 @@ use core::fmt; use core_io::{self as io, BufRead, BufReader, Read, Seek, SeekFrom, Write}; use libboard_zynq::sdio; +use libcortex_a9::once_lock::OnceLock; pub mod bootgen; pub mod net_settings; @@ -66,12 +67,7 @@ fn parse_config(key: &str, buffer: &mut Vec, file: fatfs::File; -static mut FS: Option = None; - -#[allow(static_mut_refs)] -pub fn get_filesystem() -> &'static Option { - unsafe { &FS } -} +pub static FS: OnceLock = OnceLock::new(); const NEWLINE: &[u8] = b"\n"; @@ -85,18 +81,16 @@ pub fn init() -> Result<()> { let fs = reader.mount_fatfs(sd_reader::PartitionEntry::Entry1)?; - unsafe { - FS = Some(fs); - } + from_fs(fs); Ok(()) } -pub fn from_fs(fs: Option) { - unsafe { FS = fs } +pub fn from_fs(fs: FileSystem) { + FS.set(fs).expect("filesystem can only be initialized once"); } pub fn read(key: &str) -> Result> { - if let Some(fs) = get_filesystem() { + if let Some(fs) = FS.get() { let root_dir = fs.root_dir(); let mut buffer: Vec = Vec::new(); match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) { @@ -117,7 +111,7 @@ pub fn read_str(key: &str) -> Result { } pub fn remove(key: &str) -> Result<()> { - if let Some(fs) = get_filesystem() { + if let Some(fs) = FS.get() { let root_dir = fs.root_dir(); match root_dir.remove(&["/CONFIG/", key, ".BIN"].concat()) { Ok(()) => Ok(()), @@ -147,10 +141,10 @@ pub fn remove(key: &str) -> Result<()> { } pub fn write(key: &str, value: Vec) -> Result<()> { - if get_filesystem().is_none() { + if FS.get().is_none() { return Err(Error::NoConfig); } - let fs = get_filesystem().as_ref().unwrap(); + let fs = FS.get().unwrap(); let root_dir = fs.root_dir(); let is_str = value.len() <= 100 && value.is_ascii() && !value.contains(&b'\n'); if key == "boot" { diff --git a/szl/src/main.rs b/szl/src/main.rs index f9467e4..bbcd9bf 100644 --- a/szl/src/main.rs +++ b/szl/src/main.rs @@ -87,21 +87,24 @@ pub fn main_core0() { ram::init_alloc_core0(); let sdio0 = sdio::Sdio::sdio0(true); - let fs = if sdio0.is_card_inserted() { + let mut bootgen_file = if sdio0.is_card_inserted() { info!("Card inserted. Mounting file system."); let sd = sdio::sd_card::SdCard::from_sdio(sdio0).unwrap(); let reader = sd_reader::SdReader::new(sd); - reader.mount_fatfs(sd_reader::PartitionEntry::Entry1).ok() + reader + .mount_fatfs(sd_reader::PartitionEntry::Entry1) + .and_then(|fs| { + libconfig::from_fs(fs); + let fs_ref = libconfig::FS.get().unwrap(); + let root_dir = fs_ref.root_dir(); + root_dir.open_file("/BOOT.BIN") + }) + .ok() } else { info!("No SD card inserted."); None }; - libconfig::from_fs(fs); - let fs_ref = libconfig::get_filesystem().as_ref(); - let root_dir = fs_ref.map(|fs| fs.root_dir()); - let mut bootgen_file = root_dir.and_then(|root_dir| root_dir.open_file("/BOOT.BIN").ok()); - let max_len = (&raw const __runtime_end).addr() - (&raw const __runtime_start).addr(); match slcr::RegisterBlock::unlocked(|slcr| slcr.boot_mode.read().boot_mode_pins()) { slcr::BootModePins::Jtag => netboot::netboot(&mut bootgen_file, (&raw mut __runtime_start).cast(), max_len),