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