# -*- coding: utf-8 -*- # Thanks to TurboCCC and kunix for all your work! from . import chksum from . import devices from binascii import hexlify from struct import pack, unpack import sys GCD_SIG = b"G\x41RM\x49Nd\00" DEFAULT_COPYRIGHT = b"Copyright 1996-2017 by G\x61rm\x69n Ltd. or its subsidiaries." DEFAULT_FIRST_PADDING = 21 DEFAULT_ALIGN = 0x1000 # second padding block pads until 0x1000 TLV_TYPES = { 0x0001: "Checksum rectifier", 0x0002: "Padding", 0x0003: "Part number?", 0x0005: "Copyright notice", 0x0006: "Block Type 7 format definition", 0x0007: "Binary descriptor", 0x0008: "Binary Region 0C (boot.bin)", 0x0401: "Binary Component Firmware (SensorHub, ANT_BLE_BT, GPS, WiFi)", 0x0505: "Binary Region 05", 0x0555: "Binary Region 55", 0x0557: "Binary Region 57", 0x02bd: "Binary Region 0E (fw_all.bin)", 0xffff: "EOF marker", } # Typical structure: # first 0x1000 Bytes: GCD_SIG > 0x0001 > 0x0002 > 0x0003 > 0x0005 > 0x0001 > 0x0002 # then: 0x0001 > ( 0x0006 > 0x0007 > 0x???? > 0x0001 ... ) > 0xffff class TLV: def __init__(self, type_id: int, expected_length: int, value=None, offset: int=None): self.type_id = type_id self.offset = offset self.comment = TLV_TYPES.get(type_id, "Type {:04x} / {:d}".format(type_id, type_id)) self.length = expected_length self.value = None self.is_parsed = False if value is not None: self.value = bytes(value) @staticmethod def factory(header: bytes, offset: int = None): (type_id, length) = unpack("20}: 0x{:04x} / {:d} ({})".format(fdesc, v, v, devices.DEVICES.get(v, "Unknown device"))) elif fid == 0x2015: print(" - {:>20}: {} Bytes".format(fdesc, v)) else: print(" - {:>20}: 0x{:04x} / {:d}".format(fdesc, v, v)) class Gcd: def __init__(self, filename: str=None): self.filename = filename self.struct = [] if filename is not None: self.load() def load(self): if self.filename is None: return False last_tlv6 = None with open(self.filename, "rb") as f: sig = f.read(8) if sig != GCD_SIG: raise Exception("Signature mismatch ({}, should be {})!".format(repr(sig), repr(GCD_SIG))) while True: cur_offset = f.tell() header = f.read(4) tlv = TLV.factory(header, offset=cur_offset) self.struct.append(tlv) if tlv.type_id == 0xFFFF: # End of file reached break tlength = tlv.get_length() payload = f.read(tlength) tlv.set_value(payload) if tlv.type_id == 0x0006: last_tlv6 = tlv elif tlv.type_id == 0x0007: tlv.set_tlv6(last_tlv6) f.close()