]>
Commit | Line | Data |
---|---|---|
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> |
7fe9b0b7 | 19 | #include "ui.h" |
f6c18637 | 20 | #include "loclass/cipherutils.h" |
7fe9b0b7 | 21 | |
22 | double CursorScaleFactor; | |
7ddb9900 | 23 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; |
7fe9b0b7 | 24 | int offline; |
ed77aabe | 25 | int flushAfterWrite = 0; //buzzy |
9492e0b0 | 26 | extern pthread_mutex_t print_lock; |
27 | ||
7fe9b0b7 | 28 | static char *logfilename = "proxmark3.log"; |
29 | ||
30 | void PrintAndLog(char *fmt, ...) | |
31 | { | |
51969283 M |
32 | char *saved_line; |
33 | int saved_point; | |
9492e0b0 | 34 | va_list argptr, argptr2; |
35 | static FILE *logfile = NULL; | |
36 | static int logging=1; | |
7fe9b0b7 | 37 | |
9492e0b0 | 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 | } | |
51969283 M |
48 | |
49 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
7fe9b0b7 | 50 | |
51969283 M |
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 | ||
9492e0b0 | 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"); | |
51969283 M |
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 | ||
9492e0b0 | 74 | if (logging && logfile) { |
75 | vfprintf(logfile, fmt, argptr2); | |
76 | fprintf(logfile,"\n"); | |
77 | fflush(logfile); | |
78 | } | |
79 | va_end(argptr2); | |
80 | ||
ed77aabe | 81 | if (flushAfterWrite == 1) //buzzy |
82 | { | |
83 | fflush(NULL); | |
84 | } | |
9492e0b0 | 85 | //release lock |
86 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 87 | } |
88 | ||
89 | void SetLogFilename(char *fn) | |
90 | { | |
91 | logfilename = fn; | |
92 | } | |
f38a1528 | 93 | |
f6c18637 | 94 | int manchester_decode( int * data, const size_t len, uint8_t * dataout){ |
f38a1528 | 95 | |
b44e5233 | 96 | int bitlength = 0; |
97 | int i, clock, high, low, startindex; | |
98 | low = startindex = 0; | |
f38a1528 | 99 | high = 1; |
b44e5233 | 100 | uint8_t bitStream[len]; |
f6c18637 | 101 | |
102 | memset(bitStream, 0x00, len); | |
b44e5233 | 103 | |
f38a1528 | 104 | /* Detect high and lows */ |
b44e5233 | 105 | for (i = 0; i < len; i++) { |
f38a1528 | 106 | if (data[i] > high) |
107 | high = data[i]; | |
108 | else if (data[i] < low) | |
109 | low = data[i]; | |
110 | } | |
111 | ||
112 | /* get clock */ | |
b44e5233 | 113 | clock = GetT55x7Clock( data, len, high ); |
f6c18637 | 114 | startindex = DetectFirstTransition(data, len, high); |
b44e5233 | 115 | |
f6c18637 | 116 | PrintAndLog(" Clock : %d", clock); |
117 | PrintAndLog(" startindex : %d", startindex); | |
b44e5233 | 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 | ||
f6c18637 | 124 | if ( bitlength > 0 ) |
b44e5233 | 125 | PrintPaddedManchester(bitStream, bitlength, clock); |
b44e5233 | 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) { | |
f38a1528 | 149 | /* if this is the beginning of a peak */ |
b44e5233 | 150 | if ( data[i-1] != data[i] && data[i] == peak) { |
f38a1528 | 151 | /* find lowest difference between peaks */ |
152 | if (lastpeak && i - lastpeak < clock) | |
153 | clock = i - lastpeak; | |
154 | lastpeak = i; | |
155 | } | |
156 | } | |
b44e5233 | 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 | } | |
f6c18637 | 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; | |
b44e5233 | 183 | } |
184 | ||
f6c18637 | 185 | int DetectFirstTransition(const int * data, const size_t len, int threshold){ |
b44e5233 | 186 | |
f6c18637 | 187 | int i =0; |
188 | /* now look for the first threshold */ | |
189 | for (; i < len; ++i) { | |
190 | if (data[i] == threshold) { | |
f38a1528 | 191 | break; |
192 | } | |
f6c18637 | 193 | } |
194 | return i; | |
b44e5233 | 195 | } |
196 | ||
197 | int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){ | |
198 | ||
f6c18637 | 199 | int i, j, z, hithigh, hitlow, bitIndex, startType; |
200 | i = 0; | |
b44e5233 | 201 | bitIndex = 0; |
f6c18637 | 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; | |
b44e5233 | 208 | |
f6c18637 | 209 | // i = clock frame of data |
b44e5233 | 210 | for (; i < (int)(len / clock); i++) |
f38a1528 | 211 | { |
f38a1528 | 212 | hithigh = 0; |
213 | hitlow = 0; | |
f6c18637 | 214 | startType = -1; |
215 | z = startIndex + (i*clock); | |
216 | isDamp = 0; | |
217 | ||
218 | ||
f38a1528 | 219 | /* Find out if we hit both high and low peaks */ |
220 | for (j = 0; j < clock; j++) | |
f6c18637 | 221 | { |
222 | if (data[z+j] == high){ | |
f38a1528 | 223 | hithigh = 1; |
f6c18637 | 224 | if ( startType == -1) |
225 | startType = 1; | |
226 | } | |
227 | ||
228 | if (data[z+j] == low ){ | |
f38a1528 | 229 | hitlow = 1; |
f6c18637 | 230 | if ( startType == -1) |
231 | startType = 0; | |
232 | } | |
233 | ||
f38a1528 | 234 | if (hithigh && hitlow) |
235 | break; | |
b44e5233 | 236 | } |
f6c18637 | 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 | } | |
f38a1528 | 252 | |
f6c18637 | 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; | |
f38a1528 | 270 | } |
b44e5233 | 271 | return bitIndex; |
272 | } | |
273 | ||
274 | int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){ | |
275 | ||
f6c18637 | 276 | PrintAndLog(" Path B"); |
277 | ||
b44e5233 | 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 | ||
f6c18637 | 286 | /* Detect duration between 2 successive transitions */ |
b44e5233 | 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; | |
f38a1528 | 314 | } |
315 | } | |
316 | } | |
317 | } | |
b44e5233 | 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 { | |
f38a1528 | 330 | i++; |
331 | warnings++; | |
332 | PrintAndLog("Unsynchronized, resync..."); | |
b44e5233 | 333 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); |
334 | ||
335 | if (warnings > 10) { | |
f38a1528 | 336 | PrintAndLog("Error: too many decode errors, aborting."); |
337 | return 0; | |
338 | } | |
339 | } | |
340 | } | |
b44e5233 | 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]; | |
f38a1528 | 353 | |
b44e5233 | 354 | j = warnings = 0; |
f38a1528 | 355 | |
b44e5233 | 356 | uint8_t lastbit = 0; |
f38a1528 | 357 | |
b44e5233 | 358 | for (i = 0; i < len; i += 2) { |
359 | ||
360 | uint8_t first = bitstream[i]; | |
361 | uint8_t second = bitstream[i+1]; | |
f38a1528 | 362 | |
b44e5233 | 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 | ||
f38a1528 | 383 | void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){ |
384 | ||
f6c18637 | 385 | PrintAndLog(" Manchester decoded : %d bits", len); |
f38a1528 | 386 | |
f6c18637 | 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) { | |
f38a1528 | 393 | PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) ); |
f6c18637 | 394 | } |
395 | ||
396 | if ( mod > 0 ) | |
397 | PrintAndLog(" %s", sprint_bin(bitStream+i, mod) ); | |
398 | } |