Tools: use crc_crc32() in app descriptor

This commit is contained in:
Andrew Tridgell 2019-10-21 17:11:33 +11:00
parent 92bd1a485a
commit c5afc3ff99
2 changed files with 49 additions and 44 deletions

View File

@ -13,7 +13,6 @@ import shutil
import sys
import re
import pickle
import zlib
import struct
_dynamic_env_data = {}
@ -126,9 +125,12 @@ class set_app_descriptor(Task.Task):
# to CRC32 of image after descriptor. This is very efficient
# for bootloader to calculate
# after CRC comes image length and 32 bit git hash
upload_tools = self.env.get_flat('UPLOAD_TOOLS')
sys.path.append(upload_tools)
from uploader import crc32
desc_len = 16
crc1 = to_unsigned(zlib.crc32(img[:offset]))
crc2 = to_unsigned(zlib.crc32(img[offset+desc_len:]))
crc1 = to_unsigned(crc32(bytearray(img[:offset])))
crc2 = to_unsigned(crc32(bytearray(img[offset+desc_len:])))
githash = to_unsigned(int('0x' + self.generator.bld.git_head_hash(short=True),16))
desc = struct.pack('<IIII', crc1, crc2, len(img), githash)
img = img[:offset] + desc + img[offset+desc_len:]

View File

@ -96,12 +96,8 @@ else:
compatible_IDs = {33: (9, 'AUAVX2.1')}
class firmware(object):
'''Loads a firmware file'''
desc = {}
image = bytes()
crctab = array.array('I', [
# CRC equivalent to crc_crc32() in AP_Math/crc.cpp
crctab = array.array('I', [
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
@ -134,6 +130,19 @@ class firmware(object):
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d])
def crc32(bytes, state=0):
'''crc32 exposed for use by chibios.py'''
for byte in bytes:
index = (state ^ byte) & 0xff
state = crctab[index] ^ (state >> 8)
return state
class firmware(object):
'''Loads a firmware file'''
desc = {}
image = bytes()
crcpad = bytearray(b'\xff\xff\xff\xff')
def __init__(self, path):
@ -152,16 +161,10 @@ class firmware(object):
def property(self, propname):
return self.desc[propname]
def __crc32(self, bytes, state):
for byte in bytes:
index = (state ^ byte) & 0xff
state = self.crctab[index] ^ (state >> 8)
return state
def crc(self, padlen):
state = self.__crc32(self.image, int(0))
state = crc32(self.image, int(0))
for i in range(len(self.image), (padlen - 1), 4):
state = self.__crc32(self.crcpad, state)
state = crc32(self.crcpad, state)
return state