]>
Commit | Line | Data |
---|---|---|
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 | // Data and Graph commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
15 | #include "proxmark3.h" | |
16 | #include "data.h" | |
17 | #include "ui.h" | |
18 | #include "graph.h" | |
19 | #include "cmdparser.h" | |
20 | #include "util.h" | |
21 | #include "cmdmain.h" | |
22 | #include "cmddata.h" | |
23 | ||
24 | ||
25 | static int CmdHelp(const char *Cmd); | |
26 | ||
27 | int CmdAmp(const char *Cmd) | |
28 | { | |
29 | int i, rising, falling; | |
30 | int max = INT_MIN, min = INT_MAX; | |
31 | ||
32 | for (i = 10; i < GraphTraceLen; ++i) { | |
33 | if (GraphBuffer[i] > max) | |
34 | max = GraphBuffer[i]; | |
35 | if (GraphBuffer[i] < min) | |
36 | min = GraphBuffer[i]; | |
37 | } | |
38 | ||
39 | if (max != min) { | |
40 | rising = falling= 0; | |
41 | for (i = 0; i < GraphTraceLen; ++i) { | |
42 | if (GraphBuffer[i + 1] < GraphBuffer[i]) { | |
43 | if (rising) { | |
44 | GraphBuffer[i] = max; | |
45 | rising = 0; | |
46 | } | |
47 | falling = 1; | |
48 | } | |
49 | if (GraphBuffer[i + 1] > GraphBuffer[i]) { | |
50 | if (falling) { | |
51 | GraphBuffer[i] = min; | |
52 | falling = 0; | |
53 | } | |
54 | rising= 1; | |
55 | } | |
56 | } | |
57 | } | |
58 | RepaintGraphWindow(); | |
59 | return 0; | |
60 | } | |
61 | ||
62 | /* | |
63 | * Generic command to demodulate ASK. | |
64 | * | |
65 | * Argument is convention: positive or negative (High mod means zero | |
66 | * or high mod means one) | |
67 | * | |
68 | * Updates the Graph trace with 0/1 values | |
69 | * | |
70 | * Arguments: | |
71 | * c : 0 or 1 | |
72 | */ | |
73 | int Cmdaskdemod(const char *Cmd) | |
74 | { | |
75 | int i; | |
76 | int c, high = 0, low = 0; | |
77 | ||
78 | sscanf(Cmd, "%i", &c); | |
79 | ||
80 | if (c != 0 && c != 1) { | |
81 | PrintAndLog("Invalid argument: %s", Cmd); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | /* Detect high and lows */ | |
86 | for (i = 0; i < GraphTraceLen; ++i) | |
87 | { | |
88 | if (GraphBuffer[i] > high) | |
89 | high = GraphBuffer[i]; | |
90 | else if (GraphBuffer[i] < low) | |
91 | low = GraphBuffer[i]; | |
92 | } | |
93 | ||
94 | if (GraphBuffer[0] > 0) { | |
95 | GraphBuffer[0] = 1-c; | |
96 | } else { | |
97 | GraphBuffer[0] = c; | |
98 | } | |
99 | for (i = 1; i < GraphTraceLen; ++i) { | |
100 | /* Transitions are detected at each peak | |
101 | * Transitions are either: | |
102 | * - we're low: transition if we hit a high | |
103 | * - we're high: transition if we hit a low | |
104 | * (we need to do it this way because some tags keep high or | |
105 | * low for long periods, others just reach the peak and go | |
106 | * down) | |
107 | */ | |
108 | if ((GraphBuffer[i] == high) && (GraphBuffer[i - 1] == c)) { | |
109 | GraphBuffer[i] = 1 - c; | |
110 | } else if ((GraphBuffer[i] == low) && (GraphBuffer[i - 1] == (1 - c))){ | |
111 | GraphBuffer[i] = c; | |
112 | } else { | |
113 | /* No transition */ | |
114 | GraphBuffer[i] = GraphBuffer[i - 1]; | |
115 | } | |
116 | } | |
117 | RepaintGraphWindow(); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | int CmdAutoCorr(const char *Cmd) | |
122 | { | |
123 | static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; | |
124 | ||
125 | int window = atoi(Cmd); | |
126 | ||
127 | if (window == 0) { | |
128 | PrintAndLog("needs a window"); | |
129 | return 0; | |
130 | } | |
131 | if (window >= GraphTraceLen) { | |
132 | PrintAndLog("window must be smaller than trace (%d samples)", | |
133 | GraphTraceLen); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | PrintAndLog("performing %d correlations", GraphTraceLen - window); | |
138 | ||
139 | for (int i = 0; i < GraphTraceLen - window; ++i) { | |
140 | int sum = 0; | |
141 | for (int j = 0; j < window; ++j) { | |
142 | sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256; | |
143 | } | |
144 | CorrelBuffer[i] = sum; | |
145 | } | |
146 | GraphTraceLen = GraphTraceLen - window; | |
147 | memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int)); | |
148 | ||
149 | RepaintGraphWindow(); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | int CmdBitsamples(const char *Cmd) | |
154 | { | |
155 | int cnt = 0; | |
156 | uint8_t got[12288]; | |
157 | ||
158 | GetFromBigBuf(got,sizeof(got),0); | |
159 | WaitForResponse(CMD_ACK,NULL); | |
160 | ||
161 | for (int j = 0; j < sizeof(got); j++) { | |
162 | for (int k = 0; k < 8; k++) { | |
163 | if(got[j] & (1 << (7 - k))) { | |
164 | GraphBuffer[cnt++] = 1; | |
165 | } else { | |
166 | GraphBuffer[cnt++] = 0; | |
167 | } | |
168 | } | |
169 | } | |
170 | GraphTraceLen = cnt; | |
171 | RepaintGraphWindow(); | |
172 | return 0; | |
173 | } | |
174 | ||
175 | /* | |
176 | * Convert to a bitstream | |
177 | */ | |
178 | int CmdBitstream(const char *Cmd) | |
179 | { | |
180 | int i, j; | |
181 | int bit; | |
182 | int gtl; | |
183 | int clock; | |
184 | int low = 0; | |
185 | int high = 0; | |
186 | int hithigh, hitlow, first; | |
187 | ||
188 | /* Detect high and lows and clock */ | |
189 | for (i = 0; i < GraphTraceLen; ++i) | |
190 | { | |
191 | if (GraphBuffer[i] > high) | |
192 | high = GraphBuffer[i]; | |
193 | else if (GraphBuffer[i] < low) | |
194 | low = GraphBuffer[i]; | |
195 | } | |
196 | ||
197 | /* Get our clock */ | |
198 | clock = GetClock(Cmd, high, 1); | |
199 | gtl = ClearGraph(0); | |
200 | ||
201 | bit = 0; | |
202 | for (i = 0; i < (int)(gtl / clock); ++i) | |
203 | { | |
204 | hithigh = 0; | |
205 | hitlow = 0; | |
206 | first = 1; | |
207 | /* Find out if we hit both high and low peaks */ | |
208 | for (j = 0; j < clock; ++j) | |
209 | { | |
210 | if (GraphBuffer[(i * clock) + j] == high) | |
211 | hithigh = 1; | |
212 | else if (GraphBuffer[(i * clock) + j] == low) | |
213 | hitlow = 1; | |
214 | /* it doesn't count if it's the first part of our read | |
215 | because it's really just trailing from the last sequence */ | |
216 | if (first && (hithigh || hitlow)) | |
217 | hithigh = hitlow = 0; | |
218 | else | |
219 | first = 0; | |
220 | ||
221 | if (hithigh && hitlow) | |
222 | break; | |
223 | } | |
224 | ||
225 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
226 | if (!hithigh || !hitlow) | |
227 | bit ^= 1; | |
228 | ||
229 | AppendGraph(0, clock, bit); | |
230 | // for (j = 0; j < (int)(clock/2); j++) | |
231 | // GraphBuffer[(i * clock) + j] = bit ^ 1; | |
232 | // for (j = (int)(clock/2); j < clock; j++) | |
233 | // GraphBuffer[(i * clock) + j] = bit; | |
234 | } | |
235 | ||
236 | RepaintGraphWindow(); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | int CmdBuffClear(const char *Cmd) | |
241 | { | |
242 | UsbCommand c = {CMD_BUFF_CLEAR}; | |
243 | SendCommand(&c); | |
244 | ClearGraph(true); | |
245 | return 0; | |
246 | } | |
247 | ||
248 | int CmdDec(const char *Cmd) | |
249 | { | |
250 | for (int i = 0; i < (GraphTraceLen / 2); ++i) | |
251 | GraphBuffer[i] = GraphBuffer[i * 2]; | |
252 | GraphTraceLen /= 2; | |
253 | PrintAndLog("decimated by 2"); | |
254 | RepaintGraphWindow(); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | /* Print our clock rate */ | |
259 | int CmdDetectClockRate(const char *Cmd) | |
260 | { | |
261 | int clock = DetectClock(0); | |
262 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | int CmdFSKdemod(const char *Cmd) | |
267 | { | |
268 | static const int LowTone[] = { | |
269 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
270 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
271 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
272 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
273 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 | |
274 | }; | |
275 | static const int HighTone[] = { | |
276 | 1, 1, 1, 1, 1, -1, -1, -1, -1, | |
277 | 1, 1, 1, 1, -1, -1, -1, -1, | |
278 | 1, 1, 1, 1, -1, -1, -1, -1, | |
279 | 1, 1, 1, 1, -1, -1, -1, -1, | |
280 | 1, 1, 1, 1, -1, -1, -1, -1, | |
281 | 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
282 | }; | |
283 | ||
284 | int lowLen = sizeof (LowTone) / sizeof (int); | |
285 | int highLen = sizeof (HighTone) / sizeof (int); | |
286 | int convLen = (highLen > lowLen) ? highLen : lowLen; | |
287 | uint32_t hi = 0, lo = 0; | |
288 | ||
289 | int i, j; | |
290 | int minMark = 0, maxMark = 0; | |
291 | ||
292 | for (i = 0; i < GraphTraceLen - convLen; ++i) { | |
293 | int lowSum = 0, highSum = 0; | |
294 | ||
295 | for (j = 0; j < lowLen; ++j) { | |
296 | lowSum += LowTone[j]*GraphBuffer[i+j]; | |
297 | } | |
298 | for (j = 0; j < highLen; ++j) { | |
299 | highSum += HighTone[j] * GraphBuffer[i + j]; | |
300 | } | |
301 | lowSum = abs(100 * lowSum / lowLen); | |
302 | highSum = abs(100 * highSum / highLen); | |
303 | GraphBuffer[i] = (highSum << 16) | lowSum; | |
304 | } | |
305 | ||
306 | for(i = 0; i < GraphTraceLen - convLen - 16; ++i) { | |
307 | int lowTot = 0, highTot = 0; | |
308 | // 10 and 8 are f_s divided by f_l and f_h, rounded | |
309 | for (j = 0; j < 10; ++j) { | |
310 | lowTot += (GraphBuffer[i+j] & 0xffff); | |
311 | } | |
312 | for (j = 0; j < 8; j++) { | |
313 | highTot += (GraphBuffer[i + j] >> 16); | |
314 | } | |
315 | GraphBuffer[i] = lowTot - highTot; | |
316 | if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i]; | |
317 | if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i]; | |
318 | } | |
319 | ||
320 | GraphTraceLen -= (convLen + 16); | |
321 | RepaintGraphWindow(); | |
322 | ||
323 | // Find bit-sync (3 lo followed by 3 high) | |
324 | int max = 0, maxPos = 0; | |
325 | for (i = 0; i < 6000; ++i) { | |
326 | int dec = 0; | |
327 | for (j = 0; j < 3 * lowLen; ++j) { | |
328 | dec -= GraphBuffer[i + j]; | |
329 | } | |
330 | for (; j < 3 * (lowLen + highLen ); ++j) { | |
331 | dec += GraphBuffer[i + j]; | |
332 | } | |
333 | if (dec > max) { | |
334 | max = dec; | |
335 | maxPos = i; | |
336 | } | |
337 | } | |
338 | ||
339 | // place start of bit sync marker in graph | |
340 | GraphBuffer[maxPos] = maxMark; | |
341 | GraphBuffer[maxPos + 1] = minMark; | |
342 | ||
343 | maxPos += j; | |
344 | ||
345 | // place end of bit sync marker in graph | |
346 | GraphBuffer[maxPos] = maxMark; | |
347 | GraphBuffer[maxPos+1] = minMark; | |
348 | ||
349 | PrintAndLog("actual data bits start at sample %d", maxPos); | |
350 | PrintAndLog("length %d/%d", highLen, lowLen); | |
351 | ||
352 | uint8_t bits[46]; | |
353 | bits[sizeof(bits)-1] = '\0'; | |
354 | ||
355 | // find bit pairs and manchester decode them | |
356 | for (i = 0; i < arraylen(bits) - 1; ++i) { | |
357 | int dec = 0; | |
358 | for (j = 0; j < lowLen; ++j) { | |
359 | dec -= GraphBuffer[maxPos + j]; | |
360 | } | |
361 | for (; j < lowLen + highLen; ++j) { | |
362 | dec += GraphBuffer[maxPos + j]; | |
363 | } | |
364 | maxPos += j; | |
365 | // place inter bit marker in graph | |
366 | GraphBuffer[maxPos] = maxMark; | |
367 | GraphBuffer[maxPos + 1] = minMark; | |
368 | ||
369 | // hi and lo form a 64 bit pair | |
370 | hi = (hi << 1) | (lo >> 31); | |
371 | lo = (lo << 1); | |
372 | // store decoded bit as binary (in hi/lo) and text (in bits[]) | |
373 | if(dec < 0) { | |
374 | bits[i] = '1'; | |
375 | lo |= 1; | |
376 | } else { | |
377 | bits[i] = '0'; | |
378 | } | |
379 | } | |
380 | PrintAndLog("bits: '%s'", bits); | |
381 | PrintAndLog("hex: %08x %08x", hi, lo); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | int CmdGrid(const char *Cmd) | |
386 | { | |
387 | sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); | |
388 | PlotGridXdefault= PlotGridX; | |
389 | PlotGridYdefault= PlotGridY; | |
390 | RepaintGraphWindow(); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | int CmdHexsamples(const char *Cmd) | |
395 | { | |
396 | int i, j; | |
397 | int requested = 0; | |
398 | int offset = 0; | |
399 | char string_buf[25]; | |
400 | char* string_ptr = string_buf; | |
401 | uint8_t got[40000]; | |
402 | ||
403 | sscanf(Cmd, "%i %i", &requested, &offset); | |
404 | ||
405 | /* if no args send something */ | |
406 | if (requested == 0) { | |
407 | requested = 8; | |
408 | } | |
409 | if (offset + requested > sizeof(got)) { | |
410 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000"); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | GetFromBigBuf(got,requested,offset); | |
415 | WaitForResponse(CMD_ACK,NULL); | |
416 | ||
417 | i = 0; | |
418 | for (j = 0; j < requested; j++) { | |
419 | i++; | |
420 | string_ptr += sprintf(string_ptr, "%02x ", got[j]); | |
421 | if (i == 8) { | |
422 | *(string_ptr - 1) = '\0'; // remove the trailing space | |
423 | PrintAndLog("%s", string_buf); | |
424 | string_buf[0] = '\0'; | |
425 | string_ptr = string_buf; | |
426 | i = 0; | |
427 | } | |
428 | if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes | |
429 | *(string_ptr - 1) = '\0'; | |
430 | PrintAndLog("%s", string_buf); | |
431 | string_buf[0] = '\0'; | |
432 | } | |
433 | } | |
434 | return 0; | |
435 | } | |
436 | ||
437 | int CmdHide(const char *Cmd) | |
438 | { | |
439 | HideGraphWindow(); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | int CmdHpf(const char *Cmd) | |
444 | { | |
445 | int i; | |
446 | int accum = 0; | |
447 | ||
448 | for (i = 10; i < GraphTraceLen; ++i) | |
449 | accum += GraphBuffer[i]; | |
450 | accum /= (GraphTraceLen - 10); | |
451 | for (i = 0; i < GraphTraceLen; ++i) | |
452 | GraphBuffer[i] -= accum; | |
453 | ||
454 | RepaintGraphWindow(); | |
455 | return 0; | |
456 | } | |
457 | ||
458 | int CmdSamples(const char *Cmd) | |
459 | { | |
460 | uint8_t got[36440] = {0x00}; | |
461 | ||
462 | int n = strtol(Cmd, NULL, 0); | |
463 | if (n == 0) | |
464 | n = 16000; | |
465 | if (n > sizeof(got)) | |
466 | n = sizeof(got); | |
467 | ||
468 | PrintAndLog("Reading %d samples from device memory\n", n); | |
469 | GetFromBigBuf(got,n,3560); | |
470 | WaitForResponse(CMD_ACK,NULL); | |
471 | for (int j = 0; j < n; ++j) { | |
472 | GraphBuffer[j] = ((int)got[j]) - 128; | |
473 | } | |
474 | GraphTraceLen = n; | |
475 | RepaintGraphWindow(); | |
476 | return 0; | |
477 | } | |
478 | int CmdTuneSamples(const char *Cmd) | |
479 | { | |
480 | int timeout = 0; | |
481 | printf("\nMeasuring antenna characteristics, please wait..."); | |
482 | ||
483 | UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING}; | |
484 | SendCommand(&c); | |
485 | ||
486 | UsbCommand resp; | |
487 | while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) { | |
488 | timeout++; | |
489 | printf("."); | |
490 | if (timeout > 7) { | |
491 | PrintAndLog("\nNo response from Proxmark. Aborting..."); | |
492 | return 1; | |
493 | } | |
494 | } | |
495 | ||
496 | int peakv, peakf; | |
497 | int vLf125, vLf134, vHf; | |
498 | vLf125 = resp.arg[0] & 0xffff; | |
499 | vLf134 = resp.arg[0] >> 16; | |
500 | vHf = resp.arg[1] & 0xffff;; | |
501 | peakf = resp.arg[2] & 0xffff; | |
502 | peakv = resp.arg[2] >> 16; | |
503 | PrintAndLog(""); | |
504 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); | |
505 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
506 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
507 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
508 | if (peakv<2000) | |
509 | PrintAndLog("# Your LF antenna is unusable."); | |
510 | else if (peakv<10000) | |
511 | PrintAndLog("# Your LF antenna is marginal."); | |
512 | if (vHf<2000) | |
513 | PrintAndLog("# Your HF antenna is unusable."); | |
514 | else if (vHf<5000) | |
515 | PrintAndLog("# Your HF antenna is marginal."); | |
516 | ||
517 | for (int i = 0; i < 256; i++) { | |
518 | GraphBuffer[i] = resp.d.asBytes[i] - 128; | |
519 | } | |
520 | ||
521 | PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n"); | |
522 | PrintAndLog("\n"); | |
523 | GraphTraceLen = 256; | |
524 | ShowGraphWindow(); | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | ||
530 | int CmdLoad(const char *Cmd) | |
531 | { | |
532 | char filename[FILE_PATH_SIZE] = {0x00}; | |
533 | int len = 0; | |
534 | ||
535 | len = strlen(Cmd); | |
536 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
537 | memcpy(filename, Cmd, len); | |
538 | ||
539 | FILE *f = fopen(filename, "r"); | |
540 | if (!f) { | |
541 | PrintAndLog("couldn't open '%s'", filename); | |
542 | return 0; | |
543 | } | |
544 | ||
545 | GraphTraceLen = 0; | |
546 | char line[80]; | |
547 | while (fgets(line, sizeof (line), f)) { | |
548 | GraphBuffer[GraphTraceLen] = atoi(line); | |
549 | GraphTraceLen++; | |
550 | } | |
551 | fclose(f); | |
552 | PrintAndLog("loaded %d samples", GraphTraceLen); | |
553 | RepaintGraphWindow(); | |
554 | return 0; | |
555 | } | |
556 | ||
557 | int CmdLtrim(const char *Cmd) | |
558 | { | |
559 | int ds = atoi(Cmd); | |
560 | ||
561 | for (int i = ds; i < GraphTraceLen; ++i) | |
562 | GraphBuffer[i-ds] = GraphBuffer[i]; | |
563 | GraphTraceLen -= ds; | |
564 | ||
565 | RepaintGraphWindow(); | |
566 | return 0; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Manchester demodulate a bitstream. The bitstream needs to be already in | |
571 | * the GraphBuffer as 0 and 1 values | |
572 | * | |
573 | * Give the clock rate as argument in order to help the sync - the algorithm | |
574 | * resyncs at each pulse anyway. | |
575 | * | |
576 | * Not optimized by any means, this is the 1st time I'm writing this type of | |
577 | * routine, feel free to improve... | |
578 | * | |
579 | * 1st argument: clock rate (as number of samples per clock rate) | |
580 | * Typical values can be 64, 32, 128... | |
581 | */ | |
582 | int CmdManchesterDemod(const char *Cmd) | |
583 | { | |
584 | int i, j, invert= 0; | |
585 | int bit; | |
586 | int clock; | |
587 | int lastval = 0; | |
588 | int low = 0; | |
589 | int high = 0; | |
590 | int hithigh, hitlow, first; | |
591 | int lc = 0; | |
592 | int bitidx = 0; | |
593 | int bit2idx = 0; | |
594 | int warnings = 0; | |
595 | ||
596 | /* check if we're inverting output */ | |
597 | if (*Cmd == 'i') | |
598 | { | |
599 | PrintAndLog("Inverting output"); | |
600 | invert = 1; | |
601 | ++Cmd; | |
602 | do | |
603 | ++Cmd; | |
604 | while(*Cmd == ' '); // in case a 2nd argument was given | |
605 | } | |
606 | ||
607 | /* Holds the decoded bitstream: each clock period contains 2 bits */ | |
608 | /* later simplified to 1 bit after manchester decoding. */ | |
609 | /* Add 10 bits to allow for noisy / uncertain traces without aborting */ | |
610 | /* int BitStream[GraphTraceLen*2/clock+10]; */ | |
611 | ||
612 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
613 | /* large array */ | |
614 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
615 | ||
616 | /* Detect high and lows */ | |
617 | for (i = 0; i < GraphTraceLen; i++) | |
618 | { | |
619 | if (GraphBuffer[i] > high) | |
620 | high = GraphBuffer[i]; | |
621 | else if (GraphBuffer[i] < low) | |
622 | low = GraphBuffer[i]; | |
623 | } | |
624 | ||
625 | /* Get our clock */ | |
626 | clock = GetClock(Cmd, high, 1); | |
627 | int tolerance = clock/4; | |
628 | ||
629 | /* Detect first transition */ | |
630 | /* Lo-Hi (arbitrary) */ | |
631 | /* skip to the first high */ | |
632 | for (i= 0; i < GraphTraceLen; i++) | |
633 | if (GraphBuffer[i] == high) | |
634 | break; | |
635 | /* now look for the first low */ | |
636 | for (; i < GraphTraceLen; i++) | |
637 | { | |
638 | if (GraphBuffer[i] == low) | |
639 | { | |
640 | lastval = i; | |
641 | break; | |
642 | } | |
643 | } | |
644 | ||
645 | /* If we're not working with 1/0s, demod based off clock */ | |
646 | if (high != 1) | |
647 | { | |
648 | PrintAndLog("Entering path A"); | |
649 | bit = 0; /* We assume the 1st bit is zero, it may not be | |
650 | * the case: this routine (I think) has an init problem. | |
651 | * Ed. | |
652 | */ | |
653 | for (; i < (int)(GraphTraceLen / clock); i++) | |
654 | { | |
655 | hithigh = 0; | |
656 | hitlow = 0; | |
657 | first = 1; | |
658 | ||
659 | /* Find out if we hit both high and low peaks */ | |
660 | for (j = 0; j < clock; j++) | |
661 | { | |
662 | if (GraphBuffer[(i * clock) + j] == high) | |
663 | hithigh = 1; | |
664 | else if (GraphBuffer[(i * clock) + j] == low) | |
665 | hitlow = 1; | |
666 | ||
667 | /* it doesn't count if it's the first part of our read | |
668 | because it's really just trailing from the last sequence */ | |
669 | if (first && (hithigh || hitlow)) | |
670 | hithigh = hitlow = 0; | |
671 | else | |
672 | first = 0; | |
673 | ||
674 | if (hithigh && hitlow) | |
675 | break; | |
676 | } | |
677 | ||
678 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
679 | if (!hithigh || !hitlow) | |
680 | bit ^= 1; | |
681 | ||
682 | BitStream[bit2idx++] = bit ^ invert; | |
683 | } | |
684 | } | |
685 | ||
686 | /* standard 1/0 bitstream */ | |
687 | else | |
688 | { | |
689 | ||
690 | /* Then detect duration between 2 successive transitions */ | |
691 | for (bitidx = 1; i < GraphTraceLen; i++) | |
692 | { | |
693 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
694 | { | |
695 | lc = i-lastval; | |
696 | lastval = i; | |
697 | ||
698 | // Error check: if bitidx becomes too large, we do not | |
699 | // have a Manchester encoded bitstream or the clock is really | |
700 | // wrong! | |
701 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
702 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
703 | return 0; | |
704 | } | |
705 | // Then switch depending on lc length: | |
706 | // Tolerance is 1/4 of clock rate (arbitrary) | |
707 | if (abs(lc-clock/2) < tolerance) { | |
708 | // Short pulse : either "1" or "0" | |
709 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
710 | } else if (abs(lc-clock) < tolerance) { | |
711 | // Long pulse: either "11" or "00" | |
712 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
713 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
714 | } else { | |
715 | // Error | |
716 | warnings++; | |
717 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); | |
718 | PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)"); | |
719 | ||
720 | if (warnings > 10) | |
721 | { | |
722 | PrintAndLog("Error: too many detection errors, aborting."); | |
723 | return 0; | |
724 | } | |
725 | } | |
726 | } | |
727 | } | |
728 | ||
729 | // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream | |
730 | // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful | |
731 | // to stop output at the final bitidx2 value, not bitidx | |
732 | ||
733 | //http://www.proxmark.org/forum/viewtopic.php?id=403 | |
734 | for (i = 1; i < bitidx; i += 2) { | |
735 | if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) { | |
736 | BitStream[bit2idx++] = 1 ^ invert; | |
737 | } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) { | |
738 | BitStream[bit2idx++] = 0 ^ invert; | |
739 | } else { | |
740 | // We cannot end up in this state, this means we are unsynchronized, | |
741 | // move up 1 bit: | |
742 | i++; | |
743 | warnings++; | |
744 | PrintAndLog("Unsynchronized, resync..."); | |
745 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
746 | ||
747 | if (warnings > 10) | |
748 | { | |
749 | PrintAndLog("Error: too many decode errors, aborting."); | |
750 | return 0; | |
751 | } | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
756 | PrintAndLog("Manchester decoded bitstream"); | |
757 | // Now output the bitstream to the scrollback by line of 16 bits | |
758 | for (i = 0; i < (bit2idx-16); i+=16) { | |
759 | PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i", | |
760 | BitStream[i], | |
761 | BitStream[i+1], | |
762 | BitStream[i+2], | |
763 | BitStream[i+3], | |
764 | BitStream[i+4], | |
765 | BitStream[i+5], | |
766 | BitStream[i+6], | |
767 | BitStream[i+7], | |
768 | BitStream[i+8], | |
769 | BitStream[i+9], | |
770 | BitStream[i+10], | |
771 | BitStream[i+11], | |
772 | BitStream[i+12], | |
773 | BitStream[i+13], | |
774 | BitStream[i+14], | |
775 | BitStream[i+15]); | |
776 | } | |
777 | return bit2idx; | |
778 | } | |
779 | ||
780 | /* Modulate our data into manchester */ | |
781 | int CmdManchesterMod(const char *Cmd) | |
782 | { | |
783 | int i, j; | |
784 | int bit, lastbit, wave; | |
785 | int clock = GetClock(Cmd, 0, 1); | |
786 | int clock1 = GetT55x7Clock( GraphBuffer, GraphTraceLen, 0 ); | |
787 | PrintAndLog("MAN MOD CLOCKS: %d ice %d", clock,clock1); | |
788 | ||
789 | int half = (int)(clock/2); | |
790 | ||
791 | wave = 0; | |
792 | lastbit = 1; | |
793 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
794 | { | |
795 | bit = GraphBuffer[i * clock] ^ 1; | |
796 | ||
797 | for (j = 0; j < half; ++j) | |
798 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave; | |
799 | for (j = half; j < clock; ++j) | |
800 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1; | |
801 | ||
802 | /* Keep track of how we start our wave and if we changed or not this time */ | |
803 | wave ^= bit ^ lastbit; | |
804 | lastbit = bit; | |
805 | } | |
806 | ||
807 | RepaintGraphWindow(); | |
808 | return 0; | |
809 | } | |
810 | ||
811 | int CmdNorm(const char *Cmd) | |
812 | { | |
813 | int i; | |
814 | int max = INT_MIN, min = INT_MAX; | |
815 | ||
816 | for (i = 10; i < GraphTraceLen; ++i) { | |
817 | if (GraphBuffer[i] > max) | |
818 | max = GraphBuffer[i]; | |
819 | if (GraphBuffer[i] < min) | |
820 | min = GraphBuffer[i]; | |
821 | } | |
822 | ||
823 | if (max != min) { | |
824 | for (i = 0; i < GraphTraceLen; ++i) { | |
825 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 1000 / | |
826 | (max - min); | |
827 | } | |
828 | } | |
829 | RepaintGraphWindow(); | |
830 | return 0; | |
831 | } | |
832 | ||
833 | int CmdPlot(const char *Cmd) | |
834 | { | |
835 | ShowGraphWindow(); | |
836 | return 0; | |
837 | } | |
838 | ||
839 | int CmdSave(const char *Cmd) | |
840 | { | |
841 | char filename[FILE_PATH_SIZE] = {0x00}; | |
842 | int len = 0; | |
843 | ||
844 | len = strlen(Cmd); | |
845 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
846 | memcpy(filename, Cmd, len); | |
847 | ||
848 | ||
849 | FILE *f = fopen(filename, "w"); | |
850 | if(!f) { | |
851 | PrintAndLog("couldn't open '%s'", filename); | |
852 | return 0; | |
853 | } | |
854 | int i; | |
855 | for (i = 0; i < GraphTraceLen; i++) { | |
856 | fprintf(f, "%d\n", GraphBuffer[i]); | |
857 | } | |
858 | fclose(f); | |
859 | PrintAndLog("saved to '%s'", Cmd); | |
860 | return 0; | |
861 | } | |
862 | ||
863 | int CmdScale(const char *Cmd) | |
864 | { | |
865 | CursorScaleFactor = atoi(Cmd); | |
866 | if (CursorScaleFactor == 0) { | |
867 | PrintAndLog("bad, can't have zero scale"); | |
868 | CursorScaleFactor = 1; | |
869 | } | |
870 | RepaintGraphWindow(); | |
871 | return 0; | |
872 | } | |
873 | ||
874 | int CmdThreshold(const char *Cmd) | |
875 | { | |
876 | int threshold = atoi(Cmd); | |
877 | ||
878 | for (int i = 0; i < GraphTraceLen; ++i) { | |
879 | if (GraphBuffer[i] >= threshold) | |
880 | GraphBuffer[i] = 1; | |
881 | else | |
882 | GraphBuffer[i] = -1; | |
883 | } | |
884 | RepaintGraphWindow(); | |
885 | return 0; | |
886 | } | |
887 | ||
888 | int CmdDirectionalThreshold(const char *Cmd) | |
889 | { | |
890 | int8_t upThres = param_get8(Cmd, 0); | |
891 | int8_t downThres = param_get8(Cmd, 1); | |
892 | ||
893 | printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); | |
894 | ||
895 | int lastValue = GraphBuffer[0]; | |
896 | GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. | |
897 | ||
898 | for (int i = 1; i < GraphTraceLen; ++i) { | |
899 | // Apply first threshold to samples heading up | |
900 | if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue) | |
901 | { | |
902 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
903 | GraphBuffer[i] = 1; | |
904 | } | |
905 | // Apply second threshold to samples heading down | |
906 | else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue) | |
907 | { | |
908 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
909 | GraphBuffer[i] = -1; | |
910 | } | |
911 | else | |
912 | { | |
913 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
914 | GraphBuffer[i] = GraphBuffer[i-1]; | |
915 | ||
916 | } | |
917 | } | |
918 | GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample. | |
919 | RepaintGraphWindow(); | |
920 | return 0; | |
921 | } | |
922 | ||
923 | int CmdZerocrossings(const char *Cmd) | |
924 | { | |
925 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
926 | CmdHpf(""); | |
927 | ||
928 | int sign = 1; | |
929 | int zc = 0; | |
930 | int lastZc = 0; | |
931 | ||
932 | for (int i = 0; i < GraphTraceLen; ++i) { | |
933 | if (GraphBuffer[i] * sign >= 0) { | |
934 | // No change in sign, reproduce the previous sample count. | |
935 | zc++; | |
936 | GraphBuffer[i] = lastZc; | |
937 | } else { | |
938 | // Change in sign, reset the sample count. | |
939 | sign = -sign; | |
940 | GraphBuffer[i] = lastZc; | |
941 | if (sign > 0) { | |
942 | lastZc = zc; | |
943 | zc = 0; | |
944 | } | |
945 | } | |
946 | } | |
947 | ||
948 | RepaintGraphWindow(); | |
949 | return 0; | |
950 | } | |
951 | ||
952 | static command_t CommandTable[] = | |
953 | { | |
954 | {"help", CmdHelp, 1, "This help"}, | |
955 | {"amp", CmdAmp, 1, "Amplify peaks"}, | |
956 | {"askdemod", Cmdaskdemod, 1, "<0|1> -- Attempt to demodulate simple ASK tags"}, | |
957 | {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"}, | |
958 | {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"}, | |
959 | {"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"}, | |
960 | {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"}, | |
961 | {"dec", CmdDec, 1, "Decimate samples"}, | |
962 | {"detectclock", CmdDetectClockRate, 1, "Detect clock rate"}, | |
963 | {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."}, | |
964 | {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"}, | |
965 | {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"}, | |
966 | {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"}, | |
967 | {"hide", CmdHide, 1, "Hide graph window"}, | |
968 | {"hpf", CmdHpf, 1, "Remove DC offset from trace"}, | |
969 | {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"}, | |
970 | {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"}, | |
971 | {"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"}, | |
972 | {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"}, | |
973 | {"norm", CmdNorm, 1, "Normalize max/min to +/-500"}, | |
974 | {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"}, | |
975 | {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window"}, | |
976 | {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"}, | |
977 | {"scale", CmdScale, 1, "<int> -- Set cursor display scale"}, | |
978 | {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"}, | |
979 | {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"}, | |
980 | {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"}, | |
981 | {NULL, NULL, 0, NULL} | |
982 | }; | |
983 | ||
984 | int CmdData(const char *Cmd) | |
985 | { | |
986 | CmdsParse(CommandTable, Cmd); | |
987 | return 0; | |
988 | } | |
989 | ||
990 | int CmdHelp(const char *Cmd) | |
991 | { | |
992 | CmdsHelp(CommandTable); | |
993 | return 0; | |
994 | } |