From 5e1b04def11dd43422ee81fa9f24771745db6536 Mon Sep 17 00:00:00 2001 From: Msprg <18015488+Msprg@users.noreply.github.com> Date: Tue, 15 Sep 2026 03:14:42 +0200 Subject: [PATCH] pm5: hw bwm wifipower, WiFi off or the modem power-save type New `hw bwm wifipower [off|none|min|max]`. `off` turns the BWM's WiFi fully off (BLE-only, persisted; the same path as `hw bwm wifi stop`, and the module's default state). The other three set the ESP's persisted WiFi modem power-save type (CMD_PM5_BWM_WIFI_PS 0x0182, module commands 2052/2053, esp_wifi_set_ps()) used while WiFi is up: min (the ESP-IDF default, the modem sleeps between DTIM beacons), max (sleeps for the whole listen interval, lowest current, slower to react), none (modem always on, lowest latency). With no argument it reports whether WiFi is up at all and the stored type. Independent of `hw bwm powersave`. The link helper's "cmd failed" diagnostic is now printed only at debug level: a status query while WiFi is off fails by design and was cluttering every call. Verified on hardware: default reads min with WiFi off; none, max and min each set and read back; off tears WiFi down; an unknown value is rejected; max survives a module reboot. Not measured with WiFi associated. Co-Authored-By: Claude Fable 5.1 --- CHANGELOG.md | 1 + armsrc/appmain.c | 29 +++++++++++ armsrc/bwm_wifi.c | 31 ++++++++---- armsrc/bwm_wifi.h | 5 ++ client/src/cmdbwm.c | 120 ++++++++++++++++++++++++++++++++++++++------ doc/commands.json | 20 +++++++- doc/commands.md | 1 + include/pm3_cmd.h | 8 +++ 8 files changed, 190 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff62b6015..b42d632aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hw bwm wifipower` - turn the BWM WiFi fully off, or set its modem power-save type (none/min/max), persisted on the module (@Msprg) - Added `hw bwm powersave` - show/set the BWM (ESP32) power-save switch: DFS, light sleep and slow advertising after 30 s, or the stock always-on behaviour, persisted on the module (@Msprg) - Added `hw powersave` - PM5 low-power idle, on by default: between commands the core drops to 48 MHz with the PLL off, the FPGA clock stopped and the CPU halted (WFI); USB now runs crystal-less off HICK. Ported from the Fantasi firmware (@Msprg) - Fixed `hf mf autopwn` on FM11RF08S - static encrypted nonce is now detected via the backdoor key (@iceman1001) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 26fe723e9..8f668d2b0 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -4317,6 +4317,35 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_PM5_BWM_POWERSAVE, res, &state, (res == PM3_SUCCESS) ? 1 : 0); #else reply_ng(CMD_PM5_BWM_POWERSAVE, PM3_ENOTIMPL, NULL, 0); +#endif + break; + } + case CMD_PM5_BWM_WIFI_PS: { +#ifdef WITH_BWM_FORWARD + // Payload / reply layout: pm3_cmd.h. wifi_state rides along so the host + // can say the type is moot while WiFi is down. + if (packet->length < 1) { + reply_ng(CMD_PM5_BWM_WIFI_PS, PM3_EINVARG, NULL, 0); + break; + } + uint8_t action = packet->data.asBytes[0]; + uint8_t out[2] = { 0, BWM_WIFI_STATE_OFF }; + int res; + if (action == BWM_WIFI_PS_ACTION_GET) { + res = bwm_esp_get_wifi_ps(&out[0]); + } else if (action == BWM_WIFI_PS_ACTION_SET && packet->length >= 2 && packet->data.asBytes[1] <= BWM_WIFI_PS_MAX) { + res = bwm_esp_set_wifi_ps(packet->data.asBytes[1], &out[0]); + } else { + reply_ng(CMD_PM5_BWM_WIFI_PS, PM3_EINVARG, NULL, 0); + break; + } + if (res == PM3_SUCCESS) { + uint32_t ip = 0; + (void)bwm_wifi_forward_status(&out[1], &ip); // 0xFF when the WiFi stack is down + } + reply_ng(CMD_PM5_BWM_WIFI_PS, res, out, (res == PM3_SUCCESS) ? sizeof(out) : 0); +#else + reply_ng(CMD_PM5_BWM_WIFI_PS, PM3_ENOTIMPL, NULL, 0); #endif break; } diff --git a/armsrc/bwm_wifi.c b/armsrc/bwm_wifi.c index 3fa2b8686..122bf4c88 100644 --- a/armsrc/bwm_wifi.c +++ b/armsrc/bwm_wifi.c @@ -160,7 +160,11 @@ int bwm_cmd(uint16_t cmd, const uint8_t *req, uint16_t req_len, esp_err = (int32_t)((uint32_t)pbuf[2] | ((uint32_t)pbuf[3] << 8) | ((uint32_t)pbuf[4] << 16) | ((uint32_t)pbuf[5] << 24)); } - Dbprintf("[bwm-wifi] cmd 0x%04x failed, esp_err=0x%08x", (unsigned)cmd, (unsigned)esp_err); + // Expected in places (e.g. a status query while WiFi is + // off): only worth seeing when debugging the link. + if (g_dbglevel >= DBG_DEBUG) { + Dbprintf("[bwm-wifi] cmd 0x%04x failed, esp_err=0x%08x", (unsigned)cmd, (unsigned)esp_err); + } return PM3_EFAILED; } } @@ -317,25 +321,34 @@ int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen, uint32_t timeout_ms) { return bwm_cmd(BWM_CMD_GET_VERSION_INFO, NULL, 0, buf, buflen, timeout_ms); } -int bwm_esp_get_power_save(uint8_t *state, uint32_t timeout_ms) { +// One u8-in / u8-out ESP setting round trip: send req (NULL/0 for a GET) and +// expect a one-byte reply in *out. PM3_EFAILED if the ESP acked without a payload. +static int bwm_esp_u8_cmd(uint16_t cmd, const uint8_t *req, uint16_t req_len, uint8_t *out, uint32_t timeout_ms) { uint16_t len = 1; - int r = bwm_cmd(BWM_CMD_GET_SYS_POWER_SAVE, NULL, 0, state, &len, timeout_ms); + int r = bwm_cmd(cmd, req, req_len, out, &len, timeout_ms); if (r == PM3_SUCCESS && len < 1) { r = PM3_EFAILED; } return r; } +int bwm_esp_get_power_save(uint8_t *state, uint32_t timeout_ms) { + return bwm_esp_u8_cmd(BWM_CMD_GET_SYS_POWER_SAVE, NULL, 0, state, timeout_ms); +} + int bwm_esp_set_power_save(bool on, uint8_t *state) { // Applied at once on the ESP (no reboot) and saved to its NVS; the reply // carries the state the ESP ended up in. uint8_t v = on ? 1 : 0; - uint16_t len = 1; - int r = bwm_cmd(BWM_CMD_SET_SYS_POWER_SAVE, &v, 1, state, &len, 3000); - if (r == PM3_SUCCESS && len < 1) { - r = PM3_EFAILED; - } - return r; + return bwm_esp_u8_cmd(BWM_CMD_SET_SYS_POWER_SAVE, &v, 1, state, 3000); +} + +int bwm_esp_get_wifi_ps(uint8_t *mode) { + return bwm_esp_u8_cmd(BWM_CMD_GET_WIFI_CFG_PS_MODE, NULL, 0, mode, 3000); +} + +int bwm_esp_set_wifi_ps(uint8_t mode, uint8_t *applied) { + return bwm_esp_u8_cmd(BWM_CMD_SET_WIFI_CFG_PS_MODE, &mode, 1, applied, 3000); } int bwm_esp_get_ble_name(uint8_t *buf, uint16_t *buflen) { diff --git a/armsrc/bwm_wifi.h b/armsrc/bwm_wifi.h index 934bb61dd..63422b996 100644 --- a/armsrc/bwm_wifi.h +++ b/armsrc/bwm_wifi.h @@ -93,6 +93,11 @@ int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out); #define BWM_CMD_SET_SYS_POWER_SAVE 1019 // req: u8 0=off 1=on; resp: u8 applied state #define BWM_CMD_GET_SYS_POWER_SAVE 1020 // resp: u8 state int bwm_esp_get_power_save(uint8_t *state, uint32_t timeout_ms); +// ESP WiFi modem power-save type (0 none, 1 min, 2 max), persisted on the ESP. +#define BWM_CMD_SET_WIFI_CFG_PS_MODE 2052 // req: u8 mode; resp: u8 applied mode +#define BWM_CMD_GET_WIFI_CFG_PS_MODE 2053 // resp: u8 mode +int bwm_esp_get_wifi_ps(uint8_t *mode); +int bwm_esp_set_wifi_ps(uint8_t mode, uint8_t *applied); int bwm_esp_set_power_save(bool on, uint8_t *state); int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen, uint32_t timeout_ms); int bwm_esp_get_ble_name(uint8_t *buf, uint16_t *buflen); diff --git a/client/src/cmdbwm.c b/client/src/cmdbwm.c index 07714cbfc..5ebb5df5f 100644 --- a/client/src/cmdbwm.c +++ b/client/src/cmdbwm.c @@ -697,6 +697,40 @@ static int CmdBwmName(const char *Cmd) { return PM3_SUCCESS; } +// Round trip for the BWM u8 settings (`hw bwm powersave`, `hw bwm wifipower`): +// send [action][value] (value only when has_value) and wait for the ESP's answer. +// Prints the generic failure messages; on PM3_SUCCESS resp holds >= 1 byte. +static int bwm_setting_txn(uint16_t cmd, uint8_t action, uint8_t value, bool has_value, + PacketResponseNG *resp, const char *what) { + uint8_t payload[2] = { action, value }; + clearCommandBuffer(); + SendCommandNG(cmd, payload, has_value ? 2 : 1); + if (WaitForResponseTimeout(cmd, resp, 8000) == false) { + PrintAndLogEx(WARNING, "command timeout (is this a PM5 with a responsive BWM?)"); + return PM3_ETIMEOUT; + } + if (resp->status == PM3_ENOTIMPL) { + PrintAndLogEx(WARNING, "firmware built without BWM link support"); + return resp->status; + } + if (resp->status != PM3_SUCCESS || resp->length < 1) { + PrintAndLogEx(FAILED, "BWM did not answer the %s command (BWM firmware too old?)", what); + return (resp->status != PM3_SUCCESS) ? resp->status : PM3_EFAILED; + } + return PM3_SUCCESS; +} + +static const char *bwm_wifi_state_name(uint8_t state) { + switch (state) { + case 0: return "disconnected"; + case 1: return "connecting"; + case 2: return "connected"; + case 3: return "reconnecting"; + case 4: return "connect task stopped"; + default: return "unknown state"; + } +} + static int CmdBwmPowerSave(const char *Cmd) { // Positional sub-action (no dashes): hw bwm powersave [on | off]; no verb shows the state char verb[16] = {0}; @@ -728,27 +762,84 @@ static int CmdBwmPowerSave(const char *Cmd) { return PM3_EINVARG; } - uint8_t payload[2] = { show ? BWM_POWERSAVE_ACTION_GET : BWM_POWERSAVE_ACTION_SET, on ? 1 : 0 }; - clearCommandBuffer(); - SendCommandNG(CMD_PM5_BWM_POWERSAVE, payload, show ? 1 : 2); PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_PM5_BWM_POWERSAVE, &resp, 5000) == false) { - PrintAndLogEx(WARNING, "command timeout (is this a PM5 with a responsive BWM?)"); - return PM3_ETIMEOUT; - } - if (resp.status == PM3_ENOTIMPL) { - PrintAndLogEx(WARNING, "firmware built without BWM link support"); - return resp.status; - } - if (resp.status != PM3_SUCCESS || resp.length < 1) { - PrintAndLogEx(FAILED, "BWM did not answer the power-save command (BWM firmware too old?)"); - return (resp.status != PM3_SUCCESS) ? resp.status : PM3_EFAILED; + int res = bwm_setting_txn(CMD_PM5_BWM_POWERSAVE, show ? BWM_POWERSAVE_ACTION_GET : BWM_POWERSAVE_ACTION_SET, + on ? 1 : 0, !show, &resp, "power-save"); + if (res != PM3_SUCCESS) { + return res; } bool state = (resp.data.asBytes[0] != 0); PrintAndLogEx(SUCCESS, "BWM power save..... %s", state ? _GREEN_("on") : _YELLOW_("off")); return PM3_SUCCESS; } +static const char *bwm_wifi_ps_name(uint8_t mode) { + switch (mode) { + case BWM_WIFI_PS_NONE: return "none"; + case BWM_WIFI_PS_MIN: return "min"; + case BWM_WIFI_PS_MAX: return "max"; + default: return "?"; + } +} + +static int CmdBwmWifiPower(const char *Cmd) { + // Positional sub-action (no dashes): hw bwm wifipower [off | none | min | max]; no verb shows the state + char verb[16] = {0}; + sscanf(Cmd, "%15s", verb); + int mode = -1; + if (verb[0] == 0) { + mode = -1; + } else if (strcmp(verb, "off") == 0) { + // WiFi fully off: same as `hw bwm wifi stop`. + return CmdBWMWifi("stop"); + } else if (strcmp(verb, "none") == 0) { + mode = BWM_WIFI_PS_NONE; + } else if (strcmp(verb, "min") == 0) { + mode = BWM_WIFI_PS_MIN; + } else if (strcmp(verb, "max") == 0) { + mode = BWM_WIFI_PS_MAX; + } else { + // Not a recognised sub-action: render help (also serves -h), or error on + // a stray token, then stop. + CLIParserContext *ctx; + CLIParserInit(&ctx, "hw bwm wifipower", + "Show or set how much power the BWM (ESP32) spends on WiFi.\n" + _YELLOW_("off") " turns WiFi fully off (BLE-only, persisted; the default state, same as\n" + "`hw bwm wifi stop`). Bring it back with " _YELLOW_("hw bwm wifi --ssid --pwd ") ".\n" + "The other three set the modem power-save type while WiFi is up, stored on the\n" + "BWM: " _GREEN_("min") " (default) sleeps between DTIM beacons, " _YELLOW_("max") " for the whole\n" + "listen interval, " _YELLOW_("none") " never. Independent of " _YELLOW_("hw bwm powersave") ". PM5 only.", + "hw bwm wifipower --> show whether WiFi is up and the power-save type\n" + "hw bwm wifipower off --> WiFi fully off, BLE-only\n" + "hw bwm wifipower none --> modem always on while WiFi is up\n" + "hw bwm wifipower min --> ESP-IDF default (DTIM sleep)\n" + "hw bwm wifipower max --> deepest modem sleep"); + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); + PrintAndLogEx(WARNING, "specify " _YELLOW_("off") ", " _YELLOW_("none") ", " _YELLOW_("min") ", " _YELLOW_("max") " or nothing to show the state"); + return PM3_EINVARG; + } + + PacketResponseNG resp; + int res = bwm_setting_txn(CMD_PM5_BWM_WIFI_PS, (mode < 0) ? BWM_WIFI_PS_ACTION_GET : BWM_WIFI_PS_ACTION_SET, + (mode < 0) ? 0 : (uint8_t)mode, mode >= 0, &resp, "WiFi power"); + if (res != PM3_SUCCESS) { + return res; + } + uint8_t wifi_state = (resp.length >= 2) ? resp.data.asBytes[1] : BWM_WIFI_STATE_OFF; + if (wifi_state == BWM_WIFI_STATE_OFF) { + PrintAndLogEx(SUCCESS, "BWM WiFi................ " _GREEN_("off") " (BLE-only)"); + } else { + PrintAndLogEx(SUCCESS, "BWM WiFi................ " _YELLOW_("on") " (%s)", bwm_wifi_state_name(wifi_state)); + } + PrintAndLogEx(SUCCESS, "BWM WiFi power save..... " _YELLOW_("%s"), bwm_wifi_ps_name(resp.data.asBytes[0])); + return PM3_SUCCESS; +} + static command_t BwmCommandTable[] = { {"help", CmdHelpBwm, AlwaysAvailable, "This help"}, {"autooff", CmdBwmAutoOff, IfBwm, "Toggle auto power-off on USB unplug"}, @@ -759,6 +850,7 @@ static command_t BwmCommandTable[] = { {"upgrade", CmdBWMUpgrade, IfBwm, "Reflash BWM (ESP32) firmware over the BWM link, no header"}, {"vchg", CmdBwmVchg, IfBwm, "Set charger charge-voltage target (default 4100 mV)"}, {"wifi", CmdBWMWifi, IfBwm, "Bring up WiFi (STA + TCP server) for a tcp: connection"}, + {"wifipower", CmdBwmWifiPower, IfBwm, "WiFi fully off, or the modem power-save type (none/min/max)"}, {NULL, NULL, NULL, NULL} }; diff --git a/doc/commands.json b/doc/commands.json index 66197e779..b3a21fc71 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -11145,6 +11145,22 @@ ], "usage": "hw bwm wifi [-h] [--ssid ] [--pwd ] [--port ] [--hostname ]" }, + "hw bwm wifipower": { + "command": "hw bwm wifipower", + "description": "Show or set how much power the BWM (ESP32) spends on WiFi. off turns WiFi fully off (BLE-only, persisted; the default state, same as `hw bwm wifi stop`). Bring it back with hw bwm wifi --ssid --pwd . The other three set the modem power-save type while WiFi is up, stored on the BWM: min (default) sleeps between DTIM beacons, max for the whole listen interval, none never. Independent of hw bwm powersave. PM5 only.", + "notes": [ + "hw bwm wifipower -> show whether WiFi is up and the power-save type", + "hw bwm wifipower off -> WiFi fully off, BLE-only", + "hw bwm wifipower none -> modem always on while WiFi is up", + "hw bwm wifipower min -> ESP-IDF default (DTIM sleep)", + "hw bwm wifipower max -> deepest modem sleep" + ], + "offline": false, + "options": [ + "-h, --help This help" + ], + "usage": "hw bwm wifipower [-h]" + }, "hw connect": { "command": "hw connect", "description": "Connects to a Proxmark3 device via specified serial port. Baudrate here is only for physical UART or UART-BT, NOT for USB-CDC or blue shark add-on", @@ -16678,8 +16694,8 @@ } }, "metadata": { - "commands_extracted": 928, + "commands_extracted": 929, "extracted_by": "PM3Help2JSON v1.00", - "extracted_on": "2026-09-24T08:30:13+00:00" + "extracted_on": "2026-09-24T08:30:49+00:00" } } diff --git a/doc/commands.md b/doc/commands.md index 690ab782a..a58fe479f 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -1056,6 +1056,7 @@ Check column "offline" for their availability. |`hw bwm upgrade `|N |`Reflash BWM (ESP32) firmware over the BWM link, no header` |`hw bwm vchg `|N |`Set charger charge-voltage target (default 4100 mV)` |`hw bwm wifi `|N |`Bring up WiFi (STA + TCP server) for a tcp: connection` +|`hw bwm wifipower `|N |`WiFi fully off, or the modem power-save type (none/min/max)` ### lf diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 0de708f5f..66adbe114 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -931,6 +931,14 @@ typedef struct { #define CMD_PM5_BWM_POWERSAVE 0x0181 // payload: [action:u8][state:u8 if SET]; resp: u8 applied state #define BWM_POWERSAVE_ACTION_GET 0x00 #define BWM_POWERSAVE_ACTION_SET 0x01 +// PM5, BWM WiFi modem power-save type (persisted on the ESP). Used by `hw bwm wifipower`. +#define CMD_PM5_BWM_WIFI_PS 0x0182 // payload: [action:u8][mode:u8 if SET]; resp: [mode:u8][wifi_state:u8, 0xFF = WiFi off] +#define BWM_WIFI_PS_ACTION_GET 0x00 +#define BWM_WIFI_PS_ACTION_SET 0x01 +#define BWM_WIFI_PS_NONE 0 // modem never sleeps +#define BWM_WIFI_PS_MIN 1 // sleeps between DTIM beacons (ESP-IDF default) +#define BWM_WIFI_PS_MAX 2 // sleeps for the listen interval +#define BWM_WIFI_STATE_OFF 0xFF // wifi_state byte: WiFi stack down (BLE-only) #define BWM_OTA_ACTION_BEGIN 0x00 #define BWM_OTA_ACTION_WRITE 0x01 #define BWM_OTA_ACTION_END 0x02