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
)
268 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
269 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
); // No requirement to energise my coils
270 if(*wait
< 10) { *wait
= 10; }
274 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
275 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
281 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
282 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
287 *samples
= (c
+ *wait
) << 3;
298 // number of decoded bytes
299 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
302 uint8_t *dest
= (uint8_t *)BigBuf
;
308 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
309 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
313 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
314 AT91C_BASE_SSC
->SSC_THR
= 0x43;
316 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
318 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
320 // The samples are correlations against I and Q versions of the
321 // tone that the tag AM-modulates, so every other sample is I,
322 // every other is Q. We just want power, so abs(I) + abs(Q) is
323 // close to what we want.
338 dest
[c
++] = (uint8_t)r
;
351 //////////////////////////////////////////
352 /////////// DEMODULATE ///////////////////
353 //////////////////////////////////////////
356 int max
= 0, maxPos
=0;
360 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
362 // First, correlate for SOF
363 for(i
= 0; i
< 100; i
++) {
365 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
366 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
373 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
375 int k
= 0; // this will be our return value
377 // greg - If correlation is less than 1 then there's little point in continuing
378 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)
381 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
384 memset(outBuf
, 0, sizeof(outBuf
));
387 int corr0
= 0, corr1
= 0, corrEOF
= 0;
388 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
389 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
391 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
392 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
394 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
395 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
397 // Even things out by the length of the target waveform.
401 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
402 // DbpString("EOF at %d", i);
404 } else if(corr1
> corr0
) {
405 i
+= arraylen(Logic1
)/skip
;
408 i
+= arraylen(Logic0
)/skip
;
415 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
416 DbpString("ran off end!");
420 if(mask
!= 0x01) { // this happens, when we miss the EOF
421 // TODO: for some reason this happens quite often
422 if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
);
423 if (mask
<0x08) k
--; // discard the last uneven octet;
424 // 0x08 is an assumption - but works quite often
428 // strncat(str1," octets read",8);
430 // DbpString( str1); // DbpString("%d octets", k);
432 // for(i = 0; i < k; i+=3) {
433 // //DbpString("# %2d: %02x ", i, outBuf[i]);
434 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
437 for(i
= 0; i
< k
; i
++) {
438 receivedResponse
[i
] = outBuf
[i
];
440 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
441 return k
; // return the number of bytes demodulated
443 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
448 // Now the GetISO15693 message from sniffing command
449 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
452 uint8_t *dest
= (uint8_t *)BigBuf
;
458 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
459 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
463 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
464 AT91C_BASE_SSC
->SSC_THR
= 0x43;
466 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
468 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
470 // The samples are correlations against I and Q versions of the
471 // tone that the tag AM-modulates, so every other sample is I,
472 // every other is Q. We just want power, so abs(I) + abs(Q) is
473 // close to what we want.
488 dest
[c
++] = (uint8_t)r
;
501 //////////////////////////////////////////
502 /////////// DEMODULATE ///////////////////
503 //////////////////////////////////////////
506 int max
= 0, maxPos
=0;
510 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
512 // First, correlate for SOF
513 for(i
= 0; i
< 19000; i
++) {
515 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
516 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
523 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
525 int k
= 0; // this will be our return value
527 // greg - If correlation is less than 1 then there's little point in continuing
528 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) // THIS SHOULD BE 1
531 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
534 memset(outBuf
, 0, sizeof(outBuf
));
537 int corr0
= 0, corr1
= 0, corrEOF
= 0;
538 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
539 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
541 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
542 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
544 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
545 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
547 // Even things out by the length of the target waveform.
551 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
552 // DbpString("EOF at %d", i);
554 } else if(corr1
> corr0
) {
555 i
+= arraylen(Logic1
)/skip
;
558 i
+= arraylen(Logic0
)/skip
;
565 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
566 DbpString("ran off end!");
571 DbpString("sniff: error, uneven octet! (discard extra bits!)");
572 /// DbpString(" mask=%02x", mask);
576 // strncat(str1," octets read",8);
578 // DbpString( str1); // DbpString("%d octets", k);
580 // for(i = 0; i < k; i+=3) {
581 // //DbpString("# %2d: %02x ", i, outBuf[i]);
582 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
585 for(i
= 0; i
< k
; i
++) {
586 receivedResponse
[i
] = outBuf
[i
];
588 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
589 return k
; // return the number of bytes demodulated
591 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
595 static void BuildIdentifyRequest(void);
596 //-----------------------------------------------------------------------------
597 // Start to read an ISO 15693 tag. We send an identify request, then wait
598 // for the response. The response is not demodulated, just left in the buffer
599 // so that it can be downloaded to a PC and processed there.
600 //-----------------------------------------------------------------------------
601 void AcquireRawAdcSamplesIso15693(void)
604 uint8_t *dest
= (uint8_t *)BigBuf
;
609 BuildIdentifyRequest();
611 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
613 // Give the tags time to energize
614 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
617 // Now send the command
619 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
623 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
624 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
626 if(c
== ToSendMax
+3) {
630 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
631 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
637 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
642 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
643 AT91C_BASE_SSC
->SSC_THR
= 0x43;
645 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
647 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
649 // The samples are correlations against I and Q versions of the
650 // tone that the tag AM-modulates, so every other sample is I,
651 // every other is Q. We just want power, so abs(I) + abs(Q) is
652 // close to what we want.
667 dest
[c
++] = (uint8_t)r
;
682 void RecordRawAdcSamplesIso15693(void)
685 uint8_t *dest
= (uint8_t *)BigBuf
;
693 // Start from off (no field generated)
694 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
697 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
701 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
706 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
707 AT91C_BASE_SSC
->SSC_THR
= 0x43;
709 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
711 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
713 // The samples are correlations against I and Q versions of the
714 // tone that the tag AM-modulates, so every other sample is I,
715 // every other is Q. We just want power, so abs(I) + abs(Q) is
716 // close to what we want.
731 dest
[c
++] = (uint8_t)r
;
744 Dbprintf("fin record");
748 // Initialize the proxmark as iso15k reader
749 // (this might produces glitches that confuse some tags
750 void Iso15693InitReader() {
759 // Start from off (no field generated)
760 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
763 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
766 // Give the tags time to energize
767 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
776 ///////////////////////////////////////////////////////////////////////
777 // ISO 15693 Part 3 - Air Interface
778 // This section basicly contains transmission and receiving of bits
779 ///////////////////////////////////////////////////////////////////////
781 // Encode (into the ToSend buffers) an identify request, which is the first
782 // thing that you must send to a tag to get a response.
783 static void BuildIdentifyRequest(void)
788 // one sub-carrier, inventory, 1 slot, fast rate
789 // AFI is at bit 5 (1<<4) when doing an INVENTORY
790 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
791 // inventory command code
800 CodeIso15693AsReader(cmd
, sizeof(cmd
));
803 // uid is in transmission order (which is reverse of display order)
804 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
809 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
810 // followed by teh block data
811 // one sub-carrier, inventory, 1 slot, fast rate
812 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
813 // READ BLOCK command code
815 // UID may be optionally specified here
824 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
825 // Block number to read
826 cmd
[10] = blockNumber
;//0x00;
828 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
829 cmd
[11] = crc
& 0xff;
832 CodeIso15693AsReader(cmd
, sizeof(cmd
));
835 // Now the VICC>VCD responses when we are simulating a tag
836 static void BuildInventoryResponse(void)
841 // one sub-carrier, inventory, 1 slot, fast rate
842 // AFI is at bit 5 (1<<4) when doing an INVENTORY
843 cmd
[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1);
856 cmd
[10] = crc
& 0xff;
859 CodeIso15693AsReader(cmd
, sizeof(cmd
));
862 // Universal Method for sending to and recv bytes from a tag
863 // init ... should we initialize the reader?
864 // speed ... 0 low speed, 1 hi speed
865 // **recv will return you a pointer to the received data
866 // If you do not need the answer use NULL for *recv[]
867 // return: lenght of received data
868 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
881 uint8_t *answer
= (((uint8_t *)BigBuf
) + 3660);
882 if (recv
!=NULL
) memset(BigBuf
+ 3660, 0, 100);
884 if (init
) Iso15693InitReader();
887 // low speed (1 out of 256)
888 CodeIso15693AsReader256(send
, sendlen
);
890 // high speed (1 out of 4)
891 CodeIso15693AsReader(send
, sendlen
);
897 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
898 // Now wait for a response
902 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
915 // --------------------------------------------------------------------
917 // --------------------------------------------------------------------
919 // Decodes a message from a tag and displays its metadata and content
920 #define DBD15STATLEN 48
921 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
922 char status
[DBD15STATLEN
+1]={0};
927 strncat(status
,"ProtExt ",DBD15STATLEN
);
930 strncat(status
,"Error ",DBD15STATLEN
);
933 strncat(status
,"01:notSupp",DBD15STATLEN
);
936 strncat(status
,"02:notRecog",DBD15STATLEN
);
939 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
942 strncat(status
,"0f:noInfo",DBD15STATLEN
);
945 strncat(status
,"10:dontExist",DBD15STATLEN
);
948 strncat(status
,"11:lockAgain",DBD15STATLEN
);
951 strncat(status
,"12:locked",DBD15STATLEN
);
954 strncat(status
,"13:progErr",DBD15STATLEN
);
957 strncat(status
,"14:lockErr",DBD15STATLEN
);
960 strncat(status
,"unknownErr",DBD15STATLEN
);
962 strncat(status
," ",DBD15STATLEN
);
964 strncat(status
,"NoErr ",DBD15STATLEN
);
968 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
969 strncat(status
,"CrcOK",DBD15STATLEN
);
971 strncat(status
,"CrcFail!",DBD15STATLEN
);
973 Dbprintf("%s",status
);
979 ///////////////////////////////////////////////////////////////////////
980 // Functions called via USB/Client
981 ///////////////////////////////////////////////////////////////////////
983 void SetDebugIso15693(uint32_t debug
) {
985 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
991 //-----------------------------------------------------------------------------
992 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
993 // all demodulation performed in arm rather than host. - greg
994 //-----------------------------------------------------------------------------
995 void ReaderIso15693(uint32_t parameter
)
1002 //DbpString(parameter);
1004 //uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much)
1005 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1006 uint8_t *answer2
= (((uint8_t *)BigBuf
) + 3760);
1007 uint8_t *answer3
= (((uint8_t *)BigBuf
) + 3860);
1008 //uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960); // where we hold the uid for hi15reader
1009 // int answerLen0 = 0;
1016 memset(BigBuf
+ 3660, 0, 300);
1021 // Start from off (no field generated)
1022 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1025 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1028 // Give the tags time to energize
1029 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1042 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1043 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1044 uint8_t TagUID
[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // where we hold the uid for hi15reader
1046 // BuildIdentifyRequest();
1047 // //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1048 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1049 // // Now wait for a response
1050 // responseLen0 = GetIso15693AnswerFromTag(receivedAnswer0, 100, &samples, &elapsed) ;
1051 // if (responseLen0 >=12) // we should do a better check than this
1053 // // really we should check it is a valid mesg
1054 // // but for now just grab what we think is the uid
1055 // TagUID[0] = receivedAnswer0[2];
1056 // TagUID[1] = receivedAnswer0[3];
1057 // TagUID[2] = receivedAnswer0[4];
1058 // TagUID[3] = receivedAnswer0[5];
1059 // TagUID[4] = receivedAnswer0[6];
1060 // TagUID[5] = receivedAnswer0[7];
1061 // TagUID[6] = receivedAnswer0[8]; // IC Manufacturer code
1062 // DbpIntegers(TagUID[6],TagUID[5],TagUID[4]);
1065 // Now send the IDENTIFY command
1066 BuildIdentifyRequest();
1067 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1068 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
); // No longer ToSendMax+3
1069 // Now wait for a response
1070 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
1072 if (answerLen1
>=12) // we should do a better check than this
1075 TagUID
[0] = answer1
[2];
1076 TagUID
[1] = answer1
[3];
1077 TagUID
[2] = answer1
[4];
1078 TagUID
[3] = answer1
[5];
1079 TagUID
[4] = answer1
[6];
1080 TagUID
[5] = answer1
[7];
1081 TagUID
[6] = answer1
[8]; // IC Manufacturer code
1082 TagUID
[7] = answer1
[9]; // always E0
1084 // Now send the SELECT command
1085 // since the SELECT command is optional, we should not rely on it.
1086 //// BuildSelectRequest(TagUID);
1087 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1088 // Now wait for a response
1089 /// answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
1091 // Now send the MULTI READ command
1092 // BuildArbitraryRequest(*TagUID,parameter);
1093 /// BuildArbitraryCustomRequest(TagUID,parameter);
1094 // BuildReadBlockRequest(*TagUID,parameter);
1095 // BuildSysInfoRequest(*TagUID);
1096 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1097 /// TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1098 // Now wait for a response
1099 /// answerLen3 = GetIso15693AnswerFromTag(answer3, 100, &samples, &elapsed) ;
1103 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
1104 DbdecodeIso15693Answer(answerLen1
,answer1
);
1105 Dbhexdump(answerLen1
,answer1
,true);
1109 //Dbprintf("UID = %*D",8,TagUID," ");
1110 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",TagUID
[7],TagUID
[6],TagUID
[5],
1111 TagUID
[4],TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1114 Dbprintf("%d octets read from SELECT request:", answerLen2
);
1115 DbdecodeIso15693Answer(answerLen2
,answer2
);
1116 Dbhexdump(answerLen2
,answer2
,true);
1118 Dbprintf("%d octets read from XXX request:", answerLen3
);
1119 DbdecodeIso15693Answer(answerLen3
,answer3
);
1120 Dbhexdump(answerLen3
,answer3
,true);
1124 if (answerLen1
>=12 && DEBUG
) {
1126 while (i
<32) { // sanity check, assume max 32 pages
1127 BuildReadBlockRequest(TagUID
,i
);
1128 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1129 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
1131 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
1132 DbdecodeIso15693Answer(answerLen2
,answer2
);
1133 Dbhexdump(answerLen2
,answer2
,true);
1134 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1141 // for(i = 0; i < responseLen3; i++) {
1142 // itoa(str1,receivedAnswer3[i]);
1143 // strncat(str2,str1,8);
1153 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1154 // all demodulation performed in arm rather than host. - greg
1155 void SimTagIso15693(uint32_t parameter
)
1162 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1166 memset(answer1
, 0, 100);
1171 // Start from off (no field generated)
1172 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1175 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1178 // Give the tags time to energize
1179 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); // NO GOOD FOR SIM TAG!!!!
1192 answerLen1
= GetIso15693AnswerFromSniff(answer1
, 100, &samples
, &elapsed
) ;
1194 if (answerLen1
>=1) // we should do a better check than this
1196 // Build a suitable reponse to the reader INVENTORY cocmmand
1197 BuildInventoryResponse();
1198 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
);
1201 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1202 answer1
[0], answer1
[1], answer1
[2],
1203 answer1
[3], answer1
[4], answer1
[5],
1204 answer1
[6], answer1
[7], answer1
[8]);
1213 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1214 // (some manufactures offer a way to read the AFI, though)
1215 void BruteforceIso15693Afi(uint32_t speed
)
1219 int datalen
=0, recvlen
=0;
1221 Iso15693InitReader();
1223 // first without AFI
1224 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1226 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1227 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1228 data
[1]=ISO15_CMD_INVENTORY
;
1229 data
[2]=0; // mask length
1230 datalen
=AddCrc(data
,3);
1231 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1234 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1239 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1240 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1241 data
[1]=ISO15_CMD_INVENTORY
;
1243 data
[3]=0; // mask length
1245 for (int i
=0;i
<256;i
++) {
1247 datalen
=AddCrc(data
,4);
1248 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1251 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1254 Dbprintf("AFI Bruteforcing done.");
1258 // Allows to directly send commands to the tag via the client
1259 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1262 uint8_t *recvbuf
=(uint8_t *)BigBuf
;
1267 Dbhexdump(datalen
,data
,true);
1270 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1273 // n.cmd=/* CMD_ISO_15693_COMMAND_DONE */ CMD_ACK;
1274 // n.arg[0]=recvlen>48?48:recvlen;
1275 // memcpy(n.d.asBytes, recvbuf, 48);
1277 cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48);
1278 // UsbSendPacket((uint8_t *)&n, sizeof(n));
1283 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1284 Dbhexdump(recvlen
,recvbuf
,true);
1293 // --------------------------------------------------------------------
1294 // -- Misc & deprecated functions
1295 // --------------------------------------------------------------------
1299 // do not use; has a fix UID
1300 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1305 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1306 // followed by teh block data
1307 // one sub-carrier, inventory, 1 slot, fast rate
1308 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1309 // System Information command code
1311 // UID may be optionally specified here
1320 cmd[9]= 0xe0; // always e0 (not exactly unique)
1322 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1323 cmd[10] = crc & 0xff;
1326 CodeIso15693AsReader(cmd, sizeof(cmd));
1330 // do not use; has a fix UID
1331 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1336 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1337 // followed by teh block data
1338 // one sub-carrier, inventory, 1 slot, fast rate
1339 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1340 // READ Multi BLOCK command code
1342 // UID may be optionally specified here
1351 cmd[9]= 0xe0; // always e0 (not exactly unique)
1352 // First Block number to read
1354 // Number of Blocks to read
1355 cmd[11] = 0x2f; // read quite a few
1357 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1358 cmd[12] = crc & 0xff;
1361 CodeIso15693AsReader(cmd, sizeof(cmd));
1364 // do not use; has a fix UID
1365 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1370 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1371 // followed by teh block data
1372 // one sub-carrier, inventory, 1 slot, fast rate
1373 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1374 // READ BLOCK command code
1376 // UID may be optionally specified here
1385 cmd[9]= 0xe0; // always e0 (not exactly unique)
1391 // cmd[13] = 0x00; //Now the CRC
1392 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1393 cmd[12] = crc & 0xff;
1396 CodeIso15693AsReader(cmd, sizeof(cmd));
1399 // do not use; has a fix UID
1400 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1405 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1406 // followed by teh block data
1407 // one sub-carrier, inventory, 1 slot, fast rate
1408 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1409 // READ BLOCK command code
1411 // UID may be optionally specified here
1420 cmd[9]= 0xe0; // always e0 (not exactly unique)
1422 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1426 // cmd[13] = 0x00; //Now the CRC
1427 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1428 cmd[12] = crc & 0xff;
1431 CodeIso15693AsReader(cmd, sizeof(cmd));