-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashcrc_flash.py
39 lines (29 loc) · 1.15 KB
/
hashcrc_flash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def hashcrc_flash(begin=0x00, length=2**21, block_size=2**12, verbose=False):
'''
SHA256 Hash and CRC32 of the entirety, or from begin to begin+length, of QSPI Flash memory
assumes that utils.flash_read() behaves as if imported from Maix,
ie: `from Maix import utils`
'''
from hashlib import sha256
from binascii import hexlify, crc32
_hash = sha256()
checksum = 0
if verbose:
print('Hashing and CRCsumming %s bytes of flash at %s...' % (length, hex(begin)), end='')
bytes_read = 0
while bytes_read < length:
if bytes_read + block_size < length:
some_bytes = utils.flash_read(begin+bytes_read, block_size)
else:
some_bytes = utils.flash_read(begin+bytes_read, length-bytes_read)
_hash.update(some_bytes)
checksum = crc32(some_bytes, checksum)
bytes_read += len(some_bytes)
if verbose:
print('.', end='')
answer = _hash.digest(), checksum
if verbose:
print('\n%s bytes at %s:\n sha256: %s\n crc32: %s' % (
bytes_read, hex(begin), hexlify(answer[0]).decode(), answer[1]
))
return answer