This commit is contained in:
2019-06-28 20:33:53 +02:00
parent e4fc81e7be
commit 5ca2f8cad5
2 changed files with 17 additions and 11 deletions
+12 -7
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Thanks to TurboCCC and kunix for all your work! # Thanks to TurboCCC and kunix for all your work!
from .ansi import RED, GREEN, RESET
from .chksum import ChkSum from .chksum import ChkSum
from .tlv import TLV, TLV6, TLV7, TLVbinary from .tlv import TLV, TLV6, TLV7, TLVbinary
from struct import unpack from struct import unpack
@@ -35,10 +36,14 @@ class Gcd:
with open(self.filename, "rb") as f: with open(self.filename, "rb") as f:
sig = f.read(8) sig = f.read(8)
if sig != GCD_SIG: if sig != GCD_SIG:
raise ParseException("Signature mismatch ({}, should be {})!".format(repr(sig), repr(GCD_SIG))) raise ParseException(RED + "Signature mismatch ({}, should be {})!".format(repr(sig) + RESET, repr(GCD_SIG)))
while True: while True:
cur_offset = f.tell() cur_offset = f.tell()
header = f.read(4) header = f.read(4)
if len(header) < 4:
#raise ParseException("File truncated. End marker not reached yet.")
print(RED + "WARNING: File truncated. End marker not reached yet. (pos={})".format(f.tell()) + RESET, file=sys.stderr)
break
(type_id, length) = unpack("<HH", header) (type_id, length) = unpack("<HH", header)
tlv = TLV.factory(type_id, length, offset=cur_offset) tlv = TLV.factory(type_id, length, offset=cur_offset)
self.add_tlv(tlv) self.add_tlv(tlv)
@@ -103,17 +108,17 @@ class Gcd:
file_cs = chksum.get_last_byte() file_cs = chksum.get_last_byte()
if print_stats: if print_stats:
if expected_cs == file_cs: if expected_cs == file_cs:
state = "OK" state = GREEN + "OK" + RESET
else: else:
state = "INVALID" state = RED + "INVALID" + RESET
print("TLV{:04x} at 0x{:x}: {:02x} (expected: {:02x}) = {}".format(tlv.type_id, tlv.offset, file_cs, expected_cs, state)) print("TLV{:04x} at 0x{:x}: {:02x} (expected: {:02x}) = {}".format(tlv.type_id, tlv.offset, file_cs, expected_cs, state))
if expected_cs != file_cs: if expected_cs != file_cs:
all_ok = False all_ok = False
if print_stats: if print_stats:
if all_ok: if all_ok:
print("☑ ALL CHECKSUMS VALID.") print(GREEN + "☑ ALL CHECKSUMS VALID." + RESET)
else: else:
print("☒ ONE OR MORE CHECKSUMS INVALID!") print(RED + "☒ ONE OR MORE CHECKSUMS INVALID!" + RESET)
return all_ok return all_ok
def fix_checksums(self): def fix_checksums(self):
@@ -182,9 +187,9 @@ class Gcd:
rcp = configparser.ConfigParser() rcp = configparser.ConfigParser()
rcp.read(recipe_file) rcp.read(recipe_file)
if rcp["GCD_DUMP"]["dump_by"] != "grmn-gcd": if rcp["GCD_DUMP"]["dump_by"] != "grmn-gcd":
raise ParseException("Recipe file invalid.") raise ParseException(RED + "Recipe file invalid." + RESET)
if rcp["GCD_DUMP"]["dump_ver"] != "1": if rcp["GCD_DUMP"]["dump_ver"] != "1":
raise ParseException("Recipe file wrong version.") raise ParseException(RED + "Recipe file wrong version." + RESET)
for s in rcp.sections(): for s in rcp.sections():
if s == "GCD_DUMP": if s == "GCD_DUMP":
continue continue
+5 -4
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from . import devices from . import devices
from .ansi import RESET, RED
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from struct import pack, unpack from struct import pack, unpack
import sys import sys
@@ -190,7 +191,7 @@ class TLV5(TLV):
elif k == "length": elif k == "length":
self.length = int(v) self.length = int(v)
if len(self.value) != self.length: if len(self.value) != self.length:
print("WARNING: Imported copyright text doesn't match supposed length.") print(RED + "WARNING: Imported copyright text doesn't match supposed length." + RESET, file=sys.stderr)
self.length = len(self.value) self.length = len(self.value)
class TLV6(TLV): class TLV6(TLV):
@@ -245,7 +246,7 @@ class TLV6(TLV):
# already parsed # already parsed
return return
if len(self.value) % 2 != 0: if len(self.value) % 2 != 0:
raise Exception("Invalid TLV6 payload length!") raise Exception(RED + "Invalid TLV6 payload length!" + RESET)
self.fids = [] self.fids = []
self.format = "" self.format = ""
@@ -319,7 +320,7 @@ class TLV7(TLV):
fdesc = self.tlv6.fields[i] fdesc = self.tlv6.fields[i]
(fid, v) = pair (fid, v) = pair
if fid == 0x1009: if fid == 0x1009:
txt += "\n - Field {:d} ({:04x}): {:>20}: 0x{:04x} / {:d} ({})".format(i+1, fid, fdesc, v, v, devices.DEVICES.get(v, "Unknown device")) txt += "\n - Field {:d} ({:04x}): {:>20}: 0x{:04x} / {:d} ({})".format(i+1, fid, fdesc, v, v, devices.DEVICES.get(v, RED + "Unknown device" + RESET))
elif fid == 0x2015: elif fid == 0x2015:
txt += "\n - Field {:d} ({:04x}): {:>20}: {} Bytes".format(i+1, fid, fdesc, v) txt += "\n - Field {:d} ({:04x}): {:>20}: {} Bytes".format(i+1, fid, fdesc, v)
elif fid == 0x4007: elif fid == 0x4007:
@@ -401,7 +402,7 @@ class TLVbinary0401(TLVbinary):
sku = self.value[10:20].decode("utf-8") sku = self.value[10:20].decode("utf-8")
hwid = int(sku[4:8]) hwid = int(sku[4:8])
txt += "\n - SKU: {}-{}-{}".format(sku[0:3], sku[3:8], sku[8:10]) txt += "\n - SKU: {}-{}-{}".format(sku[0:3], sku[3:8], sku[8:10])
txt += "\n - hw_id: 0x{:04x} / {:d} ({})".format(hwid, hwid, devices.DEVICES.get(hwid, "Unknown device")) txt += "\n - hw_id: 0x{:04x} / {:d} ({})".format(hwid, hwid, devices.DEVICES.get(hwid, RED + "Unknown device" + RESET))
txt += "\n - Version: 0x{:04x} / {:d}".format(version, version) txt += "\n - Version: 0x{:04x} / {:d}".format(version, version)
else: else:
txt += "\n - Unknown header format (0x{:04x} / 0x{:04x})".format(hdr1, hdr2) txt += "\n - Unknown header format (0x{:04x} / 0x{:04x})".format(hdr1, hdr2)