12 #include "cmdlfem4x.h"
14 static int CmdHelp(const char *Cmd
);
16 /* send a command before reading */
17 int CmdLFCommandRead(const char *Cmd
)
23 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
24 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
25 // in case they specified 'h'
26 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
31 int CmdFlexdemod(const char *Cmd
)
34 for (i
= 0; i
< GraphTraceLen
; ++i
) {
35 if (GraphBuffer
[i
] < 0) {
44 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
45 int first
= GraphBuffer
[start
];
46 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
47 if (GraphBuffer
[i
] != first
) {
51 if (i
== (start
+ LONG_WAIT
)) {
55 if (start
== GraphTraceLen
- LONG_WAIT
) {
56 PrintAndLog("nothing to wait for");
60 GraphBuffer
[start
] = 2;
61 GraphBuffer
[start
+1] = -2;
67 for (bit
= 0; bit
< 64; bit
++) {
70 for (j
= 0; j
< 16; j
++) {
71 sum
+= GraphBuffer
[i
++];
78 PrintAndLog("bit %d sum %d", bit
, sum
);
81 for (bit
= 0; bit
< 64; bit
++) {
84 for (j
= 0; j
< 16; j
++) {
85 sum
+= GraphBuffer
[i
++];
87 if (sum
> 0 && bits
[bit
] != 1) {
88 PrintAndLog("oops1 at %d", bit
);
90 if (sum
< 0 && bits
[bit
] != 0) {
91 PrintAndLog("oops2 at %d", bit
);
95 GraphTraceLen
= 32*64;
98 for (bit
= 0; bit
< 64; bit
++) {
105 for (j
= 0; j
< 32; j
++) {
106 GraphBuffer
[i
++] = phase
;
111 RepaintGraphWindow();
115 int CmdIndalaDemod(const char *Cmd
)
117 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
122 // worst case with GraphTraceLen=64000 is < 4096
123 // under normal conditions it's < 2048
124 uint8_t rawbits
[4096];
126 int worst
= 0, worstPos
= 0;
127 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
128 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
130 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
132 for (j
= 0; j
< count
- 8; j
+= 16) {
133 rawbits
[rawbit
++] = 0;
135 if ((abs(count
- j
)) > worst
) {
136 worst
= abs(count
- j
);
142 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
144 for (j
= 0; j
< count
- 8; j
+= 16) {
145 rawbits
[rawbit
++] = 1;
147 if ((abs(count
- j
)) > worst
) {
148 worst
= abs(count
- j
);
156 PrintAndLog("Recovered %d raw bits", rawbit
);
157 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
159 // Finding the start of a UID
160 int uidlen
, long_wait
;
161 if (strcmp(Cmd
, "224") == 0) {
170 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
171 first
= rawbits
[start
];
172 for (i
= start
; i
< start
+ long_wait
; i
++) {
173 if (rawbits
[i
] != first
) {
177 if (i
== (start
+ long_wait
)) {
181 if (start
== rawbit
- uidlen
+ 1) {
182 PrintAndLog("nothing to wait for");
186 // Inverting signal if needed
188 for (i
= start
; i
< rawbit
; i
++) {
189 rawbits
[i
] = !rawbits
[i
];
196 showbits
[uidlen
]='\0';
200 if (uidlen
> rawbit
) {
201 PrintAndLog("Warning: not enough raw bits to get a full UID");
202 for (bit
= 0; bit
< rawbit
; bit
++) {
203 bits
[bit
] = rawbits
[i
++];
204 // As we cannot know the parity, let's use "." and "/"
205 showbits
[bit
] = '.' + bits
[bit
];
207 showbits
[bit
+1]='\0';
208 PrintAndLog("Partial UID=%s", showbits
);
211 for (bit
= 0; bit
< uidlen
; bit
++) {
212 bits
[bit
] = rawbits
[i
++];
213 showbits
[bit
] = '0' + bits
[bit
];
217 PrintAndLog("UID=%s", showbits
);
219 // Checking UID against next occurences
220 for (; i
+ uidlen
<= rawbit
;) {
222 for (bit
= 0; bit
< uidlen
; bit
++) {
223 if (bits
[bit
] != rawbits
[i
++]) {
233 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
235 // Remodulating for tag cloning
236 GraphTraceLen
= 32*uidlen
;
239 for (bit
= 0; bit
< uidlen
; bit
++) {
240 if (bits
[bit
] == 0) {
246 for (j
= 0; j
< 32; j
++) {
247 GraphBuffer
[i
++] = phase
;
252 RepaintGraphWindow();
256 int CmdLFRead(const char *Cmd
)
258 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
259 // 'h' means higher-low-frequency, 134 kHz
262 } else if (*Cmd
== '\0') {
265 PrintAndLog("use 'read' or 'read h'");
269 WaitForResponse(CMD_ACK
);
273 static void ChkBitstream(const char *str
)
277 /* convert to bitstream if necessary */
278 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
280 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
288 int CmdLFSim(const char *Cmd
)
293 sscanf(Cmd
, "%i", &gap
);
295 /* convert to bitstream if necessary */
298 PrintAndLog("Sending data, please wait...");
299 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
300 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
302 for (j
= 0; j
< 48; j
++) {
303 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
306 WaitForResponse(CMD_ACK
);
309 PrintAndLog("Starting simulator...");
310 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
315 int CmdLFSimBidir(const char *Cmd
)
317 /* Set ADC to twice the carrier for a slight supersampling */
318 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
323 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
324 int CmdLFSimManchester(const char *Cmd
)
326 static int clock
, gap
;
327 static char data
[1024], gapstring
[8];
329 /* get settings/bits */
330 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
332 /* clear our graph */
335 /* fill it with our bitstream */
336 for (int i
= 0; i
< strlen(data
) ; ++i
)
337 AppendGraph(0, clock
, data
[i
]- '0');
340 CmdManchesterMod("");
342 /* show what we've done */
343 RepaintGraphWindow();
346 sprintf(&gapstring
[0], "%i", gap
);
351 int CmdVchDemod(const char *Cmd
)
353 // Is this the entire sync pattern, or does this also include some
354 // data bits that happen to be the same everywhere? That would be
356 static const int SyncPattern
[] = {
357 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
369 // So first, we correlate for the sync pattern, and mark that.
370 int bestCorrel
= 0, bestPos
= 0;
372 // It does us no good to find the sync pattern, with fewer than
373 // 2048 samples after it...
374 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
377 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
378 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
380 if (sum
> bestCorrel
) {
385 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
393 for (i
= 0; i
< 2048; i
+= 8) {
396 for (j
= 0; j
< 8; j
++) {
397 sum
+= GraphBuffer
[bestPos
+i
+j
];
404 if(abs(sum
) < worst
) {
409 PrintAndLog("bits:");
410 PrintAndLog("%s", bits
);
411 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
413 if (strcmp(Cmd
, "clone")==0) {
416 for(s
= bits
; *s
; s
++) {
418 for(j
= 0; j
< 16; j
++) {
419 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
422 RepaintGraphWindow();
427 static command_t CommandTable
[] =
429 {"help", CmdHelp
, 1, "This help"},
430 {"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)"},
431 {"em4x", CmdLFEM4X
, 1, "EM4X RFIDs"},
432 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
433 {"hid", CmdLFHID
, 1, "HID RFIDs"},
434 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
435 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
436 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
437 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
438 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
439 {"ti", CmdLFTI
, 1, "TI RFIDs"},
440 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
441 {NULL
, NULL
, 0, NULL
}
444 int CmdLF(const char *Cmd
)
446 CmdsParse(CommandTable
, Cmd
);
450 int CmdHelp(const char *Cmd
)
452 CmdsHelp(CommandTable
);