]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
ADD: Holimans new changes in master.
[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
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 static int CmdHelp(const char *Cmd);
28
29 int CmdReadBlk(const char *Cmd)
30 {
31 int block = -1;
32 sscanf(Cmd, "%d", &block);
33
34 if ((block > 7) | (block < 0)) {
35 PrintAndLog("Block must be between 0 and 7");
36 return 1;
37 }
38
39 UsbCommand c;
40 c.cmd = CMD_T55XX_READ_BLOCK;
41 c.d.asBytes[0] = 0x00;
42 c.arg[0] = 0;
43 c.arg[1] = block;
44 c.arg[2] = 0;
45 SendCommand(&c);
46 WaitForResponse(CMD_ACK, NULL);
47
48 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
49
50 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,0); //3560 -- should be offset..
51 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
52
53 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
54 GraphBuffer[j] = (int)data[j];
55 }
56 GraphTraceLen = LF_TRACE_BUFF_SIZE;
57 ManchesterDemod(block);
58 RepaintGraphWindow();
59 return 0;
60 }
61
62 int CmdReadBlkPWD(const char *Cmd)
63 {
64 int Block = -1; //default to invalid block
65 int Password = 0xFFFFFFFF; //default to blank Block 7
66 UsbCommand c;
67
68 sscanf(Cmd, "%d %x", &Block, &Password);
69
70 if ((Block > 7) | (Block < 0)) {
71 PrintAndLog("Block must be between 0 and 7");
72 return 1;
73 }
74
75 PrintAndLog("Reading page 0 block %d pwd %08X", Block, Password);
76
77 c.cmd = CMD_T55XX_READ_BLOCK;
78 c.d.asBytes[0] = 0x1; //Password mode
79 c.arg[0] = 0;
80 c.arg[1] = Block;
81 c.arg[2] = Password;
82 SendCommand(&c);
83 WaitForResponse(CMD_ACK, NULL);
84
85 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
86
87 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,0); //3560 -- should be offset..
88 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
89
90 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
91 GraphBuffer[j] = ((int)data[j]);
92 }
93 GraphTraceLen = LF_TRACE_BUFF_SIZE;
94 ManchesterDemod(Block);
95 RepaintGraphWindow();
96 return 0;
97 }
98
99 int CmdWriteBlk(const char *Cmd)
100 {
101 int Block = 8; //default to invalid block
102 int Data = 0xFFFFFFFF; //default to blank Block
103 UsbCommand c;
104
105 sscanf(Cmd, "%x %d", &Data, &Block);
106
107 if (Block > 7) {
108 PrintAndLog("Block must be between 0 and 7");
109 return 1;
110 }
111
112 PrintAndLog("Writting block %d with data %08X", Block, Data);
113
114 c.cmd = CMD_T55XX_WRITE_BLOCK;
115 c.d.asBytes[0] = 0x0; //Normal mode
116 c.arg[0] = Data;
117 c.arg[1] = Block;
118 c.arg[2] = 0;
119 SendCommand(&c);
120 return 0;
121 }
122
123 int CmdWriteBlkPWD(const char *Cmd)
124 {
125 int Block = 8; //default to invalid block
126 int Data = 0xFFFFFFFF; //default to blank Block
127 int Password = 0xFFFFFFFF; //default to blank Block 7
128 UsbCommand c;
129
130 sscanf(Cmd, "%x %d %x", &Data, &Block, &Password);
131
132 if (Block > 7) {
133 PrintAndLog("Block must be between 0 and 7");
134 return 1;
135 }
136
137 PrintAndLog("Writting block %d with data %08X and password %08X", Block, Data, Password);
138
139 c.cmd = CMD_T55XX_WRITE_BLOCK;
140 c.d.asBytes[0] = 0x1; //Password mode
141 c.arg[0] = Data;
142 c.arg[1] = Block;
143 c.arg[2] = Password;
144 SendCommand(&c);
145 return 0;
146 }
147
148 int CmdReadTrace(const char *Cmd)
149 {
150 char cmdp = param_getchar(Cmd, 0);
151
152 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
153 PrintAndLog("Usage: lf t55xx trace [use data from Graphbuffer]");
154 PrintAndLog(" [use data from Graphbuffer], if not set, try reading data from tag.");
155 PrintAndLog("");
156 PrintAndLog(" sample: lf t55xx trace");
157 PrintAndLog(" sample: lf t55xx trace 1");
158 return 0;
159 }
160
161 if ( strlen(Cmd)==0){
162 UsbCommand c = {CMD_T55XX_READ_TRACE, {0, 0, 0}};
163 SendCommand(&c);
164 WaitForResponse(CMD_ACK, NULL);
165
166 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
167
168 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,0); //3560 -- should be offset..
169 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
170
171 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
172 GraphBuffer[j] = ((int)data[j]);
173 }
174 GraphTraceLen = LF_TRACE_BUFF_SIZE;
175 }
176
177 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
178 uint8_t * bitstream = bits;
179
180 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream, LF_BITSSTREAM_LEN);
181 RepaintGraphWindow();
182
183 uint8_t si = 5;
184 uint32_t bl0 = PackBits(si, 32, bitstream);
185 uint32_t bl1 = PackBits(si+32, 32, bitstream);
186
187 uint32_t acl = PackBits(si, 8, bitstream); si += 8;
188 uint32_t mfc = PackBits(si, 8, bitstream); si += 8;
189 uint32_t cid = PackBits(si, 5, bitstream); si += 5;
190 uint32_t icr = PackBits(si, 3, bitstream); si += 3;
191 uint32_t year = PackBits(si, 4, bitstream); si += 4;
192 uint32_t quarter = PackBits(si, 2, bitstream); si += 2;
193 uint32_t lotid = PackBits(si, 12, bitstream); si += 12;
194 uint32_t wafer = PackBits(si, 5, bitstream); si += 5;
195 uint32_t dw = PackBits(si, 15, bitstream);
196
197 PrintAndLog("");
198 PrintAndLog("-- T55xx Trace Information ----------------------------------");
199 PrintAndLog("-------------------------------------------------------------");
200 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);
201 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d)", mfc, mfc);
202 PrintAndLog(" CID : 0x%02X (%d)", cid, cid);
203 PrintAndLog(" ICR IC Revision : %d",icr );
204 PrintAndLog(" Manufactured");
205 PrintAndLog(" Year/Quarter : %d/%d",2000+year, quarter );
206 PrintAndLog(" Lot ID : %d", lotid );
207 PrintAndLog(" Wafer number : %d", wafer);
208 PrintAndLog(" Die Number : %d", dw);
209 PrintAndLog("-------------------------------------------------------------");
210 PrintAndLog(" Raw Data - Page 1");
211 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bitstream+5,32) );
212 PrintAndLog(" Block 0 : 0x%08X %s", bl1, sprint_bin(bitstream+37,32) );
213 PrintAndLog("-------------------------------------------------------------");
214 /*
215 TRACE - BLOCK O
216 Bits Definition HEX
217 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
218 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
219 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
220 22-24 ICR IC revision
221 25-28 YEAR (BCD encoded) 9 (= 2009)
222 29-30 QUARTER 1,2,3,4
223 31-32 LOT ID
224
225 TRACE - BLOCK 1
226 1-12 LOT ID
227 13-17 Wafer number
228 18-32 DW, die number sequential
229 */
230
231 return 0;
232 }
233
234 int CmdInfo(const char *Cmd){
235 /*
236 Page 0 Block 0 Configuration data.
237 Normal mode
238 Extended mode
239 */
240 char cmdp = param_getchar(Cmd, 0);
241
242 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
243 PrintAndLog("Usage: lf t55xx info [use data from Graphbuffer]");
244 PrintAndLog(" [use data from Graphbuffer], if not set, try reading data from tag.");
245 PrintAndLog("");
246 PrintAndLog(" sample: lf t55xx info");
247 PrintAndLog(" sample: lf t55xx info 1");
248 return 0;
249 }
250
251 if ( strlen(Cmd) == 0 ){
252 CmdReadBlk("0");
253 }
254
255 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
256
257 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bits, LF_BITSSTREAM_LEN);
258
259 uint8_t si = 5;
260 uint32_t bl0 = PackBits(si, 32, bits);
261
262 uint32_t safer = PackBits(si, 4, bits); si += 4;
263 uint32_t resv = PackBits(si, 7, bits); si += 7;
264 uint32_t dbr = PackBits(si, 3, bits); si += 3;
265 uint32_t extend = PackBits(si, 1, bits); si += 1;
266 uint32_t datamodulation = PackBits(si, 5, bits); si += 5;
267 uint32_t pskcf = PackBits(si, 2, bits); si += 2;
268 uint32_t aor = PackBits(si, 1, bits); si += 1;
269 uint32_t otp = PackBits(si, 1, bits); si += 1;
270 uint32_t maxblk = PackBits(si, 3, bits); si += 3;
271 uint32_t pwd = PackBits(si, 1, bits); si += 1;
272 uint32_t sst = PackBits(si, 1, bits); si += 1;
273 uint32_t fw = PackBits(si, 1, bits); si += 1;
274 uint32_t inv = PackBits(si, 1, bits); si += 1;
275 uint32_t por = PackBits(si, 1, bits); si += 1;
276
277 PrintAndLog("");
278 PrintAndLog("-- T55xx Configuration --------------------------------------");
279 PrintAndLog("-------------------------------------------------------------");
280 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
281 PrintAndLog(" reserved : %d", resv);
282 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));
283 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
284 PrintAndLog(" Modulation : %s", GetModulationStr(datamodulation) );
285 PrintAndLog(" PSK clock freq : %d", pskcf);
286 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
287 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
288 PrintAndLog(" Max block : %d", maxblk);
289 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
290 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
291 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
292 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
293 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
294 PrintAndLog("-------------------------------------------------------------");
295 PrintAndLog(" Raw Data - Page 0");
296 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
297 PrintAndLog("-------------------------------------------------------------");
298
299 return 0;
300 }
301
302 int CmdDump(const char *Cmd){
303
304 char cmdp = param_getchar(Cmd, 0);
305 char s[20];
306 uint8_t pwd[4] = {0x00};
307 bool hasPwd = ( strlen(Cmd) > 0);
308
309 if ( cmdp == 'h' || cmdp == 'H') {
310 PrintAndLog("Usage: lf t55xx dump <password>");
311 PrintAndLog(" sample: lf t55xx dump FFFFFFFF");
312 return 0;
313 }
314
315 if ( hasPwd ){
316 if (param_gethex(Cmd, 0, pwd, 8)) {
317 PrintAndLog("password must include 8 HEX symbols");
318 return 0;
319 }
320 }
321
322 for ( int i = 0; i <8; ++i){
323 memset(s,0,sizeof(s));
324 if ( hasPwd ) {
325 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);
326 CmdReadBlkPWD(s);
327 } else {
328 sprintf(s,"%d", i);
329 CmdReadBlk(s);
330 }
331 }
332 return 0;
333 }
334
335 int CmdIceFsk(const char *Cmd){
336
337 if (!HasGraphData()) return 0;
338
339 iceFsk3(GraphBuffer, LF_TRACE_BUFF_SIZE);
340 RepaintGraphWindow();
341 return 0;
342 }
343 int CmdIceManchester(const char *Cmd){
344 ManchesterDemod( -1);
345 return 0;
346 }
347 int ManchesterDemod(int blockNum){
348
349 if (!HasGraphData()) return 0;
350
351 uint8_t sizebyte = 32;
352 // the value 5 was selected during empirical studies of the decoded data. Some signal noise to skip.
353 uint8_t offset = 5;
354 uint32_t blockData;
355 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
356 uint8_t * bitstream = bits;
357
358 //manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream, LF_BITSSTREAM_LEN);
359 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bits, LF_BITSSTREAM_LEN);
360 //blockData = PackBits(offset, sizebyte, bitstream);
361 blockData = PackBits(offset, sizebyte, bits);
362
363 if ( blockNum < 0)
364 PrintAndLog(" Decoded : 0x%08X %s", blockData, sprint_bin(bitstream+offset,sizebyte) );
365 else
366 PrintAndLog(" Block %d : 0x%08X %s", blockNum, blockData, sprint_bin(bitstream+offset,sizebyte) );
367
368 return 0;
369 }
370
371 char * GetBitRateStr(uint32_t id){
372 static char buf[40];
373 char *retStr = buf;
374 switch (id){
375 case 0:
376 sprintf(retStr,"%d - RF/8",id);
377 break;
378 case 1:
379 sprintf(retStr,"%d - RF/16",id);
380 break;
381 case 2:
382 sprintf(retStr,"%d - RF/32",id);
383 break;
384 case 3:
385 sprintf(retStr,"%d - RF/40",id);
386 break;
387 case 4:
388 sprintf(retStr,"%d - RF/50",id);
389 break;
390 case 5:
391 sprintf(retStr,"%d - RF/64",id);
392 break;
393 case 6:
394 sprintf(retStr,"%d - RF/100",id);
395 break;
396 case 7:
397 sprintf(retStr,"%d - RF/128",id);
398 break;
399 default:
400 sprintf(retStr,"%d - (Unknown)",id);
401 break;
402 }
403
404 return buf;
405 }
406
407 char * GetSaferStr(uint32_t id){
408 static char buf[40];
409 char *retStr = buf;
410
411 sprintf(retStr,"%d",id);
412 if (id == 6) {
413 sprintf(retStr,"%d - pasdwd",id);
414 }
415 if (id == 9 ){
416 sprintf(retStr,"%d - testmode ",id);
417 }
418
419 return buf;
420 }
421 char * GetModulationStr( uint32_t id){
422 static char buf[40];
423 char *retStr = buf;
424
425 switch (id){
426 case 0:
427 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);
428 break;
429 case 1:
430 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);
431 break;
432 case 2:
433 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);
434 break;
435 case 3:
436 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);
437 break;
438 case 4:
439 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);
440 break;
441 case 5:
442 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);
443 break;
444 case 6:
445 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);
446 break;
447 case 7:
448 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);
449 break;
450 case 8:
451 sprintf(retStr,"%d - Manschester",id);
452 break;
453 case 16:
454 sprintf(retStr,"%d - Biphase",id);
455 break;
456 case 17:
457 sprintf(retStr,"%d - Reserved",id);
458 break;
459 default:
460 sprintf(retStr,"0x%02X (Unknown)",id);
461 break;
462 }
463 return buf;
464 }
465
466
467 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
468
469 int i = start;
470 int j = len-1;
471 if (len > 32) {
472 return 0;
473 }
474 uint32_t tmp = 0;
475 for (; j >= 0; --j, ++i){
476 tmp |= bits[i] << j;
477 }
478 return tmp;
479 }
480
481 static command_t CommandTable[] =
482 {
483 {"help", CmdHelp, 1, "This help"},
484 {"rd", CmdReadBlk, 0, "<block> -- Read T55xx block data (page 0)"},
485 {"rdpwd", CmdReadBlkPWD, 0, "<block> <password> -- Read T55xx block data with password mode"},
486 {"wr", CmdWriteBlk, 0, "<data> <block> -- Write T55xx block data (page 0)"},
487 {"wrpwd", CmdWriteBlkPWD, 0, "<data> <block> <password> -- Write T55xx block data with password"},
488 {"trace", CmdReadTrace, 0, "[1] Read T55xx traceability data (page 1/ blk 0-1)"},
489 {"info", CmdInfo, 0, "[1] Read T55xx configuration data (page 0/ blk 0)"},
490 {"dump", CmdDump, 0, "[password] Dump T55xx card block 0-7. optional with password"},
491 {"fsk", CmdIceFsk, 0, "FSK demod"},
492 {"man", CmdIceManchester, 0, "Manchester demod (with SST)"},
493 {NULL, NULL, 0, NULL}
494 };
495
496 int CmdLFT55XX(const char *Cmd)
497 {
498 CmdsParse(CommandTable, Cmd);
499 return 0;
500 }
501
502 int CmdHelp(const char *Cmd)
503 {
504 CmdsHelp(CommandTable);
505 return 0;
506 }
Impressum, Datenschutz