]>
Commit | Line | Data |
---|---|---|
a553f267 | 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 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
902cb3c0 | 15 | #include "proxmark3.h" |
7fe9b0b7 | 16 | #include "data.h" |
17 | #include "ui.h" | |
18 | #include "graph.h" | |
19 | #include "cmdparser.h" | |
d51b2eda | 20 | #include "util.h" |
7fe9b0b7 | 21 | #include "cmdmain.h" |
22 | #include "cmddata.h" | |
7db5f1ca | 23 | #include "lfdemod.h" |
31abe49f MHS |
24 | #include "usb_cmd.h" |
25 | ||
4118b74d | 26 | uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; |
ec75f5c1 | 27 | uint8_t g_debugMode; |
4118b74d | 28 | int DemodBufferLen; |
7fe9b0b7 | 29 | static int CmdHelp(const char *Cmd); |
30 | ||
4118b74d | 31 | //set the demod buffer with given array of binary (one bit per byte) |
32 | //by marshmellow | |
ec75f5c1 | 33 | void setDemodBuf(uint8_t *buff, size_t size, size_t startIdx) |
4118b74d | 34 | { |
ec75f5c1 | 35 | size_t i = 0; |
36 | for (; i < size; i++){ | |
37 | DemodBuffer[i]=buff[startIdx++]; | |
ba1a299c | 38 | } |
39 | DemodBufferLen=size; | |
40 | return; | |
4118b74d | 41 | } |
42 | ||
ec75f5c1 | 43 | int CmdSetDebugMode(const char *Cmd) |
44 | { | |
45 | int demod=0; | |
46 | sscanf(Cmd, "%i", &demod); | |
47 | g_debugMode=(uint8_t)demod; | |
48 | return 1; | |
49 | } | |
50 | ||
4118b74d | 51 | //by marshmellow |
52 | void printDemodBuff() | |
53 | { | |
ba1a299c | 54 | uint32_t i = 0; |
55 | int bitLen = DemodBufferLen; | |
56 | if (bitLen<16) { | |
57 | PrintAndLog("no bits found in demod buffer"); | |
58 | return; | |
59 | } | |
60 | if (bitLen>512) bitLen=512; //max output to 512 bits if we have more - should be plenty | |
61 | for (i = 0; i <= (bitLen-16); i+=16) { | |
62 | PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i", | |
63 | DemodBuffer[i], | |
64 | DemodBuffer[i+1], | |
65 | DemodBuffer[i+2], | |
66 | DemodBuffer[i+3], | |
67 | DemodBuffer[i+4], | |
68 | DemodBuffer[i+5], | |
69 | DemodBuffer[i+6], | |
70 | DemodBuffer[i+7], | |
71 | DemodBuffer[i+8], | |
72 | DemodBuffer[i+9], | |
73 | DemodBuffer[i+10], | |
74 | DemodBuffer[i+11], | |
75 | DemodBuffer[i+12], | |
76 | DemodBuffer[i+13], | |
77 | DemodBuffer[i+14], | |
78 | DemodBuffer[i+15]); | |
79 | } | |
80 | return; | |
4118b74d | 81 | } |
82 | ||
83 | ||
7fe9b0b7 | 84 | int CmdAmp(const char *Cmd) |
85 | { | |
86 | int i, rising, falling; | |
87 | int max = INT_MIN, min = INT_MAX; | |
88 | ||
89 | for (i = 10; i < GraphTraceLen; ++i) { | |
90 | if (GraphBuffer[i] > max) | |
91 | max = GraphBuffer[i]; | |
92 | if (GraphBuffer[i] < min) | |
93 | min = GraphBuffer[i]; | |
94 | } | |
95 | ||
96 | if (max != min) { | |
97 | rising = falling= 0; | |
98 | for (i = 0; i < GraphTraceLen; ++i) { | |
99 | if (GraphBuffer[i + 1] < GraphBuffer[i]) { | |
100 | if (rising) { | |
101 | GraphBuffer[i] = max; | |
102 | rising = 0; | |
103 | } | |
104 | falling = 1; | |
105 | } | |
106 | if (GraphBuffer[i + 1] > GraphBuffer[i]) { | |
107 | if (falling) { | |
108 | GraphBuffer[i] = min; | |
109 | falling = 0; | |
110 | } | |
111 | rising= 1; | |
112 | } | |
113 | } | |
114 | } | |
115 | RepaintGraphWindow(); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | /* | |
120 | * Generic command to demodulate ASK. | |
121 | * | |
122 | * Argument is convention: positive or negative (High mod means zero | |
123 | * or high mod means one) | |
124 | * | |
125 | * Updates the Graph trace with 0/1 values | |
126 | * | |
127 | * Arguments: | |
128 | * c : 0 or 1 | |
129 | */ | |
2fc2150e | 130 | //this method is dependant on all highs and lows to be the same(or clipped) this creates issues[marshmellow] it also ignores the clock |
7fe9b0b7 | 131 | int Cmdaskdemod(const char *Cmd) |
132 | { | |
133 | int i; | |
134 | int c, high = 0, low = 0; | |
ba1a299c | 135 | |
7fe9b0b7 | 136 | // TODO: complain if we do not give 2 arguments here ! |
137 | // (AL - this doesn't make sense! we're only using one argument!!!) | |
138 | sscanf(Cmd, "%i", &c); | |
ba1a299c | 139 | |
7fe9b0b7 | 140 | /* Detect high and lows and clock */ |
ba1a299c | 141 | // (AL - clock???) |
7fe9b0b7 | 142 | for (i = 0; i < GraphTraceLen; ++i) |
143 | { | |
144 | if (GraphBuffer[i] > high) | |
145 | high = GraphBuffer[i]; | |
146 | else if (GraphBuffer[i] < low) | |
147 | low = GraphBuffer[i]; | |
148 | } | |
eb191de6 | 149 | high=abs(high*.75); |
150 | low=abs(low*.75); | |
7fe9b0b7 | 151 | if (c != 0 && c != 1) { |
152 | PrintAndLog("Invalid argument: %s", Cmd); | |
153 | return 0; | |
154 | } | |
b3b70669 | 155 | //prime loop |
7fe9b0b7 | 156 | if (GraphBuffer[0] > 0) { |
157 | GraphBuffer[0] = 1-c; | |
158 | } else { | |
159 | GraphBuffer[0] = c; | |
160 | } | |
161 | for (i = 1; i < GraphTraceLen; ++i) { | |
162 | /* Transitions are detected at each peak | |
163 | * Transitions are either: | |
164 | * - we're low: transition if we hit a high | |
165 | * - we're high: transition if we hit a low | |
166 | * (we need to do it this way because some tags keep high or | |
167 | * low for long periods, others just reach the peak and go | |
168 | * down) | |
169 | */ | |
ae2f73c1 | 170 | //[marhsmellow] change == to >= for high and <= for low for fuzz |
171 | if ((GraphBuffer[i] == high) && (GraphBuffer[i - 1] == c)) { | |
7fe9b0b7 | 172 | GraphBuffer[i] = 1 - c; |
ae2f73c1 | 173 | } else if ((GraphBuffer[i] == low) && (GraphBuffer[i - 1] == (1 - c))){ |
7fe9b0b7 | 174 | GraphBuffer[i] = c; |
175 | } else { | |
176 | /* No transition */ | |
177 | GraphBuffer[i] = GraphBuffer[i - 1]; | |
178 | } | |
179 | } | |
180 | RepaintGraphWindow(); | |
181 | return 0; | |
182 | } | |
183 | ||
d5a72d2f | 184 | //by marshmellow |
185 | void printBitStream(uint8_t BitStream[], uint32_t bitLen) | |
186 | { | |
2fc2150e | 187 | uint32_t i = 0; |
188 | if (bitLen<16) { | |
189 | PrintAndLog("Too few bits found: %d",bitLen); | |
190 | return; | |
191 | } | |
192 | if (bitLen>512) bitLen=512; | |
2df8c079 | 193 | for (i = 0; i <= (bitLen-16); i+=16) { |
2fc2150e | 194 | PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i", |
195 | BitStream[i], | |
196 | BitStream[i+1], | |
197 | BitStream[i+2], | |
198 | BitStream[i+3], | |
199 | BitStream[i+4], | |
200 | BitStream[i+5], | |
201 | BitStream[i+6], | |
202 | BitStream[i+7], | |
203 | BitStream[i+8], | |
204 | BitStream[i+9], | |
205 | BitStream[i+10], | |
206 | BitStream[i+11], | |
207 | BitStream[i+12], | |
208 | BitStream[i+13], | |
209 | BitStream[i+14], | |
210 | BitStream[i+15]); | |
211 | } | |
ba1a299c | 212 | return; |
2fc2150e | 213 | } |
d5a72d2f | 214 | //by marshmellow |
ba1a299c | 215 | //print EM410x ID in multiple formats |
eb191de6 | 216 | void printEM410x(uint64_t id) |
2fc2150e | 217 | { |
eb191de6 | 218 | if (id !=0){ |
0e74c023 | 219 | uint64_t iii=1; |
ec75f5c1 | 220 | uint64_t id2lo=0; |
eb191de6 | 221 | uint32_t ii=0; |
222 | uint32_t i=0; | |
0e74c023 | 223 | for (ii=5; ii>0;ii--){ |
2fc2150e | 224 | for (i=0;i<8;i++){ |
ba1a299c | 225 | id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8))); |
2fc2150e | 226 | } |
227 | } | |
0e74c023 | 228 | //output em id |
eb191de6 | 229 | PrintAndLog("EM TAG ID : %010llx", id); |
ec75f5c1 | 230 | PrintAndLog("Unique TAG ID: %010llx", id2lo); |
eb191de6 | 231 | PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF); |
232 | PrintAndLog("DEZ 10 : %010lld",id & 0xFFFFFF); | |
233 | PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF)); | |
234 | PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id>>32ll),(id & 0xFFFF)); | |
235 | PrintAndLog("DEZ 14/IK2 : %014lld",id); | |
0e74c023 | 236 | PrintAndLog("DEZ 15/IK3 : %015lld",id2lo); |
eb191de6 | 237 | PrintAndLog("Other : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF)); |
ba1a299c | 238 | } |
eb191de6 | 239 | return; |
240 | } | |
241 | ||
d5a72d2f | 242 | //by marshmellow |
ba1a299c | 243 | //take binary from demod buffer and see if we can find an EM410x ID |
eb191de6 | 244 | int CmdEm410xDecode(const char *Cmd) |
245 | { | |
246 | uint64_t id=0; | |
ec75f5c1 | 247 | size_t size = DemodBufferLen, idx=0; |
248 | id = Em410xDecode(DemodBuffer, &size, &idx); | |
249 | if (id>0){ | |
250 | setDemodBuf(DemodBuffer, size, idx); | |
251 | if (g_debugMode){ | |
252 | PrintAndLog("DEBUG: Printing demod buffer:"); | |
253 | printDemodBuff(); | |
254 | } | |
255 | printEM410x(id); | |
256 | return 1; | |
257 | } | |
2fc2150e | 258 | return 0; |
259 | } | |
260 | ||
f822a063 | 261 | |
e888ed8e | 262 | //by marshmellow |
eb191de6 | 263 | //takes 2 arguments - clock and invert both as integers |
ba1a299c | 264 | //attempts to demodulate ask while decoding manchester |
e888ed8e | 265 | //prints binary found and saves in graphbuffer for further commands |
9e6dd4eb | 266 | int Cmdaskmandemod(const char *Cmd) |
e888ed8e | 267 | { |
ec75f5c1 | 268 | int invert=0; |
ba1a299c | 269 | int clk=0; |
eb191de6 | 270 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
ba1a299c | 271 | sscanf(Cmd, "%i %i", &clk, &invert); |
e888ed8e | 272 | if (invert != 0 && invert != 1) { |
273 | PrintAndLog("Invalid argument: %s", Cmd); | |
274 | return 0; | |
275 | } | |
ba1a299c | 276 | |
277 | size_t BitLen = getFromGraphBuf(BitStream); | |
ec75f5c1 | 278 | if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen); |
eb191de6 | 279 | int errCnt=0; |
280 | errCnt = askmandemod(BitStream, &BitLen,&clk,&invert); | |
ba1a299c | 281 | if (errCnt<0||BitLen<16){ //if fatal error (or -1) |
ec75f5c1 | 282 | if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk); |
e888ed8e | 283 | return 0; |
ba1a299c | 284 | } |
f822a063 | 285 | PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen); |
ba1a299c | 286 | |
d5a72d2f | 287 | //output |
eb191de6 | 288 | if (errCnt>0){ |
289 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
e888ed8e | 290 | } |
eb191de6 | 291 | PrintAndLog("ASK/Manchester decoded bitstream:"); |
292 | // Now output the bitstream to the scrollback by line of 16 bits | |
ec75f5c1 | 293 | setDemodBuf(BitStream,BitLen,0); |
ba1a299c | 294 | printDemodBuff(); |
eb191de6 | 295 | uint64_t lo =0; |
ec75f5c1 | 296 | size_t idx=0; |
297 | lo = Em410xDecode(BitStream, &BitLen, &idx); | |
d5a72d2f | 298 | if (lo>0){ |
299 | //set GraphBuffer for clone or sim command | |
ec75f5c1 | 300 | setDemodBuf(BitStream, BitLen, idx); |
301 | if (g_debugMode){ | |
302 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); | |
303 | printDemodBuff(); | |
304 | } | |
d5a72d2f | 305 | PrintAndLog("EM410x pattern found: "); |
306 | printEM410x(lo); | |
ac914e56 | 307 | return 1; |
d5a72d2f | 308 | } |
eb191de6 | 309 | return 0; |
310 | } | |
311 | ||
312 | //by marshmellow | |
d5a72d2f | 313 | //manchester decode |
eb191de6 | 314 | //stricktly take 10 and 01 and convert to 0 and 1 |
315 | int Cmdmandecoderaw(const char *Cmd) | |
316 | { | |
317 | int i =0; | |
318 | int errCnt=0; | |
ba1a299c | 319 | size_t size=0; |
eb191de6 | 320 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
321 | int high=0,low=0; | |
ba1a299c | 322 | for (;i<DemodBufferLen;++i){ |
323 | if (DemodBuffer[i]>high) high=DemodBuffer[i]; | |
324 | else if(DemodBuffer[i]<low) low=DemodBuffer[i]; | |
325 | BitStream[i]=DemodBuffer[i]; | |
eb191de6 | 326 | } |
327 | if (high>1 || low <0 ){ | |
328 | PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode"); | |
329 | return 0; | |
330 | } | |
ba1a299c | 331 | size=i; |
332 | errCnt=manrawdecode(BitStream, &size); | |
2df8c079 | 333 | if (errCnt>=20){ |
334 | PrintAndLog("Too many errors: %d",errCnt); | |
335 | return 0; | |
336 | } | |
eb191de6 | 337 | PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt); |
ba1a299c | 338 | printBitStream(BitStream, size); |
eb191de6 | 339 | if (errCnt==0){ |
ba1a299c | 340 | uint64_t id = 0; |
ec75f5c1 | 341 | size_t idx=0; |
342 | id = Em410xDecode(BitStream, &size, &idx); | |
343 | if (id>0){ | |
344 | //need to adjust to set bitstream back to manchester encoded data | |
345 | //setDemodBuf(BitStream, size, idx); | |
346 | ||
347 | printEM410x(id); | |
348 | } | |
eb191de6 | 349 | } |
d5a72d2f | 350 | return 1; |
351 | } | |
352 | ||
353 | //by marshmellow | |
354 | //biphase decode | |
355 | //take 01 or 10 = 0 and 11 or 00 = 1 | |
1e090a61 | 356 | //takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit |
357 | // and "invert" default = 0 if 1 it will invert output | |
ba1a299c | 358 | // since it is not like manchester and doesn't have an incorrect bit pattern we |
d5a72d2f | 359 | // cannot determine if our decode is correct or if it should be shifted by one bit |
360 | // the argument offset allows us to manually shift if the output is incorrect | |
361 | // (better would be to demod and decode at the same time so we can distinguish large | |
362 | // width waves vs small width waves to help the decode positioning) or askbiphdemod | |
363 | int CmdBiphaseDecodeRaw(const char *Cmd) | |
364 | { | |
365 | int i = 0; | |
366 | int errCnt=0; | |
ba1a299c | 367 | size_t size=0; |
d5a72d2f | 368 | int offset=0; |
1e090a61 | 369 | int invert=0; |
d5a72d2f | 370 | int high=0, low=0; |
1e090a61 | 371 | sscanf(Cmd, "%i %i", &offset, &invert); |
d5a72d2f | 372 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
373 | //get graphbuffer & high and low | |
ba1a299c | 374 | for (;i<DemodBufferLen;++i){ |
375 | if(DemodBuffer[i]>high)high=DemodBuffer[i]; | |
376 | else if(DemodBuffer[i]<low)low=DemodBuffer[i]; | |
377 | BitStream[i]=DemodBuffer[i]; | |
d5a72d2f | 378 | } |
379 | if (high>1 || low <0){ | |
380 | PrintAndLog("Error: please raw demod the wave first then decode"); | |
381 | return 0; | |
382 | } | |
ba1a299c | 383 | size=i; |
1e090a61 | 384 | errCnt=BiphaseRawDecode(BitStream, &size, offset, invert); |
d5a72d2f | 385 | if (errCnt>=20){ |
386 | PrintAndLog("Too many errors attempting to decode: %d",errCnt); | |
387 | return 0; | |
388 | } | |
389 | PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt); | |
ba1a299c | 390 | printBitStream(BitStream, size); |
d5a72d2f | 391 | PrintAndLog("\nif bitstream does not look right try offset=1"); |
392 | return 1; | |
eb191de6 | 393 | } |
394 | ||
395 | //by marshmellow | |
396 | //takes 2 arguments - clock and invert both as integers | |
397 | //attempts to demodulate ask only | |
398 | //prints binary found and saves in graphbuffer for further commands | |
399 | int Cmdaskrawdemod(const char *Cmd) | |
400 | { | |
ba1a299c | 401 | int invert=0; |
402 | int clk=0; | |
eb191de6 | 403 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
ba1a299c | 404 | sscanf(Cmd, "%i %i", &clk, &invert); |
eb191de6 | 405 | if (invert != 0 && invert != 1) { |
406 | PrintAndLog("Invalid argument: %s", Cmd); | |
407 | return 0; | |
408 | } | |
ba1a299c | 409 | size_t BitLen = getFromGraphBuf(BitStream); |
eb191de6 | 410 | int errCnt=0; |
ba1a299c | 411 | errCnt = askrawdemod(BitStream, &BitLen,&clk,&invert); |
412 | if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) | |
413 | PrintAndLog("no data found"); | |
ec75f5c1 | 414 | if (g_debugMode==1) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert); |
eb191de6 | 415 | return 0; |
ba1a299c | 416 | } |
f822a063 | 417 | PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); |
ec75f5c1 | 418 | |
419 | //move BitStream back to DemodBuffer | |
420 | setDemodBuf(BitStream,BitLen,0); | |
ba1a299c | 421 | |
ec75f5c1 | 422 | //output |
eb191de6 | 423 | if (errCnt>0){ |
424 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
425 | } | |
426 | PrintAndLog("ASK demoded bitstream:"); | |
427 | // Now output the bitstream to the scrollback by line of 16 bits | |
428 | printBitStream(BitStream,BitLen); | |
952a8bb5 | 429 | |
f822a063 | 430 | return 1; |
e888ed8e | 431 | } |
432 | ||
7fe9b0b7 | 433 | int CmdAutoCorr(const char *Cmd) |
434 | { | |
435 | static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; | |
436 | ||
437 | int window = atoi(Cmd); | |
438 | ||
439 | if (window == 0) { | |
440 | PrintAndLog("needs a window"); | |
441 | return 0; | |
442 | } | |
443 | if (window >= GraphTraceLen) { | |
444 | PrintAndLog("window must be smaller than trace (%d samples)", | |
445 | GraphTraceLen); | |
446 | return 0; | |
447 | } | |
448 | ||
449 | PrintAndLog("performing %d correlations", GraphTraceLen - window); | |
450 | ||
451 | for (int i = 0; i < GraphTraceLen - window; ++i) { | |
452 | int sum = 0; | |
453 | for (int j = 0; j < window; ++j) { | |
454 | sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256; | |
455 | } | |
456 | CorrelBuffer[i] = sum; | |
457 | } | |
458 | GraphTraceLen = GraphTraceLen - window; | |
459 | memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int)); | |
460 | ||
461 | RepaintGraphWindow(); | |
462 | return 0; | |
463 | } | |
464 | ||
465 | int CmdBitsamples(const char *Cmd) | |
466 | { | |
467 | int cnt = 0; | |
90d74dc2 | 468 | uint8_t got[12288]; |
952a8bb5 | 469 | |
90d74dc2 | 470 | GetFromBigBuf(got,sizeof(got),0); |
471 | WaitForResponse(CMD_ACK,NULL); | |
7fe9b0b7 | 472 | |
90d74dc2 | 473 | for (int j = 0; j < sizeof(got); j++) { |
7fe9b0b7 | 474 | for (int k = 0; k < 8; k++) { |
90d74dc2 | 475 | if(got[j] & (1 << (7 - k))) { |
7fe9b0b7 | 476 | GraphBuffer[cnt++] = 1; |
477 | } else { | |
478 | GraphBuffer[cnt++] = 0; | |
479 | } | |
480 | } | |
7fe9b0b7 | 481 | } |
482 | GraphTraceLen = cnt; | |
483 | RepaintGraphWindow(); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | /* | |
488 | * Convert to a bitstream | |
489 | */ | |
490 | int CmdBitstream(const char *Cmd) | |
491 | { | |
492 | int i, j; | |
493 | int bit; | |
494 | int gtl; | |
495 | int clock; | |
496 | int low = 0; | |
497 | int high = 0; | |
498 | int hithigh, hitlow, first; | |
499 | ||
500 | /* Detect high and lows and clock */ | |
501 | for (i = 0; i < GraphTraceLen; ++i) | |
502 | { | |
503 | if (GraphBuffer[i] > high) | |
504 | high = GraphBuffer[i]; | |
505 | else if (GraphBuffer[i] < low) | |
506 | low = GraphBuffer[i]; | |
507 | } | |
508 | ||
509 | /* Get our clock */ | |
510 | clock = GetClock(Cmd, high, 1); | |
511 | gtl = ClearGraph(0); | |
512 | ||
513 | bit = 0; | |
514 | for (i = 0; i < (int)(gtl / clock); ++i) | |
515 | { | |
516 | hithigh = 0; | |
517 | hitlow = 0; | |
518 | first = 1; | |
519 | /* Find out if we hit both high and low peaks */ | |
520 | for (j = 0; j < clock; ++j) | |
521 | { | |
522 | if (GraphBuffer[(i * clock) + j] == high) | |
523 | hithigh = 1; | |
524 | else if (GraphBuffer[(i * clock) + j] == low) | |
525 | hitlow = 1; | |
526 | /* it doesn't count if it's the first part of our read | |
527 | because it's really just trailing from the last sequence */ | |
528 | if (first && (hithigh || hitlow)) | |
529 | hithigh = hitlow = 0; | |
530 | else | |
531 | first = 0; | |
532 | ||
533 | if (hithigh && hitlow) | |
534 | break; | |
535 | } | |
536 | ||
537 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
538 | if (!hithigh || !hitlow) | |
539 | bit ^= 1; | |
540 | ||
541 | AppendGraph(0, clock, bit); | |
7fe9b0b7 | 542 | } |
ba1a299c | 543 | |
7fe9b0b7 | 544 | RepaintGraphWindow(); |
545 | return 0; | |
546 | } | |
547 | ||
548 | int CmdBuffClear(const char *Cmd) | |
549 | { | |
550 | UsbCommand c = {CMD_BUFF_CLEAR}; | |
551 | SendCommand(&c); | |
552 | ClearGraph(true); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | int CmdDec(const char *Cmd) | |
557 | { | |
558 | for (int i = 0; i < (GraphTraceLen / 2); ++i) | |
559 | GraphBuffer[i] = GraphBuffer[i * 2]; | |
560 | GraphTraceLen /= 2; | |
561 | PrintAndLog("decimated by 2"); | |
562 | RepaintGraphWindow(); | |
563 | return 0; | |
564 | } | |
698b649e MHS |
565 | /** |
566 | * Undecimate - I'd call it 'interpolate', but we'll save that | |
567 | * name until someone does an actual interpolation command, not just | |
568 | * blindly repeating samples | |
569 | * @param Cmd | |
570 | * @return | |
571 | */ | |
572 | int CmdUndec(const char *Cmd) | |
573 | { | |
c856ceae MHS |
574 | if(param_getchar(Cmd, 0) == 'h') |
575 | { | |
576 | PrintAndLog("Usage: data undec [factor]"); | |
577 | PrintAndLog("This function performs un-decimation, by repeating each sample N times"); | |
578 | PrintAndLog("Options: "); | |
579 | PrintAndLog(" h This help"); | |
580 | PrintAndLog(" factor The number of times to repeat each sample.[default:2]"); | |
581 | PrintAndLog("Example: 'data undec 3'"); | |
582 | return 0; | |
583 | } | |
584 | ||
585 | uint8_t factor = param_get8ex(Cmd, 0,2, 10); | |
698b649e MHS |
586 | //We have memory, don't we? |
587 | int swap[MAX_GRAPH_TRACE_LEN] = { 0 }; | |
c856ceae MHS |
588 | uint32_t g_index = 0 ,s_index = 0; |
589 | while(g_index < GraphTraceLen && s_index < MAX_GRAPH_TRACE_LEN) | |
698b649e | 590 | { |
c856ceae MHS |
591 | int count = 0; |
592 | for(count = 0; count < factor && s_index+count < MAX_GRAPH_TRACE_LEN; count ++) | |
593 | swap[s_index+count] = GraphBuffer[g_index]; | |
594 | s_index+=count; | |
698b649e | 595 | } |
698b649e | 596 | |
c856ceae MHS |
597 | memcpy(GraphBuffer,swap, s_index * sizeof(int)); |
598 | GraphTraceLen = s_index; | |
599 | RepaintGraphWindow(); | |
698b649e MHS |
600 | return 0; |
601 | } | |
7fe9b0b7 | 602 | |
ec75f5c1 | 603 | //by marshmellow |
604 | //shift graph zero up or down based on input + or - | |
605 | int CmdGraphShiftZero(const char *Cmd) | |
606 | { | |
607 | ||
608 | int shift=0; | |
609 | //set options from parameters entered with the command | |
610 | sscanf(Cmd, "%i", &shift); | |
611 | int shiftedVal=0; | |
612 | for(int i = 0; i<GraphTraceLen; i++){ | |
613 | shiftedVal=GraphBuffer[i]+shift; | |
614 | if (shiftedVal>127) | |
615 | shiftedVal=127; | |
616 | else if (shiftedVal<-127) | |
617 | shiftedVal=-127; | |
618 | GraphBuffer[i]= shiftedVal; | |
619 | } | |
620 | CmdNorm(""); | |
621 | return 0; | |
622 | } | |
623 | ||
7fe9b0b7 | 624 | /* Print our clock rate */ |
ba1a299c | 625 | // uses data from graphbuffer |
7fe9b0b7 | 626 | int CmdDetectClockRate(const char *Cmd) |
627 | { | |
d5a72d2f | 628 | GetClock("",0,0); |
ba1a299c | 629 | //int clock = DetectASKClock(0); |
630 | //PrintAndLog("Auto-detected clock rate: %d", clock); | |
7fe9b0b7 | 631 | return 0; |
632 | } | |
66707a3b | 633 | |
2fc2150e | 634 | //by marshmellow |
eb191de6 | 635 | //fsk raw demod and print binary |
f822a063 | 636 | //takes 4 arguments - Clock, invert, rchigh, rclow |
637 | //defaults: clock = 50, invert=0, rchigh=10, rclow=8 (RF/10 RF/8 (fsk2a)) | |
e888ed8e | 638 | int CmdFSKrawdemod(const char *Cmd) |
b3b70669 | 639 | { |
eb191de6 | 640 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave |
b3b70669 | 641 | //set defaults |
03e6bb4a | 642 | int rfLen = 0; |
f822a063 | 643 | int invert=0; |
03e6bb4a | 644 | int fchigh=0; |
645 | int fclow=0; | |
b3b70669 | 646 | //set options from parameters entered with the command |
ba1a299c | 647 | sscanf(Cmd, "%i %i %i %i", &rfLen, &invert, &fchigh, &fclow); |
648 | ||
b3b70669 | 649 | if (strlen(Cmd)>0 && strlen(Cmd)<=2) { |
b3b70669 | 650 | if (rfLen==1){ |
651 | invert=1; //if invert option only is used | |
03e6bb4a | 652 | rfLen = 0; |
653 | } | |
ba1a299c | 654 | } |
03e6bb4a | 655 | |
eb191de6 | 656 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
ba1a299c | 657 | size_t BitLen = getFromGraphBuf(BitStream); |
03e6bb4a | 658 | //get field clock lengths |
659 | uint16_t fcs=0; | |
660 | if (fchigh==0 || fclow == 0){ | |
661 | fcs=countFC(BitStream, BitLen); | |
662 | if (fcs==0){ | |
663 | fchigh=10; | |
664 | fclow=8; | |
665 | }else{ | |
666 | fchigh = (fcs >> 8) & 0xFF; | |
667 | fclow = fcs & 0xFF; | |
668 | } | |
669 | } | |
670 | //get bit clock length | |
671 | if (rfLen==0){ | |
672 | rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow); | |
673 | if (rfLen == 0) rfLen = 50; | |
674 | } | |
675 | PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow); | |
ba1a299c | 676 | int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow); |
d5a72d2f | 677 | if (size>0){ |
678 | PrintAndLog("FSK decoded bitstream:"); | |
ec75f5c1 | 679 | setDemodBuf(BitStream,size,0); |
ba1a299c | 680 | |
d5a72d2f | 681 | // Now output the bitstream to the scrollback by line of 16 bits |
682 | 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 | |
683 | printBitStream(BitStream,size); | |
684 | } else{ | |
685 | PrintAndLog("no FSK data found"); | |
eb191de6 | 686 | } |
b3b70669 | 687 | return 0; |
688 | } | |
689 | ||
eb191de6 | 690 | //by marshmellow (based on existing demod + holiman's refactor) |
691 | //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded) | |
692 | //print full HID Prox ID and some bit format details if found | |
b3b70669 | 693 | int CmdFSKdemodHID(const char *Cmd) |
694 | { | |
695 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
b3b70669 | 696 | uint32_t hi2=0, hi=0, lo=0; |
ba1a299c | 697 | |
eb191de6 | 698 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
ba1a299c | 699 | size_t BitLen = getFromGraphBuf(BitStream); |
b3b70669 | 700 | //get binary from fsk wave |
a1d17964 | 701 | int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo); |
ec75f5c1 | 702 | if (idx<0){ |
a1d17964 | 703 | if (g_debugMode){ |
704 | if (idx==-1){ | |
705 | PrintAndLog("DEBUG: Just Noise Detected"); | |
706 | } else if (idx == -2) { | |
707 | PrintAndLog("DEBUG: Error demoding fsk"); | |
708 | } else if (idx == -3) { | |
709 | PrintAndLog("DEBUG: Preamble not found"); | |
710 | } else if (idx == -4) { | |
711 | PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen); | |
712 | } else { | |
713 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
714 | } | |
715 | } | |
ec75f5c1 | 716 | return 0; |
717 | } | |
718 | if (hi2==0 && hi==0 && lo==0) { | |
719 | if (g_debugMode) PrintAndLog("DEBUG: Error - no values found"); | |
eb191de6 | 720 | return 0; |
721 | } | |
722 | if (hi2 != 0){ //extra large HID tags | |
ba1a299c | 723 | PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)", |
eb191de6 | 724 | (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF); |
725 | } | |
726 | else { //standard HID tags <38 bits | |
d5a72d2f | 727 | uint8_t fmtLen = 0; |
eb191de6 | 728 | uint32_t fc = 0; |
729 | uint32_t cardnum = 0; | |
730 | if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used | |
731 | uint32_t lo2=0; | |
84871873 | 732 | lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit |
eb191de6 | 733 | uint8_t idx3 = 1; |
734 | while(lo2>1){ //find last bit set to 1 (format len bit) | |
735 | lo2=lo2>>1; | |
736 | idx3++; | |
b3b70669 | 737 | } |
ba1a299c | 738 | fmtLen =idx3+19; |
eb191de6 | 739 | fc =0; |
740 | cardnum=0; | |
d5a72d2f | 741 | if(fmtLen==26){ |
eb191de6 | 742 | cardnum = (lo>>1)&0xFFFF; |
743 | fc = (lo>>17)&0xFF; | |
744 | } | |
d5a72d2f | 745 | if(fmtLen==34){ |
eb191de6 | 746 | cardnum = (lo>>1)&0xFFFF; |
747 | fc= ((hi&1)<<15)|(lo>>17); | |
748 | } | |
d5a72d2f | 749 | if(fmtLen==35){ |
eb191de6 | 750 | cardnum = (lo>>1)&0xFFFFF; |
751 | fc = ((hi&1)<<11)|(lo>>21); | |
b3b70669 | 752 | } |
b3b70669 | 753 | } |
eb191de6 | 754 | else { //if bit 38 is not set then 37 bit format is used |
84871873 | 755 | fmtLen = 37; |
756 | fc = 0; | |
757 | cardnum = 0; | |
758 | if(fmtLen == 37){ | |
eb191de6 | 759 | cardnum = (lo>>1)&0x7FFFF; |
760 | fc = ((hi&0xF)<<12)|(lo>>20); | |
761 | } | |
ba1a299c | 762 | } |
763 | PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d", | |
eb191de6 | 764 | (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF, |
d5a72d2f | 765 | (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum); |
b3b70669 | 766 | } |
ec75f5c1 | 767 | setDemodBuf(BitStream,BitLen,idx); |
768 | if (g_debugMode){ | |
769 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); | |
770 | printDemodBuff(); | |
771 | } | |
772 | return 1; | |
b3b70669 | 773 | } |
774 | ||
04d2721b | 775 | //by marshmellow |
776 | //Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded) | |
ec75f5c1 | 777 | //print full Paradox Prox ID and some bit format details if found |
778 | int CmdFSKdemodParadox(const char *Cmd) | |
779 | { | |
780 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
781 | uint32_t hi2=0, hi=0, lo=0; | |
782 | ||
783 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
784 | size_t BitLen = getFromGraphBuf(BitStream); | |
785 | //get binary from fsk wave | |
a1d17964 | 786 | int idx = ParadoxdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo); |
ec75f5c1 | 787 | if (idx<0){ |
a1d17964 | 788 | if (g_debugMode){ |
789 | if (idx==-1){ | |
790 | PrintAndLog("DEBUG: Just Noise Detected"); | |
791 | } else if (idx == -2) { | |
792 | PrintAndLog("DEBUG: Error demoding fsk"); | |
793 | } else if (idx == -3) { | |
794 | PrintAndLog("DEBUG: Preamble not found"); | |
795 | } else if (idx == -4) { | |
796 | PrintAndLog("DEBUG: Error in Manchester data"); | |
797 | } else { | |
798 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
799 | } | |
800 | } | |
ec75f5c1 | 801 | return 0; |
802 | } | |
803 | if (hi2==0 && hi==0 && lo==0){ | |
804 | if (g_debugMode) PrintAndLog("DEBUG: Error - no value found"); | |
805 | return 0; | |
806 | } | |
807 | uint32_t fc = ((hi & 0x3)<<6) | (lo>>26); | |
808 | uint32_t cardnum = (lo>>10)&0xFFFF; | |
809 | ||
810 | PrintAndLog("Paradox TAG ID: %x%08x - FC: %d - Card: %d - Checksum: %02x", | |
811 | hi>>10, (hi & 0x3)<<26 | (lo>>10), fc, cardnum, (lo>>2) & 0xFF ); | |
812 | setDemodBuf(BitStream,BitLen,idx); | |
813 | if (g_debugMode){ | |
814 | PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx, BitLen); | |
815 | printDemodBuff(); | |
816 | } | |
817 | return 1; | |
b3b70669 | 818 | } |
819 | ||
ec75f5c1 | 820 | |
2fc2150e | 821 | //by marshmellow |
eb191de6 | 822 | //IO-Prox demod - FSK RF/64 with preamble of 000000001 |
823 | //print ioprox ID and some format details | |
b3b70669 | 824 | int CmdFSKdemodIO(const char *Cmd) |
825 | { | |
826 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
827 | //set defaults | |
ba1a299c | 828 | int idx=0; |
ec75f5c1 | 829 | //something in graphbuffer? |
830 | if (GraphTraceLen < 65) { | |
831 | if (g_debugMode)PrintAndLog("DEBUG: not enough samples in GraphBuffer"); | |
832 | return 0; | |
833 | } | |
eb191de6 | 834 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; |
ba1a299c | 835 | size_t BitLen = getFromGraphBuf(BitStream); |
ec75f5c1 | 836 | |
eb191de6 | 837 | //get binary from fsk wave |
ba1a299c | 838 | idx = IOdemodFSK(BitStream,BitLen); |
eb191de6 | 839 | if (idx<0){ |
a1d17964 | 840 | if (g_debugMode){ |
841 | if (idx==-1){ | |
842 | PrintAndLog("DEBUG: Just Noise Detected"); | |
843 | } else if (idx == -2) { | |
844 | PrintAndLog("DEBUG: not enough samples"); | |
845 | } else if (idx == -3) { | |
846 | PrintAndLog("DEBUG: error during fskdemod"); | |
847 | } else if (idx == -4) { | |
848 | PrintAndLog("DEBUG: Preamble not found"); | |
849 | } else if (idx == -5) { | |
850 | PrintAndLog("DEBUG: Separator bits not found"); | |
851 | } else { | |
852 | PrintAndLog("DEBUG: Error demoding fsk %d", idx); | |
853 | } | |
854 | } | |
eb191de6 | 855 | return 0; |
856 | } | |
857 | if (idx==0){ | |
ec75f5c1 | 858 | if (g_debugMode==1){ |
859 | PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen); | |
860 | if (BitLen > 92) printBitStream(BitStream,92); | |
861 | } | |
d5a72d2f | 862 | return 0; |
b3b70669 | 863 | } |
b3b70669 | 864 | //Index map |
865 | //0 10 20 30 40 50 60 | |
866 | //| | | | | | | | |
867 | //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23 | |
868 | //----------------------------------------------------------------------------- | |
869 | //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11 | |
870 | // | |
871 | //XSF(version)facility:codeone+codetwo (raw) | |
872 | //Handle the data | |
ec75f5c1 | 873 | if (idx+64>BitLen) { |
874 | if (g_debugMode==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen); | |
875 | return 0; | |
876 | } | |
eb191de6 | 877 | 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]); |
ba1a299c | 878 | 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]); |
f822a063 | 879 | 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]); |
880 | 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]); | |
881 | 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]); | |
882 | 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]); | |
883 | 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]); | |
ba1a299c | 884 | |
eb191de6 | 885 | uint32_t code = bytebits_to_byte(BitStream+idx,32); |
ba1a299c | 886 | uint32_t code2 = bytebits_to_byte(BitStream+idx+32,32); |
f822a063 | 887 | uint8_t version = bytebits_to_byte(BitStream+idx+27,8); //14,4 |
888 | uint8_t facilitycode = bytebits_to_byte(BitStream+idx+18,8) ; | |
eb191de6 | 889 | uint16_t number = (bytebits_to_byte(BitStream+idx+36,8)<<8)|(bytebits_to_byte(BitStream+idx+45,8)); //36,9 |
ba1a299c | 890 | PrintAndLog("IO Prox XSF(%02d)%02x:%05d (%08x%08x)",version,facilitycode,number,code,code2); |
ec75f5c1 | 891 | setDemodBuf(BitStream,64,idx); |
892 | if (g_debugMode){ | |
893 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx,64); | |
894 | printDemodBuff(); | |
895 | } | |
896 | return 1; | |
b3b70669 | 897 | } |
1e090a61 | 898 | |
899 | ||
900 | //by marshmellow | |
901 | //AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream) | |
902 | //print full AWID Prox ID and some bit format details if found | |
903 | int CmdFSKdemodAWID(const char *Cmd) | |
904 | { | |
905 | ||
ec75f5c1 | 906 | //int verbose=1; |
907 | //sscanf(Cmd, "%i", &verbose); | |
1e090a61 | 908 | |
909 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave | |
910 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
911 | size_t size = getFromGraphBuf(BitStream); | |
912 | ||
913 | //get binary from fsk wave | |
a1d17964 | 914 | int idx = AWIDdemodFSK(BitStream, &size); |
1e090a61 | 915 | if (idx<=0){ |
ec75f5c1 | 916 | if (g_debugMode==1){ |
1e090a61 | 917 | if (idx == -1) |
ec75f5c1 | 918 | PrintAndLog("DEBUG: Error - not enough samples"); |
1e090a61 | 919 | else if (idx == -2) |
a1d17964 | 920 | PrintAndLog("DEBUG: Error - only noise found"); |
1e090a61 | 921 | else if (idx == -3) |
ec75f5c1 | 922 | PrintAndLog("DEBUG: Error - problem during FSK demod"); |
1e090a61 | 923 | // else if (idx == -3) |
924 | // PrintAndLog("Error: thought we had a tag but the parity failed"); | |
925 | else if (idx == -4) | |
ec75f5c1 | 926 | PrintAndLog("DEBUG: Error - AWID preamble not found"); |
927 | else if (idx == -5) | |
a1d17964 | 928 | PrintAndLog("DEBUG: Error - Size not correct: %d", size); |
ec75f5c1 | 929 | else |
930 | PrintAndLog("DEBUG: Error %d",idx); | |
1e090a61 | 931 | } |
932 | return 0; | |
933 | } | |
ba1a299c | 934 | |
1e090a61 | 935 | // Index map |
936 | // 0 10 20 30 40 50 60 | |
937 | // | | | | | | | | |
938 | // 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 | |
939 | // ----------------------------------------------------------------------------- | |
940 | // 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 | |
941 | // 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 | |
942 | // |---26 bit---| |-----117----||-------------142-------------| | |
943 | // b = format bit len, o = odd parity of last 3 bits | |
944 | // f = facility code, c = card number | |
945 | // w = wiegand parity | |
946 | // (26 bit format shown) | |
947 | ||
948 | //get raw ID before removing parities | |
949 | uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32); | |
950 | uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32); | |
951 | uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32); | |
ec75f5c1 | 952 | setDemodBuf(BitStream,96,idx); |
953 | ||
1e090a61 | 954 | size = removeParity(BitStream, idx+8, 4, 1, 88); |
955 | if (size != 66){ | |
ec75f5c1 | 956 | if (g_debugMode==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format"); |
1e090a61 | 957 | return 0; |
958 | } | |
959 | // ok valid card found! | |
960 | ||
961 | // Index map | |
962 | // 0 10 20 30 40 50 60 | |
963 | // | | | | | | | | |
964 | // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456 | |
965 | // ----------------------------------------------------------------------------- | |
966 | // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000 | |
967 | // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
968 | // |26 bit| |-117--| |-----142------| | |
969 | // b = format bit len, o = odd parity of last 3 bits | |
970 | // f = facility code, c = card number | |
971 | // w = wiegand parity | |
972 | // (26 bit format shown) | |
973 | ||
974 | uint32_t fc = 0; | |
975 | uint32_t cardnum = 0; | |
976 | uint32_t code1 = 0; | |
977 | uint32_t code2 = 0; | |
978 | uint8_t fmtLen = bytebits_to_byte(BitStream,8); | |
979 | if (fmtLen==26){ | |
980 | fc = bytebits_to_byte(BitStream+9, 8); | |
981 | cardnum = bytebits_to_byte(BitStream+17, 16); | |
982 | code1 = bytebits_to_byte(BitStream+8,fmtLen); | |
983 | PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo); | |
984 | } else { | |
985 | cardnum = bytebits_to_byte(BitStream+8+(fmtLen-17), 16); | |
986 | if (fmtLen>32){ | |
987 | code1 = bytebits_to_byte(BitStream+8,fmtLen-32); | |
988 | code2 = bytebits_to_byte(BitStream+8+(fmtLen-32),32); | |
989 | PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %x%08x%08x", fmtLen, cardnum, code1, code2, rawHi2, rawHi, rawLo); | |
990 | } else{ | |
991 | code1 = bytebits_to_byte(BitStream+8,fmtLen); | |
992 | PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %x%08x%08x", fmtLen, cardnum, code1, rawHi2, rawHi, rawLo); | |
993 | } | |
994 | } | |
ec75f5c1 | 995 | if (g_debugMode){ |
996 | PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx, 96); | |
997 | printDemodBuff(); | |
998 | } | |
1e090a61 | 999 | //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands |
d5a72d2f | 1000 | return 1; |
b3b70669 | 1001 | } |
1e090a61 | 1002 | |
1003 | //by marshmellow | |
1004 | //Pyramid Prox demod - FSK RF/50 with preamble of 0000000000000001 (always a 128 bit data stream) | |
1005 | //print full Farpointe Data/Pyramid Prox ID and some bit format details if found | |
1006 | int CmdFSKdemodPyramid(const char *Cmd) | |
1007 | { | |
1e090a61 | 1008 | //raw fsk demod no manchester decoding no start bit finding just get binary from wave |
1009 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1010 | size_t size = getFromGraphBuf(BitStream); | |
1011 | ||
1012 | //get binary from fsk wave | |
a1d17964 | 1013 | int idx = PyramiddemodFSK(BitStream, &size); |
1e090a61 | 1014 | if (idx < 0){ |
ec75f5c1 | 1015 | if (g_debugMode==1){ |
1e090a61 | 1016 | if (idx == -5) |
ec75f5c1 | 1017 | PrintAndLog("DEBUG: Error - not enough samples"); |
1e090a61 | 1018 | else if (idx == -1) |
a1d17964 | 1019 | PrintAndLog("DEBUG: Error - only noise found"); |
1e090a61 | 1020 | else if (idx == -2) |
ec75f5c1 | 1021 | PrintAndLog("DEBUG: Error - problem during FSK demod"); |
1022 | else if (idx == -3) | |
a1d17964 | 1023 | PrintAndLog("DEBUG: Error - Size not correct: %d", size); |
1e090a61 | 1024 | else if (idx == -4) |
ec75f5c1 | 1025 | PrintAndLog("DEBUG: Error - Pyramid preamble not found"); |
1026 | else | |
1027 | PrintAndLog("DEBUG: Error - idx: %d",idx); | |
1e090a61 | 1028 | } |
1e090a61 | 1029 | return 0; |
1030 | } | |
1e090a61 | 1031 | // Index map |
1032 | // 0 10 20 30 40 50 60 | |
1033 | // | | | | | | | | |
1034 | // 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 | |
1035 | // ----------------------------------------------------------------------------- | |
1036 | // 0000000 0 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 | |
1037 | // premable xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o | |
1038 | ||
1039 | // 64 70 80 90 100 110 120 | |
1040 | // | | | | | | | | |
1041 | // 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 | |
1042 | // ----------------------------------------------------------------------------- | |
1043 | // 0000000 1 0000000 1 0000000 1 0110111 0 0011000 1 0000001 0 0001100 1 1001010 0 | |
1044 | // xxxxxxx o xxxxxxx o xxxxxxx o xswffff o ffffccc o ccccccc o ccccccw o ppppppp o | |
1045 | // |---115---||---------71---------| | |
1046 | // s = format start bit, o = odd parity of last 7 bits | |
1047 | // f = facility code, c = card number | |
1048 | // w = wiegand parity, x = extra space for other formats | |
1049 | // p = unknown checksum | |
1050 | // (26 bit format shown) | |
1051 | ||
1052 | //get raw ID before removing parities | |
1053 | uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32); | |
1054 | uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32); | |
1055 | uint32_t rawHi2 = bytebits_to_byte(BitStream+idx+32,32); | |
1056 | uint32_t rawHi3 = bytebits_to_byte(BitStream+idx,32); | |
ec75f5c1 | 1057 | setDemodBuf(BitStream,128,idx); |
1058 | ||
1e090a61 | 1059 | size = removeParity(BitStream, idx+8, 8, 1, 120); |
1060 | if (size != 105){ | |
ec75f5c1 | 1061 | if (g_debugMode==1) PrintAndLog("DEBUG: Error at parity check-tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3); |
1e090a61 | 1062 | return 0; |
1063 | } | |
1064 | ||
1065 | // ok valid card found! | |
1066 | ||
1067 | // Index map | |
1068 | // 0 10 20 30 40 50 60 70 | |
1069 | // | | | | | | | | | |
1070 | // 01234567890123456789012345678901234567890123456789012345678901234567890 | |
1071 | // ----------------------------------------------------------------------- | |
1072 | // 00000000000000000000000000000000000000000000000000000000000000000000000 | |
1073 | // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
1074 | ||
1075 | // 71 80 90 100 | |
1076 | // | | | | | |
1077 | // 1 2 34567890 1234567890123456 7 8901234 | |
1078 | // --------------------------------------- | |
1079 | // 1 1 01110011 0000000001000110 0 1001010 | |
1080 | // s w ffffffff cccccccccccccccc w ppppppp | |
1081 | // |--115-| |------71------| | |
1082 | // s = format start bit, o = odd parity of last 7 bits | |
1083 | // f = facility code, c = card number | |
1084 | // w = wiegand parity, x = extra space for other formats | |
1085 | // p = unknown checksum | |
1086 | // (26 bit format shown) | |
1087 | ||
1088 | //find start bit to get fmtLen | |
1e090a61 | 1089 | int j; |
1090 | for (j=0; j<size; j++){ | |
ec75f5c1 | 1091 | if(BitStream[j]) break; |
1e090a61 | 1092 | } |
1093 | uint8_t fmtLen = size-j-8; | |
1094 | uint32_t fc = 0; | |
1095 | uint32_t cardnum = 0; | |
1096 | uint32_t code1 = 0; | |
1097 | //uint32_t code2 = 0; | |
1098 | if (fmtLen==26){ | |
1099 | fc = bytebits_to_byte(BitStream+73, 8); | |
1100 | cardnum = bytebits_to_byte(BitStream+81, 16); | |
1101 | code1 = bytebits_to_byte(BitStream+72,fmtLen); | |
ec75f5c1 | 1102 | PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %x%08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi3, rawHi2, rawHi, rawLo); |
1e090a61 | 1103 | } else if (fmtLen==45){ |
1104 | fmtLen=42; //end = 10 bits not 7 like 26 bit fmt | |
1105 | fc = bytebits_to_byte(BitStream+53, 10); | |
1106 | cardnum = bytebits_to_byte(BitStream+63, 32); | |
ec75f5c1 | 1107 | PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Raw: %x%08x%08x%08x", fmtLen, fc, cardnum, rawHi3, rawHi2, rawHi, rawLo); |
1e090a61 | 1108 | } else { |
1109 | cardnum = bytebits_to_byte(BitStream+81, 16); | |
1110 | if (fmtLen>32){ | |
1111 | //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen-32); | |
1112 | //code2 = bytebits_to_byte(BitStream+(size-32),32); | |
ec75f5c1 | 1113 | PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo); |
1e090a61 | 1114 | } else{ |
1115 | //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen); | |
ec75f5c1 | 1116 | PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo); |
1e090a61 | 1117 | } |
1118 | } | |
ec75f5c1 | 1119 | if (g_debugMode){ |
1120 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128); | |
1121 | printDemodBuff(); | |
1122 | } | |
1e090a61 | 1123 | return 1; |
1124 | } | |
1125 | ||
e888ed8e | 1126 | int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating |
7fe9b0b7 | 1127 | { |
1128 | static const int LowTone[] = { | |
1129 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1130 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1131 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1132 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1133 | 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 | |
1134 | }; | |
1135 | static const int HighTone[] = { | |
1136 | 1, 1, 1, 1, 1, -1, -1, -1, -1, | |
1137 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1138 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1139 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1140 | 1, 1, 1, 1, -1, -1, -1, -1, | |
1141 | 1, 1, 1, 1, -1, -1, -1, -1, -1, | |
1142 | }; | |
1143 | ||
1144 | int lowLen = sizeof (LowTone) / sizeof (int); | |
1145 | int highLen = sizeof (HighTone) / sizeof (int); | |
b915fda3 | 1146 | int convLen = (highLen > lowLen) ? highLen : lowLen; |
7fe9b0b7 | 1147 | uint32_t hi = 0, lo = 0; |
1148 | ||
1149 | int i, j; | |
1150 | int minMark = 0, maxMark = 0; | |
952a8bb5 | 1151 | |
7fe9b0b7 | 1152 | for (i = 0; i < GraphTraceLen - convLen; ++i) { |
1153 | int lowSum = 0, highSum = 0; | |
1154 | ||
1155 | for (j = 0; j < lowLen; ++j) { | |
1156 | lowSum += LowTone[j]*GraphBuffer[i+j]; | |
1157 | } | |
1158 | for (j = 0; j < highLen; ++j) { | |
1159 | highSum += HighTone[j] * GraphBuffer[i + j]; | |
1160 | } | |
1161 | lowSum = abs(100 * lowSum / lowLen); | |
1162 | highSum = abs(100 * highSum / highLen); | |
1163 | GraphBuffer[i] = (highSum << 16) | lowSum; | |
1164 | } | |
1165 | ||
1166 | for(i = 0; i < GraphTraceLen - convLen - 16; ++i) { | |
1167 | int lowTot = 0, highTot = 0; | |
1168 | // 10 and 8 are f_s divided by f_l and f_h, rounded | |
1169 | for (j = 0; j < 10; ++j) { | |
1170 | lowTot += (GraphBuffer[i+j] & 0xffff); | |
1171 | } | |
1172 | for (j = 0; j < 8; j++) { | |
1173 | highTot += (GraphBuffer[i + j] >> 16); | |
1174 | } | |
1175 | GraphBuffer[i] = lowTot - highTot; | |
1176 | if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i]; | |
1177 | if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i]; | |
1178 | } | |
1179 | ||
1180 | GraphTraceLen -= (convLen + 16); | |
1181 | RepaintGraphWindow(); | |
1182 | ||
b3b70669 | 1183 | // Find bit-sync (3 lo followed by 3 high) (HID ONLY) |
7fe9b0b7 | 1184 | int max = 0, maxPos = 0; |
1185 | for (i = 0; i < 6000; ++i) { | |
1186 | int dec = 0; | |
1187 | for (j = 0; j < 3 * lowLen; ++j) { | |
1188 | dec -= GraphBuffer[i + j]; | |
1189 | } | |
1190 | for (; j < 3 * (lowLen + highLen ); ++j) { | |
1191 | dec += GraphBuffer[i + j]; | |
1192 | } | |
1193 | if (dec > max) { | |
1194 | max = dec; | |
1195 | maxPos = i; | |
1196 | } | |
1197 | } | |
1198 | ||
1199 | // place start of bit sync marker in graph | |
1200 | GraphBuffer[maxPos] = maxMark; | |
1201 | GraphBuffer[maxPos + 1] = minMark; | |
1202 | ||
1203 | maxPos += j; | |
1204 | ||
1205 | // place end of bit sync marker in graph | |
1206 | GraphBuffer[maxPos] = maxMark; | |
1207 | GraphBuffer[maxPos+1] = minMark; | |
1208 | ||
1209 | PrintAndLog("actual data bits start at sample %d", maxPos); | |
1210 | PrintAndLog("length %d/%d", highLen, lowLen); | |
ba1a299c | 1211 | |
a1557c4c | 1212 | uint8_t bits[46] = {0x00}; |
ba1a299c | 1213 | |
7fe9b0b7 | 1214 | // find bit pairs and manchester decode them |
1215 | for (i = 0; i < arraylen(bits) - 1; ++i) { | |
1216 | int dec = 0; | |
1217 | for (j = 0; j < lowLen; ++j) { | |
1218 | dec -= GraphBuffer[maxPos + j]; | |
1219 | } | |
1220 | for (; j < lowLen + highLen; ++j) { | |
1221 | dec += GraphBuffer[maxPos + j]; | |
1222 | } | |
1223 | maxPos += j; | |
1224 | // place inter bit marker in graph | |
1225 | GraphBuffer[maxPos] = maxMark; | |
1226 | GraphBuffer[maxPos + 1] = minMark; | |
1227 | ||
1228 | // hi and lo form a 64 bit pair | |
1229 | hi = (hi << 1) | (lo >> 31); | |
1230 | lo = (lo << 1); | |
1231 | // store decoded bit as binary (in hi/lo) and text (in bits[]) | |
1232 | if(dec < 0) { | |
1233 | bits[i] = '1'; | |
1234 | lo |= 1; | |
1235 | } else { | |
1236 | bits[i] = '0'; | |
1237 | } | |
1238 | } | |
1239 | PrintAndLog("bits: '%s'", bits); | |
1240 | PrintAndLog("hex: %08x %08x", hi, lo); | |
1241 | return 0; | |
1242 | } | |
e888ed8e | 1243 | |
ec75f5c1 | 1244 | //by marshmellow |
1245 | //attempt to detect the field clock and bit clock for FSK | |
1e090a61 | 1246 | int CmdFSKfcDetect(const char *Cmd) |
1247 | { | |
1248 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1249 | size_t size = getFromGraphBuf(BitStream); | |
1250 | ||
03e6bb4a | 1251 | uint16_t ans = countFC(BitStream, size); |
1252 | if (ans==0) { | |
1253 | if (g_debugMode) PrintAndLog("DEBUG: No data found"); | |
1254 | return 0; | |
1255 | } | |
1256 | uint8_t fc1, fc2; | |
1e090a61 | 1257 | fc1 = (ans >> 8) & 0xFF; |
1258 | fc2 = ans & 0xFF; | |
03e6bb4a | 1259 | |
1260 | uint8_t rf1 = detectFSKClk(BitStream, size, fc1, fc2); | |
1261 | if (rf1==0) { | |
1262 | if (g_debugMode) PrintAndLog("DEBUG: Clock detect error"); | |
1263 | return 0; | |
1264 | } | |
1e090a61 | 1265 | PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); |
1266 | return 1; | |
1267 | } | |
1268 | ||
8c65b650 | 1269 | //by marshmellow |
1270 | //attempt to detect the bit clock for PSK or NRZ modulations | |
4118b74d | 1271 | int CmdDetectNRZpskClockRate(const char *Cmd) |
1272 | { | |
ba1a299c | 1273 | GetNRZpskClock("",0,0); |
1274 | return 0; | |
4118b74d | 1275 | } |
1276 | ||
8c65b650 | 1277 | //by marshmellow |
1278 | //attempt to psk1 or nrz demod graph buffer | |
1279 | //NOTE CURRENTLY RELIES ON PEAKS :( | |
04d2721b | 1280 | int PSKnrzDemod(const char *Cmd, uint8_t verbose) |
ec75f5c1 | 1281 | { |
ba1a299c | 1282 | int invert=0; |
1283 | int clk=0; | |
1284 | sscanf(Cmd, "%i %i", &clk, &invert); | |
1285 | if (invert != 0 && invert != 1) { | |
1286 | PrintAndLog("Invalid argument: %s", Cmd); | |
1287 | return -1; | |
1288 | } | |
1289 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0}; | |
1290 | size_t BitLen = getFromGraphBuf(BitStream); | |
1291 | int errCnt=0; | |
1292 | errCnt = pskNRZrawDemod(BitStream, &BitLen,&clk,&invert); | |
1293 | if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first) | |
ec75f5c1 | 1294 | if (g_debugMode==1) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt); |
ba1a299c | 1295 | return -1; |
1296 | } | |
04d2721b | 1297 | if (verbose) PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen); |
ba1a299c | 1298 | |
1299 | //prime demod buffer for output | |
ec75f5c1 | 1300 | setDemodBuf(BitStream,BitLen,0); |
ba1a299c | 1301 | return errCnt; |
4118b74d | 1302 | } |
1303 | // Indala 26 bit decode | |
1304 | // by marshmellow | |
1305 | // optional arguments - same as CmdpskNRZrawDemod (clock & invert) | |
1306 | int CmdIndalaDecode(const char *Cmd) | |
1307 | { | |
84871873 | 1308 | int ans; |
1309 | if (strlen(Cmd)>0){ | |
04d2721b | 1310 | ans = PSKnrzDemod(Cmd, 0); |
84871873 | 1311 | } else{ //default to RF/32 |
04d2721b | 1312 | ans = PSKnrzDemod("32", 0); |
84871873 | 1313 | } |
4118b74d | 1314 | |
ba1a299c | 1315 | if (ans < 0){ |
ec75f5c1 | 1316 | if (g_debugMode==1) |
84871873 | 1317 | PrintAndLog("Error1: %d",ans); |
ba1a299c | 1318 | return 0; |
1319 | } | |
1320 | uint8_t invert=0; | |
1321 | ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert); | |
1322 | if (ans < 1) { | |
ec75f5c1 | 1323 | if (g_debugMode==1) |
84871873 | 1324 | PrintAndLog("Error2: %d",ans); |
ba1a299c | 1325 | return -1; |
1326 | } | |
a1d17964 | 1327 | char showbits[251]={0x00}; |
84871873 | 1328 | if (invert) |
ec75f5c1 | 1329 | if (g_debugMode==1) |
84871873 | 1330 | PrintAndLog("Had to invert bits"); |
ec75f5c1 | 1331 | |
ba1a299c | 1332 | //convert UID to HEX |
1333 | uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7; | |
1334 | int idx; | |
1335 | uid1=0; | |
1336 | uid2=0; | |
1337 | PrintAndLog("BitLen: %d",DemodBufferLen); | |
1338 | if (DemodBufferLen==64){ | |
1339 | for( idx=0; idx<64; idx++) { | |
1340 | uid1=(uid1<<1)|(uid2>>31); | |
1341 | if (DemodBuffer[idx] == 0) { | |
1342 | uid2=(uid2<<1)|0; | |
1343 | showbits[idx]='0'; | |
1344 | } else { | |
1345 | uid2=(uid2<<1)|1; | |
1346 | showbits[idx]='1'; | |
1347 | } | |
1348 | } | |
1349 | showbits[idx]='\0'; | |
1350 | PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2); | |
1351 | } | |
1352 | else { | |
1353 | uid3=0; | |
1354 | uid4=0; | |
1355 | uid5=0; | |
1356 | uid6=0; | |
1357 | uid7=0; | |
1358 | for( idx=0; idx<DemodBufferLen; idx++) { | |
1359 | uid1=(uid1<<1)|(uid2>>31); | |
1360 | uid2=(uid2<<1)|(uid3>>31); | |
1361 | uid3=(uid3<<1)|(uid4>>31); | |
1362 | uid4=(uid4<<1)|(uid5>>31); | |
1363 | uid5=(uid5<<1)|(uid6>>31); | |
1364 | uid6=(uid6<<1)|(uid7>>31); | |
1365 | if (DemodBuffer[idx] == 0) { | |
1366 | uid7=(uid7<<1)|0; | |
1367 | showbits[idx]='0'; | |
1368 | } | |
1369 | else { | |
1370 | uid7=(uid7<<1)|1; | |
1371 | showbits[idx]='1'; | |
1372 | } | |
1373 | } | |
1374 | showbits[idx]='\0'; | |
1375 | PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7); | |
1376 | } | |
a1d17964 | 1377 | if (g_debugMode){ |
1378 | PrintAndLog("DEBUG: printing demodbuffer:"); | |
1379 | printDemodBuff(); | |
1380 | } | |
ba1a299c | 1381 | return 1; |
4118b74d | 1382 | } |
1383 | ||
8c65b650 | 1384 | //by marshmellow |
1385 | //attempt to clean psk wave noise after a peak | |
1386 | //NOTE RELIES ON PEAKS :( | |
4118b74d | 1387 | int CmdPskClean(const char *Cmd) |
1388 | { | |
ba1a299c | 1389 | uint8_t bitStream[MAX_GRAPH_TRACE_LEN]={0}; |
1390 | size_t bitLen = getFromGraphBuf(bitStream); | |
1391 | pskCleanWave(bitStream, bitLen); | |
1392 | setGraphBuf(bitStream, bitLen); | |
1393 | return 0; | |
4118b74d | 1394 | } |
1395 | ||
04d2721b | 1396 | // by marshmellow |
1397 | // takes 2 arguments - clock and invert both as integers | |
1398 | // attempts to demodulate psk only | |
1399 | // prints binary found and saves in demodbuffer for further commands | |
4118b74d | 1400 | int CmdpskNRZrawDemod(const char *Cmd) |
1401 | { | |
84871873 | 1402 | int errCnt; |
ec75f5c1 | 1403 | |
04d2721b | 1404 | errCnt = PSKnrzDemod(Cmd, 1); |
ba1a299c | 1405 | //output |
ec75f5c1 | 1406 | if (errCnt<0){ |
1407 | if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt); | |
1408 | return 0; | |
1409 | } | |
ba1a299c | 1410 | if (errCnt>0){ |
ec75f5c1 | 1411 | if (g_debugMode){ |
84871873 | 1412 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); |
ec75f5c1 | 1413 | PrintAndLog("PSK or NRZ demoded bitstream:"); |
1414 | // Now output the bitstream to the scrollback by line of 16 bits | |
1415 | printDemodBuff(); | |
1416 | } | |
1417 | }else{ | |
1418 | PrintAndLog("PSK or NRZ demoded bitstream:"); | |
1419 | // Now output the bitstream to the scrollback by line of 16 bits | |
1420 | printDemodBuff(); | |
1421 | return 1; | |
1422 | } | |
1423 | return 0; | |
4118b74d | 1424 | } |
ba1a299c | 1425 | |
04d2721b | 1426 | // by marshmellow |
1427 | // takes same args as cmdpsknrzrawdemod | |
1428 | int CmdPSK2rawDemod(const char *Cmd) | |
1429 | { | |
1430 | int errCnt=0; | |
1431 | errCnt=PSKnrzDemod(Cmd, 1); | |
1432 | if (errCnt<0){ | |
1433 | if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt); | |
1434 | return 0; | |
1435 | } | |
1436 | psk1TOpsk2(DemodBuffer, DemodBufferLen); | |
1437 | if (errCnt>0){ | |
1438 | if (g_debugMode){ | |
1439 | PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt); | |
1440 | PrintAndLog("PSK2 demoded bitstream:"); | |
1441 | // Now output the bitstream to the scrollback by line of 16 bits | |
1442 | printDemodBuff(); | |
1443 | } | |
1444 | }else{ | |
1445 | PrintAndLog("PSK2 demoded bitstream:"); | |
1446 | // Now output the bitstream to the scrollback by line of 16 bits | |
1447 | printDemodBuff(); | |
1448 | } | |
1449 | return 1; | |
4118b74d | 1450 | } |
1451 | ||
7fe9b0b7 | 1452 | int CmdGrid(const char *Cmd) |
1453 | { | |
1454 | sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); | |
7ddb9900 | 1455 | PlotGridXdefault= PlotGridX; |
1456 | PlotGridYdefault= PlotGridY; | |
7fe9b0b7 | 1457 | RepaintGraphWindow(); |
1458 | return 0; | |
1459 | } | |
1460 | ||
1461 | int CmdHexsamples(const char *Cmd) | |
1462 | { | |
4961e292 | 1463 | int i, j; |
7fe9b0b7 | 1464 | int requested = 0; |
1465 | int offset = 0; | |
4961e292 | 1466 | char string_buf[25]; |
1467 | char* string_ptr = string_buf; | |
f71f4deb | 1468 | uint8_t got[BIGBUF_SIZE]; |
952a8bb5 | 1469 | |
4961e292 | 1470 | sscanf(Cmd, "%i %i", &requested, &offset); |
90d74dc2 | 1471 | |
4961e292 | 1472 | /* if no args send something */ |
1473 | if (requested == 0) { | |
90d74dc2 | 1474 | requested = 8; |
1475 | } | |
90d74dc2 | 1476 | if (offset + requested > sizeof(got)) { |
f71f4deb | 1477 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE); |
4961e292 | 1478 | return 0; |
952a8bb5 | 1479 | } |
90d74dc2 | 1480 | |
4961e292 | 1481 | GetFromBigBuf(got,requested,offset); |
90d74dc2 | 1482 | WaitForResponse(CMD_ACK,NULL); |
1483 | ||
4961e292 | 1484 | i = 0; |
1485 | for (j = 0; j < requested; j++) { | |
1486 | i++; | |
1487 | string_ptr += sprintf(string_ptr, "%02x ", got[j]); | |
1488 | if (i == 8) { | |
1489 | *(string_ptr - 1) = '\0'; // remove the trailing space | |
1490 | PrintAndLog("%s", string_buf); | |
1491 | string_buf[0] = '\0'; | |
1492 | string_ptr = string_buf; | |
1493 | i = 0; | |
1494 | } | |
1495 | if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes | |
1496 | *(string_ptr - 1) = '\0'; | |
1497 | PrintAndLog("%s", string_buf); | |
1498 | string_buf[0] = '\0'; | |
7fe9b0b7 | 1499 | } |
ba1a299c | 1500 | } |
7fe9b0b7 | 1501 | return 0; |
1502 | } | |
1503 | ||
7fe9b0b7 | 1504 | int CmdHide(const char *Cmd) |
1505 | { | |
1506 | HideGraphWindow(); | |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | int CmdHpf(const char *Cmd) | |
1511 | { | |
1512 | int i; | |
1513 | int accum = 0; | |
1514 | ||
1515 | for (i = 10; i < GraphTraceLen; ++i) | |
1516 | accum += GraphBuffer[i]; | |
1517 | accum /= (GraphTraceLen - 10); | |
1518 | for (i = 0; i < GraphTraceLen; ++i) | |
1519 | GraphBuffer[i] -= accum; | |
1520 | ||
1521 | RepaintGraphWindow(); | |
1522 | return 0; | |
1523 | } | |
f6d9fb17 MHS |
1524 | typedef struct { |
1525 | uint8_t * buffer; | |
1526 | uint32_t numbits; | |
1527 | uint32_t position; | |
1528 | }BitstreamOut; | |
1529 | ||
1530 | bool _headBit( BitstreamOut *stream) | |
1531 | { | |
1532 | int bytepos = stream->position >> 3; // divide by 8 | |
1533 | int bitpos = (stream->position++) & 7; // mask out 00000111 | |
1534 | return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1; | |
1535 | } | |
1536 | ||
1537 | uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b) | |
1538 | { | |
1539 | int i; | |
1540 | uint8_t val = 0; | |
1541 | for(i =0 ; i < bits_per_sample; i++) | |
1542 | { | |
1543 | val |= (_headBit(b) << (7-i)); | |
1544 | } | |
1545 | return val; | |
1546 | } | |
7fe9b0b7 | 1547 | |
8d183c53 | 1548 | int CmdSamples(const char *Cmd) |
7fe9b0b7 | 1549 | { |
31abe49f MHS |
1550 | //If we get all but the last byte in bigbuf, |
1551 | // we don't have to worry about remaining trash | |
1552 | // in the last byte in case the bits-per-sample | |
1553 | // does not line up on byte boundaries | |
0644d5e3 | 1554 | uint8_t got[BIGBUF_SIZE-1] = { 0 }; |
ba1a299c | 1555 | |
d6d20c54 | 1556 | int n = strtol(Cmd, NULL, 0); |
1557 | if (n == 0) | |
31abe49f | 1558 | n = sizeof(got); |
d6d20c54 | 1559 | |
1560 | if (n > sizeof(got)) | |
1561 | n = sizeof(got); | |
ba1a299c | 1562 | |
f6d9fb17 MHS |
1563 | PrintAndLog("Reading %d bytes from device memory\n", n); |
1564 | GetFromBigBuf(got,n,0); | |
1565 | PrintAndLog("Data fetched"); | |
1566 | UsbCommand response; | |
1567 | WaitForResponse(CMD_ACK, &response); | |
31abe49f MHS |
1568 | uint8_t bits_per_sample = 8; |
1569 | ||
1570 | //Old devices without this feature would send 0 at arg[0] | |
1571 | if(response.arg[0] > 0) | |
1572 | { | |
1573 | sample_config *sc = (sample_config *) response.d.asBytes; | |
1574 | PrintAndLog("Samples @ %d bits/smpl, decimation 1:%d ", sc->bits_per_sample | |
1575 | , sc->decimation); | |
1576 | bits_per_sample = sc->bits_per_sample; | |
1577 | } | |
f6d9fb17 MHS |
1578 | if(bits_per_sample < 8) |
1579 | { | |
1580 | PrintAndLog("Unpacking..."); | |
1581 | BitstreamOut bout = { got, bits_per_sample * n, 0}; | |
1582 | int j =0; | |
31abe49f | 1583 | for (j = 0; j * bits_per_sample < n * 8 && j < sizeof(GraphBuffer); j++) { |
f6d9fb17 MHS |
1584 | uint8_t sample = getByte(bits_per_sample, &bout); |
1585 | GraphBuffer[j] = ((int) sample )- 128; | |
1586 | } | |
1587 | GraphTraceLen = j; | |
1588 | PrintAndLog("Unpacked %d samples" , j ); | |
1589 | }else | |
1590 | { | |
1591 | for (int j = 0; j < n; j++) { | |
1592 | GraphBuffer[j] = ((int)got[j]) - 128; | |
1593 | } | |
1594 | GraphTraceLen = n; | |
f6d9fb17 MHS |
1595 | } |
1596 | ||
1597 | RepaintGraphWindow(); | |
1598 | return 0; | |
7fe9b0b7 | 1599 | } |
1600 | ||
d6a120a2 MHS |
1601 | int CmdTuneSamples(const char *Cmd) |
1602 | { | |
3aa4014b | 1603 | int timeout = 0; |
1604 | printf("\nMeasuring antenna characteristics, please wait..."); | |
ba1a299c | 1605 | |
3aa4014b | 1606 | UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING}; |
1607 | SendCommand(&c); | |
1608 | ||
1609 | UsbCommand resp; | |
1610 | while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) { | |
1611 | timeout++; | |
1612 | printf("."); | |
1613 | if (timeout > 7) { | |
1614 | PrintAndLog("\nNo response from Proxmark. Aborting..."); | |
1615 | return 1; | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | int peakv, peakf; | |
1620 | int vLf125, vLf134, vHf; | |
1621 | vLf125 = resp.arg[0] & 0xffff; | |
1622 | vLf134 = resp.arg[0] >> 16; | |
1623 | vHf = resp.arg[1] & 0xffff;; | |
1624 | peakf = resp.arg[2] & 0xffff; | |
1625 | peakv = resp.arg[2] >> 16; | |
1626 | PrintAndLog(""); | |
1627 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); | |
1628 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
1629 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
1630 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
1631 | if (peakv<2000) | |
1632 | PrintAndLog("# Your LF antenna is unusable."); | |
1633 | else if (peakv<10000) | |
1634 | PrintAndLog("# Your LF antenna is marginal."); | |
1635 | if (vHf<2000) | |
1636 | PrintAndLog("# Your HF antenna is unusable."); | |
1637 | else if (vHf<5000) | |
1638 | PrintAndLog("# Your HF antenna is marginal."); | |
1639 | ||
1640 | for (int i = 0; i < 256; i++) { | |
1641 | GraphBuffer[i] = resp.d.asBytes[i] - 128; | |
ba1a299c | 1642 | } |
1643 | ||
d5a72d2f | 1644 | PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n"); |
1645 | PrintAndLog("\n"); | |
3aa4014b | 1646 | GraphTraceLen = 256; |
1647 | ShowGraphWindow(); | |
1648 | ||
1649 | return 0; | |
d6a120a2 MHS |
1650 | } |
1651 | ||
3aa4014b | 1652 | |
7fe9b0b7 | 1653 | int CmdLoad(const char *Cmd) |
1654 | { | |
ec75f5c1 | 1655 | char filename[FILE_PATH_SIZE] = {0x00}; |
1656 | int len = 0; | |
b915fda3 | 1657 | |
ec75f5c1 | 1658 | len = strlen(Cmd); |
1659 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
1660 | memcpy(filename, Cmd, len); | |
b915fda3 | 1661 | |
ec75f5c1 | 1662 | FILE *f = fopen(filename, "r"); |
7fe9b0b7 | 1663 | if (!f) { |
b915fda3 | 1664 | PrintAndLog("couldn't open '%s'", filename); |
7fe9b0b7 | 1665 | return 0; |
1666 | } | |
1667 | ||
1668 | GraphTraceLen = 0; | |
1669 | char line[80]; | |
1670 | while (fgets(line, sizeof (line), f)) { | |
1671 | GraphBuffer[GraphTraceLen] = atoi(line); | |
1672 | GraphTraceLen++; | |
1673 | } | |
1674 | fclose(f); | |
1675 | PrintAndLog("loaded %d samples", GraphTraceLen); | |
1676 | RepaintGraphWindow(); | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | int CmdLtrim(const char *Cmd) | |
1681 | { | |
1682 | int ds = atoi(Cmd); | |
1683 | ||
1684 | for (int i = ds; i < GraphTraceLen; ++i) | |
1685 | GraphBuffer[i-ds] = GraphBuffer[i]; | |
1686 | GraphTraceLen -= ds; | |
1687 | ||
1688 | RepaintGraphWindow(); | |
1689 | return 0; | |
1690 | } | |
ec75f5c1 | 1691 | |
1692 | // trim graph to input argument length | |
9ec1416a | 1693 | int CmdRtrim(const char *Cmd) |
1694 | { | |
1695 | int ds = atoi(Cmd); | |
1696 | ||
1697 | GraphTraceLen = ds; | |
1698 | ||
1699 | RepaintGraphWindow(); | |
1700 | return 0; | |
1701 | } | |
7fe9b0b7 | 1702 | |
1703 | /* | |
1704 | * Manchester demodulate a bitstream. The bitstream needs to be already in | |
1705 | * the GraphBuffer as 0 and 1 values | |
1706 | * | |
1707 | * Give the clock rate as argument in order to help the sync - the algorithm | |
1708 | * resyncs at each pulse anyway. | |
1709 | * | |
1710 | * Not optimized by any means, this is the 1st time I'm writing this type of | |
1711 | * routine, feel free to improve... | |
1712 | * | |
1713 | * 1st argument: clock rate (as number of samples per clock rate) | |
1714 | * Typical values can be 64, 32, 128... | |
1715 | */ | |
1716 | int CmdManchesterDemod(const char *Cmd) | |
1717 | { | |
1718 | int i, j, invert= 0; | |
1719 | int bit; | |
1720 | int clock; | |
fddf220a | 1721 | int lastval = 0; |
7fe9b0b7 | 1722 | int low = 0; |
1723 | int high = 0; | |
1724 | int hithigh, hitlow, first; | |
1725 | int lc = 0; | |
1726 | int bitidx = 0; | |
1727 | int bit2idx = 0; | |
1728 | int warnings = 0; | |
1729 | ||
1730 | /* check if we're inverting output */ | |
c6f1fb9d | 1731 | if (*Cmd == 'i') |
7fe9b0b7 | 1732 | { |
1733 | PrintAndLog("Inverting output"); | |
1734 | invert = 1; | |
fffad860 | 1735 | ++Cmd; |
7fe9b0b7 | 1736 | do |
1737 | ++Cmd; | |
1738 | while(*Cmd == ' '); // in case a 2nd argument was given | |
1739 | } | |
1740 | ||
1741 | /* Holds the decoded bitstream: each clock period contains 2 bits */ | |
1742 | /* later simplified to 1 bit after manchester decoding. */ | |
1743 | /* Add 10 bits to allow for noisy / uncertain traces without aborting */ | |
1744 | /* int BitStream[GraphTraceLen*2/clock+10]; */ | |
1745 | ||
1746 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
1747 | /* large array */ | |
90e278d3 | 1748 | uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; |
7fe9b0b7 | 1749 | |
1750 | /* Detect high and lows */ | |
1751 | for (i = 0; i < GraphTraceLen; i++) | |
1752 | { | |
1753 | if (GraphBuffer[i] > high) | |
1754 | high = GraphBuffer[i]; | |
1755 | else if (GraphBuffer[i] < low) | |
1756 | low = GraphBuffer[i]; | |
1757 | } | |
1758 | ||
1759 | /* Get our clock */ | |
1760 | clock = GetClock(Cmd, high, 1); | |
1761 | ||
1762 | int tolerance = clock/4; | |
1763 | ||
1764 | /* Detect first transition */ | |
1765 | /* Lo-Hi (arbitrary) */ | |
1766 | /* skip to the first high */ | |
1767 | for (i= 0; i < GraphTraceLen; i++) | |
1768 | if (GraphBuffer[i] == high) | |
1769 | break; | |
1770 | /* now look for the first low */ | |
1771 | for (; i < GraphTraceLen; i++) | |
1772 | { | |
1773 | if (GraphBuffer[i] == low) | |
1774 | { | |
1775 | lastval = i; | |
1776 | break; | |
1777 | } | |
1778 | } | |
1779 | ||
1780 | /* If we're not working with 1/0s, demod based off clock */ | |
1781 | if (high != 1) | |
1782 | { | |
1783 | bit = 0; /* We assume the 1st bit is zero, it may not be | |
1784 | * the case: this routine (I think) has an init problem. | |
1785 | * Ed. | |
1786 | */ | |
1787 | for (; i < (int)(GraphTraceLen / clock); i++) | |
1788 | { | |
1789 | hithigh = 0; | |
1790 | hitlow = 0; | |
1791 | first = 1; | |
1792 | ||
1793 | /* Find out if we hit both high and low peaks */ | |
1794 | for (j = 0; j < clock; j++) | |
1795 | { | |
1796 | if (GraphBuffer[(i * clock) + j] == high) | |
1797 | hithigh = 1; | |
1798 | else if (GraphBuffer[(i * clock) + j] == low) | |
1799 | hitlow = 1; | |
1800 | ||
1801 | /* it doesn't count if it's the first part of our read | |
1802 | because it's really just trailing from the last sequence */ | |
1803 | if (first && (hithigh || hitlow)) | |
1804 | hithigh = hitlow = 0; | |
1805 | else | |
1806 | first = 0; | |
1807 | ||
1808 | if (hithigh && hitlow) | |
1809 | break; | |
1810 | } | |
1811 | ||
1812 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
1813 | if (!hithigh || !hitlow) | |
1814 | bit ^= 1; | |
1815 | ||
1816 | BitStream[bit2idx++] = bit ^ invert; | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | /* standard 1/0 bitstream */ | |
1821 | else | |
1822 | { | |
1823 | ||
1824 | /* Then detect duration between 2 successive transitions */ | |
1825 | for (bitidx = 1; i < GraphTraceLen; i++) | |
1826 | { | |
1827 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
1828 | { | |
b3b70669 | 1829 | lc = i-lastval; |
1830 | lastval = i; | |
1831 | ||
1832 | // Error check: if bitidx becomes too large, we do not | |
1833 | // have a Manchester encoded bitstream or the clock is really | |
1834 | // wrong! | |
1835 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
1836 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
1837 | return 0; | |
1838 | } | |
1839 | // Then switch depending on lc length: | |
1840 | // Tolerance is 1/4 of clock rate (arbitrary) | |
1841 | if (abs(lc-clock/2) < tolerance) { | |
1842 | // Short pulse : either "1" or "0" | |
1843 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1844 | } else if (abs(lc-clock) < tolerance) { | |
1845 | // Long pulse: either "11" or "00" | |
1846 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1847 | BitStream[bitidx++]=GraphBuffer[i-1]; | |
1848 | } else { | |
7fe9b0b7 | 1849 | // Error |
1850 | warnings++; | |
b3b70669 | 1851 | PrintAndLog("Warning: Manchester decode error for pulse width detection."); |
1852 | PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)"); | |
7fe9b0b7 | 1853 | |
1854 | if (warnings > 10) | |
1855 | { | |
1856 | PrintAndLog("Error: too many detection errors, aborting."); | |
1857 | return 0; | |
1858 | } | |
1859 | } | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream | |
1864 | // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful | |
1865 | // to stop output at the final bitidx2 value, not bitidx | |
1866 | for (i = 0; i < bitidx; i += 2) { | |
1867 | if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) { | |
1868 | BitStream[bit2idx++] = 1 ^ invert; | |
b3b70669 | 1869 | } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) { |
1870 | BitStream[bit2idx++] = 0 ^ invert; | |
1871 | } else { | |
1872 | // We cannot end up in this state, this means we are unsynchronized, | |
1873 | // move up 1 bit: | |
1874 | i++; | |
7fe9b0b7 | 1875 | warnings++; |
b3b70669 | 1876 | PrintAndLog("Unsynchronized, resync..."); |
1877 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
7fe9b0b7 | 1878 | |
1879 | if (warnings > 10) | |
1880 | { | |
1881 | PrintAndLog("Error: too many decode errors, aborting."); | |
1882 | return 0; | |
1883 | } | |
1884 | } | |
1885 | } | |
1886 | } | |
1887 | ||
1888 | PrintAndLog("Manchester decoded bitstream"); | |
1889 | // Now output the bitstream to the scrollback by line of 16 bits | |
1890 | for (i = 0; i < (bit2idx-16); i+=16) { | |
1891 | PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i", | |
1892 | BitStream[i], | |
1893 | BitStream[i+1], | |
1894 | BitStream[i+2], | |
1895 | BitStream[i+3], | |
1896 | BitStream[i+4], | |
1897 | BitStream[i+5], | |
1898 | BitStream[i+6], | |
1899 | BitStream[i+7], | |
1900 | BitStream[i+8], | |
1901 | BitStream[i+9], | |
1902 | BitStream[i+10], | |
1903 | BitStream[i+11], | |
1904 | BitStream[i+12], | |
1905 | BitStream[i+13], | |
1906 | BitStream[i+14], | |
1907 | BitStream[i+15]); | |
1908 | } | |
1909 | return 0; | |
1910 | } | |
1911 | ||
1912 | /* Modulate our data into manchester */ | |
1913 | int CmdManchesterMod(const char *Cmd) | |
1914 | { | |
1915 | int i, j; | |
1916 | int clock; | |
1917 | int bit, lastbit, wave; | |
1918 | ||
1919 | /* Get our clock */ | |
1920 | clock = GetClock(Cmd, 0, 1); | |
1921 | ||
1922 | wave = 0; | |
1923 | lastbit = 1; | |
1924 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
1925 | { | |
1926 | bit = GraphBuffer[i * clock] ^ 1; | |
1927 | ||
1928 | for (j = 0; j < (int)(clock/2); j++) | |
1929 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave; | |
1930 | for (j = (int)(clock/2); j < clock; j++) | |
1931 | GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1; | |
1932 | ||
1933 | /* Keep track of how we start our wave and if we changed or not this time */ | |
1934 | wave ^= bit ^ lastbit; | |
1935 | lastbit = bit; | |
1936 | } | |
1937 | ||
1938 | RepaintGraphWindow(); | |
1939 | return 0; | |
1940 | } | |
1941 | ||
1942 | int CmdNorm(const char *Cmd) | |
1943 | { | |
1944 | int i; | |
1945 | int max = INT_MIN, min = INT_MAX; | |
1946 | ||
1947 | for (i = 10; i < GraphTraceLen; ++i) { | |
1948 | if (GraphBuffer[i] > max) | |
1949 | max = GraphBuffer[i]; | |
1950 | if (GraphBuffer[i] < min) | |
1951 | min = GraphBuffer[i]; | |
1952 | } | |
1953 | ||
1954 | if (max != min) { | |
1955 | for (i = 0; i < GraphTraceLen; ++i) { | |
ba1a299c | 1956 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 256 / |
7fe9b0b7 | 1957 | (max - min); |
ba1a299c | 1958 | //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work |
7fe9b0b7 | 1959 | } |
1960 | } | |
1961 | RepaintGraphWindow(); | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | int CmdPlot(const char *Cmd) | |
1966 | { | |
1967 | ShowGraphWindow(); | |
1968 | return 0; | |
1969 | } | |
1970 | ||
1971 | int CmdSave(const char *Cmd) | |
1972 | { | |
ec75f5c1 | 1973 | char filename[FILE_PATH_SIZE] = {0x00}; |
1974 | int len = 0; | |
b915fda3 | 1975 | |
ec75f5c1 | 1976 | len = strlen(Cmd); |
1977 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
1978 | memcpy(filename, Cmd, len); | |
b915fda3 | 1979 | |
1980 | ||
1981 | FILE *f = fopen(filename, "w"); | |
7fe9b0b7 | 1982 | if(!f) { |
b915fda3 | 1983 | PrintAndLog("couldn't open '%s'", filename); |
7fe9b0b7 | 1984 | return 0; |
1985 | } | |
1986 | int i; | |
1987 | for (i = 0; i < GraphTraceLen; i++) { | |
1988 | fprintf(f, "%d\n", GraphBuffer[i]); | |
1989 | } | |
1990 | fclose(f); | |
1991 | PrintAndLog("saved to '%s'", Cmd); | |
1992 | return 0; | |
1993 | } | |
1994 | ||
1995 | int CmdScale(const char *Cmd) | |
1996 | { | |
1997 | CursorScaleFactor = atoi(Cmd); | |
1998 | if (CursorScaleFactor == 0) { | |
1999 | PrintAndLog("bad, can't have zero scale"); | |
2000 | CursorScaleFactor = 1; | |
2001 | } | |
2002 | RepaintGraphWindow(); | |
2003 | return 0; | |
2004 | } | |
2005 | ||
2006 | int CmdThreshold(const char *Cmd) | |
2007 | { | |
2008 | int threshold = atoi(Cmd); | |
2009 | ||
2010 | for (int i = 0; i < GraphTraceLen; ++i) { | |
2011 | if (GraphBuffer[i] >= threshold) | |
2012 | GraphBuffer[i] = 1; | |
2013 | else | |
7bb9d33e | 2014 | GraphBuffer[i] = -1; |
7fe9b0b7 | 2015 | } |
2016 | RepaintGraphWindow(); | |
2017 | return 0; | |
2018 | } | |
2019 | ||
d51b2eda MHS |
2020 | int CmdDirectionalThreshold(const char *Cmd) |
2021 | { | |
2022 | int8_t upThres = param_get8(Cmd, 0); | |
2023 | int8_t downThres = param_get8(Cmd, 1); | |
ba1a299c | 2024 | |
d51b2eda | 2025 | printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); |
952a8bb5 | 2026 | |
d51b2eda MHS |
2027 | int lastValue = GraphBuffer[0]; |
2028 | GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. | |
952a8bb5 | 2029 | |
d51b2eda MHS |
2030 | for (int i = 1; i < GraphTraceLen; ++i) { |
2031 | // Apply first threshold to samples heading up | |
2032 | if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue) | |
2033 | { | |
2034 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2035 | GraphBuffer[i] = 1; | |
2036 | } | |
2037 | // Apply second threshold to samples heading down | |
2038 | else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue) | |
2039 | { | |
2040 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2041 | GraphBuffer[i] = -1; | |
2042 | } | |
2043 | else | |
2044 | { | |
2045 | lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. | |
2046 | GraphBuffer[i] = GraphBuffer[i-1]; | |
2047 | ||
2048 | } | |
2049 | } | |
2050 | GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample. | |
2051 | RepaintGraphWindow(); | |
2052 | return 0; | |
2053 | } | |
2054 | ||
7fe9b0b7 | 2055 | int CmdZerocrossings(const char *Cmd) |
2056 | { | |
2057 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
2058 | CmdHpf(""); | |
2059 | ||
2060 | int sign = 1; | |
2061 | int zc = 0; | |
2062 | int lastZc = 0; | |
2063 | ||
2064 | for (int i = 0; i < GraphTraceLen; ++i) { | |
2065 | if (GraphBuffer[i] * sign >= 0) { | |
2066 | // No change in sign, reproduce the previous sample count. | |
2067 | zc++; | |
2068 | GraphBuffer[i] = lastZc; | |
2069 | } else { | |
2070 | // Change in sign, reset the sample count. | |
2071 | sign = -sign; | |
2072 | GraphBuffer[i] = lastZc; | |
2073 | if (sign > 0) { | |
2074 | lastZc = zc; | |
2075 | zc = 0; | |
2076 | } | |
2077 | } | |
2078 | } | |
2079 | ||
2080 | RepaintGraphWindow(); | |
2081 | return 0; | |
2082 | } | |
2083 | ||
ba1a299c | 2084 | static command_t CommandTable[] = |
7fe9b0b7 | 2085 | { |
2086 | {"help", CmdHelp, 1, "This help"}, | |
2087 | {"amp", CmdAmp, 1, "Amplify peaks"}, | |
57c69556 | 2088 | {"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"}, |
1e090a61 | 2089 | {"askmandemod", Cmdaskmandemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional)"}, |
2090 | {"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK tags and output bin (args optional)"}, | |
7fe9b0b7 | 2091 | {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"}, |
1e090a61 | 2092 | {"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] [invert<0|1>] Biphase decode bin stream in demod buffer (offset = 0|1 bits to shift the decode start)"}, |
7fe9b0b7 | 2093 | {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"}, |
2094 | {"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"}, | |
2095 | {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"}, | |
2096 | {"dec", CmdDec, 1, "Decimate samples"}, | |
ba1a299c | 2097 | {"detectclock", CmdDetectClockRate, 1, "Detect ASK clock rate"}, |
e888ed8e | 2098 | {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"}, |
1e090a61 | 2099 | {"fskawiddemod", CmdFSKdemodAWID, 1, "Demodulate graph window as an AWID FSK tag using raw"}, |
2100 | {"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"}, | |
2101 | {"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate graph window as a HID FSK tag using raw"}, | |
2102 | {"fskiodemod", CmdFSKdemodIO, 1, "Demodulate graph window as an IO Prox tag FSK using raw"}, | |
2103 | {"fskpyramiddemod",CmdFSKdemodPyramid,1, "Demodulate graph window as a Pyramid FSK tag using raw"}, | |
ec75f5c1 | 2104 | {"fskparadoxdemod",CmdFSKdemodParadox,1, "Demodulate graph window as a Paradox FSK tag using raw"}, |
1e090a61 | 2105 | {"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to bin (clock = 50)(invert = 1|0)(rchigh = 10)(rclow=8)"}, |
7fe9b0b7 | 2106 | {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"}, |
ba1a299c | 2107 | {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"}, |
7fe9b0b7 | 2108 | {"hide", CmdHide, 1, "Hide graph window"}, |
2109 | {"hpf", CmdHpf, 1, "Remove DC offset from trace"}, | |
7fe9b0b7 | 2110 | {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"}, |
2111 | {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"}, | |
9ec1416a | 2112 | {"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"}, |
7fe9b0b7 | 2113 | {"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"}, |
eb191de6 | 2114 | {"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream already in graph buffer"}, |
7fe9b0b7 | 2115 | {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"}, |
ba1a299c | 2116 | {"norm", CmdNorm, 1, "Normalize max/min to +/-128"}, |
7ddb9900 | 2117 | {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"}, |
ba1a299c | 2118 | {"pskclean", CmdPskClean, 1, "Attempt to clean psk wave"}, |
2119 | {"pskdetectclock",CmdDetectNRZpskClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"}, | |
04d2721b | 2120 | {"pskindalademod",CmdIndalaDecode, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 indala tags and output ID binary & hex (args optional)"}, |
2121 | {"psk1nrzrawdemod",CmdpskNRZrawDemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 or nrz tags and output binary (args optional)"}, | |
2122 | {"psk2rawdemod", CmdPSK2rawDemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk2 tags and output binary (args optional)"}, | |
90d74dc2 | 2123 | {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window"}, |
7fe9b0b7 | 2124 | {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"}, |
2125 | {"scale", CmdScale, 1, "<int> -- Set cursor display scale"}, | |
ec75f5c1 | 2126 | {"setdebugmode", CmdSetDebugMode, 1, "<0|1> -- Turn on or off Debugging Mode for demods"}, |
2127 | {"shiftgraphzero",CmdGraphShiftZero, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"}, | |
dbf444a1 | 2128 | {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"}, |
ba1a299c | 2129 | {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."}, |
2130 | {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"}, | |
698b649e MHS |
2131 | {"undec", CmdUndec, 1, "Un-decimate samples by 2"}, |
2132 | {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"}, | |
7fe9b0b7 | 2133 | {NULL, NULL, 0, NULL} |
2134 | }; | |
2135 | ||
2136 | int CmdData(const char *Cmd) | |
2137 | { | |
2138 | CmdsParse(CommandTable, Cmd); | |
2139 | return 0; | |
2140 | } | |
2141 | ||
2142 | int CmdHelp(const char *Cmd) | |
2143 | { | |
2144 | CmdsHelp(CommandTable); | |
2145 | return 0; | |
2146 | } |