]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - common/lfdemod.c
1 //-----------------------------------------------------------------------------
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 // Low frequency commands
9 //-----------------------------------------------------------------------------
16 //takes 1s and 0s and searches for EM410x format - output EM ID
17 uint64_t Em410xDecode(uint8_t *BitStream
,uint32_t BitLen
)
19 //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
20 // otherwise could be a void with no arguments
23 uint64_t lo
=0; //hi=0,
26 uint32_t initLoopMax
= 65;
27 if (initLoopMax
>BitLen
) initLoopMax
=BitLen
;
29 for (;i
< initLoopMax
; ++i
) //65 samples should be plenty to find high and low values
31 if (BitStream
[i
] > high
)
33 else if (BitStream
[i
] < low
)
36 if (((high
!=1)||(low
!=0))){ //allow only 1s and 0s
37 // PrintAndLog("no data found");
41 // 111111111 bit pattern represent start of frame
42 uint8_t frame_marker_mask
[] = {1,1,1,1,1,1,1,1,1};
46 while( (idx
+ 64) < BitLen
) {
48 // search for a start of frame marker
49 if ( memcmp(BitStream
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
50 { // frame marker found
51 idx
+=9;//sizeof(frame_marker_mask);
53 for(ii
=0; ii
<5; ++ii
){
54 parityTest
+= BitStream
[(i
*5)+ii
+idx
];
56 if (parityTest
== ((parityTest
>>1)<<1)){
58 for (ii
=0; ii
<4;++ii
){
59 //hi = (hi<<1)|(lo>>31);
60 lo
=(lo
<<1LL)|(BitStream
[(i
*5)+ii
+idx
]);
62 //PrintAndLog("DEBUG: EM parity passed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d,lo: %d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1],lo);
63 }else {//parity failed
64 //PrintAndLog("DEBUG: EM parity failed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1]);
67 if (resetCnt
>5)return 0;
69 goto restart
;//continue;
72 //skip last 5 bit parity test for simplicity.
82 //takes 2 arguments - clock and invert both as integers
83 //attempts to demodulate ask while decoding manchester
84 //prints binary found and saves in graphbuffer for further commands
85 int askmandemod(uint8_t * BinStream
,uint32_t *BitLen
,int *clk
, int *invert
)
88 int high
= 0, low
= 128;
89 *clk
=DetectASKClock(BinStream
,(size_t)*BitLen
,*clk
); //clock default
93 if (*invert
!= 1) *invert
=0;
95 uint32_t initLoopMax
= 200;
96 if (initLoopMax
>*BitLen
) initLoopMax
=*BitLen
;
98 // Detect high and lows
99 for (i
= 0; i
< initLoopMax
; ++i
) //200 samples should be enough to find high and low values
101 if (BinStream
[i
] > high
)
103 else if (BinStream
[i
] < low
)
106 if ((high
< 158) ){ //throw away static
109 //25% fuzz in case highs and lows aren't clipped [marshmellow]
110 high
=(int)((high
-128)*.75)+128;
111 low
= (int)((low
-128)*.75)+128;
113 //PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
114 int lastBit
= 0; //set first clock check
115 uint32_t bitnum
= 0; //output counter
116 int tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
117 if (*clk
==32)tol
=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
119 uint32_t gLen
= *BitLen
;
120 if (gLen
> 3000) gLen
=3000;
122 uint32_t bestStart
= *BitLen
;
123 uint32_t bestErrCnt
= (*BitLen
/1000);
124 uint32_t maxErr
= (*BitLen
/1000);
126 //loop to find first wave that works
127 for (iii
=0; iii
< gLen
; ++iii
){
128 if ((BinStream
[iii
]>=high
)||(BinStream
[iii
]<=low
)){
131 //loop through to see if this start location works
132 for (i
= iii
; i
< *BitLen
; ++i
) {
133 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
135 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
136 //low found and we are expecting a bar
139 //mid value found or no bar supposed to be here
140 if ((i
-lastBit
)>(*clk
+tol
)){
141 //should have hit a high or low based on clock!!
144 lastBit
+=*clk
;//skip over until hit too many errors
145 if (errCnt
>(maxErr
)) break; //allow 1 error for every 1000 samples else start over
148 if ((i
-iii
) >(400 * *clk
)) break; //got plenty of bits
150 //we got more than 64 good bits and not all errors
151 if ((((i
-iii
)/ *clk
) > (64+errCnt
)) && (errCnt
<maxErr
)) {
156 break; //great read - finish
158 if (errCnt
<bestErrCnt
){ //set this as new best run
165 if (bestErrCnt
<maxErr
){
166 //best run is good enough set to best run and set overwrite BinStream
168 lastBit
=bestStart
-*clk
;
170 for (i
= iii
; i
< *BitLen
; ++i
) {
171 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
173 BinStream
[bitnum
] = *invert
;
175 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
176 //low found and we are expecting a bar
178 BinStream
[bitnum
] = 1-*invert
;
181 //mid value found or no bar supposed to be here
182 if ((i
-lastBit
)>(*clk
+tol
)){
183 //should have hit a high or low based on clock!!
186 BinStream
[bitnum
]=77;
190 lastBit
+=*clk
;//skip over error
193 if (bitnum
>=400) break;
205 //take 10 and 01 and manchester decode
206 //run through 2 times and take least errCnt
207 int manrawdecode(uint8_t * BitStream
, int *bitLen
)
215 for (ii
=1;ii
<3;++ii
){
217 for (i
=i
+ii
;i
<*bitLen
-2;i
+=2){
218 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
219 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
223 if(bitnum
>300) break;
235 for (i
=i
+ii
;i
<*bitLen
-2;i
+=2){
236 if(BitStream
[i
]==1 && (BitStream
[i
+1]==0)){
237 BitStream
[bitnum
++]=0;
238 } else if((BitStream
[i
]==0)&& BitStream
[i
+1]==1){
239 BitStream
[bitnum
++]=1;
241 BitStream
[bitnum
++]=77;
244 if(bitnum
>300) break;
253 //take 01 or 10 = 0 and 11 or 00 = 1
254 int BiphaseRawDecode(uint8_t * BitStream
, int *bitLen
, int offset
)
260 for (;i
<*bitLen
-2;i
+=2){
261 if((BitStream
[i
]==1 && BitStream
[i
+1]==0)||(BitStream
[i
]==0 && BitStream
[i
+1]==1)){
262 BitStream
[bitnum
++]=1;
263 } else if((BitStream
[i
]==0 && BitStream
[i
+1]==0)||(BitStream
[i
]==1 && BitStream
[i
+1]==1)){
264 BitStream
[bitnum
++]=0;
266 BitStream
[bitnum
++]=77;
269 if(bitnum
>250) break;
276 //takes 2 arguments - clock and invert both as integers
277 //attempts to demodulate ask only
278 //prints binary found and saves in graphbuffer for further commands
279 int askrawdemod(uint8_t *BinStream
, int *bitLen
,int *clk
, int *invert
)
282 // int invert=0; //invert default
283 int high
= 0, low
= 128;
284 *clk
=DetectASKClock(BinStream
,*bitLen
,*clk
); //clock default
285 uint8_t BitStream
[502] = {0};
287 if (*clk
<8) *clk
=64;
288 if (*clk
<32) *clk
=32;
289 if (*invert
!= 1) *invert
= 0;
291 uint32_t initLoopMax
= 200;
292 if (initLoopMax
>*bitLen
) initLoopMax
=*bitLen
;
293 // Detect high and lows
294 for (i
= 0; i
< initLoopMax
; ++i
) //200 samples should be plenty to find high and low values
296 if (BinStream
[i
] > high
)
298 else if (BinStream
[i
] < low
)
301 if ((high
< 158)){ //throw away static
304 //25% fuzz in case highs and lows aren't clipped [marshmellow]
305 high
=(int)((high
-128)*.75)+128;
306 low
= (int)((low
-128)*.75)+128;
308 int lastBit
= 0; //set first clock check
309 uint32_t bitnum
= 0; //output counter
310 uint8_t tol
= 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
311 if (*clk
==32) tol
=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
313 uint32_t gLen
= *bitLen
;
314 if (gLen
> 500) gLen
=500;
316 uint32_t bestStart
= *bitLen
;
317 uint32_t bestErrCnt
= (*bitLen
/1000);
320 //loop to find first wave that works
321 for (iii
=0; iii
< gLen
; ++iii
){
322 if ((BinStream
[iii
]>=high
)||(BinStream
[iii
]<=low
)){
324 //loop through to see if this start location works
325 for (i
= iii
; i
< *bitLen
; ++i
) {
326 if ((BinStream
[i
] >= high
) && ((i
-lastBit
)>(*clk
-tol
))){
328 BitStream
[bitnum
] = *invert
;
331 } else if ((BinStream
[i
] <= low
) && ((i
-lastBit
)>(*clk
-tol
))){
332 //low found and we are expecting a bar
334 BitStream
[bitnum
] = 1-*invert
;
337 } else if ((BinStream
[i
]<=low
) && (midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
340 BitStream
[bitnum
]= 1-*invert
;
342 } else if ((BinStream
[i
]>=high
)&&(midBit
==0) && ((i
-lastBit
)>((*clk
/2)-tol
))){
345 BitStream
[bitnum
]= *invert
;
347 } else if ((i
-lastBit
)>((*clk
/2)+tol
)&&(midBit
==0)){
350 BitStream
[bitnum
]= BitStream
[bitnum
-1];
353 //mid value found or no bar supposed to be here
355 if ((i
-lastBit
)>(*clk
+tol
)){
356 //should have hit a high or low based on clock!!
359 BitStream
[bitnum
]=77;
364 lastBit
+=*clk
;//skip over until hit too many errors
365 if (errCnt
>((*bitLen
/1000))){ //allow 1 error for every 1000 samples else start over
367 bitnum
=0;//start over
372 if (bitnum
>500) break;
374 //we got more than 64 good bits and not all errors
375 if ((bitnum
> (64+errCnt
)) && (errCnt
<(*bitLen
/1000))) {
377 if (errCnt
==0) break; //great read - finish
378 if (bestStart
== iii
) break; //if current run == bestErrCnt run (after exhausted testing) then finish
379 if (errCnt
<bestErrCnt
){ //set this as new best run
385 if (iii
>=gLen
){ //exhausted test
386 //if there was a ok test go back to that one and re-run the best run (then dump after that run)
387 if (bestErrCnt
< (*bitLen
/1000)) iii
=bestStart
;
392 for (i
=0; i
< bitnum
; ++i
){
393 BinStream
[i
]=BitStream
[i
];
401 //translate wave to 11111100000 (1 for each short wave 0 for each long wave)
402 size_t fsk_wave_demod(uint8_t * dest
, size_t size
, uint8_t fchigh
, uint8_t fclow
)
404 uint32_t last_transition
= 0;
407 if (fchigh
==0) fchigh
=10;
408 if (fclow
==0) fclow
=8;
409 // we do care about the actual theshold value as sometimes near the center of the
410 // wave we may get static that changes direction of wave for one value
411 // if our value is too low it might affect the read. and if our tag or
412 // antenna is weak a setting too high might not see anything. [marshmellow]
413 if (size
<100) return 0;
414 for(idx
=1; idx
<100; idx
++){
415 if(maxVal
<dest
[idx
]) maxVal
= dest
[idx
];
417 // set close to the top of the wave threshold with 25% margin for error
418 // less likely to get a false transition up there.
419 // (but have to be careful not to go too high and miss some short waves)
420 uint8_t threshold_value
= (uint8_t)(((maxVal
-128)*.75)+128);
422 // sync to first lo-hi transition, and threshold
423 // Need to threshold first sample
425 if(dest
[0] < threshold_value
) dest
[0] = 0;
429 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
430 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
431 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
432 for(idx
= 1; idx
< size
; idx
++) {
433 // threshold current value
435 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
438 // Check for 0->1 transition
439 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
440 if ((idx
-last_transition
)<(fclow
-2)){ //0-5 = garbage noise
441 //do nothing with extra garbage
442 } else if ((idx
-last_transition
) < (fchigh
-1)) { //6-8 = 8 waves
444 } else { //9+ = 10 waves
447 last_transition
= idx
;
451 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
454 uint32_t myround2(float f
)
456 if (f
>= 2000) return 2000;//something bad happened
457 return (uint32_t) (f
+ (float)0.5);
460 //translate 11111100000 to 10
461 size_t aggregate_bits(uint8_t *dest
,size_t size
, uint8_t rfLen
, uint8_t maxConsequtiveBits
, uint8_t invert
,uint8_t fchigh
,uint8_t fclow
)// uint8_t h2l_crossing_value,uint8_t l2h_crossing_value,
463 uint8_t lastval
=dest
[0];
468 for( idx
=1; idx
< size
; idx
++) {
470 if (dest
[idx
]==lastval
) {
474 //if lastval was 1, we have a 1->0 crossing
475 if ( dest
[idx
-1]==1 ) {
476 n
=myround2((float)(n
+1)/((float)(rfLen
)/(float)fclow
));
477 //n=(n+1) / h2l_crossing_value;
478 } else {// 0->1 crossing
479 n
=myround2((float)(n
+1)/((float)(rfLen
-2)/(float)fchigh
)); //-2 for fudge factor
480 //n=(n+1) / l2h_crossing_value;
484 if(n
< maxConsequtiveBits
) //Consecutive
486 if(invert
==0){ //invert bits
487 memset(dest
+numBits
, dest
[idx
-1] , n
);
489 memset(dest
+numBits
, dest
[idx
-1]^1 , n
);
498 //by marshmellow (from holiman's base)
499 // full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
500 int fskdemod(uint8_t *dest
, size_t size
, uint8_t rfLen
, uint8_t invert
, uint8_t fchigh
, uint8_t fclow
)
503 size
= fsk_wave_demod(dest
, size
, fchigh
, fclow
);
504 size
= aggregate_bits(dest
, size
,rfLen
,192,invert
,fchigh
,fclow
);
507 // loop to get raw HID waveform then FSK demodulate the TAG ID from it
508 int HIDdemodFSK(uint8_t *dest
, size_t size
, uint32_t *hi2
, uint32_t *hi
, uint32_t *lo
)
511 size_t idx
=0; //, found=0; //size=0,
513 size
= fskdemod(dest
, size
,50,0,10,8);
515 // final loop, go over previously decoded manchester data and decode into usable tag ID
516 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
517 uint8_t frame_marker_mask
[] = {1,1,1,0,0,0};
521 while( idx
+ sizeof(frame_marker_mask
) < size
) {
522 // search for a start of frame marker
523 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
524 { // frame marker found
525 idx
+=sizeof(frame_marker_mask
);
526 while(dest
[idx
] != dest
[idx
+1] && idx
< size
-2)
528 // Keep going until next frame marker (or error)
529 // Shift in a bit. Start by shifting high registers
530 *hi2
= (*hi2
<<1)|(*hi
>>31);
531 *hi
= (*hi
<<1)|(*lo
>>31);
532 //Then, shift in a 0 or one into low
533 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
540 // Hopefully, we read a tag and hit upon the next frame marker
541 if(idx
+ sizeof(frame_marker_mask
) < size
)
543 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
550 *hi2
= *hi
= *lo
= 0;
559 uint32_t bytebits_to_byte(uint8_t* src
, int numbits
)
562 for(int i
= 0 ; i
< numbits
; i
++) {
563 num
= (num
<< 1) | (*src
);
569 int IOdemodFSK(uint8_t *dest
, size_t size
)
572 //make sure buffer has data
573 if (size
< 66) return -1;
574 //test samples are not just noise
576 for(idx
=0;idx
<65;idx
++){
577 if (testMax
<dest
[idx
]) testMax
=dest
[idx
];
583 size
= fskdemod(dest
, size
,64,1,10,8); // RF/64 and invert
584 if (size
< 65) return -1; //did we get a good demod?
586 //0 10 20 30 40 50 60
588 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
589 //-----------------------------------------------------------------------------
590 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
592 //XSF(version)facility:codeone+codetwo
594 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,1};
595 for( idx
=0; idx
< (size
- 65); idx
++) {
596 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
))==0) {
598 if (!dest
[idx
+8] && dest
[idx
+17]==1 && dest
[idx
+26]==1 && dest
[idx
+35]==1 && dest
[idx
+44]==1 && dest
[idx
+53]==1){
599 //confirmed proper separator bits found
600 //return start position
610 // not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
611 // maybe somehow adjust peak trimming value based on samples to fix?
612 int DetectASKClock(uint8_t dest
[], size_t size
, int clock
)
617 int clk
[]={16,32,40,50,64,100,128,256};
618 int loopCnt
= 256; //don't need to loop through entire array...
619 if (size
<loopCnt
) loopCnt
= size
;
621 //if we already have a valid clock quit
623 if (clk
[i
]==clock
) return clock
;
625 //get high and low peak
626 for (i
=0;i
<loopCnt
;++i
){
634 peak
=(int)((peak
-128)*.75)+128;
635 low
= (int)((low
-128)*.75)+128;
640 int errCnt
[]={0,0,0,0,0,0,0,0};
641 //test each valid clock from smallest to greatest to see which lines up
642 for(clkCnt
=0; clkCnt
<6;++clkCnt
){
643 if (clk
[clkCnt
]==32){
649 //try lining up the peaks by moving starting point (try first 256)
650 for (ii
=0; ii
<loopCnt
; ++ii
){
651 if ((dest
[ii
]>=peak
) || (dest
[ii
]<=low
)){
653 // now that we have the first one lined up test rest of wave array
654 for (i
=0; i
<((int)(size
/clk
[clkCnt
])-1); ++i
){
655 if (dest
[ii
+(i
*clk
[clkCnt
])]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])]<=low
){
656 }else if(dest
[ii
+(i
*clk
[clkCnt
])-tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])-tol
]<=low
){
657 }else if(dest
[ii
+(i
*clk
[clkCnt
])+tol
]>=peak
|| dest
[ii
+(i
*clk
[clkCnt
])+tol
]<=low
){
658 }else{ //error no peak detected
662 //if we found no errors this is correct one - return this clock
663 if(errCnt
[clkCnt
]==0) return clk
[clkCnt
];
664 //if we found errors see if it is lowest so far and save it as best run
665 if(errCnt
[clkCnt
]<bestErr
) bestErr
=errCnt
[clkCnt
];
671 for (iii
=0; iii
<6;++iii
){
672 if (errCnt
[iii
]<errCnt
[best
]){