#!/home/geodd/.local/opt/rea-python/bin/python """Original selected format/levels/default getter and complete BAR2 instance bytes. Private Unicorn execution only. Address/aperture/volatility getters are explicit owned-descriptor models; no native driver, MMIO, root bind or TLB runs. """ import argparse,hashlib,json,struct,warnings from pathlib import Path warnings.filterwarnings('ignore',category=UserWarning,module='unicorn') from elftools.elf.elffile import ELFFile from unicorn import Uc,UC_ARCH_X86,UC_MODE_64,UC_HOOK_CODE from unicorn.x86_const import * parser=argparse.ArgumentParser();parser.add_argument('--check',action='store_true');options=parser.parse_args() R=Path(__file__).resolve().parents[1] P=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary') TEXT=0x10000000;RO=0x18000000;DATA=0x30000000;STACK=0x40000000;STUB=0x50000000;STOP=0x60000000 GPU=DATA;MMU=DATA+0x10000;FAMILY=DATA+0x20000;BUS=DATA+0x30000;INSTANCE=DATA+0x40000;LEVELS=DATA+0x50000;DESC=DATA+0x60000;FMT64=DATA+0x70000;FMT128=DATA+0x71000 names=['_nv040785rm','_nv040762rm','_nv040755rm','_nv040786rm','_nv039298rm','_nv039476rm'];hooks={} with P.open('rb') as stream: elf=ELFFile(stream);syms=list(elf.get_section_by_name('.symtab').iter_symbols());defined={s.name:s for s in syms} text=bytearray(elf.get_section_by_name('.text').data());ro=elf.get_section_by_name('.rodata').data();ti=elf.get_section_index('.text');ri=elf.get_section_index('.rodata') functions={n:(defined[n]['st_value'],defined[n]['st_size']) for n in names};undefined={} for rel in elf.get_section_by_name('.rela.text').iter_relocations(): offset=rel['r_offset'] if not any(start<=offset>32,limit|4095) instances.append({'address':descriptor_address,'aperture':descriptor_aperture,'volatile':bool(descriptor_volatile),'big_page':big,'limit':limit,'bytes':data}) report={'passed':True,'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'support':support,'levels':levels,'default_getters':getters,'instances':instances,'scope':'Whole original040785/040762/040755/040786/039298/039476 instructions. Real rodata version table. Only owned address/aperture/volatility getters mocked for instance inputs. No native code, actual BAR2 root bind, DMA, IRQ, TLB or hardware execution.'} lines=['// Generated from exact580 original BAR2 format/instance instructions.','pub struct Levels {\n pub big_page: u64,\n pub base: u64,\n pub bytes: [u8; 144],\n}','pub struct Instance {\n pub address: u64,\n pub aperture: u32,\n pub volatile: bool,\n pub big_page: u64,\n pub limit: u64,\n pub bytes: [u8; 16],\n}','#[rustfmt::skip]','pub const SUPPORT: &[(u32,bool)] = &['] for version,supported in support:lines.append(f'({version},{str(bool(supported)).lower()}),') lines+= ['];','#[rustfmt::skip]','pub const LEVELS: &[Levels] = &['] for row in levels:lines.append('Levels{big_page:%d,base:%d,bytes:%s},'%(row['big_page'],row['base'],row['bytes'])) lines+= ['];','#[rustfmt::skip]','pub const GETTERS: &[(u64,u32,u64,u64)] = &['] for row in getters:lines.append('(%d,%d,%d,%d),'%tuple(row)) lines+= ['];','#[rustfmt::skip]','pub const INSTANCES: &[Instance] = &['] for row in instances:lines.append('Instance{address:%d,aperture:%d,volatile:%s,big_page:%d,limit:%d,bytes:%s},'%(row['address'],row['aperture'],str(row['volatile']).lower(),row['big_page'],row['limit'],row['bytes'])) lines.append('];') outputs={R/'reports/bar2-root-binary.json':json.dumps(report,indent=2)+'\n',R/'src/nvidia/src/mmu/bar2_vectors.rs':'\n'.join(lines)+'\n'} for path,content in outputs.items(): if options.check: if not path.exists() or path.read_text()!=content:raise SystemExit('Generated output differs: '+str(path)) else:path.write_text(content) print(len(support)+len(levels)+len(getters)+len(instances),'original BAR2 format/instance cases passed')