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 "../include/proxmark3.h"
65 #include "../common/iso15693tools.h"
66 #include "../common/cmd.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 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
610 BuildIdentifyRequest();
612 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
614 // Give the tags time to energize
615 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
618 // Now send the command
620 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
624 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
625 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
627 if(c
== ToSendMax
+3) {
631 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
632 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
638 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
643 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
644 AT91C_BASE_SSC
->SSC_THR
= 0x43;
646 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
648 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
650 // The samples are correlations against I and Q versions of the
651 // tone that the tag AM-modulates, so every other sample is I,
652 // every other is Q. We just want power, so abs(I) + abs(Q) is
653 // close to what we want.
668 dest
[c
++] = (uint8_t)r
;
683 void RecordRawAdcSamplesIso15693(void)
686 uint8_t *dest
= (uint8_t *)BigBuf
;
691 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
695 // Start from off (no field generated)
696 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
699 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
703 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
708 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
709 AT91C_BASE_SSC
->SSC_THR
= 0x43;
711 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
713 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
715 // The samples are correlations against I and Q versions of the
716 // tone that the tag AM-modulates, so every other sample is I,
717 // every other is Q. We just want power, so abs(I) + abs(Q) is
718 // close to what we want.
733 dest
[c
++] = (uint8_t)r
;
746 Dbprintf("fin record");
750 // Initialize the proxmark as iso15k reader
751 // (this might produces glitches that confuse some tags
752 void Iso15693InitReader() {
758 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
762 // Start from off (no field generated)
763 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
766 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
769 // Give the tags time to energize
770 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
779 ///////////////////////////////////////////////////////////////////////
780 // ISO 15693 Part 3 - Air Interface
781 // This section basicly contains transmission and receiving of bits
782 ///////////////////////////////////////////////////////////////////////
784 // Encode (into the ToSend buffers) an identify request, which is the first
785 // thing that you must send to a tag to get a response.
786 static void BuildIdentifyRequest(void)
791 // one sub-carrier, inventory, 1 slot, fast rate
792 // AFI is at bit 5 (1<<4) when doing an INVENTORY
793 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
794 // inventory command code
803 CodeIso15693AsReader(cmd
, sizeof(cmd
));
806 // uid is in transmission order (which is reverse of display order)
807 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
812 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
813 // followed by teh block data
814 // one sub-carrier, inventory, 1 slot, fast rate
815 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
816 // READ BLOCK command code
818 // UID may be optionally specified here
827 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
828 // Block number to read
829 cmd
[10] = blockNumber
;//0x00;
831 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
832 cmd
[11] = crc
& 0xff;
835 CodeIso15693AsReader(cmd
, sizeof(cmd
));
838 // Now the VICC>VCD responses when we are simulating a tag
839 static void BuildInventoryResponse(void)
844 // one sub-carrier, inventory, 1 slot, fast rate
845 // AFI is at bit 5 (1<<4) when doing an INVENTORY
846 cmd
[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1);
859 cmd
[10] = crc
& 0xff;
862 CodeIso15693AsReader(cmd
, sizeof(cmd
));
865 // Universal Method for sending to and recv bytes from a tag
866 // init ... should we initialize the reader?
867 // speed ... 0 low speed, 1 hi speed
868 // **recv will return you a pointer to the received data
869 // If you do not need the answer use NULL for *recv[]
870 // return: lenght of received data
871 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
884 uint8_t *answer
= (((uint8_t *)BigBuf
) + 3660);
885 if (recv
!=NULL
) memset(BigBuf
+ 3660, 0, 100);
887 if (init
) Iso15693InitReader();
890 // low speed (1 out of 256)
891 CodeIso15693AsReader256(send
, sendlen
);
893 // high speed (1 out of 4)
894 CodeIso15693AsReader(send
, sendlen
);
900 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
901 // Now wait for a response
905 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
918 // --------------------------------------------------------------------
920 // --------------------------------------------------------------------
922 // Decodes a message from a tag and displays its metadata and content
923 #define DBD15STATLEN 48
924 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
925 char status
[DBD15STATLEN
+1]={0};
930 strncat(status
,"ProtExt ",DBD15STATLEN
);
933 strncat(status
,"Error ",DBD15STATLEN
);
936 strncat(status
,"01:notSupp",DBD15STATLEN
);
939 strncat(status
,"02:notRecog",DBD15STATLEN
);
942 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
945 strncat(status
,"0f:noInfo",DBD15STATLEN
);
948 strncat(status
,"10:dontExist",DBD15STATLEN
);
951 strncat(status
,"11:lockAgain",DBD15STATLEN
);
954 strncat(status
,"12:locked",DBD15STATLEN
);
957 strncat(status
,"13:progErr",DBD15STATLEN
);
960 strncat(status
,"14:lockErr",DBD15STATLEN
);
963 strncat(status
,"unknownErr",DBD15STATLEN
);
965 strncat(status
," ",DBD15STATLEN
);
967 strncat(status
,"NoErr ",DBD15STATLEN
);
971 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
972 strncat(status
,"CrcOK",DBD15STATLEN
);
974 strncat(status
,"CrcFail!",DBD15STATLEN
);
976 Dbprintf("%s",status
);
982 ///////////////////////////////////////////////////////////////////////
983 // Functions called via USB/Client
984 ///////////////////////////////////////////////////////////////////////
986 void SetDebugIso15693(uint32_t debug
) {
988 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
994 //-----------------------------------------------------------------------------
995 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
996 // all demodulation performed in arm rather than host. - greg
997 //-----------------------------------------------------------------------------
998 void ReaderIso15693(uint32_t parameter
)
1005 //DbpString(parameter);
1007 //uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much)
1008 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1009 uint8_t *answer2
= (((uint8_t *)BigBuf
) + 3760);
1010 uint8_t *answer3
= (((uint8_t *)BigBuf
) + 3860);
1011 //uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960); // where we hold the uid for hi15reader
1012 // int answerLen0 = 0;
1019 memset(BigBuf
+ 3660, 0, 300);
1021 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1025 // Start from off (no field generated)
1026 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1029 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1032 // Give the tags time to energize
1033 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1046 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1047 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1048 uint8_t TagUID
[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // where we hold the uid for hi15reader
1050 // BuildIdentifyRequest();
1051 // //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1052 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1053 // // Now wait for a response
1054 // responseLen0 = GetIso15693AnswerFromTag(receivedAnswer0, 100, &samples, &elapsed) ;
1055 // if (responseLen0 >=12) // we should do a better check than this
1057 // // really we should check it is a valid mesg
1058 // // but for now just grab what we think is the uid
1059 // TagUID[0] = receivedAnswer0[2];
1060 // TagUID[1] = receivedAnswer0[3];
1061 // TagUID[2] = receivedAnswer0[4];
1062 // TagUID[3] = receivedAnswer0[5];
1063 // TagUID[4] = receivedAnswer0[6];
1064 // TagUID[5] = receivedAnswer0[7];
1065 // TagUID[6] = receivedAnswer0[8]; // IC Manufacturer code
1066 // DbpIntegers(TagUID[6],TagUID[5],TagUID[4]);
1069 // Now send the IDENTIFY command
1070 BuildIdentifyRequest();
1071 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1072 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
); // No longer ToSendMax+3
1073 // Now wait for a response
1074 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
1076 if (answerLen1
>=12) // we should do a better check than this
1079 TagUID
[0] = answer1
[2];
1080 TagUID
[1] = answer1
[3];
1081 TagUID
[2] = answer1
[4];
1082 TagUID
[3] = answer1
[5];
1083 TagUID
[4] = answer1
[6];
1084 TagUID
[5] = answer1
[7];
1085 TagUID
[6] = answer1
[8]; // IC Manufacturer code
1086 TagUID
[7] = answer1
[9]; // always E0
1088 // Now send the SELECT command
1089 // since the SELECT command is optional, we should not rely on it.
1090 //// BuildSelectRequest(TagUID);
1091 // TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1092 // Now wait for a response
1093 /// answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
1095 // Now send the MULTI READ command
1096 // BuildArbitraryRequest(*TagUID,parameter);
1097 /// BuildArbitraryCustomRequest(TagUID,parameter);
1098 // BuildReadBlockRequest(*TagUID,parameter);
1099 // BuildSysInfoRequest(*TagUID);
1100 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
1101 /// TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); // No longer ToSendMax+3
1102 // Now wait for a response
1103 /// answerLen3 = GetIso15693AnswerFromTag(answer3, 100, &samples, &elapsed) ;
1107 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
1108 DbdecodeIso15693Answer(answerLen1
,answer1
);
1109 Dbhexdump(answerLen1
,answer1
,true);
1113 //Dbprintf("UID = %*D",8,TagUID," ");
1114 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",TagUID
[7],TagUID
[6],TagUID
[5],
1115 TagUID
[4],TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1118 Dbprintf("%d octets read from SELECT request:", answerLen2
);
1119 DbdecodeIso15693Answer(answerLen2
,answer2
);
1120 Dbhexdump(answerLen2
,answer2
,true);
1122 Dbprintf("%d octets read from XXX request:", answerLen3
);
1123 DbdecodeIso15693Answer(answerLen3
,answer3
);
1124 Dbhexdump(answerLen3
,answer3
,true);
1128 if (answerLen1
>=12 && DEBUG
) {
1130 while (i
<32) { // sanity check, assume max 32 pages
1131 BuildReadBlockRequest(TagUID
,i
);
1132 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1133 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
1135 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
1136 DbdecodeIso15693Answer(answerLen2
,answer2
);
1137 Dbhexdump(answerLen2
,answer2
,true);
1138 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1145 // for(i = 0; i < responseLen3; i++) {
1146 // itoa(str1,receivedAnswer3[i]);
1147 // strncat(str2,str1,8);
1157 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1158 // all demodulation performed in arm rather than host. - greg
1159 void SimTagIso15693(uint32_t parameter
)
1166 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
1170 memset(answer1
, 0, 100);
1172 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1176 // Start from off (no field generated)
1177 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1180 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1183 // Give the tags time to energize
1184 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); // NO GOOD FOR SIM TAG!!!!
1197 answerLen1
= GetIso15693AnswerFromSniff(answer1
, 100, &samples
, &elapsed
) ;
1199 if (answerLen1
>=1) // we should do a better check than this
1201 // Build a suitable reponse to the reader INVENTORY cocmmand
1202 BuildInventoryResponse();
1203 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
);
1206 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1207 answer1
[0], answer1
[1], answer1
[2],
1208 answer1
[3], answer1
[4], answer1
[5],
1209 answer1
[6], answer1
[7], answer1
[8]);
1218 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1219 // (some manufactures offer a way to read the AFI, though)
1220 void BruteforceIso15693Afi(uint32_t speed
)
1224 int datalen
=0, recvlen
=0;
1226 Iso15693InitReader();
1228 // first without AFI
1229 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1231 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1232 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1233 data
[1]=ISO15_CMD_INVENTORY
;
1234 data
[2]=0; // mask length
1235 datalen
=AddCrc(data
,3);
1236 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1239 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1244 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1245 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1246 data
[1]=ISO15_CMD_INVENTORY
;
1248 data
[3]=0; // mask length
1250 for (int i
=0;i
<256;i
++) {
1252 datalen
=AddCrc(data
,4);
1253 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1256 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1259 Dbprintf("AFI Bruteforcing done.");
1263 // Allows to directly send commands to the tag via the client
1264 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1267 uint8_t *recvbuf
=(uint8_t *)BigBuf
;
1272 Dbhexdump(datalen
,data
,true);
1275 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1279 cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48);
1284 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1285 Dbhexdump(recvlen
,recvbuf
,true);
1294 // --------------------------------------------------------------------
1295 // -- Misc & deprecated functions
1296 // --------------------------------------------------------------------
1300 // do not use; has a fix UID
1301 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1306 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1307 // followed by teh block data
1308 // one sub-carrier, inventory, 1 slot, fast rate
1309 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1310 // System Information command code
1312 // UID may be optionally specified here
1321 cmd[9]= 0xe0; // always e0 (not exactly unique)
1323 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1324 cmd[10] = crc & 0xff;
1327 CodeIso15693AsReader(cmd, sizeof(cmd));
1331 // do not use; has a fix UID
1332 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1337 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1338 // followed by teh block data
1339 // one sub-carrier, inventory, 1 slot, fast rate
1340 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1341 // READ Multi BLOCK command code
1343 // UID may be optionally specified here
1352 cmd[9]= 0xe0; // always e0 (not exactly unique)
1353 // First Block number to read
1355 // Number of Blocks to read
1356 cmd[11] = 0x2f; // read quite a few
1358 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1359 cmd[12] = crc & 0xff;
1362 CodeIso15693AsReader(cmd, sizeof(cmd));
1365 // do not use; has a fix UID
1366 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1371 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1372 // followed by teh block data
1373 // one sub-carrier, inventory, 1 slot, fast rate
1374 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1375 // READ BLOCK command code
1377 // UID may be optionally specified here
1386 cmd[9]= 0xe0; // always e0 (not exactly unique)
1392 // cmd[13] = 0x00; //Now the CRC
1393 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1394 cmd[12] = crc & 0xff;
1397 CodeIso15693AsReader(cmd, sizeof(cmd));
1400 // do not use; has a fix UID
1401 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1406 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1407 // followed by teh block data
1408 // one sub-carrier, inventory, 1 slot, fast rate
1409 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1410 // READ BLOCK command code
1412 // UID may be optionally specified here
1421 cmd[9]= 0xe0; // always e0 (not exactly unique)
1423 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1427 // cmd[13] = 0x00; //Now the CRC
1428 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1429 cmd[12] = crc & 0xff;
1432 CodeIso15693AsReader(cmd, sizeof(cmd));