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