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