]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
FIX: hf mf eload - now supports specifying 0,1,2,4 in card memory for the file to...
[proxmark3-svn] / client / ui.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 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
7fe9b0b7 12#include <stdarg.h>
51969283 13#include <stdlib.h>
7fe9b0b7 14#include <stdio.h>
f6c18637 15#include <stdbool.h>
7fe9b0b7 16#include <time.h>
51969283 17#include <readline/readline.h>
9492e0b0 18#include <pthread.h>
f6c18637 19#include "loclass/cipherutils.h"
7bd30f12 20#include "ui.h"
081151ea 21#include "cmdmain.h"
22#include "cmddata.h"
7bd30f12 23//#include <liquid/liquid.h>
24#define M_PI 3.14159265358979323846264338327
7fe9b0b7 25
26double CursorScaleFactor;
7ddb9900 27int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
7fe9b0b7 28int offline;
ed77aabe 29int flushAfterWrite = 0; //buzzy
9492e0b0 30extern pthread_mutex_t print_lock;
31
7fe9b0b7 32static char *logfilename = "proxmark3.log";
33
34void PrintAndLog(char *fmt, ...)
35{
51969283
M
36 char *saved_line;
37 int saved_point;
9492e0b0 38 va_list argptr, argptr2;
39 static FILE *logfile = NULL;
40 static int logging=1;
7fe9b0b7 41
9492e0b0 42 // lock this section to avoid interlacing prints from different threats
43 pthread_mutex_lock(&print_lock);
44
45 if (logging && !logfile) {
46 logfile=fopen(logfilename, "a");
47 if (!logfile) {
48 fprintf(stderr, "Can't open logfile, logging disabled!\n");
49 logging=0;
50 }
51 }
51969283
M
52
53 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 54
51969283
M
55 if (need_hack) {
56 saved_point = rl_point;
57 saved_line = rl_copy_text(0, rl_end);
58 rl_save_prompt();
59 rl_replace_line("", 0);
60 rl_redisplay();
61 }
62
9492e0b0 63 va_start(argptr, fmt);
64 va_copy(argptr2, argptr);
65 vprintf(fmt, argptr);
66 printf(" "); // cleaning prompt
67 va_end(argptr);
68 printf("\n");
51969283
M
69
70 if (need_hack) {
71 rl_restore_prompt();
72 rl_replace_line(saved_line, 0);
73 rl_point = saved_point;
74 rl_redisplay();
75 free(saved_line);
76 }
77
9492e0b0 78 if (logging && logfile) {
79 vfprintf(logfile, fmt, argptr2);
80 fprintf(logfile,"\n");
81 fflush(logfile);
463ca973 82 fclose(logfile); // ICEMAN, this logfile is never closed?!?
9492e0b0 83 }
84 va_end(argptr2);
85
ed77aabe 86 if (flushAfterWrite == 1) //buzzy
87 {
88 fflush(NULL);
89 }
9492e0b0 90 //release lock
91 pthread_mutex_unlock(&print_lock);
7fe9b0b7 92}
93
94void SetLogFilename(char *fn)
95{
96 logfilename = fn;
97}
f38a1528 98
c6be64da 99int manchester_decode( int * data, const size_t len, uint8_t * dataout, size_t dataoutlen){
f38a1528 100
b44e5233 101 int bitlength = 0;
102 int i, clock, high, low, startindex;
103 low = startindex = 0;
f38a1528 104 high = 1;
c6be64da 105 uint8_t * bitStream = (uint8_t* ) malloc(sizeof(uint8_t) * dataoutlen);
106 memset(bitStream, 0x00, dataoutlen);
b44e5233 107
f38a1528 108 /* Detect high and lows */
b44e5233 109 for (i = 0; i < len; i++) {
f38a1528 110 if (data[i] > high)
111 high = data[i];
112 else if (data[i] < low)
113 low = data[i];
114 }
115
116 /* get clock */
b44e5233 117 clock = GetT55x7Clock( data, len, high );
f6c18637 118 startindex = DetectFirstTransition(data, len, high);
b44e5233 119
c6be64da 120 //PrintAndLog(" Clock : %d", clock);
149aeada 121
b44e5233 122 if (high != 1)
c6be64da 123 bitlength = ManchesterConvertFrom255(data, len, bitStream, dataoutlen, high, low, clock, startindex);
b44e5233 124 else
c6be64da 125 bitlength= ManchesterConvertFrom1(data, len, bitStream, dataoutlen, clock, startindex);
b44e5233 126
b44e5233 127 memcpy(dataout, bitStream, bitlength);
149aeada 128 free(bitStream);
b44e5233 129 return bitlength;
130}
131
132 int GetT55x7Clock( const int * data, const size_t len, int peak ){
133
134 int i,lastpeak,clock;
135 clock = 0xFFFF;
136 lastpeak = 0;
137
138 /* Detect peak if we don't have one */
139 if (!peak) {
140 for (i = 0; i < len; ++i) {
141 if (data[i] > peak) {
142 peak = data[i];
143 }
144 }
145 }
146
147 for (i = 1; i < len; ++i) {
f38a1528 148 /* if this is the beginning of a peak */
b44e5233 149 if ( data[i-1] != data[i] && data[i] == peak) {
f38a1528 150 /* find lowest difference between peaks */
151 if (lastpeak && i - lastpeak < clock)
152 clock = i - lastpeak;
153 lastpeak = i;
154 }
155 }
f6c18637 156
157 // When detected clock is 31 or 33 then then return
158 int clockmod = clock%8;
a61b4976 159 if ( clockmod == 0) return clock;
160
161 if ( clockmod == 7 ) clock += 1;
162 else if ( clockmod == 1 ) clock -= 1;
f6c18637 163
164 return clock;
b44e5233 165 }
166
f6c18637 167 int DetectFirstTransition(const int * data, const size_t len, int threshold){
b44e5233 168
f6c18637 169 int i =0;
170 /* now look for the first threshold */
171 for (; i < len; ++i) {
172 if (data[i] == threshold) {
f38a1528 173 break;
174 }
f6c18637 175 }
176 return i;
b44e5233 177 }
178
c6be64da 179 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int dataoutlen, int high, int low, int clock, int startIndex){
b44e5233 180
f6c18637 181 int i, j, z, hithigh, hitlow, bitIndex, startType;
182 i = 0;
b44e5233 183 bitIndex = 0;
f6c18637 184
185 int isDamp = 0;
186 int damplimit = (int)((high / 2) * 0.3);
187 int dampHi = (high/2)+damplimit;
188 int dampLow = (high/2)-damplimit;
189 int firstST = 0;
b44e5233 190
f6c18637 191 // i = clock frame of data
c6be64da 192 for (; i < (int)(len/clock); i++)
f38a1528 193 {
f38a1528 194 hithigh = 0;
195 hitlow = 0;
f6c18637 196 startType = -1;
197 z = startIndex + (i*clock);
198 isDamp = 0;
77376577 199
f38a1528 200 /* Find out if we hit both high and low peaks */
201 for (j = 0; j < clock; j++)
f6c18637 202 {
203 if (data[z+j] == high){
f38a1528 204 hithigh = 1;
f6c18637 205 if ( startType == -1)
206 startType = 1;
207 }
208
209 if (data[z+j] == low ){
f38a1528 210 hitlow = 1;
f6c18637 211 if ( startType == -1)
212 startType = 0;
213 }
214
f38a1528 215 if (hithigh && hitlow)
216 break;
b44e5233 217 }
f6c18637 218
219 // No high value found, are we in a dampening field?
220 if ( !hithigh ) {
221 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
081151ea 222 for (j = 0; j < clock; j++) {
f6c18637 223 if (
224 (data[z+j] <= dampHi && data[z+j] >= dampLow)
225 ){
77376577 226 isDamp++;
f6c18637 227 }
f6c18637 228 }
229 }
f38a1528 230
f6c18637 231 /* Manchester Switching..
232 0: High -> Low
233 1: Low -> High
234 */
235 if (startType == 0)
236 dataout[bitIndex++] = 1;
237 else if (startType == 1)
238 dataout[bitIndex++] = 0;
239 else
240 dataout[bitIndex++] = 2;
241
77376577 242 if ( isDamp > clock/2 ) {
f6c18637 243 firstST++;
244 }
245
246 if ( firstST == 4)
247 break;
c6be64da 248 if ( bitIndex >= dataoutlen-1 )
249 break;
f38a1528 250 }
b44e5233 251 return bitIndex;
252 }
253
c6be64da 254 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout,int dataoutlen, int clock, int startIndex){
b44e5233 255
f6c18637 256 PrintAndLog(" Path B");
257
b44e5233 258 int i,j, bitindex, lc, tolerance, warnings;
259 warnings = 0;
260 int upperlimit = len*2/clock+8;
261 i = startIndex;
262 j = 0;
263 tolerance = clock/4;
264 uint8_t decodedArr[len];
265
f6c18637 266 /* Detect duration between 2 successive transitions */
b44e5233 267 for (bitindex = 1; i < len; i++) {
268
269 if (data[i-1] != data[i]) {
270 lc = i - startIndex;
271 startIndex = i;
272
273 // Error check: if bitindex becomes too large, we do not
274 // have a Manchester encoded bitstream or the clock is really wrong!
275 if (bitindex > upperlimit ) {
276 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
277 return 0;
278 }
279 // Then switch depending on lc length:
280 // Tolerance is 1/4 of clock rate (arbitrary)
281 if (abs((lc-clock)/2) < tolerance) {
282 // Short pulse : either "1" or "0"
283 decodedArr[bitindex++] = data[i-1];
284 } else if (abs(lc-clock) < tolerance) {
285 // Long pulse: either "11" or "00"
286 decodedArr[bitindex++] = data[i-1];
287 decodedArr[bitindex++] = data[i-1];
288 } else {
289 ++warnings;
290 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
291 if (warnings > 10) {
292 PrintAndLog("Error: too many detection errors, aborting.");
293 return 0;
f38a1528 294 }
295 }
296 }
297 }
b44e5233 298
299 /*
300 * We have a decodedArr of "01" ("1") or "10" ("0")
301 * parse it into final decoded dataout
302 */
303 for (i = 0; i < bitindex; i += 2) {
304
305 if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
306 dataout[j++] = 1;
307 } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
308 dataout[j++] = 0;
309 } else {
f38a1528 310 i++;
311 warnings++;
312 PrintAndLog("Unsynchronized, resync...");
b44e5233 313 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
314
315 if (warnings > 10) {
f38a1528 316 PrintAndLog("Error: too many decode errors, aborting.");
317 return 0;
318 }
319 }
320 }
b44e5233 321
322 PrintAndLog("%s", sprint_hex(dataout, j));
323 return j;
324 }
325
326 void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
327 /*
328 * We have a bitstream of "01" ("1") or "10" ("0")
329 * parse it into final decoded bitstream
330 */
331 int i, j, warnings;
332 uint8_t decodedArr[(len/2)+1];
f38a1528 333
b44e5233 334 j = warnings = 0;
f38a1528 335
b44e5233 336 uint8_t lastbit = 0;
f38a1528 337
b44e5233 338 for (i = 0; i < len; i += 2) {
339
340 uint8_t first = bitstream[i];
341 uint8_t second = bitstream[i+1];
f38a1528 342
b44e5233 343 if ( first == second ) {
344 ++i;
345 ++warnings;
346 if (warnings > 10) {
347 PrintAndLog("Error: too many decode errors, aborting.");
348 return;
349 }
350 }
351 else if ( lastbit != first ) {
352 decodedArr[j++] = 0 ^ invert;
353 }
354 else {
355 decodedArr[j++] = 1 ^ invert;
356 }
357 lastbit = second;
358 }
359
360 PrintAndLog("%s", sprint_hex(decodedArr, j));
361}
362
f38a1528 363void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
364
f6c18637 365 PrintAndLog(" Manchester decoded : %d bits", len);
f38a1528 366
f6c18637 367 uint8_t mod = len % blocksize;
368 uint8_t div = len / blocksize;
369 int i;
370
371 // Now output the bitstream to the scrollback by line of 16 bits
372 for (i = 0; i < div*blocksize; i+=blocksize) {
f38a1528 373 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
f6c18637 374 }
375
376 if ( mod > 0 )
377 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
7bd30f12 378}
379
7bd30f12 380/* Sliding DFT
381 Smooths out
382*/
383void iceFsk2(int * data, const size_t len){
384
385 int i, j;
149aeada 386 int * output = (int* ) malloc(sizeof(int) * len);
387 memset(output, 0x00, len);
388
7bd30f12 389 // for (i=0; i<len-5; ++i){
390 // for ( j=1; j <=5; ++j) {
391 // output[i] += data[i*j];
392 // }
393 // output[i] /= 5;
394 // }
395 int rest = 127;
396 int tmp =0;
397 for (i=0; i<len; ++i){
398 if ( data[i] < 127)
399 output[i] = 0;
400 else {
401 tmp = (100 * (data[i]-rest)) / rest;
402 output[i] = (tmp > 60)? 100:0;
403 }
404 }
405
406 for (j=0; j<len; ++j)
407 data[j] = output[j];
149aeada 408
409 free(output);
7bd30f12 410}
411
412void iceFsk3(int * data, const size_t len){
413
414 int i,j;
149aeada 415
416 int * output = (int* ) malloc(sizeof(int) * len);
417 memset(output, 0x00, len);
418 float fc = 0.1125f; // center frequency
081151ea 419 size_t adjustedLen = len;
420
7bd30f12 421 // create very simple low-pass filter to remove images (2nd-order Butterworth)
422 float complex iir_buf[3] = {0,0,0};
423 float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
424 float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
425
081151ea 426 float sample = 0; // input sample read from file
427 float complex x_prime = 1.0f; // save sample for estimating frequency
7bd30f12 428 float complex x;
429
081151ea 430 for (i=0; i<adjustedLen; ++i) {
7bd30f12 431
081151ea 432 sample = data[i]+128;
7bd30f12 433
434 // remove DC offset and mix to complex baseband
435 x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i );
436
437 // apply low-pass filter, removing spectral image (IIR using direct-form II)
438 iir_buf[2] = iir_buf[1];
439 iir_buf[1] = iir_buf[0];
440 iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2];
441 x = b[0]*iir_buf[0] +
442 b[1]*iir_buf[1] +
443 b[2]*iir_buf[2];
444
445 // compute instantaneous frequency by looking at phase difference
446 // between adjacent samples
447 float freq = cargf(x*conjf(x_prime));
448 x_prime = x; // retain this sample for next iteration
449
450 output[i] =(freq > 0)? 10 : -10;
451 }
452
453 // show data
081151ea 454 for (j=0; j<adjustedLen; ++j)
7bd30f12 455 data[j] = output[j];
456
457 CmdLtrim("30");
081151ea 458 adjustedLen -= 30;
7bd30f12 459
460 // zero crossings.
081151ea 461 for (j=0; j<adjustedLen; ++j){
7bd30f12 462 if ( data[j] == 10) break;
463 }
464 int startOne =j;
465
081151ea 466 for (;j<adjustedLen; ++j){
7bd30f12 467 if ( data[j] == -10 ) break;
468 }
469 int stopOne = j-1;
470
471 int fieldlen = stopOne-startOne;
7bd30f12 472
fbceacc5 473 fieldlen = (fieldlen == 39 || fieldlen == 41)? 40 : fieldlen;
474 fieldlen = (fieldlen == 59 || fieldlen == 51)? 50 : fieldlen;
475 if ( fieldlen != 40 && fieldlen != 50){
476 printf("Detected field Length: %d \n", fieldlen);
081151ea 477 printf("Can only handle 40 or 50. Aborting...\n");
fbceacc5 478 return;
479 }
7bd30f12 480
481 // FSK sequence start == 000111
482 int startPos = 0;
081151ea 483 for (i =0; i<adjustedLen; ++i){
7bd30f12 484 int dec = 0;
485 for ( j = 0; j < 6*fieldlen; ++j){
486 dec += data[i + j];
487 }
488 if (dec == 0) {
489 startPos = i;
490 break;
491 }
492 }
493
494 printf("000111 position: %d \n", startPos);
495
72e930ef 496 startPos += 6*fieldlen+5;
7bd30f12 497
72e930ef 498 int bit =0;
7bd30f12 499 printf("BINARY\n");
500 printf("R/40 : ");
081151ea 501 for (i =startPos ; i < adjustedLen; i += 40){
72e930ef 502 bit = data[i]>0 ? 1:0;
503 printf("%d", bit );
7bd30f12 504 }
505 printf("\n");
506
507 printf("R/50 : ");
081151ea 508 for (i =startPos ; i < adjustedLen; i += 50){
72e930ef 509 bit = data[i]>0 ? 1:0;
510 printf("%d", bit ); }
7bd30f12 511 printf("\n");
512
149aeada 513 free(output);
7bd30f12 514}
515
516float complex cexpf (float complex Z)
517{
518 float complex Res;
519 double rho = exp (__real__ Z);
520 __real__ Res = rho * cosf(__imag__ Z);
521 __imag__ Res = rho * sinf(__imag__ Z);
522 return Res;
523}
Impressum, Datenschutz