| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import glob |
| 8 |
import hashlib |
| 9 |
import os |
| 10 |
import re |
| 11 |
import struct |
| 12 |
import sys |
| 13 |
import subprocess |
| 14 |
|
| 15 |
from check import load_config |
| 16 |
|
| 17 |
|
| 18 |
VERBOSE = bool(os.environ.get('V')) |
| 19 |
|
| 20 |
def segment_hashes(data): |
| 21 |
"""Return the per-segment SHA-256 digests of a Hexagon ELF, or None. |
| 22 |
|
| 23 |
The signed DSP firmware authorises each Hexagon binary by storing, in |
| 24 |
program-header order, the SHA-256 digest of every ELF segment (computed |
| 25 |
over its on-disk bytes, p_offset .. p_offset + p_filesz). |
| 26 |
""" |
| 27 |
|
| 28 |
if data[:4] != b'\x7fELF' or data[4] != 1 or data[5] != 1: |
| 29 |
return None |
| 30 |
|
| 31 |
e_phoff = struct.unpack_from('<I', data, 28)[0] |
| 32 |
e_phentsize = struct.unpack_from('<H', data, 42)[0] |
| 33 |
e_phnum = struct.unpack_from('<H', data, 44)[0] |
| 34 |
|
| 35 |
hashes = [] |
| 36 |
for i in range(e_phnum): |
| 37 |
base = e_phoff + i * e_phentsize |
| 38 |
p_offset = struct.unpack_from('<I', data, base + 4)[0] |
| 39 |
p_filesz = struct.unpack_from('<I', data, base + 16)[0] |
| 40 |
hashes.append(hashlib.sha256(data[p_offset:p_offset + p_filesz]).digest()) |
| 41 |
|
| 42 |
return hashes |
| 43 |
|
| 44 |
def load_hashes_ignore(bindir): |
| 45 |
"""Return the set of filenames whose hash mismatch should be ignored. |
| 46 |
|
| 47 |
A binary version directory may carry a 'hashes-ignore.txt' listing files |
| 48 |
(one per line, '#' comments allowed) that are known not to match the |
| 49 |
firmware, e.g. when Qualcomm shipped rebuilt binaries under an unchanged |
| 50 |
QC_IMAGE_VERSION_STRING. |
| 51 |
""" |
| 52 |
ignore = set() |
| 53 |
path = os.path.join(bindir, "hashes-ignore.txt") |
| 54 |
if os.path.isfile(path): |
| 55 |
with open(path, encoding="utf-8") as f: |
| 56 |
for line in f: |
| 57 |
name = line.split('#', 1)[0].strip() |
| 58 |
if name: |
| 59 |
ignore.add(name) |
| 60 |
return ignore |
| 61 |
|
| 62 |
def check_hashes(fwfile, bindir): |
| 63 |
"""Verify every Hexagon binary in bindir is authorised by fwfile. |
| 64 |
|
| 65 |
The firmware stores each binary's segment digests contiguously, so a binary |
| 66 |
is authorised iff the concatenation of its digests appears in the image. |
| 67 |
Files listed in the directory's hashes-ignore.txt are exempt. Returns True |
| 68 |
if all non-ignored binaries are present, printing the ones that are not. |
| 69 |
""" |
| 70 |
okay = True |
| 71 |
|
| 72 |
if not os.path.isdir(bindir): |
| 73 |
return True |
| 74 |
|
| 75 |
ignore = load_hashes_ignore(bindir) |
| 76 |
|
| 77 |
with open(fwfile, 'rb') as f: |
| 78 |
blob = f.read() |
| 79 |
|
| 80 |
for file in sorted(os.listdir(bindir)): |
| 81 |
path = os.path.join(bindir, file) |
| 82 |
if not os.path.isfile(path): |
| 83 |
continue |
| 84 |
|
| 85 |
with open(path, 'rb') as f: |
| 86 |
hashes = segment_hashes(f.read()) |
| 87 |
if hashes is None: |
| 88 |
continue |
| 89 |
|
| 90 |
if blob.find(b''.join(hashes)) < 0: |
| 91 |
if file in ignore: |
| 92 |
if VERBOSE: |
| 93 |
print("warning: ignoring hash mismatch %s (listed in hashes-ignore.txt)" % path) |
| 94 |
else: |
| 95 |
print("hash mismatch %s not authorised by %s" % (path, fwfile)) |
| 96 |
okay = False |
| 97 |
|
| 98 |
return okay |
| 99 |
|
| 100 |
def get_ver(file): |
| 101 |
pattern_ver = re.compile("^QC_IMAGE_VERSION_STRING=(.*)$") |
| 102 |
result = subprocess.run(['strings', file], stdout=subprocess.PIPE) |
| 103 |
|
| 104 |
for line in result.stdout.decode('utf-8').split('\n'): |
| 105 |
match = pattern_ver.match(line) |
| 106 |
if match: |
| 107 |
return match.group(1) |
| 108 |
|
| 109 |
return None |
| 110 |
|
| 111 |
|
| 112 |
def load_fws(qcompath): |
| 113 |
result = {} |
| 114 |
prefix = os.path.join(qcompath, "") |
| 115 |
pattern_dsp = re.compile("(qc)?((.*dsp[0-9]?|slpi)([0-9][0-9[0-9]][0-9])?).mbn$") |
| 116 |
|
| 117 |
for subdir in os.walk(qcompath): |
| 118 |
for file in subdir[2]: |
| 119 |
if 'dtb' in file: |
| 120 |
continue |
| 121 |
|
| 122 |
match = pattern_dsp.match(file) |
| 123 |
if not match: |
| 124 |
continue |
| 125 |
|
| 126 |
subpath = subdir[0].removeprefix(prefix) |
| 127 |
dsp = match.group(2) |
| 128 |
if dsp == "slpi": |
| 129 |
dsp = "sdsp" |
| 130 |
elif dsp == "cdsp0": |
| 131 |
dsp = "cdsp" |
| 132 |
elif dsp.startswith("gp"): |
| 133 |
dsp = 'g' + dsp[2:] |
| 134 |
name = os.path.join(subpath, dsp) |
| 135 |
version = get_ver(os.path.join(subdir[0], file)) |
| 136 |
result[name] = {'version': version, 'filename': os.path.join(subpath, file), 'seen': False} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
return result |
| 141 |
|
| 142 |
def main(): |
| 143 |
if len(sys.argv) != 2: |
| 144 |
print("Usage: %s <path-to-linux-firmware>" % sys.argv[0]) |
| 145 |
return 1 |
| 146 |
|
| 147 |
fwpath = sys.argv[1] |
| 148 |
qcompath = os.path.join(fwpath, "qcom") |
| 149 |
fws = load_fws(qcompath) |
| 150 |
|
| 151 |
okay = True |
| 152 |
|
| 153 |
for data in load_config(): |
| 154 |
if data[0] != "install": |
| 155 |
continue |
| 156 |
|
| 157 |
soc = data[2].split('/')[0] |
| 158 |
path = os.path.join(data[2], data[3]) |
| 159 |
socpath = os.path.join(soc, data[3]) |
| 160 |
version = data[4].removeprefix("%s-" % data[3]) |
| 161 |
bindir = os.path.join(data[2], data[4]) |
| 162 |
|
| 163 |
mismatch = None |
| 164 |
|
| 165 |
if socpath in fws: |
| 166 |
if fws[socpath]['version'] == version: |
| 167 |
fws[socpath]['seen'] = True |
| 168 |
|
| 169 |
fwfile = os.path.join(qcompath, fws[socpath]['filename']) |
| 170 |
okay = check_hashes(fwfile, bindir) and okay |
| 171 |
continue |
| 172 |
mismatch = f"version mismatch %s %s vs %s %s" % (path, version, fws[socpath]['filename'], fws[socpath]['version']) |
| 173 |
|
| 174 |
if path in fws: |
| 175 |
if fws[path]['version'] == version: |
| 176 |
fws[path]['seen'] = True |
| 177 |
|
| 178 |
fwfile = os.path.join(qcompath, fws[path]['filename']) |
| 179 |
okay = check_hashes(fwfile, bindir) and okay |
| 180 |
continue |
| 181 |
mismatch = f"version mismatch %s %s vs %s %s" % (path, version, fws[path]['filename'], fws[path]['version']) |
| 182 |
|
| 183 |
if mismatch: |
| 184 |
print(mismatch) |
| 185 |
else: |
| 186 |
print("no match for DSP binaries", path, "version", version) |
| 187 |
|
| 188 |
for data in fws.values(): |
| 189 |
if not data['seen']: |
| 190 |
print("no match for DSP firmware", data["filename"], "version", data["version"]) |
| 191 |
|
| 192 |
return 0 if okay else 1 |
| 193 |
|
| 194 |
if __name__ == "__main__": |
| 195 |
sys.exit(main()) |