update: Version-check the UPD catalogue entries

This commit is contained in:
Keir Fraser
2020-06-08 14:00:39 +01:00
parent 3d89501538
commit 5faead7ada
2 changed files with 27 additions and 20 deletions

View File

@@ -36,7 +36,7 @@ def update_firmware(usb, args):
with open(filename, "rb") as f:
dat = f.read()
if struct.unpack('4s', dat[:4])[0] != b'GWUP':
print('%s: Not an UPD file' % (filename))
print('%s: Not a valid UPD file' % (filename))
return
crc32 = crcmod.predefined.Crc('crc-32-mpeg')
crc32.update(dat)
@@ -48,8 +48,9 @@ def update_firmware(usb, args):
# Search the catalogue for a match on our Weazle's hardware type.
while dat:
upd_len, hw_model = struct.unpack("<2H", dat[:4])
upd_type, = struct.unpack("2s", dat[upd_len-4:upd_len-2])
if hw_model == usb.hw_model and upd_type == req_type:
upd_type, major, minor = struct.unpack("2s2B", dat[upd_len-4:upd_len])
if ((hw_model, upd_type, major, minor)
== (usb.hw_model, req_type, version.major, version.minor)):
# Match: Pull out the embedded update file.
dat = dat[4:upd_len+4]
break
@@ -57,7 +58,10 @@ def update_firmware(usb, args):
dat = dat[upd_len+4:]
if not dat:
print("%s: No match for F%u hardware" % (filename, usb.hw_model))
print("%s: F%u v%u.%u %s update not found"
% (filename, usb.hw_model,
version.major, version.minor,
'bootloader' if args.bootloader else 'firmware'))
return
# Check the matching update file's footer.

View File

@@ -64,26 +64,29 @@ def cat_upd(argv):
assert crc32.crcValue == 0
dat += d[4:-4]
return dat
def _verify_upd(d):
assert struct.unpack('4s', d[:4])[0] == b'GWUP'
crc32 = crcmod.predefined.Crc('crc-32-mpeg')
crc32.update(d)
assert crc32.crcValue == 0
d = d[4:-4]
while d:
upd_len, hw_model = struct.unpack("<2H", d[:4])
upd_type, major, minor = struct.unpack("2s2B", d[upd_len-4:upd_len])
crc16 = crcmod.predefined.Crc('crc-ccitt-false')
crc16.update(d[4:upd_len+4])
assert crc16.crcValue == 0
print('F%u %s v%u.%u' % (hw_model,
{b'BL': 'Boot', b'GW': 'Main'}[upd_type],
major, minor))
d = d[upd_len+4:]
def verify_upd(argv):
dat = b'GWUP'
for fname in argv:
with open(fname, "rb") as f:
d = f.read()
assert struct.unpack('4s', d[:4])[0] == b'GWUP'
crc32 = crcmod.predefined.Crc('crc-32-mpeg')
crc32.update(d)
assert crc32.crcValue == 0
d = d[4:-4]
while d:
upd_len, hw_model = struct.unpack("<2H", d[:4])
upd_type, = struct.unpack("2s", d[upd_len-4:upd_len-2])
crc16 = crcmod.predefined.Crc('crc-ccitt-false')
crc16.update(d[4:upd_len+4])
assert crc16.crcValue == 0
print('F%u %s' % (hw_model, {b'BL': 'Boot',
b'GW': 'Main'}[upd_type]))
d = d[upd_len+4:]
_verify_upd(d)
def main(argv):
if argv[1] == 'new':