]>
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> |
15 | #include <time.h> | |
51969283 | 16 | #include <readline/readline.h> |
9492e0b0 | 17 | #include <pthread.h> |
7fe9b0b7 | 18 | |
19 | #include "ui.h" | |
20 | ||
21 | double CursorScaleFactor; | |
7ddb9900 | 22 | int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64; |
7fe9b0b7 | 23 | int offline; |
ed77aabe | 24 | int flushAfterWrite = 0; //buzzy |
9492e0b0 | 25 | extern pthread_mutex_t print_lock; |
26 | ||
7fe9b0b7 | 27 | static char *logfilename = "proxmark3.log"; |
28 | ||
29 | void PrintAndLog(char *fmt, ...) | |
30 | { | |
51969283 M |
31 | char *saved_line; |
32 | int saved_point; | |
9492e0b0 | 33 | va_list argptr, argptr2; |
34 | static FILE *logfile = NULL; | |
35 | static int logging=1; | |
7fe9b0b7 | 36 | |
9492e0b0 | 37 | // lock this section to avoid interlacing prints from different threats |
38 | pthread_mutex_lock(&print_lock); | |
39 | ||
40 | if (logging && !logfile) { | |
41 | logfile=fopen(logfilename, "a"); | |
42 | if (!logfile) { | |
43 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
44 | logging=0; | |
45 | } | |
46 | } | |
51969283 M |
47 | |
48 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
7fe9b0b7 | 49 | |
51969283 M |
50 | if (need_hack) { |
51 | saved_point = rl_point; | |
52 | saved_line = rl_copy_text(0, rl_end); | |
53 | rl_save_prompt(); | |
54 | rl_replace_line("", 0); | |
55 | rl_redisplay(); | |
56 | } | |
57 | ||
9492e0b0 | 58 | va_start(argptr, fmt); |
59 | va_copy(argptr2, argptr); | |
60 | vprintf(fmt, argptr); | |
61 | printf(" "); // cleaning prompt | |
62 | va_end(argptr); | |
63 | printf("\n"); | |
51969283 M |
64 | |
65 | if (need_hack) { | |
66 | rl_restore_prompt(); | |
67 | rl_replace_line(saved_line, 0); | |
68 | rl_point = saved_point; | |
69 | rl_redisplay(); | |
70 | free(saved_line); | |
71 | } | |
72 | ||
9492e0b0 | 73 | if (logging && logfile) { |
74 | vfprintf(logfile, fmt, argptr2); | |
75 | fprintf(logfile,"\n"); | |
76 | fflush(logfile); | |
77 | } | |
78 | va_end(argptr2); | |
79 | ||
ed77aabe | 80 | if (flushAfterWrite == 1) //buzzy |
81 | { | |
82 | fflush(NULL); | |
83 | } | |
9492e0b0 | 84 | //release lock |
85 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 86 | } |
87 | ||
9492e0b0 | 88 | |
7fe9b0b7 | 89 | void SetLogFilename(char *fn) |
90 | { | |
91 | logfilename = fn; | |
92 | } | |
f38a1528 | 93 | |
94 | ||
b44e5233 | 95 | int manchester_decode(const int * data, const size_t len, uint8_t * dataout){ |
f38a1528 | 96 | |
b44e5233 | 97 | int bitlength = 0; |
98 | int i, clock, high, low, startindex; | |
99 | low = startindex = 0; | |
f38a1528 | 100 | high = 1; |
b44e5233 | 101 | uint8_t bitStream[len]; |
f38a1528 | 102 | |
b44e5233 | 103 | memset(bitStream, 0x00, len); |
104 | ||
f38a1528 | 105 | /* Detect high and lows */ |
b44e5233 | 106 | for (i = 0; i < len; i++) { |
f38a1528 | 107 | if (data[i] > high) |
108 | high = data[i]; | |
109 | else if (data[i] < low) | |
110 | low = data[i]; | |
111 | } | |
112 | ||
113 | /* get clock */ | |
b44e5233 | 114 | clock = GetT55x7Clock( data, len, high ); |
115 | startindex = DetectFirstTransition(data, len, high, low); | |
116 | ||
117 | PrintAndLog(" Clock : %d", clock); | |
118 | PrintAndLog(" startindex : %d", startindex); | |
119 | ||
120 | if (high != 1) | |
121 | bitlength = ManchesterConvertFrom255(data, len, bitStream, high, low, clock, startindex); | |
122 | else | |
123 | bitlength= ManchesterConvertFrom1(data, len, bitStream, clock, startindex); | |
124 | ||
125 | if ( bitlength > 0 ){ | |
126 | PrintPaddedManchester(bitStream, bitlength, clock); | |
127 | } | |
128 | ||
129 | memcpy(dataout, bitStream, bitlength); | |
130 | ||
131 | free(bitStream); | |
132 | return bitlength; | |
133 | } | |
134 | ||
135 | int GetT55x7Clock( const int * data, const size_t len, int peak ){ | |
136 | ||
137 | int i,lastpeak,clock; | |
138 | clock = 0xFFFF; | |
139 | lastpeak = 0; | |
140 | ||
141 | /* Detect peak if we don't have one */ | |
142 | if (!peak) { | |
143 | for (i = 0; i < len; ++i) { | |
144 | if (data[i] > peak) { | |
145 | peak = data[i]; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | for (i = 1; i < len; ++i) { | |
f38a1528 | 151 | /* if this is the beginning of a peak */ |
b44e5233 | 152 | if ( data[i-1] != data[i] && data[i] == peak) { |
f38a1528 | 153 | /* find lowest difference between peaks */ |
154 | if (lastpeak && i - lastpeak < clock) | |
155 | clock = i - lastpeak; | |
156 | lastpeak = i; | |
157 | } | |
158 | } | |
b44e5233 | 159 | //return clock; |
160 | //defaults clock to precise values. | |
161 | switch(clock){ | |
162 | case 8: | |
163 | case 16: | |
164 | case 32: | |
165 | case 40: | |
166 | case 50: | |
167 | case 64: | |
168 | case 100: | |
169 | case 128: | |
170 | return clock; | |
171 | break; | |
172 | default: break; | |
173 | } | |
174 | return 32; | |
175 | } | |
176 | ||
177 | int DetectFirstTransition(const int * data, const size_t len, int high, int low){ | |
178 | ||
179 | int i, retval; | |
180 | retval = 0; | |
181 | /* | |
182 | Detect first transition Lo-Hi (arbitrary) | |
183 | skip to the first high | |
184 | */ | |
185 | for (i = 0; i < len; ++i) | |
f38a1528 | 186 | if (data[i] == high) |
187 | break; | |
188 | ||
189 | /* now look for the first low */ | |
b44e5233 | 190 | for (; i < len; ++i) { |
f38a1528 | 191 | if (data[i] == low) { |
b44e5233 | 192 | retval = i; |
f38a1528 | 193 | break; |
194 | } | |
195 | } | |
b44e5233 | 196 | return retval; |
197 | } | |
198 | ||
199 | int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){ | |
200 | ||
201 | int i, j, hithigh, hitlow, first, bit, bitIndex; | |
202 | i = startIndex; | |
203 | bitIndex = 0; | |
204 | ||
205 | /* | |
206 | * We assume the 1st bit is zero, it may not be | |
207 | * the case: this routine (I think) has an init problem. | |
208 | * Ed. | |
209 | */ | |
210 | bit = 0; | |
211 | ||
212 | for (; i < (int)(len / clock); i++) | |
f38a1528 | 213 | { |
f38a1528 | 214 | hithigh = 0; |
215 | hitlow = 0; | |
216 | first = 1; | |
217 | ||
218 | /* Find out if we hit both high and low peaks */ | |
219 | for (j = 0; j < clock; j++) | |
220 | { | |
221 | if (data[(i * clock) + j] == high) | |
222 | hithigh = 1; | |
223 | else if (data[(i * clock) + j] == low) | |
224 | hitlow = 1; | |
225 | ||
226 | /* it doesn't count if it's the first part of our read | |
227 | because it's really just trailing from the last sequence */ | |
228 | if (first && (hithigh || hitlow)) | |
229 | hithigh = hitlow = 0; | |
230 | else | |
231 | first = 0; | |
232 | ||
233 | if (hithigh && hitlow) | |
234 | break; | |
b44e5233 | 235 | } |
f38a1528 | 236 | |
b44e5233 | 237 | /* If we didn't hit both high and low peaks, we had a bit transition */ |
238 | if (!hithigh || !hitlow) | |
f38a1528 | 239 | bit ^= 1; |
240 | ||
b44e5233 | 241 | dataout[bitIndex++] = bit; |
f38a1528 | 242 | } |
b44e5233 | 243 | return bitIndex; |
244 | } | |
245 | ||
246 | int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){ | |
247 | ||
248 | int i,j, bitindex, lc, tolerance, warnings; | |
249 | warnings = 0; | |
250 | int upperlimit = len*2/clock+8; | |
251 | i = startIndex; | |
252 | j = 0; | |
253 | tolerance = clock/4; | |
254 | uint8_t decodedArr[len]; | |
255 | ||
256 | /* Then detect duration between 2 successive transitions */ | |
257 | for (bitindex = 1; i < len; i++) { | |
258 | ||
259 | if (data[i-1] != data[i]) { | |
260 | lc = i - startIndex; | |
261 | startIndex = i; | |
262 | ||
263 | // Error check: if bitindex becomes too large, we do not | |
264 | // have a Manchester encoded bitstream or the clock is really wrong! | |
265 | if (bitindex > upperlimit ) { | |
266 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
267 | return 0; | |
268 | } | |
269 | // Then switch depending on lc length: | |
270 | // Tolerance is 1/4 of clock rate (arbitrary) | |
271 | if (abs((lc-clock)/2) < tolerance) { | |
272 | // Short pulse : either "1" or "0" | |
273 | decodedArr[bitindex++] = data[i-1]; | |
274 | } else if (abs(lc-clock) < tolerance) { | |
275 | // Long pulse: either "11" or "00" | |
276 | decodedArr[bitindex++] = data[i-1]; | |
277 | decodedArr[bitindex++] = data[i-1]; | |
278 | } else { | |
279 | ++warnings; | |
280 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); | |
281 | if (warnings > 10) { | |
282 | PrintAndLog("Error: too many detection errors, aborting."); | |
283 | return 0; | |
f38a1528 | 284 | } |
285 | } | |
286 | } | |
287 | } | |
b44e5233 | 288 | |
289 | /* | |
290 | * We have a decodedArr of "01" ("1") or "10" ("0") | |
291 | * parse it into final decoded dataout | |
292 | */ | |
293 | for (i = 0; i < bitindex; i += 2) { | |
294 | ||
295 | if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) { | |
296 | dataout[j++] = 1; | |
297 | } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) { | |
298 | dataout[j++] = 0; | |
299 | } else { | |
f38a1528 | 300 | i++; |
301 | warnings++; | |
302 | PrintAndLog("Unsynchronized, resync..."); | |
b44e5233 | 303 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); |
304 | ||
305 | if (warnings > 10) { | |
f38a1528 | 306 | PrintAndLog("Error: too many decode errors, aborting."); |
307 | return 0; | |
308 | } | |
309 | } | |
310 | } | |
b44e5233 | 311 | |
312 | PrintAndLog("%s", sprint_hex(dataout, j)); | |
313 | return j; | |
314 | } | |
315 | ||
316 | void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){ | |
317 | /* | |
318 | * We have a bitstream of "01" ("1") or "10" ("0") | |
319 | * parse it into final decoded bitstream | |
320 | */ | |
321 | int i, j, warnings; | |
322 | uint8_t decodedArr[(len/2)+1]; | |
f38a1528 | 323 | |
b44e5233 | 324 | j = warnings = 0; |
f38a1528 | 325 | |
b44e5233 | 326 | uint8_t lastbit = 0; |
f38a1528 | 327 | |
b44e5233 | 328 | for (i = 0; i < len; i += 2) { |
329 | ||
330 | uint8_t first = bitstream[i]; | |
331 | uint8_t second = bitstream[i+1]; | |
f38a1528 | 332 | |
b44e5233 | 333 | if ( first == second ) { |
334 | ++i; | |
335 | ++warnings; | |
336 | if (warnings > 10) { | |
337 | PrintAndLog("Error: too many decode errors, aborting."); | |
338 | return; | |
339 | } | |
340 | } | |
341 | else if ( lastbit != first ) { | |
342 | decodedArr[j++] = 0 ^ invert; | |
343 | } | |
344 | else { | |
345 | decodedArr[j++] = 1 ^ invert; | |
346 | } | |
347 | lastbit = second; | |
348 | } | |
349 | ||
350 | PrintAndLog("%s", sprint_hex(decodedArr, j)); | |
351 | } | |
352 | ||
353 | ||
f38a1528 | 354 | void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){ |
355 | ||
356 | PrintAndLog(" Manchester decoded bitstream : %d bits", len); | |
357 | ||
358 | uint8_t mod = len % blocksize; | |
359 | uint8_t div = len / blocksize; | |
360 | int i; | |
361 | // Now output the bitstream to the scrollback by line of 16 bits | |
362 | for (i = 0; i < div*blocksize; i+=blocksize) { | |
363 | PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) ); | |
364 | } | |
365 | if ( mod > 0 ){ | |
366 | PrintAndLog(" %s", sprint_bin(bitStream+i, mod) ); | |
367 | } | |
368 | } |