[Cryptech-Commits] [sw/stm32] branch ksng updated: Rewrite core upload loop to simplify and fix race conditions.

git at cryptech.is git at cryptech.is
Wed Dec 21 20:50:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

sra at hactrn.net pushed a commit to branch ksng
in repository sw/stm32.

The following commit(s) were added to refs/heads/ksng by this push:
     new a86b6d2  Rewrite core upload loop to simplify and fix race conditions.
a86b6d2 is described below

commit a86b6d255187a0a6a91916c14b0da49210fbed91
Author: Rob Austein <sra at hactrn.net>
AuthorDate: Wed Dec 21 15:42:09 2016 -0500

    Rewrite core upload loop to simplify and fix race conditions.
    
    The main loop in cryptech_upload:send_file() was much more complicated
    than necessary, and also contained some hidden assumptions about
    serial I/O timing which happened to fail on the first two machines I
    tested.  We already had a perfectly good buffered-input function, so
    rewrote to use that, and simplified control structure in the process.
    
    In theory, the new code should work in any environment where the old
    one did, but this has not yet been confirmed.
---
 projects/hsm/cryptech_upload | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

diff --git a/projects/hsm/cryptech_upload b/projects/hsm/cryptech_upload
index 17395ce..b41f25b 100755
--- a/projects/hsm/cryptech_upload
+++ b/projects/hsm/cryptech_upload
@@ -191,33 +191,24 @@ def send_file(src, size, args, dst):
         return False
 
     # 2. Write file contents while calculating CRC-32
-    while True:
+    chunks = int((size + chunk_size - 1) / chunk_size)
+    for counter in xrange(chunks):
         data = src.read(chunk_size)
-        if not data:
-            break
         dst.write(data)
         dst.flush()
-        print("Wrote {!s} bytes (chunk {!s}/{!s})".format(len(data), counter, int(size / chunk_size)))
+        print("Wrote {!s} bytes (chunk {!s}/{!s})".format(len(data), counter + 1, chunks))
         # read ACK (a counter of number of 4k chunks received)
-        while True:
-            ack_bytes = dst.read(4)
-            if len(ack_bytes) == 4:
-                break
-            print("ERROR: Did not receive an ACK, got {!r}".format(ack_bytes))
-            dst.write("\r")  # eventually get back to the CLI prompt
-            dst.flush()
-        ack = struct.unpack("<I", ack_bytes)[0]
+        ack_bytes = ""
+        while len(ack_bytes) < 4:
+            ack_bytes += _read(dst)
+        ack = struct.unpack("<I", ack_bytes[:4])[0]
         if ack != counter + 1:
             print("ERROR: Did not receive the expected counter as ACK (got {!r}/{!r}, not {!r})".format(ack, ack_bytes, counter))
-            flush = dst.read(100)
-            print("FLUSH data: {!r}".format(flush))
             return False
         counter += 1
 
         crc = crc32(data, crc) & 0xffffffff
 
-    _read(dst)
-
     # 3. Write CRC-32 (4 bytes)
     _write(dst, struct.pack("<I", crc))
     response = _read(dst)

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Commits mailing list