]> sigrok.org Git - sigrok-util.git/blob - firmware/kingst-la/sigrok-fwextract-kingst-la2016
firmware: update SEE ALSO in firmware extraction utility manpages
[sigrok-util.git] / firmware / kingst-la / sigrok-fwextract-kingst-la2016
1 #!/usr/bin/python3
2 ##
3 ## This file is part of the sigrok-util project.
4 ##
5 ## Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
6 ##
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.
11 ##
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.
16 ##
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/>.
19 ##
20
21 import os
22 import sys
23 import re
24 import struct
25 import codecs
26 import importlib.util
27 import zlib
28
29 # reuse parseelf.py module from saleae-logic16:
30 fwdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31 parseelf_py = os.path.join(fwdir, "saleae-logic16", "parseelf.py")
32 spec = importlib.util.spec_from_file_location("parseelf", parseelf_py)
33 parseelf = importlib.util.module_from_spec(spec)
34 spec.loader.exec_module(parseelf)
35
36 class qt_resources(object):
37     def __init__(self, program):
38         self._elf = parseelf.elf(program)
39         self._elf_sections = {} # idx -> data
40         self._read_resources()
41
42     def _get_elf_section(self, idx):
43         s = self._elf_sections.get(idx)
44         if s is None:
45             shdr = self._elf.shdrs[idx]
46             s = self._elf.read_section(shdr), shdr
47             self._elf_sections[idx] = s
48         return s
49
50     def _get_elf_sym_value(self, sname):
51         sym = self._elf.symtab[sname]
52         section, shdr = self._get_elf_section(sym["st_shndx"])
53         addr = sym["st_value"] - shdr["sh_addr"]
54         value = section[addr:addr + sym["st_size"]]
55         if len(value) != sym["st_size"]:
56             print("warning: symbol %s should be %d bytes, but in section is only %d bytes" % (
57                 sname, sym["st_size"], len(value)))
58         return value
59
60     # qt resource stuff:
61     def _get_resource_name(self, offset):
62         length, i = struct.unpack(">HI", self._res_names[offset:offset + 2 + 4])
63         offset += 2 + 4
64         name = self._res_names[offset:offset + 2 * length].decode("utf-16be")
65         return name
66
67     def _get_resource_data(self, offset):
68         length = struct.unpack(">I", self._res_datas[offset:offset + 4])[0]
69         offset += 4
70         return self._res_datas[offset:offset + length]
71
72     def _read_resources(self):
73         RCCFileInfo_Directory = 0x02
74         def read_table():
75             table = []
76             offset = 0
77             while offset < len(self._res_struct):
78                 name_offset, flags = struct.unpack(">IH", self._res_struct[offset:offset+4+2])
79                 offset += 6
80                 name = self._get_resource_name(name_offset)
81                 if flags & RCCFileInfo_Directory:
82                     child_count, first_child_offset = struct.unpack(">II", self._res_struct[offset:offset + 4 + 4])
83                     offset += 4 + 4
84                     table.append((name, flags, child_count, first_child_offset))
85                 else:
86                     country, language, data_offset = struct.unpack(">HHI", self._res_struct[offset:offset + 2 + 2 + 4])
87                     offset += 2 + 2 + 4
88                     table.append((name, flags, country, language, data_offset))
89             return table
90         def read_dir_entries(table, which, parents=[]):
91             name, flags = which[:2]
92             if not flags & RCCFileInfo_Directory:
93                 raise Exception("not a directory!")
94             child_count, first_child = which[2:]
95             for i in range(child_count):
96                 child = table[first_child + i]
97                 if child[1] & RCCFileInfo_Directory:
98                     read_dir_entries(table, child, parents + [child[0]])
99                 else:
100                     country, language, data_offset = child[2:]
101                     full_name = "/".join(parents + [child[0]])
102                     self._resources[full_name] = data_offset
103
104         self._res_datas = self._get_elf_sym_value("_ZL16qt_resource_data")
105         self._res_names = self._get_elf_sym_value("_ZL16qt_resource_name")
106         self._res_struct = self._get_elf_sym_value("_ZL18qt_resource_struct")
107
108         self._resources = {} # res_fn -> res_offset
109         table = read_table()
110         read_dir_entries(table, table[0])
111
112     def get_resource(self, res_fn):
113         offset = self._resources[res_fn]
114         data = self._get_resource_data(offset)
115         return data
116
117     def find_resource_names(self, res_fn_re):
118         for key in self._resources.keys():
119             m = re.match(res_fn_re, key)
120             if m is not None:
121                 yield key
122
123 class res_writer(object):
124     def __init__(self, res):
125         self.res = res
126
127     def _write_file(self, fn, data, decoder=None, zero_pad_to=None):
128         if decoder is not None:
129             data = decoder(data)
130         if zero_pad_to is not None:
131             if len(data) > zero_pad_to:
132                 raise Exception("can not zero_pad_to %d bytes -- data is already %d bytes" % (zero_pad_to, len(data)))
133             data += b"\0" * (zero_pad_to - len(data))
134         with open(fn, "wb") as fp:
135             fp.write(data)
136         data_crc32 = zlib.crc32(data) & 0xffffffff
137         print("saved %d bytes to %s (crc32=%08x)" % (len(data), fn, data_crc32))
138
139     def extract(self, res_fn, out_fn, decoder=None, zero_pad_to=None):
140         self._write_file(out_fn, self.res.get_resource(res_fn), decoder=decoder, zero_pad_to=zero_pad_to)
141
142     def extract_re(self, res_fn_re, out_fn, decoder=None):
143         for res_fn in res.find_resource_names(res_fn_re):
144             fn = re.sub(res_fn_re, out_fn, res_fn).lower()
145             self._write_file(fn, self.res.get_resource(res_fn), decoder=decoder)
146
147 def decode_intel_hex(hexdata):
148     """ return list of (address, data)
149     """
150     datas = []
151     # assume \n or \r\n*
152     for line in hexdata.split(b"\n"):
153         line = line.strip()
154         if chr(line[0]) != ":": raise Exception("invalid line: %r" % line)
155         offset = 1
156         record = codecs.decode(line[offset:], "hex")
157         byte_count, address, record_type = struct.unpack(">BHB", record[:1 + 2 + 1])
158         offset = 1 + 2 + 1
159         if byte_count > 0:
160             data = record[offset:offset + byte_count]
161             offset += byte_count
162         checksum = record[offset]
163         ex_checksum = (~sum(record[:offset]) + 1) & 0xff
164         if ex_checksum != checksum: raise Exception("invalid checksum %#x in %r" % (checksum, line))
165         if record_type == 0:
166             datas.append((address, data))
167         elif record_type == 1:
168             break
169     return datas
170
171 def intel_hex_as_blob(hexdata):
172     """ return continuous bytes sequence including all data
173     (loosing start address here)
174     """
175     data = decode_intel_hex(hexdata)
176     data.sort()
177     last = data[-1]
178     length = last[0] + len(last[1])
179     img = bytearray(length)
180     for off, part in data:
181         img[off:off + len(part)] = part
182     return img
183
184 def maybe_intel_hex_as_blob(data):
185     if data[0] == ord(":") and max(data) < 127:
186           return intel_hex_as_blob(data)
187     return data # keep binary data
188
189 if __name__ == "__main__":
190     if len(sys.argv) != 2:
191         print("sigrok-fwextract-kingst-la2016 <programfile>")
192         sys.exit()
193
194     res = qt_resources(sys.argv[1])
195
196     writer = res_writer(res)
197
198     '''
199     05-APR-2021
200     This extraction script has been tested with the
201     vendor software versions v3.5.0 and v3.5.1
202     These files were extracted and tested successfully:
203     saved 5430 bytes to kingst-la-01a2.fw (crc32=720551a9)
204     saved 178362 bytes to kingst-la2016a1-fpga.bitstream (crc32=7cc894fa)
205     saved 178542 bytes to kingst-la2016-fpga.bitstream (crc32=20694ff1)
206     saved 178379 bytes to kingst-la1016a1-fpga.bitstream (crc32=166866be)
207     saved 178151 bytes to kingst-la1016-fpga.bitstream (crc32=7db70001)
208     '''
209
210     # extract all firmware and fpga bitstreams
211     # writer.extract_re(r"fwfpga/(.*)", r"kingst-\1-fpga.bitstream")
212     # writer.extract_re(r"fwusb/fw(.*)", r"kingst-la-\1.fw", decoder=maybe_intel_hex_as_blob)
213
214     # extract fx2 mcu firmware for both the la2016 and la1016
215     # note that 0x01a2 is the usb pid for both of these devices
216     writer.extract_re("fwusb/fw01A2", "kingst-la-01a2.fw", decoder=maybe_intel_hex_as_blob)
217
218     # extract fpga bitstreams for la2016
219     # there are two bitstreams, newer hardware uses the 'a1' bitstream
220     writer.extract_re("fwfpga/LA2016(.*)", r"kingst-la2016\1-fpga.bitstream")
221
222     # extract fpga bitstreams for la1016
223     # there are two bitstreams, newer hardware uses the 'a1' bitstream
224     writer.extract_re("fwfpga/LA1016(.*)", r"kingst-la1016\1-fpga.bitstream")