#!/usr/bin/env python3 """Verify cmtd+0x08 checksums: prove_checksum.py a.bin [b.bin ...] Exit 0 iff every image's stored checksum equals compute_checksum(image). See lofi_image.compute_checksum for the algorithm. """ import os import struct import sys sys.path.insert(0, os.path.dirname(__file__)) from lofi_image import compute_checksum, parse_image def main() -> int: ok = True for path in sys.argv[1:]: d = open(path, "rb").read() info = parse_image(d) # validates container chain stored = info["cmtd"]["checksum"] calc = compute_checksum(d) match = stored == calc ok &= match fw = ".".join(map(str, info["mmtd"]["fw"])) print("%s fw=%s size=%d stored=%08X calc=%08X %s" % (path, fw, len(d), stored, calc, "MATCH" if match else "MISMATCH")) return 0 if ok and len(sys.argv) > 1 else 1 if __name__ == "__main__": sys.exit(main())