Skip to content

fix(wdt): harden the Teensy 4.x boot guard (#4719 review) - #4732

Merged
zackees merged 1 commit into
masterfrom
fix/wdt-boot-guard-review
Oct 7, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/wdt-boot-guard-review

Conversation

@zackees

@zackees zackees commented Oct 7, 2026

Copy link
Copy Markdown
Member

Follow-up to #4719, which a post-merge review found defects in.

# Defect Fix
1 Boot count survives a reflash Record keyed to image length + FNV-1a of every 16th flash word
2 disable() touches WDOG3 with its clock gate off Uses mxrt1062BootGuardConfigureWdog3, which sets CCM_CCGR5_WDOG3 first
3 Record sits in the OCRAM heap Written only while __brkval is below it; garbage fails check/image key
4 escapeToBootloaderIfLooping() dead on Teensy Crash count moved from RAM into the persistent record
5 watchdogBootGuardWrite differs between stub and mxrt Both always store, via the shared record helpers
6 No fail-safe for the 30 s early WDOG3 markBootHealthy() disables the early timer if the sketch never called begin()
7 Docs and gates mismatch boot_guard.h docs fixed; gate matches the watchdog dispatcher

Evidence (Teensy 4.0, usb 1-1)

(a) FL_WATCHDOG_DEBUG_EARLY_HANG (temporary, not committed): flashed 21:01:41, then hung before USB, and after the 30 s WDOG3 resets it came back in HalfKay with no button press:

21:01:41 usb 1-1: USB disconnect, device number 81
21:02:40 usb 1-1: New USB device found, idVendor=16c0, idProduct=0478

(b) Crash, then reflash: a temporary build (threshold 4) trapped in setup() after usb_init on boots 1 and 2, and boot 3 idled unhealthy at count 3:

21:05:39 New USB device found, idProduct=0483   (boot 1, crash)
21:05:49 New USB device found, idProduct=0483   (boot 2, crash)
21:06:00 New USB device found, idProduct=0483   (boot 3, count 3)

Next I reflashed the normal build (threshold 3). Before this fix it would have inherited count 3 and escaped. It stayed at 0483 and answered the ping (pong, lastResetCause POWER_ON):

21:07:41 idProduct=0478  (loader during flash)
21:07:52 idProduct=0483  (stays up)

(c) bash autoresearch teensy40 --timeout 120 passes (pins 8 -> 22).

Local validation

  • bash lint passes.
  • bash test watchdog passes. New stub tests cover reflash re-keying (1), the persistent crash count (4), write semantics (5) and the early-timer release (6).
  • AutoResearch compiles for teensy40, teensy41, esp32s3, esp32c6 and rp2350.

- Key the OCRAM record to a sampled hash of the flash image, so a reflash
  starts the boot count from 0 instead of escaping on its first boot.
- disable() goes through the WDOG3 helper, which enables the CCM_CCGR5
  clock gate before touching WDOG3 registers.
- Write the record only while the heap break is below it (it sits inside
  the OCRAM heap); stale heap data fails the check word / image key.
- Keep the consecutive crash count in the same reset-persistent record,
  so escapeToBootloaderIfLooping() can actually fire on Teensy.
- watchdogBootGuardWrite() always stores on every platform; the stub now
  uses the shared record helpers.
- markBootHealthy() disables the guard's early WDOG3 timer when the
  sketch never called begin().
- Align boot_guard.h docs and the platform gate with the dispatcher.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 7, 2026

Copy link
Copy Markdown

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: FastLED/FastLED/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0a33d0da-c2ab-45bf-a615-c870e86f49ab
📥 Commits

Reviewing files that changed from the base of the PR and between 2e04eaf and 0d83964.

📒 Files selected for processing (11)
  • src/fl/wdt/boot_guard.h
  • src/fl/wdt/boot_guard_record.h
  • src/fl/wdt/watchdog.cpp.hpp
  • src/fl/wdt/watchdog.h
  • src/platforms/arm/mxrt1062/watchdog_boot_guard_mxrt1062.h
  • src/platforms/arm/mxrt1062/watchdog_mxrt1062.impl.hpp
  • src/platforms/shared/watchdog_boot_guard_noop.hpp
  • src/platforms/stub/watchdog_stub.h
  • src/platforms/stub/watchdog_stub.impl.hpp
  • src/platforms/watchdog_boot_guard.h
  • tests/fl/wdt/watchdog.cpp
 ____________________________________________
< I feel the need - the need for clean code! >
 --------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit d84bf17 into master Oct 7, 2026
118 of 119 checks passed
@zackees
zackees deleted the fix/wdt-boot-guard-review branch November 5, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant