]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
PCF7931: Print found single/consecutive block(s), fixes to block 1 check
[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 "cmdlft55xx.h"
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include "comms.h"
18 #include "ui.h"
19 #include "graph.h"
20 #include "cmdmain.h"
21 #include "cmdparser.h"
22 #include "cmddata.h"
23 #include "cmdlf.h"
24 #include "util.h"
25 #include "lfdemod.h"
26 #include "protocols.h"
27 #include "taginfo.h"
28
29 #define T55x7_CONFIGURATION_BLOCK 0x00
30 #define T55x7_PAGE0 0x00
31 #define T55x7_PAGE1 0x01
32 //#define T55x7_PWD 0x00000010
33 #define REGULAR_READ_MODE_BLOCK 0xFF
34
35 // Default configuration
36 t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = false, .offset = 0x00, .block0 = 0x00, .Q5 = false };
37
38 t55xx_conf_block_t Get_t55xx_Config(){
39 return config;
40 }
41 void Set_t55xx_Config(t55xx_conf_block_t conf){
42 config = conf;
43 }
44
45 int usage_t55xx_config(){
46 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>] [Q5]");
47 PrintAndLog("Options:");
48 PrintAndLog(" h This help");
49 PrintAndLog(" b <8|16|32|40|50|64|100|128> Set bitrate");
50 PrintAndLog(" d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa> Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A");
51 PrintAndLog(" i [1] Invert data signal, defaults to normal");
52 PrintAndLog(" o [offset] Set offset, where data should start decode in bitstream");
53 PrintAndLog(" Q5 Set as Q5(T5555) chip instead of T55x7");
54 PrintAndLog(" ST Set Sequence Terminator on");
55 PrintAndLog("");
56 PrintAndLog("Examples:");
57 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");
58 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");
59 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");
60 PrintAndLog("");
61 return 0;
62 }
63 int usage_t55xx_read(){
64 PrintAndLog("Usage: lf t55xx read [b <block>] [p <password>] <override_safety> <page1>");
65 PrintAndLog("Options:");
66 PrintAndLog(" b <block> - block number to read. Between 0-7");
67 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
68 PrintAndLog(" o - OPTIONAL override safety check");
69 PrintAndLog(" 1 - OPTIONAL read Page 1 instead of Page 0");
70 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");
71 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference");
72 PrintAndLog(" ****WARNING****");
73 PrintAndLog(" Use of read with password on a tag not configured for a pwd");
74 PrintAndLog(" can damage the tag");
75 PrintAndLog("");
76 PrintAndLog("Examples:");
77 PrintAndLog(" lf t55xx read b 0 - read data from block 0");
78 PrintAndLog(" lf t55xx read b 0 p feedbeef - read data from block 0 password feedbeef");
79 PrintAndLog(" lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check");
80 PrintAndLog("");
81 return 0;
82 }
83 int usage_t55xx_write(){
84 PrintAndLog("Usage: lf t55xx write [b <block>] [d <data>] [p <password>] [1] [t]");
85 PrintAndLog("Options:");
86 PrintAndLog(" b <block> - block number to write. Between 0-7");
87 PrintAndLog(" d <data> - 4 bytes of data to write (8 hex characters)");
88 PrintAndLog(" p <password> - OPTIONAL password 4bytes (8 hex characters)");
89 PrintAndLog(" 1 - OPTIONAL write Page 1 instead of Page 0");
90 PrintAndLog(" t - OPTIONAL test mode write - ****DANGER****");
91 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");
92 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference");
93 PrintAndLog("");
94 PrintAndLog("Examples:");
95 PrintAndLog(" lf t55xx write b 3 d 11223344 - write 11223344 to block 3");
96 PrintAndLog(" lf t55xx write b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef");
97 PrintAndLog("");
98 return 0;
99 }
100 int usage_t55xx_trace() {
101 PrintAndLog("Usage: lf t55xx trace [1]");
102 PrintAndLog("Options:");
103 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");
104 PrintAndLog("");
105 PrintAndLog("Examples:");
106 PrintAndLog(" lf t55xx trace");
107 PrintAndLog(" lf t55xx trace 1");
108 PrintAndLog("");
109 return 0;
110 }
111 int usage_t55xx_info() {
112 PrintAndLog("Usage: lf t55xx info [1]");
113 PrintAndLog("Options:");
114 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");
115 PrintAndLog("");
116 PrintAndLog("Examples:");
117 PrintAndLog(" lf t55xx info");
118 PrintAndLog(" lf t55xx info 1");
119 PrintAndLog("");
120 return 0;
121 }
122 int usage_t55xx_dump(){
123 PrintAndLog("Usage: lf t55xx dump <password> [o]");
124 PrintAndLog("Options:");
125 PrintAndLog(" <password> - OPTIONAL password 4bytes (8 hex symbols)");
126 PrintAndLog(" o - OPTIONAL override, force pwd read despite danger to card");
127 PrintAndLog("");
128 PrintAndLog("Examples:");
129 PrintAndLog(" lf t55xx dump");
130 PrintAndLog(" lf t55xx dump feedbeef o");
131 PrintAndLog("");
132 return 0;
133 }
134 int usage_t55xx_detect(){
135 PrintAndLog("Usage: lf t55xx detect [1] [p <password>]");
136 PrintAndLog("Options:");
137 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");
138 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
139 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");
140 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference");
141 PrintAndLog("");
142 PrintAndLog("Examples:");
143 PrintAndLog(" lf t55xx detect");
144 PrintAndLog(" lf t55xx detect 1");
145 PrintAndLog(" lf t55xx detect p 11223344");
146 PrintAndLog("");
147 return 0;
148 }
149 int usage_t55xx_detectP1(){
150 PrintAndLog("Command: Detect Page 1 of a t55xx chip");
151 PrintAndLog("Usage: lf t55xx p1detect [1] [p <password>]");
152 PrintAndLog("Options:");
153 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");
154 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
155 PrintAndLog("");
156 PrintAndLog("Examples:");
157 PrintAndLog(" lf t55xx p1detect");
158 PrintAndLog(" lf t55xx p1detect 1");
159 PrintAndLog(" lf t55xx p1detect p 11223344");
160 PrintAndLog("");
161 return 0;
162 }
163 int usage_t55xx_wakup(){
164 PrintAndLog("Usage: lf t55xx wakeup [h] <password>");
165 PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards.");
166 PrintAndLog("Options:");
167 PrintAndLog(" h - this help");
168 PrintAndLog(" <password> - [required] password 4bytes (8 hex symbols)");
169 PrintAndLog("");
170 PrintAndLog("Examples:");
171 PrintAndLog(" lf t55xx wakeup 11223344 - send wakeup password");
172 return 0;
173 }
174 int usage_t55xx_bruteforce(){
175 PrintAndLog("This command uses A) bruteforce to scan a number range");
176 PrintAndLog(" B) a dictionary attack");
177 PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");
178 PrintAndLog(" password must be 4 bytes (8 hex symbols)");
179 PrintAndLog("Options:");
180 PrintAndLog(" h - this help");
181 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default)");
182 PrintAndLog(" '1' long leading reference, '2' leading zero ");
183 PrintAndLog(" '3' 1 of 4 coding reference, '4' special - try all downlink modes");
184 PrintAndLog(" <start_pwd> - 4 byte hex value to start pwd search at");
185 PrintAndLog(" <end_pwd> - 4 byte hex value to end pwd search at");
186 PrintAndLog(" i <*.dic> - loads a default keys dictionary file <*.dic>");
187 PrintAndLog("");
188 PrintAndLog("Examples:");
189 PrintAndLog(" lf t55xx bruteforce [r 2] aaaaaaaa bbbbbbbb");
190 PrintAndLog(" lf t55xx bruteforce [r 2] i default_pwd.dic");
191 PrintAndLog("");
192 return 0;
193 }
194 int usage_t55xx_wipe(){
195 PrintAndLog("Usage: lf t55xx wipe [h] [Q5]");
196 PrintAndLog("This commands wipes a tag, fills blocks 1-7 with zeros and a default configuration block");
197 PrintAndLog("Options:");
198 PrintAndLog(" h - this help");
199 PrintAndLog(" Q5 - indicates to use the T5555 (Q5) default configuration block");
200 PrintAndLog("");
201 PrintAndLog("Examples:");
202 PrintAndLog(" lf t55xx wipe - wipes a t55x7 tag, config block 0x000880E0");
203 PrintAndLog(" lf t55xx wipe Q5 - wipes a t5555 Q5 tag, config block 0x6001F004");
204 return 0;
205 }
206
207
208 static int CmdHelp(const char *Cmd);
209
210 void printT5xxHeader(uint8_t page){
211 PrintAndLog("Reading Page %d:", page);
212 PrintAndLog("blk | hex data | binary");
213 PrintAndLog("----+----------+---------------------------------");
214 }
215
216 int CmdT55xxSetConfig(const char *Cmd) {
217
218 uint8_t offset = 0;
219 char modulation[6] = {0x00};
220 char tmp = 0x00;
221 uint8_t bitRate = 0;
222 uint8_t rates[9] = {8,16,32,40,50,64,100,128,0};
223 uint8_t cmdp = 0;
224 bool errors = false;
225 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)
226 {
227 tmp = param_getchar(Cmd, cmdp);
228 switch(tmp)
229 {
230 case 'h':
231 case 'H':
232 return usage_t55xx_config();
233 case 'b':
234 errors |= param_getdec(Cmd, cmdp+1, &bitRate);
235 if ( !errors){
236 uint8_t i = 0;
237 for (; i < 9; i++){
238 if (rates[i]==bitRate) {
239 config.bitrate = i;
240 break;
241 }
242 }
243 if (i==9) errors = true;
244 }
245 cmdp+=2;
246 break;
247 case 'd':
248 param_getstr(Cmd, cmdp+1, modulation, sizeof(modulation));
249 cmdp += 2;
250
251 if ( strcmp(modulation, "FSK" ) == 0) {
252 config.modulation = DEMOD_FSK;
253 } else if ( strcmp(modulation, "FSK1" ) == 0) {
254 config.modulation = DEMOD_FSK1;
255 config.inverted=1;
256 } else if ( strcmp(modulation, "FSK1a" ) == 0) {
257 config.modulation = DEMOD_FSK1a;
258 config.inverted=0;
259 } else if ( strcmp(modulation, "FSK2" ) == 0) {
260 config.modulation = DEMOD_FSK2;
261 config.inverted=0;
262 } else if ( strcmp(modulation, "FSK2a" ) == 0) {
263 config.modulation = DEMOD_FSK2a;
264 config.inverted=1;
265 } else if ( strcmp(modulation, "ASK" ) == 0) {
266 config.modulation = DEMOD_ASK;
267 } else if ( strcmp(modulation, "NRZ" ) == 0) {
268 config.modulation = DEMOD_NRZ;
269 } else if ( strcmp(modulation, "PSK1" ) == 0) {
270 config.modulation = DEMOD_PSK1;
271 } else if ( strcmp(modulation, "PSK2" ) == 0) {
272 config.modulation = DEMOD_PSK2;
273 } else if ( strcmp(modulation, "PSK3" ) == 0) {
274 config.modulation = DEMOD_PSK3;
275 } else if ( strcmp(modulation, "BIa" ) == 0) {
276 config.modulation = DEMOD_BIa;
277 config.inverted=1;
278 } else if ( strcmp(modulation, "BI" ) == 0) {
279 config.modulation = DEMOD_BI;
280 config.inverted=0;
281 } else {
282 PrintAndLog("Unknown modulation '%s'", modulation);
283 errors = true;
284 }
285 break;
286 case 'i':
287 config.inverted = param_getchar(Cmd,cmdp+1) == '1';
288 cmdp+=2;
289 break;
290 case 'o':
291 errors |= param_getdec(Cmd, cmdp+1, &offset);
292 if ( !errors )
293 config.offset = offset;
294 cmdp+=2;
295 break;
296 case 'Q':
297 case 'q':
298 config.Q5 = true;
299 cmdp++;
300 break;
301 case 'S':
302 case 's':
303 config.ST = true;
304 cmdp++;
305 break;
306 default:
307 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
308 errors = true;
309 break;
310 }
311 }
312
313 // No args
314 if (cmdp == 0) return printConfiguration( config);
315
316 //Validations
317 if (errors) return usage_t55xx_config();
318
319 config.block0 = 0;
320 return printConfiguration ( config);
321 }
322
323 int T55xxReadBlock(uint8_t block, bool page1, bool usepwd, bool override, uint32_t password, uint8_t downlink_mode){
324 //Password mode
325 if ( usepwd ) {
326 // try reading the config block and verify that PWD bit is set before doing this!
327 if ( !override ) {
328 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0,downlink_mode ) ) return 0;
329 if ( !tryDetectModulation() ) {
330 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits.");
331 return 0;
332 } else {
333 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password...");
334 usepwd = false;
335 page1 = false;
336 }
337 } else {
338 PrintAndLog("Safety Check Overriden - proceeding despite risk");
339 }
340 }
341
342 if (!AquireData(page1, block, usepwd, password,downlink_mode) ) return 0;
343 if (!DecodeT55xxBlock()) return 0;
344
345 char blk[10]={0};
346 sprintf(blk,"%d", block);
347 printT55xxBlock(blk);
348 return 1;
349 }
350
351 int CmdT55xxReadBlock(const char *Cmd) {
352 uint8_t block = REGULAR_READ_MODE_BLOCK;
353 uint32_t password = 0; //default to blank Block 7
354 uint8_t downlink_mode = 0;
355
356 bool usepwd = false;
357 bool override = false;
358 bool page1 = false;
359 bool errors = false;
360 uint8_t cmdp = 0;
361 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
362 switch(param_getchar(Cmd, cmdp)) {
363 case 'h':
364 case 'H':
365 return usage_t55xx_read();
366 case 'b':
367 case 'B':
368 errors |= param_getdec(Cmd, cmdp+1, &block);
369 cmdp += 2;
370 break;
371 case 'o':
372 case 'O':
373 override = true;
374 cmdp++;
375 break;
376 case 'p':
377 case 'P':
378 password = param_get32ex(Cmd, cmdp+1, 0, 16);
379 usepwd = true;
380 cmdp += 2;
381 break;
382 case '1':
383 page1 = true;
384 cmdp++;
385 break;
386 case 'r':
387 case 'R':
388 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
389 if (downlink_mode > 3) downlink_mode = 0;
390 cmdp +=2;
391 break;
392 default:
393 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
394 errors = true;
395 break;
396 }
397 }
398 if (errors) return usage_t55xx_read();
399
400 if (block > 7 && block != REGULAR_READ_MODE_BLOCK ) {
401 PrintAndLog("Block must be between 0 and 7");
402 return 0;
403 }
404
405 printT5xxHeader(page1);
406 return T55xxReadBlock(block, page1, usepwd, override, password, downlink_mode);
407 }
408
409 bool DecodeT55xxBlock(){
410
411 char buf[30] = {0x00};
412 char *cmdStr = buf;
413 int ans = 0;
414 bool ST = config.ST;
415 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};
416 DemodBufferLen = 0x00;
417
418 switch( config.modulation ){
419 case DEMOD_FSK:
420 snprintf(cmdStr, sizeof(buf),"%d %d", bitRate[config.bitrate], config.inverted );
421 ans = FSKrawDemod(cmdStr, false);
422 break;
423 case DEMOD_FSK1:
424 case DEMOD_FSK1a:
425 snprintf(cmdStr, sizeof(buf),"%d %d 8 5", bitRate[config.bitrate], config.inverted );
426 ans = FSKrawDemod(cmdStr, false);
427 break;
428 case DEMOD_FSK2:
429 case DEMOD_FSK2a:
430 snprintf(cmdStr, sizeof(buf),"%d %d 10 8", bitRate[config.bitrate], config.inverted );
431 ans = FSKrawDemod(cmdStr, false);
432 break;
433 case DEMOD_ASK:
434 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );
435 ans = ASKDemod_ext(cmdStr, false, false, 1, &ST);
436 break;
437 case DEMOD_PSK1:
438 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
439 save_restoreGB(GRAPH_SAVE);
440 CmdLtrim("160");
441 snprintf(cmdStr, sizeof(buf),"%d %d 6", bitRate[config.bitrate], config.inverted );
442 ans = PSKDemod(cmdStr, false);
443 //undo trim samples
444 save_restoreGB(GRAPH_RESTORE);
445 break;
446 case DEMOD_PSK2: //inverted won't affect this
447 case DEMOD_PSK3: //not fully implemented
448 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
449 save_restoreGB(GRAPH_SAVE);
450 CmdLtrim("160");
451 snprintf(cmdStr, sizeof(buf),"%d 0 6", bitRate[config.bitrate] );
452 ans = PSKDemod(cmdStr, false);
453 psk1TOpsk2(DemodBuffer, DemodBufferLen);
454 //undo trim samples
455 save_restoreGB(GRAPH_RESTORE);
456 break;
457 case DEMOD_NRZ:
458 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );
459 ans = NRZrawDemod(cmdStr, false);
460 break;
461 case DEMOD_BI:
462 case DEMOD_BIa:
463 snprintf(cmdStr, sizeof(buf),"0 %d %d 1", bitRate[config.bitrate], config.inverted );
464 ans = ASKbiphaseDemod(cmdStr, false);
465 break;
466 default:
467 return false;
468 }
469 return (bool) ans;
470 }
471
472 bool DecodeT5555TraceBlock() {
473 DemodBufferLen = 0x00;
474
475 // According to datasheet. Always: RF/64, not inverted, Manchester
476 return (bool) ASKDemod("64 0 1", false, false, 1);
477 }
478
479 void T55xx_Print_DownlinkMode (uint8_t downlink_mode)
480 {
481 char Msg[80];
482 sprintf (Msg,"Downlink Mode used : ");
483
484 switch (downlink_mode) {
485 case 0 : strcat (Msg,"default/fixed bit length"); break;
486 case 1 : strcat (Msg,"long leading reference (r 1)"); break;
487 case 2 : strcat (Msg,"leading zero reference (r 2)"); break;
488 case 3 : strcat (Msg,"1 of 4 coding reference (r 3)"); break;
489 default :
490 strcat (Msg,"default/fixed bit length"); break;
491
492 }
493
494 PrintAndLog (Msg);
495
496 }
497
498 int CmdT55xxDetect(const char *Cmd){
499 bool errors = false;
500 bool useGB = false;
501 bool usepwd = false;
502 uint32_t password = 0;
503 uint8_t cmdp = 0;
504 uint8_t downlink_mode = 0;
505
506 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
507 switch(param_getchar(Cmd, cmdp)) {
508 case 'h':
509 case 'H':
510 return usage_t55xx_detect();
511 case 'p':
512 case 'P':
513 password = param_get32ex(Cmd, cmdp+1, 0, 16);
514 usepwd = true;
515 cmdp += 2;
516 break;
517 case '1':
518 // use Graphbuffer data
519 useGB = true;
520 cmdp++;
521 break;
522 case 'r':
523 case 'R':
524 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
525 if (downlink_mode > 3) downlink_mode = 0;
526 cmdp +=2;
527 break;
528 default:
529 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
530 errors = true;
531 break;
532 }
533 }
534 if (errors) return usage_t55xx_detect();
535
536 if ( !useGB) {
537 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password,downlink_mode) )
538 return 0;
539 }
540
541 if ( !tryDetectModulation() )
542 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");
543 else {
544 // Add downlink mode for reference.
545 T55xx_Print_DownlinkMode (downlink_mode);
546 }
547 return 1;
548 }
549
550 // detect configuration?
551 bool tryDetectModulation(){
552 uint8_t hits = 0;
553 t55xx_conf_block_t tests[15];
554 int bitRate=0;
555 uint8_t fc1 = 0, fc2 = 0, ans = 0;
556 int clk = 0, firstClockEdge = 0;
557 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);
558 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
559 if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
560 tests[hits].modulation = DEMOD_FSK;
561 if (fc1==8 && fc2 == 5)
562 tests[hits].modulation = DEMOD_FSK1a;
563 else if (fc1==10 && fc2 == 8)
564 tests[hits].modulation = DEMOD_FSK2;
565 tests[hits].bitrate = bitRate;
566 tests[hits].inverted = false;
567 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
568 tests[hits].ST = false;
569 ++hits;
570 }
571 if ( FSKrawDemod("0 1", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
572 tests[hits].modulation = DEMOD_FSK;
573 if (fc1 == 8 && fc2 == 5)
574 tests[hits].modulation = DEMOD_FSK1;
575 else if (fc1 == 10 && fc2 == 8)
576 tests[hits].modulation = DEMOD_FSK2a;
577 tests[hits].bitrate = bitRate;
578 tests[hits].inverted = true;
579 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
580 tests[hits].ST = false;
581 ++hits;
582 }
583 } else {
584 clk = GetAskClock("", false, false);
585 if (clk>0) {
586 tests[hits].ST = true;
587 if ( ASKDemod_ext("0 0 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
588 tests[hits].modulation = DEMOD_ASK;
589 tests[hits].bitrate = bitRate;
590 tests[hits].inverted = false;
591 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
592 ++hits;
593 }
594 tests[hits].ST = true;
595 if ( ASKDemod_ext("0 1 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
596 tests[hits].modulation = DEMOD_ASK;
597 tests[hits].bitrate = bitRate;
598 tests[hits].inverted = true;
599 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
600 ++hits;
601 }
602 if ( ASKbiphaseDemod("0 0 0 2", false) && test(DEMOD_BI, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {
603 tests[hits].modulation = DEMOD_BI;
604 tests[hits].bitrate = bitRate;
605 tests[hits].inverted = false;
606 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
607 tests[hits].ST = false;
608 ++hits;
609 }
610 if ( ASKbiphaseDemod("0 0 1 2", false) && test(DEMOD_BIa, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {
611 tests[hits].modulation = DEMOD_BIa;
612 tests[hits].bitrate = bitRate;
613 tests[hits].inverted = true;
614 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
615 tests[hits].ST = false;
616 ++hits;
617 }
618 }
619 clk = GetNrzClock("", false, false);
620 if (clk>8) { //clock of rf/8 is likely a false positive, so don't use it.
621 if ( NRZrawDemod("0 0 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
622 tests[hits].modulation = DEMOD_NRZ;
623 tests[hits].bitrate = bitRate;
624 tests[hits].inverted = false;
625 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
626 tests[hits].ST = false;
627 ++hits;
628 }
629
630 if ( NRZrawDemod("0 1 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
631 tests[hits].modulation = DEMOD_NRZ;
632 tests[hits].bitrate = bitRate;
633 tests[hits].inverted = true;
634 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
635 tests[hits].ST = false;
636 ++hits;
637 }
638 }
639
640 clk = GetPskClock("", false, false);
641 if (clk>0) {
642 // allow undo
643 save_restoreGB(GRAPH_SAVE);
644 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
645 CmdLtrim("160");
646 if ( PSKDemod("0 0 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
647 tests[hits].modulation = DEMOD_PSK1;
648 tests[hits].bitrate = bitRate;
649 tests[hits].inverted = false;
650 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
651 tests[hits].ST = false;
652 ++hits;
653 }
654 if ( PSKDemod("0 1 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
655 tests[hits].modulation = DEMOD_PSK1;
656 tests[hits].bitrate = bitRate;
657 tests[hits].inverted = true;
658 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
659 tests[hits].ST = false;
660 ++hits;
661 }
662 // PSK2 - needs a call to psk1TOpsk2.
663 if ( PSKDemod("0 0 6", false)) {
664 psk1TOpsk2(DemodBuffer, DemodBufferLen);
665 if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
666 tests[hits].modulation = DEMOD_PSK2;
667 tests[hits].bitrate = bitRate;
668 tests[hits].inverted = false;
669 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
670 tests[hits].ST = false;
671 ++hits;
672 }
673 } // inverse waves does not affect this demod
674 // PSK3 - needs a call to psk1TOpsk2.
675 if ( PSKDemod("0 0 6", false)) {
676 psk1TOpsk2(DemodBuffer, DemodBufferLen);
677 if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
678 tests[hits].modulation = DEMOD_PSK3;
679 tests[hits].bitrate = bitRate;
680 tests[hits].inverted = false;
681 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
682 tests[hits].ST = false;
683 ++hits;
684 }
685 } // inverse waves does not affect this demod
686 //undo trim samples
687 save_restoreGB(GRAPH_RESTORE);
688 }
689 }
690 if ( hits == 1) {
691 config.modulation = tests[0].modulation;
692 config.bitrate = tests[0].bitrate;
693 config.inverted = tests[0].inverted;
694 config.offset = tests[0].offset;
695 config.block0 = tests[0].block0;
696 config.Q5 = tests[0].Q5;
697 config.ST = tests[0].ST;
698
699 printConfiguration( config);
700 return true;
701 }
702
703 if ( hits > 1) {
704 PrintAndLog("Found [%d] possible matches for modulation.",hits);
705 for(int i=0; i<hits; ++i){
706 PrintAndLog("--[%d]---------------", i+1);
707 printConfiguration( tests[i]);
708 }
709 }
710 return false;
711 }
712
713 bool testModulation(uint8_t mode, uint8_t modread){
714 switch( mode ){
715 case DEMOD_FSK:
716 if (modread >= DEMOD_FSK1 && modread <= DEMOD_FSK2a) return true;
717 break;
718 case DEMOD_ASK:
719 if (modread == DEMOD_ASK) return true;
720 break;
721 case DEMOD_PSK1:
722 if (modread == DEMOD_PSK1) return true;
723 break;
724 case DEMOD_PSK2:
725 if (modread == DEMOD_PSK2) return true;
726 break;
727 case DEMOD_PSK3:
728 if (modread == DEMOD_PSK3) return true;
729 break;
730 case DEMOD_NRZ:
731 if (modread == DEMOD_NRZ) return true;
732 break;
733 case DEMOD_BI:
734 if (modread == DEMOD_BI) return true;
735 break;
736 case DEMOD_BIa:
737 if (modread == DEMOD_BIa) return true;
738 break;
739 default:
740 return false;
741 }
742 return false;
743 }
744
745 bool testQ5Modulation(uint8_t mode, uint8_t modread){
746 switch( mode ){
747 case DEMOD_FSK:
748 if (modread >= 4 && modread <= 5) return true;
749 break;
750 case DEMOD_ASK:
751 if (modread == 0) return true;
752 break;
753 case DEMOD_PSK1:
754 if (modread == 1) return true;
755 break;
756 case DEMOD_PSK2:
757 if (modread == 2) return true;
758 break;
759 case DEMOD_PSK3:
760 if (modread == 3) return true;
761 break;
762 case DEMOD_NRZ:
763 if (modread == 7) return true;
764 break;
765 case DEMOD_BI:
766 if (modread == 6) return true;
767 break;
768 default:
769 return false;
770 }
771 return false;
772 }
773
774 int convertQ5bitRate(uint8_t bitRateRead) {
775 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};
776 for (int i=0; i<8; i++)
777 if (expected[i] == bitRateRead)
778 return i;
779
780 return -1;
781 }
782
783 bool testQ5(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk){
784
785 if ( DemodBufferLen < 64 ) return false;
786 uint8_t si = 0;
787 for (uint8_t idx = 28; idx < 64; idx++){
788 si = idx;
789 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;
790
791 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
792 uint8_t resv = PackBits(si, 8, DemodBuffer); si += 8;
793 // 2nibble must be zeroed.
794 if (safer != 0x6 && safer != 0x9) continue;
795 if ( resv > 0x00) continue;
796 //uint8_t pageSel = PackBits(si, 1, DemodBuffer); si += 1;
797 //uint8_t fastWrite = PackBits(si, 1, DemodBuffer); si += 1;
798 si += 1+1;
799 int bitRate = PackBits(si, 6, DemodBuffer)*2 + 2; si += 6; //bit rate
800 if (bitRate > 128 || bitRate < 8) continue;
801
802 //uint8_t AOR = PackBits(si, 1, DemodBuffer); si += 1;
803 //uint8_t PWD = PackBits(si, 1, DemodBuffer); si += 1;
804 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2; //could check psk cr
805 //uint8_t inverse = PackBits(si, 1, DemodBuffer); si += 1;
806 si += 1+1+2+1;
807 uint8_t modread = PackBits(si, 3, DemodBuffer); si += 3;
808 uint8_t maxBlk = PackBits(si, 3, DemodBuffer); si += 3;
809 //uint8_t ST = PackBits(si, 1, DemodBuffer); si += 1;
810 if (maxBlk == 0) continue;
811 //test modulation
812 if (!testQ5Modulation(mode, modread)) continue;
813 if (bitRate != clk) continue;
814 *fndBitRate = convertQ5bitRate(bitRate);
815 if (*fndBitRate < 0) continue;
816 *offset = idx;
817
818 return true;
819 }
820 return false;
821 }
822
823 bool testBitRate(uint8_t readRate, uint8_t clk){
824 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};
825 if (expected[readRate] == clk)
826 return true;
827
828 return false;
829 }
830
831 bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5){
832
833 if ( DemodBufferLen < 64 ) return false;
834 uint8_t si = 0;
835 for (uint8_t idx = 28; idx < 64; idx++){
836 si = idx;
837 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;
838
839 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
840 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode
841 // 2nibble must be zeroed.
842 // moved test to here, since this gets most faults first.
843 if ( resv > 0x00) continue;
844
845 int bitRate = PackBits(si, 6, DemodBuffer); si += 6; //bit rate (includes extended mode part of rate)
846 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode
847 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1;
848 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //could check psk cr
849 //uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24, 30, 31 could be tested for 0 if not extended mode
850 //uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;
851
852 //if extended mode
853 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? true : false;
854
855 if (!extMode) {
856 if (bitRate > 7) continue;
857 if (!testBitRate(bitRate, clk)) continue;
858 } else { //extended mode bitrate = same function to calc bitrate as em4x05
859 if (EM4x05_GET_BITRATE(bitRate) != clk) continue;
860
861 }
862 //test modulation
863 if (!testModulation(mode, modread)) continue;
864 *fndBitRate = bitRate;
865 *offset = idx;
866 *Q5 = false;
867 return true;
868 }
869 if (testQ5(mode, offset, fndBitRate, clk)) {
870 *Q5 = true;
871 return true;
872 }
873 return false;
874 }
875
876 void printT55xxBlock(const char *blockNum){
877
878 uint8_t i = config.offset;
879 uint8_t endpos = 32 + i;
880 uint32_t blockData = 0;
881 uint8_t bits[64] = {0x00};
882
883 if ( !DemodBufferLen) return;
884
885 if ( endpos > DemodBufferLen){
886 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);
887 return;
888 }
889
890 for (; i < endpos; ++i)
891 bits[i - config.offset]=DemodBuffer[i];
892
893 blockData = PackBits(0, 32, bits);
894
895 PrintAndLog(" %s | %08X | %s", blockNum, blockData, sprint_bin(bits,32));
896 }
897
898 int special(const char *Cmd) {
899 uint32_t blockData = 0;
900 uint8_t bits[32] = {0x00};
901
902 PrintAndLog("OFFSET | DATA | BINARY");
903 PrintAndLog("----------------------------------------------------");
904 int i,j = 0;
905 for (; j < 64; ++j){
906
907 for (i = 0; i < 32; ++i)
908 bits[i]=DemodBuffer[j+i];
909
910 blockData = PackBits(0, 32, bits);
911
912 PrintAndLog(" %02d | 0x%08X | %s",j , blockData, sprint_bin(bits,32));
913 }
914 return 0;
915 }
916
917 int printConfiguration( t55xx_conf_block_t b){
918 PrintAndLog("Chip Type : %s", (b.Q5) ? "T5555(Q5)" : "T55x7");
919 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );
920 PrintAndLog("Bit Rate : %s", GetBitRateStr(b.bitrate, (b.block0 & T55x7_X_MODE && (b.block0>>28==6 || b.block0>>28==9))) );
921 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );
922 PrintAndLog("Offset : %d", b.offset);
923 PrintAndLog("Seq. Term. : %s", (b.ST) ? "Yes" : "No" );
924 PrintAndLog("Block0 : 0x%08X", b.block0);
925 PrintAndLog("");
926 return 0;
927 }
928
929 int CmdT55xxWakeUp(const char *Cmd) {
930 uint32_t password = 0;
931 if ( strlen(Cmd) <= 0 ) return usage_t55xx_wakup();
932 char cmdp = param_getchar(Cmd, 0);
933 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wakup();
934
935 password = param_get32ex(Cmd, 0, 0, 16);
936
937 UsbCommand c = {CMD_T55XX_WAKEUP, {password, 0, 0}};
938 clearCommandBuffer();
939 SendCommand(&c);
940 PrintAndLog("Wake up command sent. Try read now");
941 return 0;
942 }
943
944 int CmdT55xxWriteBlock(const char *Cmd) {
945 uint8_t block = 0xFF; //default to invalid block
946 uint32_t data = 0; //default to blank Block
947 uint32_t password = 0; //default to blank Block 7
948 uint32_t downlink_mode = 0;
949
950 bool usepwd = false;
951 bool page1 = false;
952 bool gotdata = false;
953 bool testMode = false;
954 bool errors = false;
955 uint8_t cmdp = 0;
956 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
957 switch(param_getchar(Cmd, cmdp)) {
958 case 'h':
959 case 'H':
960 return usage_t55xx_write();
961 case 'b':
962 case 'B':
963 errors |= param_getdec(Cmd, cmdp+1, &block);
964 cmdp += 2;
965 break;
966 case 'd':
967 case 'D':
968 data = param_get32ex(Cmd, cmdp+1, 0, 16);
969 gotdata = true;
970 cmdp += 2;
971 break;
972 case 'p':
973 case 'P':
974 password = param_get32ex(Cmd, cmdp+1, 0, 16);
975 usepwd = true;
976 cmdp += 2;
977 break;
978 case 't':
979 case 'T':
980 testMode = true;
981 cmdp++;
982 break;
983 case '1':
984 page1 = true;
985 cmdp++;
986 break;
987 case 'r':
988 case 'R':
989 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
990 if (downlink_mode > 3) downlink_mode = 0;
991 cmdp +=2;
992 break;
993 default:
994 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
995 errors = true;
996 break;
997 }
998 }
999 if (errors || !gotdata) return usage_t55xx_write();
1000
1001 if (block > 7) {
1002 PrintAndLog("Block number must be between 0 and 7");
1003 return 0;
1004 }
1005
1006 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
1007 UsbCommand resp;
1008 c.d.asBytes[0] = (page1) ? 0x2 : 0;
1009 c.d.asBytes[0] |= (testMode) ? 0x4 : 0;
1010 c.d.asBytes[0] |= (downlink_mode << 3);
1011
1012 char pwdStr[16] = {0};
1013 snprintf(pwdStr, sizeof(pwdStr), "pwd: 0x%08X", password);
1014
1015 PrintAndLog("Writing page %d block: %02d data: 0x%08X %s", page1, block, data, (usepwd) ? pwdStr : "" );
1016
1017 //Password mode
1018 if (usepwd) {
1019 c.arg[2] = password;
1020 c.d.asBytes[0] |= 0x1;
1021 }
1022
1023 clearCommandBuffer();
1024 SendCommand(&c);
1025 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
1026 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)");
1027 return 0;
1028 }
1029 return 1;
1030 }
1031
1032 int CmdT55xxReadTrace(const char *Cmd) {
1033 char cmdp = param_getchar(Cmd, 0);
1034 bool pwdmode = false;
1035 uint32_t password = 0;
1036 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
1037 return usage_t55xx_trace();
1038
1039 if (strlen(Cmd)==0)
1040 if ( !AquireData( T55x7_PAGE1, REGULAR_READ_MODE_BLOCK, pwdmode, password,0 ) )
1041 return 0;
1042
1043 if ( config.Q5 ) {
1044 if (!DecodeT5555TraceBlock()) return 0;
1045 } else {
1046 if (!DecodeT55xxBlock()) return 0;
1047 }
1048
1049 if ( !DemodBufferLen ) return 0;
1050
1051 RepaintGraphWindow();
1052 uint8_t repeat = (config.offset > 5) ? 32 : 0;
1053
1054 uint8_t si = config.offset+repeat;
1055 uint32_t bl1 = PackBits(si, 32, DemodBuffer);
1056 uint32_t bl2 = PackBits(si+32, 32, DemodBuffer);
1057
1058 if (config.Q5) {
1059 uint32_t hdr = PackBits(si, 9, DemodBuffer); si += 9;
1060
1061 if (hdr != 0x1FF) {
1062 PrintAndLog("Invalid Q5 Trace data header (expected 0x1FF, found %X)", hdr);
1063 return 0;
1064 }
1065
1066 t5555_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .icr = 0, .lotidc = '?', .lotid = 0, .wafer = 0, .dw =0};
1067
1068 data.icr = PackBits(si, 2, DemodBuffer); si += 2;
1069 data.lotidc = 'Z' - PackBits(si, 2, DemodBuffer); si += 3;
1070
1071 data.lotid = PackBits(si, 4, DemodBuffer); si += 5;
1072 data.lotid <<= 4;
1073 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1074 data.lotid <<= 4;
1075 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1076 data.lotid <<= 4;
1077 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1078 data.lotid <<= 1;
1079 data.lotid |= PackBits(si, 1, DemodBuffer); si += 1;
1080
1081 data.wafer = PackBits(si, 3, DemodBuffer); si += 4;
1082 data.wafer <<= 2;
1083 data.wafer |= PackBits(si, 2, DemodBuffer); si += 2;
1084
1085 data.dw = PackBits(si, 2, DemodBuffer); si += 3;
1086 data.dw <<= 4;
1087 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1088 data.dw <<= 4;
1089 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1090 data.dw <<= 4;
1091 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1092
1093 printT5555Trace(data, repeat);
1094
1095 } else {
1096
1097 t55x7_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .acl = 0, .mfc = 0, .cid = 0, .year = 0, .quarter = 0, .icr = 0, .lotid = 0, .wafer = 0, .dw = 0};
1098
1099 data.acl = PackBits(si, 8, DemodBuffer); si += 8;
1100 if ( data.acl != 0xE0 ) {
1101 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");
1102 return 0;
1103 }
1104
1105 data.mfc = PackBits(si, 8, DemodBuffer); si += 8;
1106 data.cid = PackBits(si, 5, DemodBuffer); si += 5;
1107 data.icr = PackBits(si, 3, DemodBuffer); si += 3;
1108 data.year = PackBits(si, 4, DemodBuffer); si += 4;
1109 data.quarter = PackBits(si, 2, DemodBuffer); si += 2;
1110 data.lotid = PackBits(si, 14, DemodBuffer); si += 14;
1111 data.wafer = PackBits(si, 5, DemodBuffer); si += 5;
1112 data.dw = PackBits(si, 15, DemodBuffer);
1113
1114 time_t t = time(NULL);
1115 struct tm tm = *localtime(&t);
1116 if ( data.year > tm.tm_year-110)
1117 data.year += 2000;
1118 else
1119 data.year += 2010;
1120
1121 printT55x7Trace(data, repeat);
1122 }
1123 return 0;
1124 }
1125
1126 void printT55x7Trace( t55x7_tracedata_t data, uint8_t repeat ){
1127 PrintAndLog("-- T55x7 Trace Information ----------------------------------");
1128 PrintAndLog("-------------------------------------------------------------");
1129 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", data.acl, data.acl);
1130 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", data.mfc, data.mfc, getManufacturerName(data.mfc));
1131 PrintAndLog(" CID : 0x%02X (%d) - %s", data.cid, data.cid, GetModelStrFromCID(data.cid));
1132 PrintAndLog(" ICR IC Revision : %d", data.icr );
1133 PrintAndLog(" Manufactured");
1134 PrintAndLog(" Year/Quarter : %d/%d", data.year, data.quarter);
1135 PrintAndLog(" Lot ID : %d", data.lotid );
1136 PrintAndLog(" Wafer number : %d", data.wafer);
1137 PrintAndLog(" Die Number : %d", data.dw);
1138 PrintAndLog("-------------------------------------------------------------");
1139 PrintAndLog(" Raw Data - Page 1");
1140 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );
1141 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );
1142 PrintAndLog("-------------------------------------------------------------");
1143
1144 /*
1145 TRACE - BLOCK O
1146 Bits Definition HEX
1147 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
1148 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
1149 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
1150 22-24 ICR IC revision
1151 25-28 YEAR (BCD encoded) 9 (= 2009)
1152 29-30 QUARTER 1,2,3,4
1153 31-32 LOT ID
1154
1155 TRACE - BLOCK 1
1156 1-12 LOT ID
1157 13-17 Wafer number
1158 18-32 DW, die number sequential
1159 */
1160 }
1161
1162 void printT5555Trace( t5555_tracedata_t data, uint8_t repeat ){
1163 PrintAndLog("-- T5555 (Q5) Trace Information -----------------------------");
1164 PrintAndLog("-------------------------------------------------------------");
1165 PrintAndLog(" ICR IC Revision : %d", data.icr );
1166 PrintAndLog(" Lot : %c%d", data.lotidc, data.lotid);
1167 PrintAndLog(" Wafer number : %d", data.wafer);
1168 PrintAndLog(" Die Number : %d", data.dw);
1169 PrintAndLog("-------------------------------------------------------------");
1170 PrintAndLog(" Raw Data - Page 1");
1171 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );
1172 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );
1173
1174 /*
1175 ** Q5 **
1176 TRACE - BLOCK O and BLOCK1
1177 Bits Definition HEX
1178 1-9 Header 0x1FF
1179 10-11 IC Revision
1180 12-13 Lot ID char
1181 15-35 Lot ID (NB parity)
1182 36-41 Wafer number (NB parity)
1183 42-58 DW, die number sequential (NB parity)
1184 60-63 Parity bits
1185 64 Always zero
1186 */
1187 }
1188
1189 //need to add Q5 info...
1190 int CmdT55xxInfo(const char *Cmd){
1191 /*
1192 Page 0 Block 0 Configuration data.
1193 Normal mode
1194 Extended mode
1195 */
1196 bool pwdmode = false;
1197 uint32_t password = 0;
1198 char cmdp = param_getchar(Cmd, 0);
1199
1200 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
1201 return usage_t55xx_info();
1202
1203 if (strlen(Cmd)==0)
1204 if ( !AquireData( T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, pwdmode, password,0 ) )
1205 return 1;
1206
1207 if (!DecodeT55xxBlock()) return 1;
1208
1209 // too little space to start with
1210 if ( DemodBufferLen < 32) return 1;
1211
1212 uint8_t si = config.offset;
1213 uint32_t bl0 = PackBits(si, 32, DemodBuffer);
1214
1215 uint32_t safer = PackBits(si, 4, DemodBuffer); si += 4;
1216 uint32_t resv = PackBits(si, 7, DemodBuffer); si += 7;
1217 uint32_t dbr = PackBits(si, 3, DemodBuffer); si += 3;
1218 uint32_t extend = PackBits(si, 1, DemodBuffer); si += 1;
1219 uint32_t datamod = PackBits(si, 5, DemodBuffer); si += 5;
1220 uint32_t pskcf = PackBits(si, 2, DemodBuffer); si += 2;
1221 uint32_t aor = PackBits(si, 1, DemodBuffer); si += 1;
1222 uint32_t otp = PackBits(si, 1, DemodBuffer); si += 1;
1223 uint32_t maxblk = PackBits(si, 3, DemodBuffer); si += 3;
1224 uint32_t pwd = PackBits(si, 1, DemodBuffer); si += 1;
1225 uint32_t sst = PackBits(si, 1, DemodBuffer); si += 1;
1226 uint32_t fw = PackBits(si, 1, DemodBuffer); si += 1;
1227 uint32_t inv = PackBits(si, 1, DemodBuffer); si += 1;
1228 uint32_t por = PackBits(si, 1, DemodBuffer); si += 1;
1229
1230 if (config.Q5) PrintAndLog("*** Warning *** Config Info read off a Q5 will not display as expected");
1231 PrintAndLog("");
1232 PrintAndLog("-- T55x7 Configuration & Tag Information --------------------");
1233 PrintAndLog("-------------------------------------------------------------");
1234 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
1235 PrintAndLog(" reserved : %d", resv);
1236 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr, extend));
1237 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
1238 PrintAndLog(" Modulation : %s", GetModulationStr(datamod));
1239 PrintAndLog(" PSK clock frequency : %d", pskcf);
1240 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
1241 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
1242 PrintAndLog(" Max block : %d", maxblk);
1243 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
1244 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
1245 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
1246 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
1247 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
1248 PrintAndLog("-------------------------------------------------------------");
1249 PrintAndLog(" Raw Data - Page 0");
1250 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset,32) );
1251 PrintAndLog("-------------------------------------------------------------");
1252
1253 return 0;
1254 }
1255
1256 int CmdT55xxDump(const char *Cmd){
1257
1258 uint32_t password = 0;
1259 char cmdp = param_getchar(Cmd, 0);
1260 bool override = false;
1261 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_dump();
1262
1263 bool usepwd = ( strlen(Cmd) > 0);
1264 if ( usepwd ){
1265 password = param_get32ex(Cmd, 0, 0, 16);
1266 if (param_getchar(Cmd, 1) =='o' )
1267 override = true;
1268 }
1269
1270 printT5xxHeader(0);
1271 for ( uint8_t i = 0; i <8; ++i)
1272 T55xxReadBlock(i, 0, usepwd, override, password,0);
1273
1274 printT5xxHeader(1);
1275 for ( uint8_t i = 0; i<4; i++)
1276 T55xxReadBlock(i, 1, usepwd, override, password,0);
1277
1278 return 1;
1279 }
1280
1281 int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password, uint8_t downlink_mode ){
1282 // arg0 bitmodes:
1283 // bit0 = pwdmode
1284 // bit1 = page to read from
1285 uint8_t arg0 = (page<<1) | pwdmode;
1286 arg0 |= (downlink_mode << 3);
1287 UsbCommand c = {CMD_T55XX_READ_BLOCK, {arg0, block, password}};
1288
1289 clearCommandBuffer();
1290 SendCommand(&c);
1291 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
1292 PrintAndLog("command execution time out");
1293 return 0;
1294 }
1295 getSamples(12000,true);
1296 return 1;
1297 }
1298
1299 char * GetBitRateStr(uint32_t id, bool xmode) {
1300 static char buf[25];
1301
1302 char *retStr = buf;
1303 if (xmode) { //xmode bitrate calc is same as em4x05 calc
1304 snprintf(retStr,sizeof(buf),"%d - RF/%d", id, EM4x05_GET_BITRATE(id));
1305 } else {
1306 switch (id) {
1307 case 0: snprintf(retStr,sizeof(buf),"%d - RF/8",id); break;
1308 case 1: snprintf(retStr,sizeof(buf),"%d - RF/16",id); break;
1309 case 2: snprintf(retStr,sizeof(buf),"%d - RF/32",id); break;
1310 case 3: snprintf(retStr,sizeof(buf),"%d - RF/40",id); break;
1311 case 4: snprintf(retStr,sizeof(buf),"%d - RF/50",id); break;
1312 case 5: snprintf(retStr,sizeof(buf),"%d - RF/64",id); break;
1313 case 6: snprintf(retStr,sizeof(buf),"%d - RF/100",id); break;
1314 case 7: snprintf(retStr,sizeof(buf),"%d - RF/128",id); break;
1315 default: snprintf(retStr,sizeof(buf),"%d - (Unknown)",id); break;
1316 }
1317 }
1318 return buf;
1319 }
1320
1321 char * GetSaferStr(uint32_t id) {
1322 static char buf[40];
1323 char *retStr = buf;
1324
1325 snprintf(retStr,sizeof(buf),"%d",id);
1326 if (id == 6) {
1327 snprintf(retStr,sizeof(buf),"%d - passwd",id);
1328 }
1329 if (id == 9 ){
1330 snprintf(retStr,sizeof(buf),"%d - testmode",id);
1331 }
1332
1333 return buf;
1334 }
1335
1336 char * GetModulationStr( uint32_t id){
1337 static char buf[60];
1338 char *retStr = buf;
1339
1340 switch (id){
1341 case 0: snprintf(retStr,sizeof(buf),"%d - DIRECT (ASK/NRZ)",id); break;
1342 case 1: snprintf(retStr,sizeof(buf),"%d - PSK 1 phase change when input changes",id); break;
1343 case 2: snprintf(retStr,sizeof(buf),"%d - PSK 2 phase change on bitclk if input high",id); break;
1344 case 3: snprintf(retStr,sizeof(buf),"%d - PSK 3 phase change on rising edge of input",id); break;
1345 case 4: snprintf(retStr,sizeof(buf),"%d - FSK 1 RF/8 RF/5",id); break;
1346 case 5: snprintf(retStr,sizeof(buf),"%d - FSK 2 RF/8 RF/10",id); break;
1347 case 6: snprintf(retStr,sizeof(buf),"%d - FSK 1a RF/5 RF/8",id); break;
1348 case 7: snprintf(retStr,sizeof(buf),"%d - FSK 2a RF/10 RF/8",id); break;
1349 case 8: snprintf(retStr,sizeof(buf),"%d - Manchester",id); break;
1350 case 16: snprintf(retStr,sizeof(buf),"%d - Biphase",id); break;
1351 case 0x18: snprintf(retStr,sizeof(buf),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id); break;
1352 case 17: snprintf(retStr,sizeof(buf),"%d - Reserved",id); break;
1353 default: snprintf(retStr,sizeof(buf),"0x%02X (Unknown)",id); break;
1354 }
1355 return buf;
1356 }
1357
1358 char * GetModelStrFromCID(uint32_t cid){
1359
1360 static char buf[10];
1361 char *retStr = buf;
1362
1363 if (cid == 1) snprintf(retStr, sizeof(buf),"ATA5577M1");
1364 if (cid == 2) snprintf(retStr, sizeof(buf),"ATA5577M2");
1365 return buf;
1366 }
1367
1368 char * GetSelectedModulationStr( uint8_t id){
1369
1370 static char buf[20];
1371 char *retStr = buf;
1372
1373 switch (id) {
1374 case DEMOD_FSK: snprintf(retStr,sizeof(buf),"FSK"); break;
1375 case DEMOD_FSK1: snprintf(retStr,sizeof(buf),"FSK1"); break;
1376 case DEMOD_FSK1a: snprintf(retStr,sizeof(buf),"FSK1a"); break;
1377 case DEMOD_FSK2: snprintf(retStr,sizeof(buf),"FSK2"); break;
1378 case DEMOD_FSK2a: snprintf(retStr,sizeof(buf),"FSK2a"); break;
1379 case DEMOD_ASK: snprintf(retStr,sizeof(buf),"ASK"); break;
1380 case DEMOD_NRZ: snprintf(retStr,sizeof(buf),"DIRECT/NRZ"); break;
1381 case DEMOD_PSK1: snprintf(retStr,sizeof(buf),"PSK1"); break;
1382 case DEMOD_PSK2: snprintf(retStr,sizeof(buf),"PSK2"); break;
1383 case DEMOD_PSK3: snprintf(retStr,sizeof(buf),"PSK3"); break;
1384 case DEMOD_BI: snprintf(retStr,sizeof(buf),"BIPHASE"); break;
1385 case DEMOD_BIa: snprintf(retStr,sizeof(buf),"BIPHASEa - (CDP)"); break;
1386 default: snprintf(retStr,sizeof(buf),"(Unknown)"); break;
1387 }
1388 return buf;
1389 }
1390
1391 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
1392
1393 int i = start;
1394 int j = len-1;
1395
1396 if (len > 32) return 0;
1397
1398 uint32_t tmp = 0;
1399 for (; j >= 0; --j, ++i)
1400 tmp |= bits[i] << j;
1401
1402 return tmp;
1403 }
1404
1405 int CmdResetRead(const char *Cmd) {
1406 UsbCommand c = {CMD_T55XX_RESET_READ, {0,0,0}};
1407
1408 clearCommandBuffer();
1409 SendCommand(&c);
1410 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
1411 PrintAndLog("command execution time out");
1412 return 0;
1413 }
1414
1415 uint8_t got[BIGBUF_SIZE-1];
1416 GetFromBigBuf(got, sizeof(got), 0, NULL, -1 , 0);
1417 setGraphBuf(got, sizeof(got));
1418 return 1;
1419 }
1420
1421 int CmdT55xxWipe(const char *Cmd) {
1422 char writeData[20] = {0};
1423 char *ptrData = writeData;
1424
1425 char cmdp = param_getchar(Cmd, 0);
1426 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wipe();
1427
1428 bool Q5 = (cmdp == 'q' || cmdp == 'Q');
1429
1430 // Try with the default password to reset block 0
1431 // With a pwd should work even if pwd bit not set
1432 PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n");
1433
1434 if ( Q5 ){
1435 snprintf(ptrData,sizeof(writeData),"b 0 d 6001F004 p 0");
1436 } else {
1437 snprintf(ptrData,sizeof(writeData),"b 0 d 00088040 p 0");
1438 }
1439
1440 if (!CmdT55xxWriteBlock(ptrData)) PrintAndLog("Error writing blk 0");
1441
1442 for (uint8_t blk = 1; blk<8; blk++) {
1443 snprintf(ptrData,sizeof(writeData),"b %d d 0", blk);
1444 if (!CmdT55xxWriteBlock(ptrData))
1445 PrintAndLog("Error writing blk %d", blk);
1446
1447 memset(writeData, 0x00, sizeof(writeData));
1448 }
1449 return 0;
1450 }
1451
1452 int CmdT55xxBruteForce(const char *Cmd) {
1453
1454 // load a default pwd file.
1455 char buf[9];
1456 char filename[FILE_PATH_SIZE]={0};
1457 int keycnt = 0;
1458 int ch;
1459 uint8_t stKeyBlock = 20;
1460 uint8_t *keyBlock = NULL, *p = NULL;
1461 uint32_t start_password = 0x00000000; //start password
1462 uint32_t end_password = 0xFFFFFFFF; //end password
1463 bool found = false;
1464 uint8_t downlink_mode = 0;
1465 bool try_all_dl_modes = false;
1466 uint8_t dl_mode = 0;
1467 uint8_t cmd_offset = 0;
1468 int cmd_opt = 0;
1469
1470 char cmdp = param_getchar(Cmd, 0);
1471
1472 if (cmdp == 'h' || cmdp == 'H') return usage_t55xx_bruteforce();
1473 if (cmdp == 'r' || cmdp == 'R') {
1474 downlink_mode = param_getchar(Cmd, 1) - '0'; // get 2nd option, as this is fixed order.
1475 if (downlink_mode == 4) try_all_dl_modes = true;
1476 if (downlink_mode > 3) downlink_mode = 0;
1477 cmd_opt += 2; // To help start/end passwords for range to be found
1478 cmd_offset += 4; // r <sp> x <sp> To help the string offset for filename start position in cmd
1479 cmdp = param_getchar(Cmd, 2); // get 3rd option, as this is fixed order.
1480 }
1481
1482 keyBlock = calloc(stKeyBlock, 6);
1483 if (keyBlock == NULL) return 1;
1484
1485 if (cmdp == 'i' || cmdp == 'I') {
1486
1487 int len = strlen(Cmd+2);
1488 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
1489 memcpy(filename, Cmd+2+cmd_offset, len);
1490
1491 FILE * f = fopen( filename , "r");
1492
1493 if ( !f ) {
1494 PrintAndLog("File: %s: not found or locked.", filename);
1495 free(keyBlock);
1496 return 1;
1497 }
1498
1499 while( fgets(buf, sizeof(buf), f) ) {
1500 if (strlen(buf) < 8 || buf[7] == '\n') continue;
1501
1502 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1503
1504 //The line start with # is comment, skip
1505 if( buf[0]=='#' ) continue;
1506
1507 if (!isxdigit((unsigned char)buf[0])) {
1508 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);
1509 continue;
1510 }
1511
1512 buf[8] = 0;
1513
1514 if ( stKeyBlock - keycnt < 2) {
1515 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1516 if (!p) {
1517 PrintAndLog("Cannot allocate memory for defaultKeys");
1518 free(keyBlock);
1519 fclose(f);
1520 return 2;
1521 }
1522 keyBlock = p;
1523 }
1524 memset(keyBlock + 4 * keycnt, 0, 4);
1525 num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);
1526 PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));
1527 keycnt++;
1528 memset(buf, 0, sizeof(buf));
1529 }
1530 fclose(f);
1531
1532 if (keycnt == 0) {
1533 PrintAndLog("No keys found in file");
1534 free(keyBlock);
1535 return 1;
1536 }
1537 PrintAndLog("Loaded %d keys", keycnt);
1538
1539 // loop
1540 uint64_t testpwd = 0x00;
1541 for (uint16_t c = 0; c < keycnt; ++c ) {
1542
1543 if (ukbhit()) {
1544 ch = getchar();
1545 (void)ch;
1546 printf("\naborted via keyboard!\n");
1547 free(keyBlock);
1548 return 0;
1549 }
1550
1551 testpwd = bytes_to_num(keyBlock + 4*c, 4);
1552
1553 PrintAndLog("Testing %08X", testpwd);
1554
1555 // Try each downlink_mode if asked to
1556 // donwlink_mode will = 0 if > 3 or set to 0, so loop from 0 - 3
1557 for (dl_mode = downlink_mode; dl_mode <= 3; dl_mode++){
1558 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, testpwd, dl_mode)) {
1559 PrintAndLog("Acquiring data from device failed. Quitting");
1560 free(keyBlock);
1561 return 0;
1562 }
1563
1564 found = tryDetectModulation();
1565
1566 if ( found ) {
1567 PrintAndLog("Found valid password: [%08X]", testpwd);
1568 free(keyBlock);
1569
1570 T55xx_Print_DownlinkMode (dl_mode);
1571
1572 return 0;
1573 }
1574 if (!try_all_dl_modes) // Exit loop if not trying all downlink modes
1575 dl_mode = 4;
1576 }
1577 }
1578 PrintAndLog("Password NOT found.");
1579 free(keyBlock);
1580 return 0;
1581 }
1582
1583 // Try to read Block 7, first :)
1584
1585 // incremental pwd range search
1586 start_password = param_get32ex(Cmd, cmd_opt , 0, 16);
1587 end_password = param_get32ex(Cmd, cmd_opt+1 , 0, 16);
1588
1589 if ( start_password >= end_password ) {
1590 free(keyBlock);
1591 return usage_t55xx_bruteforce();
1592 }
1593 PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);
1594
1595 uint32_t i = start_password;
1596
1597 while ((!found) && (i <= end_password)) {
1598
1599 printf(".");
1600 fflush(stdout);
1601 if (ukbhit()) {
1602 ch = getchar();
1603 (void)ch;
1604 printf("\naborted via keyboard!\n");
1605 free(keyBlock);
1606 return 0;
1607 }
1608 // Try each downlink_mode if asked to
1609 // donwlink_mode will = 0 if > 3 or set to 0, so loop from 0 - 3
1610 for (dl_mode = downlink_mode; dl_mode <= 3; dl_mode++){
1611 if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, i,dl_mode)) {
1612 PrintAndLog("Acquiring data from device failed. Quitting");
1613 free(keyBlock);
1614 return 0;
1615 }
1616 found = tryDetectModulation();
1617
1618 if (found) break;
1619 if (!try_all_dl_modes) // Exit loop if not trying all downlink modes
1620 dl_mode = 4;
1621 }
1622 if (found) break;
1623 i++;
1624 }
1625
1626 if (found){
1627 PrintAndLog("Found valid password: [%08x]", i);
1628 T55xx_Print_DownlinkMode (downlink_mode);
1629 }
1630 else{
1631 PrintAndLog("");
1632 PrintAndLog("Password NOT found. Last tried: [%08x]", --i);
1633 }
1634
1635 free(keyBlock);
1636 return 0;
1637 }
1638 // note length of data returned is different for different chips.
1639 // some return all page 1 (64 bits) and others return just that block (32 bits)
1640 // unfortunately the 64 bits makes this more likely to get a false positive...
1641 bool tryDetectP1(bool getData) {
1642 uint8_t preamble[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1};
1643 size_t startIdx = 0;
1644 uint8_t fc1 = 0, fc2 = 0, ans = 0;
1645 int clk = 0, firstClockEdge = 0;
1646 bool st = true;
1647
1648 if ( getData ) {
1649 if ( !AquireData(T55x7_PAGE1, 1, false, 0,0) )
1650 return false;
1651 }
1652
1653 // try fsk clock detect. if successful it cannot be any other type of modulation... (in theory...)
1654 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);
1655 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
1656 if ( FSKrawDemod("0 0", false) &&
1657 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1658 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1659 return true;
1660 }
1661 if ( FSKrawDemod("0 1", false) &&
1662 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1663 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1664 return true;
1665 }
1666 return false;
1667 }
1668
1669 // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...)
1670 clk = GetPskClock("", false, false);
1671 if (clk>0) {
1672 // allow undo
1673 // save_restoreGB(1);
1674 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
1675 //CmdLtrim("160");
1676 if ( PSKDemod("0 0 6", false) &&
1677 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1678 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1679 //save_restoreGB(0);
1680 return true;
1681 }
1682 if ( PSKDemod("0 1 6", false) &&
1683 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1684 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1685 //save_restoreGB(0);
1686 return true;
1687 }
1688 // PSK2 - needs a call to psk1TOpsk2.
1689 if ( PSKDemod("0 0 6", false)) {
1690 psk1TOpsk2(DemodBuffer, DemodBufferLen);
1691 if (preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1692 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1693 //save_restoreGB(0);
1694 return true;
1695 }
1696 } // inverse waves does not affect PSK2 demod
1697 //undo trim samples
1698 //save_restoreGB(0);
1699 // no other modulation clocks = 2 or 4 so quit searching
1700 if (fc1 != 8) return false;
1701 }
1702
1703 // try ask clock detect. it could be another type even if successful.
1704 clk = GetAskClock("", false, false);
1705 if (clk>0) {
1706 if ( ASKDemod_ext("0 0 1", false, false, 1, &st) &&
1707 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1708 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1709 return true;
1710 }
1711 st = true;
1712 if ( ASKDemod_ext("0 1 1", false, false, 1, &st) &&
1713 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1714 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1715 return true;
1716 }
1717 if ( ASKbiphaseDemod("0 0 0 2", false) &&
1718 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1719 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1720 return true;
1721 }
1722 if ( ASKbiphaseDemod("0 0 1 2", false) &&
1723 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1724 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1725 return true;
1726 }
1727 }
1728
1729 // try NRZ clock detect. it could be another type even if successful.
1730 clk = GetNrzClock("", false, false); //has the most false positives :(
1731 if (clk>0) {
1732 if ( NRZrawDemod("0 0 1", false) &&
1733 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1734 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1735 return true;
1736 }
1737 if ( NRZrawDemod("0 1 1", false) &&
1738 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1739 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1740 return true;
1741 }
1742 }
1743 return false;
1744 }
1745 // does this need to be a callable command?
1746 int CmdT55xxDetectPage1(const char *Cmd){
1747 bool errors = false;
1748 bool useGB = false;
1749 bool usepwd = false;
1750 uint32_t password = 0;
1751 uint8_t cmdp = 0;
1752
1753 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
1754 switch(param_getchar(Cmd, cmdp)) {
1755 case 'h':
1756 case 'H':
1757 return usage_t55xx_detectP1();
1758 case 'p':
1759 case 'P':
1760 password = param_get32ex(Cmd, cmdp+1, 0, 16);
1761 usepwd = true;
1762 cmdp += 2;
1763 break;
1764 case '1':
1765 // use Graphbuffer data
1766 useGB = true;
1767 cmdp++;
1768 break;
1769 default:
1770 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1771 errors = true;
1772 break;
1773 }
1774 }
1775 if (errors) return usage_t55xx_detectP1();
1776
1777 if ( !useGB ) {
1778 if ( !AquireData(T55x7_PAGE1, 1, usepwd, password,0) )
1779 return false;
1780 }
1781 bool success = tryDetectP1(false);
1782 if (success) PrintAndLog("T55xx chip found!");
1783 return success;
1784 }
1785
1786 static command_t CommandTable[] = {
1787 {"help", CmdHelp, 1, "This help"},
1788 {"bruteforce",CmdT55xxBruteForce,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"},
1789 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},
1790 {"detect", CmdT55xxDetect, 1, "[1] Try detecting the tag modulation from reading the configuration block."},
1791 {"p1detect", CmdT55xxDetectPage1,1, "[1] Try detecting if this is a t55xx tag by reading page 1"},
1792 {"read", CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},
1793 {"resetread", CmdResetRead, 0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"},
1794 {"write", CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},
1795 {"trace", CmdT55xxReadTrace, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},
1796 {"info", CmdT55xxInfo, 1, "[1] Show T55x7 configuration data (page 0/ blk 0)"},
1797 {"dump", CmdT55xxDump, 0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},
1798 {"special", special, 0, "Show block changes with 64 different offsets"},
1799 {"wakeup", CmdT55xxWakeUp, 0, "Send AOR wakeup command"},
1800 {"wipe", CmdT55xxWipe, 0, "[q] Wipe a T55xx tag and set defaults (will destroy any data on tag)"},
1801 {NULL, NULL, 0, NULL}
1802 };
1803
1804 int CmdLFT55XX(const char *Cmd) {
1805 CmdsParse(CommandTable, Cmd);
1806 return 0;
1807 }
1808
1809 int CmdHelp(const char *Cmd) {
1810 CmdsHelp(CommandTable);
1811 return 0;
1812 }
Impressum, Datenschutz