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