15 #include "cmdlfem4x.h"
17 static int CmdHelp(const char *Cmd
);
19 /* send a command before reading */
20 int CmdLFCommandRead(const char *Cmd
)
26 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
27 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
28 // in case they specified 'h'
29 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
34 int CmdFlexdemod(const char *Cmd
)
37 for (i
= 0; i
< GraphTraceLen
; ++i
) {
38 if (GraphBuffer
[i
] < 0) {
47 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
48 int first
= GraphBuffer
[start
];
49 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
50 if (GraphBuffer
[i
] != first
) {
54 if (i
== (start
+ LONG_WAIT
)) {
58 if (start
== GraphTraceLen
- LONG_WAIT
) {
59 PrintAndLog("nothing to wait for");
63 GraphBuffer
[start
] = 2;
64 GraphBuffer
[start
+1] = -2;
70 for (bit
= 0; bit
< 64; bit
++) {
73 for (j
= 0; j
< 16; j
++) {
74 sum
+= GraphBuffer
[i
++];
81 PrintAndLog("bit %d sum %d", bit
, sum
);
84 for (bit
= 0; bit
< 64; bit
++) {
87 for (j
= 0; j
< 16; j
++) {
88 sum
+= GraphBuffer
[i
++];
90 if (sum
> 0 && bits
[bit
] != 1) {
91 PrintAndLog("oops1 at %d", bit
);
93 if (sum
< 0 && bits
[bit
] != 0) {
94 PrintAndLog("oops2 at %d", bit
);
98 GraphTraceLen
= 32*64;
101 for (bit
= 0; bit
< 64; bit
++) {
102 if (bits
[bit
] == 0) {
108 for (j
= 0; j
< 32; j
++) {
109 GraphBuffer
[i
++] = phase
;
114 RepaintGraphWindow();
118 int CmdIndalaDemod(const char *Cmd
)
120 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
125 // worst case with GraphTraceLen=64000 is < 4096
126 // under normal conditions it's < 2048
127 uint8_t rawbits
[4096];
129 int worst
= 0, worstPos
= 0;
130 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
131 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
133 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
135 for (j
= 0; j
< count
- 8; j
+= 16) {
136 rawbits
[rawbit
++] = 0;
138 if ((abs(count
- j
)) > worst
) {
139 worst
= abs(count
- j
);
145 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
147 for (j
= 0; j
< count
- 8; j
+= 16) {
148 rawbits
[rawbit
++] = 1;
150 if ((abs(count
- j
)) > worst
) {
151 worst
= abs(count
- j
);
159 PrintAndLog("Recovered %d raw bits", rawbit
);
160 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
162 // Finding the start of a UID
163 int uidlen
, long_wait
;
164 if (strcmp(Cmd
, "224") == 0) {
173 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
174 first
= rawbits
[start
];
175 for (i
= start
; i
< start
+ long_wait
; i
++) {
176 if (rawbits
[i
] != first
) {
180 if (i
== (start
+ long_wait
)) {
184 if (start
== rawbit
- uidlen
+ 1) {
185 PrintAndLog("nothing to wait for");
189 // Inverting signal if needed
191 for (i
= start
; i
< rawbit
; i
++) {
192 rawbits
[i
] = !rawbits
[i
];
199 showbits
[uidlen
]='\0';
203 if (uidlen
> rawbit
) {
204 PrintAndLog("Warning: not enough raw bits to get a full UID");
205 for (bit
= 0; bit
< rawbit
; bit
++) {
206 bits
[bit
] = rawbits
[i
++];
207 // As we cannot know the parity, let's use "." and "/"
208 showbits
[bit
] = '.' + bits
[bit
];
210 showbits
[bit
+1]='\0';
211 PrintAndLog("Partial UID=%s", showbits
);
214 for (bit
= 0; bit
< uidlen
; bit
++) {
215 bits
[bit
] = rawbits
[i
++];
216 showbits
[bit
] = '0' + bits
[bit
];
220 PrintAndLog("UID=%s", showbits
);
222 // Checking UID against next occurences
223 for (; i
+ uidlen
<= rawbit
;) {
225 for (bit
= 0; bit
< uidlen
; bit
++) {
226 if (bits
[bit
] != rawbits
[i
++]) {
236 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
238 // Remodulating for tag cloning
239 GraphTraceLen
= 32*uidlen
;
242 for (bit
= 0; bit
< uidlen
; bit
++) {
243 if (bits
[bit
] == 0) {
249 for (j
= 0; j
< 32; j
++) {
250 GraphBuffer
[i
++] = phase
;
255 RepaintGraphWindow();
259 int CmdLFRead(const char *Cmd
)
261 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
262 // 'h' means higher-low-frequency, 134 kHz
265 } else if (*Cmd
== '\0') {
268 PrintAndLog("use 'read' or 'read h'");
272 WaitForResponse(CMD_ACK
);
276 static void ChkBitstream(const char *str
)
280 /* convert to bitstream if necessary */
281 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
283 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
291 int CmdLFSim(const char *Cmd
)
296 sscanf(Cmd
, "%i", &gap
);
298 /* convert to bitstream if necessary */
301 PrintAndLog("Sending data, please wait...");
302 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
303 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
305 for (j
= 0; j
< 48; j
++) {
306 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
309 WaitForResponse(CMD_ACK
);
312 PrintAndLog("Starting simulator...");
313 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
318 int CmdLFSimBidir(const char *Cmd
)
320 /* Set ADC to twice the carrier for a slight supersampling */
321 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
326 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
327 int CmdLFSimManchester(const char *Cmd
)
329 static int clock
, gap
;
330 static char data
[1024], gapstring
[8];
332 /* get settings/bits */
333 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
335 /* clear our graph */
338 /* fill it with our bitstream */
339 for (int i
= 0; i
< strlen(data
) ; ++i
)
340 AppendGraph(0, clock
, data
[i
]- '0');
343 CmdManchesterMod("");
345 /* show what we've done */
346 RepaintGraphWindow();
349 sprintf(&gapstring
[0], "%i", gap
);
354 int CmdVchDemod(const char *Cmd
)
356 // Is this the entire sync pattern, or does this also include some
357 // data bits that happen to be the same everywhere? That would be
359 static const int SyncPattern
[] = {
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,
368 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
369 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
372 // So first, we correlate for the sync pattern, and mark that.
373 int bestCorrel
= 0, bestPos
= 0;
375 // It does us no good to find the sync pattern, with fewer than
376 // 2048 samples after it...
377 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
380 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
381 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
383 if (sum
> bestCorrel
) {
388 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
396 for (i
= 0; i
< 2048; i
+= 8) {
399 for (j
= 0; j
< 8; j
++) {
400 sum
+= GraphBuffer
[bestPos
+i
+j
];
407 if(abs(sum
) < worst
) {
412 PrintAndLog("bits:");
413 PrintAndLog("%s", bits
);
414 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
416 if (strcmp(Cmd
, "clone")==0) {
419 for(s
= bits
; *s
; s
++) {
421 for(j
= 0; j
< 16; j
++) {
422 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
425 RepaintGraphWindow();
430 static command_t CommandTable
[] =
432 {"help", CmdHelp
, 1, "This help"},
433 {"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)"},
434 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
435 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
436 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
437 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
438 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
439 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
440 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
441 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
442 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
443 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
444 {NULL
, NULL
, 0, NULL
}
447 int CmdLF(const char *Cmd
)
449 CmdsParse(CommandTable
, Cmd
);
453 int CmdHelp(const char *Cmd
)
455 CmdsHelp(CommandTable
);