Fix BLE firmware flashing through USB IAP - #24
Open
hitsmaxft wants to merge 1 commit into
Open
Conversation
hitsmaxft
force-pushed
the
codex/ble-iap-hardening
branch
from
July 25, 2026 21:16
bf4b8c4 to
158aa04
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why this change
This PR primarily fixes BLE firmware flashing through the Anne Pro 2 USB IAP firmware.
The tool was originally documented as tested only for the main MCU. Although the CLI accepted
--target ble, the BLE path did not validate the IAP transaction results: it used a fixed base, read whichever HID report arrived next, ignored the device status, continued after failed chunks, and could wait forever for a missing reply. It also wrote the main-MCU AP flag after every transfer, including BLE transfers.As a result,
Flash completeonly meant that USB writes had been attempted. It did not reliably mean that the BLE erase and every BLE flash chunk had been accepted by the keyboard.This change makes the BLE path a checked request/reply transaction and was validated on a real Anne Pro 2 C18. The corrected tool transferred a complete 155,648-byte BLE 2.13 image, the BLE firmware booted, macOS connected to it, and normal keyboard input worked over Bluetooth.
The same transaction handling also makes main and LED target failures safer, but BLE flashing is the reason for and primary validated use case of this PR.
What was wrong with BLE flashing
0x4000write()resultFlash completeOn the tested C18, the IAP-reported BLE transport base is also
0x4000. The fix is not based on claiming that this constant was wrong for that board; it removes an unchecked assumption and rejects a conflicting explicit base on other IAP layouts.How the BLE path works after this PR
--baseis accepted only when it matches that value.--boot, restart through the main-target mode-2 transition used by ObinsKit, without the old unconditional AP-flag write.--probeexposes the detected layout and target modes without writing flash, which makes the address and IAP state inspectable before an update.Real-hardware validation
The BLE flashing implementation was tested on an Anne Pro 2 C18 using a 155,648-byte BLE 2.13 image:
0x4000;After that hardware run, the final commit added only complete-image pre-read, empty-image rejection, address-span validation, and their host tests. These checks run before erase and do not change the validated erase/write transaction sequence for a valid image.
Host-side checks on the PR head:
cargo fmt -- --checkcargo test— 10 tests passedcargo clippy --all-targets -- -D warningsgit diff --checknix flake check --no-buildVerification boundary
The IAP protocol still has no verified flash-readback command. Matching status-zero replies prove that the keyboard IAP accepted the BLE erase/write transactions; they do not prove byte-for-byte flash contents by themselves. The subsequent BLE boot, advertisement, macOS connection, and Bluetooth keyboard input provide separate runtime validation that the flashed BLE firmware worked on the tested C18.
LED-target hardware behavior has not been independently validated.