X-Git-Url: https://sigrok.org/gitweb/?p=sigrok-util.git;a=blobdiff_plain;f=firmware%2Fkingst-la%2Fsigrok-fwextract-kingst-la2016;h=712bb01f9399c5479a96b2249318a746293bb291;hp=a5832184577a9f661a559925cb122f19b72cd919;hb=9814f29fa896bd7a68d33548df2a6f4d8c66bb16;hpb=f571189ce1df1abe3dca03e0f8b78ad4ab1dae28 diff --git a/firmware/kingst-la/sigrok-fwextract-kingst-la2016 b/firmware/kingst-la/sigrok-fwextract-kingst-la2016 index a583218..712bb01 100755 --- a/firmware/kingst-la/sigrok-fwextract-kingst-la2016 +++ b/firmware/kingst-la/sigrok-fwextract-kingst-la2016 @@ -18,14 +18,20 @@ ## along with this program; if not, see . ## +# This utility extracts FX2 MCU firmware and FPGA bitstream images from +# the "KingstVIS" vendor software. The blobs are kept in Qt resources +# sections. The script was tested with several v3.5 software versions. + +import argparse import os import sys import re import struct import codecs import importlib.util +import zlib -# reuse parseelf.py module from saleae-logic16: +# Reuse the parseelf.py module from saleae-logic16. fwdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) parseelf_py = os.path.join(fwdir, "saleae-logic16", "parseelf.py") spec = importlib.util.spec_from_file_location("parseelf", parseelf_py) @@ -56,7 +62,7 @@ class qt_resources(object): sname, sym["st_size"], len(value))) return value - # qt resource stuff: + # Qt resource stuff. def _get_resource_name(self, offset): length, i = struct.unpack(">HI", self._res_names[offset:offset + 2 + 4]) offset += 2 + 4 @@ -132,7 +138,8 @@ class res_writer(object): data += b"\0" * (zero_pad_to - len(data)) with open(fn, "wb") as fp: fp.write(data) - print("saved %d bytes to %s" % (len(data), fn)) + data_crc32 = zlib.crc32(data) & 0xffffffff + print("saved %d bytes to %s (crc32=%08x)" % (len(data), fn, data_crc32)) def extract(self, res_fn, out_fn, decoder=None, zero_pad_to=None): self._write_file(out_fn, self.res.get_resource(res_fn), decoder=decoder, zero_pad_to=zero_pad_to) @@ -146,7 +153,7 @@ def decode_intel_hex(hexdata): """ return list of (address, data) """ datas = [] - # assume \n or \r\n* + # Assume LF-only or CR-LF style end-of-line. for line in hexdata.split(b"\n"): line = line.strip() if chr(line[0]) != ":": raise Exception("invalid line: %r" % line) @@ -182,15 +189,23 @@ def intel_hex_as_blob(hexdata): def maybe_intel_hex_as_blob(data): if data[0] == ord(":") and max(data) < 127: return intel_hex_as_blob(data) - return data # keep binary data + return data # Keep binary data. if __name__ == "__main__": - if len(sys.argv) != 2: - print("sigrok-fwextract-kingst-la2016 ") - sys.exit() - - res = qt_resources(sys.argv[1]) + parser = argparse.ArgumentParser(description = "KingstVIS firmware extraction") + parser.add_argument('executable', help = "KingstVIS executable file") + options = parser.parse_args() + exe_fn = options.executable + res = qt_resources(exe_fn) writer = res_writer(res) - writer.extract("fwfpga/LA2016A", "kingst-la2016a-fpga.bitstream") + + # Extract all MCU firmware and FPGA bitstream images. The sigrok + # project may not cover all KingstVIS supported devices. Users can + # either just copy those files which are strictly required for their + # specific device (diagnostics will identify those). Or just copy a + # few more files while some of them remain unused later (their size + # is small). Seeing which files would be contained is considered + # valuable, to identify device variants or candidate models. writer.extract_re(r"fwusb/fw(.*)", r"kingst-la-\1.fw", decoder=maybe_intel_hex_as_blob) + writer.extract_re(r"fwfpga/(.*)", r"kingst-\1-fpga.bitstream")