]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
3 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // UI utilities | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include <stdarg.h> | |
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <stdbool.h> | |
16 | #include <time.h> | |
17 | #include <readline/readline.h> | |
18 | #include <pthread.h> | |
19 | #include "loclass/cipherutils.h" | |
20 | #include "ui.h" | |
21 | ||
22 | //#include <liquid/liquid.h> | |
23 | #define M_PI 3.14159265358979323846264338327 | |
24 | ||
25 | double CursorScaleFactor; | |
26 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; | |
27 | int offline; | |
28 | int flushAfterWrite = 0; //buzzy | |
29 | extern pthread_mutex_t print_lock; | |
30 | ||
31 | static char *logfilename = "proxmark3.log"; | |
32 | ||
33 | void PrintAndLog(char *fmt, ...) | |
34 | { | |
35 | char *saved_line; | |
36 | int saved_point; | |
37 | va_list argptr, argptr2; | |
38 | static FILE *logfile = NULL; | |
39 | static int logging=1; | |
40 | ||
41 | // lock this section to avoid interlacing prints from different threats | |
42 | pthread_mutex_lock(&print_lock); | |
43 | ||
44 | if (logging && !logfile) { | |
45 | logfile=fopen(logfilename, "a"); | |
46 | if (!logfile) { | |
47 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
48 | logging=0; | |
49 | } | |
50 | } | |
51 | ||
52 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
53 | ||
54 | if (need_hack) { | |
55 | saved_point = rl_point; | |
56 | saved_line = rl_copy_text(0, rl_end); | |
57 | rl_save_prompt(); | |
58 | rl_replace_line("", 0); | |
59 | rl_redisplay(); | |
60 | } | |
61 | ||
62 | va_start(argptr, fmt); | |
63 | va_copy(argptr2, argptr); | |
64 | vprintf(fmt, argptr); | |
65 | printf(" "); // cleaning prompt | |
66 | va_end(argptr); | |
67 | printf("\n"); | |
68 | ||
69 | if (need_hack) { | |
70 | rl_restore_prompt(); | |
71 | rl_replace_line(saved_line, 0); | |
72 | rl_point = saved_point; | |
73 | rl_redisplay(); | |
74 | free(saved_line); | |
75 | } | |
76 | ||
77 | if (logging && logfile) { | |
78 | vfprintf(logfile, fmt, argptr2); | |
79 | fprintf(logfile,"\n"); | |
80 | fflush(logfile); | |
81 | } | |
82 | va_end(argptr2); | |
83 | ||
84 | if (flushAfterWrite == 1) //buzzy | |
85 | { | |
86 | fflush(NULL); | |
87 | } | |
88 | //release lock | |
89 | pthread_mutex_unlock(&print_lock); | |
90 | } | |
91 | ||
92 | void SetLogFilename(char *fn) | |
93 | { | |
94 | logfilename = fn; | |
95 | } | |
96 | ||
97 | int manchester_decode( int * data, const size_t len, uint8_t * dataout){ | |
98 | ||
99 | int bitlength = 0; | |
100 | int i, clock, high, low, startindex; | |
101 | low = startindex = 0; | |
102 | high = 1; | |
103 | uint8_t bitStream[len]; | |
104 | ||
105 | memset(bitStream, 0x00, len); | |
106 | ||
107 | /* Detect high and lows */ | |
108 | for (i = 0; i < len; i++) { | |
109 | if (data[i] > high) | |
110 | high = data[i]; | |
111 | else if (data[i] < low) | |
112 | low = data[i]; | |
113 | } | |
114 | ||
115 | /* get clock */ | |
116 | clock = GetT55x7Clock( data, len, high ); | |
117 | startindex = DetectFirstTransition(data, len, high); | |
118 | ||
119 | //PrintAndLog(" Clock : %d", clock); | |
120 | //PrintAndLog(" startindex : %d", startindex); | |
121 | ||
122 | if (high != 1) | |
123 | bitlength = ManchesterConvertFrom255(data, len, bitStream, high, low, clock, startindex); | |
124 | else | |
125 | bitlength= ManchesterConvertFrom1(data, len, bitStream, clock, startindex); | |
126 | ||
127 | //if ( bitlength > 0 ) | |
128 | // PrintPaddedManchester(bitStream, bitlength, clock); | |
129 | ||
130 | memcpy(dataout, bitStream, bitlength); | |
131 | return bitlength; | |
132 | } | |
133 | ||
134 | int GetT55x7Clock( const int * data, const size_t len, int peak ){ | |
135 | ||
136 | int i,lastpeak,clock; | |
137 | clock = 0xFFFF; | |
138 | lastpeak = 0; | |
139 | ||
140 | /* Detect peak if we don't have one */ | |
141 | if (!peak) { | |
142 | for (i = 0; i < len; ++i) { | |
143 | if (data[i] > peak) { | |
144 | peak = data[i]; | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | for (i = 1; i < len; ++i) { | |
150 | /* if this is the beginning of a peak */ | |
151 | if ( data[i-1] != data[i] && data[i] == peak) { | |
152 | /* find lowest difference between peaks */ | |
153 | if (lastpeak && i - lastpeak < clock) | |
154 | clock = i - lastpeak; | |
155 | lastpeak = i; | |
156 | } | |
157 | } | |
158 | //return clock; | |
159 | //defaults clock to precise values. | |
160 | switch(clock){ | |
161 | case 8: | |
162 | case 16: | |
163 | case 32: | |
164 | case 40: | |
165 | case 50: | |
166 | case 64: | |
167 | case 100: | |
168 | case 128: | |
169 | return clock; | |
170 | break; | |
171 | default: break; | |
172 | } | |
173 | ||
174 | //PrintAndLog(" Found Clock : %d - trying to adjust", clock); | |
175 | ||
176 | // When detected clock is 31 or 33 then then return | |
177 | int clockmod = clock%8; | |
178 | if ( clockmod == 7 ) | |
179 | clock += 1; | |
180 | else if ( clockmod == 1 ) | |
181 | clock -= 1; | |
182 | ||
183 | return clock; | |
184 | } | |
185 | ||
186 | int DetectFirstTransition(const int * data, const size_t len, int threshold){ | |
187 | ||
188 | int i =0; | |
189 | /* now look for the first threshold */ | |
190 | for (; i < len; ++i) { | |
191 | if (data[i] == threshold) { | |
192 | break; | |
193 | } | |
194 | } | |
195 | return i; | |
196 | } | |
197 | ||
198 | int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){ | |
199 | ||
200 | int i, j, z, hithigh, hitlow, bitIndex, startType; | |
201 | i = 0; | |
202 | bitIndex = 0; | |
203 | ||
204 | int isDamp = 0; | |
205 | int damplimit = (int)((high / 2) * 0.3); | |
206 | int dampHi = (high/2)+damplimit; | |
207 | int dampLow = (high/2)-damplimit; | |
208 | int firstST = 0; | |
209 | ||
210 | // i = clock frame of data | |
211 | for (; i < (int)(len / clock); i++) | |
212 | { | |
213 | hithigh = 0; | |
214 | hitlow = 0; | |
215 | startType = -1; | |
216 | z = startIndex + (i*clock); | |
217 | isDamp = 0; | |
218 | ||
219 | /* Find out if we hit both high and low peaks */ | |
220 | for (j = 0; j < clock; j++) | |
221 | { | |
222 | if (data[z+j] == high){ | |
223 | hithigh = 1; | |
224 | if ( startType == -1) | |
225 | startType = 1; | |
226 | } | |
227 | ||
228 | if (data[z+j] == low ){ | |
229 | hitlow = 1; | |
230 | if ( startType == -1) | |
231 | startType = 0; | |
232 | } | |
233 | ||
234 | if (hithigh && hitlow) | |
235 | break; | |
236 | } | |
237 | ||
238 | // No high value found, are we in a dampening field? | |
239 | if ( !hithigh ) { | |
240 | //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j); | |
241 | for (j = 0; j < clock; j++) | |
242 | { | |
243 | if ( | |
244 | (data[z+j] <= dampHi && data[z+j] >= dampLow) | |
245 | ){ | |
246 | isDamp++; | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | /* Manchester Switching.. | |
252 | 0: High -> Low | |
253 | 1: Low -> High | |
254 | */ | |
255 | if (startType == 0) | |
256 | dataout[bitIndex++] = 1; | |
257 | else if (startType == 1) | |
258 | dataout[bitIndex++] = 0; | |
259 | else | |
260 | dataout[bitIndex++] = 2; | |
261 | ||
262 | if ( isDamp > clock/2 ) { | |
263 | firstST++; | |
264 | } | |
265 | ||
266 | if ( firstST == 4) | |
267 | break; | |
268 | } | |
269 | return bitIndex; | |
270 | } | |
271 | ||
272 | int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){ | |
273 | ||
274 | PrintAndLog(" Path B"); | |
275 | ||
276 | int i,j, bitindex, lc, tolerance, warnings; | |
277 | warnings = 0; | |
278 | int upperlimit = len*2/clock+8; | |
279 | i = startIndex; | |
280 | j = 0; | |
281 | tolerance = clock/4; | |
282 | uint8_t decodedArr[len]; | |
283 | ||
284 | /* Detect duration between 2 successive transitions */ | |
285 | for (bitindex = 1; i < len; i++) { | |
286 | ||
287 | if (data[i-1] != data[i]) { | |
288 | lc = i - startIndex; | |
289 | startIndex = i; | |
290 | ||
291 | // Error check: if bitindex becomes too large, we do not | |
292 | // have a Manchester encoded bitstream or the clock is really wrong! | |
293 | if (bitindex > upperlimit ) { | |
294 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
295 | return 0; | |
296 | } | |
297 | // Then switch depending on lc length: | |
298 | // Tolerance is 1/4 of clock rate (arbitrary) | |
299 | if (abs((lc-clock)/2) < tolerance) { | |
300 | // Short pulse : either "1" or "0" | |
301 | decodedArr[bitindex++] = data[i-1]; | |
302 | } else if (abs(lc-clock) < tolerance) { | |
303 | // Long pulse: either "11" or "00" | |
304 | decodedArr[bitindex++] = data[i-1]; | |
305 | decodedArr[bitindex++] = data[i-1]; | |
306 | } else { | |
307 | ++warnings; | |
308 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); | |
309 | if (warnings > 10) { | |
310 | PrintAndLog("Error: too many detection errors, aborting."); | |
311 | return 0; | |
312 | } | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
317 | /* | |
318 | * We have a decodedArr of "01" ("1") or "10" ("0") | |
319 | * parse it into final decoded dataout | |
320 | */ | |
321 | for (i = 0; i < bitindex; i += 2) { | |
322 | ||
323 | if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) { | |
324 | dataout[j++] = 1; | |
325 | } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) { | |
326 | dataout[j++] = 0; | |
327 | } else { | |
328 | i++; | |
329 | warnings++; | |
330 | PrintAndLog("Unsynchronized, resync..."); | |
331 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
332 | ||
333 | if (warnings > 10) { | |
334 | PrintAndLog("Error: too many decode errors, aborting."); | |
335 | return 0; | |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | PrintAndLog("%s", sprint_hex(dataout, j)); | |
341 | return j; | |
342 | } | |
343 | ||
344 | void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){ | |
345 | /* | |
346 | * We have a bitstream of "01" ("1") or "10" ("0") | |
347 | * parse it into final decoded bitstream | |
348 | */ | |
349 | int i, j, warnings; | |
350 | uint8_t decodedArr[(len/2)+1]; | |
351 | ||
352 | j = warnings = 0; | |
353 | ||
354 | uint8_t lastbit = 0; | |
355 | ||
356 | for (i = 0; i < len; i += 2) { | |
357 | ||
358 | uint8_t first = bitstream[i]; | |
359 | uint8_t second = bitstream[i+1]; | |
360 | ||
361 | if ( first == second ) { | |
362 | ++i; | |
363 | ++warnings; | |
364 | if (warnings > 10) { | |
365 | PrintAndLog("Error: too many decode errors, aborting."); | |
366 | return; | |
367 | } | |
368 | } | |
369 | else if ( lastbit != first ) { | |
370 | decodedArr[j++] = 0 ^ invert; | |
371 | } | |
372 | else { | |
373 | decodedArr[j++] = 1 ^ invert; | |
374 | } | |
375 | lastbit = second; | |
376 | } | |
377 | ||
378 | PrintAndLog("%s", sprint_hex(decodedArr, j)); | |
379 | } | |
380 | ||
381 | void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){ | |
382 | ||
383 | PrintAndLog(" Manchester decoded : %d bits", len); | |
384 | ||
385 | uint8_t mod = len % blocksize; | |
386 | uint8_t div = len / blocksize; | |
387 | int i; | |
388 | ||
389 | // Now output the bitstream to the scrollback by line of 16 bits | |
390 | for (i = 0; i < div*blocksize; i+=blocksize) { | |
391 | PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) ); | |
392 | } | |
393 | ||
394 | if ( mod > 0 ) | |
395 | PrintAndLog(" %s", sprint_bin(bitStream+i, mod) ); | |
396 | } | |
397 | ||
398 | void iceFsk(int * data, const size_t len){ | |
399 | ||
400 | //34359738 == 125khz (2^32 / 125) = | |
401 | ||
402 | // parameters | |
403 | float phase_offset = 0.00f; // carrier phase offset | |
404 | float frequency_offset = 0.30f; // carrier frequency offset | |
405 | float wn = 0.01f; // pll bandwidth | |
406 | float zeta = 0.707f; // pll damping factor | |
407 | float K = 1000; // pll loop gain | |
408 | size_t n = len; // number of samples | |
409 | ||
410 | // generate loop filter parameters (active PI design) | |
411 | float t1 = K/(wn*wn); // tau_1 | |
412 | float t2 = 2*zeta/wn; // tau_2 | |
413 | ||
414 | // feed-forward coefficients (numerator) | |
415 | float b0 = (4*K/t1)*(1.+t2/2.0f); | |
416 | float b1 = (8*K/t1); | |
417 | float b2 = (4*K/t1)*(1.-t2/2.0f); | |
418 | ||
419 | // feed-back coefficients (denominator) | |
420 | // a0 = 1.0 is implied | |
421 | float a1 = -2.0f; | |
422 | float a2 = 1.0f; | |
423 | ||
424 | // filter buffer | |
425 | float v0=0.0f, v1=0.0f, v2=0.0f; | |
426 | ||
427 | // initialize states | |
428 | float phi = phase_offset; // input signal's initial phase | |
429 | float phi_hat = 0.0f; // PLL's initial phase | |
430 | ||
431 | unsigned int i; | |
432 | float complex x,y; | |
433 | float complex output[n]; | |
434 | ||
435 | for (i=0; i<n; i++) { | |
436 | // INPUT SIGNAL | |
437 | x = data[i]; | |
438 | phi += frequency_offset; | |
439 | ||
440 | // generate complex sinusoid | |
441 | y = cosf(phi_hat) + _Complex_I*sinf(phi_hat); | |
442 | ||
443 | output[i] = y; | |
444 | ||
445 | // compute error estimate | |
446 | float delta_phi = cargf( x * conjf(y) ); | |
447 | ||
448 | ||
449 | // print results to standard output | |
450 | printf(" %6u %12.8f %12.8f %12.8f %12.8f %12.8f\n", | |
451 | i, | |
452 | crealf(x), cimagf(x), | |
453 | crealf(y), cimagf(y), | |
454 | delta_phi); | |
455 | ||
456 | // push result through loop filter, updating phase estimate | |
457 | ||
458 | // advance buffer | |
459 | v2 = v1; // shift center register to upper register | |
460 | v1 = v0; // shift lower register to center register | |
461 | ||
462 | // compute new lower register | |
463 | v0 = delta_phi - v1*a1 - v2*a2; | |
464 | ||
465 | // compute new output | |
466 | phi_hat = v0*b0 + v1*b1 + v2*b2; | |
467 | ||
468 | } | |
469 | ||
470 | for (i=0; i<len; ++i){ | |
471 | data[i] = (int)crealf(output[i]); | |
472 | } | |
473 | } | |
474 | ||
475 | /* Sliding DFT | |
476 | Smooths out | |
477 | */ | |
478 | void iceFsk2(int * data, const size_t len){ | |
479 | ||
480 | int i, j; | |
481 | int output[len]; | |
482 | ||
483 | // for (i=0; i<len-5; ++i){ | |
484 | // for ( j=1; j <=5; ++j) { | |
485 | // output[i] += data[i*j]; | |
486 | // } | |
487 | // output[i] /= 5; | |
488 | // } | |
489 | int rest = 127; | |
490 | int tmp =0; | |
491 | for (i=0; i<len; ++i){ | |
492 | if ( data[i] < 127) | |
493 | output[i] = 0; | |
494 | else { | |
495 | tmp = (100 * (data[i]-rest)) / rest; | |
496 | output[i] = (tmp > 60)? 100:0; | |
497 | } | |
498 | } | |
499 | ||
500 | for (j=0; j<len; ++j) | |
501 | data[j] = output[j]; | |
502 | } | |
503 | ||
504 | void iceFsk3(int * data, const size_t len){ | |
505 | ||
506 | int i,j; | |
507 | int output[len]; | |
508 | float fc = 0.1125f; // center frequency | |
509 | ||
510 | // create very simple low-pass filter to remove images (2nd-order Butterworth) | |
511 | float complex iir_buf[3] = {0,0,0}; | |
512 | float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929}; | |
513 | float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023}; | |
514 | ||
515 | // process entire input file one sample at a time | |
516 | float sample = 0; // input sample read from file | |
517 | float complex x_prime = 1.0f; // save sample for estimating frequency | |
518 | float complex x; | |
519 | ||
520 | for (i=0; i<len; ++i) { | |
521 | ||
522 | sample = data[i]; | |
523 | ||
524 | // remove DC offset and mix to complex baseband | |
525 | x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i ); | |
526 | ||
527 | // apply low-pass filter, removing spectral image (IIR using direct-form II) | |
528 | iir_buf[2] = iir_buf[1]; | |
529 | iir_buf[1] = iir_buf[0]; | |
530 | iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2]; | |
531 | x = b[0]*iir_buf[0] + | |
532 | b[1]*iir_buf[1] + | |
533 | b[2]*iir_buf[2]; | |
534 | ||
535 | // compute instantaneous frequency by looking at phase difference | |
536 | // between adjacent samples | |
537 | float freq = cargf(x*conjf(x_prime)); | |
538 | x_prime = x; // retain this sample for next iteration | |
539 | ||
540 | output[i] =(freq > 0)? 10 : -10; | |
541 | } | |
542 | ||
543 | // show data | |
544 | for (j=0; j<len; ++j) | |
545 | data[j] = output[j]; | |
546 | ||
547 | CmdLtrim("30"); | |
548 | ||
549 | // zero crossings. | |
550 | for (j=0; j<len; ++j){ | |
551 | if ( data[j] == 10) break; | |
552 | } | |
553 | int startOne =j; | |
554 | ||
555 | for (;j<len; ++j){ | |
556 | if ( data[j] == -10 ) break; | |
557 | } | |
558 | int stopOne = j-1; | |
559 | ||
560 | int fieldlen = stopOne-startOne; | |
561 | printf("FIELD Length: %d \n", fieldlen); | |
562 | ||
563 | ||
564 | // FSK sequence start == 000111 | |
565 | int startPos = 0; | |
566 | for (i =0; i<len; ++i){ | |
567 | int dec = 0; | |
568 | for ( j = 0; j < 6*fieldlen; ++j){ | |
569 | dec += data[i + j]; | |
570 | } | |
571 | if (dec == 0) { | |
572 | startPos = i; | |
573 | break; | |
574 | } | |
575 | } | |
576 | ||
577 | printf("000111 position: %d \n", startPos); | |
578 | ||
579 | startPos += 6*fieldlen+1; | |
580 | ||
581 | printf("BINARY\n"); | |
582 | printf("R/40 : "); | |
583 | for (i =startPos ; i < len; i += 40){ | |
584 | if ( data[i] > 0 ) | |
585 | printf("1"); | |
586 | else | |
587 | printf("0"); | |
588 | } | |
589 | printf("\n"); | |
590 | ||
591 | printf("R/50 : "); | |
592 | for (i =startPos ; i < len; i += 50){ | |
593 | if ( data[i] > 0 ) | |
594 | printf("1"); | |
595 | else | |
596 | printf("0"); | |
597 | } | |
598 | printf("\n"); | |
599 | ||
600 | } | |
601 | ||
602 | float complex cexpf (float complex Z) | |
603 | { | |
604 | float complex Res; | |
605 | double rho = exp (__real__ Z); | |
606 | __real__ Res = rho * cosf(__imag__ Z); | |
607 | __imag__ Res = rho * sinf(__imag__ Z); | |
608 | return Res; | |
609 | } |