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