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