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