Rewrote checksum calculation into class.
This commit is contained in:
parent
80c0317da9
commit
43dfdbe8b2
42
gcksum.py
42
gcksum.py
@ -1,39 +1,31 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from binascii import hexlify
|
from grmn import ChkSum
|
||||||
from struct import unpack
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
FILE = sys.argv[1]
|
FILE = sys.argv[1]
|
||||||
|
BLOCKSIZE = 4 * 1024
|
||||||
|
|
||||||
print("Opening {}".format(FILE))
|
print("Reading {} ".format(FILE), end="")
|
||||||
|
|
||||||
csum_pre = 0
|
csum = ChkSum()
|
||||||
csum = 0
|
|
||||||
last_byte = 0xff
|
|
||||||
|
|
||||||
with open(FILE, "rb") as f:
|
with open(FILE, "rb") as f:
|
||||||
while True:
|
while True:
|
||||||
block = f.read(1024)
|
block = f.read(BLOCKSIZE)
|
||||||
for c in block:
|
csum.add(block)
|
||||||
csum_pre = csum
|
print(".", end="", flush=True)
|
||||||
csum += c
|
if len(block) < BLOCKSIZE:
|
||||||
csum &= 0xff
|
print(" done.")
|
||||||
last_byte = c
|
|
||||||
if len(block) < 1024:
|
|
||||||
print("End reached.")
|
|
||||||
break
|
break
|
||||||
|
f.close()
|
||||||
|
|
||||||
print("Sum of all bytes: {:02x}".format(csum))
|
print("Sum of all bytes: {:02x}".format(csum.get_sum()))
|
||||||
if csum == 0:
|
print("Last byte: {:02x}".format(csum.get_last_byte()))
|
||||||
print("CHECKSUM VALID.")
|
if csum.is_valid():
|
||||||
|
print("☑ CHECKSUM VALID.")
|
||||||
else:
|
else:
|
||||||
print("CHECKSUM INVALID!!! (Or GCD or other type.)")
|
print("☒ CHECKSUM INVALID!!! (Or GCD or other type.)")
|
||||||
|
expected_cksum = csum.get_expected()
|
||||||
#print("Sum without last: {:02x}".format(csum_pre))
|
print("Expected last byte: {:02x}".format(expected_cksum))
|
||||||
|
|
||||||
expected_cksum = ( 0x100 - csum_pre ) & 0xff
|
|
||||||
|
|
||||||
print("Expected last byte: {:02x}".format(expected_cksum))
|
|
||||||
print("Actual last byte: {:02x}".format(last_byte))
|
|
||||||
|
4
grmn/__init__.py
Normal file
4
grmn/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
"""Library for firmware reading/writing."""
|
||||||
|
|
||||||
|
#from .gcd import *
|
||||||
|
from .chksum import *
|
34
grmn/chksum.py
Normal file
34
grmn/chksum.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
class ChkSum:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.chksum = 0
|
||||||
|
self.chksum_without_last = 0
|
||||||
|
self.last_byte = 0xff
|
||||||
|
|
||||||
|
def add(self, data):
|
||||||
|
for c in data[:-1]:
|
||||||
|
self.chksum += c
|
||||||
|
self.chksum_without_last = self.chksum
|
||||||
|
self.chksum_without_last &= 0xff
|
||||||
|
self.last_byte = data[-1]
|
||||||
|
self.chksum += self.last_byte
|
||||||
|
self.chksum &= 0xff
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
remainder = ( 0x100 - self.chksum ) & 0xff
|
||||||
|
return remainder
|
||||||
|
|
||||||
|
def get_sum(self):
|
||||||
|
return self.chksum
|
||||||
|
|
||||||
|
def is_valid(self):
|
||||||
|
return (self.chksum == 0)
|
||||||
|
|
||||||
|
def get_expected(self):
|
||||||
|
expected = ( 0x100 - self.chksum_without_last ) & 0xff
|
||||||
|
return expected
|
||||||
|
|
||||||
|
def get_last_byte(self):
|
||||||
|
return self.last_byte
|
Loading…
x
Reference in New Issue
Block a user