]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlft55xx.c
FIX: narrowed down the detect modulation even further.
[proxmark3-svn] / client / cmdlft55xx.c
CommitLineData
54a942b0 1//-----------------------------------------------------------------------------\r
2//\r
3// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
4// at your option, any later version. See the LICENSE.txt file for the text of\r
5// the license.\r
6//-----------------------------------------------------------------------------\r
7// Low frequency T55xx commands\r
8//-----------------------------------------------------------------------------\r
9\r
10#include <stdio.h>\r
11#include <string.h>\r
12#include <inttypes.h>\r
54a942b0 13#include "proxmark3.h"\r
14#include "ui.h"\r
15#include "graph.h"\r
f38a1528 16#include "cmdmain.h"\r
54a942b0 17#include "cmdparser.h"\r
18#include "cmddata.h"\r
19#include "cmdlf.h"\r
20#include "cmdlft55xx.h"\r
f38a1528 21#include "util.h"\r
22#include "data.h"\r
c4e3b1b6 23#include "lfdemod.h"\r
83a42ef9 24#include "../common/crc.h"\r
db693638 25#include "../common/iso14443crc.h"\r
8713b64f 26#include "cmdhf14a.h"\r
54a942b0 27\r
78e87580 28#define CONFIGURATION_BLOCK 0x00\r
29#define TRACE_BLOCK 0x01\r
30\r
db693638 31// Default configuration\r
78e87580 32t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = FALSE, .offset = 0x00, .block0 = 0x00};\r
83a42ef9 33\r
34int usage_t55xx_config(){\r
db693638 35 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>]");\r
78e87580 36 PrintAndLog("Options:");\r
37 PrintAndLog(" h This help");\r
38 PrintAndLog(" d <FSK|ASK|PSK1|PSK2|PSK3|NRZ|BI> Set demodulation");\r
39 PrintAndLog(" i [1] Inverse data signal, defaults to normal");\r
40 PrintAndLog(" o [offset] Set offset where data should start decode in bitstream");\r
118bfa1b 41 PrintAndLog("");\r
83a42ef9 42 PrintAndLog("Examples:");\r
db693638 43 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");\r
44 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");\r
78e87580 45 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");\r
118bfa1b 46 PrintAndLog("");\r
83a42ef9 47 return 0;\r
48}\r
68008fb5 49int usage_t55xx_read(){\r
50 PrintAndLog("Usage: lf t55xx read <block> <password>");\r
4ecde0e1 51 PrintAndLog(" <block>, block number to read. Between 0-7");\r
52 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");\r
53 PrintAndLog("");\r
118bfa1b 54 PrintAndLog("Examples:");\r
55 PrintAndLog(" lf t55xx read 0 - read data from block 0");\r
56 PrintAndLog(" lf t55xx read 0 feedbeef - read data from block 0 password feedbeef");\r
4ecde0e1 57 PrintAndLog("");\r
58 return 0;\r
59}\r
68008fb5 60int usage_t55xx_write(){\r
4ecde0e1 61 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");\r
62 PrintAndLog(" <block>, block number to read. Between 0-7");\r
63 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");\r
64 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");\r
65 PrintAndLog("");\r
118bfa1b 66 PrintAndLog("Examples:");\r
67 PrintAndLog(" lf t55xx wd 3 11223344 - write 11223344 to block 3");\r
68 PrintAndLog(" lf t55xx wd 3 11223344 feedbeef - write 11223344 to block 3 password feedbeef");\r
4ecde0e1 69 PrintAndLog("");\r
70 return 0;\r
71}\r
72int usage_t55xx_trace() {\r
118bfa1b 73 PrintAndLog("Usage: lf t55xx trace [1]");\r
4ecde0e1 74 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");\r
75 PrintAndLog("");\r
118bfa1b 76 PrintAndLog("Examples:");\r
77 PrintAndLog(" lf t55xx trace");\r
78 PrintAndLog(" lf t55xx trace 1");\r
4ecde0e1 79 PrintAndLog("");\r
80 return 0;\r
81}\r
82int usage_t55xx_info() {\r
118bfa1b 83 PrintAndLog("Usage: lf t55xx info [1]");\r
4ecde0e1 84 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");\r
85 PrintAndLog("");\r
118bfa1b 86 PrintAndLog("Examples:");\r
87 PrintAndLog(" lf t55xx info");\r
88 PrintAndLog(" lf t55xx info 1");\r
4ecde0e1 89 PrintAndLog("");\r
90 return 0;\r
91}\r
4ecde0e1 92int usage_t55xx_dump(){\r
93 PrintAndLog("Usage: lf t55xx dump <password>");\r
118bfa1b 94 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex symbols)");\r
4ecde0e1 95 PrintAndLog("");\r
118bfa1b 96 PrintAndLog("Examples:");\r
97 PrintAndLog(" lf t55xx dump");\r
98 PrintAndLog(" lf t55xx dump feedbeef");\r
4ecde0e1 99 PrintAndLog("");\r
100 return 0;\r
101}\r
33add187 102int usage_t55xx_detect(){\r
78e87580 103 PrintAndLog("Usage: lf t55xx detect [1]");\r
104 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");\r
33add187 105 PrintAndLog("");\r
106 PrintAndLog("Examples:");\r
107 PrintAndLog(" lf t55xx detect");\r
108 PrintAndLog(" lf t55xx detect 1");\r
109 PrintAndLog("");\r
110 return 0;\r
111}\r
0310364d 112\r
4ecde0e1 113static int CmdHelp(const char *Cmd);\r
c4e3b1b6 114\r
78e87580 115int CmdT55xxSetConfig(const char *Cmd) {\r
118bfa1b 116\r
db693638 117 uint8_t offset = 0;\r
118bfa1b 118 bool errors = FALSE;\r
119 uint8_t cmdp = 0;\r
8713b64f 120 char modulation[5] = {0x00};\r
545158b3 121 char tmp = 0x00;\r
83a42ef9 122 \r
118bfa1b 123 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)\r
83a42ef9 124 {\r
545158b3 125 tmp = param_getchar(Cmd, cmdp);\r
126 switch(tmp)\r
118bfa1b 127 {\r
128 case 'h':\r
129 case 'H':\r
130 return usage_t55xx_config();\r
131 case 'd':\r
545158b3 132 param_getstr(Cmd, cmdp+1, modulation);\r
133 cmdp += 2;\r
134 \r
8713b64f 135 if ( strcmp(modulation, "FSK" ) == 0)\r
136 config.modulation = DEMOD_FSK;\r
137 else if ( strcmp(modulation, "ASK" ) == 0)\r
138 config.modulation = DEMOD_ASK;\r
139 else if ( strcmp(modulation, "NRZ" ) == 0)\r
140 config.modulation = DEMOD_NRZ;\r
141 else if ( strcmp(modulation, "PSK1" ) == 0)\r
142 config.modulation = DEMOD_PSK1;\r
143 else if ( strcmp(modulation, "PSK2" ) == 0)\r
144 config.modulation = DEMOD_PSK2;\r
78e87580 145 else if ( strcmp(modulation, "PSK3" ) == 0)\r
146 config.modulation = DEMOD_PSK3; \r
8713b64f 147 else if ( strcmp(modulation, "BI" ) == 0)\r
148 config.modulation = DEMOD_BI;\r
118bfa1b 149 else {\r
150 PrintAndLog("Unknown modulation '%s'", modulation);\r
151 errors = TRUE;\r
152 }\r
153 break;\r
154 case 'i':\r
78e87580 155 config.inverted = param_getchar(Cmd,cmdp+1) == '1';\r
118bfa1b 156 cmdp+=2;\r
157 break;\r
db693638 158 case 'o':\r
159 errors |= param_getdec(Cmd, cmdp+1,&offset);\r
545158b3 160 if ( !errors )\r
161 config.offset = offset;\r
162 cmdp += 2;\r
db693638 163 break;\r
118bfa1b 164 default:\r
165 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r
166 errors = TRUE;\r
167 break;\r
168 }\r
83a42ef9 169 }\r
8713b64f 170\r
118bfa1b 171 // No args\r
172 if (cmdp == 0) {\r
71020824 173 printConfiguration( config );\r
118bfa1b 174 return 0;\r
175 }\r
176 //Validations\r
177 if (errors)\r
178 return usage_t55xx_config();\r
78e87580 179 \r
118bfa1b 180 config.block0 = 0;\r
545158b3 181 printConfiguration( config );\r
83a42ef9 182 return 0;\r
183}\r
83a42ef9 184\r
78e87580 185int CmdT55xxReadBlock(const char *Cmd) {\r
c6be64da 186 int block = -1;\r
4ecde0e1 187 int password = 0xFFFFFFFF; //default to blank Block 7\r
83a42ef9 188\r
4ecde0e1 189 char cmdp = param_getchar(Cmd, 0);\r
83a42ef9 190 if (cmdp == 'h' || cmdp == 'H')\r
191 return usage_t55xx_read();\r
54a942b0 192\r
4ecde0e1 193 int res = sscanf(Cmd, "%d %x", &block, &password);\r
54a942b0 194\r
7b40affb 195 if ( res < 1 || res > 2 )\r
196 return usage_t55xx_read();\r
197\r
4ecde0e1 198 \r
199 if ((block < 0) | (block > 7)) {\r
b44e5233 200 PrintAndLog("Block must be between 0 and 7");\r
201 return 1;\r
4ecde0e1 202 } \r
54a942b0 203\r
4ecde0e1 204 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};\r
205 c.d.asBytes[0] = 0x0; \r
54a942b0 206\r
4ecde0e1 207 //Password mode\r
208 if ( res == 2 ) {\r
209 c.arg[2] = password;\r
210 c.d.asBytes[0] = 0x1; \r
c4e3b1b6 211 }\r
54a942b0 212\r
b44e5233 213 SendCommand(&c);\r
68008fb5 214 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
c4e3b1b6 215 PrintAndLog("command execution time out");\r
385f3987 216 return 2;\r
c4e3b1b6 217 }\r
f38a1528 218 \r
68008fb5 219 uint8_t got[12000];\r
220 GetFromBigBuf(got,sizeof(got),0);\r
221 WaitForResponse(CMD_ACK,NULL);\r
68008fb5 222 setGraphBuf(got, 12000);\r
83a42ef9 223\r
33add187 224 DecodeT55xxBlock();\r
33add187 225 printT55xxBlock("");\r
33add187 226 return 0;\r
227}\r
228\r
229void DecodeT55xxBlock(){\r
230 \r
8713b64f 231 char buf[8] = {0x00};\r
33add187 232 char *cmdStr = buf;\r
233\r
8a131214 234 DemodBufferLen = 0x00;\r
235 \r
33add187 236 switch( config.modulation ){\r
8713b64f 237 case DEMOD_FSK:\r
78e87580 238 sprintf(cmdStr,"0 %d", config.inverted );\r
33add187 239 FSKrawDemod(cmdStr, FALSE);\r
240 break;\r
8713b64f 241 case DEMOD_ASK:\r
78e87580 242 sprintf(cmdStr,"0 %d 1", config.inverted );\r
33add187 243 ASKmanDemod(cmdStr, FALSE, FALSE);\r
33add187 244 break;\r
8713b64f 245 case DEMOD_PSK1:\r
78e87580 246 sprintf(cmdStr,"0 %d 1", config.inverted );\r
33add187 247 PSKDemod(cmdStr, FALSE);\r
248 break;\r
8713b64f 249 case DEMOD_PSK2:\r
78e87580 250 sprintf(cmdStr,"0 %d 1", config.inverted );\r
8713b64f 251 PSKDemod(cmdStr, FALSE);\r
252 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
253 break;\r
254 case DEMOD_PSK3:\r
78e87580 255 sprintf(cmdStr,"0 %d 1", config.inverted );\r
8713b64f 256 PSKDemod(cmdStr, FALSE);\r
257 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
258 break;\r
259 case DEMOD_NRZ:\r
78e87580 260 sprintf(cmdStr,"0 %d 1", config.inverted );\r
33add187 261 NRZrawDemod(cmdStr, FALSE);\r
262 break;\r
8713b64f 263 case DEMOD_BI:\r
78e87580 264 sprintf(cmdStr,"0 0 %d 1", config.inverted );\r
265 ASKbiphaseDemod(cmdStr, FALSE);\r
33add187 266 break;\r
267 default:\r
268 return;\r
269 }\r
270}\r
271\r
272int CmdT55xxDetect(const char *Cmd){\r
78e87580 273\r
33add187 274 char cmdp = param_getchar(Cmd, 0);\r
33add187 275 \r
78e87580 276 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') \r
277 return usage_t55xx_detect();\r
33add187 278\r
78e87580 279 if ( strlen(Cmd)==0)\r
280 AquireData(CONFIGURATION_BLOCK);\r
68008fb5 281 \r
78e87580 282 if ( !tryDetectModulation() )\r
7b40affb 283 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
78e87580 284\r
33add187 285 return 0;\r
286}\r
287\r
33add187 288bool tryDetectModulation(){\r
289 \r
290 uint8_t hits = 0;\r
78e87580 291 t55xx_conf_block_t tests[15];\r
33add187 292 \r
33add187 293 if (GetFskClock("", FALSE, FALSE)){ \r
8713b64f 294 if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset)){\r
8a131214 295 tests[hits].modulation = DEMOD_FSK;\r
78e87580 296 tests[hits].inverted = FALSE;\r
297 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
33add187 298 ++hits;\r
299 }\r
8713b64f 300 if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset)) {\r
8a131214 301 tests[hits].modulation = DEMOD_FSK;\r
78e87580 302 tests[hits].inverted = TRUE;\r
303 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
33add187 304 ++hits;\r
7b40affb 305 }\r
83a42ef9 306 } else {\r
8713b64f 307 if ( ASKmanDemod("0 0 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {\r
8a131214 308 tests[hits].modulation = DEMOD_ASK;\r
78e87580 309 tests[hits].inverted = FALSE;\r
310 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
33add187 311 ++hits;\r
7b40affb 312 }\r
33add187 313\r
8713b64f 314 if ( ASKmanDemod("0 1 1", FALSE, FALSE) && test(DEMOD_ASK, &tests[hits].offset)) {\r
8a131214 315 tests[hits].modulation = DEMOD_ASK;\r
78e87580 316 tests[hits].inverted = TRUE;\r
317 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
33add187 318 ++hits;\r
7b40affb 319 }\r
83a42ef9 320 \r
8713b64f 321 if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {\r
322 tests[hits].modulation = DEMOD_NRZ;\r
78e87580 323 tests[hits].inverted = FALSE;\r
324 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
33add187 325 ++hits;\r
326 }\r
83a42ef9 327\r
8713b64f 328 if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset)) {\r
329 tests[hits].modulation = DEMOD_NRZ;\r
78e87580 330 tests[hits].inverted = TRUE;\r
331 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
33add187 332 ++hits;\r
7b40affb 333 }\r
118bfa1b 334 \r
8713b64f 335 if ( PSKDemod("0 0 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {\r
336 tests[hits].modulation = DEMOD_PSK1;\r
78e87580 337 tests[hits].inverted = FALSE;\r
338 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
33add187 339 ++hits;\r
340 }\r
118bfa1b 341 \r
8713b64f 342 if ( PSKDemod("0 1 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset)) {\r
343 tests[hits].modulation = DEMOD_PSK1;\r
78e87580 344 tests[hits].inverted = TRUE;\r
345 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
33add187 346 ++hits;\r
347 }\r
8713b64f 348\r
78e87580 349 // PSK2 - needs a call to psk1TOpsk2.\r
8713b64f 350 if ( PSKDemod("0 0 1", FALSE)) {\r
351 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
352 if (test(DEMOD_PSK2, &tests[hits].offset)){\r
353 tests[hits].modulation = DEMOD_PSK2;\r
78e87580 354 tests[hits].inverted = FALSE;\r
355 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
8713b64f 356 ++hits;\r
357 }\r
358 }\r
78e87580 359 // PSK2 - needs a call to psk1TOpsk2.\r
8713b64f 360 if ( PSKDemod("0 1 1", FALSE)) {\r
361 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
362 if (test(DEMOD_PSK2, &tests[hits].offset)){\r
363 tests[hits].modulation = DEMOD_PSK2;\r
78e87580 364 tests[hits].inverted = TRUE;\r
365 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
8713b64f 366 ++hits;\r
367 }\r
368 }\r
369\r
78e87580 370 // PSK3 - needs a call to psk1TOpsk2.\r
371 if ( PSKDemod("0 0 1", FALSE)) {\r
372 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
373 if (test(DEMOD_PSK3, &tests[hits].offset)){\r
374 tests[hits].modulation = DEMOD_PSK3;\r
375 tests[hits].inverted = FALSE;\r
376 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer); \r
377 ++hits;\r
378 }\r
379 }\r
380 // PSK3 - needs a call to psk1TOpsk2.\r
381 if ( PSKDemod("0 1 1", FALSE)) {\r
382 psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
383 if (test(DEMOD_PSK3, &tests[hits].offset)){\r
384 tests[hits].modulation = DEMOD_PSK3;\r
385 tests[hits].inverted = TRUE;\r
386 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
387 ++hits;\r
388 }\r
389 }\r
390 \r
8713b64f 391 if ( ASKbiphaseDemod("0 0 0 1", FALSE) && test(DEMOD_BI, &tests[hits].offset) ) {\r
392 tests[hits].modulation = DEMOD_BI;\r
78e87580 393 tests[hits].inverted = FALSE;\r
394 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
8713b64f 395 ++hits;\r
396 }\r
397 if ( ASKbiphaseDemod("0 0 1 1", FALSE) && test(DEMOD_BI, &tests[hits].offset) ) {\r
398 tests[hits].modulation = DEMOD_BI;\r
78e87580 399 tests[hits].inverted = TRUE;\r
400 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
8713b64f 401 ++hits;\r
402 }\r
33add187 403 } \r
78e87580 404\r
7b40affb 405 if ( hits == 1) {\r
7b40affb 406 config.modulation = tests[0].modulation;\r
78e87580 407 config.inverted = tests[0].inverted;\r
8713b64f 408 config.offset = tests[0].offset;\r
78e87580 409 \r
410 DecodeT55xxBlock();\r
411 if (DemodBufferLen > config.offset + 32) \r
412 config.block0 = PackBits(config.offset, 32, DemodBuffer);\r
71020824 413 printConfiguration( config );\r
33add187 414 return TRUE;\r
7b40affb 415 }\r
33add187 416 \r
7b40affb 417 if ( hits > 1) {\r
33add187 418 PrintAndLog("Found [%d] possible matches for modulation.",hits);\r
7b40affb 419 for(int i=0; i<hits; ++i){\r
2c5ed706 420 PrintAndLog("--[%d]---------------", i+1);\r
71020824 421 printConfiguration( tests[i] );\r
7b40affb 422 }\r
423 }\r
33add187 424 return FALSE;\r
83a42ef9 425}\r
33add187 426\r
8713b64f 427bool testModulation(uint8_t mode, uint8_t modread){\r
428 switch( mode ){\r
429 case DEMOD_FSK:\r
430 if (modread > 3 && modread < 8) return TRUE;\r
431 break;\r
432 case DEMOD_ASK:\r
433 if (modread == 8) return TRUE;\r
434 break;\r
435 case DEMOD_PSK1:\r
436 if (modread == 1) return TRUE;\r
437 break;\r
438 case DEMOD_PSK2:\r
439 if (modread == 2) return TRUE;\r
440 break;\r
441 case DEMOD_PSK3:\r
442 if (modread == 3) return TRUE;\r
443 break;\r
444 case DEMOD_NRZ:\r
445 if (!modread) return TRUE;\r
446 break;\r
447 case DEMOD_BI:\r
448 if (modread == 16) return TRUE;\r
449 break;\r
450 default:\r
451 return FALSE;\r
452 }\r
453 return FALSE;\r
454}\r
455\r
456bool test(uint8_t mode, uint8_t *offset){\r
3e4811c8 457\r
78e87580 458 if ( !DemodBufferLen) return FALSE;\r
459 \r
460 uint8_t si = 0;\r
461 for (uint8_t idx = 0; idx < 64; ++idx){\r
462 si = idx;\r
463 if ( PackBits(si, 32, DemodBuffer) == 0x00 ) continue;\r
464\r
8713b64f 465 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key\r
466 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode\r
78e87580 467\r
468 // 2nibble must be zeroed.\r
469 // moved test to here, since this gets most faults first.\r
470 if ( resv > 0x00) continue;\r
471\r
8713b64f 472 uint8_t xtRate = PackBits(si, 3, DemodBuffer); si += 3+3; //new\r
473 //uint8_t bitRate = PackBits(si, 3, DemodBuffer); si += 3; //new could check bit rate\r
474 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode\r
475 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1; //new\r
476 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //new could check psk cr\r
477 uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24 , 30, 31 could be tested for 0 if not extended mode\r
478 uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;\r
479 \r
8713b64f 480 //if extended mode\r
78e87580 481 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? TRUE : FALSE;\r
8713b64f 482\r
483 if (!extMode){\r
484 if (nml01 || nml02 || xtRate) continue;\r
485 }\r
486\r
487 //test modulation\r
488 if (!testModulation(mode, modread)) continue;\r
78e87580 489 \r
8713b64f 490 *offset = idx;\r
78e87580 491 return TRUE;\r
3e4811c8 492 }\r
3e4811c8 493 return FALSE;\r
494}\r
83a42ef9 495\r
33add187 496void printT55xxBlock(const char *demodStr){\r
78e87580 497\r
498 uint8_t i = config.offset;\r
499 uint8_t endpos = 32 + i; \r
83a42ef9 500 uint32_t blockData = 0;\r
db693638 501 uint8_t bits[64] = {0x00};\r
78e87580 502\r
503 if ( !DemodBufferLen) return;\r
4e7af352 504 \r
78e87580 505 if ( endpos > DemodBufferLen){\r
506 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);\r
db693638 507 return;\r
508 }\r
78e87580 509\r
510 for (; i < endpos; ++i)\r
511 bits[i - config.offset] = DemodBuffer[i];\r
385f3987 512 \r
db693638 513 blockData = PackBits(0, 32, bits);\r
514 PrintAndLog("0x%08X %s [%s]", blockData, sprint_bin(bits,32), demodStr);\r
515}\r
516\r
517int special(const char *Cmd) {\r
518 uint32_t blockData = 0;\r
545158b3 519 uint8_t bits[32] = {0x00};\r
db693638 520\r
521 PrintAndLog("[OFFSET] [DATA] [BINARY]");\r
522 PrintAndLog("----------------------------------------------------");\r
523 int i,j = 0;\r
78e87580 524 for (; j < 64; ++j){\r
db693638 525 \r
526 for (i = 0; i < 32; ++i)\r
527 bits[i]=DemodBuffer[j+i];\r
528 \r
529 blockData = PackBits(0, 32, bits);\r
78e87580 530\r
531 //char indicate[4] = {0x00};\r
532 // if ( (blockData >> 24) == 0xE0 )\r
533 // sprintf(indicate,"<--");\r
534 //PrintAndLog("[%02d] 0x%08X %s %s",j , blockData, sprint_bin(bits,32), indicate); \r
535 PrintAndLog("[%02d] 0x%08X %s",j , blockData, sprint_bin(bits,32)); \r
db693638 536 }\r
537 \r
538 return 0;\r
54a942b0 539}\r
540\r
71020824 541void printConfiguration( t55xx_conf_block_t b){\r
542 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );\r
78e87580 543 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );\r
db693638 544 PrintAndLog("Offset : %d", b.offset);\r
78e87580 545 PrintAndLog("Block0 : 0x%08X", b.block0);\r
71020824 546 PrintAndLog("");\r
547}\r
548\r
33add187 549int CmdT55xxWriteBlock(const char *Cmd)\r
54a942b0 550{\r
4ecde0e1 551 int block = 8; //default to invalid block\r
552 int data = 0xFFFFFFFF; //default to blank Block \r
553 int password = 0xFFFFFFFF; //default to blank Block 7\r
554 \r
555 char cmdp = param_getchar(Cmd, 0);\r
556 if (cmdp == 'h' || cmdp == 'H') {\r
68008fb5 557 usage_t55xx_write();\r
4ecde0e1 558 return 0;\r
559 }\r
560 \r
561 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);\r
562 \r
563 if ( res < 2 || res > 3) {\r
68008fb5 564 usage_t55xx_write();\r
4ecde0e1 565 return 1;\r
566 }\r
54a942b0 567\r
4ecde0e1 568 if (block > 7) {\r
b44e5233 569 PrintAndLog("Block must be between 0 and 7");\r
570 return 1;\r
f38a1528 571 }\r
4ecde0e1 572 \r
573 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};\r
574 c.d.asBytes[0] = 0x0; \r
54a942b0 575\r
68008fb5 576 PrintAndLog("Writing to T55x7");\r
577 PrintAndLog("block : %d", block);\r
578 PrintAndLog("data : 0x%08X", data);\r
579\r
580 //Password mode\r
581 if (res == 3) {\r
4ecde0e1 582 c.arg[2] = password;\r
583 c.d.asBytes[0] = 0x1; \r
68008fb5 584 PrintAndLog("pwd : 0x%08X", password);\r
4ecde0e1 585 }\r
4ecde0e1 586 SendCommand(&c);\r
587 return 0;\r
54a942b0 588}\r
589\r
33add187 590int CmdT55xxReadTrace(const char *Cmd)\r
54a942b0 591{\r
0310364d 592 char cmdp = param_getchar(Cmd, 0);\r
593 \r
7b40affb 594 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') \r
595 return usage_t55xx_trace();\r
f38a1528 596\r
fbceacc5 597 if ( strlen(Cmd)==0){\r
78e87580 598 AquireData( TRACE_BLOCK );\r
f38a1528 599 }\r
f38a1528 600 \r
7b40affb 601 DecodeT55xxBlock();\r
602\r
78e87580 603 if (!DemodBufferLen) return 1;\r
83a42ef9 604 \r
f6c18637 605 RepaintGraphWindow();\r
8713b64f 606 uint8_t repeat = 0;\r
78e87580 607 if (config.offset > 5) \r
608 repeat = 32;\r
8713b64f 609 uint8_t si = config.offset+repeat;\r
7b40affb 610 uint32_t bl0 = PackBits(si, 32, DemodBuffer);\r
611 uint32_t bl1 = PackBits(si+32, 32, DemodBuffer);\r
612 \r
613 uint32_t acl = PackBits(si, 8, DemodBuffer); si += 8;\r
614 uint32_t mfc = PackBits(si, 8, DemodBuffer); si += 8;\r
615 uint32_t cid = PackBits(si, 5, DemodBuffer); si += 5;\r
616 uint32_t icr = PackBits(si, 3, DemodBuffer); si += 3;\r
617 uint32_t year = PackBits(si, 4, DemodBuffer); si += 4;\r
618 uint32_t quarter = PackBits(si, 2, DemodBuffer); si += 2;\r
619 uint32_t lotid = PackBits(si, 12, DemodBuffer); si += 12;\r
620 uint32_t wafer = PackBits(si, 5, DemodBuffer); si += 5;\r
621 uint32_t dw = PackBits(si, 15, DemodBuffer); \r
622 \r
623 year += 2000;\r
f6c18637 624 \r
625 PrintAndLog("");\r
626 PrintAndLog("-- T55xx Trace Information ----------------------------------");\r
627 PrintAndLog("-------------------------------------------------------------");\r
628 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);\r
8713b64f 629 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", mfc, mfc, getTagInfo(mfc));\r
630 PrintAndLog(" CID : 0x%02X (%d) - %s", cid, cid, GetModelStrFromCID(cid));\r
f6c18637 631 PrintAndLog(" ICR IC Revision : %d",icr );\r
632 PrintAndLog(" Manufactured");\r
7b40affb 633 PrintAndLog(" Year/Quarter : %d/%d",year, quarter );\r
77376577 634 PrintAndLog(" Lot ID : %d", lotid );\r
f6c18637 635 PrintAndLog(" Wafer number : %d", wafer);\r
636 PrintAndLog(" Die Number : %d", dw);\r
637 PrintAndLog("-------------------------------------------------------------");\r
77376577 638 PrintAndLog(" Raw Data - Page 1");\r
8713b64f 639 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset+repeat,32) );\r
640 PrintAndLog(" Block 1 : 0x%08X %s", bl1, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );\r
f6c18637 641 PrintAndLog("-------------------------------------------------------------");\r
78e87580 642 \r
643 if ( acl != 0xE0 ) \r
644 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");\r
f6c18637 645 /*\r
646 TRACE - BLOCK O\r
647 Bits Definition HEX\r
648 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0 \r
649 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation\r
650 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2 \r
651 22-24 ICR IC revision\r
652 25-28 YEAR (BCD encoded) 9 (= 2009)\r
653 29-30 QUARTER 1,2,3,4 \r
77376577 654 31-32 LOT ID\r
f6c18637 655 \r
656 TRACE - BLOCK 1\r
77376577 657 1-12 LOT ID \r
f6c18637 658 13-17 Wafer number\r
659 18-32 DW, die number sequential\r
660 */\r
661 \r
662 return 0;\r
663}\r
f38a1528 664\r
33add187 665int CmdT55xxInfo(const char *Cmd){\r
f6c18637 666 /*\r
667 Page 0 Block 0 Configuration data.\r
668 Normal mode\r
669 Extended mode\r
670 */\r
fbceacc5 671 char cmdp = param_getchar(Cmd, 0);\r
672\r
78e87580 673 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') \r
83a42ef9 674 return usage_t55xx_info();\r
7b40affb 675 \r
78e87580 676 if (strlen(Cmd)==0)\r
677 AquireData( CONFIGURATION_BLOCK );\r
83a42ef9 678 \r
7b40affb 679 DecodeT55xxBlock();\r
8d0a3e87 680\r
78e87580 681 if (!DemodBufferLen) return 1;\r
7b40affb 682 \r
545158b3 683 uint8_t si = config.offset;\r
7b40affb 684 uint32_t bl0 = PackBits(si, 32, DemodBuffer);\r
685 \r
686 uint32_t safer = PackBits(si, 4, DemodBuffer); si += 4; \r
687 uint32_t resv = PackBits(si, 7, DemodBuffer); si += 7;\r
688 uint32_t dbr = PackBits(si, 3, DemodBuffer); si += 3;\r
689 uint32_t extend = PackBits(si, 1, DemodBuffer); si += 1;\r
690 uint32_t datamod = PackBits(si, 5, DemodBuffer); si += 5;\r
691 uint32_t pskcf = PackBits(si, 2, DemodBuffer); si += 2;\r
692 uint32_t aor = PackBits(si, 1, DemodBuffer); si += 1; \r
693 uint32_t otp = PackBits(si, 1, DemodBuffer); si += 1; \r
694 uint32_t maxblk = PackBits(si, 3, DemodBuffer); si += 3;\r
695 uint32_t pwd = PackBits(si, 1, DemodBuffer); si += 1; \r
696 uint32_t sst = PackBits(si, 1, DemodBuffer); si += 1; \r
697 uint32_t fw = PackBits(si, 1, DemodBuffer); si += 1;\r
698 uint32_t inv = PackBits(si, 1, DemodBuffer); si += 1; \r
699 uint32_t por = PackBits(si, 1, DemodBuffer); si += 1;\r
b44e5233 700 \r
f6c18637 701 PrintAndLog("");\r
99a71418 702 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");\r
f6c18637 703 PrintAndLog("-------------------------------------------------------------");\r
704 PrintAndLog(" Safer key : %s", GetSaferStr(safer));\r
705 PrintAndLog(" reserved : %d", resv);\r
706 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));\r
707 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");\r
3e4811c8 708 PrintAndLog(" Modulation : %s", GetModulationStr(datamod));\r
78e87580 709 PrintAndLog(" PSK clock frequency : %d", pskcf);\r
f6c18637 710 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");\r
711 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );\r
712 PrintAndLog(" Max block : %d", maxblk);\r
713 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");\r
714 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");\r
3e4811c8 715 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");\r
f6c18637 716 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");\r
717 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");\r
718 PrintAndLog("-------------------------------------------------------------");\r
77376577 719 PrintAndLog(" Raw Data - Page 0");\r
8713b64f 720 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset,32) );\r
f6c18637 721 PrintAndLog("-------------------------------------------------------------");\r
f6c18637 722 return 0;\r
723}\r
724\r
33add187 725int CmdT55xxDump(const char *Cmd){\r
77376577 726\r
4ecde0e1 727 char s[20] = {0x00};\r
77376577 728 uint8_t pwd[4] = {0x00};\r
54a942b0 729\r
4ecde0e1 730 char cmdp = param_getchar(Cmd, 0);\r
149aeada 731 if ( cmdp == 'h' || cmdp == 'H') {\r
4ecde0e1 732 usage_t55xx_dump();\r
77376577 733 return 0;\r
734 }\r
4ecde0e1 735\r
736 bool hasPwd = ( strlen(Cmd) > 0); \r
77376577 737 if ( hasPwd ){\r
2ae8a312 738 if (param_gethex(Cmd, 0, pwd, 8)) {\r
739 PrintAndLog("password must include 8 HEX symbols");\r
c4e3b1b6 740 return 1;\r
77376577 741 }\r
742 }\r
a501c82b 743 \r
77376577 744 for ( int i = 0; i <8; ++i){\r
149aeada 745 memset(s,0,sizeof(s));\r
77376577 746 if ( hasPwd ) {\r
c6be64da 747 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);\r
77376577 748 } else {\r
749 sprintf(s,"%d", i);\r
77376577 750 }\r
33add187 751 CmdT55xxReadBlock(s);\r
77376577 752 }\r
753 return 0;\r
754}\r
755\r
78e87580 756int AquireData( uint8_t block ){\r
757\r
758 UsbCommand c;\r
759 \r
760 if ( block == CONFIGURATION_BLOCK ) \r
761 c.cmd = CMD_T55XX_READ_BLOCK;\r
762 else if (block == TRACE_BLOCK )\r
763 c.cmd = CMD_T55XX_READ_TRACE;\r
764 \r
765 c.arg[0] = 0x00;\r
766 c.arg[1] = 0x00;\r
767 c.arg[2] = 0x00;\r
768 c.d.asBytes[0] = 0x0; \r
769\r
770 //Password mode\r
771 // if ( res == 2 ) {\r
772 // c.arg[2] = password;\r
773 // c.d.asBytes[0] = 0x1; \r
774 // }\r
775\r
776 SendCommand(&c);\r
777 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {\r
778 PrintAndLog("command execution time out");\r
779 return 1;\r
780 }\r
781\r
782 uint8_t got[12000];\r
783 GetFromBigBuf(got,sizeof(got),0);\r
784 WaitForResponse(CMD_ACK,NULL);\r
785 setGraphBuf(got, 12000);\r
786 return 0;\r
787}\r
788\r
f6c18637 789char * GetBitRateStr(uint32_t id){\r
790 static char buf[40];\r
791 char *retStr = buf;\r
792 switch (id){\r
793 case 0: \r
794 sprintf(retStr,"%d - RF/8",id);\r
795 break;\r
796 case 1:\r
797 sprintf(retStr,"%d - RF/16",id);\r
798 break;\r
799 case 2: \r
800 sprintf(retStr,"%d - RF/32",id);\r
801 break;\r
802 case 3:\r
803 sprintf(retStr,"%d - RF/40",id);\r
804 break;\r
805 case 4:\r
806 sprintf(retStr,"%d - RF/50",id);\r
807 break;\r
808 case 5:\r
809 sprintf(retStr,"%d - RF/64",id);\r
810 break;\r
811 case 6:\r
812 sprintf(retStr,"%d - RF/100",id);\r
813 break;\r
814 case 7:\r
815 sprintf(retStr,"%d - RF/128",id);\r
816 break;\r
817 default:\r
818 sprintf(retStr,"%d - (Unknown)",id);\r
819 break;\r
820 }\r
821\r
822 return buf;\r
823}\r
824\r
f6c18637 825char * GetSaferStr(uint32_t id){\r
826 static char buf[40];\r
827 char *retStr = buf;\r
828 \r
829 sprintf(retStr,"%d",id);\r
830 if (id == 6) {\r
3e4811c8 831 sprintf(retStr,"%d - passwd",id);\r
f6c18637 832 }\r
833 if (id == 9 ){\r
3e4811c8 834 sprintf(retStr,"%d - testmode",id);\r
f6c18637 835 }\r
836 \r
837 return buf;\r
838}\r
839char * GetModulationStr( uint32_t id){\r
840 static char buf[40];\r
841 char *retStr = buf;\r
842 \r
843 switch (id){\r
844 case 0: \r
7bd30f12 845 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);\r
f6c18637 846 break;\r
847 case 1:\r
848 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);\r
849 break;\r
850 case 2: \r
851 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);\r
852 break;\r
853 case 3:\r
854 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);\r
855 break;\r
856 case 4:\r
857 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);\r
858 break;\r
859 case 5:\r
860 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);\r
861 break;\r
862 case 6:\r
863 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);\r
864 break;\r
865 case 7:\r
866 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);\r
867 break;\r
868 case 8:\r
869 sprintf(retStr,"%d - Manschester",id);\r
870 break;\r
871 case 16:\r
872 sprintf(retStr,"%d - Biphase",id);\r
873 break;\r
874 case 17:\r
875 sprintf(retStr,"%d - Reserved",id);\r
876 break;\r
877 default:\r
878 sprintf(retStr,"0x%02X (Unknown)",id);\r
879 break;\r
880 }\r
881 return buf;\r
882}\r
883\r
8713b64f 884char * GetModelStrFromCID(uint32_t cid){\r
885 \r
886 static char buf[10];\r
887 char *retStr = buf;\r
888 \r
889 if (cid == 1) sprintf(retStr,"ATA5577M1");\r
890 if (cid == 2) sprintf(retStr,"ATA5577M2"); \r
891 return buf;\r
892}\r
893\r
71020824 894char * GetSelectedModulationStr( uint8_t id){\r
895\r
896 static char buf[16];\r
897 char *retStr = buf;\r
898 \r
899 switch (id){\r
8a131214 900 case DEMOD_FSK:\r
78e87580 901 sprintf(retStr,"FSK");\r
71020824 902 break;\r
8a131214 903 case DEMOD_ASK: \r
78e87580 904 sprintf(retStr,"ASK");\r
71020824 905 break;\r
8713b64f 906 case DEMOD_NRZ:\r
78e87580 907 sprintf(retStr,"DIRECT/NRZ");\r
71020824 908 break;\r
8713b64f 909 case DEMOD_PSK1:\r
78e87580 910 sprintf(retStr,"PSK1");\r
8713b64f 911 break;\r
912 case DEMOD_PSK2:\r
78e87580 913 sprintf(retStr,"PSK2");\r
71020824 914 break;\r
78e87580 915 case DEMOD_PSK3:\r
916 sprintf(retStr,"PSK3");\r
917 break; \r
8a131214 918 case DEMOD_BI:\r
78e87580 919 sprintf(retStr,"BIPHASE");\r
71020824 920 break;\r
921 default:\r
922 sprintf(retStr,"(Unknown)");\r
923 break;\r
924 }\r
925 return buf;\r
926}\r
927\r
f6c18637 928uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){\r
929 \r
930 int i = start;\r
931 int j = len-1;\r
78e87580 932\r
933 if (len > 32) return 0;\r
934\r
f6c18637 935 uint32_t tmp = 0;\r
78e87580 936 for (; j >= 0; --j, ++i)\r
f6c18637 937 tmp |= bits[i] << j;\r
78e87580 938 \r
f6c18637 939 return tmp;\r
54a942b0 940}\r
941\r
942static command_t CommandTable[] =\r
943{\r
83a42ef9 944 {"help", CmdHelp, 1, "This help"},\r
78e87580 945 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset)"},\r
946 {"detect", CmdT55xxDetect, 0, "[1] Try detecting the tag modulation from reading the configuration block."},\r
33add187 947 {"read", CmdT55xxReadBlock, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},\r
948 {"write", CmdT55xxWriteBlock,0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},\r
949 {"trace", CmdT55xxReadTrace, 0, "[1] Show T55xx traceability data (page 1/ blk 0-1)"},\r
950 {"info", CmdT55xxInfo, 0, "[1] Show T55xx configuration data (page 0/ blk 0)"},\r
951 {"dump", CmdT55xxDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},\r
78e87580 952 {"special", special, 0, "Show block changes with 64 different offsets"},\r
54a942b0 953 {NULL, NULL, 0, NULL}\r
954};\r
955\r
956int CmdLFT55XX(const char *Cmd)\r
957{\r
958 CmdsParse(CommandTable, Cmd);\r
959 return 0;\r
960}\r
961\r
962int CmdHelp(const char *Cmd)\r
963{\r
964 CmdsHelp(CommandTable);\r
965 return 0;\r
966}\r
Impressum, Datenschutz