Added info to changelog about bootroom update
[legacy-proxmark3.git] / client / cmdlft55xx.c
blobb6b29c053240496b512951d4188f37c23997da29
1 //-----------------------------------------------------------------------------
2 //
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
5 // the license.
6 //-----------------------------------------------------------------------------
7 // Low frequency T55xx commands
8 //-----------------------------------------------------------------------------
10 #include <stdio.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include "proxmark3.h"
14 #include "ui.h"
15 #include "graph.h"
16 #include "cmdmain.h"
17 #include "cmdparser.h"
18 #include "cmddata.h"
19 #include "cmdlf.h"
20 #include "cmdlft55xx.h"
21 #include "util.h"
22 #include "data.h"
23 #include "lfdemod.h"
24 #include "../common/crc.h"
25 #include "../common/iso14443crc.h"
26 #include "cmdhf14a.h"
28 #define CONFIGURATION_BLOCK 0x00
29 #define TRACE_BLOCK 0x01
31 // Default configuration
32 t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = FALSE, .offset = 0x00, .block0 = 0x00};
34 int usage_t55xx_config(){
35 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>]");
36 PrintAndLog("Options: ");
37 PrintAndLog(" h This help");
38 PrintAndLog(" b <8|16|32|40|50|64|100|128> Set bitrate");
39 PrintAndLog(" d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NZ|BI|BIa> Set demodulation FSK / ASK / PSK / NZ / Biphase / Biphase A");
40 PrintAndLog(" i [1] Invert data signal, defaults to normal");
41 PrintAndLog(" o [offset] Set offset, where data should start decode in bitstream");
42 PrintAndLog("");
43 PrintAndLog("Examples:");
44 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");
45 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");
46 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");
47 PrintAndLog("");
48 return 0;
50 int usage_t55xx_read(){
51 PrintAndLog("Usage: lf t55xx read <block> <password>");
52 PrintAndLog(" <block>, block number to read. Between 0-7");
53 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");
54 PrintAndLog("");
55 PrintAndLog("Examples:");
56 PrintAndLog(" lf t55xx read 0 - read data from block 0");
57 PrintAndLog(" lf t55xx read 0 feedbeef - read data from block 0 password feedbeef");
58 PrintAndLog("");
59 return 0;
61 int usage_t55xx_write(){
62 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");
63 PrintAndLog(" <block>, block number to read. Between 0-7");
64 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");
65 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");
66 PrintAndLog("");
67 PrintAndLog("Examples:");
68 PrintAndLog(" lf t55xx wd 3 11223344 - write 11223344 to block 3");
69 PrintAndLog(" lf t55xx wd 3 11223344 feedbeef - write 11223344 to block 3 password feedbeef");
70 PrintAndLog("");
71 return 0;
73 int usage_t55xx_trace() {
74 PrintAndLog("Usage: lf t55xx trace [1]");
75 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
76 PrintAndLog("");
77 PrintAndLog("Examples:");
78 PrintAndLog(" lf t55xx trace");
79 PrintAndLog(" lf t55xx trace 1");
80 PrintAndLog("");
81 return 0;
83 int usage_t55xx_info() {
84 PrintAndLog("Usage: lf t55xx info [1]");
85 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
86 PrintAndLog("");
87 PrintAndLog("Examples:");
88 PrintAndLog(" lf t55xx info");
89 PrintAndLog(" lf t55xx info 1");
90 PrintAndLog("");
91 return 0;
93 int usage_t55xx_dump(){
94 PrintAndLog("Usage: lf t55xx dump <password>");
95 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex symbols)");
96 PrintAndLog("");
97 PrintAndLog("Examples:");
98 PrintAndLog(" lf t55xx dump");
99 PrintAndLog(" lf t55xx dump feedbeef");
100 PrintAndLog("");
101 return 0;
103 int usage_t55xx_detect(){
104 PrintAndLog("Usage: lf t55xx detect");
105 PrintAndLog("");
106 PrintAndLog("Examples:");
107 PrintAndLog(" lf t55xx detect");
108 PrintAndLog(" lf t55xx detect 1");
109 PrintAndLog("");
110 return 0;
113 static int CmdHelp(const char *Cmd);
115 int CmdT55xxSetConfig(const char *Cmd) {
117 uint8_t offset = 0;
118 bool errors = FALSE;
119 uint8_t cmdp = 0;
120 char modulation[5] = {0x00};
121 char tmp = 0x00;
122 uint8_t bitRate = 0;
123 uint8_t rates[9] = {8,16,32,40,50,64,100,128,0};
124 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)
126 tmp = param_getchar(Cmd, cmdp);
127 switch(tmp)
129 case 'h':
130 case 'H':
131 return usage_t55xx_config();
132 case 'b':
133 errors |= param_getdec(Cmd, cmdp+1, &bitRate);
134 if ( !errors){
135 uint8_t i = 0;
136 for (; i < 9; i++){
137 if (rates[i]==bitRate) {
138 config.bitrate = i;
139 break;
142 if (i==9) errors = TRUE;
144 cmdp+=2;
145 break;
146 case 'd':
147 param_getstr(Cmd, cmdp+1, modulation);
148 cmdp += 2;
150 if ( strcmp(modulation, "FSK" ) == 0)
151 config.modulation = DEMOD_FSK;
152 else if ( strcmp(modulation, "FSK1" ) == 0)
153 config.modulation = DEMOD_FSK1;
154 else if ( strcmp(modulation, "FSK1a" ) == 0)
155 config.modulation = DEMOD_FSK1a;
156 else if ( strcmp(modulation, "FSK2" ) == 0)
157 config.modulation = DEMOD_FSK2;
158 else if ( strcmp(modulation, "FSK2a" ) == 0)
159 config.modulation = DEMOD_FSK2a;
160 else if ( strcmp(modulation, "ASK" ) == 0)
161 config.modulation = DEMOD_ASK;
162 else if ( strcmp(modulation, "NRZ" ) == 0)
163 config.modulation = DEMOD_NRZ;
164 else if ( strcmp(modulation, "PSK1" ) == 0)
165 config.modulation = DEMOD_PSK1;
166 else if ( strcmp(modulation, "PSK2" ) == 0)
167 config.modulation = DEMOD_PSK2;
168 else if ( strcmp(modulation, "PSK3" ) == 0)
169 config.modulation = DEMOD_PSK3;
170 else if ( strcmp(modulation, "BIa" ) == 0)
171 config.modulation = DEMOD_BIa;
172 else if ( strcmp(modulation, "BI" ) == 0)
173 config.modulation = DEMOD_BI;
174 else {
175 PrintAndLog("Unknown modulation '%s'", modulation);
176 errors = TRUE;
178 break;
179 case 'i':
180 config.inverted = param_getchar(Cmd,cmdp+1) == '1';
181 cmdp+=2;
182 break;
183 case 'o':
184 errors |= param_getdec(Cmd, cmdp+1, &offset);
185 if ( !errors )
186 config.offset = offset;
187 cmdp+=2;
188 break;
189 default:
190 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
191 errors = TRUE;
192 break;
196 // No args
197 if (cmdp == 0) {
198 printConfiguration( config );
199 return 0;
201 //Validations
202 if (errors)
203 return usage_t55xx_config();
205 config.block0 = 0;
206 printConfiguration ( config );
207 return 0;
210 int CmdT55xxReadBlock(const char *Cmd) {
211 int block = -1;
212 int password = 0xFFFFFFFF; //default to blank Block 7
214 char cmdp = param_getchar(Cmd, 0);
215 if (cmdp == 'h' || cmdp == 'H')
216 return usage_t55xx_read();
218 int res = sscanf(Cmd, "%d %x", &block, &password);
220 if ( res < 1 || res > 2 )
221 return usage_t55xx_read();
224 if ((block < 0) | (block > 7)) {
225 PrintAndLog("Block must be between 0 and 7");
226 return 1;
229 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};
230 c.d.asBytes[0] = 0x0;
232 //Password mode
233 if ( res == 2 ) {
234 c.arg[2] = password;
235 c.d.asBytes[0] = 0x1;
238 SendCommand(&c);
239 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
240 PrintAndLog("command execution time out");
241 return 2;
244 uint8_t got[12000];
245 GetFromBigBuf(got,sizeof(got),0);
246 WaitForResponse(CMD_ACK,NULL);
247 setGraphBuf(got, 12000);
248 DemodBufferLen=0;
249 if (!DecodeT55xxBlock()) return 3;
250 char blk[10]={0};
251 sprintf(blk,"%d", block);
252 printT55xxBlock(blk);
253 return 0;
256 bool DecodeT55xxBlock(){
258 char buf[8] = {0x00};
259 char *cmdStr = buf;
260 int ans = 0;
261 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};
263 DemodBufferLen = 0x00;
265 switch( config.modulation ){
266 case DEMOD_FSK:
267 //CmdLtrim("26");
268 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
269 CmdLtrim(cmdStr);
270 sprintf(cmdStr,"%d %d", bitRate[config.bitrate], config.inverted );
271 ans = FSKrawDemod(cmdStr, FALSE);
272 break;
273 case DEMOD_FSK1:
274 //CmdLtrim("26");
275 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
276 CmdLtrim(cmdStr);
277 sprintf(cmdStr,"%d 1 8 5", bitRate[config.bitrate] );
278 ans = FSKrawDemod(cmdStr, FALSE);
279 break;
280 case DEMOD_FSK1a:
281 //CmdLtrim("26");
282 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
283 CmdLtrim(cmdStr);
284 sprintf(cmdStr,"%d 0 8 5", bitRate[config.bitrate] );
285 ans = FSKrawDemod(cmdStr, FALSE);
286 break;
287 case DEMOD_FSK2:
288 //CmdLtrim("26");
289 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
290 CmdLtrim(cmdStr);
291 sprintf(cmdStr,"%d 0 10 8", bitRate[config.bitrate] );
292 ans = FSKrawDemod(cmdStr, FALSE);
293 break;
294 case DEMOD_FSK2a:
295 //CmdLtrim("26");
296 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
297 CmdLtrim(cmdStr);
298 sprintf(cmdStr,"%d 1 10 8", bitRate[config.bitrate] );
299 ans = FSKrawDemod(cmdStr, FALSE);
300 break;
301 case DEMOD_ASK:
302 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
303 ans = ASKmanDemod(cmdStr, FALSE, FALSE);
304 break;
305 case DEMOD_PSK1:
306 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
307 ans = PSKDemod(cmdStr, FALSE);
308 break;
309 case DEMOD_PSK2:
310 sprintf(cmdStr,"%d 1", bitRate[config.bitrate] );
311 ans = PSKDemod(cmdStr, FALSE);
312 psk1TOpsk2(DemodBuffer, DemodBufferLen);
313 break;
314 case DEMOD_PSK3:
315 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
316 ans = PSKDemod(cmdStr, FALSE);
317 psk1TOpsk2(DemodBuffer, DemodBufferLen);
318 break;
319 case DEMOD_NRZ:
320 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
321 ans = NRZrawDemod(cmdStr, FALSE);
322 break;
323 case DEMOD_BI:
324 sprintf(cmdStr,"0 %d 0 1", bitRate[config.bitrate] );
325 ans = ASKbiphaseDemod(cmdStr, FALSE);
326 break;
327 case DEMOD_BIa:
328 sprintf(cmdStr,"0 %d 1 1", bitRate[config.bitrate] );
329 ans = ASKbiphaseDemod(cmdStr, FALSE);
330 break;
331 default:
332 return FALSE;
334 return (bool) ans;
337 int CmdT55xxDetect(const char *Cmd){
339 char cmdp = param_getchar(Cmd, 0);
340 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
341 return usage_t55xx_detect();
343 if (strlen(Cmd)==0)
344 AquireData( CONFIGURATION_BLOCK );
346 if ( !tryDetectModulation() )
347 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");
349 return 0;
352 // detect configuration?
353 bool tryDetectModulation(){
354 char cmdStr[8] = {0};
355 uint8_t hits = 0;
356 t55xx_conf_block_t tests[15];
358 if (GetFskClock("", FALSE, FALSE)){
359 uint8_t fc1 = 0, fc2 = 0, clk=0;
360 fskClocks(&fc1, &fc2, &clk, FALSE);
361 sprintf(cmdStr,"%d", clk/2);
362 CmdLtrim(cmdStr);
363 if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset)){
364 tests[hits].modulation = DEMOD_FSK;
365 if (fc1==8 && fc2 == 5)
366 tests[hits].modulation = DEMOD_FSK1a;
367 else if (fc1==10 && fc2 == 8)
368 tests[hits].modulation = DEMOD_FSK2;
370 tests[hits].inverted = FALSE;
371 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
372 ++hits;
374 if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset)) {
375 tests[hits].modulation = DEMOD_FSK;
376 if (fc1==8 && fc2 == 5)
377 tests[hits].modulation = DEMOD_FSK1;
378 else if (fc1==10 && fc2 == 8)
379 tests[hits].modulation = DEMOD_FSK2a;
381 tests[hits].inverted = TRUE;
382 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
383 ++hits;
385 } else {
386 if ( ASKmanDemod("0 0 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {
387 tests[hits].modulation = DEMOD_ASK;
388 tests[hits].inverted = FALSE;
389 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
390 ++hits;
393 if ( ASKmanDemod("0 1 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {
394 tests[hits].modulation = DEMOD_ASK;
395 tests[hits].inverted = TRUE;
396 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
397 ++hits;
400 if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {
401 tests[hits].modulation = DEMOD_NRZ;
402 tests[hits].inverted = FALSE;
403 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
404 ++hits;
407 if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {
408 tests[hits].modulation = DEMOD_NRZ;
409 tests[hits].inverted = TRUE;
410 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
411 ++hits;
414 if ( PSKDemod("0 0 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {
415 tests[hits].modulation = DEMOD_PSK1;
416 tests[hits].inverted = FALSE;
417 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
418 ++hits;
421 if ( PSKDemod("0 1 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {
422 tests[hits].modulation = DEMOD_PSK1;
423 tests[hits].inverted = TRUE;
424 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
425 ++hits;
428 // PSK2 - needs a call to psk1TOpsk2.
429 if ( PSKDemod("0 0 1", FALSE)) {
430 psk1TOpsk2(DemodBuffer, DemodBufferLen);
431 if (test(DEMOD_PSK2, &tests[hits].offset)){
432 tests[hits].modulation = DEMOD_PSK2;
433 tests[hits].inverted = FALSE;
434 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
435 ++hits;
437 } // inverse waves does not affect this demod
439 // PSK3 - needs a call to psk1TOpsk2.
440 if ( PSKDemod("0 0 1", FALSE)) {
441 psk1TOpsk2(DemodBuffer, DemodBufferLen);
442 if (test(DEMOD_PSK3, &tests[hits].offset)){
443 tests[hits].modulation = DEMOD_PSK3;
444 tests[hits].inverted = FALSE;
445 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
446 ++hits;
448 } // inverse waves does not affect this demod
450 if ( ASKbiphaseDemod("0 0 0 1", FALSE) && test(DEMOD_BI, &tests[hits].offset) ) {
451 tests[hits].modulation = DEMOD_BI;
452 tests[hits].inverted = FALSE;
453 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
454 ++hits;
456 if ( ASKbiphaseDemod("0 0 1 1", FALSE) && test(DEMOD_BIa, &tests[hits].offset) ) {
457 tests[hits].modulation = DEMOD_BIa;
458 tests[hits].inverted = TRUE;
459 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
460 ++hits;
463 if ( hits == 1) {
464 config.modulation = tests[0].modulation;
465 config.inverted = tests[0].inverted;
466 config.offset = tests[0].offset;
467 config.block0 = tests[0].block0;
468 printConfiguration( config );
469 return TRUE;
472 if ( hits > 1) {
473 PrintAndLog("Found [%d] possible matches for modulation.",hits);
474 for(int i=0; i<hits; ++i){
475 PrintAndLog("--[%d]---------------", i+1);
476 printConfiguration( tests[i] );
479 return FALSE;
482 bool testModulation(uint8_t mode, uint8_t modread){
483 switch( mode ){
484 case DEMOD_FSK:
485 if (modread > 3 && modread < 8) return TRUE;
486 break;
487 case DEMOD_ASK:
488 if (modread == DEMOD_ASK) return TRUE;
489 break;
490 case DEMOD_PSK1:
491 if (modread == DEMOD_PSK1) return TRUE;
492 break;
493 case DEMOD_PSK2:
494 if (modread == DEMOD_PSK2) return TRUE;
495 break;
496 case DEMOD_PSK3:
497 if (modread == DEMOD_PSK3) return TRUE;
498 break;
499 case DEMOD_NRZ:
500 if (modread == DEMOD_NRZ) return TRUE;
501 break;
502 case DEMOD_BI:
503 if (modread == DEMOD_BI) return TRUE;
504 break;
505 case DEMOD_BIa:
506 if (modread == DEMOD_BIa) return TRUE;
507 break;
508 default:
509 return FALSE;
511 return FALSE;
514 bool testBitRate(uint8_t readRate, uint8_t mod){
515 uint8_t expected[8] = {8, 16, 32, 40, 50, 64, 100, 128};
516 uint8_t detRate = 0;
517 switch( mod ){
518 case DEMOD_FSK:
519 detRate = GetFskClock("",FALSE, FALSE);
520 if (expected[readRate] == detRate) {
521 config.bitrate = readRate;
522 return TRUE;
524 break;
525 case DEMOD_FSK1:
526 detRate = GetFskClock("",FALSE, FALSE);
527 if (expected[readRate] == detRate) {
528 config.bitrate = readRate;
529 return TRUE;
531 break;
532 case DEMOD_FSK1a:
533 detRate = GetFskClock("",FALSE, FALSE);
534 if (expected[readRate] == detRate) {
535 config.bitrate = readRate;
536 return TRUE;
538 break;
539 case DEMOD_FSK2:
540 detRate = GetFskClock("",FALSE, FALSE);
541 if (expected[readRate] == detRate) {
542 config.bitrate = readRate;
543 return TRUE;
545 break;
546 case DEMOD_FSK2a:
547 detRate = GetFskClock("",FALSE, FALSE);
548 if (expected[readRate] == detRate) {
549 config.bitrate = readRate;
550 return TRUE;
552 break;
553 case DEMOD_ASK:
554 detRate = GetAskClock("",FALSE, FALSE);
555 if (expected[readRate] == detRate) {
556 config.bitrate = readRate;
557 return TRUE;
559 break;
560 case DEMOD_PSK1:
561 detRate = GetPskClock("",FALSE, FALSE);
562 if (expected[readRate] == detRate) {
563 config.bitrate = readRate;
564 return TRUE;
566 break;
567 case DEMOD_PSK2:
568 detRate = GetPskClock("",FALSE, FALSE);
569 if (expected[readRate] == detRate) {
570 config.bitrate = readRate;
571 return TRUE;
573 break;
574 case DEMOD_PSK3:
575 detRate = GetPskClock("",FALSE, FALSE);
576 if (expected[readRate] == detRate) {
577 config.bitrate = readRate;
578 return TRUE;
580 break;
581 case DEMOD_NRZ:
582 detRate = GetNrzClock("",FALSE, FALSE);
583 if (expected[readRate] == detRate) {
584 config.bitrate = readRate;
585 return TRUE;
587 break;
588 case DEMOD_BI:
589 detRate = GetAskClock("",FALSE, FALSE);
590 if (expected[readRate] == detRate) {
591 config.bitrate = readRate;
592 return TRUE;
594 break;
595 default:
596 return FALSE;
598 return FALSE;
601 bool test(uint8_t mode, uint8_t *offset){
603 if ( !DemodBufferLen) return FALSE;
604 uint8_t si = 0;
605 for (uint8_t idx = 0; idx < 64; idx++){
606 si = idx;
607 if ( PackBits(si, 32, DemodBuffer) == 0x00 ) continue;
609 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
610 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode
611 // 2nibble must be zeroed.
612 // moved test to here, since this gets most faults first.
613 if ( resv > 0x00) continue;
615 uint8_t xtRate = PackBits(si, 3, DemodBuffer); si += 3; //new
616 uint8_t bitRate = PackBits(si, 3, DemodBuffer); si += 3; //new could check bit rate
617 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode
618 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1; //new
619 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //new could check psk cr
620 uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24 , 30, 31 could be tested for 0 if not extended mode
621 uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;
623 //if extended mode
624 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? TRUE : FALSE;
626 if (!extMode){
627 if (nml01 || nml02 || xtRate) continue;
629 //test modulation
630 if (!testModulation(mode, modread)) continue;
632 *offset = idx;
633 if (!testBitRate(bitRate, mode)) continue;
634 return TRUE;
636 return FALSE;
639 void printT55xxBlock(const char *demodStr){
641 uint8_t i = config.offset;
642 uint8_t endpos = 32 + i;
643 uint32_t blockData = 0;
644 uint8_t bits[64] = {0x00};
646 if ( !DemodBufferLen) return;
648 if ( endpos > DemodBufferLen){
649 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);
650 return;
653 for (; i < endpos; ++i)
654 bits[i - config.offset]=DemodBuffer[i];
656 blockData = PackBits(0, 32, bits);
657 PrintAndLog("0x%08X %s [%s]", blockData, sprint_bin(bits,32), demodStr);
660 int special(const char *Cmd) {
661 uint32_t blockData = 0;
662 uint8_t bits[32] = {0x00};
664 PrintAndLog("[OFFSET] [DATA] [BINARY]");
665 PrintAndLog("----------------------------------------------------");
666 int i,j = 0;
667 for (; j < 64; ++j){
669 for (i = 0; i < 32; ++i)
670 bits[i]=DemodBuffer[j+i];
672 blockData = PackBits(0, 32, bits);
674 PrintAndLog("[%02d] 0x%08X %s",j , blockData, sprint_bin(bits,32));
676 return 0;
679 void printConfiguration( t55xx_conf_block_t b){
680 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );
681 PrintAndLog("Bit Rate : %s", GetBitRateStr(b.bitrate) );
682 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );
683 PrintAndLog("Offset : %d", b.offset);
684 PrintAndLog("Block0 : 0x%08X", b.block0);
685 PrintAndLog("");
688 int CmdT55xxWriteBlock(const char *Cmd)
690 int block = 8; //default to invalid block
691 int data = 0xFFFFFFFF; //default to blank Block
692 int password = 0xFFFFFFFF; //default to blank Block 7
694 char cmdp = param_getchar(Cmd, 0);
695 if (cmdp == 'h' || cmdp == 'H') {
696 usage_t55xx_write();
697 return 0;
700 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);
702 if ( res < 2 || res > 3) {
703 usage_t55xx_write();
704 return 1;
707 if (block > 7) {
708 PrintAndLog("Block number must be between 0 and 7");
709 return 1;
712 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
713 c.d.asBytes[0] = 0x0;
715 PrintAndLog("Writing to block: %d data : 0x%08X", block, data);
717 //Password mode
718 if (res == 3) {
719 c.arg[2] = password;
720 c.d.asBytes[0] = 0x1;
721 PrintAndLog("pwd : 0x%08X", password);
723 SendCommand(&c);
724 return 0;
727 int CmdT55xxReadTrace(const char *Cmd)
729 char cmdp = param_getchar(Cmd, 0);
731 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
732 return usage_t55xx_trace();
734 if (strlen(Cmd)==0)
735 AquireData( TRACE_BLOCK );
737 if (!DecodeT55xxBlock()) return 1;
739 if ( !DemodBufferLen) return 1;
741 RepaintGraphWindow();
742 uint8_t repeat = 0;
743 if (config.offset > 5)
744 repeat = 32;
745 uint8_t si = config.offset+repeat;
746 uint32_t bl0 = PackBits(si, 32, DemodBuffer);
747 uint32_t bl1 = PackBits(si+32, 32, DemodBuffer);
749 uint32_t acl = PackBits(si, 8, DemodBuffer); si += 8;
750 uint32_t mfc = PackBits(si, 8, DemodBuffer); si += 8;
751 uint32_t cid = PackBits(si, 5, DemodBuffer); si += 5;
752 uint32_t icr = PackBits(si, 3, DemodBuffer); si += 3;
753 uint32_t year = PackBits(si, 4, DemodBuffer); si += 4;
754 uint32_t quarter = PackBits(si, 2, DemodBuffer); si += 2;
755 uint32_t lotid = PackBits(si, 14, DemodBuffer); si += 14;
756 uint32_t wafer = PackBits(si, 5, DemodBuffer); si += 5;
757 uint32_t dw = PackBits(si, 15, DemodBuffer);
759 PrintAndLog("");
760 PrintAndLog("-- T55xx Trace Information ----------------------------------");
761 PrintAndLog("-------------------------------------------------------------");
762 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);
763 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", mfc, mfc, getTagInfo(mfc));
764 PrintAndLog(" CID : 0x%02X (%d) - %s", cid, cid, GetModelStrFromCID(cid));
765 PrintAndLog(" ICR IC Revision : %d",icr );
766 PrintAndLog(" Manufactured");
767 PrintAndLog(" Year/Quarter : 20?%d/%d",year, quarter);
768 PrintAndLog(" Lot ID : %d", lotid );
769 PrintAndLog(" Wafer number : %d", wafer);
770 PrintAndLog(" Die Number : %d", dw);
771 PrintAndLog("-------------------------------------------------------------");
772 PrintAndLog(" Raw Data - Page 1");
773 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset+repeat,32) );
774 PrintAndLog(" Block 1 : 0x%08X %s", bl1, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );
775 PrintAndLog("-------------------------------------------------------------");
777 if ( acl != 0xE0 )
778 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");
780 TRACE - BLOCK O
781 Bits Definition HEX
782 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
783 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
784 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
785 22-24 ICR IC revision
786 25-28 YEAR (BCD encoded) 9 (= 2009)
787 29-30 QUARTER 1,2,3,4
788 31-32 LOT ID
790 TRACE - BLOCK 1
791 1-12 LOT ID
792 13-17 Wafer number
793 18-32 DW, die number sequential
796 return 0;
799 int CmdT55xxInfo(const char *Cmd){
801 Page 0 Block 0 Configuration data.
802 Normal mode
803 Extended mode
805 char cmdp = param_getchar(Cmd, 0);
807 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
808 return usage_t55xx_info();
810 if (strlen(Cmd)==0)
811 AquireData( CONFIGURATION_BLOCK );
813 if (!DecodeT55xxBlock()) return 1;
815 if ( !DemodBufferLen) return 1;
817 uint8_t si = config.offset;
818 uint32_t bl0 = PackBits(si, 32, DemodBuffer);
820 uint32_t safer = PackBits(si, 4, DemodBuffer); si += 4;
821 uint32_t resv = PackBits(si, 7, DemodBuffer); si += 7;
822 uint32_t dbr = PackBits(si, 3, DemodBuffer); si += 3;
823 uint32_t extend = PackBits(si, 1, DemodBuffer); si += 1;
824 uint32_t datamod = PackBits(si, 5, DemodBuffer); si += 5;
825 uint32_t pskcf = PackBits(si, 2, DemodBuffer); si += 2;
826 uint32_t aor = PackBits(si, 1, DemodBuffer); si += 1;
827 uint32_t otp = PackBits(si, 1, DemodBuffer); si += 1;
828 uint32_t maxblk = PackBits(si, 3, DemodBuffer); si += 3;
829 uint32_t pwd = PackBits(si, 1, DemodBuffer); si += 1;
830 uint32_t sst = PackBits(si, 1, DemodBuffer); si += 1;
831 uint32_t fw = PackBits(si, 1, DemodBuffer); si += 1;
832 uint32_t inv = PackBits(si, 1, DemodBuffer); si += 1;
833 uint32_t por = PackBits(si, 1, DemodBuffer); si += 1;
835 PrintAndLog("");
836 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");
837 PrintAndLog("-------------------------------------------------------------");
838 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
839 PrintAndLog(" reserved : %d", resv);
840 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));
841 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
842 PrintAndLog(" Modulation : %s", GetModulationStr(datamod));
843 PrintAndLog(" PSK clock frequency : %d", pskcf);
844 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
845 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
846 PrintAndLog(" Max block : %d", maxblk);
847 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
848 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
849 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
850 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
851 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
852 PrintAndLog("-------------------------------------------------------------");
853 PrintAndLog(" Raw Data - Page 0");
854 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset,32) );
855 PrintAndLog("-------------------------------------------------------------");
857 return 0;
860 int CmdT55xxDump(const char *Cmd){
862 char s[20] = {0x00};
863 uint8_t pwd[4] = {0x00};
865 char cmdp = param_getchar(Cmd, 0);
866 if ( cmdp == 'h' || cmdp == 'H') {
867 usage_t55xx_dump();
868 return 0;
871 bool hasPwd = ( strlen(Cmd) > 0);
872 if ( hasPwd ){
873 if (param_gethex(Cmd, 0, pwd, 8)) {
874 PrintAndLog("password must include 8 HEX symbols");
875 return 1;
879 for ( int i = 0; i <8; ++i){
880 memset(s,0,sizeof(s));
881 if ( hasPwd ) {
882 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);
883 } else {
884 sprintf(s,"%d", i);
886 CmdT55xxReadBlock(s);
888 return 0;
891 int AquireData( uint8_t block ){
893 UsbCommand c;
895 if ( block == CONFIGURATION_BLOCK )
896 c.cmd = CMD_T55XX_READ_BLOCK;
897 else if (block == TRACE_BLOCK )
898 c.cmd = CMD_T55XX_READ_TRACE;
900 c.arg[0] = 0x00;
901 c.arg[1] = 0x00;
902 c.arg[2] = 0x00;
903 c.d.asBytes[0] = 0x0;
905 //Password mode
906 // if ( res == 2 ) {
907 // c.arg[2] = password;
908 // c.d.asBytes[0] = 0x1;
909 // }
911 SendCommand(&c);
912 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
913 PrintAndLog("command execution time out");
914 return 1;
917 uint8_t got[12000];
918 GetFromBigBuf(got,sizeof(got),0);
919 WaitForResponse(CMD_ACK,NULL);
920 setGraphBuf(got, 12000);
921 return 0;
924 char * GetBitRateStr(uint32_t id){
925 static char buf[40];
926 char *retStr = buf;
927 switch (id){
928 case 0:
929 snprintf(retStr,sizeof(buf),"%d - RF/8",id);
930 break;
931 case 1:
932 snprintf(retStr,sizeof(buf),"%d - RF/16",id);
933 break;
934 case 2:
935 snprintf(retStr,sizeof(buf),"%d - RF/32",id);
936 break;
937 case 3:
938 snprintf(retStr,sizeof(buf),"%d - RF/40",id);
939 break;
940 case 4:
941 snprintf(retStr,sizeof(buf),"%d - RF/50",id);
942 break;
943 case 5:
944 snprintf(retStr,sizeof(buf),"%d - RF/64",id);
945 break;
946 case 6:
947 snprintf(retStr,sizeof(buf),"%d - RF/100",id);
948 break;
949 case 7:
950 snprintf(retStr,sizeof(buf),"%d - RF/128",id);
951 break;
952 default:
953 snprintf(retStr,sizeof(buf),"%d - (Unknown)",id);
954 break;
957 return buf;
961 char * GetSaferStr(uint32_t id){
962 static char buf[40];
963 char *retStr = buf;
965 snprintf(retStr,sizeof(buf),"%d",id);
966 if (id == 6) {
967 snprintf(retStr,sizeof(buf),"%d - passwd",id);
969 if (id == 9 ){
970 snprintf(retStr,sizeof(buf),"%d - testmode",id);
973 return buf;
976 char * GetModulationStr( uint32_t id){
977 static char buf[40];
978 char *retStr = buf;
980 switch (id){
981 case 0:
982 snprintf(retStr,sizeof(buf),"%d - DIRECT (ASK/NRZ)",id);
983 break;
984 case 1:
985 snprintf(retStr,sizeof(buf),"%d - PSK 1 phase change when input changes",id);
986 break;
987 case 2:
988 snprintf(retStr,sizeof(buf),"%d - PSK 2 phase change on bitclk if input high",id);
989 break;
990 case 3:
991 snprintf(retStr,sizeof(buf),"%d - PSK 3 phase change on rising edge of input",id);
992 break;
993 case 4:
994 snprintf(retStr,sizeof(buf),"%d - FSK 1 RF/8 RF/5",id);
995 break;
996 case 5:
997 snprintf(retStr,sizeof(buf),"%d - FSK 2 RF/8 RF/10",id);
998 break;
999 case 6:
1000 snprintf(retStr,sizeof(buf),"%d - FSK 1a RF/5 RF/8",id);
1001 break;
1002 case 7:
1003 snprintf(retStr,sizeof(buf),"%d - FSK 2a RF/10 RF/8",id);
1004 break;
1005 case 8:
1006 snprintf(retStr,sizeof(buf),"%d - Manschester",id);
1007 break;
1008 case 16:
1009 snprintf(retStr,sizeof(buf),"%d - Biphase",id);
1010 break;
1011 case 0x18:
1012 snprintf(retStr,sizeof(buf),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id);
1013 break;
1014 case 17:
1015 snprintf(retStr,sizeof(buf),"%d - Reserved",id);
1016 break;
1017 default:
1018 snprintf(retStr,sizeof(buf),"0x%02X (Unknown)",id);
1019 break;
1021 return buf;
1024 char * GetModelStrFromCID(uint32_t cid){
1026 static char buf[10];
1027 char *retStr = buf;
1029 if (cid == 1) sprintf(retStr,"ATA5577M1");
1030 if (cid == 2) sprintf(retStr,"ATA5577M2");
1031 return buf;
1034 char * GetSelectedModulationStr( uint8_t id){
1036 static char buf[20];
1037 char *retStr = buf;
1039 switch (id){
1040 case DEMOD_FSK:
1041 snprintf(retStr,sizeof(buf),"FSK");
1042 break;
1043 case DEMOD_FSK1:
1044 snprintf(retStr,sizeof(buf),"FSK1");
1045 break;
1046 case DEMOD_FSK1a:
1047 snprintf(retStr,sizeof(buf),"FSK1a");
1048 break;
1049 case DEMOD_FSK2:
1050 snprintf(retStr,sizeof(buf),"FSK2");
1051 break;
1052 case DEMOD_FSK2a:
1053 snprintf(retStr,sizeof(buf),"FSK2a");
1054 break;
1055 case DEMOD_ASK:
1056 snprintf(retStr,sizeof(buf),"ASK");
1057 break;
1058 case DEMOD_NRZ:
1059 snprintf(retStr,sizeof(buf),"DIRECT/NRZ");
1060 break;
1061 case DEMOD_PSK1:
1062 snprintf(retStr,sizeof(buf),"PSK1");
1063 break;
1064 case DEMOD_PSK2:
1065 snprintf(retStr,sizeof(buf),"PSK2");
1066 break;
1067 case DEMOD_PSK3:
1068 snprintf(retStr,sizeof(buf),"PSK3");
1069 break;
1070 case DEMOD_BI:
1071 snprintf(retStr,sizeof(buf),"BIPHASE");
1072 break;
1073 case DEMOD_BIa:
1074 snprintf(retStr,sizeof(buf),"BIPHASEa - (CDP)");
1075 break;
1076 default:
1077 snprintf(retStr,sizeof(buf),"(Unknown)");
1078 break;
1080 return buf;
1083 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
1085 int i = start;
1086 int j = len-1;
1088 if (len > 32) return 0;
1090 uint32_t tmp = 0;
1091 for (; j >= 0; --j, ++i)
1092 tmp |= bits[i] << j;
1094 return tmp;
1097 static command_t CommandTable[] =
1099 {"help", CmdHelp, 1, "This help"},
1100 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},
1101 {"detect", CmdT55xxDetect, 0, "[1] Try detecting the tag modulation from reading the configuration block."},
1102 {"read", CmdT55xxReadBlock, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
1103 {"write", CmdT55xxWriteBlock,0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
1104 {"trace", CmdT55xxReadTrace, 0, "[1] Show T55xx traceability data (page 1/ blk 0-1)"},
1105 {"info", CmdT55xxInfo, 0, "[1] Show T55xx configuration data (page 0/ blk 0)"},
1106 {"dump", CmdT55xxDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
1107 {"special", special, 0, "Show block changes with 64 different offsets"},
1108 {NULL, NULL, 0, NULL}
1111 int CmdLFT55XX(const char *Cmd)
1113 CmdsParse(CommandTable, Cmd);
1114 return 0;
1117 int CmdHelp(const char *Cmd)
1119 CmdsHelp(CommandTable);
1120 return 0;