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