3 ## This file is part of the sigrok 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([25])[a-z0-9]+1.sys$', filename, re.I)
34 model = m.group(1) + 'xxx'
44 blob = array('B', [0] * 0x4000)
45 while p <= len(data) and data[p+4] == 0:
46 num_bytes = struct.unpack("<H", data[p:p+2])[0]
47 address = struct.unpack("<H", data[p+2:p+4])[0]
49 chunk.frombytes(data[p+5:p+5+num_bytes])
53 # the FX2 only has 16K RAM. other writes are to registers
54 # in the 0xe000 region, skip those
57 blob[address:address+num_bytes] = chunk
59 if address + num_bytes > maxaddr:
60 maxaddr = address + num_bytes
62 return blob[:maxaddr].tostring()
66 print("hantek-dso-extract.py <driverfile>")
74 if len(sys.argv) != 2:
78 filename = sys.argv[1]
79 binihx = parsepe.extract_symbol(filename, '_firmware')
81 raise Exception("no firmware found")
82 blob = unsparse(binihx)
83 outfile = 'hantek-dso-' + find_model(filename) + '.fw'
84 open(outfile, 'wb').write(blob)
85 print("saved %d bytes to %s" % (len(blob), outfile))
86 except Exception as e:
87 print("Error: %s" % str(e))