forked from M-Labs/zynq-rs
Compare commits
No commits in common. "fix_cmd_error" and "master" have entirely different histories.
fix_cmd_er
...
master
@ -11,17 +11,17 @@ pub mod net_settings;
|
|||||||
pub mod bootgen;
|
pub mod bootgen;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error<'a> {
|
||||||
SdError(sdio::sd_card::CardInitializationError),
|
SdError(sdio::sd_card::CardInitializationError),
|
||||||
IoError(io::Error),
|
IoError(io::Error),
|
||||||
Utf8Error(FromUtf8Error),
|
Utf8Error(FromUtf8Error),
|
||||||
KeyNotFoundError(String),
|
KeyNotFoundError(&'a str),
|
||||||
NoConfig,
|
NoConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<'a, T> = core::result::Result<T, Error<'a>>;
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl<'a> fmt::Display for Error<'a> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Error::SdError(error) => write!(f, "SD error: {}", error),
|
Error::SdError(error) => write!(f, "SD error: {}", error),
|
||||||
@ -33,29 +33,29 @@ impl fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<sdio::sd_card::CardInitializationError> for Error {
|
impl<'a> From<sdio::sd_card::CardInitializationError> for Error<'a> {
|
||||||
fn from(error: sdio::sd_card::CardInitializationError) -> Self {
|
fn from(error: sdio::sd_card::CardInitializationError) -> Self {
|
||||||
Error::SdError(error)
|
Error::SdError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for Error {
|
impl<'a> From<io::Error> for Error<'a> {
|
||||||
fn from(error: io::Error) -> Self {
|
fn from(error: io::Error) -> Self {
|
||||||
Error::IoError(error)
|
Error::IoError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FromUtf8Error> for Error {
|
impl<'a> From<FromUtf8Error> for Error<'a> {
|
||||||
fn from(error: FromUtf8Error) -> Self {
|
fn from(error: FromUtf8Error) -> Self {
|
||||||
Error::Utf8Error(error)
|
Error::Utf8Error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_config(
|
fn parse_config<'a>(
|
||||||
key: &str,
|
key: &'a str,
|
||||||
buffer: &mut Vec<u8>,
|
buffer: &mut Vec<u8>,
|
||||||
file: fatfs::File<sd_reader::SdReader>,
|
file: fatfs::File<sd_reader::SdReader>,
|
||||||
) -> Result<()> {
|
) -> Result<'a, ()> {
|
||||||
let prefix = [key, "="].concat().to_ascii_lowercase();
|
let prefix = [key, "="].concat().to_ascii_lowercase();
|
||||||
for line in BufReader::new(file).lines() {
|
for line in BufReader::new(file).lines() {
|
||||||
let line = line?.to_ascii_lowercase();
|
let line = line?.to_ascii_lowercase();
|
||||||
@ -64,7 +64,7 @@ fn parse_config(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(Error::KeyNotFoundError(key.into()))
|
Err(Error::KeyNotFoundError(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@ -74,7 +74,7 @@ pub struct Config {
|
|||||||
const NEWLINE: &[u8] = b"\n";
|
const NEWLINE: &[u8] = b"\n";
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<'static, Self> {
|
||||||
let sdio = sdio::Sdio::sdio0(true);
|
let sdio = sdio::Sdio::sdio0(true);
|
||||||
if !sdio.is_card_inserted() {
|
if !sdio.is_card_inserted() {
|
||||||
Err(sdio::sd_card::CardInitializationError::NoCardInserted)?;
|
Err(sdio::sd_card::CardInitializationError::NoCardInserted)?;
|
||||||
@ -94,7 +94,7 @@ impl Config {
|
|||||||
Config { fs: None }
|
Config { fs: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&self, key: &str) -> Result<Vec<u8>> {
|
pub fn read<'b>(&self, key: &'b str) -> Result<'b, Vec<u8>> {
|
||||||
if let Some(fs) = &self.fs {
|
if let Some(fs) = &self.fs {
|
||||||
let root_dir = fs.root_dir();
|
let root_dir = fs.root_dir();
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
@ -102,7 +102,7 @@ impl Config {
|
|||||||
Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?,
|
Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?,
|
||||||
Err(_) => match root_dir.open_file("/CONFIG.TXT") {
|
Err(_) => match root_dir.open_file("/CONFIG.TXT") {
|
||||||
Ok(f) => parse_config(key, &mut buffer, f)?,
|
Ok(f) => parse_config(key, &mut buffer, f)?,
|
||||||
Err(_) => return Err(Error::KeyNotFoundError(key.into())),
|
Err(_) => return Err(Error::KeyNotFoundError(key)),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
@ -111,11 +111,11 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_str(&self, key: &str) -> Result<String> {
|
pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> {
|
||||||
Ok(String::from_utf8(self.read(key)?)?)
|
Ok(String::from_utf8(self.read(key)?)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&self, key: &str) -> Result<()> {
|
pub fn remove<'b>(&self, key: &'b str) -> Result<'b, ()> {
|
||||||
if let Some(fs) = &self.fs {
|
if let Some(fs) = &self.fs {
|
||||||
let root_dir = fs.root_dir();
|
let root_dir = fs.root_dir();
|
||||||
match root_dir.remove(&["/CONFIG/", key, ".BIN"].concat()) {
|
match root_dir.remove(&["/CONFIG/", key, ".BIN"].concat()) {
|
||||||
@ -136,7 +136,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
Err(_) => Err(Error::KeyNotFoundError(key.into()))
|
Err(_) => Err(Error::KeyNotFoundError(key))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self, key: &str, value: Vec<u8>) -> Result<()> {
|
pub fn write<'b>(&self, key: &'b str, value: Vec<u8>) -> Result<'b, ()> {
|
||||||
if self.fs.is_none() {
|
if self.fs.is_none() {
|
||||||
return Err(Error::NoConfig);
|
return Err(Error::NoConfig);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use core_io::{BufRead, Error, ErrorKind, Read, Result as IoResult, Seek, SeekFrom, Write};
|
use core_io::{BufRead, Error, ErrorKind, Read, Result as IoResult, Seek, SeekFrom, Write};
|
||||||
use fatfs;
|
use fatfs;
|
||||||
use libboard_zynq::sdio::sd_card::SdCard;
|
use libboard_zynq::sdio::{sd_card::SdCard, CmdTransferError};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
@ -12,6 +12,10 @@ const PARTID_FAT32: u8 = 0x0B;
|
|||||||
const PARTID_FAT32_LBA: u8 = 0x0C;
|
const PARTID_FAT32_LBA: u8 = 0x0C;
|
||||||
const PARTID_FAT16_LBA: u8 = 0x0E;
|
const PARTID_FAT16_LBA: u8 = 0x0E;
|
||||||
|
|
||||||
|
fn cmd_error_to_io_error(_: CmdTransferError) -> Error {
|
||||||
|
Error::new(ErrorKind::Other, "Command transfer error")
|
||||||
|
}
|
||||||
|
|
||||||
const BLOCK_SIZE: usize = 512;
|
const BLOCK_SIZE: usize = 512;
|
||||||
|
|
||||||
/// SdReader struct implementing `Read + BufRead + Write + Seek` traits for `core_io`.
|
/// SdReader struct implementing `Read + BufRead + Write + Seek` traits for `core_io`.
|
||||||
@ -217,7 +221,7 @@ impl BufRead for SdReader {
|
|||||||
// reload buffer
|
// reload buffer
|
||||||
self.sd
|
self.sd
|
||||||
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
|
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
|
||||||
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
|
.map_err(cmd_error_to_io_error)?;
|
||||||
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
|
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
Ok(&self.buffer[self.index..])
|
Ok(&self.buffer[self.index..])
|
||||||
@ -256,7 +260,7 @@ impl Write for SdReader {
|
|||||||
let block_addr = (self.byte_addr - self.index as u32) / (BLOCK_SIZE as u32);
|
let block_addr = (self.byte_addr - self.index as u32) / (BLOCK_SIZE as u32);
|
||||||
self.sd
|
self.sd
|
||||||
.write_block(block_addr, 1, &self.buffer)
|
.write_block(block_addr, 1, &self.buffer)
|
||||||
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
|
.map_err(cmd_error_to_io_error)?;
|
||||||
self.dirty = false;
|
self.dirty = false;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user