Skip to content

Commit 12f5a41

Browse files
authored
Fix direct-boot image check for ESP32-S3 (#207)
* Fix direct-boot image check for ESP32-S3 * Renaming and some safety checks
1 parent 4c6b94d commit 12f5a41

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

espflash/src/chip/esp32/esp32c2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ChipType for Esp32c2 {
8484
flash_size,
8585
flash_freq,
8686
)?)),
87-
ImageFormatId::DirectBoot => Ok(Box::new(Esp32DirectBootFormat::new(image)?)),
87+
ImageFormatId::DirectBoot => Ok(Box::new(Esp32DirectBootFormat::new(image, 0)?)),
8888
}
8989
}
9090

espflash/src/chip/esp32/esp32c3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ChipType for Esp32c3 {
8484
flash_freq,
8585
)?)),
8686
(ImageFormatId::DirectBoot, None | Some(3..)) => {
87-
Ok(Box::new(Esp32DirectBootFormat::new(image)?))
87+
Ok(Box::new(Esp32DirectBootFormat::new(image, 0)?))
8888
}
8989
_ => Err(
9090
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision).into(),

espflash/src/chip/esp32/esp32s3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl ChipType for Esp32s3 {
7676
flash_size,
7777
flash_freq,
7878
)?)),
79-
ImageFormatId::DirectBoot => Ok(Box::new(Esp32DirectBootFormat::new(image)?)),
79+
ImageFormatId::DirectBoot => Ok(Box::new(Esp32DirectBootFormat::new(image, 0x400)?)),
8080
}
8181
}
8282
}

espflash/src/image_format/esp32directboot.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::{
66
};
77
use std::iter::once;
88

9-
/// Image format for esp32 family chips using a 2nd stage bootloader
9+
/// Image format for esp32 family chips not using a 2nd stage bootloader
1010
pub struct Esp32DirectBootFormat<'a> {
1111
segment: RomSegment<'a>,
1212
}
1313

1414
impl<'a> Esp32DirectBootFormat<'a> {
15-
pub fn new(image: &'a dyn FirmwareImage<'a>) -> Result<Self, Error> {
15+
pub fn new(image: &'a dyn FirmwareImage<'a>, magic_offset: usize) -> Result<Self, Error> {
1616
let mut segment = image
1717
.segments_with_load_addresses()
1818
.map(|mut segment| {
@@ -27,7 +27,9 @@ impl<'a> Esp32DirectBootFormat<'a> {
2727
segment.pad_align(4);
2828

2929
if segment.addr != 0
30-
|| segment.data()[0..8] != [0x1d, 0x04, 0xdb, 0xae, 0x1d, 0x04, 0xdb, 0xae]
30+
|| (segment.data().len() >= magic_offset + 8
31+
&& segment.data()[magic_offset..][..8]
32+
!= [0x1d, 0x04, 0xdb, 0xae, 0x1d, 0x04, 0xdb, 0xae])
3133
{
3234
return Err(Error::InvalidDirectBootBinary);
3335
}

0 commit comments

Comments
 (0)