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