diff --git a/grmn/gcd.py b/grmn/gcd.py index 5264512..82ee9de 100644 --- a/grmn/gcd.py +++ b/grmn/gcd.py @@ -2,7 +2,7 @@ # Thanks to TurboCCC and kunix for all your work! from .chksum import ChkSum -from .tlv import TLV, TLV6, TLV7 +from .tlv import TLV, TLV6, TLV7, TLVbinary from struct import unpack import configparser @@ -166,13 +166,39 @@ class Gcd: if s == "GCD_DUMP": continue print("Parsing {}".format(s)) + params = [] + for k in rcp[s]: + params.append((k, rcp[s][k])) if "from_file" in rcp[s]: - # BINARY! Must create type 0006, 0007 and actual binary blocks + # BINARY! Must create type 0006, 0007 and actual binary block(s) + tlv6 = TLV6(0x0006, None) + tlv6.load_dump(params) + gcd.struct.append(tlv6) + + tlv7 = TLV7(0x0007, None) + tlv7.set_tlv6(tlv6) + tlv7.load_dump(params) + gcd.struct.append(tlv7) + + tlv7.parse() + filename = rcp[s]["from_file"] + file_type_id = tlv7.binary_type_id + + with open(filename, "rb") as bf: + while True: + read_bytes = bf.read(0xff00) + btlv = TLVbinary(file_type_id, len(read_bytes)) + btlv.value = read_bytes + gcd.struct.append(btlv) + if len(read_bytes) < 0xff00: + break + bf.close() + print("Binary block") else: - params = [] - for k in rcp[s]: - params.append((k, rcp[s][k])) tlv = TLV.create_from_dump(params) gcd.struct.append(tlv) return gcd + + def save(self, filename): + self.filename = filename diff --git a/grmn/tlv.py b/grmn/tlv.py index f0d14cc..6bf77bf 100644 --- a/grmn/tlv.py +++ b/grmn/tlv.py @@ -180,20 +180,28 @@ class TLV6(TLV): 0x5003: ["", "End of definition marker"], } - def parse(self): - if len(self.value) % 2 != 0: - raise Exception("Invalid TLV6 payload length!") - + def __init__(self, type_id: int, expected_length: int, value=None, offset: int=None): + super().__init__(type_id, expected_length, value, offset) self.fids = [] self.format = "" self.fields = [] + def add_fid(self, fid: int): + fdef = self.FIELD_TYPES[fid] + self.fids.append(fid) + self.format += fdef[0] + self.fields.append(fdef[1]) + + def parse(self): + if self.is_parsed: + # already parsed + return + if len(self.value) % 2 != 0: + raise Exception("Invalid TLV6 payload length!") + for i in range(0, len(self.value), 2): - fid = unpack("H", self.value[i:i+2])[0] - fdef = self.FIELD_TYPES[fid] - self.fids.append(fid) - self.format += fdef[0] - self.fields.append(fdef[1]) + fid = unpack("