litex: reorganize things, first work working version
[litex.git] / litex / soc / tools / mkmscimg.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import binascii
5
6
7 def insert_crc(i_filename, fbi_mode=False, o_filename=None):
8 if o_filename is None:
9 o_filename = i_filename
10
11 with open(i_filename, "rb") as f:
12 fdata = f.read()
13 fcrc = binascii.crc32(fdata).to_bytes(4, byteorder="big")
14 flength = len(fdata).to_bytes(4, byteorder="big")
15
16 with open(o_filename, "wb") as f:
17 if fbi_mode:
18 f.write(flength)
19 f.write(fcrc)
20 f.write(fdata)
21 else:
22 f.write(fdata)
23 f.write(fcrc)
24
25
26 def main():
27 parser = argparse.ArgumentParser(description="CRC32 computation tool and MiSoC image file writer.")
28 parser.add_argument("input", help="input file")
29 parser.add_argument("-o", "--output", default=None, help="output file (if not specified, use input file)")
30 parser.add_argument("-f", "--fbi", default=False, action="store_true", help="build flash boot image (FBI) file")
31 args = parser.parse_args()
32 insert_crc(args.input, args.fbi, args.output)
33
34
35 if __name__ == "__main__":
36 main()