Use new ChkSum class.
This commit is contained in:
parent
cbb602ea83
commit
155d0e023c
19
gcd.py
19
gcd.py
@ -7,6 +7,7 @@ GCD_SIG = b"GARMINd\00"
|
|||||||
|
|
||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
from struct import unpack
|
from struct import unpack
|
||||||
|
from grmn import ChkSum
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
FILE = sys.argv[1]
|
FILE = sys.argv[1]
|
||||||
@ -33,18 +34,12 @@ DEV_TYPES = {
|
|||||||
3196: "D2 Delta",
|
3196: "D2 Delta",
|
||||||
}
|
}
|
||||||
|
|
||||||
running_cksum = 0
|
cksum = ChkSum()
|
||||||
all_cksum_ok = True
|
all_cksum_ok = True
|
||||||
last_type6_fids = []
|
last_type6_fids = []
|
||||||
last_type6_format = ""
|
last_type6_format = ""
|
||||||
last_type6_fields = []
|
last_type6_fields = []
|
||||||
|
|
||||||
def add_to_cksum(payload):
|
|
||||||
global running_cksum
|
|
||||||
for c in payload:
|
|
||||||
running_cksum += c
|
|
||||||
running_cksum &= 0xff
|
|
||||||
|
|
||||||
def get_tlv_comment(ttype):
|
def get_tlv_comment(ttype):
|
||||||
if ttype in TLV_TYPES:
|
if ttype in TLV_TYPES:
|
||||||
return TLV_TYPES[ttype]
|
return TLV_TYPES[ttype]
|
||||||
@ -64,11 +59,11 @@ def parseTLVheader(hdr):
|
|||||||
return (ttype, tlen)
|
return (ttype, tlen)
|
||||||
|
|
||||||
def parseTLV1(payload):
|
def parseTLV1(payload):
|
||||||
global running_cksum, all_cksum_ok
|
global cksum, all_cksum_ok
|
||||||
if len(payload) != 1:
|
if len(payload) != 1:
|
||||||
print(" ! Checksum has invalid length!")
|
print(" ! Checksum has invalid length!")
|
||||||
all_cksum_ok = False
|
all_cksum_ok = False
|
||||||
expected = ( 0x100 - running_cksum ) & 0xFF
|
expected = cksum.get()
|
||||||
payload = unpack("B", payload)[0]
|
payload = unpack("B", payload)[0]
|
||||||
state = "INVALID!"
|
state = "INVALID!"
|
||||||
if expected == payload:
|
if expected == payload:
|
||||||
@ -138,7 +133,7 @@ def parseTLV7(payload):
|
|||||||
|
|
||||||
with open(FILE, "rb") as f:
|
with open(FILE, "rb") as f:
|
||||||
sig = f.read(8)
|
sig = f.read(8)
|
||||||
add_to_cksum(sig)
|
cksum.add(sig)
|
||||||
if sig == GCD_SIG:
|
if sig == GCD_SIG:
|
||||||
print("Signature ok.")
|
print("Signature ok.")
|
||||||
else:
|
else:
|
||||||
@ -150,7 +145,7 @@ with open(FILE, "rb") as f:
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
hdr = f.read(4)
|
hdr = f.read(4)
|
||||||
add_to_cksum(hdr)
|
cksum.add(hdr)
|
||||||
(ttype, tlen) = parseTLVheader(hdr)
|
(ttype, tlen) = parseTLVheader(hdr)
|
||||||
print("#{:04} TLV type {:04x} (offset 0x{:x}, length {} Bytes) - {}".format(i, ttype, f.tell(), tlen, get_tlv_comment(ttype)))
|
print("#{:04} TLV type {:04x} (offset 0x{:x}, length {} Bytes) - {}".format(i, ttype, f.tell(), tlen, get_tlv_comment(ttype)))
|
||||||
if ttype == 0xFFFF:
|
if ttype == 0xFFFF:
|
||||||
@ -176,7 +171,7 @@ with open(FILE, "rb") as f:
|
|||||||
#print(" > " + " ".join("{:02x}".format(c) for c in payloadshort))
|
#print(" > " + " ".join("{:02x}".format(c) for c in payloadshort))
|
||||||
#print(hexlify(payload).decode("utf-8"))
|
#print(hexlify(payload).decode("utf-8"))
|
||||||
#print(" > " + repr(payloadshort))
|
#print(" > " + repr(payloadshort))
|
||||||
add_to_cksum(payload)
|
cksum.add(payload)
|
||||||
if ttype in [0x0008, 0x0401, 0x0505, 0x0555, 0x0557, 0x02bd]:
|
if ttype in [0x0008, 0x0401, 0x0505, 0x0555, 0x0557, 0x02bd]:
|
||||||
outname = "{}_{:04x}.bin".format(FILE, ttype)
|
outname = "{}_{:04x}.bin".format(FILE, ttype)
|
||||||
if ttype != cur_ttype:
|
if ttype != cur_ttype:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user