2020-11-21 16:07:48 +00:00
|
|
|
#!/usr/bin/env python
|
2015-03-04 05:30:36 +00:00
|
|
|
|
|
|
|
# Written by Antonio Galea - 2010/11/18
|
|
|
|
# Distributed under Gnu LGPL 3.0
|
|
|
|
# see http://www.gnu.org/licenses/lgpl-3.0.txt
|
2020-11-21 16:07:48 +00:00
|
|
|
#
|
|
|
|
# Add support for Python 3 inspired by script found in Bitcraze repository
|
2015-03-04 05:30:36 +00:00
|
|
|
|
|
|
|
import sys,struct,zlib,os
|
|
|
|
from optparse import OptionParser
|
|
|
|
from intelhex import IntelHex
|
|
|
|
|
2020-11-21 16:07:48 +00:00
|
|
|
python3 = False
|
|
|
|
if (sys.version_info > (3, 0)):
|
|
|
|
python3 = True
|
|
|
|
|
2015-03-04 05:30:36 +00:00
|
|
|
DEFAULT_DEVICE="0x0483:0xdf11"
|
|
|
|
|
|
|
|
def named(tuple,names):
|
|
|
|
return dict(zip(names.split(),tuple))
|
|
|
|
def consume(fmt,data,names):
|
|
|
|
n = struct.calcsize(fmt)
|
|
|
|
return named(struct.unpack(fmt,data[:n]),names),data[n:]
|
|
|
|
def cstring(string):
|
|
|
|
return string.split('\0',1)[0]
|
|
|
|
def compute_crc(data):
|
|
|
|
return 0xFFFFFFFF & -zlib.crc32(data) -1
|
|
|
|
|
|
|
|
def parse(file,dump_images=False):
|
2020-11-21 16:07:48 +00:00
|
|
|
print('File: "%s"' % file)
|
2015-03-04 05:30:36 +00:00
|
|
|
data = open(file,'rb').read()
|
|
|
|
crc = compute_crc(data[:-4])
|
|
|
|
prefix, data = consume('<5sBIB',data,'signature version size targets')
|
2020-11-21 16:07:48 +00:00
|
|
|
print('%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix)
|
2015-03-04 05:30:36 +00:00
|
|
|
for t in range(prefix['targets']):
|
|
|
|
tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
|
|
|
|
tprefix['num'] = t
|
|
|
|
if tprefix['named']:
|
|
|
|
tprefix['name'] = cstring(tprefix['name'])
|
|
|
|
else:
|
|
|
|
tprefix['name'] = ''
|
2020-11-21 16:07:48 +00:00
|
|
|
print('%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix)
|
2015-03-04 05:30:36 +00:00
|
|
|
tsize = tprefix['size']
|
|
|
|
target, data = data[:tsize], data[tsize:]
|
|
|
|
for e in range(tprefix['elements']):
|
|
|
|
eprefix, target = consume('<2I',target,'address size')
|
|
|
|
eprefix['num'] = e
|
2020-11-21 16:07:48 +00:00
|
|
|
print(' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix)
|
2015-03-04 05:30:36 +00:00
|
|
|
esize = eprefix['size']
|
|
|
|
image, target = target[:esize], target[esize:]
|
|
|
|
if dump_images:
|
|
|
|
out = '%s.target%d.image%d.bin' % (file,t,e)
|
|
|
|
open(out,'wb').write(image)
|
2020-11-21 16:07:48 +00:00
|
|
|
print(' DUMPED IMAGE TO "%s"' % out)
|
2015-03-04 05:30:36 +00:00
|
|
|
if len(target):
|
2020-11-21 16:07:48 +00:00
|
|
|
print("target %d: PARSE ERROR" % t)
|
2015-03-04 05:30:36 +00:00
|
|
|
suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
|
2020-11-21 16:07:48 +00:00
|
|
|
print('usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix)
|
2015-03-04 05:30:36 +00:00
|
|
|
if crc != suffix['crc']:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("CRC ERROR: computed crc32 is 0x%08x" % crc)
|
2015-03-04 05:30:36 +00:00
|
|
|
data = data[16:]
|
|
|
|
if data:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("PARSE ERROR")
|
2015-03-04 05:30:36 +00:00
|
|
|
|
|
|
|
def build(file,targets,device=DEFAULT_DEVICE):
|
|
|
|
data = ''
|
2020-11-21 16:07:48 +00:00
|
|
|
if (python3):
|
|
|
|
data = b''
|
2015-03-04 05:30:36 +00:00
|
|
|
for t,target in enumerate(targets):
|
|
|
|
tdata = ''
|
2020-11-21 16:07:48 +00:00
|
|
|
if (python3):
|
|
|
|
tdata = b''
|
2015-03-04 05:30:36 +00:00
|
|
|
for image in target:
|
|
|
|
tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
|
2020-11-21 16:07:48 +00:00
|
|
|
|
|
|
|
trgt = 'Target'
|
|
|
|
st = 'ST...'
|
|
|
|
if (python3):
|
|
|
|
trgt = b'Target'
|
|
|
|
st = b'ST...'
|
|
|
|
tdata = struct.pack('<6sBI255s2I',trgt,0,1,st,len(tdata),len(target)) + tdata
|
2015-03-04 05:30:36 +00:00
|
|
|
data += tdata
|
2020-11-21 16:07:48 +00:00
|
|
|
|
|
|
|
dfu_se = 'DfuSe'
|
|
|
|
ufd = 'UFD'
|
|
|
|
if (python3):
|
|
|
|
dfu_se = b'DfuSe'
|
|
|
|
ufd = b'UFD'
|
|
|
|
data = struct.pack('<5sBIB',dfu_se,1,len(data)+11,len(targets)) + data
|
|
|
|
|
2015-03-04 05:30:36 +00:00
|
|
|
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
|
2020-11-21 16:07:48 +00:00
|
|
|
data += struct.pack('<4H3sB',0,d,v,0x011a,ufd,16)
|
2015-03-04 05:30:36 +00:00
|
|
|
crc = compute_crc(data)
|
|
|
|
data += struct.pack('<I',crc)
|
|
|
|
open(file,'wb').write(data)
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
usage = """
|
|
|
|
%prog [-d|--dump] infile.dfu
|
|
|
|
%prog {-b|--build} address:file.bin [-b address:file.bin ...] [{-D|--device}=vendor:device] outfile.dfu
|
|
|
|
%prog {-i|--ihex} file.hex [-i file.hex ...] [{-D|--device}=vendor:device] outfile.dfu"""
|
|
|
|
|
|
|
|
parser = OptionParser(usage=usage)
|
|
|
|
parser.add_option("-b", "--build", action="append", dest="binfiles",
|
|
|
|
help="build a DFU file from given BINFILES", metavar="BINFILES")
|
|
|
|
parser.add_option("-i", "--ihex", action="append", dest="hexfiles",
|
|
|
|
help="build a DFU file from given HEXFILES", metavar="HEXFILES")
|
|
|
|
parser.add_option("-D", "--device", action="store", dest="device",
|
|
|
|
help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
|
|
|
|
parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
|
|
|
|
default=False, help="dump contained images to current directory")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if (options.binfiles or options.hexfiles) and len(args)==1:
|
|
|
|
target = []
|
|
|
|
|
|
|
|
if options.binfiles:
|
|
|
|
for arg in options.binfiles:
|
|
|
|
try:
|
|
|
|
address,binfile = arg.split(':',1)
|
|
|
|
except ValueError:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Address:file couple '%s' invalid." % arg)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
try:
|
|
|
|
address = int(address,0) & 0xFFFFFFFF
|
|
|
|
except ValueError:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Address %s invalid." % address)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
if not os.path.isfile(binfile):
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Unreadable file '%s'." % binfile)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
target.append({ 'address': address, 'data': open(binfile,'rb').read() })
|
|
|
|
|
|
|
|
if options.hexfiles:
|
|
|
|
for hex in options.hexfiles:
|
|
|
|
ih = IntelHex(hex)
|
|
|
|
address = ih.minaddr()
|
|
|
|
data = ih.tobinstr()
|
|
|
|
try:
|
|
|
|
address = address & 0xFFFFFFFF
|
|
|
|
except ValueError:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Address %s invalid." % address)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
target.append({ 'address': address, 'data': data })
|
|
|
|
|
|
|
|
outfile = args[0]
|
|
|
|
device = DEFAULT_DEVICE
|
|
|
|
if options.device:
|
|
|
|
device=options.device
|
|
|
|
try:
|
|
|
|
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
|
|
|
|
except:
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Invalid device '%s'." % device)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
build(outfile,[target],device)
|
|
|
|
elif len(args)==1:
|
|
|
|
infile = args[0]
|
|
|
|
if not os.path.isfile(infile):
|
2020-11-21 16:07:48 +00:00
|
|
|
print("Unreadable file '%s'." % infile)
|
2015-03-04 05:30:36 +00:00
|
|
|
sys.exit(1)
|
|
|
|
parse(infile, dump_images=options.dump_images)
|
|
|
|
else:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|