3 ## This file is part of the sigrok-util project.
5 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 3 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
25 from array import array
30 def find_model(filename):
31 filename = os.path.split(filename)[-1]
32 m = re.search('^dso([a-z0-9]+)1.sys$', filename, re.I)
34 model = m.group(1).upper()
35 model = model.replace('X86', '').replace('AMD64', '').replace('IA64', '')
47 blob = array('B', [0] * 0x4000)
48 while p <= len(data) and data[p+4] == 0:
49 num_bytes = struct.unpack("<H", data[p:p+2])[0]
50 address = struct.unpack("<H", data[p+2:p+4])[0]
52 chunk.frombytes(data[p+5:p+5+num_bytes])
56 # the FX2 only has 16K RAM. other writes are to registers
57 # in the 0xe000 region, skip those
60 blob[address:address+num_bytes] = chunk
62 if address + num_bytes > maxaddr:
63 maxaddr = address + num_bytes
65 return blob[:maxaddr].tostring()
69 print("sigrok-fwextract-hantek-dso <driverfile>")
77 if len(sys.argv) != 2:
81 filename = sys.argv[1]
82 binihx = parsepe.extract_symbol(filename, '_firmware')
84 raise Exception("no firmware found")
85 blob = unsparse(binihx)
86 outfile = 'hantek-dso-' + find_model(filename) + '.fw'
87 open(outfile, 'wb').write(blob)
88 print("saved %d bytes to %s" % (len(blob), outfile))
89 except Exception as e:
90 print("Error: %s" % str(e))