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)
87 // ---------------------------
89 // ---------------------------
91 // prepare data using "1 out of 4" code for later transmission
92 // resulting data rate is 26,48 kbit/s (fc/512)
94 // n ... length of data
95 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
101 // Give it a bit of slack at the beginning
102 for(i
= 0; i
< 24; i
++) {
115 for(i
= 0; i
< n
; i
++) {
116 for(j
= 0; j
< 8; j
+= 2) {
117 int these
= (cmd
[i
] >> j
) & 3;
168 // And slack at the end, too.
169 for(i
= 0; i
< 24; i
++) {
174 // encode data using "1 out of 256" sheme
175 // data rate is 1,66 kbit/s (fc/8192)
176 // is designed for more robust communication over longer distances
177 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
183 // Give it a bit of slack at the beginning
184 for(i
= 0; i
< 24; i
++) {
198 for(i
= 0; i
< n
; i
++) {
199 for (j
= 0; j
<=255; j
++) {
215 // And slack at the end, too.
216 for(i
= 0; i
< 24; i
++) {
222 // Transmit the command (to the tag) that was placed in ToSend[].
223 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
227 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
228 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
229 if(*wait
< 10) { *wait
= 10; }
231 // for(c = 0; c < *wait;) {
232 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
233 // AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
236 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
237 // volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
245 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
246 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
252 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
253 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
258 *samples
= (c
+ *wait
) << 3;
261 //-----------------------------------------------------------------------------
262 // Transmit the command (to the reader) that was placed in ToSend[].
263 //-----------------------------------------------------------------------------
264 static void TransmitTo15693Reader(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
267 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
268 if(*wait
< 10) { *wait
= 10; }
271 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
272 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
278 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
279 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
284 *samples
= (c
+ *wait
) << 3;
295 // number of decoded bytes
296 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
298 uint8_t *dest
= BigBuf_get_addr();
304 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
305 SpinDelay(100); // greg - experiment to get rid of some of the 0 byte/failed reads
308 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
309 AT91C_BASE_SSC
->SSC_THR
= 0x43;
311 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
313 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
315 // The samples are correlations against I and Q versions of the
316 // tone that the tag AM-modulates, so every other sample is I,
317 // every other is Q. We just want power, so abs(I) + abs(Q) is
318 // close to what we want.
336 dest
[c
++] = (uint8_t)r
;
349 //////////////////////////////////////////
350 /////////// DEMODULATE ///////////////////
351 //////////////////////////////////////////
354 int max
= 0, maxPos
=0;
358 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
360 // First, correlate for SOF
361 for(i
= 0; i
< 100; i
++) {
363 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
364 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
371 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
373 int k
= 0; // this will be our return value
375 // greg - If correlation is less than 1 then there's little point in continuing
376 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)
379 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
382 memset(outBuf
, 0, sizeof(outBuf
));
385 int corr0
= 0, corr1
= 0, corrEOF
= 0;
386 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
387 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
389 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
390 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
392 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
393 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
395 // Even things out by the length of the target waveform.
399 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
400 // DbpString("EOF at %d", i);
402 } else if(corr1
> corr0
) {
403 i
+= arraylen(Logic1
)/skip
;
406 i
+= arraylen(Logic0
)/skip
;
413 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
414 DbpString("ran off end!");
418 if(mask
!= 0x01) { // this happens, when we miss the EOF
419 // TODO: for some reason this happens quite often
420 if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
);
421 if (mask
<0x08) k
--; // discard the last uneven octet;
422 // 0x08 is an assumption - but works quite often
426 // strncat(str1," octets read",8);
428 // DbpString( str1); // DbpString("%d octets", k);
430 // for(i = 0; i < k; i+=3) {
431 // //DbpString("# %2d: %02x ", i, outBuf[i]);
432 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
435 for(i
= 0; i
< k
; i
++) {
436 receivedResponse
[i
] = outBuf
[i
];
438 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
439 return k
; // return the number of bytes demodulated
441 // DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
445 // Now the GetISO15693 message from sniffing command
446 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
448 uint8_t *dest
= BigBuf_get_addr();
454 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
455 SpinDelay(100); // greg - experiment to get rid of some of the 0 byte/failed reads
458 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
459 AT91C_BASE_SSC
->SSC_THR
= 0x43;
461 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
462 int8_t b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
464 // The samples are correlations against I and Q versions of the
465 // tone that the tag AM-modulates, so every other sample is I,
466 // every other is Q. We just want power, so abs(I) + abs(Q) is
467 // close to what we want.
483 dest
[c
++] = (uint8_t)r
;
496 //////////////////////////////////////////
497 /////////// DEMODULATE ///////////////////
498 //////////////////////////////////////////
501 int max
= 0, maxPos
=0;
505 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
507 // First, correlate for SOF
508 for(i
= 0; i
< 19000; i
++) {
510 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
511 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
518 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
520 int k
= 0; // this will be our return value
522 // greg - If correlation is less than 1 then there's little point in continuing
523 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) // THIS SHOULD BE 1
526 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
529 memset(outBuf
, 0, sizeof(outBuf
));
532 int corr0
= 0, corr1
= 0, corrEOF
= 0;
533 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
534 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
536 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
537 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
539 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
540 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
542 // Even things out by the length of the target waveform.
546 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
547 // DbpString("EOF at %d", i);
549 } else if(corr1
> corr0
) {
550 i
+= arraylen(Logic1
)/skip
;
553 i
+= arraylen(Logic0
)/skip
;
560 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
561 DbpString("ran off end!");
566 DbpString("sniff: error, uneven octet! (discard extra bits!)");
567 /// DbpString(" mask=%02x", mask);
571 // strncat(str1," octets read",8);
573 // DbpString( str1); // DbpString("%d octets", k);
575 // for(i = 0; i < k; i+=3) {
576 // //DbpString("# %2d: %02x ", i, outBuf[i]);
577 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
580 for(i
= 0; i
< k
; i
++) {
581 receivedResponse
[i
] = outBuf
[i
];
583 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
584 return k
; // return the number of bytes demodulated
586 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
590 static void BuildIdentifyRequest(void);
591 //-----------------------------------------------------------------------------
592 // Start to read an ISO 15693 tag. We send an identify request, then wait
593 // for the response. The response is not demodulated, just left in the buffer
594 // so that it can be downloaded to a PC and processed there.
595 //-----------------------------------------------------------------------------
596 void AcquireRawAdcSamplesIso15693(void)
598 uint8_t *dest
= BigBuf_get_addr();
604 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
605 BuildIdentifyRequest();
607 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
609 // Give the tags time to energize
610 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
613 // Now send the command
615 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
619 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
620 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
622 if(c
== ToSendMax
+3) {
626 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
627 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
633 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
638 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
639 AT91C_BASE_SSC
->SSC_THR
= 0x43;
641 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
643 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
645 // The samples are correlations against I and Q versions of the
646 // tone that the tag AM-modulates, so every other sample is I,
647 // every other is Q. We just want power, so abs(I) + abs(Q) is
648 // close to what we want.
663 dest
[c
++] = (uint8_t)r
;
678 void RecordRawAdcSamplesIso15693(void)
680 uint8_t *dest
= BigBuf_get_addr();
686 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
690 // Start from off (no field generated)
691 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
694 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
698 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
703 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
704 AT91C_BASE_SSC
->SSC_THR
= 0x43;
706 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
708 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
710 // The samples are correlations against I and Q versions of the
711 // tone that the tag AM-modulates, so every other sample is I,
712 // every other is Q. We just want power, so abs(I) + abs(Q) is
713 // close to what we want.
728 dest
[c
++] = (uint8_t)r
;
741 Dbprintf("fin record");
745 // Initialize the proxmark as iso15k reader
746 // (this might produces glitches that confuse some tags
747 void Iso15693InitReader() {
753 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
757 // Start from off (no field generated)
758 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
761 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
764 // Give the tags time to energize
765 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
774 ///////////////////////////////////////////////////////////////////////
775 // ISO 15693 Part 3 - Air Interface
776 // This section basicly contains transmission and receiving of bits
777 ///////////////////////////////////////////////////////////////////////
779 // Encode (into the ToSend buffers) an identify request, which is the first
780 // thing that you must send to a tag to get a response.
781 static void BuildIdentifyRequest(void)
786 // one sub-carrier, inventory, 1 slot, fast rate
787 // AFI is at bit 5 (1<<4) when doing an INVENTORY
788 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
789 // inventory command code
798 CodeIso15693AsReader(cmd
, sizeof(cmd
));
801 // uid is in transmission order (which is reverse of display order)
802 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
807 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
808 // followed by teh block data
809 // one sub-carrier, inventory, 1 slot, fast rate
810 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
811 // READ BLOCK command code
813 // UID may be optionally specified here
822 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
823 // Block number to read
824 cmd
[10] = blockNumber
;//0x00;
826 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
827 cmd
[11] = crc
& 0xff;
830 CodeIso15693AsReader(cmd
, sizeof(cmd
));
833 // Now the VICC>VCD responses when we are simulating a tag
834 static void BuildInventoryResponse( uint8_t *uid
)
839 // one sub-carrier, inventory, 1 slot, fast rate
840 // AFI is at bit 5 (1<<4) when doing an INVENTORY
841 //(1 << 2) | (1 << 5) | (1 << 1);
843 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
845 cmd
[2] = uid
[7]; //0x32;
846 cmd
[3] = uid
[6]; //0x4b;
847 cmd
[4] = uid
[5]; //0x03;
848 cmd
[5] = uid
[4]; //0x01;
849 cmd
[6] = uid
[3]; //0x00;
850 cmd
[7] = uid
[2]; //0x10;
851 cmd
[8] = uid
[1]; //0x05;
852 cmd
[9] = uid
[0]; //0xe0;
855 cmd
[10] = crc
& 0xff;
858 CodeIso15693AsReader(cmd
, sizeof(cmd
));
861 // Universal Method for sending to and recv bytes from a tag
862 // init ... should we initialize the reader?
863 // speed ... 0 low speed, 1 hi speed
864 // **recv will return you a pointer to the received data
865 // If you do not need the answer use NULL for *recv[]
866 // return: lenght of received data
867 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
879 if (init
) Iso15693InitReader();
882 uint8_t *answer
= BigBuf_get_addr() + 3660;
883 if (recv
!= NULL
) memset(answer
, 0, 100);
886 // low speed (1 out of 256)
887 CodeIso15693AsReader256(send
, sendlen
);
889 // high speed (1 out of 4)
890 CodeIso15693AsReader(send
, sendlen
);
896 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
897 // Now wait for a response
901 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
911 // --------------------------------------------------------------------
913 // --------------------------------------------------------------------
915 // Decodes a message from a tag and displays its metadata and content
916 #define DBD15STATLEN 48
917 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
918 char status
[DBD15STATLEN
+1]={0};
923 strncat(status
,"ProtExt ",DBD15STATLEN
);
926 strncat(status
,"Error ",DBD15STATLEN
);
929 strncat(status
,"01:notSupp",DBD15STATLEN
);
932 strncat(status
,"02:notRecog",DBD15STATLEN
);
935 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
938 strncat(status
,"0f:noInfo",DBD15STATLEN
);
941 strncat(status
,"10:dontExist",DBD15STATLEN
);
944 strncat(status
,"11:lockAgain",DBD15STATLEN
);
947 strncat(status
,"12:locked",DBD15STATLEN
);
950 strncat(status
,"13:progErr",DBD15STATLEN
);
953 strncat(status
,"14:lockErr",DBD15STATLEN
);
956 strncat(status
,"unknownErr",DBD15STATLEN
);
958 strncat(status
," ",DBD15STATLEN
);
960 strncat(status
,"NoErr ",DBD15STATLEN
);
964 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
965 strncat(status
,"CrcOK",DBD15STATLEN
);
967 strncat(status
,"CrcFail!",DBD15STATLEN
);
969 Dbprintf("%s",status
);
975 ///////////////////////////////////////////////////////////////////////
976 // Functions called via USB/Client
977 ///////////////////////////////////////////////////////////////////////
979 void SetDebugIso15693(uint32_t debug
) {
981 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
987 //-----------------------------------------------------------------------------
988 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
989 // all demodulation performed in arm rather than host. - greg
990 //-----------------------------------------------------------------------------
991 void ReaderIso15693(uint32_t parameter
)
1006 uint8_t TagUID
[8] = {0x00};
1008 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1010 uint8_t *answer1
= BigBuf_get_addr() + 3660;
1011 uint8_t *answer2
= BigBuf_get_addr() + 3760;
1012 uint8_t *answer3
= BigBuf_get_addr() + 3860;
1014 memset(answer1
, 0x00, 300);
1016 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1020 // Start from off (no field generated)
1021 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1024 // Give the tags time to energize
1025 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1033 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1034 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1036 // Now send the IDENTIFY command
1037 BuildIdentifyRequest();
1039 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1041 // Now wait for a response
1042 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
1044 if (answerLen1
>=12) // we should do a better check than this
1046 TagUID
[0] = answer1
[2];
1047 TagUID
[1] = answer1
[3];
1048 TagUID
[2] = answer1
[4];
1049 TagUID
[3] = answer1
[5];
1050 TagUID
[4] = answer1
[6];
1051 TagUID
[5] = answer1
[7];
1052 TagUID
[6] = answer1
[8]; // IC Manufacturer code
1053 TagUID
[7] = answer1
[9]; // always E0
1057 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
1058 DbdecodeIso15693Answer(answerLen1
,answer1
);
1059 Dbhexdump(answerLen1
,answer1
,true);
1063 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1064 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
1065 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1068 Dbprintf("%d octets read from SELECT request:", answerLen2
);
1069 DbdecodeIso15693Answer(answerLen2
,answer2
);
1070 Dbhexdump(answerLen2
,answer2
,true);
1072 Dbprintf("%d octets read from XXX request:", answerLen3
);
1073 DbdecodeIso15693Answer(answerLen3
,answer3
);
1074 Dbhexdump(answerLen3
,answer3
,true);
1077 if (answerLen1
>=12 && DEBUG
) {
1079 while (i
<32) { // sanity check, assume max 32 pages
1080 BuildReadBlockRequest(TagUID
,i
);
1081 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1082 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
1084 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
1085 DbdecodeIso15693Answer(answerLen2
,answer2
);
1086 Dbhexdump(answerLen2
,answer2
,true);
1087 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1099 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1100 // all demodulation performed in arm rather than host. - greg
1101 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
)
1114 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1116 uint8_t *buf
= BigBuf_get_addr() + 3660;
1117 memset(buf
, 0x00, 100);
1119 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1122 // Start from off (no field generated)
1123 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1132 answerLen1
= GetIso15693AnswerFromSniff(buf
, 100, &samples
, &elapsed
) ;
1134 if (answerLen1
>=1) // we should do a better check than this
1136 // Build a suitable reponse to the reader INVENTORY cocmmand
1137 // not so obsvious, but in the call to BuildInventoryResponse, the command is copied to the global ToSend buffer used below.
1139 BuildInventoryResponse(uid
);
1141 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
);
1144 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1145 buf
[0], buf
[1], buf
[2], buf
[3],
1146 buf
[4], buf
[5], buf
[6], buf
[7], buf
[8]);
1148 Dbprintf("Simulationg uid: %x %x %x %x %x %x %x %x",
1149 uid
[0], uid
[1], uid
[2], uid
[3],
1150 uid
[4], uid
[5], uid
[6], uid
[7]);
1159 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1160 // (some manufactures offer a way to read the AFI, though)
1161 void BruteforceIso15693Afi(uint32_t speed
)
1165 int datalen
=0, recvlen
=0;
1167 Iso15693InitReader();
1169 // first without AFI
1170 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1172 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1173 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1174 data
[1]=ISO15_CMD_INVENTORY
;
1175 data
[2]=0; // mask length
1176 datalen
=AddCrc(data
,3);
1177 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1180 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1185 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1186 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1187 data
[1]=ISO15_CMD_INVENTORY
;
1189 data
[3]=0; // mask length
1191 for (int i
=0;i
<256;i
++) {
1193 datalen
=AddCrc(data
,4);
1194 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1197 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1200 Dbprintf("AFI Bruteforcing done.");
1204 // Allows to directly send commands to the tag via the client
1205 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1208 uint8_t *recvbuf
= BigBuf_get_addr();
1213 Dbhexdump(datalen
,data
,true);
1216 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1220 cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48);
1225 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1226 Dbhexdump(recvlen
,recvbuf
,true);
1235 // --------------------------------------------------------------------
1236 // -- Misc & deprecated functions
1237 // --------------------------------------------------------------------
1241 // do not use; has a fix UID
1242 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1247 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1248 // followed by teh block data
1249 // one sub-carrier, inventory, 1 slot, fast rate
1250 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1251 // System Information command code
1253 // UID may be optionally specified here
1262 cmd[9]= 0xe0; // always e0 (not exactly unique)
1264 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1265 cmd[10] = crc & 0xff;
1268 CodeIso15693AsReader(cmd, sizeof(cmd));
1272 // do not use; has a fix UID
1273 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1278 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1279 // followed by teh block data
1280 // one sub-carrier, inventory, 1 slot, fast rate
1281 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1282 // READ Multi BLOCK command code
1284 // UID may be optionally specified here
1293 cmd[9]= 0xe0; // always e0 (not exactly unique)
1294 // First Block number to read
1296 // Number of Blocks to read
1297 cmd[11] = 0x2f; // read quite a few
1299 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1300 cmd[12] = crc & 0xff;
1303 CodeIso15693AsReader(cmd, sizeof(cmd));
1306 // do not use; has a fix UID
1307 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1312 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1313 // followed by teh block data
1314 // one sub-carrier, inventory, 1 slot, fast rate
1315 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1316 // READ BLOCK command code
1318 // UID may be optionally specified here
1327 cmd[9]= 0xe0; // always e0 (not exactly unique)
1333 // cmd[13] = 0x00; //Now the CRC
1334 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1335 cmd[12] = crc & 0xff;
1338 CodeIso15693AsReader(cmd, sizeof(cmd));
1341 // do not use; has a fix UID
1342 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1347 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1348 // followed by teh block data
1349 // one sub-carrier, inventory, 1 slot, fast rate
1350 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1351 // READ BLOCK command code
1353 // UID may be optionally specified here
1362 cmd[9]= 0xe0; // always e0 (not exactly unique)
1364 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1368 // cmd[13] = 0x00; //Now the CRC
1369 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1370 cmd[12] = crc & 0xff;
1373 CodeIso15693AsReader(cmd, sizeof(cmd));