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