From 14ab313ca718265ac0047dadbf233302008d2ead Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Tue, 8 Sep 2026 12:11:32 +0200 Subject: [PATCH 1/3] Implement firmware image validation for ESP32-C2 Add checks for firmware image validity before flashing. Signed-off-by: Niel Nielsen --- client/src/cmdhw.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index 062cbd815..f996c5fe5 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -2534,6 +2534,7 @@ static int CmdBWMUpgrade(const char *Cmd) { // BWM ESP is an ESP32-C2; a wrong/other-chip image would brick it. // [0x00] == 0xE9 -> ESP image magic // [0x0C..0x0D] == 0x000C -> chip_id ESP32-C2 (LE uint16) + // [0xABCD5432..0x20] == 0x000C -> app descriptor (LE uint32) if (fwlen < 16) { PrintAndLogEx(FAILED, "file is too small to be an ESP firmware image (%zu bytes)", fwlen); free(fw); @@ -2550,6 +2551,13 @@ static int CmdBWMUpgrade(const char *Cmd) { free(fw); return PM3_EFILE; } + + uint32_t app_sign = (uint32_t)(fw[0x20] | (fw[0x21] << 8) | (fw[0x22] << 16) | ((uint32_t)fw[0x23] << 24)); + if (app_sign != 0xABCD5432) { + PrintAndLogEx(FAILED, "refusing to flash: image app_sign " _YELLOW_("0x%08X") " is invalid (expected 0xABCD5432)", app_sign); + free(fw); + return PM3_EFILE; + } // Record the running version first, so we can confirm the update actually took // even when the finalize ack is lost (the case that used to discard a completed From b0aa484eba8848be78bcde0dc5ed18e1eaa23107 Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Tue, 8 Sep 2026 12:17:34 +0200 Subject: [PATCH 2/3] Fix comment for app descriptor in cmdhw.c Signed-off-by: Niel Nielsen --- client/src/cmdhw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index f996c5fe5..831b44eb0 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -2534,7 +2534,7 @@ static int CmdBWMUpgrade(const char *Cmd) { // BWM ESP is an ESP32-C2; a wrong/other-chip image would brick it. // [0x00] == 0xE9 -> ESP image magic // [0x0C..0x0D] == 0x000C -> chip_id ESP32-C2 (LE uint16) - // [0xABCD5432..0x20] == 0x000C -> app descriptor (LE uint32) + // [0xABCD5432..0x20] == 0xABCD5432 -> app descriptor (LE uint32) if (fwlen < 16) { PrintAndLogEx(FAILED, "file is too small to be an ESP firmware image (%zu bytes)", fwlen); free(fw); From 6e84d4ca8bfdda5f3c577ad90a01ad0eeac96733 Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Tue, 8 Sep 2026 13:35:48 +0200 Subject: [PATCH 3/3] Refactor chip ID and app signature checks Signed-off-by: Niel Nielsen --- client/src/cmdhw.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index 831b44eb0..7d33f7245 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -2545,15 +2545,18 @@ static int CmdBWMUpgrade(const char *Cmd) { free(fw); return PM3_EFILE; } - uint16_t chip_id = (uint16_t)(fw[0x0C] | (fw[0x0D] << 8)); + + // Check Chip ID at offset 0x0C (2 bytes, little-endian) + uint16_t chip_id = MemLeToUint2byte(fw + 0x0C); if (chip_id != 0x000C) { PrintAndLogEx(FAILED, "refusing to flash: image chip_id " _YELLOW_("0x%04X") " is not ESP32-C2 (0x000C)", chip_id); free(fw); return PM3_EFILE; } - uint32_t app_sign = (uint32_t)(fw[0x20] | (fw[0x21] << 8) | (fw[0x22] << 16) | ((uint32_t)fw[0x23] << 24)); - if (app_sign != 0xABCD5432) { + // Check Application Signature at offset 0x20 (4 bytes, little-endian) + uint32_t app_sign = MemLeToUint4byte(fw + 0x20); + if (app_sign != 0xABCD5432) { PrintAndLogEx(FAILED, "refusing to flash: image app_sign " _YELLOW_("0x%08X") " is invalid (expected 0xABCD5432)", app_sign); free(fw); return PM3_EFILE;