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