From: marshmellow42 <marshmellowrf@gmail.com>
Date: Tue, 7 Mar 2017 13:48:16 +0000 (-0500)
Subject: add iceman1001 s lf visa2000 commands
X-Git-Tag: v3.0.0~56^2
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/8b6abef506d69e3906c95a532e6ece5e71462e96?ds=sidebyside

add iceman1001 s lf visa2000 commands

-reset my fork due to corruption - now add back in the lf visa
commands...
also adjusts lfdemod all detect clock functions to always return a
starting point of the clock detected ( for graphing purposes)
---

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d016b76d..2a4e91fc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac
 ## [unreleased][unreleased]
 
 ### Added
+- Added lf visa2000 demod, read, clone, sim commands (iceman)
 - Added markers in the graph around found Sequence Terminator after askmandemod.
 - Added data mtrim <start> <stop> command to trim out samples between start and stop
 - Added data setgraphmarkers <orange> <blue> command to set two extra markers on the graph (marshmellow)
diff --git a/client/Makefile b/client/Makefile
index c1ff5ee3..2a572c11 100644
--- a/client/Makefile
+++ b/client/Makefile
@@ -90,23 +90,24 @@ CMDSRCS = 	nonce2key/crapto1.c\
 			cmdhftopaz.c \
 			cmdhw.c \
 			cmdlf.c \
-			cmdlfio.c \
-			cmdlfhid.c \
 			cmdlfawid.c \
+			cmdlfcotag.c\
 			cmdlfem4x.c \
+			cmdlfhid.c \
 			cmdlfhitag.c \
-			cmdlfti.c \
-			cmdparser.c \
-			cmdmain.c \
-			cmdlft55xx.c \
+			cmdlfio.c \
 			cmdlfpcf7931.c\
-			cmdlfviking.c\
 			cmdlfpresco.c\
 			cmdlfpyramid.c\
-			cmdlfcotag.c\
-			pm3_binlib.c\
+			cmdlft55xx.c \
+			cmdlfti.c \
+			cmdlfviking.c\
+			cmdlfvisa2000.c\
+			cmdparser.c \
+			cmdmain.c \
 			scripting.c\
 			cmdscript.c\
+			pm3_binlib.c\
 			pm3_bitlib.c\
 			aes.c\
 			protocols.c\
diff --git a/client/cmdlf.c b/client/cmdlf.c
index dcc6cfae..f34637db 100644
--- a/client/cmdlf.c
+++ b/client/cmdlf.c
@@ -34,6 +34,7 @@
 #include "cmdlfpyramid.h"// for pyramid menu
 #include "cmdlfviking.h" // for viking menu
 #include "cmdlfcotag.h"  // for COTAG menu
+#include "cmdlfvisa2000.h"  // for VISA2000 menu
 
 bool g_lf_threshold_set = false;
 static int CmdHelp(const char *Cmd);
@@ -1195,6 +1196,12 @@ int CmdLFfind(const char *Cmd)
 		return CheckChipType(cmdp);
 	}
 
