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