]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Low frequency commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
590f8ff9 | 12 | #include <stdlib.h> |
7fe9b0b7 | 13 | #include <string.h> |
393c3ef9 | 14 | #include <limits.h> |
7fe9b0b7 | 15 | #include "proxusb.h" |
16 | #include "data.h" | |
17 | #include "graph.h" | |
18 | #include "ui.h" | |
19 | #include "cmdparser.h" | |
040a7baa | 20 | #include "cmdmain.h" |
7fe9b0b7 | 21 | #include "cmddata.h" |
22 | #include "cmdlf.h" | |
23 | #include "cmdlfhid.h" | |
24 | #include "cmdlfti.h" | |
25 | #include "cmdlfem4x.h" | |
26 | ||
27 | static int CmdHelp(const char *Cmd); | |
28 | ||
29 | /* send a command before reading */ | |
30 | int CmdLFCommandRead(const char *Cmd) | |
31 | { | |
32 | static char dummy[3]; | |
33 | ||
34 | dummy[0]= ' '; | |
35 | ||
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); | |
40 | SendCommand(&c); | |
41 | return 0; | |
42 | } | |
43 | ||
44 | int CmdFlexdemod(const char *Cmd) | |
45 | { | |
46 | int i; | |
47 | for (i = 0; i < GraphTraceLen; ++i) { | |
48 | if (GraphBuffer[i] < 0) { | |
49 | GraphBuffer[i] = -1; | |
50 | } else { | |
51 | GraphBuffer[i] = 1; | |
52 | } | |
53 | } | |
54 | ||
55 | #define LONG_WAIT 100 | |
56 | int start; | |
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) { | |
61 | break; | |
62 | } | |
63 | } | |
64 | if (i == (start + LONG_WAIT)) { | |
65 | break; | |
66 | } | |
67 | } | |
68 | if (start == GraphTraceLen - LONG_WAIT) { | |
69 | PrintAndLog("nothing to wait for"); | |
70 | return 0; | |
71 | } | |
72 | ||
73 | GraphBuffer[start] = 2; | |
74 | GraphBuffer[start+1] = -2; | |
75 | ||
76 | uint8_t bits[64]; | |
77 | ||
78 | int bit; | |
79 | i = start; | |
80 | for (bit = 0; bit < 64; bit++) { | |
81 | int j; | |
82 | int sum = 0; | |
83 | for (j = 0; j < 16; j++) { | |
84 | sum += GraphBuffer[i++]; | |
85 | } | |
86 | if (sum > 0) { | |
87 | bits[bit] = 1; | |
88 | } else { | |
89 | bits[bit] = 0; | |
90 | } | |
91 | PrintAndLog("bit %d sum %d", bit, sum); | |
92 | } | |
93 | ||
94 | for (bit = 0; bit < 64; bit++) { | |
95 | int j; | |
96 | int sum = 0; | |
97 | for (j = 0; j < 16; j++) { | |
98 | sum += GraphBuffer[i++]; | |
99 | } | |
100 | if (sum > 0 && bits[bit] != 1) { | |
101 | PrintAndLog("oops1 at %d", bit); | |
102 | } | |
103 | if (sum < 0 && bits[bit] != 0) { | |
104 | PrintAndLog("oops2 at %d", bit); | |
105 | } | |
106 | } | |
107 | ||
108 | GraphTraceLen = 32*64; | |
109 | i = 0; | |
110 | int phase = 0; | |
111 | for (bit = 0; bit < 64; bit++) { | |
112 | if (bits[bit] == 0) { | |
113 | phase = 0; | |
114 | } else { | |
115 | phase = 1; | |
116 | } | |
117 | int j; | |
118 | for (j = 0; j < 32; j++) { | |
119 | GraphBuffer[i++] = phase; | |
120 | phase = !phase; | |
121 | } | |
122 | } | |
123 | ||
124 | RepaintGraphWindow(); | |
125 | return 0; | |
126 | } | |
127 | ||
128 | int CmdIndalaDemod(const char *Cmd) | |
129 | { | |
130 | // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID | |
131 | ||
132 | int state = -1; | |
133 | int count = 0; | |
134 | int i, j; | |
135 | // worst case with GraphTraceLen=64000 is < 4096 | |
136 | // under normal conditions it's < 2048 | |
137 | uint8_t rawbits[4096]; | |
138 | int rawbit = 0; | |
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) { | |
142 | count += 1; | |
143 | if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) { | |
144 | if (state == 0) { | |
145 | for (j = 0; j < count - 8; j += 16) { | |
146 | rawbits[rawbit++] = 0; | |
147 | } | |
148 | if ((abs(count - j)) > worst) { | |
149 | worst = abs(count - j); | |
150 | worstPos = i; | |
151 | } | |
152 | } | |
153 | state = 1; | |
154 | count = 0; | |
155 | } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) { | |
156 | if (state == 1) { | |
157 | for (j = 0; j < count - 8; j += 16) { | |
158 | rawbits[rawbit++] = 1; | |
159 | } | |
160 | if ((abs(count - j)) > worst) { | |
161 | worst = abs(count - j); | |
162 | worstPos = i; | |
163 | } | |
164 | } | |
165 | state = 0; | |
166 | count = 0; | |
167 | } | |
168 | } | |
169 | PrintAndLog("Recovered %d raw bits", rawbit); | |
170 | PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos); | |
171 | ||
172 | // Finding the start of a UID | |
173 | int uidlen, long_wait; | |
174 | if (strcmp(Cmd, "224") == 0) { | |
175 | uidlen = 224; | |
176 | long_wait = 30; | |
177 | } else { | |
178 | uidlen = 64; | |
179 | long_wait = 29; | |
180 | } | |
181 | int start; | |
182 | int first = 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) { | |
187 | break; | |
188 | } | |
189 | } | |
190 | if (i == (start + long_wait)) { | |
191 | break; | |
192 | } | |
193 | } | |
194 | if (start == rawbit - uidlen + 1) { | |
195 | PrintAndLog("nothing to wait for"); | |
196 | return 0; | |
197 | } | |
198 | ||
199 | // Inverting signal if needed | |
200 | if (first == 1) { | |
201 | for (i = start; i < rawbit; i++) { | |
202 | rawbits[i] = !rawbits[i]; | |
203 | } | |
204 | } | |
205 | ||
206 | // Dumping UID | |
207 | uint8_t bits[224]; | |
208 | char showbits[225]; | |
209 | showbits[uidlen]='\0'; | |
210 | int bit; | |
211 | i = start; | |
212 | int times = 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]; | |
219 | } | |
220 | showbits[bit+1]='\0'; | |
221 | PrintAndLog("Partial UID=%s", showbits); | |
222 | return 0; | |
223 | } else { | |
224 | for (bit = 0; bit < uidlen; bit++) { | |
225 | bits[bit] = rawbits[i++]; | |
226 | showbits[bit] = '0' + bits[bit]; | |
227 | } | |
228 | times = 1; | |
229 | } | |
2414f978 | 230 | |
231 | //convert UID to HEX | |
232 | uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; | |
233 | int idx; | |
234 | uid1=0; | |
235 | uid2=0; | |
236 | if (uidlen==64){ | |
237 | for( idx=0; idx<64; idx++) { | |
238 | if (showbits[idx] == '0') { | |
239 | uid1=(uid1<<1)|(uid2>>31); | |
240 | uid2=(uid2<<1)|0; | |
241 | } else { | |
242 | uid1=(uid1<<1)|(uid2>>31); | |
243 | uid2=(uid2<<1)|1; | |
244 | } | |
245 | } | |
246 | PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2); | |
247 | } | |
248 | else { | |
249 | uid3=0; | |
250 | uid4=0; | |
251 | uid5=0; | |
252 | uid6=0; | |
253 | uid7=0; | |
254 | for( idx=0; idx<224; idx++) { | |
255 | uid1=(uid1<<1)|(uid2>>31); | |
256 | uid2=(uid2<<1)|(uid3>>31); | |
257 | uid3=(uid3<<1)|(uid4>>31); | |
258 | uid4=(uid4<<1)|(uid5>>31); | |
259 | uid5=(uid5<<1)|(uid6>>31); | |
260 | uid6=(uid6<<1)|(uid7>>31); | |
261 | if (showbits[idx] == '0') uid7=(uid7<<1)|0; | |
262 | else uid7=(uid7<<1)|1; | |
263 | } | |
264 | PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7); | |
265 | } | |
7fe9b0b7 | 266 | |
267 | // Checking UID against next occurences | |
268 | for (; i + uidlen <= rawbit;) { | |
269 | int failed = 0; | |
270 | for (bit = 0; bit < uidlen; bit++) { | |
271 | if (bits[bit] != rawbits[i++]) { | |
272 | failed = 1; | |
273 | break; | |
274 | } | |
275 | } | |
276 | if (failed == 1) { | |
277 | break; | |
278 | } | |
279 | times += 1; | |
280 | } | |
281 | PrintAndLog("Occurences: %d (expected %d)", times, (rawbit - start) / uidlen); | |
282 | ||
283 | // Remodulating for tag cloning | |
284 | GraphTraceLen = 32*uidlen; | |
285 | i = 0; | |
286 | int phase = 0; | |
287 | for (bit = 0; bit < uidlen; bit++) { | |
288 | if (bits[bit] == 0) { | |
289 | phase = 0; | |
290 | } else { | |
291 | phase = 1; | |
292 | } | |
293 | int j; | |
294 | for (j = 0; j < 32; j++) { | |
295 | GraphBuffer[i++] = phase; | |
296 | phase = !phase; | |
297 | } | |
298 | } | |
299 | ||
300 | RepaintGraphWindow(); | |
301 | return 0; | |
302 | } | |
303 | ||
2414f978 | 304 | int CmdIndalaClone(const char *Cmd) |
305 | { | |
306 | unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7; | |
307 | UsbCommand c; | |
308 | uid1=0; | |
309 | uid2=0; | |
310 | uid3=0; | |
311 | uid4=0; | |
312 | uid5=0; | |
313 | uid6=0; | |
314 | uid7=0; | |
315 | int n = 0, i = 0; | |
316 | ||
317 | if (strchr(Cmd,'l') != 0) { | |
318 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
319 | uid1 = (uid1 << 4) | (uid2 >> 28); | |
320 | uid2 = (uid2 << 4) | (uid3 >> 28); | |
321 | uid3 = (uid3 << 4) | (uid4 >> 28); | |
322 | uid4 = (uid4 << 4) | (uid5 >> 28); | |
323 | uid5 = (uid5 << 4) | (uid6 >> 28); | |
324 | uid6 = (uid6 << 4) | (uid7 >> 28); | |
325 | uid7 = (uid7 << 4) | (n & 0xf); | |
326 | } | |
327 | PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7); | |
328 | c.cmd = CMD_INDALA_CLONE_TAG_L; | |
329 | c.d.asDwords[0] = uid1; | |
330 | c.d.asDwords[1] = uid2; | |
331 | c.d.asDwords[2] = uid3; | |
332 | c.d.asDwords[3] = uid4; | |
333 | c.d.asDwords[4] = uid5; | |
334 | c.d.asDwords[5] = uid6; | |
335 | c.d.asDwords[6] = uid7; | |
336 | } | |
337 | else | |
338 | { | |
339 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
340 | uid1 = (uid1 << 4) | (uid2 >> 28); | |
341 | uid2 = (uid2 << 4) | (n & 0xf); | |
342 | } | |
343 | PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2); | |
344 | c.cmd = CMD_INDALA_CLONE_TAG; | |
345 | c.arg[0] = uid1; | |
346 | c.arg[1] = uid2; | |
347 | } | |
348 | ||
349 | SendCommand(&c); | |
350 | return 0; | |
351 | } | |
352 | ||
7fe9b0b7 | 353 | int CmdLFRead(const char *Cmd) |
354 | { | |
355 | UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K}; | |
356 | // 'h' means higher-low-frequency, 134 kHz | |
357 | if(*Cmd == 'h') { | |
358 | c.arg[0] = 1; | |
359 | } else if (*Cmd == '\0') { | |
360 | c.arg[0] = 0; | |
361 | } else { | |
362 | PrintAndLog("use 'read' or 'read h'"); | |
363 | return 0; | |
364 | } | |
365 | SendCommand(&c); | |
bdd1de1b | 366 | WaitForResponse(CMD_ACK); |
7fe9b0b7 | 367 | return 0; |
368 | } | |
369 | ||
370 | static void ChkBitstream(const char *str) | |
371 | { | |
372 | int i; | |
373 | ||
374 | /* convert to bitstream if necessary */ | |
375 | for (i = 0; i < (int)(GraphTraceLen / 2); i++) | |
376 | { | |
377 | if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) | |
378 | { | |
379 | CmdBitstream(str); | |
380 | break; | |
381 | } | |
382 | } | |
383 | } | |
384 | ||
385 | int CmdLFSim(const char *Cmd) | |
386 | { | |
387 | int i; | |
388 | static int gap; | |
389 | ||
390 | sscanf(Cmd, "%i", &gap); | |
391 | ||
392 | /* convert to bitstream if necessary */ | |
393 | ChkBitstream(Cmd); | |
394 | ||
395 | PrintAndLog("Sending data, please wait..."); | |
396 | for (i = 0; i < GraphTraceLen; i += 48) { | |
397 | UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}}; | |
398 | int j; | |
399 | for (j = 0; j < 48; j++) { | |
400 | c.d.asBytes[j] = GraphBuffer[i+j]; | |
401 | } | |
402 | SendCommand(&c); | |
403 | WaitForResponse(CMD_ACK); | |
404 | } | |
405 | ||
406 | PrintAndLog("Starting simulator..."); | |
407 | UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}}; | |
408 | SendCommand(&c); | |
409 | return 0; | |
410 | } | |
411 | ||
412 | int CmdLFSimBidir(const char *Cmd) | |
413 | { | |
414 | /* Set ADC to twice the carrier for a slight supersampling */ | |
415 | UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}}; | |
416 | SendCommand(&c); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */ | |
421 | int CmdLFSimManchester(const char *Cmd) | |
422 | { | |
423 | static int clock, gap; | |
424 | static char data[1024], gapstring[8]; | |
425 | ||
426 | /* get settings/bits */ | |
427 | sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap); | |
428 | ||
429 | /* clear our graph */ | |
430 | ClearGraph(0); | |
431 | ||
432 | /* fill it with our bitstream */ | |
433 | for (int i = 0; i < strlen(data) ; ++i) | |
434 | AppendGraph(0, clock, data[i]- '0'); | |
435 | ||
436 | /* modulate */ | |
437 | CmdManchesterMod(""); | |
438 | ||
439 | /* show what we've done */ | |
440 | RepaintGraphWindow(); | |
441 | ||
442 | /* simulate */ | |
443 | sprintf(&gapstring[0], "%i", gap); | |
444 | CmdLFSim(gapstring); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | int CmdVchDemod(const char *Cmd) | |
449 | { | |
450 | // Is this the entire sync pattern, or does this also include some | |
451 | // data bits that happen to be the same everywhere? That would be | |
452 | // lovely to know. | |
453 | static const int SyncPattern[] = { | |
454 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
455 | 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
456 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
457 | 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
458 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
459 | 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
460 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
461 | 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
462 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
463 | 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
464 | }; | |
465 | ||
466 | // So first, we correlate for the sync pattern, and mark that. | |
467 | int bestCorrel = 0, bestPos = 0; | |
468 | int i; | |
469 | // It does us no good to find the sync pattern, with fewer than | |
470 | // 2048 samples after it... | |
471 | for (i = 0; i < (GraphTraceLen-2048); i++) { | |
472 | int sum = 0; | |
473 | int j; | |
474 | for (j = 0; j < arraylen(SyncPattern); j++) { | |
475 | sum += GraphBuffer[i+j]*SyncPattern[j]; | |
476 | } | |
477 | if (sum > bestCorrel) { | |
478 | bestCorrel = sum; | |
479 | bestPos = i; | |
480 | } | |
481 | } | |
482 | PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel); | |
483 | ||
484 | char bits[257]; | |
485 | bits[256] = '\0'; | |
486 | ||
487 | int worst = INT_MAX; | |
fddf220a | 488 | int worstPos = 0; |
7fe9b0b7 | 489 | |
490 | for (i = 0; i < 2048; i += 8) { | |
491 | int sum = 0; | |
492 | int j; | |
493 | for (j = 0; j < 8; j++) { | |
494 | sum += GraphBuffer[bestPos+i+j]; | |
495 | } | |
496 | if (sum < 0) { | |
497 | bits[i/8] = '.'; | |
498 | } else { | |
499 | bits[i/8] = '1'; | |
500 | } | |
501 | if(abs(sum) < worst) { | |
502 | worst = abs(sum); | |
503 | worstPos = i; | |
504 | } | |
505 | } | |
506 | PrintAndLog("bits:"); | |
507 | PrintAndLog("%s", bits); | |
508 | PrintAndLog("worst metric: %d at pos %d", worst, worstPos); | |
509 | ||
510 | if (strcmp(Cmd, "clone")==0) { | |
511 | GraphTraceLen = 0; | |
512 | char *s; | |
513 | for(s = bits; *s; s++) { | |
514 | int j; | |
515 | for(j = 0; j < 16; j++) { | |
516 | GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0; | |
517 | } | |
518 | } | |
519 | RepaintGraphWindow(); | |
520 | } | |
521 | return 0; | |
522 | } | |
523 | ||
524 | static command_t CommandTable[] = | |
525 | { | |
526 | {"help", CmdHelp, 1, "This help"}, | |
527 | {"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)"}, | |
37239a7c | 528 | {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"}, |
7fe9b0b7 | 529 | {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"}, |
37239a7c | 530 | {"hid", CmdLFHID, 1, "{ HID RFIDs... }"}, |
7fe9b0b7 | 531 | {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"}, |
2414f978 | 532 | {"indalaclone", CmdIndalaClone, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"}, |
7fe9b0b7 | 533 | {"read", CmdLFRead, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"}, |
534 | {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"}, | |
535 | {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"}, | |
536 | {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"}, | |
37239a7c | 537 | {"ti", CmdLFTI, 1, "{ TI RFIDs... }"}, |
7fe9b0b7 | 538 | {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"}, |
539 | {NULL, NULL, 0, NULL} | |
540 | }; | |
541 | ||
542 | int CmdLF(const char *Cmd) | |
543 | { | |
544 | CmdsParse(CommandTable, Cmd); | |
545 | return 0; | |
546 | } | |
547 | ||
548 | int CmdHelp(const char *Cmd) | |
549 | { | |
550 | CmdsHelp(CommandTable); | |
551 | return 0; | |
552 | } |