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
];
230 PrintAndLog("UID=%s", showbits
);
232 // Checking UID against next occurences
233 for (; i
+ uidlen
<= rawbit
;) {
235 for (bit
= 0; bit
< uidlen
; bit
++) {
236 if (bits
[bit
] != rawbits
[i
++]) {
246 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
248 // Remodulating for tag cloning
249 GraphTraceLen
= 32*uidlen
;
252 for (bit
= 0; bit
< uidlen
; bit
++) {
253 if (bits
[bit
] == 0) {
259 for (j
= 0; j
< 32; j
++) {
260 GraphBuffer
[i
++] = phase
;
265 RepaintGraphWindow();
269 int CmdLFRead(const char *Cmd
)
271 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
272 // 'h' means higher-low-frequency, 134 kHz
275 } else if (*Cmd
== '\0') {
278 PrintAndLog("use 'read' or 'read h'");
282 WaitForResponse(CMD_ACK
);
286 static void ChkBitstream(const char *str
)
290 /* convert to bitstream if necessary */
291 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
293 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
301 int CmdLFSim(const char *Cmd
)
306 sscanf(Cmd
, "%i", &gap
);
308 /* convert to bitstream if necessary */
311 PrintAndLog("Sending data, please wait...");
312 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
313 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
315 for (j
= 0; j
< 48; j
++) {
316 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
319 WaitForResponse(CMD_ACK
);
322 PrintAndLog("Starting simulator...");
323 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
328 int CmdLFSimBidir(const char *Cmd
)
330 /* Set ADC to twice the carrier for a slight supersampling */
331 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
336 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
337 int CmdLFSimManchester(const char *Cmd
)
339 static int clock
, gap
;
340 static char data
[1024], gapstring
[8];
342 /* get settings/bits */
343 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
345 /* clear our graph */
348 /* fill it with our bitstream */
349 for (int i
= 0; i
< strlen(data
) ; ++i
)
350 AppendGraph(0, clock
, data
[i
]- '0');
353 CmdManchesterMod("");
355 /* show what we've done */
356 RepaintGraphWindow();
359 sprintf(&gapstring
[0], "%i", gap
);
364 int CmdVchDemod(const char *Cmd
)
366 // Is this the entire sync pattern, or does this also include some
367 // data bits that happen to be the same everywhere? That would be
369 static const int SyncPattern
[] = {
370 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
371 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
372 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
373 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
375 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
376 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
377 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
378 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
379 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
382 // So first, we correlate for the sync pattern, and mark that.
383 int bestCorrel
= 0, bestPos
= 0;
385 // It does us no good to find the sync pattern, with fewer than
386 // 2048 samples after it...
387 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
390 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
391 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
393 if (sum
> bestCorrel
) {
398 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
406 for (i
= 0; i
< 2048; i
+= 8) {
409 for (j
= 0; j
< 8; j
++) {
410 sum
+= GraphBuffer
[bestPos
+i
+j
];
417 if(abs(sum
) < worst
) {
422 PrintAndLog("bits:");
423 PrintAndLog("%s", bits
);
424 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
426 if (strcmp(Cmd
, "clone")==0) {
429 for(s
= bits
; *s
; s
++) {
431 for(j
= 0; j
< 16; j
++) {
432 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
435 RepaintGraphWindow();
440 static command_t CommandTable
[] =
442 {"help", CmdHelp
, 1, "This help"},
443 {"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)"},
444 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
445 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
446 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
447 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
448 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
449 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
450 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
451 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
452 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
453 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
454 {NULL
, NULL
, 0, NULL
}
457 int CmdLF(const char *Cmd
)
459 CmdsParse(CommandTable
, Cmd
);
463 int CmdHelp(const char *Cmd
)
465 CmdsHelp(CommandTable
);