|  | @@ -1,12 +1,18 @@
 | 
											
												
													
														|  | -#!/usr/bin/env python2
 |  | 
 | 
											
												
													
														|  | 
 |  | +#!/usr/bin/env python
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  # 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
 | 
											
												
													
														|  | 
 |  | +#
 | 
											
												
													
														|  | 
 |  | +# Removed ihex functionality and dependency, Bitcraze - 2014-12-11
 | 
											
												
													
														|  | 
 |  | +# Added support for python3, Bitcraze - 2020-02-18
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import sys,struct,zlib,os
 |  |  import sys,struct,zlib,os
 | 
											
												
													
														|  |  from optparse import OptionParser
 |  |  from optparse import OptionParser
 | 
											
												
													
														|  | -from intelhex import IntelHex
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +python3 = False
 | 
											
												
													
														|  | 
 |  | +if (sys.version_info > (3, 0)):
 | 
											
												
													
														|  | 
 |  | +      python3 = True
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  DEFAULT_DEVICE="0x0483:0xdf11"
 |  |  DEFAULT_DEVICE="0x0483:0xdf11"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -21,11 +27,11 @@ def compute_crc(data):
 | 
											
												
													
														|  |    return 0xFFFFFFFF & -zlib.crc32(data) -1
 |  |    return 0xFFFFFFFF & -zlib.crc32(data) -1
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  def parse(file,dump_images=False):
 |  |  def parse(file,dump_images=False):
 | 
											
												
													
														|  | -  print 'File: "%s"' % file
 |  | 
 | 
											
												
													
														|  | 
 |  | +  print('File: "%s"' % file)
 | 
											
												
													
														|  |    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)s 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
 | 
											
										
											
												
													
														|  | @@ -33,40 +39,57 @@ def parse(file,dump_images=False):
 | 
											
												
													
														|  |        tprefix['name'] = cstring(tprefix['name'])
 |  |        tprefix['name'] = cstring(tprefix['name'])
 | 
											
												
													
														|  |      else:
 |  |      else:
 | 
											
												
													
														|  |        tprefix['name'] = ''
 |  |        tprefix['name'] = ''
 | 
											
												
													
														|  | -    print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix
 |  | 
 | 
											
												
													
														|  | 
 |  | +    print('%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", 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']):
 | 
											
												
													
														|  |        eprefix, target = consume('<2I',target,'address size')
 |  |        eprefix, target = consume('<2I',target,'address size')
 | 
											
												
													
														|  |        eprefix['num'] = e
 |  |        eprefix['num'] = e
 | 
											
												
													
														|  | -      print '  %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix
 |  | 
 | 
											
												
													
														|  | 
 |  | +      print('  %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix)
 | 
											
												
													
														|  |        esize = eprefix['size']
 |  |        esize = eprefix['size']
 | 
											
												
													
														|  |        image, target = target[:esize], target[esize:]
 |  |        image, target = target[:esize], target[esize:]
 | 
											
												
													
														|  |        if dump_images:
 |  |        if dump_images:
 | 
											
												
													
														|  |          out = '%s.target%d.image%d.bin' % (file,t,e)
 |  |          out = '%s.target%d.image%d.bin' % (file,t,e)
 | 
											
												
													
														|  |          open(out,'wb').write(image)
 |  |          open(out,'wb').write(image)
 | 
											
												
													
														|  | -        print '    DUMPED IMAGE TO "%s"' % out
 |  | 
 | 
											
												
													
														|  | 
 |  | +        print('    DUMPED IMAGE TO "%s"' % out)
 | 
											
												
													
														|  |      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)s, %(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:]
 | 
											
												
													
														|  |    if data:
 |  |    if data:
 | 
											
												
													
														|  | -    print "PARSE ERROR"
 |  | 
 | 
											
												
													
														|  | 
 |  | +    print("PARSE ERROR")
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  def build(file,targets,device=DEFAULT_DEVICE):
 |  |  def build(file,targets,device=DEFAULT_DEVICE):
 | 
											
												
													
														|  |    data = ''
 |  |    data = ''
 | 
											
												
													
														|  | 
 |  | +  if (python3):
 | 
											
												
													
														|  | 
 |  | +        data = b''
 | 
											
												
													
														|  |    for t,target in enumerate(targets):
 |  |    for t,target in enumerate(targets):
 | 
											
												
													
														|  |      tdata = ''
 |  |      tdata = ''
 | 
											
												
													
														|  | 
 |  | +    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']
 | 
											
												
													
														|  | -    tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    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
 | 
											
												
													
														|  |      data += tdata
 |  |      data += tdata
 | 
											
												
													
														|  | -  data  = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +  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
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |    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)
 | 
											
												
													
														|  |    crc   = compute_crc(data)
 |  |    crc   = compute_crc(data)
 | 
											
												
													
														|  |    data += struct.pack('<I',crc)
 |  |    data += struct.pack('<I',crc)
 | 
											
												
													
														|  |    open(file,'wb').write(data)
 |  |    open(file,'wb').write(data)
 | 
											
										
											
												
													
														|  | @@ -74,52 +97,37 @@ def build(file,targets,device=DEFAULT_DEVICE):
 | 
											
												
													
														|  |  if __name__=="__main__":
 |  |  if __name__=="__main__":
 | 
											
												
													
														|  |    usage = """
 |  |    usage = """
 | 
											
												
													
														|  |  %prog [-d|--dump] infile.dfu
 |  |  %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"""
 |  | 
 | 
											
												
													
														|  | 
 |  | +%prog {-b|--build} address:file.bin [-b address:file.bin ...] [{-D|--device}=vendor:device] outfile.dfu"""
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |    parser = OptionParser(usage=usage)
 |  |    parser = OptionParser(usage=usage)
 | 
											
												
													
														|  |    parser.add_option("-b", "--build", action="append", dest="binfiles",
 |  |    parser.add_option("-b", "--build", action="append", dest="binfiles",
 | 
											
												
													
														|  |      help="build a DFU file from given BINFILES", metavar="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",
 |  |    parser.add_option("-D", "--device", action="store", dest="device",
 | 
											
												
													
														|  |      help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
 |  |      help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
 | 
											
												
													
														|  |    parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
 |  |    parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
 | 
											
												
													
														|  |      default=False, help="dump contained images to current directory")
 |  |      default=False, help="dump contained images to current directory")
 | 
											
												
													
														|  |    (options, args) = parser.parse_args()
 |  |    (options, args) = parser.parse_args()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -  if (options.binfiles or options.hexfiles) and len(args)==1:
 |  | 
 | 
											
												
													
														|  | 
 |  | +  if options.binfiles and len(args)==1:
 | 
											
												
													
														|  |      target = []
 |  |      target = []
 | 
											
												
													
														|  | -    
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      if options.binfiles:
 |  |      if options.binfiles:
 | 
											
												
													
														|  |        for arg in options.binfiles:
 |  |        for arg in options.binfiles:
 | 
											
												
													
														|  |          try:
 |  |          try:
 | 
											
												
													
														|  |            address,binfile = arg.split(':',1)
 |  |            address,binfile = arg.split(':',1)
 | 
											
												
													
														|  |          except ValueError:
 |  |          except ValueError:
 | 
											
												
													
														|  | -          print "Address:file couple '%s' invalid." % arg
 |  | 
 | 
											
												
													
														|  | 
 |  | +          print("Address:file couple '%s' invalid." % arg)
 | 
											
												
													
														|  |            sys.exit(1)
 |  |            sys.exit(1)
 | 
											
												
													
														|  |          try:
 |  |          try:
 | 
											
												
													
														|  |            address = int(address,0) & 0xFFFFFFFF
 |  |            address = int(address,0) & 0xFFFFFFFF
 | 
											
												
													
														|  |          except ValueError:
 |  |          except ValueError:
 | 
											
												
													
														|  | -          print "Address %s invalid." % address
 |  | 
 | 
											
												
													
														|  | 
 |  | +          print("Address %s invalid." % address)
 | 
											
												
													
														|  |            sys.exit(1)
 |  |            sys.exit(1)
 | 
											
												
													
														|  |          if not os.path.isfile(binfile):
 |  |          if not os.path.isfile(binfile):
 | 
											
												
													
														|  | -          print "Unreadable file '%s'." % binfile
 |  | 
 | 
											
												
													
														|  | 
 |  | +          print("Unreadable file '%s'." % binfile)
 | 
											
												
													
														|  |            sys.exit(1)
 |  |            sys.exit(1)
 | 
											
												
													
														|  |          target.append({ 'address': address, 'data': open(binfile,'rb').read() })
 |  |          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:
 |  | 
 | 
											
												
													
														|  | -          print "Address %s invalid." % address
 |  | 
 | 
											
												
													
														|  | -          sys.exit(1)
 |  | 
 | 
											
												
													
														|  | -        target.append({ 'address': address, 'data': data })
 |  | 
 | 
											
												
													
														|  | -    
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      outfile = args[0]
 |  |      outfile = args[0]
 | 
											
												
													
														|  |      device = DEFAULT_DEVICE
 |  |      device = DEFAULT_DEVICE
 | 
											
												
													
														|  |      if options.device:
 |  |      if options.device:
 | 
											
										
											
												
													
														|  | @@ -127,13 +135,13 @@ if __name__=="__main__":
 | 
											
												
													
														|  |      try:
 |  |      try:
 | 
											
												
													
														|  |        v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
 |  |        v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
 | 
											
												
													
														|  |      except:
 |  |      except:
 | 
											
												
													
														|  | -      print "Invalid device '%s'." % device
 |  | 
 | 
											
												
													
														|  | 
 |  | +      print("Invalid device '%s'." % device)
 | 
											
												
													
														|  |        sys.exit(1)
 |  |        sys.exit(1)
 | 
											
												
													
														|  |      build(outfile,[target],device)
 |  |      build(outfile,[target],device)
 | 
											
												
													
														|  |    elif len(args)==1:
 |  |    elif len(args)==1:
 | 
											
												
													
														|  |      infile = args[0]
 |  |      infile = args[0]
 | 
											
												
													
														|  |      if not os.path.isfile(infile):
 |  |      if not os.path.isfile(infile):
 | 
											
												
													
														|  | -      print "Unreadable file '%s'." % infile
 |  | 
 | 
											
												
													
														|  | 
 |  | +      print("Unreadable file '%s'." % infile)
 | 
											
												
													
														|  |        sys.exit(1)
 |  |        sys.exit(1)
 | 
											
												
													
														|  |      parse(infile, dump_images=options.dump_images)
 |  |      parse(infile, dump_images=options.dump_images)
 | 
											
												
													
														|  |    else:
 |  |    else:
 |