libconfig: wrap FS in OnceLock

This commit is contained in:
2025-07-29 17:00:36 +08:00
parent 8ca9f6f4dd
commit b69de9f37f
4 changed files with 21 additions and 22 deletions

1
Cargo.lock generated
View File

@@ -104,6 +104,7 @@ dependencies = [
"core_io",
"fatfs",
"libboard_zynq",
"libcortex_a9",
"log",
]

View File

@@ -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"

View File

@@ -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<u8>, file: fatfs::File<sd_reader::Sd
}
type FileSystem = fatfs::FileSystem<sd_reader::SdReader>;
static mut FS: Option<FileSystem> = None;
#[allow(static_mut_refs)]
pub fn get_filesystem() -> &'static Option<FileSystem> {
unsafe { &FS }
}
pub static FS: OnceLock<FileSystem> = 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<FileSystem>) {
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<Vec<u8>> {
if let Some(fs) = get_filesystem() {
if let Some(fs) = FS.get() {
let root_dir = fs.root_dir();
let mut buffer: Vec<u8> = Vec::new();
match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) {
@@ -117,7 +111,7 @@ pub fn read_str(key: &str) -> Result<String> {
}
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<u8>) -> 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" {

View File

@@ -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),