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