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