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