]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/ui.c
chg: LF t55xx trace
[proxmark3-svn] / client / ui.c
... / ...
CommitLineData
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 "ui.h"
20#include "loclass/cipherutils.h"
21
22double CursorScaleFactor;
23int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
24int offline;
25int flushAfterWrite = 0; //buzzy
26extern pthread_mutex_t print_lock;
27
28static char *logfilename = "proxmark3.log";
29
30void PrintAndLog(char *fmt, ...)
31{
32 char *saved_line;
33 int saved_point;
34 va_list argptr, argptr2;
35 static FILE *logfile = NULL;
36 static int logging=1;
37
38 // lock this section to avoid interlacing prints from different threats
39 pthread_mutex_lock(&print_lock);
40
41 if (logging && !logfile) {
42 logfile=fopen(logfilename, "a");
43 if (!logfile) {
44 fprintf(stderr, "Can't open logfile, logging disabled!\n");
45 logging=0;
46 }
47 }
48
49 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
50
51 if (need_hack) {
52 saved_point = rl_point;
53 saved_line = rl_copy_text(0, rl_end);
54 rl_save_prompt();
55 rl_replace_line("", 0);
56 rl_redisplay();
57 }
58
59 va_start(argptr, fmt);
60 va_copy(argptr2, argptr);
61 vprintf(fmt, argptr);
62 printf(" "); // cleaning prompt
63 va_end(argptr);
64 printf("\n");
65
66 if (need_hack) {
67 rl_restore_prompt();
68 rl_replace_line(saved_line, 0);
69 rl_point = saved_point;
70 rl_redisplay();
71 free(saved_line);
72 }
73
74 if (logging && logfile) {
75 vfprintf(logfile, fmt, argptr2);
76 fprintf(logfile,"\n");
77 fflush(logfile);
78 }
79 va_end(argptr2);
80
81 if (flushAfterWrite == 1) //buzzy
82 {
83 fflush(NULL);
84 }
85 //release lock
86 pthread_mutex_unlock(&print_lock);
87}
88
89void SetLogFilename(char *fn)
90{
91 logfilename = fn;
92}
93
94int manchester_decode( int * data, const size_t len, uint8_t * dataout){
95
96 int bitlength = 0;
97 int i, clock, high, low, startindex;
98 low = startindex = 0;
99 high = 1;
100 uint8_t bitStream[len];
101
102 memset(bitStream, 0x00, len);
103
104 /* Detect high and lows */
105 for (i = 0; i < len; i++) {
106 if (data[i] > high)
107 high = data[i];
108 else if (data[i] < low)
109 low = data[i];
110 }
111
112 /* get clock */
113 clock = GetT55x7Clock( data, len, high );
114 startindex = DetectFirstTransition(data, len, high);
115
116 PrintAndLog(" Clock : %d", clock);
117 PrintAndLog(" startindex : %d", startindex);
118
119 if (high != 1)
120 bitlength = ManchesterConvertFrom255(data, len, bitStream, high, low, clock, startindex);
121 else
122 bitlength= ManchesterConvertFrom1(data, len, bitStream, clock, startindex);
123
124 if ( bitlength > 0 )
125 PrintPaddedManchester(bitStream, bitlength, clock);
126
127 memcpy(dataout, bitStream, bitlength);
128
129 free(bitStream);
130 return bitlength;
131}
132
133 int GetT55x7Clock( const int * data, const size_t len, int peak ){
134
135 int i,lastpeak,clock;
136 clock = 0xFFFF;
137 lastpeak = 0;
138
139 /* Detect peak if we don't have one */
140 if (!peak) {
141 for (i = 0; i < len; ++i) {
142 if (data[i] > peak) {
143 peak = data[i];
144 }
145 }
146 }
147
148 for (i = 1; i < len; ++i) {
149 /* if this is the beginning of a peak */
150 if ( data[i-1] != data[i] && data[i] == peak) {
151 /* find lowest difference between peaks */
152 if (lastpeak && i - lastpeak < clock)
153 clock = i - lastpeak;
154 lastpeak = i;
155 }
156 }
157 //return clock;
158 //defaults clock to precise values.
159 switch(clock){
160 case 8:
161 case 16:
162 case 32:
163 case 40:
164 case 50:
165 case 64:
166 case 100:
167 case 128:
168 return clock;
169 break;
170 default: break;
171 }
172
173 PrintAndLog(" Found Clock : %d - trying to adjust", clock);
174
175 // When detected clock is 31 or 33 then then return
176 int clockmod = clock%8;
177 if ( clockmod == 7 )
178 clock += 1;
179 else if ( clockmod == 1 )
180 clock -= 1;
181
182 return clock;
183 }
184
185 int DetectFirstTransition(const int * data, const size_t len, int threshold){
186
187 int i =0;
188 /* now look for the first threshold */
189 for (; i < len; ++i) {
190 if (data[i] == threshold) {
191 break;
192 }
193 }
194 return i;
195 }
196
197 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){
198
199 int i, j, z, hithigh, hitlow, bitIndex, startType;
200 i = 0;
201 bitIndex = 0;
202
203 int isDamp = 0;
204 int damplimit = (int)((high / 2) * 0.3);
205 int dampHi = (high/2)+damplimit;
206 int dampLow = (high/2)-damplimit;
207 int firstST = 0;
208
209 // i = clock frame of data
210 for (; i < (int)(len / clock); i++)
211 {
212 hithigh = 0;
213 hitlow = 0;
214 startType = -1;
215 z = startIndex + (i*clock);
216 isDamp = 0;
217
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/2; j++)
242 {
243 if (
244 (data[z+j] <= dampHi && data[z+j] >= dampLow)
245 ){
246 isDamp = 1;
247 }
248 else
249 isDamp = 0;
250 }
251 }
252
253 /* Manchester Switching..
254 0: High -> Low
255 1: Low -> High
256 */
257 if (startType == 0)
258 dataout[bitIndex++] = 1;
259 else if (startType == 1)
260 dataout[bitIndex++] = 0;
261 else
262 dataout[bitIndex++] = 2;
263
264 if ( isDamp ) {
265 firstST++;
266 }
267
268 if ( firstST == 4)
269 break;
270 }
271 return bitIndex;
272 }
273
274 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){
275
276 PrintAndLog(" Path B");
277
278 int i,j, bitindex, lc, tolerance, warnings;
279 warnings = 0;
280 int upperlimit = len*2/clock+8;
281 i = startIndex;
282 j = 0;
283 tolerance = clock/4;
284 uint8_t decodedArr[len];
285
286 /* Detect duration between 2 successive transitions */
287 for (bitindex = 1; i < len; i++) {
288
289 if (data[i-1] != data[i]) {
290 lc = i - startIndex;
291 startIndex = i;
292
293 // Error check: if bitindex becomes too large, we do not
294 // have a Manchester encoded bitstream or the clock is really wrong!
295 if (bitindex > upperlimit ) {
296 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
297 return 0;
298 }
299 // Then switch depending on lc length:
300 // Tolerance is 1/4 of clock rate (arbitrary)
301 if (abs((lc-clock)/2) < tolerance) {
302 // Short pulse : either "1" or "0"
303 decodedArr[bitindex++] = data[i-1];
304 } else if (abs(lc-clock) < tolerance) {
305 // Long pulse: either "11" or "00"
306 decodedArr[bitindex++] = data[i-1];
307 decodedArr[bitindex++] = data[i-1];
308 } else {
309 ++warnings;
310 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
311 if (warnings > 10) {
312 PrintAndLog("Error: too many detection errors, aborting.");
313 return 0;
314 }
315 }
316 }
317 }
318
319 /*
320 * We have a decodedArr of "01" ("1") or "10" ("0")
321 * parse it into final decoded dataout
322 */
323 for (i = 0; i < bitindex; i += 2) {
324
325 if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
326 dataout[j++] = 1;
327 } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
328 dataout[j++] = 0;
329 } else {
330 i++;
331 warnings++;
332 PrintAndLog("Unsynchronized, resync...");
333 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
334
335 if (warnings > 10) {
336 PrintAndLog("Error: too many decode errors, aborting.");
337 return 0;
338 }
339 }
340 }
341
342 PrintAndLog("%s", sprint_hex(dataout, j));
343 return j;
344 }
345
346 void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
347 /*
348 * We have a bitstream of "01" ("1") or "10" ("0")
349 * parse it into final decoded bitstream
350 */
351 int i, j, warnings;
352 uint8_t decodedArr[(len/2)+1];
353
354 j = warnings = 0;
355
356 uint8_t lastbit = 0;
357
358 for (i = 0; i < len; i += 2) {
359
360 uint8_t first = bitstream[i];
361 uint8_t second = bitstream[i+1];
362
363 if ( first == second ) {
364 ++i;
365 ++warnings;
366 if (warnings > 10) {
367 PrintAndLog("Error: too many decode errors, aborting.");
368 return;
369 }
370 }
371 else if ( lastbit != first ) {
372 decodedArr[j++] = 0 ^ invert;
373 }
374 else {
375 decodedArr[j++] = 1 ^ invert;
376 }
377 lastbit = second;
378 }
379
380 PrintAndLog("%s", sprint_hex(decodedArr, j));
381}
382
383void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
384
385 PrintAndLog(" Manchester decoded : %d bits", len);
386
387 uint8_t mod = len % blocksize;
388 uint8_t div = len / blocksize;
389 int i;
390
391 // Now output the bitstream to the scrollback by line of 16 bits
392 for (i = 0; i < div*blocksize; i+=blocksize) {
393 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
394 }
395
396 if ( mod > 0 )
397 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
398}
Impressum, Datenschutz