1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, split Nov 2006
3 // Modified by Greg Jones, Jan 2009
4 // Modified by Adrian Dabrowski "atrox", Mar-Sept 2010,Oct 2011
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // Routines to support ISO 15693. This includes both the reader software and
11 // the `fake tag' modes, but at the moment I've implemented only the reader
12 // stuff, and that barely.
13 // Modified to perform modulation onboard in arm rather than on PC
14 // Also added additional reader commands (SELECT, READ etc.)
15 //-----------------------------------------------------------------------------
16 // The ISO 15693 describes two transmission modes from reader to tag, and 4
17 // transmission modes from tag to reader. As of Mar 2010 this code only
18 // supports one of each: "1of4" mode from reader to tag, and the highspeed
19 // variant with one subcarrier from card to reader.
20 // As long, as the card fully support ISO 15693 this is no problem, since the
21 // reader chooses both data rates, but some non-standard tags do not. Further for
22 // the simulation to work, we will need to support all data rates.
24 // VCD (reader) -> VICC (tag)
26 // data rate: 1,66 kbit/s (fc/8192)
27 // used for long range
29 // data rate: 26,48 kbit/s (fc/512)
30 // used for short range, high speed
32 // VICC (tag) -> VCD (reader)
34 // ASK / one subcarrier (423,75 khz)
35 // FSK / two subcarriers (423,75 khz && 484,28 khz)
36 // Data Rates / Modes:
37 // low ASK: 6,62 kbit/s
38 // low FSK: 6.67 kbit/s
39 // high ASK: 26,48 kbit/s
40 // high FSK: 26,69 kbit/s
41 //-----------------------------------------------------------------------------
42 // added "1 out of 256" mode (for VCD->PICC) - atrox 20100911
46 // *) UID is always used "transmission order" (LSB), which is reverse to display order
48 // TODO / BUGS / ISSUES:
49 // *) writing to tags takes longer: we miss the answer from the tag in most cases
50 // -> tweak the read-timeout times
51 // *) signal decoding from the card is still a bit shaky.
52 // *) signal decoding is unable to detect collissions.
53 // *) add anti-collission support for inventory-commands
54 // *) read security status of a block
55 // *) sniffing and simulation do only support one transmission mode. need to support
56 // all 8 transmission combinations
57 // *) remove or refactor code under "depricated"
58 // *) document all the functions
61 #include "proxmark3.h"
65 #include "iso15693tools.h"
68 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
70 ///////////////////////////////////////////////////////////////////////
71 // ISO 15693 Part 2 - Air Interface
72 // This section basicly contains transmission and receiving of bits
73 ///////////////////////////////////////////////////////////////////////
75 #define FrameSOF Iso15693FrameSOF
76 #define Logic0 Iso15693Logic0
77 #define Logic1 Iso15693Logic1
78 #define FrameEOF Iso15693FrameEOF
80 #define Crc(data,datalen) Iso15693Crc(data,datalen)
81 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
82 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
84 // approximate amplitude=sqrt(ci^2+cq^2)
85 #define AMPLITUDE(ci, cq) (MAX(ABS(ci), ABS(cq)) + (MIN(ABS(ci), ABS(cq))>>1))
90 // ---------------------------
92 // ---------------------------
94 // prepare data using "1 out of 4" code for later transmission
95 // resulting data rate is 26,48 kbit/s (fc/512)
97 // n ... length of data
98 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
104 // Give it a bit of slack at the beginning
105 for(i
= 0; i
< 24; i
++) {
118 for(i
= 0; i
< n
; i
++) {
119 for(j
= 0; j
< 8; j
+= 2) {
120 int these
= (cmd
[i
] >> j
) & 3;
171 // And slack at the end, too.
172 for(i
= 0; i
< 24; i
++) {
177 // encode data using "1 out of 256" sheme
178 // data rate is 1,66 kbit/s (fc/8192)
179 // is designed for more robust communication over longer distances
180 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
186 // Give it a bit of slack at the beginning
187 for(i
= 0; i
< 24; i
++) {
201 for(i
= 0; i
< n
; i
++) {
202 for (j
= 0; j
<=255; j
++) {
218 // And slack at the end, too.
219 for(i
= 0; i
< 24; i
++) {
225 // Transmit the command (to the tag) that was placed in ToSend[].
226 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
230 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
231 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
232 if(*wait
< 10) { *wait
= 10; }
236 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
237 AT91C_BASE_SSC
->SSC_THR
= ~cmd
[c
];
243 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
244 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
249 *samples
= (c
+ *wait
) << 3;
252 //-----------------------------------------------------------------------------
253 // Transmit the command (to the reader) that was placed in ToSend[].
254 //-----------------------------------------------------------------------------
255 static void TransmitTo15693Reader(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
258 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
259 if(*wait
< 10) { *wait
= 10; }
262 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
263 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
269 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
270 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
275 *samples
= (c
+ *wait
) << 3;
286 // number of decoded bytes
287 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
290 uint8_t *dest
= BigBuf_get_addr();
293 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
294 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
297 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
298 uint16_t iq
= AT91C_BASE_SSC
->SSC_RHR
;
299 // The samples are correlations against I and Q versions of the
300 // tone that the tag AM-modulates. We just want power.
303 uint8_t r
= AMPLITUDE(i
, q
);
313 //////////////////////////////////////////
314 /////////// DEMODULATE ///////////////////
315 //////////////////////////////////////////
318 int max
= 0, maxPos
=0;
322 // First, correlate for SOF
323 for(i
= 0; i
< 200; i
++) { // usually, SOF is found around i = 60
325 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
326 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
333 if (DEBUG
) Dbprintf("SOF at %d, correlation %d", maxPos
, max
/(arraylen(FrameSOF
)/skip
));
335 int k
= 0; // this will be our return value
337 // greg - If correlation is less than 1 then there's little point in continuing
338 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)
341 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
344 memset(outBuf
, 0, sizeof(outBuf
));
347 int corr0
= 0, corr00
= 0, corr01
= 0, corr1
= 0, corrEOF
= 0;
348 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
349 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
351 corr01
= corr00
= corr0
;
352 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
353 corr00
+= Logic0
[j
]*dest
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
354 corr01
+= Logic1
[j
]*dest
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
356 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
357 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
359 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
360 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
362 // Even things out by the length of the target waveform.
368 if(corrEOF
> corr1
&& corrEOF
> corr00
&& corrEOF
> corr01
) {
369 if (DEBUG
) Dbprintf("EOF at %d, correlation %d (corr01: %d, corr00: %d, corr1: %d, corr0: %d)",
370 i
, corrEOF
, corr01
, corr00
, corr1
, corr0
);
372 } else if(corr1
> corr0
) {
373 i
+= arraylen(Logic1
)/skip
;
376 i
+= arraylen(Logic0
)/skip
;
383 if((i
+(int)arraylen(FrameEOF
)/skip
) >= 4000) {
384 DbpString("ran off end!");
388 if(mask
!= 0x01) { // this happens, when we miss the EOF
389 // TODO: for some reason this happens quite often
390 if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
);
391 if (mask
<0x08) k
--; // discard the last uneven octet;
392 // 0x08 is an assumption - but works quite often
396 // strncat(str1," octets read",8);
398 // DbpString( str1); // DbpString("%d octets", k);
400 // for(i = 0; i < k; i+=3) {
401 // //DbpString("# %2d: %02x ", i, outBuf[i]);
402 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
405 for(i
= 0; i
< k
; i
++) {
406 receivedResponse
[i
] = outBuf
[i
];
408 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
409 return k
; // return the number of bytes demodulated
411 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
416 // Now the GetISO15693 message from sniffing command
417 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
420 uint8_t *dest
= BigBuf_get_addr();
423 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
424 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
427 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
428 uint16_t iq
= AT91C_BASE_SSC
->SSC_RHR
;
429 // The samples are correlations against I and Q versions of the
430 // tone that the tag AM-modulates. We just want power,
431 // so abs(I) + abs(Q) is close to what we want.
434 uint8_t r
= AMPLITUDE(i
, q
);
438 if(c
>= BIGBUF_SIZE
) {
444 //////////////////////////////////////////
445 /////////// DEMODULATE ///////////////////
446 //////////////////////////////////////////
449 int max
= 0, maxPos
=0;
453 // First, correlate for SOF
454 for(i
= 0; i
< 38000; i
++) {
456 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
457 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
464 if (DEBUG
) Dbprintf("SOF at %d, correlation %d", maxPos
,max
/(arraylen(FrameSOF
)/skip
));
466 int k
= 0; // this will be our return value
468 // greg - If correlation is less than 1 then there's little point in continuing
469 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) // THIS SHOULD BE 1
472 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
475 memset(outBuf
, 0, sizeof(outBuf
));
478 int corr0
= 0, corr00
= 0, corr01
= 0, corr1
= 0, corrEOF
= 0;
479 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
480 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
482 corr01
= corr00
= corr0
;
483 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
484 corr00
+= Logic0
[j
]*dest
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
485 corr01
+= Logic1
[j
]*dest
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
487 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
488 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
490 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
491 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
493 // Even things out by the length of the target waveform.
499 if(corrEOF
> corr1
&& corrEOF
> corr00
&& corrEOF
> corr01
) {
500 if (DEBUG
) Dbprintf("EOF at %d, correlation %d (corr01: %d, corr00: %d, corr1: %d, corr0: %d)",
501 i
, corrEOF
, corr01
, corr00
, corr1
, corr0
);
503 } else if(corr1
> corr0
) {
504 i
+= arraylen(Logic1
)/skip
;
507 i
+= arraylen(Logic0
)/skip
;
514 if((i
+(int)arraylen(FrameEOF
)/skip
) >= BIGBUF_SIZE
) {
515 DbpString("ran off end!");
520 DbpString("sniff: error, uneven octet! (discard extra bits!)");
521 /// DbpString(" mask=%02x", mask);
525 // strncat(str1," octets read",8);
527 // DbpString( str1); // DbpString("%d octets", k);
529 // for(i = 0; i < k; i+=3) {
530 // //DbpString("# %2d: %02x ", i, outBuf[i]);
531 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
534 for(i
= 0; i
< k
; i
++) {
535 receivedResponse
[i
] = outBuf
[i
];
537 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
538 return k
; // return the number of bytes demodulated
540 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
544 static void BuildIdentifyRequest(void);
545 //-----------------------------------------------------------------------------
546 // Start to read an ISO 15693 tag. We send an identify request, then wait
547 // for the response. The response is not demodulated, just left in the buffer
548 // so that it can be downloaded to a PC and processed there.
549 //-----------------------------------------------------------------------------
550 void AcquireRawAdcSamplesIso15693(void)
552 uint8_t *dest
= BigBuf_get_addr();
556 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
557 BuildIdentifyRequest();
559 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
561 // Give the tags time to energize
562 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
565 // Now send the command
566 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
567 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
571 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
572 AT91C_BASE_SSC
->SSC_THR
= ~ToSend
[c
];
574 if(c
== ToSendMax
+3) {
581 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
582 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
586 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
587 uint16_t iq
= AT91C_BASE_SSC
->SSC_RHR
;
588 // The samples are correlations against I and Q versions of the
589 // tone that the tag AM-modulates. We just want power,
590 // so abs(I) + abs(Q) is close to what we want.
593 uint8_t r
= AMPLITUDE(i
, q
);
605 void RecordRawAdcSamplesIso15693(void)
607 uint8_t *dest
= BigBuf_get_addr();
611 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
613 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
615 // Start from off (no field generated)
616 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
619 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
623 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
627 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
628 uint16_t iq
= AT91C_BASE_SSC
->SSC_RHR
;
629 // The samples are correlations against I and Q versions of the
630 // tone that the tag AM-modulates. We just want power,
631 // so abs(I) + abs(Q) is close to what we want.
634 uint8_t r
= AMPLITUDE(i
, q
);
643 Dbprintf("finished recording");
647 // Initialize the proxmark as iso15k reader
648 // (this might produces glitches that confuse some tags
649 void Iso15693InitReader() {
655 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
659 // Start from off (no field generated)
660 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
663 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
664 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
666 // Give the tags time to energize
667 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
676 ///////////////////////////////////////////////////////////////////////
677 // ISO 15693 Part 3 - Air Interface
678 // This section basicly contains transmission and receiving of bits
679 ///////////////////////////////////////////////////////////////////////
681 // Encode (into the ToSend buffers) an identify request, which is the first
682 // thing that you must send to a tag to get a response.
683 static void BuildIdentifyRequest(void)
688 // one sub-carrier, inventory, 1 slot, fast rate
689 // AFI is at bit 5 (1<<4) when doing an INVENTORY
690 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
691 // inventory command code
700 CodeIso15693AsReader(cmd
, sizeof(cmd
));
703 // uid is in transmission order (which is reverse of display order)
704 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
709 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
710 // followed by teh block data
711 // one sub-carrier, inventory, 1 slot, fast rate
712 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
713 // READ BLOCK command code
715 // UID may be optionally specified here
724 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
725 // Block number to read
726 cmd
[10] = blockNumber
;//0x00;
728 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
729 cmd
[11] = crc
& 0xff;
732 CodeIso15693AsReader(cmd
, sizeof(cmd
));
735 // Now the VICC>VCD responses when we are simulating a tag
736 static void BuildInventoryResponse( uint8_t *uid
)
741 // one sub-carrier, inventory, 1 slot, fast rate
742 // AFI is at bit 5 (1<<4) when doing an INVENTORY
743 //(1 << 2) | (1 << 5) | (1 << 1);
745 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
747 cmd
[2] = uid
[7]; //0x32;
748 cmd
[3] = uid
[6]; //0x4b;
749 cmd
[4] = uid
[5]; //0x03;
750 cmd
[5] = uid
[4]; //0x01;
751 cmd
[6] = uid
[3]; //0x00;
752 cmd
[7] = uid
[2]; //0x10;
753 cmd
[8] = uid
[1]; //0x05;
754 cmd
[9] = uid
[0]; //0xe0;
757 cmd
[10] = crc
& 0xff;
760 CodeIso15693AsReader(cmd
, sizeof(cmd
));
763 // Universal Method for sending to and recv bytes from a tag
764 // init ... should we initialize the reader?
765 // speed ... 0 low speed, 1 hi speed
766 // **recv will return you a pointer to the received data
767 // If you do not need the answer use NULL for *recv[]
768 // return: lenght of received data
769 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
781 if (init
) Iso15693InitReader();
784 uint8_t *answer
= BigBuf_get_addr() + 4000;
785 if (recv
!= NULL
) memset(answer
, 0, 100);
788 // low speed (1 out of 256)
789 CodeIso15693AsReader256(send
, sendlen
);
791 // high speed (1 out of 4)
792 CodeIso15693AsReader(send
, sendlen
);
798 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
799 // Now wait for a response
803 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
816 // --------------------------------------------------------------------
818 // --------------------------------------------------------------------
820 // Decodes a message from a tag and displays its metadata and content
821 #define DBD15STATLEN 48
822 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
823 char status
[DBD15STATLEN
+1]={0};
828 strncat(status
,"ProtExt ",DBD15STATLEN
);
831 strncat(status
,"Error ",DBD15STATLEN
);
834 strncat(status
,"01:notSupp",DBD15STATLEN
);
837 strncat(status
,"02:notRecog",DBD15STATLEN
);
840 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
843 strncat(status
,"0f:noInfo",DBD15STATLEN
);
846 strncat(status
,"10:dontExist",DBD15STATLEN
);
849 strncat(status
,"11:lockAgain",DBD15STATLEN
);
852 strncat(status
,"12:locked",DBD15STATLEN
);
855 strncat(status
,"13:progErr",DBD15STATLEN
);
858 strncat(status
,"14:lockErr",DBD15STATLEN
);
861 strncat(status
,"unknownErr",DBD15STATLEN
);
863 strncat(status
," ",DBD15STATLEN
);
865 strncat(status
,"NoErr ",DBD15STATLEN
);
869 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
870 strncat(status
,"CrcOK",DBD15STATLEN
);
872 strncat(status
,"CrcFail!",DBD15STATLEN
);
874 Dbprintf("%s",status
);
880 ///////////////////////////////////////////////////////////////////////
881 // Functions called via USB/Client
882 ///////////////////////////////////////////////////////////////////////
884 void SetDebugIso15693(uint32_t debug
) {
886 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
892 //-----------------------------------------------------------------------------
893 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
894 // all demodulation performed in arm rather than host. - greg
895 //-----------------------------------------------------------------------------
896 void ReaderIso15693(uint32_t parameter
)
905 // int answerLen3 = 0;
911 uint8_t TagUID
[8] = {0x00};
913 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
915 uint8_t *answer1
= BigBuf_get_addr() + 4000;
916 uint8_t *answer2
= BigBuf_get_addr() + 4100;
917 // uint8_t *answer3 = BigBuf_get_addr() + 4200;
919 memset(answer1
, 0x00, 200);
921 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
923 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
925 // Start from off (no field generated)
926 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
929 // Give the tags time to energize
930 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
938 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
939 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
941 // Now send the IDENTIFY command
942 BuildIdentifyRequest();
944 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
946 // Now wait for a response
947 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
949 if (answerLen1
>=12) // we should do a better check than this
951 TagUID
[0] = answer1
[2];
952 TagUID
[1] = answer1
[3];
953 TagUID
[2] = answer1
[4];
954 TagUID
[3] = answer1
[5];
955 TagUID
[4] = answer1
[6];
956 TagUID
[5] = answer1
[7];
957 TagUID
[6] = answer1
[8]; // IC Manufacturer code
958 TagUID
[7] = answer1
[9]; // always E0
962 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
963 DbdecodeIso15693Answer(answerLen1
,answer1
);
964 Dbhexdump(answerLen1
,answer1
,true);
968 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
969 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
970 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
973 // Dbprintf("%d octets read from SELECT request:", answerLen2);
974 // DbdecodeIso15693Answer(answerLen2,answer2);
975 // Dbhexdump(answerLen2,answer2,true);
977 // Dbprintf("%d octets read from XXX request:", answerLen3);
978 // DbdecodeIso15693Answer(answerLen3,answer3);
979 // Dbhexdump(answerLen3,answer3,true);
982 if (answerLen1
>=12 && DEBUG
) {
984 while (i
<32) { // sanity check, assume max 32 pages
985 BuildReadBlockRequest(TagUID
,i
);
986 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
987 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
989 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
990 DbdecodeIso15693Answer(answerLen2
,answer2
);
991 Dbhexdump(answerLen2
,answer2
,true);
992 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1004 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1005 // all demodulation performed in arm rather than host. - greg
1006 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
)
1019 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1021 uint8_t *buf
= BigBuf_get_addr() + 4000;
1022 memset(buf
, 0x00, 100);
1024 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1025 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1027 // Start from off (no field generated)
1028 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1037 answerLen1
= GetIso15693AnswerFromSniff(buf
, 100, &samples
, &elapsed
) ;
1039 if (answerLen1
>=1) // we should do a better check than this
1041 // Build a suitable reponse to the reader INVENTORY cocmmand
1042 // not so obsvious, but in the call to BuildInventoryResponse, the command is copied to the global ToSend buffer used below.
1044 BuildInventoryResponse(uid
);
1046 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
);
1049 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1050 buf
[0], buf
[1], buf
[2], buf
[3],
1051 buf
[4], buf
[5], buf
[6], buf
[7], buf
[8]);
1053 Dbprintf("Simulationg uid: %x %x %x %x %x %x %x %x",
1054 uid
[0], uid
[1], uid
[2], uid
[3],
1055 uid
[4], uid
[5], uid
[6], uid
[7]);
1064 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1065 // (some manufactures offer a way to read the AFI, though)
1066 void BruteforceIso15693Afi(uint32_t speed
)
1070 int datalen
=0, recvlen
=0;
1072 Iso15693InitReader();
1074 // first without AFI
1075 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1077 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1078 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1079 data
[1]=ISO15_CMD_INVENTORY
;
1080 data
[2]=0; // mask length
1081 datalen
=AddCrc(data
,3);
1082 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1085 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1090 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1091 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1092 data
[1]=ISO15_CMD_INVENTORY
;
1094 data
[3]=0; // mask length
1096 for (int i
=0;i
<256;i
++) {
1098 datalen
=AddCrc(data
,4);
1099 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1102 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1105 Dbprintf("AFI Bruteforcing done.");
1109 // Allows to directly send commands to the tag via the client
1110 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1113 uint8_t *recvbuf
= BigBuf_get_addr();
1118 Dbhexdump(datalen
,data
,true);
1121 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1125 cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48);
1130 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1131 Dbhexdump(recvlen
,recvbuf
,true);
1140 // --------------------------------------------------------------------
1141 // -- Misc & deprecated functions
1142 // --------------------------------------------------------------------
1146 // do not use; has a fix UID
1147 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1152 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1153 // followed by teh block data
1154 // one sub-carrier, inventory, 1 slot, fast rate
1155 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1156 // System Information command code
1158 // UID may be optionally specified here
1167 cmd[9]= 0xe0; // always e0 (not exactly unique)
1169 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1170 cmd[10] = crc & 0xff;
1173 CodeIso15693AsReader(cmd, sizeof(cmd));
1177 // do not use; has a fix UID
1178 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1183 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1184 // followed by teh block data
1185 // one sub-carrier, inventory, 1 slot, fast rate
1186 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1187 // READ Multi BLOCK command code
1189 // UID may be optionally specified here
1198 cmd[9]= 0xe0; // always e0 (not exactly unique)
1199 // First Block number to read
1201 // Number of Blocks to read
1202 cmd[11] = 0x2f; // read quite a few
1204 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1205 cmd[12] = crc & 0xff;
1208 CodeIso15693AsReader(cmd, sizeof(cmd));
1211 // do not use; has a fix UID
1212 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1217 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1218 // followed by teh block data
1219 // one sub-carrier, inventory, 1 slot, fast rate
1220 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1221 // READ BLOCK command code
1223 // UID may be optionally specified here
1232 cmd[9]= 0xe0; // always e0 (not exactly unique)
1238 // cmd[13] = 0x00; //Now the CRC
1239 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1240 cmd[12] = crc & 0xff;
1243 CodeIso15693AsReader(cmd, sizeof(cmd));
1246 // do not use; has a fix UID
1247 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1252 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1253 // followed by teh block data
1254 // one sub-carrier, inventory, 1 slot, fast rate
1255 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1256 // READ BLOCK command code
1258 // UID may be optionally specified here
1267 cmd[9]= 0xe0; // always e0 (not exactly unique)
1269 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1273 // cmd[13] = 0x00; //Now the CRC
1274 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1275 cmd[12] = crc & 0xff;
1278 CodeIso15693AsReader(cmd, sizeof(cmd));