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
) 
 270 //      FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX); 
 271         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
);        // No requirement to energise my coils 
 272         if(*wait 
< 10) { *wait 
= 10; } 
 276         if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 277             AT91C_BASE_SSC
->SSC_THR 
= cmd
[c
]; 
 283         if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 284             volatile uint32_t r 
= AT91C_BASE_SSC
->SSC_RHR
; 
 289         *samples 
= (c 
+ *wait
) << 3; 
 300 //              number of decoded bytes 
 301 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) 
 304         uint8_t *dest 
= (uint8_t *)BigBuf
; 
 310         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 311         //spindelay(60);        // greg - experiment to get rid of some of the 0 byte/failed reads 
 315                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 316                         AT91C_BASE_SSC
->SSC_THR 
= 0x43; 
 318                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 320                         b 
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
; 
 322                         // The samples are correlations against I and Q versions of the 
 323                         // tone that the tag AM-modulates, so every other sample is I, 
 324                         // every other is Q. We just want power, so abs(I) + abs(Q) is 
 325                         // close to what we want. 
 340                                 dest
[c
++] = (uint8_t)r
; 
 353         ////////////////////////////////////////// 
 354         /////////// DEMODULATE /////////////////// 
 355         ////////////////////////////////////////// 
 358         int max 
= 0, maxPos
=0; 
 362         //      if(GraphTraceLen < 1000) return;        // THIS CHECKS FOR A BUFFER TO SMALL 
 364         // First, correlate for SOF 
 365         for(i 
= 0; i 
< 100; i
++) { 
 367                 for(j 
= 0; j 
< arraylen(FrameSOF
); j 
+= skip
) { 
 368                         corr 
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)]; 
 375         //      DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip)); 
 377         int k 
= 0; // this will be our return value 
 379         // greg - If correlation is less than 1 then there's little point in continuing 
 380         if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1) 
 383                 i 
= maxPos 
+ arraylen(FrameSOF
)/skip
; 
 386                 memset(outBuf
, 0, sizeof(outBuf
)); 
 389                         int corr0 
