From 87a8504304571a207a4108b7aeb8c45bde61ea35 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 19 Apr 2014 00:01:29 +0200 Subject: [PATCH] Refactor CRC tools --- crc.py | 40 ++++++++++++++---------------------- mkmscimg.py | 13 +++++------- software/bios/Makefile | 3 +-- software/videomixer/Makefile | 3 +-- 4 files changed, 22 insertions(+), 37 deletions(-) mode change 100644 => 100755 mkmscimg.py diff --git a/crc.py b/crc.py index 23ff96e0f..26323fe2d 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 - # 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(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") + + 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 3b27656aa..78e191193 --- 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 d1fbb078c..d338c0072 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 9417acb51..a312fa0ec 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