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