33 lines
2.3 KiB
Python
33 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Original ground input/apply spans and isolated copied-code probe preparation."""
|
|
import argparse,hashlib,json,re,struct,subprocess
|
|
from pathlib import Path
|
|
from checkpoint_script_state import EXE_SHA256,find_case,read_va
|
|
from checkpoint_camera_audio_ui import DLL_SHA256
|
|
from pe_inspect import PE
|
|
RANGES=((0x43915d,0x4395cf),(0x437c40,0x437f7f),(0x4723f0,0x472420),
|
|
(0x472480,0x472570),(0x472b10,0x472c10),(0x476ff0,0x477490))
|
|
def collect(original):
|
|
pe=PE(find_case(Path(original),'Neutron.exe'))
|
|
if hashlib.sha256(pe.data).hexdigest()!=EXE_SHA256:raise ValueError('Unexpected original EXE')
|
|
if hashlib.sha256(find_case(Path(original),'OMT2.dll').read_bytes()).hexdigest()!=DLL_SHA256:raise ValueError('Unexpected original DLL')
|
|
return dict(source_sha256=EXE_SHA256,dll_sha256=DLL_SHA256,ranges=[dict(va=hex(a),file_offset=hex(pe.offset(a-pe.base)),length=b-a,
|
|
sha256=hashlib.sha256(read_va(pe,a,b-a)).hexdigest()) for a,b in RANGES])
|
|
def generate(original,output):
|
|
original=Path(original).resolve();output=Path(output).resolve()
|
|
if output.is_relative_to(original) or original.is_relative_to(output):raise ValueError('Separate output required')
|
|
report=collect(original);pe=PE(find_case(original,'Neutron.exe'));targets={0x48d104,0x48d108,0x48d10c}
|
|
for a,b in RANGES:
|
|
asm=subprocess.run(['objdump','-d','-Mintel',f'--start-address={a}',f'--stop-address={b}',str(pe.path)],capture_output=True,text=True,check=True).stdout
|
|
targets.update(int(x,16) for x in re.findall(r'ds:0x([a-f0-9]+)',asm))
|
|
reloc=[]
|
|
for a,b in RANGES:
|
|
data=read_va(pe,a,b-a)
|
|
for target in sorted(targets):reloc.extend((a+m.start(),target) for m in re.finditer(re.escape(struct.pack('<I',target)),data))
|
|
output.mkdir(parents=True,exist_ok=True)
|
|
body=Path(__file__).with_name('jimmy_ground_motion_probe.c').read_text().replace('/* RELOCATIONS */',','.join('{0x%x,0x%x}'%r for r in reloc))
|
|
(output/'jimmy_ground_motion_probe.c').write_text(body);(output/'source.json').write_text(json.dumps(report,indent=2)+'\n');return report
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('original',type=Path);p.add_argument('--generate-probe',type=Path);a=p.parse_args()
|
|
print(json.dumps(generate(a.original,a.generate_probe) if a.generate_probe else collect(a.original),indent=2))
|