fix SpinDelayUsPrecision() clock, no more wrapping 43ms> issues. Still capped at 1390ms. This affects a bunch of tearoff attacks 14b especially win 100ms actually being 16ms wrapped. Should be handled according to new HAL layer. Next things is rate renegotiated at reconnect for sim module. Makes things a more stable if something fails. Thanks Claude!

This commit is contained in:
iceman1001 committed 2026-08-29 06:42:17 +02:00
1 parent 41c1809404
commit 9b61ef867c
5 files changed
+133 -37

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
## [unreleased][unreleased]
- Fixed clock `SpinDelayUsPrecision` to not wrap. Now more 43ms issues (@iceman1001)
- Changed `hf felica raw` - now can stream per-window envelope for better measurments (@iceman1001)
- Updated `fpga_pm3_felica.bit` - FeliCa signal handling got refactored. (@iceman1001)
- Changed client tab completion - vocabulary is now built at runtime from the live command tree and uses the same availability rules as `help`, removing the generated `pm3line_vocabulary.h` and `pm3_help2list.py` (@Msprg)
+83 -7
View File
@@ -71,6 +71,11 @@ static struct {
bool tried; // already negotiated against this ATR
} s_pps = { {0}, 0, 0, 0, true, false };
// Defined further down, next to the Fi/Di tables it needs. Declared here
// because the callers that reset the module without asking for an ATR come
// first in this file.
static void sc_rate_restore(void);
// try i2c bus recovery at 100kHz = 5us high, 5us low
void I2C_recovery(void) {
@@ -826,6 +831,10 @@ void I2C_print_status(void) {
int I2C_get_version(uint8_t *major, uint8_t *minor) {
uint8_t resp[] = {0, 0, 0, 0};
I2C_Reset_EnterMainProgram();
// The capability probe runs this on every client connect. Without the
// restore, reconnecting a client leaves the module on the default rate and
// an already negotiated card unreachable.
sc_rate_restore();
uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
if (len > 1) {
*major = resp[0];
@@ -986,19 +995,76 @@ static uint8_t atr_first_proto(const uint8_t *atr, uint8_t len) {
// A card with no TA1 offers nothing but the default, so nothing is proposed.
#define SC_PPS_MIN_RELOAD 2
static uint8_t sc_pps_best_ta1(const uint8_t *atr, uint8_t len) {
// ISO/IEC 7816-3 tables 7 and 8. 0 marks an RFU entry, which nothing may use.
static const uint16_t s_fi_tab[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0,
0, 512, 768, 1024, 1536, 2048, 0, 0
};
static const uint8_t s_di_tab[16] = {0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0, 0, 0, 0, 0, 0};
static const uint16_t fi_tab[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0,
0, 512, 768, 1024, 1536, 2048, 0, 0
};
static const uint8_t di_tab[16] = {0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0, 0, 0, 0, 0, 0};
// The module's UART reload for a TA1, or 0 when the pair is unusable. Same
// arithmetic as UART_Set_FiDi() in the module firmware: R = Fi / (16 * Di),
// which has to come out whole or the sampling point drifts across a character.
static uint16_t sc_ta1_reload(uint8_t ta1) {
uint16_t f = s_fi_tab[(ta1 >> 4) & 0x0F];
uint8_t d = s_di_tab[ta1 & 0x0F];
if ((f == 0) || (d == 0)) {
return 0;
}
if ((f % (uint16_t)(16u * d)) != 0) {
return 0;
}
return (uint16_t)(f / (uint16_t)(16u * d));
}
// Put the module back on the negotiated rate without touching the card.
//
// A module reset returns its UART to the default divisor, but the card keeps
// the rate a PPS put it at - only an RST pulse clears that, and the callers
// below deliberately do not pulse one. Left alone the two sit at different
// rates and every exchange after the reset is garbage; the only other cure is
// an ATR, which resets the card and destroys a SAM's open secure channel.
//
// Only the rate is restored. TC1 and TC2 - guard time and WI - come from the
// ATR and are gone with the reset, so a card naming non-default ones still
// needs a fresh ATR. What the module comes up with is what those cards ran at
// before their ATR was read anyway.
static void sc_rate_restore(void) {
if ((s_pps.ta1 == 0) || (s_pps.ta1 == 0x11)) {
return; // nothing negotiated, the default is right
}
uint16_t r = sc_ta1_reload(s_pps.ta1);
if (r == 0) {
return;
}
// SETBAUD carries TH1, which the module turns back into 256 - TH1. R = 256
// wraps to 0, which is exactly what the timer wants.
uint8_t th1 = (uint8_t)((256u - r) & 0xFFu);
if (I2C_WriteByte(th1, I2C_DEVICE_CMD_SETBAUD, I2C_DEVICE_ADDRESS_MAIN) == false) {
if (g_dbglevel >= DBG_ERROR) {
DbpString("SC: could not put the module back on the negotiated rate");
}
return;
}
if (g_dbglevel >= DBG_INFO) {
Dbprintf("SC: module rate restored without a card reset, TA1 %02X (R=%u)", s_pps.ta1, r);
}
}
static uint8_t sc_pps_best_ta1(const uint8_t *atr, uint8_t len) {
if ((len < 3) || ((atr[1] & 0x10) == 0)) {
return 0; // no TA1 - default only
}
uint8_t fi_idx = (uint8_t)((atr[2] >> 4) & 0x0F);
uint16_t f = fi_tab[fi_idx];
uint16_t f = s_fi_tab[fi_idx];
if (f == 0) {
return 0; // RFU
}
@@ -1008,7 +1074,7 @@ static uint8_t sc_pps_best_ta1(const uint8_t *atr, uint8_t len) {
for (uint8_t di_idx = 1; di_idx < 16; di_idx++) {
uint8_t d = di_tab[di_idx];
uint8_t d = s_di_tab[di_idx];
if (d == 0) {
continue;
}
@@ -1222,6 +1288,13 @@ void SmartCardRaw(const smart_card_raw_t *p) {
I2C_Reset_EnterMainProgram();
// Without SC_SELECT there is no ATR to negotiate against, so put the
// rate back by hand. With it, GetATR() resets the card and runs the PPS
// itself - and the module has to be on the default to hear that ATR.
if ((flags & SC_SELECT) != SC_SELECT) {
sc_rate_restore();
}
if ((flags & SC_SELECT) == SC_SELECT) {
smart_card_atr_t card;
bool gotATR = GetATR(&card, true);
@@ -1460,6 +1533,9 @@ void SmartCardSetClock(uint64_t arg0) {
LED_D_ON();
set_tracing(true);
I2C_Reset_EnterMainProgram();
// Fsys and the card clock move together, so a negotiated etu stays valid in
// card clocks - but the reset still wiped the divisor that produces it.
sc_rate_restore();
// Send SIM CLC
// start [C0 05 xx] stop
I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
+1 -1
View File
@@ -44,7 +44,7 @@ void SpinDelayUs(int us);
// Wrap in #ifndef to avoid accidental bloat of bootrom
void SpinDelay(int ms);
void SpinDelayUsPrecision(int us); // precision 0.6us , running for 43ms before
void SpinDelayUsPrecision(int us); // precision 0.6us, no upper bound on the delay
void StartTickCount(void);
uint32_t RAMFUNC GetTickCount(void);
+39 -27
View File
@@ -20,10 +20,30 @@
#include "proxmark3_arm.h"
// Both delays below run on PWM channel 0 and wait the same way.
//
// The wait is on the wrapped difference from a starting count, not on
// `now == end`. Equality only holds if the loop happens to sample that exact
// value, so an interrupt or a slow read that steps the counter past it used to
// cost a full 16 bit period before it came round again - 1.39 s at this
// prescaler, 43.7 ms at the precision one. It also needed an `if (end == 0)
// end++` guard that the difference form does not.
//
// The counter is 16 bit, so one comparison spans at most 65535 ticks. Longer
// waits are walked in chunks, advancing `start` by exactly the chunk that
// elapsed so they join without drift; chunks are capped at half a period to
// keep the unsigned difference unambiguous. The tick count used to be
// truncated into a uint16, which silently returned early: a requested 60 ms
// delay measured 17.5 ms on an RDV4, and `hw tearoff --delay` accepts values
// right across that boundary.
//
// Keep this inline. Splitting the arming and the wait into helpers broke the
// SIM module link outright - `smart info` could not read the module version -
// with arithmetic that is otherwise identical.
// timer counts in 21.3us increments (1024/48MHz), rounding applies
// WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
void SpinDelayUs(int us) {
int ticks = ((MCK / 1000000) * us + 512) >> 10;
uint32_t ticks = (us > 0) ? ((((uint32_t)(MCK / 1000000) * (uint32_t)us) + 512) >> 10) : 0;
// Borrow a PWM unit for my real-time clock
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
@@ -33,19 +53,15 @@ void SpinDelayUs(int us) {
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; // Channel Period Register
uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
if (end == 0) { // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
end++; // so we have to end++ to avoid inivity loop
}
for (;;) {
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
if (now == end) {
return;
uint32_t remaining = (uint32_t)ticks;
uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
while (remaining) {
uint16_t chunk = (remaining > 0x8000UL) ? 0x8000U : (uint16_t)remaining;
while ((uint16_t)(AT91C_BASE_PWMC_CH0->PWMC_CCNTR - start) < chunk) {
WDT_HIT();
}
WDT_HIT();
start = (uint16_t)(start + chunk);
remaining -= chunk;
}
}
@@ -54,7 +70,7 @@ void SpinDelayUs(int us) {
// timer counts in 666ns increments (32/48MHz), rounding applies
// WARNING: timer can't measure more than 43ms (666ns * 0xFFFF)
void SpinDelayUsPrecision(int us) {
int ticks = ((MCK / 1000000) * us + 16) >> 5;
uint32_t ticks = (us > 0) ? ((((uint32_t)(MCK / 1000000) * (uint32_t)us) + 16) >> 5) : 0;
// Borrow a PWM unit for my real-time clock
AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
@@ -64,19 +80,15 @@ void SpinDelayUsPrecision(int us) {
AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xFFFF; // Channel Period Register
uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
if (end == 0) { // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
end++; // so we have to end++ to avoid inivity loop
}
for (;;) {
uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
if (now == end) {
return;
uint32_t remaining = (uint32_t)ticks;
uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
while (remaining) {
uint16_t chunk = (remaining > 0x8000UL) ? 0x8000U : (uint16_t)remaining;
while ((uint16_t)(AT91C_BASE_PWMC_CH0->PWMC_CCNTR - start) < chunk) {
WDT_HIT();
}
WDT_HIT();
start = (uint16_t)(start + chunk);
remaining -= chunk;
}
}
+9 -2
View File
@@ -160,8 +160,15 @@ Current usages:
Busy wait based on 46.875 kHz PWM Channel 0
* 21.3 us precision and maximum 1.39 s
* *Precision* variant: 0.7 us precision and maximum 43 ms
* 21.3 us precision, no upper bound on the delay
* *Precision* variant: 0.7 us precision, no upper bound on the delay
Both used to truncate their tick count into the 16 bit PWM counter and so
returned early past those two limits without saying so: a requested 60 ms
`SpinDelayUsPrecision()` measured 17.5 ms on an RDV4, and `hw tearoff --delay`
accepts values right across that boundary. Longer waits are now walked in
chunks of half a counter period. `SpinDelay()` still clamps its own argument at
1390 ms and complains above that.
## Occasional TC0+TC1 / CountUS functions
^[Top](#top)