Implement firmware image validation for ESP32-C2

Add checks for firmware image validity before flashing.

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
This commit is contained in:
Niel Nielsen
2026-09-08 12:11:32 +02:00
committed by GitHub
parent aef0a76409
commit 14ab313ca7
+8
View File
@@ -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