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