- Added option c to 'hf list' (mark CRC bytes) (piwi)
### Changed
+- Adjusted the lf demods to auto align and set the grid for the graph plot.
- `lf snoop` now automatically gets samples from the device
- `lf read` now accepts [#samples] as arg. && now automatically gets samples from the device
- adjusted lf t5 chip timings to use WaitUS. and adjusted the readblock timings
uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
uint8_t g_debugMode=0;
size_t DemodBufferLen=0;
-//size_t g_demodStartIdx=0;
-//uint8_t g_demodClock=0;
static int CmdHelp(const char *Cmd);
}
bool st = false;
size_t ststart = 0, stend = 0;
- if (*stCheck) st = DetectST_ext(BitStream, &BitLen, &foundclk, &ststart, &stend);
+ if (*stCheck) st = DetectST(BitStream, &BitLen, &foundclk, &ststart, &stend);
*stCheck = st;
if (st) {
clk = (clk == 0) ? foundclk : clk;
//}
//RepaintGraphWindow();
}
- int errCnt = askdemod(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType);
+ int startIdx = 0;
+ int errCnt = askdemod_ext(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx);
if (errCnt<0 || BitLen<16){ //if fatal error (or -1)
if (g_debugMode) PrintAndLog("DEBUG: no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
return 0;
//output
setDemodBuf(BitStream,BitLen,0);
+ setClockGrid(clk, startIdx);
+
if (verbose || g_debugMode){
if (errCnt>0) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
if (askType) PrintAndLog("ASK/Manchester - Clock: %d - Decoded bitstream:",clk);
}
//get bit clock length
if (!rfLen) {
- rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow);
+ int firstClockEdge = 0; //todo - align grid on graph with this...
+ rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow, &firstClockEdge);
if (!rfLen) rfLen = 50;
}
- int size = fskdemod(BitStream, BitLen, rfLen, invert, fchigh, fclow);
+ int startIdx = 0;
+ int size = fskdemod_ext(BitStream, BitLen, rfLen, invert, fchigh, fclow, &startIdx);
if (size > 0) {
setDemodBuf(BitStream,size,0);
+ setClockGrid(rfLen, startIdx);
// Now output the bitstream to the scrollback by line of 16 bits
if (verbose || g_debugMode) {
size_t BitLen = getFromGraphBuf(BitStream);
if (BitLen==0) return 0;
int errCnt=0;
- errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert);
+ int startIdx = 0;
+ errCnt = pskRawDemod_ext(BitStream, &BitLen, &clk, &invert, &startIdx);
if (errCnt > maxErr){
if (g_debugMode || verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
return 0;
}
//prime demod buffer for output
setDemodBuf(BitStream,BitLen,0);
+ setClockGrid(clk, startIdx);
+
return 1;
}
if (verbose || g_debugMode) PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
//prime demod buffer for output
setDemodBuf(BitStream,BitLen,0);
+ setClockGrid(clk, clkStartIdx);
+
if (errCnt>0 && (verbose || g_debugMode)) PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
if (verbose || g_debugMode) {
return ans;
}
+void setClockGrid(int clk, int offset) {
+ if (offset > clk) offset %= clk;
+ if (offset < 0) offset += clk;
+
+ if (offset > GraphTraceLen || offset < 0) return;
+ if (clk < 8 || clk > GraphTraceLen) {
+ GridLocked = false;
+ GridOffset = 0;
+ PlotGridX = 0;
+ PlotGridXdefault = 0;
+ RepaintGraphWindow();
+ } else {
+ GridLocked = true;
+ GridOffset = offset;
+ PlotGridX = clk;
+ PlotGridXdefault = clk;
+ RepaintGraphWindow();
+ }
+}
+
int CmdGrid(const char *Cmd)
{
sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
{"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
{"dec", CmdDec, 1, "Decimate samples"},
{"detectclock", CmdDetectClockRate, 1, "[modulation] Detect clock rate of wave in GraphBuffer (options: 'a','f','n','p' for ask, fsk, nrz, psk respectively)"},
- //{"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"},
{"getbitstream", CmdGetBitStream, 1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"},
{"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
{"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
int PSKDemod(const char *Cmd, bool verbose);
int NRZrawDemod(const char *Cmd, bool verbose);
int getSamples(int n, bool silent);
-
+void setClockGrid(int clk, int offset);
#define MAX_DEMOD_BUF_LEN (1024*128)
extern uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
extern size_t DemodBufferLen;
extern uint8_t g_debugMode;
-//extern size_t g_demodStartIdx;
-//extern uint8_t g_demodClock;
#define BIGBUF_SIZE 40000
#endif
{
return usage_lf_simfsk();
}
-
+ int firstClockEdge = 0;
if (dataLen == 0){ //using DemodBuffer
if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
- uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0);
+ uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0, &firstClockEdge);
if (ans==0){
if (!fcHigh) fcHigh=10;
if (!fcLow) fcLow=8;
t55xx_conf_block_t tests[15];\r
int bitRate=0;\r
uint8_t fc1 = 0, fc2 = 0, ans = 0;\r
- int clk=0;\r
- ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false);\r
+ int clk = 0, firstClockEdge = 0;\r
+ ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);\r
if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {\r
if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {\r
tests[hits].modulation = DEMOD_FSK;\r
uint8_t preamble[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1};\r
size_t startIdx = 0;\r
uint8_t fc1 = 0, fc2 = 0, ans = 0;\r
- int clk = 0;\r
+ int clk = 0, firstClockEdge = 0;\r
bool st = true;\r
\r
if ( getData ) {\r
}\r
\r
// try fsk clock detect. if successful it cannot be any other type of modulation... (in theory...)\r
- ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false);\r
+ ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);\r
if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {\r
if ( FSKrawDemod("0 0", false) && \r
preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && \r
PrintAndLog("Failed to copy from graphbuffer");
return -1;
}
- bool st = DetectST(grph, &size, &clock);
- int start = 0;
+ //, size_t *ststart, size_t *stend
+ size_t ststart = 0, stend = 0;
+ bool st = DetectST(grph, &size, &clock, &ststart, &stend);
+ int start = stend;
if (st == false) {
start = DetectASKClock(grph, size, &clock, 20);
}
+ setClockGrid(clock, start);
// Only print this message if we're not looping something
if (printAns || g_debugMode) {
PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
size_t firstPhaseShiftLoc = 0;
uint8_t curPhase = 0, fc = 0;
clock = DetectPSKClock(grph, size, 0, &firstPhaseShiftLoc, &curPhase, &fc);
+ setClockGrid(clock, firstPhaseShiftLoc);
// Only print this message if we're not looping something
if (printAns){
PrintAndLog("Auto-detected clock rate: %d", clock);
}
size_t clkStartIdx = 0;
clock = DetectNRZClock(grph, size, 0, &clkStartIdx);
+ setClockGrid(clock, clkStartIdx);
// Only print this message if we're not looping something
if (printAns){
PrintAndLog("Auto-detected clock rate: %d", clock);
uint8_t fc1=0, fc2=0, rf1=0;
- uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose);
+ int firstClockEdge = 0;
+ uint8_t ans = fskClocks(&fc1, &fc2, &rf1, verbose, &firstClockEdge);
if (ans == 0) return 0;
if ((fc1==10 && fc2==8) || (fc1==8 && fc2==5)){
if (printAns) PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
+ setClockGrid(rf1, firstClockEdge);
return rf1;
}
if (verbose){
}
return 0;
}
-uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose)
+uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge)
{
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
size_t size = getFromGraphBuf(BitStream);
}
*fc1 = (ans >> 8) & 0xFF;
*fc2 = ans & 0xFF;
-
- *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2);
+ //int firstClockEdge = 0;
+ *rf1 = detectFSKClk(BitStream, size, *fc1, *fc2, firstClockEdge);
if (*rf1==0) {
if (verbose || g_debugMode) PrintAndLog("DEBUG: Clock detect error");
return 0;
uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose);
uint8_t GetNrzClock(const char str[], bool printAns, bool verbose);
uint8_t GetFskClock(const char str[], bool printAns, bool verbose);
-uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose);
+uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge);
+//uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose);
bool graphJustNoise(int *BitStream, int size);
void setGraphBuf(uint8_t *buff, size_t size);
void save_restoreGB(uint8_t saveOpt);
extern int GraphBuffer[MAX_GRAPH_TRACE_LEN];
extern int GraphTraceLen;
extern double CursorScaleFactor;
-extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos;
+extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset;
extern int CommandFinished;
extern int offline;
+extern bool GridLocked;
#ifdef __cplusplus
}
#include <stdio.h>
#include "proxgui.h"
-int GridOffset= 0;
-bool GridLocked= 0;
int startMax;
int PageWidth;
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
+#include <stdbool.h>
#include <readline/readline.h>
#include <pthread.h>
#include "ui.h"
-double CursorScaleFactor;
+double CursorScaleFactor = 1;
int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
int offline;
int flushAfterWrite = 0; //buzzy
+int GridOffset = 0;
+bool GridLocked = false;
+
extern pthread_mutex_t print_lock;
static char *logfilename = "proxmark3.log";
#ifndef UI_H__
#define UI_H__
+#include <stdbool.h>
+
void ShowGui(void);
void HideGraphWindow(void);
void ShowGraphWindow(void);
void SetLogFilename(char *fn);
extern double CursorScaleFactor;
-extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos;
+extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset;
extern int offline;
extern int flushAfterWrite; //buzzy
+extern bool GridLocked;
#endif
return clk[best];
}
-//int DetectPSKClock(uint8_t dest[], size_t size, int clock) {
-// size_t firstPhaseShift = 0;
-// uint8_t curPhase = 0;
-// return DetectPSKClock_ext(dest, size, clock, &firstPhaseShift, &curPhase);
-//}
-
//by marshmellow
//detects the bit clock for FSK given the high and low Field Clocks
-uint8_t detectFSKClk_ext(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge) {
+uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge) {
uint8_t clk[] = {8,16,32,40,50,64,100,128,0};
uint16_t rfLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
uint8_t rfCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
return clk[ii];
}
-uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow) {
- int firstClockEdge = 0;
- return detectFSKClk_ext(BitStream, size, fcHigh, fcLow, &firstClockEdge);
-}
-
//**********************************************************************************************
//--------------------Modulation Demods &/or Decoding Section-----------------------------------
//**********************************************************************************************
}
//by marshmellow
//attempt to identify a Sequence Terminator in ASK modulated raw wave
-bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend) {
+bool DetectST(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend) {
size_t bufsize = *size;
//need to loop through all samples and identify our clock, look for the ST pattern
int clk = 0;
*size = newloc;
return true;
}
-bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) {
- size_t ststart = 0, stend = 0;
- return DetectST_ext(buffer, size, foundclock, &ststart, &stend);
-}
//by marshmellow
//take 11 10 01 11 00 and make 01100 ... miller decoding
extern uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj);
extern int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr);
extern uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low);
-extern uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow);
-extern uint8_t detectFSKClk_ext(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge);
+extern uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge);
extern int DetectNRZClock(uint8_t dest[], size_t size, int clock, size_t *clockStartIdx);
extern int DetectPSKClock(uint8_t dest[], size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc);
extern int DetectStrongAskClock(uint8_t dest[], size_t size, int high, int low, int *clock);
-extern bool DetectST(uint8_t buffer[], size_t *size, int *foundclock);
-extern bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend);
+extern bool DetectST(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend);
extern int fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
extern int fskdemod_ext(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx);
extern int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);