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