Skip to content

Conversation

@bugadani
Copy link
Contributor

@bugadani bugadani commented Mar 25, 2025

This PR reads chip name from the .elf and prevents flashing if it differs from the detected device.

@bugadani
Copy link
Contributor Author

bugadani commented Mar 25, 2025

I'm honestly not sure where exactly to add the checks. We could modify connect, pass the elf data to that and check where we check the -c argument, but it feels out of scope for connect. On the other hand, now we have to add a check to every place it matters, so the PR is messier than it should be. Ideas?

@MabezDev
Copy link
Member

I think I agree that it's a bit much for connect, but we already have flasher.verify_minimum_revision(), maybe a function on Flasher might make sense here. It might also avoid the Option<&[u8]> for elf data, as we can move that option check outside so that we only call flasher.verify_elf_compat() when there is elf data to verify against?

If you don't like that idea, I also think what you currently have isn't so bad, it's just three checks.

@bugadani bugadani marked this pull request as ready for review March 27, 2025 11:57
@bugadani
Copy link
Contributor Author

I've grouped the two bits of metadata into Metadata. Ideally, I think espflash should have a single Image struct that is parsed once, then used to extract metadata & symbols, but that's a bigger refactor than I'd like. Perf-wise we read all symbols twice because we extract Metadata twice, as opposed to looking up two symbols using linear search, so in the worst case we are in the same place.

@bugadani bugadani requested a review from Copilot March 28, 2025 15:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds chip detection via metadata extracted from the ELF file to ensure that the firmware being flashed matches the target device. Key changes include:

  • Introduction of a new Metadata module to parse metadata from the ELF file.
  • Integration of chip detection in both the CLI and cargo tools via an added ensure_chip_compatibility function.
  • Addition of a new FirmwareChipMismatch error variant to handle chip mismatches.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
espflash/src/image_format/mod.rs Re-exports the new Metadata module.
espflash/src/image_format/metadata.rs Implements Metadata parsing from the ELF’s .espressif.metadata section.
espflash/src/error.rs Adds a new FirmwareChipMismatch error variant.
espflash/src/cli/monitor/mod.rs Updates log format detection to use the Metadata instead of raw symbols.
espflash/src/cli/mod.rs Adds the ensure_chip_compatibility function and updates bin writing logic.
cargo-espflash/src/main.rs Integrates chip compatibility check into the flashing workflow.
CHANGELOG.md Updates the changelog to reflect the addition of chip detection.
Comments suppressed due to low confidence (1)

espflash/src/cli/mod.rs:1090

  • [nitpick] The variable 'elf_chip' is reused (shadowed) in the match block, which can be confusing. Consider renaming the parsed output (for example, to 'parsed_chip') for clarity.
match Chip::from_str(elf_chip, false) {

Move metadata handling out of Symbols

Clippy
Copy link
Member

@SergioGasquez SergioGasquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks


mod line_endings;
mod symbols;
pub(crate) mod symbols;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if I've missed something, but why does the visibility of this module need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't I just forgot to undo it, thanks

@jessebraham jessebraham enabled auto-merge April 1, 2025 08:08
@jessebraham jessebraham added this pull request to the merge queue Apr 1, 2025
Merged via the queue into main with commit 8aa91ef Apr 1, 2025
39 checks passed
@bugadani bugadani deleted the chip-check branch April 1, 2025 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants