mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
109 lines
5.0 KiB
Python
109 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Export exact shipped resource descriptors; never infer live device support."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_ANALYSIS_ROOT = Path.home() / 'nvidia-driver/analysis-580.178.04'
|
|
REPORT_REL = 'reports/proprietary-all-resource-descriptors.json'
|
|
|
|
|
|
def find_input(explicit: Path | None = None) -> Path:
|
|
if explicit is not None:
|
|
if not explicit.is_file():
|
|
raise SystemExit(f'input file not found: {explicit}')
|
|
return explicit
|
|
analysis = Path(os.environ.get('NV_RS_ANALYSIS_ROOT', str(DEFAULT_ANALYSIS_ROOT)))
|
|
candidate = analysis / REPORT_REL
|
|
if candidate.is_file():
|
|
return candidate
|
|
raise SystemExit(f'input file not found at {candidate}; pass --input or set NV_RS_ANALYSIS_ROOT')
|
|
|
|
|
|
def find_rustfmt() -> str:
|
|
if os.environ.get('RUSTFMT'):
|
|
return os.environ['RUSTFMT']
|
|
which = shutil.which('rustfmt')
|
|
if which:
|
|
return which
|
|
candidates = []
|
|
if os.environ.get('CARGO_HOME'):
|
|
candidates.append(Path(os.environ['CARGO_HOME']) / 'bin/rustfmt')
|
|
candidates.append(Path.home() / '.cargo/bin/rustfmt')
|
|
for candidate in candidates:
|
|
if candidate.is_file():
|
|
return str(candidate)
|
|
raise SystemExit('rustfmt not found; set RUSTFMT or add rustfmt to PATH')
|
|
|
|
|
|
def main(argv=None) -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--input', type=Path, default=None)
|
|
args = parser.parse_args(argv)
|
|
|
|
inp = find_input(args.input)
|
|
j = json.loads(inp.read_text(encoding='utf-8'))
|
|
rows = j['resources']
|
|
if len(rows) != 279 or len({r['external_class'] for r in rows}) != 279:
|
|
raise SystemExit(f'expected 279 unique resource descriptors, found {len(rows)}')
|
|
|
|
out = [
|
|
'// Generated by tools/generate_resources.py from exact 580.178.04 proprietary metadata.',
|
|
'// Input RM SHA256: ' + j['input_sha256'],
|
|
'use nv_rs_abi::NvStatus;',
|
|
'#[derive(Clone, Copy, Debug, PartialEq, Eq)]',
|
|
"pub struct ResourceDescriptor { pub class: u32, pub internal_class: u32, pub parameter_size: u32, pub required: bool, pub multi_instance: bool, pub any_parent: bool, pub flags: u32, pub free_priority: u32, pub required_rights_count: u32, pub parents: &'static [u32], pub class_info_symbol: &'static str }",
|
|
'pub static RESOURCES: [ResourceDescriptor; 279] = [',
|
|
]
|
|
for r in rows:
|
|
out.append(
|
|
'ResourceDescriptor {class:' + r['external_class']
|
|
+ ', internal_class:' + r['internal_class']
|
|
+ ', parameter_size:' + str(r['allocation_parameter_size'])
|
|
+ ', required:' + str(r['required']).lower()
|
|
+ ', multi_instance:' + str(r['multi_instance']).lower()
|
|
+ ', any_parent:' + str(r['any_parent']).lower()
|
|
+ ', flags:' + r['flags']
|
|
+ ', free_priority:' + str(r['free_priority'])
|
|
+ ', required_rights_count:' + str(r['required_rights_length'])
|
|
+ ', parents:&[' + ','.join(r['parent_internal_classes'])
|
|
+ '], class_info_symbol:"' + r['class_info']['symbol'] + '"},'
|
|
)
|
|
out += [
|
|
'];',
|
|
"pub fn lookup(class: u32) -> Option<&'static ResourceDescriptor> { RESOURCES.iter().find(|r|r.class==class) }",
|
|
'/// Implicit allocation size, including the recovered optional-pointer policy.',
|
|
'pub fn parameter_size(class: u32, pointer_nonnull: bool) -> Result<u32,NvStatus> { let desc=lookup(class).ok_or(NvStatus::INVALID_CLASS)?; if desc.required && !pointer_nonnull {return Err(NvStatus::INVALID_ARGUMENT);} Ok(if desc.required || pointer_nonnull {desc.parameter_size} else {0}) }',
|
|
'#[cfg(test)] mod tests {use super::*;',
|
|
'#[test] fn known_legacy_sizes_and_null_policy() {assert_eq!(parameter_size(0xc3b5,true),Ok(8));assert_eq!(parameter_size(0xc3c0,true),Ok(16));assert_eq!(parameter_size(0xc3c0,false),Ok(0));assert_eq!(parameter_size(0xc36f,true),Ok(368));assert_eq!(parameter_size(0xc36f,false),Err(NvStatus::INVALID_ARGUMENT));assert_eq!(parameter_size(0xffffffff,true),Err(NvStatus::INVALID_CLASS));}',
|
|
'#[test] fn all_classes_unique() {for(i,a)in RESOURCES.iter().enumerate(){for b in &RESOURCES[i+1..]{assert_ne!(a.class,b.class);}}}',
|
|
'}',
|
|
]
|
|
target = ROOT / 'src/nvidia/src/resources.rs'
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_text('\n'.join(out) + '\n', encoding='utf-8')
|
|
report = ROOT / 'reports/resource-metadata-provenance.json'
|
|
report.parent.mkdir(parents=True, exist_ok=True)
|
|
report.write_text(
|
|
json.dumps(
|
|
{
|
|
'source': str(inp),
|
|
'source_sha256': j['input_sha256'],
|
|
'record_count': len(rows),
|
|
'scope': j['scope'],
|
|
},
|
|
indent=2,
|
|
)
|
|
+ '\n',
|
|
encoding='utf-8',
|
|
)
|
|
subprocess.run([find_rustfmt(), '--edition', '2024', str(target)], check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|