#!/usr/bin/env python3 """Offline ARM harness: original wlan-cal code, mocked CAL and file I/O only.""" import argparse, sys, json, struct, hashlib, importlib.util from pathlib import Path from unicorn import Uc, UC_ARCH_ARM, UC_MODE_ARM, UC_HOOK_CODE from unicorn.arm_const import UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3, UC_ARM_REG_SP, UC_ARM_REG_LR, UC_ARM_REG_PC from elftools.elf.elffile import ELFFile base=Path(__file__).resolve().parent p=argparse.ArgumentParser(description=__doc__) p.add_argument('--binary',type=Path,required=True) p.add_argument('--cal-image',type=Path,required=True) p.add_argument('--out',type=Path,required=True) a=p.parse_args() a.out.mkdir(parents=True,exist_ok=False) a.out.chmod(0o700) spec=importlib.util.spec_from_file_location('cal_inspect',base.parent/'scripts/n810-cal-inspect.py') cal=importlib.util.module_from_spec(spec);spec.loader.exec_module(cal) backup=a.cal_image records=cal.scan(backup.read_bytes());selected={} for r in records: if r['header_crc_ok'] and r['data_crc_ok']: if r['name'] not in selected or r['version']>selected[r['name']]['version']:selected[r['name']]=r binary=a.binary if hashlib.sha256(binary.read_bytes()).hexdigest() != 'e562b686c736adef908b6d052f6c742d8fee93d482def95d4793d1be5fac5ca9': raise SystemExit('This harness requires the pinned Maemo wlan-cal binary; hook addresses are version-specific.') u=Uc(UC_ARCH_ARM, UC_MODE_ARM);u.mem_map(0x8000,0x18000);u.mem_map(0x200000,0x200000);u.mem_map(0x400000,0x100000);u.mem_map(0x600000,0x1000) with binary.open('rb') as f: elf=ELFFile(f) for seg in elf.iter_segments(): if seg['p_type']=='PT_LOAD':u.mem_write(seg['p_vaddr'],seg.data()) heap=0x200000;fds={};writes={};reads=[];ppu=[] def alloc(data): global heap p=heap;heap+=(len(data)+15)&~15;u.mem_write(p,data);return p def string(p): b=bytearray() for i in range(4096): x=bytes(u.mem_read(p+i,1)) if x==b'\0':return bytes(b) b+=x raise RuntimeError('Unterminated emulated string') def ret(value):u.reg_write(UC_ARM_REG_R0,value&0xffffffff);u.reg_write(UC_ARM_REG_PC,u.reg_read(UC_ARM_REG_LR)) def hook(uc,address,size,data): if address==0x600000:uc.emu_stop();return if address not in {0x8594,0x85a0,0x85ac,0x85b8,0x85c4,0x85d0,0x85dc,0x85f4,0x8600,0x860c,0x8618,0x8630,0x863c}:return a,b,c,d=[uc.reg_read(r) for r in [UC_ARM_REG_R0,UC_ARM_REG_R1,UC_ARM_REG_R2,UC_ARM_REG_R3]] if address==0x8594: name=string(b).decode();r=selected.get(name);flags=struct.unpack('y)-(x