]>
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 | // Data and Graph commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
902cb3c0 | 15 | #include "proxmark3.h" |
7fe9b0b7 | 16 | #include "data.h" |
17 | #include "ui.h" | |
18 | #include "graph.h" | |
19 | #include "cmdparser.h" | |
d51b2eda | 20 | #include "util.h" |
7fe9b0b7 | 21 | #include "cmdmain.h" |
22 | #include "cmddata.h" | |
6ff6ade2 | 23 | #include "lfdemod.h" |
c15d2bdc | 24 | |
7fe9b0b7 | 25 | static int CmdHelp(const char *Cmd); |
26 | ||
27 | int CmdAmp(const char *Cmd) | |
28 | { | |
8d0a3e87 | 29 | int i, rising, falling; |
30 | int max = INT_MIN, min = INT_MAX; | |
31 | ||
32 | DetectHighLowInGraph( &max, &min, FALSE); | |
33 | ||
34 | if (max != min) { | |
35 | rising = falling = 0; | |
36 | ||
37 | for (i = 0; i < GraphTraceLen; ++i) { | |
38 | if (GraphBuffer[i + 1] < GraphBuffer[i]) { | |
39 | if (rising) { | |
40 | GraphBuffer[i] = max; | |
41 | rising = 0; | |
42 | } | |
43 | falling = 1; | |
44 | } | |
45 | if (GraphBuffer[i + 1] > GraphBuffer[i]) { | |
46 | if (falling) { | |
47 | GraphBuffer[i] = min; | |
48 | falling = 0; | |
49 | } | |
50 | rising= 1; | |
51 | } | |
52 | } | |
53 | } | |
54 | RepaintGraphWindow(); | |
55 | return 0; | |
7fe9b0b7 | 56 | } |
57 | ||
58 | /* | |
59 | * Generic command to demodulate ASK. | |
60 | * | |
61 | * Argument is convention: positive or negative (High mod means zero | |
62 | * or high mod means one) | |
63 | * | |
64 | * Updates the Graph trace with 0/1 values | |
65 | * | |
66 | * Arguments: | |
67 | * c : 0 or 1 | |
68 | */ | |
1b492a97 | 69 | //this method is dependant on all highs and lows to be the same(or clipped) this creates issues[marshmellow] it also ignores the clock |
7fe9b0b7 | 70 | int Cmdaskdemod(const char *Cmd) |
71 | { | |
8d0a3e87 | 72 | int i; |
73 | int c, high = 0, low = 0; | |
7fe9b0b7 | 74 | |
8d0a3e87 | 75 | sscanf(Cmd, "%i", &c); |
7fe9b0b7 | 76 | |
8d0a3e87 | 77 | if (c != 0 && c != 1) { |
78 | PrintAndLog("Invalid argument: %s", Cmd); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | DetectHighLowInGraph( &high, &low, FALSE); | |
83 | ||
84 | high = abs(high * .75); | |
85 | low = abs(low * .75); | |
86 | ||
87 | //prime loop | |
88 | if (GraphBuffer[0] > 0) { | |
89 | GraphBuffer[0] = 1-c; | |
90 | } else { | |
91 | GraphBuffer[0] = c; | |
92 | } | |
93 | ||
94 | for (i = 1; i < GraphTraceLen; ++i) { | |
95 | /* Transitions are detected at each peak | |
96 | * Transitions are either: | |
97 | * - we're low: transition if we hit a high | |
98 | * - we're high: transition if we hit a low | |
99 | * (we need to do it this way because some tags keep high or | |
100 | * low for long periods, others just reach the peak and go | |
101 | * down) | |
102 | */ | |
d3a22c7d | 103 | //[marhsmellow] change == to >= for high and <= for low for fuzz |
8d0a3e87 | 104 | if ((GraphBuffer[i] == high) && (GraphBuffer[i - 1] == c)) { |
105 | GraphBuffer[i] = 1 - c; | |
106 | } else if ((GraphBuffer[i] == low) && (GraphBuffer[i - 1] == (1 - c))){ | |
107 | GraphBuffer[i] = c; | |
108 | } else { | |
109 | /* No transition */ | |
110 | GraphBuffer[i] = GraphBuffer[i - 1]; | |
111 | } | |
112 | } | |
7fe9b0b7 | 113 | RepaintGraphWindow(); |
114 | return 0; | |
115 | } | |
116 | ||
8d0a3e87 | 117 | void printBitStream(uint8_t bits[], uint32_t bitLen){ |
118 | ||
119 | uint32_t i = 0; | |
120 | if (bitLen < 16) { | |
121 | PrintAndLog("Too few bits found: %d",bitLen); | |
122 | return; | |
123 | } | |
124 | if (bitLen > 512) | |
125 | bitLen = 512; | |
126 | ||
127 | if ( ( bitLen % 16 ) > 0) { | |
128 | bitLen = ((bitLen / 16) * 16); | |
129 | PrintAndLog("ICE: equally divided with 16 = %d",bitLen); | |
130 | } | |
131 | ||
132 | for (i = 0; i <= ( bitLen - 16); i += 16) { | |
133 | PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i", | |
134 | bits[i], | |
135 | bits[i+1], | |
136 | bits[i+2], | |
137 | bits[i+3], | |
138 | bits[i+4], | |
139 | bits[i+5], | |
140 | bits[i+6], | |
141 | bits[i+7], | |
142 | bits[i+8], | |
143 | bits[i+9], | |
144 | bits[i+10], | |
145 | bits[i+11], | |
146 | bits[i+12], | |
147 | bits[i+13], | |
148 | bits[i+14], | |
149 | bits[i+15]); | |
150 | } | |
1b492a97 | 151 | return; |
152 | } | |
8d0a3e87 | 153 | |
154 | void printEM410x(uint64_t id) { | |
155 | ||
156 | if ( id <= 0 ) return; | |
157 | ||
158 | uint64_t id2lo = 0; | |
159 | uint32_t i,j; | |
160 | i = j = 0; | |
161 | ||
162 | for (j = 5; j > 0; j--){ | |
163 | for (i = 0; i < 8; i++){ | |
164 | id2lo = ( id2lo << 1LL)|((id & ( 1 << ( i +( ( j-1 ) * 8 )))) >> ( i + (( j-1) *8 ))); | |
165 | } | |
166 | } | |
167 | //output em id | |
168 | PrintAndLog("EM TAG ID : %010llx", id); | |
169 | PrintAndLog("Unique TAG ID: %010llx", id2lo); | |
170 | PrintAndLog("DEZ 8 : %08lld", id & 0xFFFFFF); | |
171 | PrintAndLog("DEZ 10 : %010lld", id & 0xFFFFFF); | |
172 | PrintAndLog("DEZ 5.5 : %05lld.%05lld", (id>>16LL) & 0xFFFF, (id & 0xFFFF)); | |
173 | PrintAndLog("DEZ 3.5A : %03lld.%05lld", (id>>32ll), (id & 0xFFFF)); | |
174 | PrintAndLog("DEZ 14/IK2 : %014lld", id); | |
175 | PrintAndLog("DEZ 15/IK3 : %015lld", id2lo); | |
176 | PrintAndLog("Other : %05lld_%03lld_%08lld", (id & 0xFFFF), (( id >> 16LL) & 0xFF), (id & 0xFFFFFF)); | |
1b492a97 | 177 | } |
178 | ||
6ff6ade2 | 179 | int CmdEm410xDecode(const char *Cmd) |
180 | { | |
8d0a3e87 | 181 | uint64_t id = 0; |
182 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
183 | uint32_t len = GetFromGraphBuf(bits); | |
184 | id = Em410xDecode(bits, len); | |
185 | printEM410x(id); | |
186 | if ( id > 0 ) | |
187 | return 1; | |
188 | return 0; | |
6ff6ade2 | 189 | } |
1b492a97 | 190 | |
191 | //by marshmellow | |
192 | //takes 2 arguments - clock and invert both as integers | |
6ff6ade2 | 193 | //attempts to demodulate ask while decoding manchester |
1b492a97 | 194 | //prints binary found and saves in graphbuffer for further commands |
195 | int Cmdaskmandemod(const char *Cmd) | |
196 | { | |
8d0a3e87 | 197 | int invert = 0; |
198 | int clk = 0; | |
6ff6ade2 | 199 | |
8d0a3e87 | 200 | sscanf(Cmd, "%i %i", &clk, &invert); |
6ff6ade2 | 201 | |
8d0a3e87 | 202 | if (invert != 0 && invert != 1) { |
203 | PrintAndLog("Invalid argument: %s", Cmd); | |
204 | return 0; | |
205 | } | |
206 | ||
207 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
208 | uint32_t len = GetFromGraphBuf(bits); | |
209 | ||
210 | int errCnt = askmandemod(bits, &len, &clk, &invert); | |
211 | ||
212 | if (errCnt < 0) return 0; | |
213 | if (len < 16) return 0; | |
214 | ||
215 | PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,len); | |
216 | ||
217 | if (errCnt > 0){ | |
218 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
219 | } | |
220 | ||
221 | PrintAndLog("ASK/Manchester decoded bitstream:"); | |
222 | ||
223 | printBitStream(bits, len); | |
8d0a3e87 | 224 | |
f0cf62cd | 225 | uint64_t tagid = Em410xDecode(bits, len); |
226 | ||
227 | if (tagid > 0){ | |
228 | SetGraphBuf(bits, len); | |
229 | printEM410x(tagid); | |
8d0a3e87 | 230 | return 1; |
231 | } | |
232 | return 0; | |
6ff6ade2 | 233 | } |
234 | ||
235 | //by marshmellow | |
236 | //manchester decode | |
237 | //stricktly take 10 and 01 and convert to 0 and 1 | |
238 | int Cmdmandecoderaw(const char *Cmd) | |
239 | { | |
8d0a3e87 | 240 | int i = 0; |
241 | int errCnt = 0; | |
242 | int bitnum = 0; | |
243 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
244 | int high = 0, low = 0; | |
245 | ||
246 | for (; i < GraphTraceLen; ++i){ | |
247 | if (GraphBuffer[i] > high) high = GraphBuffer[i]; | |
248 | else if (GraphBuffer[i] < low) low = GraphBuffer[i]; | |
249 | bits[i] = GraphBuffer[i]; | |
250 | } | |
251 | ||
252 | if (high > 1 || low < 0 ){ | |
253 | PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode"); | |
254 | return 0; | |
255 | } | |
256 | ||
257 | bitnum = i; | |
258 | errCnt = manrawdecode(bits, &bitnum); | |
259 | ||
260 | if (errCnt>=20){ | |
261 | PrintAndLog("Too many errors: %d",errCnt); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt); | |
266 | printBitStream(bits,bitnum); | |
267 | ||
268 | if (errCnt==0){ | |
269 | //put back in graphbuffer | |
270 | SetGraphBuf(bits, bitnum); | |
271 | ||
272 | uint64_t id = Em410xDecode(bits,i); | |
273 | printEM410x(id); | |
274 | } | |
275 | return 1; | |
6ff6ade2 | 276 | } |
277 | ||
278 | //by marshmellow | |
279 | //biphase decode | |
280 | //take 01 or 10 = 0 and 11 or 00 = 1 | |
281 | //takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit | |
282 | // since it is not like manchester and doesn't have an incorrect bit pattern we | |
283 | // cannot determine if our decode is correct or if it should be shifted by one bit | |
284 | // the argument offset allows us to manually shift if the output is incorrect | |
285 | // (better would be to demod and decode at the same time so we can distinguish large | |
286 | // width waves vs small width waves to help the decode positioning) or askbiphdemod | |
287 | int CmdBiphaseDecodeRaw(const char *Cmd) | |
288 | { | |
8d0a3e87 | 289 | int i = 0; |
290 | int errCnt = 0; | |
291 | int bitnum = 0; | |
292 | int offset = 0; | |
293 | int high = 0, low = 0; | |
294 | sscanf(Cmd, "%i", &offset); | |
295 | ||
296 | uint8_t bits[MAX_GRAPH_TRACE_LEN]={0}; | |
297 | ||
298 | //get graphbuffer & high and low | |
299 | for (; i<GraphTraceLen; ++i){ | |
300 | if (GraphBuffer[i] > high) high = GraphBuffer[i]; | |
301 | else if (GraphBuffer[i] < low) low = GraphBuffer[i]; | |
302 | bits[i] = GraphBuffer[i]; | |
303 | } | |
304 | if (high > 1 || low < 0){ | |
305 | PrintAndLog("Error: please raw demod the wave first then decode"); | |
306 | return 0; | |
307 | } | |
308 | bitnum = i; | |
309 | errCnt = BiphaseRawDecode(bits, &bitnum, offset); | |
310 | if (errCnt >= 20){ | |
311 | PrintAndLog("Too many errors attempting to decode: %d", errCnt); | |
312 | return 0; | |
313 | } | |
314 | PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:", offset, errCnt); | |
315 | printBitStream(bits, bitnum); | |
316 | PrintAndLog("\nif bitstream does not look right try offset=1"); | |
317 | return 1; | |
6ff6ade2 | 318 | } |
319 | ||
320 | ||
321 | //by marshmellow | |
322 | //takes 2 arguments - clock and invert both as integers | |
323 | //attempts to demodulate ask only | |
324 | //prints binary found and saves in graphbuffer for further commands | |
325 | int Cmdaskrawdemod(const char *Cmd) | |
326 | { | |
8d0a3e87 | 327 | int invert = 0; |
328 | int clk = 0; | |
329 | ||
330 | sscanf(Cmd, "%i %i", &clk, &invert); | |
331 | ||
332 | if (invert != 0 && invert != 1 ) { | |
333 | PrintAndLog("Invalid argument: %s", Cmd); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | if ( clock < 0 ) { | |
338 | PrintAndLog("Wrong clock argument"); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
343 | int len = GetFromGraphBuf(bits); | |
344 | int errCnt = 0; | |
345 | ||
346 | errCnt = askrawdemod(bits, &len, &clk, &invert); | |
347 | ||
348 | //throw away static - allow 1 and -1 (in case of threshold command first) | |
349 | if (errCnt == -1) { | |
350 | PrintAndLog("no data found"); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | if (len < 16) return 0; | |
355 | ||
356 | PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,len); | |
357 | ||
358 | //move BitStream back to GraphBuffer | |
359 | SetGraphBuf(bits, len); | |
360 | ||
361 | if (errCnt > 0){ | |
362 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
363 | } | |
364 | ||
365 | PrintAndLog("ASK demoded bitstream:"); | |
366 | ||
367 | // Now output the bitstream to the scrollback by line of 16 bits | |
368 | printBitStream(bits,len); | |
369 | return 1; | |
1b492a97 | 370 | } |
371 | ||
7fe9b0b7 | 372 | int CmdAutoCorr(const char *Cmd) |
373 | { | |
374 | static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; | |
375 | ||
376 | int window = atoi(Cmd); | |
377 | ||
378 | if (window == 0) { | |
379 | PrintAndLog("needs a window"); | |
380 | return 0; | |
381 | } | |
382 | if (window >= GraphTraceLen) { | |
383 | PrintAndLog("window must be smaller than trace (%d samples)", | |
384 | GraphTraceLen); | |
385 | return 0; | |
386 | } | |
387 | ||
388 | PrintAndLog("performing %d correlations", GraphTraceLen - window); | |
389 | ||
390 | for (int i = 0; i < GraphTraceLen - window; ++i) { | |
391 | int sum = 0; | |
392 | for (int j = 0; j < window; ++j) { | |
393 | sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256; | |
394 | } | |
395 | CorrelBuffer[i] = sum; | |
396 | } | |
397 | GraphTraceLen = GraphTraceLen - window; | |
398 | memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int)); | |
399 | ||
400 | RepaintGraphWindow(); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | int CmdBitsamples(const char *Cmd) | |
405 | { | |
406 | int cnt = 0; | |
8d0a3e87 | 407 | uint8_t got[10000]; |
90d74dc2 | 408 | |
409 | GetFromBigBuf(got,sizeof(got),0); | |
410 | WaitForResponse(CMD_ACK,NULL); | |
7fe9b0b7 | 411 | |
90d74dc2 | 412 | for (int j = 0; j < sizeof(got); j++) { |
7fe9b0b7 | 413 | for (int k = 0; k < 8; k++) { |
90d74dc2 | 414 | if(got[j] & (1 << (7 - k))) { |
7fe9b0b7 | 415 | GraphBuffer[cnt++] = 1; |
416 | } else { | |
417 | GraphBuffer[cnt++] = 0; | |
418 | } | |
419 | } | |
7fe9b0b7 | 420 | } |
421 | GraphTraceLen = cnt; | |
422 | RepaintGraphWindow(); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | /* | |
427 | * Convert to a bitstream | |
428 | */ | |
429 | int CmdBitstream(const char *Cmd) | |
430 | { | |
431 | int i, j; | |
432 | int bit; | |
433 | int gtl; | |
434 | int clock; | |
435 | int low = 0; | |
436 | int high = 0; | |
437 | int hithigh, hitlow, first; | |
438 | ||
439 | /* Detect high and lows and clock */ | |
8d0a3e87 | 440 | DetectHighLowInGraph( &high, &low, FALSE); |
7fe9b0b7 | 441 | |
442 | /* Get our clock */ | |
8d0a3e87 | 443 | clock = GetClock(Cmd, 0); |
7fe9b0b7 | 444 | gtl = ClearGraph(0); |
445 | ||
446 | bit = 0; | |
447 | for (i = 0; i < (int)(gtl / clock); ++i) | |
448 | { | |
449 | hithigh = 0; | |
450 | hitlow = 0; | |
451 | first = 1; | |
452 | /* Find out if we hit both high and low peaks */ | |
453 | for (j = 0; j < clock; ++j) | |
454 | { | |
455 | if (GraphBuffer[(i * clock) + j] == high) | |
456 | hithigh = 1; | |
457 | else if (GraphBuffer[(i * clock) + j] == low) | |
458 | hitlow = 1; | |
459 | /* it doesn't count if it's the first part of our read | |
460 | because it's really just trailing from the last sequence */ | |
461 | if (first && (hithigh || hitlow)) | |
462 | hithigh = hitlow = 0; | |
463 | else | |
464 | first = 0; | |
465 | ||
466 | if (hithigh && hitlow) | |
467 | break; | |
468 | } | |
469 | ||
470 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
471 | if (!hithigh || !hitlow) | |
472 | bit ^= 1; | |
473 | ||
474 | AppendGraph(0, clock, bit); | |
7fe9b0b7 | 475 | } |
476 | ||
477 | RepaintGraphWindow(); | |
478 | return 0; | |
479 | } | |
480 | ||
481 | int CmdBuffClear(const char *Cmd) | |
482 | { | |
483 | UsbCommand c = {CMD_BUFF_CLEAR}; | |
484 | SendCommand(&c); | |
485 | ClearGraph(true); | |
486 | return 0; | |
487 | } | |
488 | ||
489 | int CmdDec(const char *Cmd) | |
490 | { | |
491 | for (int i = 0; i < (GraphTraceLen / 2); ++i) | |
492 | GraphBuffer[i] = GraphBuffer[i * 2]; | |
493 | GraphTraceLen /= 2; | |
494 | PrintAndLog("decimated by 2"); | |
495 | RepaintGraphWindow(); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | /* Print our clock rate */ | |
6ff6ade2 | 500 | // uses data from graphbuffer |
7fe9b0b7 | 501 | int CmdDetectClockRate(const char *Cmd) |
502 | { | |
8d0a3e87 | 503 | GetClock("",1); |
7fe9b0b7 | 504 | return 0; |
505 | } | |
506 | ||
1b492a97 | 507 | //by marshmellow |
6ff6ade2 | 508 | //fsk raw demod and print binary |
509 | //takes 4 arguments - Clock, invert, rchigh, rclow | |
510 | //defaults: clock = 50, invert=0, rchigh=10, rclow=8 (RF/10 RF/8 (fsk2a)) | |
1b492a97 | 511 | int CmdFSKrawdemod(const char *Cmd) |
512 | { | |
8d0a3e87 | 513 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave |
514 | int rfLen = 50; | |
515 | int invert = 0; | |
516 | int fchigh = 10; | |
517 | int fclow = 8; | |
518 | ||
519 | //set options from parameters entered with the command | |
520 | sscanf(Cmd, "%i %i %i %i", &rfLen, &invert, &fchigh, &fclow); | |
6ff6ade2 | 521 | |
8d0a3e87 | 522 | // A lots of checks if chigh, clow is out-of bounds. |
1b492a97 | 523 | |
8d0a3e87 | 524 | if (strlen(Cmd)>0 && strlen(Cmd)<=2) { |
525 | ||
526 | rfLen = 50; | |
527 | ||
528 | //if invert option only is used | |
529 | if (rfLen == 1){ | |
530 | invert=1; | |
531 | } | |
532 | } | |
533 | ||
534 | PrintAndLog("Args invert: %d - Clock:%d - FC high:%d - FC low: %d",invert,rfLen,fchigh, fclow); | |
535 | ||
536 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
537 | uint32_t len = GetFromGraphBuf(bits); | |
538 | ||
539 | int size = fskdemod(bits, len,(uint8_t)rfLen, (uint8_t)invert, (uint8_t)fchigh, (uint8_t)fclow); | |
540 | ||
541 | if (size > 0) { | |
542 | PrintAndLog("FSK decoded bitstream:"); | |
543 | ||
544 | SetGraphBuf(bits, size); | |
545 | ||
546 | // Now output the bitstream to the scrollback by line of 16 bits | |
547 | // only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size | |
548 | if(size > (8*32)+2) | |
549 | size = (8*32)+2; | |
550 | printBitStream(bits,size); | |
551 | } else { | |
552 | PrintAndLog("no FSK data found"); | |
553 | } | |
554 | return 0; | |
1b492a97 | 555 | } |
556 | ||
6ff6ade2 | 557 | //by marshmellow (based on existing demod + holiman's refactor) |
558 | //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded) | |
559 | //print full HID Prox ID and some bit format details if found | |
1b492a97 | 560 | int CmdFSKdemodHID(const char *Cmd) |
561 | { | |
8d0a3e87 | 562 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave |
563 | uint32_t hi2=0, hi=0, lo=0; | |
564 | ||
565 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
566 | uint32_t BitLen = GetFromGraphBuf(BitStream); | |
567 | ||
568 | //get binary from fsk wave | |
569 | size_t size = HIDdemodFSK(BitStream,BitLen,&hi2,&hi,&lo); | |
570 | ||
571 | if (size < 0){ | |
572 | PrintAndLog("Error demoding fsk"); | |
573 | return 0; | |
574 | } | |
575 | ||
576 | if (hi2==0 && hi==0 && lo==0) return 0; | |
577 | ||
578 | //extra large HID tags | |
579 | if (hi2 != 0){ | |
580 | PrintAndLog("TAG ID: %x%08x%08x (%d)", | |
581 | (unsigned int) hi2, | |
582 | (unsigned int) hi, | |
583 | (unsigned int) lo, | |
584 | (unsigned int) (lo>>1) & 0xFFFF); | |
585 | SetGraphBuf(BitStream,BitLen); | |
586 | return 1; | |
587 | } else { | |
588 | //standard HID tags <38 bits | |
589 | uint8_t fmtLen = 0; | |
590 | uint32_t fc = 0; | |
591 | uint32_t cardnum = 0; | |
592 | ||
593 | //if bit 38 is set then < 37 bit format is used | |
594 | if (((hi>>5) & 1)==1){ | |
595 | uint32_t lo2 = 0; | |
596 | ||
597 | //get bits 21-37 to check for format len bit | |
598 | lo2 = (((hi & 15) << 12) | (lo>>20)); | |
599 | uint8_t idx3 = 1; | |
600 | ||
601 | //find last bit set to 1 (format len bit) | |
602 | while( lo2 > 1){ | |
603 | lo2=lo2>>1; | |
604 | idx3++; | |
605 | } | |
606 | fmtLen = idx3 + 19; | |
607 | fc = 0; | |
608 | cardnum = 0; | |
609 | ||
610 | if(fmtLen==26){ | |
611 | cardnum = (lo>>1)&0xFFFF; | |
612 | fc = (lo>>17)&0xFF; | |
613 | } | |
614 | if(fmtLen==37){ | |
1b492a97 | 615 | cardnum = (lo>>1)&0x7FFFF; |
616 | fc = ((hi&0xF)<<12)|(lo>>20); | |
617 | } | |
8d0a3e87 | 618 | if(fmtLen==34){ |
1b492a97 | 619 | cardnum = (lo>>1)&0xFFFF; |
620 | fc= ((hi&1)<<15)|(lo>>17); | |
8d0a3e87 | 621 | } |
622 | if(fmtLen==35){ | |
1b492a97 | 623 | cardnum = (lo>>1)&0xFFFFF; |
624 | fc = ((hi&1)<<11)|(lo>>21); | |
8d0a3e87 | 625 | } |
626 | } else { | |
627 | //if bit 38 is not set then 37 bit format is used | |
628 | fmtLen= 37; | |
629 | fc =0; | |
630 | cardnum=0; | |
631 | ||
632 | if (fmtLen==37){ | |
633 | cardnum = (lo>>1) & 0x7FFFF; | |
634 | fc = ((hi&0xF) << 12) | (lo >> 20); | |
1b492a97 | 635 | } |
8d0a3e87 | 636 | } |
637 | PrintAndLog("TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d", | |
638 | (unsigned int) hi, | |
639 | (unsigned int) lo, | |
640 | (unsigned int) (lo>>1) & 0xFFFF, | |
641 | (unsigned int) fmtLen, | |
642 | (unsigned int) fc, | |
643 | (unsigned int) cardnum); | |
644 | SetGraphBuf(BitStream,BitLen); | |
6ff6ade2 | 645 | return 1; |
1b492a97 | 646 | } |
1b492a97 | 647 | return 0; |
648 | } | |
649 | ||
650 | //by marshmellow | |
6ff6ade2 | 651 | //IO-Prox demod - FSK RF/64 with preamble of 000000001 |
652 | //print ioprox ID and some format details | |
1b492a97 | 653 | int CmdFSKdemodIO(const char *Cmd) |
654 | { | |
8d0a3e87 | 655 | if (GraphTraceLen < 65) { |
656 | PrintAndLog("data samples size is too small"); | |
657 | return 0; | |
658 | } | |
659 | ||
660 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
661 | //set defaults | |
662 | int idx = 0; | |
663 | uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00}; | |
664 | uint32_t bitlen = GetFromGraphBuf(bits); | |
665 | ||
666 | //get binary from fsk wave | |
667 | idx = IOdemodFSK(bits, bitlen); | |
668 | ||
669 | if (idx == 0) { | |
670 | return 0; | |
671 | } | |
672 | if (idx == -1) { | |
673 | PrintAndLog("data samples size is too small"); | |
674 | return 0; | |
675 | } | |
676 | if (idx == -2) { | |
677 | PrintAndLog("Data samples has too much noice"); | |
678 | return 0; | |
679 | } | |
680 | if (idx == -3){ | |
681 | PrintAndLog("No good demod"); | |
682 | return 0; | |
683 | } | |
684 | ||
685 | if (idx+64 > bitlen) return 0; | |
686 | ||
687 | //Index map | |
688 | //0 10 20 30 40 50 60 | |
689 | //| | | | | | | | |
690 | //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23 | |
691 | //----------------------------------------------------------------------------- | |
692 | //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 | |
693 | // | |
694 | //XSF(version)facility:codeone+codetwo (raw) | |
695 | //Handle the data | |
696 | ||
697 | PrintAndLog("%d%d%d%d%d%d%d%d %d", bits[idx] , bits[idx+1], bits[idx+2], bits[idx+3], bits[idx+4], bits[idx+5], bits[idx+6], bits[idx+7], bits[idx+8]); | |
698 | PrintAndLog("%d%d%d%d%d%d%d%d %d", bits[idx+9] , bits[idx+10], bits[idx+11], bits[idx+12], bits[idx+13], bits[idx+14], bits[idx+15], bits[idx+16], bits[idx+17]); | |
699 | PrintAndLog("%d%d%d%d%d%d%d%d %d facility", bits[idx+18], bits[idx+19], bits[idx+20], bits[idx+21], bits[idx+22], bits[idx+23], bits[idx+24], bits[idx+25], bits[idx+26]); | |
700 | PrintAndLog("%d%d%d%d%d%d%d%d %d version", bits[idx+27], bits[idx+28], bits[idx+29], bits[idx+30], bits[idx+31], bits[idx+32], bits[idx+33], bits[idx+34], bits[idx+35]); | |
701 | PrintAndLog("%d%d%d%d%d%d%d%d %d code1", bits[idx+36], bits[idx+37], bits[idx+38], bits[idx+39], bits[idx+40], bits[idx+41], bits[idx+42], bits[idx+43], bits[idx+44]); | |
702 | PrintAndLog("%d%d%d%d%d%d%d%d %d code2", bits[idx+45], bits[idx+46], bits[idx+47], bits[idx+48], bits[idx+49], bits[idx+50], bits[idx+51], bits[idx+52], bits[idx+53]); | |
703 | PrintAndLog("%d%d%d%d%d%d%d%d %d%d checksum", bits[idx+54], bits[idx+55], bits[idx+56], bits[idx+57], bits[idx+58], bits[idx+59], bits[idx+60], bits[idx+61], bits[idx+62], bits[idx+63]); | |
704 | ||
705 | uint32_t code = bytebits_to_byte(bits+idx,32); | |
706 | uint32_t code2 = bytebits_to_byte(bits+idx+32,32); | |
707 | uint8_t version = bytebits_to_byte(bits+idx+27,8); //14,4 | |
708 | uint8_t facilitycode = bytebits_to_byte(bits+idx+18,8) ; | |
709 | uint16_t number = (bytebits_to_byte(bits+idx+36,8)<<8)|(bytebits_to_byte(bits+idx+45,8)); //36,9 | |
710 | ||
711 | PrintAndLog("XSF(%02d)%02x:%05d (%08x%08x)", version, facilitycode, number, code, code2); | |
712 | SetGraphBuf(bits, bitlen); | |
713 | return 1; | |
1b492a97 | 714 | } |
6ff6ade2 | 715 | |
1b492a97 | 716 | int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating |
7fe9b0b7 | 717 | { |
718 | static const int LowTone[] = { | |
719 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
720 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
721 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
722 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
723 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 | |
724 | }; | |
725 | static const int HighTone[] = { | |
726 | 1, 1, 1, 1, 1, -1, -1, -1, -1, | |
727 | 1, 1, 1, 1, -1, -1, -1, -1, | |
728 | 1, 1, 1, 1, -1, -1, -1, -1, | |
729 | 1, 1, 1, 1, -1, -1, -1, -1, | |
730 | 1, 1, 1, 1, -1, -1, -1, -1, | |
731 | 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
732 | }; | |
733 | ||
734 | int lowLen = sizeof (LowTone) / sizeof (int); | |
735 | int highLen = sizeof (HighTone) / sizeof (int); | |
736 | int convLen = (highLen > lowLen) ? highLen : lowLen; | |
737 | uint32_t hi = 0, lo = 0; | |
738 | ||
739 | int i, j; | |
740 | int minMark = 0, maxMark = 0; | |
741 | ||
742 | for (i = 0; i < GraphTraceLen - convLen; ++i) { | |
743 | int lowSum = 0, highSum = 0; | |
744 | ||
745 | for (j = 0; j < lowLen; ++j) { | |
746 | lowSum += LowTone[j]*GraphBuffer[i+j]; | |
747 | } | |
748 | for (j = 0; j < highLen; ++j) { | |
749 | highSum += HighTone[j] * GraphBuffer[i + j]; | |
750 | } | |
751 | lowSum = abs(100 * lowSum / lowLen); | |
752 | highSum = abs(100 * highSum / highLen); | |
753 | GraphBuffer[i] = (highSum << 16) | lowSum; | |
754 | } | |
755 | ||
756 | for(i = 0; i < GraphTraceLen - convLen - 16; ++i) { | |
757 | int lowTot = 0, highTot = 0; | |
758 | // 10 and 8 are f_s divided by f_l and f_h, rounded | |
759 | for (j = 0; j < 10; ++j) { | |
760 | lowTot += (GraphBuffer[i+j] & 0xffff); | |
761 | } | |
762 | for (j = 0; j < 8; j++) { | |
763 | highTot += (GraphBuffer[i + j] >> 16); | |
764 | } | |
765 | GraphBuffer[i] = lowTot - highTot; | |
766 | if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i]; | |
767 | if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i]; | |
768 | } | |
769 | ||
770 | GraphTraceLen -= (convLen + 16); | |
771 | RepaintGraphWindow(); | |
772 | ||
1b492a97 | 773 | // Find bit-sync (3 lo followed by 3 high) (HID ONLY) |
7fe9b0b7 | 774 | int max = 0, maxPos = 0; |
775 | for (i = 0; i < 6000; ++i) { | |
776 | int dec = 0; | |
777 | for (j = 0; j < 3 * lowLen; ++j) { | |
778 | dec -= GraphBuffer[i + j]; | |
779 | } | |
780 | for (; j < 3 * (lowLen + highLen ); ++j) { | |
781 | dec += GraphBuffer[i + j]; | |
782 | } | |
783 | if (dec > max) { | |
784 | max = dec; | |
785 | maxPos = i; | |
786 | } | |
787 | } | |
788 | ||
789 | // place start of bit sync marker in graph | |
790 | GraphBuffer[maxPos] = maxMark; | |
791 | GraphBuffer[maxPos + 1] = minMark; | |
792 | ||
793 | maxPos += j; | |
794 | ||
795 | // place end of bit sync marker in graph | |
796 | GraphBuffer[maxPos] = maxMark; | |
797 | GraphBuffer[maxPos+1] = minMark; | |
798 | ||
799 | PrintAndLog("actual data bits start at sample %d", maxPos); | |
800 | PrintAndLog("length %d/%d", highLen, lowLen); | |
801 | ||
8d0a3e87 | 802 | uint8_t bits[46] = {0x00}; |
7fe9b0b7 | 803 | |
804 | // find bit pairs and manchester decode them | |
805 | for (i = 0; i < arraylen(bits) - 1; ++i) { | |
806 | int dec = 0; | |
807 | for (j = 0; j < lowLen; ++j) { | |
808 | dec -= GraphBuffer[maxPos + j]; | |
809 | } | |
810 | for (; j < lowLen + highLen; ++j) { | |
811 | dec += GraphBuffer[maxPos + j]; | |
812 | } | |
813 | maxPos += j; | |
814 | // place inter bit marker in graph | |
815 | GraphBuffer[maxPos] = maxMark; | |
816 | GraphBuffer[maxPos + 1] = minMark; | |
817 | ||
818 | // hi and lo form a 64 bit pair | |
819 | hi = (hi << 1) | (lo >> 31); | |
820 | lo = (lo << 1); | |
821 | // store decoded bit as binary (in hi/lo) and text (in bits[]) | |
822 | if(dec < 0) { | |
823 | bits[i] = '1'; | |
824 | lo |= 1; | |
825 | } else { | |
826 | bits[i] = '0'; | |
827 | } | |
828 | } | |
829 | PrintAndLog("bits: '%s'", bits); | |
830 | PrintAndLog("hex: %08x %08x", hi, lo); | |
831 | return 0; | |
832 | } | |
833 | ||
834 | int CmdGrid(const char *Cmd) | |
835 | { | |
836 | sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); | |
7ddb9900 | 837 | PlotGridXdefault= PlotGridX; |
838 | PlotGridYdefault= PlotGridY; | |
7fe9b0b7 | 839 | RepaintGraphWindow(); |
840 | return 0; | |
841 | } | |
842 | ||
843 | int CmdHexsamples(const char *Cmd) | |
844 | { | |
4961e292 | 845 | int i, j; |
7fe9b0b7 | 846 | int requested = 0; |
847 | int offset = 0; | |
4961e292 | 848 | char string_buf[25]; |
849 | char* string_ptr = string_buf; | |
90d74dc2 | 850 | uint8_t got[40000]; |
4961e292 | 851 | |
852 | sscanf(Cmd, "%i %i", &requested, &offset); | |
90d74dc2 | 853 | |
4961e292 | 854 | /* if no args send something */ |
855 | if (requested == 0) { | |
90d74dc2 | 856 | requested = 8; |
857 | } | |
90d74dc2 | 858 | if (offset + requested > sizeof(got)) { |
859 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000"); | |
4961e292 | 860 | return 0; |
861 | } | |
90d74dc2 | 862 | |
4961e292 | 863 | GetFromBigBuf(got,requested,offset); |
90d74dc2 | 864 | WaitForResponse(CMD_ACK,NULL); |
865 | ||
4961e292 | 866 | i = 0; |
867 | for (j = 0; j < requested; j++) { | |
868 | i++; | |
869 | string_ptr += sprintf(string_ptr, "%02x ", got[j]); | |
870 | if (i == 8) { | |
871 | *(string_ptr - 1) = '\0'; // remove the trailing space | |
872 | PrintAndLog("%s", string_buf); | |
873 | string_buf[0] = '\0'; | |
874 | string_ptr = string_buf; | |
875 | i = 0; | |
876 | } | |
877 | if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes | |
878 | *(string_ptr - 1) = '\0'; | |
879 | PrintAndLog("%s", string_buf); | |
880 | string_buf[0] = '\0'; | |
881 | } | |
7fe9b0b7 | 882 | } |
883 | return 0; | |
884 | } | |
885 | ||
7fe9b0b7 | 886 | int CmdHide(const char *Cmd) |
887 | { | |
888 | HideGraphWindow(); | |
889 | return 0; | |
890 | } | |
891 | ||
892 | int CmdHpf(const char *Cmd) | |
893 | { | |
894 | int i; | |
895 | int accum = 0; | |
896 | ||
897 | for (i = 10; i < GraphTraceLen; ++i) | |
898 | accum += GraphBuffer[i]; | |
899 | accum /= (GraphTraceLen - 10); | |
900 | for (i = 0; i < GraphTraceLen; ++i) | |
901 | GraphBuffer[i] -= accum; | |
902 | ||
903 | RepaintGraphWindow(); | |
904 | return 0; | |
905 | } | |
906 | ||
8d183c53 | 907 | int CmdSamples(const char *Cmd) |
7fe9b0b7 | 908 | { |
8d0a3e87 | 909 | uint8_t got[40000] = {0x00}; |
910 | ||
081151ea | 911 | int n = strtol(Cmd, NULL, 0); |
912 | if (n == 0) | |
6ff6ade2 | 913 | n = 20000; |
8d0a3e87 | 914 | |
081151ea | 915 | if (n > sizeof(got)) |
916 | n = sizeof(got); | |
a2847518 | 917 | |
081151ea | 918 | PrintAndLog("Reading %d samples from device memory\n", n); |
8d0a3e87 | 919 | GetFromBigBuf(got,n,0); |
081151ea | 920 | WaitForResponse(CMD_ACK,NULL); |
921 | for (int j = 0; j < n; ++j) { | |
922 | GraphBuffer[j] = ((int)got[j]) - 128; | |
923 | } | |
924 | GraphTraceLen = n; | |
925 | RepaintGraphWindow(); | |
926 | return 0; | |
7fe9b0b7 | 927 | } |
6ff6ade2 | 928 | |
02306bac | 929 | int CmdTuneSamples(const char *Cmd) |
930 | { | |
d3499d36 | 931 | int timeout = 0; |
932 | printf("\nMeasuring antenna characteristics, please wait..."); | |
933 | ||
934 | UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING}; | |
935 | SendCommand(&c); | |
936 | ||
937 | UsbCommand resp; | |
938 | while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) { | |
939 | timeout++; | |
940 | printf("."); | |
941 | if (timeout > 7) { | |
942 | PrintAndLog("\nNo response from Proxmark. Aborting..."); | |
943 | return 1; | |
944 | } | |
945 | } | |
02306bac | 946 | |
d3499d36 | 947 | int peakv, peakf; |
948 | int vLf125, vLf134, vHf; | |
949 | vLf125 = resp.arg[0] & 0xffff; | |
950 | vLf134 = resp.arg[0] >> 16; | |
951 | vHf = resp.arg[1] & 0xffff;; | |
952 | peakf = resp.arg[2] & 0xffff; | |
953 | peakv = resp.arg[2] >> 16; | |
954 | PrintAndLog(""); | |
955 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); | |
956 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
957 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
958 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
959 | if (peakv<2000) | |
960 | PrintAndLog("# Your LF antenna is unusable."); | |
961 | else if (peakv<10000) | |
962 | PrintAndLog("# Your LF antenna is marginal."); | |
963 | if (vHf<2000) | |
964 | PrintAndLog("# Your HF antenna is unusable."); | |
965 | else if (vHf<5000) | |
966 | PrintAndLog("# Your HF antenna is marginal."); | |
967 | ||
968 | for (int i = 0; i < 256; i++) { | |
969 | GraphBuffer[i] = resp.d.asBytes[i] - 128; | |
970 | } | |
02306bac | 971 | |
d3499d36 | 972 | PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n"); |
973 | PrintAndLog("\n"); | |
974 | GraphTraceLen = 256; | |
975 | ShowGraphWindow(); | |
976 | ||
02306bac | 977 | return 0; |
978 | } | |
979 | ||
7fe9b0b7 | 980 | int CmdLoad(const char *Cmd) |
981 | { | |
463ca973 | 982 | char filename[FILE_PATH_SIZE] = {0x00}; |
983 | int len = 0; | |
984 | ||
985 | len = strlen(Cmd); | |
986 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
987 | memcpy(filename, Cmd, len); | |
988 | ||
989 | FILE *f = fopen(filename, "r"); | |
990 | if (!f) { | |
991 | PrintAndLog("couldn't open '%s'", filename); | |
992 | return 0; | |
993 | } | |
7fe9b0b7 | 994 | |
995 | GraphTraceLen = 0; | |
996 | char line[80]; | |
997 | while (fgets(line, sizeof (line), f)) { | |
998 | GraphBuffer[GraphTraceLen] = atoi(line); | |
999 | GraphTraceLen++; | |
1000 | } | |
1001 | fclose(f); | |
1002 | PrintAndLog("loaded %d samples", GraphTraceLen); | |
1003 | RepaintGraphWindow(); | |
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | int CmdLtrim(const char *Cmd) | |
1008 | { | |
1009 | int ds = atoi(Cmd); | |
1010 | ||
1011 | for (int i = ds; i < GraphTraceLen; ++i) | |
1012 | GraphBuffer[i-ds] = GraphBuffer[i]; | |
1013 | GraphTraceLen -= ds; | |
1014 | ||
1015 | RepaintGraphWindow(); | |
1016 | return 0; | |
1017 | } | |
0a966150 | 1018 | int CmdRtrim(const char *Cmd) |
1019 | { | |
1020 | int ds = atoi(Cmd); | |
1021 | ||
1022 | GraphTraceLen = ds; | |
1023 | ||
1024 | RepaintGraphWindow(); | |
1025 | return 0; | |
1026 | } | |
7fe9b0b7 | 1027 | |
1028 | /* | |
1029 | * Manchester demodulate a bitstream. The bitstream needs to be already in | |
1030 | * the GraphBuffer as 0 and 1 values | |
1031 | * | |
1032 | * Give the clock rate as argument in order to help the sync - the algorithm | |
1033 | * resyncs at each pulse anyway. | |
1034 | * | |
1035 | * Not optimized by any means, this is the 1st time I'm writing this type of | |
1036 | * routine, feel free to improve... | |
1037 | * | |
1038 | * 1st argument: clock rate (as number of samples per clock rate) | |
1039 | * Typical values can be 64, 32, 128... | |
1040 | */ | |
1041 | int CmdManchesterDemod(const char *Cmd) | |
1042 | { | |
1043 | int i, j, invert= 0; | |
1044 | int bit; | |
1045 | int clock; | |
fddf220a | 1046 | int lastval = 0; |
7fe9b0b7 | 1047 | int low = 0; |
1048 | int high = 0; | |
1049 | int hithigh, hitlow, first; | |
1050 | int lc = 0; | |
1051 | int bitidx = 0; | |
1052 | int bit2idx = 0; | |
1053 | int warnings = 0; | |
1054 | ||
1055 | /* check if we're inverting output */ | |
c6f1fb9d | 1056 | if (*Cmd == 'i') |
7fe9b0b7 | 1057 | { |
1058 | PrintAndLog("Inverting output"); | |
1059 | invert = 1; | |
fffad860 | 1060 | ++Cmd; |
7fe9b0b7 | 1061 | do |
1062 | ++Cmd; | |
1063 | while(*Cmd == ' '); // in case a 2nd argument was given | |
1064 | } | |
1065 | ||
1066 | /* Holds the decoded bitstream: each clock period contains 2 bits */ | |
1067 | /* later simplified to 1 bit after manchester decoding. */ | |
1068 | /* Add 10 bits to allow for noisy / uncertain traces without aborting */ | |
1069 | /* int BitStream[GraphTraceLen*2/clock+10]; */ | |
1070 | ||
1071 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
1072 | /* large array */ | |
a61b4976 | 1073 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0x00}; |
7fe9b0b7 | 1074 | |
1075 | /* Detect high and lows */ | |
8d0a3e87 | 1076 | DetectHighLowInGraph( &high, &low, TRUE); |
1077 | ||
7fe9b0b7 | 1078 | /* Get our clock */ |
8d0a3e87 | 1079 | clock = GetClock(Cmd, 0); |
7fe9b0b7 | 1080 | int tolerance = clock/4; |
1081 | ||
1082 | /* Detect first transition */ | |
1083 | /* Lo-Hi (arbitrary) */ | |
1084 | /* skip to the first high */ | |
1085 | for (i= 0; i < GraphTraceLen; i++) | |
1086 | if (GraphBuffer[i] == high) | |
1087 | break; | |
1088 | /* now look for the first low */ | |
1089 | for (; i < GraphTraceLen; i++) | |
1090 | { | |
1091 | if (GraphBuffer[i] == low) | |
1092 | { | |
1093 | lastval = i; | |
1094 | break; | |
1095 | } | |
1096 | } | |
b44e5233 | 1097 | |
7fe9b0b7 | 1098 | /* If we're not working with 1/0s, demod based off clock */ |
1099 | if (high != 1) | |
1100 | { | |
1101 | bit = 0; /* We assume the 1st bit is zero, it may not be | |
1102 | * the case: this routine (I think) has an init problem. | |
1103 | * Ed. | |
b44e5233 | 1104 | */ |
7fe9b0b7 | 1105 | for (; i < (int)(GraphTraceLen / clock); i++) |
1106 | { | |
1107 | hithigh = 0; | |
1108 | hitlow = 0; | |
1109 | first = 1; | |
1110 | ||
1111 | /* Find out if we hit both high and low peaks */ | |
1112 | for (j = 0; j < clock; j++) | |
1113 | { | |
1114 | if (GraphBuffer[(i * clock) + j] == high) | |
1115 | hithigh = 1; | |
1116 | else if (GraphBuffer[(i * clock) + j] == low) | |
1117 | hitlow = 1; | |
1118 | ||
1119 | /* it doesn't count if it's the first part of our read | |
1120 | because it's really just trailing from the last sequence */ | |
1121 | if (first && (hithigh || hitlow)) | |
1122 | hithigh = hitlow = 0; | |
1123 | else | |
1124 | first = 0; | |
1125 | ||
1126 | if (hithigh && hitlow) | |
1127 | break; | |
1128 | } | |
1129 | ||
1130 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
1131 | if (!hithigh || !hitlow) | |
1132 | bit ^= 1; | |
1133 | ||
1134 | BitStream[bit2idx++] = bit ^ invert; | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | /* standard 1/0 bitstream */ | |
1139 | else | |
1140 | { | |
1141 | ||
1142 | /* Then detect duration between 2 successive transitions */ | |
1143 | for (bitidx = 1; i < GraphTraceLen; i++) | |
1144 | { | |
1145 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
1146 | { | |
1147 | lc = i-lastval; | |
1148 | lastval = i; | |
1149 | ||
1150 | // Error check: if bitidx becomes too large, we do not | |
1151 | // have a Manchester encoded bitstream or the clock is really | |
1152 | // wrong! | |
1153 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
1154 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
1155 | return 0; | |
1156 | } | |
1157 | // Then switch depending on lc length: | |
1158 | // Tolerance is 1/4 of clock rate (arbitrary) | |
1159 | if (abs(lc-clock/2) < tolerance) { | |
1160 | // Short pulse : either "1" or "0" | |
1161 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1162 | } else if (abs(lc-clock) < tolerance) { | |
1163 | // Long pulse: either "11" or "00" | |
1164 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1165 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1166 | } else { | |
1167 | // Error | |
1168 | warnings++; | |
1169 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); | |
1170 | PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)"); | |
1171 | ||
1172 | if (warnings > 10) | |
1173 | { | |
1174 | PrintAndLog("Error: too many detection errors, aborting."); | |
1175 | return 0; | |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream | |
1182 | // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful | |
1183 | // to stop output at the final bitidx2 value, not bitidx | |
c15d2bdc | 1184 | |
1185 | //http://www.proxmark.org/forum/viewtopic.php?id=403 | |
1186 | for (i = 1; i < bitidx; i += 2) { | |
7fe9b0b7 | 1187 | if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) { |
1188 | BitStream[bit2idx++] = 1 ^ invert; | |
1189 | } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) { | |
1190 | BitStream[bit2idx++] = 0 ^ invert; | |
1191 | } else { | |
1192 | // We cannot end up in this state, this means we are unsynchronized, | |
1193 | // move up 1 bit: | |
1194 | i++; | |
081151ea | 1195 | warnings++; |
7fe9b0b7 | 1196 | PrintAndLog("Unsynchronized, resync..."); |
1197 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
1198 | ||
1199 | if (warnings > 10) | |
1200 | { | |
1201 | PrintAndLog("Error: too many decode errors, aborting."); | |
1202 | return 0; | |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | PrintAndLog("Manchester decoded bitstream"); | |
1209 | // Now output the bitstream to the scrollback by line of 16 bits | |
1210 | for (i = 0; i < (bit2idx-16); i+=16) { | |
1211 | PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i", | |
1212 | BitStream[i], | |
1213 | BitStream[i+1], | |
1214 | BitStream[i+2], | |
1215 | BitStream[i+3], | |
1216 | BitStream[i+4], | |
1217 | BitStream[i+5], | |
1218 | BitStream[i+6], | |
1219 | BitStream[i+7], | |
1220 | BitStream[i+8], | |
1221 | BitStream[i+9], | |
1222 | BitStream[i+10], | |
1223 | BitStream[i+11], | |
1224 | BitStream[i+12], | |
1225 | BitStream[i+13], | |
1226 | BitStream[i+14], | |
1227 | BitStream[i+15]); | |
1228 | } | |
c15d2bdc | 1229 | return bit2idx; |
7fe9b0b7 | 1230 | } |
1231 | ||
1232 | /* Modulate our data into manchester */ | |
1233 | int CmdManchesterMod(const char *Cmd) | |
1234 | { | |
1235 | int i, j; | |
7fe9b0b7 | 1236 | int bit, lastbit, wave; |
8d0a3e87 | 1237 | int clock = GetClock(Cmd, 0); |
1238 | ||
a61b4976 | 1239 | int half = (int)(clock/2); |
1240 | ||
7fe9b0b7 | 1241 | wave = 0; |
1242 | lastbit = 1; | |
1243 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
1244 | { | |
1245 | bit = GraphBuffer[i * clock] ^ 1; | |
1246 | ||
a61b4976 | 1247 | for (j = 0; j < half; ++j) |
7fe9b0b7 | 1248 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave; |
a61b4976 | 1249 | for (j = half; j < clock; ++j) |
7fe9b0b7 | 1250 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1; |
1251 | ||
1252 | /* Keep track of how we start our wave and if we changed or not this time */ | |
1253 | wave ^= bit ^ lastbit; | |
1254 | lastbit = bit; | |
1255 | } | |
1256 | ||
1257 | RepaintGraphWindow(); | |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | int CmdNorm(const char *Cmd) | |
1262 | { | |
1263 | int i; | |
1264 | int max = INT_MIN, min = INT_MAX; | |
1265 | ||
1266 | for (i = 10; i < GraphTraceLen; ++i) { | |
1267 | if (GraphBuffer[i] > max) | |
1268 | max = GraphBuffer[i]; | |
1269 | if (GraphBuffer[i] < min) | |
1270 | min = GraphBuffer[i]; | |
1271 | } | |
1272 | ||
1273 | if (max != min) { | |
1274 | for (i = 0; i < GraphTraceLen; ++i) { | |
1275 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 1000 / | |
1276 | (max - min); | |
1277 | } | |
1278 | } | |
1279 | RepaintGraphWindow(); | |
1280 | return 0; | |
1281 | } | |
1282 | ||
1283 | int CmdPlot(const char *Cmd) | |
1284 | { | |
1285 | ShowGraphWindow(); | |
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | int CmdSave(const char *Cmd) | |
1290 | { | |
463ca973 | 1291 | char filename[FILE_PATH_SIZE] = {0x00}; |
1292 | int len = 0; | |
1293 | ||
1294 | len = strlen(Cmd); | |
1295 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
1296 | memcpy(filename, Cmd, len); | |
1297 | ||
1298 | ||
1299 | FILE *f = fopen(filename, "w"); | |
7fe9b0b7 | 1300 | if(!f) { |
463ca973 | 1301 | PrintAndLog("couldn't open '%s'", filename); |
7fe9b0b7 | 1302 | return 0; |
1303 | } | |
1304 | int i; | |
1305 | for (i = 0; i < GraphTraceLen; i++) { | |
1306 | fprintf(f, "%d\n", GraphBuffer[i]); | |
1307 | } | |
1308 | fclose(f); | |
1309 | PrintAndLog("saved to '%s'", Cmd); | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | int CmdScale(const char *Cmd) | |
1314 | { | |
1315 | CursorScaleFactor = atoi(Cmd); | |
1316 | if (CursorScaleFactor == 0) { | |
1317 | PrintAndLog("bad, can't have zero scale"); | |
1318 | CursorScaleFactor = 1; | |
1319 | } | |
1320 | RepaintGraphWindow(); | |
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | int CmdThreshold(const char *Cmd) | |
1325 | { | |
1326 | int threshold = atoi(Cmd); | |
1327 | ||
1328 | for (int i = 0; i < GraphTraceLen; ++i) { | |
1329 | if (GraphBuffer[i] >= threshold) | |
1330 | GraphBuffer[i] = 1; | |
1331 | else | |
7bb9d33e | 1332 | GraphBuffer[i] = -1; |
7fe9b0b7 | 1333 | } |
1334 | RepaintGraphWindow(); | |
1335 | return 0; | |
1336 | } | |
1337 | ||
d51b2eda MHS |
1338 | int CmdDirectionalThreshold(const char *Cmd) |
1339 | { | |
1340 | int8_t upThres = param_get8(Cmd, 0); | |
1341 | int8_t downThres = param_get8(Cmd, 1); | |
1342 | ||
1343 | printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); | |
1344 | ||
1345 | int lastValue = GraphBuffer[0]; | |
1346 | GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. | |
1347 | ||
1348 | for (int i = 1; i < GraphTraceLen; ++i) { | |
1349 | // Apply first threshold to samples heading up | |
1350 | if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue) | |
1351 | { | |
1352 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
1353 | GraphBuffer[i] = 1; | |
1354 | } | |
1355 | // Apply second threshold to samples heading down | |
1356 | else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue) | |
1357 | { | |
1358 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
1359 | GraphBuffer[i] = -1; | |
1360 | } | |
1361 | else | |
1362 | { | |
1363 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
1364 | GraphBuffer[i] = GraphBuffer[i-1]; | |
1365 | ||
1366 | } | |
1367 | } | |
1368 | GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample. | |
1369 | RepaintGraphWindow(); | |
1370 | return 0; | |
1371 | } | |
1372 | ||
7fe9b0b7 | 1373 | int CmdZerocrossings(const char *Cmd) |
1374 | { | |
1375 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
1376 | CmdHpf(""); | |
1377 | ||
1378 | int sign = 1; | |
1379 | int zc = 0; | |
1380 | int lastZc = 0; | |
1381 | ||
1382 | for (int i = 0; i < GraphTraceLen; ++i) { | |
1383 | if (GraphBuffer[i] * sign >= 0) { | |
1384 | // No change in sign, reproduce the previous sample count. | |
1385 | zc++; | |
1386 | GraphBuffer[i] = lastZc; | |
1387 | } else { | |
1388 | // Change in sign, reset the sample count. | |
1389 | sign = -sign; | |
1390 | GraphBuffer[i] = lastZc; | |
1391 | if (sign > 0) { | |
1392 | lastZc = zc; | |
1393 | zc = 0; | |
1394 | } | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | RepaintGraphWindow(); | |
1399 | return 0; | |
1400 | } | |
1401 | ||
1402 | static command_t CommandTable[] = | |
1403 | { | |
1404 | {"help", CmdHelp, 1, "This help"}, | |
1405 | {"amp", CmdAmp, 1, "Amplify peaks"}, | |
6ff6ade2 | 1406 | {"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"}, |
8d0a3e87 | 1407 | {"askmandemod", Cmdaskmandemod, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK/Manchester tags and output binary"}, |
1408 | {"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK tags and output binary"}, | |
7fe9b0b7 | 1409 | {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"}, |
6ff6ade2 | 1410 | {"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] Biphase decode binary stream already in graph buffer (offset = bit to start decode from)"}, |
7fe9b0b7 | 1411 | {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"}, |
1412 | {"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"}, | |
1413 | {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"}, | |
1414 | {"dec", CmdDec, 1, "Decimate samples"}, | |
6ff6ade2 | 1415 | {"detectaskclock",CmdDetectClockRate, 1, "Detect ASK clock rate"}, |
02306bac | 1416 | {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."}, |
8d0a3e87 | 1417 | {"em4xdecode", CmdEm410xDecode, 1, "decode em4x from graph buffer"}, |
7fe9b0b7 | 1418 | {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"}, |
1b492a97 | 1419 | {"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate graph window as a HID FSK using raw"}, |
1420 | {"fskiodemod", CmdFSKdemodIO, 1, "Demodulate graph window as an IO Prox FSK using raw"}, | |
0a966150 | 1421 | {"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to binary (clock = 50)(invert = 1 or 0)(rchigh = 10)(rclow=8)"}, |
7fe9b0b7 | 1422 | {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"}, |
90d74dc2 | 1423 | {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"}, |
7fe9b0b7 | 1424 | {"hide", CmdHide, 1, "Hide graph window"}, |
1425 | {"hpf", CmdHpf, 1, "Remove DC offset from trace"}, | |
7fe9b0b7 | 1426 | {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"}, |
1427 | {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"}, | |
0a966150 | 1428 | {"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"}, |
7fe9b0b7 | 1429 | {"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"}, |
6ff6ade2 | 1430 | {"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream already in graph buffer"}, |
7fe9b0b7 | 1431 | {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"}, |
1432 | {"norm", CmdNorm, 1, "Normalize max/min to +/-500"}, | |
7ddb9900 | 1433 | {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"}, |
90d74dc2 | 1434 | {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window"}, |
7fe9b0b7 | 1435 | {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"}, |
1436 | {"scale", CmdScale, 1, "<int> -- Set cursor display scale"}, | |
dbf444a1 | 1437 | {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"}, |
02306bac | 1438 | {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"}, |
7fe9b0b7 | 1439 | {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"}, |
1440 | {NULL, NULL, 0, NULL} | |
1441 | }; | |
1442 | ||
1443 | int CmdData(const char *Cmd) | |
1444 | { | |
1445 | CmdsParse(CommandTable, Cmd); | |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | int CmdHelp(const char *Cmd) | |
1450 | { | |
1451 | CmdsHelp(CommandTable); | |
1452 | return 0; | |
1453 | } |