Skip to content

Commit 12d989f

Browse files
authored
Merge pull request #67 from jessebraham/feature/default-flash-size
If flash size cannot be detected, print a warning and default to 4MB
2 parents 5892cc6 + 4df7c4b commit 12d989f

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

espflash/src/flasher.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ pub enum FlashSize {
100100
Flash8Mb = 0x17,
101101
#[strum(serialize = "16MB")]
102102
Flash16Mb = 0x18,
103+
#[strum(serialize = "32MB")]
104+
Flash32Mb = 0x19,
105+
#[strum(serialize = "64MB")]
106+
Flash64Mb = 0x1a,
103107
FlashRetry = 0xFF, // used to hint that alternate detection should be tried
104108
}
105109

@@ -113,6 +117,8 @@ impl FlashSize {
113117
0x16 => Ok(FlashSize::Flash4Mb),
114118
0x17 => Ok(FlashSize::Flash8Mb),
115119
0x18 => Ok(FlashSize::Flash16Mb),
120+
0x19 => Ok(FlashSize::Flash32Mb),
121+
0x1a => Ok(FlashSize::Flash64Mb),
116122
0xFF => Ok(FlashSize::FlashRetry),
117123
_ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))),
118124
}
@@ -266,7 +272,18 @@ impl Flasher {
266272
let flash_id = self.spi_command(Command::FlashDetect, &[], 24)?;
267273
let size_id = flash_id >> 16;
268274

269-
self.flash_size = FlashSize::from(size_id as u8)?;
275+
self.flash_size = match FlashSize::from(size_id as u8) {
276+
Ok(size) => size,
277+
Err(_) => {
278+
eprintln!(
279+
"Warning: could not detect flash size (FlashID=0x{:02X}, SizeID=0x{:02X}), defaulting to 4MB\n",
280+
flash_id,
281+
size_id
282+
);
283+
FlashSize::Flash4Mb
284+
}
285+
};
286+
270287
Ok(self.flash_size != FlashSize::FlashRetry)
271288
}
272289

espflash/src/image_format/esp32bootloader.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use crate::chip::Esp32Params;
2-
use crate::elf::{
3-
merge_segments, update_checksum, CodeSegment, FirmwareImage, RomSegment, ESP_CHECKSUM_MAGIC,
4-
};
5-
use crate::error::{Error, FlashDetectError};
6-
use crate::flasher::FlashSize;
7-
use crate::image_format::{
8-
EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC, WP_PIN_DISABLED,
9-
};
10-
use crate::{Chip, PartitionTable};
11-
use bytemuck::bytes_of;
12-
use bytemuck::{Pod, Zeroable};
13-
use sha2::{Digest, Sha256};
141
use std::{borrow::Cow, io::Write, iter::once};
152

3+
use bytemuck::{bytes_of, Pod, Zeroable};
4+
use sha2::{Digest, Sha256};
5+
6+
use crate::{
7+
chip::Esp32Params,
8+
elf::{
9+
merge_segments, update_checksum, CodeSegment, FirmwareImage, RomSegment, ESP_CHECKSUM_MAGIC,
10+
},
11+
error::{Error, FlashDetectError},
12+
flasher::FlashSize,
13+
image_format::{EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC, WP_PIN_DISABLED},
14+
Chip, PartitionTable,
15+
};
16+
1617
/// Image format for esp32 family chips using a 2nd stage bootloader
1718
pub struct Esp32BootloaderFormat<'a> {
1819
params: Esp32Params,
@@ -72,7 +73,8 @@ impl<'a> Esp32BootloaderFormat<'a> {
7273
if pad_len > 0 {
7374
if pad_len > SEG_HEADER_LEN {
7475
if let Some(ram_segment) = ram_segments.first_mut() {
75-
// save up to `pad_len` from the ram segment, any remaining bits in the ram segments will be saved later
76+
// save up to `pad_len` from the ram segment, any remaining bits in the
77+
// ram segments will be saved later
7678
let pad_segment = ram_segment.split_off(pad_len as usize);
7779
checksum = save_segment(&mut data, &pad_segment, checksum)?;
7880
if ram_segment.data().is_empty() {
@@ -152,14 +154,12 @@ impl<'a> ImageFormat<'a> for Esp32BootloaderFormat<'a> {
152154

153155
fn encode_flash_size(size: FlashSize) -> Result<u8, FlashDetectError> {
154156
match size {
155-
FlashSize::Flash256Kb => Err(FlashDetectError::from(size as u8)),
156-
FlashSize::Flash512Kb => Err(FlashDetectError::from(size as u8)),
157157
FlashSize::Flash1Mb => Ok(0x00),
158158
FlashSize::Flash2Mb => Ok(0x10),
159159
FlashSize::Flash4Mb => Ok(0x20),
160160
FlashSize::Flash8Mb => Ok(0x30),
161161
FlashSize::Flash16Mb => Ok(0x40),
162-
FlashSize::FlashRetry => Err(FlashDetectError::from(size as u8)),
162+
_ => Err(FlashDetectError::from(size as u8)),
163163
}
164164
}
165165

espflash/src/image_format/esp8266.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
use crate::elf::{update_checksum, CodeSegment, FirmwareImage, RomSegment, ESP_CHECKSUM_MAGIC};
2-
use crate::error::{Error, FlashDetectError};
3-
use crate::flasher::FlashSize;
4-
use crate::image_format::{EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC};
5-
use crate::Chip;
6-
use bytemuck::bytes_of;
71
use std::{borrow::Cow, io::Write, iter::once, mem::size_of};
82

3+
use bytemuck::bytes_of;
4+
5+
use crate::{
6+
elf::{update_checksum, CodeSegment, FirmwareImage, RomSegment, ESP_CHECKSUM_MAGIC},
7+
error::{Error, FlashDetectError},
8+
flasher::FlashSize,
9+
image_format::{EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC},
10+
Chip,
11+
};
12+
913
const IROM_MAP_START: u32 = 0x40200000;
1014

1115
/// Image format for flashing to esp8266 chips
@@ -122,6 +126,6 @@ fn encode_flash_size(size: FlashSize) -> Result<u8, FlashDetectError> {
122126
FlashSize::Flash4Mb => Ok(0x40),
123127
FlashSize::Flash8Mb => Ok(0x80),
124128
FlashSize::Flash16Mb => Ok(0x90),
125-
FlashSize::FlashRetry => Err(FlashDetectError::from(size as u8)),
129+
_ => Err(FlashDetectError::from(size as u8)),
126130
}
127131
}

0 commit comments

Comments
 (0)