+	ans=CmdVisa2kDemod("");
+	if (ans>0) {
+		PrintAndLog("\nValid Visa2000 ID Found!");
+		return CheckChipType(cmdp);		
+	}
+	
 	ans=CmdG_Prox_II_Demod("");
 	if (ans>0) {
 		PrintAndLog("\nValid G Prox II ID Found!");
@@ -1274,12 +1281,13 @@ static command_t CommandTable[] =
 	{"hid",         CmdLFHID,           1, "{ HID RFIDs...     }"},
 	{"hitag",       CmdLFHitag,         1, "{ Hitag tags and transponders... }"},
 	{"io",          CmdLFIO,            1, "{ ioProx tags...   }"},
-	{"presco",      CmdLFPresco,        1, "{ Presco RFIDs... }"},
+	{"presco",      CmdLFPresco,        1, "{ Presco RFIDs...  }"},
 	{"pcf7931",     CmdLFPCF7931,       1, "{ PCF7931 RFIDs... }"},
 	{"pyramid",     CmdLFPyramid,       1, "{ Farpointe/Pyramid RFIDs... }"},
 	{"t55xx",       CmdLFT55XX,         1, "{ T55xx RFIDs...   }"},
 	{"ti",          CmdLFTI,            1, "{ TI RFIDs...      }"},
 	{"viking",      CmdLFViking,        1, "{ Viking tags...   }"},
+	{"visa2000",    CmdLFVisa2k,        1, "{ Visa2000 RFIDs...}"},
 	{"cmdread",     CmdLFCommandRead,   0, "<d period> <z period> <o period> <c command> ['H'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'H' for 134)"},
 	{"config",      CmdLFSetConfig,     0, "Set config for LF sampling, bit/sample, decimation, frequency"},
 	{"flexdemod",   CmdFlexdemod,       1, "Demodulate samples for FlexPass"},
diff --git a/client/cmdlfvisa2000.c b/client/cmdlfvisa2000.c
new file mode 100644
index 00000000..9ce35f98
--- /dev/null
+++ b/client/cmdlfvisa2000.c
@@ -0,0 +1,239 @@
+//-----------------------------------------------------------------------------
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+// Low frequency visa 200 tag commands
+// by iceman
+//-----------------------------------------------------------------------------
+
+#include "cmdlfvisa2000.h"
+#include "proxmark3.h"
+#include "ui.h"
+#include "util.h"
+#include "graph.h"
+#include "cmddata.h"    // for ASKDemod_ext, g_debugMode, DemodBuffer ...
+#include "cmdmain.h"    // for clearCommandBuffer and WaitForResponseTimeout
+#include "cmdlf.h"
+#include "protocols.h"  // for T55xx config register definitions
+#include "lfdemod.h"    // for Visa2kDemod_AM
+
+#define BL0CK1 0x56495332
+
+static int CmdHelp(const char *Cmd);
+
+int usage_lf_visa2k_clone(void){
+	PrintAndLog("clone a Visa2000 tag to a T55x7 tag.");
+	PrintAndLog("Usage: lf visa2k clone [h] <card ID> <Q5>");
+	PrintAndLog("Options:");
+	PrintAndLog("      h          : This help");
+	PrintAndLog("      <card ID>  : Visa2k card ID");
+	PrintAndLog("      <Q5>       : specify write to Q5 (t5555 instead of t55x7)");
+	PrintAndLog("");
+	PrintAndLog("Sample: lf visa2k clone 112233");
+	return 0;
+}
+
+int usage_lf_visa2k_sim(void) {
+	PrintAndLog("Enables simulation of visa2k card with specified card number.");
+	PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
+	PrintAndLog("");
+	PrintAndLog("Usage:  lf visa2k sim [h] <card ID>");
+	PrintAndLog("Options:");
+	PrintAndLog("      h          : This help");
+	PrintAndLog("      <card ID>  : Visa2k card ID");
+	PrintAndLog("");
+	PrintAndLog("Sample: lf visa2k sim 112233");
+	return 0;
+}
+
+static uint8_t visa_chksum( uint32_t id ) {
+	uint8_t sum = 0;
+	for (uint8_t i = 0; i < 32; i += 4)
+		sum ^=  (id >> i) & 0xF;
+
+	return sum;
+}
+
+static uint8_t visa_parity( uint32_t id) {
+	// 4bit parity LUT
+	uint8_t par_lut[] = {
+		 0,1,1,0
+		,1,0,0,1
+		,1,0,0,1
+		,0,1,1,0
+	};
+	uint8_t par = 0;
+	par |= par_lut[ (id >> 28) & 0xF ] << 7;
+	par |= par_lut[ (id >> 24) & 0xF ] << 6;
+	par |= par_lut[ (id >> 20) & 0xF ] << 5;
+	par |= par_lut[ (id >> 16) & 0xF ] << 4;
+	par |= par_lut[ (id >> 12) & 0xF ] << 3;
+	par |= par_lut[ (id >>  8) & 0xF ] << 2;
+	par |= par_lut[ (id >>  4) & 0xF ] << 1;
+	par |= par_lut[ (id & 0xF) ];
+	return par;	
+}
+
+
+/**
+*
+* 56495332 00096ebd 00000077 —> tag id 618173
+* aaaaaaaa iiiiiiii -----..c
+*
+* a = fixed value  ascii 'VIS2'
+* i = card id
+* c = checksum  (xor of card id)
+* . = unknown 
+* 
+**/
+//see ASKDemod for what args are accepted
+int CmdVisa2kDemod(const char *Cmd) {
+
+	//sCmdAskEdgeDetect("");
+
+	//ASK / Manchester
+	bool st = true;
+	if (!ASKDemod_ext("64 0 0", false, false, 1, &st)) {
+		if (g_debugMode) PrintAndLog("DEBUG: Error - Visa2k: ASK/Manchester Demod failed");
+		return 0;
+	}
+	size_t size = DemodBufferLen;
+	int ans = Visa2kDemod_AM(DemodBuffer, &size);
+	if (ans < 0){
+		if (g_debugMode){
+			if (ans == -1)
+				PrintAndLog("DEBUG: Error - Visa2k: too few bits found");
+			else if (ans == -2)
+				PrintAndLog("DEBUG: Error - Visa2k: preamble not found");
+			else if (ans == -3)
+				PrintAndLog("DEBUG: Error - Visa2k: Size not correct: %d", size);
+			else
+				PrintAndLog("DEBUG: Error - Visa2k: ans: %d", ans);
+		}
+		return 0;
+	}
+	setDemodBuf(DemodBuffer, 96, ans);
+	//setGrid_Clock(64);
+
+	//got a good demod
+	uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32);
+	uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32);
+	uint32_t raw3 = bytebits_to_byte(DemodBuffer+64, 32);
+
+	// chksum
+	uint8_t calc = visa_chksum(raw2);
+	uint8_t chk = raw3 & 0xF;	
+
+	// test checksums
+	if ( chk != calc ) { 
+		printf("DEBUG: error: Visa2000 checksum failed %x - %x\n", chk, calc);
+		return 0;
+	}
+	// parity
+	uint8_t calc_par = visa_parity(raw2);
+	uint8_t chk_par = (raw3 & 0xFF0) >> 4;
+	if ( calc_par != chk_par) {
+		printf("DEBUG: error: Visa2000 parity failed %x - %x\n", chk_par, calc_par);
+		return 0;
+	}
+	PrintAndLog("Visa2000 Tag Found: Card ID %u,  Raw: %08X%08X%08X", raw2,  raw1 ,raw2, raw3);
+	return 1;
+}
+
+int CmdVisa2kRead(const char *Cmd) {
+	CmdLFRead("s");
+	//64*96*2=12288 samples just in case we just missed the first preamble we can still catch 2 of them
+	getSamples("12500",true); 
+	return CmdVisa2kDemod(Cmd);
+}
+
+int CmdVisa2kClone(const char *Cmd) {
+
+	uint64_t id = 0;
+	uint32_t blocks[4] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_64 | T55x7_ST_TERMINATOR | 3 << T55x7_MAXBLOCK_SHIFT, BL0CK1, 0};
+
+	char cmdp = param_getchar(Cmd, 0);
+	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_clone();
+
+	id = param_get32ex(Cmd, 0, 0, 10);
+	
+	//Q5
+	if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') {
+		//t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
+		blocks[0] = T5555_MODULATION_MANCHESTER | ((64-2)>>1) << T5555_BITRATE_SHIFT | T5555_ST_TERMINATOR | 3 << T5555_MAXBLOCK_SHIFT;
+	}
+
+	blocks[2] = id;
+	blocks[3] = (visa_parity(id) << 4) | visa_chksum(id);
+
+	PrintAndLog("Preparing to clone Visa2000 to T55x7 with CardId: %u", id);
+	PrintAndLog("Blk | Data ");
+	PrintAndLog("----+------------");
+	for(int i = 0; i<4; ++i)
+		PrintAndLog(" %02d | 0x%08x", i , blocks[i]);
+	
+	UsbCommand resp;
+	UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
+
+	for (int i = 3; i >= 0; --i) {
+		c.arg[0] = blocks[i];
+		c.arg[1] = i;
+		clearCommandBuffer();
+		SendCommand(&c);
+		if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
+			PrintAndLog("Error occurred, device did not respond during write operation.");
+			return -1;
+		}
+	}
+	return 0;
+}
+
+int CmdVisa2kSim(const char *Cmd) {
+
+	uint32_t id = 0;
+	char cmdp = param_getchar(Cmd, 0);
+	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_sim();
+
+	id = param_get32ex(Cmd, 0, 0, 10);
+
+	uint8_t clk = 64, encoding = 1, separator = 1, invert = 0;
+	uint16_t arg1, arg2;
+	size_t size = 96;
+	arg1 = clk << 8 | encoding;
+	arg2 = invert << 8 | separator;
+
+	PrintAndLog("Simulating Visa2000 - CardId: %u", id);
+
+	UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
+
+	uint32_t blocks[3] = { BL0CK1, id, (visa_parity(id) << 4) | visa_chksum(id) };
+
+	for(int i=0; i<3; ++i)
+		num_to_bytebits(blocks[i], 32, c.d.asBytes + i*32);
+
+	clearCommandBuffer();
+	SendCommand(&c);
+	return 0;
+}
+
+static command_t CommandTable[] = {
+	{"help",  CmdHelp,        1, "This help"},
+	{"demod", CmdVisa2kDemod, 1, "Attempt to demod from GraphBuffer"},
+	{"read",  CmdVisa2kRead,  0, "Attempt to read and extract tag data"},
+	{"clone", CmdVisa2kClone, 0, "clone Visa2000 tag"},
+	{"sim",   CmdVisa2kSim,   0, "simulate Visa2000 tag"},
+	{NULL, NULL, 0, NULL}
+};
+
+int CmdLFVisa2k(const char *Cmd) {
+	clearCommandBuffer();
+	CmdsParse(CommandTable, Cmd);
+	return 0;
+}
+
+int CmdHelp(const char *Cmd) {
+	CmdsHelp(CommandTable);
+	return 0;
+}
diff --git a/client/cmdlfvisa2000.h b/client/cmdlfvisa2000.h
new file mode 100644
index 00000000..611768b2
--- /dev/null
+++ b/client/cmdlfvisa2000.h
@@ -0,0 +1,19 @@
+//-----------------------------------------------------------------------------
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+// Low frequency T55xx commands
+//-----------------------------------------------------------------------------
+#ifndef CMDLFVISA2000_H__
+#define CMDLFVISA2000_H__
+#include <inttypes.h>
+extern int CmdLFVisa2k(const char *Cmd);
+extern int CmdVisa2kClone(const char *Cmd);
+extern int CmdVisa2kSim(const char *Cmd);
+extern int CmdVisa2kRead(const char *Cmd);
+extern int CmdVisa2kDemod(const char *Cmd);
+
+#endif
+
diff --git a/common/lfdemod.c b/common/lfdemod.c
index 3f597a1f..1b53c445 100644
--- a/common/lfdemod.c
+++ b/common/lfdemod.c
@@ -337,7 +337,7 @@ int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr
 	int lastBit;              //set first clock check - can go negative
 	size_t i, bitnum = 0;     //output counter
 	uint8_t midBit = 0;
