libconfig: split cmd_error helper

This commit is contained in:
Simon Renblad 2024-12-27 17:34:14 +08:00
parent 1fb10abb75
commit 7c50b03a92

View File

@ -1,6 +1,6 @@
use core_io::{BufRead, Error, ErrorKind, Read, Result as IoResult, Seek, SeekFrom, Write};
use fatfs;
use libboard_zynq::sdio::{sd_card::SdCard, CmdTransferError};
use libboard_zynq::sdio::sd_card::SdCard;
use log::debug;
use alloc::vec::Vec;
@ -12,10 +12,6 @@ const PARTID_FAT32: u8 = 0x0B;
const PARTID_FAT32_LBA: u8 = 0x0C;
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;
/// SdReader struct implementing `Read + BufRead + Write + Seek` traits for `core_io`.
@ -221,7 +217,7 @@ impl BufRead for SdReader {
// reload buffer
self.sd
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
.map_err(cmd_error_to_io_error)?;
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
}
Ok(&self.buffer[self.index..])
@ -260,7 +256,7 @@ impl Write for SdReader {
let block_addr = (self.byte_addr - self.index as u32) / (BLOCK_SIZE as u32);
self.sd
.write_block(block_addr, 1, &self.buffer)
.map_err(cmd_error_to_io_error)?;
.map_err(|_| Error::new(ErrorKind::Other, "Command transfer error"))?;
self.dirty = false;
}
Ok(())