= 0, corr1 
= 0, corrEOF 
= 0; 
 390                         for(j 
= 0; j 
< arraylen(Logic0
); j 
+= skip
) { 
 391                                 corr0 
+= Logic0
[j
]*dest
[i
+(j
/skip
)]; 
 393                         for(j 
= 0; j 
< arraylen(Logic1
); j 
+= skip
) { 
 394                                 corr1 
+= Logic1
[j
]*dest
[i
+(j
/skip
)]; 
 396                         for(j 
= 0; j 
< arraylen(FrameEOF
); j 
+= skip
) { 
 397                                 corrEOF 
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)]; 
 399                         // Even things out by the length of the target waveform. 
 403                         if(corrEOF 
> corr1 
&& corrEOF 
> corr0
) { 
 404         //                      DbpString("EOF at %d", i); 
 406                         } else if(corr1 
> corr0
) { 
 407                                 i 
+= arraylen(Logic1
)/skip
; 
 410                                 i 
+= arraylen(Logic0
)/skip
; 
 417                         if((i
+(int)arraylen(FrameEOF
)) >= 2000) { 
 418                                 DbpString("ran off end!"); 
 422                 if(mask 
!= 0x01) { // this happens, when we miss the EOF 
 423                         // TODO: for some reason this happens quite often 
 424                         if (DEBUG
) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask
); 
 425                         if (mask
<0x08) k
--; // discard the last uneven octet; 
 426                         // 0x08 is an assumption - but works quite often 
 430         //      strncat(str1," octets read",8); 
 432         //      DbpString(  str1);    // DbpString("%d octets", k); 
 434         //      for(i = 0; i < k; i+=3) { 
 435         //              //DbpString("# %2d: %02x ", i, outBuf[i]); 
 436         //              DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]); 
 439                 for(i 
= 0; i 
< k
; i
++) { 
 440                         receivedResponse
[i
] = outBuf
[i
]; 
 442         } // "end if correlation > 0"   (max/(arraylen(FrameSOF)/skip)) 
 443         return k
; // return the number of bytes demodulated 
 445 ///     DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2)); 
 450 // Now the GetISO15693 message from sniffing command 
 451 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) 
 454         uint8_t *dest 
= (uint8_t *)BigBuf
; 
 460         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 461         //spindelay(60);        // greg - experiment to get rid of some of the 0 byte/failed reads 
 465                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 466                         AT91C_BASE_SSC
->SSC_THR 
= 0x43; 
 468                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 470                         b 
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
; 
 472                         // The samples are correlations against I and Q versions of the 
 473                         // tone that the tag AM-modulates, so every other sample is I, 
 474                         // every other is Q. We just want power, so abs(I) + abs(Q) is 
 475                         // close to what we want. 
 490                                 dest
[c
++] = (uint8_t)r
; 
 503         ////////////////////////////////////////// 
 504         /////////// DEMODULATE /////////////////// 
 505         ////////////////////////////////////////// 
 508         int max 
= 0, maxPos
=0; 
 512 //      if(GraphTraceLen < 1000) return;        // THIS CHECKS FOR A BUFFER TO SMALL 
 514         // First, correlate for SOF 
 515         for(i 
= 0; i 
< 19000; i
++) { 
 517                 for(j 
= 0; j 
< arraylen(FrameSOF
); j 
+= skip
) { 
 518                         corr 
+= FrameSOF
[j
]*dest
[i
+(j
/skip
)]; 
 525 //      DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip)); 
 527         int k 
= 0; // this will be our return value 
 529         // greg - If correlation is less than 1 then there's little point in continuing 
 530         if ((max
/(arraylen(FrameSOF
)/skip
)) >= 1)       // THIS SHOULD BE 1 
 533                 i 
= maxPos 
+ arraylen(FrameSOF
)/skip
; 
 536                 memset(outBuf
, 0, sizeof(outBuf
)); 
 539                         int corr0 
= 0, corr1 
= 0, corrEOF 
= 0; 
 540                         for(j 
= 0; j 
< arraylen(Logic0
); j 
+= skip
) { 
 541                                 corr0 
+= Logic0
[j
]*dest
[i
+(j
/skip
)]; 
 543                         for(j 
= 0; j 
< arraylen(Logic1
); j 
+= skip
) { 
 544                                 corr1 
+= Logic1
[j
]*dest
[i
+(j
/skip
)]; 
 546                         for(j 
= 0; j 
< arraylen(FrameEOF
); j 
+= skip
) { 
 547                                 corrEOF 
+= FrameEOF
[j
]*dest
[i
+(j
/skip
)]; 
 549                         // Even things out by the length of the target waveform. 
 553                         if(corrEOF 
> corr1 
&& corrEOF 
> corr0
) { 
 554         //                      DbpString("EOF at %d", i); 
 556                         } else if(corr1 
> corr0
) { 
 557                                 i 
+= arraylen(Logic1
)/skip
; 
 560                                 i 
+= arraylen(Logic0
)/skip
; 
 567                         if((i
+(int)arraylen(FrameEOF
)) >= 2000) { 
 568                                 DbpString("ran off end!"); 
 573                         DbpString("sniff: error, uneven octet! (discard extra bits!)"); 
 574         ///             DbpString("   mask=%02x", mask); 
 578         //      strncat(str1," octets read",8); 
 580         //      DbpString(  str1);    // DbpString("%d octets", k); 
 582         //      for(i = 0; i < k; i+=3) { 
 583         //              //DbpString("# %2d: %02x ", i, outBuf[i]); 
 584         //              DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]); 
 587                 for(i 
= 0; i 
< k
; i
++) { 
 588                         receivedResponse
[i
] = outBuf
[i
]; 
 590         } // "end if correlation > 0"   (max/(arraylen(FrameSOF)/skip)) 
 591         return k
; // return the number of bytes demodulated 
 593 ///     DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2)); 
 597 static void BuildIdentifyRequest(void); 
 598 //----------------------------------------------------------------------------- 
 599 // Start to read an ISO 15693 tag. We send an identify request, then wait 
 600 // for the response. The response is not demodulated, just left in the buffer 
 601 // so that it can be downloaded to a PC and processed there. 
 602 //----------------------------------------------------------------------------- 
 603 void AcquireRawAdcSamplesIso15693(void) 
 605         uint8_t *dest 
=  mifare_get_bigbufptr(); 
 611         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
 612         BuildIdentifyRequest(); 
 614         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
 616         // Give the tags time to energize 
 617         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 620         // Now send the command 
 622         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
); 
 626                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 627                         AT91C_BASE_SSC
->SSC_THR 
= ToSend
[c
]; 
 629                         if(c 
== ToSendMax
+3) { 
 633                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 634                         volatile uint32_t r 
= AT91C_BASE_SSC
->SSC_RHR
; 
 640         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 645                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 646                         AT91C_BASE_SSC
->SSC_THR 
= 0x43; 
 648                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 650                         b 
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
; 
 652                         // The samples are correlations against I and Q versions of the 
 653                         // tone that the tag AM-modulates, so every other sample is I, 
 654                         // every other is Q. We just want power, so abs(I) + abs(Q) is 
 655                         // close to what we want. 
 670                                 dest
[c
++] = (uint8_t)r
; 
 685 void RecordRawAdcSamplesIso15693(void) 
 687         uint8_t *dest 
=  mifare_get_bigbufptr(); 
 693         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
 697         // Start from off (no field generated) 
 698         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 701         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
 705         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 710                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 711                         AT91C_BASE_SSC
->SSC_THR 
= 0x43; 
 713                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 715                         b 
= (int8_t)AT91C_BASE_SSC
->SSC_RHR
; 
 717                         // The samples are correlations against I and Q versions of the 
 718                         // tone that the tag AM-modulates, so every other sample is I, 
 719                         // every other is Q. We just want power, so abs(I) + abs(Q) is 
 720                         // close to what we want. 
 735                                 dest
[c
++] = (uint8_t)r
; 
 748         Dbprintf("fin record"); 
 752 // Initialize the proxmark as iso15k reader  
 753 // (this might produces glitches that confuse some tags 
 754 void Iso15693InitReader() { 
 760         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
 764         // Start from off (no field generated) 
 765         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 768         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
 771         // Give the tags time to energize 
 772         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
 781 /////////////////////////////////////////////////////////////////////// 
 782 // ISO 15693 Part 3 - Air Interface 
 783 // This section basicly contains transmission and receiving of bits 
 784 /////////////////////////////////////////////////////////////////////// 
 786 // Encode (into the ToSend buffers) an identify request, which is the first 
 787 // thing that you must send to a tag to get a response. 
 788 static void BuildIdentifyRequest(void) 
 793         // one sub-carrier, inventory, 1 slot, fast rate 
 794         // AFI is at bit 5 (1<<4) when doing an INVENTORY 
 795         cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1); 
 796         // inventory command code 
 805         CodeIso15693AsReader(cmd
, sizeof(cmd
)); 
 808 // uid is in transmission order (which is reverse of display order) 
 809 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber 
) 
 814         // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block 
 815         // followed by teh block data 
 816         // one sub-carrier, inventory, 1 slot, fast rate 
 817         cmd
[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit 
 818         // READ BLOCK command code 
 820         // UID may be optionally specified here 
 829         cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique) 
 830         // Block number to read 
 831         cmd
[10] = blockNumber
;//0x00; 
 833         crc 
= Crc(cmd
, 11); // the crc needs to be calculated over 12 bytes 
 834         cmd
[11] = crc 
& 0xff; 
 837         CodeIso15693AsReader(cmd
, sizeof(cmd
)); 
 840 // Now the VICC>VCD responses when we are simulating a tag 
 841  static void BuildInventoryResponse(void) 
 846         // one sub-carrier, inventory, 1 slot, fast rate 
 847         // AFI is at bit 5 (1<<4) when doing an INVENTORY 
 848         cmd
[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1); 
 861         cmd
[10] = crc 
& 0xff; 
 864         CodeIso15693AsReader(cmd
, sizeof(cmd
)); 
 867 // Universal Method for sending to and recv bytes from a tag 
 868 //      init ... should we initialize the reader? 
 869 //      speed ... 0 low speed, 1 hi speed  
 870 //      **recv will return you a pointer to the received data 
 871 //      If you do not need the answer use NULL for *recv[]  
 872 //      return: lenght of received data 
 873 int SendDataTag(uint8_t *send
, int sendlen
, int init
, int speed
, uint8_t **recv
) { 
 886         uint8_t *answer 
= (((uint8_t *)BigBuf
) + 3660); 
 887         if (recv
!=NULL
) memset(BigBuf 
+ 3660, 0, 100); 
 889         if (init
) Iso15693InitReader(); 
 892                 // low speed (1 out of 256) 
 893                 CodeIso15693AsReader256(send
, sendlen
); 
 895                 // high speed (1 out of 4) 
 896                 CodeIso15693AsReader(send
, sendlen
); 
 902         TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);   
 903         // Now wait for a response 
 907                 answerLen 
= GetIso15693AnswerFromTag(answer
, 100, &samples
, &elapsed
) ;  
 920 // -------------------------------------------------------------------- 
 922 // -------------------------------------------------------------------- 
 924 // Decodes a message from a tag and displays its metadata and content 
 925 #define DBD15STATLEN 48 
 926 void DbdecodeIso15693Answer(int len
, uint8_t *d
) { 
 927         char status
[DBD15STATLEN
+1]={0}; 
 932                         strncat(status
,"ProtExt ",DBD15STATLEN
); 
 935                         strncat(status
,"Error ",DBD15STATLEN
); 
 938                                         strncat(status
,"01:notSupp",DBD15STATLEN
); 
 941                                         strncat(status
,"02:notRecog",DBD15STATLEN
); 
 944                                         strncat(status
,"03:optNotSupp",DBD15STATLEN
); 
 947                                         strncat(status
,"0f:noInfo",DBD15STATLEN
); 
 950                                         strncat(status
,"10:dontExist",DBD15STATLEN
); 
 953                                         strncat(status
,"11:lockAgain",DBD15STATLEN
); 
 956                                         strncat(status
,"12:locked",DBD15STATLEN
); 
 959                                         strncat(status
,"13:progErr",DBD15STATLEN
); 
 962                                         strncat(status
,"14:lockErr",DBD15STATLEN
); 
 965                                         strncat(status
,"unknownErr",DBD15STATLEN
); 
 967                         strncat(status
," ",DBD15STATLEN
); 
 969                         strncat(status
,"NoErr ",DBD15STATLEN
); 
 973                 if ( (( crc 
& 0xff ) == d
[len
-2]) && (( crc 
>> 8 ) == d
[len
-1]) )  
 974                         strncat(status
,"CrcOK",DBD15STATLEN
); 
 976                         strncat(status
,"CrcFail!",DBD15STATLEN
);  
 978                 Dbprintf("%s",status
); 
 984 /////////////////////////////////////////////////////////////////////// 
 985 // Functions called via USB/Client 
 986 /////////////////////////////////////////////////////////////////////// 
 988 void SetDebugIso15693(uint32_t debug
) { 
 990         Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off"); 
 996 //----------------------------------------------------------------------------- 
 997 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector 
 998 // all demodulation performed in arm rather than host. - greg 
 999 //----------------------------------------------------------------------------- 
1000 void ReaderIso15693(uint32_t parameter
) 
1007 //DbpString(parameter); 
1009         //uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much) 
1010         uint8_t *answer1 
= (((uint8_t *)BigBuf
) + 3660); // 
1011         uint8_t *answer2 
= (((uint8_t *)BigBuf
) + 3760); 
1012         uint8_t *answer3 
= (((uint8_t *)BigBuf
) + 3860); 
1013         //uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960);                // where we hold the uid for hi15reader 
1014 //      int answerLen0 = 0; 
1021         memset(BigBuf 
+ 3660, 0, 300); 
1023         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1027         // Start from off (no field generated) 
1028         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1031         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1034         // Give the tags time to energize 
1035         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
); 
1048         // FIRST WE RUN AN INVENTORY TO GET THE TAG UID 
1049         // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME 
1050          uint8_t TagUID
[8] = {0, 0, 0, 0, 0, 0, 0, 0};          // where we hold the uid for hi15reader 
1052 //      BuildIdentifyRequest(); 
1053 //      //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait); 
1054 //      TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);  // No longer ToSendMax+3 
1055 //      // Now wait for a response 
1056 //      responseLen0 = GetIso15693AnswerFromTag(receivedAnswer0, 100, &samples, &elapsed) ; 
1057 //      if (responseLen0 >=12) // we should do a better check than this 
1059 //              // really we should check it is a valid mesg 
1060 //              // but for now just grab what we think is the uid 
1061 //              TagUID[0] = receivedAnswer0[2]; 
1062 //              TagUID[1] = receivedAnswer0[3]; 
1063 //              TagUID[2] = receivedAnswer0[4]; 
1064 //              TagUID[3] = receivedAnswer0[5]; 
1065 //              TagUID[4] = receivedAnswer0[6]; 
1066 //              TagUID[5] = receivedAnswer0[7]; 
1067 //              TagUID[6] = receivedAnswer0[8]; // IC Manufacturer code 
1068 //      DbpIntegers(TagUID[6],TagUID[5],TagUID[4]); 
1071         // Now send the IDENTIFY command 
1072         BuildIdentifyRequest(); 
1073         //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait); 
1074         TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);  // No longer ToSendMax+3 
1075         // Now wait for a response 
1076         answerLen1 
= GetIso15693AnswerFromTag(answer1
, 100, &samples
, &elapsed
) ; 
1078         if (answerLen1 
>=12) // we should do a better check than this 
1081                 TagUID
[0] = answer1
[2]; 
1082                 TagUID
[1] = answer1
[3]; 
1083                 TagUID
[2] = answer1
[4]; 
1084                 TagUID
[3] = answer1
[5]; 
1085                 TagUID
[4] = answer1
[6]; 
1086                 TagUID
[5] = answer1
[7]; 
1087                 TagUID
[6] = answer1
[8]; // IC Manufacturer code 
1088                 TagUID
[7] = answer1
[9]; // always E0 
1090                 // Now send the SELECT command 
1091                 // since the SELECT command is optional, we should not rely on it. 
1092 ////                            BuildSelectRequest(TagUID); 
1093 //              TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);  // No longer ToSendMax+3 
1094                 // Now wait for a response 
1095 ///             answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed); 
1097                 // Now send the MULTI READ command 
1098 //              BuildArbitraryRequest(*TagUID,parameter); 
1099 ///             BuildArbitraryCustomRequest(TagUID,parameter); 
1100 //              BuildReadBlockRequest(*TagUID,parameter); 
1101 //              BuildSysInfoRequest(*TagUID); 
1102                 //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait); 
1103 ///             TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);  // No longer ToSendMax+3 
1104                 // Now wait for a response 
1105 ///             answerLen3 = GetIso15693AnswerFromTag(answer3, 100, &samples, &elapsed) ; 
1109         Dbprintf("%d octets read from IDENTIFY request:", answerLen1
); 
1110         DbdecodeIso15693Answer(answerLen1
,answer1
); 
1111         Dbhexdump(answerLen1
,answer1
,true); 
1115                 //Dbprintf("UID = %*D",8,TagUID," "); 
1116                 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",TagUID
[7],TagUID
[6],TagUID
[5], 
1117                         TagUID
[4],TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]); 
1120         Dbprintf("%d octets read from SELECT request:", answerLen2
); 
1121         DbdecodeIso15693Answer(answerLen2
,answer2
); 
1122         Dbhexdump(answerLen2
,answer2
,true); 
1124         Dbprintf("%d octets read from XXX request:", answerLen3
); 
1125         DbdecodeIso15693Answer(answerLen3
,answer3
); 
1126         Dbhexdump(answerLen3
,answer3
,true); 
1130         if (answerLen1
>=12 && DEBUG
) { 
1132                 while (i
<32) {  // sanity check, assume max 32 pages 
1133                         BuildReadBlockRequest(TagUID
,i
); 
1134               TransmitTo15693Tag(ToSend
,ToSendMax
,&tsamples
, &wait
);   
1135          answerLen2 
= GetIso15693AnswerFromTag(answer2
, 100, &samples
, &elapsed
); 
1137                                 Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i
,answerLen2
); 
1138                                 DbdecodeIso15693Answer(answerLen2
,answer2
); 
1139                                 Dbhexdump(answerLen2
,answer2
,true); 
1140                                 if ( *((uint32_t*) answer2
) == 0x07160101 ) break; // exit on NoPageErr  
1147 //      for(i = 0; i < responseLen3; i++) { 
1148 //              itoa(str1,receivedAnswer3[i]); 
1149 //              strncat(str2,str1,8); 
1159 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands 
1160 // all demodulation performed in arm rather than host. - greg 
1161 void SimTagIso15693(uint32_t parameter
) 
1168         uint8_t *answer1 
= (((uint8_t *)BigBuf
) + 3660); // 
1172         memset(answer1
, 0, 100); 
1174         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1178         // Start from off (no field generated) 
1179         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1182         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1185         // Give the tags time to energize 
1186 //      FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);  // NO GOOD FOR SIM TAG!!!! 
1199         answerLen1 
= GetIso15693AnswerFromSniff(answer1
, 100, &samples
, &elapsed
) ; 
1201         if (answerLen1 
>=1) // we should do a better check than this 
1203                 // Build a suitable reponse to the reader INVENTORY cocmmand 
1204                 BuildInventoryResponse(); 
1205                 TransmitTo15693Reader(ToSend
,ToSendMax
, &tsamples
, &wait
); 
1208         Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1
, 
1209                 answer1
[0], answer1
[1], answer1
[2], 
1210                 answer1
[3], answer1
[4], answer1
[5], 
1211                 answer1
[6], answer1
[7], answer1
[8]); 
1220 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it 
1221 // (some manufactures offer a way to read the AFI, though) 
1222 void BruteforceIso15693Afi(uint32_t speed
)  
1226         int datalen
=0, recvlen
=0; 
1228         Iso15693InitReader(); 
1230         // first without AFI 
1231         // Tags should respond wihtout AFI and with AFI=0 even when AFI is active 
1233         data
[0]=ISO15_REQ_SUBCARRIER_SINGLE 
| ISO15_REQ_DATARATE_HIGH 
|  
1234                 ISO15_REQ_INVENTORY 
| ISO15_REQINV_SLOT1
; 
1235         data
[1]=ISO15_CMD_INVENTORY
; 
1236         data
[2]=0; // mask length 
1237         datalen
=AddCrc(data
,3); 
1238         recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
); 
1241                 Dbprintf("NoAFI UID=%s",sprintUID(NULL
,&recv
[2])); 
1246         data
[0]=ISO15_REQ_SUBCARRIER_SINGLE 
| ISO15_REQ_DATARATE_HIGH 
|  
1247                 ISO15_REQ_INVENTORY 
| ISO15_REQINV_AFI 
| ISO15_REQINV_SLOT1
; 
1248         data
[1]=ISO15_CMD_INVENTORY
; 
1250         data
[3]=0; // mask length 
1252         for (int i
=0;i
<256;i
++) { 
1254                 datalen
=AddCrc(data
,4); 
1255                 recvlen
=SendDataTag(data
,datalen
,0,speed
,&recv
); 
1258                         Dbprintf("AFI=%i UID=%s",i
,sprintUID(NULL
,&recv
[2])); 
1261         Dbprintf("AFI Bruteforcing done."); 
1265 // Allows to directly send commands to the tag via the client 
1266 void DirectTag15693Command(uint32_t datalen
,uint32_t speed
, uint32_t recv
, uint8_t data
[]) { 
1269         uint8_t *recvbuf
=(uint8_t *)BigBuf
; 
1274                 Dbhexdump(datalen
,data
,true); 
1277         recvlen
=SendDataTag(data
,datalen
,1,speed
,(recv
?&recvbuf
:NULL
)); 
1281     cmd_send(CMD_ACK
,recvlen
>48?48:recvlen
,0,0,recvbuf
,48); 
1286                         DbdecodeIso15693Answer(recvlen
,recvbuf
);  
1287                         Dbhexdump(recvlen
,recvbuf
,true); 
1296 // -------------------------------------------------------------------- 
1297 // -- Misc & deprecated functions 
1298 // -------------------------------------------------------------------- 
1302 // do not use; has a fix UID 
1303 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid) 
1308         // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block 
1309         // followed by teh block data 
1310         // one sub-carrier, inventory, 1 slot, fast rate 
1311         cmd[0] =  (1 << 5) | (1 << 1); // no SELECT bit 
1312         // System Information command code 
1314         // UID may be optionally specified here 
1323         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1325         crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes 
1326         cmd[10] = crc & 0xff; 
1329         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1333 // do not use; has a fix UID 
1334 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid) 
1339         // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block 
1340         // followed by teh block data 
1341         // one sub-carrier, inventory, 1 slot, fast rate 
1342         cmd[0] =  (1 << 5) | (1 << 1); // no SELECT bit 
1343         // READ Multi BLOCK command code 
1345         // UID may be optionally specified here 
1354         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1355         // First Block number to read 
1357         // Number of Blocks to read 
1358         cmd[11] = 0x2f; // read quite a few 
1360         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1361         cmd[12] = crc & 0xff; 
1364         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1367 // do not use; has a fix UID 
1368 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode) 
1373         // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block 
1374         // followed by teh block data 
1375         // one sub-carrier, inventory, 1 slot, fast rate 
1376         cmd[0] =   (1 << 5) | (1 << 1); // no SELECT bit 
1377         // READ BLOCK command code 
1379         // UID may be optionally specified here 
1388         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1394 //      cmd[13] = 0x00; //Now the CRC 
1395         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1396         cmd[12] = crc & 0xff; 
1399         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1402 // do not use; has a fix UID 
1403 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode) 
1408         // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block 
1409         // followed by teh block data 
1410         // one sub-carrier, inventory, 1 slot, fast rate 
1411         cmd[0] =   (1 << 5) | (1 << 1); // no SELECT bit 
1412         // READ BLOCK command code 
1414         // UID may be optionally specified here 
1423         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1425         cmd[10] = 0x05; // for custom codes this must be manufcturer code 
1429 //      cmd[13] = 0x00; //Now the CRC 
1430         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1431         cmd[12] = crc & 0xff; 
1434         CodeIso15693AsReader(cmd, sizeof(cmd));