-	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
+	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
 	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
 	size_t MaxBits = 3072;    //max bits to collect
 	lastBit = start - *clk;
@@ -819,12 +819,12 @@ uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t lo
 // by marshmellow
 // to help detect clocks on heavily clipped samples
 // based on count of low to low
-int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low)
-{
+int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low, int *clock) {
 	uint8_t fndClk[] = {8,16,32,40,50,64,128};
 	size_t startwave;
 	size_t i = 100;
 	size_t minClk = 255;
+	int shortestWaveIdx = 0;
 		// get to first full low to prime loop and skip incomplete first pulse
 	while ((dest[i] < high) && (i < size))
 		++i;
@@ -836,20 +836,24 @@ int DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low)
 		// measure from low to low
 		while ((dest[i] > low) && (i < size))
 			++i;
-		startwave= i;
+		startwave = i;
 		while ((dest[i] < high) && (i < size))
 			++i;
 		while ((dest[i] > low) && (i < size))
 			++i;
 		//get minimum measured distance
-		if (i-startwave < minClk && i < size)
+		if (i-startwave < minClk && i < size) {
 			minClk = i - startwave;
+			shortestWaveIdx = startwave;
+		}
 	}
 	// set clock
 	if (g_debugMode==2) prnt("DEBUG ASK: detectstrongASKclk smallest wave: %d",minClk);
 	for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) {
-		if (minClk >= fndClk[clkCnt]-(fndClk[clkCnt]/8) && minClk <= fndClk[clkCnt]+1)
-			return fndClk[clkCnt];
+		if (minClk >= fndClk[clkCnt]-(fndClk[clkCnt]/8) && minClk <= fndClk[clkCnt]+1) {
+			*clock = fndClk[clkCnt];
+			return shortestWaveIdx;
+		}
 	}
 	return 0;
 }
