From 2f71d951e16ac9fefc89181f5b36429ef7380335 Mon Sep 17 00:00:00 2001
From: Philippe Teuwen <phil@teuwen.org>
Date: Wed, 8 May 2019 19:16:37 +0200
Subject: [PATCH] iclass: avoid last ping in fast modes

---
 client/cmdhficlass.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c
index ddafd5a46..5b22979cc 100644
--- a/client/cmdhficlass.c
+++ b/client/cmdhficlass.c
@@ -529,13 +529,6 @@ static int CmdHFiClassReader_Replay(const char *Cmd) {
     return 0;
 }
 
-/*
-static int iclassEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {
-    clearCommandBuffer();
-    SendCommandOLD(CMD_MIFARE_EML_MEMSET, blockNum, blocksCount, 0, data, blocksCount * 16);
-    return 0;
-}
-*/
 static int CmdHFiClassELoad(const char *Cmd) {
 
     char ctmp = tolower(param_getchar(Cmd, 0));
@@ -596,6 +589,10 @@ static int CmdHFiClassELoad(const char *Cmd) {
 
     while (bytes_remaining > 0) {
         uint32_t bytes_in_packet = MIN(PM3_CMD_DATA_SIZE, bytes_remaining);
+        if (bytes_in_packet == bytes_remaining) {
+            // Disable fast mode on last packet
+            conn.block_after_ACK = false;
+        }
         clearCommandBuffer();
         SendCommandOLD(CMD_ICLASS_EML_MEMSET, bytes_sent, bytes_in_packet, 0, dump + bytes_sent, bytes_in_packet);
         bytes_remaining -= bytes_in_packet;
@@ -603,11 +600,6 @@ static int CmdHFiClassELoad(const char *Cmd) {
     }
     free(dump);
 
-    // Disable fast mode and send a dummy command to make it effective
-    conn.block_after_ACK = false;
-    SendCommandNG(CMD_PING, NULL, 0);
-    WaitForResponseTimeout(CMD_PING, NULL, 1000);
-
     PrintAndLogEx(SUCCESS, "sent %d bytes of data to device emulator memory", bytes_sent);
     return 0;
 }
@@ -1966,9 +1958,11 @@ static int CmdHFiClassCheckKeys(const char *Cmd) {
         uint32_t keys = ((keycnt - i)  > chunksize) ? chunksize : keycnt - i;
 
         // last chunk?
-        if (keys == keycnt - i)
+        if (keys == keycnt - i) {
             lastChunk = true;
-
+            // Disable fast mode on last command
+            conn.block_after_ACK = false;
+        }
         uint32_t flags = lastChunk << 8;
         // bit 16
         //   - 1 indicates credit key
@@ -2035,11 +2029,6 @@ out:
 
     PrintAndLogEx(SUCCESS, "\nTime in iclass checkkeys: %.0f seconds\n", (float)t1 / 1000.0);
 
-    // Disable fast mode and send a dummy command to make it effective
-    conn.block_after_ACK = false;
-    SendCommandNG(CMD_PING, NULL, 0);
-    WaitForResponseTimeout(CMD_PING, NULL, 1000);
-
     DropField();
     free(pre);
     free(keyBlock);