This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bit2bin.py
executable file
·79 lines (67 loc) · 2.66 KB
/
bit2bin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
# Copyright 2014-2015 Robert Jordens <[email protected]>
# after
# https://github.com/mfischer/fpgadev-zynq/blob/master/top/python/bit_to_zynq_bin.py
#
# This file is part of redpid.
#
# redpid is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# redpid is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with redpid. If not, see <http://www.gnu.org/licenses/>.
import struct
def flip32(data):
sl = struct.Struct("<I")
sb = struct.Struct(">I")
b = memoryview(data)
d = bytearray(len(data))
for offset in range(0, len(data), sl.size):
sb.pack_into(d, offset, *sl.unpack_from(b, offset))
return d
def bit2bin(bit, bin, flip=False):
bitfile = open(bit, "rb")
l, = struct.unpack(">H", bitfile.read(2))
if l != 9:
raise ValueError("Missing <0009> header, not a bit file")
bitfile.read(l)
d = bitfile.read(*struct.unpack(">H", bitfile.read(2)))
if d != b"a":
raise ValueError("Missing <a> header, not a bit file")
d = bitfile.read(*struct.unpack(">H", bitfile.read(2)))
print("Design name:", d)
while True:
key = bitfile.read(1)
if not key:
break
if key in b"bcd":
d = bitfile.read(*struct.unpack(">H", bitfile.read(2)))
name = {b"b": "Partname", b"c": "Date", b"d": "Time"}[key]
print(name, d)
elif key == b"e":
l, = struct.unpack(">I", bitfile.read(4))
print("found binary data length:", l)
d = bitfile.read(l)
if flip:
d = flip32(d)
open(bin, "wb").write(d)
else:
d = bitfile.read(*struct.unpack(">H", bitfile.read(2)))
print("Unexpected key: ", key, d)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Convert FPGA bit files to raw bin format suitable for flashing")
parser.add_argument("-f", "--flip", dest="flip", action="store_true",
default=False, help="Flip 32-bit endianess (needed for Zynq)")
parser.add_argument("bitfile", help="Input bit file name")
parser.add_argument("binfile", help="Output bin file name")
args = parser.parse_args()
bit2bin(args.bitfile, args.binfile, args.flip)