From e3b2bc9839dbb4b880d6fdcefe7187d2528a23f7 Mon Sep 17 00:00:00 2001 From: n-hutton Date: Thu, 20 Feb 2025 14:39:31 +0000 Subject: [PATCH] strip date time files --- fpga/Makefile | 2 +- fpga/strip_date_time_from_binary.py | 52 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 fpga/strip_date_time_from_binary.py diff --git a/fpga/Makefile b/fpga/Makefile index 963fd1e28..dd9849a19 100644 --- a/fpga/Makefile +++ b/fpga/Makefile @@ -188,7 +188,7 @@ work: $(Q)$(RM) $@ $*.drc $*.rbt $(info [=] BITGEN $@) $(Q)$(XILINX_TOOLS_PREFIX)bitgen $(VERBOSITY) -w $* $@ - echo "FFFFFFFFFFFFFFFFFFFFFF" | dd of=fpga_pm3_hf.bit bs=1 seek=48 conv=notrunc + #python3 ../strip_date_time_from_binary.py $@ || true $(Q)$(CP) $@ .. # Build all targets diff --git a/fpga/strip_date_time_from_binary.py b/fpga/strip_date_time_from_binary.py new file mode 100644 index 000000000..12e34c9dd --- /dev/null +++ b/fpga/strip_date_time_from_binary.py @@ -0,0 +1,52 @@ +import sys + +# File to take a .bit file generated by xilinx webpack ISE +# and replace the date and time embedded within with 'F'. +# The header of the bitfile is seperated by ascii markers +# 'a', 'b', 'c', 'd' etc. +# see fpga_compress.c for comparison + +def parse_and_split_file(filename): + split_chars = ['a', 'b', 'c', 'd'] # Characters to split on + extracted_data = [] # for debug + + print("Overwriting date and time in bitfile {}".format(filename)) + + with open(filename, 'rb') as f: # Read as binary to handle non-text files + data = f.read(100) # Read first 100 bytes which should contain all information + + decoded_data = list(data.decode(errors='ignore')) + + for i in range(len(decoded_data) - 3): + # subsequent two bytes after marker are null and the length + next_byte = ord(decoded_data[i+1]) + data_length = ord(decoded_data[i+2]) + + if decoded_data[i] == split_chars[0] and next_byte == 0x0: + start = i+3 + extracted_data.append(''.join(decoded_data[start:start+data_length])) + + # time, date + if split_chars[0] == 'c' or split_chars[0] == 'd': + decoded_data[start:start+data_length] = 'F' * data_length + + split_chars.pop(0) + + if not split_chars: + break + + print("Extracted data from bitfile: {}".format(extracted_data)) + decoded_data = ''.join(decoded_data).encode() + + with open(filename, 'r+b') as f: # Write back modified bytes + f.seek(0) + f.write(decoded_data.ljust(100, b' ')) + print("writing complete") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python script.py ") + sys.exit(1) + + filename = sys.argv[1] + parse_and_split_file(filename)