connection: error handling (#270)

* connection: Check the error field in the response instead of status

* Handle generic stub errors

* esp32: Fix size in FlashDeflateBegin

The `size` field for `Command::FlashDeflateBegin` is the uncompressed size of the segment.
It was rounded up to the memory block size, and the stub was expecting more data when the flash ended,
even though the inflator was returning successfully.

* Fix typo in espflash/src/error.rs

Co-authored-by: Jesse Braham <jessebraham@users.noreply.github.com>

Co-authored-by: Jesse Braham <jessebraham@users.noreply.github.com>
This commit is contained in:
Maxime BORGES
2022-10-24 22:19:35 +02:00
committed by GitHub
parent 92251a929b
commit c29b1dd2da
3 changed files with 46 additions and 2 deletions

View File

@@ -226,7 +226,7 @@ impl Connection {
for _ in 0..100 {
match self.read_response().for_command(ty)? {
Some(response) if response.return_op == ty as u8 => {
return if response.status == 1 {
return if response.error != 0 {
let _error = self.flush();
Err(Error::RomError(RomError::new(
command.command_type(),

View File

@@ -287,6 +287,38 @@ pub enum RomErrorKind {
#[error("Malformed compressed data received")]
#[diagnostic(code(espflash::rom::deflate))]
DeflateError = 0x0b,
#[error("Bad data length")]
#[diagnostic(code(espflash::rom::data_len))]
BadDataLen = 0xc0,
#[error("Bad data checksum")]
#[diagnostic(code(espflash::rom::data_crc))]
BadDataChecksum = 0xc1,
#[error("Bad block size")]
#[diagnostic(code(espflash::rom::block_size))]
BadBlocksize = 0xc2,
#[error("Invalid command")]
#[diagnostic(code(espflash::rom::cmd))]
InvalidCommand = 0xc3,
#[error("SPI operation failed")]
#[diagnostic(code(espflash::rom::spi))]
FailedSpiOp = 0xc4,
#[error("SPI unlock failed")]
#[diagnostic(code(espflash::rom::spi_unlock))]
FailedSpiUnlock = 0xc5,
#[error("Not in flash mode")]
#[diagnostic(code(espflash::rom::flash_mode))]
NotInFlashMode = 0xc6,
#[error("Error when uncompressing the data")]
#[diagnostic(code(espflash::rom::inflate))]
InflateError = 0xc7,
#[error("Didn't receive enough data")]
#[diagnostic(code(espflash::rom::not_enough))]
NotEnoughData = 0xc8,
#[error("Received too much data")]
#[diagnostic(code(espflash::rom::too_much_data))]
TooMuchData = 0xc9,
#[error("Other")]
#[diagnostic(code(espflash::rom::other))]
Other = 0xff,
@@ -302,6 +334,18 @@ impl From<u8> for RomErrorKind {
0x09 => RomErrorKind::FlashReadError,
0x0a => RomErrorKind::FlashReadLengthError,
0x0b => RomErrorKind::DeflateError,
0xc0 => RomErrorKind::BadDataLen,
0xc1 => RomErrorKind::BadDataChecksum,
0xc2 => RomErrorKind::BadBlocksize,
0xc3 => RomErrorKind::InvalidCommand,
0xc4 => RomErrorKind::FailedSpiOp,
0xc5 => RomErrorKind::FailedSpiUnlock,
0xc6 => RomErrorKind::NotInFlashMode,
0xc7 => RomErrorKind::InflateError,
0xc8 => RomErrorKind::NotEnoughData,
0xc9 => RomErrorKind::TooMuchData,
_ => RomErrorKind::Other,
}
}

View File

@@ -115,7 +115,7 @@ impl FlashTarget for Esp32Target {
CommandType::FlashDeflateBegin.timeout_for_size(erase_size),
|connection| {
connection.command(Command::FlashDeflateBegin {
size: erase_size,
size: segment.data.len() as u32,
blocks: block_count as u32,
block_size: flash_write_size as u32,
offset: addr,