1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Low frequency commands
9 //-----------------------------------------------------------------------------
19 #include "cmdparser.h"
25 #include "cmdlfem4x.h"
26 #include "cmdlfhitag.h"
28 static int CmdHelp(const char *Cmd
);
30 /* send a command before reading */
31 int CmdLFCommandRead(const char *Cmd
)
37 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
38 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
39 // in case they specified 'h'
40 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
45 int CmdFlexdemod(const char *Cmd
)
48 for (i
= 0; i
< GraphTraceLen
; ++i
) {
49 if (GraphBuffer
[i
] < 0) {
58 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
59 int first
= GraphBuffer
[start
];
60 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
61 if (GraphBuffer
[i
] != first
) {
65 if (i
== (start
+ LONG_WAIT
)) {
69 if (start
== GraphTraceLen
- LONG_WAIT
) {
70 PrintAndLog("nothing to wait for");
74 GraphBuffer
[start
] = 2;
75 GraphBuffer
[start
+1] = -2;
81 for (bit
= 0; bit
< 64; bit
++) {
84 for (j
= 0; j
< 16; j
++) {
85 sum
+= GraphBuffer
[i
++];
92 PrintAndLog("bit %d sum %d", bit
, sum
);
95 for (bit
= 0; bit
< 64; bit
++) {
98 for (j
= 0; j
< 16; j
++) {
99 sum
+= GraphBuffer
[i
++];
101 if (sum
> 0 && bits
[bit
] != 1) {
102 PrintAndLog("oops1 at %d", bit
);
104 if (sum
< 0 && bits
[bit
] != 0) {
105 PrintAndLog("oops2 at %d", bit
);
109 GraphTraceLen
= 32*64;
112 for (bit
= 0; bit
< 64; bit
++) {
113 if (bits
[bit
] == 0) {
119 for (j
= 0; j
< 32; j
++) {
120 GraphBuffer
[i
++] = phase
;
125 RepaintGraphWindow();
129 int CmdIndalaDemod(const char *Cmd
)
131 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
136 // worst case with GraphTraceLen=64000 is < 4096
137 // under normal conditions it's < 2048
138 uint8_t rawbits
[4096];
140 int worst
= 0, worstPos
= 0;
141 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
142 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
144 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
146 for (j
= 0; j
< count
- 8; j
+= 16) {
147 rawbits
[rawbit
++] = 0;
149 if ((abs(count
- j
)) > worst
) {
150 worst
= abs(count
- j
);
156 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
158 for (j
= 0; j
< count
- 8; j
+= 16) {
159 rawbits
[rawbit
++] = 1;
161 if ((abs(count
- j
)) > worst
) {
162 worst
= abs(count
- j
);
170 PrintAndLog("Recovered %d raw bits", rawbit
);
171 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
173 // Finding the start of a UID
174 int uidlen
, long_wait
;
175 if (strcmp(Cmd
, "224") == 0) {
184 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
185 first
= rawbits
[start
];
186 for (i
= start
; i
< start
+ long_wait
; i
++) {
187 if (rawbits
[i
] != first
) {
191 if (i
== (start
+ long_wait
)) {
195 if (start
== rawbit
- uidlen
+ 1) {
196 PrintAndLog("nothing to wait for");
200 // Inverting signal if needed
202 for (i
= start
; i
< rawbit
; i
++) {
203 rawbits
[i
] = !rawbits
[i
];
210 showbits
[uidlen
]='\0';
214 if (uidlen
> rawbit
) {
215 PrintAndLog("Warning: not enough raw bits to get a full UID");
216 for (bit
= 0; bit
< rawbit
; bit
++) {
217 bits
[bit
] = rawbits
[i
++];
218 // As we cannot know the parity, let's use "." and "/"
219 showbits
[bit
] = '.' + bits
[bit
];
221 showbits
[bit
+1]='\0';
222 PrintAndLog("Partial UID=%s", showbits
);
225 for (bit
= 0; bit
< uidlen
; bit
++) {
226 bits
[bit
] = rawbits
[i
++];
227 showbits
[bit
] = '0' + bits
[bit
];
233 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
238 for( idx
=0; idx
<64; idx
++) {
239 if (showbits
[idx
] == '0') {
240 uid1
=(uid1
<<1)|(uid2
>>31);
243 uid1
=(uid1
<<1)|(uid2
>>31);
247 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
255 for( idx
=0; idx
<224; idx
++) {
256 uid1
=(uid1
<<1)|(uid2
>>31);
257 uid2
=(uid2
<<1)|(uid3
>>31);
258 uid3
=(uid3
<<1)|(uid4
>>31);
259 uid4
=(uid4
<<1)|(uid5
>>31);
260 uid5
=(uid5
<<1)|(uid6
>>31);
261 uid6
=(uid6
<<1)|(uid7
>>31);
262 if (showbits
[idx
] == '0') uid7
=(uid7
<<1)|0;
263 else uid7
=(uid7
<<1)|1;
265 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
268 // Checking UID against next occurences
269 for (; i
+ uidlen
<= rawbit
;) {
271 for (bit
= 0; bit
< uidlen
; bit
++) {
272 if (bits
[bit
] != rawbits
[i
++]) {
282 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
284 // Remodulating for tag cloning
285 GraphTraceLen
= 32*uidlen
;
288 for (bit
= 0; bit
< uidlen
; bit
++) {
289 if (bits
[bit
] == 0) {
295 for (j
= 0; j
< 32; j
++) {
296 GraphBuffer
[i
++] = phase
;
301 RepaintGraphWindow();
305 int CmdIndalaClone(const char *Cmd
)
307 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
318 if (strchr(Cmd
,'l') != 0) {
319 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
320 uid1
= (uid1
<< 4) | (uid2
>> 28);
321 uid2
= (uid2
<< 4) | (uid3
>> 28);
322 uid3
= (uid3
<< 4) | (uid4
>> 28);
323 uid4
= (uid4
<< 4) | (uid5
>> 28);
324 uid5
= (uid5
<< 4) | (uid6
>> 28);
325 uid6
= (uid6
<< 4) | (uid7
>> 28);
326 uid7
= (uid7
<< 4) | (n
& 0xf);
328 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
329 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
330 c
.d
.asDwords
[0] = uid1
;
331 c
.d
.asDwords
[1] = uid2
;
332 c
.d
.asDwords
[2] = uid3
;
333 c
.d
.asDwords
[3] = uid4
;
334 c
.d
.asDwords
[4] = uid5
;
335 c
.d
.asDwords
[5] = uid6
;
336 c
.d
.asDwords
[6] = uid7
;
340 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
341 uid1
= (uid1
<< 4) | (uid2
>> 28);
342 uid2
= (uid2
<< 4) | (n
& 0xf);
344 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
345 c
.cmd
= CMD_INDALA_CLONE_TAG
;
354 int CmdLFRead(const char *Cmd
)
356 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
357 // 'h' means higher-low-frequency, 134 kHz
360 } else if (*Cmd
== '\0') {
363 PrintAndLog("use 'read' or 'read h'");
367 WaitForResponse(CMD_ACK
);
371 static void ChkBitstream(const char *str
)
375 /* convert to bitstream if necessary */
376 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
378 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
386 int CmdLFSim(const char *Cmd
)
391 sscanf(Cmd
, "%i", &gap
);
393 /* convert to bitstream if necessary */
396 PrintAndLog("Sending data, please wait...");
397 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
398 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
400 for (j
= 0; j
< 48; j
++) {
401 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
404 WaitForResponse(CMD_ACK
);
407 PrintAndLog("Starting simulator...");
408 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
413 int CmdLFSimBidir(const char *Cmd
)
415 /* Set ADC to twice the carrier for a slight supersampling */
416 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
421 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
422 int CmdLFSimManchester(const char *Cmd
)
424 static int clock
, gap
;
425 static char data
[1024], gapstring
[8];
427 /* get settings/bits */
428 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
430 /* clear our graph */
433 /* fill it with our bitstream */
434 for (int i
= 0; i
< strlen(data
) ; ++i
)
435 AppendGraph(0, clock
, data
[i
]- '0');
438 CmdManchesterMod("");
440 /* show what we've done */
441 RepaintGraphWindow();
444 sprintf(&gapstring
[0], "%i", gap
);
449 int CmdVchDemod(const char *Cmd
)
451 // Is this the entire sync pattern, or does this also include some
452 // data bits that happen to be the same everywhere? That would be
454 static const int SyncPattern
[] = {
455 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
456 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
457 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
458 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
459 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
460 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
461 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
462 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
463 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
464 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
467 // So first, we correlate for the sync pattern, and mark that.
468 int bestCorrel
= 0, bestPos
= 0;
470 // It does us no good to find the sync pattern, with fewer than
471 // 2048 samples after it...
472 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
475 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
476 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
478 if (sum
> bestCorrel
) {
483 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
491 for (i
= 0; i
< 2048; i
+= 8) {
494 for (j
= 0; j
< 8; j
++) {
495 sum
+= GraphBuffer
[bestPos
+i
+j
];
502 if(abs(sum
) < worst
) {
507 PrintAndLog("bits:");
508 PrintAndLog("%s", bits
);
509 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
511 if (strcmp(Cmd
, "clone")==0) {
514 for(s
= bits
; *s
; s
++) {
516 for(j
= 0; j
< 16; j
++) {
517 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
520 RepaintGraphWindow();
525 static command_t CommandTable
[] =
527 {"help", CmdHelp
, 1, "This help"},
528 {"cmdread", CmdLFCommandRead
, 0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},
529 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
530 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
531 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
532 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
533 {"indalaclone", CmdIndalaClone
, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
534 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
535 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
536 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
537 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
538 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
539 {"hitag", CmdLFHitag
, 1, "{ Hitag tags and transponders... }"},
540 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
541 {NULL
, NULL
, 0, NULL
}
544 int CmdLF(const char *Cmd
)
546 CmdsParse(CommandTable
, Cmd
);
550 int CmdHelp(const char *Cmd
)
552 CmdsHelp(CommandTable
);