Skip to content

Commit 417e436

Browse files
committed
add proper error when trying to use direct-boot with c3 rev2 or lower
1 parent 353c6da commit 417e436

File tree

8 files changed

+95
-27
lines changed

8 files changed

+95
-27
lines changed

cargo-espflash/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ fn save_image(
388388
.transpose()?
389389
.or(metadata.format);
390390

391-
let flash_image = chip.get_flash_image(&image, None, None, image_format)?;
391+
let flash_image = chip.get_flash_image(&image, None, None, image_format, None)?;
392392
let parts: Vec<_> = flash_image.ota_segments().collect();
393393

394394
let out_path = matches.value_of("file").unwrap();

espflash/src/chip/esp32/esp32.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl ChipType for Esp32 {
118118
bootloader: Option<Vec<u8>>,
119119
partition_table: Option<PartitionTable>,
120120
image_format: ImageFormatId,
121+
_chip_revision: Option<u32>,
121122
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
122123
match image_format {
123124
ImageFormatId::Bootloader => Ok(Box::new(Esp32BootloaderFormat::new(
@@ -127,7 +128,7 @@ impl ChipType for Esp32 {
127128
partition_table,
128129
bootloader,
129130
)?)),
130-
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp8266).into()),
131+
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp32, None).into()),
131132
}
132133
}
133134

espflash/src/chip/esp32/esp32c3.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ops::Range;
22

33
use super::Esp32Params;
4+
use crate::error::UnsupportedImageFormatError;
45
use crate::image_format::Esp32DirectBootFormat;
56
use crate::{
67
chip::{bytes_to_mac_addr, ChipType, ReadEFuse, SpiRegisters},
@@ -71,16 +72,22 @@ impl ChipType for Esp32c3 {
7172
bootloader: Option<Vec<u8>>,
7273
partition_table: Option<PartitionTable>,
7374
image_format: ImageFormatId,
75+
chip_revision: Option<u32>,
7476
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
75-
match image_format {
76-
ImageFormatId::Bootloader => Ok(Box::new(Esp32BootloaderFormat::new(
77+
match (image_format, chip_revision) {
78+
(ImageFormatId::Bootloader, _) => Ok(Box::new(Esp32BootloaderFormat::new(
7779
image,
7880
Chip::Esp32c3,
7981
PARAMS,
8082
partition_table,
8183
bootloader,
8284
)?)),
83-
ImageFormatId::DirectBoot => Ok(Box::new(Esp32DirectBootFormat::new(image)?)),
85+
(ImageFormatId::DirectBoot, None | Some(3..)) => {
86+
Ok(Box::new(Esp32DirectBootFormat::new(image)?))
87+
}
88+
_ => Err(
89+
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision).into(),
90+
),
8491
}
8592
}
8693

espflash/src/chip/esp32/esp32s2.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl ChipType for Esp32s2 {
9595
bootloader: Option<Vec<u8>>,
9696
partition_table: Option<PartitionTable>,
9797
image_format: ImageFormatId,
98+
_chip_revision: Option<u32>,
9899
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
99100
match image_format {
100101
ImageFormatId::Bootloader => Ok(Box::new(Esp32BootloaderFormat::new(
@@ -104,7 +105,7 @@ impl ChipType for Esp32s2 {
104105
partition_table,
105106
bootloader,
106107
)?)),
107-
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp8266).into()),
108+
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp32s2, None).into()),
108109
}
109110
}
110111

espflash/src/chip/esp8266.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ impl ChipType for Esp8266 {
4747
_bootloader: Option<Vec<u8>>,
4848
_partition_table: Option<PartitionTable>,
4949
image_format: ImageFormatId,
50+
_chip_revision: Option<u32>,
5051
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
5152
match image_format {
5253
ImageFormatId::Bootloader => Ok(Box::new(Esp8266Format::new(image)?)),
53-
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp8266).into()),
54+
_ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp8266, None).into()),
5455
}
5556
}
5657

