Skip to content

Commit a749b1e

Browse files
pwpiwiuzlonewolf
andauthored
speedup 'hf mf chk' (#901)
* add separate timeout for tag response to nr_ar * measure response time and use it for response timeout * don't drop field between keyblocks * some reformatting * some whitespace fixes * fishing for microseconds in TransmitFor14443a() * allow arbitrary number of keys in MifareChkKeys() * and move progress printing to MifareChkKeys() Co-authored-by: uzlonewolf <[email protected]>
1 parent f0c4855 commit a749b1e

File tree

9 files changed

+284
-319
lines changed

9 files changed

+284
-319
lines changed

armsrc/iso14443a.c

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ const bool Mod_Miller_LUT[] = {
270270
#define IsMillerModulationNibble1(b) (Mod_Miller_LUT[(b & 0x000000F0) >> 4])
271271
#define IsMillerModulationNibble2(b) (Mod_Miller_LUT[(b & 0x0000000F)])
272272

273-
static void UartReset()
274-
{
273+
static void UartReset() {
275274
Uart.state = STATE_UNSYNCD;
276275
Uart.bitCount = 0;
277276
Uart.len = 0; // number of decoded data bytes
@@ -280,8 +279,7 @@ static void UartReset()
280279
Uart.parityBits = 0; // holds 8 parity bits
281280
}
282281

283-
static void UartInit(uint8_t *data, uint8_t *parity)
284-
{
282+
static void UartInit(uint8_t *data, uint8_t *parity) {
285283
Uart.output = data;
286284
Uart.parity = parity;
287285
Uart.fourBits = 0x00000000; // clear the buffer for 4 Bits
@@ -291,8 +289,7 @@ static void UartInit(uint8_t *data, uint8_t *parity)
291289
}
292290

293291
// use parameter non_real_time to provide a timestamp. Set to 0 if the decoder should measure real time
294-
static RAMFUNC bool MillerDecoding(uint8_t bit, uint32_t non_real_time)
295-
{
292+
static RAMFUNC bool MillerDecoding(uint8_t bit, uint32_t non_real_time) {
296293

297294
Uart.fourBits = (Uart.fourBits << 8) | bit;
298295

@@ -447,8 +444,7 @@ const bool Mod_Manchester_LUT[] = {
447444
#define IsManchesterModulationNibble2(b) (Mod_Manchester_LUT[(b & 0x000F)])
448445

449446

450-
static void DemodReset()
451-
{
447+
static void DemodReset() {
452448
Demod.state = DEMOD_UNSYNCD;
453449
Demod.len = 0; // number of decoded data bytes
454450
Demod.parityLen = 0;
@@ -461,16 +457,14 @@ static void DemodReset()
461457
Demod.endTime = 0;
462458
}
463459

464-
static void DemodInit(uint8_t *data, uint8_t *parity)
465-
{
460+
static void DemodInit(uint8_t *data, uint8_t *parity) {
466461
Demod.output = data;
467462
Demod.parity = parity;
468463
DemodReset();
469464
}
470465

471466
// use parameter non_real_time to provide a timestamp. Set to 0 if the decoder should measure real time
472-
static RAMFUNC int ManchesterDecoding(uint8_t bit, uint16_t offset, uint32_t non_real_time)
473-
{
467+
static RAMFUNC int ManchesterDecoding(uint8_t bit, uint16_t offset, uint32_t non_real_time) {
474468

475469
Demod.twoBits = (Demod.twoBits << 8) | bit;
476470

@@ -729,8 +723,7 @@ void RAMFUNC SnoopIso14443a(uint8_t param) {
729723
//-----------------------------------------------------------------------------
730724
// Prepare tag messages
731725
//-----------------------------------------------------------------------------
732-
static void CodeIso14443aAsTagPar(const uint8_t *cmd, uint16_t len, uint8_t *parity)
733-
{
726+
static void CodeIso14443aAsTagPar(const uint8_t *cmd, uint16_t len, uint8_t *parity) {
734727
ToSendReset();
735728

736729
// Correction bit, might be removed when not needed
@@ -778,8 +771,7 @@ static void CodeIso14443aAsTagPar(const uint8_t *cmd, uint16_t len, uint8_t *par
778771
}
779772

780773

781-
static void Code4bitAnswerAsTag(uint8_t cmd)
782-
{
774+
static void Code4bitAnswerAsTag(uint8_t cmd) {
783775
int i;
784776

785777
ToSendReset();
@@ -853,8 +845,7 @@ static void EmLogTraceTag(uint8_t *tag_data, uint16_t tag_len, uint8_t *tag_Pari
853845
// Stop when button is pressed
854846
// Or return true when command is captured
855847
//-----------------------------------------------------------------------------
856-
static int GetIso14443aCommandFromReader(uint8_t *received, uint8_t *parity, int *len)
857-
{
848+
static int GetIso14443aCommandFromReader(uint8_t *received, uint8_t *parity, int *len) {
858849
// Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
859850
// only, since we are receiving, not transmitting).
860851
// Signal field is off with the appropriate LED
@@ -951,8 +942,8 @@ bool prepare_allocated_tag_modulation(tag_response_info_t* response_info, uint8_
951942
// Main loop of simulated tag: receive commands from reader, decide what
952943
// response to send, and send it.
953944
//-----------------------------------------------------------------------------
954-
void SimulateIso14443aTag(int tagType, int uid_1st, int uid_2nd, byte_t* data)
955-
{
945+
void SimulateIso14443aTag(int tagType, int uid_1st, int uid_2nd, byte_t* data) {
946+
956947
uint8_t sak;
957948

958949
// The first response contains the ATQA (note: bytes are transmitted in reverse order).
@@ -1231,8 +1222,7 @@ void SimulateIso14443aTag(int tagType, int uid_1st, int uid_2nd, byte_t* data)
12311222

12321223
// prepare a delayed transfer. This simply shifts ToSend[] by a number
12331224
// of bits specified in the delay parameter.
1234-
static void PrepareDelayedTransfer(uint16_t delay)
1235-
{
1225+
static void PrepareDelayedTransfer(uint16_t delay) {
12361226
uint8_t bitmask = 0;
12371227
uint8_t bits_to_shift = 0;
12381228
uint8_t bits_shifted = 0;
@@ -1261,21 +1251,20 @@ static void PrepareDelayedTransfer(uint16_t delay)
12611251
// if == 0: transfer immediately and return time of transfer
12621252
// if != 0: delay transfer until time specified
12631253
//-------------------------------------------------------------------------------------
1264-
static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing)
1265-
{
1254+
static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing) {
12661255
LED_B_ON();
12671256
LED_D_ON();
12681257
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
12691258

12701259
uint32_t ThisTransferTime = 0;
12711260

12721261
if (timing) {
1273-
if(*timing == 0) { // Measure time
1262+
if (*timing == 0) { // Measure time
12741263
*timing = (GetCountSspClk() + 8) & 0xfffffff8;
12751264
} else {
12761265
PrepareDelayedTransfer(*timing & 0x00000007); // Delay transfer (fine tuning - up to 7 MF clock ticks)
12771266
}
1278-
if(MF_DBGLEVEL >= 4 && GetCountSspClk() >= (*timing & 0xfffffff8)) Dbprintf("TransmitFor14443a: Missed timing");
1267+
if (MF_DBGLEVEL >= 4 && GetCountSspClk() >= (*timing & 0xfffffff8)) Dbprintf("TransmitFor14443a: Missed timing");
12791268
while (GetCountSspClk() < (*timing & 0xfffffff8)); // Delay transfer (multiple of 8 MF clock ticks)
12801269
LastTimeProxToAirStart = *timing;
12811270
} else {
@@ -1284,12 +1273,9 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing
12841273
LastTimeProxToAirStart = ThisTransferTime;
12851274
}
12861275

1287-
// clear TXRDY
1288-
AT91C_BASE_SSC->SSC_THR = SEC_Y;
1289-
12901276
uint16_t c = 0;
12911277
for (;;) {
1292-
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1278+
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
12931279
AT91C_BASE_SSC->SSC_THR = cmd[c];
12941280
c++;
12951281
if(c >= len) {
@@ -1306,8 +1292,7 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing
13061292
//-----------------------------------------------------------------------------
13071293
// Prepare reader command (in bits, support short frames) to send to FPGA
13081294
//-----------------------------------------------------------------------------
1309-
static void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8_t *parity)
1310-
{
1295+
static void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8_t *parity) {
13111296
int i, j;
13121297
int last;
13131298
uint8_t b;
@@ -1390,8 +1375,7 @@ static void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, cons
13901375
// Stop when button is pressed (return 1) or field was gone (return 2)
13911376
// Or return 0 when command is captured
13921377
//-----------------------------------------------------------------------------
1393-
int EmGetCmd(uint8_t *received, uint16_t *len, uint8_t *parity)
1394-
{
1378+
int EmGetCmd(uint8_t *received, uint16_t *len, uint8_t *parity) {
13951379
uint32_t field_off_time = -1;
13961380
uint32_t samples = 0;
13971381
int ret = 0;
@@ -1475,8 +1459,7 @@ int EmGetCmd(uint8_t *received, uint16_t *len, uint8_t *parity)
14751459
}
14761460

14771461

1478-
static int EmSendCmd14443aRaw(uint8_t *resp, uint16_t respLen)
1479-
{
1462+
static int EmSendCmd14443aRaw(uint8_t *resp, uint16_t respLen) {
14801463
LED_C_ON();
14811464

14821465
uint8_t b;
@@ -1578,8 +1561,7 @@ int EmSendPrecompiledCmd(tag_response_info_t *response_info) {
15781561
// If a response is captured return true
15791562
// If it takes too long return false
15801563
//-----------------------------------------------------------------------------
1581-
static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse, uint8_t *receivedResponsePar, uint16_t offset)
1582-
{
1564+
static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse, uint8_t *receivedResponsePar, uint16_t offset) {
15831565
uint32_t c;
15841566

15851567
// Set FPGA mode to "reader listen mode", no modulation (listen
@@ -1598,9 +1580,9 @@ static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse, uint8_t *receive
15981580
for (;;) {
15991581
WDT_HIT();
16001582

1601-
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1583+
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
16021584
b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1603-
if(ManchesterDecoding(b, offset, 0)) {
1585+
if (ManchesterDecoding(b, offset, 0)) {
16041586
NextTransferTime = MAX(NextTransferTime, Demod.endTime - (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER)/16 + FRAME_DELAY_TIME_PICC_TO_PCD);
16051587
return true;
16061588
} else if (c++ > iso14a_timeout && Demod.state == DEMOD_UNSYNCD) {
@@ -1611,55 +1593,51 @@ static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse, uint8_t *receive
16111593
}
16121594

16131595

1614-
void ReaderTransmitBitsPar(uint8_t* frame, uint16_t bits, uint8_t *par, uint32_t *timing)
1615-
{
1596+
void ReaderTransmitBitsPar(uint8_t* frame, uint16_t bits, uint8_t *par, uint32_t *timing) {
1597+
16161598
CodeIso14443aBitsAsReaderPar(frame, bits, par);
16171599

16181600
// Send command to tag
16191601
TransmitFor14443a(ToSend, ToSendMax, timing);
1620-
if(trigger)
1602+
if (trigger)
16211603
LED_A_ON();
16221604

16231605
// Log reader command in trace buffer
16241606
LogTrace(frame, nbytes(bits), LastTimeProxToAirStart*16 + DELAY_ARM2AIR_AS_READER, (LastTimeProxToAirStart + LastProxToAirDuration)*16 + DELAY_ARM2AIR_AS_READER, par, true);
16251607
}
16261608

16271609

1628-
void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *timing)
1629-
{
1630-
ReaderTransmitBitsPar(frame, len*8, par, timing);
1610+
void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *timing) {
1611+
ReaderTransmitBitsPar(frame, len*8, par, timing);
16311612
}
16321613

16331614

1634-
static void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing)
1635-
{
1615+
static void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing) {
16361616
// Generate parity and redirect
16371617
uint8_t par[MAX_PARITY_SIZE];
16381618
GetParity(frame, len/8, par);
16391619
ReaderTransmitBitsPar(frame, len, par, timing);
16401620
}
16411621

16421622

1643-
void ReaderTransmit(uint8_t* frame, uint16_t len, uint32_t *timing)
1644-
{
1623+
void ReaderTransmit(uint8_t* frame, uint16_t len, uint32_t *timing) {
16451624
// Generate parity and redirect
16461625
uint8_t par[MAX_PARITY_SIZE];
16471626
GetParity(frame, len, par);
16481627
ReaderTransmitBitsPar(frame, len*8, par, timing);
16491628
}
16501629

16511630

1652-
static int ReaderReceiveOffset(uint8_t* receivedAnswer, uint16_t offset, uint8_t *parity)
1653-
{
1631+
static int ReaderReceiveOffset(uint8_t* receivedAnswer, uint16_t offset, uint8_t *parity) {
16541632
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset)) return false;
16551633
LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, false);
16561634
return Demod.len;
16571635
}
16581636

16591637

1660-
int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity)
1661-
{
1638+
int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity) {
16621639
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0)) return false;
1640+
16631641
LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, false);
16641642
return Demod.len;
16651643
}
@@ -1696,7 +1674,7 @@ static void iso14a_set_ATS_times(uint8_t *ats) {
16961674
static int GetATQA(uint8_t *resp, uint8_t *resp_par) {
16971675

16981676
#define WUPA_RETRY_TIMEOUT 10 // 10ms
1699-
uint8_t wupa[] = { 0x52 }; // 0x26 - REQA 0x52 - WAKE-UP
1677+
uint8_t wupa[] = {ISO14443A_CMD_WUPA}; // 0x26 - REQA 0x52 - WAKE-UP
17001678

17011679
uint32_t save_iso14a_timeout = iso14a_get_timeout();
17021680
iso14a_set_timeout(1236/(16*8)+1); // response to WUPA is expected at exactly 1236/fc. No need to wait longer.
@@ -1737,7 +1715,7 @@ int iso14443a_select_card(byte_t *uid_ptr, iso14a_card_select_t *p_hi14a_card, u
17371715
int len;
17381716

17391717
// init card struct
1740-
if(p_hi14a_card) {
1718+
if (p_hi14a_card) {
17411719
p_hi14a_card->uidlen = 0;
17421720
memset(p_hi14a_card->uid, 0, 10);
17431721
p_hi14a_card->ats_len = 0;
@@ -1747,7 +1725,7 @@ int iso14443a_select_card(byte_t *uid_ptr, iso14a_card_select_t *p_hi14a_card, u
17471725
return 0;
17481726
}
17491727

1750-
if(p_hi14a_card) {
1728+
if (p_hi14a_card) {
17511729
memcpy(p_hi14a_card->atqa, resp, 2);
17521730
}
17531731

@@ -2034,8 +2012,8 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u
20342012
// Read an ISO 14443a tag. Send out commands and store answers.
20352013
//
20362014
//-----------------------------------------------------------------------------
2037-
void ReaderIso14443a(UsbCommand *c)
2038-
{
2015+
void ReaderIso14443a(UsbCommand *c) {
2016+
20392017
iso14a_command_t param = c->arg[0];
20402018
uint8_t *cmd = c->d.asBytes;
20412019
size_t len = c->arg[1] & 0xffff;

armsrc/iso14443a.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ extern int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *
5252
extern int iso14443a_select_card(uint8_t *uid_ptr, iso14a_card_select_t *resp_data, uint32_t *cuid_ptr, bool anticollision, uint8_t num_cascades, bool no_rats);
5353
extern void iso14a_set_trigger(bool enable);
5454
extern void iso14a_set_timeout(uint32_t timeout);
55+
extern uint32_t iso14a_get_timeout(void);
5556
#endif /* __ISO14443A_H */

0 commit comments

Comments
 (0)