From 36d2ab11980695b8b350d3b1419dacad5a417d75 Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Fri, 4 Sep 2026 09:02:09 +0200 Subject: [PATCH] Refactor OTA handling and improve timeout duration Signed-off-by: Niel Nielsen --- client/src/cmdhw.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index a77e0f9cd..08995c69d 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -2309,7 +2309,7 @@ static void progressbar(long sent, long total, int style) { // caller must restart the whole thing. static int bwm_ota_once(const uint8_t *fw, size_t fwlen, uint32_t write_delay_ms) { PacketResponseNG resp; - + clearCommandBuffer(); // BEGIN: tell the BWM how many bytes are coming (it erases the target partition) uint8_t beg[5] = { BWM_OTA_ACTION_BEGIN, (uint8_t)(fwlen & 0xFF), (uint8_t)((fwlen >> 8) & 0xFF), @@ -2329,7 +2329,6 @@ static int bwm_ota_once(const uint8_t *fw, size_t fwlen, uint32_t write_delay_ms return PM3_EMALLOC; } size_t sent = 0; - clearCommandBuffer(); while (sent < fwlen) { size_t n = MIN(maxchunk, fwlen - sent); buf[0] = BWM_OTA_ACTION_WRITE; @@ -2371,17 +2370,12 @@ static int bwm_ota_once(const uint8_t *fw, size_t fwlen, uint32_t write_delay_ms return PM3_SUCCESS; } if (got_end) { - // The BWM answered END with an error. The common one is a size mismatch: - // esp_ota_end() found written < total, i.e. chunks were dropped in transit. - // That is a genuine failure (boot partition NOT switched) - restart, and - // hint at pacing, which is the usual cure. + // The BWM answered END with an error. PrintAndLogEx(WARNING, "OTA finalize rejected (status %d) - data was lost in transit", resp.status); PrintAndLogEx(HINT, "Try a per-write delay: " _YELLOW_("hw bwmupgrade -f --delay 10")); return PM3_EFAILED; } - // No answer at all. Over BLE the END auto-reboot drops the link before the ack - // returns, so a timeout here means "reached END, reboot likely happened" - - // verify by version rather than discarding a possibly-good flash. + // No answer at all. return PM3_ETIMEOUT; } @@ -2391,7 +2385,7 @@ static int bwm_get_version(char *out, size_t outlen) { clearCommandBuffer(); SendCommandNG(CMD_PM5_BWM_ESP_OTA, a, sizeof(a)); PacketResponseNG r; - if ((WaitForResponseTimeout(CMD_PM5_BWM_ESP_OTA, &r, 5000) == false) || (r.status != PM3_SUCCESS)) { + if ((WaitForResponseTimeout(CMD_PM5_BWM_ESP_OTA, &r, 500) == false) || (r.status != PM3_SUCCESS)) { return PM3_EFAILED; } uint16_t n = (r.length < (uint16_t)(outlen - 1)) ? r.length : (uint16_t)(outlen - 1); @@ -2480,11 +2474,8 @@ static int CmdBWMUpgrade(const char *Cmd) { if (res == PM3_ETIMEOUT) { PrintAndLogEx(INFO, "finalize ack not seen - all data was sent, confirming by version..."); } - // The device's OTA_END handler already reboots the ESP into the new image - // (that is what drops the finalize ack over BLE). Just wait for it to come - // back and re-link, then confirm by version. PrintAndLogEx(INFO, "BWM rebooting into the new image (link drops briefly)..."); - msleep(8000); // reboot + re-negotiate baud + re-link + msleep(500); // reboot + re-negotiate baud + re-link char ver_after[64] = {0}; bool have_after = (bwm_get_version(ver_after, sizeof(ver_after)) == PM3_SUCCESS);