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