Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit 29f8c2c

Browse files
committed
ADD: added the 'max frame size' parameter set, valid sizes: 16|24|32|40|48|64|96|128|256
it can be verified by setting debuglevel to 3 or more.
1 parent b62cbad commit 29f8c2c

File tree

1 file changed

+90
-60
lines changed

1 file changed

+90
-60
lines changed

armsrc/iso14443b.c

Lines changed: 90 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include "iso14443b.h"
1212

1313
#ifndef FWT_TIMEOUT_14B
14-
# define FWT_TIMEOUT_14B 60000
14+
// defaults to 2000ms
15+
# define FWT_TIMEOUT_14B 35312
1516
#endif
1617
#ifndef ISO14443B_DMA_BUFFER_SIZE
1718
# define ISO14443B_DMA_BUFFER_SIZE 256
@@ -37,29 +38,19 @@
3738
// 4sample
3839
#define SEND4STUFFBIT(x) ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);
3940
//#define SEND4STUFFBIT(x) ToSendStuffBit(x);
41+
// iceman, this threshold value, what makes 8 a good amplituted for this IQ values?
42+
#ifndef SUBCARRIER_DETECT_THRESHOLD
43+
# define SUBCARRIER_DETECT_THRESHOLD 6
44+
#endif
4045

46+
static void iso14b_set_timeout(uint32_t timeout);
47+
static void iso14b_set_maxframesize(uint16_t size);
4148
static void switch_off(void);
4249

4350
// the block number for the ISO14443-4 PCB (used with APDUs)
4451
static uint8_t pcb_blocknum = 0;
45-
4652
static uint32_t iso14b_timeout = FWT_TIMEOUT_14B;
47-
// param timeout is in ftw_
48-
void iso14b_set_timeout(uint32_t timeout) {
49-
// 9.4395us = 1etu.
50-
// clock is about 1.5 us
51-
iso14b_timeout = timeout;
52-
if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Timeout set to %ld fwt", iso14b_timeout);
53-
}
5453

55-
static void switch_off(void){
56-
if (MF_DBGLEVEL > 3) Dbprintf("switch_off");
57-
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
58-
SpinDelay(100);
59-
FpgaDisableSscDma();
60-
set_tracing(FALSE);
61-
LEDsoff();
62-
}
6354

6455
//=============================================================================
6556
// An ISO 14443 Type B tag. We listen for commands from the reader, using
@@ -150,6 +141,44 @@ static void DemodInit(uint8_t *data) {
150141
// memset(Demod.output, 0x00, MAX_FRAME_SIZE);
151142
}
152143

