pub struct Writer<'a> {
flash: &'a dyn Flash,
next: u32,
buffer: &'a mut [u8],
filled: usize,
wear: WearCheck,
}Expand description
Writer over the good blocks of an MTD device.
The writer collects data until it fills a block. It then erases the next
good block, writes the data and reads it back to verify it. When a block
fails to erase, write or verify, the writer marks it bad and uses the next
good block. This keeps the layout that super::Reader and bootloaders
read. If the next block fails too, the writer returns the error.
Call Writer::finish to write the last partial block, or
Writer::finish_and_erase_rest to also erase the blocks after it.
io::Write::flush only writes a full block, because a partial one would
leave a gap in the data. Errors of io::Write are DeviceErrors
wrapped in io::Error.
A full block is only written by the next call to write, flush,
finish or finish_and_erase_rest, so that call returns its error. Call
flush after each full block to get its error right away.
Fields§
§flash: &'a dyn Flash§next: u32Physical block where the search for the next good block starts.
buffer: &'a mut [u8]Buffer for the data of one block.
filled: usizeNumber of bytes in buffer.
wear: WearCheckImplementations§
§impl<'a> Writer<'a>
impl<'a> Writer<'a>
pub fn new(
flash: &'a dyn Flash,
buffer: &'a mut [u8],
) -> Result<Self, DeviceError>
pub fn new( flash: &'a dyn Flash, buffer: &'a mut [u8], ) -> Result<Self, DeviceError>
Creates a writer that starts at the first good block.
The writer collects the data of a block in buffer, which must be
exactly one block long.
§Errors
Returns DeviceError::BufferLength if buffer isn’t one block long.
pub fn starting_at(
flash: &'a dyn Flash,
buffer: &'a mut [u8],
block: u32,
) -> Result<Self, DeviceError>
pub fn starting_at( flash: &'a dyn Flash, buffer: &'a mut [u8], block: u32, ) -> Result<Self, DeviceError>
Creates a writer that starts at good block block.
block counts good blocks only. Bad blocks before it are skipped.
§Errors
Returns DeviceError::NoSpace if the flash has fewer good blocks,
DeviceError::BufferLength if buffer isn’t one block long, or an
error if the bad block markers can’t be read.
fn with_start(
flash: &'a dyn Flash,
buffer: &'a mut [u8],
start: u32,
) -> Result<Self, DeviceError>
fn with_start( flash: &'a dyn Flash, buffer: &'a mut [u8], start: u32, ) -> Result<Self, DeviceError>
Creates a writer that starts at physical block start.
pub fn finish(self) -> Result<(), DeviceError>
pub fn finish(self) -> Result<(), DeviceError>
Writes the pending data and consumes the writer.
A partial last page is padded with 0xFF, the value of erased flash.
The remaining pages of the last block are left erased.
§Errors
Returns DeviceError::NoSpace if no good block is left, or an error
if the flash fails in a way that isn’t a worn block.
pub fn finish_and_erase_rest(self) -> Result<(), DeviceError>
pub fn finish_and_erase_rest(self) -> Result<(), DeviceError>
Writes the pending data, erases all good blocks after it and consumes the writer.
Use this to write an image to a whole partition. Otherwise the blocks after the image keep their old data. UBI, for example, scans every block of the partition and would find stale headers there. Blocks that fail to erase are marked bad.
§Errors
Returns an error if Writer::finish would fail, or if the flash
fails in a way that isn’t a worn block.
fn write_pending(&mut self) -> Result<(), DeviceError>
fn write_pending(&mut self) -> Result<(), DeviceError>
Writes the buffer, padded to whole pages.
fn write_full_block(&mut self) -> Result<(), DeviceError>
fn write_full_block(&mut self) -> Result<(), DeviceError>
Writes the buffer if it holds a full block.
Trait Implementations§
§impl Write for Writer<'_>
impl Write for Writer<'_>
§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)