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