# -*- 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("