]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmddata.c
6d151140b14997a8f8765cfeef1e8a0a5bfb11a9
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 static int CmdHelp(const char *Cmd
);
26 int CmdAmp(const char *Cmd
)
28 int i
, rising
, falling
;
29 int max
= INT_MIN
, min
= INT_MAX
;
31 for (i
= 10; i
< GraphTraceLen
; ++i
) {
32 if (GraphBuffer
[i
] > max
)
34 if (GraphBuffer
[i
] < min
)
40 for (i
= 0; i
< GraphTraceLen
; ++i
) {
41 if (GraphBuffer
[i
+ 1] < GraphBuffer
[i
]) {
48 if (GraphBuffer
[i
+ 1] > GraphBuffer
[i
]) {
62 * Generic command to demodulate ASK.
64 * Argument is convention: positive or negative (High mod means zero
65 * or high mod means one)
67 * Updates the Graph trace with 0/1 values
72 //this method is dependant on all highs and lows to be the same(or clipped) this could be an issue[marshmellow]
73 //might be able to use clock to help identify highs and lows with some more tolerance
74 //but for now I will try a fuzz factor
75 int Cmdaskdemod(const char *Cmd
)
78 int c
, high
= 0, low
= 0;
80 // TODO: complain if we do not give 2 arguments here !
81 // (AL - this doesn't make sense! we're only using one argument!!!)
82 sscanf(Cmd
, "%i", &c
);
84 /* Detect high and lows and clock */
86 for (i
= 0; i
< GraphTraceLen
; ++i
)
88 if (GraphBuffer
[i
] > high
)
89 high
= GraphBuffer
[i
];
90 else if (GraphBuffer
[i
] < low
)
93 if (c
!= 0 && c
!= 1) {
94 PrintAndLog("Invalid argument: %s", Cmd
);
98 if (GraphBuffer
[0] > 0) {
103 //13% fuzz [marshmellow]
104 high
=(int)(0.87*high
);
106 for (i
= 1; i
< GraphTraceLen
; ++i
) {
107 /* Transitions are detected at each peak
108 * Transitions are either:
109 * - we're low: transition if we hit a high
110 * - we're high: transition if we hit a low
111 * (we need to do it this way because some tags keep high or
112 * low for long periods, others just reach the peak and go
115 //[marhsmellow] change == to >= for high and <= for low for fuzz
116 if ((GraphBuffer
[i
] == high
) && (GraphBuffer
[i
- 1] == c
)) {
117 GraphBuffer
[i
] = 1 - c
;
118 } else if ((GraphBuffer
[i
] == low
) && (GraphBuffer
[i
- 1] == (1 - c
))){
122 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
125 RepaintGraphWindow();
129 void printBitStream(uint8_t BitStream
[], uint32_t bitLen
){
131 if (bitLen
<16) return;
132 if (bitLen
>512) bitLen
=512;
133 for (i
= 0; i
< (bitLen
-16); i
+=16) {
134 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
156 //takes 2 arguments - clock and invert both as integers
157 //prints binary found and saves in graphbuffer for further commands
158 int Cmdaskrawdemod(const char *Cmd
)
161 int invert
=0; //invert default
162 int high
= 0, low
= 0;
163 int clk
=64; //clock default
164 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
165 sscanf(Cmd
, "%i %i", &clk
, &invert
);
167 PrintAndLog("Invalid argument: %s",Cmd
);
170 if (invert
!= 0 && invert
!= 1) {
171 PrintAndLog("Invalid argument: %s", Cmd
);
174 uint32_t initLoopMax
= 1000;
175 if (initLoopMax
>GraphTraceLen
) initLoopMax
=GraphTraceLen
;
176 // Detect high and lows
177 PrintAndLog("Using Clock: %d and invert=%d",clk
,invert
);
178 for (i
= 0; i
< initLoopMax
; ++i
) //1000 samples should be plenty to find high and low values
180 if (GraphBuffer
[i
] > high
)
181 high
= GraphBuffer
[i
];
182 else if (GraphBuffer
[i
] < low
)
183 low
= GraphBuffer
[i
];
185 if ((high
< 30) && ((high
!=1)||(low
!=-1))){ //throw away static - allow 1 and -1 (in case of threshold command first)
186 PrintAndLog("no data found");
189 //13% fuzz in case highs and lows aren't clipped [marshmellow]
190 high
=(int)(0.75*high
);
193 //PrintAndLog("valid high: %d - valid low: %d",high,low);
194 int lastBit
= 0; //set first clock check
195 uint32_t bitnum
= 0; //output counter
196 uint8_t tol
= 32;//clock tolerance adjust
198 uint32_t gLen
= GraphTraceLen
;
199 if (gLen
> 500) gLen
=500;
202 //PrintAndLog("lastbit - %d",lastBit);
204 //loop to find first wave that works
205 for (iii
=0; iii
< gLen
; ++iii
){
206 if ((GraphBuffer
[iii
]>=high
)||(GraphBuffer
[iii
]<=low
)){
208 //loop through to see if this start location works
209 for (i
= iii
; i
< GraphTraceLen
; ++i
) {
210 if ((GraphBuffer
[i
] >= high
) && ((i
-lastBit
)>(clk
-((int)clk
/tol
)))) { // && GraphBuffer[i-1] < high
212 BitStream
[bitnum
] = invert
;
214 } else if ((GraphBuffer
[i
] <= low
) && ((i
-lastBit
)>(clk
-((int)clk
/tol
)))){
215 //low found and we are expecting a bar
217 BitStream
[bitnum
] = 1-invert
;
220 //mid value found or no bar supposed to be here
221 if ((i
-lastBit
)>(clk
+((int)(clk
/tol
)))){
222 //should have hit a high or low based on clock!!
226 PrintAndLog("no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(clk/tol)))),(lastBit+(clk+((int)(clk/tol)))),lastBit);
228 BitStream[bitnum]=77;
234 lastBit
+=clk
;//skip over until hit too many errors
235 if (errCnt
>((GraphTraceLen
/1000)*2)){ //allow 2 errors for every 1000 samples else start over
237 bitnum
=0;//start over
245 if ((bitnum
>64) && (BitStream
[bitnum
-1]!=77)) break;
250 //move BitStream back to GraphBuffer
251 for (i
=0; i
< bitnum
; ++i
){
252 GraphBuffer
[i
]=BitStream
[i
];
254 GraphTraceLen
=bitnum
;
255 RepaintGraphWindow();
259 PrintAndLog("# Error during Demoding: %d\n",errCnt
);
261 PrintAndLog("ASK decoded bitstream:");
262 // Now output the bitstream to the scrollback by line of 16 bits
263 printBitStream(BitStream
,bitnum
);
268 int CmdAutoCorr(const char *Cmd
)
270 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
272 int window
= atoi(Cmd
);
275 PrintAndLog("needs a window");
278 if (window
>= GraphTraceLen
) {
279 PrintAndLog("window must be smaller than trace (%d samples)",
284 PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
286 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
288 for (int j
= 0; j
< window
; ++j
) {
289 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
291 CorrelBuffer
[i
] = sum
;
293 GraphTraceLen
= GraphTraceLen
- window
;
294 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
296 RepaintGraphWindow();
300 int CmdBitsamples(const char *Cmd
)
305 GetFromBigBuf(got
,sizeof(got
),0);
306 WaitForResponse(CMD_ACK
,NULL
);
308 for (int j
= 0; j
< sizeof(got
); j
++) {
309 for (int k
= 0; k
< 8; k
++) {
310 if(got
[j
] & (1 << (7 - k
))) {
311 GraphBuffer
[cnt
++] = 1;
313 GraphBuffer
[cnt
++] = 0;
318 RepaintGraphWindow();
323 * Convert to a bitstream
325 int CmdBitstream(const char *Cmd
)
333 int hithigh
, hitlow
, first
;
335 /* Detect high and lows and clock */
336 for (i
= 0; i
< GraphTraceLen
; ++i
)
338 if (GraphBuffer
[i
] > high
)
339 high
= GraphBuffer
[i
];
340 else if (GraphBuffer
[i
] < low
)
341 low
= GraphBuffer
[i
];
345 clock
= GetClock(Cmd
, high
, 1);
349 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
354 /* Find out if we hit both high and low peaks */
355 for (j
= 0; j
< clock
; ++j
)
357 if (GraphBuffer
[(i
* clock
) + j
] == high
)
359 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
361 /* it doesn't count if it's the first part of our read
362 because it's really just trailing from the last sequence */
363 if (first
&& (hithigh
|| hitlow
))
364 hithigh
= hitlow
= 0;
368 if (hithigh
&& hitlow
)
372 /* If we didn't hit both high and low peaks, we had a bit transition */
373 if (!hithigh
|| !hitlow
)
376 AppendGraph(0, clock
, bit
);
377 // for (j = 0; j < (int)(clock/2); j++)
378 // GraphBuffer[(i * clock) + j] = bit ^ 1;
379 // for (j = (int)(clock/2); j < clock; j++)
380 // GraphBuffer[(i * clock) + j] = bit;
383 RepaintGraphWindow();
387 int CmdBuffClear(const char *Cmd
)
389 UsbCommand c
= {CMD_BUFF_CLEAR
};
395 int CmdDec(const char *Cmd
)
397 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
398 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
400 PrintAndLog("decimated by 2");
401 RepaintGraphWindow();
405 /* Print our clock rate */
406 int CmdDetectClockRate(const char *Cmd
)
408 int clock
= DetectClock(0);
409 PrintAndLog("Auto-detected clock rate: %d", clock
);
413 //demod GraphBuffer wave to 0s and 1s for each wave - 0s for short waves 1s for long waves
414 size_t fsk_wave_demod(int size
)
416 uint32_t last_transition
= 0;
419 // we don't care about actual value, only if it's more or less than a
420 // threshold essentially we capture zero crossings for later analysis
421 for(idx
=1; idx
<size
; idx
++){
422 if(maxVal
<GraphBuffer
[idx
]) maxVal
= GraphBuffer
[idx
];
424 // set close to the top of the wave threshold with 13% margin for error
425 // less likely to get a false transition up there.
426 // (but have to be careful not to go too high and miss some short waves)
427 uint32_t threshold_value
= (uint32_t)(maxVal
*.87);
429 // int threshold_value = 100;
431 // sync to first lo-hi transition, and threshold
432 // PrintAndLog("FSK init complete size: %d",size);//debug
433 // Need to threshold first sample
434 if(GraphBuffer
[0] < threshold_value
) GraphBuffer
[0] = 0;
435 else GraphBuffer
[0] = 1;
437 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
438 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
439 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
440 for(idx
= 1; idx
< size
; idx
++) {
441 // threshold current value
442 if (GraphBuffer
[idx
] < threshold_value
) GraphBuffer
[idx
] = 0;
443 else GraphBuffer
[idx
] = 1;
444 // Check for 0->1 transition
445 if (GraphBuffer
[idx
-1] < GraphBuffer
[idx
]) { // 0 -> 1 transition
446 if (idx
-last_transition
<6){
447 // do nothing with extra garbage (shouldn't be any) noise tolerance?
448 } else if(idx
-last_transition
< 9) {
449 GraphBuffer
[numBits
]=1;
450 // Other fsk demods reverse this making the short waves 1 and long waves 0
451 // this is really backwards... smaller waves will typically be 0 and larger 1 [marshmellow]
452 // but will leave as is and invert when needed later
454 GraphBuffer
[numBits
]=0;
456 last_transition
= idx
;
458 // PrintAndLog("numbits %d",numBits);
461 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
463 uint32_t myround(float f
)
465 if (f
>= UINT_MAX
) return UINT_MAX
;
466 return (uint32_t) (f
+ (float)0.5);
468 //translate 11111100000 to 10
469 size_t aggregate_bits(int size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
, uint8_t invert
) //,uint8_t l2h_crossing_value
471 int lastval
=GraphBuffer
[0];
476 for( idx
=1; idx
< size
; idx
++) {
478 if (GraphBuffer
[idx
]==lastval
) {
482 // if lastval was 1, we have a 1->0 crossing
483 if ( GraphBuffer
[idx
-1]==1 ) {
484 n
=myround((float)(n
+1)/((float)(rfLen
)/(float)8)); //-2 noise tolerance
486 // n=(n+1) / h2l_crossing_value;
487 //truncating could get us into trouble
488 //now we will try with actual clock (RF/64 or RF/50) variable instead
489 //then devide with float casting then truncate after more acurate division
490 //and round to nearest int
491 //like n = (((float)n)/(float)rfLen/(float)10);
492 } else {// 0->1 crossing
493 n
=myround((float)(n
+1)/((float)(rfLen
-2)/(float)10)); // as int 120/6 = 20 as float 120/(64/10) = 18 (18.75)
494 //n=(n+1) / l2h_crossing_value;
496 if (n
== 0) n
= 1; //this should never happen... should we error if it does?
498 if (n
< maxConsequtiveBits
) // Consecutive //when the consecutive bits are low - the noise tolerance can be high
499 //if it is high then we must be careful how much noise tolerance we allow
501 if (invert
==0){ // do not invert bits
502 for (n2
=0; n2
<n
; n2
++){
503 GraphBuffer
[numBits
+n2
]=GraphBuffer
[idx
-1];
505 //memset(GraphBuffer+numBits, GraphBuffer[idx-1] , n);
506 }else{ // invert bits
507 for (n2
=0; n2
<n
; n2
++){
508 GraphBuffer
[numBits
+n2
]=GraphBuffer
[idx
-1]^1;
510 //memset(GraphBuffer+numBits, GraphBuffer[idx-1]^1 , n);
515 lastval
=GraphBuffer
[idx
];
519 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
520 size_t fskdemod(uint8_t rfLen
, uint8_t invert
)
522 //uint8_t h2l_crossing_value = 6;
523 //uint8_t l2h_crossing_value = 5;
525 // if (rfLen==64) //currently only know settings for RF/64 change from default if option entered
527 // h2l_crossing_value=8; //or 8 as 64/8 = 8
528 // l2h_crossing_value=6; //or 6.4 as 64/10 = 6.4
530 size_t size
= GraphTraceLen
;
532 size
= fsk_wave_demod(size
);
533 size
= aggregate_bits(size
,rfLen
,192,invert
);
534 // size = aggregate_bits(size, h2l_crossing_value, l2h_crossing_value,192, invert); //192=no limit to same values
535 //done messing with GraphBuffer - repaint
536 RepaintGraphWindow();
539 uint32_t bytebits_to_byte(int* src
, int numbits
)
542 for(int i
= 0 ; i
< numbits
; i
++)
544 num
= (num
<< 1) | (*src
);
550 //fsk demod and print binary
551 int CmdFSKrawdemod(const char *Cmd
)
553 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
557 //set options from parameters entered with the command
558 if (strlen(Cmd
)>0 && strlen(Cmd
)<=2) {
559 rfLen
=param_get8(Cmd
, 0); //if rfLen option only is used
561 invert
=1; //if invert option only is used
563 } else if(rfLen
==0) rfLen
=50;
566 rfLen
=param_get8(Cmd
, 0); //if both options are used
567 invert
=param_get8(Cmd
,1);
569 PrintAndLog("Args invert: %d \nClock:%d",invert
,rfLen
);
571 size_t size
= fskdemod(rfLen
,invert
);
573 PrintAndLog("FSK decoded bitstream:");
574 // Now output the bitstream to the scrollback by line of 16 bits
575 if(size
> (7*32)+2) size
= (7*32)+2; //only output a max of 7 blocks of 32 bits most tags will have full bit stream inside that sample size
577 for (int i
= 2; i
< (size
-16); i
+=16) {
578 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
600 int CmdFSKdemodHID(const char *Cmd
)
602 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
605 uint8_t invert
=0;//param_get8(Cmd, 0);
607 uint32_t hi2
=0, hi
=0, lo
=0;
609 //get binary from fsk wave
610 size_t size
= fskdemod(rfLen
,invert
);
612 // final loop, go over previously decoded fsk data and now manchester decode into usable tag ID
613 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
614 int frame_marker_mask
[] = {1,1,1,0,0,0};
617 while( idx
+ 6 < size
) {
618 // search for a start of frame marker
620 if ( memcmp(GraphBuffer
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
621 { // frame marker found
622 idx
+=6;//sizeof(frame_marker_mask); //size of int is >6
623 while(GraphBuffer
[idx
] != GraphBuffer
[idx
+1] && idx
< size
-2)
625 // Keep going until next frame marker (or error)
626 // Shift in a bit. Start by shifting high registers
627 hi2
= (hi2
<<1)|(hi
>>31);
628 hi
= (hi
<<1)|(lo
>>31);
629 //Then, shift in a 0 or one into low
630 if (GraphBuffer
[idx
] && !GraphBuffer
[idx
+1]) // 1 0
638 //PrintAndLog("Num shifts: %d ", numshifts);
639 // Hopefully, we read a tag and hit upon the next frame marker
642 if ( memcmp(GraphBuffer
+(idx
), frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
644 if (hi2
!= 0){ //extra large HID tags
645 PrintAndLog("TAG ID: %x%08x%08x (%d)",
646 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
648 else { //standard HID tags <38 bits
649 //Dbprintf("TAG ID: %x%08x (%d)",(unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF); //old print cmd
652 uint32_t cardnum
= 0;
653 if (((hi
>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
655 lo2
=(((hi
& 15) << 12) | (lo
>>20)); //get bits 21-37 to check for format len bit
657 while(lo2
>1){ //find last bit set to 1 (format len bit)
665 cardnum
= (lo
>>1)&0xFFFF;
669 cardnum
= (lo
>>1)&0x7FFFF;
670 fc
= ((hi
&0xF)<<12)|(lo
>>20);
673 cardnum
= (lo
>>1)&0xFFFF;
674 fc
= ((hi
&1)<<15)|(lo
>>17);
677 cardnum
= (lo
>>1)&0xFFFFF;
678 fc
= ((hi
&1)<<11)|(lo
>>21);
681 else { //if bit 38 is not set then 37 bit format is used
686 cardnum
= (lo
>>1)&0x7FFFF;
687 fc
= ((hi
&0xF)<<12)|(lo
>>20);
691 PrintAndLog("TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
692 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF,
693 (unsigned int) bitlen
, (unsigned int) fc
, (unsigned int) cardnum
);
707 if (idx
+ sizeof(frame_marker_mask
) >= size
){
708 PrintAndLog("start bits for hid not found");
709 PrintAndLog("FSK decoded bitstream:");
710 // Now output the bitstream to the scrollback by line of 16 bits
711 if(size
> (7*32)+2) size
= (7*32)+2; //only output a max of 7 blocks of 32 bits most tags will have full bit stream inside that sample size
712 printBitStream(GraphBuffer
,size
);
714 for (int i = 2; i < (size-16); i+=16) {
715 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
740 int CmdFSKdemodIO(const char *Cmd
)
742 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
748 //test samples are not just noise
749 if (GraphTraceLen
< 64) return 0;
750 for(idx
=0;idx
<64;idx
++){
751 if (testMax
<GraphBuffer
[idx
]) testMax
=GraphBuffer
[idx
];
754 //get full binary from fsk wave
755 size_t size
= fskdemod(rfLen
,invert
);
758 //PrintAndLog("testMax %d",testMax);
761 //0 10 20 30 40 50 60
763 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
764 //-----------------------------------------------------------------------------
765 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
767 //XSF(version)facility:codeone+codetwo (raw)
769 int mask
[] = {0,0,0,0,0,0,0,0,0,1};
770 for( idx
=0; idx
< (size
- 74); idx
++) {
771 if ( memcmp(GraphBuffer
+ idx
, mask
, sizeof(mask
))==0) {
773 if (GraphBuffer
[idx
+17]==1 && GraphBuffer
[idx
+26]==1 && GraphBuffer
[idx
+35]==1 && GraphBuffer
[idx
+44]==1 && GraphBuffer
[idx
+53]==1){
774 //confirmed proper separator bits found
776 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
], GraphBuffer
[idx
+1], GraphBuffer
[idx
+2], GraphBuffer
[idx
+3], GraphBuffer
[idx
+4], GraphBuffer
[idx
+5], GraphBuffer
[idx
+6], GraphBuffer
[idx
+7], GraphBuffer
[idx
+8]);
777 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
+9], GraphBuffer
[idx
+10], GraphBuffer
[idx
+11],GraphBuffer
[idx
+12],GraphBuffer
[idx
+13],GraphBuffer
[idx
+14],GraphBuffer
[idx
+15],GraphBuffer
[idx
+16],GraphBuffer
[idx
+17]);
778 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
+18], GraphBuffer
[idx
+19], GraphBuffer
[idx
+20],GraphBuffer
[idx
+21],GraphBuffer
[idx
+22],GraphBuffer
[idx
+23],GraphBuffer
[idx
+24],GraphBuffer
[idx
+25],GraphBuffer
[idx
+26]);
779 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
+27], GraphBuffer
[idx
+28], GraphBuffer
[idx
+29],GraphBuffer
[idx
+30],GraphBuffer
[idx
+31],GraphBuffer
[idx
+32],GraphBuffer
[idx
+33],GraphBuffer
[idx
+34],GraphBuffer
[idx
+35]);
780 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
+36], GraphBuffer
[idx
+37], GraphBuffer
[idx
+38],GraphBuffer
[idx
+39],GraphBuffer
[idx
+40],GraphBuffer
[idx
+41],GraphBuffer
[idx
+42],GraphBuffer
[idx
+43],GraphBuffer
[idx
+44]);
781 PrintAndLog("%d%d%d%d%d%d%d%d %d",GraphBuffer
[idx
+45], GraphBuffer
[idx
+46], GraphBuffer
[idx
+47],GraphBuffer
[idx
+48],GraphBuffer
[idx
+49],GraphBuffer
[idx
+50],GraphBuffer
[idx
+51],GraphBuffer
[idx
+52],GraphBuffer
[idx
+53]);
782 PrintAndLog("%d%d%d%d%d%d%d%d %d%d",GraphBuffer
[idx
+54],GraphBuffer
[idx
+55],GraphBuffer
[idx
+56],GraphBuffer
[idx
+57],GraphBuffer
[idx
+58],GraphBuffer
[idx
+59],GraphBuffer
[idx
+60],GraphBuffer
[idx
+61],GraphBuffer
[idx
+62],GraphBuffer
[idx
+63]);
784 uint32_t code
= bytebits_to_byte(GraphBuffer
+idx
,32);
785 uint32_t code2
= bytebits_to_byte(GraphBuffer
+idx
+32,32);
786 short version
= bytebits_to_byte(GraphBuffer
+idx
+27,8); //14,4
787 uint8_t facilitycode
= bytebits_to_byte(GraphBuffer
+idx
+19,8) ;
788 uint16_t number
= (bytebits_to_byte(GraphBuffer
+idx
+36,8)<<8)|(bytebits_to_byte(GraphBuffer
+idx
+45,8)); //36,9
790 PrintAndLog("XSF(%02d)%02x:%d (%08x%08x)",version
,facilitycode
,number
,code
,code2
);
794 PrintAndLog("thought we had a valid tag but did not match format");
798 if (idx
>= (size
-74)){
799 PrintAndLog("start bits for io prox not found");
800 PrintAndLog("FSK decoded bitstream:");
801 // Now output the bitstream to the scrollback by line of 16 bits
802 if(size
> (7*32)+2) size
= (7*32)+2; //only output a max of 7 blocks of 32 bits most tags will have full bit stream inside that sample size
803 printBitStream(GraphBuffer
,size
);
805 for (int i = 2; i < (size-16); i+=16) {
806 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
830 int CmdFSKdemod(const char *Cmd
) //old CmdFSKdemod needs updating
832 static const int LowTone
[] = {
833 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
834 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
835 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
836 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
837 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
839 static const int HighTone
[] = {
840 1, 1, 1, 1, 1, -1, -1, -1, -1,
841 1, 1, 1, 1, -1, -1, -1, -1,
842 1, 1, 1, 1, -1, -1, -1, -1,
843 1, 1, 1, 1, -1, -1, -1, -1,
844 1, 1, 1, 1, -1, -1, -1, -1,
845 1, 1, 1, 1, -1, -1, -1, -1, -1,
848 int lowLen
= sizeof (LowTone
) / sizeof (int);
849 int highLen
= sizeof (HighTone
) / sizeof (int);
850 int convLen
= (highLen
> lowLen
) ? highLen
: lowLen
; //if highlen > lowLen then highlen else lowlen
851 uint32_t hi
= 0, lo
= 0;
854 int minMark
= 0, maxMark
= 0;
856 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
857 int lowSum
= 0, highSum
= 0;
859 for (j
= 0; j
< lowLen
; ++j
) {
860 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
862 for (j
= 0; j
< highLen
; ++j
) {
863 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
865 lowSum
= abs(100 * lowSum
/ lowLen
);
866 highSum
= abs(100 * highSum
/ highLen
);
867 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
870 for(i
= 0; i
< GraphTraceLen
- convLen
- 16; ++i
) {
871 int lowTot
= 0, highTot
= 0;
872 // 10 and 8 are f_s divided by f_l and f_h, rounded
873 for (j
= 0; j
< 10; ++j
) {
874 lowTot
+= (GraphBuffer
[i
+j
] & 0xffff);
876 for (j
= 0; j
< 8; j
++) {
877 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
879 GraphBuffer
[i
] = lowTot
- highTot
;
880 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
881 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
884 GraphTraceLen
-= (convLen
+ 16);
885 RepaintGraphWindow();
887 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
888 int max
= 0, maxPos
= 0;
889 for (i
= 0; i
< 6000; ++i
) {
891 for (j
= 0; j
< 3 * lowLen
; ++j
) {
892 dec
-= GraphBuffer
[i
+ j
];
894 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
895 dec
+= GraphBuffer
[i
+ j
];
903 // place start of bit sync marker in graph
904 GraphBuffer
[maxPos
] = maxMark
;
905 GraphBuffer
[maxPos
+ 1] = minMark
;
909 // place end of bit sync marker in graph
910 GraphBuffer
[maxPos
] = maxMark
;
911 GraphBuffer
[maxPos
+1] = minMark
;
913 PrintAndLog("actual data bits start at sample %d", maxPos
);
914 PrintAndLog("length %d/%d", highLen
, lowLen
);
917 bits
[sizeof(bits
)-1] = '\0';
919 // find bit pairs and manchester decode them
920 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
922 for (j
= 0; j
< lowLen
; ++j
) {
923 dec
-= GraphBuffer
[maxPos
+ j
];
925 for (; j
< lowLen
+ highLen
; ++j
) {
926 dec
+= GraphBuffer
[maxPos
+ j
];
929 // place inter bit marker in graph
930 GraphBuffer
[maxPos
] = maxMark
;
931 GraphBuffer
[maxPos
+ 1] = minMark
;
933 // hi and lo form a 64 bit pair
934 hi
= (hi
<< 1) | (lo
>> 31);
936 // store decoded bit as binary (in hi/lo) and text (in bits[])
944 PrintAndLog("bits: '%s'", bits
);
945 PrintAndLog("hex: %08x %08x", hi
, lo
);
949 int CmdGrid(const char *Cmd
)
951 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
952 PlotGridXdefault
= PlotGridX
;
953 PlotGridYdefault
= PlotGridY
;
954 RepaintGraphWindow();
958 int CmdHexsamples(const char *Cmd
)
964 char* string_ptr
= string_buf
;
967 sscanf(Cmd
, "%i %i", &requested
, &offset
);
969 /* if no args send something */
970 if (requested
== 0) {
973 if (offset
+ requested
> sizeof(got
)) {
974 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000");
978 GetFromBigBuf(got
,requested
,offset
);
979 WaitForResponse(CMD_ACK
,NULL
);
982 for (j
= 0; j
< requested
; j
++) {
984 string_ptr
+= sprintf(string_ptr
, "%02x ", got
[j
]);
986 *(string_ptr
- 1) = '\0'; // remove the trailing space
987 PrintAndLog("%s", string_buf
);
988 string_buf
[0] = '\0';
989 string_ptr
= string_buf
;
992 if (j
== requested
- 1 && string_buf
[0] != '\0') { // print any remaining bytes
993 *(string_ptr
- 1) = '\0';
994 PrintAndLog("%s", string_buf
);
995 string_buf
[0] = '\0';
1001 int CmdHide(const char *Cmd
)
1007 int CmdHpf(const char *Cmd
)
1012 for (i
= 10; i
< GraphTraceLen
; ++i
)
1013 accum
+= GraphBuffer
[i
];
1014 accum
/= (GraphTraceLen
- 10);
1015 for (i
= 0; i
< GraphTraceLen
; ++i
)
1016 GraphBuffer
[i
] -= accum
;
1018 RepaintGraphWindow();
1022 int CmdSamples(const char *Cmd
)
1028 n
= strtol(Cmd
, NULL
, 0);
1029 if (n
== 0) n
= 6000;
1030 if (n
> sizeof(got
)) n
= sizeof(got
);
1032 PrintAndLog("Reading %d samples\n", n
);
1033 GetFromBigBuf(got
,n
,0);
1034 WaitForResponse(CMD_ACK
,NULL
);
1035 for (int j
= 0; j
< n
; j
++) {
1036 GraphBuffer
[cnt
++] = ((int)got
[j
]) - 128;
1039 PrintAndLog("Done!\n");
1041 RepaintGraphWindow();
1045 int CmdTuneSamples(const char *Cmd
)
1051 PrintAndLog("Reading %d samples\n", n
);
1052 GetFromBigBuf(got
,n
,7256); // armsrc/apps.h: #define FREE_BUFFER_OFFSET 7256
1053 WaitForResponse(CMD_ACK
,NULL
);
1054 for (int j
= 0; j
< n
; j
++) {
1055 GraphBuffer
[cnt
++] = ((int)got
[j
]) - 128;
1058 PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n");
1061 RepaintGraphWindow();
1065 int CmdLoad(const char *Cmd
)
1067 FILE *f
= fopen(Cmd
, "r");
1069 PrintAndLog("couldn't open '%s'", Cmd
);
1075 while (fgets(line
, sizeof (line
), f
)) {
1076 GraphBuffer
[GraphTraceLen
] = atoi(line
);
1080 PrintAndLog("loaded %d samples", GraphTraceLen
);
1081 RepaintGraphWindow();
1085 int CmdLtrim(const char *Cmd
)
1089 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
1090 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
1091 GraphTraceLen
-= ds
;
1093 RepaintGraphWindow();
1098 * Manchester demodulate a bitstream. The bitstream needs to be already in
1099 * the GraphBuffer as 0 and 1 values
1101 * Give the clock rate as argument in order to help the sync - the algorithm
1102 * resyncs at each pulse anyway.
1104 * Not optimized by any means, this is the 1st time I'm writing this type of
1105 * routine, feel free to improve...
1107 * 1st argument: clock rate (as number of samples per clock rate)
1108 * Typical values can be 64, 32, 128...
1110 int CmdManchesterDemod(const char *Cmd
)
1112 int i
, j
, invert
= 0;
1118 int hithigh
, hitlow
, first
;
1124 /* check if we're inverting output */
1127 PrintAndLog("Inverting output");
1132 while(*Cmd
== ' '); // in case a 2nd argument was given
1135 /* Holds the decoded bitstream: each clock period contains 2 bits */
1136 /* later simplified to 1 bit after manchester decoding. */
1137 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
1138 /* int BitStream[GraphTraceLen*2/clock+10]; */
1140 /* But it does not work if compiling on WIndows: therefore we just allocate a */
1142 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
] = {0};
1144 /* Detect high and lows */
1145 for (i
= 0; i
< GraphTraceLen
; i
++)
1147 if (GraphBuffer
[i
] > high
)
1148 high
= GraphBuffer
[i
];
1149 else if (GraphBuffer
[i
] < low
)
1150 low
= GraphBuffer
[i
];
1154 clock
= GetClock(Cmd
, high
, 1);
1156 int tolerance
= clock
/4;
1158 /* Detect first transition */
1159 /* Lo-Hi (arbitrary) */
1160 /* skip to the first high */
1161 for (i
= 0; i
< GraphTraceLen
; i
++)
1162 if (GraphBuffer
[i
] == high
)
1164 /* now look for the first low */
1165 for (; i
< GraphTraceLen
; i
++)
1167 if (GraphBuffer
[i
] == low
)
1174 /* If we're not working with 1/0s, demod based off clock */
1177 bit
= 0; /* We assume the 1st bit is zero, it may not be
1178 * the case: this routine (I think) has an init problem.
1181 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
1187 /* Find out if we hit both high and low peaks */
1188 for (j
= 0; j
< clock
; j
++)
1190 if (GraphBuffer
[(i
* clock
) + j
] == high
)
1192 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
1195 /* it doesn't count if it's the first part of our read
1196 because it's really just trailing from the last sequence */
1197 if (first
&& (hithigh
|| hitlow
))
1198 hithigh
= hitlow
= 0;
1202 if (hithigh
&& hitlow
)
1206 /* If we didn't hit both high and low peaks, we had a bit transition */
1207 if (!hithigh
|| !hitlow
)
1210 BitStream
[bit2idx
++] = bit
^ invert
;
1214 /* standard 1/0 bitstream */
1218 /* Then detect duration between 2 successive transitions */
1219 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
1221 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
1226 // Error check: if bitidx becomes too large, we do not
1227 // have a Manchester encoded bitstream or the clock is really
1229 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
1230 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
1233 // Then switch depending on lc length:
1234 // Tolerance is 1/4 of clock rate (arbitrary)
1235 if (abs(lc
-clock
/2) < tolerance
) {
1236 // Short pulse : either "1" or "0"
1237 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1238 } else if (abs(lc
-clock
) < tolerance
) {
1239 // Long pulse: either "11" or "00"
1240 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1241 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1245 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
1246 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
1250 PrintAndLog("Error: too many detection errors, aborting.");
1257 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
1258 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
1259 // to stop output at the final bitidx2 value, not bitidx
1260 for (i
= 0; i
< bitidx
; i
+= 2) {
1261 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
1262 BitStream
[bit2idx
++] = 1 ^ invert
;
1263 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
1264 BitStream
[bit2idx
++] = 0 ^ invert
;
1266 // We cannot end up in this state, this means we are unsynchronized,
1270 PrintAndLog("Unsynchronized, resync...");
1271 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
1275 PrintAndLog("Error: too many decode errors, aborting.");
1282 PrintAndLog("Manchester decoded bitstream");
1283 // Now output the bitstream to the scrollback by line of 16 bits
1284 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
1285 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
1306 /* Modulate our data into manchester */
1307 int CmdManchesterMod(const char *Cmd
)
1311 int bit
, lastbit
, wave
;
1314 clock
= GetClock(Cmd
, 0, 1);
1318 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
1320 bit
= GraphBuffer
[i
* clock
] ^ 1;
1322 for (j
= 0; j
< (int)(clock
/2); j
++)
1323 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
1324 for (j
= (int)(clock
/2); j
< clock
; j
++)
1325 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
1327 /* Keep track of how we start our wave and if we changed or not this time */
1328 wave
^= bit
^ lastbit
;
1332 RepaintGraphWindow();
1336 int CmdNorm(const char *Cmd
)
1339 int max
= INT_MIN
, min
= INT_MAX
;
1341 for (i
= 10; i
< GraphTraceLen
; ++i
) {
1342 if (GraphBuffer
[i
] > max
)
1343 max
= GraphBuffer
[i
];
1344 if (GraphBuffer
[i
] < min
)
1345 min
= GraphBuffer
[i
];
1349 for (i
= 0; i
< GraphTraceLen
; ++i
) {
1350 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 1000 /
1354 RepaintGraphWindow();
1358 int CmdPlot(const char *Cmd
)
1364 int CmdSave(const char *Cmd
)
1366 FILE *f
= fopen(Cmd
, "w");
1368 PrintAndLog("couldn't open '%s'", Cmd
);
1372 for (i
= 0; i
< GraphTraceLen
; i
++) {
1373 fprintf(f
, "%d\n", GraphBuffer
[i
]);
1376 PrintAndLog("saved to '%s'", Cmd
);
1380 int CmdScale(const char *Cmd
)
1382 CursorScaleFactor
= atoi(Cmd
);
1383 if (CursorScaleFactor
== 0) {
1384 PrintAndLog("bad, can't have zero scale");
1385 CursorScaleFactor
= 1;
1387 RepaintGraphWindow();
1391 int CmdThreshold(const char *Cmd
)
1393 int threshold
= atoi(Cmd
);
1395 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1396 if (GraphBuffer
[i
] >= threshold
)
1399 GraphBuffer
[i
] = -1;
1401 RepaintGraphWindow();
1405 int CmdDirectionalThreshold(const char *Cmd
)
1407 int8_t upThres
= param_get8(Cmd
, 0);
1408 int8_t downThres
= param_get8(Cmd
, 1);
1410 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres
, downThres
);
1412 int lastValue
= GraphBuffer
[0];
1413 GraphBuffer
[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
1415 for (int i
= 1; i
< GraphTraceLen
; ++i
) {
1416 // Apply first threshold to samples heading up
1417 if (GraphBuffer
[i
] >= upThres
&& GraphBuffer
[i
] > lastValue
)
1419 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1422 // Apply second threshold to samples heading down
1423 else if (GraphBuffer
[i
] <= downThres
&& GraphBuffer
[i
] < lastValue
)
1425 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1426 GraphBuffer
[i
] = -1;
1430 lastValue
= GraphBuffer
[i
]; // Buffer last value as we overwrite it.
1431 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1435 GraphBuffer
[0] = GraphBuffer
[1]; // Aline with first edited sample.
1436 RepaintGraphWindow();
1440 int CmdZerocrossings(const char *Cmd
)
1442 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1449 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
1450 if (GraphBuffer
[i
] * sign
>= 0) {
1451 // No change in sign, reproduce the previous sample count.
1453 GraphBuffer
[i
] = lastZc
;
1455 // Change in sign, reset the sample count.
1457 GraphBuffer
[i
] = lastZc
;
1465 RepaintGraphWindow();
1469 static command_t CommandTable
[] =
1471 {"help", CmdHelp
, 1, "This help"},
1472 {"amp", CmdAmp
, 1, "Amplify peaks"},
1473 {"askdemod", Cmdaskdemod
, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
1474 {"askrawdemod", Cmdaskrawdemod
, 1, "[clock] [invert<0 or 1>] -- Attempt to demodulate simple ASK tags and output binary (args optional-defaults='64 0'"},
1475 {"autocorr", CmdAutoCorr
, 1, "<window length> -- Autocorrelation over window"},
1476 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
1477 {"bitstream", CmdBitstream
, 1, "[clock rate] -- Convert waveform into a bitstream"},
1478 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
1479 {"dec", CmdDec
, 1, "Decimate samples"},
1480 {"detectclock", CmdDetectClockRate
, 1, "Detect clock rate"},
1481 {"fskdemod", CmdFSKdemod
, 1, "Demodulate graph window as a HID FSK"},
1482 {"fskhiddemod", CmdFSKdemodHID
, 1, "Demodulate graph window as a HID FSK using raw"},
1483 {"fskiodemod", CmdFSKdemodIO
, 1, "Demodulate graph window as an IO Prox FSK using raw"},
1484 {"fskrawdemod", CmdFSKrawdemod
, 1, "[clock rate] [invert] Demodulate graph window from FSK to binary (clock = 64 or 50)(invert = 1 or 0)"},
1485 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
1486 {"hexsamples", CmdHexsamples
, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
1487 {"hide", CmdHide
, 1, "Hide graph window"},
1488 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
1489 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
1490 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
1491 {"mandemod", CmdManchesterDemod
, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
1492 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
1493 {"norm", CmdNorm
, 1, "Normalize max/min to +/-500"},
1494 {"plot", CmdPlot
, 1, "Show graph window (hit 'h' in window for keystroke help)"},
1495 {"samples", CmdSamples
, 0, "[512 - 40000] -- Get raw samples for graph window"},
1496 {"tune", CmdTuneSamples
, 0, "Get hw tune samples for graph window"},
1497 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
1498 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
1499 {"threshold", CmdThreshold
, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
1500 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
1501 {"dirthreshold", CmdDirectionalThreshold
, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
1502 {NULL
, NULL
, 0, NULL
}
1505 int CmdData(const char *Cmd
)
1507 CmdsParse(CommandTable
, Cmd
);
1511 int CmdHelp(const char *Cmd
)
1513 CmdsHelp(CommandTable
);