File: bit_to_zynq_bin.py

package info (click to toggle)
uhd 3.7.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 456,376 kB
  • ctags: 89,637
  • sloc: ansic: 51,090; cpp: 42,755; xml: 19,627; vhdl: 12,678; tcl: 5,944; python: 5,870; ada: 2,760; sh: 2,175; makefile: 615; pascal: 230; csh: 224; perl: 11
file content (62 lines) | stat: -rwxr-xr-x 1,640 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
import sys
import os
import struct

def flip32(data):
	sl = struct.Struct('<I')
	sb = struct.Struct('>I')
	b = buffer(data)
	d = bytearray(len(data))
	for offset in xrange(0, len(data), 4):
		 sb.pack_into(d, offset, sl.unpack_from(b, offset)[0])
	return d

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()

short = struct.Struct('>H')
ulong = struct.Struct('>I')

bitfile = open(args.bitfile, 'rb')

l = short.unpack(bitfile.read(2))[0]
if l != 9:
	raise Exception, "Missing <0009> header (0x%x), not a bit file" % l
bitfile.read(l)
l = short.unpack(bitfile.read(2))[0]
d = bitfile.read(l)
if d != 'a':
	raise Exception, "Missing <a> header, not a bit file"

l = short.unpack(bitfile.read(2))[0]
d = bitfile.read(l)
print "Design name:", d

KEYNAMES = {'b': "Partname", 'c': "Date", 'd': "Time"}

while 1:
	k = bitfile.read(1)
	if not k:
		raise Exception, "unexpected EOF"
	elif k == 'e':
		l = ulong.unpack(bitfile.read(4))[0]
		print "found binary data:", l
		d = bitfile.read(l)
		if args.flip:
			d = flip32(d)
		open(args.binfile, 'wb').write(d)
		break
	elif k in KEYNAMES:
		l = short.unpack(bitfile.read(2))[0]
		d = bitfile.read(l)
		print KEYNAMES[k], d
	else:
		print "Unexpected key: ", k
		l = short.unpack(bitfile.read(2))[0]
		d = bitfile.read(l)