From 62f9fa8414d3c0cffb2a5fd292e36cdd7af9f890 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 9 Nov 2026 11:44:12 +0700 Subject: [PATCH] em4x50: only retry a read if it can still end in time Next attempt must fit in 2.5 s, judged by the longest attempt so far. Client waits 5 s for info/dump. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- armsrc/em4x50.c | 16 +++++++++++++--- client/src/cmdlfem4x50.c | 4 ++-- include/em4x50.h | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e2864dd2..4e9c4f595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Added `hf gp info`, `hf gp crs` and `hf gp aids` - read GlobalPlatform card info, list CRS registered applets and find applet AIDs, all without keys (@iceman1001) - Added GlobalPlatform ISD, CRS, ARA-M, telecom, UnionPay and digital key AIDs to `aidlist.json` (@iceman1001) - Changed `trace list -t 14a` - APDUs inside an ISO14443-4 session are named, error status words explained and SW1 SW2 highlighted (@iceman1001) -- Fixed `lf em 4x50 rdbl/info/dump`, `lf search` - RM was sent into the tag data instead of the second listen window on strong, AC-coupled signals, so every command failed; reads now retry within 1 s (@abou28) +- Fixed `lf em 4x50 rdbl/info/dump`, `lf search` - RM was sent into the tag data instead of the second listen window on strong, AC-coupled signals, so every command failed; reads now retry while a new attempt still ends within 2.5 s (@abou28) - Fixed PM5 ssp_clk counter - uninitialised TMR2 input filter could drop edges, skewing ISO14443-A timing and `hf mf nested` (@adrian-stepien) - Changed `lf cotag sim` - add and default to passive mode, improve field drop and activation sequence detection (@towelbyte) - Fixed `lf noralsy clone` and `sim` - CN above 9999999 and years outside 1961-2060 are refused instead of cut (@matrix) diff --git a/armsrc/em4x50.c b/armsrc/em4x50.c index 04bd3b436..927448bb2 100644 --- a/armsrc/em4x50.c +++ b/armsrc/em4x50.c @@ -65,9 +65,10 @@ #define EM4X50_TAG_MAX_NO_BYTES 136 #define EM4X50_TIMEOUT_PULSE_EVAL (4 * EM4X50_T_TAG_FULL_PERIOD * CYCLES2TICKS) // 4bit data periods -// read retries, no new attempt after the window (ms) to stay below the client timeout +// read retries, no new attempt that could end after the window (ms) to stay below the client timeout #define EM4X50_COMMAND_RETRIES 5 -#define EM4X50_T_RETRY_WINDOW 1000 +#define EM4X50_T_RETRY_WINDOW 2500 +#define EM4X50_MS2TICKS 1500 uint8_t g_High = 190; uint8_t g_Low = 60; @@ -914,6 +915,7 @@ static int selective_read(uint32_t addresses, uint32_t *words) { static int login_and_read(const em4x50_data_t *etd, uint32_t addresses, uint32_t *words) { int status = PM3_EFAILED; uint32_t tstart = GetTicks(); + uint32_t tlongest = 0; for (int i = 0; i < EM4X50_COMMAND_RETRIES; i++) { @@ -921,6 +923,7 @@ static int login_and_read(const em4x50_data_t *etd, uint32_t addresses, uint32_t return PM3_EOPABORTED; } + uint32_t tattempt = GetTicks(); status = PM3_SUCCESS; // try to login with given password @@ -930,7 +933,14 @@ static int login_and_read(const em4x50_data_t *etd, uint32_t addresses, uint32_t if (status == PM3_SUCCESS) status = selective_read(addresses, words); - if ((status == PM3_SUCCESS) || (GetTicks() - tstart > EM4X50_T_RETRY_WINDOW * 1500)) { + if (status == PM3_SUCCESS) { + break; + } + + // next attempt must fit, judged by the longest one so far + uint32_t now = GetTicks(); + tlongest = MAX(tlongest, now - tattempt); + if ((now - tstart) + tlongest > EM4X50_T_RETRY_WINDOW * EM4X50_MS2TICKS) { break; } } diff --git a/client/src/cmdlfem4x50.c b/client/src/cmdlfem4x50.c index 8fa3a663e..896159004 100644 --- a/client/src/cmdlfem4x50.c +++ b/client/src/cmdlfem4x50.c @@ -753,7 +753,7 @@ static int CmdEM4x50Info(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_INFO, (uint8_t *)&etd, sizeof(etd)); PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, EM4X50_TIMEOUT_CMD) == false) { + if (WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, EM4X50_TIMEOUT_INFO) == false) { PrintAndLogEx(WARNING, "timeout while waiting for reply"); return PM3_ETIMEOUT; } @@ -876,7 +876,7 @@ static int CmdEM4x50Dump(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_INFO, (uint8_t *)&etd, sizeof(etd)); PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, EM4X50_TIMEOUT_CMD) == false) { + if (WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, EM4X50_TIMEOUT_INFO) == false) { PrintAndLogEx(WARNING, "timeout while waiting for reply"); return PM3_ETIMEOUT; } diff --git a/include/em4x50.h b/include/em4x50.h index c8f7ace15..1ad2a4a6d 100644 --- a/include/em4x50.h +++ b/include/em4x50.h @@ -55,6 +55,7 @@ // misc #define EM4X50_TIMEOUT_CMD 3000 +#define EM4X50_TIMEOUT_INFO 5000 // 34 words, with read retries #define EM4X50_DUMP_FILESIZE 136 typedef struct {