#! /usr/bin/python3 # SPDX-License-Identifier: MIT # Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. # # Check firmware version strings in binaries against linux-firmware repo import glob import hashlib import os import re import struct import sys import subprocess from check import load_config # Print warnings about ignored hash mismatches only when V is set non-empty. VERBOSE = bool(os.environ.get('V')) def segment_hashes(data): """Return the per-segment SHA-256 digests of a Hexagon ELF, or None. The signed DSP firmware authorises each Hexagon binary by storing, in program-header order, the SHA-256 digest of every ELF segment (computed over its on-disk bytes, p_offset .. p_offset + p_filesz). """ # little-endian 32-bit ELF only (EI_CLASS, EI_DATA) if data[:4] != b'\x7fELF' or data[4] != 1 or data[5] != 1: return None e_phoff = struct.unpack_from('" % sys.argv[0]) return 1 fwpath = sys.argv[1] qcompath = os.path.join(fwpath, "qcom") fws = load_fws(qcompath) okay = True for data in load_config(): if data[0] != "install": continue soc = data[2].split('/')[0] path = os.path.join(data[2], data[3]) socpath = os.path.join(soc, data[3]) version = data[4].removeprefix("%s-" % data[3]) bindir = os.path.join(data[2], data[4]) mismatch = None if socpath in fws: if fws[socpath]['version'] == version: fws[socpath]['seen'] = True #print("seen %s" % socpath) fwfile = os.path.join(qcompath, fws[socpath]['filename']) okay = check_hashes(fwfile, bindir) and okay continue mismatch = f"version mismatch %s %s vs %s %s" % (path, version, fws[socpath]['filename'], fws[socpath]['version']) if path in fws: if fws[path]['version'] == version: fws[path]['seen'] = True #print("seen %s" % path) fwfile = os.path.join(qcompath, fws[path]['filename']) okay = check_hashes(fwfile, bindir) and okay continue mismatch = f"version mismatch %s %s vs %s %s" % (path, version, fws[path]['filename'], fws[path]['version']) if mismatch: print(mismatch) else: print("no match for DSP binaries", path, "version", version) for data in fws.values(): if not data['seen']: print("no match for DSP firmware", data["filename"], "version", data["version"]) return 0 if okay else 1 if __name__ == "__main__": sys.exit(main())