]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
7a377cc9bac4d6187596acb3950bd4bc05dd3a33
[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
26 #define LF_TRACE_BUFF_SIZE 20000 // 32 x 32 x 10 (32 bit times numofblock (7), times clock skip..)
27 #define LF_BITSSTREAM_LEN 1000 // more then 1000 bits shouldn't happend.. 8block * 4 bytes * 8bits =
28
29 // 0 = FSK
30 // 1 = ASK
31 // 2 = PSK
32 // 4 = NZR (direct)
33 typedef struct {
34 uint8_t modulation;
35 bool inversed;
36 uint32_t block0;
37 } t55xx_conf_block_t;
38
39 // Default configuration: FSK, not inversed.
40 t55xx_conf_block_t config = { .modulation = 0, .inversed = FALSE, .block0 = 0x00};
41
42 int usage_t55xx_config(){
43 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1]");
44 PrintAndLog("Options: ");
45 PrintAndLog(" h This help");
46 PrintAndLog(" d <FSK|ASK|PSK|NZ|BI> Set demodulation FSK / ASK / PSK / NZ / Biphase");
47 PrintAndLog(" i [1] Inverse data signal, defaults to normal");
48 PrintAndLog("");
49 PrintAndLog("Examples:");
50 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");
51 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");
52 PrintAndLog("");
53 return 0;
54 }
55 int usage_t55xx_read(){
56 PrintAndLog("Usage: lf t55xx read <block> <password>");
57 PrintAndLog(" <block>, block number to read. Between 0-7");
58 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");
59 PrintAndLog("");
60 PrintAndLog("Examples:");
61 PrintAndLog(" lf t55xx read 0 - read data from block 0");
62 PrintAndLog(" lf t55xx read 0 feedbeef - read data from block 0 password feedbeef");
63 PrintAndLog("");
64 return 0;
65 }
66 int usage_t55xx_write(){
67 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");
68 PrintAndLog(" <block>, block number to read. Between 0-7");
69 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");
70 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");
71 PrintAndLog("");
72 PrintAndLog("Examples:");
73 PrintAndLog(" lf t55xx wd 3 11223344 - write 11223344 to block 3");
74 PrintAndLog(" lf t55xx wd 3 11223344 feedbeef - write 11223344 to block 3 password feedbeef");
75 PrintAndLog("");
76 return 0;
77 }
78 int usage_t55xx_trace() {
79 PrintAndLog("Usage: lf t55xx trace [1]");
80 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
81 PrintAndLog("");
82 PrintAndLog("Examples:");
83 PrintAndLog(" lf t55xx trace");
84 PrintAndLog(" lf t55xx trace 1");
85 PrintAndLog("");
86 return 0;
87 }
88 int usage_t55xx_info() {
89 PrintAndLog("Usage: lf t55xx info [1]");
90 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
91 PrintAndLog("");
92 PrintAndLog("Examples:");
93 PrintAndLog(" lf t55xx info");
94 PrintAndLog(" lf t55xx info 1");
95 PrintAndLog("");
96 return 0;
97 }
98 int usage_t55xx_dump(){
99 PrintAndLog("Usage: lf t55xx dump <password>");
100 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex symbols)");
101 PrintAndLog("");
102 PrintAndLog("Examples:");
103 PrintAndLog(" lf t55xx dump");
104 PrintAndLog(" lf t55xx dump feedbeef");
105 PrintAndLog("");
106 return 0;
107 }
108
109 static int CmdHelp(const char *Cmd);
110
111 int CmdT55xxSetConfig(const char *Cmd){
112
113 int len;
114 bool inverse;
115 bool errors = FALSE;
116 uint8_t cmdp = 0;
117 char modulation[4] = {0x00};
118
119 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)
120 {
121 switch(param_getchar(Cmd, cmdp))
122 {
123 case 'h':
124 case 'H':
125 return usage_t55xx_config();
126 case 'd':
127 len = param_getstr(Cmd, cmdp+1, modulation);
128 cmdp+= len+1;
129 //FSK|ASK|PSK|NZ|BI
130 if ( strcmp(modulation, "FSK" ) == 0)
131 len = 1;
132 else if ( strcmp(modulation, "ASK" ) == 0)
133 len = 2;
134 else if ( strcmp(modulation, "PSK" ) == 0)
135 len = 3;
136 else if ( strcmp(modulation, "NZ" ) == 0)
137 len = 4;
138 else if ( strcmp(modulation, "BI" ) == 0)
139 len = 5;
140 else {
141 PrintAndLog("Unknown modulation '%s'", modulation);
142 errors = TRUE;
143 }
144 break;
145 case 'i':
146 inverse = param_getchar(Cmd,cmdp+1) == '1';
147 cmdp+=2;
148 break;
149 default:
150 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
151 errors = TRUE;
152 break;
153 }
154 }
155 // No args
156 if (cmdp == 0) {
157 PrintAndLog("Modulation: %d", config.modulation);
158 PrintAndLog("Invert : %d", config.inversed);
159 PrintAndLog("Block0 : %08X", config.block0);
160 return 0;
161 }
162 //Validations
163 if (errors)
164 return usage_t55xx_config();
165
166 config.modulation = len;
167 config.inversed = inverse;
168 config.block0 = 0;
169 return 0;
170 }
171 // detect configuration?
172
173 int CmdReadBlk(const char *Cmd)
174 {
175 int block = -1;
176 int password = 0xFFFFFFFF; //default to blank Block 7
177
178 char cmdp = param_getchar(Cmd, 0);
179 if (cmdp == 'h' || cmdp == 'H')
180 return usage_t55xx_read();
181
182 int res = sscanf(Cmd, "%d %x", &block, &password);
183
184 if ( res < 1 || res > 2 ){
185 usage_t55xx_read();
186 return 1;
187 }
188
189 if ((block < 0) | (block > 7)) {
190 PrintAndLog("Block must be between 0 and 7");
191 return 1;
192 }
193
194 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};
195 c.d.asBytes[0] = 0x0;
196
197 //Password mode
198 if ( res == 2 ) {
199 c.arg[2] = password;
200 c.d.asBytes[0] = 0x1;
201 }
202
203 SendCommand(&c);
204 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
205 PrintAndLog("command execution time out");
206 return 2;
207 }
208
209 uint8_t got[12000];
210 GetFromBigBuf(got,sizeof(got),0);
211 WaitForResponse(CMD_ACK,NULL);
212
213 setGraphBuf(got, 12000);
214
215 if (block == 0){
216 // try a detection.
217 }
218
219 if (CmdDetectClockRate("f")){ //wave is almost certainly FSK
220 // FSK
221 if ( FSKrawDemod("0 0", FALSE))
222 printT55xx("FSK");
223 // FSK inverted
224 if ( FSKrawDemod("0 1", FALSE))
225 printT55xx("FSK inv");
226 } else {
227 // ASK/MAN (autoclock, normal, maxerrors 1)
228 if ( ASKmanDemod("0 0 1", FALSE, FALSE) )
229 printT55xx("ASK/MAN");
230
231 // ASK/MAN (autoclock, inverted, maxerrors 1)
232 if ( ASKmanDemod("0 1 1", FALSE, FALSE) )
233 printT55xx("ASK/MAN Inv");
234
235 // NZR (autoclock, normal, maxerrors 1)
236 if ( NRZrawDemod("0 0 1", FALSE) )
237 printT55xx("NZR");
238 // NZR (autoclock, inverted, maxerrors 1)
239 if ( NRZrawDemod("0 1 1", FALSE) )
240 printT55xx("NZR inv");
241
242 // PSK (autoclock, normal, maxerrors 1)
243 if (!PSKDemod("0 0 1", FALSE))
244 printT55xx("PSK");
245
246 // PSK (autoclock, inverted, maxerrors 1)
247 if (!PSKDemod("0 1 1", FALSE))
248 printT55xx("PSK inv");
249
250 //PSK2?
251
252 // if (!BiphaseRawDecode("0",FALSE))
253 // printT55xx("BIPHASE");
254
255 // if (!BiphaseRawDecode("1",FALSE))
256 // printT55xx("BIPHASE inv");
257 }
258 return 0;
259 }
260
261 void printT55xx(const char *demodStr){
262
263 uint32_t blockData = 0;
264 uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
265
266 if ( !DemodBufferLen)
267 return;
268
269 int i =0;
270 for (;i<DemodBufferLen;++i)
271 bits[i]=DemodBuffer[i];
272
273 blockData = PackBits(1, 32, bits);
274 PrintAndLog("0x%08X %s [%s]", blockData, sprint_bin(bits+1,32), demodStr );
275 }
276
277 /*
278 FSK1 / FSK1a
279 size = fskdemod(dest, size, 32, 0, 8, 10); // fsk1 RF/32
280 size = fskdemod(dest, size, 32, 1, 8, 10); // fsk1a RF/32
281
282 FSK2 / FSK2a
283 size = fskdemod(dest, size, 32, 0, 10, 8); // fsk2 RF/32
284 size = fskdemod(dest, size, 32, 1, 10, 8); // fsk2a RF/32
285 size = fskdemod(dest, size, 50, 1, 10, 8); // fsk2a RF/50
286 size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64
287
288 PSK1
289 errCnt = pskRawDemod(bits, &bitlen, 32, 0);
290 */
291 int CmdWriteBlk(const char *Cmd)
292 {
293 int block = 8; //default to invalid block
294 int data = 0xFFFFFFFF; //default to blank Block
295 int password = 0xFFFFFFFF; //default to blank Block 7
296
297 char cmdp = param_getchar(Cmd, 0);
298 if (cmdp == 'h' || cmdp == 'H') {
299 usage_t55xx_write();
300 return 0;
301 }
302
303 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);
304
305 if ( res < 2 || res > 3) {
306 usage_t55xx_write();
307 return 1;
308 }
309
310 if (block > 7) {
311 PrintAndLog("Block must be between 0 and 7");
312 return 1;
313 }
314
315 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
316 c.d.asBytes[0] = 0x0;
317
318 PrintAndLog("Writing to T55x7");
319 PrintAndLog("block : %d", block);
320 PrintAndLog("data : 0x%08X", data);
321
322 //Password mode
323 if (res == 3) {
324 c.arg[2] = password;
325 c.d.asBytes[0] = 0x1;
326 PrintAndLog("pwd : 0x%08X", password);
327 }
328 SendCommand(&c);
329 return 0;
330 }
331
332 int CmdReadTrace(const char *Cmd)
333 {
334 uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
335
336 char cmdp = param_getchar(Cmd, 0);
337
338 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
339 usage_t55xx_trace();
340 return 0;
341 }
342
343 if ( strlen(Cmd)==0){
344
345 UsbCommand c = {CMD_T55XX_READ_TRACE, {0, 0, 0}};
346 SendCommand(&c);
347 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
348 PrintAndLog("command execution time out");
349 return 1;
350 }
351 //darn
352 //CmdSamples("12000");
353 }
354
355 size_t bitlen = getFromGraphBuf(bits);
356 if ( bitlen == 0 )
357 return 2;
358
359 RepaintGraphWindow();
360
361 uint8_t si = 5;
362 uint32_t bl0 = PackBits(si, 32, bits);
363 uint32_t bl1 = PackBits(si+32, 32, bits);
364
365 uint32_t acl = PackBits(si, 8, bits); si += 8;
366 uint32_t mfc = PackBits(si, 8, bits); si += 8;
367 uint32_t cid = PackBits(si, 5, bits); si += 5;
368 uint32_t icr = PackBits(si, 3, bits); si += 3;
369 uint32_t year = PackBits(si, 4, bits); si += 4;
370 uint32_t quarter = PackBits(si, 2, bits); si += 2;
371 uint32_t lotid = PackBits(si, 12, bits); si += 12;
372 uint32_t wafer = PackBits(si, 5, bits); si += 5;
373 uint32_t dw = PackBits(si, 15, bits);
374
375 PrintAndLog("");
376 PrintAndLog("-- T55xx Trace Information ----------------------------------");
377 PrintAndLog("-------------------------------------------------------------");
378 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);
379 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d)", mfc, mfc);
380 PrintAndLog(" CID : 0x%02X (%d)", cid, cid);
381 PrintAndLog(" ICR IC Revision : %d",icr );
382 PrintAndLog(" Manufactured");
383 PrintAndLog(" Year/Quarter : %d/%d",2000+year, quarter );
384 PrintAndLog(" Lot ID : %d", lotid );
385 PrintAndLog(" Wafer number : %d", wafer);
386 PrintAndLog(" Die Number : %d", dw);
387 PrintAndLog("-------------------------------------------------------------");
388 PrintAndLog(" Raw Data - Page 1");
389 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
390 PrintAndLog(" Block 0 : 0x%08X %s", bl1, sprint_bin(bits+37,32) );
391 PrintAndLog("-------------------------------------------------------------");
392 /*
393 TRACE - BLOCK O
394 Bits Definition HEX
395 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
396 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
397 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
398 22-24 ICR IC revision
399 25-28 YEAR (BCD encoded) 9 (= 2009)
400 29-30 QUARTER 1,2,3,4
401 31-32 LOT ID
402
403 TRACE - BLOCK 1
404 1-12 LOT ID
405 13-17 Wafer number
406 18-32 DW, die number sequential
407 */
408
409 return 0;
410 }
411
412 int CmdInfo(const char *Cmd){
413 /*
414 Page 0 Block 0 Configuration data.
415 Normal mode
416 Extended mode
417 */
418 char cmdp = param_getchar(Cmd, 0);
419
420 if (cmdp == 'h' || cmdp == 'H') {
421 return usage_t55xx_info();
422 } else {
423 CmdReadBlk("0");
424 }
425
426 // config
427
428 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
429
430 uint8_t si = 1;
431 uint32_t bl0 = PackBits(si, 32, bits);
432
433 uint32_t safer = PackBits(si, 4, bits); si += 4;
434 uint32_t resv = PackBits(si, 7, bits); si += 7;
435 uint32_t dbr = PackBits(si, 3, bits); si += 3;
436 uint32_t extend = PackBits(si, 1, bits); si += 1;
437 uint32_t datamodulation = PackBits(si, 5, bits); si += 5;
438 uint32_t pskcf = PackBits(si, 2, bits); si += 2;
439 uint32_t aor = PackBits(si, 1, bits); si += 1;
440 uint32_t otp = PackBits(si, 1, bits); si += 1;
441 uint32_t maxblk = PackBits(si, 3, bits); si += 3;
442 uint32_t pwd = PackBits(si, 1, bits); si += 1;
443 uint32_t sst = PackBits(si, 1, bits); si += 1;
444 uint32_t fw = PackBits(si, 1, bits); si += 1;
445 uint32_t inv = PackBits(si, 1, bits); si += 1;
446 uint32_t por = PackBits(si, 1, bits); si += 1;
447
448 PrintAndLog("");
449 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");
450 PrintAndLog("-------------------------------------------------------------");
451 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
452 PrintAndLog(" reserved : %d", resv);
453 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));
454 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
455 PrintAndLog(" Modulation : %s", GetModulationStr(datamodulation) );
456 PrintAndLog(" PSK clock freq : %d", pskcf);
457 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
458 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
459 PrintAndLog(" Max block : %d", maxblk);
460 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
461 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
462 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
463 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
464 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
465 PrintAndLog("-------------------------------------------------------------");
466 PrintAndLog(" Raw Data - Page 0");
467 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
468 PrintAndLog("-------------------------------------------------------------");
469
470 return 0;
471 }
472
473 int CmdDump(const char *Cmd){
474
475 char s[20] = {0x00};
476 uint8_t pwd[4] = {0x00};
477
478 char cmdp = param_getchar(Cmd, 0);
479 if ( cmdp == 'h' || cmdp == 'H') {
480 usage_t55xx_dump();
481 return 0;
482 }
483
484 bool hasPwd = ( strlen(Cmd) > 0);
485 if ( hasPwd ){
486 if (param_gethex(Cmd, 0, pwd, 8)) {
487 PrintAndLog("password must include 8 HEX symbols");
488 return 1;
489 }
490 }
491
492 for ( int i = 0; i <8; ++i){
493 memset(s,0,sizeof(s));
494 if ( hasPwd ) {
495 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);
496 } else {
497 sprintf(s,"%d", i);
498 }
499 CmdReadBlk(s);
500 }
501 return 0;
502 }
503
504 char * GetBitRateStr(uint32_t id){
505 static char buf[40];
506 char *retStr = buf;
507 switch (id){
508 case 0:
509 sprintf(retStr,"%d - RF/8",id);
510 break;
511 case 1:
512 sprintf(retStr,"%d - RF/16",id);
513 break;
514 case 2:
515 sprintf(retStr,"%d - RF/32",id);
516 break;
517 case 3:
518 sprintf(retStr,"%d - RF/40",id);
519 break;
520 case 4:
521 sprintf(retStr,"%d - RF/50",id);
522 break;
523 case 5:
524 sprintf(retStr,"%d - RF/64",id);
525 break;
526 case 6:
527 sprintf(retStr,"%d - RF/100",id);
528 break;
529 case 7:
530 sprintf(retStr,"%d - RF/128",id);
531 break;
532 default:
533 sprintf(retStr,"%d - (Unknown)",id);
534 break;
535 }
536
537 return buf;
538 }
539
540 char * GetSaferStr(uint32_t id){
541 static char buf[40];
542 char *retStr = buf;
543
544 sprintf(retStr,"%d",id);
545 if (id == 6) {
546 sprintf(retStr,"%d - pasdwd",id);
547 }
548 if (id == 9 ){
549 sprintf(retStr,"%d - testmode ",id);
550 }
551
552 return buf;
553 }
554 char * GetModulationStr( uint32_t id){
555 static char buf[40];
556 char *retStr = buf;
557
558 switch (id){
559 case 0:
560 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);
561 break;
562 case 1:
563 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);
564 break;
565 case 2:
566 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);
567 break;
568 case 3:
569 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);
570 break;
571 case 4:
572 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);
573 break;
574 case 5:
575 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);
576 break;
577 case 6:
578 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);
579 break;
580 case 7:
581 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);
582 break;
583 case 8:
584 sprintf(retStr,"%d - Manschester",id);
585 break;
586 case 16:
587 sprintf(retStr,"%d - Biphase",id);
588 break;
589 case 17:
590 sprintf(retStr,"%d - Reserved",id);
591 break;
592 default:
593 sprintf(retStr,"0x%02X (Unknown)",id);
594 break;
595 }
596 return buf;
597 }
598
599 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
600
601 int i = start;
602 int j = len-1;
603 if (len > 32) {
604 return 0;
605 }
606 uint32_t tmp = 0;
607 for (; j >= 0; --j, ++i){
608 tmp |= bits[i] << j;
609 }
610 return tmp;
611 }
612
613 static command_t CommandTable[] =
614 {
615 {"help", CmdHelp, 1, "This help"},
616 {"config", CmdT55xxSetConfig, 1, "Set T55XX config for modulation, inversed data"},
617 {"read", CmdReadBlk, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
618 {"write", CmdWriteBlk, 0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
619 {"trace", CmdReadTrace, 0, "[1] Read T55xx traceability data (page 1/ blk 0-1)"},
620 {"info", CmdInfo, 0, "[1] Read T55xx configuration data (page 0/ blk 0)"},
621 {"dump", CmdDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
622 {NULL, NULL, 0, NULL}
623 };
624
625 int CmdLFT55XX(const char *Cmd)
626 {
627 CmdsParse(CommandTable, Cmd);
628 return 0;
629 }
630
631 int CmdHelp(const char *Cmd)
632 {
633 CmdsHelp(CommandTable);
634 return 0;
635 }
Impressum, Datenschutz