espflash/src/chip/mod.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub trait ChipType {
5353
bootloader: Option<Vec<u8>>,
5454
partition_table: Option<PartitionTable>,
5555
image_format: ImageFormatId,
56+
chip_revision: Option<u32>,
5657
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error>;
5758

5859
/// Read the MAC address of the connected chip.
@@ -158,20 +159,35 @@ impl Chip {
158159
bootloader: Option<Vec<u8>>,
159160
partition_table: Option<PartitionTable>,
160161
image_format: Option<ImageFormatId>,
162+
chip_revision: Option<u32>,
161163
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
162164
let image_format = image_format.unwrap_or_else(|| self.default_image_format());
163165

164166
match self {
165-
Chip::Esp32 => {
166-
Esp32::get_flash_segments(image, bootloader, partition_table, image_format)
167+
Chip::Esp32 => Esp32::get_flash_segments(
168+
image,
169+
bootloader,
170+
partition_table,
171+
image_format,
172+
chip_revision,
173+
),
174+
Chip::Esp32c3 => Esp32c3::get_flash_segments(
175+
image,
176+
bootloader,
177+
partition_table,
178+
image_format,
179+
chip_revision,
180+
),
181+
Chip::Esp32s2 => Esp32s2::get_flash_segments(
182+
image,
183+
bootloader,
184+
partition_table,
185+
image_format,
186+
chip_revision,
187+
),
188+
Chip::Esp8266 => {
189+
Esp8266::get_flash_segments(image, None, None, image_format, chip_revision)
167190
}
168-
Chip::Esp32c3 => {
169-
Esp32c3::get_flash_segments(image, bootloader, partition_table, image_format)
170-
}
171-
Chip::Esp32s2 => {
172-
Esp32s2::get_flash_segments(image, bootloader, partition_table, image_format)
173-
}
174-
Chip::Esp8266 => Esp8266::get_flash_segments(image, None, None, image_format),
175191
}
176192
}
177193

espflash/src/error.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,58 @@ impl From<u8> for FlashDetectError {
497497
}
498498
}
499499

500-
#[derive(Debug, Error, Diagnostic)]
501-
#[error("Image format {format} is not supported by the {chip}")]
502-
#[diagnostic(
503-
code(espflash::unsupported_image_format),
504-
help("The following image formats are supported by the {}: {}", self.chip, self.supported_formats())
505-
)]
500+
#[derive(Debug)]
506501
pub struct UnsupportedImageFormatError {
507502
format: ImageFormatId,
508503
chip: Chip,
504+
revision: Option<u32>,
505+
}
506+
507+
impl Display for UnsupportedImageFormatError {
508+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
509+
write!(
510+
f,
511+
"Image format {} is not supported by the {}",
512+
self.format, self.chip
513+
)?;
514+
if let Some(revision) = self.revision {
515+
write!(f, " revision {}", revision)?;
516+
}
517+
Ok(())
518+
}
519+
}
520+
521+
impl std::error::Error for UnsupportedImageFormatError {}
522+
523+
impl Diagnostic for UnsupportedImageFormatError {
524+
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
525+
Some(Box::new("espflash::unsupported_image_format"))
526+
}
527+
528+
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
529+
let str = if self.chip == Chip::Esp32c3 && self.format == ImageFormatId::DirectBoot {
530+
format!(
531+
"The {}: only supports direct-boot starting with revision 3",
532+
self.chip,
533+
)
534+
} else {
535+
format!(
536+
"The following image formats are supported by the {}: {}",
537+
self.chip,
538+
self.supported_formats()
539+
)
540+
};
541+
Some(Box::new(str))
542+
}
509543
}
510544

511545
impl UnsupportedImageFormatError {
512-
pub fn new(format: ImageFormatId, chip: Chip) -> Self {
513-
UnsupportedImageFormatError { format, chip }
546+
pub fn new(format: ImageFormatId, chip: Chip, revision: Option<u32>) -> Self {
547+
UnsupportedImageFormatError {
548+
format,
549+
chip,
550+
revision,
551+
}
514552
}
515553

516554
fn supported_formats(&self) -> String {

espflash/src/flasher.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,13 @@ impl Flasher {
542542
let mut target = self.chip.flash_target(self.spi_params);
543543
target.begin(&mut self.connection, &image).flashing()?;
544544

545-
let flash_image =
546-
self.chip
547-
.get_flash_image(&image, bootloader, partition_table, image_format)?;
545+
let flash_image = self.chip.get_flash_image(
546+
&image,
547+
bootloader,
548+
partition_table,
549+
image_format,
550+
self.chip.chip_revision(&mut self.connection)?,
551+
)?;
548552

549553
for segment in flash_image.flash_segments() {
550554
target

0 commit comments

Comments
 (0)