@@ -879,15 +883,10 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
 	//test for large clean peaks
 	if (!clockFnd){
 		if (DetectCleanAskWave(dest, size, peak, low)==1){
-			int ans = DetectStrongAskClock(dest, size, peak, low);
-			if (g_debugMode==2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %d",ans);
-			for (i=clkEnd-1; i>0; i--){
-				if (clk[i] == ans) {
-					*clock = ans;
-					//clockFnd = i;
-					return 0;  // for strong waves i don't use the 'best start position' yet...
-					//break; //clock found but continue to find best startpos [not yet]
-				}
+			int ans = DetectStrongAskClock(dest, size, peak, low, clock);
+			if (g_debugMode==2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i",clock, ans);
+			if (ans > 0) {
+				return ans; //return shortest wave start position
 			}
 		}
 	}
@@ -960,11 +959,16 @@ int DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr)
 	return bestStart[best];
 }
 
+
+int DetectPSKClock(uint8_t dest[], size_t size, int clock) {
+	int firstPhaseShift = 0;
+	return DetectPSKClock_ext(dest, size, clock, &firstPhaseShift);
+}
+
 //by marshmellow
 //detect psk clock by reading each phase shift
 // a phase shift is determined by measuring the sample length of each wave
-int DetectPSKClock(uint8_t dest[], size_t size, int clock)
-{
+int DetectPSKClock_ext(uint8_t dest[], size_t size, int clock, int *firstPhaseShift) {
 	uint8_t clk[]={255,16,32,40,50,64,100,128,255}; //255 is not a valid clock
 	uint16_t loopCnt = 4096;  //don't need to loop through entire array...
 	if (size == 0) return 0;
@@ -1003,8 +1007,8 @@ int DetectPSKClock(uint8_t dest[], size_t size, int clock)
 			}
 		}
 	}
