Skip to content

Adds support for flash encryption #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Flash encryption: variable renames
  • Loading branch information
4rzael committed Jan 6, 2025
commit 6be91521b2ddc42e93aae1d703b5762d54f8977d
12 changes: 6 additions & 6 deletions espflash/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub enum Command<'a> {
block_size: u32,
offset: u32,
supports_encryption: bool,
perform_encryption: bool,
encrypt: bool,
},
FlashData {
data: &'a [u8],
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Command<'_> {
block_size,
offset,
supports_encryption,
perform_encryption,
encrypt,
} => {
begin_command(
writer,
Expand All @@ -247,7 +247,7 @@ impl Command<'_> {
block_size,
offset,
supports_encryption,
perform_encryption,
encrypt,
)?;
}
Command::FlashData {
Expand Down Expand Up @@ -446,10 +446,10 @@ fn begin_command<W: Write>(
block_size: u32,
offset: u32,
supports_encryption: bool,
perform_encryption: bool,
encrypt: bool,
) -> std::io::Result<()> {
assert!(
!(perform_encryption && !supports_encryption),
!(encrypt && !supports_encryption),
"Target does not support encryption, yet encryption is requested"
);

Expand All @@ -467,7 +467,7 @@ fn begin_command<W: Write>(
blocks,
block_size,
offset,
encrypted: perform_encryption as u32,
encrypted: encrypt as u32,
};

let bytes = bytes_of(&params);
Expand Down
4 changes: 2 additions & 2 deletions espflash/src/connection/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn soft_reset(
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
offset,
supports_encryption: false,
perform_encryption: false,
encrypt: false,
})
})?;
connection.with_timeout(CommandType::FlashEnd.timeout(), |connection| {
Expand All @@ -279,7 +279,7 @@ pub fn soft_reset(
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
offset,
supports_encryption: false,
perform_encryption: false,
encrypt: false,
})
})?;
connection.with_timeout(CommandType::FlashEnd.timeout(), |connection| {
Expand Down
8 changes: 4 additions & 4 deletions espflash/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub struct RomSegment<'a> {
/// Segment data
pub data: Cow<'a, [u8]>,
/// Whether the segment shall be encrypted before being writen
pub encrypt: bool,
pub encrypted: bool,
}

impl<'a> RomSegment<'a> {
Expand All @@ -249,15 +249,15 @@ impl<'a> RomSegment<'a> {
RomSegment {
addr: self.addr,
data: Cow::Borrowed(self.data.as_ref()),
encrypt: self.encrypt,
encrypted: self.encrypted,
}
}

pub fn from_code_segment(segment: &CodeSegment<'a>, encrypt: bool) -> Self {
pub fn from_code_segment(segment: &CodeSegment<'a>, encrypted: bool) -> Self {
RomSegment {
addr: segment.addr,
data: segment.data.clone(),
encrypt,
encrypted,
}
}
}
8 changes: 4 additions & 4 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ impl Flasher {
RomSegment {
addr: text_addr,
data: Cow::Borrowed(&text),
encrypt: false,
encrypted: false,
},
&mut None,
)
Expand All @@ -701,7 +701,7 @@ impl Flasher {
RomSegment {
addr: data_addr,
data: Cow::Borrowed(&data),
encrypt: false,
encrypted: false,
},
&mut None,
)
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl Flasher {
let segment = RomSegment {
addr,
data: Cow::from(data),
encrypt,
encrypted: encrypt,
};
self.write_bins_to_flash(&[segment], progress)?;

Expand All @@ -1037,7 +1037,7 @@ impl Flasher {
segments: &[RomSegment],
mut progress: Option<&mut dyn ProgressCallbacks>,
) -> Result<(), Error> {
let encrypt = segments.iter().any(|seg| seg.encrypt);
let encrypt = segments.iter().any(|seg| seg.encrypted);
let mut target =
self.chip
.flash_target(self.spi_params, self.use_stub, false, false, encrypt);
Expand Down
10 changes: 5 additions & 5 deletions espflash/src/image_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'a> IdfBootloaderFormat<'a> {
target_app_partition: Option<String>,
bootloader: Option<Vec<u8>>,
flash_settings: FlashSettings,
encrypt: bool,
encrypted: bool,
) -> Result<Self, Error> {
let partition_table = partition_table.unwrap_or_else(|| {
params.default_partition_table(flash_settings.size.map(|v| v.size()))
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a> IdfBootloaderFormat<'a> {
let flash_segment = RomSegment {
addr: target_app_partition.offset(),
data: Cow::Owned(data),
encrypt,
encrypted,
};

// If the user did not specify a partition offset, we need to assume that the
Expand Down Expand Up @@ -306,19 +306,19 @@ impl<'a> IdfBootloaderFormat<'a> {
let bootloader_segment = RomSegment {
addr: self.params.boot_addr,
data: Cow::Borrowed(&self.bootloader),
encrypt: self.flash_segment.encrypt,
encrypted: self.flash_segment.encrypted,
};

let partition_table_segment = RomSegment {
addr: self.partition_table_offset,
data: Cow::Owned(self.partition_table.to_bin().unwrap()),
encrypt: self.flash_segment.encrypt,
encrypted: self.flash_segment.encrypted,
};

let app_segment = RomSegment {
addr: self.flash_segment.addr,
data: Cow::Borrowed(&self.flash_segment.data),
encrypt: self.flash_segment.encrypt,
encrypted: self.flash_segment.encrypted,
};

Box::new(
Expand Down
6 changes: 3 additions & 3 deletions espflash/src/targets/flash_target/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl FlashTarget for Esp32Target {
md5_hasher.update(&segment.data);
let checksum_md5 = md5_hasher.finalize();

if self.skip && !segment.encrypt {
if self.skip && !segment.encrypted {
let flash_checksum_md5: u128 =
connection.with_timeout(CommandType::FlashMd5.timeout(), |connection| {
connection
Expand Down Expand Up @@ -191,7 +191,7 @@ impl FlashTarget for Esp32Target {
block_size: flash_write_size as u32,
offset: addr,
supports_encryption: self.chip != Chip::Esp32 && !self.use_stub,
perform_encryption: segment.encrypt,
encrypt: segment.encrypted,
})?;
Ok(())
},
Expand Down Expand Up @@ -277,7 +277,7 @@ impl FlashTarget for Esp32Target {
cb.finish()
}

if self.verify && !segment.encrypt {
if self.verify && !segment.encrypted {
let flash_checksum_md5: u128 =
connection.with_timeout(CommandType::FlashMd5.timeout(), |connection| {
connection
Expand Down