]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdlft55xx.c
Fix some printf/scanf format strings
[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 "cmdlft55xx.h"\r
11\r
12#include <stdio.h>\r
13#include <string.h>\r
14#include <inttypes.h>\r
15#include <ctype.h>\r
16#include <time.h>\r
17#include "comms.h"\r
18#include "ui.h"\r
19#include "graph.h"\r
20#include "cmdmain.h"\r
21#include "cmdparser.h"\r
22#include "cmddata.h"\r
23#include "cmdlf.h"\r
24#include "util.h"\r
25#include "lfdemod.h"\r
26#include "protocols.h"\r
27#include "taginfo.h"\r
28\r
29#define T55x7_CONFIGURATION_BLOCK 0x00\r
30#define T55x7_PAGE0 0x00\r
31#define T55x7_PAGE1 0x01\r
32//#define T55x7_PWD 0x00000010\r
33#define REGULAR_READ_MODE_BLOCK 0xFF\r
34\r
35// Default configuration\r
36t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = false, .offset = 0x00, .block0 = 0x00, .Q5 = false };\r
37\r
38t55xx_conf_block_t Get_t55xx_Config(){\r
39 return config;\r
40}\r
41void Set_t55xx_Config(t55xx_conf_block_t conf){\r
42 config = conf;\r
43}\r
44\r
45int usage_t55xx_config(){\r
46 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>] [Q5]");\r
47 PrintAndLog("Options:");\r
48 PrintAndLog(" h This help");\r
49 PrintAndLog(" b <8|16|32|40|50|64|100|128> Set bitrate");\r
50 PrintAndLog(" d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa> Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A");\r
51 PrintAndLog(" i [1] Invert data signal, defaults to normal");\r
52 PrintAndLog(" o [offset] Set offset, where data should start decode in bitstream");\r
53 PrintAndLog(" Q5 Set as Q5(T5555) chip instead of T55x7");\r
54 PrintAndLog(" ST Set Sequence Terminator on");\r
55 PrintAndLog("");\r
56 PrintAndLog("Examples:");\r
57 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");\r
58 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");\r
59 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");\r
60 PrintAndLog("");\r
61 return 0;\r
62}\r
63int usage_t55xx_read(){\r
64 PrintAndLog("Usage: lf t55xx read [b <block>] [p <password>] <override_safety> <page1>");\r
65 PrintAndLog("Options:");\r
66 PrintAndLog(" b <block> - block number to read. Between 0-7");\r
67 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");\r
68 PrintAndLog(" o - OPTIONAL override safety check");\r
69 PrintAndLog(" 1 - OPTIONAL read Page 1 instead of Page 0");\r
70 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");\r
71 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference"); \r
72 PrintAndLog(" ****WARNING****");\r
73 PrintAndLog(" Use of read with password on a tag not configured for a pwd");\r
74 PrintAndLog(" can damage the tag");\r
75 PrintAndLog("");\r
76 PrintAndLog("Examples:");\r
77 PrintAndLog(" lf t55xx read b 0 - read data from block 0");\r
78 PrintAndLog(" lf t55xx read b 0 p feedbeef - read data from block 0 password feedbeef");\r
79 PrintAndLog(" lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check");\r
80 PrintAndLog("");\r
81 return 0;\r
82}\r
83int usage_t55xx_write(){\r
84 PrintAndLog("Usage: lf t55xx write [b <block>] [d <data>] [p <password>] [1] [t]");\r
85 PrintAndLog("Options:");\r
86 PrintAndLog(" b <block> - block number to write. Between 0-7");\r
87 PrintAndLog(" d <data> - 4 bytes of data to write (8 hex characters)");\r
88 PrintAndLog(" p <password> - OPTIONAL password 4bytes (8 hex characters)");\r
89 PrintAndLog(" 1 - OPTIONAL write Page 1 instead of Page 0");\r
90 PrintAndLog(" t - OPTIONAL test mode write - ****DANGER****"); \r
91 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");\r
92 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference"); \r
93 PrintAndLog("");\r
94 PrintAndLog("Examples:");\r
95 PrintAndLog(" lf t55xx write b 3 d 11223344 - write 11223344 to block 3");\r
96 PrintAndLog(" lf t55xx write b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef");\r
97 PrintAndLog("");\r
98 return 0;\r
99}\r
100int usage_t55xx_trace() {\r
101 PrintAndLog("Usage: lf t55xx trace [1]");\r
102 PrintAndLog("Options:");\r
103 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");\r
104 PrintAndLog("");\r
105 PrintAndLog("Examples:");\r
106 PrintAndLog(" lf t55xx trace");\r
107 PrintAndLog(" lf t55xx trace 1");\r
108 PrintAndLog("");\r
109 return 0;\r
110}\r
111int usage_t55xx_info() {\r
112 PrintAndLog("Usage: lf t55xx info [1]");\r
113 PrintAndLog("Options:");\r
114 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");\r
115 PrintAndLog("");\r
116 PrintAndLog("Examples:");\r
117 PrintAndLog(" lf t55xx info");\r
118 PrintAndLog(" lf t55xx info 1");\r
119 PrintAndLog("");\r
120 return 0;\r
121}\r
122int usage_t55xx_dump(){\r
123 PrintAndLog("Usage: lf t55xx dump <password> [o]");\r
124 PrintAndLog("Options:");\r
125 PrintAndLog(" <password> - OPTIONAL password 4bytes (8 hex symbols)");\r
126 PrintAndLog(" o - OPTIONAL override, force pwd read despite danger to card");\r
127 PrintAndLog("");\r
128 PrintAndLog("Examples:");\r
129 PrintAndLog(" lf t55xx dump");\r
130 PrintAndLog(" lf t55xx dump feedbeef o");\r
131 PrintAndLog("");\r
132 return 0;\r
133}\r
134int usage_t55xx_detect(){\r
135 PrintAndLog("Usage: lf t55xx detect [1] [p <password>]");\r
136 PrintAndLog("Options:");\r
137 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");\r
138 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");\r
139 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default), '1' long leading reference");\r
140 PrintAndLog(" '2' leading zero, '3' 1 of 4 coding reference"); \r
141 PrintAndLog("");\r
142 PrintAndLog("Examples:");\r
143 PrintAndLog(" lf t55xx detect");\r
144 PrintAndLog(" lf t55xx detect 1");\r
145 PrintAndLog(" lf t55xx detect p 11223344");\r
146 PrintAndLog("");\r
147 return 0;\r
148}\r
149int usage_t55xx_detectP1(){\r
150 PrintAndLog("Command: Detect Page 1 of a t55xx chip");\r
151 PrintAndLog("Usage: lf t55xx p1detect [1] [p <password>]");\r
152 PrintAndLog("Options:");\r
153 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");\r
154 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");\r
155 PrintAndLog("");\r
156 PrintAndLog("Examples:");\r
157 PrintAndLog(" lf t55xx p1detect");\r
158 PrintAndLog(" lf t55xx p1detect 1");\r
159 PrintAndLog(" lf t55xx p1detect p 11223344");\r
160 PrintAndLog("");\r
161 return 0;\r
162}\r
163int usage_t55xx_wakup(){\r
164 PrintAndLog("Usage: lf t55xx wakeup [h] <password>");\r
165 PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards.");\r
166 PrintAndLog("Options:");\r
167 PrintAndLog(" h - this help");\r
168 PrintAndLog(" <password> - [required] password 4bytes (8 hex symbols)");\r
169 PrintAndLog("");\r
170 PrintAndLog("Examples:");\r
171 PrintAndLog(" lf t55xx wakeup 11223344 - send wakeup password");\r
172 return 0;\r
173}\r
174int usage_t55xx_bruteforce(){\r
175 PrintAndLog("This command uses A) bruteforce to scan a number range");\r
176 PrintAndLog(" B) a dictionary attack");\r
177 PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");\r
178 PrintAndLog(" password must be 4 bytes (8 hex symbols)");\r
179 PrintAndLog("Options:");\r
180 PrintAndLog(" h - this help");\r
181 PrintAndLog(" r <mode> - OPTIONAL downlink encoding '0' fixed bit length (default)"); \r
182 PrintAndLog(" '1' long leading reference, '2' leading zero ");\r
183 PrintAndLog(" '3' 1 of 4 coding reference, '4' special - try all downlink modes");\r
184 PrintAndLog(" <start_pwd> - 4 byte hex value to start pwd search at");\r
185 PrintAndLog(" <end_pwd> - 4 byte hex value to end pwd search at");\r
186 PrintAndLog(" i <*.dic> - loads a default keys dictionary file <*.dic>");\r
187 PrintAndLog("");\r
188 PrintAndLog("Examples:");\r
189 PrintAndLog(" lf t55xx bruteforce [r 2] aaaaaaaa bbbbbbbb");\r
190 PrintAndLog(" lf t55xx bruteforce [r 2] i default_pwd.dic");\r
191 PrintAndLog("");\r
192 return 0;\r
193}\r
194int usage_t55xx_wipe(){\r
195 PrintAndLog("Usage: lf t55xx wipe [h] [Q5]");\r
196 PrintAndLog("This commands wipes a tag, fills blocks 1-7 with zeros and a default configuration block");\r
197 PrintAndLog("Options:");\r
198 PrintAndLog(" h - this help");\r
199 PrintAndLog(" Q5 - indicates to use the T5555 (Q5) default configuration block");\r
200 PrintAndLog("");\r
201 PrintAndLog("Examples:");\r
202 PrintAndLog(" lf t55xx wipe - wipes a t55x7 tag, config block 0x000880E0");\r
203 PrintAndLog(" lf t55xx wipe Q5 - wipes a t5555 Q5 tag, config block 0x6001F004");\r
204 return 0;\r
205}\r
206\r
207\r
208static int CmdHelp(const char *Cmd);\r
209\r
210void printT5xxHeader(uint8_t page){\r
211 PrintAndLog("Reading Page %d:", page);\r
212 PrintAndLog("blk | hex data | binary");\r
213 PrintAndLog("----+----------+---------------------------------"); \r
214}\r
215\r
216int CmdT55xxSetConfig(const char *Cmd) {\r
217\r
218 uint8_t offset = 0;\r
219 char modulation[6] = {0x00};\r
220 char tmp = 0x00;\r
221 uint8_t bitRate = 0;\r
222 uint8_t rates[9] = {8,16,32,40,50,64,100,128,0};\r
223 uint8_t cmdp = 0;\r
224 bool errors = false;\r
225 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)\r
226 {\r
227 tmp = param_getchar(Cmd, cmdp);\r
228 switch(tmp)\r
229 {\r
230 case 'h':\r
231 case 'H':\r
232 return usage_t55xx_config();\r
233 case 'b':\r
234 errors |= param_getdec(Cmd, cmdp+1, &bitRate);\r
235 if ( !errors){\r
236 uint8_t i = 0;\r
237 for (; i < 9; i++){\r
238 if (rates[i]==bitRate) {\r
239 config.bitrate = i;\r
240 break;\r
241 }\r
242 }\r
243 if (i==9) errors = true;\r
244 }\r
245 cmdp+=2;\r
246 break;\r
247 case 'd':\r
248 param_getstr(Cmd, cmdp+1, modulation, sizeof(modulation));\r
249 cmdp += 2;\r
250\r
251 if ( strcmp(modulation, "FSK" ) == 0) {\r
252 config.modulation = DEMOD_FSK;\r
253 } else if ( strcmp(modulation, "FSK1" ) == 0) {\r
254 config.modulation = DEMOD_FSK1;\r
255 config.inverted=1;\r
256 } else if ( strcmp(modulation, "FSK1a" ) == 0) {\r
257 config.modulation = DEMOD_FSK1a;\r
258 config.inverted=0;\r
259 } else if ( strcmp(modulation, "FSK2" ) == 0) {\r
260 config.modulation = DEMOD_FSK2;\r
261 config.inverted=0;\r
262 } else if ( strcmp(modulation, "FSK2a" ) == 0) {\r
263 config.modulation = DEMOD_FSK2a;\r
264 config.inverted=1;\r
265 } else if ( strcmp(modulation, "ASK" ) == 0) {\r
266 config.modulation = DEMOD_ASK;\r
267 } else if ( strcmp(modulation, "NRZ" ) == 0) {\r
268 config.modulation = DEMOD_NRZ;\r
269 } else if ( strcmp(modulation, "PSK1" ) == 0) {\r
270 config.modulation = DEMOD_PSK1;\r
271 } else if ( strcmp(modulation, "PSK2" ) == 0) {\r
272 config.modulation = DEMOD_PSK2;\r
273 } else if ( strcmp(modulation, "PSK3" ) == 0) {\r
274 config.modulation = DEMOD_PSK3;\r
275 } else if ( strcmp(modulation, "BIa" ) == 0) {\r
276 config.modulation = DEMOD_BIa;\r
277 config.inverted=1;\r
278 } else if ( strcmp(modulation, "BI" ) == 0) {\r
279 config.modulation = DEMOD_BI;\r
280 config.inverted=0;\r
281 } else {\r
282 PrintAndLog("Unknown modulation '%s'", modulation);\r
283 errors = true;\r
284 }\r
285 break;\r
286 case 'i':\r
287 config.inverted = param_getchar(Cmd,cmdp+1) == '1';\r
288 cmdp+=2;\r
289 break;\r
290 case 'o':\r
291 errors |= param_getdec(Cmd, cmdp+1, &offset);\r
292 if ( !errors )\r
293 config.offset = offset;\r
294 cmdp+=2;\r
295 break;\r
296 case 'Q':\r
297 case 'q': \r
298 config.Q5 = true;\r
299 cmdp++;\r
300 break;\r
301 case 'S':\r
302 case 's': \r
303 config.ST = true;\r
304 cmdp++;\r
305 break;\r
306 default:\r
307 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
308 errors = true;\r
309 break;\r
310 }\r
311 }\r
312\r
313 // No args\r
314 if (cmdp == 0) return printConfiguration( config);\r
315\r
316 //Validations\r
317 if (errors) return usage_t55xx_config();\r
318\r
319 config.block0 = 0;\r
320 return printConfiguration ( config);\r
321}\r
322\r
323int T55xxReadBlock(uint8_t block, bool page1, bool usepwd, bool override, uint32_t password, uint8_t downlink_mode){\r
324 //Password mode\r
325 if ( usepwd ) {\r
326 // try reading the config block and verify that PWD bit is set before doing this!\r
327 if ( !override ) {\r
328 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0,downlink_mode ) ) return 0;\r
329 if ( !tryDetectModulation() ) {\r
330 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits.");\r
331 return 0;\r
332 } else {\r
333 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password..."); \r
334 usepwd = false;\r
335 page1 = false;\r
336 }\r
337 } else {\r
338 PrintAndLog("Safety Check Overriden - proceeding despite risk");\r
339 }\r
340 }\r
341\r
342 if (!AquireData(page1, block, usepwd, password,downlink_mode) ) return 0;\r
343 if (!DecodeT55xxBlock()) return 0;\r
344\r
345 char blk[10]={0};\r
346 sprintf(blk,"%d", block);\r
347 printT55xxBlock(blk); \r
348 return 1;\r
349}\r
350\r
351int CmdT55xxReadBlock(const char *Cmd) {\r
352 uint8_t block = REGULAR_READ_MODE_BLOCK;\r
353 uint32_t password = 0; //default to blank Block 7\r
354 uint8_t downlink_mode = 0;\r
355 \r
356 bool usepwd = false;\r
357 bool override = false;\r
358 bool page1 = false;\r
359 bool errors = false;\r
360 uint8_t cmdp = 0;\r
361 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
362 switch(param_getchar(Cmd, cmdp)) {\r
363 case 'h':\r
364 case 'H':\r
365 return usage_t55xx_read();\r
366 case 'b':\r
367 case 'B':\r
368 errors |= param_getdec(Cmd, cmdp+1, &block);\r
369 cmdp += 2;\r
370 break;\r
371 case 'o':\r
372 case 'O':\r
373 override = true;\r
374 cmdp++;\r
375 break;\r
376 case 'p':\r
377 case 'P':\r
378 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
379 usepwd = true;\r
380 cmdp += 2;\r
381 break;\r
382 case '1':\r
383 page1 = true;\r
384 cmdp++;\r
385 break;\r
386 case 'r':\r
387 case 'R':\r
388 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';\r
389 if (downlink_mode > 3) downlink_mode = 0;\r
390 cmdp +=2;\r
391 break;\r
392 default:\r
393 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
394 errors = true;\r
395 break;\r
396 }\r
397 }\r
398 if (errors) return usage_t55xx_read();\r
399\r
400 if (block > 7 && block != REGULAR_READ_MODE_BLOCK ) {\r
401 PrintAndLog("Block must be between 0 and 7");\r
402 return 0;\r
403 }\r
404\r
405 printT5xxHeader(page1);\r
406 return T55xxReadBlock(block, page1, usepwd, override, password, downlink_mode);\r
407}\r
408\r
409bool DecodeT55xxBlock(){\r
410 \r
411 char buf[30] = {0x00};\r
412 char *cmdStr = buf;\r
413 int ans = 0;\r
414 bool ST = config.ST;\r
415 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};\r
416 DemodBufferLen = 0x00;\r
417\r
418 switch( config.modulation ){\r
419 case DEMOD_FSK:\r
420 snprintf(cmdStr, sizeof(buf),"%d %d", bitRate[config.bitrate], config.inverted );\r
421 ans = FSKrawDemod(cmdStr, false);\r
422 break;\r
423 case DEMOD_FSK1:\r
424 case DEMOD_FSK1a:\r
425 snprintf(cmdStr, sizeof(buf),"%d %d 8 5", bitRate[config.bitrate], config.inverted );\r
426 ans = FSKrawDemod(cmdStr, false);\r
427 break;\r
428 case DEMOD_FSK2:\r
429 case DEMOD_FSK2a:\r
430 snprintf(cmdStr, sizeof(buf),"%d %d 10 8", bitRate[config.bitrate], config.inverted );\r
431 ans = FSKrawDemod(cmdStr, false);\r
432 break;\r
433 case DEMOD_ASK:\r
434 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );\r
435 ans = ASKDemod_ext(cmdStr, false, false, 1, &ST);\r
436 break;\r
437 case DEMOD_PSK1:\r
438 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
439 save_restoreGB(GRAPH_SAVE);\r
440 CmdLtrim("160");\r
441 snprintf(cmdStr, sizeof(buf),"%d %d 6", bitRate[config.bitrate], config.inverted );\r
442 ans = PSKDemod(cmdStr, false);\r
443 //undo trim samples\r
444 save_restoreGB(GRAPH_RESTORE);\r
445 break;\r
446 case DEMOD_PSK2: //inverted won't affect this\r
447 case DEMOD_PSK3: //not fully implemented\r
448 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
449 save_restoreGB(GRAPH_SAVE);\r
450 CmdLtrim("160");\r
451 snprintf(cmdStr, sizeof(buf),"%d 0 6", bitRate[config.bitrate] );\r
452 ans = PSKDemod(cmdStr, false);\r
453 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
454 //undo trim samples\r
455 save_restoreGB(GRAPH_RESTORE);\r
456 break;\r
457 case DEMOD_NRZ:\r
458 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );\r
459 ans = NRZrawDemod(cmdStr, false);\r
460 break;\r
461 case DEMOD_BI:\r
462 case DEMOD_BIa:\r
463 snprintf(cmdStr, sizeof(buf),"0 %d %d 1", bitRate[config.bitrate], config.inverted );\r
464 ans = ASKbiphaseDemod(cmdStr, false);\r
465 break;\r
466 default:\r
467 return false;\r
468 }\r
469 return (bool) ans;\r
470}\r
471\r
472bool DecodeT5555TraceBlock() {\r
473 DemodBufferLen = 0x00;\r
474 \r
475 // According to datasheet. Always: RF/64, not inverted, Manchester\r
476 return (bool) ASKDemod("64 0 1", false, false, 1);\r
477}\r
478\r
479void T55xx_Print_DownlinkMode (uint8_t downlink_mode)\r
480{\r
481 char Msg[80];\r
482 sprintf (Msg,"Downlink Mode used : ");\r
483 \r
484 switch (downlink_mode) {\r
485 case 0 : strcat (Msg,"default/fixed bit length"); break;\r
486 case 1 : strcat (Msg,"long leading reference (r 1)"); break;\r
487 case 2 : strcat (Msg,"leading zero reference (r 2)"); break;\r
488 case 3 : strcat (Msg,"1 of 4 coding reference (r 3)"); break;\r
489 default :\r
490 strcat (Msg,"default/fixed bit length"); break;\r
491\r
492 }\r
493 \r
494 PrintAndLog (Msg);\r
495 \r
496}\r
497\r
498int CmdT55xxDetect(const char *Cmd){\r
499 bool errors = false;\r
500 bool useGB = false;\r
501 bool usepwd = false;\r
502 uint32_t password = 0;\r
503 uint8_t cmdp = 0;\r
504 uint8_t downlink_mode = 0;\r
505\r
506 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
507 switch(param_getchar(Cmd, cmdp)) {\r
508 case 'h':\r
509 case 'H':\r
510 return usage_t55xx_detect();\r
511 case 'p':\r
512 case 'P':\r
513 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
514 usepwd = true;\r
515 cmdp += 2;\r
516 break;\r
517 case '1':\r
518 // use Graphbuffer data\r
519 useGB = true;\r
520 cmdp++;\r
521 break;\r
522 case 'r':\r
523 case 'R':\r
524 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';\r
525 if (downlink_mode > 3) downlink_mode = 0;\r
526 cmdp +=2;\r
527 break; \r
528 default:\r
529 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
530 errors = true;\r
531 break;\r
532 }\r
533 }\r
534 if (errors) return usage_t55xx_detect();\r
535 \r
536 if ( !useGB) {\r
537 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password,downlink_mode) )\r
538 return 0;\r
539 }\r
540 \r
541 if ( !tryDetectModulation() )\r
542 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
543 else {\r
544 // Add downlink mode for reference.\r
545 T55xx_Print_DownlinkMode (downlink_mode);\r
546 }\r
547 return 1;\r
548}\r
549\r
550// detect configuration?\r
551bool tryDetectModulation(){\r
552 uint8_t hits = 0;\r
553 t55xx_conf_block_t tests[15];\r
554 int bitRate=0;\r
555 uint8_t fc1 = 0, fc2 = 0, ans = 0;\r
556 int clk = 0, firstClockEdge = 0;\r
557 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);\r
558 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {\r
559 if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
560 tests[hits].modulation = DEMOD_FSK;\r
561 if (fc1==8 && fc2 == 5)\r
562 tests[hits].modulation = DEMOD_FSK1a;\r
563 else if (fc1==10 && fc2 == 8)\r
564 tests[hits].modulation = DEMOD_FSK2;\r
565 tests[hits].bitrate = bitRate;\r
566 tests[hits].inverted = false;\r
567 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
568 tests[hits].ST = false;\r
569 ++hits;\r
570 }\r
571 if ( FSKrawDemod("0 1", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
572 tests[hits].modulation = DEMOD_FSK;\r
573 if (fc1 == 8 && fc2 == 5)\r
574 tests[hits].modulation = DEMOD_FSK1;\r
575 else if (fc1 == 10 && fc2 == 8)\r
576 tests[hits].modulation = DEMOD_FSK2a;\r
577 tests[hits].bitrate = bitRate;\r
578 tests[hits].inverted = true;\r
579 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
580 tests[hits].ST = false;\r
581 ++hits;\r
582 }\r
583 } else {\r
584 clk = GetAskClock("", false, false);\r
585 if (clk>0) {\r
586 tests[hits].ST = true;\r
587 if ( ASKDemod_ext("0 0 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
588 tests[hits].modulation = DEMOD_ASK;\r
589 tests[hits].bitrate = bitRate;\r
590 tests[hits].inverted = false;\r
591 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
592 ++hits;\r
593 }\r
594 tests[hits].ST = true;\r
595 if ( ASKDemod_ext("0 1 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
596 tests[hits].modulation = DEMOD_ASK;\r
597 tests[hits].bitrate = bitRate;\r
598 tests[hits].inverted = true;\r
599 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
600 ++hits;\r
601 }\r
602 if ( ASKbiphaseDemod("0 0 0 2", false) && test(DEMOD_BI, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {\r
603 tests[hits].modulation = DEMOD_BI;\r
604 tests[hits].bitrate = bitRate;\r
605 tests[hits].inverted = false;\r
606 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
607 tests[hits].ST = false;\r
608 ++hits;\r
609 }\r
610 if ( ASKbiphaseDemod("0 0 1 2", false) && test(DEMOD_BIa, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {\r
611 tests[hits].modulation = DEMOD_BIa;\r
612 tests[hits].bitrate = bitRate;\r
613 tests[hits].inverted = true;\r
614 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
615 tests[hits].ST = false;\r
616 ++hits;\r
617 }\r
618 }\r
619 clk = GetNrzClock("", false, false);\r
620 if (clk>8) { //clock of rf/8 is likely a false positive, so don't use it.\r
621 if ( NRZrawDemod("0 0 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
622 tests[hits].modulation = DEMOD_NRZ;\r
623 tests[hits].bitrate = bitRate;\r
624 tests[hits].inverted = false;\r
625 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
626 tests[hits].ST = false;\r
627 ++hits;\r
628 }\r
629\r
630 if ( NRZrawDemod("0 1 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
631 tests[hits].modulation = DEMOD_NRZ;\r
632 tests[hits].bitrate = bitRate;\r
633 tests[hits].inverted = true;\r
634 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
635 tests[hits].ST = false;\r
636 ++hits;\r
637 }\r
638 }\r
639 \r
640 clk = GetPskClock("", false, false);\r
641 if (clk>0) {\r
642 // allow undo\r
643 save_restoreGB(GRAPH_SAVE);\r
644 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
645 CmdLtrim("160");\r
646 if ( PSKDemod("0 0 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
647 tests[hits].modulation = DEMOD_PSK1;\r
648 tests[hits].bitrate = bitRate;\r
649 tests[hits].inverted = false;\r
650 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
651 tests[hits].ST = false;\r
652 ++hits;\r
653 }\r
654 if ( PSKDemod("0 1 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
655 tests[hits].modulation = DEMOD_PSK1;\r
656 tests[hits].bitrate = bitRate;\r
657 tests[hits].inverted = true;\r
658 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
659 tests[hits].ST = false;\r
660 ++hits;\r
661 }\r
662 // PSK2 - needs a call to psk1TOpsk2.\r
663 if ( PSKDemod("0 0 6", false)) {\r
664 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
665 if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\r
666 tests[hits].modulation = DEMOD_PSK2;\r
667 tests[hits].bitrate = bitRate;\r
668 tests[hits].inverted = false;\r
669 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
670 tests[hits].ST = false;\r
671 ++hits;\r
672 }\r
673 } // inverse waves does not affect this demod\r
674 // PSK3 - needs a call to psk1TOpsk2.\r
675 if ( PSKDemod("0 0 6", false)) {\r
676 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
677 if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){\r
678 tests[hits].modulation = DEMOD_PSK3;\r
679 tests[hits].bitrate = bitRate;\r
680 tests[hits].inverted = false;\r
681 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
682 tests[hits].ST = false;\r
683 ++hits;\r
684 }\r
685 } // inverse waves does not affect this demod\r
686 //undo trim samples\r
687 save_restoreGB(GRAPH_RESTORE);\r
688 }\r
689 } \r
690 if ( hits == 1) {\r
691 config.modulation = tests[0].modulation;\r
692 config.bitrate = tests[0].bitrate;\r
693 config.inverted = tests[0].inverted;\r
694 config.offset = tests[0].offset;\r
695 config.block0 = tests[0].block0;\r
696 config.Q5 = tests[0].Q5;\r
697 config.ST = tests[0].ST;\r
698 \r
699 printConfiguration( config);\r
700 return true;\r
701 }\r
702 \r
703 if ( hits > 1) {\r
704 PrintAndLog("Found [%d] possible matches for modulation.",hits);\r
705 for(int i=0; i<hits; ++i){\r
706 PrintAndLog("--[%d]---------------", i+1);\r
707 printConfiguration( tests[i]);\r
708 }\r
709 }\r
710 return false;\r
711}\r
712\r
713bool testModulation(uint8_t mode, uint8_t modread){\r
714 switch( mode ){\r
715 case DEMOD_FSK:\r
716 if (modread >= DEMOD_FSK1 && modread <= DEMOD_FSK2a) return true;\r
717 break;\r
718 case DEMOD_ASK:\r
719 if (modread == DEMOD_ASK) return true;\r
720 break;\r
721 case DEMOD_PSK1:\r
722 if (modread == DEMOD_PSK1) return true;\r
723 break;\r
724 case DEMOD_PSK2:\r
725 if (modread == DEMOD_PSK2) return true;\r
726 break;\r
727 case DEMOD_PSK3:\r
728 if (modread == DEMOD_PSK3) return true;\r
729 break;\r
730 case DEMOD_NRZ:\r
731 if (modread == DEMOD_NRZ) return true;\r
732 break;\r
733 case DEMOD_BI:\r
734 if (modread == DEMOD_BI) return true;\r
735 break;\r
736 case DEMOD_BIa:\r
737 if (modread == DEMOD_BIa) return true;\r
738 break; \r
739 default:\r
740 return false;\r
741 }\r
742 return false;\r
743}\r
744\r
745bool testQ5Modulation(uint8_t mode, uint8_t modread){\r
746 switch( mode ){\r
747 case DEMOD_FSK:\r
748 if (modread >= 4 && modread <= 5) return true;\r
749 break;\r
750 case DEMOD_ASK:\r
751 if (modread == 0) return true;\r
752 break;\r
753 case DEMOD_PSK1:\r
754 if (modread == 1) return true;\r
755 break;\r
756 case DEMOD_PSK2:\r
757 if (modread == 2) return true;\r
758 break;\r
759 case DEMOD_PSK3:\r
760 if (modread == 3) return true;\r
761 break;\r
762 case DEMOD_NRZ:\r
763 if (modread == 7) return true;\r
764 break;\r
765 case DEMOD_BI:\r
766 if (modread == 6) return true;\r
767 break;\r
768 default:\r
769 return false;\r
770 }\r
771 return false;\r
772}\r
773\r
774int convertQ5bitRate(uint8_t bitRateRead) {\r
775 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};\r
776 for (int i=0; i<8; i++)\r
777 if (expected[i] == bitRateRead)\r
778 return i;\r
779\r
780 return -1;\r
781}\r
782\r
783bool testQ5(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk){\r
784\r
785 if ( DemodBufferLen < 64 ) return false;\r
786 uint8_t si = 0;\r
787 for (uint8_t idx = 28; idx < 64; idx++){\r
788 si = idx;\r
789 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;\r
790\r
791 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key\r
792 uint8_t resv = PackBits(si, 8, DemodBuffer); si += 8;\r
793 // 2nibble must be zeroed.\r
794 if (safer != 0x6 && safer != 0x9) continue;\r
795 if ( resv > 0x00) continue;\r
796 //uint8_t pageSel = PackBits(si, 1, DemodBuffer); si += 1;\r
797 //uint8_t fastWrite = PackBits(si, 1, DemodBuffer); si += 1;\r
798 si += 1+1;\r
799 int bitRate = PackBits(si, 6, DemodBuffer)*2 + 2; si += 6; //bit rate\r
800 if (bitRate > 128 || bitRate < 8) continue;\r
801\r
802 //uint8_t AOR = PackBits(si, 1, DemodBuffer); si += 1; \r
803 //uint8_t PWD = PackBits(si, 1, DemodBuffer); si += 1; \r
804 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2; //could check psk cr\r
805 //uint8_t inverse = PackBits(si, 1, DemodBuffer); si += 1;\r
806 si += 1+1+2+1;\r
807 uint8_t modread = PackBits(si, 3, DemodBuffer); si += 3;\r
808 uint8_t maxBlk = PackBits(si, 3, DemodBuffer); si += 3;\r
809 //uint8_t ST = PackBits(si, 1, DemodBuffer); si += 1;\r
810 if (maxBlk == 0) continue;\r
811 //test modulation\r
812 if (!testQ5Modulation(mode, modread)) continue;\r
813 if (bitRate != clk) continue;\r
814 *fndBitRate = convertQ5bitRate(bitRate);\r
815 if (*fndBitRate < 0) continue;\r
816 *offset = idx;\r
817\r
818 return true;\r
819 }\r
820 return false;\r
821}\r
822\r
823bool testBitRate(uint8_t readRate, uint8_t clk){\r
824 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};\r
825 if (expected[readRate] == clk)\r
826 return true;\r
827\r
828 return false;\r
829}\r
830\r
831bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5){\r
832\r
833 if ( DemodBufferLen < 64 ) return false;\r
834 uint8_t si = 0;\r
835 for (uint8_t idx = 28; idx < 64; idx++){\r
836 si = idx;\r
837 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;\r
838\r
839 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key\r
840 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode\r
841 // 2nibble must be zeroed.\r
842 // moved test to here, since this gets most faults first.\r
843 if ( resv > 0x00) continue;\r
844\r
845 int bitRate = PackBits(si, 6, DemodBuffer); si += 6; //bit rate (includes extended mode part of rate)\r
846 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode\r
847 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1; \r
848 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //could check psk cr\r
849 //uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24, 30, 31 could be tested for 0 if not extended mode\r
850 //uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;\r
851 \r
852 //if extended mode\r
853 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? true : false;\r
854\r
855 if (!extMode) {\r
856 if (bitRate > 7) continue;\r
857 if (!testBitRate(bitRate, clk)) continue;\r
858 } else { //extended mode bitrate = same function to calc bitrate as em4x05\r
859 if (EM4x05_GET_BITRATE(bitRate) != clk) continue;\r
860\r
861 }\r
862 //test modulation\r
863 if (!testModulation(mode, modread)) continue;\r
864 *fndBitRate = bitRate;\r
865 *offset = idx;\r
866 *Q5 = false;\r
867 return true;\r
868 }\r
869 if (testQ5(mode, offset, fndBitRate, clk)) {\r
870 *Q5 = true;\r
871 return true;\r
872 }\r
873 return false;\r
874}\r
875\r
876void printT55xxBlock(const char *blockNum){\r
877 \r
878 uint8_t i = config.offset;\r
879 uint8_t endpos = 32 + i;\r
880 uint32_t blockData = 0;\r
881 uint8_t bits[64] = {0x00};\r
882\r
883 if ( !DemodBufferLen) return;\r
884\r
885 if ( endpos > DemodBufferLen){\r
886 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);\r
887 return;\r
888 }\r
889\r
890 for (; i < endpos; ++i)\r
891 bits[i - config.offset]=DemodBuffer[i];\r
892\r
893 blockData = PackBits(0, 32, bits);\r
894\r
895 PrintAndLog(" %s | %08X | %s", blockNum, blockData, sprint_bin(bits,32));\r
896}\r
897\r
898int special(const char *Cmd) {\r
899 uint32_t blockData = 0;\r
900 uint8_t bits[32] = {0x00};\r
901\r
902 PrintAndLog("OFFSET | DATA | BINARY");\r
903 PrintAndLog("----------------------------------------------------");\r
904 int i,j = 0;\r
905 for (; j < 64; ++j){\r
906 \r
907 for (i = 0; i < 32; ++i)\r
908 bits[i]=DemodBuffer[j+i];\r
909 \r
910 blockData = PackBits(0, 32, bits);\r
911 \r
912 PrintAndLog(" %02d | 0x%08X | %s",j , blockData, sprint_bin(bits,32)); \r
913 }\r
914 return 0;\r
915}\r
916\r
917int printConfiguration( t55xx_conf_block_t b){\r
918 PrintAndLog("Chip Type : %s", (b.Q5) ? "T5555(Q5)" : "T55x7");\r
919 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );\r
920 PrintAndLog("Bit Rate : %s", GetBitRateStr(b.bitrate, (b.block0 & T55x7_X_MODE && (b.block0>>28==6 || b.block0>>28==9))) );\r
921 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );\r
922 PrintAndLog("Offset : %d", b.offset);\r
923 PrintAndLog("Seq. Term. : %s", (b.ST) ? "Yes" : "No" );\r
924 PrintAndLog("Block0 : 0x%08X", b.block0);\r
925 PrintAndLog("");\r
926 return 0;\r
927}\r
928\r
929int CmdT55xxWakeUp(const char *Cmd) {\r
930 uint32_t password = 0;\r
931 if ( strlen(Cmd) <= 0 ) return usage_t55xx_wakup();\r
932 char cmdp = param_getchar(Cmd, 0);\r
933 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wakup();\r
934\r
935 password = param_get32ex(Cmd, 0, 0, 16);\r
936\r
937 UsbCommand c = {CMD_T55XX_WAKEUP, {password, 0, 0}};\r
938 clearCommandBuffer();\r
939 SendCommand(&c);\r
940 PrintAndLog("Wake up command sent. Try read now");\r
941 return 0;\r
942}\r
943\r
944int CmdT55xxWriteBlock(const char *Cmd) {\r
945 uint8_t block = 0xFF; //default to invalid block\r
946 uint32_t data = 0; //default to blank Block \r
947 uint32_t password = 0; //default to blank Block 7\r
948 uint32_t downlink_mode = 0;\r
949 \r
950 bool usepwd = false;\r
951 bool page1 = false; \r
952 bool gotdata = false;\r
953 bool testMode = false;\r
954 bool errors = false;\r
955 uint8_t cmdp = 0;\r
956 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
957 switch(param_getchar(Cmd, cmdp)) {\r
958 case 'h':\r
959 case 'H':\r
960 return usage_t55xx_write();\r
961 case 'b':\r
962 case 'B':\r
963 errors |= param_getdec(Cmd, cmdp+1, &block);\r
964 cmdp += 2;\r
965 break;\r
966 case 'd':\r
967 case 'D':\r
968 data = param_get32ex(Cmd, cmdp+1, 0, 16);\r
969 gotdata = true;\r
970 cmdp += 2;\r
971 break;\r
972 case 'p':\r
973 case 'P':\r
974 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
975 usepwd = true;\r
976 cmdp += 2;\r
977 break;\r
978 case 't':\r
979 case 'T':\r
980 testMode = true;\r
981 cmdp++;\r
982 break;\r
983 case '1':\r
984 page1 = true;\r
985 cmdp++;\r
986 break;\r
987 case 'r':\r
988 case 'R':\r
989 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';\r
990 if (downlink_mode > 3) downlink_mode = 0;\r
991 cmdp +=2;\r
992 break;\r
993 default:\r
994 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
995 errors = true;\r
996 break;\r
997 }\r
998 }\r
999 if (errors || !gotdata) return usage_t55xx_write();\r
1000\r
1001 if (block > 7) {\r
1002 PrintAndLog("Block number must be between 0 and 7");\r
1003 return 0;\r
1004 }\r
1005 \r
1006 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};\r
1007 UsbCommand resp;\r
1008 c.d.asBytes[0] = (page1) ? 0x2 : 0; \r
1009 c.d.asBytes[0] |= (testMode) ? 0x4 : 0; \r
1010 c.d.asBytes[0] |= (downlink_mode << 3);\r
1011 \r
1012 char pwdStr[16] = {0};\r
1013 snprintf(pwdStr, sizeof(pwdStr), "pwd: 0x%08X", password);\r
1014\r
1015 PrintAndLog("Writing page %d block: %02d data: 0x%08X %s", page1, block, data, (usepwd) ? pwdStr : "" );\r
1016 \r
1017 //Password mode\r
1018 if (usepwd) {\r
1019 c.arg[2] = password;\r
1020 c.d.asBytes[0] |= 0x1; \r
1021 }\r
1022 \r
1023 clearCommandBuffer();\r
1024 SendCommand(&c);\r
1025 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){\r
1026 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)");\r
1027 return 0;\r
1028 }\r
1029 return 1;\r
1030}\r
1031\r
1032int CmdT55xxReadTrace(const char *Cmd) {\r
1033 char cmdp = param_getchar(Cmd, 0);\r
1034 bool pwdmode = false;\r
1035 uint32_t password = 0;\r
1036 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') \r
1037 return usage_t55xx_trace();\r
1038\r
1039 if (strlen(Cmd)==0)\r
1040 if ( !AquireData( T55x7_PAGE1, REGULAR_READ_MODE_BLOCK, pwdmode, password,0 ) )\r
1041 return 0;\r
1042\r
1043 if ( config.Q5 ) {\r
1044 if (!DecodeT5555TraceBlock()) return 0;\r
1045 } else {\r
1046 if (!DecodeT55xxBlock()) return 0;\r
1047 }\r
1048\r
1049 if ( !DemodBufferLen ) return 0;\r
1050\r
1051 RepaintGraphWindow();\r
1052 uint8_t repeat = (config.offset > 5) ? 32 : 0;\r
1053\r
1054 uint8_t si = config.offset+repeat;\r
1055 uint32_t bl1 = PackBits(si, 32, DemodBuffer);\r
1056 uint32_t bl2 = PackBits(si+32, 32, DemodBuffer);\r
1057\r
1058 if (config.Q5) {\r
1059 uint32_t hdr = PackBits(si, 9, DemodBuffer); si += 9;\r
1060\r
1061 if (hdr != 0x1FF) {\r
1062 PrintAndLog("Invalid Q5 Trace data header (expected 0x1FF, found %X)", hdr);\r
1063 return 0;\r
1064 }\r
1065\r
1066 t5555_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .icr = 0, .lotidc = '?', .lotid = 0, .wafer = 0, .dw =0};\r
1067\r
1068 data.icr = PackBits(si, 2, DemodBuffer); si += 2;\r
1069 data.lotidc = 'Z' - PackBits(si, 2, DemodBuffer); si += 3;\r
1070\r
1071 data.lotid = PackBits(si, 4, DemodBuffer); si += 5;\r
1072 data.lotid <<= 4;\r
1073 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1074 data.lotid <<= 4;\r
1075 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1076 data.lotid <<= 4;\r
1077 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1078 data.lotid <<= 1;\r
1079 data.lotid |= PackBits(si, 1, DemodBuffer); si += 1;\r
1080\r
1081 data.wafer = PackBits(si, 3, DemodBuffer); si += 4;\r
1082 data.wafer <<= 2;\r
1083 data.wafer |= PackBits(si, 2, DemodBuffer); si += 2;\r
1084\r
1085 data.dw = PackBits(si, 2, DemodBuffer); si += 3;\r
1086 data.dw <<= 4;\r
1087 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1088 data.dw <<= 4;\r
1089 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1090 data.dw <<= 4;\r
1091 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1092\r
1093 printT5555Trace(data, repeat);\r
1094\r
1095 } else {\r
1096\r
1097 t55x7_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .acl = 0, .mfc = 0, .cid = 0, .year = 0, .quarter = 0, .icr = 0, .lotid = 0, .wafer = 0, .dw = 0};\r
1098 \r
1099 data.acl = PackBits(si, 8, DemodBuffer); si += 8;\r
1100 if ( data.acl != 0xE0 ) {\r
1101 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");\r
1102 return 0;\r
1103 }\r
1104\r
1105 data.mfc = PackBits(si, 8, DemodBuffer); si += 8;\r
1106 data.cid = PackBits(si, 5, DemodBuffer); si += 5;\r
1107 data.icr = PackBits(si, 3, DemodBuffer); si += 3;\r
1108 data.year = PackBits(si, 4, DemodBuffer); si += 4;\r
1109 data.quarter = PackBits(si, 2, DemodBuffer); si += 2;\r
1110 data.lotid = PackBits(si, 14, DemodBuffer); si += 14;\r
1111 data.wafer = PackBits(si, 5, DemodBuffer); si += 5;\r
1112 data.dw = PackBits(si, 15, DemodBuffer); \r
1113\r
1114 time_t t = time(NULL);\r
1115 struct tm tm = *localtime(&t);\r
1116 if ( data.year > tm.tm_year-110)\r
1117 data.year += 2000;\r
1118 else\r
1119 data.year += 2010;\r
1120\r
1121 printT55x7Trace(data, repeat);\r
1122 }\r
1123 return 0;\r
1124}\r
1125\r
1126void printT55x7Trace( t55x7_tracedata_t data, uint8_t repeat ){\r
1127 PrintAndLog("-- T55x7 Trace Information ----------------------------------");\r
1128 PrintAndLog("-------------------------------------------------------------");\r
1129 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", data.acl, data.acl);\r
1130 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", data.mfc, data.mfc, getManufacturerName(data.mfc));\r
1131 PrintAndLog(" CID : 0x%02X (%d) - %s", data.cid, data.cid, GetModelStrFromCID(data.cid));\r
1132 PrintAndLog(" ICR IC Revision : %d", data.icr );\r
1133 PrintAndLog(" Manufactured");\r
1134 PrintAndLog(" Year/Quarter : %d/%d", data.year, data.quarter);\r
1135 PrintAndLog(" Lot ID : %d", data.lotid );\r
1136 PrintAndLog(" Wafer number : %d", data.wafer);\r
1137 PrintAndLog(" Die Number : %d", data.dw);\r
1138 PrintAndLog("-------------------------------------------------------------");\r
1139 PrintAndLog(" Raw Data - Page 1");\r
1140 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );\r
1141 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );\r
1142 PrintAndLog("-------------------------------------------------------------"); \r
1143\r
1144 /*\r
1145 TRACE - BLOCK O\r
1146 Bits Definition HEX\r
1147 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0 \r
1148 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation\r
1149 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2 \r
1150 22-24 ICR IC revision\r
1151 25-28 YEAR (BCD encoded) 9 (= 2009)\r
1152 29-30 QUARTER 1,2,3,4 \r
1153 31-32 LOT ID\r
1154 \r
1155 TRACE - BLOCK 1\r
1156 1-12 LOT ID \r
1157 13-17 Wafer number\r
1158 18-32 DW, die number sequential\r
1159 */\r
1160}\r
1161\r
1162void printT5555Trace( t5555_tracedata_t data, uint8_t repeat ){\r
1163 PrintAndLog("-- T5555 (Q5) Trace Information -----------------------------");\r
1164 PrintAndLog("-------------------------------------------------------------");\r
1165 PrintAndLog(" ICR IC Revision : %d", data.icr ); \r
1166 PrintAndLog(" Lot : %c%d", data.lotidc, data.lotid);\r
1167 PrintAndLog(" Wafer number : %d", data.wafer);\r
1168 PrintAndLog(" Die Number : %d", data.dw);\r
1169 PrintAndLog("-------------------------------------------------------------");\r
1170 PrintAndLog(" Raw Data - Page 1");\r
1171 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );\r
1172 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );\r
1173 \r
1174 /*\r
1175 ** Q5 **\r
1176 TRACE - BLOCK O and BLOCK1\r
1177 Bits Definition HEX\r
1178 1-9 Header 0x1FF\r
1179 10-11 IC Revision\r
1180 12-13 Lot ID char\r
1181 15-35 Lot ID (NB parity)\r
1182 36-41 Wafer number (NB parity)\r
1183 42-58 DW, die number sequential (NB parity)\r
1184 60-63 Parity bits\r
1185 64 Always zero\r
1186 */\r
1187}\r
1188\r
1189//need to add Q5 info...\r
1190int CmdT55xxInfo(const char *Cmd){\r
1191 /*\r
1192 Page 0 Block 0 Configuration data.\r
1193 Normal mode\r
1194 Extended mode\r
1195 */\r
1196 bool pwdmode = false;\r
1197 uint32_t password = 0;\r
1198 char cmdp = param_getchar(Cmd, 0);\r
1199\r
1200 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')\r
1201 return usage_t55xx_info();\r
1202 \r
1203 if (strlen(Cmd)==0)\r
1204 if ( !AquireData( T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, pwdmode, password,0 ) )\r
1205 return 1;\r
1206\r
1207 if (!DecodeT55xxBlock()) return 1;\r
1208\r
1209 // too little space to start with\r
1210 if ( DemodBufferLen < 32) return 1;\r
1211\r
1212 uint8_t si = config.offset;\r
1213 uint32_t bl0 = PackBits(si, 32, DemodBuffer);\r
1214 \r
1215 uint32_t safer = PackBits(si, 4, DemodBuffer); si += 4; \r
1216 uint32_t resv = PackBits(si, 7, DemodBuffer); si += 7;\r
1217 uint32_t dbr = PackBits(si, 3, DemodBuffer); si += 3;\r
1218 uint32_t extend = PackBits(si, 1, DemodBuffer); si += 1;\r
1219 uint32_t datamod = PackBits(si, 5, DemodBuffer); si += 5;\r
1220 uint32_t pskcf = PackBits(si, 2, DemodBuffer); si += 2;\r
1221 uint32_t aor = PackBits(si, 1, DemodBuffer); si += 1; \r
1222 uint32_t otp = PackBits(si, 1, DemodBuffer); si += 1; \r
1223 uint32_t maxblk = PackBits(si, 3, DemodBuffer); si += 3;\r
1224 uint32_t pwd = PackBits(si, 1, DemodBuffer); si += 1; \r
1225 uint32_t sst = PackBits(si, 1, DemodBuffer); si += 1; \r
1226 uint32_t fw = PackBits(si, 1, DemodBuffer); si += 1;\r
1227 uint32_t inv = PackBits(si, 1, DemodBuffer); si += 1; \r
1228 uint32_t por = PackBits(si, 1, DemodBuffer); si += 1;\r
1229 \r
1230 if (config.Q5) PrintAndLog("*** Warning *** Config Info read off a Q5 will not display as expected");\r
1231 PrintAndLog("");\r
1232 PrintAndLog("-- T55x7 Configuration & Tag Information --------------------");\r
1233 PrintAndLog("-------------------------------------------------------------");\r
1234 PrintAndLog(" Safer key : %s", GetSaferStr(safer));\r
1235 PrintAndLog(" reserved : %d", resv);\r
1236 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr, extend));\r
1237 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");\r
1238 PrintAndLog(" Modulation : %s", GetModulationStr(datamod));\r
1239 PrintAndLog(" PSK clock frequency : %d", pskcf);\r
1240 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");\r
1241 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );\r
1242 PrintAndLog(" Max block : %d", maxblk);\r
1243 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");\r
1244 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");\r
1245 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");\r
1246 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");\r
1247 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");\r
1248 PrintAndLog("-------------------------------------------------------------");\r
1249 PrintAndLog(" Raw Data - Page 0");\r
1250 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset,32) );\r
1251 PrintAndLog("-------------------------------------------------------------");\r
1252 \r
1253 return 0;\r
1254}\r
1255\r
1256int CmdT55xxDump(const char *Cmd){\r
1257\r
1258 uint32_t password = 0;\r
1259 char cmdp = param_getchar(Cmd, 0);\r
1260 bool override = false;\r
1261 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_dump();\r
1262\r
1263 bool usepwd = ( strlen(Cmd) > 0);\r
1264 if ( usepwd ){\r
1265 password = param_get32ex(Cmd, 0, 0, 16);\r
1266 if (param_getchar(Cmd, 1) =='o' )\r
1267 override = true;\r
1268 }\r
1269 \r
1270 printT5xxHeader(0);\r
1271 for ( uint8_t i = 0; i <8; ++i)\r
1272 T55xxReadBlock(i, 0, usepwd, override, password,0);\r
1273\r
1274 printT5xxHeader(1);\r
1275 for ( uint8_t i = 0; i<4; i++)\r
1276 T55xxReadBlock(i, 1, usepwd, override, password,0); \r
1277\r
1278 return 1;\r
1279}\r
1280\r
1281int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password, uint8_t downlink_mode ){\r
1282 // arg0 bitmodes:\r
1283 // bit0 = pwdmode\r
1284 // bit1 = page to read from\r
1285 uint8_t arg0 = (page<<1) | pwdmode;\r
1286 arg0 |= (downlink_mode << 3);\r
1287 UsbCommand c = {CMD_T55XX_READ_BLOCK, {arg0, block, password}};\r
1288\r
1289 clearCommandBuffer();\r
1290 SendCommand(&c);\r
1291 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
1292 PrintAndLog("command execution time out");\r
1293 return 0;\r
1294 }\r
1295 getSamples(12000,true);\r
1296 return 1;\r
1297}\r
1298\r
1299char * GetBitRateStr(uint32_t id, bool xmode) {\r
1300 static char buf[25];\r
1301\r
1302 char *retStr = buf;\r
1303 if (xmode) { //xmode bitrate calc is same as em4x05 calc\r
1304 snprintf(retStr,sizeof(buf),"%d - RF/%d", id, EM4x05_GET_BITRATE(id));\r
1305 } else {\r
1306 switch (id) {\r
1307 case 0: snprintf(retStr,sizeof(buf),"%d - RF/8",id); break;\r
1308 case 1: snprintf(retStr,sizeof(buf),"%d - RF/16",id); break;\r
1309 case 2: snprintf(retStr,sizeof(buf),"%d - RF/32",id); break;\r
1310 case 3: snprintf(retStr,sizeof(buf),"%d - RF/40",id); break;\r
1311 case 4: snprintf(retStr,sizeof(buf),"%d - RF/50",id); break;\r
1312 case 5: snprintf(retStr,sizeof(buf),"%d - RF/64",id); break;\r
1313 case 6: snprintf(retStr,sizeof(buf),"%d - RF/100",id); break;\r
1314 case 7: snprintf(retStr,sizeof(buf),"%d - RF/128",id); break;\r
1315 default: snprintf(retStr,sizeof(buf),"%d - (Unknown)",id); break;\r
1316 } \r
1317 }\r
1318 return buf;\r
1319}\r
1320\r
1321char * GetSaferStr(uint32_t id) {\r
1322 static char buf[40];\r
1323 char *retStr = buf;\r
1324 \r
1325 snprintf(retStr,sizeof(buf),"%d",id);\r
1326 if (id == 6) {\r
1327 snprintf(retStr,sizeof(buf),"%d - passwd",id);\r
1328 }\r
1329 if (id == 9 ){\r
1330 snprintf(retStr,sizeof(buf),"%d - testmode",id);\r
1331 }\r
1332 \r
1333 return buf;\r
1334}\r
1335\r
1336char * GetModulationStr( uint32_t id){\r
1337 static char buf[60];\r
1338 char *retStr = buf;\r
1339 \r
1340 switch (id){\r
1341 case 0: snprintf(retStr,sizeof(buf),"%d - DIRECT (ASK/NRZ)",id); break;\r
1342 case 1: snprintf(retStr,sizeof(buf),"%d - PSK 1 phase change when input changes",id); break;\r
1343 case 2: snprintf(retStr,sizeof(buf),"%d - PSK 2 phase change on bitclk if input high",id); break;\r
1344 case 3: snprintf(retStr,sizeof(buf),"%d - PSK 3 phase change on rising edge of input",id); break;\r
1345 case 4: snprintf(retStr,sizeof(buf),"%d - FSK 1 RF/8 RF/5",id); break;\r
1346 case 5: snprintf(retStr,sizeof(buf),"%d - FSK 2 RF/8 RF/10",id); break;\r
1347 case 6: snprintf(retStr,sizeof(buf),"%d - FSK 1a RF/5 RF/8",id); break;\r
1348 case 7: snprintf(retStr,sizeof(buf),"%d - FSK 2a RF/10 RF/8",id); break;\r
1349 case 8: snprintf(retStr,sizeof(buf),"%d - Manchester",id); break;\r
1350 case 16: snprintf(retStr,sizeof(buf),"%d - Biphase",id); break;\r
1351 case 0x18: snprintf(retStr,sizeof(buf),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id); break;\r
1352 case 17: snprintf(retStr,sizeof(buf),"%d - Reserved",id); break;\r
1353 default: snprintf(retStr,sizeof(buf),"0x%02X (Unknown)",id); break;\r
1354 }\r
1355 return buf;\r
1356}\r
1357\r
1358char * GetModelStrFromCID(uint32_t cid){\r
1359 \r
1360 static char buf[10];\r
1361 char *retStr = buf;\r
1362 \r
1363 if (cid == 1) snprintf(retStr, sizeof(buf),"ATA5577M1");\r
1364 if (cid == 2) snprintf(retStr, sizeof(buf),"ATA5577M2"); \r
1365 return buf;\r
1366}\r
1367\r
1368char * GetSelectedModulationStr( uint8_t id){\r
1369\r
1370 static char buf[20];\r
1371 char *retStr = buf;\r
1372\r
1373 switch (id) {\r
1374 case DEMOD_FSK: snprintf(retStr,sizeof(buf),"FSK"); break;\r
1375 case DEMOD_FSK1: snprintf(retStr,sizeof(buf),"FSK1"); break;\r
1376 case DEMOD_FSK1a: snprintf(retStr,sizeof(buf),"FSK1a"); break;\r
1377 case DEMOD_FSK2: snprintf(retStr,sizeof(buf),"FSK2"); break;\r
1378 case DEMOD_FSK2a: snprintf(retStr,sizeof(buf),"FSK2a"); break;\r
1379 case DEMOD_ASK: snprintf(retStr,sizeof(buf),"ASK"); break;\r
1380 case DEMOD_NRZ: snprintf(retStr,sizeof(buf),"DIRECT/NRZ"); break;\r
1381 case DEMOD_PSK1: snprintf(retStr,sizeof(buf),"PSK1"); break;\r
1382 case DEMOD_PSK2: snprintf(retStr,sizeof(buf),"PSK2"); break;\r
1383 case DEMOD_PSK3: snprintf(retStr,sizeof(buf),"PSK3"); break;\r
1384 case DEMOD_BI: snprintf(retStr,sizeof(buf),"BIPHASE"); break;\r
1385 case DEMOD_BIa: snprintf(retStr,sizeof(buf),"BIPHASEa - (CDP)"); break;\r
1386 default: snprintf(retStr,sizeof(buf),"(Unknown)"); break;\r
1387 }\r
1388 return buf;\r
1389}\r
1390\r
1391uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){\r
1392 \r
1393 int i = start;\r
1394 int j = len-1;\r
1395\r
1396 if (len > 32) return 0;\r
1397\r
1398 uint32_t tmp = 0;\r
1399 for (; j >= 0; --j, ++i)\r
1400 tmp |= bits[i] << j;\r
1401\r
1402 return tmp;\r
1403}\r
1404\r
1405int CmdResetRead(const char *Cmd) {\r
1406 UsbCommand c = {CMD_T55XX_RESET_READ, {0,0,0}};\r
1407\r
1408 clearCommandBuffer();\r
1409 SendCommand(&c);\r
1410 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
1411 PrintAndLog("command execution time out");\r
1412 return 0;\r
1413 }\r
1414\r
1415 uint8_t got[BIGBUF_SIZE-1];\r
1416 GetFromBigBuf(got, sizeof(got), 0, NULL, -1 , 0);\r
1417 setGraphBuf(got, sizeof(got));\r
1418 return 1;\r
1419}\r
1420\r
1421int CmdT55xxWipe(const char *Cmd) {\r
1422 char writeData[20] = {0};\r
1423 char *ptrData = writeData;\r
1424\r
1425 char cmdp = param_getchar(Cmd, 0); \r
1426 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wipe();\r
1427\r
1428 bool Q5 = (cmdp == 'q' || cmdp == 'Q');\r
1429\r
1430 // Try with the default password to reset block 0\r
1431 // With a pwd should work even if pwd bit not set\r
1432 PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n");\r
1433\r
1434 if ( Q5 ){\r
1435 snprintf(ptrData,sizeof(writeData),"b 0 d 6001F004 p 0");\r
1436 } else {\r
1437 snprintf(ptrData,sizeof(writeData),"b 0 d 00088040 p 0");\r
1438 }\r
1439\r
1440 if (!CmdT55xxWriteBlock(ptrData)) PrintAndLog("Error writing blk 0");\r
1441\r
1442 for (uint8_t blk = 1; blk<8; blk++) {\r
1443 snprintf(ptrData,sizeof(writeData),"b %d d 0", blk);\r
1444 if (!CmdT55xxWriteBlock(ptrData))\r
1445 PrintAndLog("Error writing blk %d", blk);\r
1446\r
1447 memset(writeData, 0x00, sizeof(writeData));\r
1448 }\r
1449 return 0;\r
1450}\r
1451\r
1452int CmdT55xxBruteForce(const char *Cmd) {\r
1453\r
1454 // load a default pwd file.\r
1455 char buf[9];\r
1456 char filename[FILE_PATH_SIZE]={0};\r
1457 int keycnt = 0;\r
1458 int ch;\r
1459 uint8_t stKeyBlock = 20;\r
1460 uint8_t *keyBlock = NULL, *p = NULL;\r
1461 uint32_t start_password = 0x00000000; //start password\r
1462 uint32_t end_password = 0xFFFFFFFF; //end password\r
1463 bool found = false;\r
1464 uint8_t downlink_mode = 0;\r
1465 bool try_all_dl_modes = false;\r
1466 uint8_t dl_mode = 0;\r
1467 uint8_t cmd_offset = 0;\r
1468 int cmd_opt = 0;\r
1469 \r
1470 char cmdp = param_getchar(Cmd, 0);\r
1471 \r
1472 if (cmdp == 'h' || cmdp == 'H') return usage_t55xx_bruteforce();\r
1473 if (cmdp == 'r' || cmdp == 'R') {\r
1474 downlink_mode = param_getchar(Cmd, 1) - '0'; // get 2nd option, as this is fixed order.\r
1475 if (downlink_mode == 4) try_all_dl_modes = true;\r
1476 if (downlink_mode > 3) downlink_mode = 0;\r
1477 cmd_opt += 2; // To help start/end passwords for range to be found\r
1478 cmd_offset += 4; // r <sp> x <sp> To help the string offset for filename start position in cmd\r
1479 cmdp = param_getchar(Cmd, 2); // get 3rd option, as this is fixed order.\r
1480 }\r
1481\r
1482 keyBlock = calloc(stKeyBlock, 6);\r
1483 if (keyBlock == NULL) return 1;\r
1484\r
1485 if (cmdp == 'i' || cmdp == 'I') {\r
1486\r
1487 int len = strlen(Cmd+2);\r
1488 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
1489 memcpy(filename, Cmd+2+cmd_offset, len);\r
1490\r
1491 FILE * f = fopen( filename , "r");\r
1492\r
1493 if ( !f ) {\r
1494 PrintAndLog("File: %s: not found or locked.", filename);\r
1495 free(keyBlock);\r
1496 return 1;\r
1497 }\r
1498\r
1499 while( fgets(buf, sizeof(buf), f) ) {\r
1500 if (strlen(buf) < 8 || buf[7] == '\n') continue;\r
1501\r
1502 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
1503\r
1504 //The line start with # is comment, skip\r
1505 if( buf[0]=='#' ) continue;\r
1506\r
1507 if (!isxdigit((unsigned char)buf[0])) {\r
1508 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);\r
1509 continue;\r
1510 }\r
1511 \r
1512 buf[8] = 0;\r
1513\r
1514 if ( stKeyBlock - keycnt < 2) {\r
1515 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1516 if (!p) {\r
1517 PrintAndLog("Cannot allocate memory for defaultKeys");\r
1518 free(keyBlock);\r
1519 fclose(f);\r
1520 return 2;\r
1521 }\r
1522 keyBlock = p;\r
1523 }\r
1524 memset(keyBlock + 4 * keycnt, 0, 4);\r
1525 num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);\r
1526 PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));\r
1527 keycnt++;\r
1528 memset(buf, 0, sizeof(buf));\r
1529 }\r
1530 fclose(f);\r
1531 \r
1532 if (keycnt == 0) {\r
1533 PrintAndLog("No keys found in file");\r
1534 free(keyBlock);\r
1535 return 1;\r
1536 }\r
1537 PrintAndLog("Loaded %d keys", keycnt);\r
1538 \r
1539 // loop\r
1540 uint64_t testpwd = 0x00;\r
1541 for (uint16_t c = 0; c < keycnt; ++c ) {\r
1542\r
1543 if (ukbhit()) {\r
1544 ch = getchar();\r
1545 (void)ch;\r
1546 printf("\naborted via keyboard!\n");\r
1547 free(keyBlock);\r
1548 return 0;\r
1549 }\r
1550\r
1551 testpwd = bytes_to_num(keyBlock + 4*c, 4);\r
1552\r
1553 PrintAndLog("Testing %08X", testpwd);\r
1554 \r
1555 // Try each downlink_mode if asked to \r
1556 // donwlink_mode will = 0 if > 3 or set to 0, so loop from 0 - 3\r
1557 for (dl_mode = downlink_mode; dl_mode <= 3; dl_mode++){\r
1558 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, testpwd, dl_mode)) {\r
1559 PrintAndLog("Acquiring data from device failed. Quitting");\r
1560 free(keyBlock);\r
1561 return 0;\r
1562 }\r
1563\r
1564 found = tryDetectModulation();\r
1565\r
1566 if ( found ) {\r
1567 PrintAndLog("Found valid password: [%08X]", testpwd);\r
1568 free(keyBlock);\r
1569 \r
1570 T55xx_Print_DownlinkMode (dl_mode);\r
1571\r
1572 return 0;\r
1573 }\r
1574 if (!try_all_dl_modes) // Exit loop if not trying all downlink modes\r
1575 dl_mode = 4;\r
1576 } \r
1577 }\r
1578 PrintAndLog("Password NOT found.");\r
1579 free(keyBlock);\r
1580 return 0;\r
1581 }\r
1582\r
1583 // Try to read Block 7, first :)\r
1584\r
1585 // incremental pwd range search\r
1586 start_password = param_get32ex(Cmd, cmd_opt , 0, 16);\r
1587 end_password = param_get32ex(Cmd, cmd_opt+1 , 0, 16);\r
1588\r
1589 if ( start_password >= end_password ) {\r
1590 free(keyBlock);\r
1591 return usage_t55xx_bruteforce();\r
1592 }\r
1593 PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);\r
1594\r
1595 uint32_t i = start_password;\r
1596\r
1597 while ((!found) && (i <= end_password)) {\r
1598\r
1599 printf(".");\r
1600 fflush(stdout);\r
1601 if (ukbhit()) {\r
1602 ch = getchar();\r
1603 (void)ch;\r
1604 printf("\naborted via keyboard!\n");\r
1605 free(keyBlock);\r
1606 return 0;\r
1607 }\r
1608 // Try each downlink_mode if asked to \r
1609 // donwlink_mode will = 0 if > 3 or set to 0, so loop from 0 - 3\r
1610 for (dl_mode = downlink_mode; dl_mode <= 3; dl_mode++){\r
1611 if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, i,dl_mode)) {\r
1612 PrintAndLog("Acquiring data from device failed. Quitting");\r
1613 free(keyBlock);\r
1614 return 0;\r
1615 }\r
1616 found = tryDetectModulation();\r
1617\r
1618 if (found) break;\r
1619 if (!try_all_dl_modes) // Exit loop if not trying all downlink modes\r
1620 dl_mode = 4; \r
1621 }\r
1622 if (found) break;\r
1623 i++;\r
1624 }\r
1625\r
1626 if (found){\r
1627 PrintAndLog("Found valid password: [%08x]", i);\r
1628 T55xx_Print_DownlinkMode (downlink_mode);\r
1629 }\r
1630 else{\r
1631 PrintAndLog("");\r
1632 PrintAndLog("Password NOT found. Last tried: [%08x]", --i);\r
1633 }\r
1634\r
1635 free(keyBlock);\r
1636 return 0;\r
1637}\r
1638// note length of data returned is different for different chips. \r
1639// some return all page 1 (64 bits) and others return just that block (32 bits) \r
1640// unfortunately the 64 bits makes this more likely to get a false positive...\r
1641bool tryDetectP1(bool getData) {\r
1642 uint8_t preamble[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1};\r
1643 size_t startIdx = 0;\r
1644 uint8_t fc1 = 0, fc2 = 0, ans = 0;\r
1645 int clk = 0, firstClockEdge = 0;\r
1646 bool st = true;\r
1647\r
1648 if ( getData ) {\r
1649 if ( !AquireData(T55x7_PAGE1, 1, false, 0,0) )\r
1650 return false;\r
1651 }\r
1652\r
1653 // try fsk clock detect. if successful it cannot be any other type of modulation... (in theory...)\r
1654 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);\r
1655 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {\r
1656 if ( FSKrawDemod("0 0", false) && \r
1657 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1658 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1659 return true;\r
1660 }\r
1661 if ( FSKrawDemod("0 1", false) && \r
1662 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1663 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1664 return true;\r
1665 }\r
1666 return false;\r
1667 }\r
1668\r
1669 // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...)\r
1670 clk = GetPskClock("", false, false);\r
1671 if (clk>0) {\r
1672 // allow undo\r
1673 // save_restoreGB(1);\r
1674 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
1675 //CmdLtrim("160");\r
1676 if ( PSKDemod("0 0 6", false) &&\r
1677 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1678 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1679 //save_restoreGB(0);\r
1680 return true;\r
1681 }\r
1682 if ( PSKDemod("0 1 6", false) &&\r
1683 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1684 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1685 //save_restoreGB(0);\r
1686 return true;\r
1687 }\r
1688 // PSK2 - needs a call to psk1TOpsk2.\r
1689 if ( PSKDemod("0 0 6", false)) {\r
1690 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
1691 if (preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1692 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1693 //save_restoreGB(0);\r
1694 return true;\r
1695 }\r
1696 } // inverse waves does not affect PSK2 demod\r
1697 //undo trim samples\r
1698 //save_restoreGB(0); \r
1699 // no other modulation clocks = 2 or 4 so quit searching\r
1700 if (fc1 != 8) return false;\r
1701 }\r
1702\r
1703 // try ask clock detect. it could be another type even if successful.\r
1704 clk = GetAskClock("", false, false);\r
1705 if (clk>0) {\r
1706 if ( ASKDemod_ext("0 0 1", false, false, 1, &st) &&\r
1707 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1708 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1709 return true;\r
1710 }\r
1711 st = true;\r
1712 if ( ASKDemod_ext("0 1 1", false, false, 1, &st) &&\r
1713 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1714 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1715 return true;\r
1716 }\r
1717 if ( ASKbiphaseDemod("0 0 0 2", false) &&\r
1718 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1719 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1720 return true;\r
1721 }\r
1722 if ( ASKbiphaseDemod("0 0 1 2", false) &&\r
1723 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1724 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1725 return true;\r
1726 }\r
1727 }\r
1728\r
1729 // try NRZ clock detect. it could be another type even if successful.\r
1730 clk = GetNrzClock("", false, false); //has the most false positives :(\r
1731 if (clk>0) {\r
1732 if ( NRZrawDemod("0 0 1", false) &&\r
1733 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1734 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1735 return true;\r
1736 }\r
1737 if ( NRZrawDemod("0 1 1", false) &&\r
1738 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
1739 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {\r
1740 return true;\r
1741 }\r
1742 }\r
1743 return false;\r
1744}\r
1745// does this need to be a callable command?\r
1746int CmdT55xxDetectPage1(const char *Cmd){\r
1747 bool errors = false;\r
1748 bool useGB = false;\r
1749 bool usepwd = false;\r
1750 uint32_t password = 0;\r
1751 uint8_t cmdp = 0;\r
1752\r
1753 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
1754 switch(param_getchar(Cmd, cmdp)) {\r
1755 case 'h':\r
1756 case 'H':\r
1757 return usage_t55xx_detectP1();\r
1758 case 'p':\r
1759 case 'P':\r
1760 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
1761 usepwd = true;\r
1762 cmdp += 2;\r
1763 break;\r
1764 case '1':\r
1765 // use Graphbuffer data\r
1766 useGB = true;\r
1767 cmdp++;\r
1768 break;\r
1769 default:\r
1770 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
1771 errors = true;\r
1772 break;\r
1773 }\r
1774 }\r
1775 if (errors) return usage_t55xx_detectP1();\r
1776\r
1777 if ( !useGB ) {\r
1778 if ( !AquireData(T55x7_PAGE1, 1, usepwd, password,0) )\r
1779 return false;\r
1780 }\r
1781 bool success = tryDetectP1(false);\r
1782 if (success) PrintAndLog("T55xx chip found!");\r
1783 return success;\r
1784}\r
1785\r
1786static command_t CommandTable[] = {\r
1787 {"help", CmdHelp, 1, "This help"},\r
1788 {"bruteforce",CmdT55xxBruteForce,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"},\r
1789 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},\r
1790 {"detect", CmdT55xxDetect, 1, "[1] Try detecting the tag modulation from reading the configuration block."},\r
1791 {"p1detect", CmdT55xxDetectPage1,1, "[1] Try detecting if this is a t55xx tag by reading page 1"},\r
1792 {"read", CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},\r
1793 {"resetread", CmdResetRead, 0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"},\r
1794 {"write", CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},\r
1795 {"trace", CmdT55xxReadTrace, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},\r
1796 {"info", CmdT55xxInfo, 1, "[1] Show T55x7 configuration data (page 0/ blk 0)"},\r
1797 {"dump", CmdT55xxDump, 0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},\r
1798 {"special", special, 0, "Show block changes with 64 different offsets"},\r
1799 {"wakeup", CmdT55xxWakeUp, 0, "Send AOR wakeup command"},\r
1800 {"wipe", CmdT55xxWipe, 0, "[q] Wipe a T55xx tag and set defaults (will destroy any data on tag)"},\r
1801 {NULL, NULL, 0, NULL}\r
1802};\r
1803\r
1804int CmdLFT55XX(const char *Cmd) {\r
1805 CmdsParse(CommandTable, Cmd);\r
1806 return 0;\r
1807}\r
1808\r
1809int CmdHelp(const char *Cmd) {\r
1810 CmdsHelp(CommandTable);\r
1811 return 0;\r
1812}\r
Impressum, Datenschutz