1 //----------------------------------------------------------------------------- 
   3 // This code is licensed to you under the terms of the GNU GPL, version 2 or, 
   4 // at your option, any later version. See the LICENSE.txt file for the text of 
   6 //----------------------------------------------------------------------------- 
   7 // Low frequency T55xx commands 
   8 //----------------------------------------------------------------------------- 
  14 #include "proxmark3.h" 
  18 #include "cmdparser.h" 
  21 #include "cmdlft55xx.h" 
  25 #include "../common/crc.h" 
  26 #include "../common/iso14443crc.h" 
  29 #define CONFIGURATION_BLOCK 0x00 
  30 #define TRACE_BLOCK 0x01 
  31 #define REGULAR_READ_MODE_BLOCK 0xFF 
  33 // Default configuration 
  34 t55xx_conf_block_t config 
= { .modulation 
= DEMOD_ASK
, .inverted 
= FALSE
, .offset 
= 0x00, .block0 
= 0x00}; 
  36 int usage_t55xx_config(){ 
  37         PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>]"); 
  38         PrintAndLog("Options:"); 
  39         PrintAndLog("       h                        This help"); 
  40         PrintAndLog("       b <8|16|32|40|50|64|100|128>     Set bitrate"); 
  41         PrintAndLog("       d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa>  Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A"); 
  42         PrintAndLog("       i [1]                            Invert data signal, defaults to normal"); 
  43         PrintAndLog("       o [offset]                       Set offset, where data should start decode in bitstream"); 
  45         PrintAndLog("Examples:"); 
  46         PrintAndLog("      lf t55xx config d FSK          - FSK demodulation"); 
  47         PrintAndLog("      lf t55xx config d FSK i 1      - FSK demodulation, inverse data"); 
  48         PrintAndLog("      lf t55xx config d FSK i 1 o 3  - FSK demodulation, inverse data, offset=3,start from position 3 to decode data"); 
  52 int usage_t55xx_read(){ 
  53         PrintAndLog("Usage:  lf t55xx read [b <block>] [p <password>] <override_safety> <page1>"); 
  54         PrintAndLog("Options:"); 
  55         PrintAndLog("     b <block>    - block number to read. Between 0-7"); 
  56         PrintAndLog("     p <password> - OPTIONAL password (8 hex characters)"); 
  57         PrintAndLog("     o            - OPTIONAL override safety check"); 
  58         PrintAndLog("     1            - OPTIONAL read Page 1 instead of Page 0"); 
  59         PrintAndLog("     ****WARNING****"); 
  60         PrintAndLog("     Use of read with password on a tag not configured for a pwd"); 
  61         PrintAndLog("     can damage the tag"); 
  63         PrintAndLog("Examples:"); 
  64         PrintAndLog("      lf t55xx read b 0              - read data from block 0"); 
  65         PrintAndLog("      lf t55xx read b 0 p feedbeef   - read data from block 0 password feedbeef"); 
  66         PrintAndLog("      lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check"); 
  70 int usage_t55xx_write(){ 
  71         PrintAndLog("Usage:  lf t55xx wr [b <block>] [d <data>] [p <password>] [1]"); 
  72         PrintAndLog("Options:"); 
  73         PrintAndLog("     b <block>    - block number to write. Between 0-7"); 
  74         PrintAndLog("     d <data>     - 4 bytes of data to write (8 hex characters)"); 
  75         PrintAndLog("     p <password> - OPTIONAL password 4bytes (8 hex characters)"); 
  76         PrintAndLog("     1            - OPTIONAL write Page 1 instead of Page 0"); 
  78         PrintAndLog("Examples:"); 
  79         PrintAndLog("      lf t55xx wr b 3 d 11223344            - write 11223344 to block 3"); 
  80         PrintAndLog("      lf t55xx wr b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef"); 
  84 int usage_t55xx_trace() { 
  85         PrintAndLog("Usage:  lf t55xx trace [1]"); 
  86         PrintAndLog("Options:"); 
  87         PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag."); 
  89         PrintAndLog("Examples:"); 
  90         PrintAndLog("      lf t55xx trace"); 
  91         PrintAndLog("      lf t55xx trace 1"); 
  95 int usage_t55xx_info() { 
  96         PrintAndLog("Usage:  lf t55xx info [1]"); 
  97         PrintAndLog("Options:"); 
  98         PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag."); 
 100         PrintAndLog("Examples:"); 
 101         PrintAndLog("      lf t55xx info"); 
 102         PrintAndLog("      lf t55xx info 1"); 
 106 int usage_t55xx_dump(){ 
 107         PrintAndLog("Usage:  lf t55xx dump <password> [o]"); 
 108         PrintAndLog("Options:"); 
 109         PrintAndLog("     <password>  - OPTIONAL password 4bytes (8 hex symbols)"); 
 110         PrintAndLog("     o           - OPTIONAL override, force pwd read despite danger to card"); 
 112         PrintAndLog("Examples:"); 
 113         PrintAndLog("      lf t55xx dump"); 
 114         PrintAndLog("      lf t55xx dump feedbeef o"); 
 118 int usage_t55xx_detect(){ 
 119         PrintAndLog("Usage:  lf t55xx detect [1]"); 
 120         PrintAndLog("Options:"); 
 121         PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag."); 
 123         PrintAndLog("Examples:"); 
 124         PrintAndLog("      lf t55xx detect"); 
 125         PrintAndLog("      lf t55xx detect 1"); 
 129 int usage_t55xx_wakup(){ 
 130         PrintAndLog("Usage:  lf t55xx wakeup [h] p <password>"); 
 131         PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards."); 
 132         PrintAndLog("Options:"); 
 133         PrintAndLog("     h             - this help"); 
 134         PrintAndLog("     p <password>  - password 4bytes (8 hex symbols)"); 
 136         PrintAndLog("Examples:"); 
 137                 PrintAndLog("      lf t55xx wakeup p 11223344  - send wakeup password"); 
 141 static int CmdHelp(const char *Cmd
); 
 143 int CmdT55xxSetConfig(const char *Cmd
) { 
 146         char modulation
[5] = {0x00}; 
 149         uint8_t rates
[9] = {8,16,32,40,50,64,100,128,0}; 
 152         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) 
 154                 tmp 
= param_getchar(Cmd
, cmdp
); 
 159                         return usage_t55xx_config(); 
 161                         errors 
|= param_getdec(Cmd
, cmdp
+1, &bitRate
); 
 165                                         if (rates
[i
]==bitRate
) { 
 170                                 if (i
==9) errors 
= TRUE
; 
 175                         param_getstr(Cmd
, cmdp
+1, modulation
); 
 178                         if ( strcmp(modulation
, "FSK" ) == 0) { 
 179                                 config
.modulation 
= DEMOD_FSK
; 
 180                         } else if ( strcmp(modulation
, "FSK1" ) == 0) { 
 181                                 config
.modulation 
= DEMOD_FSK1
; 
 183                         } else if ( strcmp(modulation
, "FSK1a" ) == 0) { 
 184                                 config
.modulation 
= DEMOD_FSK1a
; 
 186                         } else if ( strcmp(modulation
, "FSK2" ) == 0) { 
 187                                 config
.modulation 
= DEMOD_FSK2
; 
 189                         } else if ( strcmp(modulation
, "FSK2a" ) == 0) { 
 190                                 config
.modulation 
= DEMOD_FSK2a
; 
 192                         } else if ( strcmp(modulation
, "ASK" ) == 0) { 
 193                                 config
.modulation 
= DEMOD_ASK
; 
 194                         } else if ( strcmp(modulation
, "NRZ" ) == 0) { 
 195                                 config
.modulation 
= DEMOD_NRZ
; 
 196                         } else if ( strcmp(modulation
, "PSK1" ) == 0) { 
 197                                 config
.modulation 
= DEMOD_PSK1
; 
 198                         } else if ( strcmp(modulation
, "PSK2" ) == 0) { 
 199                                 config
.modulation 
= DEMOD_PSK2
; 
 200                         } else if ( strcmp(modulation
, "PSK3" ) == 0) { 
 201                                 config
.modulation 
= DEMOD_PSK3
; 
 202                         } else if ( strcmp(modulation
, "BIa" ) == 0) { 
 203                                 config
.modulation 
= DEMOD_BIa
; 
 205                         } else if ( strcmp(modulation
, "BI" ) == 0) { 
 206                                 config
.modulation 
= DEMOD_BI
; 
 209                                 PrintAndLog("Unknown modulation '%s'", modulation
); 
 214                         config
.inverted 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 218                         errors 
|= param_getdec(Cmd
, cmdp
+1, &offset
); 
 220                                 config
.offset 
= offset
; 
 224                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 231         if (cmdp 
== 0) return printConfiguration( config 
); 
 234         if (errors
) return usage_t55xx_config(); 
 237         return printConfiguration ( config 
); 
 240 int T55xxReadBlock(uint8_t block
, bool page1
, bool usepwd
, bool override
, uint32_t password
){ 
 243                 // try reading the config block and verify that PWD bit is set before doing this! 
 245                         if ( !AquireData(0, CONFIGURATION_BLOCK
, false, 0 ) ) return 0; 
 246                         if ( !tryDetectModulation() ) { 
 247                                 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits."); 
 250                                 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password...");    
 255                         PrintAndLog("Safety Check Overriden - proceeding despite risk"); 
 259         if (!AquireData(page1
, block
, usepwd
, password
) )       return 0; 
 260         if (!DecodeT55xxBlock()) return 0; 
 263         sprintf(blk
,"%d", block
); 
 264         printT55xxBlock(blk
);    
 268 int CmdT55xxReadBlock(const char *Cmd
) { 
 269         uint8_t block 
= REGULAR_READ_MODE_BLOCK
; 
 270         uint32_t password 
= 0; //default to blank Block 7 
 272         bool override 
= false; 
 276         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
 277                 switch(param_getchar(Cmd
, cmdp
)) { 
 280                         return usage_t55xx_read(); 
 283                         errors 
|= param_getdec(Cmd
, cmdp
+1, &block
); 
 293                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 302                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 307         if (errors
) return usage_t55xx_read(); 
 309         if (block 
> 7 && block 
!= REGULAR_READ_MODE_BLOCK       
) { 
 310                 PrintAndLog("Block must be between 0 and 7"); 
 313         PrintAndLog("Reading Page %d:", page1
);  
 314         PrintAndLog("blk | hex data | binary"); 
 315         return T55xxReadBlock(block
, page1
, usepwd
, override
, password
); 
 318 bool DecodeT55xxBlock(){ 
 320         char buf
[30] = {0x00}; 
 323         uint8_t bitRate
[8] = {8,16,32,40,50,64,100,128}; 
 324         DemodBufferLen 
= 0x00; 
 326         switch( config
.modulation 
){ 
 328                         snprintf(cmdStr
, sizeof(buf
),"%d %d", bitRate
[config
.bitrate
], config
.inverted 
); 
 329                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 333                         snprintf(cmdStr
, sizeof(buf
),"%d %d 8 5", bitRate
[config
.bitrate
], config
.inverted 
); 
 334                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 338                         snprintf(cmdStr
, sizeof(buf
),"%d %d 10 8", bitRate
[config
.bitrate
], config
.inverted 
); 
 339                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 342                         snprintf(cmdStr
, sizeof(buf
),"%d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 343                         ans 
= ASKDemod(cmdStr
, FALSE
, FALSE
, 1); 
 346                         snprintf(cmdStr
, sizeof(buf
),"%d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 347                         ans 
= PSKDemod(cmdStr
, FALSE
); 
 349                 case DEMOD_PSK2
: //inverted won't affect this 
 350                 case DEMOD_PSK3
: //not fully implemented 
 351                         snprintf(cmdStr
, sizeof(buf
),"%d 0 1", bitRate
[config
.bitrate
] ); 
 352                         ans 
= PSKDemod(cmdStr
, FALSE
); 
 353                         psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 356                         snprintf(cmdStr
, sizeof(buf
),"%d %d 1", bitRate
[config
.bitrate
], config
.inverted 
); 
 357                         ans 
= NRZrawDemod(cmdStr
, FALSE
); 
 361                         snprintf(cmdStr
, sizeof(buf
),"0 %d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 362                         ans 
= ASKbiphaseDemod(cmdStr
, FALSE
); 
 370 int CmdT55xxDetect(const char *Cmd
){ 
 372         char cmdp 
= param_getchar(Cmd
, 0); 
 373         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') 
 374                 return usage_t55xx_detect(); 
 377                 if ( !AquireData(0, CONFIGURATION_BLOCK
, false, 0) ) 
 380         if ( !tryDetectModulation() ) 
 381                 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'"); 
 386 // detect configuration? 
 387 bool tryDetectModulation(){ 
 389         t55xx_conf_block_t tests
[15]; 
 391         uint8_t fc1 
= 0, fc2 
= 0, clk
=0; 
 393         if (GetFskClock("", FALSE
, FALSE
)){  
 394                 fskClocks(&fc1
, &fc2
, &clk
, FALSE
); 
 395                 if ( FSKrawDemod("0 0", FALSE
) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
)){ 
 396                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 397                         if (fc1
==8 && fc2 
== 5) 
 398                                 tests
[hits
].modulation 
= DEMOD_FSK1a
; 
 399                         else if (fc1
==10 && fc2 
== 8) 
 400                                 tests
[hits
].modulation 
= DEMOD_FSK2
; 
 401                         tests
[hits
].bitrate 
= bitRate
; 
 402                         tests
[hits
].inverted 
= FALSE
; 
 403                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 406                 if ( FSKrawDemod("0 1", FALSE
) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
)) { 
 407                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 408                         if (fc1 
== 8 && fc2 
== 5) 
 409                                 tests
[hits
].modulation 
= DEMOD_FSK1
; 
 410                         else if (fc1 
== 10 && fc2 
== 8) 
 411                                 tests
[hits
].modulation 
= DEMOD_FSK2a
; 
 413                         tests
[hits
].bitrate 
= bitRate
; 
 414                         tests
[hits
].inverted 
= TRUE
; 
 415                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 419                 clk 
= GetAskClock("", FALSE
, FALSE
); 
 421                         if ( ASKDemod("0 0 0", FALSE
, FALSE
, 1) && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
)) { 
 422                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 423                                 tests
[hits
].bitrate 
= bitRate
; 
 424                                 tests
[hits
].inverted 
= FALSE
; 
 425                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 428                         if ( ASKDemod("0 1 0", FALSE
, FALSE
, 1)  && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
)) { 
 429                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 430                                 tests
[hits
].bitrate 
= bitRate
; 
 431                                 tests
[hits
].inverted 
= TRUE
; 
 432                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 435                         if ( ASKbiphaseDemod("0 0 0 0", FALSE
) && test(DEMOD_BI
, &tests
[hits
].offset
, &bitRate
) ) { 
 436                                 tests
[hits
].modulation 
= DEMOD_BI
; 
 437                                 tests
[hits
].bitrate 
= bitRate
; 
 438                                 tests
[hits
].inverted 
= FALSE
; 
 439                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 442                         if ( ASKbiphaseDemod("0 0 1 0", FALSE
) && test(DEMOD_BIa
, &tests
[hits
].offset
, &bitRate
) ) { 
 443                                 tests
[hits
].modulation 
= DEMOD_BIa
; 
 444                                 tests
[hits
].bitrate 
= bitRate
; 
 445                                 tests
[hits
].inverted 
= TRUE
; 
 446                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 452                 clk 
= GetNrzClock("", FALSE
, FALSE
); 
 454                         if ( NRZrawDemod("0 0 1", FALSE
)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
)) { 
 455                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 456                                 tests
[hits
].bitrate 
= bitRate
; 
 457                                 tests
[hits
].inverted 
= FALSE
; 
 458                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 462                         if ( NRZrawDemod("0 1 1", FALSE
)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
)) { 
 463                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 464                                 tests
[hits
].bitrate 
= bitRate
; 
 465                                 tests
[hits
].inverted 
= TRUE
; 
 466                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 473                 clk 
= GetPskClock("", FALSE
, FALSE
); 
 475                         if ( PSKDemod("0 0 1", FALSE
) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
)) { 
 476                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 477                                 tests
[hits
].bitrate 
= bitRate
; 
 478                                 tests
[hits
].inverted 
= FALSE
; 
 479                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 482                         if ( PSKDemod("0 1 1", FALSE
) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
)) { 
 483                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 484                                 tests
[hits
].bitrate 
= bitRate
; 
 485                                 tests
[hits
].inverted 
= TRUE
; 
 486                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 489                         // PSK2 - needs a call to psk1TOpsk2. 
 490                         if ( PSKDemod("0 0 1", FALSE
)) { 
 491                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 492                                 if (test(DEMOD_PSK2
, &tests
[hits
].offset
, &bitRate
)){ 
 493                                         tests
[hits
].modulation 
= DEMOD_PSK2
; 
 494                                         tests
[hits
].bitrate 
= bitRate
; 
 495                                         tests
[hits
].inverted 
= FALSE
; 
 496                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 499                         } // inverse waves does not affect this demod 
 500                         // PSK3 - needs a call to psk1TOpsk2. 
 501                         if ( PSKDemod("0 0 1", FALSE
)) { 
 502                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 503                                 if (test(DEMOD_PSK3
, &tests
[hits
].offset
, &bitRate
)){ 
 504                                         tests
[hits
].modulation 
= DEMOD_PSK3
; 
 505                                         tests
[hits
].bitrate 
= bitRate
; 
 506                                         tests
[hits
].inverted 
= FALSE
; 
 507                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 510                         } // inverse waves does not affect this demod 
 514                 config
.modulation 
= tests
[0].modulation
; 
 515                 config
.bitrate 
= tests
[0].bitrate
; 
 516                 config
.inverted 
= tests
[0].inverted
; 
 517                 config
.offset 
= tests
[0].offset
; 
 518                 config
.block0 
= tests
[0].block0
; 
 519                 printConfiguration( config 
); 
 524                 PrintAndLog("Found [%d] possible matches for modulation.",hits
); 
 525                 for(int i
=0; i
<hits
; ++i
){ 
 526                         PrintAndLog("--[%d]---------------", i
+1); 
 527                         printConfiguration( tests
[i
] ); 
 533 bool testModulation(uint8_t mode
, uint8_t modread
){ 
 536                         if (modread 
> 3 && modread 
< 8) return TRUE
; 
 539                         if (modread 
== DEMOD_ASK
) return TRUE
; 
 542                         if (modread 
== DEMOD_PSK1
) return TRUE
; 
 545                         if (modread 
== DEMOD_PSK2
) return TRUE
; 
 548                         if (modread 
== DEMOD_PSK3
) return TRUE
; 
 551                         if (modread 
== DEMOD_NRZ
) return TRUE
; 
 554                         if (modread 
== DEMOD_BI
) return TRUE
; 
 557                         if (modread 
== DEMOD_BIa
) return TRUE
; 
 565 bool testBitRate(uint8_t readRate
, uint8_t mod
){ 
 566         uint8_t expected
[8] = {8, 16, 32, 40, 50, 64, 100, 128}; 
 574                         detRate 
= GetFskClock("",FALSE
, FALSE
);  
 575                         if (expected
[readRate
] == detRate
)  
 581                         detRate 
= GetAskClock("",FALSE
, FALSE
);  
 582                         if (expected
[readRate
] == detRate
)  
 588                         detRate 
= GetPskClock("",FALSE
, FALSE
);  
 589                         if (expected
[readRate
] == detRate
) 
 593                         detRate 
= GetNrzClock("",FALSE
, FALSE
);  
 594                         if (expected
[readRate
] == detRate
) 
 603 bool test(uint8_t mode
, uint8_t *offset
, int *fndBitRate
){ 
 605         if ( DemodBufferLen 
< 64 ) return FALSE
; 
 607         for (uint8_t idx 
= 0; idx 
< 64; idx
++){ 
 609                 if ( PackBits(si
, 32, DemodBuffer
) == 0x00 ) continue; 
 611                 uint8_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //master key 
 612                 uint8_t resv     
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //was 7 & +=7+3 //should be only 4 bits if extended mode 
 613                 // 2nibble must be zeroed. 
 614                 // moved test to here, since this gets most faults first. 
 615                 if ( resv 
> 0x00) continue; 
 617                 uint8_t xtRate   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3;     //extended mode part of rate 
 618                 int bitRate  
= PackBits(si
, 3, DemodBuffer
); si 
+= 3;     //bit rate 
 619                 if (bitRate 
> 7) continue; 
 620                 uint8_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;     //bit 15 extended mode 
 621                 uint8_t modread  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5+2+1;  
 622                 //uint8_t pskcr   = PackBits(si, 2, DemodBuffer); si += 2+1;  //could check psk cr 
 623                 uint8_t nml01    
= PackBits(si
, 1, DemodBuffer
); si 
+= 1+5;   //bit 24, 30, 31 could be tested for 0 if not extended mode 
 624                 uint8_t nml02    
= PackBits(si
, 2, DemodBuffer
); si 
+= 2; 
 627                 bool extMode 
=( (safer 
== 0x6 || safer 
== 0x9) && extend
) ? TRUE 
: FALSE
; 
 630                         if (nml01 
|| nml02 
|| xtRate
) continue; 
 633                 if (!testModulation(mode
, modread
)) continue; 
 634                 if (!testBitRate(bitRate
, mode
)) continue; 
 635                 *fndBitRate 
= bitRate
; 
 642 void printT55xxBlock(const char *blockNum
){ 
 644         uint8_t i 
= config
.offset
; 
 645         uint8_t endpos 
= 32 + i
; 
 646         uint32_t blockData 
= 0; 
 647         uint8_t bits
[64] = {0x00}; 
 649         if ( !DemodBufferLen
) return; 
 651         if ( endpos 
> DemodBufferLen
){ 
 652                 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i
, DemodBufferLen
-32); 
 656         for (; i 
< endpos
; ++i
) 
 657                 bits
[i 
- config
.offset
]=DemodBuffer
[i
]; 
 659         blockData 
= PackBits(0, 32, bits
); 
 660         PrintAndLog("  %s | %08X | %s", blockNum
, blockData
, sprint_bin(bits
,32)); 
 663 int special(const char *Cmd
) { 
 664         uint32_t blockData 
= 0; 
 665         uint8_t bits
[32] = {0x00}; 
 667         PrintAndLog("OFFSET | DATA       | BINARY"); 
 668         PrintAndLog("----------------------------------------------------"); 
 672                 for (i 
= 0; i 
< 32; ++i
) 
 673                         bits
[i
]=DemodBuffer
[j
+i
]; 
 675                 blockData 
= PackBits(0, 32, bits
); 
 677                 PrintAndLog("    %02d | 0x%08X | %s",j 
, blockData
, sprint_bin(bits
,32));        
 682 int printConfiguration( t55xx_conf_block_t b
){ 
 683         PrintAndLog("Modulation : %s", GetSelectedModulationStr(b
.modulation
) ); 
 684         PrintAndLog("Bit Rate   : %s", GetBitRateStr(b
.bitrate
) ); 
 685         PrintAndLog("Inverted   : %s", (b
.inverted
) ? "Yes" : "No" ); 
 686         PrintAndLog("Offset     : %d", b
.offset
); 
 687         PrintAndLog("Block0     : 0x%08X", b
.block0
); 
 692 int CmdT55xxWakeUp(const char *Cmd
) { 
 693         uint32_t password 
= 0; 
 696         while(param_getchar(Cmd
, cmdp
) != 0x00) { 
 697                 switch(param_getchar(Cmd
, cmdp
)) { 
 700                         return usage_t55xx_wakup(); 
 703                         password 
= param_get32ex(Cmd
, cmdp
+1, 0xFFFFFFFF, 16); 
 708                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 713         if (errors
) return usage_t55xx_wakup(); 
 715         UsbCommand c 
= {CMD_T55XX_WAKEUP
, {password
, 0, 0}}; 
 716         clearCommandBuffer(); 
 718         PrintAndLog("Wake up command sent. Try read now"); 
 722 int CmdT55xxWriteBlock(const char *Cmd
) { 
 723         uint8_t block 
= 0xFF; //default to invalid block 
 724         uint32_t data 
= 0xFFFFFFFF; //default to blank Block  
 725         uint32_t password 
= 0xFFFFFFFF; //default to blank Block 7 
 728         bool gotdata 
= false; 
 731         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
 732                 switch(param_getchar(Cmd
, cmdp
)) { 
 735                         return usage_t55xx_write(); 
 738                         errors 
|= param_getdec(Cmd
, cmdp
+1, &block
); 
 743                         data 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 749                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 758                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 763         if (errors 
|| !gotdata
) return usage_t55xx_write(); 
 766                 PrintAndLog("Block number must be between 0 and 7"); 
 770         UsbCommand c 
= {CMD_T55XX_WRITE_BLOCK
, {data
, block
, 0}}; 
 772         c
.d
.asBytes
[0] = (page1
) ? 0x2 : 0;  
 774         PrintAndLog("Writing to page: %d  block: %d  data : 0x%08X", page1
, block
, data
); 
 779                 c
.d
.asBytes
[0] |= 0x1;  
 780                 PrintAndLog("pwd   : 0x%08X", password
); 
 782         clearCommandBuffer(); 
 784         if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)){ 
 785                 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)"); 
 791 int CmdT55xxReadTrace(const char *Cmd
) { 
 792         char cmdp 
= param_getchar(Cmd
, 0); 
 793         bool pwdmode 
= false; 
 794         uint32_t password 
= 0; 
 795         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H')  
 796                 return usage_t55xx_trace(); 
 799                 if ( !AquireData( TRACE_BLOCK
, REGULAR_READ_MODE_BLOCK
, pwdmode
, password 
) ) 
 802         if (!DecodeT55xxBlock()) return 0; 
 804         if ( !DemodBufferLen
) return 0; 
 806         RepaintGraphWindow(); 
 808         if (config
.offset 
> 5)  
 810         uint8_t si 
= config
.offset
+repeat
; 
 811         uint32_t bl1     
= PackBits(si
, 32, DemodBuffer
); 
 812         uint32_t bl2     
= PackBits(si
+32, 32, DemodBuffer
); 
 814         uint32_t acl     
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
 815         uint32_t mfc     
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
 816         uint32_t cid     
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
 817         uint32_t icr     
= PackBits(si
, 3,  DemodBuffer
); si 
+= 3; 
 818         uint32_t year    
= PackBits(si
, 4,  DemodBuffer
); si 
+= 4; 
 819         uint32_t quarter 
= PackBits(si
, 2,  DemodBuffer
); si 
+= 2; 
 820         uint32_t lotid   
= PackBits(si
, 14, DemodBuffer
); si 
+= 14; 
 821         uint32_t wafer   
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
 822         uint32_t dw      
= PackBits(si
, 15, DemodBuffer
);  
 824         time_t t 
= time(NULL
); 
 825         struct tm tm 
= *localtime(&t
); 
 826         if ( year 
> tm
.tm_year
-110) 
 832                 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. "); 
 837         PrintAndLog("-- T55xx Trace Information ----------------------------------"); 
 838         PrintAndLog("-------------------------------------------------------------"); 
 839         PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1)  : 0x%02X (%d)", acl
, acl
); 
 840         PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6)    : 0x%02X (%d) - %s", mfc
, mfc
, getTagInfo(mfc
)); 
 841         PrintAndLog(" CID                                     : 0x%02X (%d) - %s", cid
, cid
, GetModelStrFromCID(cid
)); 
 842         PrintAndLog(" ICR IC Revision                         : %d",icr 
); 
 843         PrintAndLog(" Manufactured"); 
 844         PrintAndLog("     Year/Quarter : %d/%d",year
, quarter
); 
 845         PrintAndLog("     Lot ID       : %d", lotid 
); 
 846         PrintAndLog("     Wafer number : %d", wafer
); 
 847         PrintAndLog("     Die Number   : %d", dw
); 
 848         PrintAndLog("-------------------------------------------------------------"); 
 849         PrintAndLog(" Raw Data - Page 1"); 
 850         PrintAndLog("     Block 1  : 0x%08X  %s", bl1
, sprint_bin(DemodBuffer
+config
.offset
+repeat
,32) ); 
 851         PrintAndLog("     Block 2  : 0x%08X  %s", bl2
, sprint_bin(DemodBuffer
+config
.offset
+repeat
+32,32) ); 
 852         PrintAndLog("-------------------------------------------------------------"); 
 857                 1-8             ACL Allocation class (ISO/IEC 15963-1)  0xE0  
 858                 9-16    MFC Manufacturer ID (ISO/IEC 7816-6)    0x15 Atmel Corporation 
 859                 17-21   CID                                                                             0x1 = Atmel ATA5577M1  0x2 = Atmel ATA5577M2  
 860                 22-24   ICR IC revision 
 861                 25-28   YEAR (BCD encoded)                                              9 (= 2009) 
 862                 29-30   QUARTER                                                                 1,2,3,4  
 868                 18-32   DW,  die number sequential 
 874 int CmdT55xxInfo(const char *Cmd
){ 
 876                 Page 0 Block 0 Configuration data. 
 880         bool pwdmode 
= false; 
 881         uint32_t password 
= 0; 
 882         char cmdp 
= param_getchar(Cmd
, 0); 
 884         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') 
 885                 return usage_t55xx_info(); 
 888                 if ( !AquireData( 0, CONFIGURATION_BLOCK
, pwdmode
, password 
) ) 
 891         if (!DecodeT55xxBlock()) return 1; 
 893         if ( DemodBufferLen 
< 32) return 1; 
 895         uint8_t si 
= config
.offset
; 
 896         uint32_t bl0      
= PackBits(si
, 32, DemodBuffer
); 
 898         uint32_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;       
 899         uint32_t resv     
= PackBits(si
, 7, DemodBuffer
); si 
+= 7; 
 900         uint32_t dbr      
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 901         uint32_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 902         uint32_t datamod  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5; 
 903         uint32_t pskcf    
= PackBits(si
, 2, DemodBuffer
); si 
+= 2; 
 904         uint32_t aor      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 905         uint32_t otp      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 906         uint32_t maxblk   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 907         uint32_t pwd      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 908         uint32_t sst      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 909         uint32_t fw       
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 910         uint32_t inv      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 911         uint32_t por      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 914         PrintAndLog("-- T55xx Configuration & Tag Information --------------------"); 
 915         PrintAndLog("-------------------------------------------------------------"); 
 916         PrintAndLog(" Safer key                 : %s", GetSaferStr(safer
)); 
 917         PrintAndLog(" reserved                  : %d", resv
); 
 918         PrintAndLog(" Data bit rate             : %s", GetBitRateStr(dbr
)); 
 919         PrintAndLog(" eXtended mode             : %s", (extend
) ? "Yes - Warning":"No"); 
 920         PrintAndLog(" Modulation                : %s", GetModulationStr(datamod
)); 
 921         PrintAndLog(" PSK clock frequency       : %d", pskcf
); 
 922         PrintAndLog(" AOR - Answer on Request   : %s", (aor
) ? "Yes":"No"); 
 923         PrintAndLog(" OTP - One Time Pad        : %s", (otp
) ? "Yes - Warning":"No" ); 
 924         PrintAndLog(" Max block                 : %d", maxblk
); 
 925         PrintAndLog(" Password mode             : %s", (pwd
) ? "Yes":"No"); 
 926         PrintAndLog(" Sequence Start Terminator : %s", (sst
) ? "Yes":"No"); 
 927         PrintAndLog(" Fast Write                : %s", (fw
)  ? "Yes":"No"); 
 928         PrintAndLog(" Inverse data              : %s", (inv
) ? "Yes":"No"); 
 929         PrintAndLog(" POR-Delay                 : %s", (por
) ? "Yes":"No"); 
 930         PrintAndLog("-------------------------------------------------------------"); 
 931         PrintAndLog(" Raw Data - Page 0"); 
 932         PrintAndLog("     Block 0  : 0x%08X  %s", bl0
, sprint_bin(DemodBuffer
+config
.offset
,32) ); 
 933         PrintAndLog("-------------------------------------------------------------"); 
 938 int CmdT55xxDump(const char *Cmd
){ 
 940         uint32_t password 
= 0; 
 941         char cmdp 
= param_getchar(Cmd
, 0); 
 942         bool override 
= false; 
 943         if ( cmdp 
== 'h' || cmdp 
== 'H') return usage_t55xx_dump(); 
 945         bool usepwd 
= ( strlen(Cmd
) > 0);        
 947                 password 
= param_get32ex(Cmd
, 0, 0, 16); 
 948                 if (param_getchar(Cmd
, 1) =='o' ) 
 952         PrintAndLog("Reading Page 0:");  
 953         PrintAndLog("blk | hex data | binary"); 
 954         for ( uint8_t i 
= 0; i 
<8; ++i
){ 
 955                 T55xxReadBlock(i
, 0, usepwd
, override
, password
); 
 956                 /*memset(s,0,sizeof(s)); 
 959                                 sprintf(s,"b %d p %02x%02x%02x%02x o", i, pwd[0],pwd[1],pwd[2],pwd[3]);                          
 961                                 sprintf(s,"b %d p %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);                            
 964                         sprintf(s,"b %d", i); 
 966                 CmdT55xxReadBlock(s);*/ 
 968         PrintAndLog("Reading Page 1:");  
 969         PrintAndLog("blk | hex data | binary"); 
 970         for ( uint8_t   i 
= 0; i
<4; i
++){ 
 971                 T55xxReadBlock(i
, 1, usepwd
, override
, password
);                
 976 int AquireData( uint8_t page
, uint8_t block
, bool pwdmode
, uint32_t password 
){ 
 978         uint8_t arg0 
= (page
<<1) | pwdmode
; 
 979         UsbCommand c 
= {CMD_T55XX_READ_BLOCK
, {arg0
, block
, password
}}; 
 981         clearCommandBuffer(); 
 983         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
 984                 PrintAndLog("command execution time out"); 
 989         GetFromBigBuf(got
,sizeof(got
),0); 
 990         WaitForResponse(CMD_ACK
,NULL
); 
 991         setGraphBuf(got
, sizeof(got
)); 
 995 char * GetBitRateStr(uint32_t id
){ 
1001                         snprintf(retStr
,sizeof(buf
),"%d - RF/8",id
); 
1004                         snprintf(retStr
,sizeof(buf
),"%d - RF/16",id
); 
1007                         snprintf(retStr
,sizeof(buf
),"%d - RF/32",id
); 
1010                         snprintf(retStr
,sizeof(buf
),"%d - RF/40",id
); 
1013                         snprintf(retStr
,sizeof(buf
),"%d - RF/50",id
); 
1016                         snprintf(retStr
,sizeof(buf
),"%d - RF/64",id
); 
1019                         snprintf(retStr
,sizeof(buf
),"%d - RF/100",id
); 
1022                         snprintf(retStr
,sizeof(buf
),"%d - RF/128",id
); 
1025                         snprintf(retStr
,sizeof(buf
),"%d - (Unknown)",id
); 
1032 char * GetSaferStr(uint32_t id
){ 
1033         static char buf
[40]; 
1036         snprintf(retStr
,sizeof(buf
),"%d",id
); 
1038                 snprintf(retStr
,sizeof(buf
),"%d - passwd",id
); 
1041                 snprintf(retStr
,sizeof(buf
),"%d - testmode",id
); 
1047 char * GetModulationStr( uint32_t id
){ 
1048         static char buf
[60]; 
1053                         snprintf(retStr
,sizeof(buf
),"%d - DIRECT (ASK/NRZ)",id
); 
1056                         snprintf(retStr
,sizeof(buf
),"%d - PSK 1 phase change when input changes",id
); 
1059                         snprintf(retStr
,sizeof(buf
),"%d - PSK 2 phase change on bitclk if input high",id
); 
1062                         snprintf(retStr
,sizeof(buf
),"%d - PSK 3 phase change on rising edge of input",id
); 
1065                         snprintf(retStr
,sizeof(buf
),"%d - FSK 1 RF/8  RF/5",id
); 
1068                         snprintf(retStr
,sizeof(buf
),"%d - FSK 2 RF/8  RF/10",id
); 
1071                         snprintf(retStr
,sizeof(buf
),"%d - FSK 1a RF/5  RF/8",id
); 
1074                         snprintf(retStr
,sizeof(buf
),"%d - FSK 2a RF/10  RF/8",id
); 
1077                         snprintf(retStr
,sizeof(buf
),"%d - Manchester",id
); 
1080                         snprintf(retStr
,sizeof(buf
),"%d - Biphase",id
); 
1083                         snprintf(retStr
,sizeof(buf
),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id
); 
1086                         snprintf(retStr
,sizeof(buf
),"%d - Reserved",id
); 
1089                         snprintf(retStr
,sizeof(buf
),"0x%02X (Unknown)",id
); 
1095 char * GetModelStrFromCID(uint32_t cid
){ 
1097         static char buf
[10]; 
1100         if (cid 
== 1) snprintf(retStr
, sizeof(buf
),"ATA5577M1"); 
1101         if (cid 
== 2) snprintf(retStr
, sizeof(buf
),"ATA5577M2");         
1105 char * GetSelectedModulationStr( uint8_t id
){ 
1107         static char buf
[20]; 
1112                         snprintf(retStr
,sizeof(buf
),"FSK"); 
1115                         snprintf(retStr
,sizeof(buf
),"FSK1"); 
1118                         snprintf(retStr
,sizeof(buf
),"FSK1a"); 
1121                         snprintf(retStr
,sizeof(buf
),"FSK2"); 
1124                         snprintf(retStr
,sizeof(buf
),"FSK2a"); 
1127                         snprintf(retStr
,sizeof(buf
),"ASK"); 
1130                         snprintf(retStr
,sizeof(buf
),"DIRECT/NRZ"); 
1133                         snprintf(retStr
,sizeof(buf
),"PSK1"); 
1136                         snprintf(retStr
,sizeof(buf
),"PSK2"); 
1139                         snprintf(retStr
,sizeof(buf
),"PSK3"); 
1142                         snprintf(retStr
,sizeof(buf
),"BIPHASE"); 
1145                         snprintf(retStr
,sizeof(buf
),"BIPHASEa - (CDP)"); 
1148                         snprintf(retStr
,sizeof(buf
),"(Unknown)"); 
1154 uint32_t PackBits(uint8_t start
, uint8_t len
, uint8_t* bits
){ 
1159         if (len 
> 32) return 0; 
1162         for (; j 
>= 0; --j
, ++i
) 
1163                 tmp     
|= bits
[i
] << j
; 
1168 int CmdResetRead(const char *Cmd
) { 
1169         UsbCommand c 
= {CMD_T55XX_RESET_READ
, {0,0,0}}; 
1171         clearCommandBuffer(); 
1173         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
1174                 PrintAndLog("command execution time out"); 
1179         GetFromBigBuf(got
,sizeof(got
),0); 
1180         WaitForResponse(CMD_ACK
,NULL
); 
1181         setGraphBuf(got
, sizeof(got
)); 
1185 static command_t CommandTable
[] = 
1187   {"help",     CmdHelp
,           1, "This help"}, 
1188   {"config",   CmdT55xxSetConfig
, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"}, 
1189   {"detect",   CmdT55xxDetect
,    0, "[1] Try detecting the tag modulation from reading the configuration block."}, 
1190   {"read",     CmdT55xxReadBlock
, 0, "b <block> p [password] [o] [1] -- Read T55xx block data (page 0) [optional password]"}, 
1191   {"resetread",CmdResetRead
,      0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"}, 
1192   {"write",    CmdT55xxWriteBlock
,0, "b <block> d <data> p [password] [1] -- Write T55xx block data (page 0) [optional password]"}, 
1193   {"trace",    CmdT55xxReadTrace
, 0, "[1] Show T55xx traceability data (page 1/ blk 0-1)"}, 
1194   {"info",     CmdT55xxInfo
,      0, "[1] Show T55xx configuration data (page 0/ blk 0)"}, 
1195   {"dump",     CmdT55xxDump
,      0, "[password] [o] Dump T55xx card block 0-7. [optional password]"}, 
1196   {"special",  special
,           0, "Show block changes with 64 different offsets"}, 
1197   {"wakeup",   CmdT55xxWakeUp
,    0, "Send AOR wakeup command"}, 
1198   {NULL
, NULL
, 0, NULL
} 
1201 int CmdLFT55XX(const char *Cmd
) 
1203   CmdsParse(CommandTable
, Cmd
); 
1207 int CmdHelp(const char *Cmd
) 
1209   CmdsHelp(CommandTable
);