Skip to content

fix: Check status byte for errors#110

Open
emolitor wants to merge 2 commits into
ch32-rs:mainfrom
emolitor:fix/silent-program-failure
Open

fix: Check status byte for errors#110
emolitor wants to merge 2 commits into
ch32-rs:mainfrom
emolitor:fix/silent-program-failure

Conversation

@emolitor

Copy link
Copy Markdown

The ISP response parser and code-flash both ignored error status. This allows a rejected operation to be reported as a successful.

  • Response::from_raw() had if true returning Response::Ok for any correctly structured reply regardless of the status byte. Honour the status byte: 0x00 (OK) and 0x82 (OK with Data) -> Ok, anything else -> Err. Resolves the existing FIXME.
  • flash_chunk() only checks the response structure, not the per chunk status byte carried in payload[0]. A rejected Program returns a reply with payload[0] != 0 which should be an error but was treated as s success.

The ISP response parser and the code-flash program loop both ignored
error status, so a rejected operation could be reported as a successful
flash.

- Response::from_raw() had `if true`, returning Response::Ok for any
  correctly-framed reply regardless of the status byte (raw[1]). Honor
  the status byte: 0x00 (and 0x82, ok-with-data) -> Ok, anything else
  -> Err. Resolves the existing FIXME.
- flash_chunk() only checked response framing, not the per-chunk Program
  result carried in the payload (payload[0]). A rejected Program returns
  a well-framed reply with payload[0] != 0 yet was treated as success.
  Check payload[0] and fail with the reported status.

Without these, `wchisp flash` can print "bytes written" / "Verify OK"
for a device the BootROM never actually programmed.
Copilot AI review requested due to automatic review settings June 27, 2026 10:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes error handling in the WCH ISP response parsing and code flashing paths by honoring protocol status bytes that were previously ignored, preventing failed operations from being reported as successful.

Changes:

  • Update Response::from_raw() to validate framing and interpret the response status byte (0x00/0x82 => Ok, otherwise Err variant).
  • Update flash_chunk() to also validate the per-chunk Program result encoded in payload[0] and fail on non-zero status.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/protocol.rs Honors the response status byte when parsing raw ISP replies so error replies no longer appear successful.
src/flashing.rs Detects per-chunk BootROM Program rejection via payload[0] so rejected flashes are surfaced as errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/protocol.rs Outdated
Comment thread src/flashing.rs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@emolitor

Copy link
Copy Markdown
Author

@andelf could you take a look when you have a chance?

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.

2 participants