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