+	*firstPhaseShift = firstFullWave;
 	if (g_debugMode ==2) prnt("DEBUG PSK: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
-	
 	//test each valid clock from greatest to smallest to see which lines up
 	for(clkCnt=7; clkCnt >= 1 ; clkCnt--){
 		lastClkBit = firstFullWave; //set end of wave as clock align
@@ -1087,10 +1091,15 @@ int DetectStrongNRZClk(uint8_t *dest, size_t size, int peak, int low){
 	return lowestTransition;
 }
 
+int DetectNRZClock(uint8_t dest[], size_t size, int clock) {
+	size_t bestStart=0;
+	return DetectNRZClock_ext(dest, size, clock, &bestStart);
+}
+
+
 //by marshmellow
 //detect nrz clock by reading #peaks vs no peaks(or errors)
-int DetectNRZClock(uint8_t dest[], size_t size, int clock)
-{
+int DetectNRZClock_ext(uint8_t dest[], size_t size, int clock, size_t *clockStartIdx) {
 	size_t i=0;
 	uint8_t clk[]={8,16,32,40,50,64,100,128,255};
 	size_t loopCnt = 4096;  //don't need to loop through entire array...
@@ -1137,6 +1146,7 @@ int DetectNRZClock(uint8_t dest[], size_t size, int clock)
 	uint8_t ignoreWindow = 4;
 	bool lastPeakHigh = 0;
 	int lastBit = 0; 
+	size_t bestStart[]={0,0,0,0,0,0,0,0,0};
 	peakcnt=0;
 	//test each valid clock from smallest to greatest to see which lines up
 	for(clkCnt=0; clkCnt < 8; ++clkCnt){
@@ -1183,6 +1193,7 @@ int DetectNRZClock(uint8_t dest[], size_t size, int clock)
 					}
 				}
 				if(peakcnt>peaksdet[clkCnt]) {
+					bestStart[clkCnt]=ii;
 					peaksdet[clkCnt]=peakcnt;
 				}
 			}
@@ -1200,7 +1211,7 @@ int DetectNRZClock(uint8_t dest[], size_t size, int clock)
 		}
 		if (g_debugMode==2) prnt("DEBUG NRZ: Clk: %d, peaks: %d, maxPeak: %d, bestClk: %d, lowestTrs: %d",clk[iii],peaksdet[iii],maxPeak, clk[best], lowestTransition);
 	}
