Files
vincentb.nikhef.nl e8eba3ecd0 Added timeslice duration flag to monitoring packet
Made buildtoimg.py Python 3 compatible
2017-07-06 13:19:45 +00:00

110 lines
2.1 KiB
Python

# This script reads the build file, the FPGA bit-file and prepends this data to the generated
# bin file
#
# Arguments:
# buildfile image-type bit-file bin-file
#
#
# Author:
# Vincent van Beveren
# 2016-10-24
from __future__ import print_function
import os,sys
import ctypes as ct
if len(sys.argv) != 5:
print("Expected 4 arguments: buildfile bitfile imgType binfile")
sys.exit(1)
buildfile = sys.argv[1]
bitfile = sys.argv[2]
imgType = int(sys.argv[3])
binfile = sys.argv[4]
# increase build number
if not os.path.exists(buildfile):
print("Buildfile does not exist: ", buildfile)
sys.exit(1)
if not os.path.exists(bitfile):
print("Bitfile does not exist: ", bitfile)
sys.exit(1)
if not os.path.exists(binfile):
print("Binfile does not exist: ", binfile)
sys.exit(1)
with open(buildfile, "r") as bf:
swRev = int(bf.read(), 16)
with open(bitfile, "rb") as bf:
header = bf.read(100)
USER_ID = str.encode("UserID=0x")
pos = header.find(USER_ID)
if pos == -1:
print("No userID found in bitfile!")
sys.exit(1)
pos += len(USER_ID)
fwRevS = header[pos:pos+8]
# fix if the revision is only 7 nibbles
if fwRevS[7] == '\x00':
fwRevS = fwRevS[0:7] + '0'
fwRev = int(fwRevS, 16)
print("SwRev=%08x HwRev=%08x Type=%d" % (swRev, fwRev, imgType))
class HEADER(ct.BigEndianStructure):
_pack_ = 1
_fields_ = [("magic", ct.c_uint16),
("fwrev", ct.c_uint32),
("swrev", ct.c_uint32),
("imgtype", ct.c_uint8)
]
hdr = HEADER()
MAGIC = 0xDA7A
with open(binfile, "rb") as bf:
binfileContent = bf.read()
# sio.StringIO(binfileContent[0:ct.sizeof(hdr)]).readinto(hdr)
ct.memmove(ct.addressof(hdr), binfileContent[0:ct.sizeof(hdr)], ct.sizeof(hdr))
# Remove magic
if hdr.magic == MAGIC:
print("Overwriting previous revision data")
binfileContent = binfileContent[ct.sizeof(hdr):]
hdr.magic = MAGIC
hdr.fwrev = fwRev
hdr.swrev = swRev
hdr.imgtype = imgType
with open(binfile, "wb") as bf:
bf.write(hdr)
bf.write(binfileContent)