Colours!
This commit is contained in:
parent
e4fc81e7be
commit
5ca2f8cad5
19
grmn/gcd.py
19
grmn/gcd.py
@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Thanks to TurboCCC and kunix for all your work!
|
||||
|
||||
from .ansi import RED, GREEN, RESET
|
||||
from .chksum import ChkSum
|
||||
from .tlv import TLV, TLV6, TLV7, TLVbinary
|
||||
from struct import unpack
|
||||
@ -35,10 +36,14 @@ class Gcd:
|
||||
with open(self.filename, "rb") as f:
|
||||
sig = f.read(8)
|
||||
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:
|
||||
cur_offset = f.tell()
|
||||
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)
|
||||
tlv = TLV.factory(type_id, length, offset=cur_offset)
|
||||
self.add_tlv(tlv)
|
||||
@ -103,17 +108,17 @@ class Gcd:
|
||||
file_cs = chksum.get_last_byte()
|
||||
if print_stats:
|
||||
if expected_cs == file_cs:
|
||||
state = "OK"
|
||||
state = GREEN + "OK" + RESET
|
||||
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))
|
||||
if expected_cs != file_cs:
|
||||
all_ok = False
|
||||
if print_stats:
|
||||
if all_ok:
|
||||
print("☑ ALL CHECKSUMS VALID.")
|
||||
print(GREEN + "☑ ALL CHECKSUMS VALID." + RESET)
|
||||
else:
|
||||
print("☒ ONE OR MORE CHECKSUMS INVALID!")
|
||||
print(RED + "☒ ONE OR MORE CHECKSUMS INVALID!" + RESET)
|
||||
return all_ok
|
||||
|
||||
def fix_checksums(self):
|
||||
@ -182,9 +187,9 @@ class Gcd:
|
||||
rcp = configparser.ConfigParser()
|
||||
rcp.read(recipe_file)
|
||||
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":
|
||||
raise ParseException("Recipe file wrong version.")
|
||||
raise ParseException(RED + "Recipe file wrong version." + RESET)
|
||||
for s in rcp.sections():
|
||||
if s == "GCD_DUMP":
|
||||
continue
|
||||
|
@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import devices
|
||||
from .ansi import RESET, RED
|
||||
from binascii import hexlify, unhexlify
|
||||
from struct import pack, unpack
|
||||
import sys
|
||||
@ -190,7 +191,7 @@ class TLV5(TLV):
|
||||
elif k == "length":
|
||||
self.length = int(v)
|
||||
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)
|
||||
|
||||
class TLV6(TLV):
|
||||
@ -245,7 +246,7 @@ class TLV6(TLV):
|
||||
# already parsed
|
||||
return
|
||||
if len(self.value) % 2 != 0:
|
||||
raise Exception("Invalid TLV6 payload length!")
|
||||
raise Exception(RED + "Invalid TLV6 payload length!" + RESET)
|
||||
|
||||
self.fids = []
|
||||
self.format = ""
|
||||
@ -319,7 +320,7 @@ class TLV7(TLV):
|
||||
fdesc = self.tlv6.fields[i]
|
||||
(fid, v) = pair
|
||||
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:
|
||||
txt += "\n - Field {:d} ({:04x}): {:>20}: {} Bytes".format(i+1, fid, fdesc, v)
|
||||
elif fid == 0x4007:
|
||||
@ -401,7 +402,7 @@ class TLVbinary0401(TLVbinary):
|
||||
sku = self.value[10:20].decode("utf-8")
|
||||
hwid = int(sku[4:8])
|
||||
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)
|
||||
else:
|
||||
txt += "\n - Unknown header format (0x{:04x} / 0x{:04x})".format(hdr1, hdr2)
|
||||
|
Loading…
x
Reference in New Issue
Block a user