-
+	*clockStartIdx	= bestStart[best];
 	return clk[best];
 }
 
@@ -1293,10 +1304,14 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert){
 	return 0;
 }
 
+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);
+}
+
 //by marshmellow
 //detects the bit clock for FSK given the high and low Field Clocks
-uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow)
-{
+uint8_t detectFSKClk_ext(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};
@@ -1352,6 +1367,7 @@ uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fc
 					rfLens[rfLensFnd++] = rfCounter;
 				}
 			} else {
+				*firstClockEdge = i;
 				firstBitFnd++;
 			}
 			rfCounter=0;
@@ -1790,3 +1806,16 @@ bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststa
 	*size = newloc;
 	return true;
 }
+
+// by iceman
+// find Visa2000 preamble in already demoded data
+int Visa2kDemod_AM(uint8_t *dest, size_t *size) {
+	if (*size < 96) return -1; //make sure buffer has data
+	size_t startIdx = 0;
+	uint8_t preamble[] = {0,1,0,1,0,1,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0};
+	if (preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx) == 0)
+		return -2; //preamble not found
+	if (*size != 96) return -3; //wrong demoded size
+	//return start position
+	return (int)startIdx;
+}
diff --git a/common/lfdemod.h b/common/lfdemod.h
index 37109552..b465a659 100644
--- a/common/lfdemod.h
+++ b/common/lfdemod.h
@@ -17,45 +17,50 @@
 #include <stdbool.h> // for bool
 
 //generic
