]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlft55xx.c
Remove duplicate #include stdio.h
[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
acf0582d 13#include <ctype.h>\r
14#include <time.h>\r
54a942b0 15#include "proxmark3.h"\r
16#include "ui.h"\r
17#include "graph.h"\r
13d77ef9 18#include "cmdmain.h"\r
54a942b0 19#include "cmdparser.h"\r
20#include "cmddata.h"\r
21#include "cmdlf.h"\r
22#include "cmdlft55xx.h"\r
13d77ef9 23#include "util.h"\r
24#include "data.h"\r
25#include "lfdemod.h"\r
3975d477 26#include "cmdhf14a.h" //for getTagInfo\r
13d77ef9 27\r
709665b5 28#define T55x7_CONFIGURATION_BLOCK 0x00\r
29#define T55x7_PAGE0 0x00\r
30#define T55x7_PAGE1 0x01\r
3975d477 31//#define T55x7_PWD 0x00000010\r
be2d41b7 32#define REGULAR_READ_MODE_BLOCK 0xFF\r
13d77ef9 33\r
34// Default configuration\r
7cb8516c 35t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = false, .offset = 0x00, .block0 = 0x00, .Q5 = false };\r
13d77ef9 36\r
9f669cb2 37t55xx_conf_block_t Get_t55xx_Config(){\r
38 return config;\r
39}\r
40void Set_t55xx_Config(t55xx_conf_block_t conf){\r
41 config = conf;\r
42}\r
43\r
13d77ef9 44int usage_t55xx_config(){\r
f6650679 45 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>] [Q5]");\r
be2d41b7 46 PrintAndLog("Options:");\r
13d77ef9 47 PrintAndLog(" h This help");\r
f6650679 48 PrintAndLog(" b <8|16|32|40|50|64|100|128> Set bitrate");\r
4ff341ef 49 PrintAndLog(" d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa> Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A");\r
f6650679 50 PrintAndLog(" i [1] Invert data signal, defaults to normal");\r
51 PrintAndLog(" o [offset] Set offset, where data should start decode in bitstream");\r
52 PrintAndLog(" Q5 Set as Q5(T5555) chip instead of T55x7");\r
d1869c33 53 PrintAndLog(" ST Set Sequence Terminator on");\r
13d77ef9 54 PrintAndLog("");\r
55 PrintAndLog("Examples:");\r
56 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");\r
57 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");\r
58 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");\r
59 PrintAndLog("");\r
60 return 0;\r
61}\r
62int usage_t55xx_read(){\r
0c8200f1 63 PrintAndLog("Usage: lf t55xx read [b <block>] [p <password>] <override_safety> <page1>");\r
be2d41b7 64 PrintAndLog("Options:");\r
0c8200f1 65 PrintAndLog(" b <block> - block number to read. Between 0-7");\r
66 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");\r
67 PrintAndLog(" o - OPTIONAL override safety check");\r
68 PrintAndLog(" 1 - OPTIONAL read Page 1 instead of Page 0");\r
f1b74c30 69 PrintAndLog(" ****WARNING****");\r
70 PrintAndLog(" Use of read with password on a tag not configured for a pwd");\r
71 PrintAndLog(" can damage the tag");\r
185e038c 72 PrintAndLog("");\r
13d77ef9 73 PrintAndLog("Examples:");\r
0c8200f1 74 PrintAndLog(" lf t55xx read b 0 - read data from block 0");\r
be2d41b7 75 PrintAndLog(" lf t55xx read b 0 p feedbeef - read data from block 0 password feedbeef");\r
76 PrintAndLog(" lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check");\r
13d77ef9 77 PrintAndLog("");\r
78 return 0;\r
79}\r
80int usage_t55xx_write(){\r
72c5877a 81 PrintAndLog("Usage: lf t55xx write [b <block>] [d <data>] [p <password>] [1]");\r
be2d41b7 82 PrintAndLog("Options:");\r
0c8200f1 83 PrintAndLog(" b <block> - block number to write. Between 0-7");\r
84 PrintAndLog(" d <data> - 4 bytes of data to write (8 hex characters)");\r
85 PrintAndLog(" p <password> - OPTIONAL password 4bytes (8 hex characters)");\r
86 PrintAndLog(" 1 - OPTIONAL write Page 1 instead of Page 0");\r
185e038c 87 PrintAndLog("");\r
13d77ef9 88 PrintAndLog("Examples:");\r
72c5877a 89 PrintAndLog(" lf t55xx write b 3 d 11223344 - write 11223344 to block 3");\r
90 PrintAndLog(" lf t55xx write b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef");\r
13d77ef9 91 PrintAndLog("");\r
92 return 0;\r
93}\r
94int usage_t55xx_trace() {\r
95 PrintAndLog("Usage: lf t55xx trace [1]");\r
be2d41b7 96 PrintAndLog("Options:");\r
0c8200f1 97 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");\r
13d77ef9 98 PrintAndLog("");\r
99 PrintAndLog("Examples:");\r
100 PrintAndLog(" lf t55xx trace");\r
101 PrintAndLog(" lf t55xx trace 1");\r
102 PrintAndLog("");\r
103 return 0;\r
104}\r
105int usage_t55xx_info() {\r
106 PrintAndLog("Usage: lf t55xx info [1]");\r
be2d41b7 107 PrintAndLog("Options:");\r
0c8200f1 108 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");\r
13d77ef9 109 PrintAndLog("");\r
110 PrintAndLog("Examples:");\r
111 PrintAndLog(" lf t55xx info");\r
112 PrintAndLog(" lf t55xx info 1");\r
113 PrintAndLog("");\r
114 return 0;\r
115}\r
116int usage_t55xx_dump(){\r
be2d41b7 117 PrintAndLog("Usage: lf t55xx dump <password> [o]");\r
118 PrintAndLog("Options:");\r
0c8200f1 119 PrintAndLog(" <password> - OPTIONAL password 4bytes (8 hex symbols)");\r
120 PrintAndLog(" o - OPTIONAL override, force pwd read despite danger to card");\r
13d77ef9 121 PrintAndLog("");\r
122 PrintAndLog("Examples:");\r
123 PrintAndLog(" lf t55xx dump");\r
be2d41b7 124 PrintAndLog(" lf t55xx dump feedbeef o");\r
13d77ef9 125 PrintAndLog("");\r
126 return 0;\r
127}\r
128int usage_t55xx_detect(){\r
ab5ffe3b 129 PrintAndLog("Usage: lf t55xx detect [1] [p <password>]");\r
be2d41b7 130 PrintAndLog("Options:");\r
ab5ffe3b 131 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");\r
132 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");\r
13d77ef9 133 PrintAndLog("");\r
134 PrintAndLog("Examples:");\r
135 PrintAndLog(" lf t55xx detect");\r
136 PrintAndLog(" lf t55xx detect 1");\r
ab5ffe3b 137 PrintAndLog(" lf t55xx detect p 11223344");\r
13d77ef9 138 PrintAndLog("");\r
139 return 0;\r
140}\r
be2d41b7 141int usage_t55xx_wakup(){\r
142 PrintAndLog("Usage: lf t55xx wakeup [h] p <password>");\r
143 PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards.");\r
144 PrintAndLog("Options:");\r
145 PrintAndLog(" h - this help");\r
146 PrintAndLog(" p <password> - password 4bytes (8 hex symbols)");\r
147 PrintAndLog("");\r
148 PrintAndLog("Examples:");\r
149 PrintAndLog(" lf t55xx wakeup p 11223344 - send wakeup password");\r
150 return 0;\r
151}\r
506672c4 152int usage_t55xx_bruteforce(){\r
51923aca 153 PrintAndLog("This command uses A) bruteforce to scan a number range");\r
154 PrintAndLog(" B) a dictionary attack");\r
506672c4 155 PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");\r
156 PrintAndLog(" password must be 4 bytes (8 hex symbols)");\r
157 PrintAndLog("Options:");\r
51923aca 158 PrintAndLog(" h - this help");\r
159 PrintAndLog(" <start_pwd> - 4 byte hex value to start pwd search at");\r
160 PrintAndLog(" <end_pwd> - 4 byte hex value to end pwd search at");\r
161 PrintAndLog(" i <*.dic> - loads a default keys dictionary file <*.dic>");\r
506672c4 162 PrintAndLog("");\r
163 PrintAndLog("Examples:");\r
164 PrintAndLog(" lf t55xx bruteforce aaaaaaaa bbbbbbbb");\r
51923aca 165 PrintAndLog(" lf t55xx bruteforce i default_pwd.dic");\r
506672c4 166 PrintAndLog("");\r
167 return 0;\r
168}\r
5490c2d6 169int usage_t55xx_wipe(){\r
170 PrintAndLog("Usage: lf t55xx wipe [h] [Q5]");\r
171 PrintAndLog("This commands wipes a tag, fills blocks 1-7 with zeros and a default configuration block");\r
172 PrintAndLog("Options:");\r
173 PrintAndLog(" h - this help");\r
174 PrintAndLog(" Q5 - indicates to use the T5555 (Q5) default configuration block");\r
175 PrintAndLog("");\r
176 PrintAndLog("Examples:");\r
177 PrintAndLog(" lf t55xx wipe - wipes a t55x7 tag, config block 0x000880E0");\r
178 PrintAndLog(" lf t55xx wipe Q5 - wipes a t5555 Q5 tag, config block 0x6001F004");\r
179 return 0;\r
180}\r
181\r
54a942b0 182\r
183static int CmdHelp(const char *Cmd);\r
184\r
709665b5 185void printT5xxHeader(uint8_t page){\r
506672c4 186 PrintAndLog("Reading Page %d:", page);\r
709665b5 187 PrintAndLog("blk | hex data | binary");\r
188 PrintAndLog("----+----------+---------------------------------"); \r
189}\r
190\r
13d77ef9 191int CmdT55xxSetConfig(const char *Cmd) {\r
54a942b0 192\r
13d77ef9 193 uint8_t offset = 0;\r
13d77ef9 194 char modulation[5] = {0x00};\r
195 char tmp = 0x00;\r
196 uint8_t bitRate = 0;\r
197 uint8_t rates[9] = {8,16,32,40,50,64,100,128,0};\r
8e99ec25 198 uint8_t cmdp = 0;\r
7cb8516c 199 bool errors = false;\r
13d77ef9 200 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)\r
201 {\r
202 tmp = param_getchar(Cmd, cmdp);\r
203 switch(tmp)\r
204 {\r
205 case 'h':\r
206 case 'H':\r
207 return usage_t55xx_config();\r
208 case 'b':\r
209 errors |= param_getdec(Cmd, cmdp+1, &bitRate);\r
210 if ( !errors){\r
211 uint8_t i = 0;\r
212 for (; i < 9; i++){\r
213 if (rates[i]==bitRate) {\r
214 config.bitrate = i;\r
215 break;\r
216 }\r
217 }\r
7cb8516c 218 if (i==9) errors = true;\r
13d77ef9 219 }\r
220 cmdp+=2;\r
221 break;\r
222 case 'd':\r
223 param_getstr(Cmd, cmdp+1, modulation);\r
224 cmdp += 2;\r
54a942b0 225\r
2767fc02 226 if ( strcmp(modulation, "FSK" ) == 0) {\r
13d77ef9 227 config.modulation = DEMOD_FSK;\r
2767fc02 228 } else if ( strcmp(modulation, "FSK1" ) == 0) {\r
13d77ef9 229 config.modulation = DEMOD_FSK1;\r
2767fc02 230 config.inverted=1;\r
231 } else if ( strcmp(modulation, "FSK1a" ) == 0) {\r
13d77ef9 232 config.modulation = DEMOD_FSK1a;\r
2767fc02 233 config.inverted=0;\r
234 } else if ( strcmp(modulation, "FSK2" ) == 0) {\r
13d77ef9 235 config.modulation = DEMOD_FSK2;\r
2767fc02 236 config.inverted=0;\r
237 } else if ( strcmp(modulation, "FSK2a" ) == 0) {\r
13d77ef9 238 config.modulation = DEMOD_FSK2a;\r
2767fc02 239 config.inverted=1;\r
240 } else if ( strcmp(modulation, "ASK" ) == 0) {\r
13d77ef9 241 config.modulation = DEMOD_ASK;\r
2767fc02 242 } else if ( strcmp(modulation, "NRZ" ) == 0) {\r
13d77ef9 243 config.modulation = DEMOD_NRZ;\r
2767fc02 244 } else if ( strcmp(modulation, "PSK1" ) == 0) {\r
13d77ef9 245 config.modulation = DEMOD_PSK1;\r
2767fc02 246 } else if ( strcmp(modulation, "PSK2" ) == 0) {\r
13d77ef9 247 config.modulation = DEMOD_PSK2;\r
2767fc02 248 } else if ( strcmp(modulation, "PSK3" ) == 0) {\r
13d77ef9 249 config.modulation = DEMOD_PSK3;\r
2767fc02 250 } else if ( strcmp(modulation, "BIa" ) == 0) {\r
13d77ef9 251 config.modulation = DEMOD_BIa;\r
2767fc02 252 config.inverted=1;\r
253 } else if ( strcmp(modulation, "BI" ) == 0) {\r
13d77ef9 254 config.modulation = DEMOD_BI;\r
2767fc02 255 config.inverted=0;\r
256 } else {\r
13d77ef9 257 PrintAndLog("Unknown modulation '%s'", modulation);\r
7cb8516c 258 errors = true;\r
13d77ef9 259 }\r
260 break;\r
261 case 'i':\r
262 config.inverted = param_getchar(Cmd,cmdp+1) == '1';\r
263 cmdp+=2;\r
264 break;\r
265 case 'o':\r
266 errors |= param_getdec(Cmd, cmdp+1, &offset);\r
267 if ( !errors )\r
268 config.offset = offset;\r
269 cmdp+=2;\r
270 break;\r
f6650679 271 case 'Q':\r
272 case 'q': \r
7cb8516c 273 config.Q5 = true;\r
f6650679 274 cmdp++;\r
275 break;\r
d1869c33 276 case 'S':\r
277 case 's': \r
7cb8516c 278 config.ST = true;\r
d1869c33 279 cmdp++;\r
280 break;\r
13d77ef9 281 default:\r
282 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
7cb8516c 283 errors = true;\r
13d77ef9 284 break;\r
285 }\r
286 }\r
54a942b0 287\r
13d77ef9 288 // No args\r
be2d41b7 289 if (cmdp == 0) return printConfiguration( config );\r
290\r
13d77ef9 291 //Validations\r
be2d41b7 292 if (errors) return usage_t55xx_config();\r
54a942b0 293\r
be2d41b7 294 config.block0 = 0;\r
295 return printConfiguration ( config );\r
296}\r
297\r
298int T55xxReadBlock(uint8_t block, bool page1, bool usepwd, bool override, uint32_t password){\r
299 //Password mode\r
300 if ( usepwd ) {\r
301 // try reading the config block and verify that PWD bit is set before doing this!\r
302 if ( !override ) {\r
709665b5 303 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0 ) ) return 0;\r
be2d41b7 304 if ( !tryDetectModulation() ) {\r
305 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits.");\r
306 return 0;\r
307 } else {\r
308 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password..."); \r
309 usepwd = false;\r
310 page1 = false;\r
311 }\r
312 } else {\r
313 PrintAndLog("Safety Check Overriden - proceeding despite risk");\r
314 }\r
315 }\r
316\r
317 if (!AquireData(page1, block, usepwd, password) ) return 0;\r
318 if (!DecodeT55xxBlock()) return 0;\r
319\r
320 char blk[10]={0};\r
321 sprintf(blk,"%d", block);\r
322 printT55xxBlock(blk); \r
323 return 1;\r
13d77ef9 324}\r
54a942b0 325\r
13d77ef9 326int CmdT55xxReadBlock(const char *Cmd) {\r
be2d41b7 327 uint8_t block = REGULAR_READ_MODE_BLOCK;\r
328 uint32_t password = 0; //default to blank Block 7\r
329 bool usepwd = false;\r
330 bool override = false;\r
331 bool page1 = false;\r
8e99ec25 332 bool errors = false;\r
be2d41b7 333 uint8_t cmdp = 0;\r
8e99ec25 334 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
335 switch(param_getchar(Cmd, cmdp)) {\r
336 case 'h':\r
337 case 'H':\r
338 return usage_t55xx_read();\r
339 case 'b':\r
340 case 'B':\r
341 errors |= param_getdec(Cmd, cmdp+1, &block);\r
be2d41b7 342 cmdp += 2;\r
8e99ec25 343 break;\r
344 case 'o':\r
345 case 'O':\r
be2d41b7 346 override = true;\r
8e99ec25 347 cmdp++;\r
348 break;\r
349 case 'p':\r
350 case 'P':\r
be2d41b7 351 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
352 usepwd = true;\r
353 cmdp += 2;\r
8e99ec25 354 break;\r
be2d41b7 355 case '1':\r
356 page1 = true;\r
8e99ec25 357 cmdp++;\r
358 break;\r
359 default:\r
360 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
361 errors = true;\r
362 break;\r
363 }\r
364 }\r
365 if (errors) return usage_t55xx_read();\r
13d77ef9 366\r
be2d41b7 367 if (block > 7 && block != REGULAR_READ_MODE_BLOCK ) {\r
368 PrintAndLog("Block must be between 0 and 7");\r
369 return 0;\r
8e99ec25 370 }\r
709665b5 371\r
372 printT5xxHeader(page1);\r
be2d41b7 373 return T55xxReadBlock(block, page1, usepwd, override, password);\r
54a942b0 374}\r
375\r
13d77ef9 376bool DecodeT55xxBlock(){\r
377 \r
fef74fdc 378 char buf[30] = {0x00};\r
13d77ef9 379 char *cmdStr = buf;\r
380 int ans = 0;\r
d1869c33 381 bool ST = config.ST;\r
13d77ef9 382 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};\r
13d77ef9 383 DemodBufferLen = 0x00;\r
54a942b0 384\r
13d77ef9 385 switch( config.modulation ){\r
386 case DEMOD_FSK:\r
224ce36e 387 snprintf(cmdStr, sizeof(buf),"%d %d", bitRate[config.bitrate], config.inverted );\r
7cb8516c 388 ans = FSKrawDemod(cmdStr, false);\r
13d77ef9 389 break;\r
390 case DEMOD_FSK1:\r
13d77ef9 391 case DEMOD_FSK1a:\r
224ce36e 392 snprintf(cmdStr, sizeof(buf),"%d %d 8 5", bitRate[config.bitrate], config.inverted );\r
7cb8516c 393 ans = FSKrawDemod(cmdStr, false);\r
13d77ef9 394 break;\r
395 case DEMOD_FSK2:\r
13d77ef9 396 case DEMOD_FSK2a:\r
224ce36e 397 snprintf(cmdStr, sizeof(buf),"%d %d 10 8", bitRate[config.bitrate], config.inverted );\r
7cb8516c 398 ans = FSKrawDemod(cmdStr, false);\r
13d77ef9 399 break;\r
400 case DEMOD_ASK:\r
db829602 401 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );\r
7cb8516c 402 ans = ASKDemod_ext(cmdStr, false, false, 1, &ST);\r
13d77ef9 403 break;\r
404 case DEMOD_PSK1:\r
db829602 405 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
534678c3 406 save_restoreGB(1);\r
db829602 407 CmdLtrim("160");\r
408 snprintf(cmdStr, sizeof(buf),"%d %d 6", bitRate[config.bitrate], config.inverted );\r
7cb8516c 409 ans = PSKDemod(cmdStr, false);\r
534678c3 410 //undo trim samples\r
411 save_restoreGB(0);\r
13d77ef9 412 break;\r
2767fc02 413 case DEMOD_PSK2: //inverted won't affect this\r
414 case DEMOD_PSK3: //not fully implemented\r
db829602 415 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
534678c3 416 save_restoreGB(1);\r
db829602 417 CmdLtrim("160");\r
418 snprintf(cmdStr, sizeof(buf),"%d 0 6", bitRate[config.bitrate] );\r
7cb8516c 419 ans = PSKDemod(cmdStr, false);\r
13d77ef9 420 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
534678c3 421 //undo trim samples\r
422 save_restoreGB(0);\r
13d77ef9 423 break;\r
424 case DEMOD_NRZ:\r
224ce36e 425 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );\r
7cb8516c 426 ans = NRZrawDemod(cmdStr, false);\r
13d77ef9 427 break;\r
428 case DEMOD_BI:\r
13d77ef9 429 case DEMOD_BIa:\r
db829602 430 snprintf(cmdStr, sizeof(buf),"0 %d %d 1", bitRate[config.bitrate], config.inverted );\r
7cb8516c 431 ans = ASKbiphaseDemod(cmdStr, false);\r
13d77ef9 432 break;\r
433 default:\r
7cb8516c 434 return false;\r
13d77ef9 435 }\r
436 return (bool) ans;\r
437}\r
54a942b0 438\r
5490c2d6 439bool DecodeT5555TraceBlock() {\r
440 DemodBufferLen = 0x00;\r
441 \r
442 // According to datasheet. Always: RF/64, not inverted, Manchester\r
7cb8516c 443 return (bool) ASKDemod("64 0 1", false, false, 1);\r
5490c2d6 444}\r
445\r
13d77ef9 446int CmdT55xxDetect(const char *Cmd){\r
7cb8516c 447 bool errors = false;\r
448 bool useGB = false;\r
449 bool usepwd = false;\r
ab5ffe3b 450 uint32_t password = 0;\r
451 uint8_t cmdp = 0;\r
54a942b0 452\r
ab5ffe3b 453 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {\r
454 switch(param_getchar(Cmd, cmdp)) {\r
455 case 'h':\r
456 case 'H':\r
457 return usage_t55xx_detect();\r
458 case 'p':\r
459 case 'P':\r
460 password = param_get32ex(Cmd, cmdp+1, 0, 16);\r
7cb8516c 461 usepwd = true;\r
ab5ffe3b 462 cmdp += 2;\r
463 break;\r
464 case '1':\r
465 // use Graphbuffer data\r
7cb8516c 466 useGB = true;\r
ab5ffe3b 467 cmdp++;\r
468 break;\r
469 default:\r
470 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
471 errors = true;\r
472 break;\r
473 }\r
709665b5 474 }\r
ab5ffe3b 475 if (errors) return usage_t55xx_detect();\r
13d77ef9 476 \r
ab5ffe3b 477 if ( !useGB) {\r
478 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password) )\r
479 return 0;\r
709665b5 480 }\r
ab5ffe3b 481 \r
13d77ef9 482 if ( !tryDetectModulation() )\r
483 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
484\r
be2d41b7 485 return 1;\r
54a942b0 486}\r
487\r
13d77ef9 488// detect configuration?\r
489bool tryDetectModulation(){\r
13d77ef9 490 uint8_t hits = 0;\r
491 t55xx_conf_block_t tests[15];\r
fef74fdc 492 int bitRate=0;\r
322f7eb1 493 uint8_t fc1 = 0, fc2 = 0, clk=0;\r
7cb8516c 494 if (GetFskClock("", false, false)){ \r
495 fskClocks(&fc1, &fc2, &clk, false);\r
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
f6650679 500 else if (fc1==10 && fc2 == 8)\r
13d77ef9 501 tests[hits].modulation = DEMOD_FSK2;\r
fef74fdc 502 tests[hits].bitrate = bitRate;\r
7cb8516c 503 tests[hits].inverted = false;\r
13d77ef9 504 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 505 tests[hits].ST = false;\r
13d77ef9 506 ++hits;\r
507 }\r
7cb8516c 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
7cb8516c 515 tests[hits].inverted = true;\r
13d77ef9 516 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 517 tests[hits].ST = false;\r
13d77ef9 518 ++hits;\r
519 }\r
520 } else {\r
7cb8516c 521 clk = GetAskClock("", false, false);\r
322f7eb1 522 if (clk>0) {\r
7cb8516c 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
7cb8516c 527 tests[hits].inverted = false;\r
322f7eb1 528 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
529 ++hits;\r
530 }\r
7cb8516c 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
7cb8516c 535 tests[hits].inverted = true;\r
322f7eb1 536 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
537 ++hits;\r
538 }\r
7cb8516c 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
7cb8516c 542 tests[hits].inverted = false;\r
322f7eb1 543 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 544 tests[hits].ST = false;\r
322f7eb1 545 ++hits;\r
546 }\r
7cb8516c 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
7cb8516c 550 tests[hits].inverted = true;\r
322f7eb1 551 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 552 tests[hits].ST = false;\r
322f7eb1 553 ++hits;\r
554 }\r
13d77ef9 555 }\r
322f7eb1 556 //undo trim from ask\r
506672c4 557 //save_restoreGB(0);\r
7cb8516c 558 clk = GetNrzClock("", false, false);\r
322f7eb1 559 if (clk>0) {\r
7cb8516c 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
7cb8516c 563 tests[hits].inverted = false;\r
322f7eb1 564 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 565 tests[hits].ST = false;\r
322f7eb1 566 ++hits;\r
567 }\r
54a942b0 568\r
7cb8516c 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
7cb8516c 572 tests[hits].inverted = true;\r
322f7eb1 573 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 574 tests[hits].ST = false;\r
322f7eb1 575 ++hits;\r
576 }\r
13d77ef9 577 }\r
578 \r
506672c4 579 // allow undo\r
db829602 580 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)\r
506672c4 581 save_restoreGB(1);\r
db829602 582 CmdLtrim("160");\r
7cb8516c 583 clk = GetPskClock("", false, false);\r
322f7eb1 584 if (clk>0) {\r
7cb8516c 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
7cb8516c 588 tests[hits].inverted = false;\r
13d77ef9 589 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 590 tests[hits].ST = false;\r
13d77ef9 591 ++hits;\r
592 }\r
7cb8516c 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
7cb8516c 596 tests[hits].inverted = true;\r
13d77ef9 597 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 598 tests[hits].ST = false;\r
13d77ef9 599 ++hits;\r
600 }\r
322f7eb1 601 // PSK2 - needs a call to psk1TOpsk2.\r
7cb8516c 602 if ( PSKDemod("0 0 6", false)) {\r
322f7eb1 603 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
f6650679 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
7cb8516c 607 tests[hits].inverted = false;\r
322f7eb1 608 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 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
7cb8516c 614 if ( PSKDemod("0 0 6", false)) {\r
322f7eb1 615 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
f6650679 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
7cb8516c 619 tests[hits].inverted = false;\r
322f7eb1 620 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
7cb8516c 621 tests[hits].ST = false;\r
322f7eb1 622 ++hits;\r
623 }\r
624 } // inverse waves does not affect this demod\r
13d77ef9 625 }\r
506672c4 626 //undo trim samples\r
627 save_restoreGB(0);\r
6fe5c94b 628 } \r
13d77ef9 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
6ca1477c 635 config.Q5 = tests[0].Q5;\r
d1869c33 636 config.ST = tests[0].ST;\r
13d77ef9 637 printConfiguration( config );\r
7cb8516c 638 return true;\r
13d77ef9 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
7cb8516c 648 return false;\r
13d77ef9 649}\r
650\r
651bool testModulation(uint8_t mode, uint8_t modread){\r
652 switch( mode ){\r
653 case DEMOD_FSK:\r
7cb8516c 654 if (modread >= DEMOD_FSK1 && modread <= DEMOD_FSK2a) return true;\r
13d77ef9 655 break;\r
656 case DEMOD_ASK:\r
7cb8516c 657 if (modread == DEMOD_ASK) return true;\r
13d77ef9 658 break;\r
659 case DEMOD_PSK1:\r
7cb8516c 660 if (modread == DEMOD_PSK1) return true;\r
13d77ef9 661 break;\r
662 case DEMOD_PSK2:\r
7cb8516c 663 if (modread == DEMOD_PSK2) return true;\r
13d77ef9 664 break;\r
665 case DEMOD_PSK3:\r
7cb8516c 666 if (modread == DEMOD_PSK3) return true;\r
13d77ef9 667 break;\r
668 case DEMOD_NRZ:\r
7cb8516c 669 if (modread == DEMOD_NRZ) return true;\r
13d77ef9 670 break;\r
671 case DEMOD_BI:\r
7cb8516c 672 if (modread == DEMOD_BI) return true;\r
13d77ef9 673 break;\r
674 case DEMOD_BIa:\r
7cb8516c 675 if (modread == DEMOD_BIa) return true;\r
13d77ef9 676 break; \r
677 default:\r
7cb8516c 678 return false;\r
13d77ef9 679 }\r
7cb8516c 680 return false;\r
13d77ef9 681}\r
682\r
f6650679 683bool testQ5Modulation(uint8_t mode, uint8_t modread){\r
684 switch( mode ){\r
13d77ef9 685 case DEMOD_FSK:\r
7cb8516c 686 if (modread >= 4 && modread <= 5) return true;\r
13d77ef9 687 break;\r
688 case DEMOD_ASK:\r
7cb8516c 689 if (modread == 0) return true;\r
13d77ef9 690 break;\r
691 case DEMOD_PSK1:\r
7cb8516c 692 if (modread == 1) return true;\r
f6650679 693 break;\r
13d77ef9 694 case DEMOD_PSK2:\r
7cb8516c 695 if (modread == 2) return true;\r
f6650679 696 break;\r
13d77ef9 697 case DEMOD_PSK3:\r
7cb8516c 698 if (modread == 3) return true;\r
13d77ef9 699 break;\r
700 case DEMOD_NRZ:\r
7cb8516c 701 if (modread == 7) return true;\r
f6650679 702 break;\r
703 case DEMOD_BI:\r
7cb8516c 704 if (modread == 6) return true;\r
13d77ef9 705 break;\r
13d77ef9 706 default:\r
7cb8516c 707 return false;\r
13d77ef9 708 }\r
7cb8516c 709 return false;\r
13d77ef9 710}\r
711\r
af5384bc 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
f6650679 721bool testQ5(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk){\r
722\r
7cb8516c 723 if ( DemodBufferLen < 64 ) return false;\r
f6650679 724 uint8_t si = 0;\r
db829602 725 for (uint8_t idx = 28; idx < 64; idx++){\r
f6650679 726 si = idx;\r
db829602 727 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;\r
f6650679 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
6ca1477c 732 if (safer != 0x6 && safer != 0x9) continue;\r
f6650679 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
6ca1477c 737 int bitRate = PackBits(si, 6, DemodBuffer)*2 + 2; si += 6; //bit rate\r
db829602 738 if (bitRate > 128 || bitRate < 8) continue;\r
f6650679 739\r
db829602 740 //uint8_t AOR = PackBits(si, 1, DemodBuffer); si += 1; \r
f6650679 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
db829602 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
f6650679 747 //uint8_t ST = PackBits(si, 1, DemodBuffer); si += 1;\r
db829602 748 if (maxBlk == 0) continue;\r
f6650679 749 //test modulation\r
750 if (!testQ5Modulation(mode, modread)) continue;\r
751 if (bitRate != clk) continue;\r
af5384bc 752 *fndBitRate = convertQ5bitRate(bitRate);\r
753 if (*fndBitRate < 0) continue;\r
f6650679 754 *offset = idx;\r
755\r
7cb8516c 756 return true;\r
f6650679 757 }\r
7cb8516c 758 return false;\r
f6650679 759}\r
760\r
761bool testBitRate(uint8_t readRate, uint8_t clk){\r
db829602 762 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};\r
f6650679 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
7cb8516c 771 if ( DemodBufferLen < 64 ) return false;\r
13d77ef9 772 uint8_t si = 0;\r
db829602 773 for (uint8_t idx = 28; idx < 64; idx++){\r
13d77ef9 774 si = idx;\r
db829602 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
db829602 784 int bitRate = PackBits(si, 3, DemodBuffer); si += 3; //bit rate\r
fef74fdc 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
35cfcfc7 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
7cb8516c 793 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? true : false;\r
13d77ef9 794\r
795 if (!extMode){\r
35cfcfc7 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
f6650679 800 if (!testBitRate(bitRate, clk)) continue;\r
fef74fdc 801 *fndBitRate = bitRate;\r
2767fc02 802 *offset = idx;\r
7cb8516c 803 *Q5 = false;\r
804 return true;\r
f6650679 805 }\r
806 if (testQ5(mode, offset, fndBitRate, clk)) {\r
7cb8516c 807 *Q5 = true;\r
808 return true;\r
13d77ef9 809 }\r
7cb8516c 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
828 bits[i - config.offset]=DemodBuffer[i];\r
93507a33 829\r
13d77ef9 830 blockData = PackBits(0, 32, bits);\r
db829602 831\r
be2d41b7 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
be2d41b7 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
be2d41b7 849 PrintAndLog(" %02d | 0x%08X | %s",j , blockData, sprint_bin(bits,32)); \r
13d77ef9 850 }\r
851 return 0;\r
852}\r
853\r
be2d41b7 854int printConfiguration( t55xx_conf_block_t b){\r
f6650679 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
8e90cd41 860 PrintAndLog("Seq. Term. : %s", (b.ST) ? "Yes" : "No" );\r
13d77ef9 861 PrintAndLog("Block0 : 0x%08X", b.block0);\r
862 PrintAndLog("");\r
be2d41b7 863 return 0;\r
13d77ef9 864}\r
865\r
be2d41b7 866int CmdT55xxWakeUp(const char *Cmd) {\r
867 uint32_t password = 0;\r
868 uint8_t cmdp = 0;\r
869 bool errors = true;\r
870 while(param_getchar(Cmd, cmdp) != 0x00) {\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, 0xFFFFFFFF, 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
13d77ef9 886 }\r
be2d41b7 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
896int CmdT55xxWriteBlock(const char *Cmd) {\r
897 uint8_t block = 0xFF; //default to invalid block\r
709665b5 898 uint32_t data = 0; //default to blank Block \r
899 uint32_t password = 0; //default to blank Block 7\r
be2d41b7 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
be2d41b7 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
be2d41b7 941 return 0;\r
13d77ef9 942 }\r
943 \r
944 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};\r
976627d5 945 UsbCommand resp;\r
be2d41b7 946 c.d.asBytes[0] = (page1) ? 0x2 : 0; \r
13d77ef9 947\r
709665b5 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
be2d41b7 954 if (usepwd) {\r
13d77ef9 955 c.arg[2] = password;\r
be2d41b7 956 c.d.asBytes[0] |= 0x1; \r
13d77ef9 957 }\r
40c5f342 958 clearCommandBuffer();\r
13d77ef9 959 SendCommand(&c);\r
976627d5
MHS
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
be2d41b7 962 return 0;\r
976627d5 963 }\r
be2d41b7 964 return 1;\r
54a942b0 965}\r
966\r
be2d41b7 967int CmdT55xxReadTrace(const char *Cmd) {\r
13d77ef9 968 char cmdp = param_getchar(Cmd, 0);\r
be2d41b7 969 bool pwdmode = false;\r
970 uint32_t password = 0;\r
13d77ef9 971 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') \r
972 return usage_t55xx_trace();\r
973\r
974 if (strlen(Cmd)==0)\r
709665b5 975 if ( !AquireData( T55x7_PAGE1, REGULAR_READ_MODE_BLOCK, pwdmode, password ) )\r
be2d41b7 976 return 0;\r
be2d41b7 977\r
5490c2d6 978 if ( config.Q5 ) {\r
979 if (!DecodeT5555TraceBlock()) return 0;\r
980 } else {\r
981 if (!DecodeT55xxBlock()) return 0;\r
982 }\r
983\r
984 if ( !DemodBufferLen ) return 0;\r
54a942b0 985\r
13d77ef9 986 RepaintGraphWindow();\r
5490c2d6 987 uint8_t repeat = (config.offset > 5) ? 32 : 0;\r
988\r
13d77ef9 989 uint8_t si = config.offset+repeat;\r
be2d41b7 990 uint32_t bl1 = PackBits(si, 32, DemodBuffer);\r
991 uint32_t bl2 = PackBits(si+32, 32, DemodBuffer);\r
224ce36e 992\r
5490c2d6 993 if (config.Q5) {\r
994 uint32_t hdr = PackBits(si, 9, DemodBuffer); si += 9;\r
995\r
996 if (hdr != 0x1FF) {\r
997 PrintAndLog("Invalid Q5 Trace data header (expected 0x1FF, found %X)", hdr);\r
998 return 0;\r
999 }\r
1000\r
1001 t5555_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .icr = 0, .lotidc = '?', .lotid = 0, .wafer = 0, .dw =0};\r
1002\r
1003 data.icr = PackBits(si, 2, DemodBuffer); si += 2;\r
1004 data.lotidc = 'Z' - PackBits(si, 2, DemodBuffer); si += 3;\r
1005\r
1006 data.lotid = PackBits(si, 4, DemodBuffer); si += 5;\r
1007 data.lotid <<= 4;\r
1008 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1009 data.lotid <<= 4;\r
1010 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1011 data.lotid <<= 4;\r
1012 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;\r
1013 data.lotid <<= 1;\r
1014 data.lotid |= PackBits(si, 1, DemodBuffer); si += 1;\r
1015\r
1016 data.wafer = PackBits(si, 3, DemodBuffer); si += 4;\r
1017 data.wafer <<= 2;\r
1018 data.wafer |= PackBits(si, 2, DemodBuffer); si += 2;\r
1019\r
1020 data.dw = PackBits(si, 2, DemodBuffer); si += 3;\r
1021 data.dw <<= 4;\r
1022 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1023 data.dw <<= 4;\r
1024 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1025 data.dw <<= 4;\r
1026 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;\r
1027\r
1028 printT5555Trace(data, repeat);\r
1029\r
1030 } else {\r
1031\r
1032 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
1033 \r
1034 data.acl = PackBits(si, 8, DemodBuffer); si += 8;\r
1035 if ( data.acl != 0xE0 ) {\r
1036 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");\r
1037 return 0;\r
1038 }\r
1039\r
1040 data.mfc = PackBits(si, 8, DemodBuffer); si += 8;\r
1041 data.cid = PackBits(si, 5, DemodBuffer); si += 5;\r
1042 data.icr = PackBits(si, 3, DemodBuffer); si += 3;\r
1043 data.year = PackBits(si, 4, DemodBuffer); si += 4;\r
1044 data.quarter = PackBits(si, 2, DemodBuffer); si += 2;\r
1045 data.lotid = PackBits(si, 14, DemodBuffer); si += 14;\r
1046 data.wafer = PackBits(si, 5, DemodBuffer); si += 5;\r
1047 data.dw = PackBits(si, 15, DemodBuffer); \r
1048\r
1049 time_t t = time(NULL);\r
1050 struct tm tm = *localtime(&t);\r
1051 if ( data.year > tm.tm_year-110)\r
1052 data.year += 2000;\r
1053 else\r
1054 data.year += 2010;\r
1055\r
1056 printT55x7Trace(data, repeat);\r
224ce36e 1057 }\r
5490c2d6 1058 return 0;\r
1059}\r
1060\r
1061void printT55x7Trace( t55x7_tracedata_t data, uint8_t repeat ){\r
1062 PrintAndLog("-- T55x7 Trace Information ----------------------------------");\r
13d77ef9 1063 PrintAndLog("-------------------------------------------------------------");\r
5490c2d6 1064 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", data.acl, data.acl);\r
1065 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", data.mfc, data.mfc, getTagInfo(data.mfc));\r
1066 PrintAndLog(" CID : 0x%02X (%d) - %s", data.cid, data.cid, GetModelStrFromCID(data.cid));\r
1067 PrintAndLog(" ICR IC Revision : %d", data.icr );\r
13d77ef9 1068 PrintAndLog(" Manufactured");\r
5490c2d6 1069 PrintAndLog(" Year/Quarter : %d/%d", data.year, data.quarter);\r
1070 PrintAndLog(" Lot ID : %d", data.lotid );\r
1071 PrintAndLog(" Wafer number : %d", data.wafer);\r
1072 PrintAndLog(" Die Number : %d", data.dw);\r
13d77ef9 1073 PrintAndLog("-------------------------------------------------------------");\r
1074 PrintAndLog(" Raw Data - Page 1");\r
5490c2d6 1075 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );\r
1076 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );\r
1077 PrintAndLog("-------------------------------------------------------------"); \r
54a942b0 1078\r
13d77ef9 1079 /*\r
1080 TRACE - BLOCK O\r
1081 Bits Definition HEX\r
1082 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0 \r
1083 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation\r
1084 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2 \r
1085 22-24 ICR IC revision\r
1086 25-28 YEAR (BCD encoded) 9 (= 2009)\r
1087 29-30 QUARTER 1,2,3,4 \r
1088 31-32 LOT ID\r
1089 \r
1090 TRACE - BLOCK 1\r
1091 1-12 LOT ID \r
1092 13-17 Wafer number\r
1093 18-32 DW, die number sequential\r
1094 */\r
5490c2d6 1095}\r
1096\r
1097void printT5555Trace( t5555_tracedata_t data, uint8_t repeat ){\r
1098 PrintAndLog("-- T5555 (Q5) Trace Information -----------------------------");\r
1099 PrintAndLog("-------------------------------------------------------------");\r
1100 PrintAndLog(" ICR IC Revision : %d", data.icr ); \r
1101 PrintAndLog(" Lot : %c%d", data.lotidc, data.lotid);\r
1102 PrintAndLog(" Wafer number : %d", data.wafer);\r
1103 PrintAndLog(" Die Number : %d", data.dw);\r
1104 PrintAndLog("-------------------------------------------------------------");\r
1105 PrintAndLog(" Raw Data - Page 1");\r
1106 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );\r
1107 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );\r
13d77ef9 1108 \r
5490c2d6 1109 /*\r
1110 ** Q5 **\r
1111 TRACE - BLOCK O and BLOCK1\r
1112 Bits Definition HEX\r
1113 1-9 Header 0x1FF\r
1114 10-11 IC Revision\r
1115 12-13 Lot ID char\r
1116 15-35 Lot ID (NB parity)\r
1117 36-41 Wafer number (NB parity)\r
1118 42-58 DW, die number sequential (NB parity)\r
1119 60-63 Parity bits\r
1120 64 Always zero\r
1121 */\r
54a942b0 1122}\r
1123\r
5490c2d6 1124//need to add Q5 info...\r
13d77ef9 1125int CmdT55xxInfo(const char *Cmd){\r
1126 /*\r
1127 Page 0 Block 0 Configuration data.\r
1128 Normal mode\r
1129 Extended mode\r
1130 */\r
be2d41b7 1131 bool pwdmode = false;\r
1132 uint32_t password = 0;\r
13d77ef9 1133 char cmdp = param_getchar(Cmd, 0);\r
1134\r
1135 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')\r
1136 return usage_t55xx_info();\r
1137 \r
1138 if (strlen(Cmd)==0)\r
709665b5 1139 if ( !AquireData( T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, pwdmode, password ) )\r
be2d41b7 1140 return 1;\r
fef74fdc 1141\r
13d77ef9 1142 if (!DecodeT55xxBlock()) return 1;\r
1143\r
5490c2d6 1144 // too little space to start with\r
fef74fdc 1145 if ( DemodBufferLen < 32) return 1;\r
13d77ef9 1146\r
1147 uint8_t si = config.offset;\r
1148 uint32_t bl0 = PackBits(si, 32, DemodBuffer);\r
1149 \r
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
5490c2d6 1164 \r
db829602 1165 if (config.Q5) PrintAndLog("*** Warning *** Config Info read off a Q5 will not display as expected");\r
13d77ef9 1166 PrintAndLog("");\r
5490c2d6 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
be2d41b7 1193 uint32_t password = 0;\r
13d77ef9 1194 char cmdp = param_getchar(Cmd, 0);\r
be2d41b7 1195 bool override = false;\r
1196 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_dump();\r
1197\r
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
709665b5 1205 printT5xxHeader(0);\r
1206 for ( uint8_t i = 0; i <8; ++i)\r
be2d41b7 1207 T55xxReadBlock(i, 0, usepwd, override, password);\r
709665b5 1208\r
1209 printT5xxHeader(1);\r
1210 for ( uint8_t i = 0; i<4; i++)\r
be2d41b7 1211 T55xxReadBlock(i, 1, usepwd, override, password); \r
709665b5 1212\r
be2d41b7 1213 return 1;\r
13d77ef9 1214}\r
1215\r
be2d41b7 1216int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){\r
709665b5 1217 // arg0 bitmodes:\r
1218 // bit0 = pwdmode\r
1219 // bit1 = page to read from\r
be2d41b7 1220 uint8_t arg0 = (page<<1) | pwdmode;\r
1221 UsbCommand c = {CMD_T55XX_READ_BLOCK, {arg0, block, password}};\r
13d77ef9 1222\r
40c5f342 1223 clearCommandBuffer();\r
13d77ef9 1224 SendCommand(&c);\r
1225 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
1226 PrintAndLog("command execution time out");\r
be2d41b7 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
be2d41b7 1233 setGraphBuf(got, sizeof(got));\r
1234 return 1;\r
13d77ef9 1235}\r
1236\r
af5384bc 1237char * GetBitRateStr(uint32_t id) {\r
1238 static char buf[25];\r
fef74fdc 1239\r
13d77ef9 1240 char *retStr = buf;\r
af5384bc 1241 switch (id) {\r
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
1251 }\r
13d77ef9 1252 return buf;\r
1253}\r
1254\r
af5384bc 1255char * GetSaferStr(uint32_t id) {\r
13d77ef9 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
5490c2d6 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
5490c2d6 1307 switch (id) {\r
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
1321 }\r
13d77ef9 1322 return buf;\r
1323}\r
1324\r
1325uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){\r
1326 \r
1327 int i = start;\r
1328 int j = len-1;\r
1329\r
1330 if (len > 32) return 0;\r
1331\r
1332 uint32_t tmp = 0;\r
1333 for (; j >= 0; --j, ++i)\r
1334 tmp |= bits[i] << j;\r
1335\r
1336 return tmp;\r
1337}\r
1338\r
66837a03 1339int CmdResetRead(const char *Cmd) {\r
1340 UsbCommand c = {CMD_T55XX_RESET_READ, {0,0,0}};\r
1341\r
1342 clearCommandBuffer();\r
1343 SendCommand(&c);\r
1344 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
1345 PrintAndLog("command execution time out");\r
1346 return 0;\r
1347 }\r
1348\r
9f669cb2 1349 uint8_t got[BIGBUF_SIZE-1];\r
66837a03 1350 GetFromBigBuf(got,sizeof(got),0);\r
1351 WaitForResponse(CMD_ACK,NULL);\r
1352 setGraphBuf(got, sizeof(got));\r
1353 return 1;\r
1354}\r
1355\r
f6650679 1356int CmdT55xxWipe(const char *Cmd) {\r
1357 char writeData[20] = {0};\r
1358 char *ptrData = writeData;\r
709665b5 1359\r
5490c2d6 1360 char cmdp = param_getchar(Cmd, 0); \r
1361 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wipe();\r
1362\r
1363 bool Q5 = (cmdp == 'q' || cmdp == 'Q');\r
1364\r
1365 // Try with the default password to reset block 0\r
1366 // With a pwd should work even if pwd bit not set\r
f6650679 1367 PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n");\r
709665b5 1368\r
5490c2d6 1369 if ( Q5 ){\r
1370 snprintf(ptrData,sizeof(writeData),"b 0 d 6001F004 p 0");\r
1371 } else {\r
1372 snprintf(ptrData,sizeof(writeData),"b 0 d 00088040 p 0");\r
1373 }\r
709665b5 1374\r
5490c2d6 1375 if (!CmdT55xxWriteBlock(ptrData)) PrintAndLog("Error writing blk 0");\r
709665b5 1376\r
1377 for (uint8_t blk = 1; blk<8; blk++) {\r
f6650679 1378 snprintf(ptrData,sizeof(writeData),"b %d d 0", blk);\r
709665b5 1379 if (!CmdT55xxWriteBlock(ptrData))\r
f6650679 1380 PrintAndLog("Error writing blk %d", blk);\r
709665b5 1381\r
534678c3 1382 memset(writeData, 0x00, sizeof(writeData));\r
f6650679 1383 }\r
1384 return 0;\r
1385}\r
1386\r
506672c4 1387int CmdT55xxBruteForce(const char *Cmd) {\r
1388\r
1389 // load a default pwd file.\r
1390 char buf[9];\r
1391 char filename[FILE_PATH_SIZE]={0};\r
1392 int keycnt = 0;\r
735136e6 1393 int ch;\r
506672c4 1394 uint8_t stKeyBlock = 20;\r
735136e6 1395 uint8_t *keyBlock = NULL, *p = NULL;\r
506672c4 1396 uint32_t start_password = 0x00000000; //start password\r
1397 uint32_t end_password = 0xFFFFFFFF; //end password\r
1398 bool found = false;\r
1399\r
1400 char cmdp = param_getchar(Cmd, 0);\r
1401 if (cmdp == 'h' || cmdp == 'H') return usage_t55xx_bruteforce();\r
1402\r
735136e6 1403 keyBlock = calloc(stKeyBlock, 6);\r
1404 if (keyBlock == NULL) return 1;\r
1405\r
506672c4 1406 if (cmdp == 'i' || cmdp == 'I') {\r
1407\r
1408 int len = strlen(Cmd+2);\r
1409 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;\r
1410 memcpy(filename, Cmd+2, len);\r
1411\r
1412 FILE * f = fopen( filename , "r");\r
1413\r
1414 if ( !f ) {\r
1415 PrintAndLog("File: %s: not found or locked.", filename);\r
1416 free(keyBlock);\r
1417 return 1;\r
1418 }\r
1419\r
5490c2d6 1420 while( fgets(buf, sizeof(buf), f) ) {\r
506672c4 1421 if (strlen(buf) < 8 || buf[7] == '\n') continue;\r
1422\r
1423 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
1424\r
1425 //The line start with # is comment, skip\r
1426 if( buf[0]=='#' ) continue;\r
1427\r
5490c2d6 1428 if (!isxdigit(buf[0])) {\r
506672c4 1429 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);\r
1430 continue;\r
1431 }\r
1432 \r
1433 buf[8] = 0;\r
1434\r
1435 if ( stKeyBlock - keycnt < 2) {\r
1436 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1437 if (!p) {\r
1438 PrintAndLog("Cannot allocate memory for defaultKeys");\r
1439 free(keyBlock);\r
735136e6 1440 fclose(f);\r
506672c4 1441 return 2;\r
1442 }\r
1443 keyBlock = p;\r
1444 }\r
1445 memset(keyBlock + 4 * keycnt, 0, 4);\r
1446 num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);\r
1447 PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));\r
1448 keycnt++;\r
1449 memset(buf, 0, sizeof(buf));\r
1450 }\r
1451 fclose(f);\r
1452 \r
1453 if (keycnt == 0) {\r
1454 PrintAndLog("No keys found in file");\r
735136e6 1455 free(keyBlock);\r
506672c4 1456 return 1;\r
1457 }\r
1458 PrintAndLog("Loaded %d keys", keycnt);\r
1459 \r
1460 // loop\r
1461 uint64_t testpwd = 0x00;\r
1462 for (uint16_t c = 0; c < keycnt; ++c ) {\r
1463\r
1464 if (ukbhit()) {\r
735136e6 1465 ch = getchar();\r
1466 (void)ch;\r
506672c4 1467 printf("\naborted via keyboard!\n");\r
735136e6 1468 free(keyBlock);\r
506672c4 1469 return 0;\r
1470 }\r
1471\r
1472 testpwd = bytes_to_num(keyBlock + 4*c, 4);\r
1473\r
1474 PrintAndLog("Testing %08X", testpwd);\r
1475\r
7cb8516c 1476 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, testpwd)) {\r
506672c4 1477 PrintAndLog("Aquireing data from device failed. Quitting");\r
735136e6 1478 free(keyBlock);\r
506672c4 1479 return 0;\r
1480 }\r
1481\r
1482 found = tryDetectModulation();\r
1483\r
1484 if ( found ) {\r
1485 PrintAndLog("Found valid password: [%08X]", testpwd);\r
735136e6 1486 free(keyBlock);\r
506672c4 1487 return 0;\r
1488 }\r
1489 }\r
1490 PrintAndLog("Password NOT found.");\r
735136e6 1491 free(keyBlock);\r
506672c4 1492 return 0;\r
1493 }\r
1494\r
1495 // Try to read Block 7, first :)\r
1496\r
1497 // incremental pwd range search\r
1498 start_password = param_get32ex(Cmd, 0, 0, 16);\r
1499 end_password = param_get32ex(Cmd, 1, 0, 16);\r
5490c2d6 1500\r
735136e6 1501 if ( start_password >= end_password ) {\r
1502 free(keyBlock);\r
1503 return usage_t55xx_bruteforce();\r
1504 }\r
506672c4 1505 PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);\r
1506\r
1507 uint32_t i = start_password;\r
1508\r
5490c2d6 1509 while ((!found) && (i <= end_password)) {\r
506672c4 1510\r
5490c2d6 1511 printf(".");\r
1512 fflush(stdout);\r
1513 if (ukbhit()) {\r
735136e6 1514 ch = getchar();\r
1515 (void)ch;\r
5490c2d6 1516 printf("\naborted via keyboard!\n");\r
735136e6 1517 free(keyBlock);\r
5490c2d6 1518 return 0;\r
1519 }\r
506672c4 1520\r
7cb8516c 1521 if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, i)) {\r
5490c2d6 1522 PrintAndLog("Aquireing data from device failed. Quitting");\r
735136e6 1523 free(keyBlock);\r
5490c2d6 1524 return 0;\r
1525 }\r
1526 found = tryDetectModulation();\r
1527\r
1528 if (found) break;\r
1529 i++;\r
506672c4 1530 }\r
1531\r
1532 PrintAndLog("");\r
1533\r
1534 if (found)\r
5490c2d6 1535 PrintAndLog("Found valid password: [%08x]", i);\r
506672c4 1536 else\r
5490c2d6 1537 PrintAndLog("Password NOT found. Last tried: [%08x]", --i);\r
735136e6 1538\r
1539 free(keyBlock);\r
506672c4 1540 return 0;\r
1541}\r
1542\r
709665b5 1543static command_t CommandTable[] = {\r
506672c4 1544 {"help", CmdHelp, 1, "This help"},\r
1545 {"bruteforce",CmdT55xxBruteForce,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"},\r
1546 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},\r
1547 {"detect", CmdT55xxDetect, 1, "[1] Try detecting the tag modulation from reading the configuration block."},\r
1548 {"read", CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},\r
1549 {"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
1550 {"write", CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},\r
1551 {"trace", CmdT55xxReadTrace, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},\r
1552 {"info", CmdT55xxInfo, 1, "[1] Show T55x7 configuration data (page 0/ blk 0)"},\r
1553 {"dump", CmdT55xxDump, 0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},\r
1554 {"special", special, 0, "Show block changes with 64 different offsets"},\r
1555 {"wakeup", CmdT55xxWakeUp, 0, "Send AOR wakeup command"},\r
5490c2d6 1556 {"wipe", CmdT55xxWipe, 0, "[q] Wipe a T55xx tag and set defaults (will destroy any data on tag)"},\r
54a942b0 1557 {NULL, NULL, 0, NULL}\r
1558};\r
1559\r
709665b5 1560int CmdLFT55XX(const char *Cmd) {\r
54a942b0 1561 CmdsParse(CommandTable, Cmd);\r
1562 return 0;\r
1563}\r
1564\r
709665b5 1565int CmdHelp(const char *Cmd) {\r
54a942b0 1566 CmdsHelp(CommandTable);\r
1567 return 0;\r
1568}\r
Impressum, Datenschutz