From: Sebastien Bourdeauducq Date: Fri, 18 Apr 2014 22:01:29 +0000 (+0200) Subject: Refactor CRC tools X-Git-Tag: 24jan2021_ls180~2724 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=87a8504304571a207a4108b7aeb8c45bde61ea35;p=litex.git Refactor CRC tools --- diff --git a/crc.py b/crc.py index 23ff96e0..26323fe2 100644 --- a/crc.py +++ b/crc.py @@ -1,29 +1,19 @@ import binascii -def CRC32(buf): - return binascii.crc32(buf).to_bytes(4, byteorder='big') - -def LENGTH(buf): - return len(buf).to_bytes(4, byteorder='big') - -def insert_crc(i_filename, o_filename=None): - f = open(i_filename, 'rb+') - fdata = f.read() - fcrc = CRC32(fdata) - flength = LENGTH(fdata) - f.close() - - # Write the CRC32 in big endian at the end of the file +def insert_crc(i_filename, fbi_mode=False, o_filename=None): if o_filename is None: - f = open(i_filename, 'wb') - f.write(fdata) - f.write(fcrc) - f.close() + o_filename = i_filename + + with open(i_filename, 'rb') as f: + fdata = f.read() + fcrc = binascii.crc32(fdata).to_bytes(4, byteorder="big") + flength = len(fdata).to_bytes(4, byteorder="big") - # Write a new file prepended with the size and CRC - else: - f = open(o_filename, 'wb') - f.write(flength) - f.write(fcrc) - f.write(fdata) - f.close() + with open(o_filename, 'wb') as f: + if fbi_mode: + f.write(flength) + f.write(fcrc) + f.write(fdata) + else: + f.write(fdata) + f.write(fcrc) diff --git a/mkmscimg.py b/mkmscimg.py old mode 100644 new mode 100755 index 3b27656a..78e19119 --- a/mkmscimg.py +++ b/mkmscimg.py @@ -1,15 +1,12 @@ #!/usr/bin/env python3 -import sys, argparse +import argparse import crc if __name__ == "__main__": parser = argparse.ArgumentParser(description="CRC32 computation tool and MiSoC image file writer.") - parser.add_argument("-i", default=None, help="input file") - parser.add_argument("-o", default=None, help="output file (if not specified = input file)") + parser.add_argument("input", help="input file") + parser.add_argument("-o", "--output", default=None, help="output file (if not specified, use input file)") + parser.add_argument("-f", "--fbi", default=False, action="store_true", help="build flash boot image (FBI) file") args = parser.parse_args() - - i_filename = args.i - o_filename = args.o - - crc.insert_crc(i_filename, o_filename) + crc.insert_crc(args.input, args.fbi, args.output) diff --git a/software/bios/Makefile b/software/bios/Makefile index d1fbb078..d338c007 100644 --- a/software/bios/Makefile +++ b/software/bios/Makefile @@ -1,6 +1,5 @@ MSCDIR=../.. include $(MSCDIR)/software/common.mak -PYTHON = python3 OBJECTS=isr.o sdram.o main.o boot-helper.o boot.o dataflow.o @@ -12,7 +11,7 @@ all: bios.bin %.bin: %.elf $(OBJCOPY) -O binary $< $@ chmod -x $@ - $(PYTHON) $(MSCDIR)/mkmscimg.py -i $@ + $(MSCDIR)/mkmscimg.py $@ bios.elf: linker.ld $(OBJECTS) libs diff --git a/software/videomixer/Makefile b/software/videomixer/Makefile index 9417acb5..a312fa0e 100644 --- a/software/videomixer/Makefile +++ b/software/videomixer/Makefile @@ -1,6 +1,5 @@ MSCDIR=../.. include $(MSCDIR)/software/common.mak -PYTHON = python3 OBJECTS=isr.o processor.o dvisampler0.o dvisampler1.o edid.o pll.o ci.o config.o main.o @@ -14,7 +13,7 @@ all: videomixer.bin videomixer.fbi chmod -x $@ %.fbi: %.bin - $(PYTHON) $(MSCDIR)/mkmscimg.py -i $< -o $@ + $(MSCDIR)/mkmscimg.py -f -o $@ $< videomixer.elf: $(OBJECTS) libs