]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
34c8e1efcbe64d41d3e07d320170f16acd389b6f
[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 else if ( strcmp(modulation, "FSK1a" ) == 0)
155 config.modulation = DEMOD_FSK1a;
156 else if ( strcmp(modulation, "FSK2" ) == 0)
157 config.modulation = DEMOD_FSK2;
158 else if ( strcmp(modulation, "FSK2a" ) == 0)
159 config.modulation = DEMOD_FSK2a;
160 else if ( strcmp(modulation, "ASK" ) == 0)
161 config.modulation = DEMOD_ASK;
162 else if ( strcmp(modulation, "NRZ" ) == 0)
163 config.modulation = DEMOD_NRZ;
164 else if ( strcmp(modulation, "PSK1" ) == 0)
165 config.modulation = DEMOD_PSK1;
166 else if ( strcmp(modulation, "PSK2" ) == 0)
167 config.modulation = DEMOD_PSK2;
168 else if ( strcmp(modulation, "PSK3" ) == 0)
169 config.modulation = DEMOD_PSK3;
170 else if ( strcmp(modulation, "BIa" ) == 0)
171 config.modulation = DEMOD_BIa;
172 else if ( strcmp(modulation, "BI" ) == 0)
173 config.modulation = DEMOD_BI;
174 else {
175 PrintAndLog("Unknown modulation '%s'", modulation);
176 errors = TRUE;
177 }
178 break;
179 case 'i':
180 config.inverted = param_getchar(Cmd,cmdp+1) == '1';
181 cmdp+=2;
182 break;
183 case 'o':
184 errors |= param_getdec(Cmd, cmdp+1, &offset);
185 if ( !errors )
186 config.offset = offset;
187 cmdp+=2;
188 break;
189 default:
190 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
191 errors = TRUE;
192 break;
193 }
194 }
195
196 // No args
197 if (cmdp == 0) {
198 printConfiguration( config );
199 return 0;
200 }
201 //Validations
202 if (errors)
203 return usage_t55xx_config();
204
205 config.block0 = 0;
206 printConfiguration ( config );
207 return 0;
208 }
209
210 int CmdT55xxReadBlock(const char *Cmd) {
211 int block = -1;
212 int password = 0xFFFFFFFF; //default to blank Block 7
213
214 char cmdp = param_getchar(Cmd, 0);
215 if (cmdp == 'h' || cmdp == 'H')
216 return usage_t55xx_read();
217
218 int res = sscanf(Cmd, "%d %x", &block, &password);
219
220 if ( res < 1 || res > 2 )
221 return usage_t55xx_read();
222
223
224 if ((block < 0) | (block > 7)) {
225 PrintAndLog("Block must be between 0 and 7");
226 return 1;
227 }
228
229 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};
230 c.d.asBytes[0] = 0x0;
231
232 //Password mode
233 if ( res == 2 ) {
234 c.arg[2] = password;
235 c.d.asBytes[0] = 0x1;
236 }
237
238 SendCommand(&c);
239 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
240 PrintAndLog("command execution time out");
241 return 2;
242 }
243
244 uint8_t got[12000];
245 GetFromBigBuf(got,sizeof(got),0);
246 WaitForResponse(CMD_ACK,NULL);
247 setGraphBuf(got, 12000);
248 DemodBufferLen=0;
249 if (!DecodeT55xxBlock()) return 3;
250 char blk[10]={0};
251 sprintf(blk,"%d", block);
252 printT55xxBlock(blk);
253 return 0;
254 }
255
256 bool DecodeT55xxBlock(){
257
258 char buf[8] = {0x00};
259 char *cmdStr = buf;
260 int ans = 0;
261 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};
262
263 DemodBufferLen = 0x00;
264
265 switch( config.modulation ){
266 case DEMOD_FSK:
267 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
268 CmdLtrim(cmdStr);
269 sprintf(cmdStr,"%d %d", bitRate[config.bitrate], config.inverted );
270 ans = FSKrawDemod(cmdStr, FALSE);
271 break;
272 case DEMOD_FSK1:
273 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
274 CmdLtrim(cmdStr);
275 sprintf(cmdStr,"%d 1 8 5", bitRate[config.bitrate] );
276 ans = FSKrawDemod(cmdStr, FALSE);
277 break;
278 case DEMOD_FSK1a:
279 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
280 CmdLtrim(cmdStr);
281 sprintf(cmdStr,"%d 0 8 5", bitRate[config.bitrate] );
282 ans = FSKrawDemod(cmdStr, FALSE);
283 break;
284 case DEMOD_FSK2:
285 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
286 CmdLtrim(cmdStr);
287 sprintf(cmdStr,"%d 0 10 8", bitRate[config.bitrate] );
288 ans = FSKrawDemod(cmdStr, FALSE);
289 break;
290 case DEMOD_FSK2a:
291 sprintf(cmdStr,"%d", bitRate[config.bitrate]/2 );
292 CmdLtrim(cmdStr);
293 sprintf(cmdStr,"%d 1 10 8", bitRate[config.bitrate] );
294 ans = FSKrawDemod(cmdStr, FALSE);
295 break;
296 case DEMOD_ASK:
297 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
298 ans = ASKmanDemod(cmdStr, FALSE, FALSE);
299 break;
300 case DEMOD_PSK1:
301 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
302 ans = PSKDemod(cmdStr, FALSE);
303 break;
304 case DEMOD_PSK2:
305 sprintf(cmdStr,"%d 1", bitRate[config.bitrate] );
306 ans = PSKDemod(cmdStr, FALSE);
307 psk1TOpsk2(DemodBuffer, DemodBufferLen);
308 break;
309 case DEMOD_PSK3:
310 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
311 ans = PSKDemod(cmdStr, FALSE);
312 psk1TOpsk2(DemodBuffer, DemodBufferLen);
313 break;
314 case DEMOD_NRZ:
315 sprintf(cmdStr,"%d %d 1", bitRate[config.bitrate], config.inverted );
316 ans = NRZrawDemod(cmdStr, FALSE);
317 break;
318 case DEMOD_BI:
319 sprintf(cmdStr,"0 %d 0 1", bitRate[config.bitrate] );
320 ans = ASKbiphaseDemod(cmdStr, FALSE);
321 break;
322 case DEMOD_BIa:
323 sprintf(cmdStr,"0 %d 1 1", bitRate[config.bitrate] );
324 ans = ASKbiphaseDemod(cmdStr, FALSE);
325 break;
326 default:
327 return FALSE;
328 }
329 return (bool) ans;
330 }
331
332 int CmdT55xxDetect(const char *Cmd){
333
334 char cmdp = param_getchar(Cmd, 0);
335 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
336 return usage_t55xx_detect();
337
338 if (strlen(Cmd)==0)
339 AquireData( CONFIGURATION_BLOCK );
340
341 if ( !tryDetectModulation() )
342 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");
343
344 return 0;
345 }
346
347 // detect configuration?
348 bool tryDetectModulation(){
349 char cmdStr[8] = {0};
350 uint8_t hits = 0;
351 t55xx_conf_block_t tests[15];
352
353 if (GetFskClock("", FALSE, FALSE)){
354 uint8_t fc1 = 0, fc2 = 0, clk=0;
355 fskClocks(&fc1, &fc2, &clk, FALSE);
356 sprintf(cmdStr,"%d", clk/2);
357 CmdLtrim(cmdStr);
358 if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset)){
359 tests[hits].modulation = DEMOD_FSK;
360 if (fc1==8 && fc2 == 5)
361 tests[hits].modulation = DEMOD_FSK1a;
362 else if (fc1==10 && fc2 == 8)
363 tests[hits].modulation = DEMOD_FSK2;
364
365 tests[hits].inverted = FALSE;
366 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
367 ++hits;
368 }
369 if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset)) {
370 tests[hits].modulation = DEMOD_FSK;
371 if (fc1==8 && fc2 == 5)
372 tests[hits].modulation = DEMOD_FSK1;
373 else if (fc1==10 && fc2 == 8)
374 tests[hits].modulation = DEMOD_FSK2a;
375
376 tests[hits].inverted = TRUE;
377 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
378 ++hits;
379 }
380 } else {
381 if ( ASKmanDemod("0 0 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {
382 tests[hits].modulation = DEMOD_ASK;
383 tests[hits].inverted = FALSE;
384 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
385 ++hits;
386 }
387
388 if ( ASKmanDemod("0 1 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {
389 tests[hits].modulation = DEMOD_ASK;
390 tests[hits].inverted = TRUE;
391 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
392 ++hits;
393 }
394
395 if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {
396 tests[hits].modulation = DEMOD_NRZ;
397 tests[hits].inverted = FALSE;
398 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
399 ++hits;
400 }
401
402 if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {
403 tests[hits].modulation = DEMOD_NRZ;
404 tests[hits].inverted = TRUE;
405 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
406 ++hits;
407 }
408
409 if ( PSKDemod("0 0 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {
410 tests[hits].modulation = DEMOD_PSK1;
411 tests[hits].inverted = FALSE;
412 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
413 ++hits;
414 }
415
416 if ( PSKDemod("0 1 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {
417 tests[hits].modulation = DEMOD_PSK1;
418 tests[hits].inverted = TRUE;
419 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
420 ++hits;
421 }
422
423 // PSK2 - needs a call to psk1TOpsk2.
424 if ( PSKDemod("0 0 1", FALSE)) {
425 psk1TOpsk2(DemodBuffer, DemodBufferLen);
426 if (test(DEMOD_PSK2, &tests[hits].offset)){
427 tests[hits].modulation = DEMOD_PSK2;
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 // PSK3 - needs a call to psk1TOpsk2.
435 if ( PSKDemod("0 0 1", FALSE)) {
436 psk1TOpsk2(DemodBuffer, DemodBufferLen);
437 if (test(DEMOD_PSK3, &tests[hits].offset)){
438 tests[hits].modulation = DEMOD_PSK3;
439 tests[hits].inverted = FALSE;
440 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
441 ++hits;
442 }
443 } // inverse waves does not affect this demod
444
445 if ( ASKbiphaseDemod("0 0 0 1", FALSE) && test(DEMOD_BI, &tests[hits].offset) ) {
446 tests[hits].modulation = DEMOD_BI;
447 tests[hits].inverted = FALSE;
448 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
449 ++hits;
450 }
451 if ( ASKbiphaseDemod("0 0 1 1", FALSE) && test(DEMOD_BIa, &tests[hits].offset) ) {
452 tests[hits].modulation = DEMOD_BIa;
453 tests[hits].inverted = TRUE;
454 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
455 ++hits;
456 }
457 }
458 if ( hits == 1) {
459 config.modulation = tests[0].modulation;
460 config.inverted = tests[0].inverted;
461 config.offset = tests[0].offset;
462 config.block0 = tests[0].block0;
463 printConfiguration( config );
464 return TRUE;
465 }
466
467 if ( hits > 1) {
468 PrintAndLog("Found [%d] possible matches for modulation.",hits);
469 for(int i=0; i<hits; ++i){
470 PrintAndLog("--[%d]---------------", i+1);
471 printConfiguration( tests[i] );
472 }
473 }
474 return FALSE;
475 }
476
477 bool testModulation(uint8_t mode, uint8_t modread){
478 switch( mode ){
479 case DEMOD_FSK:
480 if (modread > 3 && modread < 8) return TRUE;
481 break;
482 case DEMOD_ASK:
483 if (modread == DEMOD_ASK) return TRUE;
484 break;
485 case DEMOD_PSK1:
486 if (modread == DEMOD_PSK1) return TRUE;
487 break;
488 case DEMOD_PSK2:
489 if (modread == DEMOD_PSK2) return TRUE;
490 break;
491 case DEMOD_PSK3:
492 if (modread == DEMOD_PSK3) return TRUE;
493 break;
494 case DEMOD_NRZ:
495 if (modread == DEMOD_NRZ) return TRUE;
496 break;
497 case DEMOD_BI:
498 if (modread == DEMOD_BI) return TRUE;
499 break;
500 case DEMOD_BIa:
501 if (modread == DEMOD_BIa) return TRUE;
502 break;
503 default:
504 return FALSE;
505 }
506 return FALSE;
507 }
508
509 bool testBitRate(uint8_t readRate, uint8_t mod){
510 uint8_t expected[8] = {8, 16, 32, 40, 50, 64, 100, 128};
511 uint8_t detRate = 0;
512 switch( mod ){
513 case DEMOD_FSK:
514 detRate = GetFskClock("",FALSE, FALSE);
515 if (expected[readRate] == detRate) {
516 config.bitrate = readRate;
517 return TRUE;
518 }
519 break;
520 case DEMOD_FSK1:
521 detRate = GetFskClock("",FALSE, FALSE);
522 if (expected[readRate] == detRate) {
523 config.bitrate = readRate;
524 return TRUE;
525 }
526 break;
527 case DEMOD_FSK1a:
528 detRate = GetFskClock("",FALSE, FALSE);
529 if (expected[readRate] == detRate) {
530 config.bitrate = readRate;
531 return TRUE;
532 }
533 break;
534 case DEMOD_FSK2:
535 detRate = GetFskClock("",FALSE, FALSE);
536 if (expected[readRate] == detRate) {
537 config.bitrate = readRate;
538 return TRUE;
539 }
540 break;
541 case DEMOD_FSK2a:
542 detRate = GetFskClock("",FALSE, FALSE);
543 if (expected[readRate] == detRate) {
544 config.bitrate = readRate;
545 return TRUE;
546 }
547 break;
548 case DEMOD_ASK:
549 detRate = GetAskClock("",FALSE, FALSE);
550 if (expected[readRate] == detRate) {
551 config.bitrate = readRate;
552 return TRUE;
553 }
554 break;
555 case DEMOD_PSK1:
556 detRate = GetPskClock("",FALSE, FALSE);
557 if (expected[readRate] == detRate) {
558 config.bitrate = readRate;
559 return TRUE;
560 }
561 break;
562 case DEMOD_PSK2:
563 detRate = GetPskClock("",FALSE, FALSE);
564 if (expected[readRate] == detRate) {
565 config.bitrate = readRate;
566 return TRUE;
567 }
568 break;
569 case DEMOD_PSK3:
570 detRate = GetPskClock("",FALSE, FALSE);
571 if (expected[readRate] == detRate) {
572 config.bitrate = readRate;
573 return TRUE;
574 }
575 break;
576 case DEMOD_NRZ:
577 detRate = GetNrzClock("",FALSE, FALSE);
578 if (expected[readRate] == detRate) {
579 config.bitrate = readRate;
580 return TRUE;
581 }
582 break;
583 case DEMOD_BI:
584 detRate = GetAskClock("",FALSE, FALSE);
585 if (expected[readRate] == detRate) {
586 config.bitrate = readRate;
587 return TRUE;
588 }
589 break;
590 default:
591 return FALSE;
592 }
593 return FALSE;
594 }
595
596 bool test(uint8_t mode, uint8_t *offset){
597
598 if ( !DemodBufferLen) return FALSE;
599 uint8_t si = 0;
600 for (uint8_t idx = 0; idx < 64; idx++){
601 si = idx;
602 if ( PackBits(si, 32, DemodBuffer) == 0x00 ) continue;
603
604 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
605 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode
606 // 2nibble must be zeroed.
607 // moved test to here, since this gets most faults first.
608 if ( resv > 0x00) continue;
609
610 uint8_t xtRate = PackBits(si, 3, DemodBuffer); si += 3; //new
611 uint8_t bitRate = PackBits(si, 3, DemodBuffer); si += 3; //new could check bit rate
612 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode
613 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1; //new
614 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //new could check psk cr
615 uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24 , 30, 31 could be tested for 0 if not extended mode
616 uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;
617
618 //if extended mode
619 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? TRUE : FALSE;
620
621 if (!extMode){
622 if (nml01 || nml02 || xtRate) continue;
623 }
624 //test modulation
625 if (!testModulation(mode, modread)) continue;
626
627 *offset = idx;
628 if (!testBitRate(bitRate, mode)) continue;
629 return TRUE;
630 }
631 return FALSE;
632 }
633
634 void printT55xxBlock(const char *demodStr){
635
636 uint8_t i = config.offset;
637 uint8_t endpos = 32 + i;
638 uint32_t blockData = 0;
639 uint8_t bits[64] = {0x00};
640
641 if ( !DemodBufferLen) return;
642
643 if ( endpos > DemodBufferLen){
644 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);
645 return;
646 }
647
648 for (; i < endpos; ++i)
649 bits[i - config.offset]=DemodBuffer[i];
650
651 blockData = PackBits(0, 32, bits);
652 PrintAndLog("0x%08X %s [%s]", blockData, sprint_bin(bits,32), demodStr);
653 }
654
655 int special(const char *Cmd) {
656 uint32_t blockData = 0;
657 uint8_t bits[32] = {0x00};
658
659 PrintAndLog("[OFFSET] [DATA] [BINARY]");
660 PrintAndLog("----------------------------------------------------");
661 int i,j = 0;
662 for (; j < 64; ++j){
663
664 for (i = 0; i < 32; ++i)
665 bits[i]=DemodBuffer[j+i];
666
667 blockData = PackBits(0, 32, bits);
668
669 PrintAndLog("[%02d] 0x%08X %s",j , blockData, sprint_bin(bits,32));
670 }
671 return 0;
672 }
673
674 void printConfiguration( t55xx_conf_block_t b){
675 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );
676 PrintAndLog("Bit Rate : %s", GetBitRateStr(b.bitrate) );
677 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );
678 PrintAndLog("Offset : %d", b.offset);
679 PrintAndLog("Block0 : 0x%08X", b.block0);
680 PrintAndLog("");
681 }
682
683 int CmdT55xxWriteBlock(const char *Cmd)
684 {
685 int block = 8; //default to invalid block
686 int data = 0xFFFFFFFF; //default to blank Block
687 int password = 0xFFFFFFFF; //default to blank Block 7
688
689 char cmdp = param_getchar(Cmd, 0);
690 if (cmdp == 'h' || cmdp == 'H') {
691 usage_t55xx_write();
692 return 0;
693 }
694
695 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);
696
697 if ( res < 2 || res > 3) {
698 usage_t55xx_write();
699 return 1;
700 }
701
702 if (block > 7) {
703 PrintAndLog("Block number must be between 0 and 7");
704 return 1;
705 }
706
707 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
708 c.d.asBytes[0] = 0x0;
709
710 PrintAndLog("Writing to block: %d data : 0x%08X", block, data);
711
712 //Password mode
713 if (res == 3) {
714 c.arg[2] = password;
715 c.d.asBytes[0] = 0x1;
716 PrintAndLog("pwd : 0x%08X", password);
717 }
718 SendCommand(&c);
719 return 0;
720 }
721
722 int CmdT55xxReadTrace(const char *Cmd)
723 {
724 char cmdp = param_getchar(Cmd, 0);
725
726 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
727 return usage_t55xx_trace();
728
729 if (strlen(Cmd)==0)
730 AquireData( TRACE_BLOCK );
731
732 if (!DecodeT55xxBlock()) return 1;
733
734 if ( !DemodBufferLen) return 1;
735
736 RepaintGraphWindow();
737 uint8_t repeat = 0;
738 if (config.offset > 5)
739 repeat = 32;
740 uint8_t si = config.offset+repeat;
741 uint32_t bl0 = PackBits(si, 32, DemodBuffer);
742 uint32_t bl1 = PackBits(si+32, 32, DemodBuffer);
743
744 uint32_t acl = PackBits(si, 8, DemodBuffer); si += 8;
745 uint32_t mfc = PackBits(si, 8, DemodBuffer); si += 8;
746 uint32_t cid = PackBits(si, 5, DemodBuffer); si += 5;
747 uint32_t icr = PackBits(si, 3, DemodBuffer); si += 3;
748 uint32_t year = PackBits(si, 4, DemodBuffer); si += 4;
749 uint32_t quarter = PackBits(si, 2, DemodBuffer); si += 2;
750 uint32_t lotid = PackBits(si, 12, DemodBuffer); si += 12;
751 uint32_t wafer = PackBits(si, 5, DemodBuffer); si += 5;
752 uint32_t dw = PackBits(si, 15, DemodBuffer);
753
754 year += 2000;
755
756 PrintAndLog("");
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 : %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("-------------------------------------------------------------");
773
774 if ( acl != 0xE0 )
775 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");
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[40];
923 char *retStr = buf;
924 switch (id){
925 case 0:
926 sprintf(retStr,"%d - RF/8",id);
927 break;
928 case 1:
929 sprintf(retStr,"%d - RF/16",id);
930 break;
931 case 2:
932 sprintf(retStr,"%d - RF/32",id);
933 break;
934 case 3:
935 sprintf(retStr,"%d - RF/40",id);
936 break;
937 case 4:
938 sprintf(retStr,"%d - RF/50",id);
939 break;
940 case 5:
941 sprintf(retStr,"%d - RF/64",id);
942 break;
943 case 6:
944 sprintf(retStr,"%d - RF/100",id);
945 break;
946 case 7:
947 sprintf(retStr,"%d - RF/128",id);
948 break;
949 default:
950 sprintf(retStr,"%d - (Unknown)",id);
951 break;
952 }
953
954 return buf;
955 }
956
957 char * GetSaferStr(uint32_t id){
958 static char buf[40];
959 char *retStr = buf;
960
961 sprintf(retStr,"%d",id);
962 if (id == 6) {
963 sprintf(retStr,"%d - passwd",id);
964 }
965 if (id == 9 ){
966 sprintf(retStr,"%d - testmode",id);
967 }
968
969 return buf;
970 }
971 char * GetModulationStr( uint32_t id){
972 static char buf[40];
973 char *retStr = buf;
974
975 switch (id){
976 case 0:
977 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);
978 break;
979 case 1:
980 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);
981 break;
982 case 2:
983 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);
984 break;
985 case 3:
986 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);
987 break;
988 case 4:
989 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);
990 break;
991 case 5:
992 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);
993 break;
994 case 6:
995 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);
996 break;
997 case 7:
998 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);
999 break;
1000 case 8:
1001 sprintf(retStr,"%d - Manschester",id);
1002 break;
1003 case 16:
1004 sprintf(retStr,"%d - Biphase",id);
1005 break;
1006 case 0x18:
1007 sprintf(retStr,"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id);
1008 break;
1009 case 17:
1010 sprintf(retStr,"%d - Reserved",id);
1011 break;
1012 default:
1013 sprintf(retStr,"0x%02X (Unknown)",id);
1014 break;
1015 }
1016 return buf;
1017 }
1018
1019 char * GetModelStrFromCID(uint32_t cid){
1020
1021 static char buf[10];
1022 char *retStr = buf;
1023
1024 if (cid == 1) sprintf(retStr,"ATA5577M1");
1025 if (cid == 2) sprintf(retStr,"ATA5577M2");
1026 return buf;
1027 }
1028
1029 char * GetSelectedModulationStr( uint8_t id){
1030
1031 static char buf[16];
1032 char *retStr = buf;
1033
1034 switch (id){
1035 case DEMOD_FSK:
1036 sprintf(retStr,"FSK");
1037 break;
1038 case DEMOD_FSK1:
1039 sprintf(retStr,"FSK1");
1040 break;
1041 case DEMOD_FSK1a:
1042 sprintf(retStr,"FSK1a");
1043 break;
1044 case DEMOD_FSK2:
1045 sprintf(retStr,"FSK2");
1046 break;
1047 case DEMOD_FSK2a:
1048 sprintf(retStr,"FSK2a");
1049 break;
1050 case DEMOD_ASK:
1051 sprintf(retStr,"ASK");
1052 break;
1053 case DEMOD_NRZ:
1054 sprintf(retStr,"DIRECT/NRZ");
1055 break;
1056 case DEMOD_PSK1:
1057 sprintf(retStr,"PSK1");
1058 break;
1059 case DEMOD_PSK2:
1060 sprintf(retStr,"PSK2");
1061 break;
1062 case DEMOD_PSK3:
1063 sprintf(retStr,"PSK3");
1064 break;
1065 case DEMOD_BI:
1066 sprintf(retStr,"BIPHASE");
1067 break;
1068 case DEMOD_BIa:
1069 sprintf(retStr,"BIPHASEa - (CDP)");
1070 break;
1071 default:
1072 sprintf(retStr,"(Unknown)");
1073 break;
1074 }
1075 return buf;
1076 }
1077
1078 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
1079
1080 int i = start;
1081 int j = len-1;
1082
1083 if (len > 32) return 0;
1084
1085 uint32_t tmp = 0;
1086 for (; j >= 0; --j, ++i)
1087 tmp |= bits[i] << j;
1088
1089 return tmp;
1090 }
1091
1092 static command_t CommandTable[] =
1093 {
1094 {"help", CmdHelp, 1, "This help"},
1095 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},
1096 {"detect", CmdT55xxDetect, 0, "[1] Try detecting the tag modulation from reading the configuration block."},
1097 {"read", CmdT55xxReadBlock, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
1098 {"write", CmdT55xxWriteBlock,0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
1099 {"trace", CmdT55xxReadTrace, 0, "[1] Show T55xx traceability data (page 1/ blk 0-1)"},
1100 {"info", CmdT55xxInfo, 0, "[1] Show T55xx configuration data (page 0/ blk 0)"},
1101 {"dump", CmdT55xxDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
1102 {"special", special, 0, "Show block changes with 64 different offsets"},
1103 {NULL, NULL, 0, NULL}
1104 };
1105
1106 int CmdLFT55XX(const char *Cmd)
1107 {
1108 CmdsParse(CommandTable, Cmd);
1109 return 0;
1110 }
1111
1112 int CmdHelp(const char *Cmd)
1113 {
1114 CmdsHelp(CommandTable);
1115 return 0;
1116 }
Impressum, Datenschutz