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