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"
25 static int CmdHelp(const char *Cmd
);
27 int CmdAmp(const char *Cmd
)
29 int i
, rising
, falling
;
30 int max
= INT_MIN
, min
= INT_MAX
;
32 DetectHighLowInGraph( &max
, &min
, FALSE
);
37 for (i
= 0; i
< GraphTraceLen
; ++i
) {
38 if (GraphBuffer
[i
+ 1] < GraphBuffer
[i
]) {
45 if (GraphBuffer
[i
+ 1] > GraphBuffer
[i
]) {
59 * Generic command to demodulate ASK.
61 * Argument is convention: positive or negative (High mod means zero
62 * or high mod means one)
64 * Updates the Graph trace with 0/1 values
69 //this method is dependant on all highs and lows to be the same(or clipped) this creates issues[marshmellow] it also ignores the clock
70 int Cmdaskdemod(const char *Cmd
)
73 int c
, high
= 0, low
= 0;
75 sscanf(Cmd
, "%i", &c
);
77 if (c
!= 0 && c
!= 1) {
78 PrintAndLog("Invalid argument: %s", Cmd
);
82 DetectHighLowInGraph( &high
, &low
, FALSE
);
84 high
= abs(high
* .75);
88 if (GraphBuffer
[0] > 0) {
94 for (i
= 1; i
< GraphTraceLen
; ++i
) {
95 /* Transitions are detected at each peak
96 * Transitions are either:
97 * - we're low: transition if we hit a high
98 * - we're high: transition if we hit a low
99 * (we need to do it this way because some tags keep high or
100 * low for long periods, others just reach the peak and go
103 if ((GraphBuffer
[i
] == high
) && (GraphBuffer
[i
- 1] == c
)) {
104 GraphBuffer
[i
] = 1 - c
;
105 } else if ((GraphBuffer
[i
] == low
) && (GraphBuffer
[i
- 1] == (1 - c
))){
109 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
112 RepaintGraphWindow();
116 void printBitStream(uint8_t bits
[], uint32_t bitLen
){
120 PrintAndLog("Too few bits found: %d",bitLen
);
126 if ( ( bitLen
% 16 ) > 0) {
127 bitLen
= ((bitLen
/ 16) * 16);
128 PrintAndLog("ICE: equally divided with 16 = %d",bitLen
);
131 for (i
= 0; i
<= ( bitLen
- 16); i
+= 16) {
132 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
153 void printEM410x(uint64_t id
) {
155 if ( id
<= 0 ) return;
161 for (j
= 5; j
> 0; j
--){
162 for (i
= 0; i
< 8; i
++){
163 id2lo
= ( id2lo
<< 1LL)|((id
& ( 1 << ( i
+( ( j
-1 ) * 8 )))) >> ( i
+ (( j
-1) *8 )));
167 PrintAndLog("EM TAG ID : %010llx", id
);
168 PrintAndLog("Unique TAG ID: %010llx", id2lo
);
169 PrintAndLog("DEZ 8 : %08lld", id
& 0xFFFFFF);
170 PrintAndLog("DEZ 10 : %010lld", id
& 0xFFFFFF);
171 PrintAndLog("DEZ 5.5 : %05lld.%05lld", (id
>>16LL) & 0xFFFF, (id
& 0xFFFF));
172 PrintAndLog("DEZ 3.5A : %03lld.%05lld", (id
>>32ll), (id
& 0xFFFF));
173 PrintAndLog("DEZ 14/IK2 : %014lld", id
);
174 PrintAndLog("DEZ 15/IK3 : %015lld", id2lo
);
175 PrintAndLog("Other : %05lld_%03lld_%08lld", (id
& 0xFFFF), (( id
>> 16LL) & 0xFF), (id
& 0xFFFFFF));
178 int CmdEm410xDecode(const char *Cmd
)
181 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
182 uint32_t len
= GetFromGraphBuf(bits
);
183 id
= Em410xDecode(bits
, len
);
191 //takes 2 arguments - clock and invert both as integers
192 //attempts to demodulate ask while decoding manchester
193 //prints binary found and saves in graphbuffer for further commands
194 int Cmdaskmandemod(const char *Cmd
)
199 sscanf(Cmd
, "%i %i", &clk
, &invert
);
201 if (invert
!= 0 && invert
!= 1) {
202 PrintAndLog("Invalid argument: %s", Cmd
);
206 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
207 uint32_t len
= GetFromGraphBuf(bits
);
209 int errCnt
= askmandemod(bits
, &len
, &clk
, &invert
);
211 if (errCnt
< 0) return 0;
212 if (len
< 16) return 0;
214 PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk
,invert
,len
);
217 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
220 PrintAndLog("ASK/Manchester decoded bitstream:");
222 printBitStream(bits
, len
);
223 uint64_t lo
= Em410xDecode(bits
, len
);
226 SetGraphBuf(bits
,len
);
227 PrintAndLog("EM410x pattern found: ");
236 //stricktly take 10 and 01 and convert to 0 and 1
237 int Cmdmandecoderaw(const char *Cmd
)
242 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
243 int high
= 0, low
= 0;
245 for (; i
< GraphTraceLen
; ++i
){
246 if (GraphBuffer
[i
] > high
) high
= GraphBuffer
[i
];
247 else if (GraphBuffer
[i
] < low
) low
= GraphBuffer
[i
];
248 bits
[i
] = GraphBuffer
[i
];
251 if (high
> 1 || low
< 0 ){
252 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
257 errCnt
= manrawdecode(bits
, &bitnum
);
260 PrintAndLog("Too many errors: %d",errCnt
);
264 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt
);
265 printBitStream(bits
,bitnum
);
268 //put back in graphbuffer
269 SetGraphBuf(bits
, bitnum
);
271 uint64_t id
= Em410xDecode(bits
,i
);
279 //take 01 or 10 = 0 and 11 or 00 = 1
280 //takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit
281 // since it is not like manchester and doesn't have an incorrect bit pattern we
282 // cannot determine if our decode is correct or if it should be shifted by one bit
283 // the argument offset allows us to manually shift if the output is incorrect
284 // (better would be to demod and decode at the same time so we can distinguish large
285 // width waves vs small width waves to help the decode positioning) or askbiphdemod
286 int CmdBiphaseDecodeRaw(const char *Cmd
)
292 int high
= 0, low
= 0;
293 sscanf(Cmd
, "%i", &offset
);
295 uint8_t bits
[MAX_GRAPH_TRACE_LEN
]={0};
297 //get graphbuffer & high and low
298 for (; i
<GraphTraceLen
; ++i
){
299 if (GraphBuffer
[i
] > high
) high
= GraphBuffer
[i
];
300 else if (GraphBuffer
[i
] < low
) low
= GraphBuffer
[i
];
301 bits
[i
] = GraphBuffer
[i
];
303 if (high
> 1 || low
< 0){
304 PrintAndLog("Error: please raw demod the wave first then decode");
308 errCnt
= BiphaseRawDecode(bits
, &bitnum
, offset
);
310 PrintAndLog("Too many errors attempting to decode: %d", errCnt
);
313 PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:", offset
, errCnt
);
314 printBitStream(bits
, bitnum
);
315 PrintAndLog("\nif bitstream does not look right try offset=1");
321 //takes 2 arguments - clock and invert both as integers
322 //attempts to demodulate ask only
323 //prints binary found and saves in graphbuffer for further commands
324 int Cmdaskrawdemod(const char *Cmd
)
329 sscanf(Cmd
, "%i %i", &clk
, &invert
);
331 if (invert
!= 0 && invert
!= 1 ) {
332 PrintAndLog("Invalid argument: %s", Cmd
);
337 PrintAndLog("Wrong clock argument");
341 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
342 int len
= GetFromGraphBuf(bits
);
345 errCnt
= askrawdemod(bits
, &len
, &clk
, &invert
);
347 //throw away static - allow 1 and -1 (in case of threshold command first)
349 PrintAndLog("no data found");
353 if (len
< 16) return 0;
355 PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,len
);
357 //move BitStream back to GraphBuffer
358 SetGraphBuf(bits
, len
);
361 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
364 PrintAndLog("ASK demoded bitstream:");
366 // Now output the bitstream to the scrollback by line of 16 bits
367 printBitStream(bits
,len
);
371 int CmdAutoCorr(const char *Cmd
)
373 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
375 int window
= atoi(Cmd
);
378 PrintAndLog("needs a window");
381 if (window
>= GraphTraceLen
) {
382 PrintAndLog("window must be smaller than trace (%d samples)",
387 PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
389 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
391 for (int j
= 0; j
< window
; ++j
) {
392 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
394 CorrelBuffer
[i
] = sum
;
396 GraphTraceLen
= GraphTraceLen
- window
;
397 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
399 RepaintGraphWindow();
403 int CmdBitsamples(const char *Cmd
)
408 GetFromBigBuf(got
,sizeof(got
),0);
409 WaitForResponse(CMD_ACK
,NULL
);
411 for (int j
= 0; j
< sizeof(got
); j
++) {
412 for (int k
= 0; k
< 8; k
++) {
413 if(got
[j
] & (1 << (7 - k
))) {
414 GraphBuffer
[cnt
++] = 1;
416 GraphBuffer
[cnt
++] = 0;
421 RepaintGraphWindow();
426 * Convert to a bitstream
428 int CmdBitstream(const char *Cmd
)
436 int hithigh
, hitlow
, first
;
438 /* Detect high and lows and clock */
439 DetectHighLowInGraph( &high
, &low
, FALSE
);
442 clock
= GetClock(Cmd
, 0);
446 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
451 /* Find out if we hit both high and low peaks */
452 for (j
= 0; j
< clock
; ++j
)
454 if (GraphBuffer
[(i
* clock
) + j
] == high
)
456 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
458 /* it doesn't count if it's the first part of our read
459 because it's really just trailing from the last sequence */
460 if (first
&& (hithigh
|| hitlow
))
461 hithigh
= hitlow
= 0;
465 if (hithigh
&& hitlow
)
469 /* If we didn't hit both high and low peaks, we had a bit transition */
470 if (!hithigh
|| !hitlow
)
473 AppendGraph(0, clock
, bit
);
476 RepaintGraphWindow();
480 int CmdBuffClear(const char *Cmd
)
482 UsbCommand c
= {CMD_BUFF_CLEAR
};
488 int CmdDec(const char *Cmd
)
490 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
491 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
493 PrintAndLog("decimated by 2");
494 RepaintGraphWindow();
498 /* Print our clock rate */
499 // uses data from graphbuffer
500 int CmdDetectClockRate(const char *Cmd
)
507 //fsk raw demod and print binary
508 //takes 4 arguments - Clock, invert, rchigh, rclow
509 //defaults: clock = 50, invert=0, rchigh=10, rclow=8 (RF/10 RF/8 (fsk2a))
510 int CmdFSKrawdemod(const char *Cmd
)
512 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
518 //set options from parameters entered with the command
519 sscanf(Cmd
, "%i %i %i %i", &rfLen
, &invert
, &fchigh
, &fclow
);
521 // A lots of checks if chigh, clow is out-of bounds.
523 if (strlen(Cmd
)>0 && strlen(Cmd
)<=2) {
527 //if invert option only is used
533 PrintAndLog("Args invert: %d - Clock:%d - FC high:%d - FC low: %d",invert
,rfLen
,fchigh
, fclow
);
535 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
536 uint32_t len
= GetFromGraphBuf(bits
);
538 int size
= fskdemod(bits
, len
,(uint8_t)rfLen
, (uint8_t)invert
, (uint8_t)fchigh
, (uint8_t)fclow
);
541 PrintAndLog("FSK decoded bitstream:");
543 SetGraphBuf(bits
, size
);
545 // Now output the bitstream to the scrollback by line of 16 bits
546 // only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size
549 printBitStream(bits
,size
);
551 PrintAndLog("no FSK data found");
556 //by marshmellow (based on existing demod + holiman's refactor)
557 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
558 //print full HID Prox ID and some bit format details if found
559 int CmdFSKdemodHID(const char *Cmd
)
561 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
562 uint32_t hi2
=0, hi
=0, lo
=0;
564 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0x00};
565 uint32_t BitLen
= GetFromGraphBuf(BitStream
);
567 //get binary from fsk wave
568 size_t size
= HIDdemodFSK(BitStream
,BitLen
,&hi2
,&hi
,&lo
);
571 PrintAndLog("Error demoding fsk");
575 if (hi2
==0 && hi
==0 && lo
==0) return 0;
577 //extra large HID tags
579 PrintAndLog("TAG ID: %x%08x%08x (%d)",
583 (unsigned int) (lo
>>1) & 0xFFFF);
584 SetGraphBuf(BitStream
,BitLen
);
587 //standard HID tags <38 bits
590 uint32_t cardnum
= 0;
592 //if bit 38 is set then < 37 bit format is used
593 if (((hi
>>5) & 1)==1){
596 //get bits 21-37 to check for format len bit
597 lo2
= (((hi
& 15) << 12) | (lo
>>20));
600 //find last bit set to 1 (format len bit)
610 cardnum
= (lo
>>1)&0xFFFF;
614 cardnum
= (lo
>>1)&0x7FFFF;
615 fc
= ((hi
&0xF)<<12)|(lo
>>20);
618 cardnum
= (lo
>>1)&0xFFFF;
619 fc
= ((hi
&1)<<15)|(lo
>>17);
622 cardnum
= (lo
>>1)&0xFFFFF;
623 fc
= ((hi
&1)<<11)|(lo
>>21);
626 //if bit 38 is not set then 37 bit format is used
632 cardnum
= (lo
>>1) & 0x7FFFF;
633 fc
= ((hi
&0xF) << 12) | (lo
>> 20);
636 PrintAndLog("TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
639 (unsigned int) (lo
>>1) & 0xFFFF,
640 (unsigned int) fmtLen
,
642 (unsigned int) cardnum
);
643 SetGraphBuf(BitStream
,BitLen
);
650 //IO-Prox demod - FSK RF/64 with preamble of 000000001
651 //print ioprox ID and some format details
652 int CmdFSKdemodIO(const char *Cmd
)
654 if (GraphTraceLen
< 65) {
655 PrintAndLog("data samples size is too small");
659 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
662 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
663 uint32_t bitlen
= GetFromGraphBuf(bits
);
665 //get binary from fsk wave
666 idx
= IOdemodFSK(bits
, bitlen
);
672 PrintAndLog("data samples size is too small");
676 PrintAndLog("Data samples has too much noice");
680 PrintAndLog("No good demod");
684 if (idx
+64 > bitlen
) return 0;
687 //0 10 20 30 40 50 60
689 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
690 //-----------------------------------------------------------------------------
691 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
693 //XSF(version)facility:codeone+codetwo (raw)
696 PrintAndLog("%d%d%d%d%d%d%d%d %d", bits
[idx
] , bits
[idx
+1], bits
[idx
+2], bits
[idx
+3], bits
[idx
+4], bits
[idx
+5], bits
[idx
+6], bits
[idx
+7], bits
[idx
+8]);
697 PrintAndLog("%d%d%d%d%d%d%d%d %d", bits
[idx
+9] , bits
[idx
+10], bits
[idx
+11], bits
[idx
+12], bits
[idx
+13], bits
[idx
+14], bits
[idx
+15], bits
[idx
+16], bits
[idx
+17]);
698 PrintAndLog("%d%d%d%d%d%d%d%d %d facility", bits
[idx
+18], bits
[idx
+19], bits
[idx
+20], bits
[idx
+21], bits
[idx
+22], bits
[idx
+23], bits
[idx
+24], bits
[idx
+25], bits
[idx
+26]);
699 PrintAndLog("%d%d%d%d%d%d%d%d %d version", bits
[idx
+27], bits
[idx
+28], bits
[idx
+29], bits
[idx
+30], bits
[idx
+31], bits
[idx
+32], bits
[idx
+33], bits
[idx
+34], bits
[idx
+35]);
700 PrintAndLog("%d%d%d%d%d%d%d%d %d code1", bits
[idx
+36], bits
[idx
+37], bits
[idx
+38], bits
[idx
+39], bits
[idx
+40], bits
[idx
+41], bits
[idx
+42], bits
[idx
+43], bits
[idx
+44]);
701 PrintAndLog("%d%d%d%d%d%d%d%d %d code2", bits
[idx
+45], bits
[idx
+46], bits
[idx
+47], bits
[idx
+48], bits
[idx
+49], bits
[idx
+50], bits
[idx
+51], bits
[idx
+52], bits
[idx
+53]);
702 PrintAndLog("%d%d%d%d%d%d%d%d %d%d checksum", bits
[idx
+54], bits
[idx
+55], bits
[idx
+56], bits
[idx
+57], bits
[idx
+58], bits
[idx
+59], bits
[idx
+60], bits
[idx
+61], bits
[idx
+62], bits
[idx
+63]);
704 uint32_t code
= bytebits_to_byte(bits
+idx
,32);
705 uint32_t code2
= bytebits_to_byte(bits
+idx
+32,32);
706 uint8_t version
= bytebits_to_byte(bits
+idx
+27,8); //14,4
707 uint8_t facilitycode
= bytebits_to_byte(bits
+idx
+18,8) ;
708 uint16_t number
= (bytebits_to_byte(bits
+idx
+36,8)<<8)|(bytebits_to_byte(bits
+idx
+45,8)); //36,9
710 PrintAndLog("XSF(%02d)%02x:%05d (%08x%08x)", version
, facilitycode
, number
, code
, code2
);
711 SetGraphBuf(bits
, bitlen
);
715 int CmdFSKdemod(const char *Cmd
) //old CmdFSKdemod needs updating
717 static const int LowTone
[] = {
718 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
719 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
720 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
721 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
722 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
724 static const int HighTone
[] = {
725 1, 1, 1, 1, 1, -1, -1, -1, -1,
726 1, 1, 1, 1, -1, -1, -1, -1,
727 1, 1, 1, 1, -1, -1, -1, -1,
728 1, 1, 1, 1, -1, -1, -1, -1,
729 1, 1, 1, 1, -1, -1, -1, -1,
730 1, 1, 1, 1, -1, -1, -1, -1, -1,
733 int lowLen
= sizeof (LowTone
) / sizeof (int);
734 int highLen
= sizeof (HighTone
) / sizeof (int);
735 int convLen
= (highLen
> lowLen
) ? highLen
: lowLen
;
736 uint32_t hi
= 0, lo
= 0;
739 int minMark
= 0, maxMark
= 0;
741 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
742 int lowSum
= 0, highSum
= 0;
744 for (j
= 0; j
< lowLen
; ++j
) {
745 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
747 for (j
= 0; j
< highLen
; ++j
) {
748 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
750 lowSum
= abs(100 * lowSum
/ lowLen
);
751 highSum
= abs(100 * highSum
/ highLen
);
752 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
755 for(i
= 0; i
< GraphTraceLen
- convLen
- 16; ++i
) {
756 int lowTot
= 0, highTot
= 0;
757 // 10 and 8 are f_s divided by f_l and f_h, rounded
758 for (j
= 0; j
< 10; ++j
) {
759 lowTot
+= (GraphBuffer
[i
+j
] & 0xffff);
761 for (j
= 0; j
< 8; j
++) {
762 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
764 GraphBuffer
[i
] = lowTot
- highTot
;
765 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
766 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
769 GraphTraceLen
-= (convLen
+ 16);
770 RepaintGraphWindow();
772 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
773 int max
= 0, maxPos
= 0;
774 for (i
= 0; i
< 6000; ++i
) {
776 for (j
= 0; j
< 3 * lowLen
; ++j
) {
777 dec
-= GraphBuffer
[i
+ j
];
779 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
780 dec
+= GraphBuffer
[i
+ j
];
788 // place start of bit sync marker in graph
789 GraphBuffer
[maxPos
] = maxMark
;
790 GraphBuffer
[maxPos
+ 1] = minMark
;
794 // place end of bit sync marker in graph
795 GraphBuffer
[maxPos
] = maxMark
;
796 GraphBuffer
[maxPos
+1] = minMark
;
798 PrintAndLog("actual data bits start at sample %d", maxPos
);
799 PrintAndLog("length %d/%d", highLen
, lowLen
);
801 uint8_t bits
[46] = {0x00};
803 // find bit pairs and manchester decode them
804 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
806 for (j
= 0; j
< lowLen
; ++j
) {
807 dec
-= GraphBuffer
[maxPos
+ j
];
809 for (; j
< lowLen
+ highLen
; ++j
) {
810 dec
+= GraphBuffer
[maxPos
+ j
];
813 // place inter bit marker in graph
814 GraphBuffer
[maxPos
] = maxMark
;
815 GraphBuffer
[maxPos
+ 1] = minMark
;
817 // hi and lo form a 64 bit pair
818 hi
= (hi
<< 1) | (lo
>> 31);
820 // store decoded bit as binary (in hi/lo) and text (in bits[])
828 PrintAndLog("bits: '%s'", bits
);
829 PrintAndLog("hex: %08x %08x", hi
, lo
);
833 int CmdGrid(const char *Cmd
)
835 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
836 PlotGridXdefault
= PlotGridX
;
837 PlotGridYdefault
= PlotGridY
;
838 RepaintGraphWindow();
842 int CmdHexsamples(const char *Cmd
)
848 char* string_ptr
= string_buf
;
851 sscanf(Cmd
, "%i %i", &requested
, &offset
);
853 /* if no args send something */
854 if (requested
== 0) {
857 if (offset
+ requested
> sizeof(got
)) {
858 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000");
862 GetFromBigBuf(got
,requested
,offset
);
863 WaitForResponse(CMD_ACK
,NULL
);
866 for (j
= 0; j
< requested
; j
++) {
868 string_ptr
+= sprintf(string_ptr
, "%02x ", got
[j
]);
870 *(string_ptr
- 1) = '\0'; // remove the trailing space
871 PrintAndLog("%s", string_buf
);
872 string_buf
[0] = '\0';
873 string_ptr
= string_buf
;
876 if (j
== requested
- 1 && string_buf
[0] != '\0') { // print any remaining bytes
877 *(string_ptr
- 1) = '\0';
878 PrintAndLog("%s", string_buf
);
879 string_buf
[0] = '\0';
885 int CmdHide(const char *Cmd
)
891 int CmdHpf(const char *Cmd
)
896 for (i
= 10; i
< GraphTraceLen
; ++i
)
897 accum
+= GraphBuffer
[i
];
898 accum
/= (GraphTraceLen
- 10);
899 for (i
= 0; i
< GraphTraceLen
; ++i
)
900 GraphBuffer
[i
] -= accum
;
902 RepaintGraphWindow();
906 int CmdSamples(const char *Cmd
)
908 uint8_t got
[40000] = {0x00};
910 int n
= strtol(Cmd
, NULL
, 0);
917 PrintAndLog("Reading %d samples from device memory\n", n
);
918 GetFromBigBuf(got
,n
,0);
919 WaitForResponse(CMD_ACK
,NULL
);
920 for (int j
= 0; j
< n
; ++j
) {
921 GraphBuffer
[j
] = ((int)got
[j
]) - 128;
924 RepaintGraphWindow();
928 int CmdTuneSamples(const char *Cmd
)
931 printf("\nMeasuring antenna characteristics, please wait...");
933 UsbCommand c
= {CMD_MEASURE_ANTENNA_TUNING
};
937 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING
,&resp
,1000)) {
941 PrintAndLog("\nNo response from Proxmark. Aborting...");
947 int vLf125
, vLf134
, vHf
;
948 vLf125
= resp
.arg
[0] & 0xffff;
949 vLf134
= resp
.arg
[0] >> 16;
950 vHf
= resp
.arg
[1] & 0xffff;;
951 peakf
= resp
.arg
[2] & 0xffff;
952 peakv
= resp
.arg
[2] >> 16;
954 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
955 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
956 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
957 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
959 PrintAndLog("# Your LF antenna is unusable.");
960 else if (peakv
<10000)
961 PrintAndLog("# Your LF antenna is marginal.");
963 PrintAndLog("# Your HF antenna is unusable.");
965 PrintAndLog("# Your HF antenna is marginal.");
967 for (int i
= 0; i
< 256; i
++) {
968 GraphBuffer
[i
] = resp
.d
.asBytes
[i
] - 128;
971 PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n");
979 int CmdLoad(const char *Cmd
)
981 char filename
[FILE_PATH_SIZE
] = {0x00};
985 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
986 memcpy(filename
, Cmd
, len
);
988 FILE *f
= fopen(filename
, "r");
990 PrintAndLog("couldn't open '%s'", filename
);
996 while (fgets(line
, sizeof (line
), f
)) {
997 GraphBuffer
[GraphTraceLen
] = atoi(line
);
1001 PrintAndLog("loaded %d samples", GraphTraceLen
);
1002 RepaintGraphWindow();
1006 int CmdLtrim(const char *Cmd
)
1010 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
1011 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
1012 GraphTraceLen
-= ds
;
1014 RepaintGraphWindow();
1017 int CmdRtrim(const char *Cmd
)
1023 RepaintGraphWindow();
1028 * Manchester demodulate a bitstream. The bitstream needs to be already in
1029 * the GraphBuffer as 0 and 1 values
1031 * Give the clock rate as argument in order to help the sync - the algorithm
1032 * resyncs at each pulse anyway.
1034 * Not optimized by any means, this is the 1st time I'm writing this type of
1035 * routine, feel free to improve...
1037 * 1st argument: clock rate (as number of samples per clock rate)
1038 * Typical values can be 64, 32, 128...
1040 int CmdManchesterDemod(const char *Cmd
)
1042 int i
, j
, invert
= 0;
1048 int hithigh
, hitlow
, first
;
1054 /* check if we're inverting output */
1057 PrintAndLog("Inverting output");
1062 while(*Cmd
== ' '); // in case a 2nd argument was given
1065 /* Holds the decoded bitstream: each clock period contains 2 bits */
1066 /* later simplified to 1 bit after manchester decoding. */
1067 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
1068 /* int BitStream[GraphTraceLen*2/clock+10]; */
1070 /* But it does not work if compiling on WIndows: therefore we just allocate a */
1072 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0x00};
1074 /* Detect high and lows */
1075 DetectHighLowInGraph( &high
, &low
, TRUE
);
1078 clock
= GetClock(Cmd
, 0);
1079 int tolerance
= clock
/4;
1081 /* Detect first transition */
1082 /* Lo-Hi (arbitrary) */
1083 /* skip to the first high */
1084 for (i
= 0; i
< GraphTraceLen
; i
++)
1085 if (GraphBuffer
[i
] == high
)
1087 /* now look for the first low */
1088 for (; i
< GraphTraceLen
; i
++)
1090 if (GraphBuffer
[i
] == low
)
1097 /* If we're not working with 1/0s, demod based off clock */
1100 bit
= 0; /* We assume the 1st bit is zero, it may not be
1101 * the case: this routine (I think) has an init problem.
1104 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
1110 /* Find out if we hit both high and low peaks */
1111 for (j
= 0; j
< clock
; j
++)
1113 if (GraphBuffer
[(i
* clock
) + j
] == high
)
1115 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
1118 /* it doesn't count if it's the first part of our read
1119 because it's really just trailing from the last sequence */
1120 if (first
&& (hithigh
|| hitlow
))
1121 hithigh
= hitlow
= 0;
1125 if (hithigh
&& hitlow
)
1129 /* If we didn't hit both high and low peaks, we had a bit transition */
1130 if (!hithigh
|| !hitlow
)
1133 BitStream
[bit2idx
++] = bit
^ invert
;
1137 /* standard 1/0 bitstream */
1141 /* Then detect duration between 2 successive transitions */
1142 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
1144 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
1149 // Error check: if bitidx becomes too large, we do not
1150 // have a Manchester encoded bitstream or the clock is really
1152 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
1153 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
1156 // Then switch depending on lc length:
1157 // Tolerance is 1/4 of clock rate (arbitrary)
1158 if (abs(lc
-clock
/2) < tolerance
) {
1159 // Short pulse : either "1" or "0"
1160 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1161 } else if (abs(lc
-clock
) < tolerance
) {
1162 // Long pulse: either "11" or "00"
1163 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1164 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1168 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
1169 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
1173 PrintAndLog("Error: too many detection errors, aborting.");
1180 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
1181 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
1182 // to stop output at the final bitidx2 value, not bitidx
1184 //http://www.proxmark.org/forum/viewtopic.php?id=403
1185 for (i
= 1; i
< bitidx
; i
+= 2) {
1186 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
1187 BitStream
[bit2idx
++] = 1 ^ invert
;
1188 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
1189 BitStream
[bit2idx
++] = 0 ^ invert
;
1191 // We cannot end up in this state, this means we are unsynchronized,
1195 PrintAndLog("Unsynchronized, resync...");
1196 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
1200 PrintAndLog("Error: too many decode errors, aborting.");
1207 PrintAndLog("Manchester decoded bitstream");
1208 // Now output the bitstream to the scrollback by line of 16 bits
1209 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
1210 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
1231 /* Modulate our data into manchester */
1232 int CmdManchesterMod(const char *Cmd
)
1235 int bit
, lastbit
, wave
;
1236 int clock
= GetClock(Cmd
, 0);
1238 int half
= (int)(clock
/2);
1242 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
1244 bit
= GraphBuffer
[i
* clock
] ^ 1;
1246 for (j
= 0; j
< half
; ++j
)
1247 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
1248 for (j
= half
; j
< clock
; ++j
)
1249 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
1251 /* Keep track of how we start our wave and if we changed or not this time */
1252 wave
^= bit
^ lastbit
;
1256 RepaintGraphWindow();
1260 int CmdNorm(const char *Cmd
)
1263 int max
= INT_MIN
, min
= INT_MAX
;
1265 for (i
= 10; i
< GraphTraceLen
; ++i
) {
1266 if (GraphBuffer
[i
] > max
)
1267 max
= GraphBuffer
[i
];
1268 if (GraphBuffer
[i
] < min
)
1269 min
= GraphBuffer
[i
];
1273 for (i
= 0; i
< GraphTraceLen
; ++i
) {
1274 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 1000 /
1278 RepaintGraphWindow();
1282 int CmdPlot(const char *Cmd
)
1288 int CmdSave(const char *Cmd
)
1290 char filename
[FILE_PATH_SIZE
] = {0x00};
1294 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1295 memcpy(filename
, Cmd
, len
);
1298 FILE *f
= fopen(filename
, "w");
1300 PrintAndLog("couldn't open '%s'", filename
);
1304 for (i
= 0; i
< GraphTraceLen
; i
++) {
1305 fprintf(f
, "%d\n", GraphBuffer
[i
]);
1308 PrintAndLog("saved to '%s'", Cmd
);
1312 int CmdScale(const char *Cmd
)
1314 CursorScaleFactor
= atoi(Cmd
);
1315 if (CursorScaleFactor
== 0) {
1316 PrintAndLog("bad, can't have zero scale");
1317 CursorScaleFactor
= 1;
1319 RepaintGraphWindow();
1323 int CmdThreshold(const char *Cmd
)
1325 int threshold
= atoi(Cmd
);
1327 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1328 if (GraphBuffer
[i
] >= threshold
)
1331 GraphBuffer
[i
] = -1;
1333 RepaintGraphWindow();
1337 int CmdDirectionalThreshold(const char *Cmd
)
1339 int8_t upThres
= param_get8(Cmd
, 0);
1340 int8_t downThres
= param_get8(Cmd
, 1);
1342 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres
, downThres
);
1344 int lastValue
= GraphBuffer
[0];
1345 GraphBuffer
[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
1347 for (int i
= 1; i
< GraphTraceLen
; ++i
) {
1348 // Apply first threshold to samples heading up
1349 if (GraphBuffer
[i
] >= upThres
&& GraphBuffer
[i
] > lastValue
)
1351 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1354 // Apply second threshold to samples heading down
1355 else if (GraphBuffer
[i
] <= downThres
&& GraphBuffer
[i
] < lastValue
)
1357 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1358 GraphBuffer
[i
] = -1;
1362 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1363 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1367 GraphBuffer
[0] = GraphBuffer
[1]; // Aline with first edited sample.
1368 RepaintGraphWindow();
1372 int CmdZerocrossings(const char *Cmd
)
1374 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1381 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1382 if (GraphBuffer
[i
] * sign
>= 0) {
1383 // No change in sign, reproduce the previous sample count.
1385 GraphBuffer
[i
] = lastZc
;
1387 // Change in sign, reset the sample count.
1389 GraphBuffer
[i
] = lastZc
;
1397 RepaintGraphWindow();
1401 static command_t CommandTable
[] =
1403 {"help", CmdHelp
, 1, "This help"},
1404 {"amp", CmdAmp
, 1, "Amplify peaks"},
1405 {"askdemod", Cmdaskdemod
, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
1406 {"askmandemod", Cmdaskmandemod
, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK/Manchester tags and output binary"},
1407 {"askrawdemod", Cmdaskrawdemod
, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK tags and output binary"},
1408 {"autocorr", CmdAutoCorr
, 1, "<window length> -- Autocorrelation over window"},
1409 {"biphaserawdecode",CmdBiphaseDecodeRaw
,1,"[offset] Biphase decode binary stream already in graph buffer (offset = bit to start decode from)"},
1410 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
1411 {"bitstream", CmdBitstream
, 1, "[clock rate] -- Convert waveform into a bitstream"},
1412 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
1413 {"dec", CmdDec
, 1, "Decimate samples"},
1414 {"detectaskclock",CmdDetectClockRate
, 1, "Detect ASK clock rate"},
1415 {"dirthreshold", CmdDirectionalThreshold
, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
1416 {"em4xdecode", CmdEm410xDecode
, 1, "decode em4x from graph buffer"},
1417 {"fskdemod", CmdFSKdemod
, 1, "Demodulate graph window as a HID FSK"},
1418 {"fskhiddemod", CmdFSKdemodHID
, 1, "Demodulate graph window as a HID FSK using raw"},
1419 {"fskiodemod", CmdFSKdemodIO
, 1, "Demodulate graph window as an IO Prox FSK using raw"},
1420 {"fskrawdemod", CmdFSKrawdemod
, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to binary (clock = 50)(invert = 1 or 0)(rchigh = 10)(rclow=8)"},
1421 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
1422 {"hexsamples", CmdHexsamples
, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
1423 {"hide", CmdHide
, 1, "Hide graph window"},
1424 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
1425 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
1426 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
1427 {"rtrim", CmdRtrim
, 1, "<location to end trace> -- Trim samples from right of trace"},
1428 {"mandemod", CmdManchesterDemod
, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
1429 {"manrawdecode", Cmdmandecoderaw
, 1, "Manchester decode binary stream already in graph buffer"},
1430 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
1431 {"norm", CmdNorm
, 1, "Normalize max/min to +/-500"},
1432 {"plot", CmdPlot
, 1, "Show graph window (hit 'h' in window for keystroke help)"},
1433 {"samples", CmdSamples
, 0, "[512 - 40000] -- Get raw samples for graph window"},
1434 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
1435 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
1436 {"threshold", CmdThreshold
, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
1437 {"tune", CmdTuneSamples
, 0, "Get hw tune samples for graph window"},
1438 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
1439 {NULL
, NULL
, 0, NULL
}
1442 int CmdData(const char *Cmd
)
1444 CmdsParse(CommandTable
, Cmd
);
1448 int CmdHelp(const char *Cmd
)
1450 CmdsHelp(CommandTable
);