From 5ae1a014ff01bd55256a8e77e5964b289a943e3d Mon Sep 17 00:00:00 2001 From: MangaValk Date: Sat, 14 Nov 2026 23:21:18 +0100 Subject: [PATCH] 2026 dongle: pre-erase link gate + race SignalRGB for the COM port - dongle_flash.py aborts before erasing unless the ROM-bootloader link tests clean (8/8 GETs), so a flaky software-jump link can't wipe a working stick. - open_racing() retries opening the port to win the race against SignalRGB, which grabs CH340 ports on enumeration. Co-Authored-By: Claude Opus 4.8 --- 2026-current/dongle/tools/dongle_flash.py | 38 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/2026-current/dongle/tools/dongle_flash.py b/2026-current/dongle/tools/dongle_flash.py index d828a95..0ee9393 100644 --- a/2026-current/dongle/tools/dongle_flash.py +++ b/2026-current/dongle/tools/dongle_flash.py @@ -7,16 +7,28 @@ ACK, NACK = 0x79, 0x1F FLASH = 0x08000000 +def open_racing(port, open_timeout=20.0): + """Keep retrying to open the port - wins the race against programs (e.g. + SignalRGB) that also grab CH340 COM ports on enumeration.""" + t = time.time() + while True: + try: + return serial.Serial(port, 115200, parity=serial.PARITY_EVEN, + stopbits=1, bytesize=8, timeout=1.0) + except serial.SerialException: + if time.time() - t > open_timeout: + raise + time.sleep(0.02) + + class BL: def __init__(self, port): self.port = port - self.s = serial.Serial(port, 115200, parity=serial.PARITY_EVEN, - stopbits=1, bytesize=8, timeout=1.0) + self.s = open_racing(port) def reopen(self): self.s.close(); time.sleep(0.3) - self.s = serial.Serial(self.port, 115200, parity=serial.PARITY_EVEN, - stopbits=1, bytesize=8, timeout=1.0) + self.s = open_racing(self.port) def _wait_ack(self, what, tmo=3.0): self.s.timeout = tmo @@ -128,6 +140,24 @@ def main(): bl.s.reset_input_buffer() print("sync ok") + # Safety gate: the ROM bootloader auto-baud can come back garbled after a + # software jump. Prove the link is solid (several clean GETs) BEFORE erasing, + # so a flaky link aborts without wiping a working firmware. + good = 0 + for _ in range(8): + try: + d = bl.get() + if len(d) >= 2 and d[1:].count(0x44) and d[1:].count(0x31): + good += 1 + except Exception: + pass + time.sleep(0.02) + if good < 8: + print("link not reliable (%d/8 clean) - aborting BEFORE erase." % good) + print("use the BOOT0 wire + a power-cycle for a clean ROM-bootloader link.") + return 2 + print("link solid (%d/8)." % good) + # Erase. On an unlocked chip a mass-erase is enough; if it's still locked # (factory RDP1) the erase NACKs, so fall back to readout-unprotect. try: