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'");
272 static void ChkBitstream(const char *str
)
276 /* convert to bitstream if necessary */
277 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
279 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
287 int CmdLFSim(const char *Cmd
)
292 sscanf(Cmd
, "%i", &gap
);
294 /* convert to bitstream if necessary */
297 PrintAndLog("Sending data, please wait...");
298 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
299 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
301 for (j
= 0; j
< 48; j
++) {
302 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
305 WaitForResponse(CMD_ACK
);
308 PrintAndLog("Starting simulator...");
309 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
314 int CmdLFSimBidir(const char *Cmd
)
316 /* Set ADC to twice the carrier for a slight supersampling */
317 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
322 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
323 int CmdLFSimManchester(const char *Cmd
)
325 static int clock
, gap
;
326 static char data
[1024], gapstring
[8];
328 /* get settings/bits */
329 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
331 /* clear our graph */
334 /* fill it with our bitstream */
335 for (int i
= 0; i
< strlen(data
) ; ++i
)
336 AppendGraph(0, clock
, data
[i
]- '0');
339 CmdManchesterMod("");
341 /* show what we've done */
342 RepaintGraphWindow();
345 sprintf(&gapstring
[0], "%i", gap
);
350 int CmdVchDemod(const char *Cmd
)
352 // Is this the entire sync pattern, or does this also include some
353 // data bits that happen to be the same everywhere? That would be
355 static const int SyncPattern
[] = {
356 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
368 // So first, we correlate for the sync pattern, and mark that.
369 int bestCorrel
= 0, bestPos
= 0;
371 // It does us no good to find the sync pattern, with fewer than
372 // 2048 samples after it...
373 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
376 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
377 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
379 if (sum
> bestCorrel
) {
384 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
392 for (i
= 0; i
< 2048; i
+= 8) {
395 for (j
= 0; j
< 8; j
++) {
396 sum
+= GraphBuffer
[bestPos
+i
+j
];
403 if(abs(sum
) < worst
) {
408 PrintAndLog("bits:");
409 PrintAndLog("%s", bits
);
410 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
412 if (strcmp(Cmd
, "clone")==0) {
415 for(s
= bits
; *s
; s
++) {
417 for(j
= 0; j
< 16; j
++) {
418 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
421 RepaintGraphWindow();
426 static command_t CommandTable
[] =
428 {"help", CmdHelp
, 1, "This help"},
429 {"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)"},
430 {"em4x", CmdLFEM4X
, 1, "EM4X RFIDs"},
431 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
432 {"hid", CmdLFHID
, 1, "HID RFIDs"},
433 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
434 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
435 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
436 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
437 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
438 {"ti", CmdLFTI
, 1, "TI RFIDs"},
439 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
440 {NULL
, NULL
, 0, NULL
}
443 int CmdLF(const char *Cmd
)
445 CmdsParse(CommandTable
, Cmd
);
449 int CmdHelp(const char *Cmd
)
451 CmdsHelp(CommandTable
);