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 //[marhsmellow] change == to >= for high and <= for low for fuzz
104 if ((GraphBuffer
[i
] == high
) && (GraphBuffer
[i
- 1] == c
)) {
105 GraphBuffer
[i
] = 1 - c
;
106 } else if ((GraphBuffer
[i
] == low
) && (GraphBuffer
[i
- 1] == (1 - c
))){
110 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
113 RepaintGraphWindow();
117 void printBitStream(uint8_t bits
[], uint32_t bitLen
){
121 PrintAndLog("Too few bits found: %d",bitLen
);
127 if ( ( bitLen
% 16 ) > 0) {
128 bitLen
= ((bitLen
/ 16) * 16);
129 PrintAndLog("ICE: equally divided with 16 = %d",bitLen
);
132 for (i
= 0; i
<= ( bitLen
- 16); i
+= 16) {
133 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
154 void printEM410x(uint64_t id
) {
156 if ( id
<= 0 ) return;
162 for (j
= 5; j
> 0; j
--){
163 for (i
= 0; i
< 8; i
++){
164 id2lo
= ( id2lo
<< 1LL)|((id
& ( 1 << ( i
+( ( j
-1 ) * 8 )))) >> ( i
+ (( j
-1) *8 )));
168 PrintAndLog("EM TAG ID : %010llx", id
);
169 PrintAndLog("Unique TAG ID: %010llx", id2lo
);
170 PrintAndLog("DEZ 8 : %08lld", id
& 0xFFFFFF);
171 PrintAndLog("DEZ 10 : %010lld", id
& 0xFFFFFF);
172 PrintAndLog("DEZ 5.5 : %05lld.%05lld", (id
>>16LL) & 0xFFFF, (id
& 0xFFFF));
173 PrintAndLog("DEZ 3.5A : %03lld.%05lld", (id
>>32ll), (id
& 0xFFFF));
174 PrintAndLog("DEZ 14/IK2 : %014lld", id
);
175 PrintAndLog("DEZ 15/IK3 : %015lld", id2lo
);
176 PrintAndLog("Other : %05lld_%03lld_%08lld", (id
& 0xFFFF), (( id
>> 16LL) & 0xFF), (id
& 0xFFFFFF));
179 int CmdEm410xDecode(const char *Cmd
)
182 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
183 uint32_t len
= GetFromGraphBuf(bits
);
184 id
= Em410xDecode(bits
, len
);
192 //takes 2 arguments - clock and invert both as integers
193 //attempts to demodulate ask while decoding manchester
194 //prints binary found and saves in graphbuffer for further commands
195 int Cmdaskmandemod(const char *Cmd
)
200 sscanf(Cmd
, "%i %i", &clk
, &invert
);
202 if (invert
!= 0 && invert
!= 1) {
203 PrintAndLog("Invalid argument: %s", Cmd
);
207 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
208 uint32_t len
= GetFromGraphBuf(bits
);
210 int errCnt
= askmandemod(bits
, &len
, &clk
, &invert
);
212 if (errCnt
< 0) return 0;
213 if (len
< 16) return 0;
215 PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk
,invert
,len
);
218 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
221 PrintAndLog("ASK/Manchester decoded bitstream:");
223 printBitStream(bits
, len
);
225 uint64_t tagid
= Em410xDecode(bits
, len
);
228 SetGraphBuf(bits
, len
);
237 //stricktly take 10 and 01 and convert to 0 and 1
238 int Cmdmandecoderaw(const char *Cmd
)
243 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
244 int high
= 0, low
= 0;
246 for (; i
< GraphTraceLen
; ++i
){
247 if (GraphBuffer
[i
] > high
) high
= GraphBuffer
[i
];
248 else if (GraphBuffer
[i
] < low
) low
= GraphBuffer
[i
];
249 bits
[i
] = GraphBuffer
[i
];
252 if (high
> 1 || low
< 0 ){
253 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
258 errCnt
= manrawdecode(bits
, &bitnum
);
261 PrintAndLog("Too many errors: %d",errCnt
);
265 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt
);
266 printBitStream(bits
,bitnum
);
269 //put back in graphbuffer
270 SetGraphBuf(bits
, bitnum
);
272 uint64_t id
= Em410xDecode(bits
,i
);
280 //take 01 or 10 = 0 and 11 or 00 = 1
281 //takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit
282 // since it is not like manchester and doesn't have an incorrect bit pattern we
283 // cannot determine if our decode is correct or if it should be shifted by one bit
284 // the argument offset allows us to manually shift if the output is incorrect
285 // (better would be to demod and decode at the same time so we can distinguish large
286 // width waves vs small width waves to help the decode positioning) or askbiphdemod
287 int CmdBiphaseDecodeRaw(const char *Cmd
)
293 int high
= 0, low
= 0;
294 sscanf(Cmd
, "%i", &offset
);
296 uint8_t bits
[MAX_GRAPH_TRACE_LEN
]={0};
298 //get graphbuffer & high and low
299 for (; i
<GraphTraceLen
; ++i
){
300 if (GraphBuffer
[i
] > high
) high
= GraphBuffer
[i
];
301 else if (GraphBuffer
[i
] < low
) low
= GraphBuffer
[i
];
302 bits
[i
] = GraphBuffer
[i
];
304 if (high
> 1 || low
< 0){
305 PrintAndLog("Error: please raw demod the wave first then decode");
309 errCnt
= BiphaseRawDecode(bits
, &bitnum
, offset
);
311 PrintAndLog("Too many errors attempting to decode: %d", errCnt
);
314 PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:", offset
, errCnt
);
315 printBitStream(bits
, bitnum
);
316 PrintAndLog("\nif bitstream does not look right try offset=1");
322 //takes 2 arguments - clock and invert both as integers
323 //attempts to demodulate ask only
324 //prints binary found and saves in graphbuffer for further commands
325 int Cmdaskrawdemod(const char *Cmd
)
330 sscanf(Cmd
, "%i %i", &clk
, &invert
);
332 if (invert
!= 0 && invert
!= 1 ) {
333 PrintAndLog("Invalid argument: %s", Cmd
);
338 PrintAndLog("Wrong clock argument");
342 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
343 int len
= GetFromGraphBuf(bits
);
346 errCnt
= askrawdemod(bits
, &len
, &clk
, &invert
);
348 //throw away static - allow 1 and -1 (in case of threshold command first)
350 PrintAndLog("no data found");
354 if (len
< 16) return 0;
356 PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk
,invert
,len
);
358 //move BitStream back to GraphBuffer
359 SetGraphBuf(bits
, len
);
362 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt
);
365 PrintAndLog("ASK demoded bitstream:");
367 // Now output the bitstream to the scrollback by line of 16 bits
368 printBitStream(bits
,len
);
372 int CmdAutoCorr(const char *Cmd
)
374 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
376 int window
= atoi(Cmd
);
379 PrintAndLog("needs a window");
382 if (window
>= GraphTraceLen
) {
383 PrintAndLog("window must be smaller than trace (%d samples)",
388 PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
390 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
392 for (int j
= 0; j
< window
; ++j
) {
393 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
395 CorrelBuffer
[i
] = sum
;
397 GraphTraceLen
= GraphTraceLen
- window
;
398 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
400 RepaintGraphWindow();
404 int CmdBitsamples(const char *Cmd
)
409 GetFromBigBuf(got
,sizeof(got
),0);
410 WaitForResponse(CMD_ACK
,NULL
);
412 for (int j
= 0; j
< sizeof(got
); j
++) {
413 for (int k
= 0; k
< 8; k
++) {
414 if(got
[j
] & (1 << (7 - k
))) {
415 GraphBuffer
[cnt
++] = 1;
417 GraphBuffer
[cnt
++] = 0;
422 RepaintGraphWindow();
427 * Convert to a bitstream
429 int CmdBitstream(const char *Cmd
)
437 int hithigh
, hitlow
, first
;
439 /* Detect high and lows and clock */
440 DetectHighLowInGraph( &high
, &low
, FALSE
);
443 clock
= GetClock(Cmd
, 0);
447 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
452 /* Find out if we hit both high and low peaks */
453 for (j
= 0; j
< clock
; ++j
)
455 if (GraphBuffer
[(i
* clock
) + j
] == high
)
457 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
459 /* it doesn't count if it's the first part of our read
460 because it's really just trailing from the last sequence */
461 if (first
&& (hithigh
|| hitlow
))
462 hithigh
= hitlow
= 0;
466 if (hithigh
&& hitlow
)
470 /* If we didn't hit both high and low peaks, we had a bit transition */
471 if (!hithigh
|| !hitlow
)
474 AppendGraph(0, clock
, bit
);
477 RepaintGraphWindow();
481 int CmdBuffClear(const char *Cmd
)
483 UsbCommand c
= {CMD_BUFF_CLEAR
};
489 int CmdDec(const char *Cmd
)
491 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
492 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
494 PrintAndLog("decimated by 2");
495 RepaintGraphWindow();
499 /* Print our clock rate */
500 // uses data from graphbuffer
501 int CmdDetectClockRate(const char *Cmd
)
508 //fsk raw demod and print binary
509 //takes 4 arguments - Clock, invert, rchigh, rclow
510 //defaults: clock = 50, invert=0, rchigh=10, rclow=8 (RF/10 RF/8 (fsk2a))
511 int CmdFSKrawdemod(const char *Cmd
)
513 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
519 //set options from parameters entered with the command
520 sscanf(Cmd
, "%i %i %i %i", &rfLen
, &invert
, &fchigh
, &fclow
);
522 // A lots of checks if chigh, clow is out-of bounds.
524 if (strlen(Cmd
)>0 && strlen(Cmd
)<=2) {
528 //if invert option only is used
534 PrintAndLog("Args invert: %d - Clock:%d - FC high:%d - FC low: %d",invert
,rfLen
,fchigh
, fclow
);
536 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
537 uint32_t len
= GetFromGraphBuf(bits
);
539 int size
= fskdemod(bits
, len
,(uint8_t)rfLen
, (uint8_t)invert
, (uint8_t)fchigh
, (uint8_t)fclow
);
542 PrintAndLog("FSK decoded bitstream:");
544 SetGraphBuf(bits
, size
);
546 // Now output the bitstream to the scrollback by line of 16 bits
547 // only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size
550 printBitStream(bits
,size
);
552 PrintAndLog("no FSK data found");
557 //by marshmellow (based on existing demod + holiman's refactor)
558 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
559 //print full HID Prox ID and some bit format details if found
560 int CmdFSKdemodHID(const char *Cmd
)
562 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
563 uint32_t hi2
=0, hi
=0, lo
=0;
565 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0x00};
566 uint32_t BitLen
= GetFromGraphBuf(BitStream
);
568 //get binary from fsk wave
569 size_t size
= HIDdemodFSK(BitStream
,BitLen
,&hi2
,&hi
,&lo
);
572 PrintAndLog("Error demoding fsk");
576 if (hi2
==0 && hi
==0 && lo
==0) return 0;
578 //extra large HID tags
580 PrintAndLog("TAG ID: %x%08x%08x (%d)",
584 (unsigned int) (lo
>>1) & 0xFFFF);
585 SetGraphBuf(BitStream
,BitLen
);
588 //standard HID tags <38 bits
591 uint32_t cardnum
= 0;
593 //if bit 38 is set then < 37 bit format is used
594 if (((hi
>>5) & 1)==1){
597 //get bits 21-37 to check for format len bit
598 lo2
= (((hi
& 15) << 12) | (lo
>>20));
601 //find last bit set to 1 (format len bit)
611 cardnum
= (lo
>>1)&0xFFFF;
615 cardnum
= (lo
>>1)&0x7FFFF;
616 fc
= ((hi
&0xF)<<12)|(lo
>>20);
619 cardnum
= (lo
>>1)&0xFFFF;
620 fc
= ((hi
&1)<<15)|(lo
>>17);
623 cardnum
= (lo
>>1)&0xFFFFF;
624 fc
= ((hi
&1)<<11)|(lo
>>21);
627 //if bit 38 is not set then 37 bit format is used
633 cardnum
= (lo
>>1) & 0x7FFFF;
634 fc
= ((hi
&0xF) << 12) | (lo
>> 20);
637 PrintAndLog("TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
640 (unsigned int) (lo
>>1) & 0xFFFF,
641 (unsigned int) fmtLen
,
643 (unsigned int) cardnum
);
644 SetGraphBuf(BitStream
,BitLen
);
651 //IO-Prox demod - FSK RF/64 with preamble of 000000001
652 //print ioprox ID and some format details
653 int CmdFSKdemodIO(const char *Cmd
)
655 if (GraphTraceLen
< 65) {
656 PrintAndLog("data samples size is too small");
660 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
663 uint8_t bits
[MAX_GRAPH_TRACE_LEN
] = {0x00};
664 uint32_t bitlen
= GetFromGraphBuf(bits
);
666 //get binary from fsk wave
667 idx
= IOdemodFSK(bits
, bitlen
);
673 PrintAndLog("data samples size is too small");
677 PrintAndLog("Data samples has too much noice");
681 PrintAndLog("No good demod");
685 if (idx
+64 > bitlen
) return 0;
688 //0 10 20 30 40 50 60
690 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
691 //-----------------------------------------------------------------------------
692 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
694 //XSF(version)facility:codeone+codetwo (raw)
697 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]);
698 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]);
699 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]);
700 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]);
701 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]);
702 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]);
703 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]);
705 uint32_t code
= bytebits_to_byte(bits
+idx
,32);
706 uint32_t code2
= bytebits_to_byte(bits
+idx
+32,32);
707 uint8_t version
= bytebits_to_byte(bits
+idx
+27,8); //14,4
708 uint8_t facilitycode
= bytebits_to_byte(bits
+idx
+18,8) ;
709 uint16_t number
= (bytebits_to_byte(bits
+idx
+36,8)<<8)|(bytebits_to_byte(bits
+idx
+45,8)); //36,9
711 PrintAndLog("XSF(%02d)%02x:%05d (%08x%08x)", version
, facilitycode
, number
, code
, code2
);
712 SetGraphBuf(bits
, bitlen
);
716 int CmdFSKdemod(const char *Cmd
) //old CmdFSKdemod needs updating
718 static const int LowTone
[] = {
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,
723 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
725 static const int HighTone
[] = {
726 1, 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,
731 1, 1, 1, 1, -1, -1, -1, -1, -1,
734 int lowLen
= sizeof (LowTone
) / sizeof (int);
735 int highLen
= sizeof (HighTone
) / sizeof (int);
736 int convLen
= (highLen
> lowLen
) ? highLen
: lowLen
;
737 uint32_t hi
= 0, lo
= 0;
740 int minMark
= 0, maxMark
= 0;
742 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
743 int lowSum
= 0, highSum
= 0;
745 for (j
= 0; j
< lowLen
; ++j
) {
746 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
748 for (j
= 0; j
< highLen
; ++j
) {
749 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
751 lowSum
= abs(100 * lowSum
/ lowLen
);
752 highSum
= abs(100 * highSum
/ highLen
);
753 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
756 for(i
= 0; i
< GraphTraceLen
- convLen
- 16; ++i
) {
757 int lowTot
= 0, highTot
= 0;
758 // 10 and 8 are f_s divided by f_l and f_h, rounded
759 for (j
= 0; j
< 10; ++j
) {
760 lowTot
+= (GraphBuffer
[i
+j
] & 0xffff);
762 for (j
= 0; j
< 8; j
++) {
763 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
765 GraphBuffer
[i
] = lowTot
- highTot
;
766 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
767 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
770 GraphTraceLen
-= (convLen
+ 16);
771 RepaintGraphWindow();
773 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
774 int max
= 0, maxPos
= 0;
775 for (i
= 0; i
< 6000; ++i
) {
777 for (j
= 0; j
< 3 * lowLen
; ++j
) {
778 dec
-= GraphBuffer
[i
+ j
];
780 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
781 dec
+= GraphBuffer
[i
+ j
];
789 // place start of bit sync marker in graph
790 GraphBuffer
[maxPos
] = maxMark
;
791 GraphBuffer
[maxPos
+ 1] = minMark
;
795 // place end of bit sync marker in graph
796 GraphBuffer
[maxPos
] = maxMark
;
797 GraphBuffer
[maxPos
+1] = minMark
;
799 PrintAndLog("actual data bits start at sample %d", maxPos
);
800 PrintAndLog("length %d/%d", highLen
, lowLen
);
802 uint8_t bits
[46] = {0x00};
804 // find bit pairs and manchester decode them
805 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
807 for (j
= 0; j
< lowLen
; ++j
) {
808 dec
-= GraphBuffer
[maxPos
+ j
];
810 for (; j
< lowLen
+ highLen
; ++j
) {
811 dec
+= GraphBuffer
[maxPos
+ j
];
814 // place inter bit marker in graph
815 GraphBuffer
[maxPos
] = maxMark
;
816 GraphBuffer
[maxPos
+ 1] = minMark
;
818 // hi and lo form a 64 bit pair
819 hi
= (hi
<< 1) | (lo
>> 31);
821 // store decoded bit as binary (in hi/lo) and text (in bits[])
829 PrintAndLog("bits: '%s'", bits
);
830 PrintAndLog("hex: %08x %08x", hi
, lo
);
834 int CmdGrid(const char *Cmd
)
836 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
837 PlotGridXdefault
= PlotGridX
;
838 PlotGridYdefault
= PlotGridY
;
839 RepaintGraphWindow();
843 int CmdHexsamples(const char *Cmd
)
849 char* string_ptr
= string_buf
;
852 sscanf(Cmd
, "%i %i", &requested
, &offset
);
854 /* if no args send something */
855 if (requested
== 0) {
858 if (offset
+ requested
> sizeof(got
)) {
859 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000");
863 GetFromBigBuf(got
,requested
,offset
);
864 WaitForResponse(CMD_ACK
,NULL
);
867 for (j
= 0; j
< requested
; j
++) {
869 string_ptr
+= sprintf(string_ptr
, "%02x ", got
[j
]);
871 *(string_ptr
- 1) = '\0'; // remove the trailing space
872 PrintAndLog("%s", string_buf
);
873 string_buf
[0] = '\0';
874 string_ptr
= string_buf
;
877 if (j
== requested
- 1 && string_buf
[0] != '\0') { // print any remaining bytes
878 *(string_ptr
- 1) = '\0';
879 PrintAndLog("%s", string_buf
);
880 string_buf
[0] = '\0';
886 int CmdHide(const char *Cmd
)
892 int CmdHpf(const char *Cmd
)
897 for (i
= 10; i
< GraphTraceLen
; ++i
)
898 accum
+= GraphBuffer
[i
];
899 accum
/= (GraphTraceLen
- 10);
900 for (i
= 0; i
< GraphTraceLen
; ++i
)
901 GraphBuffer
[i
] -= accum
;
903 RepaintGraphWindow();
907 int CmdSamples(const char *Cmd
)
909 uint8_t got
[40000] = {0x00};
911 int n
= strtol(Cmd
, NULL
, 0);
918 PrintAndLog("Reading %d samples from device memory\n", n
);
919 GetFromBigBuf(got
,n
,0);
920 WaitForResponse(CMD_ACK
,NULL
);
921 for (int j
= 0; j
< n
; ++j
) {
922 GraphBuffer
[j
] = ((int)got
[j
]) - 128;
925 RepaintGraphWindow();
929 int CmdTuneSamples(const char *Cmd
)
932 printf("\nMeasuring antenna characteristics, please wait...");
934 UsbCommand c
= {CMD_MEASURE_ANTENNA_TUNING
};
938 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING
,&resp
,1000)) {
942 PrintAndLog("\nNo response from Proxmark. Aborting...");
948 int vLf125
, vLf134
, vHf
;
949 vLf125
= resp
.arg
[0] & 0xffff;
950 vLf134
= resp
.arg
[0] >> 16;
951 vHf
= resp
.arg
[1] & 0xffff;;
952 peakf
= resp
.arg
[2] & 0xffff;
953 peakv
= resp
.arg
[2] >> 16;
955 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
956 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
957 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
958 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
960 PrintAndLog("# Your LF antenna is unusable.");
961 else if (peakv
<10000)
962 PrintAndLog("# Your LF antenna is marginal.");
964 PrintAndLog("# Your HF antenna is unusable.");
966 PrintAndLog("# Your HF antenna is marginal.");
968 for (int i
= 0; i
< 256; i
++) {
969 GraphBuffer
[i
] = resp
.d
.asBytes
[i
] - 128;
972 PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n");
980 int CmdLoad(const char *Cmd
)
982 char filename
[FILE_PATH_SIZE
] = {0x00};
986 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
987 memcpy(filename
, Cmd
, len
);
989 FILE *f
= fopen(filename
, "r");
991 PrintAndLog("couldn't open '%s'", filename
);
997 while (fgets(line
, sizeof (line
), f
)) {
998 GraphBuffer
[GraphTraceLen
] = atoi(line
);
1002 PrintAndLog("loaded %d samples", GraphTraceLen
);
1003 RepaintGraphWindow();
1007 int CmdLtrim(const char *Cmd
)
1011 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
1012 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
1013 GraphTraceLen
-= ds
;
1015 RepaintGraphWindow();
1018 int CmdRtrim(const char *Cmd
)
1024 RepaintGraphWindow();
1029 * Manchester demodulate a bitstream. The bitstream needs to be already in
1030 * the GraphBuffer as 0 and 1 values
1032 * Give the clock rate as argument in order to help the sync - the algorithm
1033 * resyncs at each pulse anyway.
1035 * Not optimized by any means, this is the 1st time I'm writing this type of
1036 * routine, feel free to improve...
1038 * 1st argument: clock rate (as number of samples per clock rate)
1039 * Typical values can be 64, 32, 128...
1041 int CmdManchesterDemod(const char *Cmd
)
1043 int i
, j
, invert
= 0;
1049 int hithigh
, hitlow
, first
;
1055 /* check if we're inverting output */
1058 PrintAndLog("Inverting output");
1063 while(*Cmd
== ' '); // in case a 2nd argument was given
1066 /* Holds the decoded bitstream: each clock period contains 2 bits */
1067 /* later simplified to 1 bit after manchester decoding. */
1068 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
1069 /* int BitStream[GraphTraceLen*2/clock+10]; */
1071 /* But it does not work if compiling on WIndows: therefore we just allocate a */
1073 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0x00};
1075 /* Detect high and lows */
1076 DetectHighLowInGraph( &high
, &low
, TRUE
);
1079 clock
= GetClock(Cmd
, 0);
1080 int tolerance
= clock
/4;
1082 /* Detect first transition */
1083 /* Lo-Hi (arbitrary) */
1084 /* skip to the first high */
1085 for (i
= 0; i
< GraphTraceLen
; i
++)
1086 if (GraphBuffer
[i
] == high
)
1088 /* now look for the first low */
1089 for (; i
< GraphTraceLen
; i
++)
1091 if (GraphBuffer
[i
] == low
)
1098 /* If we're not working with 1/0s, demod based off clock */
1101 bit
= 0; /* We assume the 1st bit is zero, it may not be
1102 * the case: this routine (I think) has an init problem.
1105 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
1111 /* Find out if we hit both high and low peaks */
1112 for (j
= 0; j
< clock
; j
++)
1114 if (GraphBuffer
[(i
* clock
) + j
] == high
)
1116 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
1119 /* it doesn't count if it's the first part of our read
1120 because it's really just trailing from the last sequence */
1121 if (first
&& (hithigh
|| hitlow
))
1122 hithigh
= hitlow
= 0;
1126 if (hithigh
&& hitlow
)
1130 /* If we didn't hit both high and low peaks, we had a bit transition */
1131 if (!hithigh
|| !hitlow
)
1134 BitStream
[bit2idx
++] = bit
^ invert
;
1138 /* standard 1/0 bitstream */
1142 /* Then detect duration between 2 successive transitions */
1143 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
1145 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
1150 // Error check: if bitidx becomes too large, we do not
1151 // have a Manchester encoded bitstream or the clock is really
1153 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
1154 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
1157 // Then switch depending on lc length:
1158 // Tolerance is 1/4 of clock rate (arbitrary)
1159 if (abs(lc
-clock
/2) < tolerance
) {
1160 // Short pulse : either "1" or "0"
1161 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1162 } else if (abs(lc
-clock
) < tolerance
) {
1163 // Long pulse: either "11" or "00"
1164 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1165 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1169 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
1170 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
1174 PrintAndLog("Error: too many detection errors, aborting.");
1181 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
1182 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
1183 // to stop output at the final bitidx2 value, not bitidx
1185 //http://www.proxmark.org/forum/viewtopic.php?id=403
1186 for (i
= 1; i
< bitidx
; i
+= 2) {
1187 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
1188 BitStream
[bit2idx
++] = 1 ^ invert
;
1189 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
1190 BitStream
[bit2idx
++] = 0 ^ invert
;
1192 // We cannot end up in this state, this means we are unsynchronized,
1196 PrintAndLog("Unsynchronized, resync...");
1197 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
1201 PrintAndLog("Error: too many decode errors, aborting.");
1208 PrintAndLog("Manchester decoded bitstream");
1209 // Now output the bitstream to the scrollback by line of 16 bits
1210 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
1211 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
1232 /* Modulate our data into manchester */
1233 int CmdManchesterMod(const char *Cmd
)
1236 int bit
, lastbit
, wave
;
1237 int clock
= GetClock(Cmd
, 0);
1239 int half
= (int)(clock
/2);
1243 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
1245 bit
= GraphBuffer
[i
* clock
] ^ 1;
1247 for (j
= 0; j
< half
; ++j
)
1248 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
1249 for (j
= half
; j
< clock
; ++j
)
1250 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
1252 /* Keep track of how we start our wave and if we changed or not this time */
1253 wave
^= bit
^ lastbit
;
1257 RepaintGraphWindow();
1261 int CmdNorm(const char *Cmd
)
1264 int max
= INT_MIN
, min
= INT_MAX
;
1266 for (i
= 10; i
< GraphTraceLen
; ++i
) {
1267 if (GraphBuffer
[i
] > max
)
1268 max
= GraphBuffer
[i
];
1269 if (GraphBuffer
[i
] < min
)
1270 min
= GraphBuffer
[i
];
1274 for (i
= 0; i
< GraphTraceLen
; ++i
) {
1275 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 1000 /
1279 RepaintGraphWindow();
1283 int CmdPlot(const char *Cmd
)
1289 int CmdSave(const char *Cmd
)
1291 char filename
[FILE_PATH_SIZE
] = {0x00};
1295 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
1296 memcpy(filename
, Cmd
, len
);
1299 FILE *f
= fopen(filename
, "w");
1301 PrintAndLog("couldn't open '%s'", filename
);
1305 for (i
= 0; i
< GraphTraceLen
; i
++) {
1306 fprintf(f
, "%d\n", GraphBuffer
[i
]);
1309 PrintAndLog("saved to '%s'", Cmd
);
1313 int CmdScale(const char *Cmd
)
1315 CursorScaleFactor
= atoi(Cmd
);
1316 if (CursorScaleFactor
== 0) {
1317 PrintAndLog("bad, can't have zero scale");
1318 CursorScaleFactor
= 1;
1320 RepaintGraphWindow();
1324 int CmdThreshold(const char *Cmd
)
1326 int threshold
= atoi(Cmd
);
1328 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1329 if (GraphBuffer
[i
] >= threshold
)
1332 GraphBuffer
[i
] = -1;
1334 RepaintGraphWindow();
1338 int CmdDirectionalThreshold(const char *Cmd
)
1340 int8_t upThres
= param_get8(Cmd
, 0);
1341 int8_t downThres
= param_get8(Cmd
, 1);
1343 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres
, downThres
);
1345 int lastValue
= GraphBuffer
[0];
1346 GraphBuffer
[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
1348 for (int i
= 1; i
< GraphTraceLen
; ++i
) {
1349 // Apply first threshold to samples heading up
1350 if (GraphBuffer
[i
] >= upThres
&& GraphBuffer
[i
] > lastValue
)
1352 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1355 // Apply second threshold to samples heading down
1356 else if (GraphBuffer
[i
] <= downThres
&& GraphBuffer
[i
] < lastValue
)
1358 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1359 GraphBuffer
[i
] = -1;
1363 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1364 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1368 GraphBuffer
[0] = GraphBuffer
[1]; // Aline with first edited sample.
1369 RepaintGraphWindow();
1373 int CmdZerocrossings(const char *Cmd
)
1375 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1382 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1383 if (GraphBuffer
[i
] * sign
>= 0) {
1384 // No change in sign, reproduce the previous sample count.
1386 GraphBuffer
[i
] = lastZc
;
1388 // Change in sign, reset the sample count.
1390 GraphBuffer
[i
] = lastZc
;
1398 RepaintGraphWindow();
1402 static command_t CommandTable
[] =
1404 {"help", CmdHelp
, 1, "This help"},
1405 {"amp", CmdAmp
, 1, "Amplify peaks"},
1406 {"askdemod", Cmdaskdemod
, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
1407 {"askmandemod", Cmdaskmandemod
, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK/Manchester tags and output binary"},
1408 {"askrawdemod", Cmdaskrawdemod
, 1, "[clock] [invert <0|1>] -- Attempt to demodulate ASK tags and output binary"},
1409 {"autocorr", CmdAutoCorr
, 1, "<window length> -- Autocorrelation over window"},
1410 {"biphaserawdecode",CmdBiphaseDecodeRaw
,1,"[offset] Biphase decode binary stream already in graph buffer (offset = bit to start decode from)"},
1411 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
1412 {"bitstream", CmdBitstream
, 1, "[clock rate] -- Convert waveform into a bitstream"},
1413 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
1414 {"dec", CmdDec
, 1, "Decimate samples"},
1415 {"detectaskclock",CmdDetectClockRate
, 1, "Detect ASK clock rate"},
1416 {"dirthreshold", CmdDirectionalThreshold
, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
1417 {"em4xdecode", CmdEm410xDecode
, 1, "decode em4x from graph buffer"},
1418 {"fskdemod", CmdFSKdemod
, 1, "Demodulate graph window as a HID FSK"},
1419 {"fskhiddemod", CmdFSKdemodHID
, 1, "Demodulate graph window as a HID FSK using raw"},
1420 {"fskiodemod", CmdFSKdemodIO
, 1, "Demodulate graph window as an IO Prox FSK using raw"},
1421 {"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)"},
1422 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
1423 {"hexsamples", CmdHexsamples
, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
1424 {"hide", CmdHide
, 1, "Hide graph window"},
1425 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
1426 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
1427 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
1428 {"rtrim", CmdRtrim
, 1, "<location to end trace> -- Trim samples from right of trace"},
1429 {"mandemod", CmdManchesterDemod
, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
1430 {"manrawdecode", Cmdmandecoderaw
, 1, "Manchester decode binary stream already in graph buffer"},
1431 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
1432 {"norm", CmdNorm
, 1, "Normalize max/min to +/-500"},
1433 {"plot", CmdPlot
, 1, "Show graph window (hit 'h' in window for keystroke help)"},
1434 {"samples", CmdSamples
, 0, "[512 - 40000] -- Get raw samples for graph window"},
1435 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
1436 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
1437 {"threshold", CmdThreshold
, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
1438 {"tune", CmdTuneSamples
, 0, "Get hw tune samples for graph window"},
1439 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
1440 {NULL
, NULL
, 0, NULL
}
1443 int CmdData(const char *Cmd
)
1445 CmdsParse(CommandTable
, Cmd
);
1449 int CmdHelp(const char *Cmd
)
1451 CmdsHelp(CommandTable
);