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