Skip to content

Commit

Permalink
fix: merge firmware sections, fix ch32-rs#56
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Mar 30, 2024
1 parent 13a3b3d commit b4d7953
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add Windows native driver support, #39
- Use loaded memory address from ELF file or ihex file

### Fixed

- Merge gaps in firmware sections, #56

### Changed

- No erase by default when flashing
Expand Down
6 changes: 3 additions & 3 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<const N: u8> fmt::Debug for RawCommand<N> {
}
}

/// Set address and offset of the firmware, 0x01.
/// 0x01 - Set address and offset of the firmware
#[derive(Debug)]
pub struct SetWriteMemoryRegion {
// 0x08000000 or 0x00000000
Expand Down Expand Up @@ -126,14 +126,14 @@ impl Command for SetReadMemoryRegion {
}
}

/// 0x02 subset
/// 0x02 - Flash or Memory operations
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Program {
// wlink_erase
EraseFlash = 0x01,
// Before write firmware bytes, choice between 0x02 and 0x04
WriteFlash = 0x02,
// after write flash
// Write flash
WriteFlashAndVerify = 0x04,
/// Write Flash OP
WriteFlashOP = 0x05,
Expand Down
14 changes: 6 additions & 8 deletions src/firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum Firmware {
}

impl Firmware {
/// Merge sections w/ <= 256 bytes gap
/// Merge sections, and fill gap with 0xff
pub fn merge_sections(self) -> Result<Self> {
if let Firmware::Sections(mut sections) = self {
sections.sort_by_key(|s| s.address);
Expand All @@ -49,16 +49,14 @@ impl Firmware {
let mut last = it
.next()
.expect("firmware must has at least one section; qed");

for sect in it {
if let Some(gap) = sect.address.checked_sub(last.end_address()) {
if gap > 256 {
merged.push(last);
last = sect.clone();
continue;
} else {
last.data.resize(last.data.len() + gap as usize, 0);
last.data.extend_from_slice(&sect.data);
if gap > 0 {
log::debug!("Merge firmware sections with gap: {}", gap);
}
last.data.resize(last.data.len() + gap as usize, 0xff); // fill gap with 0xff
last.data.extend_from_slice(&sect.data);
} else {
return Err(anyhow::format_err!(
"section address overflow: {:#010x} + {:#x}",
Expand Down

0 comments on commit b4d7953

Please sign in to comment.