Refactor CRC tools
authorSebastien Bourdeauducq <sb@m-labs.hk>
Fri, 18 Apr 2014 22:01:29 +0000 (00:01 +0200)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Fri, 18 Apr 2014 22:01:29 +0000 (00:01 +0200)
crc.py
mkmscimg.py [changed mode: 0644->0755]
software/bios/Makefile
software/videomixer/Makefile

diff --git a/crc.py b/crc.py
index 23ff96e0f6237a93d31ef1febd662bf1d1525768..26323fe2d345c4bd89ebb09ad1a2a69972f4af54 100644 (file)
--- 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)
old mode 100644 (file)
new mode 100755 (executable)
index 3b27656..78e1911
@@ -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)
index d1fbb078cc5169e3f57c570ad03e93254f8aff10..d338c0072dc07cfe91c5708dd281bbb5d37b37f9 100644 (file)
@@ -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
 
index 9417acb51457171c1e190890fa7c7c523a446ef5..a312fa0ec684a05dbed4e5f2aeda88267a1aefd3 100644 (file)
@@ -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