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