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 //----------------------------------------------------------------------------- 
  10 #include "cmdlft55xx.h" 
  21 #include "cmdparser.h" 
  26 #include "cmdhf14a.h" //for getTagInfo 
  27 #include "protocols.h" 
  29 #define T55x7_CONFIGURATION_BLOCK 0x00 
  30 #define T55x7_PAGE0 0x00 
  31 #define T55x7_PAGE1 0x01 
  32 //#define T55x7_PWD     0x00000010 
  33 #define REGULAR_READ_MODE_BLOCK 0xFF 
  35 // Default configuration 
  36 t55xx_conf_block_t config 
= { .modulation 
= DEMOD_ASK
, .inverted 
= false, .offset 
= 0x00, .block0 
= 0x00, .Q5 
= false }; 
  38 t55xx_conf_block_t 
Get_t55xx_Config(){ 
  41 void Set_t55xx_Config(t55xx_conf_block_t conf
){ 
  45 int usage_t55xx_config(){ 
  46         PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>] [Q5]"); 
  47         PrintAndLog("Options:"); 
  48         PrintAndLog("       h                        This help"); 
  49         PrintAndLog("       b <8|16|32|40|50|64|100|128>  Set bitrate"); 
  50         PrintAndLog("       d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa>  Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A"); 
  51         PrintAndLog("       i [1]                         Invert data signal, defaults to normal"); 
  52         PrintAndLog("       o [offset]                    Set offset, where data should start decode in bitstream"); 
  53         PrintAndLog("       Q5                            Set as Q5(T5555) chip instead of T55x7"); 
  54         PrintAndLog("       ST                            Set Sequence Terminator on"); 
  56         PrintAndLog("Examples:"); 
  57         PrintAndLog("      lf t55xx config d FSK          - FSK demodulation"); 
  58         PrintAndLog("      lf t55xx config d FSK i 1      - FSK demodulation, inverse data"); 
  59         PrintAndLog("      lf t55xx config d FSK i 1 o 3  - FSK demodulation, inverse data, offset=3,start from position 3 to decode data"); 
  63 int usage_t55xx_read(){ 
  64         PrintAndLog("Usage:  lf t55xx read [b <block>] [p <password>] <override_safety> <page1>"); 
  65         PrintAndLog("Options:"); 
  66         PrintAndLog("     b <block>    - block number to read. Between 0-7"); 
  67         PrintAndLog("     p <password> - OPTIONAL password (8 hex characters)"); 
  68         PrintAndLog("     o            - OPTIONAL override safety check"); 
  69         PrintAndLog("     1            - OPTIONAL read Page 1 instead of Page 0"); 
  70         PrintAndLog("     ****WARNING****"); 
  71         PrintAndLog("     Use of read with password on a tag not configured for a pwd"); 
  72         PrintAndLog("     can damage the tag"); 
  74         PrintAndLog("Examples:"); 
  75         PrintAndLog("      lf t55xx read b 0              - read data from block 0"); 
  76         PrintAndLog("      lf t55xx read b 0 p feedbeef   - read data from block 0 password feedbeef"); 
  77         PrintAndLog("      lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check"); 
  81 int usage_t55xx_write(){ 
  82         PrintAndLog("Usage:  lf t55xx write [b <block>] [d <data>] [p <password>] [1] [t]"); 
  83         PrintAndLog("Options:"); 
  84         PrintAndLog("     b <block>    - block number to write. Between 0-7"); 
  85         PrintAndLog("     d <data>     - 4 bytes of data to write (8 hex characters)"); 
  86         PrintAndLog("     p <password> - OPTIONAL password 4bytes (8 hex characters)"); 
  87         PrintAndLog("     1            - OPTIONAL write Page 1 instead of Page 0"); 
  88         PrintAndLog("     t            - OPTIONAL test mode write - ****DANGER****");    
  90         PrintAndLog("Examples:"); 
  91         PrintAndLog("      lf t55xx write b 3 d 11223344            - write 11223344 to block 3"); 
  92         PrintAndLog("      lf t55xx write b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef"); 
  96 int usage_t55xx_trace() { 
  97         PrintAndLog("Usage:  lf t55xx trace [1]"); 
  98         PrintAndLog("Options:"); 
  99         PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag."); 
 101         PrintAndLog("Examples:"); 
 102         PrintAndLog("      lf t55xx trace"); 
 103         PrintAndLog("      lf t55xx trace 1"); 
 107 int usage_t55xx_info() { 
 108         PrintAndLog("Usage:  lf t55xx info [1]"); 
 109         PrintAndLog("Options:"); 
 110         PrintAndLog("     [graph buffer data]  - if set, use Graphbuffer otherwise read data from tag."); 
 112         PrintAndLog("Examples:"); 
 113         PrintAndLog("      lf t55xx info"); 
 114         PrintAndLog("      lf t55xx info 1"); 
 118 int usage_t55xx_dump(){ 
 119         PrintAndLog("Usage:  lf t55xx dump <password> [o]"); 
 120         PrintAndLog("Options:"); 
 121         PrintAndLog("     <password>  - OPTIONAL password 4bytes (8 hex symbols)"); 
 122         PrintAndLog("     o           - OPTIONAL override, force pwd read despite danger to card"); 
 124         PrintAndLog("Examples:"); 
 125         PrintAndLog("      lf t55xx dump"); 
 126         PrintAndLog("      lf t55xx dump feedbeef o"); 
 130 int usage_t55xx_detect(){ 
 131         PrintAndLog("Usage:  lf t55xx detect [1] [p <password>]"); 
 132         PrintAndLog("Options:"); 
 133         PrintAndLog("     1             - if set, use Graphbuffer otherwise read data from tag."); 
 134         PrintAndLog("     p <password>  - OPTIONAL password (8 hex characters)"); 
 136         PrintAndLog("Examples:"); 
 137         PrintAndLog("      lf t55xx detect"); 
 138         PrintAndLog("      lf t55xx detect 1"); 
 139         PrintAndLog("      lf t55xx detect p 11223344"); 
 143 int usage_t55xx_detectP1(){ 
 144         PrintAndLog("Command: Detect Page 1 of a t55xx chip"); 
 145         PrintAndLog("Usage:  lf t55xx p1detect [1] [p <password>]"); 
 146         PrintAndLog("Options:"); 
 147         PrintAndLog("     1             - if set, use Graphbuffer otherwise read data from tag."); 
 148         PrintAndLog("     p <password>  - OPTIONAL password (8 hex characters)"); 
 150         PrintAndLog("Examples:"); 
 151         PrintAndLog("      lf t55xx p1detect"); 
 152         PrintAndLog("      lf t55xx p1detect 1"); 
 153         PrintAndLog("      lf t55xx p1detect p 11223344"); 
 157 int usage_t55xx_wakup(){ 
 158         PrintAndLog("Usage:  lf t55xx wakeup [h] <password>"); 
 159         PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards."); 
 160         PrintAndLog("Options:"); 
 161         PrintAndLog("     h             - this help"); 
 162         PrintAndLog("     <password>  - [required] password 4bytes (8 hex symbols)"); 
 164         PrintAndLog("Examples:"); 
 165                 PrintAndLog("      lf t55xx wakeup 11223344  - send wakeup password"); 
 168 int usage_t55xx_bruteforce(){ 
 169         PrintAndLog("This command uses A) bruteforce to scan a number range"); 
 170         PrintAndLog("                  B) a dictionary attack"); 
 171         PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]"); 
 172         PrintAndLog("       password must be 4 bytes (8 hex symbols)"); 
 173         PrintAndLog("Options:"); 
 174         PrintAndLog("     h           - this help"); 
 175         PrintAndLog("     <start_pwd> - 4 byte hex value to start pwd search at"); 
 176         PrintAndLog("     <end_pwd>   - 4 byte hex value to end pwd search at"); 
 177         PrintAndLog("     i <*.dic>   - loads a default keys dictionary file <*.dic>"); 
 179         PrintAndLog("Examples:"); 
 180         PrintAndLog("       lf t55xx bruteforce aaaaaaaa bbbbbbbb"); 
 181         PrintAndLog("       lf t55xx bruteforce i default_pwd.dic"); 
 185 int usage_t55xx_wipe(){ 
 186         PrintAndLog("Usage:  lf t55xx wipe [h] [Q5]"); 
 187         PrintAndLog("This commands wipes a tag, fills blocks 1-7 with zeros and a default configuration block"); 
 188         PrintAndLog("Options:"); 
 189         PrintAndLog("     h   - this help"); 
 190         PrintAndLog("     Q5  - indicates to use the T5555 (Q5) default configuration block"); 
 192         PrintAndLog("Examples:"); 
 193         PrintAndLog("      lf t55xx wipe    -  wipes a t55x7 tag,    config block 0x000880E0"); 
 194         PrintAndLog("      lf t55xx wipe Q5 -  wipes a t5555 Q5 tag, config block 0x6001F004"); 
 199 static int CmdHelp(const char *Cmd
); 
 201 void printT5xxHeader(uint8_t page
){ 
 202         PrintAndLog("Reading Page %d:", page
); 
 203         PrintAndLog("blk | hex data | binary"); 
 204         PrintAndLog("----+----------+---------------------------------");        
 207 int CmdT55xxSetConfig(const char *Cmd
) { 
 210         char modulation
[6] = {0x00}; 
 213         uint8_t rates
[9] = {8,16,32,40,50,64,100,128,0}; 
 216         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) 
 218                 tmp 
= param_getchar(Cmd
, cmdp
); 
 223                         return usage_t55xx_config(); 
 225                         errors 
|= param_getdec(Cmd
, cmdp
+1, &bitRate
); 
 229                                         if (rates
[i
]==bitRate
) { 
 234                                 if (i
==9) errors 
= true; 
 239                         param_getstr(Cmd
, cmdp
+1, modulation
, sizeof(modulation
)); 
 242                         if ( strcmp(modulation
, "FSK" ) == 0) { 
 243                                 config
.modulation 
= DEMOD_FSK
; 
 244                         } else if ( strcmp(modulation
, "FSK1" ) == 0) { 
 245                                 config
.modulation 
= DEMOD_FSK1
; 
 247                         } else if ( strcmp(modulation
, "FSK1a" ) == 0) { 
 248                                 config
.modulation 
= DEMOD_FSK1a
; 
 250                         } else if ( strcmp(modulation
, "FSK2" ) == 0) { 
 251                                 config
.modulation 
= DEMOD_FSK2
; 
 253                         } else if ( strcmp(modulation
, "FSK2a" ) == 0) { 
 254                                 config
.modulation 
= DEMOD_FSK2a
; 
 256                         } else if ( strcmp(modulation
, "ASK" ) == 0) { 
 257                                 config
.modulation 
= DEMOD_ASK
; 
 258                         } else if ( strcmp(modulation
, "NRZ" ) == 0) { 
 259                                 config
.modulation 
= DEMOD_NRZ
; 
 260                         } else if ( strcmp(modulation
, "PSK1" ) == 0) { 
 261                                 config
.modulation 
= DEMOD_PSK1
; 
 262                         } else if ( strcmp(modulation
, "PSK2" ) == 0) { 
 263                                 config
.modulation 
= DEMOD_PSK2
; 
 264                         } else if ( strcmp(modulation
, "PSK3" ) == 0) { 
 265                                 config
.modulation 
= DEMOD_PSK3
; 
 266                         } else if ( strcmp(modulation
, "BIa" ) == 0) { 
 267                                 config
.modulation 
= DEMOD_BIa
; 
 269                         } else if ( strcmp(modulation
, "BI" ) == 0) { 
 270                                 config
.modulation 
= DEMOD_BI
; 
 273                                 PrintAndLog("Unknown modulation '%s'", modulation
); 
 278                         config
.inverted 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 282                         errors 
|= param_getdec(Cmd
, cmdp
+1, &offset
); 
 284                                 config
.offset 
= offset
; 
 298                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 305         if (cmdp 
== 0) return printConfiguration( config 
); 
 308         if (errors
) return usage_t55xx_config(); 
 311         return printConfiguration ( config 
); 
 314 int T55xxReadBlock(uint8_t block
, bool page1
, bool usepwd
, bool override
, uint32_t password
){ 
 317                 // try reading the config block and verify that PWD bit is set before doing this! 
 319                         if ( !AquireData(T55x7_PAGE0
, T55x7_CONFIGURATION_BLOCK
, false, 0 ) ) return 0; 
 320                         if ( !tryDetectModulation() ) { 
 321                                 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits."); 
 324                                 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password...");    
 329                         PrintAndLog("Safety Check Overriden - proceeding despite risk"); 
 333         if (!AquireData(page1
, block
, usepwd
, password
) )       return 0; 
 334         if (!DecodeT55xxBlock()) return 0; 
 337         sprintf(blk
,"%d", block
); 
 338         printT55xxBlock(blk
);    
 342 int CmdT55xxReadBlock(const char *Cmd
) { 
 343         uint8_t block 
= REGULAR_READ_MODE_BLOCK
; 
 344         uint32_t password 
= 0; //default to blank Block 7 
 346         bool override 
= false; 
 350         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
 351                 switch(param_getchar(Cmd
, cmdp
)) { 
 354                         return usage_t55xx_read(); 
 357                         errors 
|= param_getdec(Cmd
, cmdp
+1, &block
); 
 367                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 376                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 381         if (errors
) return usage_t55xx_read(); 
 383         if (block 
> 7 && block 
!= REGULAR_READ_MODE_BLOCK       
) { 
 384                 PrintAndLog("Block must be between 0 and 7"); 
 388         printT5xxHeader(page1
); 
 389         return T55xxReadBlock(block
, page1
, usepwd
, override
, password
); 
 392 bool DecodeT55xxBlock(){ 
 394         char buf
[30] = {0x00}; 
 398         uint8_t bitRate
[8] = {8,16,32,40,50,64,100,128}; 
 399         DemodBufferLen 
= 0x00; 
 401         switch( config
.modulation 
){ 
 403                         snprintf(cmdStr
, sizeof(buf
),"%d %d", bitRate
[config
.bitrate
], config
.inverted 
); 
 404                         ans 
= FSKrawDemod(cmdStr
, false); 
 408                         snprintf(cmdStr
, sizeof(buf
),"%d %d 8 5", bitRate
[config
.bitrate
], config
.inverted 
); 
 409                         ans 
= FSKrawDemod(cmdStr
, false); 
 413                         snprintf(cmdStr
, sizeof(buf
),"%d %d 10 8", bitRate
[config
.bitrate
], config
.inverted 
); 
 414                         ans 
= FSKrawDemod(cmdStr
, false); 
 417                         snprintf(cmdStr
, sizeof(buf
),"%d %d 1", bitRate
[config
.bitrate
], config
.inverted 
); 
 418                         ans 
= ASKDemod_ext(cmdStr
, false, false, 1, &ST
); 
 421                         // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) 
 422                         save_restoreGB(GRAPH_SAVE
); 
 424                         snprintf(cmdStr
, sizeof(buf
),"%d %d 6", bitRate
[config
.bitrate
], config
.inverted 
); 
 425                         ans 
= PSKDemod(cmdStr
, false); 
 427                         save_restoreGB(GRAPH_RESTORE
); 
 429                 case DEMOD_PSK2
: //inverted won't affect this 
 430                 case DEMOD_PSK3
: //not fully implemented 
 431                         // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) 
 432                         save_restoreGB(GRAPH_SAVE
); 
 434                         snprintf(cmdStr
, sizeof(buf
),"%d 0 6", bitRate
[config
.bitrate
] ); 
 435                         ans 
= PSKDemod(cmdStr
, false); 
 436                         psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 438                         save_restoreGB(GRAPH_RESTORE
); 
 441                         snprintf(cmdStr
, sizeof(buf
),"%d %d 1", bitRate
[config
.bitrate
], config
.inverted 
); 
 442                         ans 
= NRZrawDemod(cmdStr
, false); 
 446                         snprintf(cmdStr
, sizeof(buf
),"0 %d %d 1", bitRate
[config
.bitrate
], config
.inverted 
); 
 447                         ans 
= ASKbiphaseDemod(cmdStr
, false); 
 455 bool DecodeT5555TraceBlock() { 
 456         DemodBufferLen 
= 0x00; 
 458         // According to datasheet. Always: RF/64, not inverted, Manchester 
 459         return (bool) ASKDemod("64 0 1", false, false, 1); 
 462 int CmdT55xxDetect(const char *Cmd
){ 
 466         uint32_t password 
= 0; 
 469         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
 470                 switch(param_getchar(Cmd
, cmdp
)) { 
 473                         return usage_t55xx_detect(); 
 476                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 481                         // use Graphbuffer data 
 486                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 491         if (errors
) return usage_t55xx_detect(); 
 494                 if ( !AquireData(T55x7_PAGE0
, T55x7_CONFIGURATION_BLOCK
, usepwd
, password
) ) 
 498         if ( !tryDetectModulation() ) 
 499                 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'"); 
 504 // detect configuration? 
 505 bool tryDetectModulation(){ 
 507         t55xx_conf_block_t tests
[15]; 
 509         uint8_t fc1 
= 0, fc2 
= 0, ans 
= 0; 
 510         int clk 
= 0, firstClockEdge 
= 0; 
 511         ans 
= fskClocks(&fc1
, &fc2
, (uint8_t *)&clk
, false, &firstClockEdge
); 
 512         if (ans 
&& ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5))) { 
 513                 if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 514                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 515                         if (fc1
==8 && fc2 
== 5) 
 516                                 tests
[hits
].modulation 
= DEMOD_FSK1a
; 
 517                         else if (fc1
==10 && fc2 
== 8) 
 518                                 tests
[hits
].modulation 
= DEMOD_FSK2
; 
 519                         tests
[hits
].bitrate 
= bitRate
; 
 520                         tests
[hits
].inverted 
= false; 
 521                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 522                         tests
[hits
].ST 
= false; 
 525                 if ( FSKrawDemod("0 1", false) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 526                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 527                         if (fc1 
== 8 && fc2 
== 5) 
 528                                 tests
[hits
].modulation 
= DEMOD_FSK1
; 
 529                         else if (fc1 
== 10 && fc2 
== 8) 
 530                                 tests
[hits
].modulation 
= DEMOD_FSK2a
; 
 531                         tests
[hits
].bitrate 
= bitRate
; 
 532                         tests
[hits
].inverted 
= true; 
 533                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 534                         tests
[hits
].ST 
= false; 
 538                 clk 
= GetAskClock("", false, false); 
 540                         tests
[hits
].ST 
= true; 
 541                         if ( ASKDemod_ext("0 0 1", false, false, 1, &tests
[hits
].ST
) && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 542                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 543                                 tests
[hits
].bitrate 
= bitRate
; 
 544                                 tests
[hits
].inverted 
= false; 
 545                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 548                         tests
[hits
].ST 
= true; 
 549                         if ( ASKDemod_ext("0 1 1", false, false, 1, &tests
[hits
].ST
)  && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 550                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 551                                 tests
[hits
].bitrate 
= bitRate
; 
 552                                 tests
[hits
].inverted 
= true; 
 553                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 556                         if ( ASKbiphaseDemod("0 0 0 2", false) && test(DEMOD_BI
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
) ) { 
 557                                 tests
[hits
].modulation 
= DEMOD_BI
; 
 558                                 tests
[hits
].bitrate 
= bitRate
; 
 559                                 tests
[hits
].inverted 
= false; 
 560                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 561                                 tests
[hits
].ST 
= false; 
 564                         if ( ASKbiphaseDemod("0 0 1 2", false) && test(DEMOD_BIa
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
) ) { 
 565                                 tests
[hits
].modulation 
= DEMOD_BIa
; 
 566                                 tests
[hits
].bitrate 
= bitRate
; 
 567                                 tests
[hits
].inverted 
= true; 
 568                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 569                                 tests
[hits
].ST 
= false; 
 573                 clk 
= GetNrzClock("", false, false); 
 574                 if (clk
>8) { //clock of rf/8 is likely a false positive, so don't use it. 
 575                         if ( NRZrawDemod("0 0 1", false)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 576                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 577                                 tests
[hits
].bitrate 
= bitRate
; 
 578                                 tests
[hits
].inverted 
= false; 
 579                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 580                                 tests
[hits
].ST 
= false; 
 584                         if ( NRZrawDemod("0 1 1", false)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 585                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 586                                 tests
[hits
].bitrate 
= bitRate
; 
 587                                 tests
[hits
].inverted 
= true; 
 588                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 589                                 tests
[hits
].ST 
= false; 
 594                 clk 
= GetPskClock("", false, false); 
 597                         save_restoreGB(GRAPH_SAVE
); 
 598                         // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) 
 600                         if ( PSKDemod("0 0 6", false) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 601                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 602                                 tests
[hits
].bitrate 
= bitRate
; 
 603                                 tests
[hits
].inverted 
= false; 
 604                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 605                                 tests
[hits
].ST 
= false; 
 608                         if ( PSKDemod("0 1 6", false) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)) { 
 609                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 610                                 tests
[hits
].bitrate 
= bitRate
; 
 611                                 tests
[hits
].inverted 
= true; 
 612                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 613                                 tests
[hits
].ST 
= false; 
 616                         // PSK2 - needs a call to psk1TOpsk2. 
 617                         if ( PSKDemod("0 0 6", false)) { 
 618                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 619                                 if (test(DEMOD_PSK2
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)){ 
 620                                         tests
[hits
].modulation 
= DEMOD_PSK2
; 
 621                                         tests
[hits
].bitrate 
= bitRate
; 
 622                                         tests
[hits
].inverted 
= false; 
 623                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 624                                         tests
[hits
].ST 
= false; 
 627                         } // inverse waves does not affect this demod 
 628                         // PSK3 - needs a call to psk1TOpsk2. 
 629                         if ( PSKDemod("0 0 6", false)) { 
 630                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 631                                 if (test(DEMOD_PSK3
, &tests
[hits
].offset
, &bitRate
, clk
, &tests
[hits
].Q5
)){ 
 632                                         tests
[hits
].modulation 
= DEMOD_PSK3
; 
 633                                         tests
[hits
].bitrate 
= bitRate
; 
 634                                         tests
[hits
].inverted 
= false; 
 635                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 636                                         tests
[hits
].ST 
= false; 
 639                         } // inverse waves does not affect this demod 
 641                         save_restoreGB(GRAPH_RESTORE
); 
 645                 config
.modulation 
= tests
[0].modulation
; 
 646                 config
.bitrate 
= tests
[0].bitrate
; 
 647                 config
.inverted 
= tests
[0].inverted
; 
 648                 config
.offset 
= tests
[0].offset
; 
 649                 config
.block0 
= tests
[0].block0
; 
 650                 config
.Q5 
= tests
[0].Q5
; 
 651                 config
.ST 
= tests
[0].ST
; 
 652                 printConfiguration( config 
); 
 657                 PrintAndLog("Found [%d] possible matches for modulation.",hits
); 
 658                 for(int i
=0; i
<hits
; ++i
){ 
 659                         PrintAndLog("--[%d]---------------", i
+1); 
 660                         printConfiguration( tests
[i
] ); 
 666 bool testModulation(uint8_t mode
, uint8_t modread
){ 
 669                         if (modread 
>= DEMOD_FSK1 
&& modread 
<= DEMOD_FSK2a
) return true; 
 672                         if (modread 
== DEMOD_ASK
) return true; 
 675                         if (modread 
== DEMOD_PSK1
) return true; 
 678                         if (modread 
== DEMOD_PSK2
) return true; 
 681                         if (modread 
== DEMOD_PSK3
) return true; 
 684                         if (modread 
== DEMOD_NRZ
) return true; 
 687                         if (modread 
== DEMOD_BI
) return true; 
 690                         if (modread 
== DEMOD_BIa
) return true; 
 698 bool testQ5Modulation(uint8_t   mode
, uint8_t   modread
){ 
 701                         if (modread 
>= 4 && modread 
<= 5) return true; 
 704                         if (modread 
== 0) return true; 
 707                         if (modread 
== 1) return true; 
 710                         if (modread 
== 2) return true; 
 713                         if (modread 
== 3) return true; 
 716                         if (modread 
== 7) return true; 
 719                         if (modread 
== 6) return true; 
 727 int convertQ5bitRate(uint8_t bitRateRead
) { 
 728         uint8_t expected
[] = {8, 16, 32, 40, 50, 64, 100, 128}; 
 729         for (int i
=0; i
<8; i
++) 
 730                 if (expected
[i
] == bitRateRead
) 
 736 bool testQ5(uint8_t mode
, uint8_t *offset
, int *fndBitRate
, uint8_t     clk
){ 
 738         if ( DemodBufferLen 
< 64 ) return false; 
 740         for (uint8_t idx 
= 28; idx 
< 64; idx
++){ 
 742                 if ( PackBits(si
, 28, DemodBuffer
) == 0x00 ) continue; 
 744                 uint8_t safer     
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //master key 
 745                 uint8_t resv      
= PackBits(si
, 8, DemodBuffer
); si 
+= 8; 
 746                 // 2nibble must be zeroed. 
 747                 if (safer 
!= 0x6 && safer 
!= 0x9) continue; 
 748                 if ( resv 
> 0x00) continue; 
 749                 //uint8_t       pageSel   = PackBits(si, 1, DemodBuffer); si += 1; 
 750                 //uint8_t fastWrite = PackBits(si, 1, DemodBuffer); si += 1; 
 752                 int bitRate       
= PackBits(si
, 6, DemodBuffer
)*2 + 2; si 
+= 6;     //bit rate 
 753                 if (bitRate 
> 128 || bitRate 
< 8) continue; 
 755                 //uint8_t AOR       = PackBits(si, 1, DemodBuffer); si += 1;    
 756                 //uint8_t PWD       = PackBits(si, 1, DemodBuffer); si += 1;  
 757                 //uint8_t pskcr     = PackBits(si, 2, DemodBuffer); si += 2;  //could check psk cr 
 758                 //uint8_t inverse   = PackBits(si, 1, DemodBuffer); si += 1; 
 760                 uint8_t modread   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 761                 uint8_t maxBlk    
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 762                 //uint8_t ST        = PackBits(si, 1, DemodBuffer); si += 1; 
 763                 if (maxBlk 
== 0) continue; 
 765                 if (!testQ5Modulation(mode
, modread
)) continue; 
 766                 if (bitRate 
!= clk
) continue; 
 767                 *fndBitRate 
= convertQ5bitRate(bitRate
); 
 768                 if (*fndBitRate 
< 0) continue; 
 776 bool testBitRate(uint8_t readRate
, uint8_t clk
){ 
 777         uint8_t expected
[] = {8, 16, 32, 40, 50, 64, 100, 128}; 
 778         if (expected
[readRate
] == clk
) 
 784 bool test(uint8_t mode
, uint8_t *offset
, int *fndBitRate
, uint8_t clk
, bool *Q5
){ 
 786         if ( DemodBufferLen 
< 64 ) return false; 
 788         for (uint8_t idx 
= 28; idx 
< 64; idx
++){ 
 790                 if ( PackBits(si
, 28, DemodBuffer
) == 0x00 ) continue; 
 792                 uint8_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //master key 
 793                 uint8_t resv     
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //was 7 & +=7+3 //should be only 4 bits if extended mode 
 794                 // 2nibble must be zeroed. 
 795                 // moved test to here, since this gets most faults first. 
 796                 if ( resv 
> 0x00) continue; 
 798                 int bitRate      
= PackBits(si
, 6, DemodBuffer
); si 
+= 6;     //bit rate (includes extended mode part of rate) 
 799                 uint8_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;     //bit 15 extended mode 
 800                 uint8_t modread  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5+2+1;  
 801                 //uint8_t pskcr   = PackBits(si, 2, DemodBuffer); si += 2+1;  //could check psk cr 
 802                 //uint8_t nml01    = PackBits(si, 1, DemodBuffer); si += 1+5;   //bit 24, 30, 31 could be tested for 0 if not extended mode 
 803                 //uint8_t nml02    = PackBits(si, 2, DemodBuffer); si += 2; 
 806                 bool extMode 
=( (safer 
== 0x6 || safer 
== 0x9) && extend
) ? true : false; 
 809                         if (bitRate 
> 7) continue; 
 810                         if (!testBitRate(bitRate
, clk
)) continue; 
 811                 } else { //extended mode bitrate = same function to calc bitrate as em4x05 
 812                         if (EM4x05_GET_BITRATE(bitRate
) != clk
) continue; 
 816                 if (!testModulation(mode
, modread
)) continue; 
 817                 *fndBitRate 
= bitRate
; 
 822         if (testQ5(mode
, offset
, fndBitRate
, clk
)) { 
 829 void printT55xxBlock(const char *blockNum
){ 
 831         uint8_t i 
= config
.offset
; 
 832         uint8_t endpos 
= 32 + i
; 
 833         uint32_t blockData 
= 0; 
 834         uint8_t bits
[64] = {0x00}; 
 836         if ( !DemodBufferLen
) return; 
 838         if ( endpos 
> DemodBufferLen
){ 
 839                 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i
, DemodBufferLen
-32); 
 843         for (; i 
< endpos
; ++i
) 
 844                 bits
[i 
- config
.offset
]=DemodBuffer
[i
]; 
 846         blockData 
= PackBits(0, 32, bits
); 
 848         PrintAndLog("  %s | %08X | %s", blockNum
, blockData
, sprint_bin(bits
,32)); 
 851 int special(const char *Cmd
) { 
 852         uint32_t blockData 
= 0; 
 853         uint8_t bits
[32] = {0x00}; 
 855         PrintAndLog("OFFSET | DATA       | BINARY"); 
 856         PrintAndLog("----------------------------------------------------"); 
 860                 for (i 
= 0; i 
< 32; ++i
) 
 861                         bits
[i
]=DemodBuffer
[j
+i
]; 
 863                 blockData 
= PackBits(0, 32, bits
); 
 865                 PrintAndLog("    %02d | 0x%08X | %s",j 
, blockData
, sprint_bin(bits
,32));        
 870 int printConfiguration( t55xx_conf_block_t b
){ 
 871         PrintAndLog("Chip Type  : %s", (b
.Q5
) ? "T5555(Q5)" : "T55x7"); 
 872         PrintAndLog("Modulation : %s", GetSelectedModulationStr(b
.modulation
) ); 
 873         PrintAndLog("Bit Rate   : %s", GetBitRateStr(b
.bitrate
, (b
.block0 
& T55x7_X_MODE 
&& (b
.block0
>>28==6 || b
.block0
>>28==9))) ); 
 874         PrintAndLog("Inverted   : %s", (b
.inverted
) ? "Yes" : "No" ); 
 875         PrintAndLog("Offset     : %d", b
.offset
); 
 876         PrintAndLog("Seq. Term. : %s", (b
.ST
) ? "Yes" : "No" ); 
 877         PrintAndLog("Block0     : 0x%08X", b
.block0
); 
 882 int CmdT55xxWakeUp(const char *Cmd
) { 
 883         uint32_t password 
= 0; 
 884         if ( strlen(Cmd
) <= 0 ) return usage_t55xx_wakup(); 
 885         char cmdp 
= param_getchar(Cmd
, 0); 
 886         if ( cmdp 
== 'h' || cmdp 
== 'H') return usage_t55xx_wakup(); 
 888         password 
= param_get32ex(Cmd
, 0, 0, 16); 
 890         UsbCommand c 
= {CMD_T55XX_WAKEUP
, {password
, 0, 0}}; 
 891         clearCommandBuffer(); 
 893         PrintAndLog("Wake up command sent. Try read now"); 
 897 int CmdT55xxWriteBlock(const char *Cmd
) { 
 898         uint8_t block 
= 0xFF; //default to invalid block 
 899         uint32_t data 
= 0; //default to blank Block  
 900         uint32_t password 
= 0; //default to blank Block 7 
 903         bool gotdata 
= false; 
 904         bool testMode 
= false; 
 907         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
 908                 switch(param_getchar(Cmd
, cmdp
)) { 
 911                         return usage_t55xx_write(); 
 914                         errors 
|= param_getdec(Cmd
, cmdp
+1, &block
); 
 919                         data 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 925                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
 939                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 944         if (errors 
|| !gotdata
) return usage_t55xx_write(); 
 947                 PrintAndLog("Block number must be between 0 and 7"); 
 951         UsbCommand c 
= {CMD_T55XX_WRITE_BLOCK
, {data
, block
, 0}}; 
 953         c
.d
.asBytes
[0] = (page1
) ? 0x2 : 0;  
 954         c
.d
.asBytes
[0] |= (testMode
) ? 0x4 : 0;  
 956         char pwdStr
[16] = {0}; 
 957         snprintf(pwdStr
, sizeof(pwdStr
), "pwd: 0x%08X", password
); 
 959         PrintAndLog("Writing page %d  block: %02d  data: 0x%08X %s", page1
, block
, data
,  (usepwd
) ? pwdStr 
: "" ); 
 964                 c
.d
.asBytes
[0] |= 0x1;  
 966         clearCommandBuffer(); 
 968         if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)){ 
 969                 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)"); 
 975 int CmdT55xxReadTrace(const char *Cmd
) { 
 976         char cmdp 
= param_getchar(Cmd
, 0); 
 977         bool pwdmode 
= false; 
 978         uint32_t password 
= 0; 
 979         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H')  
 980                 return usage_t55xx_trace(); 
 983                 if ( !AquireData( T55x7_PAGE1
, REGULAR_READ_MODE_BLOCK
, pwdmode
, password 
) ) 
 987                 if (!DecodeT5555TraceBlock()) return 0; 
 989                 if (!DecodeT55xxBlock()) return 0; 
 992         if ( !DemodBufferLen 
) return 0; 
 994         RepaintGraphWindow(); 
 995         uint8_t repeat 
= (config
.offset 
> 5) ? 32 : 0; 
 997         uint8_t si 
= config
.offset
+repeat
; 
 998         uint32_t bl1     
= PackBits(si
, 32, DemodBuffer
); 
 999         uint32_t bl2     
= PackBits(si
+32, 32, DemodBuffer
); 
1002                 uint32_t hdr 
= PackBits(si
, 9,  DemodBuffer
); si 
+= 9; 
1005                         PrintAndLog("Invalid Q5 Trace data header (expected 0x1FF, found %X)", hdr
); 
1009                 t5555_tracedata_t data 
= {.bl1 
= bl1
, .bl2 
= bl2
, .icr 
= 0, .lotidc 
= '?', .lotid 
= 0, .wafer 
= 0, .dw 
=0}; 
1011                 data
.icr     
= PackBits(si
, 2,  DemodBuffer
); si 
+= 2; 
1012                 data
.lotidc  
= 'Z' - PackBits(si
, 2,  DemodBuffer
); si 
+= 3; 
1014                 data
.lotid   
= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1016                 data
.lotid  
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1018                 data
.lotid  
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1020                 data
.lotid  
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1022                 data
.lotid  
|= PackBits(si
, 1,  DemodBuffer
); si 
+= 1; 
1024                 data
.wafer   
= PackBits(si
, 3,  DemodBuffer
); si 
+= 4; 
1026                 data
.wafer  
|= PackBits(si
, 2,  DemodBuffer
); si 
+= 2; 
1028                 data
.dw      
= PackBits(si
, 2,  DemodBuffer
); si 
+= 3; 
1030                 data
.dw     
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1032                 data
.dw     
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1034                 data
.dw     
|= PackBits(si
, 4,  DemodBuffer
); si 
+= 5; 
1036                 printT5555Trace(data
, repeat
); 
1040                 t55x7_tracedata_t data 
= {.bl1 
= bl1
, .bl2 
= bl2
, .acl 
= 0, .mfc 
= 0, .cid 
= 0, .year 
= 0, .quarter 
= 0, .icr 
= 0,  .lotid 
= 0, .wafer 
= 0, .dw 
= 0}; 
1042                 data
.acl 
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
1043                 if ( data
.acl 
!= 0xE0 ) { 
1044                         PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. "); 
1048                 data
.mfc     
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
1049                 data
.cid     
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
1050                 data
.icr     
= PackBits(si
, 3,  DemodBuffer
); si 
+= 3; 
1051                 data
.year    
= PackBits(si
, 4,  DemodBuffer
); si 
+= 4; 
1052                 data
.quarter 
= PackBits(si
, 2,  DemodBuffer
); si 
+= 2; 
1053                 data
.lotid   
= PackBits(si
, 14, DemodBuffer
); si 
+= 14; 
1054                 data
.wafer   
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
1055                 data
.dw      
= PackBits(si
, 15, DemodBuffer
);  
1057                 time_t t 
= time(NULL
); 
1058                 struct tm tm 
= *localtime(&t
); 
1059                 if ( data
.year 
> tm
.tm_year
-110) 
1064                 printT55x7Trace(data
, repeat
); 
1069 void printT55x7Trace( t55x7_tracedata_t data
, uint8_t repeat 
){ 
1070         PrintAndLog("-- T55x7 Trace Information ----------------------------------"); 
1071         PrintAndLog("-------------------------------------------------------------"); 
1072         PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1)  : 0x%02X (%d)", data
.acl
, data
.acl
); 
1073         PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6)    : 0x%02X (%d) - %s", data
.mfc
, data
.mfc
, getTagInfo(data
.mfc
)); 
1074         PrintAndLog(" CID                                     : 0x%02X (%d) - %s", data
.cid
, data
.cid
, GetModelStrFromCID(data
.cid
)); 
1075         PrintAndLog(" ICR IC Revision                         : %d", data
.icr 
); 
1076         PrintAndLog(" Manufactured"); 
1077         PrintAndLog("     Year/Quarter : %d/%d", data
.year
, data
.quarter
); 
1078         PrintAndLog("     Lot ID       : %d", data
.lotid 
); 
1079         PrintAndLog("     Wafer number : %d", data
.wafer
); 
1080         PrintAndLog("     Die Number   : %d", data
.dw
); 
1081         PrintAndLog("-------------------------------------------------------------"); 
1082         PrintAndLog(" Raw Data - Page 1"); 
1083         PrintAndLog("     Block 1  : 0x%08X  %s", data
.bl1
, sprint_bin(DemodBuffer
+config
.offset
+repeat
,32) ); 
1084         PrintAndLog("     Block 2  : 0x%08X  %s", data
.bl2
, sprint_bin(DemodBuffer
+config
.offset
+repeat
+32,32) ); 
1085         PrintAndLog("-------------------------------------------------------------");    
1090                 1-8             ACL Allocation class (ISO/IEC 15963-1)  0xE0  
1091                 9-16    MFC Manufacturer ID (ISO/IEC 7816-6)    0x15 Atmel Corporation 
1092                 17-21   CID                                                                             0x1 = Atmel ATA5577M1  0x2 = Atmel ATA5577M2  
1093                 22-24   ICR IC revision 
1094                 25-28   YEAR (BCD encoded)                                              9 (= 2009) 
1095                 29-30   QUARTER                                                                 1,2,3,4  
1101                 18-32   DW,  die number sequential 
1105 void printT5555Trace( t5555_tracedata_t data
, uint8_t repeat 
){ 
1106         PrintAndLog("-- T5555 (Q5) Trace Information -----------------------------"); 
1107         PrintAndLog("-------------------------------------------------------------"); 
1108         PrintAndLog(" ICR IC Revision  : %d", data
.icr 
);        
1109         PrintAndLog("     Lot          : %c%d", data
.lotidc
, data
.lotid
); 
1110         PrintAndLog("     Wafer number : %d", data
.wafer
); 
1111         PrintAndLog("     Die Number   : %d", data
.dw
); 
1112         PrintAndLog("-------------------------------------------------------------"); 
1113         PrintAndLog(" Raw Data - Page 1"); 
1114         PrintAndLog("     Block 1  : 0x%08X  %s", data
.bl1
, sprint_bin(DemodBuffer
+config
.offset
+repeat
,32) ); 
1115         PrintAndLog("     Block 2  : 0x%08X  %s", data
.bl2
, sprint_bin(DemodBuffer
+config
.offset
+repeat
+32,32) ); 
1119                 TRACE - BLOCK O and BLOCK1 
1124                 15-35 Lot ID (NB parity) 
1125                 36-41 Wafer number (NB parity) 
1126                 42-58 DW, die number sequential (NB parity) 
1132 //need to add Q5 info... 
1133 int CmdT55xxInfo(const char *Cmd
){ 
1135                 Page 0 Block 0 Configuration data. 
1139         bool pwdmode 
= false; 
1140         uint32_t password 
= 0; 
1141         char cmdp 
= param_getchar(Cmd
, 0); 
1143         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') 
1144                 return usage_t55xx_info(); 
1147                 if ( !AquireData( T55x7_PAGE0
, T55x7_CONFIGURATION_BLOCK
, pwdmode
, password 
) ) 
1150         if (!DecodeT55xxBlock()) return 1; 
1152         // too little space to start with 
1153         if ( DemodBufferLen 
< 32) return 1; 
1155         uint8_t si 
= config
.offset
; 
1156         uint32_t bl0      
= PackBits(si
, 32, DemodBuffer
); 
1158         uint32_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;       
1159         uint32_t resv     
= PackBits(si
, 7, DemodBuffer
); si 
+= 7; 
1160         uint32_t dbr      
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
1161         uint32_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
1162         uint32_t datamod  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5; 
1163         uint32_t pskcf    
= PackBits(si
, 2, DemodBuffer
); si 
+= 2; 
1164         uint32_t aor      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
1165         uint32_t otp      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
1166         uint32_t maxblk   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
1167         uint32_t pwd      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
1168         uint32_t sst      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
1169         uint32_t fw       
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
1170         uint32_t inv      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
1171         uint32_t por      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
1173         if (config
.Q5
) PrintAndLog("*** Warning *** Config Info read off a Q5 will not display as expected"); 
1175         PrintAndLog("-- T55x7 Configuration & Tag Information --------------------"); 
1176         PrintAndLog("-------------------------------------------------------------"); 
1177         PrintAndLog(" Safer key                 : %s", GetSaferStr(safer
)); 
1178         PrintAndLog(" reserved                  : %d", resv
); 
1179         PrintAndLog(" Data bit rate             : %s", GetBitRateStr(dbr
, extend
)); 
1180         PrintAndLog(" eXtended mode             : %s", (extend
) ? "Yes - Warning":"No"); 
1181         PrintAndLog(" Modulation                : %s", GetModulationStr(datamod
)); 
1182         PrintAndLog(" PSK clock frequency       : %d", pskcf
); 
1183         PrintAndLog(" AOR - Answer on Request   : %s", (aor
) ? "Yes":"No"); 
1184         PrintAndLog(" OTP - One Time Pad        : %s", (otp
) ? "Yes - Warning":"No" ); 
1185         PrintAndLog(" Max block                 : %d", maxblk
); 
1186         PrintAndLog(" Password mode             : %s", (pwd
) ? "Yes":"No"); 
1187         PrintAndLog(" Sequence Start Terminator : %s", (sst
) ? "Yes":"No"); 
1188         PrintAndLog(" Fast Write                : %s", (fw
)  ? "Yes":"No"); 
1189         PrintAndLog(" Inverse data              : %s", (inv
) ? "Yes":"No"); 
1190         PrintAndLog(" POR-Delay                 : %s", (por
) ? "Yes":"No"); 
1191         PrintAndLog("-------------------------------------------------------------"); 
1192         PrintAndLog(" Raw Data - Page 0"); 
1193         PrintAndLog("     Block 0  : 0x%08X  %s", bl0
, sprint_bin(DemodBuffer
+config
.offset
,32) ); 
1194         PrintAndLog("-------------------------------------------------------------"); 
1199 int CmdT55xxDump(const char *Cmd
){ 
1201         uint32_t password 
= 0; 
1202         char cmdp 
= param_getchar(Cmd
, 0); 
1203         bool override 
= false; 
1204         if ( cmdp 
== 'h' || cmdp 
== 'H') return usage_t55xx_dump(); 
1206         bool usepwd 
= ( strlen(Cmd
) > 0); 
1208                 password 
= param_get32ex(Cmd
, 0, 0, 16); 
1209                 if (param_getchar(Cmd
, 1) =='o' ) 
1214         for ( uint8_t i 
= 0; i 
<8; ++i
) 
1215                 T55xxReadBlock(i
, 0, usepwd
, override
, password
); 
1218         for ( uint8_t   i 
= 0; i
<4; i
++) 
1219                 T55xxReadBlock(i
, 1, usepwd
, override
, password
);                
1224 int AquireData( uint8_t page
, uint8_t block
, bool pwdmode
, uint32_t password 
){ 
1227         // bit1 = page to read from 
1228         uint8_t arg0 
= (page
<<1) | pwdmode
; 
1229         UsbCommand c 
= {CMD_T55XX_READ_BLOCK
, {arg0
, block
, password
}}; 
1231         clearCommandBuffer(); 
1233         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
1234                 PrintAndLog("command execution time out"); 
1237         getSamples(12000,true); 
1241 char * GetBitRateStr(uint32_t id
, bool xmode
) { 
1242         static char buf
[25]; 
1245         if (xmode
) { //xmode bitrate calc is same as em4x05 calc 
1246                 snprintf(retStr
,sizeof(buf
),"%d - RF/%d", id
, EM4x05_GET_BITRATE(id
)); 
1249                         case 0:   snprintf(retStr
,sizeof(buf
),"%d - RF/8",id
);   break; 
1250                         case 1:   snprintf(retStr
,sizeof(buf
),"%d - RF/16",id
);  break; 
1251                         case 2:   snprintf(retStr
,sizeof(buf
),"%d - RF/32",id
);  break; 
1252                         case 3:   snprintf(retStr
,sizeof(buf
),"%d - RF/40",id
);  break; 
1253                         case 4:   snprintf(retStr
,sizeof(buf
),"%d - RF/50",id
);  break; 
1254                         case 5:   snprintf(retStr
,sizeof(buf
),"%d - RF/64",id
);  break; 
1255                         case 6:   snprintf(retStr
,sizeof(buf
),"%d - RF/100",id
); break; 
1256                         case 7:   snprintf(retStr
,sizeof(buf
),"%d - RF/128",id
); break; 
1257                         default:  snprintf(retStr
,sizeof(buf
),"%d - (Unknown)",id
); break; 
1263 char * GetSaferStr(uint32_t id
) { 
1264         static char buf
[40]; 
1267         snprintf(retStr
,sizeof(buf
),"%d",id
); 
1269                 snprintf(retStr
,sizeof(buf
),"%d - passwd",id
); 
1272                 snprintf(retStr
,sizeof(buf
),"%d - testmode",id
); 
1278 char * GetModulationStr( uint32_t id
){ 
1279         static char buf
[60]; 
1283                 case 0: snprintf(retStr
,sizeof(buf
),"%d - DIRECT (ASK/NRZ)",id
); break; 
1284                 case 1: snprintf(retStr
,sizeof(buf
),"%d - PSK 1 phase change when input changes",id
); break; 
1285                 case 2: snprintf(retStr
,sizeof(buf
),"%d - PSK 2 phase change on bitclk if input high",id
); break; 
1286                 case 3: snprintf(retStr
,sizeof(buf
),"%d - PSK 3 phase change on rising edge of input",id
); break; 
1287                 case 4: snprintf(retStr
,sizeof(buf
),"%d - FSK 1 RF/8  RF/5",id
); break; 
1288                 case 5: snprintf(retStr
,sizeof(buf
),"%d - FSK 2 RF/8  RF/10",id
); break; 
1289                 case 6: snprintf(retStr
,sizeof(buf
),"%d - FSK 1a RF/5  RF/8",id
); break; 
1290                 case 7: snprintf(retStr
,sizeof(buf
),"%d - FSK 2a RF/10  RF/8",id
); break; 
1291                 case 8: snprintf(retStr
,sizeof(buf
),"%d - Manchester",id
); break; 
1292                 case 16: snprintf(retStr
,sizeof(buf
),"%d - Biphase",id
); break; 
1293                 case 0x18: snprintf(retStr
,sizeof(buf
),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id
); break; 
1294                 case 17: snprintf(retStr
,sizeof(buf
),"%d - Reserved",id
); break; 
1295                 default: snprintf(retStr
,sizeof(buf
),"0x%02X (Unknown)",id
); break; 
1300 char * GetModelStrFromCID(uint32_t cid
){ 
1302         static char buf
[10]; 
1305         if (cid 
== 1) snprintf(retStr
, sizeof(buf
),"ATA5577M1"); 
1306         if (cid 
== 2) snprintf(retStr
, sizeof(buf
),"ATA5577M2");         
1310 char * GetSelectedModulationStr( uint8_t id
){ 
1312         static char buf
[20]; 
1316                 case DEMOD_FSK
: snprintf(retStr
,sizeof(buf
),"FSK");     break; 
1317                 case DEMOD_FSK1
: snprintf(retStr
,sizeof(buf
),"FSK1"); break; 
1318                 case DEMOD_FSK1a
: snprintf(retStr
,sizeof(buf
),"FSK1a"); break; 
1319                 case DEMOD_FSK2
: snprintf(retStr
,sizeof(buf
),"FSK2"); break; 
1320                 case DEMOD_FSK2a
: snprintf(retStr
,sizeof(buf
),"FSK2a"); break; 
1321                 case DEMOD_ASK
: snprintf(retStr
,sizeof(buf
),"ASK"); break; 
1322                 case DEMOD_NRZ
: snprintf(retStr
,sizeof(buf
),"DIRECT/NRZ"); break; 
1323                 case DEMOD_PSK1
: snprintf(retStr
,sizeof(buf
),"PSK1"); break; 
1324                 case DEMOD_PSK2
: snprintf(retStr
,sizeof(buf
),"PSK2"); break; 
1325                 case DEMOD_PSK3
: snprintf(retStr
,sizeof(buf
),"PSK3"); break; 
1326                 case DEMOD_BI
: snprintf(retStr
,sizeof(buf
),"BIPHASE"); break; 
1327                 case DEMOD_BIa
: snprintf(retStr
,sizeof(buf
),"BIPHASEa - (CDP)"); break; 
1328                 default: snprintf(retStr
,sizeof(buf
),"(Unknown)"); break; 
1333 uint32_t PackBits(uint8_t start
, uint8_t len
, uint8_t* bits
){ 
1338         if (len 
> 32) return 0; 
1341         for (; j 
>= 0; --j
, ++i
) 
1342                 tmp     
|= bits
[i
] << j
; 
1347 int CmdResetRead(const char *Cmd
) { 
1348         UsbCommand c 
= {CMD_T55XX_RESET_READ
, {0,0,0}}; 
1350         clearCommandBuffer(); 
1352         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
1353                 PrintAndLog("command execution time out"); 
1357         uint8_t got
[BIGBUF_SIZE
-1]; 
1358         GetFromBigBuf(got
, sizeof(got
), 0, NULL
, -1 , 0); 
1359         setGraphBuf(got
, sizeof(got
)); 
1363 int CmdT55xxWipe(const char *Cmd
) { 
1364         char writeData
[20] = {0}; 
1365         char *ptrData 
= writeData
; 
1367         char cmdp 
= param_getchar(Cmd
, 0);       
1368         if ( cmdp 
== 'h' || cmdp 
== 'H') return usage_t55xx_wipe(); 
1370         bool Q5 
= (cmdp 
== 'q' || cmdp 
== 'Q'); 
1372         // Try with the default password to reset block 0 
1373         // With a pwd should work even if pwd bit not set 
1374         PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n"); 
1377                 snprintf(ptrData
,sizeof(writeData
),"b 0 d 6001F004 p 0"); 
1379                 snprintf(ptrData
,sizeof(writeData
),"b 0 d 00088040 p 0"); 
1382         if (!CmdT55xxWriteBlock(ptrData
)) PrintAndLog("Error writing blk 0"); 
1384         for (uint8_t blk 
= 1; blk
<8; blk
++) { 
1385                 snprintf(ptrData
,sizeof(writeData
),"b %d d 0", blk
); 
1386                 if (!CmdT55xxWriteBlock(ptrData
)) 
1387                         PrintAndLog("Error writing blk %d", blk
); 
1389                 memset(writeData
, 0x00, sizeof(writeData
)); 
1394 int CmdT55xxBruteForce(const char *Cmd
) { 
1396         // load a default pwd file. 
1398         char filename
[FILE_PATH_SIZE
]={0}; 
1401         uint8_t stKeyBlock 
= 20; 
1402         uint8_t *keyBlock 
= NULL
, *p 
= NULL
; 
1403         uint32_t start_password 
= 0x00000000; //start password 
1404         uint32_t end_password   
= 0xFFFFFFFF; //end   password 
1407         char cmdp 
= param_getchar(Cmd
, 0); 
1408         if (cmdp 
== 'h' || cmdp 
== 'H') return usage_t55xx_bruteforce(); 
1410         keyBlock 
= calloc(stKeyBlock
, 6); 
1411         if (keyBlock 
== NULL
) return 1; 
1413         if (cmdp 
== 'i' || cmdp 
== 'I') { 
1415                 int len 
= strlen(Cmd
+2); 
1416                 if (len 
> FILE_PATH_SIZE
) len 
= FILE_PATH_SIZE
; 
1417                 memcpy(filename
, Cmd
+2, len
); 
1419                 FILE * f 
= fopen( filename 
, "r"); 
1422                         PrintAndLog("File: %s: not found or locked.", filename
); 
1427                 while( fgets(buf
, sizeof(buf
), f
) ) { 
1428                         if (strlen(buf
) < 8 || buf
[7] == '\n') continue; 
1430                         while (fgetc(f
) != '\n' && !feof(f
)) ;  //goto next line 
1432                         //The line start with # is comment, skip 
1433                         if( buf
[0]=='#' ) continue; 
1435                         if (!isxdigit((unsigned char)buf
[0])) { 
1436                                 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf
); 
1442                         if ( stKeyBlock 
- keycnt 
< 2) { 
1443                                 p 
= realloc(keyBlock
, 6*(stKeyBlock
+=10)); 
1445                                         PrintAndLog("Cannot allocate memory for defaultKeys"); 
1452                         memset(keyBlock 
+ 4 * keycnt
, 0, 4); 
1453                         num_to_bytes(strtoll(buf
, NULL
, 16), 4, keyBlock 
+ 4*keycnt
); 
1454                         PrintAndLog("chk custom pwd[%2d] %08X", keycnt
, bytes_to_num(keyBlock 
+ 4*keycnt
, 4)); 
1456                         memset(buf
, 0, sizeof(buf
)); 
1461                         PrintAndLog("No keys found in file"); 
1465                 PrintAndLog("Loaded %d keys", keycnt
); 
1468                 uint64_t testpwd 
= 0x00; 
1469                 for (uint16_t c 
= 0; c 
< keycnt
; ++c 
) { 
1474                                 printf("\naborted via keyboard!\n"); 
1479                         testpwd 
= bytes_to_num(keyBlock 
+ 4*c
, 4); 
1481                         PrintAndLog("Testing %08X", testpwd
); 
1483                         if ( !AquireData(T55x7_PAGE0
, T55x7_CONFIGURATION_BLOCK
, true, testpwd
)) { 
1484                                 PrintAndLog("Aquireing data from device failed. Quitting"); 
1489                         found 
= tryDetectModulation(); 
1492                                 PrintAndLog("Found valid password: [%08X]", testpwd
); 
1497                 PrintAndLog("Password NOT found."); 
1502         // Try to read Block 7, first :) 
1504         // incremental pwd range search 
1505         start_password 
= param_get32ex(Cmd
, 0, 0, 16); 
1506         end_password 
= param_get32ex(Cmd
, 1, 0, 16); 
1508         if ( start_password 
>= end_password 
) { 
1510                 return usage_t55xx_bruteforce(); 
1512         PrintAndLog("Search password range [%08X -> %08X]", start_password
, end_password
); 
1514         uint32_t i 
= start_password
; 
1516         while ((!found
) && (i 
<= end_password
)) { 
1523                         printf("\naborted via keyboard!\n"); 
1528                 if (!AquireData(T55x7_PAGE0
, T55x7_CONFIGURATION_BLOCK
, true, i
)) { 
1529                         PrintAndLog("Aquireing data from device failed. Quitting"); 
1533                 found 
= tryDetectModulation(); 
1542                 PrintAndLog("Found valid password: [%08x]", i
); 
1544                 PrintAndLog("Password NOT found. Last tried: [%08x]", --i
); 
1550 // note length of data returned is different for different chips.   
1551 //   some return all page 1 (64 bits) and others return just that block (32 bits)  
1552 //   unfortunately the 64 bits makes this more likely to get a false positive... 
1553 bool tryDetectP1(bool getData
) { 
1554         uint8_t preamble
[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1}; 
1555         size_t startIdx 
= 0; 
1556         uint8_t fc1 
= 0, fc2 
= 0, ans 
= 0; 
1557         int clk 
= 0, firstClockEdge 
= 0; 
1561                 if ( !AquireData(T55x7_PAGE1
, 1, false, 0) ) 
1565         // try fsk clock detect. if successful it cannot be any other type of modulation...  (in theory...) 
1566         ans 
= fskClocks(&fc1
, &fc2
, (uint8_t *)&clk
, false, &firstClockEdge
); 
1567         if (ans 
&& ((fc1
==10 && fc2
==8) || (fc1
==8 && fc2
==5))) { 
1568                 if ( FSKrawDemod("0 0", false) &&  
1569                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1570                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1573                 if ( FSKrawDemod("0 1", false) &&  
1574                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1575                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1581         // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...) 
1582         clk 
= GetPskClock("", false, false); 
1585                 // save_restoreGB(1); 
1586                 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) 
1588                 if ( PSKDemod("0 0 6", false) && 
1589                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1590                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1591                         //save_restoreGB(0); 
1594                 if ( PSKDemod("0 1 6", false) && 
1595                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1596                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1597                         //save_restoreGB(0); 
1600                 // PSK2 - needs a call to psk1TOpsk2. 
1601                 if ( PSKDemod("0 0 6", false)) { 
1602                         psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
1603                         if (preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1604                                   (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1605                                 //save_restoreGB(0); 
1608                 } // inverse waves does not affect PSK2 demod 
1610                 //save_restoreGB(0);  
1611                 // no other modulation clocks = 2 or 4 so quit searching 
1612                 if (fc1 
!= 8) return false; 
1615         // try ask clock detect.  it could be another type even if successful. 
1616         clk 
= GetAskClock("", false, false); 
1618                 if ( ASKDemod_ext("0 0 1", false, false, 1, &st
) && 
1619                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1620                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1624                 if ( ASKDemod_ext("0 1 1", false, false, 1, &st
)  && 
1625                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1626                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1629                 if ( ASKbiphaseDemod("0 0 0 2", false) && 
1630                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1631                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1634                 if ( ASKbiphaseDemod("0 0 1 2", false) && 
1635                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1636                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1641         // try NRZ clock detect.  it could be another type even if successful. 
1642         clk 
= GetNrzClock("", false, false); //has the most false positives :( 
1644                 if ( NRZrawDemod("0 0 1", false)  && 
1645                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1646                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1649                 if ( NRZrawDemod("0 1 1", false)  && 
1650                           preambleSearchEx(DemodBuffer
,preamble
,sizeof(preamble
),&DemodBufferLen
,&startIdx
,false) &&  
1651                           (DemodBufferLen 
== 32 || DemodBufferLen 
== 64) ) { 
1657 //  does this need to be a callable command? 
1658 int CmdT55xxDetectPage1(const char *Cmd
){ 
1659         bool errors 
= false; 
1661         bool usepwd 
= false; 
1662         uint32_t password 
= 0; 
1665         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) { 
1666                 switch(param_getchar(Cmd
, cmdp
)) { 
1669                         return usage_t55xx_detectP1(); 
1672                         password 
= param_get32ex(Cmd
, cmdp
+1, 0, 16); 
1677                         // use Graphbuffer data 
1682                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
1687         if (errors
) return usage_t55xx_detectP1(); 
1690                 if ( !AquireData(T55x7_PAGE1
, 1, usepwd
, password
) ) 
1693         bool success 
= tryDetectP1(false); 
1694         if (success
) PrintAndLog("T55xx chip found!"); 
1698 static command_t CommandTable
[] = { 
1699   {"help",      CmdHelp
,           1, "This help"}, 
1700         {"bruteforce",CmdT55xxBruteForce
,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"}, 
1701   {"config",    CmdT55xxSetConfig
, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"}, 
1702   {"detect",    CmdT55xxDetect
,    1, "[1] Try detecting the tag modulation from reading the configuration block."}, 
1703   {"p1detect",  CmdT55xxDetectPage1
,1, "[1] Try detecting if this is a t55xx tag by reading page 1"}, 
1704   {"read",      CmdT55xxReadBlock
, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"}, 
1705   {"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)"}, 
1706   {"write",     CmdT55xxWriteBlock
,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"}, 
1707   {"trace",     CmdT55xxReadTrace
, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"}, 
1708   {"info",      CmdT55xxInfo
,      1, "[1] Show T55x7 configuration data (page 0/ blk 0)"}, 
1709   {"dump",      CmdT55xxDump
,      0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"}, 
1710   {"special",   special
,           0, "Show block changes with 64 different offsets"}, 
1711   {"wakeup",    CmdT55xxWakeUp
,    0, "Send AOR wakeup command"}, 
1712   {"wipe",      CmdT55xxWipe
,      0, "[q] Wipe a T55xx tag and set defaults (will destroy any data on tag)"}, 
1713   {NULL
, NULL
, 0, NULL
} 
1716 int CmdLFT55XX(const char *Cmd
) { 
1717   CmdsParse(CommandTable
, Cmd
); 
1721 int CmdHelp(const char *Cmd
) { 
1722   CmdsHelp(CommandTable
);