-size_t   addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType);
-int      askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType);
-void     askAmp(uint8_t *BitStream, size_t size);
-int      BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset, int invert);
-uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
-uint32_t bytebits_to_byteLSBF(uint8_t* src, size_t numbits);
-uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj);
-int      DetectASKClock(uint8_t dest[], size_t size, int *clock, int maxErr);
-uint8_t  DetectCleanAskWave(uint8_t dest[], size_t size, uint8_t high, uint8_t low);
-uint8_t  detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fcLow);
-int      DetectNRZClock(uint8_t dest[], size_t size, int clock);
-int      DetectPSKClock(uint8_t dest[], size_t size, int clock);
-int      DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t low);
-bool     DetectST(uint8_t buffer[], size_t *size, int *foundclock);
-bool     DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststart, size_t *stend);
-int      fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
-int      getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
-uint32_t manchesterEncode2Bytes(uint16_t datain);
-int      ManchesterEncode(uint8_t *BitStream, size_t size);
-int      manrawdecode(uint8_t *BitStream, size_t *size, uint8_t invert);
-int      nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert);
-uint8_t  parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType);
-uint8_t  preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx);
-bool     preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone);
-int      pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert);
-void     psk2TOpsk1(uint8_t *BitStream, size_t size);
-void     psk1TOpsk2(uint8_t *BitStream, size_t size);
-size_t   removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen);
+extern size_t   addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType);
+extern int      askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType);
+extern void     askAmp(uint8_t *BitStream, size_t size);
+extern int      BiphaseRawDecode(uint8_t * BitStream, size_t *size, int offset, int invert);
+extern uint32_t bytebits_to_byte(uint8_t* src, size_t numbits);
+extern uint32_t bytebits_to_byteLSBF(uint8_t* src, size_t numbits);
+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 int      DetectNRZClock(uint8_t dest[], size_t size, int clock);
+extern int      DetectNRZClock_ext(uint8_t dest[], size_t size, int clock, size_t *clockStartIdx);
+extern int      DetectPSKClock(uint8_t dest[], size_t size, int clock);
+extern int      DetectPSKClock_ext(uint8_t dest[], size_t size, int clock, int *firstPhaseShift);
+extern int      DetectStrongAskClock(uint8_t dest[], size_t size, uint8_t high, uint8_t 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 int      fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow);
+extern int      getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
+extern uint32_t manchesterEncode2Bytes(uint16_t datain);
+extern int      ManchesterEncode(uint8_t *BitStream, size_t size);
+extern int      manrawdecode(uint8_t *BitStream, size_t *size, uint8_t invert);
+extern int      nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert);
+extern uint8_t  parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType);
+extern uint8_t  preambleSearch(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx);
+extern bool     preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone);
+extern int      pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert);
+extern void     psk2TOpsk1(uint8_t *BitStream, size_t size);
+extern void     psk1TOpsk2(uint8_t *BitStream, size_t size);
+extern size_t   removeParity(uint8_t *BitStream, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen);
 
 //tag specific
-int AWIDdemodFSK(uint8_t *dest, size_t *size);
-uint8_t  Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo);
-int FDXBdemodBI(uint8_t *dest, size_t *size);
-int gProxII_Demod(uint8_t BitStream[], size_t *size);
-int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
-int IOdemodFSK(uint8_t *dest, size_t size);
-int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert);
-int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
-int PyramiddemodFSK(uint8_t *dest, size_t *size);
-int VikingDemod_AM(uint8_t *dest, size_t *size);
-int PrescoDemod(uint8_t *dest, size_t *size);
+extern int AWIDdemodFSK(uint8_t *dest, size_t *size);
+extern uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo);
+extern int FDXBdemodBI(uint8_t *dest, size_t *size);
+extern int gProxII_Demod(uint8_t BitStream[], size_t *size);
+extern int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
+extern int IOdemodFSK(uint8_t *dest, size_t size);
+extern int indala26decode(uint8_t *bitStream, size_t *size, uint8_t *invert);
+extern int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo);
+extern int PrescoDemod(uint8_t *dest, size_t *size);
+extern int PyramiddemodFSK(uint8_t *dest, size_t *size);
+extern int VikingDemod_AM(uint8_t *dest, size_t *size);
+extern int Visa2kDemod_AM(uint8_t *dest, size_t *size);
+
 #endif
diff --git a/common/protocols.h b/common/protocols.h
index b57c414f..0798db47 100644
--- a/common/protocols.h
+++ b/common/protocols.h
@@ -268,6 +268,8 @@ void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *
 #define T5555_FAST_WRITE            0x00004000
 #define T5555_PAGE_SELECT           0x00008000
 
+#define T55XX_WRITE_TIMEOUT 1500
+
 uint32_t GetT55xxClockBit(uint32_t clock);
 
 #endif