144+
145+
/*
146+
* 9.4395 us = 1 ETU and clock is about 1.5 us
147+
* 13560000Hz
148+
* 1000ms/s
149+
* timeout in ETUs (time to transfer 1 bit, 9.4395 us)
150+
*
151+
* Formula to calculate FWT (in ETUs) by timeout (in ms):
152+
* fwt = 13560000 * 1000 / (8*16) * timeout;
153+
* Sample: 3sec == 3000ms
154+
* 13560000 * 1000 / (8*16) * 3000 ==
155+
* 13560000000 / 384000 = 35312 FWT
156+
* @param timeout is in frame wait time, fwt, measured in ETUs
157+
*/
158+
static void iso14b_set_timeout(uint32_t timeout) {
159+
#define MAX_TIMEOUT 40542464 // 13560000Hz * 1000ms / (2^32-1) * (8*16)
160+
if(timeout > MAX_TIMEOUT)
161+
timeout = MAX_TIMEOUT;
162+
163+
iso14b_timeout = timeout;
164+
if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Timeout set to %ld fwt", iso14b_timeout);
165+
}
166+
static void iso14b_set_maxframesize(uint16_t size) {
167+
if (size > 256)
168+
size = MAX_FRAME_SIZE;
169+
170+
Uart.byteCntMax = size;
171+
if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Max frame size set to %d bytes", Uart.byteCntMax);
172+
}
173+
static void switch_off(void){
174+
if (MF_DBGLEVEL > 3) Dbprintf("switch_off");
175+
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
176+
SpinDelay(100);
177+
FpgaDisableSscDma();
178+
set_tracing(FALSE);
179+
LEDsoff();
180+
}
181+
153182
void AppendCrc14443b(uint8_t* data, int len) {
154183
ComputeCrc14443(CRC_14443_B, data, len, data+len, data+len+1);
155184
}
@@ -298,9 +327,9 @@ static void CodeIso14443bAsTag(const uint8_t *cmd, int len) {
298327
* false if we are still waiting for some more
299328
*/
300329
static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
301-
switch(Uart.state) {
330+
switch (Uart.state) {
302331
case STATE_UNSYNCD:
303-
if(!bit) {
332+
if (!bit) {
304333
// we went low, so this could be the beginning of an SOF
305334
Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
306335
Uart.posCnt = 0;
@@ -310,40 +339,37 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
310339

311340
case STATE_GOT_FALLING_EDGE_OF_SOF:
312341
Uart.posCnt++;
313-
if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
314-
if(bit) {
315-
if(Uart.bitCnt > 9) {
342+
if (Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
343+
if (bit) {
344+
if (Uart.bitCnt > 9) {
316345
// we've seen enough consecutive
317346
// zeros that it's a valid SOF
318347
Uart.posCnt = 0;
319348
Uart.byteCnt = 0;
320349
Uart.state = STATE_AWAITING_START_BIT;
321350
LED_A_ON(); // Indicate we got a valid SOF
322351
} else {
323-
// didn't stay down long enough
324-
// before going high, error
352+
// didn't stay down long enough before going high, error
325353
Uart.state = STATE_UNSYNCD;
326354
}
327355
} else {
328356
// do nothing, keep waiting
329357
}
330358
Uart.bitCnt++;
331359
}
332-
if(Uart.posCnt >= 4) Uart.posCnt = 0;
333-
if(Uart.bitCnt > 12) {
334-
// Give up if we see too many zeros without
335-
// a one, too.
360+
if (Uart.posCnt >= 4) Uart.posCnt = 0;
361+
if (Uart.bitCnt > 12) {
362+
// Give up if we see too many zeros without a one, too.
336363
LED_A_OFF();
337364
Uart.state = STATE_UNSYNCD;
338365
}
339366
break;
340367

341368
case STATE_AWAITING_START_BIT:
342369
Uart.posCnt++;
343-
if(bit) {
344-
if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
345-
// stayed high for too long between
346-
// characters, error
370+
if (bit) {
371+
if (Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
372+
// stayed high for too long between characters, error
347373
Uart.state = STATE_UNSYNCD;
348374
}
349375
} else {
@@ -357,26 +383,26 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
357383

358384
case STATE_RECEIVING_DATA:
359385
Uart.posCnt++;
360-
if(Uart.posCnt == 2) {
386+
if (Uart.posCnt == 2) {
361387
// time to sample a bit
362388
Uart.shiftReg >>= 1;
363-
if(bit) {
389+
if (bit) {
364390
Uart.shiftReg |= 0x200;
365391
}
366392
Uart.bitCnt++;
367393
}
368-
if(Uart.posCnt >= 4) {
394+
if (Uart.posCnt >= 4) {
369395
Uart.posCnt = 0;
370396
}
371-
if(Uart.bitCnt == 10) {
372-
if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
397+
if (Uart.bitCnt == 10) {
398+
if ((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
373399
{
374400
// this is a data byte, with correct
375401
// start and stop bits
376402
Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
377403
Uart.byteCnt++;
378404

379-
if(Uart.byteCnt >= Uart.byteCntMax) {
405+
if (Uart.byteCnt >= Uart.byteCntMax) {
380406
// Buffer overflowed, give up
381407
LED_A_OFF();
382408
Uart.state = STATE_UNSYNCD;
@@ -389,9 +415,9 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
389415
// this is an EOF byte
390416
LED_A_OFF(); // Finished receiving
391417
Uart.state = STATE_UNSYNCD;
392-
if (Uart.byteCnt != 0) {
393-
return TRUE;
394-
}
418+
if (Uart.byteCnt != 0)
419+
return TRUE;
420+
395421
} else {
396422
// this is an error
397423
LED_A_OFF();
@@ -405,7 +431,6 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
405431
Uart.state = STATE_UNSYNCD;
406432
break;
407433
}
408-
409434
return FALSE;
410435
}
411436

@@ -716,13 +741,9 @@ void SimulateIso14443bTag(uint32_t pupi) {
716741
* false if we are still waiting for some more
717742
*
718743
*/
719-
// iceman, this threshold value, what makes 8 a good amplituted for this IQ values?
720-
#ifndef SUBCARRIER_DETECT_THRESHOLD
721-
# define SUBCARRIER_DETECT_THRESHOLD 6
722-
#endif
723-
724744
static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
725-
int v = 0, myI = 0, myQ = 0;
745+
int v = 0, myI = ABS(ci), myQ = ABS(cq);
746+
726747
// The soft decision on the bit uses an estimate of just the
727748
// quadrant of the reference angle, not the exact angle.
728749
#define MAKE_SOFT_DECISION() { \
@@ -774,8 +795,6 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
774795

775796
//note: couldn't we just use MAX(ABS(ci),ABS(cq)) + (MIN(ABS(ci),ABS(cq))/2) from common.h - marshmellow
776797
#define CHECK_FOR_SUBCARRIER() { \
777-
myI = ABS(ci); \
778-
myQ = ABS(cq); \
779798
v = MAX(myI, myQ) + (MIN(myI, myQ) >> 1); \
780799
}
781800

@@ -895,18 +914,19 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
895914
uint16_t s = Demod.shiftReg;
896915

897916
// stop bit == '1', start bit == '0'
898-
if((s & 0x200) && !(s & 0x001)) {
899-
uint8_t b = (s >> 1);
900-
Demod.output[Demod.len] = b;
917+
if ((s & 0x200) && (s & 0x001) == 0 ) {
918+
// left shift to drop the startbit
919+
Demod.output[Demod.len] = (s >> 1) & 0xFF;
901920
++Demod.len;
902921
Demod.state = DEMOD_AWAITING_START_BIT;
903922
} else {
923+
// this one is a bit hard, either its a correc byte or its unsynced.
904924
Demod.state = DEMOD_UNSYNCD;
905925
Demod.endTime = GetCountSspClk();
906926
LED_C_OFF();
907927

908928
// This is EOF (start, stop and all data bits == '0'
909-
if(s == 0) return TRUE;
929+
if (s == 0) return TRUE;
910930
}
911931
}
912932
Demod.posCount = 0;
@@ -929,7 +949,7 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
929949
static void GetTagSamplesFor14443bDemod() {
930950
bool gotFrame = FALSE, finished = FALSE;
931951
int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
932-
int ci = 0, cq = 0, samples = 0;
952+
int ci = 0, cq = 0;
933953
uint32_t time_0 = 0, time_stop = 0;
934954

935955
BigBuf_free();
@@ -963,8 +983,6 @@ static void GetTagSamplesFor14443bDemod() {
963983
ci = upTo[0] >> 1;
964984
cq = upTo[1] >> 1;
965985
upTo += 2;
966-
samples += 2;
967-
968986
lastRxCounter -= 2;
969987

970988
// restart DMA buffer to receive again.
@@ -976,7 +994,6 @@ static void GetTagSamplesFor14443bDemod() {
976994
}
977995

978996
// https://github.com/Proxmark/proxmark3/issues/103
979-
//gotFrame = Handle14443bTagSamplesDemod(ci & 0xfe, cq & 0xfe);
980997
gotFrame = Handle14443bTagSamplesDemod(ci, cq);
981998
time_stop = GetCountSspClk() - time_0;
982999

@@ -1285,10 +1302,23 @@ uint8_t iso14443b_select_card(iso14b_card_select_t *card )
12851302
ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2, &crc[0], &crc[1]);
12861303
if ( crc[0] != Demod.output[1] || crc[1] != Demod.output[2] )
12871304
return 3;
1288-
1289-
// CID
1305+
12901306
if (card) {
1307+
1308+
// CID
12911309
card->cid = Demod.output[0];
1310+
1311+
// MAX FRAME
1312+
uint16_t maxFrame = card->atqb[5] >> 4;
1313+
if (maxFrame < 5) maxFrame = 8 * maxFrame + 16;
1314+
else if (maxFrame == 5) maxFrame = 64;
1315+
else if (maxFrame == 6) maxFrame = 96;
1316+
else if (maxFrame == 7) maxFrame = 128;
1317+
else if (maxFrame == 8) maxFrame = 256;
1318+
else maxFrame = 257;
1319+
iso14b_set_maxframesize(maxFrame);
1320+
1321+
// FWT
12921322
uint8_t fwt = card->atqb[6] >> 4;
12931323
if ( fwt < 16 ){
12941324
uint32_t fwt_time = (302 << fwt);

0 commit comments

Comments
 (0)