From 83959b26914d9681d108816e25032b4020c244fb Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Mon, 29 Jul 2024 17:54:35 +0800 Subject: [PATCH] libconfig rewrites --- libconfig/src/lib.rs | 34 +++++++++++++++++++++---------- libconfig/src/sd_reader.rs | 41 ++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index f1bf1b1..670d227 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -3,7 +3,6 @@ extern crate alloc; use core::fmt; use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc}; -use core_io::{self as io, BufRead, BufReader, Read, Write, Seek, SeekFrom}; use libboard_zynq::sdio; pub mod sd_reader; @@ -13,7 +12,7 @@ pub mod bootgen; #[derive(Debug)] pub enum Error<'a> { SdError(sdio::sd_card::CardInitializationError), - IoError(io::Error), + IoError(sd_reader::SdReader::Error), Utf8Error(FromUtf8Error), KeyNotFoundError(&'a str), NoConfig, @@ -39,8 +38,8 @@ impl<'a> From for Error<'a> { } } -impl<'a> From for Error<'a> { - fn from(error: io::Error) -> Self { +impl<'a> From for Error<'a> { + fn from(error: sd_reader::SdReader::Error) -> Self { Error::IoError(error) } } @@ -57,14 +56,29 @@ fn parse_config<'a>( file: fatfs::File, ) -> Result<'a, ()> { let prefix = [key, "="].concat().to_ascii_lowercase(); - for line in BufReader::new(file).lines() { - let line = line?.to_ascii_lowercase(); - if line.starts_with(&prefix) { - buffer.extend(line[prefix.len()..].as_bytes()); - return Ok(()); + let mut file_buf: Vec = Vec::new(); + loop { + match memchr::memchr(b'\n', &file_buf) { + Some(i) => { + let s = String::from_utf8(&file_buf[..=i]) + if s.starts_with(&prefix) { + buffer.extend_from_slice(&file_buf[..=i]); + return Ok(()) + } else { + file.consume(i + 1) + } + } + None => { + let s = String::from_utf8(file_buf) + if s.starts_with(&prefix) { + buffer.extend_from_slice(file_buf); + return Ok(()) + } else { + return Err(Error::KeyNotFoundError(key)) + } + } } } - Err(Error::KeyNotFoundError(key)) } pub struct Config { diff --git a/libconfig/src/sd_reader.rs b/libconfig/src/sd_reader.rs index fc93304..7293c91 100644 --- a/libconfig/src/sd_reader.rs +++ b/libconfig/src/sd_reader.rs @@ -188,6 +188,25 @@ impl SdReader { fn cmd_error_to_io_error(_: CmdTransferError) -> Self::Error { Self::Error::Other("Command transfer error") } + fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { + if self.index == BLOCK_SIZE { + // flush the buffer if it is dirty before overwriting it with new data + if self.dirty { + self.flush()?; + } + // reload buffer + self.sd + .read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer) + .map_err(cmd_error_to_io_error)?; + self.index = (self.byte_addr as usize) % BLOCK_SIZE; + } + Ok(&self.buffer[self.index..]) + } + + fn consume(&mut self, amt: usize) { + self.index += amt; + self.byte_addr += amt as u32; + } } @@ -217,28 +236,6 @@ impl Read for SdReader { } } -// impl BufRead for SdReader { -// fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { -// if self.index == BLOCK_SIZE { -// // flush the buffer if it is dirty before overwriting it with new data -// if self.dirty { -// self.flush()?; -// } -// // reload buffer -// self.sd -// .read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer) -// .map_err(cmd_error_to_io_error)?; -// self.index = (self.byte_addr as usize) % BLOCK_SIZE; -// } -// Ok(&self.buffer[self.index..]) -// } -// -// fn consume(&mut self, amt: usize) { -// self.index += amt; -// self.byte_addr += amt as u32; -// } -// } - impl Write for SdReader { fn write(&mut self, buf: &[u8]) -> Result { let (a, b, c) = self.block_align(buf);