|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | +import platform |
| 6 | +from packaging import version |
| 7 | + |
| 8 | +FIRMWARE_SEARCH_DIR = Path("/var/snap/intel-npu-driver/current/intel/vpu") |
| 9 | + |
| 10 | +# Version string always starts with a date followed by an asterisk, the date is |
| 11 | +# in one of these two formats: YYYYMMDD or Mmm DD YYYY |
| 12 | +# |
| 13 | +# Examples of both formats: |
| 14 | +# - "20250925*MTL_CLIENT_SILICON-NVR+NN-deployment*2485cfeafeed591eaa9a320bfa\ |
| 15 | +# e2407c1b83b29f*2485cfeafeed591eaa9a320bfae2407c1b83b29f*2485cfeafee" |
| 16 | +# - "Sep 25 2025*NPU40xx*build/ci/npu-fw-ci-ci_branch_UD202538_PTL_PV_npu_rel\ |
| 17 | +# ease_25ww35-20250915_222036-29036-1-g2485cfeafee*2485cfeafeed591eaa9a\ |
| 18 | +# 320bfae2407c1b83b29f" |
| 19 | +VERSION_PATTERN = re.compile(r"^(\d{8}\*|[A-Z][a-z]{2}\s+\d{1,2}\s+\d{4}\*).*") |
| 20 | + |
| 21 | + |
| 22 | +def check_kernel_version(required_version): |
| 23 | + current_version_str = platform.release() |
| 24 | + |
| 25 | + current_version = version.Version(current_version_str.rsplit("-", 1)[0]) |
| 26 | + required_version = version.Version(required_version.rsplit("-", 1)[0]) |
| 27 | + |
| 28 | + # Return True if current version is at least the required version |
| 29 | + return ( |
| 30 | + current_version is not None |
| 31 | + and required_version is not None |
| 32 | + and current_version >= required_version |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def get_active_firmware_line(): |
| 37 | + result = subprocess.check_output( |
| 38 | + ["journalctl", "--dmesg"], universal_newlines=True |
| 39 | + ).splitlines() |
| 40 | + |
| 41 | + matching_lines = [line for line in result if "Firmware: intel/vpu" in line] |
| 42 | + |
| 43 | + if not matching_lines: |
| 44 | + raise SystemExit("No 'intel_vpu' firmware logs found in dmesg.") |
| 45 | + |
| 46 | + return matching_lines[-1] |
| 47 | + |
| 48 | + |
| 49 | +def find_version_in_file(filepath): |
| 50 | + try: |
| 51 | + result = subprocess.check_output( |
| 52 | + ["strings", filepath], universal_newlines=True |
| 53 | + ) |
| 54 | + for line in result.splitlines(): |
| 55 | + # Return the first match found |
| 56 | + if VERSION_PATTERN.match(line): |
| 57 | + return line |
| 58 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 59 | + # This can happen with corrupted files or if 'strings' isn't installed |
| 60 | + raise SystemExit( |
| 61 | + "`strings` is not installed or can't read " |
| 62 | + "the firmware file {}.".format(filepath) |
| 63 | + ) |
| 64 | + |
| 65 | + print("No version number found in the file {}".format(filepath)) |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + if not check_kernel_version("6.8"): |
| 70 | + print( |
| 71 | + "Getting the firmware version out of dmesg is not supported " |
| 72 | + "on this system. Skipping..." |
| 73 | + ) |
| 74 | + return |
| 75 | + |
| 76 | + active_firmware_line = get_active_firmware_line() |
| 77 | + |
| 78 | + if not FIRMWARE_SEARCH_DIR.is_dir(): |
| 79 | + raise SystemExit("Firmware directory not found.") |
| 80 | + |
| 81 | + found_driver_versions = [] |
| 82 | + for filepath in FIRMWARE_SEARCH_DIR.iterdir(): |
| 83 | + if filepath.is_file() and filepath.suffix == ".bin": |
| 84 | + driver_version = find_version_in_file(filepath) |
| 85 | + |
| 86 | + if driver_version: |
| 87 | + if driver_version in active_firmware_line: |
| 88 | + print( |
| 89 | + "Test success: Loaded NPU firmware version matches a " |
| 90 | + "file from the snap. Version: {}, File: " |
| 91 | + "{}".format(driver_version, str(filepath)) |
| 92 | + ) |
| 93 | + return |
| 94 | + |
| 95 | + found_driver_versions.append(driver_version) |
| 96 | + |
| 97 | + raise SystemExit( |
| 98 | + "The loaded firmware does not match any version in the snap files.\n" |
| 99 | + "Loaded firmware: {}\n" |
| 100 | + "Versions found in the snap: {}".format( |
| 101 | + active_firmware_line, |
| 102 | + "\n".join(map(lambda e: " - " + e, found_driver_versions)), |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments