Align segment lengths after merging (#831)

* Align segment length after merging

* Explain the need for pad_alignment
This commit is contained in:
Dániel Buga 2025-04-04 15:33:20 +02:00 committed by GitHub
parent 8137c434d4
commit e6af2af321
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 7 deletions

View File

@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed typos in error variant names (#782)
- Fix `read-flash` which didn't work with some lengths (#804)
- espflash can now flash an ESP32-S2 in download mode over USB (#813)
- Fixed a case where esplash transformed the firmware elf in a way that made it unbootable (#831)
### Removed

View File

@ -213,8 +213,14 @@ impl<'a> IdfBootloaderFormat<'a> {
let mut data = bytes_of(&header).to_vec();
let flash_segments: Vec<_> = merge_adjacent_segments(rom_segments(chip, &elf).collect());
let mut ram_segments: Vec<_> = merge_adjacent_segments(ram_segments(chip, &elf).collect());
// The bootloader needs segments to be 4-byte aligned, but ensuring that
// alignment by padding segments might result in overlapping segments. We
// need to merge adjacent segments first to avoid the possibility of them
// overlapping, and then do the padding.
let flash_segments: Vec<_> =
pad_align_segments(merge_adjacent_segments(rom_segments(chip, &elf).collect()));
let mut ram_segments: Vec<_> =
pad_align_segments(merge_adjacent_segments(ram_segments(chip, &elf).collect()));
let mut checksum = ESP_CHECKSUM_MAGIC;
let mut segment_count = 0;
@ -446,6 +452,11 @@ fn merge_adjacent_segments(mut segments: Vec<Segment<'_>>) -> Vec<Segment<'_>> {
merged
}
fn pad_align_segments(mut segments: Vec<Segment<'_>>) -> Vec<Segment<'_>> {
segments.iter_mut().for_each(|segment| segment.pad_align(4));
segments
}
/// Save a segment to the data buffer.
fn save_flash_segment(
data: &mut Vec<u8>,

View File

@ -33,13 +33,12 @@ pub struct Segment<'a> {
impl<'a> Segment<'a> {
pub fn new(addr: u32, data: &'a [u8]) -> Self {
let mut segment = Segment {
// Do not pad the data here, as it might result in overlapping segments
// in the ELF file. The padding should be done after merging adjacent segments.
Segment {
addr,
data: Cow::Borrowed(data),
};
segment.pad_align(4);
segment
}
}
/// Split of the first `count` bytes into a new segment, adjusting the