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