Add checksum validation to Gcd class.

This commit is contained in:
Markus Birth 2018-10-18 00:57:45 +02:00
parent 4aa3e39d7d
commit c0a1d833ba
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
2 changed files with 31 additions and 0 deletions

View File

@ -15,3 +15,4 @@ print("Opening {}".format(FILE))
gcd = Gcd(FILE)
gcd.print_struct()
gcd.validate(True)

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Thanks to TurboCCC and kunix for all your work!
from .chksum import ChkSum
from .tlv import TLV, TLV6, TLV7
GCD_SIG = b"G\x41RM\x49Nd\00"
@ -68,3 +69,32 @@ class Gcd:
tlv_count += 1
tlv_length += tlv.length
last_tlv = tlv.type_id
def validate(self, print_stats: bool=False):
"""
Checks and verifies all checksums in the GCD.
"""
chksum = ChkSum()
chksum.add(GCD_SIG)
all_ok = True
if print_stats:
print("\nChecksum validation:")
for tlv in self.struct:
chksum.add(tlv.get())
if tlv.type_id == 0x0001:
expected_cs = chksum.get_expected()
file_cs = chksum.get_last_byte()
if print_stats:
if expected_cs == file_cs:
state = "OK"
else:
state = "INVALID"
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.")
else:
print("☒ ONE OR MORE CHECKSUMS INVALID!")
return all_ok