|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Written by Antonio Galea - 2010/11/18 |
| 4 | +# Distributed under Gnu LGPL 3.0 |
| 5 | +# see http://www.gnu.org/licenses/lgpl-3.0.txt |
| 6 | + |
| 7 | +import sys,struct,zlib,os |
| 8 | +from optparse import OptionParser |
| 9 | + |
| 10 | +DEFAULT_DEVICE="0x0483:0xdf11" |
| 11 | + |
| 12 | +def named(tuple,names): |
| 13 | + return dict(zip(names.split(),tuple)) |
| 14 | +def consume(fmt,data,names): |
| 15 | + n = struct.calcsize(fmt) |
| 16 | + return named(struct.unpack(fmt,data[:n]),names),data[n:] |
| 17 | +def cstring(string): |
| 18 | + return string.split('\0',1)[0] |
| 19 | +def compute_crc(data): |
| 20 | + return 0xFFFFFFFF & -zlib.crc32(data) -1 |
| 21 | + |
| 22 | +def parse(file,dump_images=False): |
| 23 | + print 'File: "%s"' % file |
| 24 | + data = open(file,'rb').read() |
| 25 | + crc = compute_crc(data[:-4]) |
| 26 | + prefix, data = consume('<5sBIB',data,'signature version size targets') |
| 27 | + print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix |
| 28 | + for t in range(prefix['targets']): |
| 29 | + tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements') |
| 30 | + tprefix['num'] = t |
| 31 | + if tprefix['named']: |
| 32 | + tprefix['name'] = cstring(tprefix['name']) |
| 33 | + else: |
| 34 | + tprefix['name'] = '' |
| 35 | + print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix |
| 36 | + tsize = tprefix['size'] |
| 37 | + target, data = data[:tsize], data[tsize:] |
| 38 | + for e in range(tprefix['elements']): |
| 39 | + eprefix, target = consume('<2I',target,'address size') |
| 40 | + eprefix['num'] = e |
| 41 | + print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix |
| 42 | + esize = eprefix['size'] |
| 43 | + image, target = target[:esize], target[esize:] |
| 44 | + if dump_images: |
| 45 | + out = '%s.target%d.image%d.bin' % (file,t,e) |
| 46 | + open(out,'wb').write(image) |
| 47 | + print ' DUMPED IMAGE TO "%s"' % out |
| 48 | + if len(target): |
| 49 | + print "target %d: PARSE ERROR" % t |
| 50 | + suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc') |
| 51 | + print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix |
| 52 | + if crc != suffix['crc']: |
| 53 | + print "CRC ERROR: computed crc32 is 0x%08x" % crc |
| 54 | + data = data[16:] |
| 55 | + if data: |
| 56 | + print "PARSE ERROR" |
| 57 | + |
| 58 | +def build(file,targets,device=DEFAULT_DEVICE): |
| 59 | + data = '' |
| 60 | + for t,target in enumerate(targets): |
| 61 | + tdata = '' |
| 62 | + for image in target: |
| 63 | + tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] |
| 64 | + tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata |
| 65 | + data += tdata |
| 66 | + data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data |
| 67 | + v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) |
| 68 | + data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16) |
| 69 | + crc = compute_crc(data) |
| 70 | + data += struct.pack('<I',crc) |
| 71 | + open(file,'wb').write(data) |
| 72 | + |
| 73 | +if __name__=="__main__": |
| 74 | + usage = """ |
| 75 | +%prog [-d|--dump] infile.dfu |
| 76 | +%prog {-b|--build} address:file.bin [-b address:file.bin ...] [{-D|--device}=vendor:device] outfile.dfu""" |
| 77 | + parser = OptionParser(usage=usage) |
| 78 | + parser.add_option("-b", "--build", action="append", dest="binfiles", |
| 79 | + help="build a DFU file from given BINFILES", metavar="BINFILES") |
| 80 | + parser.add_option("-D", "--device", action="store", dest="device", |
| 81 | + help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE") |
| 82 | + parser.add_option("-d", "--dump", action="store_true", dest="dump_images", |
| 83 | + default=False, help="dump contained images to current directory") |
| 84 | + (options, args) = parser.parse_args() |
| 85 | + |
| 86 | + if options.binfiles and len(args)==1: |
| 87 | + target = [] |
| 88 | + for arg in options.binfiles: |
| 89 | + try: |
| 90 | + address,binfile = arg.split(':',1) |
| 91 | + except ValueError: |
| 92 | + print "Address:file couple '%s' invalid." % arg |
| 93 | + sys.exit(1) |
| 94 | + try: |
| 95 | + address = int(address,0) & 0xFFFFFFFF |
| 96 | + except ValueError: |
| 97 | + print "Address %s invalid." % address |
| 98 | + sys.exit(1) |
| 99 | + if not os.path.isfile(binfile): |
| 100 | + print "Unreadable file '%s'." % binfile |
| 101 | + sys.exit(1) |
| 102 | + target.append({ 'address': address, 'data': open(binfile,'rb').read() }) |
| 103 | + outfile = args[0] |
| 104 | + device = DEFAULT_DEVICE |
| 105 | + if options.device: |
| 106 | + device=options.device |
| 107 | + try: |
| 108 | + v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) |
| 109 | + except: |
| 110 | + print "Invalid device '%s'." % device |
| 111 | + sys.exit(1) |
| 112 | + build(outfile,[target],device) |
| 113 | + elif len(args)==1: |
| 114 | + infile = args[0] |
| 115 | + if not os.path.isfile(infile): |
| 116 | + print "Unreadable file '%s'." % infile |
| 117 | + sys.exit(1) |
| 118 | + parse(infile, dump_images=options.dump_images) |
| 119 | + else: |
| 120 | + parser.print_help() |
| 121 | + sys.exit(1) |
0 commit comments