Files
nvdriver/tools/generate_hardware.py

32 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Generate no-allocation hardware lookup tables from hash-pinned recovered fixtures."""
from pathlib import Path
import hashlib,json,sys
R=Path(__file__).resolve().parents[1];D=R/'abi/hardware';p=json.loads((D/'provenance.json').read_text())
if p['original_elf_sha256'] != '5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b':
sys.exit("unexpected original ELF digest in provenance.json")
for name, digest in p['fixtures'].items():
if hashlib.sha256((D/name).read_bytes()).hexdigest() != digest:
sys.exit(f"fixture digest mismatch: {name}")
matrix=json.loads((D/'product-matrix.json').read_text())
rows=matrix['rows']
if len(rows) != 1129 or len({row["devid"] for row in rows}) != 450:
sys.exit("unexpected product or device ID count in product-matrix.json")
q=lambda x:json.dumps(x,ensure_ascii=True)
lines=['// Generated by tools/generate_hardware.py; do not edit.','use super::{Product, Route, Legacy, ChipTuple};','pub static PRODUCTS: [Product; 1129] = [']
for r in rows:
route={'proprietary_pre_turing':'Proprietary','open_and_proprietary_gsp':'BothGsp','open_only':'OpenOnly'}[r['kernel_route']]
sub=lambda k:'Some('+r[k].lower()+')' if k in r else 'None'
lines.append(f'Product {{ device: {r["devid"].lower()}, subvendor: {sub("subvendorid")}, subdevice: {sub("subdevid")}, name: {q(r["name"])}, route: Route::{route} }},')
lines+= ['];','pub static LEGACY: [Legacy; 1388] = [']
for r in json.loads((D/'pci-blacklist.json').read_text()):lines.append(f'Legacy {{ device: {r["device"]}, subvendor: {r["subvendor"]}, subdevice: {r["subdevice"]}, branch: {r["branch"]} }},')
lines+= ['];','pub static CHIPS: [ChipTuple; 113] = [']
for r in json.loads((D/'chip-tuples.json').read_text())['tuples']:lines.append(f'ChipTuple {{ architecture: {r["architecture"]}, implementation: {r["implementation"]}, soc: {r["soc_combined"]} }},')
lines+= ['];'];out='\n'.join(lines)+'\n';dest=R/'src/nvidia/src/hardware/generated.rs'
if '--check' in sys.argv:
if dest.read_text() != out:
sys.exit(f"generated hardware tables stale: {dest.name}")
else:
dest.write_text(out)
print('hardware tables verified:1129products,450IDs,1388blacklist records,113chip tuples')