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 #include "mifareutil.h"
70 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
72 ///////////////////////////////////////////////////////////////////////
73 // ISO 15693 Part 2 - Air Interface
74 // This section basicly contains transmission and receiving of bits
75 ///////////////////////////////////////////////////////////////////////
77 #define FrameSOF Iso15693FrameSOF
78 #define Logic0 Iso15693Logic0
79 #define Logic1 Iso15693Logic1
80 #define FrameEOF Iso15693FrameEOF
82 #define Crc(data,datalen) Iso15693Crc(data,datalen)
83 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
84 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
89 // ---------------------------
91 // ---------------------------
93 // prepare data using "1 out of 4" code for later transmission
94 // resulting data rate is 26,48 kbit/s (fc/512)
96 // n ... length of data
97 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
103 // Give it a bit of slack at the beginning
104 for(i
= 0; i
< 24; i
++) {
117 for(i
= 0; i
< n
; i
++) {
118 for(j
= 0; j
< 8; j
+= 2) {
119 int these
= (cmd
[i
] >> j
) & 3;
170 // And slack at the end, too.
171 for(i
= 0; i
< 24; i
++) {
176 // encode data using "1 out of 256" sheme
177 // data rate is 1,66 kbit/s (fc/8192)
178 // is designed for more robust communication over longer distances
179 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
185 // Give it a bit of slack at the beginning
186 for(i
= 0; i
< 24; i
++) {
200 for(i
= 0; i
< n
; i
++) {
201 for (j
= 0; j
<=255; j
++) {
217 // And slack at the end, too.
218 for(i
= 0; i
< 24; i
++) {
224 // Transmit the command (to the tag) that was placed in ToSend[].
225 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
229 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
230 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
231 if(*wait
< 10) { *wait
= 10; }
233 // for(c = 0; c < *wait;) {
234 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
235 // AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
238 // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
239 // volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
247 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
248 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
254 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
255 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
260 *samples
= (c
+ *wait
) << 3;
263 //-----------------------------------------------------------------------------
264 // Transmit the command (to the reader) that was placed in ToSend[].
265 //-----------------------------------------------------------------------------
266 static void TransmitTo15693Reader(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
269 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
270 if(*wait
< 10) { *wait
= 10; }
273 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
274 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
280 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
281 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
286 *samples
= (c
+ *wait
) << 3;
297 // number of decoded bytes
298 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
301 uint8_t *dest
= (uint8_t *)BigBuf
;
307 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
308 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
312 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
313 AT91C_BASE_SSC
->SSC_THR
= 0x43;
315 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
317 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
319 // The samples are correlations against I and Q versions of the
320 // tone that the tag AM-modulates, so every other sample is I,
321 // every other is Q. We just want power, so abs(I) + abs(Q) is
322 // close to what we want.
337 dest
[c
++] = (uint8_t)r
;
350 //////////////////////////////////////////
351 /////////// DEMODULATE ///////////////////
352 //////////////////////////////////////////
355 int max
= 0, maxPos
=0;
359 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
361 // First, correlate for SOF
362 for(i
= 0; i
< 100; i
++) {
364 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
365 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
372 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
374 int k
= 0; // this will be our return value
376 // greg - If correlation is less than 1 then there's little point in continuing
377 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)
380 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
383 memset(outBuf
, 0, sizeof(outBuf
));
386 int corr0
= 0, corr1
= 0, corrEOF
= 0;
387 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
388 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
390 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
391 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
393 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
394 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
396 // Even things out by the length of the target waveform.
400 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
401 // DbpString("EOF at %d", i);
403 } else if(corr1
> corr0
) {
404 i
+= arraylen(Logic1
)/skip
;
407 i
+= arraylen(Logic0
)/skip
;
414 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
415 DbpString("ran off end!");
419 if(mask
!= 0x01) { // this happens, when we miss the EOF
420 // TODO: for some reason this happens quite often
421 if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
);
422 if (mask
<0x08) k
--; // discard the last uneven octet;
423 // 0x08 is an assumption - but works quite often
427 // strncat(str1," octets read",8);
429 // DbpString( str1); // DbpString("%d octets", k);
431 // for(i = 0; i < k; i+=3) {
432 // //DbpString("# %2d: %02x ", i, outBuf[i]);
433 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
436 for(i
= 0; i
< k
; i
++) {
437 receivedResponse
[i
] = outBuf
[i
];
439 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
440 return k
; // return the number of bytes demodulated
442 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
447 // Now the GetISO15693 message from sniffing command
448 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
)
451 uint8_t *dest
= (uint8_t *)BigBuf
;
457 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
458 //spindelay(60); // greg - experiment to get rid of some of the 0 byte/failed reads
462 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
463 AT91C_BASE_SSC
->SSC_THR
= 0x43;
465 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
466 int8_t b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
468 // The samples are correlations against I and Q versions of the
469 // tone that the tag AM-modulates, so every other sample is I,
470 // every other is Q. We just want power, so abs(I) + abs(Q) is
471 // close to what we want.
474 dest
[c
++] = abs(b
) + abs(prev
);
487 //////////////////////////////////////////
488 /////////// DEMODULATE ///////////////////
489 //////////////////////////////////////////
492 int max
= 0, maxPos
=0;
496 // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL
498 // First, correlate for SOF
499 for(i
= 0; i
< 19000; i
++) {
501 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
502 corr
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)];
509 // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
511 int k
= 0; // this will be our return value
513 // greg - If correlation is less than 1 then there's little point in continuing
514 if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) // THIS SHOULD BE 1
517 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
520 memset(outBuf
, 0, sizeof(outBuf
));
523 int corr0
= 0, corr1
= 0, corrEOF
= 0;
524 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
525 corr0
+= Logic0
[j
]*dest
[i
+(j
/skip
)];
527 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
528 corr1
+= Logic1
[j
]*dest
[i
+(j
/skip
)];
530 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
531 corrEOF
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)];
533 // Even things out by the length of the target waveform.
537 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
538 // DbpString("EOF at %d", i);
540 } else if(corr1
> corr0
) {
541 i
+= arraylen(Logic1
)/skip
;
544 i
+= arraylen(Logic0
)/skip
;
551 if((i
+(int)arraylen(FrameEOF
)) >= 2000) {
552 DbpString("ran off end!");
557 DbpString("sniff: error, uneven octet! (discard extra bits!)");
558 /// DbpString(" mask=%02x", mask);
562 // strncat(str1," octets read",8);
564 // DbpString( str1); // DbpString("%d octets", k);
566 // for(i = 0; i < k; i+=3) {
567 // //DbpString("# %2d: %02x ", i, outBuf[i]);
568 // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]);
571 for(i
= 0; i
< k
; i
++) {
572 receivedResponse
[i
] = outBuf
[i
];
574 } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip))
575 return k
; // return the number of bytes demodulated
577 /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2));
581 static void BuildIdentifyRequest(void);
582 //-----------------------------------------------------------------------------
583 // Start to read an ISO 15693 tag. We send an identify request, then wait
584 // for the response. The response is not demodulated, just left in the buffer
585 // so that it can be downloaded to a PC and processed there.
586 //-----------------------------------------------------------------------------
587 void AcquireRawAdcSamplesIso15693(void)
589 uint8_t *dest
= get_bigbufptr_recvrespbuf();
595 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
596 BuildIdentifyRequest();
598 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
600 // Give the tags time to energize
601 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
604 // Now send the command
606 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
610 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
611 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
613 if(c
== ToSendMax
+3) {
617 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
618 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
624 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
629 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
630 AT91C_BASE_SSC
->SSC_THR
= 0x43;
632 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
634 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
636 // The samples are correlations against I and Q versions of the
637 // tone that the tag AM-modulates, so every other sample is I,
638 // every other is Q. We just want power, so abs(I) + abs(Q) is
639 // close to what we want.
654 dest
[c
++] = (uint8_t)r
;
669 void RecordRawAdcSamplesIso15693(void)
671 uint8_t *dest
= get_bigbufptr_recvrespbuf();
677 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
681 // Start from off (no field generated)
682 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
685 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
689 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
694 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
695 AT91C_BASE_SSC
->SSC_THR
= 0x43;
697 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
699 b
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
;
701 // The samples are correlations against I and Q versions of the
702 // tone that the tag AM-modulates, so every other sample is I,
703 // every other is Q. We just want power, so abs(I) + abs(Q) is
704 // close to what we want.
719 dest
[c
++] = (uint8_t)r
;
732 Dbprintf("fin record");
736 // Initialize the proxmark as iso15k reader
737 // (this might produces glitches that confuse some tags
738 void Iso15693InitReader() {
744 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
748 // Start from off (no field generated)
749 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
752 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
755 // Give the tags time to energize
756 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
765 ///////////////////////////////////////////////////////////////////////
766 // ISO 15693 Part 3 - Air Interface
767 // This section basicly contains transmission and receiving of bits
768 ///////////////////////////////////////////////////////////////////////
770 // Encode (into the ToSend buffers) an identify request, which is the first
771 // thing that you must send to a tag to get a response.
772 static void BuildIdentifyRequest(void)
777 // one sub-carrier, inventory, 1 slot, fast rate
778 // AFI is at bit 5 (1<<4) when doing an INVENTORY
779 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
780 // inventory command code
789 CodeIso15693AsReader(cmd
, sizeof(cmd
));
792 // uid is in transmission order (which is reverse of display order)
793 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
798 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
799 // followed by teh block data
800 // one sub-carrier, inventory, 1 slot, fast rate
801 cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
802 // READ BLOCK command code
804 // UID may be optionally specified here
813 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
814 // Block number to read
815 cmd
[10] = blockNumber
;//0x00;
817 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes
818 cmd
[11] = crc
& 0xff;
821 CodeIso15693AsReader(cmd
, sizeof(cmd
));
824 // Now the VICC>VCD responses when we are simulating a tag
825 static void BuildInventoryResponse( uint8_t *uid
)
830 // one sub-carrier, inventory, 1 slot, fast rate
831 // AFI is at bit 5 (1<<4) when doing an INVENTORY
832 //(1 << 2) | (1 << 5) | (1 << 1);
834 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
836 cmd
[2] = uid
[7]; //0x32;
837 cmd
[3] = uid
[6]; //0x4b;
838 cmd
[4] = uid
[5]; //0x03;
839 cmd
[5] = uid
[4]; //0x01;
840 cmd
[6] = uid
[3]; //0x00;
841 cmd
[7] = uid
[2]; //0x10;
842 cmd
[8] = uid
[1]; //0x05;
843 cmd
[9] = uid
[0]; //0xe0;
846 cmd
[10] = crc
& 0xff;
849 CodeIso15693AsReader(cmd
, sizeof(cmd
));
852 // Universal Method for sending to and recv bytes from a tag
853 // init ... should we initialize the reader?
854 // speed ... 0 low speed, 1 hi speed
855 // **recv will return you a pointer to the received data
856 // If you do not need the answer use NULL for *recv[]
857 // return: lenght of received data
858 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) {
871 uint8_t *answer
= (((uint8_t *)BigBuf
) + 3660);
872 if (recv
!=NULL
) memset(BigBuf
+ 3660, 0, 100);
874 if (init
) Iso15693InitReader();
877 // low speed (1 out of 256)
878 CodeIso15693AsReader256(send
, sendlen
);
880 // high speed (1 out of 4)
881 CodeIso15693AsReader(send
, sendlen
);
887 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
888 // Now wait for a response
892 answerLen
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;
905 // --------------------------------------------------------------------
907 // --------------------------------------------------------------------
909 // Decodes a message from a tag and displays its metadata and content
910 #define DBD15STATLEN 48
911 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
912 char status
[DBD15STATLEN
+1]={0};
917 strncat(status
,"ProtExt ",DBD15STATLEN
);
920 strncat(status
,"Error ",DBD15STATLEN
);
923 strncat(status
,"01:notSupp",DBD15STATLEN
);
926 strncat(status
,"02:notRecog",DBD15STATLEN
);
929 strncat(status
,"03:optNotSupp",DBD15STATLEN
);
932 strncat(status
,"0f:noInfo",DBD15STATLEN
);
935 strncat(status
,"10:dontExist",DBD15STATLEN
);
938 strncat(status
,"11:lockAgain",DBD15STATLEN
);
941 strncat(status
,"12:locked",DBD15STATLEN
);
944 strncat(status
,"13:progErr",DBD15STATLEN
);
947 strncat(status
,"14:lockErr",DBD15STATLEN
);
950 strncat(status
,"unknownErr",DBD15STATLEN
);
952 strncat(status
," ",DBD15STATLEN
);
954 strncat(status
,"NoErr ",DBD15STATLEN
);
958 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
959 strncat(status
,"CrcOK",DBD15STATLEN
);
961 strncat(status
,"CrcFail!",DBD15STATLEN
);
963 Dbprintf("%s",status
);
969 ///////////////////////////////////////////////////////////////////////
970 // Functions called via USB/Client
971 ///////////////////////////////////////////////////////////////////////
973 void SetDebugIso15693(uint32_t debug
) {
975 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
981 //-----------------------------------------------------------------------------
982 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
983 // all demodulation performed in arm rather than host. - greg
984 //-----------------------------------------------------------------------------
985 void ReaderIso15693(uint32_t parameter
)
992 uint8_t *answer1
= (((uint8_t *)BigBuf
) + 3660); //
993 uint8_t *answer2
= (((uint8_t *)BigBuf
) + 3760);
994 uint8_t *answer3
= (((uint8_t *)BigBuf
) + 3860);
1004 uint8_t TagUID
[8] = {0x00};
1008 memset(BigBuf
+ 3660, 0x00, 300);
1010 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1012 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1016 // Start from off (no field generated)
1017 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1020 // Give the tags time to energize
1021 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1029 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1030 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1032 // Now send the IDENTIFY command
1033 BuildIdentifyRequest();
1035 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1037 // Now wait for a response
1038 answerLen1
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ;
1040 if (answerLen1
>=12) // we should do a better check than this
1042 TagUID
[0] = answer1
[2];
1043 TagUID
[1] = answer1
[3];
1044 TagUID
[2] = answer1
[4];
1045 TagUID
[3] = answer1
[5];
1046 TagUID
[4] = answer1
[6];
1047 TagUID
[5] = answer1
[7];
1048 TagUID
[6] = answer1
[8]; // IC Manufacturer code
1049 TagUID
[7] = answer1
[9]; // always E0
1053 Dbprintf("%d octets read from IDENTIFY request:", answerLen1
);
1054 DbdecodeIso15693Answer(answerLen1
,answer1
);
1055 Dbhexdump(answerLen1
,answer1
,true);
1058 if (answerLen1
>= 12)
1059 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1060 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
1061 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1064 Dbprintf("%d octets read from SELECT request:", answerLen2
);
1065 DbdecodeIso15693Answer(answerLen2
,answer2
);
1066 Dbhexdump(answerLen2
,answer2
,true);
1068 Dbprintf("%d octets read from XXX request:", answerLen3
);
1069 DbdecodeIso15693Answer(answerLen3
,answer3
);
1070 Dbhexdump(answerLen3
,answer3
,true);
1073 if (answerLen1
>= 12 && DEBUG
) {
1075 while (i
< 32) { // sanity check, assume max 32 pages
1076 BuildReadBlockRequest(TagUID
,i
);
1077 TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);
1078 answerLen2
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
);
1079 if (answerLen2
> 0) {
1080 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
);
1081 DbdecodeIso15693Answer(answerLen2
,answer2
);
1082 Dbhexdump(answerLen2
,answer2
,true);
1083 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr
1095 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
1096 // all demodulation performed in arm rather than host. - greg
1097 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
)
1104 uint8_t *buf
= (((uint8_t *)BigBuf
) + 3660); //
1112 memset(buf
, 0x00, 100);
1114 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1116 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1120 // Start from off (no field generated)
1121 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1130 answerLen1
= GetIso15693AnswerFromSniff(buf
, 100, &samples
, &elapsed
) ;
1132 if (answerLen1
>=1) // we should do a better check than this
1134 // Build a suitable reponse to the reader INVENTORY cocmmand
1135 // not so obsvious, but in the call to BuildInventoryResponse, the command is copied to the global ToSend buffer used below.
1137 BuildInventoryResponse(uid
);
1139 TransmitTo15693Reader(ToSend
, ToSendMax
, &tsamples
, &wait
);
1142 Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
,
1143 buf
[0], buf
[1], buf
[2], buf
[3],
1144 buf
[4], buf
[5], buf
[6], buf
[7], buf
[8]);
1146 Dbprintf("Simulationg uid: %x %x %x %x %x %x %x %x",
1147 uid
[0], uid
[1], uid
[2], uid
[3],
1148 uid
[4], uid
[5], uid
[6], uid
[7]);
1157 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1158 // (some manufactures offer a way to read the AFI, though)
1159 void BruteforceIso15693Afi(uint32_t speed
)
1163 int datalen
=0, recvlen
=0;
1165 Iso15693InitReader();
1167 // first without AFI
1168 // Tags should respond wihtout AFI and with AFI=0 even when AFI is active
1170 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1171 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
1172 data
[1]=ISO15_CMD_INVENTORY
;
1173 data
[2]=0; // mask length
1174 datalen
=AddCrc(data
,3);
1175 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1178 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2]));
1183 data
[0]=ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
1184 ISO15_REQ_INVENTORY
| ISO15_REQINV_AFI
| ISO15_REQINV_SLOT1
;
1185 data
[1]=ISO15_CMD_INVENTORY
;
1187 data
[3]=0; // mask length
1189 for (int i
=0;i
<256;i
++) {
1191 datalen
=AddCrc(data
,4);
1192 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
);
1195 Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2]));
1198 Dbprintf("AFI Bruteforcing done.");
1202 // Allows to directly send commands to the tag via the client
1203 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1206 uint8_t *recvbuf
=(uint8_t *)BigBuf
;
1211 Dbhexdump(datalen
,data
,true);
1214 recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
));
1218 cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48);
1223 DbdecodeIso15693Answer(recvlen
,recvbuf
);
1224 Dbhexdump(recvlen
,recvbuf
,true);
1233 // --------------------------------------------------------------------
1234 // -- Misc & deprecated functions
1235 // --------------------------------------------------------------------
1239 // do not use; has a fix UID
1240 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1245 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1246 // followed by teh block data
1247 // one sub-carrier, inventory, 1 slot, fast rate
1248 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1249 // System Information command code
1251 // UID may be optionally specified here
1260 cmd[9]= 0xe0; // always e0 (not exactly unique)
1262 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1263 cmd[10] = crc & 0xff;
1266 CodeIso15693AsReader(cmd, sizeof(cmd));
1270 // do not use; has a fix UID
1271 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1276 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1277 // followed by teh block data
1278 // one sub-carrier, inventory, 1 slot, fast rate
1279 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1280 // READ Multi BLOCK command code
1282 // UID may be optionally specified here
1291 cmd[9]= 0xe0; // always e0 (not exactly unique)
1292 // First Block number to read
1294 // Number of Blocks to read
1295 cmd[11] = 0x2f; // read quite a few
1297 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1298 cmd[12] = crc & 0xff;
1301 CodeIso15693AsReader(cmd, sizeof(cmd));
1304 // do not use; has a fix UID
1305 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1310 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1311 // followed by teh block data
1312 // one sub-carrier, inventory, 1 slot, fast rate
1313 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1314 // READ BLOCK command code
1316 // UID may be optionally specified here
1325 cmd[9]= 0xe0; // always e0 (not exactly unique)
1331 // cmd[13] = 0x00; //Now the CRC
1332 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1333 cmd[12] = crc & 0xff;
1336 CodeIso15693AsReader(cmd, sizeof(cmd));
1339 // do not use; has a fix UID
1340 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1345 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1346 // followed by teh block data
1347 // one sub-carrier, inventory, 1 slot, fast rate
1348 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1349 // READ BLOCK command code
1351 // UID may be optionally specified here
1360 cmd[9]= 0xe0; // always e0 (not exactly unique)
1362 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1366 // cmd[13] = 0x00; //Now the CRC
1367 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1368 cmd[12] = crc & 0xff;
1371 CodeIso15693AsReader(cmd, sizeof(cmd));