scripts: fix dfu-convert.py for Python 3 and drop Python 2 support
This commit is contained in:
parent
ed156076a2
commit
ca9d8cd0ae
|
@ -1,19 +1,13 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# Written by Antonio Galea - 2010/11/18
|
# Written by Antonio Galea - 2010/11/18
|
||||||
# Distributed under Gnu LGPL 3.0
|
# Distributed under Gnu LGPL 3.0
|
||||||
# see http://www.gnu.org/licenses/lgpl-3.0.txt
|
# see http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||||
#
|
|
||||||
# Add support for Python 3 inspired by script found in Bitcraze repository
|
|
||||||
|
|
||||||
import sys,struct,zlib,os
|
import sys,struct,zlib,os
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
from intelhex import IntelHex
|
from intelhex import IntelHex
|
||||||
|
|
||||||
python3 = False
|
|
||||||
if (sys.version_info > (3, 0)):
|
|
||||||
python3 = True
|
|
||||||
|
|
||||||
DEFAULT_DEVICE="0x0483:0xdf11"
|
DEFAULT_DEVICE="0x0483:0xdf11"
|
||||||
|
|
||||||
def named(tuple,names):
|
def named(tuple,names):
|
||||||
|
@ -22,7 +16,7 @@ def consume(fmt,data,names):
|
||||||
n = struct.calcsize(fmt)
|
n = struct.calcsize(fmt)
|
||||||
return named(struct.unpack(fmt,data[:n]),names),data[n:]
|
return named(struct.unpack(fmt,data[:n]),names),data[n:]
|
||||||
def cstring(string):
|
def cstring(string):
|
||||||
return string.split('\0',1)[0]
|
return string.split(b'\0',1)[0]
|
||||||
def compute_crc(data):
|
def compute_crc(data):
|
||||||
return 0xFFFFFFFF & -zlib.crc32(data) -1
|
return 0xFFFFFFFF & -zlib.crc32(data) -1
|
||||||
|
|
||||||
|
@ -31,15 +25,15 @@ def parse(file,dump_images=False):
|
||||||
data = open(file,'rb').read()
|
data = open(file,'rb').read()
|
||||||
crc = compute_crc(data[:-4])
|
crc = compute_crc(data[:-4])
|
||||||
prefix, data = consume('<5sBIB',data,'signature version size targets')
|
prefix, data = consume('<5sBIB',data,'signature version size targets')
|
||||||
print('%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix)
|
print('%(signature)r v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix)
|
||||||
for t in range(prefix['targets']):
|
for t in range(prefix['targets']):
|
||||||
tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
|
tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
|
||||||
tprefix['num'] = t
|
tprefix['num'] = t
|
||||||
if tprefix['named']:
|
if tprefix['named']:
|
||||||
tprefix['name'] = cstring(tprefix['name'])
|
tprefix['name'] = cstring(tprefix['name'])
|
||||||
else:
|
else:
|
||||||
tprefix['name'] = ''
|
tprefix['name'] = b''
|
||||||
print('%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix)
|
print('%(signature)r %(num)d, alt setting: %(altsetting)r, name: %(name)r, size: %(size)d, elements: %(elements)d' % tprefix)
|
||||||
tsize = tprefix['size']
|
tsize = tprefix['size']
|
||||||
target, data = data[:tsize], data[tsize:]
|
target, data = data[:tsize], data[tsize:]
|
||||||
for e in range(tprefix['elements']):
|
for e in range(tprefix['elements']):
|
||||||
|
@ -55,7 +49,7 @@ def parse(file,dump_images=False):
|
||||||
if len(target):
|
if len(target):
|
||||||
print("target %d: PARSE ERROR" % t)
|
print("target %d: PARSE ERROR" % t)
|
||||||
suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
|
suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
|
||||||
print('usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix)
|
print('usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)r, %(len)d, 0x%(crc)08x' % suffix)
|
||||||
if crc != suffix['crc']:
|
if crc != suffix['crc']:
|
||||||
print("CRC ERROR: computed crc32 is 0x%08x" % crc)
|
print("CRC ERROR: computed crc32 is 0x%08x" % crc)
|
||||||
data = data[16:]
|
data = data[16:]
|
||||||
|
@ -63,30 +57,20 @@ def parse(file,dump_images=False):
|
||||||
print("PARSE ERROR")
|
print("PARSE ERROR")
|
||||||
|
|
||||||
def build(file,targets,device=DEFAULT_DEVICE):
|
def build(file,targets,device=DEFAULT_DEVICE):
|
||||||
data = ''
|
data = b''
|
||||||
if (python3):
|
|
||||||
data = b''
|
|
||||||
for t,target in enumerate(targets):
|
for t,target in enumerate(targets):
|
||||||
tdata = ''
|
tdata = b''
|
||||||
if (python3):
|
|
||||||
tdata = b''
|
|
||||||
for image in target:
|
for image in target:
|
||||||
tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
|
tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
|
||||||
|
|
||||||
trgt = 'Target'
|
trgt = b'Target'
|
||||||
st = 'ST...'
|
st = b'ST...'
|
||||||
if (python3):
|
|
||||||
trgt = b'Target'
|
|
||||||
st = b'ST...'
|
|
||||||
tdata = struct.pack('<6sBI255s2I',trgt,0,1,st,len(tdata),len(target)) + tdata
|
tdata = struct.pack('<6sBI255s2I',trgt,0,1,st,len(tdata),len(target)) + tdata
|
||||||
data += tdata
|
data += tdata
|
||||||
|
|
||||||
dfu_se = 'DfuSe'
|
dfu_se = b'DfuSe'
|
||||||
ufd = 'UFD'
|
ufd = b'UFD'
|
||||||
if (python3):
|
data = struct.pack('<5sBIB',dfu_se,1,len(data)+11,len(targets)) + data
|
||||||
dfu_se = b'DfuSe'
|
|
||||||
ufd = b'UFD'
|
|
||||||
data = struct.pack('<5sBIB',dfu_se,1,len(data)+11,len(targets)) + data
|
|
||||||
|
|
||||||
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
|
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
|
||||||
data += struct.pack('<4H3sB',0,d,v,0x011a,ufd,16)
|
data += struct.pack('<4H3sB',0,d,v,0x011a,ufd,16)
|
||||||
|
|
|
@ -27,7 +27,7 @@ all: blackmagic.bin blackmagic.hex
|
||||||
|
|
||||||
blackmagic.dfu: blackmagic.hex
|
blackmagic.dfu: blackmagic.hex
|
||||||
@echo Creating $@
|
@echo Creating $@
|
||||||
@python ../scripts/dfu-convert.py -i $< $@
|
@python3 ../scripts/dfu-convert.py -i $< $@
|
||||||
|
|
||||||
host_clean:
|
host_clean:
|
||||||
-$(Q)$(RM) blackmagic.bin blackmagic.hex blackmagic.dfu
|
-$(Q)$(RM) blackmagic.bin blackmagic.hex blackmagic.dfu
|
||||||
|
|
Loading…
Reference in New Issue