1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Data and Graph commands
9 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
19 #include "cmdparser.h"
24 uint8_t DemodBuffer
[MAX_DEMOD_BUF_LEN
];
27 static int CmdHelp(const char *Cmd
);
29 //set the demod buffer with given array of binary (one bit per byte)
31 void setDemodBuf(uint8_t *buff
, size_t size
, size_t startIdx
)
34 for (; i
< size
; i
++){
35 DemodBuffer
[i
]=buff
[startIdx
++];
41 int CmdSetDebugMode(const char *Cmd
)
44 sscanf(Cmd
, "%i", &demod
);
45 g_debugMode
=(uint8_t)demod
;
53 int bitLen
= DemodBufferLen
;
55 PrintAndLog("no bits found in demod buffer");
58 if (bitLen
>512) bitLen
=512; //max output to 512 bits if we have more - should be plenty
60 // ensure equally divided by 16
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",
86 int CmdAmp(const char *Cmd
)
88 int i
, rising
, falling
;
89 int max
= INT_MIN
, min
= INT_MAX
;
91 for (i
= 10; i
< GraphTraceLen
; ++i
) {
92 if (GraphBuffer
[i
] > max
)
94 if (GraphBuffer
[i
] < min
)
100 for (i
= 0; i
< GraphTraceLen
; ++i
) {
101 if (GraphBuffer
[i
+ 1] < GraphBuffer
[i
]) {
103 GraphBuffer
[i
] = max
;
108 if (GraphBuffer
[i
+ 1] > GraphBuffer
[i
]) {
110 GraphBuffer
[i
] = min
;
117 RepaintGraphWindow();
122 * Generic command to demodulate ASK.
124 * Argument is convention: positive or negative (High mod means zero
125 * or high mod means one)
127 * Updates the Graph trace with 0/1 values
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
)
136 int c
, high
= 0, low
= 0;
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
);
142 /* Detect high and lows and clock */
144 for (i
= 0; i
< GraphTraceLen
; ++i
)
146 if (GraphBuffer
[i
] > high
)
147 high
= GraphBuffer
[i
];
148 else if (GraphBuffer
[i
] < low
)
149 low
= GraphBuffer
[i
];
153 if (c
!= 0 && c
!= 1) {
154 PrintAndLog("Invalid argument: %s", Cmd
);
158 if (GraphBuffer
[0] > 0) {
159 GraphBuffer
[0] = 1-c
;
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
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
))){
179 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
182 RepaintGraphWindow();
187 void printBitStream(uint8_t BitStream
[], uint32_t bitLen
)
191 PrintAndLog("Too few bits found: %d",bitLen
);
194 if (bitLen
>512) bitLen
=512;
196 // ensure equally divided by 16
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",
222 //print EM410x ID in multiple formats
223 void printEM410x(uint64_t id
)
230 for (ii
=5; ii
>0;ii
--){
232 id2lo
=(id2lo
<<1LL) | ((id
& (iii
<< (i
+((ii
-1)*8)))) >> (i
+((ii
-1)*8)));
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));
250 //take binary from demod buffer and see if we can find an EM410x ID
251 int CmdEm410xDecode(const char *Cmd
)
254 size_t size
= DemodBufferLen
, idx
=0;
255 id
= Em410xDecode(DemodBuffer
, &size
, &idx
);
257 setDemodBuf(DemodBuffer
, size
, idx
);
259 PrintAndLog("DEBUG: Printing demod buffer:");
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
)
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
);
284 size_t BitLen
= getFromGraphBuf(BitStream
);
285 if (g_debugMode
==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen
);
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
);
292 PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
296 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
298 PrintAndLog("ASK/Manchester decoded bitstream:");
299 // Now output the bitstream to the scrollback by line of 16 bits
300 setDemodBuf(BitStream
,BitLen
,0);
304 lo
= Em410xDecode(BitStream
, &BitLen
, &idx
);
306 //set GraphBuffer for clone or sim command
307 setDemodBuf(BitStream
, BitLen
, idx
);
309 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
312 PrintAndLog("EM410x pattern found: ");
321 //stricktly take 10 and 01 and convert to 0 and 1
322 int Cmdmandecoderaw(const char *Cmd
)
327 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={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
];
334 if (high
>1 || low
<0 ){
335 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
339 errCnt
=manrawdecode(BitStream
, &size
);
341 PrintAndLog("Too many errors: %d",errCnt
);
344 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt
);
345 printBitStream(BitStream
, size
);
349 id
= Em410xDecode(BitStream
, &size
, &idx
);
351 //need to adjust to set bitstream back to manchester encoded data
352 //setDemodBuf(BitStream, size, idx);
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
)
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
];
386 if (high
>1 || low
<0){
387 PrintAndLog("Error: please raw demod the wave first then decode");
391 errCnt
=BiphaseRawDecode(BitStream
, &size
, offset
, invert
);
393 PrintAndLog("Too many errors attempting to decode: %d",errCnt
);
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");
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
)
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
);
416 size_t BitLen
= getFromGraphBuf(BitStream
);
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
);
424 PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
426 //move BitStream back to DemodBuffer
427 setDemodBuf(BitStream
,BitLen
,0);
431 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
433 PrintAndLog("ASK demoded bitstream:");
434 // Now output the bitstream to the scrollback by line of 16 bits
435 printBitStream(BitStream
,BitLen
);
440 int CmdAutoCorr(const char *Cmd
)
442 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
444 int window
= atoi(Cmd
);
447 PrintAndLog("needs a window");
450 if (window
>= GraphTraceLen
) {
451 PrintAndLog("window must be smaller than trace (%d samples)",
456 PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
458 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
460 for (int j
= 0; j
< window
; ++j
) {
461 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
463 CorrelBuffer
[i
] = sum
;
465 GraphTraceLen
= GraphTraceLen
- window
;
466 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
468 RepaintGraphWindow();
472 int CmdBitsamples(const char *Cmd
)
477 GetFromBigBuf(got
,sizeof(got
),0);
478 WaitForResponse(CMD_ACK
,NULL
);
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;
485 GraphBuffer
[cnt
++] = 0;
490 RepaintGraphWindow();
495 * Convert to a bitstream
497 int CmdBitstream(const char *Cmd
)
505 int hithigh
, hitlow
, first
;
507 /* Detect high and lows and clock */
508 for (i
= 0; i
< GraphTraceLen
; ++i
)
510 if (GraphBuffer
[i
] > high
)
511 high
= GraphBuffer
[i
];
512 else if (GraphBuffer
[i
] < low
)
513 low
= GraphBuffer
[i
];
517 clock
= GetClock(Cmd
, high
, 1);
521 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
526 /* Find out if we hit both high and low peaks */
527 for (j
= 0; j
< clock
; ++j
)
529 if (GraphBuffer
[(i
* clock
) + j
] == high
)
531 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
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;
540 if (hithigh
&& hitlow
)
544 /* If we didn't hit both high and low peaks, we had a bit transition */
545 if (!hithigh
|| !hitlow
)
548 AppendGraph(0, clock
, bit
);
551 RepaintGraphWindow();
555 int CmdBuffClear(const char *Cmd
)
557 UsbCommand c
= {CMD_BUFF_CLEAR
};
563 int CmdDec(const char *Cmd
)
565 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
566 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
568 PrintAndLog("decimated by 2");
569 RepaintGraphWindow();
574 //shift graph zero up or down based on input + or -
575 int CmdGraphShiftZero(const char *Cmd
)
579 //set options from parameters entered with the command
580 sscanf(Cmd
, "%i", &shift
);
582 for(int i
= 0; i
<GraphTraceLen
; i
++){
583 shiftedVal
=GraphBuffer
[i
]+shift
;
586 else if (shiftedVal
<-127)
588 GraphBuffer
[i
]= shiftedVal
;
594 /* Print our clock rate */
595 // uses data from graphbuffer
596 int CmdDetectClockRate(const char *Cmd
)
599 //int clock = DetectASKClock(0);
600 //PrintAndLog("Auto-detected clock rate: %d", clock);
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
)
610 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
616 //set options from parameters entered with the command
617 sscanf(Cmd
, "%i %i %i %i", &rfLen
, &invert
, &fchigh
, &fclow
);
619 if (strlen(Cmd
)>0 && strlen(Cmd
)<=2) {
621 invert
=1; //if invert option only is used
626 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
627 size_t BitLen
= getFromGraphBuf(BitStream
);
628 //get field clock lengths
630 if (fchigh
==0 || fclow
== 0){
631 fcs
=countFC(BitStream
, BitLen
);
636 fchigh
= (fcs
>> 8) & 0xFF;
640 //get bit clock length
642 rfLen
= detectFSKClk(BitStream
, BitLen
, fchigh
, fclow
);
643 if (rfLen
== 0) rfLen
= 50;
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
);
648 PrintAndLog("FSK decoded bitstream:");
649 setDemodBuf(BitStream
,size
,0);
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
);
655 PrintAndLog("no FSK data found");
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
)
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;
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
);
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
);
683 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
688 if (hi2
==0 && hi
==0 && lo
==0) {
689 if (g_debugMode
) PrintAndLog("DEBUG: Error - no values found");
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);
696 else { //standard HID tags <38 bits
699 uint32_t cardnum
= 0;
700 if (((hi
>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
702 lo2
=(((hi
& 31) << 12) | (lo
>>20)); //get bits 21-37 to check for format len bit
704 while(lo2
>1){ //find last bit set to 1 (format len bit)
712 cardnum
= (lo
>>1)&0xFFFF;
716 cardnum
= (lo
>>1)&0xFFFF;
717 fc
= ((hi
&1)<<15)|(lo
>>17);
720 cardnum
= (lo
>>1)&0xFFFFF;
721 fc
= ((hi
&1)<<11)|(lo
>>21);
724 else { //if bit 38 is not set then 37 bit format is used
729 cardnum
= (lo
>>1)&0x7FFFF;
730 fc
= ((hi
&0xF)<<12)|(lo
>>20);
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
);
737 setDemodBuf(BitStream
,BitLen
,idx
);
739 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
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
)
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;
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
);
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");
768 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
773 if (hi2
==0 && hi
==0 && lo
==0){
774 if (g_debugMode
) PrintAndLog("DEBUG: Error - no value found");
777 uint32_t fc
= ((hi
& 0x3)<<6) | (lo
>>26);
778 uint32_t cardnum
= (lo
>>10)&0xFFFF;
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
);
784 PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx
, BitLen
);
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
)
796 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
799 //something in graphbuffer?
800 if (GraphTraceLen
< 65) {
801 if (g_debugMode
)PrintAndLog("DEBUG: not enough samples in GraphBuffer");
804 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
805 size_t BitLen
= getFromGraphBuf(BitStream
);
807 //get binary from fsk wave
808 idx
= IOdemodFSK(BitStream
,BitLen
);
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");
822 PrintAndLog("DEBUG: Error demoding fsk %d", idx
);
829 PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen
);
830 if (BitLen
> 92) printBitStream(BitStream
,92);
835 //0 10 20 30 40 50 60
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
841 //XSF(version)facility:codeone+codetwo (raw)
844 if (g_debugMode
==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen
);
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]);
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
);
863 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx
,64);
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
)
877 //sscanf(Cmd, "%i", &verbose);
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
);
883 //get binary from fsk wave
884 int idx
= AWIDdemodFSK(BitStream
, &size
);
888 PrintAndLog("DEBUG: Error - not enough samples");
890 PrintAndLog("DEBUG: Error - only noise found");
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");
896 PrintAndLog("DEBUG: Error - AWID preamble not found");
898 PrintAndLog("DEBUG: Error - Size not correct: %d", size
);
900 PrintAndLog("DEBUG: Error %d",idx
);
906 // 0 10 20 30 40 50 60
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)
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
);
924 size
= removeParity(BitStream
, idx
+8, 4, 1, 88);
926 if (g_debugMode
==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
929 // ok valid card found!
932 // 0 10 20 30 40 50 60
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)
945 uint32_t cardnum
= 0;
948 uint8_t fmtLen
= bytebits_to_byte(BitStream
,8);
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
);
955 cardnum
= bytebits_to_byte(BitStream
+8+(fmtLen
-17), 16);
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
);
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
);
966 PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx
, 96);
969 //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands
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
)
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
);
982 //get binary from fsk wave
983 int idx
= PyramiddemodFSK(BitStream
, &size
);
987 PrintAndLog("DEBUG: Error - not enough samples");
989 PrintAndLog("DEBUG: Error - only noise found");
991 PrintAndLog("DEBUG: Error - problem during FSK demod");
993 PrintAndLog("DEBUG: Error - Size not correct: %d", size
);
995 PrintAndLog("DEBUG: Error - Pyramid preamble not found");
997 PrintAndLog("DEBUG: Error - idx: %d",idx
);
1002 // 0 10 20 30 40 50 60
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
1009 // 64 70 80 90 100 110 120
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)
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
);
1029 size
= removeParity(BitStream
, idx
+8, 8, 1, 120);
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
);
1035 // ok valid card found!
1038 // 0 10 20 30 40 50 60 70
1040 // 01234567890123456789012345678901234567890123456789012345678901234567890
1041 // -----------------------------------------------------------------------
1042 // 00000000000000000000000000000000000000000000000000000000000000000000000
1043 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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)
1058 //find start bit to get fmtLen
1060 for (j
=0; j
<size
; j
++){
1061 if(BitStream
[j
]) break;
1063 uint8_t fmtLen
= size
-j
-8;
1065 uint32_t cardnum
= 0;
1067 //uint32_t code2 = 0;
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
);
1079 cardnum
= bytebits_to_byte(BitStream
+81, 16);
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
);
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
);
1090 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, 128);
1096 int CmdFSKdemod(const char *Cmd
) //old CmdFSKdemod needs updating
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
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,
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;
1120 int minMark
= 0, maxMark
= 0;
1122 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
1123 int lowSum
= 0, highSum
= 0;
1125 for (j
= 0; j
< lowLen
; ++j
) {
1126 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
1128 for (j
= 0; j
< highLen
; ++j
) {
1129 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
1131 lowSum
= abs(100 * lowSum
/ lowLen
);
1132 highSum
= abs(100 * highSum
/ highLen
);
1133 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
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);
1142 for (j
= 0; j
< 8; j
++) {
1143 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
1145 GraphBuffer
[i
] = lowTot
- highTot
;
1146 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
1147 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
1150 GraphTraceLen
-= (convLen
+ 16);
1151 RepaintGraphWindow();
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
) {
1157 for (j
= 0; j
< 3 * lowLen
; ++j
) {
1158 dec
-= GraphBuffer
[i
+ j
];
1160 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
1161 dec
+= GraphBuffer
[i
+ j
];
1169 // place start of bit sync marker in graph
1170 GraphBuffer
[maxPos
] = maxMark
;
1171 GraphBuffer
[maxPos
+ 1] = minMark
;
1175 // place end of bit sync marker in graph
1176 GraphBuffer
[maxPos
] = maxMark
;
1177 GraphBuffer
[maxPos
+1] = minMark
;
1179 PrintAndLog("actual data bits start at sample %d", maxPos
);
1180 PrintAndLog("length %d/%d", highLen
, lowLen
);
1182 uint8_t bits
[46] = {0x00};
1184 // find bit pairs and manchester decode them
1185 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
1187 for (j
= 0; j
< lowLen
; ++j
) {
1188 dec
-= GraphBuffer
[maxPos
+ j
];
1190 for (; j
< lowLen
+ highLen
; ++j
) {
1191 dec
+= GraphBuffer
[maxPos
+ j
];
1194 // place inter bit marker in graph
1195 GraphBuffer
[maxPos
] = maxMark
;
1196 GraphBuffer
[maxPos
+ 1] = minMark
;
1198 // hi and lo form a 64 bit pair
1199 hi
= (hi
<< 1) | (lo
>> 31);
1201 // store decoded bit as binary (in hi/lo) and text (in bits[])
1209 PrintAndLog("bits: '%s'", bits
);
1210 PrintAndLog("hex: %08x %08x", hi
, lo
);
1215 //attempt to detect the field clock and bit clock for FSK
1216 int CmdFSKfcDetect(const char *Cmd
)
1218 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1219 size_t size
= getFromGraphBuf(BitStream
);
1221 uint16_t ans
= countFC(BitStream
, size
);
1223 if (g_debugMode
) PrintAndLog("DEBUG: No data found");
1227 fc1
= (ans
>> 8) & 0xFF;
1230 uint8_t rf1
= detectFSKClk(BitStream
, size
, fc1
, fc2
);
1232 if (g_debugMode
) PrintAndLog("DEBUG: Clock detect error");
1235 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1
, fc2
, rf1
);
1240 //attempt to detect the bit clock for PSK or NRZ modulations
1241 int CmdDetectNRZpskClockRate(const char *Cmd
)
1243 GetNRZpskClock("",0,0);
1248 //attempt to psk1 or nrz demod graph buffer
1249 //NOTE CURRENTLY RELIES ON PEAKS :(
1250 int PSKnrzDemod(const char *Cmd
, uint8_t verbose
)
1254 sscanf(Cmd
, "%i %i", &clk
, &invert
);
1255 if (invert
!= 0 && invert
!= 1) {
1256 PrintAndLog("Invalid argument: %s", Cmd
);
1259 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1260 size_t BitLen
= getFromGraphBuf(BitStream
);
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
);
1267 if (verbose
) PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,BitLen
);
1269 //prime demod buffer for output
1270 setDemodBuf(BitStream
,BitLen
,0);
1273 // Indala 26 bit decode
1275 // optional arguments - same as CmdpskNRZrawDemod (clock & invert)
1276 int CmdIndalaDecode(const char *Cmd
)
1280 ans
= PSKnrzDemod(Cmd
, 0);
1281 } else{ //default to RF/32
1282 ans
= PSKnrzDemod("32", 0);
1287 PrintAndLog("Error1: %d",ans
);
1291 ans
= indala26decode(DemodBuffer
,(size_t *) &DemodBufferLen
, &invert
);
1294 PrintAndLog("Error2: %d",ans
);
1297 char showbits
[251]={0x00};
1300 PrintAndLog("Had to invert bits");
1302 //convert UID to HEX
1303 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
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) {
1320 PrintAndLog("Indala UID=%s (%x%08x)", showbits
, uid1
, uid2
);
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) {
1345 PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
1348 PrintAndLog("DEBUG: printing demodbuffer:");
1355 //attempt to clean psk wave noise after a peak
1356 //NOTE RELIES ON PEAKS :(
1357 int CmdPskClean(const char *Cmd
)
1359 uint8_t bitStream
[MAX_GRAPH_TRACE_LEN
]={0};
1360 size_t bitLen
= getFromGraphBuf(bitStream
);
1361 pskCleanWave(bitStream
, bitLen
);
1362 setGraphBuf(bitStream
, bitLen
);
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
)
1374 errCnt
= PSKnrzDemod(Cmd
, 1);
1377 if (g_debugMode
) PrintAndLog("Error demoding: %d",errCnt
);
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
1388 PrintAndLog("PSK or NRZ demoded bitstream:");
1389 // Now output the bitstream to the scrollback by line of 16 bits
1397 // takes same args as cmdpsknrzrawdemod
1398 int CmdPSK2rawDemod(const char *Cmd
)
1401 errCnt
=PSKnrzDemod(Cmd
, 1);
1403 if (g_debugMode
) PrintAndLog("Error demoding: %d",errCnt
);
1406 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
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
1415 PrintAndLog("PSK2 demoded bitstream:");
1416 // Now output the bitstream to the scrollback by line of 16 bits
1422 int CmdGrid(const char *Cmd
)
1424 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
1425 PlotGridXdefault
= PlotGridX
;
1426 PlotGridYdefault
= PlotGridY
;
1427 RepaintGraphWindow();
1431 int CmdHexsamples(const char *Cmd
)
1436 char string_buf
[25];
1437 char* string_ptr
= string_buf
;
1438 uint8_t got
[BIGBUF_SIZE
];
1440 sscanf(Cmd
, "%i %i", &requested
, &offset
);
1442 /* if no args send something */
1443 if (requested
== 0) {
1446 if (offset
+ requested
> sizeof(got
)) {
1447 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE
);
1451 GetFromBigBuf(got
,requested
,offset
);
1452 WaitForResponse(CMD_ACK
,NULL
);
1455 for (j
= 0; j
< requested
; j
++) {
1457 string_ptr
+= sprintf(string_ptr
, "%02x ", got
[j
]);
1459 *(string_ptr
- 1) = '\0'; // remove the trailing space
1460 PrintAndLog("%s", string_buf
);
1461 string_buf
[0] = '\0';
1462 string_ptr
= string_buf
;
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';
1474 int CmdHide(const char *Cmd
)
1480 int CmdHpf(const char *Cmd
)
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
;
1491 RepaintGraphWindow();
1495 int CmdSamples(const char *Cmd
)
1497 uint8_t got
[BIGBUF_SIZE
] = {0x00};
1499 int n
= strtol(Cmd
, NULL
, 0);
1503 if (n
> sizeof(got
))
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;
1513 RepaintGraphWindow();
1517 int CmdTuneSamples(const char *Cmd
)
1520 printf("\nMeasuring antenna characteristics, please wait...");
1522 UsbCommand c
= {CMD_MEASURE_ANTENNA_TUNING
};
1526 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING
,&resp
,1000)) {
1530 PrintAndLog("\nNo response from Proxmark. Aborting...");
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;
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);
1548 PrintAndLog("# Your LF antenna is unusable.");
1549 else if (peakv
<10000)
1550 PrintAndLog("# Your LF antenna is marginal.");
1552 PrintAndLog("# Your HF antenna is unusable.");
1554 PrintAndLog("# Your HF antenna is marginal.");
1556 for (int i
= 0; i
< 256; i
++) {
1557 GraphBuffer
[i
] = resp
.d
.asBytes
[i
] - 128;
1560 PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n");
1562 GraphTraceLen
= 256;
1569 int CmdLoad(const char *Cmd
)
1571 char filename
[FILE_PATH_SIZE
] = {0x00};
1575 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1576 memcpy(filename
, Cmd
, len
);
1578 FILE *f
= fopen(filename
, "r");
1580 PrintAndLog("couldn't open '%s'", filename
);
1586 while (fgets(line
, sizeof (line
), f
)) {
1587 GraphBuffer
[GraphTraceLen
] = atoi(line
);
1591 PrintAndLog("loaded %d samples", GraphTraceLen
);
1592 RepaintGraphWindow();
1596 int CmdLtrim(const char *Cmd
)
1600 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
1601 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
1602 GraphTraceLen
-= ds
;
1604 RepaintGraphWindow();
1608 // trim graph to input argument length
1609 int CmdRtrim(const char *Cmd
)
1615 RepaintGraphWindow();
1620 * Manchester demodulate a bitstream. The bitstream needs to be already in
1621 * the GraphBuffer as 0 and 1 values
1623 * Give the clock rate as argument in order to help the sync - the algorithm
1624 * resyncs at each pulse anyway.
1626 * Not optimized by any means, this is the 1st time I'm writing this type of
1627 * routine, feel free to improve...
1629 * 1st argument: clock rate (as number of samples per clock rate)
1630 * Typical values can be 64, 32, 128...
1632 int CmdManchesterDemod(const char *Cmd
)
1634 int i
, j
, invert
= 0;
1640 int hithigh
, hitlow
, first
;
1646 /* check if we're inverting output */
1649 PrintAndLog("Inverting output");
1654 while(*Cmd
== ' '); // in case a 2nd argument was given
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]; */
1662 /* But it does not work if compiling on WIndows: therefore we just allocate a */
1664 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
1666 /* Detect high and lows */
1667 for (i
= 0; i
< GraphTraceLen
; i
++)
1669 if (GraphBuffer
[i
] > high
)
1670 high
= GraphBuffer
[i
];
1671 else if (GraphBuffer
[i
] < low
)
1672 low
= GraphBuffer
[i
];
1676 clock
= GetClock(Cmd
, high
, 1);
1678 int tolerance
= clock
/4;
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
)
1686 /* now look for the first low */
1687 for (; i
< GraphTraceLen
; i
++)
1689 if (GraphBuffer
[i
] == low
)
1696 /* If we're not working with 1/0s, demod based off clock */
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.
1703 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
1709 /* Find out if we hit both high and low peaks */
1710 for (j
= 0; j
< clock
; j
++)
1712 if (GraphBuffer
[(i
* clock
) + j
] == high
)
1714 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
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;
1724 if (hithigh
&& hitlow
)
1728 /* If we didn't hit both high and low peaks, we had a bit transition */
1729 if (!hithigh
|| !hitlow
)
1732 BitStream
[bit2idx
++] = bit
^ invert
;
1736 /* standard 1/0 bitstream */
1740 /* Then detect duration between 2 successive transitions */
1741 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
1743 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
1748 // Error check: if bitidx becomes too large, we do not
1749 // have a Manchester encoded bitstream or the clock is really
1751 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
1752 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
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];
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)");
1772 PrintAndLog("Error: too many detection errors, aborting.");
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
;
1788 // We cannot end up in this state, this means we are unsynchronized,
1792 PrintAndLog("Unsynchronized, resync...");
1793 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
1797 PrintAndLog("Error: too many decode errors, aborting.");
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",
1828 /* Modulate our data into manchester */
1829 int CmdManchesterMod(const char *Cmd
)
1833 int bit
, lastbit
, wave
;
1836 clock
= GetClock(Cmd
, 0, 1);
1840 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
1842 bit
= GraphBuffer
[i
* clock
] ^ 1;
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;
1849 /* Keep track of how we start our wave and if we changed or not this time */
1850 wave
^= bit
^ lastbit
;
1854 RepaintGraphWindow();
1858 int CmdNorm(const char *Cmd
)
1861 int max
= INT_MIN
, min
= INT_MAX
;
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
];
1871 for (i
= 0; i
< GraphTraceLen
; ++i
) {
1872 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 256 /
1874 //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work
1877 RepaintGraphWindow();
1881 int CmdPlot(const char *Cmd
)
1887 int CmdSave(const char *Cmd
)
1889 char filename
[FILE_PATH_SIZE
] = {0x00};
1893 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1894 memcpy(filename
, Cmd
, len
);
1897 FILE *f
= fopen(filename
, "w");
1899 PrintAndLog("couldn't open '%s'", filename
);
1903 for (i
= 0; i
< GraphTraceLen
; i
++) {
1904 fprintf(f
, "%d\n", GraphBuffer
[i
]);
1907 PrintAndLog("saved to '%s'", Cmd
);
1911 int CmdScale(const char *Cmd
)
1913 CursorScaleFactor
= atoi(Cmd
);
1914 if (CursorScaleFactor
== 0) {
1915 PrintAndLog("bad, can't have zero scale");
1916 CursorScaleFactor
= 1;
1918 RepaintGraphWindow();
1922 int CmdThreshold(const char *Cmd
)
1924 int threshold
= atoi(Cmd
);
1926 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1927 if (GraphBuffer
[i
] >= threshold
)
1930 GraphBuffer
[i
] = -1;
1932 RepaintGraphWindow();
1936 int CmdDirectionalThreshold(const char *Cmd
)
1938 int8_t upThres
= param_get8(Cmd
, 0);
1939 int8_t downThres
= param_get8(Cmd
, 1);
1941 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres
, downThres
);
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.
1946 for (int i
= 1; i
< GraphTraceLen
; ++i
) {
1947 // Apply first threshold to samples heading up
1948 if (GraphBuffer
[i
] >= upThres
&& GraphBuffer
[i
] > lastValue
)
1950 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1953 // Apply second threshold to samples heading down
1954 else if (GraphBuffer
[i
] <= downThres
&& GraphBuffer
[i
] < lastValue
)
1956 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1957 GraphBuffer
[i
] = -1;
1961 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1962 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1966 GraphBuffer
[0] = GraphBuffer
[1]; // Aline with first edited sample.
1967 RepaintGraphWindow();
1971 int CmdZerocrossings(const char *Cmd
)
1973 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1980 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1981 if (GraphBuffer
[i
] * sign
>= 0) {
1982 // No change in sign, reproduce the previous sample count.
1984 GraphBuffer
[i
] = lastZc
;
1986 // Change in sign, reset the sample count.
1988 GraphBuffer
[i
] = lastZc
;
1996 RepaintGraphWindow();
2000 static command_t CommandTable
[] =
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
}
2051 int CmdData(const char *Cmd
)
2053 CmdsParse(CommandTable
, Cmd
);
2057 int CmdHelp(const char *Cmd
)
2059 CmdsHelp(CommandTable
);