fix(mifare): propagate GDM write failures

Return device-side write errors from GDM commands instead of reporting success, and handle write timeouts before inspecting response status.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
This commit is contained in:
歐歪andOpenAI Codex committed 2026-09-16 03:55:32 +08:00
1 parent 0884083530
commit 2ca0514389
1 file changed
+38 -19
+38 -19
View File
@@ -9628,7 +9628,7 @@ static int gdm_write_config(uint8_t wakeup, uint8_t auth_cmd, const uint8_t *key
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write config. Status: %d", resp.status);
return PM3_EFAILED;
return resp.status;
}
return PM3_SUCCESS;
}
@@ -10000,10 +10000,12 @@ static int CmdHF14AGen4_GDM_SetCfg(const char *Cmd) {
if (resp.status == PM3_SUCCESS) {
PrintAndLogEx(SUCCESS, "Write ( " _GREEN_("ok") " )");
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf mf gdmgetcfg") "` to verify");
} else if (resp.status == PM3_ETEAROFF) {
PrintAndLogEx(INFO, "Tear off triggered");
} else {
PrintAndLogEx(FAILED, "Write ( " _RED_("fail") " )");
}
return PM3_SUCCESS;
return resp.status;
}
// Config flag mode
@@ -10076,8 +10078,9 @@ static int CmdHF14AGen4_GDM_SetCfg(const char *Cmd) {
}
// Step 2: Write config
if (gdm_write_config(wakeup_type, auth_cmd, key, config) != PM3_SUCCESS) {
return PM3_EFAILED;
int res = gdm_write_config(wakeup_type, auth_cmd, key, config);
if (res != PM3_SUCCESS) {
return res;
}
PrintAndLogEx(SUCCESS, "Config updated successfully");
@@ -10182,7 +10185,7 @@ static int CmdHF14AGen4_GDM_SetBlk(const char *Cmd) {
} else {
PrintAndLogEx(FAILED, "Write ( " _RED_("fail") " )");
}
return PM3_SUCCESS;
return resp.status;
}
static int CmdHF14AGen4_GDM_SetHidBlk(const char *Cmd) {
@@ -10327,7 +10330,7 @@ static int CmdHF14AGen4_GDM_SetHidBlk(const char *Cmd) {
} else {
PrintAndLogEx(FAILED, "Write ( " _RED_("fail") " )");
}
return PM3_SUCCESS;
return resp.status;
}
// Shared worker for gdmgetblk / gdmgethidblk: reads a single block (--blk) or a
@@ -10579,8 +10582,9 @@ static int CmdHF14AGen4_GDM_SetUid(const char *Cmd) {
// Step 3: Write config if needed
if (update_config) {
if (gdm_write_config(wakeup_type, auth_cmd, key, config) != PM3_SUCCESS) {
return PM3_EFAILED;
int res = gdm_write_config(wakeup_type, auth_cmd, key, config);
if (res != PM3_SUCCESS) {
return res;
}
PrintAndLogEx(SUCCESS, "Config updated successfully");
}
@@ -10595,7 +10599,7 @@ static int CmdHF14AGen4_GDM_SetUid(const char *Cmd) {
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write hidden block 0. Status: %d", resp.status);
return PM3_EFAILED;
return resp.status;
}
PrintAndLogEx(SUCCESS, "Hidden block 0 updated successfully");
}
@@ -10609,7 +10613,7 @@ static int CmdHF14AGen4_GDM_SetUid(const char *Cmd) {
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write real block %d. Status: %d", write_payload.block_no, resp.status);
return PM3_EFAILED;
return resp.status;
}
PrintAndLogEx(SUCCESS, "Real block %d updated successfully", write_payload.block_no);
@@ -10679,7 +10683,9 @@ static int CmdHF14AGen4_GDM_Wipe(const char *Cmd) {
clearCommandBuffer();
SendCommandNG(CMD_HF_MIFARE_WRITEBL_EX, (uint8_t *)&config_payload, sizeof(config_payload));
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false || resp.status != PM3_SUCCESS) {
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "Failed to write standard config: timeout");
} else if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write standard config. Status: %d", resp.status);
} else {
PrintAndLogEx(SUCCESS, "Standard config written");
@@ -10815,9 +10821,13 @@ static int CmdHF14AGen4_GDM_SetSig(const char *Cmd) {
clearCommandBuffer();
SendCommandNG(CMD_HF_MIFARE_WRITEBL_EX, (uint8_t *)&write_payload, sizeof(write_payload));
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false || resp.status != PM3_SUCCESS) {
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "Failed to write signature part 1 (block 5): timeout");
return PM3_ETIMEOUT;
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write signature part 1 (block 5). Status: %d", resp.status);
return PM3_EFAILED;
return resp.status;
}
PrintAndLogEx(SUCCESS, "Signature part 1 written to block 5");
@@ -10827,9 +10837,13 @@ static int CmdHF14AGen4_GDM_SetSig(const char *Cmd) {
clearCommandBuffer();
SendCommandNG(CMD_HF_MIFARE_WRITEBL_EX, (uint8_t *)&write_payload, sizeof(write_payload));
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false || resp.status != PM3_SUCCESS) {
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "Failed to write signature part 2 (block 6): timeout");
return PM3_ETIMEOUT;
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write signature part 2 (block 6). Status: %d", resp.status);
return PM3_EFAILED;
return resp.status;
}
PrintAndLogEx(SUCCESS, "Signature part 2 written to block 6");
@@ -10841,8 +10855,9 @@ static int CmdHF14AGen4_GDM_SetSig(const char *Cmd) {
if (config[13] != 0x5A) {
config[13] = 0x5A;
if (gdm_write_config(wakeup_type, auth_cmd, key, config) != PM3_SUCCESS) {
return PM3_EFAILED;
int res = gdm_write_config(wakeup_type, auth_cmd, key, config);
if (res != PM3_SUCCESS) {
return res;
}
PrintAndLogEx(SUCCESS, "Enabled signature sector in config (config[13] = 0x5A)");
} else {
@@ -10861,9 +10876,13 @@ static int CmdHF14AGen4_GDM_SetSig(const char *Cmd) {
clearCommandBuffer();
SendCommandNG(CMD_HF_MIFARE_WRITEBL_EX, (uint8_t *)&trailer_payload, sizeof(trailer_payload));
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false || resp.status != PM3_SUCCESS) {
if (WaitForResponseTimeout(CMD_HF_MIFARE_WRITEBL_EX, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "Failed to write signature sector trailer (hidden block 7): timeout");
return PM3_ETIMEOUT;
}
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "Failed to write signature sector trailer (hidden block 7). Status: %d", resp.status);
return PM3_EFAILED;
return resp.status;
}
PrintAndLogEx(SUCCESS, "Signature sector trailer written to hidden block 7");