1 //-----------------------------------------------------------------------------
2 // Gerhard de Koning Gans - May 2008
3 // Hagen Fritsch - June 2010
4 // Gerhard de Koning Gans - May 2011
5 // Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
7 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
8 // at your option, any later version. See the LICENSE.txt file for the text of
10 //-----------------------------------------------------------------------------
11 // Routines to support iClass.
12 //-----------------------------------------------------------------------------
13 // Based on ISO14443a implementation. Still in experimental phase.
14 // Contribution made during a security research at Radboud University Nijmegen
16 // Please feel free to contribute and extend iClass support!!
17 //-----------------------------------------------------------------------------
21 // We still have sometimes a demodulation error when snooping iClass communication.
22 // The resulting trace of a read-block-03 command may look something like this:
24 // + 22279: : 0c 03 e8 01
26 // ...with an incorrect answer...
28 // + 85: 0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb 33 bb 00 01! 0e! 04! bb !crc
30 // We still left the error signalling bytes in the traces like 0xbb
32 // A correct trace should look like this:
34 // + 21112: : 0c 03 e8 01
35 // + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5
37 //-----------------------------------------------------------------------------
39 #include "proxmark3.h"
45 // Needed for CRC in emulation mode;
46 // same construction as in ISO 14443;
47 // different initial value (CRC_ICLASS)
48 #include "iso14443crc.h"
49 #include "iso15693tools.h"
51 static int timeout
= 4096;
54 // Sequence D: 11110000 modulation with subcarrier during first half
55 // Sequence E: 00001111 modulation with subcarrier during second half
56 // Sequence F: 00000000 no modulation with subcarrier
58 // Sequence X: 00001100 drop after half a period
59 // Sequence Y: 00000000 no drop
60 // Sequence Z: 11000000 drop at start
65 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
);
67 //-----------------------------------------------------------------------------
68 // The software UART that receives commands from the reader, and its state
70 //-----------------------------------------------------------------------------
74 STATE_START_OF_COMMUNICATION
,
95 static RAMFUNC
int OutOfNDecoding(int bit
)
100 if(!Uart
.bitBuffer
) {
101 Uart
.bitBuffer
= bit
^ 0xFF0;
105 Uart
.bitBuffer
<<= 4;
106 Uart
.bitBuffer
^= bit
;
110 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
113 if(Uart.byteCnt > 15) { return TRUE; }
119 if(Uart
.state
!= STATE_UNSYNCD
) {
122 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
128 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
134 if(bit
!= bitright
) { bit
= bitright
; }
137 // So, now we only have to deal with *bit*, lets see...
138 if(Uart
.posCnt
== 1) {
139 // measurement first half bitperiod
141 // Drop in first half means that we are either seeing
144 if(Uart
.nOutOfCnt
== 1) {
145 // End of Communication
146 Uart
.state
= STATE_UNSYNCD
;
148 if(Uart
.byteCnt
== 0) {
149 // Its not straightforward to show single EOFs
150 // So just leave it and do not return TRUE
151 Uart
.output
[Uart
.byteCnt
] = 0xf0;
154 // Calculate the parity bit for the client...
161 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
162 // When not part of SOF or EOF, it is an error
163 Uart
.state
= STATE_UNSYNCD
;
170 // measurement second half bitperiod
171 // Count the bitslot we are in... (ISO 15693)
175 if(Uart
.dropPosition
) {
176 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
182 // It is an error if we already have seen a drop in current frame
183 Uart
.state
= STATE_UNSYNCD
;
187 Uart
.dropPosition
= Uart
.nOutOfCnt
;
194 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
197 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
198 if(Uart
.dropPosition
== 4) {
199 Uart
.state
= STATE_RECEIVING
;
202 else if(Uart
.dropPosition
== 3) {
203 Uart
.state
= STATE_RECEIVING
;
205 //Uart.output[Uart.byteCnt] = 0xdd;
209 Uart
.state
= STATE_UNSYNCD
;
212 Uart
.dropPosition
= 0;
217 if(!Uart
.dropPosition
) {
218 Uart
.state
= STATE_UNSYNCD
;
227 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
228 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
230 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
232 Uart
.dropPosition
= 0;
234 if(Uart
.bitCnt
== 8) {
235 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
238 // Calculate the parity bit for the client...
239 Uart
.parityBits
<<= 1;
240 Uart
.parityBits
^= OddByteParity
[(Uart
.shiftReg
& 0xff)];
248 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
251 if(!Uart
.dropPosition
) {
252 Uart
.state
= STATE_UNSYNCD
;
258 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
261 // Calculate the parity bit for the client...
262 Uart
.parityBits
<<= 1;
263 Uart
.parityBits
^= OddByteParity
[(Uart
.dropPosition
& 0xff)];
268 Uart
.dropPosition
= 0;
273 Uart.output[Uart.byteCnt] = 0xAA;
275 Uart.output[Uart.byteCnt] = error & 0xFF;
277 Uart.output[Uart.byteCnt] = 0xAA;
279 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
281 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
283 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
285 Uart.output[Uart.byteCnt] = 0xAA;
293 bit
= Uart
.bitBuffer
& 0xf0;
295 bit
^= 0x0F; // drops become 1s ;-)
297 // should have been high or at least (4 * 128) / fc
298 // according to ISO this should be at least (9 * 128 + 20) / fc
299 if(Uart
.highCnt
== 8) {
300 // we went low, so this could be start of communication
301 // it turns out to be safer to choose a less significant
302 // syncbit... so we check whether the neighbour also represents the drop
303 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
304 Uart
.syncBit
= bit
& 8;
306 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
307 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
308 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
309 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
310 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
311 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
314 // the first half bit period is expected in next sample
319 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
322 Uart
.state
= STATE_START_OF_COMMUNICATION
;
327 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
328 Uart
.dropPosition
= 0;
337 if(Uart
.highCnt
< 8) {
346 //=============================================================================
348 //=============================================================================
353 DEMOD_START_OF_COMMUNICATION
,
354 DEMOD_START_OF_COMMUNICATION2
,
355 DEMOD_START_OF_COMMUNICATION3
,
359 DEMOD_END_OF_COMMUNICATION
,
360 DEMOD_END_OF_COMMUNICATION2
,
384 static RAMFUNC
int ManchesterDecoding(int v
)
391 Demod
.buffer
= Demod
.buffer2
;
392 Demod
.buffer2
= Demod
.buffer3
;
400 if(Demod
.state
==DEMOD_UNSYNCD
) {
401 Demod
.output
[Demod
.len
] = 0xfa;
404 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
407 Demod
.syncBit
= 0x08;
414 Demod
.syncBit
= 0x04;
421 Demod
.syncBit
= 0x02;
424 if(bit
& 0x01 && Demod
.syncBit
) {
425 Demod
.syncBit
= 0x01;
430 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
431 Demod
.sub
= SUB_FIRST_HALF
;
434 Demod
.parityBits
= 0;
437 //if(trigger) LED_A_OFF(); // Not useful in this case...
438 switch(Demod
.syncBit
) {
439 case 0x08: Demod
.samples
= 3; break;
440 case 0x04: Demod
.samples
= 2; break;
441 case 0x02: Demod
.samples
= 1; break;
442 case 0x01: Demod
.samples
= 0; break;
444 // SOF must be long burst... otherwise stay unsynced!!!
445 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
446 Demod
.state
= DEMOD_UNSYNCD
;
450 // SOF must be long burst... otherwise stay unsynced!!!
451 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
452 Demod
.state
= DEMOD_UNSYNCD
;
462 modulation
= bit
& Demod
.syncBit
;
463 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
464 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
468 if(Demod
.posCount
==0) {
471 Demod
.sub
= SUB_FIRST_HALF
;
474 Demod
.sub
= SUB_NONE
;
479 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
480 if(Demod.state!=DEMOD_ERROR_WAIT) {
481 Demod.state = DEMOD_ERROR_WAIT;
482 Demod.output[Demod.len] = 0xaa;
486 //else if(modulation) {
488 if(Demod
.sub
== SUB_FIRST_HALF
) {
489 Demod
.sub
= SUB_BOTH
;
492 Demod
.sub
= SUB_SECOND_HALF
;
495 else if(Demod
.sub
== SUB_NONE
) {
496 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
497 Demod
.output
[Demod
.len
] = 0x0f;
499 Demod
.parityBits
<<= 1;
500 Demod
.parityBits
^= OddByteParity
[0x0f];
501 Demod
.state
= DEMOD_UNSYNCD
;
506 Demod
.state
= DEMOD_ERROR_WAIT
;
509 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
510 Demod.state = DEMOD_ERROR_WAIT;
511 Demod.output[Demod.len] = 0xaa;
516 switch(Demod
.state
) {
517 case DEMOD_START_OF_COMMUNICATION
:
518 if(Demod
.sub
== SUB_BOTH
) {
519 //Demod.state = DEMOD_MANCHESTER_D;
520 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
522 Demod
.sub
= SUB_NONE
;
525 Demod
.output
[Demod
.len
] = 0xab;
526 Demod
.state
= DEMOD_ERROR_WAIT
;
530 case DEMOD_START_OF_COMMUNICATION2
:
531 if(Demod
.sub
== SUB_SECOND_HALF
) {
532 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
535 Demod
.output
[Demod
.len
] = 0xab;
536 Demod
.state
= DEMOD_ERROR_WAIT
;
540 case DEMOD_START_OF_COMMUNICATION3
:
541 if(Demod
.sub
== SUB_SECOND_HALF
) {
542 // Demod.state = DEMOD_MANCHESTER_D;
543 Demod
.state
= DEMOD_SOF_COMPLETE
;
544 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
548 Demod
.output
[Demod
.len
] = 0xab;
549 Demod
.state
= DEMOD_ERROR_WAIT
;
553 case DEMOD_SOF_COMPLETE
:
554 case DEMOD_MANCHESTER_D
:
555 case DEMOD_MANCHESTER_E
:
556 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
557 // 00001111 = 1 (0 in 14443)
558 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
560 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
561 Demod
.state
= DEMOD_MANCHESTER_D
;
563 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
565 Demod
.shiftReg
>>= 1;
566 Demod
.state
= DEMOD_MANCHESTER_E
;
568 else if(Demod
.sub
== SUB_BOTH
) {
569 Demod
.state
= DEMOD_MANCHESTER_F
;
572 Demod
.state
= DEMOD_ERROR_WAIT
;
577 case DEMOD_MANCHESTER_F
:
578 // Tag response does not need to be a complete byte!
579 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
580 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
581 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
582 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
584 // No parity bit, so just shift a 0
585 Demod
.parityBits
<<= 1;
588 Demod
.state
= DEMOD_UNSYNCD
;
592 Demod
.output
[Demod
.len
] = 0xad;
593 Demod
.state
= DEMOD_ERROR_WAIT
;
598 case DEMOD_ERROR_WAIT
:
599 Demod
.state
= DEMOD_UNSYNCD
;
603 Demod
.output
[Demod
.len
] = 0xdd;
604 Demod
.state
= DEMOD_UNSYNCD
;
608 /*if(Demod.bitCount>=9) {
609 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
612 Demod.parityBits <<= 1;
613 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
618 if(Demod
.bitCount
>=8) {
619 Demod
.shiftReg
>>= 1;
620 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
623 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
624 Demod
.parityBits
<<= 1;
625 Demod
.parityBits
^= OddByteParity
[(Demod
.shiftReg
& 0xff)];
632 Demod
.output
[Demod
.len
] = 0xBB;
634 Demod
.output
[Demod
.len
] = error
& 0xFF;
636 Demod
.output
[Demod
.len
] = 0xBB;
638 Demod
.output
[Demod
.len
] = bit
& 0xFF;
640 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
643 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
645 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
647 Demod
.output
[Demod
.len
] = 0xBB;
654 } // end (state != UNSYNCED)
659 //=============================================================================
660 // Finally, a `sniffer' for iClass communication
661 // Both sides of communication!
662 //=============================================================================
664 //-----------------------------------------------------------------------------
665 // Record the sequence of commands sent by the reader to the tag, with
666 // triggering so that we start recording at the point that the tag is moved
668 //-----------------------------------------------------------------------------
669 void RAMFUNC
SnoopIClass(void)
672 // #define RECV_CMD_OFFSET 3032
673 // #define RECV_RES_OFFSET 3096
674 // #define DMA_BUFFER_OFFSET 3160
675 // #define DMA_BUFFER_SIZE 4096
676 // #define TRACE_SIZE 3000
678 // We won't start recording the frames that we acquire until we trigger;
679 // a good trigger condition to get started is probably when we see a
680 // response from the tag.
681 //int triggered = FALSE; // FALSE to wait first for card
683 // The command (reader -> tag) that we're receiving.
684 // The length of a received command will in most cases be no more than 18 bytes.
685 // So 32 should be enough!
686 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
687 // The response (tag -> reader) that we're receiving.
688 uint8_t *receivedResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
690 // As we receive stuff, we copy it from receivedCmd or receivedResponse
691 // into trace, along with its length and other annotations.
692 //uint8_t *trace = (uint8_t *)BigBuf;
694 // reset traceLen to 0
695 iso14a_set_tracing(TRUE
);
696 iso14a_clear_trace();
697 iso14a_set_trigger(FALSE
);
699 // The DMA buffer, used to stream samples from the FPGA
700 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
706 // Count of samples received so far, so that we can include timing
707 // information in the trace buffer.
711 memset(trace
, 0x44, RECV_CMD_OFFSET
);
713 // Set up the demodulator for tag -> reader responses.
714 Demod
.output
= receivedResponse
;
716 Demod
.state
= DEMOD_UNSYNCD
;
718 // Setup for the DMA.
721 lastRxCounter
= DMA_BUFFER_SIZE
;
722 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
724 // And the reader -> tag commands
725 memset(&Uart
, 0, sizeof(Uart
));
726 Uart
.output
= receivedCmd
;
727 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
728 Uart
.state
= STATE_UNSYNCD
;
730 // And put the FPGA in the appropriate mode
731 // Signal field is off with the appropriate LED
733 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
734 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
741 // And now we loop, receiving samples.
745 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
747 if(behindBy
> maxBehindBy
) {
748 maxBehindBy
= behindBy
;
750 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
754 if(behindBy
< 1) continue;
760 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
761 upTo
-= DMA_BUFFER_SIZE
;
762 lastRxCounter
+= DMA_BUFFER_SIZE
;
763 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
764 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
773 //decbyte ^= ((smpl & 0x01) << (3 - div));
774 //decbyte ^= (((smpl & 0x01) | ((smpl & 0x02) >> 1)) << (3 - div)); // better already...
775 //decbyte ^= (((smpl & 0x01) | ((smpl & 0x02) >> 1) | ((smpl & 0x04) >> 2)) << (3 - div)); // even better...
777 decbyte
^= (1 << (3 - div
));
779 //decbyte ^= (MajorityNibble[(smpl & 0x0F)] << (3 - div));
781 // FOR READER SIDE COMMUMICATION...
782 //decbyte ^= ((smpl & 0x10) << (3 - div));
784 decbyter
^= (smpl
& 0x30);
788 if((div
+ 1) % 2 == 0) {
790 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
791 rsamples
= samples
- Uart
.samples
;
794 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
795 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
796 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
797 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
798 trace
[traceLen
++] = ((Uart
.parityBits
>> 0) & 0xff);
799 trace
[traceLen
++] = ((Uart
.parityBits
>> 8) & 0xff);
800 trace
[traceLen
++] = ((Uart
.parityBits
>> 16) & 0xff);
801 trace
[traceLen
++] = ((Uart
.parityBits
>> 24) & 0xff);
802 trace
[traceLen
++] = Uart
.byteCnt
;
803 memcpy(trace
+traceLen
, receivedCmd
, Uart
.byteCnt
);
804 traceLen
+= Uart
.byteCnt
;
805 if(traceLen
> TRACE_SIZE
) break;
807 /* And ready to receive another command. */
808 Uart
.state
= STATE_UNSYNCD
;
809 /* And also reset the demod code, which might have been */
810 /* false-triggered by the commands from the reader. */
811 Demod
.state
= DEMOD_UNSYNCD
;
820 if(ManchesterDecoding(smpl
& 0x0F)) {
821 rsamples
= samples
- Demod
.samples
;
824 // timestamp, as a count of samples
825 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
826 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
827 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
828 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
829 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
830 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
831 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
832 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
834 trace
[traceLen
++] = Demod
.len
;
835 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
836 traceLen
+= Demod
.len
;
837 if(traceLen
> TRACE_SIZE
) break;
841 // And ready to receive another response.
842 memset(&Demod
, 0, sizeof(Demod
));
843 Demod
.output
= receivedResponse
;
844 Demod
.state
= DEMOD_UNSYNCD
;
854 DbpString("cancelled_a");
859 DbpString("COMMAND FINISHED");
861 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
862 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
865 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
866 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
867 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
874 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
876 for(i
= 0; i
< 8; i
++) {
877 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
881 //-----------------------------------------------------------------------------
882 // Wait for commands from reader
883 // Stop when button is pressed
884 // Or return TRUE when command is captured
885 //-----------------------------------------------------------------------------
886 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
888 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
889 // only, since we are receiving, not transmitting).
890 // Signal field is off with the appropriate LED
892 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
894 // Now run a `software UART' on the stream of incoming samples.
895 Uart
.output
= received
;
896 Uart
.byteCntMax
= maxLen
;
897 Uart
.state
= STATE_UNSYNCD
;
902 if(BUTTON_PRESS()) return FALSE
;
904 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
905 AT91C_BASE_SSC
->SSC_THR
= 0x00;
907 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
908 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
909 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
913 if(OutOfNDecoding(b
& 0x0f)) {
922 //-----------------------------------------------------------------------------
923 // Prepare tag messages
924 //-----------------------------------------------------------------------------
925 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
932 ToSend
[++ToSendMax
] = 0x00;
933 ToSend
[++ToSendMax
] = 0x00;
934 ToSend
[++ToSendMax
] = 0x00;
935 ToSend
[++ToSendMax
] = 0xff;
936 ToSend
[++ToSendMax
] = 0xff;
937 ToSend
[++ToSendMax
] = 0xff;
938 ToSend
[++ToSendMax
] = 0x00;
939 ToSend
[++ToSendMax
] = 0xff;
941 for(i
= 0; i
< len
; i
++) {
946 for(j
= 0; j
< 8; j
++) {
948 ToSend
[++ToSendMax
] = 0x00;
949 ToSend
[++ToSendMax
] = 0xff;
951 ToSend
[++ToSendMax
] = 0xff;
952 ToSend
[++ToSendMax
] = 0x00;
959 ToSend
[++ToSendMax
] = 0xff;
960 ToSend
[++ToSendMax
] = 0x00;
961 ToSend
[++ToSendMax
] = 0xff;
962 ToSend
[++ToSendMax
] = 0xff;
963 ToSend
[++ToSendMax
] = 0xff;
964 ToSend
[++ToSendMax
] = 0x00;
965 ToSend
[++ToSendMax
] = 0x00;
966 ToSend
[++ToSendMax
] = 0x00;
968 // Convert from last byte pos to length
973 static void CodeIClassTagSOF()
978 ToSend
[++ToSendMax
] = 0x00;
979 ToSend
[++ToSendMax
] = 0x00;
980 ToSend
[++ToSendMax
] = 0x00;
981 ToSend
[++ToSendMax
] = 0xff;
982 ToSend
[++ToSendMax
] = 0xff;
983 ToSend
[++ToSendMax
] = 0xff;
984 ToSend
[++ToSendMax
] = 0x00;
985 ToSend
[++ToSendMax
] = 0xff;
987 // Convert from last byte pos to length
991 //-----------------------------------------------------------------------------
992 // Simulate iClass Card
993 // Only CSN (Card Serial Number)
995 //-----------------------------------------------------------------------------
996 void SimulateIClass(uint8_t arg0
, uint8_t *datain
)
998 uint8_t simType
= arg0
;
1000 // Enable and clear the trace
1003 memset(trace
, 0x44, TRACE_SIZE
);
1005 // CSN followed by two CRC bytes
1006 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1007 uint8_t response3
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
1010 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1013 // Use the CSN from commandline
1014 memcpy(response3
, datain
, 8);
1017 // Construct anticollision-CSN
1018 rotateCSN(response3
,response2
);
1020 // Compute CRC on both CSNs
1021 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1022 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1027 // Tag anticoll. CSN
1028 // Reader 81 anticoll. CSN
1033 uint8_t* respdata
= NULL
;
1037 // Respond SOF -- takes 8 bytes
1038 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1041 // Anticollision CSN (rotated CSN)
1042 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1043 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1047 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1048 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1052 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1053 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1057 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1058 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1061 // Prepare card messages
1064 // First card answer: SOF
1066 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1068 // Anticollision CSN
1069 CodeIClassTagAnswer(response2
, sizeof(response2
));
1070 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1073 CodeIClassTagAnswer(response3
, sizeof(response3
));
1074 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1077 CodeIClassTagAnswer(response4
, sizeof(response4
));
1078 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1080 // We need to listen to the high-frequency, peak-detected path.
1081 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1084 // To control where we are in the protocol
1090 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1091 DbpString("button press");
1095 // Okay, look at the command now.
1096 if(receivedCmd
[0] == 0x0a) {
1097 // Reader in anticollission phase
1098 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1100 respsize
= sizeof(sof
);
1101 //resp = resp2; respLen = resp2Len; order = 2;
1102 //DbpString("Hello request from reader:");
1103 } else if(receivedCmd
[0] == 0x0c) {
1104 // Reader asks for anticollission CSN
1105 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1106 respdata
= response2
;
1107 respsize
= sizeof(response2
);
1108 //DbpString("Reader requests anticollission CSN:");
1109 } else if(receivedCmd
[0] == 0x81) {
1110 // Reader selects anticollission CSN.
1111 // Tag sends the corresponding real CSN
1112 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1113 respdata
= response3
;
1114 respsize
= sizeof(response3
);
1115 //DbpString("Reader selects anticollission CSN:");
1116 } else if(receivedCmd
[0] == 0x88) {
1117 // Read e-purse (88 02)
1118 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1119 respdata
= response4
;
1120 respsize
= sizeof(response4
);
1122 } else if(receivedCmd
[0] == 0x05) {
1123 // Reader random and reader MAC!!!
1124 // Lets store this ;-)
1126 Dbprintf(" CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1127 response3[0], response3[1], response3[2],
1128 response3[3], response3[4], response3[5],
1129 response3[6], response3[7]);
1131 Dbprintf("READER AUTH (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1133 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1134 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1135 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1138 // We do not know what to answer, so lets keep quit
1139 resp
= resp1
; respLen
= 0; //order = 5;
1142 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1143 // Reader ends the session
1144 resp
= resp1
; respLen
= 0; //order = 0;
1148 // Never seen this command before
1149 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1151 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1152 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1153 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1155 resp
= resp1
; respLen
= 0; //order = 0;
1160 if(cmdsRecvd
> 999) {
1161 DbpString("1000 commands later...");
1169 SendIClassAnswer(resp
, respLen
, 21);
1173 LogTrace(receivedCmd
,len
, rsamples
, Uart
.parityBits
, TRUE
);
1174 if (respdata
!= NULL
) {
1175 LogTrace(respdata
,respsize
, rsamples
, SwapBits(GetParity(respdata
,respsize
),respsize
), FALSE
);
1177 if(traceLen
> TRACE_SIZE
) {
1178 DbpString("Trace full");
1183 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1186 Dbprintf("%x", cmdsRecvd
);
1191 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1193 int i
= 0, u
= 0, d
= 0;
1196 // Modulate Manchester
1197 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_MOD424);
1198 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1199 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1204 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1205 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1208 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1213 else if(i
>= respLen
) {
1219 if(u
> 1) { i
++; u
= 0; }
1221 AT91C_BASE_SSC
->SSC_THR
= b
;
1225 if(BUTTON_PRESS()) {
1235 //-----------------------------------------------------------------------------
1236 // Transmit the command (to the tag) that was placed in ToSend[].
1237 //-----------------------------------------------------------------------------
1238 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1242 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1243 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1250 for(c
= 0; c
< *wait
;) {
1251 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1252 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1255 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1256 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1263 bool firstpart
= TRUE
;
1266 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1268 // DOUBLE THE SAMPLES!
1270 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1273 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1276 if(sendbyte
== 0xff) {
1279 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1280 firstpart
= !firstpart
;
1286 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1287 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1292 if (samples
) *samples
= (c
+ *wait
) << 3;
1296 //-----------------------------------------------------------------------------
1297 // Prepare iClass reader command to send to FPGA
1298 //-----------------------------------------------------------------------------
1299 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1306 // Start of Communication: 1 out of 4
1307 ToSend
[++ToSendMax
] = 0xf0;
1308 ToSend
[++ToSendMax
] = 0x00;
1309 ToSend
[++ToSendMax
] = 0x0f;
1310 ToSend
[++ToSendMax
] = 0x00;
1312 // Modulate the bytes
1313 for (i
= 0; i
< len
; i
++) {
1315 for(j
= 0; j
< 4; j
++) {
1316 for(k
= 0; k
< 4; k
++) {
1318 ToSend
[++ToSendMax
] = 0x0f;
1321 ToSend
[++ToSendMax
] = 0x00;
1328 // End of Communication
1329 ToSend
[++ToSendMax
] = 0x00;
1330 ToSend
[++ToSendMax
] = 0x00;
1331 ToSend
[++ToSendMax
] = 0xf0;
1332 ToSend
[++ToSendMax
] = 0x00;
1334 // Convert from last character reference to length
1338 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1344 // This is tied to other size changes
1345 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1346 CodeIClassCommand(frame
,len
);
1349 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1353 // Store reader command in buffer
1354 if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
);
1357 //-----------------------------------------------------------------------------
1358 // Wait a certain time for tag response
1359 // If a response is captured return TRUE
1360 // If it takes too long return FALSE
1361 //-----------------------------------------------------------------------------
1362 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1364 // buffer needs to be 512 bytes
1367 // Set FPGA mode to "reader listen mode", no modulation (listen
1368 // only, since we are receiving, not transmitting).
1369 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1371 // Now get the answer from the card
1372 Demod
.output
= receivedResponse
;
1374 Demod
.state
= DEMOD_UNSYNCD
;
1377 if (elapsed
) *elapsed
= 0;
1385 if(BUTTON_PRESS()) return FALSE
;
1387 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1388 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1389 if (elapsed
) (*elapsed
)++;
1391 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1392 if(c
< timeout
) { c
++; } else { return FALSE
; }
1393 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1396 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1397 *samples = ((c - 1) << 3) + 4;
1400 if(ManchesterDecoding(b
& 0x0f)) {
1408 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1411 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1412 rsamples
+= samples
;
1413 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
);
1414 if(samples
== 0) return FALSE
;
1418 // Reader iClass Anticollission
1419 void ReaderIClass(uint8_t arg0
) {
1420 uint8_t act_all
[] = { 0x0a };
1421 uint8_t identify
[] = { 0x0c };
1422 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1424 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1426 // Reset trace buffer
1427 memset(trace
, 0x44, RECV_CMD_OFFSET
);
1432 // Start from off (no field generated)
1433 // Signal field is off with the appropriate LED
1435 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1438 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1440 // Now give it time to spin up.
1441 // Signal field is on with the appropriate LED
1442 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1449 if(traceLen
> TRACE_SIZE
) {
1450 DbpString("Trace full");
1454 if (BUTTON_PRESS()) break;
1457 ReaderTransmitIClass(act_all
, 1);
1459 if(ReaderReceiveIClass(resp
)) {
1460 ReaderTransmitIClass(identify
, 1);
1461 if(ReaderReceiveIClass(resp
) == 10) {
1463 memcpy(&select
[1],resp
,8);
1464 ReaderTransmitIClass(select
, sizeof(select
));
1466 if(ReaderReceiveIClass(resp
) == 10) {
1467 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1468 resp
[0], resp
[1], resp
[2],
1469 resp
[3], resp
[4], resp
[5],
1472 // Card selected, whats next... ;-)
1481 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1482 uint8_t act_all
[] = { 0x0a };
1483 uint8_t identify
[] = { 0x0c };
1484 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1485 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1486 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1487 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1491 bool read_success
=false;
1494 static struct memory_t
{
1502 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1504 // Reset trace buffer
1505 memset(trace
, 0x44, RECV_CMD_OFFSET
);
1510 // Start from off (no field generated)
1511 // Signal field is off with the appropriate LED
1513 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1516 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1518 // Now give it time to spin up.
1519 // Signal field is on with the appropriate LED
1520 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1525 for(int i
=0;i
<1;i
++) {
1527 if(traceLen
> TRACE_SIZE
) {
1528 DbpString("Trace full");
1532 if (BUTTON_PRESS()) break;
1535 ReaderTransmitIClass(act_all
, 1);
1537 if(ReaderReceiveIClass(resp
)) {
1538 ReaderTransmitIClass(identify
, 1);
1539 if(ReaderReceiveIClass(resp
) == 10) {
1541 memcpy(&select
[1],resp
,8);
1542 ReaderTransmitIClass(select
, sizeof(select
));
1544 if(ReaderReceiveIClass(resp
) == 10) {
1545 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1546 resp
[0], resp
[1], resp
[2],
1547 resp
[3], resp
[4], resp
[5],
1551 Dbprintf("Readcheck on Sector 2");
1552 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1553 if(ReaderReceiveIClass(resp
) == 8) {
1554 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1555 resp
[0], resp
[1], resp
[2],
1556 resp
[3], resp
[4], resp
[5],
1559 Dbprintf("Authenticate");
1560 //for now replay captured auth (as cc not updated)
1561 memcpy(check
+5,MAC
,4);
1562 Dbprintf(" AA: %02x %02x %02x %02x",
1563 check
[5], check
[6], check
[7],check
[8]);
1564 ReaderTransmitIClass(check
, sizeof(check
));
1565 if(ReaderReceiveIClass(resp
) == 4) {
1566 Dbprintf(" AR: %02x %02x %02x %02x",
1567 resp
[0], resp
[1], resp
[2],resp
[3]);
1569 Dbprintf("Error: Authentication Fail!");
1572 Dbprintf("Dump Contents");
1573 //first get configuration block
1576 uint8_t *blockno
=&read
[1];
1577 crc
= iclass_crc16((char *)blockno
,1);
1579 read
[3] = crc
& 0xff;
1580 while(!read_success
){
1581 ReaderTransmitIClass(read
, sizeof(read
));
1582 if(ReaderReceiveIClass(resp
) == 10) {
1585 memory
.k16
= (mem
& 0x80);
1586 memory
.book
= (mem
& 0x20);
1587 memory
.k2
= (mem
& 0x8);
1588 memory
.lockauth
= (mem
& 0x2);
1589 memory
.keyaccess
= (mem
& 0x1);
1596 //then loop around remaining blocks
1597 for(uint8_t j
=0; j
<cardsize
; j
++){
1599 uint8_t *blockno
=&j
;
1602 crc
= iclass_crc16((char *)blockno
,1);
1604 read
[3] = crc
& 0xff;
1605 while(!read_success
){
1606 ReaderTransmitIClass(read
, sizeof(read
));
1607 if(ReaderReceiveIClass(resp
) == 10) {
1609 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1610 j
, resp
[0], resp
[1], resp
[2],
1611 resp
[3], resp
[4], resp
[5],
1624 //1. Create Method to Read sectors/blocks 0,1,2 and Send to client
1625 void IClass_iso14443A_GetPublic(uint8_t arg0
) {
1626 uint8_t act_all
[] = { 0x0a };
1627 uint8_t identify
[] = { 0x0c };
1628 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1629 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1630 //uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1631 uint8_t card_data
[24]={0};
1633 //bool read_success=false;
1634 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1636 // Reset trace buffer
1637 memset(trace
, 0x44, RECV_CMD_OFFSET
);
1642 // Start from off (no field generated)
1643 // Signal field is off with the appropriate LED
1645 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1648 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1650 // Now give it time to spin up.
1651 // Signal field is on with the appropriate LED
1652 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1657 for(int i
=0;i
<1;i
++) {
1659 if(traceLen
> TRACE_SIZE
) {
1660 DbpString("Trace full");
1664 if (BUTTON_PRESS()) break;
1667 ReaderTransmitIClass(act_all
, 1);
1669 if(ReaderReceiveIClass(resp
)) {
1670 ReaderTransmitIClass(identify
, 1);
1671 if(ReaderReceiveIClass(resp
) == 10) {
1673 memcpy(&select
[1],resp
,8);
1674 ReaderTransmitIClass(select
, sizeof(select
));
1676 if(ReaderReceiveIClass(resp
) == 10) {
1677 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1678 resp
[0], resp
[1], resp
[2],
1679 resp
[3], resp
[4], resp
[5],
1682 memcpy(card_data
,resp
,8);
1684 Dbprintf("Readcheck on Sector 2");
1685 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1686 if(ReaderReceiveIClass(resp
) == 8) {
1687 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1688 resp
[0], resp
[1], resp
[2],
1689 resp
[3], resp
[4], resp
[5],
1692 memcpy(card_data
+8,resp
,8);
1693 //prep to read config block
1694 /* read card configuration block
1695 while(!read_success){
1696 uint8_t sector_config=0x01;
1697 memcpy(read+1,§or_config,1);
1698 ReaderTransmitIClass(read, sizeof(read));
1699 if(ReaderReceiveIClass(resp) == 8) {
1700 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1701 resp[0], resp[1], resp[2],
1702 resp[3], resp[4], resp[5],
1705 memcpy(card_data+16,resp,8);
1712 //Dbprintf("DEBUG: %02x%02x%02x%02x%02x%02x%02x%02x",card_data[0],card_data[1],card_data[2],card_data[3],card_data[4],card_data[5],card_data[6],card_data[7]);
1713 //Dbprintf("DEBUG: %02x%02x%02x%02x%02x%02x%02x%02x",card_data[8],card_data[9],card_data[10],card_data[11],card_data[12],card_data[13],card_data[14],card_data[15]);
1716 //send data back to the client
1717 cmd_send(CMD_ACK
,0,0,0,card_data
,16);
1721 //TODO: Create Write method