From fca1c9b7cfce4d9ca4e3ac0a272e0c9b1e5fa558 Mon Sep 17 00:00:00 2001
From: iceman1001 <iceman@iuse.se>
Date: Mon, 5 Feb 2018 16:47:10 +0100
Subject: [PATCH] chg:  'hf mf mifare' - (deviceside) reset cycles when
 negative or too large

---
 Makefile           |  2 --
 armsrc/iso14443a.c | 16 ++++++++++++----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile
index e61006b71..d7bd727d4 100644
--- a/Makefile
+++ b/Makefile
@@ -27,10 +27,8 @@ all clean: %: client/% bootrom/% armsrc/% recovery/% mfkey/% nonce2key/%
 
 mfkey/%: FORCE
 	$(MAKE) -C tools/mfkey $(patsubst mfkey/%,%,$@)
-
 nonce2key/%: FORCE
 	$(MAKE) -C tools/nonce2key $(patsubst nonce2key/%,%,$@)
-
 bootrom/%: FORCE
 	$(MAKE) -C bootrom $(patsubst bootrom/%,%,$@)
 armsrc/%: FORCE
diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c
index 0dda14de5..c5c33eff4 100644
--- a/armsrc/iso14443a.c
+++ b/armsrc/iso14443a.c
@@ -2344,7 +2344,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
 	// static variables here, is re-used in the next call
 	static uint32_t nt_attacked = 0;
 	static uint32_t sync_time = 0;
-	static uint32_t sync_cycles = 0;
+	static int32_t sync_cycles = 0;
 	static uint8_t par_low = 0;
 	static uint8_t mf_nr_ar3 = 0;
 	
@@ -2455,15 +2455,23 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
 				
 				sync_cycles = (sync_cycles - nt_distance)/elapsed_prng_sequences;
 				
-				if (sync_cycles <= 0)
-					sync_cycles += PRNG_SEQUENCE_LENGTH;
+				// no negative sync_cycles
+				if (sync_cycles <= 0) sync_cycles += PRNG_SEQUENCE_LENGTH;
+
+				// reset sync_cycles
+				if (sync_cycles > PRNG_SEQUENCE_LENGTH * 2 ) {
+					sync_cycles = PRNG_SEQUENCE_LENGTH;
+					sync_time = GetCountSspClk() & 0xfffffff8;
+				}
 				
 				if (MF_DBGLEVEL >= 4)
 					Dbprintf("calibrating in cycle %d. nt_distance=%d, elapsed_prng_sequences=%d, new sync_cycles: %d\n", i, nt_distance, elapsed_prng_sequences, sync_cycles);
 
+				LED_B_OFF();
 				continue;
 			}
 		}
+		LED_B_OFF();
 
 		if ( (nt != nt_attacked) && nt_attacked) { 	// we somehow lost sync. Try to catch up again...
 			
@@ -2503,7 +2511,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) {
 		if (received_nack) {
 			catch_up_cycles = 8; 	// the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
 	
-			if (nt_diff == 0  && first_try)
+			if (nt_diff == 0)
 				par_low = par[0] & 0xE0; // there is no need to check all parities for other nt_diff. Parity Bits for mf_nr_ar[0..2] won't change
 
 			par_list[nt_diff] = reflect8(par[0]);