11
11
#include "iso14443b.h"
12
12
13
13
#ifndef FWT_TIMEOUT_14B
14
- # define FWT_TIMEOUT_14B 60000
14
+ // defaults to 2000ms
15
+ # define FWT_TIMEOUT_14B 35312
15
16
#endif
16
17
#ifndef ISO14443B_DMA_BUFFER_SIZE
17
18
# define ISO14443B_DMA_BUFFER_SIZE 256
37
38
// 4sample
38
39
#define SEND4STUFFBIT (x ) ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);
39
40
//#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
40
45
46
+ static void iso14b_set_timeout (uint32_t timeout );
47
+ static void iso14b_set_maxframesize (uint16_t size );
41
48
static void switch_off (void );
42
49
43
50
// the block number for the ISO14443-4 PCB (used with APDUs)
44
51
static uint8_t pcb_blocknum = 0 ;
45
-
46
52
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
- }
54
53
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
- }
63
54
64
55
//=============================================================================
65
56
// An ISO 14443 Type B tag. We listen for commands from the reader, using
@@ -150,6 +141,44 @@ static void DemodInit(uint8_t *data) {
150
141
// memset(Demod.output, 0x00, MAX_FRAME_SIZE);
151
142
}
152
143
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
+
153
182
void AppendCrc14443b (uint8_t * data , int len ) {
154
183
ComputeCrc14443 (CRC_14443_B , data , len , data + len , data + len + 1 );
155
184
}
@@ -298,9 +327,9 @@ static void CodeIso14443bAsTag(const uint8_t *cmd, int len) {
298
327
* false if we are still waiting for some more
299
328
*/
300
329
static RAMFUNC int Handle14443bReaderUartBit (uint8_t bit ) {
301
- switch (Uart .state ) {
330
+ switch (Uart .state ) {
302
331
case STATE_UNSYNCD :
303
- if (!bit ) {
332
+ if (!bit ) {
304
333
// we went low, so this could be the beginning of an SOF
305
334
Uart .state = STATE_GOT_FALLING_EDGE_OF_SOF ;
306
335
Uart .posCnt = 0 ;
@@ -310,40 +339,37 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
310
339
311
340
case STATE_GOT_FALLING_EDGE_OF_SOF :
312
341
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 ) {
316
345
// we've seen enough consecutive
317
346
// zeros that it's a valid SOF
318
347
Uart .posCnt = 0 ;
319
348
Uart .byteCnt = 0 ;
320
349
Uart .state = STATE_AWAITING_START_BIT ;
321
350
LED_A_ON (); // Indicate we got a valid SOF
322
351
} else {
323
- // didn't stay down long enough
324
- // before going high, error
352
+ // didn't stay down long enough before going high, error
325
353
Uart .state = STATE_UNSYNCD ;
326
354
}
327
355
} else {
328
356
// do nothing, keep waiting
329
357
}
330
358
Uart .bitCnt ++ ;
331
359
}
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.
336
363
LED_A_OFF ();
337
364
Uart .state = STATE_UNSYNCD ;
338
365
}
339
366
break ;
340
367
341
368
case STATE_AWAITING_START_BIT :
342
369
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
347
373
Uart .state = STATE_UNSYNCD ;
348
374
}
349
375
} else {
@@ -357,26 +383,26 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
357
383
358
384
case STATE_RECEIVING_DATA :
359
385
Uart .posCnt ++ ;
360
- if (Uart .posCnt == 2 ) {
386
+ if (Uart .posCnt == 2 ) {
361
387
// time to sample a bit
362
388
Uart .shiftReg >>= 1 ;
363
- if (bit ) {
389
+ if (bit ) {
364
390
Uart .shiftReg |= 0x200 ;
365
391
}
366
392
Uart .bitCnt ++ ;
367
393
}
368
- if (Uart .posCnt >= 4 ) {
394
+ if (Uart .posCnt >= 4 ) {
369
395
Uart .posCnt = 0 ;
370
396
}
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 ))
373
399
{
374
400
// this is a data byte, with correct
375
401
// start and stop bits
376
402
Uart .output [Uart .byteCnt ] = (Uart .shiftReg >> 1 ) & 0xff ;
377
403
Uart .byteCnt ++ ;
378
404
379
- if (Uart .byteCnt >= Uart .byteCntMax ) {
405
+ if (Uart .byteCnt >= Uart .byteCntMax ) {
380
406
// Buffer overflowed, give up
381
407
LED_A_OFF ();
382
408
Uart .state = STATE_UNSYNCD ;
@@ -389,9 +415,9 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
389
415
// this is an EOF byte
390
416
LED_A_OFF (); // Finished receiving
391
417
Uart .state = STATE_UNSYNCD ;
392
- if (Uart .byteCnt != 0 ) {
393
- return TRUE;
394
- }
418
+ if (Uart .byteCnt != 0 )
419
+ return TRUE;
420
+
395
421
} else {
396
422
// this is an error
397
423
LED_A_OFF ();
@@ -405,7 +431,6 @@ static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) {
405
431
Uart .state = STATE_UNSYNCD ;
406
432
break ;
407
433
}
408
-
409
434
return FALSE;
410
435
}
411
436
@@ -716,13 +741,9 @@ void SimulateIso14443bTag(uint32_t pupi) {
716
741
* false if we are still waiting for some more
717
742
*
718
743
*/
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
-
724
744
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
+
726
747
// The soft decision on the bit uses an estimate of just the
727
748
// quadrant of the reference angle, not the exact angle.
728
749
#define MAKE_SOFT_DECISION () { \
@@ -774,8 +795,6 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
774
795
775
796
//note: couldn't we just use MAX(ABS(ci),ABS(cq)) + (MIN(ABS(ci),ABS(cq))/2) from common.h - marshmellow
776
797
#define CHECK_FOR_SUBCARRIER () { \
777
- myI = ABS(ci); \
778
- myQ = ABS(cq); \
779
798
v = MAX(myI, myQ) + (MIN(myI, myQ) >> 1); \
780
799
}
781
800
@@ -895,18 +914,19 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
895
914
uint16_t s = Demod .shiftReg ;
896
915
897
916
// 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 ;
901
920
++ Demod .len ;
902
921
Demod .state = DEMOD_AWAITING_START_BIT ;
903
922
} else {
923
+ // this one is a bit hard, either its a correc byte or its unsynced.
904
924
Demod .state = DEMOD_UNSYNCD ;
905
925
Demod .endTime = GetCountSspClk ();
906
926
LED_C_OFF ();
907
927
908
928
// This is EOF (start, stop and all data bits == '0'
909
- if (s == 0 ) return TRUE;
929
+ if (s == 0 ) return TRUE;
910
930
}
911
931
}
912
932
Demod .posCount = 0 ;
@@ -929,7 +949,7 @@ static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
929
949
static void GetTagSamplesFor14443bDemod () {
930
950
bool gotFrame = FALSE, finished = FALSE;
931
951
int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE ;
932
- int ci = 0 , cq = 0 , samples = 0 ;
952
+ int ci = 0 , cq = 0 ;
933
953
uint32_t time_0 = 0 , time_stop = 0 ;
934
954
935
955
BigBuf_free ();
@@ -963,8 +983,6 @@ static void GetTagSamplesFor14443bDemod() {
963
983
ci = upTo [0 ] >> 1 ;
964
984
cq = upTo [1 ] >> 1 ;
965
985
upTo += 2 ;
966
- samples += 2 ;
967
-
968
986
lastRxCounter -= 2 ;
969
987
970
988
// restart DMA buffer to receive again.
@@ -976,7 +994,6 @@ static void GetTagSamplesFor14443bDemod() {
976
994
}
977
995
978
996
// https://github.com/Proxmark/proxmark3/issues/103
979
- //gotFrame = Handle14443bTagSamplesDemod(ci & 0xfe, cq & 0xfe);
980
997
gotFrame = Handle14443bTagSamplesDemod (ci , cq );
981
998
time_stop = GetCountSspClk () - time_0 ;
982
999
@@ -1285,10 +1302,23 @@ uint8_t iso14443b_select_card(iso14b_card_select_t *card )
1285
1302
ComputeCrc14443 (CRC_14443_B , Demod .output , Demod .len - 2 , & crc [0 ], & crc [1 ]);
1286
1303
if ( crc [0 ] != Demod .output [1 ] || crc [1 ] != Demod .output [2 ] )
1287
1304
return 3 ;
1288
-
1289
- // CID
1305
+
1290
1306
if (card ) {
1307
+
1308
+ // CID
1291
1309
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
1292
1322
uint8_t fwt = card -> atqb [6 ] >> 4 ;
1293
1323
if ( fwt < 16 ){
1294
1324
uint32_t fwt_time = (302 << fwt );
0 commit comments