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"
27 static int CmdHelp(const char *Cmd
);
29 /* send a command before reading */
30 int CmdLFCommandRead(const char *Cmd
)
36 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
37 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
38 // in case they specified 'h'
39 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
44 int CmdFlexdemod(const char *Cmd
)
47 for (i
= 0; i
< GraphTraceLen
; ++i
) {
48 if (GraphBuffer
[i
] < 0) {
57 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
58 int first
= GraphBuffer
[start
];
59 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
60 if (GraphBuffer
[i
] != first
) {
64 if (i
== (start
+ LONG_WAIT
)) {
68 if (start
== GraphTraceLen
- LONG_WAIT
) {
69 PrintAndLog("nothing to wait for");
73 GraphBuffer
[start
] = 2;
74 GraphBuffer
[start
+1] = -2;
80 for (bit
= 0; bit
< 64; bit
++) {
83 for (j
= 0; j
< 16; j
++) {
84 sum
+= GraphBuffer
[i
++];
91 PrintAndLog("bit %d sum %d", bit
, sum
);
94 for (bit
= 0; bit
< 64; bit
++) {
97 for (j
= 0; j
< 16; j
++) {
98 sum
+= GraphBuffer
[i
++];
100 if (sum
> 0 && bits
[bit
] != 1) {
101 PrintAndLog("oops1 at %d", bit
);
103 if (sum
< 0 && bits
[bit
] != 0) {
104 PrintAndLog("oops2 at %d", bit
);
108 GraphTraceLen
= 32*64;
111 for (bit
= 0; bit
< 64; bit
++) {
112 if (bits
[bit
] == 0) {
118 for (j
= 0; j
< 32; j
++) {
119 GraphBuffer
[i
++] = phase
;
124 RepaintGraphWindow();
128 int CmdIndalaDemod(const char *Cmd
)
130 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
135 // worst case with GraphTraceLen=64000 is < 4096
136 // under normal conditions it's < 2048
137 uint8_t rawbits
[4096];
139 int worst
= 0, worstPos
= 0;
140 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
141 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
143 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
145 for (j
= 0; j
< count
- 8; j
+= 16) {
146 rawbits
[rawbit
++] = 0;
148 if ((abs(count
- j
)) > worst
) {
149 worst
= abs(count
- j
);
155 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
157 for (j
= 0; j
< count
- 8; j
+= 16) {
158 rawbits
[rawbit
++] = 1;
160 if ((abs(count
- j
)) > worst
) {
161 worst
= abs(count
- j
);
169 PrintAndLog("Recovered %d raw bits", rawbit
);
170 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
172 // Finding the start of a UID
173 int uidlen
, long_wait
;
174 if (strcmp(Cmd
, "224") == 0) {
183 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
184 first
= rawbits
[start
];
185 for (i
= start
; i
< start
+ long_wait
; i
++) {
186 if (rawbits
[i
] != first
) {
190 if (i
== (start
+ long_wait
)) {
194 if (start
== rawbit
- uidlen
+ 1) {
195 PrintAndLog("nothing to wait for");
199 // Inverting signal if needed
201 for (i
= start
; i
< rawbit
; i
++) {
202 rawbits
[i
] = !rawbits
[i
];
209 showbits
[uidlen
]='\0';
213 if (uidlen
> rawbit
) {
214 PrintAndLog("Warning: not enough raw bits to get a full UID");
215 for (bit
= 0; bit
< rawbit
; bit
++) {
216 bits
[bit
] = rawbits
[i
++];
217 // As we cannot know the parity, let's use "." and "/"
218 showbits
[bit
] = '.' + bits
[bit
];
220 showbits
[bit
+1]='\0';
221 PrintAndLog("Partial UID=%s", showbits
);
224 for (bit
= 0; bit
< uidlen
; bit
++) {
225 bits
[bit
] = rawbits
[i
++];
226 showbits
[bit
] = '0' + bits
[bit
];
232 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
237 for( idx
=0; idx
<64; idx
++) {
238 if (showbits
[idx
] == '0') {
239 uid1
=(uid1
<<1)|(uid2
>>31);
242 uid1
=(uid1
<<1)|(uid2
>>31);
246 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
254 for( idx
=0; idx
<224; idx
++) {
255 uid1
=(uid1
<<1)|(uid2
>>31);
256 uid2
=(uid2
<<1)|(uid3
>>31);
257 uid3
=(uid3
<<1)|(uid4
>>31);
258 uid4
=(uid4
<<1)|(uid5
>>31);
259 uid5
=(uid5
<<1)|(uid6
>>31);
260 uid6
=(uid6
<<1)|(uid7
>>31);
261 if (showbits
[idx
] == '0') uid7
=(uid7
<<1)|0;
262 else uid7
=(uid7
<<1)|1;
264 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
267 // Checking UID against next occurences
268 for (; i
+ uidlen
<= rawbit
;) {
270 for (bit
= 0; bit
< uidlen
; bit
++) {
271 if (bits
[bit
] != rawbits
[i
++]) {
281 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
283 // Remodulating for tag cloning
284 GraphTraceLen
= 32*uidlen
;
287 for (bit
= 0; bit
< uidlen
; bit
++) {
288 if (bits
[bit
] == 0) {
294 for (j
= 0; j
< 32; j
++) {
295 GraphBuffer
[i
++] = phase
;
300 RepaintGraphWindow();
304 int CmdIndalaClone(const char *Cmd
)
306 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
317 if (strchr(Cmd
,'l') != 0) {
318 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
319 uid1
= (uid1
<< 4) | (uid2
>> 28);
320 uid2
= (uid2
<< 4) | (uid3
>> 28);
321 uid3
= (uid3
<< 4) | (uid4
>> 28);
322 uid4
= (uid4
<< 4) | (uid5
>> 28);
323 uid5
= (uid5
<< 4) | (uid6
>> 28);
324 uid6
= (uid6
<< 4) | (uid7
>> 28);
325 uid7
= (uid7
<< 4) | (n
& 0xf);
327 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
328 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
329 c
.d
.asDwords
[0] = uid1
;
330 c
.d
.asDwords
[1] = uid2
;
331 c
.d
.asDwords
[2] = uid3
;
332 c
.d
.asDwords
[3] = uid4
;
333 c
.d
.asDwords
[4] = uid5
;
334 c
.d
.asDwords
[5] = uid6
;
335 c
.d
.asDwords
[6] = uid7
;
339 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
340 uid1
= (uid1
<< 4) | (uid2
>> 28);
341 uid2
= (uid2
<< 4) | (n
& 0xf);
343 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
344 c
.cmd
= CMD_INDALA_CLONE_TAG
;
353 int CmdLFRead(const char *Cmd
)
355 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
356 // 'h' means higher-low-frequency, 134 kHz
359 } else if (*Cmd
== '\0') {
362 PrintAndLog("use 'read' or 'read h'");
366 WaitForResponse(CMD_ACK
);
370 static void ChkBitstream(const char *str
)
374 /* convert to bitstream if necessary */
375 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
377 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
385 int CmdLFSim(const char *Cmd
)
390 sscanf(Cmd
, "%i", &gap
);
392 /* convert to bitstream if necessary */
395 PrintAndLog("Sending data, please wait...");
396 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
397 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
399 for (j
= 0; j
< 48; j
++) {
400 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
403 WaitForResponse(CMD_ACK
);
406 PrintAndLog("Starting simulator...");
407 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
412 int CmdLFSimBidir(const char *Cmd
)
414 /* Set ADC to twice the carrier for a slight supersampling */
415 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
420 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
421 int CmdLFSimManchester(const char *Cmd
)
423 static int clock
, gap
;
424 static char data
[1024], gapstring
[8];
426 /* get settings/bits */
427 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
429 /* clear our graph */
432 /* fill it with our bitstream */
433 for (int i
= 0; i
< strlen(data
) ; ++i
)
434 AppendGraph(0, clock
, data
[i
]- '0');
437 CmdManchesterMod("");
439 /* show what we've done */
440 RepaintGraphWindow();
443 sprintf(&gapstring
[0], "%i", gap
);
448 int CmdVchDemod(const char *Cmd
)
450 // Is this the entire sync pattern, or does this also include some
451 // data bits that happen to be the same everywhere? That would be
453 static const int SyncPattern
[] = {
454 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
466 // So first, we correlate for the sync pattern, and mark that.
467 int bestCorrel
= 0, bestPos
= 0;
469 // It does us no good to find the sync pattern, with fewer than
470 // 2048 samples after it...
471 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
474 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
475 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
477 if (sum
> bestCorrel
) {
482 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
490 for (i
= 0; i
< 2048; i
+= 8) {
493 for (j
= 0; j
< 8; j
++) {
494 sum
+= GraphBuffer
[bestPos
+i
+j
];
501 if(abs(sum
) < worst
) {
506 PrintAndLog("bits:");
507 PrintAndLog("%s", bits
);
508 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
510 if (strcmp(Cmd
, "clone")==0) {
513 for(s
= bits
; *s
; s
++) {
515 for(j
= 0; j
< 16; j
++) {
516 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
519 RepaintGraphWindow();
524 static command_t CommandTable
[] =
526 {"help", CmdHelp
, 1, "This help"},
527 {"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)"},
528 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
529 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
530 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
531 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
532 {"indalaclone", CmdIndalaClone
, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
533 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
534 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
535 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
536 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
537 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
538 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
539 {NULL
, NULL
, 0, NULL
}
542 int CmdLF(const char *Cmd
)
544 CmdsParse(CommandTable
, Cmd
);
548 int CmdHelp(const char *Cmd
)
550 CmdsHelp(CommandTable
);