13 #include "cmdlfem4x.h"
15 static int CmdHelp(const char *Cmd
);
17 /* send a command before reading */
18 int CmdLFCommandRead(const char *Cmd
)
24 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
25 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
26 // in case they specified 'h'
27 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
32 int CmdFlexdemod(const char *Cmd
)
35 for (i
= 0; i
< GraphTraceLen
; ++i
) {
36 if (GraphBuffer
[i
] < 0) {
45 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
46 int first
= GraphBuffer
[start
];
47 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
48 if (GraphBuffer
[i
] != first
) {
52 if (i
== (start
+ LONG_WAIT
)) {
56 if (start
== GraphTraceLen
- LONG_WAIT
) {
57 PrintAndLog("nothing to wait for");
61 GraphBuffer
[start
] = 2;
62 GraphBuffer
[start
+1] = -2;
68 for (bit
= 0; bit
< 64; bit
++) {
71 for (j
= 0; j
< 16; j
++) {
72 sum
+= GraphBuffer
[i
++];
79 PrintAndLog("bit %d sum %d", bit
, sum
);
82 for (bit
= 0; bit
< 64; bit
++) {
85 for (j
= 0; j
< 16; j
++) {
86 sum
+= GraphBuffer
[i
++];
88 if (sum
> 0 && bits
[bit
] != 1) {
89 PrintAndLog("oops1 at %d", bit
);
91 if (sum
< 0 && bits
[bit
] != 0) {
92 PrintAndLog("oops2 at %d", bit
);
96 GraphTraceLen
= 32*64;
99 for (bit
= 0; bit
< 64; bit
++) {
100 if (bits
[bit
] == 0) {
106 for (j
= 0; j
< 32; j
++) {
107 GraphBuffer
[i
++] = phase
;
112 RepaintGraphWindow();
116 int CmdIndalaDemod(const char *Cmd
)
118 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
123 // worst case with GraphTraceLen=64000 is < 4096
124 // under normal conditions it's < 2048
125 uint8_t rawbits
[4096];
127 int worst
= 0, worstPos
= 0;
128 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
129 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
131 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
133 for (j
= 0; j
< count
- 8; j
+= 16) {
134 rawbits
[rawbit
++] = 0;
136 if ((abs(count
- j
)) > worst
) {
137 worst
= abs(count
- j
);
143 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
145 for (j
= 0; j
< count
- 8; j
+= 16) {
146 rawbits
[rawbit
++] = 1;
148 if ((abs(count
- j
)) > worst
) {
149 worst
= abs(count
- j
);
157 PrintAndLog("Recovered %d raw bits", rawbit
);
158 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
160 // Finding the start of a UID
161 int uidlen
, long_wait
;
162 if (strcmp(Cmd
, "224") == 0) {
171 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
172 first
= rawbits
[start
];
173 for (i
= start
; i
< start
+ long_wait
; i
++) {
174 if (rawbits
[i
] != first
) {
178 if (i
== (start
+ long_wait
)) {
182 if (start
== rawbit
- uidlen
+ 1) {
183 PrintAndLog("nothing to wait for");
187 // Inverting signal if needed
189 for (i
= start
; i
< rawbit
; i
++) {
190 rawbits
[i
] = !rawbits
[i
];
197 showbits
[uidlen
]='\0';
201 if (uidlen
> rawbit
) {
202 PrintAndLog("Warning: not enough raw bits to get a full UID");
203 for (bit
= 0; bit
< rawbit
; bit
++) {
204 bits
[bit
] = rawbits
[i
++];
205 // As we cannot know the parity, let's use "." and "/"
206 showbits
[bit
] = '.' + bits
[bit
];
208 showbits
[bit
+1]='\0';
209 PrintAndLog("Partial UID=%s", showbits
);
212 for (bit
= 0; bit
< uidlen
; bit
++) {
213 bits
[bit
] = rawbits
[i
++];
214 showbits
[bit
] = '0' + bits
[bit
];
218 PrintAndLog("UID=%s", showbits
);
220 // Checking UID against next occurences
221 for (; i
+ uidlen
<= rawbit
;) {
223 for (bit
= 0; bit
< uidlen
; bit
++) {
224 if (bits
[bit
] != rawbits
[i
++]) {
234 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
236 // Remodulating for tag cloning
237 GraphTraceLen
= 32*uidlen
;
240 for (bit
= 0; bit
< uidlen
; bit
++) {
241 if (bits
[bit
] == 0) {
247 for (j
= 0; j
< 32; j
++) {
248 GraphBuffer
[i
++] = phase
;
253 RepaintGraphWindow();
257 int CmdLFRead(const char *Cmd
)
259 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
260 // 'h' means higher-low-frequency, 134 kHz
263 } else if (*Cmd
== '\0') {
266 PrintAndLog("use 'read' or 'read h'");
270 WaitForResponse(CMD_ACK
);
274 static void ChkBitstream(const char *str
)
278 /* convert to bitstream if necessary */
279 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
281 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
289 int CmdLFSim(const char *Cmd
)
294 sscanf(Cmd
, "%i", &gap
);
296 /* convert to bitstream if necessary */
299 PrintAndLog("Sending data, please wait...");
300 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
301 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
303 for (j
= 0; j
< 48; j
++) {
304 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
307 WaitForResponse(CMD_ACK
);
310 PrintAndLog("Starting simulator...");
311 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
316 int CmdLFSimBidir(const char *Cmd
)
318 /* Set ADC to twice the carrier for a slight supersampling */
319 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
324 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
325 int CmdLFSimManchester(const char *Cmd
)
327 static int clock
, gap
;
328 static char data
[1024], gapstring
[8];
330 /* get settings/bits */
331 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
333 /* clear our graph */
336 /* fill it with our bitstream */
337 for (int i
= 0; i
< strlen(data
) ; ++i
)
338 AppendGraph(0, clock
, data
[i
]- '0');
341 CmdManchesterMod("");
343 /* show what we've done */
344 RepaintGraphWindow();
347 sprintf(&gapstring
[0], "%i", gap
);
352 int CmdVchDemod(const char *Cmd
)
354 // Is this the entire sync pattern, or does this also include some
355 // data bits that happen to be the same everywhere? That would be
357 static const int SyncPattern
[] = {
358 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
359 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
360 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
361 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
362 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
363 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
364 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
365 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
366 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
367 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
370 // So first, we correlate for the sync pattern, and mark that.
371 int bestCorrel
= 0, bestPos
= 0;
373 // It does us no good to find the sync pattern, with fewer than
374 // 2048 samples after it...
375 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
378 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
379 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
381 if (sum
> bestCorrel
) {
386 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
394 for (i
= 0; i
< 2048; i
+= 8) {
397 for (j
= 0; j
< 8; j
++) {
398 sum
+= GraphBuffer
[bestPos
+i
+j
];
405 if(abs(sum
) < worst
) {
410 PrintAndLog("bits:");
411 PrintAndLog("%s", bits
);
412 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
414 if (strcmp(Cmd
, "clone")==0) {
417 for(s
= bits
; *s
; s
++) {
419 for(j
= 0; j
< 16; j
++) {
420 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
423 RepaintGraphWindow();
428 static command_t CommandTable
[] =
430 {"help", CmdHelp
, 1, "This help"},
431 {"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)"},
432 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
433 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
434 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
435 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
436 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
437 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
438 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
439 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
440 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
441 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
442 {NULL
, NULL
, 0, NULL
}
445 int CmdLF(const char *Cmd
)
447 CmdsParse(CommandTable
, Cmd
);
451 int CmdHelp(const char *Cmd
)
453 CmdsHelp(CommandTable
);