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