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