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