Created new detectclock function + EM decode addons

new detectclock is somewhat more reliable for ASK modulated tags.  added
this detect to askrawdemod if no clock in passed as an argument.  also
added more EM ID formats to output
This commit is contained in:
marshmellow42 2014-12-24 11:48:41 -05:00
parent cd48c19c31
commit 0e74c023bd
2 changed files with 113 additions and 31 deletions

View file

@ -11,6 +11,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include <limits.h> #include <limits.h>
#include "proxmark3.h" #include "proxmark3.h"
#include "data.h" #include "data.h"
@ -183,7 +185,7 @@ int Em410xDecode(const char *Cmd)
// otherwise could be a void with no arguments // otherwise could be a void with no arguments
//set defaults //set defaults
int high=0, low=0; int high=0, low=0;
uint32_t hi=0, lo=0; uint64_t lo=0; //hi=0,
uint32_t i = 0; uint32_t i = 0;
uint32_t initLoopMax = 1000; uint32_t initLoopMax = 1000;
@ -219,8 +221,8 @@ restart:
if (parityTest== ((parityTest>>1)<<1)){ if (parityTest== ((parityTest>>1)<<1)){
parityTest=0; parityTest=0;
for (ii=0; ii<4;++ii){ for (ii=0; ii<4;++ii){
hi = (hi<<1)|(lo>>31); //hi = (hi<<1)|(lo>>31);
lo=(lo<<1)|(GraphBuffer[(i*5)+ii+idx]); lo=(lo<<1LL)|(GraphBuffer[(i*5)+ii+idx]);
} }
//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,GraphBuffer[idx+ii+(i*5)-5],GraphBuffer[idx+ii+(i*5)-4],GraphBuffer[idx+ii+(i*5)-3],GraphBuffer[idx+ii+(i*5)-2],GraphBuffer[idx+ii+(i*5)-1],lo); //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,GraphBuffer[idx+ii+(i*5)-5],GraphBuffer[idx+ii+(i*5)-4],GraphBuffer[idx+ii+(i*5)-3],GraphBuffer[idx+ii+(i*5)-2],GraphBuffer[idx+ii+(i*5)-1],lo);
}else {//parity failed }else {//parity failed
@ -234,24 +236,27 @@ restart:
} }
//skip last 5 bit parity test for simplicity. //skip last 5 bit parity test for simplicity.
//output em id
PrintAndLog("EM TAG ID : %02x%08x", hi, lo);
//get Unique ID //get Unique ID
uint32_t iii=1; uint64_t iii=1;
uint32_t id2hi=0,id2lo=0; uint64_t id2lo=0; //id2hi=0,
for (i=0;i<8;i++){ //for (i=0;i<8;i++){ //for uint32 instead of uint64
id2hi=(id2hi<<1)|((hi & (iii<<(i)))>>i); // id2hi=(id2hi<<1)|((hi & (iii<<(i)))>>i);
} //}
for (ii=4; ii>0;ii--){ for (ii=5; ii>0;ii--){
for (i=0;i<8;i++){ for (i=0;i<8;i++){
id2lo=(id2lo<<1)|((lo & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8))); id2lo=(id2lo<<1LL)|((lo & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8)));
} }
} }
PrintAndLog("Unique TAG ID: %02x%08x", id2hi, id2lo); //output em id
PrintAndLog("DEZ 8 : %08d",lo & 0xFFFFFF); PrintAndLog("EM TAG ID : %010llx", lo);
PrintAndLog("DEZ 10 : %010d",lo & 0xFFFFFF); PrintAndLog("Unique TAG ID: %010llx", id2lo); //id2hi,
PrintAndLog("DEZ 5.5 : %05d.%05d",(lo>>16) & 0xFFFF,lo & 0xFFFF); PrintAndLog("DEZ 8 : %08lld",lo & 0xFFFFFF);
PrintAndLog("DEZ 3.5A : %03d.%05d",hi,lo &0xFFFF); PrintAndLog("DEZ 10 : %010lld",lo & 0xFFFFFF);
PrintAndLog("DEZ 5.5 : %05lld.%05lld",(lo>>16LL) & 0xFFFF,(lo & 0xFFFF));
PrintAndLog("DEZ 3.5A : %03lld.%05lld",(lo>>32ll),(lo & 0xFFFF));
PrintAndLog("DEZ 14/IK2 : %014lld",lo);
PrintAndLog("DEZ 15/IK3 : %015lld",id2lo);
PrintAndLog("Other : %05lld_%03lld_%08lld",(lo&0xFFFF),((lo>>16LL) & 0xFF),(lo & 0xFFFFFF));
return 0; return 0;
}else{ }else{
idx++; idx++;
@ -269,13 +274,12 @@ int Cmdaskrawdemod(const char *Cmd)
uint32_t i; uint32_t i;
int invert=0; //invert default int invert=0; //invert default
int high = 0, low = 0; int high = 0, low = 0;
int clk=64; //clock default int clk=DetectClock(0); //clock default
uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
sscanf(Cmd, "%i %i", &clk, &invert); sscanf(Cmd, "%i %i", &clk, &invert);
if (!(clk>8)){ if (clk<8) clk =64;
PrintAndLog("Invalid argument: %s",Cmd); if (clk<32) clk=32;
return 0;
}
if (invert != 0 && invert != 1) { if (invert != 0 && invert != 1) {
PrintAndLog("Invalid argument: %s", Cmd); PrintAndLog("Invalid argument: %s", Cmd);
return 0; return 0;
@ -355,7 +359,7 @@ int Cmdaskrawdemod(const char *Cmd)
if ((bitnum > (64+errCnt)) && (errCnt<(GraphTraceLen/1000))) { if ((bitnum > (64+errCnt)) && (errCnt<(GraphTraceLen/1000))) {
//possible good read //possible good read
if (errCnt==0) break; //great read - finish if (errCnt==0) break; //great read - finish
if (bestStart = iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish if (bestStart == iii) break; //if current run == bestErrCnt run (after exhausted testing) then finish
if (errCnt<bestErrCnt){ //set this as new best run if (errCnt<bestErrCnt){ //set this as new best run
bestErrCnt=errCnt; bestErrCnt=errCnt;
bestStart = iii; bestStart = iii;

View file

@ -46,24 +46,27 @@ int ClearGraph(int redraw)
/* /*
* Detect clock rate * Detect clock rate
*/ */
int DetectClock(int peak) //decommissioned - has difficulty detecting rf/32 and only works if data is manchester encoded
/*
int DetectClock2(int peak)
{ {
int i; int i;
int clock = 0xFFFF; int clock = 0xFFFF;
int lastpeak = 0; int lastpeak = 0;
/* Detect peak if we don't have one */ // Detect peak if we don't have one
if (!peak) if (!peak)
for (i = 0; i < GraphTraceLen; ++i) for (i = 0; i < GraphTraceLen; ++i)
if (GraphBuffer[i] > peak) if (GraphBuffer[i] > peak)
peak = GraphBuffer[i]; peak = GraphBuffer[i];
// peak=(int)(peak*.75);
for (i = 1; i < GraphTraceLen; ++i) for (i = 1; i < GraphTraceLen; ++i)
{ {
/* If this is the beginning of a peak */ // If this is the beginning of a peak
if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] == peak) if (GraphBuffer[i - 1] != GraphBuffer[i] && GraphBuffer[i] >= peak)
{ {
/* Find lowest difference between peaks */ // Find lowest difference between peaks
if (lastpeak && i - lastpeak < clock) if (lastpeak && i - lastpeak < clock)
clock = i - lastpeak; clock = i - lastpeak;
lastpeak = i; lastpeak = i;
@ -72,12 +75,84 @@ int DetectClock(int peak)
return clock; return clock;
} }
*/
// by marshmellow
// not perfect especially with lower clocks or VERY good antennas (heavy wave clipping)
// maybe somehow adjust peak trimming value based on samples to fix?
int DetectClock(int peak)
{
int i=0;
int low=0;
int clk[]={16,32,40,50,64,100,128,256};
if (!peak){
for (i=0;i<GraphTraceLen;++i){
if(GraphBuffer[i]>peak){
peak = GraphBuffer[i];
}
if(GraphBuffer[i]<low){
low = GraphBuffer[i];
}
}
peak=(int)(peak*.75);
low= (int)(low*.75);
}
//int numbits;
int ii;
int loopCnt = 256;
if (GraphTraceLen<loopCnt) loopCnt = GraphTraceLen;
int clkCnt;
int tol = 0;
int bestErr=1000;
int errCnt[]={0,0,0,0,0,0,0,0};
// int good;
for(clkCnt=0; clkCnt<6;++clkCnt){
if (clk[clkCnt]==32){
tol=1;
}else{
tol=0;
}
bestErr=1000;
for (ii=0; ii<loopCnt; ++ii){
if ((GraphBuffer[ii]>=peak) || (GraphBuffer[ii]<=low)){
//numbits=0;
//good=1;
errCnt[clkCnt]=0;
for (i=0; i<((int)(GraphTraceLen/clk[clkCnt])-1); ++i){
if (GraphBuffer[ii+(i*clk[clkCnt])]>=peak || GraphBuffer[ii+(i*clk[clkCnt])]<=low){
//numbits++;
}else if(GraphBuffer[ii+(i*clk[clkCnt])-tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])-tol]<=low){
}else if(GraphBuffer[ii+(i*clk[clkCnt])+tol]>=peak || GraphBuffer[ii+(i*clk[clkCnt])+tol]<=low){
}else{ //error no peak detected
//numbits=0;
//good=0;
errCnt[clkCnt]++;
//break;
}
}
if(errCnt[clkCnt]==0) return clk[clkCnt];
if(errCnt[clkCnt]<bestErr) bestErr=errCnt[clkCnt];
}
}
errCnt[clkCnt]=bestErr;
}
int iii=0;
int best=0;
for (iii=0; iii<6;++iii){
if (errCnt[iii]<errCnt[best]){
best = iii;
}
}
PrintAndLog("clkCnt: %d, ii: %d, i: %d peak: %d, low: %d, errcnt: %d, errCnt64: %d",clkCnt,ii,i,peak,low,errCnt[best],errCnt[4]);
return clk[best];
}
/* Get or auto-detect clock rate */ /* Get or auto-detect clock rate */
int GetClock(const char *str, int peak, int verbose) int GetClock(const char *str, int peak, int verbose)
{ {
int clock; int clock;
// int clock2;
sscanf(str, "%i", &clock); sscanf(str, "%i", &clock);
if (!strcmp(str, "")) if (!strcmp(str, ""))
clock = 0; clock = 0;
@ -86,9 +161,12 @@ int GetClock(const char *str, int peak, int verbose)
if (!clock) if (!clock)
{ {
clock = DetectClock(peak); clock = DetectClock(peak);
//clock2 = DetectClock2(peak);
/* Only print this message if we're not looping something */ /* Only print this message if we're not looping something */
if (!verbose) if (!verbose){
PrintAndLog("Auto-detected clock rate: %d", clock); PrintAndLog("Auto-detected clock rate: %d", clock);
//PrintAndLog("clock2: %d",clock2);
}
} }
return clock; return clock;