]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Data and Graph commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
15 | #include "proxmark3.h" | |
16 | #include "data.h" | |
17 | #include "ui.h" | |
18 | #include "graph.h" | |
19 | #include "cmdparser.h" | |
20 | #include "util.h" | |
21 | #include "cmdmain.h" | |
22 | #include "cmddata.h" | |
23 | #include "lfdemod.h" | |
24 | #include "usb_cmd.h" | |
25 | #include "crc.h" | |
26 | ||
27 | uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; | |
28 | uint8_t g_debugMode; | |
29 | int DemodBufferLen; | |
30 | static int CmdHelp(const char *Cmd); | |
31 | ||
32 | //set the demod buffer with given array of binary (one bit per byte) | |
33 | //by marshmellow | |
34 | void setDemodBuf(uint8_t *buff, size_t size, size_t startIdx) | |
35 | { | |
36 | if (buff == NULL) | |
37 | return; | |
38 | ||
39 | if ( size >= MAX_DEMOD_BUF_LEN) | |
40 | size = MAX_DEMOD_BUF_LEN; | |
41 | ||
42 | size_t i = 0; | |
43 | for (; i < size; i++){ | |
44 | DemodBuffer[i]=buff[startIdx++]; | |
45 | } | |
46 | DemodBufferLen=size; | |
47 | return; | |
48 | } | |
49 | ||
50 | int CmdSetDebugMode(const char *Cmd) | |
51 | { | |
52 | int demod=0; | |
53 | sscanf(Cmd, "%i", &demod); | |
54 | g_debugMode=(uint8_t)demod; | |
55 | return 1; | |
56 | } | |
57 | ||
58 | //by marshmellow | |
59 | void printDemodBuff(void) | |
60 | { | |
61 | uint32_t i = 0; | |
62 | int bitLen = DemodBufferLen; | |
63 | if (bitLen<16) { | |
64 | PrintAndLog("no bits found in demod buffer"); | |
65 | return; | |
66 | } | |
67 | if (bitLen>512) bitLen=512; //max output to 512 bits if we have more - should be plenty | |
68 | ||
69 | // ensure equally divided by 16 | |
70 | bitLen &= 0xfff0; | |
71 | ||
72 | for (i = 0; i <= (bitLen-16); i+=16) { | |
73 | PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i", | |
74 | DemodBuffer[i], | |
75 | DemodBuffer[i+1], | |
76 | DemodBuffer[i+2], | |
77 | DemodBuffer[i+3], | |
78 | DemodBuffer[i+4], | |
79 | DemodBuffer[i+5], | |
80 | DemodBuffer[i+6], | |
81 | DemodBuffer[i+7], | |
82 | DemodBuffer[i+8], | |
83 | DemodBuffer[i+9], | |
84 | DemodBuffer[i+10], | |
85 | DemodBuffer[i+11], | |
86 | DemodBuffer[i+12], | |
87 | DemodBuffer[i+13], | |
88 | DemodBuffer[i+14], | |
89 | DemodBuffer[i+15]); | |
90 | } | |
91 | return; | |
92 | } | |
93 | ||
94 | int CmdPrintDemodBuff(const char *Cmd) | |
95 | { | |
96 | char hex; | |
97 | char printBuff[512]={0x00}; | |
98 | uint8_t numBits = DemodBufferLen & 0xFFF0; | |
99 | sscanf(Cmd, "%c", &hex); | |
100 | if (hex == 'h'){ | |
101 | PrintAndLog("Usage: data printdemodbuffer [x]"); | |
102 | PrintAndLog("Options: "); | |
103 | PrintAndLog(" h This help"); | |
104 | PrintAndLog(" x output in hex (omit for binary output)"); | |
105 | return 0; | |
106 | } | |
107 | if (hex == 'x'){ | |
108 | numBits = binarraytohex(printBuff, (char *)DemodBuffer, numBits); | |
109 | if (numBits==0) return 0; | |
110 | PrintAndLog("DemodBuffer: %s",printBuff); | |
111 | } else { | |
112 | printDemodBuff(); | |
113 | } | |
114 | return 1; | |
115 | } | |
116 | int CmdAmp(const char *Cmd) | |
117 | { | |
118 | int i, rising, falling; | |
119 | int max = INT_MIN, min = INT_MAX; | |
120 | ||
121 | for (i = 10; i < GraphTraceLen; ++i) { | |
122 | if (GraphBuffer[i] > max) | |
123 | max = GraphBuffer[i]; | |
124 | if (GraphBuffer[i] < min) | |
125 | min = GraphBuffer[i]; | |
126 | } | |
127 | ||
128 | if (max != min) { | |
129 | rising = falling= 0; | |
130 | for (i = 0; i < GraphTraceLen; ++i) { | |
131 | if (GraphBuffer[i + 1] < GraphBuffer[i]) { | |
132 | if (rising) { | |
133 | GraphBuffer[i] = max; | |
134 | rising = 0; | |
135 | } | |
136 | falling = 1; | |
137 | } | |
138 | if (GraphBuffer[i + 1] > GraphBuffer[i]) { | |
139 | if (falling) { | |
140 | GraphBuffer[i] = min; | |
141 | falling = 0; | |
142 | } | |
143 | rising= 1; | |
144 | } | |
145 | } | |
146 | } | |
147 | RepaintGraphWindow(); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Generic command to demodulate ASK. | |
153 | * | |
154 | * Argument is convention: positive or negative (High mod means zero | |
155 | * or high mod means one) | |
156 | * | |
157 | * Updates the Graph trace with 0/1 values | |
158 | * | |
159 | * Arguments: | |
160 | * c : 0 or 1 (or invert) | |
161 | */ | |
162 | //this method ignores the clock | |
163 | ||
164 | //this function strictly converts highs and lows to 1s and 0s for each sample in the graphbuffer | |
165 | int Cmdaskdemod(const char *Cmd) | |
166 | { | |
167 | int i; | |
168 | int c, high = 0, low = 0; | |
169 | ||
170 | sscanf(Cmd, "%i", &c); | |
171 | ||
172 | /* Detect high and lows */ | |
173 | for (i = 0; i < GraphTraceLen; ++i) | |
174 | { | |
175 | if (GraphBuffer[i] > high) | |
176 | high = GraphBuffer[i]; | |
177 | else if (GraphBuffer[i] < low) | |
178 | low = GraphBuffer[i]; | |
179 | } | |
180 | high=abs(high*.75); | |
181 | low=abs(low*.75); | |
182 | if (c != 0 && c != 1) { | |
183 | PrintAndLog("Invalid argument: %s", Cmd); | |
184 | return 0; | |
185 | } | |
186 | //prime loop | |
187 | if (GraphBuffer[0] > 0) { | |
188 | GraphBuffer[0] = 1-c; | |
189 | } else { | |
190 | GraphBuffer[0] = c; | |
191 | } | |
192 | for (i = 1; i < GraphTraceLen; ++i) { | |
193 | /* Transitions are detected at each peak | |
194 | * Transitions are either: | |
195 | * - we're low: transition if we hit a high | |
196 | * - we're high: transition if we hit a low | |
197 | * (we need to do it this way because some tags keep high or | |
198 | * low for long periods, others just reach the peak and go | |
199 | * down) | |
200 | */ | |
201 | //[marhsmellow] change == to >= for high and <= for low for fuzz | |
202 | if ((GraphBuffer[i] >= high) && (GraphBuffer[i - 1] == c)) { | |
203 | GraphBuffer[i] = 1 - c; | |
204 | } else if ((GraphBuffer[i] <= low) && (GraphBuffer[i - 1] == (1 - c))){ | |
205 | GraphBuffer[i] = c; | |
206 | } else { | |
207 | /* No transition */ | |
208 | GraphBuffer[i] = GraphBuffer[i - 1]; | |
209 | } | |
210 | } | |
211 | RepaintGraphWindow(); | |
212 | return 0; | |
213 | } | |
214 | ||
215 | //this function strictly converts >1 to 1 and <1 to 0 for each sample in the graphbuffer | |
216 | int CmdGetBitStream(const char *Cmd) | |
217 | { | |
218 | int i; | |
219 | CmdHpf(Cmd); | |
220 | for (i = 0; i < GraphTraceLen; i++) { | |
221 | if (GraphBuffer[i] >= 1) { | |
222 | GraphBuffer[i] = 1; | |
223 | } else { | |
224 | GraphBuffer[i] = 0; | |
225 | } | |
226 | } | |
227 | RepaintGraphWindow(); | |
228 | return 0; | |
229 | } | |
230 | ||
231 | ||
232 | //by marshmellow | |
233 | void printBitStream(uint8_t BitStream[], uint32_t bitLen) | |
234 | { | |
235 | uint32_t i = 0; | |
236 | if (bitLen<16) { | |
237 | PrintAndLog("Too few bits found: %d",bitLen); | |
238 | return; | |
239 | } | |
240 | if (bitLen>512) bitLen=512; | |
241 | ||
242 | // ensure equally divided by 16 | |
243 | bitLen &= 0xfff0; | |
244 | ||
245 | ||
246 | for (i = 0; i <= (bitLen-16); i+=16) { | |
247 | PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i", | |
248 | BitStream[i], | |
249 | BitStream[i+1], | |
250 | BitStream[i+2], | |
251 | BitStream[i+3], | |
252 | BitStream[i+4], | |
253 | BitStream[i+5], | |
254 | BitStream[i+6], | |
255 | BitStream[i+7], | |
256 | BitStream[i+8], | |
257 | BitStream[i+9], | |
258 | BitStream[i+10], | |
259 | BitStream[i+11], | |
260 | BitStream[i+12], | |
261 | BitStream[i+13], | |
262 | BitStream[i+14], | |
263 | BitStream[i+15]); | |
264 | } | |
265 | return; | |
266 | } | |
267 | //by marshmellow | |
268 | //print 64 bit EM410x ID in multiple formats | |
269 | void printEM410x(uint32_t hi, uint64_t id) | |
270 | { | |
271 | if (id || hi){ | |
272 | uint64_t iii=1; | |
273 | uint64_t id2lo=0; | |
274 | uint32_t ii=0; | |
275 | uint32_t i=0; | |
276 | for (ii=5; ii>0;ii--){ | |
277 | for (i=0;i<8;i++){ | |
278 | id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8))); | |
279 | } | |
280 | } | |
281 | if (hi){ | |
282 | //output 88 bit em id | |
283 | PrintAndLog("EM TAG ID : %06x%016llx", hi, id); | |
284 | } else{ | |
285 | //output 40 bit em id | |
286 | PrintAndLog("EM TAG ID : %010llx", id); | |
287 | PrintAndLog("Unique TAG ID: %010llx", id2lo); | |
288 | PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF); | |
289 | PrintAndLog("DEZ 10 : %010lld",id & 0xFFFFFF); | |
290 | PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF)); | |
291 | PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id>>32ll),(id & 0xFFFF)); | |
292 | PrintAndLog("DEZ 14/IK2 : %014lld",id); | |
293 | PrintAndLog("DEZ 15/IK3 : %015lld",id2lo); | |
294 | PrintAndLog("Other : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF)); | |
295 | } | |
296 | } | |
297 | return; | |
298 | } | |
299 | ||
300 | //by marshmellow | |
301 | //takes 3 arguments - clock, invert and maxErr as integers | |
302 | //attempts to demodulate ask while decoding manchester | |
303 | //prints binary found and saves in graphbuffer for further commands | |
304 | int CmdAskEM410xDemod(const char *Cmd) | |
305 | { | |
306 | char cmdp = param_getchar(Cmd, 0); | |
307 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
308 | PrintAndLog("Usage: data askem410xdemod [clock] <0|1> [maxError]"); | |
309 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); | |
310 | PrintAndLog(" <invert>, 1 for invert output"); | |
311 | PrintAndLog(" [set maximum allowed errors], default = 100."); | |
312 | PrintAndLog(""); | |
313 | PrintAndLog(" sample: data askem410xdemod = demod an EM410x Tag ID from GraphBuffer"); | |
314 | PrintAndLog(" : data askem410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32"); | |
315 | PrintAndLog(" : data askem410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data"); | |
316 | PrintAndLog(" : data askem410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data"); | |
317 | PrintAndLog(" : data askem410xdemod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors"); | |
318 | return 0; | |
319 | } | |
320 | int ans = ASKmanDemod(Cmd, FALSE, FALSE); | |
321 | if (!ans) return 0; | |
322 | ||
323 | uint64_t lo =0; | |
324 | uint32_t hi =0; | |
325 | size_t idx=0; | |
326 | if (Em410xDecode(DemodBuffer,(size_t *) &DemodBufferLen, &idx, &hi, &lo)){ | |
327 | if (g_debugMode){ | |
328 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, DemodBufferLen); | |
329 | printDemodBuff(); | |
330 | } | |
331 | PrintAndLog("EM410x pattern found: "); | |
332 | printEM410x(hi, lo); | |
333 | return 1; | |
334 | } | |
335 | return 0; | |
336 | } | |
337 | ||
338 | int ASKmanDemod(const char *Cmd, bool verbose, bool emSearch) | |
339 | { | |
340 | int invert=0; | |
341 | int clk=0; | |
342 | int maxErr=100; | |
343 | ||
344 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
345 | sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); | |
346 | if (invert != 0 && invert != 1) { | |
347 | PrintAndLog("Invalid argument: %s", Cmd); | |
348 | return 0; | |
349 | } | |
350 | if (clk==1){ | |
351 | invert=1; | |
352 | clk=0; | |
353 | } | |
354 | size_t BitLen = getFromGraphBuf(BitStream); | |
355 | if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen); | |
356 | if (BitLen==0) return 0; | |
357 | int errCnt=0; | |
358 | errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr); | |
359 | if (errCnt<0||BitLen<16){ //if fatal error (or -1) | |
360 | if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk); | |
361 | return 0; | |
362 | } | |
363 | if (verbose || g_debugMode) PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen); | |
364 | ||
365 | //output | |
366 | if (errCnt>0){ | |
367 | if (verbose || g_debugMode) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
368 | } | |
369 | if (verbose || g_debugMode) PrintAndLog("ASK/Manchester decoded bitstream:"); | |
370 | // Now output the bitstream to the scrollback by line of 16 bits | |
371 | setDemodBuf(BitStream,BitLen,0); | |
372 | if (verbose || g_debugMode) printDemodBuff(); | |
373 | uint64_t lo =0; | |
374 | uint32_t hi =0; | |
375 | size_t idx=0; | |
376 | if (emSearch){ | |
377 | if (Em410xDecode(BitStream, &BitLen, &idx, &hi, &lo)){ | |
378 | //set GraphBuffer for clone or sim command | |
379 | setDemodBuf(BitStream, BitLen, idx); | |
380 | if (g_debugMode){ | |
381 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); | |
382 | printDemodBuff(); | |
383 | } | |
384 | if (verbose) PrintAndLog("EM410x pattern found: "); | |
385 | if (verbose) printEM410x(hi, lo); | |
386 | return 1; | |
387 | } | |
388 | } | |
389 | return 1; | |
390 | } | |
391 | ||
392 | //by marshmellow | |
393 | //takes 3 arguments - clock, invert, maxErr as integers | |
394 | //attempts to demodulate ask while decoding manchester | |
395 | //prints binary found and saves in graphbuffer for further commands | |
396 | int Cmdaskmandemod(const char *Cmd) | |
397 | { | |
398 | char cmdp = param_getchar(Cmd, 0); | |
399 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
400 | PrintAndLog("Usage: data rawdemod am [clock] <0|1> [maxError]"); | |
401 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); | |
402 | PrintAndLog(" <invert>, 1 for invert output"); | |
403 | PrintAndLog(" [set maximum allowed errors], default = 100."); | |
404 | PrintAndLog(""); | |
405 | PrintAndLog(" sample: data rawdemod am = demod an ask/manchester tag from GraphBuffer"); | |
406 | PrintAndLog(" : data rawdemod am 32 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32"); | |
407 | PrintAndLog(" : data rawdemod am 32 1 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32 and inverting data"); | |
408 | PrintAndLog(" : data rawdemod am 1 = demod an ask/manchester tag from GraphBuffer while inverting data"); | |
409 | PrintAndLog(" : data rawdemod am 64 1 0 = demod an ask/manchester tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors"); | |
410 | return 0; | |
411 | } | |
412 | return ASKmanDemod(Cmd, TRUE, TRUE); | |
413 | } | |
414 | ||
415 | //by marshmellow | |
416 | //manchester decode | |
417 | //stricktly take 10 and 01 and convert to 0 and 1 | |
418 | int Cmdmandecoderaw(const char *Cmd) | |
419 | { | |
420 | int i =0; | |
421 | int errCnt=0; | |
422 | size_t size=0; | |
423 | size_t maxErr = 20; | |
424 | char cmdp = param_getchar(Cmd, 0); | |
425 | if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') { | |
426 | PrintAndLog("Usage: data manrawdecode"); | |
427 | PrintAndLog(" Takes 10 and 01 and converts to 0 and 1 respectively"); | |
428 | PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)"); | |
429 | PrintAndLog(""); | |
430 | PrintAndLog(" sample: data manrawdecode = decode manchester bitstream from the demodbuffer"); | |
431 | return 0; | |
432 | } | |
433 | if (DemodBufferLen==0) return 0; | |
434 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
435 | int high=0,low=0; | |
436 | for (;i<DemodBufferLen;++i){ | |
437 | if (DemodBuffer[i]>high) high=DemodBuffer[i]; | |
438 | else if(DemodBuffer[i]<low) low=DemodBuffer[i]; | |
439 | BitStream[i]=DemodBuffer[i]; | |
440 | } | |
441 | if (high>1 || low <0 ){ | |
442 | PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode"); | |
443 | return 0; | |
444 | } | |
445 | size=i; | |
446 | errCnt=manrawdecode(BitStream, &size); | |
447 | if (errCnt>=maxErr){ | |
448 | PrintAndLog("Too many errors: %d",errCnt); | |
449 | return 0; | |
450 | } | |
451 | PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt); | |
452 | printBitStream(BitStream, size); | |
453 | if (errCnt==0){ | |
454 | uint64_t id = 0; | |
455 | uint32_t hi = 0; | |
456 | size_t idx=0; | |
457 | if (Em410xDecode(BitStream, &size, &idx, &hi, &id)){ | |
458 | //need to adjust to set bitstream back to manchester encoded data | |
459 | //setDemodBuf(BitStream, size, idx); | |
460 | ||
461 | printEM410x(hi, id); | |
462 | } | |
463 | } | |
464 | return 1; | |
465 | } | |
466 | ||
467 | //by marshmellow | |
468 | //biphase decode | |
469 | //take 01 or 10 = 0 and 11 or 00 = 1 | |
470 | //takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit | |
471 | // and "invert" default = 0 if 1 it will invert output | |
472 | // since it is not like manchester and doesn't have an incorrect bit pattern we | |
473 | // cannot determine if our decode is correct or if it should be shifted by one bit | |
474 | // the argument offset allows us to manually shift if the output is incorrect | |
475 | // (better would be to demod and decode at the same time so we can distinguish large | |
476 | // width waves vs small width waves to help the decode positioning) or askbiphdemod | |
477 | int CmdBiphaseDecodeRaw(const char *Cmd) | |
478 | { | |
479 | size_t size=0; | |
480 | int offset=0, invert=0, maxErr=20, errCnt=0; | |
481 | char cmdp = param_getchar(Cmd, 0); | |
482 | if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') { | |
483 | PrintAndLog("Usage: data biphaserawdecode [offset] [invert] [maxErr]"); | |
484 | PrintAndLog(" Converts 10 or 01 to 1 and 11 or 00 to 0"); | |
485 | PrintAndLog(" --must have binary sequence in demodbuffer (run data askrawdemod first)"); | |
486 | PrintAndLog(""); | |
487 | PrintAndLog(" [offset <0|1>], set to 0 not to adjust start position or to 1 to adjust decode start position"); | |
488 | PrintAndLog(" [invert <0|1>], set to 1 to invert output"); | |
489 | PrintAndLog(" [maxErr int], set max errors tolerated - default=20"); | |
490 | PrintAndLog(""); | |
491 | PrintAndLog(" sample: data biphaserawdecode = decode biphase bitstream from the demodbuffer"); | |
492 | PrintAndLog(" sample: data biphaserawdecode 1 1 = decode biphase bitstream from the demodbuffer, set offset, and invert output"); | |
493 | return 0; | |
494 | } | |
495 | sscanf(Cmd, "%i %i %i", &offset, &invert, &maxErr); | |
496 | if (DemodBufferLen==0){ | |
497 | PrintAndLog("DemodBuffer Empty - run 'data rawdemod ar' first"); | |
498 | return 0; | |
499 | } | |
500 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
501 | memcpy(BitStream, DemodBuffer, DemodBufferLen); | |
502 | size = DemodBufferLen; | |
503 | errCnt=BiphaseRawDecode(BitStream, &size, offset, invert); | |
504 | if (errCnt<0){ | |
505 | PrintAndLog("Error during decode:%d", errCnt); | |
506 | return 0; | |
507 | } | |
508 | if (errCnt>maxErr){ | |
509 | PrintAndLog("Too many errors attempting to decode: %d",errCnt); | |
510 | return 0; | |
511 | } | |
512 | ||
513 | if (errCnt>0){ | |
514 | PrintAndLog("# Errors found during Demod (shown as 77 in bit stream): %d",errCnt); | |
515 | } | |
516 | PrintAndLog("Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert); | |
517 | printBitStream(BitStream, size); | |
518 | ||
519 | if (offset) setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset); //remove first bit from raw demod | |
520 | return 1; | |
521 | } | |
522 | ||
523 | // set demod buffer back to raw after biphase demod | |
524 | void setBiphasetoRawDemodBuf(uint8_t *BitStream, size_t size) | |
525 | { | |
526 | uint8_t rawStream[512]={0x00}; | |
527 | size_t i=0; | |
528 | uint8_t curPhase=0; | |
529 | if (size > 256) { | |
530 | PrintAndLog("ERROR - Biphase Demod Buffer overrun"); | |
531 | return; | |
532 | } | |
533 | for (size_t idx=0; idx<size; idx++){ | |
534 | if(!BitStream[idx]){ | |
535 | rawStream[i++] = curPhase; | |
536 | rawStream[i++] = curPhase; | |
537 | curPhase ^= 1; | |
538 | } else { | |
539 | rawStream[i++] = curPhase; | |
540 | rawStream[i++] = curPhase ^ 1; | |
541 | } | |
542 | } | |
543 | setDemodBuf(rawStream,i,0); | |
544 | return; | |
545 | } | |
546 | ||
547 | //by marshmellow | |
548 | //takes 4 arguments - clock, invert, maxErr as integers and amplify as char | |
549 | //attempts to demodulate ask only | |
550 | //prints binary found and saves in graphbuffer for further commands | |
551 | int ASKrawDemod(const char *Cmd, bool verbose) | |
552 | { | |
553 | int invert=0; | |
554 | int clk=0; | |
555 | int maxErr=100; | |
556 | uint8_t askAmp = 0; | |
557 | char amp = param_getchar(Cmd, 0); | |
558 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
559 | sscanf(Cmd, "%i %i %i %c", &clk, &invert, &maxErr, &); | |
560 | if (invert != 0 && invert != 1) { | |
561 | if (verbose || g_debugMode) PrintAndLog("Invalid argument: %s", Cmd); | |
562 | return 0; | |
563 | } | |
564 | if (clk==1){ | |
565 | invert=1; | |
566 | clk=0; | |
567 | } | |
568 | if (amp == 'a' || amp == 'A') askAmp=1; | |
569 | size_t BitLen = getFromGraphBuf(BitStream); | |
570 | if (BitLen==0) return 0; | |
571 | int errCnt=0; | |
572 | errCnt = askrawdemod(BitStream, &BitLen, &clk, &invert, maxErr, askAmp); | |
573 | if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) | |
574 | if (verbose || g_debugMode) PrintAndLog("no data found"); | |
575 | if (g_debugMode) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert); | |
576 | return 0; | |
577 | } | |
578 | if (verbose || g_debugMode) PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen); | |
579 | ||
580 | //move BitStream back to DemodBuffer | |
581 | setDemodBuf(BitStream,BitLen,0); | |
582 | ||
583 | //output | |
584 | if (errCnt>0 && (verbose || g_debugMode)){ | |
585 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d", errCnt); | |
586 | } | |
587 | if (verbose || g_debugMode){ | |
588 | PrintAndLog("ASK demoded bitstream:"); | |
589 | // Now output the bitstream to the scrollback by line of 16 bits | |
590 | printBitStream(BitStream,BitLen); | |
591 | } | |
592 | return 1; | |
593 | } | |
594 | ||
595 | //by marshmellow | |
596 | // - ASK Demod then Biphase decode GraphBuffer samples | |
597 | int ASKbiphaseDemod(const char *Cmd, bool verbose) | |
598 | { | |
599 | //ask raw demod GraphBuffer first | |
600 | int offset=0, clk=0, invert=0, maxErr=0, ans=0; | |
601 | ans = sscanf(Cmd, "%i %i %i %i", &offset, &clk, &invert, &maxErr); | |
602 | if (ans>0) | |
603 | ans = ASKrawDemod(Cmd+2, FALSE); | |
604 | else | |
605 | ans = ASKrawDemod(Cmd, FALSE); | |
606 | if (!ans) { | |
607 | if (g_debugMode || verbose) PrintAndLog("Error AskrawDemod: %d", ans); | |
608 | return 0; | |
609 | } | |
610 | ||
611 | //attempt to Biphase decode DemodBuffer | |
612 | size_t size = DemodBufferLen; | |
613 | uint8_t BitStream[MAX_DEMOD_BUF_LEN]; | |
614 | memcpy(BitStream, DemodBuffer, DemodBufferLen); | |
615 | ||
616 | int errCnt = BiphaseRawDecode(BitStream, &size, offset, invert); | |
617 | if (errCnt < 0){ | |
618 | if (g_debugMode || verbose) PrintAndLog("Error BiphaseRawDecode: %d", errCnt); | |
619 | return 0; | |
620 | } | |
621 | if (errCnt > maxErr) { | |
622 | if (g_debugMode || verbose) PrintAndLog("Error BiphaseRawDecode too many errors: %d", errCnt); | |
623 | return 0; | |
624 | } | |
625 | //success set DemodBuffer and return | |
626 | setDemodBuf(BitStream, size, 0); | |
627 | if (g_debugMode || verbose){ | |
628 | PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt); | |
629 | printDemodBuff(); | |
630 | } | |
631 | return 1; | |
632 | } | |
633 | //by marshmellow - see ASKbiphaseDemod | |
634 | int Cmdaskbiphdemod(const char *Cmd) | |
635 | { | |
636 | char cmdp = param_getchar(Cmd, 0); | |
637 | if (strlen(Cmd) > 12 || cmdp == 'h' || cmdp == 'H') { | |
638 | PrintAndLog("Usage: data rawdemod ab [offset] [clock] <invert> [maxError] <amplify>"); | |
639 | PrintAndLog(" [offset], offset to begin biphase, default=0"); | |
640 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect"); | |
641 | PrintAndLog(" <invert>, 1 to invert output"); | |
642 | PrintAndLog(" [set maximum allowed errors], default = 100"); | |
643 | PrintAndLog(" <amplify>, 'a' to attempt demod with ask amplification, default = no amp"); | |
644 | PrintAndLog(" NOTE: <invert> can be entered as second or third argument"); | |
645 | PrintAndLog(" NOTE: <amplify> can be entered as first, second or last argument"); | |
646 | PrintAndLog(" NOTE: any other arg must have previous args set to work"); | |
647 | PrintAndLog(""); | |
648 | PrintAndLog(" sample: data rawdemod ab = demod an ask/biph tag from GraphBuffer"); | |
649 | PrintAndLog(" : data rawdemod ab a = demod an ask/biph tag from GraphBuffer, amplified"); | |
650 | PrintAndLog(" : data rawdemod ab 1 32 = demod an ask/biph tag from GraphBuffer using an offset of 1 and a clock of RF/32"); | |
651 | PrintAndLog(" : data rawdemod ab 0 32 1 = demod an ask/biph tag from GraphBuffer using a clock of RF/32 and inverting data"); | |
652 | PrintAndLog(" : data rawdemod ab 0 1 = demod an ask/biph tag from GraphBuffer while inverting data"); | |
653 | PrintAndLog(" : data rawdemod ab 0 64 1 0 = demod an ask/biph tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors"); | |
654 | PrintAndLog(" : data rawdemod ab 0 64 1 0 a = demod an ask/biph tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors, and amp"); | |
655 | return 0; | |
656 | } | |
657 | return ASKbiphaseDemod(Cmd, TRUE); | |
658 | } | |
659 | ||
660 | //by marshmellow | |
661 | //attempts to demodulate and identify a G_Prox_II verex/chubb card | |
662 | //WARNING: if it fails during some points it will destroy the DemodBuffer data | |
663 | // but will leave the GraphBuffer intact. | |
664 | //if successful it will push askraw data back to demod buffer ready for emulation | |
665 | int CmdG_Prox_II_Demod(const char *Cmd) | |
666 | { | |
667 | if (!ASKbiphaseDemod(Cmd, FALSE)){ | |
668 | if (g_debugMode) PrintAndLog("ASKbiphaseDemod failed 1st try"); | |
669 | return 0; | |
670 | } | |
671 | size_t size = DemodBufferLen; | |
672 | //call lfdemod.c demod for gProxII | |
673 | int ans = gProxII_Demod(DemodBuffer, &size); | |
674 | if (ans < 0){ | |
675 | if (g_debugMode) PrintAndLog("Error gProxII_Demod"); | |
676 | return 0; | |
677 | } | |
678 | //got a good demod | |
679 | uint32_t ByteStream[65] = {0x00}; | |
680 | uint8_t xorKey=0; | |
681 | uint8_t keyCnt=0; | |
682 | uint8_t bitCnt=0; | |
683 | uint8_t ByteCnt=0; | |
684 | size_t startIdx = ans + 6; //start after preamble | |
685 | for (size_t idx = 0; idx<size-6; idx++){ | |
686 | if ((idx+1) % 5 == 0){ | |
687 | //spacer bit - should be 0 | |
688 | if (DemodBuffer[startIdx+idx] != 0) { | |
689 | if (g_debugMode) PrintAndLog("Error spacer not 0: %d, pos: %d",DemodBuffer[startIdx+idx],startIdx+idx); | |
690 | return 0; | |
691 | } | |
692 | continue; | |
693 | } | |
694 | if (keyCnt<8){ //lsb first | |
695 | xorKey = xorKey | (DemodBuffer[startIdx+idx]<<keyCnt); | |
696 | keyCnt++; | |
697 | if (keyCnt==8 && g_debugMode) PrintAndLog("xorKey Found: %02x", xorKey); | |
698 | continue; | |
699 | } | |
700 | //lsb first | |
701 | ByteStream[ByteCnt] = ByteStream[ByteCnt] | (DemodBuffer[startIdx+idx]<<bitCnt); | |
702 | bitCnt++; | |
703 | if (bitCnt % 8 == 0){ | |
704 | if (g_debugMode) PrintAndLog("byte %d: %02x",ByteCnt,ByteStream[ByteCnt]); | |
705 | bitCnt=0; | |
706 | ByteCnt++; | |
707 | } | |
708 | } | |
709 | for (uint8_t i = 0; i < ByteCnt; i++){ | |
710 | ByteStream[i] ^= xorKey; //xor | |
711 | if (g_debugMode) PrintAndLog("byte %d after xor: %02x", i, ByteStream[i]); | |
712 | } | |
713 | //now ByteStream contains 64 bytes of decrypted raw tag data | |
714 | // | |
715 | uint8_t fmtLen = ByteStream[0]>>2; | |
716 | uint32_t FC = 0; | |
717 | uint32_t Card = 0; | |
718 | uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans,32); | |
719 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32); | |
720 | uint32_t raw3 = bytebits_to_byte(DemodBuffer+ans+64, 32); | |
721 | ||
722 | if (fmtLen==36){ | |
723 | FC = ((ByteStream[3] & 0x7F)<<7) | (ByteStream[4]>>1); | |
724 | Card = ((ByteStream[4]&1)<<19) | (ByteStream[5]<<11) | (ByteStream[6]<<3) | (ByteStream[7]>>5); | |
725 | PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen,FC,Card); | |
726 | } else if(fmtLen==26){ | |
727 | FC = ((ByteStream[3] & 0x7F)<<1) | (ByteStream[4]>>7); | |
728 | Card = ((ByteStream[4]&0x7F)<<9) | (ByteStream[5]<<1) | (ByteStream[6]>>7); | |
729 | PrintAndLog("G-Prox-II Found: FmtLen %d, FC %d, Card %d",fmtLen,FC,Card); | |
730 | } else { | |
731 | PrintAndLog("Unknown G-Prox-II Fmt Found: FmtLen %d",fmtLen); | |
732 | } | |
733 | PrintAndLog("Raw: %08x%08x%08x", raw1,raw2,raw3); | |
734 | setDemodBuf(DemodBuffer+ans, 96, 0); | |
735 | return 1; | |
736 | } | |
737 | ||
738 | //by marshmellow - see ASKrawDemod | |
739 | int Cmdaskrawdemod(const char *Cmd) | |
740 | { | |
741 | char cmdp = param_getchar(Cmd, 0); | |
742 | if (strlen(Cmd) > 12 || cmdp == 'h' || cmdp == 'H') { | |
743 | PrintAndLog("Usage: data rawdemod ar [clock] <invert> [maxError] [amplify]"); | |
744 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect"); | |
745 | PrintAndLog(" <invert>, 1 to invert output"); | |
746 | PrintAndLog(" [set maximum allowed errors], default = 100"); | |
747 | PrintAndLog(" <amplify>, 'a' to attempt demod with ask amplification, default = no amp"); | |
748 | PrintAndLog(""); | |
749 | PrintAndLog(" sample: data rawdemod ar = demod an ask tag from GraphBuffer"); | |
750 | PrintAndLog(" : data rawdemod ar a = demod an ask tag from GraphBuffer, amplified"); | |
751 | PrintAndLog(" : data rawdemod ar 32 = demod an ask tag from GraphBuffer using a clock of RF/32"); | |
752 | PrintAndLog(" : data rawdemod ar 32 1 = demod an ask tag from GraphBuffer using a clock of RF/32 and inverting data"); | |
753 | PrintAndLog(" : data rawdemod ar 1 = demod an ask tag from GraphBuffer while inverting data"); | |
754 | PrintAndLog(" : data rawdemod ar 64 1 0 = demod an ask tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors"); | |
755 | PrintAndLog(" : data rawdemod ar 64 1 0 a = demod an ask tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors, and amp"); | |
756 | return 0; | |
757 | } | |
758 | return ASKrawDemod(Cmd, TRUE); | |
759 | } | |
760 | ||
761 | int AutoCorrelate(int window, bool SaveGrph, bool verbose) | |
762 | { | |
763 | static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; | |
764 | size_t Correlation = 0; | |
765 | int maxSum = 0; | |
766 | int lastMax = 0; | |
767 | if (verbose) PrintAndLog("performing %d correlations", GraphTraceLen - window); | |
768 | for (int i = 0; i < GraphTraceLen - window; ++i) { | |
769 | int sum = 0; | |
770 | for (int j = 0; j < window; ++j) { | |
771 | sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256; | |
772 | } | |
773 | CorrelBuffer[i] = sum; | |
774 | if (sum >= maxSum-100 && sum <= maxSum+100){ | |
775 | //another max | |
776 | Correlation = i-lastMax; | |
777 | lastMax = i; | |
778 | if (sum > maxSum) maxSum = sum; | |
779 | } else if (sum > maxSum){ | |
780 | maxSum=sum; | |
781 | lastMax = i; | |
782 | } | |
783 | } | |
784 | if (Correlation==0){ | |
785 | //try again with wider margin | |
786 | for (int i = 0; i < GraphTraceLen - window; i++){ | |
787 | if (CorrelBuffer[i] >= maxSum-(maxSum*0.05) && CorrelBuffer[i] <= maxSum+(maxSum*0.05)){ | |
788 | //another max | |
789 | Correlation = i-lastMax; | |
790 | lastMax = i; | |
791 | //if (CorrelBuffer[i] > maxSum) maxSum = sum; | |
792 | } | |
793 | } | |
794 | } | |
795 | if (verbose && Correlation > 0) PrintAndLog("Possible Correlation: %d samples",Correlation); | |
796 | ||
797 | if (SaveGrph){ | |
798 | GraphTraceLen = GraphTraceLen - window; | |
799 | memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int)); | |
800 | RepaintGraphWindow(); | |
801 | } | |
802 | return Correlation; | |
803 | } | |
804 | ||
805 | int usage_data_autocorr(void) | |
806 | { | |
807 | //print help | |
808 | PrintAndLog("Usage: data autocorr [window] [g]"); | |
809 | PrintAndLog("Options: "); | |
810 | PrintAndLog(" h This help"); | |
811 | PrintAndLog(" [window] window length for correlation - default = 4000"); | |
812 | PrintAndLog(" g save back to GraphBuffer (overwrite)"); | |
813 | return 0; | |
814 | } | |
815 | ||
816 | int CmdAutoCorr(const char *Cmd) | |
817 | { | |
818 | char cmdp = param_getchar(Cmd, 0); | |
819 | if (cmdp == 'h' || cmdp == 'H') | |
820 | return usage_data_autocorr(); | |
821 | int window = 4000; //set default | |
822 | char grph=0; | |
823 | bool updateGrph = FALSE; | |
824 | sscanf(Cmd, "%i %c", &window, &grph); | |
825 | ||
826 | if (window >= GraphTraceLen) { | |
827 | PrintAndLog("window must be smaller than trace (%d samples)", | |
828 | GraphTraceLen); | |
829 | return 0; | |
830 | } | |
831 | if (grph == 'g') updateGrph=TRUE; | |
832 | return AutoCorrelate(window, updateGrph, TRUE); | |
833 | } | |
834 | ||
835 | int CmdBitsamples(const char *Cmd) | |
836 | { | |
837 | int cnt = 0; | |
838 | uint8_t got[12288]; | |
839 | ||
840 | GetFromBigBuf(got,sizeof(got),0); | |
841 | WaitForResponse(CMD_ACK,NULL); | |
842 | ||
843 | for (int j = 0; j < sizeof(got); j++) { | |
844 | for (int k = 0; k < 8; k++) { | |
845 | if(got[j] & (1 << (7 - k))) { | |
846 | GraphBuffer[cnt++] = 1; | |
847 | } else { | |
848 | GraphBuffer[cnt++] = 0; | |
849 | } | |
850 | } | |
851 | } | |
852 | GraphTraceLen = cnt; | |
853 | RepaintGraphWindow(); | |
854 | return 0; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Convert to a bitstream | |
859 | */ | |
860 | int CmdBitstream(const char *Cmd) | |
861 | { | |
862 | int i, j; | |
863 | int bit; | |
864 | int gtl; | |
865 | int clock; | |
866 | int low = 0; | |
867 | int high = 0; | |
868 | int hithigh, hitlow, first; | |
869 | ||
870 | /* Detect high and lows and clock */ | |
871 | for (i = 0; i < GraphTraceLen; ++i) | |
872 | { | |
873 | if (GraphBuffer[i] > high) | |
874 | high = GraphBuffer[i]; | |
875 | else if (GraphBuffer[i] < low) | |
876 | low = GraphBuffer[i]; | |
877 | } | |
878 | ||
879 | /* Get our clock */ | |
880 | clock = GetAskClock(Cmd, high, 1); | |
881 | gtl = ClearGraph(0); | |
882 | ||
883 | bit = 0; | |
884 | for (i = 0; i < (int)(gtl / clock); ++i) | |
885 | { | |
886 | hithigh = 0; | |
887 | hitlow = 0; | |
888 | first = 1; | |
889 | /* Find out if we hit both high and low peaks */ | |
890 | for (j = 0; j < clock; ++j) | |
891 | { | |
892 | if (GraphBuffer[(i * clock) + j] == high) | |
893 | hithigh = 1; | |
894 | else if (GraphBuffer[(i * clock) + j] == low) | |
895 | hitlow = 1; | |
896 | /* it doesn't count if it's the first part of our read | |
897 | because it's really just trailing from the last sequence */ | |
898 | if (first && (hithigh || hitlow)) | |
899 | hithigh = hitlow = 0; | |
900 | else | |
901 | first = 0; | |
902 | ||
903 | if (hithigh && hitlow) | |
904 | break; | |
905 | } | |
906 | ||
907 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
908 | if (!hithigh || !hitlow) | |
909 | bit ^= 1; | |
910 | ||
911 | AppendGraph(0, clock, bit); | |
912 | } | |
913 | ||
914 | RepaintGraphWindow(); | |
915 | return 0; | |
916 | } | |
917 | ||
918 | int CmdBuffClear(const char *Cmd) | |
919 | { | |
920 | UsbCommand c = {CMD_BUFF_CLEAR}; | |
921 | SendCommand(&c); | |
922 | ClearGraph(true); | |
923 | return 0; | |
924 | } | |
925 | ||
926 | int CmdDec(const char *Cmd) | |
927 | { | |
928 | for (int i = 0; i < (GraphTraceLen / 2); ++i) | |
929 | GraphBuffer[i] = GraphBuffer[i * 2]; | |
930 | GraphTraceLen /= 2; | |
931 | PrintAndLog("decimated by 2"); | |
932 | RepaintGraphWindow(); | |
933 | return 0; | |
934 | } | |
935 | /** | |
936 | * Undecimate - I'd call it 'interpolate', but we'll save that | |
937 | * name until someone does an actual interpolation command, not just | |
938 | * blindly repeating samples | |
939 | * @param Cmd | |
940 | * @return | |
941 | */ | |
942 | int CmdUndec(const char *Cmd) | |
943 | { | |
944 | if(param_getchar(Cmd, 0) == 'h') | |
945 | { | |
946 | PrintAndLog("Usage: data undec [factor]"); | |
947 | PrintAndLog("This function performs un-decimation, by repeating each sample N times"); | |
948 | PrintAndLog("Options: "); | |
949 | PrintAndLog(" h This help"); | |
950 | PrintAndLog(" factor The number of times to repeat each sample.[default:2]"); | |
951 | PrintAndLog("Example: 'data undec 3'"); | |
952 | return 0; | |
953 | } | |
954 | ||
955 | uint8_t factor = param_get8ex(Cmd, 0,2, 10); | |
956 | //We have memory, don't we? | |
957 | int swap[MAX_GRAPH_TRACE_LEN] = { 0 }; | |
958 | uint32_t g_index = 0 ,s_index = 0; | |
959 | while(g_index < GraphTraceLen && s_index < MAX_GRAPH_TRACE_LEN) | |
960 | { | |
961 | int count = 0; | |
962 | for(count = 0; count < factor && s_index+count < MAX_GRAPH_TRACE_LEN; count ++) | |
963 | swap[s_index+count] = GraphBuffer[g_index]; | |
964 | s_index+=count; | |
965 | } | |
966 | ||
967 | memcpy(GraphBuffer,swap, s_index * sizeof(int)); | |
968 | GraphTraceLen = s_index; | |
969 | RepaintGraphWindow(); | |
970 | return 0; | |
971 | } | |
972 | ||
973 | //by marshmellow | |
974 | //shift graph zero up or down based on input + or - | |
975 | int CmdGraphShiftZero(const char *Cmd) | |
976 | { | |
977 | ||
978 | int shift=0; | |
979 | //set options from parameters entered with the command | |
980 | sscanf(Cmd, "%i", &shift); | |
981 | int shiftedVal=0; | |
982 | for(int i = 0; i<GraphTraceLen; i++){ | |
983 | shiftedVal=GraphBuffer[i]+shift; | |
984 | if (shiftedVal>127) | |
985 | shiftedVal=127; | |
986 | else if (shiftedVal<-127) | |
987 | shiftedVal=-127; | |
988 | GraphBuffer[i]= shiftedVal; | |
989 | } | |
990 | CmdNorm(""); | |
991 | return 0; | |
992 | } | |
993 | ||
994 | //by marshmellow | |
995 | //use large jumps in read samples to identify edges of waves and then amplify that wave to max | |
996 | //similar to dirtheshold, threshold, and askdemod commands | |
997 | //takes a threshold length which is the measured length between two samples then determines an edge | |
998 | int CmdAskEdgeDetect(const char *Cmd) | |
999 | { | |
1000 | int thresLen = 25; | |
1001 | sscanf(Cmd, "%i", &thresLen); | |
1002 | int shift = 127; | |
1003 | int shiftedVal=0; | |
1004 | for(int i = 1; i<GraphTraceLen; i++){ | |
1005 | if (GraphBuffer[i]-GraphBuffer[i-1]>=thresLen) //large jump up | |
1006 | shift=127; | |
1007 | else if(GraphBuffer[i]-GraphBuffer[i-1]<=-1*thresLen) //large jump down | |
1008 | shift=-127; | |
1009 | ||
1010 | shiftedVal=GraphBuffer[i]+shift; | |
1011 | ||
1012 | if (shiftedVal>127) | |
1013 | shiftedVal=127; | |
1014 | else if (shiftedVal<-127) | |
1015 | shiftedVal=-127; | |
1016 | GraphBuffer[i-1] = shiftedVal; | |
1017 | } | |
1018 | RepaintGraphWindow(); | |
1019 | //CmdNorm(""); | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | /* Print our clock rate */ | |
1024 | // uses data from graphbuffer | |
1025 | // adjusted to take char parameter for type of modulation to find the clock - by marshmellow. | |
1026 | int CmdDetectClockRate(const char *Cmd) | |
1027 | { | |
1028 | char cmdp = param_getchar(Cmd, 0); | |
1029 | if (strlen(Cmd) > 3 || strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
1030 | PrintAndLog("Usage: data detectclock [modulation]"); | |
1031 | PrintAndLog(" [modulation as char], specify the modulation type you want to detect the clock of"); | |
1032 | PrintAndLog(" 'a' = ask, 'f' = fsk, 'n' = nrz/direct, 'p' = psk"); | |
1033 | PrintAndLog(""); | |
1034 | PrintAndLog(" sample: data detectclock a = detect the clock of an ask modulated wave in the GraphBuffer"); | |
1035 | PrintAndLog(" data detectclock f = detect the clock of an fsk modulated wave in the GraphBuffer"); | |
1036 | PrintAndLog(" data detectclock p = detect the clock of an psk modulated wave in the GraphBuffer"); | |
1037 | PrintAndLog(" data detectclock n = detect the clock of an nrz/direct modulated wave in the GraphBuffer"); | |
1038 | } | |
1039 | int ans=0; | |
1040 | if (cmdp == 'a'){ | |
1041 | ans = GetAskClock("", true, false); | |
1042 | } else if (cmdp == 'f'){ | |
1043 | ans = GetFskClock("", true, false); | |
1044 | } else if (cmdp == 'n'){ | |
1045 | ans = GetNrzClock("", true, false); | |
1046 | } else if (cmdp == 'p'){ | |
1047 | ans = GetPskClock("", true, false); | |
1048 | } else { | |
1049 | PrintAndLog ("Please specify a valid modulation to detect the clock of - see option h for help"); | |
1050 | } | |
1051 | return ans; | |
1052 | } | |
1053 | ||
1054 | //by marshmellow | |
1055 | //fsk raw demod and print binary | |
1056 | //takes 4 arguments - Clock, invert, fchigh, fclow | |
1057 | //defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a)) | |
1058 | int FSKrawDemod(const char *Cmd, bool verbose) | |
1059 | { | |
1060 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1061 | //set defaults | |
1062 | int rfLen = 0; | |
1063 | int invert = 0; | |
1064 | int fchigh = 0; | |
1065 | int fclow = 0; | |
1066 | ||
1067 | //set options from parameters entered with the command | |
1068 | sscanf(Cmd, "%i %i %i %i", &rfLen, &invert, &fchigh, &fclow); | |
1069 | ||
1070 | if (strlen(Cmd)>0 && strlen(Cmd)<=2) { | |
1071 | if (rfLen==1){ | |
1072 | invert=1; //if invert option only is used | |
1073 | rfLen = 0; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1078 | size_t BitLen = getFromGraphBuf(BitStream); | |
1079 | if (BitLen==0) return 0; | |
1080 | //get field clock lengths | |
1081 | uint16_t fcs=0; | |
1082 | uint8_t dummy=0; | |
1083 | if (fchigh==0 || fclow == 0){ | |
1084 | fcs = countFC(BitStream, BitLen, &dummy); | |
1085 | if (fcs==0){ | |
1086 | fchigh=10; | |
1087 | fclow=8; | |
1088 | }else{ | |
1089 | fchigh = (fcs >> 8) & 0xFF; | |
1090 | fclow = fcs & 0xFF; | |
1091 | } | |
1092 | } | |
1093 | //get bit clock length | |
1094 | if (rfLen==0){ | |
1095 | rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow); | |
1096 | if (rfLen == 0) rfLen = 50; | |
1097 | } | |
1098 | if (verbose) PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow); | |
1099 | int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow); | |
1100 | if (size>0){ | |
1101 | setDemodBuf(BitStream,size,0); | |
1102 | ||
1103 | // Now output the bitstream to the scrollback by line of 16 bits | |
1104 | if(size > (8*32)+2) size = (8*32)+2; //only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size | |
1105 | if (verbose) { | |
1106 | PrintAndLog("FSK decoded bitstream:"); | |
1107 | printBitStream(BitStream,size); | |
1108 | } | |
1109 | ||
1110 | return 1; | |
1111 | } else{ | |
1112 | if (verbose) PrintAndLog("no FSK data found"); | |
1113 | } | |
1114 | return 0; | |
1115 | } | |
1116 | ||
1117 | //by marshmellow | |
1118 | //fsk raw demod and print binary | |
1119 | //takes 4 arguments - Clock, invert, fchigh, fclow | |
1120 | //defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a)) | |
1121 | int CmdFSKrawdemod(const char *Cmd) | |
1122 | { | |
1123 | char cmdp = param_getchar(Cmd, 0); | |
1124 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
1125 | PrintAndLog("Usage: data rawdemod fs [clock] <invert> [fchigh] [fclow]"); | |
1126 | PrintAndLog(" [set clock as integer] optional, omit for autodetect."); | |
1127 | PrintAndLog(" <invert>, 1 for invert output, can be used even if the clock is omitted"); | |
1128 | PrintAndLog(" [fchigh], larger field clock length, omit for autodetect"); | |
1129 | PrintAndLog(" [fclow], small field clock length, omit for autodetect"); | |
1130 | PrintAndLog(""); | |
1131 | PrintAndLog(" sample: data rawdemod fs = demod an fsk tag from GraphBuffer using autodetect"); | |
1132 | PrintAndLog(" : data rawdemod fs 32 = demod an fsk tag from GraphBuffer using a clock of RF/32, autodetect fc"); | |
1133 | PrintAndLog(" : data rawdemod fs 1 = demod an fsk tag from GraphBuffer using autodetect, invert output"); | |
1134 | PrintAndLog(" : data rawdemod fs 32 1 = demod an fsk tag from GraphBuffer using a clock of RF/32, invert output, autodetect fc"); | |
1135 | PrintAndLog(" : data rawdemod fs 64 0 8 5 = demod an fsk1 RF/64 tag from GraphBuffer"); | |
1136 | PrintAndLog(" : data rawdemod fs 50 0 10 8 = demod an fsk2 RF/50 tag from GraphBuffer"); | |
1137 | PrintAndLog(" : data rawdemod fs 50 1 10 8 = demod an fsk2a RF/50 tag from GraphBuffer"); | |
1138 | return 0; | |
1139 | } | |
1140 | return FSKrawDemod(Cmd, TRUE); | |
1141 | } | |
1142 | ||
1143 | //by marshmellow (based on existing demod + holiman's refactor) | |
1144 | //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded) | |
1145 | //print full HID Prox ID and some bit format details if found | |
1146 | int CmdFSKdemodHID(const char *Cmd) | |
1147 | { | |
1148 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1149 | uint32_t hi2=0, hi=0, lo=0; | |
1150 | ||
1151 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1152 | size_t BitLen = getFromGraphBuf(BitStream); | |
1153 | if (BitLen==0) return 0; | |
1154 | //get binary from fsk wave | |
1155 | int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo); | |
1156 | if (idx<0){ | |
1157 | if (g_debugMode){ | |
1158 | if (idx==-1){ | |
1159 | PrintAndLog("DEBUG: Just Noise Detected"); | |
1160 | } else if (idx == -2) { | |
1161 | PrintAndLog("DEBUG: Error demoding fsk"); | |
1162 | } else if (idx == -3) { | |
1163 | PrintAndLog("DEBUG: Preamble not found"); | |
1164 | } else if (idx == -4) { | |
1165 | PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen); | |
1166 | } else { | |
1167 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
1168 | } | |
1169 | } | |
1170 | return 0; | |
1171 | } | |
1172 | if (hi2==0 && hi==0 && lo==0) { | |
1173 | if (g_debugMode) PrintAndLog("DEBUG: Error - no values found"); | |
1174 | return 0; | |
1175 | } | |
1176 | if (hi2 != 0){ //extra large HID tags | |
1177 | PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)", | |
1178 | (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF); | |
1179 | } | |
1180 | else { //standard HID tags <38 bits | |
1181 | uint8_t fmtLen = 0; | |
1182 | uint32_t fc = 0; | |
1183 | uint32_t cardnum = 0; | |
1184 | if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used | |
1185 | uint32_t lo2=0; | |
1186 | lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit | |
1187 | uint8_t idx3 = 1; | |
1188 | while(lo2>1){ //find last bit set to 1 (format len bit) | |
1189 | lo2=lo2>>1; | |
1190 | idx3++; | |
1191 | } | |
1192 | fmtLen =idx3+19; | |
1193 | fc =0; | |
1194 | cardnum=0; | |
1195 | if(fmtLen==26){ | |
1196 | cardnum = (lo>>1)&0xFFFF; | |
1197 | fc = (lo>>17)&0xFF; | |
1198 | } | |
1199 | if(fmtLen==34){ | |
1200 | cardnum = (lo>>1)&0xFFFF; | |
1201 | fc= ((hi&1)<<15)|(lo>>17); | |
1202 | } | |
1203 | if(fmtLen==35){ | |
1204 | cardnum = (lo>>1)&0xFFFFF; | |
1205 | fc = ((hi&1)<<11)|(lo>>21); | |
1206 | } | |
1207 | } | |
1208 | else { //if bit 38 is not set then 37 bit format is used | |
1209 | fmtLen = 37; | |
1210 | fc = 0; | |
1211 | cardnum = 0; | |
1212 | if(fmtLen == 37){ | |
1213 | cardnum = (lo>>1)&0x7FFFF; | |
1214 | fc = ((hi&0xF)<<12)|(lo>>20); | |
1215 | } | |
1216 | } | |
1217 | PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d", | |
1218 | (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF, | |
1219 | (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum); | |
1220 | } | |
1221 | setDemodBuf(BitStream,BitLen,idx); | |
1222 | if (g_debugMode){ | |
1223 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); | |
1224 | printDemodBuff(); | |
1225 | } | |
1226 | return 1; | |
1227 | } | |
1228 | ||
1229 | //by marshmellow | |
1230 | //Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded) | |
1231 | //print full Paradox Prox ID and some bit format details if found | |
1232 | int CmdFSKdemodParadox(const char *Cmd) | |
1233 | { | |
1234 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1235 | uint32_t hi2=0, hi=0, lo=0; | |
1236 | ||
1237 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1238 | size_t BitLen = getFromGraphBuf(BitStream); | |
1239 | if (BitLen==0) return 0; | |
1240 | //get binary from fsk wave | |
1241 | int idx = ParadoxdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo); | |
1242 | if (idx<0){ | |
1243 | if (g_debugMode){ | |
1244 | if (idx==-1){ | |
1245 | PrintAndLog("DEBUG: Just Noise Detected"); | |
1246 | } else if (idx == -2) { | |
1247 | PrintAndLog("DEBUG: Error demoding fsk"); | |
1248 | } else if (idx == -3) { | |
1249 | PrintAndLog("DEBUG: Preamble not found"); | |
1250 | } else if (idx == -4) { | |
1251 | PrintAndLog("DEBUG: Error in Manchester data"); | |
1252 | } else { | |
1253 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
1254 | } | |
1255 | } | |
1256 | return 0; | |
1257 | } | |
1258 | if (hi2==0 && hi==0 && lo==0){ | |
1259 | if (g_debugMode) PrintAndLog("DEBUG: Error - no value found"); | |
1260 | return 0; | |
1261 | } | |
1262 | uint32_t fc = ((hi & 0x3)<<6) | (lo>>26); | |
1263 | uint32_t cardnum = (lo>>10)&0xFFFF; | |
1264 | uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32); | |
1265 | uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32); | |
1266 | uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32); | |
1267 | ||
1268 | PrintAndLog("Paradox TAG ID: %x%08x - FC: %d - Card: %d - Checksum: %02x - RAW: %08x%08x%08x", | |
1269 | hi>>10, (hi & 0x3)<<26 | (lo>>10), fc, cardnum, (lo>>2) & 0xFF, rawHi2, rawHi, rawLo); | |
1270 | setDemodBuf(BitStream,BitLen,idx); | |
1271 | if (g_debugMode){ | |
1272 | PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx, BitLen); | |
1273 | printDemodBuff(); | |
1274 | } | |
1275 | return 1; | |
1276 | } | |
1277 | ||
1278 | //by marshmellow | |
1279 | //IO-Prox demod - FSK RF/64 with preamble of 000000001 | |
1280 | //print ioprox ID and some format details | |
1281 | int CmdFSKdemodIO(const char *Cmd) | |
1282 | { | |
1283 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1284 | //set defaults | |
1285 | int idx=0; | |
1286 | //something in graphbuffer? | |
1287 | if (GraphTraceLen < 65) { | |
1288 | if (g_debugMode)PrintAndLog("DEBUG: not enough samples in GraphBuffer"); | |
1289 | return 0; | |
1290 | } | |
1291 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1292 | size_t BitLen = getFromGraphBuf(BitStream); | |
1293 | if (BitLen==0) return 0; | |
1294 | ||
1295 | //get binary from fsk wave | |
1296 | idx = IOdemodFSK(BitStream,BitLen); | |
1297 | if (idx<0){ | |
1298 | if (g_debugMode){ | |
1299 | if (idx==-1){ | |
1300 | PrintAndLog("DEBUG: Just Noise Detected"); | |
1301 | } else if (idx == -2) { | |
1302 | PrintAndLog("DEBUG: not enough samples"); | |
1303 | } else if (idx == -3) { | |
1304 | PrintAndLog("DEBUG: error during fskdemod"); | |
1305 | } else if (idx == -4) { | |
1306 | PrintAndLog("DEBUG: Preamble not found"); | |
1307 | } else if (idx == -5) { | |
1308 | PrintAndLog("DEBUG: Separator bits not found"); | |
1309 | } else { | |
1310 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
1311 | } | |
1312 | } | |
1313 | return 0; | |
1314 | } | |
1315 | if (idx==0){ | |
1316 | if (g_debugMode==1){ | |
1317 | PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen); | |
1318 | if (BitLen > 92) printBitStream(BitStream,92); | |
1319 | } | |
1320 | return 0; | |
1321 | } | |
1322 | //Index map | |
1323 | //0 10 20 30 40 50 60 | |
1324 | //| | | | | | | | |
1325 | //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23 | |
1326 | //----------------------------------------------------------------------------- | |
1327 | //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 | |
1328 | // | |
1329 | //XSF(version)facility:codeone+codetwo (raw) | |
1330 | //Handle the data | |
1331 | if (idx+64>BitLen) { | |
1332 | if (g_debugMode==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen); | |
1333 | return 0; | |
1334 | } | |
1335 | PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream[idx], BitStream[idx+1], BitStream[idx+2], BitStream[idx+3], BitStream[idx+4], BitStream[idx+5], BitStream[idx+6], BitStream[idx+7], BitStream[idx+8]); | |
1336 | PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream[idx+9], BitStream[idx+10], BitStream[idx+11],BitStream[idx+12],BitStream[idx+13],BitStream[idx+14],BitStream[idx+15],BitStream[idx+16],BitStream[idx+17]); | |
1337 | PrintAndLog("%d%d%d%d%d%d%d%d %d facility",BitStream[idx+18], BitStream[idx+19], BitStream[idx+20],BitStream[idx+21],BitStream[idx+22],BitStream[idx+23],BitStream[idx+24],BitStream[idx+25],BitStream[idx+26]); | |
1338 | PrintAndLog("%d%d%d%d%d%d%d%d %d version",BitStream[idx+27], BitStream[idx+28], BitStream[idx+29],BitStream[idx+30],BitStream[idx+31],BitStream[idx+32],BitStream[idx+33],BitStream[idx+34],BitStream[idx+35]); | |
1339 | PrintAndLog("%d%d%d%d%d%d%d%d %d code1",BitStream[idx+36], BitStream[idx+37], BitStream[idx+38],BitStream[idx+39],BitStream[idx+40],BitStream[idx+41],BitStream[idx+42],BitStream[idx+43],BitStream[idx+44]); | |
1340 | PrintAndLog("%d%d%d%d%d%d%d%d %d code2",BitStream[idx+45], BitStream[idx+46], BitStream[idx+47],BitStream[idx+48],BitStream[idx+49],BitStream[idx+50],BitStream[idx+51],BitStream[idx+52],BitStream[idx+53]); | |
1341 | PrintAndLog("%d%d%d%d%d%d%d%d %d%d checksum",BitStream[idx+54],BitStream[idx+55],BitStream[idx+56],BitStream[idx+57],BitStream[idx+58],BitStream[idx+59],BitStream[idx+60],BitStream[idx+61],BitStream[idx+62],BitStream[idx+63]); | |
1342 | ||
1343 | uint32_t code = bytebits_to_byte(BitStream+idx,32); | |
1344 | uint32_t code2 = bytebits_to_byte(BitStream+idx+32,32); | |
1345 | uint8_t version = bytebits_to_byte(BitStream+idx+27,8); //14,4 | |
1346 | uint8_t facilitycode = bytebits_to_byte(BitStream+idx+18,8) ; | |
1347 | uint16_t number = (bytebits_to_byte(BitStream+idx+36,8)<<8)|(bytebits_to_byte(BitStream+idx+45,8)); //36,9 | |
1348 | PrintAndLog("IO Prox XSF(%02d)%02x:%05d (%08x%08x)",version,facilitycode,number,code,code2); | |
1349 | setDemodBuf(BitStream,64,idx); | |
1350 | if (g_debugMode){ | |
1351 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx,64); | |
1352 | printDemodBuff(); | |
1353 | } | |
1354 | return 1; | |
1355 | } | |
1356 | ||
1357 | //by marshmellow | |
1358 | //AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream) | |
1359 | //print full AWID Prox ID and some bit format details if found | |
1360 | int CmdFSKdemodAWID(const char *Cmd) | |
1361 | { | |
1362 | ||
1363 | //int verbose=1; | |
1364 | //sscanf(Cmd, "%i", &verbose); | |
1365 | ||
1366 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1367 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1368 | size_t size = getFromGraphBuf(BitStream); | |
1369 | if (size==0) return 0; | |
1370 | ||
1371 | //get binary from fsk wave | |
1372 | int idx = AWIDdemodFSK(BitStream, &size); | |
1373 | if (idx<=0){ | |
1374 | if (g_debugMode==1){ | |
1375 | if (idx == -1) | |
1376 | PrintAndLog("DEBUG: Error - not enough samples"); | |
1377 | else if (idx == -2) | |
1378 | PrintAndLog("DEBUG: Error - only noise found"); | |
1379 | else if (idx == -3) | |
1380 | PrintAndLog("DEBUG: Error - problem during FSK demod"); | |
1381 | else if (idx == -4) | |
1382 | PrintAndLog("DEBUG: Error - AWID preamble not found"); | |
1383 | else if (idx == -5) | |
1384 | PrintAndLog("DEBUG: Error - Size not correct: %d", size); | |
1385 | else | |
1386 | PrintAndLog("DEBUG: Error %d",idx); | |
1387 | } | |
1388 | return 0; | |
1389 | } | |
1390 | ||
1391 | // Index map | |
1392 | // 0 10 20 30 40 50 60 | |
1393 | // | | | | | | | | |
1394 | // 01234567 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 - to 96 | |
1395 | // ----------------------------------------------------------------------------- | |
1396 | // 00000001 000 1 110 1 101 1 011 1 101 1 010 0 000 1 000 1 010 0 001 0 110 1 100 0 000 1 000 1 | |
1397 | // premable bbb o bbb o bbw o fff o fff o ffc o ccc o ccc o ccc o ccc o ccc o wxx o xxx o xxx o - to 96 | |
1398 | // |---26 bit---| |-----117----||-------------142-------------| | |
1399 | // b = format bit len, o = odd parity of last 3 bits | |
1400 | // f = facility code, c = card number | |
1401 | // w = wiegand parity | |
1402 | // (26 bit format shown) | |
1403 | ||
1404 | //get raw ID before removing parities | |
1405 | uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32); | |
1406 | uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32); | |
1407 | uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32); | |
1408 | setDemodBuf(BitStream,96,idx); | |
1409 | ||
1410 | size = removeParity(BitStream, idx+8, 4, 1, 88); | |
1411 | if (size != 66){ | |
1412 | if (g_debugMode==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format"); | |
1413 | return 0; | |
1414 | } | |
1415 | // ok valid card found! | |
1416 | ||
1417 | // Index map | |
1418 | // 0 10 20 30 40 50 60 | |
1419 | // | | | | | | | | |
1420 | // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456 | |
1421 | // ----------------------------------------------------------------------------- | |
1422 | // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000 | |
1423 | // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
1424 | // |26 bit| |-117--| |-----142------| | |
1425 | // b = format bit len, o = odd parity of last 3 bits | |
1426 | // f = facility code, c = card number | |
1427 | // w = wiegand parity | |
1428 | // (26 bit format shown) | |
1429 | ||
1430 | uint32_t fc = 0; | |
1431 | uint32_t cardnum = 0; | |
1432 | uint32_t code1 = 0; | |
1433 | uint32_t code2 = 0; | |
1434 | uint8_t fmtLen = bytebits_to_byte(BitStream,8); | |
1435 | if (fmtLen==26){ | |
1436 | fc = bytebits_to_byte(BitStream+9, 8); | |
1437 | cardnum = bytebits_to_byte(BitStream+17, 16); | |
1438 | code1 = bytebits_to_byte(BitStream+8,fmtLen); | |
1439 | PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo); | |
1440 | } else { | |
1441 | cardnum = bytebits_to_byte(BitStream+8+(fmtLen-17), 16); | |
1442 | if (fmtLen>32){ | |
1443 | code1 = bytebits_to_byte(BitStream+8,fmtLen-32); | |
1444 | code2 = bytebits_to_byte(BitStream+8+(fmtLen-32),32); | |
1445 | PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, code2, rawHi2, rawHi, rawLo); | |
1446 | } else{ | |
1447 | code1 = bytebits_to_byte(BitStream+8,fmtLen); | |
1448 | PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, rawHi2, rawHi, rawLo); | |
1449 | } | |
1450 | } | |
1451 | if (g_debugMode){ | |
1452 | PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx, 96); | |
1453 | printDemodBuff(); | |
1454 | } | |
1455 | //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands | |
1456 | return 1; | |
1457 | } | |
1458 | ||
1459 | //by marshmellow | |
1460 | //Pyramid Prox demod - FSK RF/50 with preamble of 0000000000000001 (always a 128 bit data stream) | |
1461 | //print full Farpointe Data/Pyramid Prox ID and some bit format details if found | |
1462 | int CmdFSKdemodPyramid(const char *Cmd) | |
1463 | { | |
1464 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
1465 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1466 | size_t size = getFromGraphBuf(BitStream); | |
1467 | if (size==0) return 0; | |
1468 | ||
1469 | //get binary from fsk wave | |
1470 | int idx = PyramiddemodFSK(BitStream, &size); | |
1471 | if (idx < 0){ | |
1472 | if (g_debugMode==1){ | |
1473 | if (idx == -5) | |
1474 | PrintAndLog("DEBUG: Error - not enough samples"); | |
1475 | else if (idx == -1) | |
1476 | PrintAndLog("DEBUG: Error - only noise found"); | |
1477 | else if (idx == -2) | |
1478 | PrintAndLog("DEBUG: Error - problem during FSK demod"); | |
1479 | else if (idx == -3) | |
1480 | PrintAndLog("DEBUG: Error - Size not correct: %d", size); | |
1481 | else if (idx == -4) | |
1482 | PrintAndLog("DEBUG: Error - Pyramid preamble not found"); | |
1483 | else | |
1484 | PrintAndLog("DEBUG: Error - idx: %d",idx); | |
1485 | } | |
1486 | return 0; | |
1487 | } | |
1488 | // Index map | |
1489 | // 0 10 20 30 40 50 60 | |
1490 | // | | | | | | | | |
1491 | // 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 | |
1492 | // ----------------------------------------------------------------------------- | |
1493 | // 0000000 0 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 | |
1494 | // premable xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o | |
1495 | ||
1496 | // 64 70 80 90 100 110 120 | |
1497 | // | | | | | | | | |
1498 | // 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 | |
1499 | // ----------------------------------------------------------------------------- | |
1500 | // 0000000 1 0000000 1 0000000 1 0110111 0 0011000 1 0000001 0 0001100 1 1001010 0 | |
1501 | // xxxxxxx o xxxxxxx o xxxxxxx o xswffff o ffffccc o ccccccc o ccccccw o ppppppp o | |
1502 | // |---115---||---------71---------| | |
1503 | // s = format start bit, o = odd parity of last 7 bits | |
1504 | // f = facility code, c = card number | |
1505 | // w = wiegand parity, x = extra space for other formats | |
1506 | // p = unknown checksum | |
1507 | // (26 bit format shown) | |
1508 | ||
1509 | //get bytes for checksum calc | |
1510 | uint8_t checksum = bytebits_to_byte(BitStream + idx + 120, 8); | |
1511 | uint8_t csBuff[14] = {0x00}; | |
1512 | for (uint8_t i = 0; i < 13; i++){ | |
1513 | csBuff[i] = bytebits_to_byte(BitStream + idx + 16 + (i*8), 8); | |
1514 | } | |
1515 | //check checksum calc | |
1516 | //checksum calc thanks to ICEMAN!! | |
1517 | uint32_t checkCS = CRC8Maxim(csBuff,13); | |
1518 | ||
1519 | //get raw ID before removing parities | |
1520 | uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32); | |
1521 | uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32); | |
1522 | uint32_t rawHi2 = bytebits_to_byte(BitStream+idx+32,32); | |
1523 | uint32_t rawHi3 = bytebits_to_byte(BitStream+idx,32); | |
1524 | setDemodBuf(BitStream,128,idx); | |
1525 | ||
1526 | size = removeParity(BitStream, idx+8, 8, 1, 120); | |
1527 | if (size != 105){ | |
1528 | if (g_debugMode==1) | |
1529 | PrintAndLog("DEBUG: Error at parity check - tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3); | |
1530 | return 0; | |
1531 | } | |
1532 | ||
1533 | // ok valid card found! | |
1534 | ||
1535 | // Index map | |
1536 | // 0 10 20 30 40 50 60 70 | |
1537 | // | | | | | | | | | |
1538 | // 01234567890123456789012345678901234567890123456789012345678901234567890 | |
1539 | // ----------------------------------------------------------------------- | |
1540 | // 00000000000000000000000000000000000000000000000000000000000000000000000 | |
1541 | // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
1542 | ||
1543 | // 71 80 90 100 | |
1544 | // | | | | | |
1545 | // 1 2 34567890 1234567890123456 7 8901234 | |
1546 | // --------------------------------------- | |
1547 | // 1 1 01110011 0000000001000110 0 1001010 | |
1548 | // s w ffffffff cccccccccccccccc w ppppppp | |
1549 | // |--115-| |------71------| | |
1550 | // s = format start bit, o = odd parity of last 7 bits | |
1551 | // f = facility code, c = card number | |
1552 | // w = wiegand parity, x = extra space for other formats | |
1553 | // p = unknown checksum | |
1554 | // (26 bit format shown) | |
1555 | ||
1556 | //find start bit to get fmtLen | |
1557 | int j; | |
1558 | for (j=0; j<size; j++){ | |
1559 | if(BitStream[j]) break; | |
1560 | } | |
1561 | uint8_t fmtLen = size-j-8; | |
1562 | uint32_t fc = 0; | |
1563 | uint32_t cardnum = 0; | |
1564 | uint32_t code1 = 0; | |
1565 | //uint32_t code2 = 0; | |
1566 | if (fmtLen==26){ | |
1567 | fc = bytebits_to_byte(BitStream+73, 8); | |
1568 | cardnum = bytebits_to_byte(BitStream+81, 16); | |
1569 | code1 = bytebits_to_byte(BitStream+72,fmtLen); | |
1570 | PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi3, rawHi2, rawHi, rawLo); | |
1571 | } else if (fmtLen==45){ | |
1572 | fmtLen=42; //end = 10 bits not 7 like 26 bit fmt | |
1573 | fc = bytebits_to_byte(BitStream+53, 10); | |
1574 | cardnum = bytebits_to_byte(BitStream+63, 32); | |
1575 | PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Raw: %08x%08x%08x%08x", fmtLen, fc, cardnum, rawHi3, rawHi2, rawHi, rawLo); | |
1576 | } else { | |
1577 | cardnum = bytebits_to_byte(BitStream+81, 16); | |
1578 | if (fmtLen>32){ | |
1579 | //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen-32); | |
1580 | //code2 = bytebits_to_byte(BitStream+(size-32),32); | |
1581 | PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo); | |
1582 | } else{ | |
1583 | //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen); | |
1584 | PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo); | |
1585 | } | |
1586 | } | |
1587 | if (checksum == checkCS) | |
1588 | PrintAndLog("Checksum %02x passed", checksum); | |
1589 | else | |
1590 | PrintAndLog("Checksum %02x failed - should have been %02x", checksum, checkCS); | |
1591 | ||
1592 | if (g_debugMode){ | |
1593 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128); | |
1594 | printDemodBuff(); | |
1595 | } | |
1596 | return 1; | |
1597 | } | |
1598 | ||
1599 | int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating | |
1600 | { | |
1601 | static const int LowTone[] = { | |
1602 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1603 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1604 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1605 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1606 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 | |
1607 | }; | |
1608 | static const int HighTone[] = { | |
1609 | 1, 1, 1, 1, 1, -1, -1, -1, -1, | |
1610 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1611 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1612 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1613 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1614 | 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1615 | }; | |
1616 | ||
1617 | int lowLen = sizeof (LowTone) / sizeof (int); | |
1618 | int highLen = sizeof (HighTone) / sizeof (int); | |
1619 | int convLen = (highLen > lowLen) ? highLen : lowLen; | |
1620 | uint32_t hi = 0, lo = 0; | |
1621 | ||
1622 | int i, j; | |
1623 | int minMark = 0, maxMark = 0; | |
1624 | ||
1625 | for (i = 0; i < GraphTraceLen - convLen; ++i) { | |
1626 | int lowSum = 0, highSum = 0; | |
1627 | ||
1628 | for (j = 0; j < lowLen; ++j) { | |
1629 | lowSum += LowTone[j]*GraphBuffer[i+j]; | |
1630 | } | |
1631 | for (j = 0; j < highLen; ++j) { | |
1632 | highSum += HighTone[j] * GraphBuffer[i + j]; | |
1633 | } | |
1634 | lowSum = abs(100 * lowSum / lowLen); | |
1635 | highSum = abs(100 * highSum / highLen); | |
1636 | GraphBuffer[i] = (highSum << 16) | lowSum; | |
1637 | } | |
1638 | ||
1639 | for(i = 0; i < GraphTraceLen - convLen - 16; ++i) { | |
1640 | int lowTot = 0, highTot = 0; | |
1641 | // 10 and 8 are f_s divided by f_l and f_h, rounded | |
1642 | for (j = 0; j < 10; ++j) { | |
1643 | lowTot += (GraphBuffer[i+j] & 0xffff); | |
1644 | } | |
1645 | for (j = 0; j < 8; j++) { | |
1646 | highTot += (GraphBuffer[i + j] >> 16); | |
1647 | } | |
1648 | GraphBuffer[i] = lowTot - highTot; | |
1649 | if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i]; | |
1650 | if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i]; | |
1651 | } | |
1652 | ||
1653 | GraphTraceLen -= (convLen + 16); | |
1654 | RepaintGraphWindow(); | |
1655 | ||
1656 | // Find bit-sync (3 lo followed by 3 high) (HID ONLY) | |
1657 | int max = 0, maxPos = 0; | |
1658 | for (i = 0; i < 6000; ++i) { | |
1659 | int dec = 0; | |
1660 | for (j = 0; j < 3 * lowLen; ++j) { | |
1661 | dec -= GraphBuffer[i + j]; | |
1662 | } | |
1663 | for (; j < 3 * (lowLen + highLen ); ++j) { | |
1664 | dec += GraphBuffer[i + j]; | |
1665 | } | |
1666 | if (dec > max) { | |
1667 | max = dec; | |
1668 | maxPos = i; | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | // place start of bit sync marker in graph | |
1673 | GraphBuffer[maxPos] = maxMark; | |
1674 | GraphBuffer[maxPos + 1] = minMark; | |
1675 | ||
1676 | maxPos += j; | |
1677 | ||
1678 | // place end of bit sync marker in graph | |
1679 | GraphBuffer[maxPos] = maxMark; | |
1680 | GraphBuffer[maxPos+1] = minMark; | |
1681 | ||
1682 | PrintAndLog("actual data bits start at sample %d", maxPos); | |
1683 | PrintAndLog("length %d/%d", highLen, lowLen); | |
1684 | ||
1685 | uint8_t bits[46] = {0x00}; | |
1686 | ||
1687 | // find bit pairs and manchester decode them | |
1688 | for (i = 0; i < arraylen(bits) - 1; ++i) { | |
1689 | int dec = 0; | |
1690 | for (j = 0; j < lowLen; ++j) { | |
1691 | dec -= GraphBuffer[maxPos + j]; | |
1692 | } | |
1693 | for (; j < lowLen + highLen; ++j) { | |
1694 | dec += GraphBuffer[maxPos + j]; | |
1695 | } | |
1696 | maxPos += j; | |
1697 | // place inter bit marker in graph | |
1698 | GraphBuffer[maxPos] = maxMark; | |
1699 | GraphBuffer[maxPos + 1] = minMark; | |
1700 | ||
1701 | // hi and lo form a 64 bit pair | |
1702 | hi = (hi << 1) | (lo >> 31); | |
1703 | lo = (lo << 1); | |
1704 | // store decoded bit as binary (in hi/lo) and text (in bits[]) | |
1705 | if(dec < 0) { | |
1706 | bits[i] = '1'; | |
1707 | lo |= 1; | |
1708 | } else { | |
1709 | bits[i] = '0'; | |
1710 | } | |
1711 | } | |
1712 | PrintAndLog("bits: '%s'", bits); | |
1713 | PrintAndLog("hex: %08x %08x", hi, lo); | |
1714 | return 0; | |
1715 | } | |
1716 | ||
1717 | //by marshmellow | |
1718 | //attempt to psk1 demod graph buffer | |
1719 | int PSKDemod(const char *Cmd, bool verbose) | |
1720 | { | |
1721 | int invert=0; | |
1722 | int clk=0; | |
1723 | int maxErr=100; | |
1724 | sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); | |
1725 | if (clk==1){ | |
1726 | invert=1; | |
1727 | clk=0; | |
1728 | } | |
1729 | if (invert != 0 && invert != 1) { | |
1730 | if (verbose) PrintAndLog("Invalid argument: %s", Cmd); | |
1731 | return 0; | |
1732 | } | |
1733 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1734 | size_t BitLen = getFromGraphBuf(BitStream); | |
1735 | if (BitLen==0) return -1; | |
1736 | uint8_t carrier=countPSK_FC(BitStream, BitLen); | |
1737 | if (carrier!=2 && carrier!=4 && carrier!=8){ | |
1738 | //invalid carrier | |
1739 | return 0; | |
1740 | } | |
1741 | int errCnt=0; | |
1742 | errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert); | |
1743 | if (errCnt > maxErr){ | |
1744 | if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); | |
1745 | return 0; | |
1746 | } | |
1747 | if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) | |
1748 | if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); | |
1749 | return 0; | |
1750 | } | |
1751 | if (verbose){ | |
1752 | PrintAndLog("Tried PSK Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); | |
1753 | if (errCnt>0){ | |
1754 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
1755 | } | |
1756 | } | |
1757 | //prime demod buffer for output | |
1758 | setDemodBuf(BitStream,BitLen,0); | |
1759 | return 1; | |
1760 | } | |
1761 | ||
1762 | // Indala 26 bit decode | |
1763 | // by marshmellow | |
1764 | // optional arguments - same as CmdpskNRZrawDemod (clock & invert) | |
1765 | int CmdIndalaDecode(const char *Cmd) | |
1766 | { | |
1767 | int ans; | |
1768 | if (strlen(Cmd)>0){ | |
1769 | ans = PSKDemod(Cmd, 0); | |
1770 | } else{ //default to RF/32 | |
1771 | ans = PSKDemod("32", 0); | |
1772 | } | |
1773 | ||
1774 | if (!ans){ | |
1775 | if (g_debugMode==1) | |
1776 | PrintAndLog("Error1: %d",ans); | |
1777 | return 0; | |
1778 | } | |
1779 | uint8_t invert=0; | |
1780 | ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert); | |
1781 | if (ans < 1) { | |
1782 | if (g_debugMode==1) | |
1783 | PrintAndLog("Error2: %d",ans); | |
1784 | return -1; | |
1785 | } | |
1786 | char showbits[251]={0x00}; | |
1787 | if (invert) | |
1788 | if (g_debugMode==1) | |
1789 | PrintAndLog("Had to invert bits"); | |
1790 | ||
1791 | //convert UID to HEX | |
1792 | uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; | |
1793 | int idx; | |
1794 | uid1=0; | |
1795 | uid2=0; | |
1796 | PrintAndLog("BitLen: %d",DemodBufferLen); | |
1797 | if (DemodBufferLen==64){ | |
1798 | for( idx=0; idx<64; idx++) { | |
1799 | uid1=(uid1<<1)|(uid2>>31); | |
1800 | if (DemodBuffer[idx] == 0) { | |
1801 | uid2=(uid2<<1)|0; | |
1802 | showbits[idx]='0'; | |
1803 | } else { | |
1804 | uid2=(uid2<<1)|1; | |
1805 | showbits[idx]='1'; | |
1806 | } | |
1807 | } | |
1808 | showbits[idx]='\0'; | |
1809 | PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2); | |
1810 | } | |
1811 | else { | |
1812 | uid3=0; | |
1813 | uid4=0; | |
1814 | uid5=0; | |
1815 | uid6=0; | |
1816 | uid7=0; | |
1817 | for( idx=0; idx<DemodBufferLen; idx++) { | |
1818 | uid1=(uid1<<1)|(uid2>>31); | |
1819 | uid2=(uid2<<1)|(uid3>>31); | |
1820 | uid3=(uid3<<1)|(uid4>>31); | |
1821 | uid4=(uid4<<1)|(uid5>>31); | |
1822 | uid5=(uid5<<1)|(uid6>>31); | |
1823 | uid6=(uid6<<1)|(uid7>>31); | |
1824 | if (DemodBuffer[idx] == 0) { | |
1825 | uid7=(uid7<<1)|0; | |
1826 | showbits[idx]='0'; | |
1827 | } | |
1828 | else { | |
1829 | uid7=(uid7<<1)|1; | |
1830 | showbits[idx]='1'; | |
1831 | } | |
1832 | } | |
1833 | showbits[idx]='\0'; | |
1834 | PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7); | |
1835 | } | |
1836 | if (g_debugMode){ | |
1837 | PrintAndLog("DEBUG: printing demodbuffer:"); | |
1838 | printDemodBuff(); | |
1839 | } | |
1840 | return 1; | |
1841 | } | |
1842 | ||
1843 | // by marshmellow | |
1844 | // takes 3 arguments - clock, invert, maxErr as integers | |
1845 | // attempts to demodulate nrz only | |
1846 | // prints binary found and saves in demodbuffer for further commands | |
1847 | ||
1848 | int NRZrawDemod(const char *Cmd, bool verbose) | |
1849 | { | |
1850 | int invert=0; | |
1851 | int clk=0; | |
1852 | int maxErr=100; | |
1853 | sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); | |
1854 | if (clk==1){ | |
1855 | invert=1; | |
1856 | clk=0; | |
1857 | } | |
1858 | if (invert != 0 && invert != 1) { | |
1859 | PrintAndLog("Invalid argument: %s", Cmd); | |
1860 | return 0; | |
1861 | } | |
1862 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1863 | size_t BitLen = getFromGraphBuf(BitStream); | |
1864 | if (BitLen==0) return 0; | |
1865 | int errCnt=0; | |
1866 | errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, maxErr); | |
1867 | if (errCnt > maxErr){ | |
1868 | if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); | |
1869 | return 0; | |
1870 | } | |
1871 | if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) | |
1872 | if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); | |
1873 | return 0; | |
1874 | } | |
1875 | if (verbose) | |
1876 | PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); | |
1877 | //prime demod buffer for output | |
1878 | setDemodBuf(BitStream,BitLen,0); | |
1879 | ||
1880 | if (errCnt>0 && verbose){ | |
1881 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
1882 | } | |
1883 | if (verbose) { | |
1884 | PrintAndLog("NRZ demoded bitstream:"); | |
1885 | // Now output the bitstream to the scrollback by line of 16 bits | |
1886 | printDemodBuff(); | |
1887 | } | |
1888 | return 1; | |
1889 | } | |
1890 | ||
1891 | int CmdNRZrawDemod(const char *Cmd) | |
1892 | { | |
1893 | char cmdp = param_getchar(Cmd, 0); | |
1894 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
1895 | PrintAndLog("Usage: data rawdemod nr [clock] <0|1> [maxError]"); | |
1896 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); | |
1897 | PrintAndLog(" <invert>, 1 for invert output"); | |
1898 | PrintAndLog(" [set maximum allowed errors], default = 100."); | |
1899 | PrintAndLog(""); | |
1900 | PrintAndLog(" sample: data rawdemod nr = demod a nrz/direct tag from GraphBuffer"); | |
1901 | PrintAndLog(" : data rawdemod nr 32 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32"); | |
1902 | PrintAndLog(" : data rawdemod nr 32 1 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32 and inverting data"); | |
1903 | PrintAndLog(" : data rawdemod nr 1 = demod a nrz/direct tag from GraphBuffer while inverting data"); | |
1904 | PrintAndLog(" : data rawdemod nr 64 1 0 = demod a nrz/direct tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors"); | |
1905 | return 0; | |
1906 | } | |
1907 | return NRZrawDemod(Cmd, TRUE); | |
1908 | } | |
1909 | ||
1910 | // by marshmellow | |
1911 | // takes 3 arguments - clock, invert, maxErr as integers | |
1912 | // attempts to demodulate psk only | |
1913 | // prints binary found and saves in demodbuffer for further commands | |
1914 | int CmdPSK1rawDemod(const char *Cmd) | |
1915 | { | |
1916 | int ans; | |
1917 | char cmdp = param_getchar(Cmd, 0); | |
1918 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
1919 | PrintAndLog("Usage: data rawdemod p1 [clock] <0|1> [maxError]"); | |
1920 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); | |
1921 | PrintAndLog(" <invert>, 1 for invert output"); | |
1922 | PrintAndLog(" [set maximum allowed errors], default = 100."); | |
1923 | PrintAndLog(""); | |
1924 | PrintAndLog(" sample: data rawdemod p1 = demod a psk1 tag from GraphBuffer"); | |
1925 | PrintAndLog(" : data rawdemod p1 32 = demod a psk1 tag from GraphBuffer using a clock of RF/32"); | |
1926 | PrintAndLog(" : data rawdemod p1 32 1 = demod a psk1 tag from GraphBuffer using a clock of RF/32 and inverting data"); | |
1927 | PrintAndLog(" : data rawdemod p1 1 = demod a psk1 tag from GraphBuffer while inverting data"); | |
1928 | PrintAndLog(" : data rawdemod p1 64 1 0 = demod a psk1 tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors"); | |
1929 | return 0; | |
1930 | } | |
1931 | ans = PSKDemod(Cmd, TRUE); | |
1932 | //output | |
1933 | if (!ans){ | |
1934 | if (g_debugMode) PrintAndLog("Error demoding: %d",ans); | |
1935 | return 0; | |
1936 | } | |
1937 | ||
1938 | PrintAndLog("PSK demoded bitstream:"); | |
1939 | // Now output the bitstream to the scrollback by line of 16 bits | |
1940 | printDemodBuff(); | |
1941 | return 1; | |
1942 | } | |
1943 | ||
1944 | // by marshmellow | |
1945 | // takes same args as cmdpsk1rawdemod | |
1946 | int CmdPSK2rawDemod(const char *Cmd) | |
1947 | { | |
1948 | int ans=0; | |
1949 | char cmdp = param_getchar(Cmd, 0); | |
1950 | if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') { | |
1951 | PrintAndLog("Usage: data rawdemod p2 [clock] <0|1> [maxError]"); | |
1952 | PrintAndLog(" [set clock as integer] optional, if not set, autodetect."); | |
1953 | PrintAndLog(" <invert>, 1 for invert output"); | |
1954 | PrintAndLog(" [set maximum allowed errors], default = 100."); | |
1955 | PrintAndLog(""); | |
1956 | PrintAndLog(" sample: data rawdemod p2 = demod a psk2 tag from GraphBuffer, autodetect clock"); | |
1957 | PrintAndLog(" : data rawdemod p2 32 = demod a psk2 tag from GraphBuffer using a clock of RF/32"); | |
1958 | PrintAndLog(" : data rawdemod p2 32 1 = demod a psk2 tag from GraphBuffer using a clock of RF/32 and inverting output"); | |
1959 | PrintAndLog(" : data rawdemod p2 1 = demod a psk2 tag from GraphBuffer, autodetect clock and invert output"); | |
1960 | PrintAndLog(" : data rawdemod p2 64 1 0 = demod a psk2 tag from GraphBuffer using a clock of RF/64, inverting output and allowing 0 demod errors"); | |
1961 | return 0; | |
1962 | } | |
1963 | ans=PSKDemod(Cmd, TRUE); | |
1964 | if (!ans){ | |
1965 | if (g_debugMode) PrintAndLog("Error demoding: %d",ans); | |
1966 | return 0; | |
1967 | } | |
1968 | psk1TOpsk2(DemodBuffer, DemodBufferLen); | |
1969 | PrintAndLog("PSK2 demoded bitstream:"); | |
1970 | // Now output the bitstream to the scrollback by line of 16 bits | |
1971 | printDemodBuff(); | |
1972 | return 1; | |
1973 | } | |
1974 | ||
1975 | // by marshmellow - combines all raw demod functions into one menu command | |
1976 | int CmdRawDemod(const char *Cmd) | |
1977 | { | |
1978 | char cmdp = Cmd[0]; //param_getchar(Cmd, 0); | |
1979 | ||
1980 | if (strlen(Cmd) > 14 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd)<2) { | |
1981 | PrintAndLog("Usage: data rawdemod [modulation] <help>|<options>"); | |
1982 | PrintAndLog(" [modulation] as 2 char, 'ab' for ask/biphase, 'am' for ask/manchester, 'ar' for ask/raw, 'fs' for fsk, ..."); | |
1983 | PrintAndLog(" 'nr' for nrz/direct, 'p1' for psk1, 'p2' for psk2"); | |
1984 | PrintAndLog(" <help> as 'h', prints the help for the specific modulation"); | |
1985 | PrintAndLog(" <options> see specific modulation help for optional parameters"); | |
1986 | PrintAndLog(""); | |
1987 | PrintAndLog(" sample: data rawdemod fs h = print help for ask/raw demod"); | |
1988 | PrintAndLog(" : data rawdemod fs = demod GraphBuffer using: fsk - autodetect"); | |
1989 | PrintAndLog(" : data rawdemod ab = demod GraphBuffer using: ask/biphase - autodetect"); | |
1990 | PrintAndLog(" : data rawdemod am = demod GraphBuffer using: ask/manchester - autodetect"); | |
1991 | PrintAndLog(" : data rawdemod ar = demod GraphBuffer using: ask/raw - autodetect"); | |
1992 | PrintAndLog(" : data rawdemod nr = demod GraphBuffer using: nrz/direct - autodetect"); | |
1993 | PrintAndLog(" : data rawdemod p1 = demod GraphBuffer using: psk1 - autodetect"); | |
1994 | PrintAndLog(" : data rawdemod p2 = demod GraphBuffer using: psk2 - autodetect"); | |
1995 | return 0; | |
1996 | } | |
1997 | char cmdp2 = Cmd[1]; | |
1998 | int ans = 0; | |
1999 | if (cmdp == 'f' && cmdp2 == 's'){ | |
2000 | ans = CmdFSKrawdemod(Cmd+3); | |
2001 | } else if(cmdp == 'a' && cmdp2 == 'b'){ | |
2002 | ans = Cmdaskbiphdemod(Cmd+3); | |
2003 | } else if(cmdp == 'a' && cmdp2 == 'm'){ | |
2004 | ans = Cmdaskmandemod(Cmd+3); | |
2005 | } else if(cmdp == 'a' && cmdp2 == 'r'){ | |
2006 | ans = Cmdaskrawdemod(Cmd+3); | |
2007 | } else if(cmdp == 'n' && cmdp2 == 'r'){ | |
2008 | ans = CmdNRZrawDemod(Cmd+3); | |
2009 | } else if(cmdp == 'p' && cmdp2 == '1'){ | |
2010 | ans = CmdPSK1rawDemod(Cmd+3); | |
2011 | } else if(cmdp == 'p' && cmdp2 == '2'){ | |
2012 | ans = CmdPSK2rawDemod(Cmd+3); | |
2013 | } else { | |
2014 | PrintAndLog("unknown modulation entered - see help ('h') for parameter structure"); | |
2015 | } | |
2016 | return ans; | |
2017 | } | |
2018 | ||
2019 | int CmdGrid(const char *Cmd) | |
2020 | { | |
2021 | sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); | |
2022 | PlotGridXdefault= PlotGridX; | |
2023 | PlotGridYdefault= PlotGridY; | |
2024 | RepaintGraphWindow(); | |
2025 | return 0; | |
2026 | } | |
2027 | ||
2028 | int CmdHexsamples(const char *Cmd) | |
2029 | { | |
2030 | int i, j; | |
2031 | int requested = 0; | |
2032 | int offset = 0; | |
2033 | char string_buf[25]; | |
2034 | char* string_ptr = string_buf; | |
2035 | uint8_t got[BIGBUF_SIZE]; | |
2036 | ||
2037 | sscanf(Cmd, "%i %i", &requested, &offset); | |
2038 | ||
2039 | /* if no args send something */ | |
2040 | if (requested == 0) { | |
2041 | requested = 8; | |
2042 | } | |
2043 | if (offset + requested > sizeof(got)) { | |
2044 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE); | |
2045 | return 0; | |
2046 | } | |
2047 | ||
2048 | GetFromBigBuf(got,requested,offset); | |
2049 | WaitForResponse(CMD_ACK,NULL); | |
2050 | ||
2051 | i = 0; | |
2052 | for (j = 0; j < requested; j++) { | |
2053 | i++; | |
2054 | string_ptr += sprintf(string_ptr, "%02x ", got[j]); | |
2055 | if (i == 8) { | |
2056 | *(string_ptr - 1) = '\0'; // remove the trailing space | |
2057 | PrintAndLog("%s", string_buf); | |
2058 | string_buf[0] = '\0'; | |
2059 | string_ptr = string_buf; | |
2060 | i = 0; | |
2061 | } | |
2062 | if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes | |
2063 | *(string_ptr - 1) = '\0'; | |
2064 | PrintAndLog("%s", string_buf); | |
2065 | string_buf[0] = '\0'; | |
2066 | } | |
2067 | } | |
2068 | return 0; | |
2069 | } | |
2070 | ||
2071 | int CmdHide(const char *Cmd) | |
2072 | { | |
2073 | HideGraphWindow(); | |
2074 | return 0; | |
2075 | } | |
2076 | ||
2077 | //zero mean GraphBuffer | |
2078 | int CmdHpf(const char *Cmd) | |
2079 | { | |
2080 | int i; | |
2081 | int accum = 0; | |
2082 | ||
2083 | for (i = 10; i < GraphTraceLen; ++i) | |
2084 | accum += GraphBuffer[i]; | |
2085 | accum /= (GraphTraceLen - 10); | |
2086 | for (i = 0; i < GraphTraceLen; ++i) | |
2087 | GraphBuffer[i] -= accum; | |
2088 | ||
2089 | RepaintGraphWindow(); | |
2090 | return 0; | |
2091 | } | |
2092 | typedef struct { | |
2093 | uint8_t * buffer; | |
2094 | uint32_t numbits; | |
2095 | uint32_t position; | |
2096 | }BitstreamOut; | |
2097 | ||
2098 | bool _headBit( BitstreamOut *stream) | |
2099 | { | |
2100 | int bytepos = stream->position >> 3; // divide by 8 | |
2101 | int bitpos = (stream->position++) & 7; // mask out 00000111 | |
2102 | return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1; | |
2103 | } | |
2104 | ||
2105 | uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b) | |
2106 | { | |
2107 | int i; | |
2108 | uint8_t val = 0; | |
2109 | for(i =0 ; i < bits_per_sample; i++) | |
2110 | { | |
2111 | val |= (_headBit(b) << (7-i)); | |
2112 | } | |
2113 | return val; | |
2114 | } | |
2115 | ||
2116 | int CmdSamples(const char *Cmd) | |
2117 | { | |
2118 | //If we get all but the last byte in bigbuf, | |
2119 | // we don't have to worry about remaining trash | |
2120 | // in the last byte in case the bits-per-sample | |
2121 | // does not line up on byte boundaries | |
2122 | uint8_t got[BIGBUF_SIZE-1] = { 0 }; | |
2123 | ||
2124 | int n = strtol(Cmd, NULL, 0); | |
2125 | if (n == 0) | |
2126 | n = sizeof(got); | |
2127 | ||
2128 | if (n > sizeof(got)) | |
2129 | n = sizeof(got); | |
2130 | ||
2131 | PrintAndLog("Reading %d bytes from device memory\n", n); | |
2132 | GetFromBigBuf(got,n,0); | |
2133 | PrintAndLog("Data fetched"); | |
2134 | UsbCommand response; | |
2135 | WaitForResponse(CMD_ACK, &response); | |
2136 | uint8_t bits_per_sample = 8; | |
2137 | ||
2138 | //Old devices without this feature would send 0 at arg[0] | |
2139 | if(response.arg[0] > 0) | |
2140 | { | |
2141 | sample_config *sc = (sample_config *) response.d.asBytes; | |
2142 | PrintAndLog("Samples @ %d bits/smpl, decimation 1:%d ", sc->bits_per_sample | |
2143 | , sc->decimation); | |
2144 | bits_per_sample = sc->bits_per_sample; | |
2145 | } | |
2146 | if(bits_per_sample < 8) | |
2147 | { | |
2148 | PrintAndLog("Unpacking..."); | |
2149 | BitstreamOut bout = { got, bits_per_sample * n, 0}; | |
2150 | int j =0; | |
2151 | for (j = 0; j * bits_per_sample < n * 8 && j < sizeof(GraphBuffer); j++) { | |
2152 | uint8_t sample = getByte(bits_per_sample, &bout); | |
2153 | GraphBuffer[j] = ((int) sample )- 128; | |
2154 | } | |
2155 | GraphTraceLen = j; | |
2156 | PrintAndLog("Unpacked %d samples" , j ); | |
2157 | }else | |
2158 | { | |
2159 | for (int j = 0; j < n; j++) { | |
2160 | GraphBuffer[j] = ((int)got[j]) - 128; | |
2161 | } | |
2162 | GraphTraceLen = n; | |
2163 | } | |
2164 | ||
2165 | RepaintGraphWindow(); | |
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | int CmdTuneSamples(const char *Cmd) | |
2170 | { | |
2171 | int timeout = 0; | |
2172 | printf("\nMeasuring antenna characteristics, please wait..."); | |
2173 | ||
2174 | UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING}; | |
2175 | SendCommand(&c); | |
2176 | ||
2177 | UsbCommand resp; | |
2178 | while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) { | |
2179 | timeout++; | |
2180 | printf("."); | |
2181 | if (timeout > 7) { | |
2182 | PrintAndLog("\nNo response from Proxmark. Aborting..."); | |
2183 | return 1; | |
2184 | } | |
2185 | } | |
2186 | ||
2187 | int peakv, peakf; | |
2188 | int vLf125, vLf134, vHf; | |
2189 | vLf125 = resp.arg[0] & 0xffff; | |
2190 | vLf134 = resp.arg[0] >> 16; | |
2191 | vHf = resp.arg[1] & 0xffff;; | |
2192 | peakf = resp.arg[2] & 0xffff; | |
2193 | peakv = resp.arg[2] >> 16; | |
2194 | PrintAndLog(""); | |
2195 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); | |
2196 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
2197 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
2198 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
2199 | ||
2200 | #define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher. | |
2201 | #define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher. | |
2202 | #define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher. | |
2203 | #define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher. | |
2204 | ||
2205 | if (peakv < LF_UNUSABLE_V) | |
2206 | PrintAndLog("# Your LF antenna is unusable."); | |
2207 | else if (peakv < LF_MARGINAL_V) | |
2208 | PrintAndLog("# Your LF antenna is marginal."); | |
2209 | if (vHf < HF_UNUSABLE_V) | |
2210 | PrintAndLog("# Your HF antenna is unusable."); | |
2211 | else if (vHf < HF_MARGINAL_V) | |
2212 | PrintAndLog("# Your HF antenna is marginal."); | |
2213 | ||
2214 | if (peakv >= LF_UNUSABLE_V) { | |
2215 | for (int i = 0; i < 256; i++) { | |
2216 | GraphBuffer[i] = resp.d.asBytes[i] - 128; | |
2217 | } | |
2218 | PrintAndLog("Displaying LF tuning graph. Divisor 89 is 134khz, 95 is 125khz.\n"); | |
2219 | PrintAndLog("\n"); | |
2220 | GraphTraceLen = 256; | |
2221 | ShowGraphWindow(); | |
2222 | RepaintGraphWindow(); | |
2223 | } | |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
2228 | ||
2229 | int CmdLoad(const char *Cmd) | |
2230 | { | |
2231 | char filename[FILE_PATH_SIZE] = {0x00}; | |
2232 | int len = 0; | |
2233 | ||
2234 | len = strlen(Cmd); | |
2235 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
2236 | memcpy(filename, Cmd, len); | |
2237 | ||
2238 | FILE *f = fopen(filename, "r"); | |
2239 | if (!f) { | |
2240 | PrintAndLog("couldn't open '%s'", filename); | |
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | GraphTraceLen = 0; | |
2245 | char line[80]; | |
2246 | while (fgets(line, sizeof (line), f)) { | |
2247 | GraphBuffer[GraphTraceLen] = atoi(line); | |
2248 | GraphTraceLen++; | |
2249 | } | |
2250 | fclose(f); | |
2251 | PrintAndLog("loaded %d samples", GraphTraceLen); | |
2252 | RepaintGraphWindow(); | |
2253 | return 0; | |
2254 | } | |
2255 | ||
2256 | int CmdLtrim(const char *Cmd) | |
2257 | { | |
2258 | int ds = atoi(Cmd); | |
2259 | ||
2260 | for (int i = ds; i < GraphTraceLen; ++i) | |
2261 | GraphBuffer[i-ds] = GraphBuffer[i]; | |
2262 | GraphTraceLen -= ds; | |
2263 | ||
2264 | RepaintGraphWindow(); | |
2265 | return 0; | |
2266 | } | |
2267 | ||
2268 | // trim graph to input argument length | |
2269 | int CmdRtrim(const char *Cmd) | |
2270 | { | |
2271 | int ds = atoi(Cmd); | |
2272 | ||
2273 | GraphTraceLen = ds; | |
2274 | ||
2275 | RepaintGraphWindow(); | |
2276 | return 0; | |
2277 | } | |
2278 | ||
2279 | /* | |
2280 | * Manchester demodulate a bitstream. The bitstream needs to be already in | |
2281 | * the GraphBuffer as 0 and 1 values | |
2282 | * | |
2283 | * Give the clock rate as argument in order to help the sync - the algorithm | |
2284 | * resyncs at each pulse anyway. | |
2285 | * | |
2286 | * Not optimized by any means, this is the 1st time I'm writing this type of | |
2287 | * routine, feel free to improve... | |
2288 | * | |
2289 | * 1st argument: clock rate (as number of samples per clock rate) | |
2290 | * Typical values can be 64, 32, 128... | |
2291 | */ | |
2292 | int CmdManchesterDemod(const char *Cmd) | |
2293 | { | |
2294 | int i, j, invert= 0; | |
2295 | int bit; | |
2296 | int clock; | |
2297 | int lastval = 0; | |
2298 | int low = 0; | |
2299 | int high = 0; | |
2300 | int hithigh, hitlow, first; | |
2301 | int lc = 0; | |
2302 | int bitidx = 0; | |
2303 | int bit2idx = 0; | |
2304 | int warnings = 0; | |
2305 | ||
2306 | /* check if we're inverting output */ | |
2307 | if (*Cmd == 'i') | |
2308 | { | |
2309 | PrintAndLog("Inverting output"); | |
2310 | invert = 1; | |
2311 | ++Cmd; | |
2312 | do | |
2313 | ++Cmd; | |
2314 | while(*Cmd == ' '); // in case a 2nd argument was given | |
2315 | } | |
2316 | ||
2317 | /* Holds the decoded bitstream: each clock period contains 2 bits */ | |
2318 | /* later simplified to 1 bit after manchester decoding. */ | |
2319 | /* Add 10 bits to allow for noisy / uncertain traces without aborting */ | |
2320 | /* int BitStream[GraphTraceLen*2/clock+10]; */ | |
2321 | ||
2322 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
2323 | /* large array */ | |
2324 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; | |
2325 | ||
2326 | /* Detect high and lows */ | |
2327 | for (i = 0; i < GraphTraceLen; i++) | |
2328 | { | |
2329 | if (GraphBuffer[i] > high) | |
2330 | high = GraphBuffer[i]; | |
2331 | else if (GraphBuffer[i] < low) | |
2332 | low = GraphBuffer[i]; | |
2333 | } | |
2334 | ||
2335 | /* Get our clock */ | |
2336 | clock = GetAskClock(Cmd, high, 1); | |
2337 | ||
2338 | int tolerance = clock/4; | |
2339 | ||
2340 | /* Detect first transition */ | |
2341 | /* Lo-Hi (arbitrary) */ | |
2342 | /* skip to the first high */ | |
2343 | for (i= 0; i < GraphTraceLen; i++) | |
2344 | if (GraphBuffer[i] == high) | |
2345 | break; | |
2346 | /* now look for the first low */ | |
2347 | for (; i < GraphTraceLen; i++) | |
2348 | { | |
2349 | if (GraphBuffer[i] == low) | |
2350 | { | |
2351 | lastval = i; | |
2352 | break; | |
2353 | } | |
2354 | } | |
2355 | ||
2356 | /* If we're not working with 1/0s, demod based off clock */ | |
2357 | if (high != 1) | |
2358 | { | |
2359 | bit = 0; /* We assume the 1st bit is zero, it may not be | |
2360 | * the case: this routine (I think) has an init problem. | |
2361 | * Ed. | |
2362 | */ | |
2363 | for (; i < (int)(GraphTraceLen / clock); i++) | |
2364 | { | |
2365 | hithigh = 0; | |
2366 | hitlow = 0; | |
2367 | first = 1; | |
2368 | ||
2369 | /* Find out if we hit both high and low peaks */ | |
2370 | for (j = 0; j < clock; j++) | |
2371 | { | |
2372 | if (GraphBuffer[(i * clock) + j] == high) | |
2373 | hithigh = 1; | |
2374 | else if (GraphBuffer[(i * clock) + j] == low) | |
2375 | hitlow = 1; | |
2376 | ||
2377 | /* it doesn't count if it's the first part of our read | |
2378 | because it's really just trailing from the last sequence */ | |
2379 | if (first && (hithigh || hitlow)) | |
2380 | hithigh = hitlow = 0; | |
2381 | else | |
2382 | first = 0; | |
2383 | ||
2384 | if (hithigh && hitlow) | |
2385 | break; | |
2386 | } | |
2387 | ||
2388 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
2389 | if (!hithigh || !hitlow) | |
2390 | bit ^= 1; | |
2391 | ||
2392 | BitStream[bit2idx++] = bit ^ invert; | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | /* standard 1/0 bitstream */ | |
2397 | else | |
2398 | { | |
2399 | ||
2400 | /* Then detect duration between 2 successive transitions */ | |
2401 | for (bitidx = 1; i < GraphTraceLen; i++) | |
2402 | { | |
2403 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
2404 | { | |
2405 | lc = i-lastval; | |
2406 | lastval = i; | |
2407 | ||
2408 | // Error check: if bitidx becomes too large, we do not | |
2409 | // have a Manchester encoded bitstream or the clock is really | |
2410 | // wrong! | |
2411 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
2412 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
2413 | return 0; | |
2414 | } | |
2415 | // Then switch depending on lc length: | |
2416 | // Tolerance is 1/4 of clock rate (arbitrary) | |
2417 | if (abs(lc-clock/2) < tolerance) { | |
2418 | // Short pulse : either "1" or "0" | |
2419 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
2420 | } else if (abs(lc-clock) < tolerance) { | |
2421 | // Long pulse: either "11" or "00" | |
2422 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
2423 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
2424 | } else { | |
2425 | // Error | |
2426 | warnings++; | |
2427 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); | |
2428 | PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)"); | |
2429 | ||
2430 | if (warnings > 10) | |
2431 | { | |
2432 | PrintAndLog("Error: too many detection errors, aborting."); | |
2433 | return 0; | |
2434 | } | |
2435 | } | |
2436 | } | |
2437 | } | |
2438 | ||
2439 | // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream | |
2440 | // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful | |
2441 | // to stop output at the final bitidx2 value, not bitidx | |
2442 | for (i = 0; i < bitidx; i += 2) { | |
2443 | if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) { | |
2444 | BitStream[bit2idx++] = 1 ^ invert; | |
2445 | } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) { | |
2446 | BitStream[bit2idx++] = 0 ^ invert; | |
2447 | } else { | |
2448 | // We cannot end up in this state, this means we are unsynchronized, | |
2449 | // move up 1 bit: | |
2450 | i++; | |
2451 | warnings++; | |
2452 | PrintAndLog("Unsynchronized, resync..."); | |
2453 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
2454 | ||
2455 | if (warnings > 10) | |
2456 | { | |
2457 | PrintAndLog("Error: too many decode errors, aborting."); | |
2458 | return 0; | |
2459 | } | |
2460 | } | |
2461 | } | |
2462 | } | |
2463 | ||
2464 | PrintAndLog("Manchester decoded bitstream"); | |
2465 | // Now output the bitstream to the scrollback by line of 16 bits | |
2466 | for (i = 0; i < (bit2idx-16); i+=16) { | |
2467 | PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i", | |
2468 | BitStream[i], | |
2469 | BitStream[i+1], | |
2470 | BitStream[i+2], | |
2471 | BitStream[i+3], | |
2472 | BitStream[i+4], | |
2473 | BitStream[i+5], | |
2474 | BitStream[i+6], | |
2475 | BitStream[i+7], | |
2476 | BitStream[i+8], | |
2477 | BitStream[i+9], | |
2478 | BitStream[i+10], | |
2479 | BitStream[i+11], | |
2480 | BitStream[i+12], | |
2481 | BitStream[i+13], | |
2482 | BitStream[i+14], | |
2483 | BitStream[i+15]); | |
2484 | } | |
2485 | return 0; | |
2486 | } | |
2487 | ||
2488 | /* Modulate our data into manchester */ | |
2489 | int CmdManchesterMod(const char *Cmd) | |
2490 | { | |
2491 | int i, j; | |
2492 | int clock; | |
2493 | int bit, lastbit, wave; | |
2494 | ||
2495 | /* Get our clock */ | |
2496 | clock = GetAskClock(Cmd, 0, 1); | |
2497 | ||
2498 | wave = 0; | |
2499 | lastbit = 1; | |
2500 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
2501 | { | |
2502 | bit = GraphBuffer[i * clock] ^ 1; | |
2503 | ||
2504 | for (j = 0; j < (int)(clock/2); j++) | |
2505 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave; | |
2506 | for (j = (int)(clock/2); j < clock; j++) | |
2507 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1; | |
2508 | ||
2509 | /* Keep track of how we start our wave and if we changed or not this time */ | |
2510 | wave ^= bit ^ lastbit; | |
2511 | lastbit = bit; | |
2512 | } | |
2513 | ||
2514 | RepaintGraphWindow(); | |
2515 | return 0; | |
2516 | } | |
2517 | ||
2518 | int CmdNorm(const char *Cmd) | |
2519 | { | |
2520 | int i; | |
2521 | int max = INT_MIN, min = INT_MAX; | |
2522 | ||
2523 | for (i = 10; i < GraphTraceLen; ++i) { | |
2524 | if (GraphBuffer[i] > max) | |
2525 | max = GraphBuffer[i]; | |
2526 | if (GraphBuffer[i] < min) | |
2527 | min = GraphBuffer[i]; | |
2528 | } | |
2529 | ||
2530 | if (max != min) { | |
2531 | for (i = 0; i < GraphTraceLen; ++i) { | |
2532 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 256 / | |
2533 | (max - min); | |
2534 | //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work | |
2535 | } | |
2536 | } | |
2537 | RepaintGraphWindow(); | |
2538 | return 0; | |
2539 | } | |
2540 | ||
2541 | int CmdPlot(const char *Cmd) | |
2542 | { | |
2543 | ShowGraphWindow(); | |
2544 | return 0; | |
2545 | } | |
2546 | ||
2547 | int CmdSave(const char *Cmd) | |
2548 | { | |
2549 | char filename[FILE_PATH_SIZE] = {0x00}; | |
2550 | int len = 0; | |
2551 | ||
2552 | len = strlen(Cmd); | |
2553 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
2554 | memcpy(filename, Cmd, len); | |
2555 | ||
2556 | ||
2557 | FILE *f = fopen(filename, "w"); | |
2558 | if(!f) { | |
2559 | PrintAndLog("couldn't open '%s'", filename); | |
2560 | return 0; | |
2561 | } | |
2562 | int i; | |
2563 | for (i = 0; i < GraphTraceLen; i++) { | |
2564 | fprintf(f, "%d\n", GraphBuffer[i]); | |
2565 | } | |
2566 | fclose(f); | |
2567 | PrintAndLog("saved to '%s'", Cmd); | |
2568 | return 0; | |
2569 | } | |
2570 | ||
2571 | int CmdScale(const char *Cmd) | |
2572 | { | |
2573 | CursorScaleFactor = atoi(Cmd); | |
2574 | if (CursorScaleFactor == 0) { | |
2575 | PrintAndLog("bad, can't have zero scale"); | |
2576 | CursorScaleFactor = 1; | |
2577 | } | |
2578 | RepaintGraphWindow(); | |
2579 | return 0; | |
2580 | } | |
2581 | ||
2582 | int CmdThreshold(const char *Cmd) | |
2583 | { | |
2584 | int threshold = atoi(Cmd); | |
2585 | ||
2586 | for (int i = 0; i < GraphTraceLen; ++i) { | |
2587 | if (GraphBuffer[i] >= threshold) | |
2588 | GraphBuffer[i] = 1; | |
2589 | else | |
2590 | GraphBuffer[i] = -1; | |
2591 | } | |
2592 | RepaintGraphWindow(); | |
2593 | return 0; | |
2594 | } | |
2595 | ||
2596 | int CmdDirectionalThreshold(const char *Cmd) | |
2597 | { | |
2598 | int8_t upThres = param_get8(Cmd, 0); | |
2599 | int8_t downThres = param_get8(Cmd, 1); | |
2600 | ||
2601 | printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); | |
2602 | ||
2603 | int lastValue = GraphBuffer[0]; | |
2604 | GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. | |
2605 | ||
2606 | for (int i = 1; i < GraphTraceLen; ++i) { | |
2607 | // Apply first threshold to samples heading up | |
2608 | if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue) | |
2609 | { | |
2610 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2611 | GraphBuffer[i] = 1; | |
2612 | } | |
2613 | // Apply second threshold to samples heading down | |
2614 | else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue) | |
2615 | { | |
2616 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2617 | GraphBuffer[i] = -1; | |
2618 | } | |
2619 | else | |
2620 | { | |
2621 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2622 | GraphBuffer[i] = GraphBuffer[i-1]; | |
2623 | ||
2624 | } | |
2625 | } | |
2626 | GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample. | |
2627 | RepaintGraphWindow(); | |
2628 | return 0; | |
2629 | } | |
2630 | ||
2631 | int CmdZerocrossings(const char *Cmd) | |
2632 | { | |
2633 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
2634 | CmdHpf(""); | |
2635 | ||
2636 | int sign = 1; | |
2637 | int zc = 0; | |
2638 | int lastZc = 0; | |
2639 | ||
2640 | for (int i = 0; i < GraphTraceLen; ++i) { | |
2641 | if (GraphBuffer[i] * sign >= 0) { | |
2642 | // No change in sign, reproduce the previous sample count. | |
2643 | zc++; | |
2644 | GraphBuffer[i] = lastZc; | |
2645 | } else { | |
2646 | // Change in sign, reset the sample count. | |
2647 | sign = -sign; | |
2648 | GraphBuffer[i] = lastZc; | |
2649 | if (sign > 0) { | |
2650 | lastZc = zc; | |
2651 | zc = 0; | |
2652 | } | |
2653 | } | |
2654 | } | |
2655 | ||
2656 | RepaintGraphWindow(); | |
2657 | return 0; | |
2658 | } | |
2659 | ||
2660 | static command_t CommandTable[] = | |
2661 | { | |
2662 | {"help", CmdHelp, 1, "This help"}, | |
2663 | {"amp", CmdAmp, 1, "Amplify peaks"}, | |
2664 | //{"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"}, | |
2665 | {"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ask demod using length of sample differences to detect the edge of a wave (default = 25)"}, | |
2666 | {"askem410xdemod",CmdAskEM410xDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"}, | |
2667 | {"askgproxiidemod",CmdG_Prox_II_Demod,1, "Demodulate a G Prox II tag from GraphBuffer"}, | |
2668 | //{"askmandemod", Cmdaskmandemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional)"}, | |
2669 | //{"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK tags and output bin (args optional)"}, | |
2670 | {"autocorr", CmdAutoCorr, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"}, | |
2671 | {"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] [invert<0|1>] Biphase decode bin stream in DemodBuffer (offset = 0|1 bits to shift the decode start)"}, | |
2672 | {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"}, | |
2673 | //{"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"}, | |
2674 | {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"}, | |
2675 | {"dec", CmdDec, 1, "Decimate samples"}, | |
2676 | {"detectclock", CmdDetectClockRate, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"}, | |
2677 | //{"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"}, | |
2678 | {"fskawiddemod", CmdFSKdemodAWID, 1, "Demodulate an AWID FSK tag from GraphBuffer"}, | |
2679 | //{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"}, | |
2680 | {"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate a HID FSK tag from GraphBuffer"}, | |
2681 | {"fskiodemod", CmdFSKdemodIO, 1, "Demodulate an IO Prox FSK tag from GraphBuffer"}, | |
2682 | {"fskpyramiddemod",CmdFSKdemodPyramid,1, "Demodulate a Pyramid FSK tag from GraphBuffer"}, | |
2683 | {"fskparadoxdemod",CmdFSKdemodParadox,1, "Demodulate a Paradox FSK tag from GraphBuffer"}, | |
2684 | //{"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to bin (clock = 50)(invert = 1|0)(rchigh = 10)(rclow=8)"}, | |
2685 | {"getbitstream", CmdGetBitStream, 1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"}, | |
2686 | {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"}, | |
2687 | {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"}, | |
2688 | {"hide", CmdHide, 1, "Hide graph window"}, | |
2689 | {"hpf", CmdHpf, 1, "Remove DC offset from trace"}, | |
2690 | {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"}, | |
2691 | {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"}, | |
2692 | {"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"}, | |
2693 | //{"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"}, | |
2694 | {"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream in DemodBuffer"}, | |
2695 | {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"}, | |
2696 | {"norm", CmdNorm, 1, "Normalize max/min to +/-128"}, | |
2697 | //{"nrzdetectclock",CmdDetectNRZClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"}, | |
2698 | //{"nrzrawdemod", CmdNRZrawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate nrz tags and output binary (args optional)"}, | |
2699 | {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"}, | |
2700 | //{"pskdetectclock",CmdDetectPSKClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"}, | |
2701 | {"printdemodbuffer",CmdPrintDemodBuff,1, "[x] -- print the data in the DemodBuffer - 'x' for hex output"}, | |
2702 | {"pskindalademod",CmdIndalaDecode, 1, "[clock] [invert<0|1>] -- Demodulate an indala tag (PSK1) from GraphBuffer (args optional)"}, | |
2703 | //{"psk1rawdemod", CmdPSK1rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk1 tags and output binary (args optional)"}, | |
2704 | //{"psk2rawdemod", CmdPSK2rawDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Attempt to demodulate psk2 tags and output binary (args optional)"}, | |
2705 | {"rawdemod", CmdRawDemod, 1, "[modulation] ... <options> -see help (h option) -- Demodulate the data in the GraphBuffer and output binary"}, | |
2706 | {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window (GraphBuffer)"}, | |
2707 | {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"}, | |
2708 | {"scale", CmdScale, 1, "<int> -- Set cursor display scale"}, | |
2709 | {"setdebugmode", CmdSetDebugMode, 1, "<0|1> -- Turn on or off Debugging Mode for demods"}, | |
2710 | {"shiftgraphzero",CmdGraphShiftZero, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"}, | |
2711 | //{"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"}, | |
2712 | {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."}, | |
2713 | {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"}, | |
2714 | {"undec", CmdUndec, 1, "Un-decimate samples by 2"}, | |
2715 | {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"}, | |
2716 | {NULL, NULL, 0, NULL} | |
2717 | }; | |
2718 | ||
2719 | int CmdData(const char *Cmd) | |
2720 | { | |
2721 | CmdsParse(CommandTable, Cmd); | |
2722 | return 0; | |
2723 | } | |
2724 | ||
2725 | int CmdHelp(const char *Cmd) | |
2726 | { | |
2727 | CmdsHelp(CommandTable); | |
2728 | return 0; | |
2729 | } |