1 //-----------------------------------------------------------------------------
3 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
4 // at your option, any later version. See the LICENSE.txt file for the text of
6 //-----------------------------------------------------------------------------
7 // Low frequency T55xx commands
8 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
17 #include "cmdparser.h"
20 #include "cmdlft55xx.h"
24 #include "../common/crc.h"
26 #define LF_TRACE_BUFF_SIZE 20000 // 32 x 32 x 10 (32 bit times numofblock (7), times clock skip..)
27 #define LF_BITSSTREAM_LEN 1000 // more then 1000 bits shouldn't happend.. 8block * 4 bytes * 8bits =
39 // Default configuration: FSK, not inversed.
40 t55xx_conf_block_t config
= { .modulation
= 0, .inversed
= FALSE
, .block0
= 0x00};
42 int usage_t55xx_config(){
43 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1]");
44 PrintAndLog("Options: ");
45 PrintAndLog(" h This help");
46 PrintAndLog(" d <FSK|ASK|PSK|NZ|BI> Set demodulation FSK / ASK / PSK / NZ / Biphase");
47 PrintAndLog(" i [1] Inverse data signal, defaults to normal");
49 PrintAndLog("Examples:");
50 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");
51 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");
55 int usage_t55xx_read(){
56 PrintAndLog("Usage: lf t55xx read <block> <password>");
57 PrintAndLog(" <block>, block number to read. Between 0-7");
58 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");
60 PrintAndLog("Examples:");
61 PrintAndLog(" lf t55xx read 0 - read data from block 0");
62 PrintAndLog(" lf t55xx read 0 feedbeef - read data from block 0 password feedbeef");
66 int usage_t55xx_write(){
67 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");
68 PrintAndLog(" <block>, block number to read. Between 0-7");
69 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");
70 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");
72 PrintAndLog("Examples:");
73 PrintAndLog(" lf t55xx wd 3 11223344 - write 11223344 to block 3");
74 PrintAndLog(" lf t55xx wd 3 11223344 feedbeef - write 11223344 to block 3 password feedbeef");
78 int usage_t55xx_trace() {
79 PrintAndLog("Usage: lf t55xx trace [1]");
80 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
82 PrintAndLog("Examples:");
83 PrintAndLog(" lf t55xx trace");
84 PrintAndLog(" lf t55xx trace 1");
88 int usage_t55xx_info() {
89 PrintAndLog("Usage: lf t55xx info [1]");
90 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
92 PrintAndLog("Examples:");
93 PrintAndLog(" lf t55xx info");
94 PrintAndLog(" lf t55xx info 1");
98 int usage_t55xx_dump(){
99 PrintAndLog("Usage: lf t55xx dump <password>");
100 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex symbols)");
102 PrintAndLog("Examples:");
103 PrintAndLog(" lf t55xx dump");
104 PrintAndLog(" lf t55xx dump feedbeef");
109 static int CmdHelp(const char *Cmd
);
111 int CmdT55xxSetConfig(const char *Cmd
){
117 char modulation
[4] = {0x00};
119 while(param_getchar(Cmd
, cmdp
) != 0x00 && !errors
)
121 switch(param_getchar(Cmd
, cmdp
))
125 return usage_t55xx_config();
127 len
= param_getstr(Cmd
, cmdp
+1, modulation
);
130 if ( strcmp(modulation
, "FSK" ) == 0)
132 else if ( strcmp(modulation
, "ASK" ) == 0)
134 else if ( strcmp(modulation
, "PSK" ) == 0)
136 else if ( strcmp(modulation
, "NZ" ) == 0)
138 else if ( strcmp(modulation
, "BI" ) == 0)
141 PrintAndLog("Unknown modulation '%s'", modulation
);
146 inverse
= param_getchar(Cmd
,cmdp
+1) == '1';
150 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
157 PrintAndLog("Modulation: %d", config
.modulation
);
158 PrintAndLog("Invert : %d", config
.inversed
);
159 PrintAndLog("Block0 : %08X", config
.block0
);
164 return usage_t55xx_config();
166 config
.modulation
= len
;
167 config
.inversed
= inverse
;
171 // detect configuration?
173 int CmdReadBlk(const char *Cmd
)
176 int password
= 0xFFFFFFFF; //default to blank Block 7
178 char cmdp
= param_getchar(Cmd
, 0);
179 if (cmdp
== 'h' || cmdp
== 'H')
180 return usage_t55xx_read();
182 int res
= sscanf(Cmd
, "%d %x", &block
, &password
);
184 if ( res
< 1 || res
> 2 ){
189 if ((block
< 0) | (block
> 7)) {
190 PrintAndLog("Block must be between 0 and 7");
194 UsbCommand c
= {CMD_T55XX_READ_BLOCK
, {0, block
, 0}};
195 c
.d
.asBytes
[0] = 0x0;
200 c
.d
.asBytes
[0] = 0x1;
204 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
205 PrintAndLog("command execution time out");
210 GetFromBigBuf(got
,sizeof(got
),0);
211 WaitForResponse(CMD_ACK
,NULL
);
213 setGraphBuf(got
, 12000);
219 if (GetFskClock("", FALSE
, FALSE
)){ //wave is almost certainly FSK
221 if ( FSKrawDemod("0 0", FALSE
) && test())
224 if ( FSKrawDemod("0 1", FALSE
) && test())
225 printT55xx("FSK inv");
227 // ASK/MAN (autoclock, normal, maxerrors 1)
228 if ( ASKmanDemod("0 0 1", FALSE
, FALSE
) && test())
229 printT55xx("ASK/MAN");
231 // ASK/MAN (autoclock, inverted, maxerrors 1)
232 if ( ASKmanDemod("0 1 1", FALSE
, FALSE
) && test())
233 printT55xx("ASK/MAN Inv");
235 // NZR (autoclock, normal, maxerrors 1)
236 if ( NRZrawDemod("0 0 1", FALSE
) && test())
238 // NZR (autoclock, inverted, maxerrors 1)
239 if ( NRZrawDemod("0 1 1", FALSE
) && test())
240 printT55xx("NZR inv");
242 // PSK (autoclock, normal, maxerrors 1)
243 if ( PSKDemod("0 0 1", FALSE
) && test())
246 // PSK (autoclock, inverted, maxerrors 1)
247 if ( PSKDemod("0 1 1", FALSE
) && test())
248 printT55xx("PSK inv");
252 // if (!BiphaseRawDecode("0",FALSE) && test())
253 // printT55xx("BIPHASE");
255 // if (!BiphaseRawDecode("1",FALSE) && test())
256 // printT55xx("BIPHASE inv");
262 if ( !DemodBufferLen
)
266 uint8_t safer
= PackBits(si
, 4, DemodBuffer
); si
+= 4;
267 uint8_t resv
= PackBits(si
, 7, DemodBuffer
); si
+= 7+3;
268 uint8_t extend
= PackBits(si
, 1, DemodBuffer
); si
+= 1;
270 //PrintAndLog("test: %X %X %X ", safer, resv, extend);
272 // 2nibble must be zeroed.
273 if ( resv
> 0x00) return FALSE
;
275 if ( safer
== 0x6 || safer
== 0x9){
279 if ( resv
== 0x00) return TRUE
;
283 void printT55xx(const char *demodStr
){
285 uint32_t blockData
= 0;
286 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
288 if ( !DemodBufferLen
)
292 for (;i
<DemodBufferLen
;++i
)
293 bits
[i
]=DemodBuffer
[i
];
295 blockData
= PackBits(1, 32, bits
);
296 PrintAndLog("0x%08X %s [%s]", blockData
, sprint_bin(bits
+1,32), demodStr
);
301 size = fskdemod(dest, size, 32, 0, 8, 10); // fsk1 RF/32
302 size = fskdemod(dest, size, 32, 1, 8, 10); // fsk1a RF/32
305 size = fskdemod(dest, size, 32, 0, 10, 8); // fsk2 RF/32
306 size = fskdemod(dest, size, 32, 1, 10, 8); // fsk2a RF/32
307 size = fskdemod(dest, size, 50, 1, 10, 8); // fsk2a RF/50
308 size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64
311 errCnt = pskRawDemod(bits, &bitlen, 32, 0);
313 int CmdWriteBlk(const char *Cmd
)
315 int block
= 8; //default to invalid block
316 int data
= 0xFFFFFFFF; //default to blank Block
317 int password
= 0xFFFFFFFF; //default to blank Block 7
319 char cmdp
= param_getchar(Cmd
, 0);
320 if (cmdp
== 'h' || cmdp
== 'H') {
325 int res
= sscanf(Cmd
, "%d %x %x",&block
, &data
, &password
);
327 if ( res
< 2 || res
> 3) {
333 PrintAndLog("Block must be between 0 and 7");
337 UsbCommand c
= {CMD_T55XX_WRITE_BLOCK
, {data
, block
, 0}};
338 c
.d
.asBytes
[0] = 0x0;
340 PrintAndLog("Writing to T55x7");
341 PrintAndLog("block : %d", block
);
342 PrintAndLog("data : 0x%08X", data
);
347 c
.d
.asBytes
[0] = 0x1;
348 PrintAndLog("pwd : 0x%08X", password
);
354 int CmdReadTrace(const char *Cmd
)
356 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
358 char cmdp
= param_getchar(Cmd
, 0);
360 if (strlen(Cmd
) > 1 || cmdp
== 'h' || cmdp
== 'H') {
365 if ( strlen(Cmd
)==0){
367 UsbCommand c
= {CMD_T55XX_READ_TRACE
, {0, 0, 0}};
369 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
370 PrintAndLog("command execution time out");
374 //CmdSamples("12000");
377 size_t bitlen
= getFromGraphBuf(bits
);
381 RepaintGraphWindow();
384 uint32_t bl0
= PackBits(si
, 32, bits
);
385 uint32_t bl1
= PackBits(si
+32, 32, bits
);
387 uint32_t acl
= PackBits(si
, 8, bits
); si
+= 8;
388 uint32_t mfc
= PackBits(si
, 8, bits
); si
+= 8;
389 uint32_t cid
= PackBits(si
, 5, bits
); si
+= 5;
390 uint32_t icr
= PackBits(si
, 3, bits
); si
+= 3;
391 uint32_t year
= PackBits(si
, 4, bits
); si
+= 4;
392 uint32_t quarter
= PackBits(si
, 2, bits
); si
+= 2;
393 uint32_t lotid
= PackBits(si
, 12, bits
); si
+= 12;
394 uint32_t wafer
= PackBits(si
, 5, bits
); si
+= 5;
395 uint32_t dw
= PackBits(si
, 15, bits
);
398 PrintAndLog("-- T55xx Trace Information ----------------------------------");
399 PrintAndLog("-------------------------------------------------------------");
400 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl
, acl
);
401 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d)", mfc
, mfc
);
402 PrintAndLog(" CID : 0x%02X (%d)", cid
, cid
);
403 PrintAndLog(" ICR IC Revision : %d",icr
);
404 PrintAndLog(" Manufactured");
405 PrintAndLog(" Year/Quarter : %d/%d",2000+year
, quarter
);
406 PrintAndLog(" Lot ID : %d", lotid
);
407 PrintAndLog(" Wafer number : %d", wafer
);
408 PrintAndLog(" Die Number : %d", dw
);
409 PrintAndLog("-------------------------------------------------------------");
410 PrintAndLog(" Raw Data - Page 1");
411 PrintAndLog(" Block 0 : 0x%08X %s", bl0
, sprint_bin(bits
+5,32) );
412 PrintAndLog(" Block 0 : 0x%08X %s", bl1
, sprint_bin(bits
+37,32) );
413 PrintAndLog("-------------------------------------------------------------");
417 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
418 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
419 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
420 22-24 ICR IC revision
421 25-28 YEAR (BCD encoded) 9 (= 2009)
422 29-30 QUARTER 1,2,3,4
428 18-32 DW, die number sequential
434 int CmdInfo(const char *Cmd
){
436 Page 0 Block 0 Configuration data.
440 char cmdp
= param_getchar(Cmd
, 0);
442 if (cmdp
== 'h' || cmdp
== 'H') {
443 return usage_t55xx_info();
450 uint8_t bits
[LF_BITSSTREAM_LEN
] = {0x00};
453 uint32_t bl0
= PackBits(si
, 32, bits
);
455 uint32_t safer
= PackBits(si
, 4, bits
); si
+= 4;
456 uint32_t resv
= PackBits(si
, 7, bits
); si
+= 7;
457 uint32_t dbr
= PackBits(si
, 3, bits
); si
+= 3;
458 uint32_t extend
= PackBits(si
, 1, bits
); si
+= 1;
459 uint32_t datamod
= PackBits(si
, 5, bits
); si
+= 5;
460 uint32_t pskcf
= PackBits(si
, 2, bits
); si
+= 2;
461 uint32_t aor
= PackBits(si
, 1, bits
); si
+= 1;
462 uint32_t otp
= PackBits(si
, 1, bits
); si
+= 1;
463 uint32_t maxblk
= PackBits(si
, 3, bits
); si
+= 3;
464 uint32_t pwd
= PackBits(si
, 1, bits
); si
+= 1;
465 uint32_t sst
= PackBits(si
, 1, bits
); si
+= 1;
466 uint32_t fw
= PackBits(si
, 1, bits
); si
+= 1;
467 uint32_t inv
= PackBits(si
, 1, bits
); si
+= 1;
468 uint32_t por
= PackBits(si
, 1, bits
); si
+= 1;
471 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");
472 PrintAndLog("-------------------------------------------------------------");
473 PrintAndLog(" Safer key : %s", GetSaferStr(safer
));
474 PrintAndLog(" reserved : %d", resv
);
475 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr
));
476 PrintAndLog(" eXtended mode : %s", (extend
) ? "Yes - Warning":"No");
477 PrintAndLog(" Modulation : %s", GetModulationStr(datamod
));
478 PrintAndLog(" PSK clock freq : %d", pskcf
);
479 PrintAndLog(" AOR - Answer on Request : %s", (aor
) ? "Yes":"No");
480 PrintAndLog(" OTP - One Time Pad : %s", (otp
) ? "Yes - Warning":"No" );
481 PrintAndLog(" Max block : %d", maxblk
);
482 PrintAndLog(" Password mode : %s", (pwd
) ? "Yes":"No");
483 PrintAndLog(" Sequence Start Terminator : %s", (sst
) ? "Yes":"No");
484 PrintAndLog(" Fast Write : %s", (fw
) ? "Yes":"No");
485 PrintAndLog(" Inverse data : %s", (inv
) ? "Yes":"No");
486 PrintAndLog(" POR-Delay : %s", (por
) ? "Yes":"No");
487 PrintAndLog("-------------------------------------------------------------");
488 PrintAndLog(" Raw Data - Page 0");
489 PrintAndLog(" Block 0 : 0x%08X %s", bl0
, sprint_bin(bits
+5,32) );
490 PrintAndLog("-------------------------------------------------------------");
495 int CmdDump(const char *Cmd
){
498 uint8_t pwd
[4] = {0x00};
500 char cmdp
= param_getchar(Cmd
, 0);
501 if ( cmdp
== 'h' || cmdp
== 'H') {
506 bool hasPwd
= ( strlen(Cmd
) > 0);
508 if (param_gethex(Cmd
, 0, pwd
, 8)) {
509 PrintAndLog("password must include 8 HEX symbols");
514 for ( int i
= 0; i
<8; ++i
){
515 memset(s
,0,sizeof(s
));
517 sprintf(s
,"%d %02x%02x%02x%02x", i
, pwd
[0],pwd
[1],pwd
[2],pwd
[3]);
526 char * GetBitRateStr(uint32_t id
){
531 sprintf(retStr
,"%d - RF/8",id
);
534 sprintf(retStr
,"%d - RF/16",id
);
537 sprintf(retStr
,"%d - RF/32",id
);
540 sprintf(retStr
,"%d - RF/40",id
);
543 sprintf(retStr
,"%d - RF/50",id
);
546 sprintf(retStr
,"%d - RF/64",id
);
549 sprintf(retStr
,"%d - RF/100",id
);
552 sprintf(retStr
,"%d - RF/128",id
);
555 sprintf(retStr
,"%d - (Unknown)",id
);
562 char * GetSaferStr(uint32_t id
){
566 sprintf(retStr
,"%d",id
);
568 sprintf(retStr
,"%d - passwd",id
);
571 sprintf(retStr
,"%d - testmode",id
);
576 char * GetModulationStr( uint32_t id
){
582 sprintf(retStr
,"%d - DIRECT (ASK/NRZ)",id
);
585 sprintf(retStr
,"%d - PSK 1 phase change when input changes",id
);
588 sprintf(retStr
,"%d - PSK 2 phase change on bitclk if input high",id
);
591 sprintf(retStr
,"%d - PSK 3 phase change on rising edge of input",id
);
594 sprintf(retStr
,"%d - FSK 1 RF/8 RF/5",id
);
597 sprintf(retStr
,"%d - FSK 2 RF/8 RF/10",id
);
600 sprintf(retStr
,"%d - FSK 1a RF/5 RF/8",id
);
603 sprintf(retStr
,"%d - FSK 2a RF/10 RF/8",id
);
606 sprintf(retStr
,"%d - Manschester",id
);
609 sprintf(retStr
,"%d - Biphase",id
);
612 sprintf(retStr
,"%d - Reserved",id
);
615 sprintf(retStr
,"0x%02X (Unknown)",id
);
621 uint32_t PackBits(uint8_t start
, uint8_t len
, uint8_t* bits
){
629 for (; j
>= 0; --j
, ++i
){
635 static command_t CommandTable
[] =
637 {"help", CmdHelp
, 1, "This help"},
638 {"config", CmdT55xxSetConfig
, 1, "Set T55XX config for modulation, inversed data"},
639 {"read", CmdReadBlk
, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
640 {"write", CmdWriteBlk
, 0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
641 {"trace", CmdReadTrace
, 0, "[1] Read T55xx traceability data (page 1/ blk 0-1)"},
642 {"info", CmdInfo
, 0, "[1] Read T55xx configuration data (page 0/ blk 0)"},
643 {"dump", CmdDump
, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
644 {NULL
, NULL
, 0, NULL
}
647 int CmdLFT55XX(const char *Cmd
)
649 CmdsParse(CommandTable
, Cmd
);
653 int CmdHelp(const char *Cmd
)
655 CmdsHelp(CommandTable
);