1 //-----------------------------------------------------------------------------
2 // Routines to support ISO 14443 type A.
4 // Gerhard de Koning Gans - May 2008
5 //-----------------------------------------------------------------------------
8 #include "../common/iso14443_crc.c"
10 static BYTE
*trace
= (BYTE
*) BigBuf
;
11 static int traceLen
= 0;
12 static int rsamples
= 0;
23 static const BYTE OddByteParity
[256] = {
24 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
25 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
26 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
27 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
28 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
29 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
30 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
31 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
32 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
33 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
34 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
35 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
36 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
37 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
38 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
39 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
42 //-----------------------------------------------------------------------------
43 // Generate the parity value for a byte sequence
45 //-----------------------------------------------------------------------------
46 DWORD
GetParity(const BYTE
* pbtCmd
, int iLen
)
51 // Generate the encrypted data
52 for (i
= 0; i
< iLen
; i
++) {
53 // Save the encrypted parity bit
54 dwPar
|= ((OddByteParity
[pbtCmd
[i
]]) << i
);
59 //-----------------------------------------------------------------------------
60 // The software UART that receives commands from the reader, and its state
62 //-----------------------------------------------------------------------------
66 STATE_START_OF_COMMUNICATION
,
90 static BOOL
MillerDecoding(int bit
)
96 Uart
.bitBuffer
= bit
^ 0xFF0;
100 Uart
.bitBuffer
<<= 4;
101 Uart
.bitBuffer
^= bit
;
106 if(Uart
.state
!= STATE_UNSYNCD
) {
109 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
115 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
121 if(bit
!= bitright
) { bit
= bitright
; }
123 if(Uart
.posCnt
== 1) {
124 // measurement first half bitperiod
126 Uart
.drop
= DROP_FIRST_HALF
;
130 // measurement second half bitperiod
131 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
132 Uart
.drop
= DROP_SECOND_HALF
;
135 // measured a drop in first and second half
136 // which should not be possible
137 Uart
.state
= STATE_ERROR_WAIT
;
144 case STATE_START_OF_COMMUNICATION
:
146 if(Uart
.drop
== DROP_SECOND_HALF
) {
147 // error, should not happen in SOC
148 Uart
.state
= STATE_ERROR_WAIT
;
153 Uart
.state
= STATE_MILLER_Z
;
160 if(Uart
.drop
== DROP_NONE
) {
161 // logic '0' followed by sequence Y
162 // end of communication
163 Uart
.state
= STATE_UNSYNCD
;
166 // if(Uart.drop == DROP_FIRST_HALF) {
167 // Uart.state = STATE_MILLER_Z; stay the same
168 // we see a logic '0' }
169 if(Uart
.drop
== DROP_SECOND_HALF
) {
170 // we see a logic '1'
171 Uart
.shiftReg
|= 0x100;
172 Uart
.state
= STATE_MILLER_X
;
178 if(Uart
.drop
== DROP_NONE
) {
179 // sequence Y, we see a '0'
180 Uart
.state
= STATE_MILLER_Y
;
183 if(Uart
.drop
== DROP_FIRST_HALF
) {
184 // Would be STATE_MILLER_Z
185 // but Z does not follow X, so error
186 Uart
.state
= STATE_ERROR_WAIT
;
189 if(Uart
.drop
== DROP_SECOND_HALF
) {
190 // We see a '1' and stay in state X
191 Uart
.shiftReg
|= 0x100;
199 if(Uart
.drop
== DROP_NONE
) {
200 // logic '0' followed by sequence Y
201 // end of communication
202 Uart
.state
= STATE_UNSYNCD
;
205 if(Uart
.drop
== DROP_FIRST_HALF
) {
207 Uart
.state
= STATE_MILLER_Z
;
209 if(Uart
.drop
== DROP_SECOND_HALF
) {
210 // We see a '1' and go to state X
211 Uart
.shiftReg
|= 0x100;
212 Uart
.state
= STATE_MILLER_X
;
216 case STATE_ERROR_WAIT
:
217 // That went wrong. Now wait for at least two bit periods
218 // and try to sync again
219 if(Uart
.drop
== DROP_NONE
) {
221 Uart
.state
= STATE_UNSYNCD
;
226 Uart
.state
= STATE_UNSYNCD
;
231 Uart
.drop
= DROP_NONE
;
233 // should have received at least one whole byte...
234 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
238 if(Uart
.bitCnt
== 9) {
239 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
242 Uart
.parityBits
<<= 1;
243 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
246 // when End of Communication received and
247 // all data bits processed..
254 Uart.output[Uart.byteCnt] = 0xAA;
256 Uart.output[Uart.byteCnt] = error & 0xFF;
258 Uart.output[Uart.byteCnt] = 0xAA;
260 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
262 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
264 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
266 Uart.output[Uart.byteCnt] = 0xAA;
274 bit
= Uart
.bitBuffer
& 0xf0;
278 // should have been high or at least (4 * 128) / fc
279 // according to ISO this should be at least (9 * 128 + 20) / fc
280 if(Uart
.highCnt
== 8) {
281 // we went low, so this could be start of communication
282 // it turns out to be safer to choose a less significant
283 // syncbit... so we check whether the neighbour also represents the drop
284 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
285 Uart
.syncBit
= bit
& 8;
287 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
288 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
289 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
290 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
291 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
292 if(Uart
.syncBit
& (Uart
.bitBuffer
& 8)) {
295 // the first half bit period is expected in next sample
300 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
303 Uart
.state
= STATE_START_OF_COMMUNICATION
;
304 Uart
.drop
= DROP_FIRST_HALF
;
315 if(Uart
.highCnt
< 8) {
324 //=============================================================================
325 // ISO 14443 Type A - Manchester
326 //=============================================================================
331 DEMOD_START_OF_COMMUNICATION
,
354 static BOOL
ManchesterDecoding(int v
)
370 if(Demod
.state
==DEMOD_UNSYNCD
) {
371 Demod
.output
[Demod
.len
] = 0xfa;
374 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
375 if(bit
& 0x08) { Demod
.syncBit
= 0x08; }
377 if(bit
& 0x04) { Demod
.syncBit
= 0x04; }
379 else if(bit
& 0x04) { Demod
.syncBit
= 0x04; bit
<<= 4; }
381 if(bit
& 0x02) { Demod
.syncBit
= 0x02; }
383 else if(bit
& 0x02) { Demod
.syncBit
= 0x02; bit
<<= 4; }
385 if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
387 if(Demod
.syncBit
& (Demod
.buffer
& 0x08)) {
388 Demod
.syncBit
= 0x08;
390 // The first half bitperiod is expected in next sample
392 Demod
.output
[Demod
.len
] = 0xfb;
395 else if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
399 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
400 Demod
.sub
= SUB_FIRST_HALF
;
403 Demod
.parityBits
= 0;
406 switch(Demod
.syncBit
) {
407 case 0x08: Demod
.samples
= 3; break;
408 case 0x04: Demod
.samples
= 2; break;
409 case 0x02: Demod
.samples
= 1; break;
410 case 0x01: Demod
.samples
= 0; break;
417 //modulation = bit & Demod.syncBit;
418 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
422 if(Demod
.posCount
==0) {
425 Demod
.sub
= SUB_FIRST_HALF
;
428 Demod
.sub
= SUB_NONE
;
433 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
434 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
435 Demod
.state
= DEMOD_ERROR_WAIT
;
436 Demod
.output
[Demod
.len
] = 0xaa;
440 else if(modulation
) {
441 Demod
.sub
= SUB_SECOND_HALF
;
444 switch(Demod
.state
) {
445 case DEMOD_START_OF_COMMUNICATION
:
446 if(Demod
.sub
== SUB_FIRST_HALF
) {
447 Demod
.state
= DEMOD_MANCHESTER_D
;
450 Demod
.output
[Demod
.len
] = 0xab;
451 Demod
.state
= DEMOD_ERROR_WAIT
;
456 case DEMOD_MANCHESTER_D
:
457 case DEMOD_MANCHESTER_E
:
458 if(Demod
.sub
== SUB_FIRST_HALF
) {
460 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
461 Demod
.state
= DEMOD_MANCHESTER_D
;
463 else if(Demod
.sub
== SUB_SECOND_HALF
) {
465 Demod
.shiftReg
>>= 1;
466 Demod
.state
= DEMOD_MANCHESTER_E
;
469 Demod
.state
= DEMOD_MANCHESTER_F
;
473 case DEMOD_MANCHESTER_F
:
474 // Tag response does not need to be a complete byte!
475 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
476 if(Demod
.bitCount
> 0) {
477 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
478 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
480 // No parity bit, so just shift a 0
481 Demod
.parityBits
<<= 1;
484 Demod
.state
= DEMOD_UNSYNCD
;
488 Demod
.output
[Demod
.len
] = 0xad;
489 Demod
.state
= DEMOD_ERROR_WAIT
;
494 case DEMOD_ERROR_WAIT
:
495 Demod
.state
= DEMOD_UNSYNCD
;
499 Demod
.output
[Demod
.len
] = 0xdd;
500 Demod
.state
= DEMOD_UNSYNCD
;
504 if(Demod
.bitCount
>=9) {
505 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
508 Demod
.parityBits
<<= 1;
509 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
516 Demod.output[Demod.len] = 0xBB;
518 Demod.output[Demod.len] = error & 0xFF;
520 Demod.output[Demod.len] = 0xBB;
522 Demod.output[Demod.len] = bit & 0xFF;
524 Demod.output[Demod.len] = Demod.buffer & 0xFF;
526 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
528 Demod.output[Demod.len] = 0xBB;
535 } // end (state != UNSYNCED)
540 //=============================================================================
541 // Finally, a `sniffer' for ISO 14443 Type A
542 // Both sides of communication!
543 //=============================================================================
545 //-----------------------------------------------------------------------------
546 // Record the sequence of commands sent by the reader to the tag, with
547 // triggering so that we start recording at the point that the tag is moved
549 //-----------------------------------------------------------------------------
550 void SnoopIso14443a(void)
553 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
555 #define RECV_CMD_OFFSET 3032
556 #define RECV_RES_OFFSET 3096
557 #define DMA_BUFFER_OFFSET 3160
558 #define DMA_BUFFER_SIZE 4096
559 #define TRACE_LENGTH 3000
561 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
562 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
563 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
564 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
565 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
567 // We won't start recording the frames that we acquire until we trigger;
568 // a good trigger condition to get started is probably when we see a
569 // response from the tag.
570 BOOL triggered
= TRUE
; // FALSE to wait first for card
572 // The command (reader -> tag) that we're receiving.
573 // The length of a received command will in most cases be no more than 18 bytes.
574 // So 32 should be enough!
575 BYTE
*receivedCmd
= (((BYTE
*)BigBuf
) + RECV_CMD_OFFSET
);
576 // The response (tag -> reader) that we're receiving.
577 BYTE
*receivedResponse
= (((BYTE
*)BigBuf
) + RECV_RES_OFFSET
);
579 // As we receive stuff, we copy it from receivedCmd or receivedResponse
580 // into trace, along with its length and other annotations.
581 //BYTE *trace = (BYTE *)BigBuf;
584 // The DMA buffer, used to stream samples from the FPGA
585 SBYTE
*dmaBuf
= ((SBYTE
*)BigBuf
) + DMA_BUFFER_OFFSET
;
591 // Count of samples received so far, so that we can include timing
592 // information in the trace buffer.
596 memset(trace
, 0x44, RECV_CMD_OFFSET
);
598 // Set up the demodulator for tag -> reader responses.
599 Demod
.output
= receivedResponse
;
601 Demod
.state
= DEMOD_UNSYNCD
;
603 // And the reader -> tag commands
604 memset(&Uart
, 0, sizeof(Uart
));
605 Uart
.output
= receivedCmd
;
606 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
607 Uart
.state
= STATE_UNSYNCD
;
609 // And put the FPGA in the appropriate mode
610 // Signal field is off with the appropriate LED
612 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
613 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
615 // Setup for the DMA.
618 lastRxCounter
= DMA_BUFFER_SIZE
;
619 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
623 // And now we loop, receiving samples.
626 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
628 if(behindBy
> maxBehindBy
) {
629 maxBehindBy
= behindBy
;
631 DbpString("blew circular buffer!");
635 if(behindBy
< 1) continue;
640 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
641 upTo
-= DMA_BUFFER_SIZE
;
642 lastRxCounter
+= DMA_BUFFER_SIZE
;
643 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (DWORD
)upTo
;
644 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
648 #define HANDLE_BIT_IF_BODY \
651 trace[traceLen++] = ((rsamples >> 0) & 0xff); \
652 trace[traceLen++] = ((rsamples >> 8) & 0xff); \
653 trace[traceLen++] = ((rsamples >> 16) & 0xff); \
654 trace[traceLen++] = ((rsamples >> 24) & 0xff); \
655 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff); \
656 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff); \
657 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff); \
658 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff); \
659 trace[traceLen++] = Uart.byteCnt; \
660 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
661 traceLen += Uart.byteCnt; \
662 if(traceLen > TRACE_LENGTH) break; \
664 /* And ready to receive another command. */ \
665 Uart.state = STATE_UNSYNCD; \
666 /* And also reset the demod code, which might have been */ \
667 /* false-triggered by the commands from the reader. */ \
668 Demod.state = DEMOD_UNSYNCD; \
671 if(MillerDecoding((smpl & 0xF0) >> 4)) {
672 rsamples
= samples
- Uart
.samples
;
675 if(ManchesterDecoding(smpl
& 0x0F)) {
676 rsamples
= samples
- Demod
.samples
;
679 // timestamp, as a count of samples
680 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
681 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
682 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
683 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
684 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
685 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
686 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
687 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
689 trace
[traceLen
++] = Demod
.len
;
690 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
691 traceLen
+= Demod
.len
;
692 if(traceLen
> TRACE_LENGTH
) break;
696 // And ready to receive another response.
697 memset(&Demod
, 0, sizeof(Demod
));
698 Demod
.output
= receivedResponse
;
699 Demod
.state
= DEMOD_UNSYNCD
;
704 DbpString("cancelled_a");
709 DbpString("COMMAND FINISHED");
711 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
712 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
715 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
716 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
717 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
724 // Prepare communication bits to send to FPGA
725 void Sequence(SecType seq
)
731 // Sequence D: 11110000
732 // modulation with subcarrier during first half
733 ToSend
[ToSendMax
] = 0xf0;
736 // Sequence E: 00001111
737 // modulation with subcarrier during second half
738 ToSend
[ToSendMax
] = 0x0f;
741 // Sequence F: 00000000
742 // no modulation with subcarrier
743 ToSend
[ToSendMax
] = 0x00;
747 // Sequence X: 00001100
748 // drop after half a period
749 ToSend
[ToSendMax
] = 0x0c;
753 // Sequence Y: 00000000
755 ToSend
[ToSendMax
] = 0x00;
758 // Sequence Z: 11000000
760 ToSend
[ToSendMax
] = 0xc0;
765 //-----------------------------------------------------------------------------
766 // Prepare tag messages
767 //-----------------------------------------------------------------------------
768 static void CodeIso14443aAsTag(const BYTE
*cmd
, int len
)
775 // Correction bit, might be removed when not needed
780 ToSendStuffBit(1); // 1
788 for(i
= 0; i
< len
; i
++) {
794 for(j
= 0; j
< 8; j
++) {
795 oddparity
^= (b
& 1);
815 // Flush the buffer in FPGA!!
816 for(i
= 0; i
< 5; i
++) {
820 // Convert from last byte pos to length
823 // Add a few more for slop
824 ToSend
[ToSendMax
++] = 0x00;
825 ToSend
[ToSendMax
++] = 0x00;
829 //-----------------------------------------------------------------------------
830 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
831 //-----------------------------------------------------------------------------
832 static void CodeStrangeAnswer()
838 // Correction bit, might be removed when not needed
843 ToSendStuffBit(1); // 1
863 // Flush the buffer in FPGA!!
864 for(i
= 0; i
< 5; i
++) {
868 // Convert from last byte pos to length
871 // Add a few more for slop
872 ToSend
[ToSendMax
++] = 0x00;
873 ToSend
[ToSendMax
++] = 0x00;
877 int LogTrace(const BYTE
* btBytes
, int iLen
, int iSamples
, DWORD dwParity
, BOOL bReader
)
879 // Trace the random, i'm curious
880 rsamples
+= iSamples
;
881 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
882 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
883 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
884 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
886 trace
[traceLen
- 1] |= 0x80;
888 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
889 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
890 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
891 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
892 trace
[traceLen
++] = iLen
;
893 memcpy(trace
+ traceLen
, btBytes
, iLen
);
895 return (traceLen
< TRACE_LENGTH
);
898 //-----------------------------------------------------------------------------
899 // Wait for commands from reader
900 // Stop when button is pressed
901 // Or return TRUE when command is captured
902 //-----------------------------------------------------------------------------
903 static BOOL
GetIso14443aCommandFromReader(BYTE
*received
, int *len
, int maxLen
)
905 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
906 // only, since we are receiving, not transmitting).
907 // Signal field is off with the appropriate LED
909 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
911 // Now run a `software UART' on the stream of incoming samples.
912 Uart
.output
= received
;
913 Uart
.byteCntMax
= maxLen
;
914 Uart
.state
= STATE_UNSYNCD
;
919 if(BUTTON_PRESS()) return FALSE
;
921 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
922 AT91C_BASE_SSC
->SSC_THR
= 0x00;
924 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
925 BYTE b
= (BYTE
)AT91C_BASE_SSC
->SSC_RHR
;
926 if(MillerDecoding((b
& 0xf0) >> 4)) {
930 if(MillerDecoding(b
& 0x0f)) {
938 //-----------------------------------------------------------------------------
939 // Main loop of simulated tag: receive commands from reader, decide what
940 // response to send, and send it.
941 //-----------------------------------------------------------------------------
942 void SimulateIso14443aTag(int tagType
, int TagUid
)
944 // This function contains the tag emulation
946 // Prepare protocol messages
947 // static const BYTE cmd1[] = { 0x26 };
948 // static const BYTE response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
950 static const BYTE response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
951 // static const BYTE response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
954 // static const BYTE cmd2[] = { 0x93, 0x20 };
955 //static const BYTE response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
960 static const BYTE response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
963 // When reader selects us during cascade1 it will send cmd3
964 //BYTE response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
965 BYTE response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
966 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
968 // send cascade2 2nd half of UID
969 static const BYTE response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
970 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
973 // When reader selects us during cascade2 it will send cmd3a
974 //BYTE response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
975 BYTE response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
976 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
978 static const BYTE response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
983 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
985 // 144 data bits (18 * 8)
988 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
989 // 1 just for the case
993 // 166 bytes, since every bit that needs to be send costs us a byte
997 // Respond with card type
998 BYTE
*resp1
= (((BYTE
*)BigBuf
) + 800);
1001 // Anticollision cascade1 - respond with uid
1002 BYTE
*resp2
= (((BYTE
*)BigBuf
) + 970);
1005 // Anticollision cascade2 - respond with 2nd half of uid if asked
1006 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
1007 BYTE
*resp2a
= (((BYTE
*)BigBuf
) + 1140);
1010 // Acknowledge select - cascade 1
1011 BYTE
*resp3
= (((BYTE
*)BigBuf
) + 1310);
1014 // Acknowledge select - cascade 2
1015 BYTE
*resp3a
= (((BYTE
*)BigBuf
) + 1480);
1018 // Response to a read request - not implemented atm
1019 BYTE
*resp4
= (((BYTE
*)BigBuf
) + 1550);
1022 // Authenticate response - nonce
1023 BYTE
*resp5
= (((BYTE
*)BigBuf
) + 1720);
1026 BYTE
*receivedCmd
= (BYTE
*)BigBuf
;
1033 // To control where we are in the protocol
1037 // Just to allow some checks
1045 memset(receivedCmd
, 0x44, 400);
1047 // Prepare the responses of the anticollision phase
1048 // there will be not enough time to do this at the moment the reader sends it REQA
1050 // Answer to request
1051 CodeIso14443aAsTag(response1
, sizeof(response1
));
1052 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1054 // Send our UID (cascade 1)
1055 CodeIso14443aAsTag(response2
, sizeof(response2
));
1056 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1058 // Answer to select (cascade1)
1059 CodeIso14443aAsTag(response3
, sizeof(response3
));
1060 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1062 // Send the cascade 2 2nd part of the uid
1063 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1064 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1066 // Answer to select (cascade 2)
1067 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1068 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1070 // Strange answer is an example of rare message size (3 bits)
1071 CodeStrangeAnswer();
1072 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1074 // Authentication answer (random nonce)
1075 CodeIso14443aAsTag(response5
, sizeof(response5
));
1076 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1078 // We need to listen to the high-frequency, peak-detected path.
1079 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1087 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1088 DbpString("button press");
1091 // doob - added loads of debug strings so we can see what the reader is saying to us during the sim as hi14alist is not populated
1092 // Okay, look at the command now.
1094 i
= 1; // first byte transmitted
1095 if(receivedCmd
[0] == 0x26) {
1096 // Received a REQUEST
1097 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1098 //DbpString("Hello request from reader:");
1099 } else if(receivedCmd
[0] == 0x52) {
1100 // Received a WAKEUP
1101 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1102 // //DbpString("Wakeup request from reader:");
1104 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1105 // Received request for UID (cascade 1)
1106 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1107 // DbpString("UID (cascade 1) request from reader:");
1108 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1111 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1112 // Received request for UID (cascade 2)
1113 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1114 // DbpString("UID (cascade 2) request from reader:");
1115 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1118 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1119 // Received a SELECT
1120 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1121 // DbpString("Select (cascade 1) request from reader:");
1122 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1125 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1126 // Received a SELECT
1127 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1128 // DbpString("Select (cascade 2) request from reader:");
1129 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1132 } else if(receivedCmd
[0] == 0x30) {
1134 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1135 DbpString("Read request from reader:");
1136 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1139 } else if(receivedCmd
[0] == 0x50) {
1141 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1142 DbpString("Reader requested we HALT!:");
1144 } else if(receivedCmd
[0] == 0x60) {
1145 // Received an authentication request
1146 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1147 DbpString("Authenticate request from reader:");
1148 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1150 } else if(receivedCmd
[0] == 0xE0) {
1151 // Received a RATS request
1152 resp
= resp1
; respLen
= 0;order
= 70;
1153 DbpString("RATS request from reader:");
1154 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1156 // Never seen this command before
1157 DbpString("Unknown command received from reader:");
1158 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1159 DbpIntegers(receivedCmd
[3], receivedCmd
[4], receivedCmd
[5]);
1160 DbpIntegers(receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1163 resp
= resp1
; respLen
= 0; order
= 0;
1166 // Count number of wakeups received after a halt
1167 if(order
== 6 && lastorder
== 5) { happened
++; }
1169 // Count number of other messages after a halt
1170 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1172 // Look at last parity bit to determine timing of answer
1173 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1174 // 1236, so correction bit needed
1178 memset(receivedCmd
, 0x44, 32);
1180 if(cmdsRecvd
> 999) {
1181 DbpString("1000 commands later...");
1188 if(respLen
<= 0) continue;
1190 // Modulate Manchester
1191 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1192 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1195 // ### Transmit the response ###
1198 fdt_indicator
= FALSE
;
1200 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1201 volatile BYTE b
= (BYTE
)AT91C_BASE_SSC
->SSC_RHR
;
1204 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1212 AT91C_BASE_SSC
->SSC_THR
= b
;
1218 if(BUTTON_PRESS()) {
1225 DbpIntegers(happened
, happened2
, cmdsRecvd
);
1229 //-----------------------------------------------------------------------------
1230 // Transmit the command (to the tag) that was placed in ToSend[].
1231 //-----------------------------------------------------------------------------
1232 static void TransmitFor14443a(const BYTE
*cmd
, int len
, int *samples
, int *wait
)
1236 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1238 if(*wait
< 10) { *wait
= 10; }
1240 for(c
= 0; c
< *wait
;) {
1241 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1242 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1245 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1246 volatile DWORD r
= AT91C_BASE_SSC
->SSC_RHR
;
1254 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1255 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
1261 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1262 volatile DWORD r
= AT91C_BASE_SSC
->SSC_RHR
;
1267 *samples
= (c
+ *wait
) << 3;
1270 //-----------------------------------------------------------------------------
1271 // To generate an arbitrary stream from reader
1273 //-----------------------------------------------------------------------------
1274 void ArbitraryFromReader(const BYTE
*cmd
, int parity
, int len
)
1283 // Start of Communication (Seq. Z)
1287 for(i
= 0; i
< len
; i
++) {
1290 for(j
= 0; j
< 8; j
++) {
1310 // Predefined parity bit, the flipper flips when needed, because of flips in byte sent
1311 if(((parity
>> (len
- i
- 1)) & 1)) {
1328 // End of Communication
1346 // Convert from last character reference to length
1350 //-----------------------------------------------------------------------------
1351 // Code a 7-bit command without parity bit
1352 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1353 //-----------------------------------------------------------------------------
1354 void ShortFrameFromReader(const BYTE
*cmd
)
1362 // Start of Communication (Seq. Z)
1367 for(j
= 0; j
< 7; j
++) {
1386 // End of Communication
1404 // Convert from last character reference to length
1408 //-----------------------------------------------------------------------------
1409 // Prepare reader command to send to FPGA
1411 //-----------------------------------------------------------------------------
1412 void CodeIso14443aAsReader(const BYTE
*cmd
, int len
)
1421 // Start of Communication (Seq. Z)
1425 for(i
= 0; i
< len
; i
++) {
1429 for(j
= 0; j
< 8; j
++) {
1430 oddparity
^= (b
& 1);
1467 // End of Communication
1485 // Convert from last character reference to length
1490 //-----------------------------------------------------------------------------
1491 // Wait a certain time for tag response
1492 // If a response is captured return TRUE
1493 // If it takes to long return FALSE
1494 //-----------------------------------------------------------------------------
1495 static BOOL
GetIso14443aAnswerFromTag(BYTE
*receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //BYTE *buffer
1497 // buffer needs to be 512 bytes
1500 // Set FPGA mode to "reader listen mode", no modulation (listen
1501 // only, since we are receiving, not transmitting).
1502 // Signal field is on with the appropriate LED
1504 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1506 // Now get the answer from the card
1507 Demod
.output
= receivedResponse
;
1509 Demod
.state
= DEMOD_UNSYNCD
;
1518 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1519 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1522 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1523 if(c
< 512) { c
++; } else { return FALSE
; }
1524 b
= (BYTE
)AT91C_BASE_SSC
->SSC_RHR
;
1525 if(ManchesterDecoding((b
& 0xf0) >> 4)) {
1526 *samples
= ((c
- 1) << 3) + 4;
1529 if(ManchesterDecoding(b
& 0x0f)) {
1539 //-----------------------------------------------------------------------------
1540 // Read an ISO 14443a tag. Send out commands and store answers.
1542 //-----------------------------------------------------------------------------
1543 void ReaderIso14443a(DWORD parameter
)
1546 static const BYTE cmd1
[] = { 0x52 }; // or 0x26
1547 static const BYTE cmd2
[] = { 0x93,0x20 };
1548 // UID = 0x2a,0x69,0x8d,0x43,0x8d, last two bytes are CRC bytes
1549 BYTE cmd3
[] = { 0x93,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1551 // For Ultralight add an extra anticollission layer -> 95 20 and then 95 70
1553 // greg - here we will add our cascade level 2 anticolission and select functions to deal with ultralight // and 7-byte UIDs in generall...
1554 BYTE cmd4
[] = {0x95,0x20}; // ask for cascade 2 select
1556 //BYTE cmd3a[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1560 BYTE cmd5
[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1563 // RATS (request for answer to select)
1564 //BYTE cmd6[] = { 0xe0,0x50,0xbc,0xa5 }; // original RATS
1565 BYTE cmd6
[] = { 0xe0,0x21,0xb2,0xc7 }; // Desfire RATS
1568 BYTE cmd7
[] = { 0x60, 0x00, 0x00, 0x00 };
1570 int reqaddr
= 2024; // was 2024 - tied to other size changes
1573 BYTE
*req1
= (((BYTE
*)BigBuf
) + reqaddr
);
1576 BYTE
*req2
= (((BYTE
*)BigBuf
) + reqaddr
+ reqsize
);
1579 BYTE
*req3
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 2));
1582 // greg added req 4 & 5 to deal with cascade 2 section
1583 BYTE
*req4
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 3));
1586 BYTE
*req5
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 4));
1589 BYTE
*req6
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 5));
1592 BYTE
*req7
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 6));
1595 BYTE
*receivedAnswer
= (((BYTE
*)BigBuf
) + 3560); // was 3560 - tied to other size changes
1597 //BYTE *trace = (BYTE *)BigBuf;
1602 memset(trace
, 0x44, 2000); // was 2000 - tied to oter size chnages
1603 // setting it to 3000 causes no tag responses to be detected (2900 is ok)
1604 // setting it to 1000 causes no tag responses to be detected
1606 // Prepare some commands!
1607 ShortFrameFromReader(cmd1
);
1608 memcpy(req1
, ToSend
, ToSendMax
); req1Len
= ToSendMax
;
1610 CodeIso14443aAsReader(cmd2
, sizeof(cmd2
));
1611 memcpy(req2
, ToSend
, ToSendMax
); req2Len
= ToSendMax
;
1613 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1614 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1617 CodeIso14443aAsReader(cmd4
, sizeof(cmd4
)); // 4 is cascade 2 request
1618 memcpy(req4
, ToSend
, ToSendMax
); req4Len
= ToSendMax
;
1621 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
)); // 5 is cascade 2 select
1622 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1625 CodeIso14443aAsReader(cmd6
, sizeof(cmd6
));
1626 memcpy(req6
, ToSend
, ToSendMax
); req6Len
= ToSendMax
;
1631 // Start from off (no field generated)
1632 // Signal field is off with the appropriate LED
1634 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1637 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1640 // Now give it time to spin up.
1641 // Signal field is on with the appropriate LED
1643 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1656 // Send WUPA (or REQA)
1657 TransmitFor14443a(req1
, req1Len
, &tsamples
, &wait
);
1659 // Store reader command in buffer
1660 if (!LogTrace(cmd1
,1,0,GetParity(cmd1
,1),TRUE
)) break;
1662 // Test if the action was cancelled
1663 if(BUTTON_PRESS()) {
1667 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1670 if (!LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
)) break;
1672 // Store reader command in buffer
1673 if (!LogTrace(cmd2
,2,0,GetParity(cmd2
,2),TRUE
)) break;
1674 TransmitFor14443a(req2
, req2Len
, &samples
, &wait
);
1676 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1679 if (!LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
)) break;
1681 // Construct SELECT UID command
1682 // First copy the 5 bytes (Mifare Classic) after the 93 70
1683 memcpy(cmd3
+2,receivedAnswer
,5);
1684 // Secondly compute the two CRC bytes at the end
1685 ComputeCrc14443(CRC_14443_A
, cmd3
, 7, &cmd3
[7], &cmd3
[8]);
1687 // Store reader command in buffer
1688 if (!LogTrace(cmd3
,9,0,GetParity(cmd5
,9),TRUE
)) break;
1690 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1691 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1694 TransmitFor14443a(req3
, req3Len
, &samples
, &wait
);
1695 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1698 if (!LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
)) break;
1700 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1701 // which case we need to make a cascade 2 request and select - this is a long UID
1702 if (receivedAnswer
[0] == 0x88)
1704 // Do cascade level 2 stuff
1705 ///////////////////////////////////////////////////////////////////
1706 // First issue a '95 20' identify request
1707 // Ask for card UID (part 2)
1708 TransmitFor14443a(req4
, req4Len
, &tsamples
, &wait
);
1710 // Store reader command in buffer
1711 if (!LogTrace(cmd4
,2,0,GetParity(cmd4
,2),TRUE
)) break;
1713 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1715 //////////////////////////////////////////////////////////////////
1716 // Then Construct SELECT UID (cascasde 2) command
1717 DbpString("Just about to copy the UID out of the cascade 2 id req");
1718 // First copy the 5 bytes (Mifare Classic) after the 95 70
1719 memcpy(cmd5
+2,receivedAnswer
,5);
1720 // Secondly compute the two CRC bytes at the end
1721 ComputeCrc14443(CRC_14443_A
, cmd4
, 7, &cmd5
[7], &cmd5
[8]);
1723 // Store reader command in buffer
1724 if (!LogTrace(cmd5
,9,0,GetParity(cmd5
,9),TRUE
)) break;
1726 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
));
1727 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1730 TransmitFor14443a(req4
, req4Len
, &samples
, &wait
);
1731 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1734 if (!LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
)) break;
1737 // Secondly compute the two CRC bytes at the end
1738 ComputeCrc14443(CRC_14443_A
, cmd7
, 2, &cmd7
[2], &cmd7
[3]);
1739 CodeIso14443aAsReader(cmd7
, sizeof(cmd7
));
1740 memcpy(req7
, ToSend
, ToSendMax
); req7Len
= ToSendMax
;
1742 // Send authentication request (Mifare Classic)
1743 TransmitFor14443a(req7
, req7Len
, &samples
, &wait
);
1744 // Store reader command in buffer
1745 if (!LogTrace(cmd7
,4,0,GetParity(cmd7
,4),TRUE
)) break;
1747 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) continue;
1749 // We received probably a random, continue and trace!
1750 if (!LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
)) break;
1754 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1756 DbpIntegers(rsamples
, 0xCC, 0xCC);
1757 DbpString("ready..");