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 
  32 // Default configuration 
  33 t55xx_conf_block_t config 
= { .modulation 
= DEMOD_ASK
, .inverted 
= FALSE
, .offset 
= 0x00, .block0 
= 0x00}; 
  35 int usage_t55xx_config(){ 
  36         PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>]"); 
  37         PrintAndLog("Options:        "); 
  38         PrintAndLog("       h                        This help"); 
  39         PrintAndLog("       b <8|16|32|40|50|64|100|128>     Set bitrate"); 
  40         PrintAndLog("       d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NZ|BI|BIa>  Set demodulation FSK / ASK / PSK / NZ / Biphase / Biphase A"); 
  41         PrintAndLog("       i [1]                            Invert data signal, defaults to normal"); 
  42         PrintAndLog("       o [offset]                       Set offset, where data should start decode in bitstream"); 
  44         PrintAndLog("Examples:"); 
  45         PrintAndLog("      lf t55xx config d FSK          - FSK demodulation"); 
  46         PrintAndLog("      lf t55xx config d FSK i 1      - FSK demodulation, inverse data"); 
  47         PrintAndLog("      lf t55xx config d FSK i 1 o 3  - FSK demodulation, inverse data, offset=3,start from position 3 to decode data"); 
  51 int usage_t55xx_read(){ 
  52         PrintAndLog("Usage:  lf t55xx read <block> <password>"); 
  53         PrintAndLog("     <block>, block number to read. Between 0-7"); 
  54         PrintAndLog("     <password>, OPTIONAL password (8 hex characters)"); 
  56         PrintAndLog("Examples:"); 
  57         PrintAndLog("      lf t55xx read 0           - read data from block 0"); 
  58         PrintAndLog("      lf t55xx read 0 feedbeef  - read data from block 0 password feedbeef"); 
  62 int usage_t55xx_write(){ 
  63         PrintAndLog("Usage:  lf t55xx wr <block> <data> [password]"); 
  64         PrintAndLog("     <block>, block number to write. Between 0-7"); 
  65         PrintAndLog("     <data>,  4 bytes of data to write (8 hex characters)"); 
  66         PrintAndLog("     [password], OPTIONAL password 4bytes (8 hex characters)"); 
  68         PrintAndLog("Examples:"); 
  69         PrintAndLog("      lf t55xx wr 3 11223344           - write 11223344 to block 3"); 
  70         PrintAndLog("      lf t55xx wr 3 11223344 feedbeef  - write 11223344 to block 3 password feedbeef"); 
  74 int usage_t55xx_trace() { 
  75         PrintAndLog("Usage:  lf t55xx trace [1]"); 
  76         PrintAndLog("     [graph buffer data], if set, use Graphbuffer otherwise read data from tag."); 
  78         PrintAndLog("Examples:"); 
  79         PrintAndLog("      lf t55xx trace"); 
  80         PrintAndLog("      lf t55xx trace 1"); 
  84 int usage_t55xx_info() { 
  85         PrintAndLog("Usage:  lf t55xx info [1]"); 
  86         PrintAndLog("     [graph buffer data], if set, use Graphbuffer otherwise read data from tag."); 
  88         PrintAndLog("Examples:"); 
  89         PrintAndLog("      lf t55xx info"); 
  90         PrintAndLog("      lf t55xx info 1"); 
  94 int usage_t55xx_dump(){ 
  95         PrintAndLog("Usage:  lf t55xx dump <password>"); 
  96     PrintAndLog("     <password>, OPTIONAL password 4bytes (8 hex symbols)"); 
  98         PrintAndLog("Examples:"); 
  99         PrintAndLog("      lf t55xx dump"); 
 100         PrintAndLog("      lf t55xx dump feedbeef"); 
 104 int usage_t55xx_detect(){ 
 105         PrintAndLog("Usage:  lf t55xx detect"); 
 107         PrintAndLog("Examples:"); 
 108         PrintAndLog("      lf t55xx detect"); 
 109         PrintAndLog("      lf t55xx detect 1"); 
 114 static int CmdHelp(const char *Cmd
); 
 116 int CmdT55xxSetConfig(const char *Cmd
) { 
 121         char modulation
[5] = {0x00}; 
 124         uint8_t rates
[9] = {8,16,32,40,50,64,100,128,0}; 
 125         while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
) 
 127                 tmp 
= param_getchar(Cmd
, cmdp
); 
 132                         return usage_t55xx_config(); 
 134                         errors 
|= param_getdec(Cmd
, cmdp
+1, &bitRate
); 
 138                                         if (rates
[i
]==bitRate
) { 
 143                                 if (i
==9) errors 
= TRUE
; 
 148                         param_getstr(Cmd
, cmdp
+1, modulation
); 
 151                         if ( strcmp(modulation
, "FSK" ) == 0) { 
 152                                 config
.modulation 
= DEMOD_FSK
; 
 153                         } else if ( strcmp(modulation
, "FSK1" ) == 0) { 
 154                                 config
.modulation 
= DEMOD_FSK1
; 
 156                         } else if ( strcmp(modulation
, "FSK1a" ) == 0) { 
 157                                 config
.modulation 
= DEMOD_FSK1a
; 
 159                         } else if ( strcmp(modulation
, "FSK2" ) == 0) { 
 160                                 config
.modulation 
= DEMOD_FSK2
; 
 162                         } else if ( strcmp(modulation
, "FSK2a" ) == 0) { 
 163                                 config
.modulation 
= DEMOD_FSK2a
; 
 165                         } else if ( strcmp(modulation
, "ASK" ) == 0) { 
 166                                 config
.modulation 
= DEMOD_ASK
; 
 167                         } else if ( strcmp(modulation
, "NRZ" ) == 0) { 
 168                                 config
.modulation 
= DEMOD_NRZ
; 
 169                         } else if ( strcmp(modulation
, "PSK1" ) == 0) { 
 170                                 config
.modulation 
= DEMOD_PSK1
; 
 171                         } else if ( strcmp(modulation
, "PSK2" ) == 0) { 
 172                                 config
.modulation 
= DEMOD_PSK2
; 
 173                         } else if ( strcmp(modulation
, "PSK3" ) == 0) { 
 174                                 config
.modulation 
= DEMOD_PSK3
; 
 175                         } else if ( strcmp(modulation
, "BIa" ) == 0) { 
 176                                 config
.modulation 
= DEMOD_BIa
; 
 178                         } else if ( strcmp(modulation
, "BI" ) == 0) { 
 179                                 config
.modulation 
= DEMOD_BI
; 
 182                                 PrintAndLog("Unknown modulation '%s'", modulation
); 
 187                         config
.inverted 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 191                         errors 
|= param_getdec(Cmd
, cmdp
+1, &offset
); 
 193                                 config
.offset 
= offset
; 
 197                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 205                 printConfiguration( config 
); 
 210                 return usage_t55xx_config(); 
 213         printConfiguration ( config 
); 
 217 int CmdT55xxReadBlock(const char *Cmd
) { 
 219         int password 
= 0xFFFFFFFF; //default to blank Block 7 
 221         char cmdp 
= param_getchar(Cmd
, 0); 
 222         if (cmdp 
== 'h' || cmdp 
== 'H') 
 223                 return usage_t55xx_read(); 
 225         int res 
= sscanf(Cmd
, "%d %x", &block
, &password
); 
 227         if ( res 
< 1 || res 
> 2 ) 
 228                 return usage_t55xx_read(); 
 231         if ((block 
< 0) | (block 
> 7)) { 
 232                 PrintAndLog("Block must be between 0 and 7"); 
 236         UsbCommand c 
= {CMD_T55XX_READ_BLOCK
, {0, block
, 0}}; 
 237         c
.d
.asBytes
[0] = 0x0;  
 242                 c
.d
.asBytes
[0] = 0x1;  
 245         clearCommandBuffer(); 
 247         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
 248                 PrintAndLog("command execution time out"); 
 253         GetFromBigBuf(got
,sizeof(got
),0); 
 254         WaitForResponse(CMD_ACK
,NULL
); 
 255         setGraphBuf(got
, 12000); 
 257         if (!DecodeT55xxBlock()) return 3; 
 259         sprintf(blk
,"%d", block
); 
 260         printT55xxBlock(blk
); 
 264 bool DecodeT55xxBlock(){ 
 266         char buf
[30] = {0x00}; 
 269         uint8_t bitRate
[8] = {8,16,32,40,50,64,100,128}; 
 270         DemodBufferLen 
= 0x00; 
 272         //trim 1/2 a clock from beginning 
 273         snprintf(cmdStr
, sizeof(buf
),"%d", bitRate
[config
.bitrate
]/2 ); 
 275         switch( config
.modulation 
){ 
 277                         snprintf(cmdStr
, sizeof(buf
),"%d %d", bitRate
[config
.bitrate
], config
.inverted 
); 
 278                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 282                         snprintf(cmdStr
, sizeof(buf
),"%d %d 8 5", bitRate
[config
.bitrate
], config
.inverted 
); 
 283                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 287                         snprintf(cmdStr
, sizeof(buf
),"%d %d 10 8", bitRate
[config
.bitrate
], config
.inverted 
); 
 288                         ans 
= FSKrawDemod(cmdStr
, FALSE
); 
 291                         snprintf(cmdStr
, sizeof(buf
),"%d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 292                         ans 
= ASKDemod(cmdStr
, FALSE
, FALSE
, 1); 
 295                         snprintf(cmdStr
, sizeof(buf
),"%d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 296                         ans 
= PSKDemod(cmdStr
, FALSE
); 
 298                 case DEMOD_PSK2
: //inverted won't affect this 
 299                 case DEMOD_PSK3
: //not fully implemented 
 300                         snprintf(cmdStr
, sizeof(buf
),"%d 0 1", bitRate
[config
.bitrate
] ); 
 301                         ans 
= PSKDemod(cmdStr
, FALSE
); 
 302                         psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 305                         snprintf(cmdStr
, sizeof(buf
),"%d %d 1", bitRate
[config
.bitrate
], config
.inverted 
); 
 306                         ans 
= NRZrawDemod(cmdStr
, FALSE
); 
 310                         snprintf(cmdStr
, sizeof(buf
),"0 %d %d 0", bitRate
[config
.bitrate
], config
.inverted 
); 
 311                         ans 
= ASKbiphaseDemod(cmdStr
, FALSE
); 
 319 int CmdT55xxDetect(const char *Cmd
){ 
 321         char cmdp 
= param_getchar(Cmd
, 0); 
 322         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') 
 323                 return usage_t55xx_detect(); 
 326                 AquireData( CONFIGURATION_BLOCK 
); 
 328         if ( !tryDetectModulation() ) 
 329                 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'"); 
 334 // detect configuration? 
 335 bool tryDetectModulation(){ 
 336         char cmdStr
[8] = {0}; 
 338         t55xx_conf_block_t tests
[15]; 
 340         uint8_t fc1 
= 0, fc2 
= 0, clk
=0; 
 342         if (GetFskClock("", FALSE
, FALSE
)){  
 343                 fskClocks(&fc1
, &fc2
, &clk
, FALSE
); 
 344                 sprintf(cmdStr
,"%d", clk
/2); 
 346                 if ( FSKrawDemod("0 0", FALSE
) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
)){ 
 347                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 348                         if (fc1
==8 && fc2 
== 5) 
 349                                 tests
[hits
].modulation 
= DEMOD_FSK1a
; 
 350                         else if (fc1
==10 && fc2 
== 8) 
 351                                 tests
[hits
].modulation 
= DEMOD_FSK2
; 
 352                         tests
[hits
].bitrate 
= bitRate
; 
 353                         tests
[hits
].inverted 
= FALSE
; 
 354                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 357                 if ( FSKrawDemod("0 1", FALSE
) && test(DEMOD_FSK
, &tests
[hits
].offset
, &bitRate
)) { 
 358                         tests
[hits
].modulation 
= DEMOD_FSK
; 
 359                         if (fc1 
== 8 && fc2 
== 5) 
 360                                 tests
[hits
].modulation 
= DEMOD_FSK1
; 
 361                         else if (fc1 
== 10 && fc2 
== 8) 
 362                                 tests
[hits
].modulation 
= DEMOD_FSK2a
; 
 364                         tests
[hits
].bitrate 
= bitRate
; 
 365                         tests
[hits
].inverted 
= TRUE
; 
 366                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 370                 clk 
= GetAskClock("", FALSE
, FALSE
); 
 372                         sprintf(cmdStr
,"%d", clk
/2); 
 374                         if ( ASKDemod("0 0 0", FALSE
, FALSE
, 1) && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
)) { 
 375                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 376                                 tests
[hits
].bitrate 
= bitRate
; 
 377                                 tests
[hits
].inverted 
= FALSE
; 
 378                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 381                         if ( ASKDemod("0 1 0", FALSE
, FALSE
, 1)  && test(DEMOD_ASK
, &tests
[hits
].offset
, &bitRate
)) { 
 382                                 tests
[hits
].modulation 
= DEMOD_ASK
; 
 383                                 tests
[hits
].bitrate 
= bitRate
; 
 384                                 tests
[hits
].inverted 
= TRUE
; 
 385                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 388                         if ( ASKbiphaseDemod("0 0 0 0", FALSE
) && test(DEMOD_BI
, &tests
[hits
].offset
, &bitRate
) ) { 
 389                                 tests
[hits
].modulation 
= DEMOD_BI
; 
 390                                 tests
[hits
].bitrate 
= bitRate
; 
 391                                 tests
[hits
].inverted 
= FALSE
; 
 392                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 395                         if ( ASKbiphaseDemod("0 0 1 0", FALSE
) && test(DEMOD_BIa
, &tests
[hits
].offset
, &bitRate
) ) { 
 396                                 tests
[hits
].modulation 
= DEMOD_BIa
; 
 397                                 tests
[hits
].bitrate 
= bitRate
; 
 398                                 tests
[hits
].inverted 
= TRUE
; 
 399                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 405                 clk 
= GetNrzClock("", FALSE
, FALSE
); 
 407                         sprintf(cmdStr
,"%d", clk
/2); 
 409                         if ( NRZrawDemod("0 0 1", FALSE
)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
)) { 
 410                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 411                                 tests
[hits
].bitrate 
= bitRate
; 
 412                                 tests
[hits
].inverted 
= FALSE
; 
 413                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 417                         if ( NRZrawDemod("0 1 1", FALSE
)  && test(DEMOD_NRZ
, &tests
[hits
].offset
, &bitRate
)) { 
 418                                 tests
[hits
].modulation 
= DEMOD_NRZ
; 
 419                                 tests
[hits
].bitrate 
= bitRate
; 
 420                                 tests
[hits
].inverted 
= TRUE
; 
 421                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 428                 clk 
= GetPskClock("", FALSE
, FALSE
); 
 430                         PrintAndLog("clk %d",clk
); 
 431                         sprintf(cmdStr
,"%d", clk
/2); 
 433                         if ( PSKDemod("0 0 1", FALSE
) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
)) { 
 434                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 435                                 tests
[hits
].bitrate 
= bitRate
; 
 436                                 tests
[hits
].inverted 
= FALSE
; 
 437                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 440                         if ( PSKDemod("0 1 1", FALSE
) && test(DEMOD_PSK1
, &tests
[hits
].offset
, &bitRate
)) { 
 441                                 tests
[hits
].modulation 
= DEMOD_PSK1
; 
 442                                 tests
[hits
].bitrate 
= bitRate
; 
 443                                 tests
[hits
].inverted 
= TRUE
; 
 444                                 tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 447                         // PSK2 - needs a call to psk1TOpsk2. 
 448                         if ( PSKDemod("0 0 1", FALSE
)) { 
 449                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 450                                 if (test(DEMOD_PSK2
, &tests
[hits
].offset
, &bitRate
)){ 
 451                                         tests
[hits
].modulation 
= DEMOD_PSK2
; 
 452                                         tests
[hits
].bitrate 
= bitRate
; 
 453                                         tests
[hits
].inverted 
= FALSE
; 
 454                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 457                         } // inverse waves does not affect this demod 
 458                         // PSK3 - needs a call to psk1TOpsk2. 
 459                         if ( PSKDemod("0 0 1", FALSE
)) { 
 460                                 psk1TOpsk2(DemodBuffer
, DemodBufferLen
); 
 461                                 if (test(DEMOD_PSK3
, &tests
[hits
].offset
, &bitRate
)){ 
 462                                         tests
[hits
].modulation 
= DEMOD_PSK3
; 
 463                                         tests
[hits
].bitrate 
= bitRate
; 
 464                                         tests
[hits
].inverted 
= FALSE
; 
 465                                         tests
[hits
].block0 
= PackBits(tests
[hits
].offset
, 32, DemodBuffer
); 
 468                         } // inverse waves does not affect this demod 
 472                 config
.modulation 
= tests
[0].modulation
; 
 473                 config
.bitrate 
= tests
[0].bitrate
; 
 474                 config
.inverted 
= tests
[0].inverted
; 
 475                 config
.offset 
= tests
[0].offset
; 
 476                 config
.block0 
= tests
[0].block0
; 
 477                 printConfiguration( config 
); 
 482                 PrintAndLog("Found [%d] possible matches for modulation.",hits
); 
 483                 for(int i
=0; i
<hits
; ++i
){ 
 484                         PrintAndLog("--[%d]---------------", i
+1); 
 485                         printConfiguration( tests
[i
] ); 
 491 bool testModulation(uint8_t mode
, uint8_t modread
){ 
 494                         if (modread 
> 3 && modread 
< 8) return TRUE
; 
 497                         if (modread 
== DEMOD_ASK
) return TRUE
; 
 500                         if (modread 
== DEMOD_PSK1
) return TRUE
; 
 503                         if (modread 
== DEMOD_PSK2
) return TRUE
; 
 506                         if (modread 
== DEMOD_PSK3
) return TRUE
; 
 509                         if (modread 
== DEMOD_NRZ
) return TRUE
; 
 512                         if (modread 
== DEMOD_BI
) return TRUE
; 
 515                         if (modread 
== DEMOD_BIa
) return TRUE
; 
 523 bool testBitRate(uint8_t readRate
, uint8_t mod
){ 
 524         uint8_t expected
[8] = {8, 16, 32, 40, 50, 64, 100, 128}; 
 532                         detRate 
= GetFskClock("",FALSE
, FALSE
);  
 533                         if (expected
[readRate
] == detRate
)  
 539                         detRate 
= GetAskClock("",FALSE
, FALSE
);  
 540                         if (expected
[readRate
] == detRate
)  
 546                         detRate 
= GetPskClock("",FALSE
, FALSE
);  
 547                         if (expected
[readRate
] == detRate
) 
 551                         detRate 
= GetNrzClock("",FALSE
, FALSE
);  
 552                         if (expected
[readRate
] == detRate
) 
 561 bool test(uint8_t mode
, uint8_t *offset
, int *fndBitRate
){ 
 563         if ( DemodBufferLen 
< 64 ) return FALSE
; 
 565         for (uint8_t idx 
= 0; idx 
< 64; idx
++){ 
 567                 if ( PackBits(si
, 32, DemodBuffer
) == 0x00 ) continue; 
 569                 uint8_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //master key 
 570                 uint8_t resv     
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;     //was 7 & +=7+3 //should be only 4 bits if extended mode 
 571                 // 2nibble must be zeroed. 
 572                 // moved test to here, since this gets most faults first. 
 573                 if ( resv 
> 0x00) continue; 
 575                 uint8_t xtRate   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3;     //extended mode part of rate 
 576                 int bitRate  
= PackBits(si
, 3, DemodBuffer
); si 
+= 3;     //bit rate 
 577                 if (bitRate 
> 7) continue; 
 578                 uint8_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;     //bit 15 extended mode 
 579                 uint8_t modread  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5+2+1;  
 580                 //uint8_t pskcr   = PackBits(si, 2, DemodBuffer); si += 2+1;  //could check psk cr 
 581                 uint8_t nml01    
= PackBits(si
, 1, DemodBuffer
); si 
+= 1+5;   //bit 24, 30, 31 could be tested for 0 if not extended mode 
 582                 uint8_t nml02    
= PackBits(si
, 2, DemodBuffer
); si 
+= 2; 
 585                 bool extMode 
=( (safer 
== 0x6 || safer 
== 0x9) && extend
) ? TRUE 
: FALSE
; 
 588                         if (nml01 
|| nml02 
|| xtRate
) continue; 
 591                 if (!testModulation(mode
, modread
)) continue; 
 592                 if (!testBitRate(bitRate
, mode
)) continue; 
 593                 *fndBitRate 
= bitRate
; 
 600 void printT55xxBlock(const char *blockNum
){ 
 602         uint8_t i 
= config
.offset
; 
 603         uint8_t endpos 
= 32 + i
; 
 604         uint32_t blockData 
= 0; 
 605         uint8_t bits
[64] = {0x00}; 
 607         if ( !DemodBufferLen
) return; 
 609         if ( endpos 
> DemodBufferLen
){ 
 610                 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i
, DemodBufferLen
-32); 
 614         for (; i 
< endpos
; ++i
) 
 615                 bits
[i 
- config
.offset
]=DemodBuffer
[i
]; 
 617         blockData 
= PackBits(0, 32, bits
); 
 618         PrintAndLog("[%s] 0x%08X  %s", blockNum
, blockData
, sprint_bin(bits
,32)); 
 621 int special(const char *Cmd
) { 
 622         uint32_t blockData 
= 0; 
 623         uint8_t bits
[32] = {0x00}; 
 625         PrintAndLog("[OFFSET] [DATA] [BINARY]"); 
 626         PrintAndLog("----------------------------------------------------"); 
 630                 for (i 
= 0; i 
< 32; ++i
) 
 631                         bits
[i
]=DemodBuffer
[j
+i
]; 
 633                 blockData 
= PackBits(0, 32, bits
); 
 635                 PrintAndLog("[%02d] 0x%08X  %s",j 
, blockData
, sprint_bin(bits
,32));     
 640 void printConfiguration( t55xx_conf_block_t b
){ 
 641         PrintAndLog("Modulation : %s", GetSelectedModulationStr(b
.modulation
) ); 
 642         PrintAndLog("Bit Rate   : %s", GetBitRateStr(b
.bitrate
) ); 
 643         PrintAndLog("Inverted   : %s", (b
.inverted
) ? "Yes" : "No" ); 
 644         PrintAndLog("Offset     : %d", b
.offset
); 
 645         PrintAndLog("Block0     : 0x%08X", b
.block0
); 
 649 int CmdT55xxWriteBlock(const char *Cmd
) 
 651         int block 
= 8; //default to invalid block 
 652         int data 
= 0xFFFFFFFF; //default to blank Block  
 653         int password 
= 0xFFFFFFFF; //default to blank Block 7 
 655         char cmdp 
= param_getchar(Cmd
, 0); 
 656         if (cmdp 
== 'h' || cmdp 
== 'H') { 
 661         int res 
= sscanf(Cmd
, "%d %x %x",&block
, &data
, &password
); 
 663         if ( res 
< 2 || res 
> 3) { 
 669                 PrintAndLog("Block number must be between 0 and 7"); 
 673         UsbCommand c 
= {CMD_T55XX_WRITE_BLOCK
, {data
, block
, 0}}; 
 675         c
.d
.asBytes
[0] = 0x0;  
 677         PrintAndLog("Writing to block: %d  data  : 0x%08X", block
, data
); 
 682                 c
.d
.asBytes
[0] = 0x1;  
 683                 PrintAndLog("pwd   : 0x%08X", password
); 
 685         clearCommandBuffer(); 
 687         if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)){ 
 688                 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)"); 
 694 int CmdT55xxReadTrace(const char *Cmd
) 
 696         char cmdp 
= param_getchar(Cmd
, 0); 
 698         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H')  
 699                 return usage_t55xx_trace(); 
 702                 AquireData( TRACE_BLOCK 
); 
 704         if (!DecodeT55xxBlock()) return 1; 
 706         if ( !DemodBufferLen
) return 1; 
 708         RepaintGraphWindow(); 
 710         if (config
.offset 
> 5)  
 712         uint8_t si 
= config
.offset
+repeat
; 
 713         uint32_t bl0     
= PackBits(si
, 32, DemodBuffer
); 
 714         uint32_t bl1     
= PackBits(si
+32, 32, DemodBuffer
); 
 716         uint32_t acl     
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
 717         uint32_t mfc     
= PackBits(si
, 8,  DemodBuffer
); si 
+= 8; 
 718         uint32_t cid     
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
 719         uint32_t icr     
= PackBits(si
, 3,  DemodBuffer
); si 
+= 3; 
 720         uint32_t year    
= PackBits(si
, 4,  DemodBuffer
); si 
+= 4; 
 721         uint32_t quarter 
= PackBits(si
, 2,  DemodBuffer
); si 
+= 2; 
 722         uint32_t lotid   
= PackBits(si
, 14, DemodBuffer
); si 
+= 14; 
 723         uint32_t wafer   
= PackBits(si
, 5,  DemodBuffer
); si 
+= 5; 
 724         uint32_t dw      
= PackBits(si
, 15, DemodBuffer
);  
 726         time_t t 
= time(NULL
); 
 727         struct tm tm 
= *localtime(&t
); 
 728         if ( year 
> tm
.tm_year
-110) 
 734                 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. "); 
 739         PrintAndLog("-- T55xx Trace Information ----------------------------------"); 
 740         PrintAndLog("-------------------------------------------------------------"); 
 741         PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1)  : 0x%02X (%d)", acl
, acl
); 
 742         PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6)    : 0x%02X (%d) - %s", mfc
, mfc
, getTagInfo(mfc
)); 
 743         PrintAndLog(" CID                                     : 0x%02X (%d) - %s", cid
, cid
, GetModelStrFromCID(cid
)); 
 744         PrintAndLog(" ICR IC Revision                         : %d",icr 
); 
 745         PrintAndLog(" Manufactured"); 
 746         PrintAndLog("     Year/Quarter : %d/%d",year
, quarter
); 
 747         PrintAndLog("     Lot ID       : %d", lotid 
); 
 748         PrintAndLog("     Wafer number : %d", wafer
); 
 749         PrintAndLog("     Die Number   : %d", dw
); 
 750         PrintAndLog("-------------------------------------------------------------"); 
 751         PrintAndLog(" Raw Data - Page 1"); 
 752         PrintAndLog("     Block 0  : 0x%08X  %s", bl0
, sprint_bin(DemodBuffer
+config
.offset
+repeat
,32) ); 
 753         PrintAndLog("     Block 1  : 0x%08X  %s", bl1
, sprint_bin(DemodBuffer
+config
.offset
+repeat
+32,32) ); 
 754         PrintAndLog("-------------------------------------------------------------"); 
 759                 1-8             ACL Allocation class (ISO/IEC 15963-1)  0xE0  
 760                 9-16    MFC Manufacturer ID (ISO/IEC 7816-6)    0x15 Atmel Corporation 
 761                 17-21   CID                                                                             0x1 = Atmel ATA5577M1  0x2 = Atmel ATA5577M2  
 762                 22-24   ICR IC revision 
 763                 25-28   YEAR (BCD encoded)                                              9 (= 2009) 
 764                 29-30   QUARTER                                                                 1,2,3,4  
 770                 18-32   DW,  die number sequential 
 776 int CmdT55xxInfo(const char *Cmd
){ 
 778                 Page 0 Block 0 Configuration data. 
 782         char cmdp 
= param_getchar(Cmd
, 0); 
 784         if (strlen(Cmd
) > 1 || cmdp 
== 'h' || cmdp 
== 'H') 
 785                 return usage_t55xx_info(); 
 788                 AquireData( CONFIGURATION_BLOCK 
); 
 790         if (!DecodeT55xxBlock()) return 1; 
 792         if ( DemodBufferLen 
< 32) return 1; 
 794         uint8_t si 
= config
.offset
; 
 795         uint32_t bl0      
= PackBits(si
, 32, DemodBuffer
); 
 797         uint32_t safer    
= PackBits(si
, 4, DemodBuffer
); si 
+= 4;       
 798         uint32_t resv     
= PackBits(si
, 7, DemodBuffer
); si 
+= 7; 
 799         uint32_t dbr      
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 800         uint32_t extend   
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 801         uint32_t datamod  
= PackBits(si
, 5, DemodBuffer
); si 
+= 5; 
 802         uint32_t pskcf    
= PackBits(si
, 2, DemodBuffer
); si 
+= 2; 
 803         uint32_t aor      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 804         uint32_t otp      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 805         uint32_t maxblk   
= PackBits(si
, 3, DemodBuffer
); si 
+= 3; 
 806         uint32_t pwd      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 807         uint32_t sst      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 808         uint32_t fw       
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 809         uint32_t inv      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1;       
 810         uint32_t por      
= PackBits(si
, 1, DemodBuffer
); si 
+= 1; 
 813         PrintAndLog("-- T55xx Configuration & Tag Information --------------------"); 
 814         PrintAndLog("-------------------------------------------------------------"); 
 815         PrintAndLog(" Safer key                 : %s", GetSaferStr(safer
)); 
 816         PrintAndLog(" reserved                  : %d", resv
); 
 817         PrintAndLog(" Data bit rate             : %s", GetBitRateStr(dbr
)); 
 818         PrintAndLog(" eXtended mode             : %s", (extend
) ? "Yes - Warning":"No"); 
 819         PrintAndLog(" Modulation                : %s", GetModulationStr(datamod
)); 
 820         PrintAndLog(" PSK clock frequency       : %d", pskcf
); 
 821         PrintAndLog(" AOR - Answer on Request   : %s", (aor
) ? "Yes":"No"); 
 822         PrintAndLog(" OTP - One Time Pad        : %s", (otp
) ? "Yes - Warning":"No" ); 
 823         PrintAndLog(" Max block                 : %d", maxblk
); 
 824         PrintAndLog(" Password mode             : %s", (pwd
) ? "Yes":"No"); 
 825         PrintAndLog(" Sequence Start Terminator : %s", (sst
) ? "Yes":"No"); 
 826         PrintAndLog(" Fast Write                : %s", (fw
)  ? "Yes":"No"); 
 827         PrintAndLog(" Inverse data              : %s", (inv
) ? "Yes":"No"); 
 828         PrintAndLog(" POR-Delay                 : %s", (por
) ? "Yes":"No"); 
 829         PrintAndLog("-------------------------------------------------------------"); 
 830         PrintAndLog(" Raw Data - Page 0"); 
 831         PrintAndLog("     Block 0  : 0x%08X  %s", bl0
, sprint_bin(DemodBuffer
+config
.offset
,32) ); 
 832         PrintAndLog("-------------------------------------------------------------"); 
 837 int CmdT55xxDump(const char *Cmd
){ 
 840         uint8_t pwd
[4] = {0x00}; 
 842         char cmdp 
= param_getchar(Cmd
, 0); 
 843         if ( cmdp 
== 'h' || cmdp 
== 'H') { 
 848         bool hasPwd 
= ( strlen(Cmd
) > 0);        
 850                 if (param_gethex(Cmd
, 0, pwd
, 8)) { 
 851                         PrintAndLog("password must include 8 HEX symbols"); 
 856         for ( int i 
= 0; i 
<8; ++i
){ 
 857                 memset(s
,0,sizeof(s
)); 
 859                         sprintf(s
,"%d %02x%02x%02x%02x", i
, pwd
[0],pwd
[1],pwd
[2],pwd
[3]); 
 863                 CmdT55xxReadBlock(s
); 
 868 int AquireData( uint8_t block 
){ 
 872         if ( block 
== CONFIGURATION_BLOCK 
)  
 873                 c
.cmd 
= CMD_T55XX_READ_BLOCK
; 
 874         else if (block 
== TRACE_BLOCK 
) 
 875                 c
.cmd 
= CMD_T55XX_READ_TRACE
; 
 880         c
.d
.asBytes
[0] = 0x0;  
 884                 // c.arg[2] = password; 
 885                 // c.d.asBytes[0] = 0x1;  
 888         clearCommandBuffer(); 
 890         if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
 891                 PrintAndLog("command execution time out"); 
 896         GetFromBigBuf(got
,sizeof(got
),0); 
 897         WaitForResponse(CMD_ACK
,NULL
); 
 898         setGraphBuf(got
, 12000); 
 902 char * GetBitRateStr(uint32_t id
){ 
 908                         snprintf(retStr
,sizeof(buf
),"%d - RF/8",id
); 
 911                         snprintf(retStr
,sizeof(buf
),"%d - RF/16",id
); 
 914                         snprintf(retStr
,sizeof(buf
),"%d - RF/32",id
); 
 917                         snprintf(retStr
,sizeof(buf
),"%d - RF/40",id
); 
 920                         snprintf(retStr
,sizeof(buf
),"%d - RF/50",id
); 
 923                         snprintf(retStr
,sizeof(buf
),"%d - RF/64",id
); 
 926                         snprintf(retStr
,sizeof(buf
),"%d - RF/100",id
); 
 929                         snprintf(retStr
,sizeof(buf
),"%d - RF/128",id
); 
 932                         snprintf(retStr
,sizeof(buf
),"%d - (Unknown)",id
); 
 939 char * GetSaferStr(uint32_t id
){ 
 943         snprintf(retStr
,sizeof(buf
),"%d",id
); 
 945                 snprintf(retStr
,sizeof(buf
),"%d - passwd",id
); 
 948                 snprintf(retStr
,sizeof(buf
),"%d - testmode",id
); 
 954 char * GetModulationStr( uint32_t id
){ 
 960                         snprintf(retStr
,sizeof(buf
),"%d - DIRECT (ASK/NRZ)",id
); 
 963                         snprintf(retStr
,sizeof(buf
),"%d - PSK 1 phase change when input changes",id
); 
 966                         snprintf(retStr
,sizeof(buf
),"%d - PSK 2 phase change on bitclk if input high",id
); 
 969                         snprintf(retStr
,sizeof(buf
),"%d - PSK 3 phase change on rising edge of input",id
); 
 972                         snprintf(retStr
,sizeof(buf
),"%d - FSK 1 RF/8  RF/5",id
); 
 975                         snprintf(retStr
,sizeof(buf
),"%d - FSK 2 RF/8  RF/10",id
); 
 978                         snprintf(retStr
,sizeof(buf
),"%d - FSK 1a RF/5  RF/8",id
); 
 981                         snprintf(retStr
,sizeof(buf
),"%d - FSK 2a RF/10  RF/8",id
); 
 984                         snprintf(retStr
,sizeof(buf
),"%d - Manchester",id
); 
 987                         snprintf(retStr
,sizeof(buf
),"%d - Biphase",id
); 
 990                         snprintf(retStr
,sizeof(buf
),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id
); 
 993                         snprintf(retStr
,sizeof(buf
),"%d - Reserved",id
); 
 996                         snprintf(retStr
,sizeof(buf
),"0x%02X (Unknown)",id
); 
1002 char * GetModelStrFromCID(uint32_t cid
){ 
1004         static char buf
[10]; 
1007         if (cid 
== 1) snprintf(retStr
, sizeof(buf
),"ATA5577M1"); 
1008         if (cid 
== 2) snprintf(retStr
, sizeof(buf
),"ATA5577M2");         
1012 char * GetSelectedModulationStr( uint8_t id
){ 
1014         static char buf
[20]; 
1019                         snprintf(retStr
,sizeof(buf
),"FSK"); 
1022                         snprintf(retStr
,sizeof(buf
),"FSK1"); 
1025                         snprintf(retStr
,sizeof(buf
),"FSK1a"); 
1028                         snprintf(retStr
,sizeof(buf
),"FSK2"); 
1031                         snprintf(retStr
,sizeof(buf
),"FSK2a"); 
1034                         snprintf(retStr
,sizeof(buf
),"ASK"); 
1037                         snprintf(retStr
,sizeof(buf
),"DIRECT/NRZ"); 
1040                         snprintf(retStr
,sizeof(buf
),"PSK1"); 
1043                         snprintf(retStr
,sizeof(buf
),"PSK2"); 
1046                         snprintf(retStr
,sizeof(buf
),"PSK3"); 
1049                         snprintf(retStr
,sizeof(buf
),"BIPHASE"); 
1052                         snprintf(retStr
,sizeof(buf
),"BIPHASEa - (CDP)"); 
1055                         snprintf(retStr
,sizeof(buf
),"(Unknown)"); 
1061 uint32_t PackBits(uint8_t start
, uint8_t len
, uint8_t* bits
){ 
1066         if (len 
> 32) return 0; 
1069         for (; j 
>= 0; --j
, ++i
) 
1070                 tmp     
|= bits
[i
] << j
; 
1075 static command_t CommandTable
[] = 
1077   {"help",   CmdHelp
,           1, "This help"}, 
1078   {"config", CmdT55xxSetConfig
, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"}, 
1079   {"detect", CmdT55xxDetect
,    0, "[1] Try detecting the tag modulation from reading the configuration block."}, 
1080   {"read",   CmdT55xxReadBlock
, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"}, 
1081   {"write",  CmdT55xxWriteBlock
,0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"}, 
1082   {"trace",  CmdT55xxReadTrace
, 0, "[1] Show T55xx traceability data (page 1/ blk 0-1)"}, 
1083   {"info",   CmdT55xxInfo
,      0, "[1] Show T55xx configuration data (page 0/ blk 0)"}, 
1084   {"dump",   CmdT55xxDump
,      0, "[password] Dump T55xx card block 0-7. [optional password]"}, 
1085   {"special", special
,          0, "Show block changes with 64 different offsets"}, 
1086   {NULL
, NULL
, 0, NULL
} 
1089 int CmdLFT55XX(const char *Cmd
) 
1091   CmdsParse(CommandTable
, Cmd
); 
1095 int CmdHelp(const char *Cmd
) 
1097   CmdsHelp(CommandTable
);