From 0aef94c667a08f78fc1a2a74e9497c5277d03b6c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 4 Sep 2026 15:18:16 +0200 Subject: [PATCH 1/3] fix 'lf hitag reader -@' mode with sending a break afterwards. --- client/src/cmdlfhitag.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/client/src/cmdlfhitag.c b/client/src/cmdlfhitag.c index 296893856..aa421a0ac 100644 --- a/client/src/cmdlfhitag.c +++ b/client/src/cmdlfhitag.c @@ -820,7 +820,7 @@ static const char *identify_transponder_hitag2(uint32_t uid) { return ""; } -static bool ht2_get_uid(uint32_t *uid) { +static bool ht2_get_uid(uint32_t *uid, bool verbose) { lf_hitag_data_t packet; memset(&packet, 0, sizeof(packet)); @@ -830,7 +830,10 @@ static bool ht2_get_uid(uint32_t *uid) { SendCommandNG(CMD_LF_HITAG_READER, (uint8_t *) &packet, sizeof(packet)); PacketResponseNG resp; if (WaitForResponseTimeout(CMD_LF_HITAG_READER, &resp, 1500) == false) { - PrintAndLogEx(WARNING, "timeout while waiting for reply"); + if (verbose) { + PrintAndLogEx(WARNING, "timeout while waiting for reply"); + } + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); return false; } @@ -902,8 +905,6 @@ static int CmdLFHitagInfo(const char *Cmd) { CLIGetHexWithReturn(ctx, 1, userkey, &ukeylen); CLIParserFree(ctx); - // The length picks the mode, so a wrong one is worth catching here rather - // than sending it and reporting a puzzling authentication failure. if ((ukeylen != 0) && (ukeylen != HITAG_PASSWORD_SIZE) && (ukeylen != HITAG_CRYPTOKEY_SIZE)) { PrintAndLogEx(ERR, "Key must be %u bytes (password) or %u bytes (crypto), got %i", HITAG_PASSWORD_SIZE, HITAG_CRYPTOKEY_SIZE, ukeylen); @@ -912,22 +913,11 @@ static int CmdLFHitagInfo(const char *Cmd) { // read UID uint32_t uid = 0; - if (ht2_get_uid(&uid) == false) { + if (ht2_get_uid(&uid, true) == false) { return PM3_ESOFT; } - // Read the configuration byte instead of guessing it. - // - // This used to print a hardcoded 0x06, so every tag was reported as - // "Password mode" whatever it actually was - a crypto mode card sitting on - // the antenna was described as password mode, which is worse than saying - // nothing. The configuration lives in page 3 and page 3 needs - // authentication, so try the two factory defaults and report what comes - // back; if neither opens the tag, say the mode is unknown rather than - // inventing one, and point at the command whose job searching actually is. - // - // Deliberately only the two factory values: running the dictionary here was - // tried and removed. `info` should be quick and answer from the tag itself, - // and a key search is what `lf hitag chk` exists for. + // Read the configuration byte may need password or key. + // Check only the two factory values static const uint8_t default_pwd[HITAG_PASSWORD_SIZE] = {0x4D, 0x49, 0x4B, 0x52}; // "MIKR" static const uint8_t default_key[HITAG_CRYPTOKEY_SIZE] = {0x4F, 0x4E, 0x4D, 0x49, 0x4B, 0x52}; // "ONMIKR" @@ -1006,11 +996,16 @@ static int CmdLFHitagReader(const char *Cmd) { do { // read UID uint32_t uid = 0; - if (ht2_get_uid(&uid)) { + if (ht2_get_uid(&uid, (cm == false))) { PrintAndLogEx(SUCCESS, "UID.... " _GREEN_("%08X"), uid); } } while (cm && (kbd_enter_pressed() == false)); + // Stop the device before returning. + if (cm) { + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); + } + return PM3_SUCCESS; } @@ -1610,10 +1605,7 @@ static int CmdLFHitag2Restore(const char *Cmd) { free(dump); - // Report the credential the tag wants NOW, which follows the configuration - // just written - not the one used to get in. Restoring a crypto dump onto a - // password tag changes which of the two applies, and getting this wrong locks - // the operator out of a tag they just wrote. + // report back which key/pwd was just set PrintAndLogEx(NORMAL, ""); if ((new_cfg & 0x08) != 0) { PrintAndLogEx(SUCCESS, "Tag is in " _GREEN_("Crypto") " mode"); @@ -2861,7 +2853,7 @@ static int CmdLFHitag2Selftest(const char *Cmd) { int ht2_read_uid(void) { uint32_t uid = 0; - if (ht2_get_uid(&uid) == false) { + if (ht2_get_uid(&uid, true) == false) { return PM3_ESOFT; } From 9a958880fff98542b5b2792543f024aa6ae11754 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 4 Sep 2026 15:21:19 +0200 Subject: [PATCH 2/3] fix bad txframe variable --- armsrc/hitag2.c | 62 ++----------------------------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index 83c03b7ac..73cd36860 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -3258,8 +3258,6 @@ static uint32_t g_ht2_per_n; static void ht2_normalise_head(uint8_t *nrz_samples, size_t *nrzs); void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) { - uint32_t d_txreal[4] = {0}, d_txnom[4] = {0}, d_txbits[4] = {0}; - uint8_t d_txn = 0; uint32_t d_norx = 0, d_nodec = 0; uint16_t d_lastn = 0; @@ -3435,25 +3433,8 @@ void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) { int16_t checked = 0; uint32_t signal_size = 10000; - // TEMP INSTRUMENT: how long one pass of this loop actually takes, so the USB - // poll interval below can be argued from a measurement rather than a guess. - uint32_t d_iter = 0, d_max = 0; - uint16_t d_t0 = GetPrecisionCounterRaw(), d_prev = 0; - while (bStop == false && BUTTON_PRESS() == false) { - { - uint16_t now = GetPrecisionCounterRaw(); - if (d_iter != 0) { - uint32_t dt = (uint16_t)(now - d_prev) / T0; - if (dt > d_max) { - d_max = dt; - } - } - d_prev = now; - d_iter++; - } - // use malloc initSampleBufferEx(&signal_size, true); @@ -3525,21 +3506,7 @@ void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) { } // Transmit the reader frame - // TEMP INSTRUMENT: how long the frame really took, against the nominal - // count hitag2_reader_send_frame() returns. The bit loop waits on ADC - // sample counts, so if it is running slow it misses samples and each - // "period" costs more real time - which stretches the frame on the air. - { - uint16_t t_tx = GetPrecisionCounterRaw(); - command_duration = hitag2_reader_send_frame(tx, txlen); - uint32_t real = (uint16_t)(GetPrecisionCounterRaw() - t_tx) / T0; - if (d_txn < 4) { - d_txreal[d_txn] = real; - d_txnom[d_txn] = command_duration; - d_txbits[d_txn] = txlen; - d_txn++; - } - } + command_duration = hitag2_reader_send_frame(tx, txlen); response_start = command_start + command_duration; // Let the antenna and ADC values settle @@ -3662,12 +3629,6 @@ void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) { out: DBG Dbprintf("RXFAIL no_signal=%u undecodable=%u last_samples=%u", d_norx, d_nodec, d_lastn); - DBG for (uint8_t i = 0; i < d_txn; i++) { - Dbprintf("TXFRAME %u bits: real=%u T0 nominal=%u T0", d_txbits[i], d_txreal[i], d_txnom[i]); - } - - DBG Dbprintf("RDRLOOP passes=%u span=%u T0 max_pass=%u T0", - d_iter, (unsigned)((uint16_t)(GetPrecisionCounterRaw() - d_t0) / T0), d_max); StopTimestamp(); lf_finalize(ledcontrol); @@ -3688,8 +3649,6 @@ out: } void WriterHitag(const lf_hitag_data_t *payload, bool ledcontrol) { - uint32_t d_txreal[4] = {0}, d_txnom[4] = {0}, d_txbits[4] = {0}; - uint8_t d_txn = 0; // TEMP INSTRUMENT: why an answer was not taken. d_noans counts silent // windows, d_badn the decodes that failed and d_badnrz their sample counts. uint16_t d_noans = 0, d_badn = 0, d_badnrz[6] = {0}, d_badraw[6] = {0}; @@ -3872,21 +3831,7 @@ void WriterHitag(const lf_hitag_data_t *payload, bool ledcontrol) { } // Transmit the reader frame - // TEMP INSTRUMENT: how long the frame really took, against the nominal - // count hitag2_reader_send_frame() returns. The bit loop waits on ADC - // sample counts, so if it is running slow it misses samples and each - // "period" costs more real time - which stretches the frame on the air. - { - uint16_t t_tx = GetPrecisionCounterRaw(); - command_duration = hitag2_reader_send_frame(tx, txlen); - uint32_t real = (uint16_t)(GetPrecisionCounterRaw() - t_tx) / T0; - if (d_txn < 4) { - d_txreal[d_txn] = real; - d_txnom[d_txn] = command_duration; - d_txbits[d_txn] = txlen; - d_txn++; - } - } + command_duration = hitag2_reader_send_frame(tx, txlen); // global write state variable used // tearoff occurred @@ -4015,9 +3960,6 @@ void WriterHitag(const lf_hitag_data_t *payload, bool ledcontrol) { } out: - DBG for (uint8_t i = 0; i < d_txn; i++) { - Dbprintf("TXFRAME %u bits: real=%u T0 nominal=%u T0", d_txbits[i], d_txreal[i], d_txnom[i]); - } if (d_badpern) { DBG Dbprintf("WRXPER %u %u %u %u %u %u %u %u %u %u %u %u", d_badper[0], d_badper[1], d_badper[2], d_badper[3], From 2025e49c097ed78037a6dbb50a17680ba412506c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 4 Sep 2026 15:34:30 +0200 Subject: [PATCH 3/3] added missing clock fct for pm5 --- common_arm/ticks/ticks_hw_at32.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common_arm/ticks/ticks_hw_at32.c b/common_arm/ticks/ticks_hw_at32.c index d063e6399..b3fda5982 100644 --- a/common_arm/ticks/ticks_hw_at32.c +++ b/common_arm/ticks/ticks_hw_at32.c @@ -232,6 +232,20 @@ uint16_t RAMFUNC GetPrecisionCounter(void) { return (uint16_t)tmr_counter_value_get(AT32_TMR_PRECISE_COUNTER); } +// The free running counter itself, and the distance from a captured value. +// +// Declared in ticks_apis.h and used by the Hitag paths, which need a reference +// they can subtract from rather than a value relative to the last reset. These +// were added for AT91 without an AT32 counterpart, which left the PM5 build +// failing to link with undefined references to both. +uint16_t RAMFUNC GetPrecisionCounterRaw(void) { + return (uint16_t)tmr_counter_value_get(AT32_TMR_PRECISE_COUNTER); +} + +uint16_t RAMFUNC GetPrecisionCounterDelta(uint16_t start) { + return (uint16_t)((uint16_t)tmr_counter_value_get(AT32_TMR_PRECISE_COUNTER) - start); +} + void StartLoEdgeCapture(void) { crm_periph_clock_enable(CRM_GPIO_PERIPH_INPUT_CAPTURE, TRUE); crm_periph_clock_enable(AT32_CRM_TMR_PERIPH_INPUT_CAPTURE, TRUE);