From e3a5752a303dff144dec71f909b5721cf2ffdb64 Mon Sep 17 00:00:00 2001 From: "Pavel V. Shatov (Meister)" Date: Mon, 20 Dec 2021 12:46:29 +0300 Subject: Small fix to the upload script. Skip dummy text information preceeding actual bitstream data. --- projects/hsm/cryptech_upload | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'projects') diff --git a/projects/hsm/cryptech_upload b/projects/hsm/cryptech_upload index b40427d..53d71e3 100755 --- a/projects/hsm/cryptech_upload +++ b/projects/hsm/cryptech_upload @@ -236,6 +236,24 @@ class ManagementPortSocket(ManagementPortAbstract): self.socket.close() +def find_bitstream_header(src, chunk_size): + + # this assumes, that the magic header is within the very first chunk of + # the bitstream file, which should always be the case + + xilinx_magic = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \ + b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \ + b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \ + b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \ + b"\x00\x00\x00\xBB\x11\x22\x00\x44" \ + b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \ + b"\xAA\x99\x55\x66" + + header = src.read(chunk_size) + + return header.find(xilinx_magic) + + def send_file(src, size, args, dst): """ Upload an image from some file-like source to the management port. @@ -260,6 +278,21 @@ def send_file(src, size, args, dst): print("Device did not accept the upload command (got {!r})".format(response)) return False + # remove garbage from beginning of bitstream + if args.fpga: + + # find magic pattern + dummy_bytes = find_bitstream_header(src, chunk_size) + if dummy_bytes < 0: + print("Can't find Xilinx magic marker in bistream") + return False + + print("Bitstream magic header found at offset {}".format(dummy_bytes)) + + # skip leading garbage + size -= dummy_bytes + src.seek(dummy_bytes) + dst.set_timeout(0.001) crc = 0 counter = 0 -- cgit v1.2.3