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