diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index adf12d0e1..64593f530 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -1930,9 +1930,9 @@ static int CmdPing(const char *Cmd) { uint32_t len = arg_get_u32_def(ctx, 1, 32); CLIParserFree(ctx); - if (len > PM3_CMD_DATA_SIZE) - len = PM3_CMD_DATA_SIZE; - + if (len > pm3_max_cmd_data_size()) + len = pm3_max_cmd_data_size(); + if (len) { PrintAndLogEx(INFO, "Ping sent with payload len... " _YELLOW_("%d"), len); } else { @@ -2294,6 +2294,93 @@ static int CmdPM5QCTest(const char *Cmd) { return PM3_SUCCESS; } +static int CmdBWMUpgrade(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hw bwmupgrade", + "Reflash the BWM (ESP32) firmware over the BWM link - no header, no soldering.\n" + "Requires a BWM that still responds; this updates a wrong-version ESP, it cannot\n" + "recover a fully bricked one (that still needs the 5-pin header + esptool).", + "hw bwmupgrade -f bwm_esp32.bin"); + void *argtable[] = { + arg_param_begin, + arg_str1("f", "file", "", "ESP32 firmware image (.bin)"), + arg_param_end, + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + int fnlen = 0; + char fn[FILE_PATH_SIZE] = {0}; + CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)fn, sizeof(fn), &fnlen); + CLIParserFree(ctx); + + if (fnlen == 0) { + PrintAndLogEx(FAILED, "no filename given"); + return PM3_EINVARG; + } + + uint8_t *fw = NULL; + size_t fwlen = 0; + if ((loadFile_safe(fn, "", (void **)&fw, &fwlen) != PM3_SUCCESS) || (fwlen == 0)) { + PrintAndLogEx(FAILED, "could not read " _YELLOW_("%s"), fn); + return PM3_EFILE; + } + + PacketResponseNG resp; + + // 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), + (uint8_t)((fwlen >> 16) & 0xFF), (uint8_t)((fwlen >> 24) & 0xFF) }; + clearCommandBuffer(); + SendCommandNG(CMD_PM5_BWM_ESP_OTA, beg, sizeof(beg)); + if ((WaitForResponseTimeout(CMD_PM5_BWM_ESP_OTA, &resp, 20000) == false) || (resp.status != PM3_SUCCESS)) { + PrintAndLogEx(FAILED, "OTA begin failed (is a responsive BWM fitted?)"); + free(fw); + return PM3_EFAILED; + } + PrintAndLogEx(INFO, "Uploading " _YELLOW_("%zu") " bytes of ESP firmware over the BWM link...", fwlen); + + // WRITE chunks (one action byte + as much firmware as fits the negotiated frame) + size_t maxchunk = (size_t)pm3_max_cmd_data_size() - 1; + uint8_t *buf = calloc(1, maxchunk + 1); + if (buf == NULL) { + free(fw); + return PM3_EMALLOC; + } + size_t sent = 0; + while (sent < fwlen) { + size_t n = MIN(maxchunk, fwlen - sent); + buf[0] = BWM_OTA_ACTION_WRITE; + memcpy(buf + 1, fw + sent, n); + clearCommandBuffer(); + SendCommandNG(CMD_PM5_BWM_ESP_OTA, buf, (uint16_t)(n + 1)); + if ((WaitForResponseTimeout(CMD_PM5_BWM_ESP_OTA, &resp, 12000) == false) || (resp.status != PM3_SUCCESS)) { + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(FAILED, "OTA write failed at offset %zu", sent); + free(buf); + free(fw); + return PM3_EFAILED; + } + sent += n; + PrintAndLogEx(INPLACE, " %zu / %zu bytes (%zu%%)", sent, fwlen, (sent * 100) / fwlen); + } + free(buf); + PrintAndLogEx(NORMAL, ""); + + // END: finalize + set the new boot partition; the BWM reboots into it + uint8_t end[1] = { BWM_OTA_ACTION_END }; + clearCommandBuffer(); + SendCommandNG(CMD_PM5_BWM_ESP_OTA, end, sizeof(end)); + if ((WaitForResponseTimeout(CMD_PM5_BWM_ESP_OTA, &resp, 30000) == false) || (resp.status != PM3_SUCCESS)) { + PrintAndLogEx(FAILED, "OTA finalize failed"); + free(fw); + return PM3_EFAILED; + } + free(fw); + PrintAndLogEx(SUCCESS, "BWM firmware updated - the BWM will reboot into the new image"); + PrintAndLogEx(HINT, "Give it a few seconds, then re-check with " _YELLOW_("hw status")); + return PM3_SUCCESS; +} + static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, {"-------------", CmdHelp, AlwaysAvailable, "----------------------- " _CYAN_("Operation") " -----------------------"}, @@ -2327,6 +2414,7 @@ static command_t CommandTable[] = { {"bwmcharge", CmdBwmCharge, IfPm5, "Enable/disable BWM battery charging (PM5, one-shot)"}, {"bwmautooff", CmdBwmAutoOff, IfPm5, "Toggle auto power-off on USB unplug (PM5, BWM)"}, {"bwmwifi", CmdBWMWifi, IfPm5, "Bring up BWM WiFi (STA + TCP server) for a tcp: connection (PM5)"}, + {"bwmupgrade", CmdBWMUpgrade, IfPm5, "Reflash BWM (ESP32) firmware over the BWM link, no header (PM5)"}, {"tune", CmdTune, IfPm3Lf, "Measure tuning of device antenna"}, {"decay", CmdDecay, IfPm3Present, "Measure HF antenna decay after field-off"}, {NULL, NULL, NULL, NULL}