]> sigrok.org Git - sigrok-util.git/blob - firmware/kingst-la/sigrok-fwextract-kingst-la2016
sigrok-fwextract-kingst-la2016: pad fpga fw to expected 180224 bytes
[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
28 # reuse parseelf.py module from saleae-logic16:
29 fwdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
30 parseelf_py = os.path.join(fwdir, "saleae-logic16", "parseelf.py")
31 spec = importlib.util.spec_from_file_location("parseelf", parseelf_py)
32 parseelf = importlib.util.module_from_spec(spec)
33 spec.loader.exec_module(parseelf)
34
35 class qt_resources(object):
36     def __init__(self, program):
37         self._elf = parseelf.elf(program)
38         self._elf_sections = {} # idx -> data
39         self._read_resources()
40
41     def _get_elf_section(self, idx):
42         s = self._elf_sections.get(idx)
43         if s is None:
44             shdr = self._elf.shdrs[idx]
45             s = self._elf.read_section(shdr), shdr
46             self._elf_sections[idx] = s
47         return s
48     
49     def _get_elf_sym_value(self, sname):
50         sym = self._elf.symtab[sname]
51         section, shdr = self._get_elf_section(sym["st_shndx"])
52         addr = sym["st_value"] - shdr["sh_addr"]
53         value = section[addr:addr + sym["st_size"]]
54         if len(value) != sym["st_size"]:
55             print("warning: symbol %s should be %d bytes, but in section is only %d bytes" % (
56                 sname, sym["st_size"], len(value)))
57         return value
58     
59     # qt resource stuff:
60     def _get_resource_name(self, offset):
61         length, i = struct.unpack(">HI", self._res_names[offset:offset + 2 + 4])
62         offset += 2 + 4
63         name = self._res_names[offset:offset + 2 * length].decode("utf-16be")
64         return name
65     
66     def _get_resource_data(self, offset):
67         length = struct.unpack(">I", self._res_datas[offset:offset + 4])[0]
68         offset += 4
69         return self._res_datas[offset:offset + length]
70     
71     def _read_resources(self):
72         RCCFileInfo_Directory = 0x02
73         def read_table():
74             table = []
75             offset = 0
76             while offset < len(self._res_struct):
77                 name_offset, flags = struct.unpack(">IH", self._res_struct[offset:offset+4+2])
78                 offset += 6
79                 name = self._get_resource_name(name_offset)
80                 if flags & RCCFileInfo_Directory:
81                     child_count, first_child_offset = struct.unpack(">II", self._res_struct[offset:offset + 4 + 4])
82                     offset += 4 + 4
83                     table.append((name, flags, child_count, first_child_offset))
84                 else:
85                     country, language, data_offset = struct.unpack(">HHI", self._res_struct[offset:offset + 2 + 2 + 4])
86                     offset += 2 + 2 + 4
87                     table.append((name, flags, country, language, data_offset))
88             return table
89         def read_dir_entries(table, which, parents=[]):
90             name, flags = which[:2]
91             if not flags & RCCFileInfo_Directory:
92                 raise Exception("not a directory!")
93             child_count, first_child = which[2:]
94             for i in range(child_count):
95                 child = table[first_child + i]
96                 if child[1] & RCCFileInfo_Directory:                
97                     read_dir_entries(table, child, parents + [child[0]])
98                 else:
99                     country, language, data_offset = child[2:]
100                     full_name = "/".join(parents + [child[0]])
101                     self._resources[full_name] = data_offset
102         
103         self._res_datas = self._get_elf_sym_value("_ZL16qt_resource_data")
104         self._res_names = self._get_elf_sym_value("_ZL16qt_resource_name")
105         self._res_struct = self._get_elf_sym_value("_ZL18qt_resource_struct")
106         
107         self._resources = {} # res_fn -> res_offset
108         table = read_table()
109         read_dir_entries(table, table[0])
110
111     def get_resource(self, res_fn):
112         offset = self._resources[res_fn]
113         data = self._get_resource_data(offset)
114         return data
115
116     def find_resource_names(self, res_fn_re):
117         for key in self._resources.keys():
118             m = re.match(res_fn_re, key)
119             if m is not None:
120                 yield key
121     
122 class res_writer(object):
123     def __init__(self, res):
124         self.res = res
125         
126     def _write_file(self, fn, data, decoder=None, zero_pad_to=None):
127         if decoder is not None:
128             data = decoder(data)
129         if zero_pad_to is not None:
130             if len(data) > zero_pad_to:
131                 raise Exception("can not zero_pad_to %d bytes -- data is already %d bytes" % (zero_pad_to, len(data)))
132             data += b"\0" * (zero_pad_to - len(data))
133         with open(fn, "wb") as fp:
134             fp.write(data)
135         print("saved %d bytes to %s" % (len(data), fn))
136         
137     def extract(self, res_fn, out_fn, decoder=None, zero_pad_to=None):
138         self._write_file(out_fn, self.res.get_resource(res_fn), decoder=decoder, zero_pad_to=zero_pad_to)
139     
140     def extract_re(self, res_fn_re, out_fn, decoder=None):
141         for res_fn in res.find_resource_names(res_fn_re):
142             fn = re.sub(res_fn_re, out_fn, res_fn).lower()
143             self._write_file(fn, self.res.get_resource(res_fn), decoder=decoder)
144
145 def decode_intel_hex(hexdata):
146     """ return list of (address, data)
147     """
148     datas = []
149     # assume \n or \r\n*
150     for line in hexdata.split(b"\n"):
151         line = line.strip()
152         if chr(line[0]) != ":": raise Exception("invalid line: %r" % line)
153         offset = 1
154         record = codecs.decode(line[offset:], "hex")
155         byte_count, address, record_type = struct.unpack(">BHB", record[:1 + 2 + 1])
156         offset = 1 + 2 + 1
157         if byte_count > 0:
158             data = record[offset:offset + byte_count]
159             offset += byte_count
160         checksum = record[offset]
161         ex_checksum = (~sum(record[:offset]) + 1) & 0xff
162         if ex_checksum != checksum: raise Exception("invalid checksum %#x in %r" % (checksum, line))
163         if record_type == 0:
164             datas.append((address, data))
165         elif record_type == 1:
166             break
167     return datas
168
169 def intel_hex_as_blob(hexdata):
170     """ return continuous bytes sequence including all data
171     (loosing start address here)
172     """
173     data = decode_intel_hex(hexdata)
174     data.sort()
175     last = data[-1]
176     length = last[0] + len(last[1])
177     img = bytearray(length)
178     for off, part in data:
179         img[off:off + len(part)] = part
180     return img
181
182 def maybe_intel_hex_as_blob(data):
183     if data[0] == ord(":") and max(data) < 127:
184           return intel_hex_as_blob(data)
185     return data # keep binary data
186
187 if __name__ == "__main__":
188     if len(sys.argv) != 2:
189         print("sigrok-fwextract-kingst-la2016 <programfile>")
190         sys.exit()
191     
192     res = qt_resources(sys.argv[1])
193     
194     writer = res_writer(res)
195     writer.extract("fwfpga/LA2016A", "kingst-la2016a-fpga.bitstream", zero_pad_to=180224)
196     writer.extract_re(r"fwusb/fw(.*)", r"kingst-la-\1.fw", decoder=maybe_intel_hex_as_blob)