- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF, AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib) - android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals), lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art - touch: shared touch controls moved from ios/Sources to dm/touch - patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults - hle_audio: AAudio resume waits through STARTING and reopens once on failure - .gitignore: local resources/ (original game data) never committed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
17 KiB
Python
250 lines
17 KiB
Python
#!/usr/bin/env python3
|
||
# 从苹果包(IPA)里的 Guest.framework 反推改写前的模块映像,写成未压缩 NSO:给拿不到原 ExeFS、只有可运行 IPA 的场合重出别的 ABI(如 DM_ABI=android)。
|
||
# 做法:按 guest_dylib.py 的节布局拼回 patched.bin;text 里每条跳进补丁区的 b 按补丁块形态还原原指令(patch_nso.py 各 site_* 的逆);
|
||
# 参数位按 patch_nso.py 的 PARAMS 表还原两条原字;最后按包内 <名>.cuts.tsv 把剪口(含代码 / 数据)还原成原字节。
|
||
# 判据:用同一规则域(IPA 为 touch:DM_RULESET=touch-run-v4)苹果 ABI 重新改写,patched.bin / fixups / gates / cuts 须与包内逐字节一致(--check 自动做)。
|
||
# 局限:NSO 文件本身(lz4 压缩、表头校验值)复原不出,extract_game.sh 里的原文件 SHA256 对不上;包里没有 subsdk0(视频库,宿主不加载);
|
||
# sdk 的构建号包里没有,写全 0(改写器只按构建号查 main 的登记表)。
|
||
# 用法:ipa_unpatch.py <IPA 或解开的 DmIOS.app 目录> <输出目录> [--check] 输出 <输出目录>/{main,sdk}
|
||
# Reconstruct the pre-rewrite module images from Guest.framework inside an Apple package (IPA) and write them as uncompressed NSOs: for when the original ExeFS is unavailable and only a runnable IPA is, so another ABI (e.g. DM_ABI=android) can be built.
|
||
# Method: reassemble patched.bin from the section layout of guest_dylib.py; for every b in text that jumps into the patch area, restore the original instruction from the patch block's shape (the inverse of each site_* in patch_nso.py);
|
||
# parameter slots get both original words back from patch_nso.py's PARAMS table; finally the package's <name>.cuts.tsv restores cuts (code / data) to their original bytes.
|
||
# Criterion: rewriting again with the same rule domain (the IPA is touch: DM_RULESET=touch-run-v4) and the Apple ABI must reproduce patched.bin / fixups / gates / cuts byte for byte (--check does this).
|
||
# Limits: the NSO file itself (lz4 compression, header hashes) cannot be recovered, so the original-file SHA256 in extract_game.sh will not match; the package has no subsdk0 (video library, not loaded by the host);
|
||
# the package does not carry sdk's build ID, so it is written as all zeros (the rewriter only looks up main's registries by build ID).
|
||
# Usage: ipa_unpatch.py <IPA or unpacked DmIOS.app directory> <output dir> [--check] writes <output dir>/{main,sdk}
|
||
import os, re, struct, subprocess, sys, tempfile, zipfile
|
||
from itertools import combinations
|
||
from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM
|
||
|
||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
MODS = ('main', 'sdk')
|
||
BUILD_IDS = {'main': 'db0507f36b84200ac05f75d76ed5026f'}
|
||
PG = 0x4000
|
||
md = Cs(CS_ARCH_ARM64, CS_MODE_ARM); md.detail = True
|
||
W = lambda b, o: struct.unpack_from('<I', b, o)[0]
|
||
r16 = lambda x: (x + PG - 1) & ~(PG - 1)
|
||
|
||
def dis(w, pc):
|
||
l = list(md.disasm(struct.pack('<I', w), pc)); assert l, f'{pc:#x} {w:08x}'; return l[0]
|
||
|
||
def bt(w, pc):
|
||
"""b / bl 目标;不是 b 返回 None。 | Target of a b; None if not a b."""
|
||
if w & 0xFC000000 != 0x14000000: return None
|
||
return pc + 4 * (((w & 0x3FFFFFF) ^ 0x2000000) - 0x2000000)
|
||
|
||
def read_app(src):
|
||
"""{相对路径: 字节},只取 dm/ 与 Guest。 | {relative path: bytes}, dm/ and Guest only."""
|
||
want = lambda p: p.startswith('dm/') or p == 'Frameworks/Guest.framework/Guest'
|
||
out = {}
|
||
if os.path.isdir(src):
|
||
for p in ['Frameworks/Guest.framework/Guest'] + ['dm/' + f for f in os.listdir(os.path.join(src, 'dm'))]:
|
||
out[p] = open(os.path.join(src, p), 'rb').read()
|
||
else:
|
||
with zipfile.ZipFile(src) as z:
|
||
for n in z.namelist():
|
||
m = re.match(r'Payload/[^/]+\.app/(.*)$', n)
|
||
if m and want(m.group(1)) and not n.endswith('/'): out[m.group(1)] = z.read(n)
|
||
return out
|
||
|
||
def sections(guest):
|
||
"""Mach-O 节 → {节名: 字节}(guest_dylib.py 写法:文件偏移 = 地址)。 | Mach-O sections → {name: bytes} (guest_dylib.py layout: file offset = address)."""
|
||
assert W(guest, 0) == 0xFEEDFACF
|
||
ncmds = W(guest, 16); o = 32; out = {}
|
||
for _ in range(ncmds):
|
||
cmd, sz = struct.unpack_from('<II', guest, o)
|
||
if cmd == 0x19:
|
||
seg = guest[o + 8:o + 24].rstrip(b'\0').decode(); ns = W(guest, o + 64)
|
||
for i in range(ns):
|
||
so = o + 72 + 80 * i
|
||
sn = guest[so:so + 16].rstrip(b'\0').decode()
|
||
addr, size = struct.unpack_from('<QQ', guest, so + 32)
|
||
out[sn if sn != '__img' else seg] = guest[addr:addr + size]
|
||
o += sz
|
||
return out
|
||
|
||
def header(txt):
|
||
return {k: int(v, 0) for k, v in (l[2:].split() for l in txt.splitlines() if l.startswith('# ') and l.split()[1] != 'BUILD_ID')}
|
||
|
||
def params_table():
|
||
"""patch_nso.py 的 PARAMS:[(A, 原字 A, B, 原字 B)]。 | patch_nso.py's PARAMS: [(A, word A, B, word B)]."""
|
||
s = open(os.path.join(HERE, 'patch_nso.py'), encoding='utf-8').read()
|
||
body = s[s.index('PARAMS = {'):s.index('params = PARAMS.get')]
|
||
return [(int(a, 16), int(wa, 16), int(b, 16), int(wb, 16)) for a, wa, b, wb in re.findall(r"\((0x[0-9a-f]+), '([0-9a-f]{8})', (0x[0-9a-f]+), '([0-9a-f]{8})'", body)]
|
||
|
||
def regs_in(ins):
|
||
"""与 patch_nso.regs_in 同。 | Same as patch_nso.regs_in."""
|
||
s = {int(n) for n in re.findall(r'\b[xw](\d+)\b', ins.op_str)}
|
||
try:
|
||
r, w = ins.regs_access()
|
||
s |= {int(m.group(1)) for m in (re.fullmatch(r'[xw](\d+)', ins.reg_name(x) or '') for x in r + w) if m}
|
||
except Exception: pass
|
||
return s
|
||
|
||
def unsubst(nw, S, pc, T=None):
|
||
"""patch_nso.subst 的逆:xS/wS → x18/w18,只在 4 个寄存器字段里找,再用 subst 的判据正向核对。
|
||
S、T 是改写器 pick() 挑的「原指令没用到的最小两个寄存器」:候选不得用到 S / T;给了 T(单条指令的块)时还须 pick(候选) == (S, T)。
|
||
Inverse of patch_nso.subst. S, T are the two lowest registers unused by the original instruction (patch_nso pick()): a candidate must not use S / T,
|
||
and when T is given (single-instruction block) pick(candidate) must equal (S, T)."""
|
||
pos = [p for p in (0, 5, 10, 16) if (nw >> p) & 31 == S]
|
||
got = []
|
||
for k in range(len(pos), 0, -1):
|
||
for c in combinations(pos, k):
|
||
w = nw
|
||
for p in c: w = (w & ~(31 << p)) | (18 << p)
|
||
try: ins = dis(w, pc)
|
||
except AssertionError: continue
|
||
if not re.search(r'\b[xw]18\b', ins.op_str): continue
|
||
want = re.sub(r'\b([xw])18\b', lambda m: f'{m.group(1)}{S}', ins.op_str)
|
||
if dis(nw, pc).mnemonic != ins.mnemonic or dis(nw, pc).op_str != want: continue
|
||
used = regs_in(ins)
|
||
if S in used or (T is not None and (T in used or [r for r in range(16) if r not in used and r != 18][:2] != [S, T])): continue
|
||
got.append((k, w))
|
||
if not got: return None
|
||
# 多个候选正向改写都得 nw 时(立即数字段恰为 S,如逻辑立即数的 immr)取替换字段最少的:编码器给的规范编码;--check 的重改写仍须逐字节一致
|
||
# When several candidates all rewrite forward to nw (an immediate field happens to equal S, e.g. immr of a logical immediate), take the one with the fewest
|
||
# substituted fields: the canonical encoding an assembler emits; --check still requires the re-rewrite to match byte for byte
|
||
kmin = min(k for k, _ in got); best = {w for k, w in got if k == kmin}
|
||
assert len(best) == 1, f'{pc:#x} {nw:08x} S=x{S}:候选 {[hex(x) for x in best]}'
|
||
if len(got) > 1: AMBIG.append(pc)
|
||
return best.pop()
|
||
AMBIG = []
|
||
|
||
class Block:
|
||
"""补丁块读取器:跳过槽块基址前缀(apple:mrs tpidrro)+ 键页三条,返回 (基址寄存器, 键下标, 后续偏移)。 | Patch block reader."""
|
||
def __init__(self, pat, key_idx): self.pat, self.kv = pat, {v: k for k, v in key_idx.items()}
|
||
def w(self, o): return W(self.pat, o)
|
||
def slot(self, o):
|
||
w = self.w(o); assert w & 0xFFFFFFE0 == 0xD53BD060, f'{o:#x} 不是 mrs tpidrro:{w:08x}'
|
||
T = w & 31; ldr = self.w(o + 8)
|
||
assert self.w(o + 4) & 0x9F000000 == 0x90000000 and ldr & 0xFFC00000 == 0xF9400000 and self.w(o + 12) & 0xFFE0FFFF == 0x8B000000 | (T << 5) | T, f'{o:#x} 键页序列不符'
|
||
return T, self.kv[(ldr >> 10) & 0xFFF], o + 16
|
||
|
||
def restore(name, app, gates):
|
||
hdr = header(app[f'dm/{name}.fixups.tsv'].decode())
|
||
se = sections(app['Frameworks/Guest.framework/Guest'])
|
||
te, po, ko = hdr['TEXT_END'], hdr['PATCH_OFF'], hdr['KEY_PAGE_OFF']
|
||
pat = bytearray(se[f'__dm{name}x0'] + se[f'__DM{name}W'] + se[f'__dm{name}x1'])
|
||
assert len(se[f'__dm{name}x0']) == r16(te) and len(se[f'__dm{name}x0']) + len(se[f'__DM{name}W']) == po
|
||
assert pat[ko:ko + PG] == bytes(PG), '键页不是全 0'
|
||
key_idx = {k[4:-4]: v for k, v in hdr.items() if k.startswith('KEY_') and k.endswith('_IDX')}
|
||
B = Block(pat, key_idx)
|
||
img = bytearray(pat[:ko])
|
||
gate_at = {orig: (k, arg) for ret, k, arg, orig in gates}
|
||
params = {a: (wa, b, wb) for a, wa, b, wb in params_table()} if name in BUILD_IDS else {}
|
||
cuts = []
|
||
if f'dm/{name}.cuts.tsv' in app:
|
||
for l in app[f'dm/{name}.cuts.tsv'].decode().splitlines():
|
||
if l.startswith('#') or not l.strip(): continue
|
||
f = l.split('\t'); cuts.append((int(f[0], 16), bytes.fromhex(f[1]), bytes.fromhex(f[2])))
|
||
cut_b = {off: nb for off, ob, nb in cuts if len(nb) == 4 and bt(W(nb, 0), off) is not None and bt(W(nb, 0), off) >= po}
|
||
st = {}
|
||
def put(o, w, kind): struct.pack_into('<I', img, o, w); st[kind] = st.get(kind, 0) + 1
|
||
for o in range(0, te & ~3, 4):
|
||
w = W(img, o); s = bt(w, o)
|
||
if s is None or s < po: continue
|
||
if o in cut_b and cut_b[o] == struct.pack('<I', w): st['cut_b'] = st.get('cut_b', 0) + 1; continue # 剪口自带的跳板:最后按 cuts.tsv 还原 | trampoline belonging to a cut: restored from cuts.tsv at the end
|
||
if o in params:
|
||
wa, b, wb = params[o]; assert W(img, b) == 0xD503201F, f'参数位 B {b:#x} 不是 nop'
|
||
put(o, wa, 'param'); struct.pack_into('<I', img, b, wb); continue
|
||
h = B.w(s)
|
||
if h == 0xA9BF7FE8 or h == 0xA9BF7BFD: # 分支门 / 门 / 观测门 | branch gate / gate / observation gate
|
||
q = s + (4 if h == 0xA9BF7FE8 else 0)
|
||
assert B.w(q) == 0xA9BF7BFD
|
||
T, key, n = B.slot(q + 4); assert T == 30 and key == 'KGATE'
|
||
assert B.w(n) == 0xF94003DE and B.w(n + 4) == 0xF94003FD and B.w(n + 8) == 0xD63F03C0 and B.w(n + 12) == 0xA8C17BFD
|
||
n += 16
|
||
if h == 0xA9BF7FE8: # cbz w8; ldp; b 目标; ldp; 原字; b o+4
|
||
assert B.w(n) == 0x34000068 and bt(B.w(n + 20), n + 20) == o + 4; put(o, B.w(n + 16), 'branch_gate')
|
||
elif bt(B.w(n), n) == o + 4: # 宿主门:svc / cntpct / cntvct
|
||
k, arg = gate_at[o]
|
||
put(o, {'svc': 0xD4000001 | arg << 5, 'cntpct': 0xD53BE020 | arg, 'cntvct': 0xD53BE040 | arg}[k], k)
|
||
else:
|
||
assert bt(B.w(n + 4), n + 4) == o + 4; put(o, B.w(n), 'obs')
|
||
continue
|
||
if h & 0xFFFFFFE0 == 0xF81F03E0: # stur xR,[sp,#-16]
|
||
R = h & 31; h2 = B.w(s + 4)
|
||
if R == 16 and h2 == 0xF81E83F1: # 跳转表 br x18
|
||
put(o, 0xD61F0240, 'br'); continue
|
||
if h2 & 0xFFFFFFE0 == 0xF81E83E0: # msr tpidr_el0, xN:stur T / stur tmp / 基址 T / str N,[T]
|
||
T, key, n = B.slot(s + 8); assert key == 'KTLS' and B.w(n) & 0xFFFFFFE0 == 0xF9000000 | (T << 5)
|
||
put(o, 0xD51BD040 | (B.w(n) & 31), 'tpidr_w'); continue
|
||
N, key, n = B.slot(s + 4) # mrs xN, tpidr_el0 / tpidrro_el0
|
||
put(o, {'KTLS': 0xD53BD040, 'KTLR': 0xD53BD060}[key] | N, 'sysreg_r'); continue
|
||
assert h & 0xFFC003E0 == 0xA90003E0, f'{o:#x} → {s:#x} 未知块头 {h:08x}' # stp xS, xT, [sp, #save]
|
||
S = h & 31; T, key, n = B.slot(s + 4); assert key == 'K18' and T == (h >> 10) & 31
|
||
w1 = B.w(n)
|
||
if w1 & 0x9F00001F == 0x90000000 | S: # adrp xS → adr / adrp x18
|
||
tgt = (n & ~0xFFF) + (((((w1 >> 5) & 0x7FFFF) << 2 | (w1 >> 29) & 3) ^ 0x100000) - 0x100000 << 12)
|
||
if B.w(n + 4) & 0xFFC003FF == 0x91000000 | (S << 5) | S:
|
||
tgt += (B.w(n + 4) >> 10) & 0xFFF; d = tgt - o
|
||
put(o, 0x10000000 | ((d & 3) << 29) | (((d >> 2) & 0x7FFFF) << 5) | 18, 'adr')
|
||
else:
|
||
d = (tgt >> 12) - (o >> 12)
|
||
put(o, 0x90000000 | ((d & 3) << 29) | (((d >> 2) & 0x7FFFF) << 5) | 18, 'adrp')
|
||
continue
|
||
assert w1 == 0xF9400000 | (T << 5) | S, f'{s:#x} 读槽不符'
|
||
n += 4; w2 = B.w(n); m = dis(w2, n).mnemonic
|
||
if B.w(n + 4) == 0xF9000000 | (T << 5) | S and B.w(n + 8) & 0xFFC07FFF == 0xA9400000 | (T << 10) | (31 << 5) | S and bt(B.w(n + 12), n + 12) == o + 4:
|
||
w0 = unsubst(w2, S, o, T); assert w0 is not None, f'{o:#x} 普通块还原不出 x18'
|
||
put(o, w0, 'plain'); continue
|
||
if m in ('cbz', 'cbnz', 'tbz', 'tbnz') and w2 & 31 == S and bt(B.w(n + 8), n + 8) == o + 4:
|
||
tgt = bt(B.w(n + 16), n + 16); off = (tgt - o) >> 2
|
||
nw = ((w2 & 0xFF000000) | ((off & 0x7FFFF) << 5) | 18) if m in ('cbz', 'cbnz') else ((w2 & 0xFFF80000) | ((off & 0x3FFF) << 5) | 18)
|
||
put(o, nw, 'cond'); continue
|
||
# 独占窗口:块里依次是窗口指令的拷贝,首条即原处那条(窗口首条是独占读,不是分支)
|
||
# Exclusive window: the block holds copies of the window instructions in order; the first is the one at the original site (an exclusive load, not a branch)
|
||
assert dis(w2, o).mnemonic.startswith(('ldxr', 'ldaxr', 'ldxp', 'ldaxp')), f'{o:#x} 窗口首条 {dis(w2, o).mnemonic}'
|
||
w0 = unsubst(w2, S, o); put(o, w2 if w0 is None else w0, 'window')
|
||
for off, ob, nb in cuts:
|
||
assert bytes(img[off:off + len(nb)]) == nb, f'剪口 {off:#x} 现值 {bytes(img[off:off + len(nb)]).hex()} ≠ 新字节 {nb.hex()}'
|
||
img[off:off + len(ob)] = ob
|
||
st['cut'] = len(cuts)
|
||
return img, hdr, st, pat
|
||
|
||
def write_nso(path, img, hdr, build_id):
|
||
te, ro, da, ko = hdr['TEXT_END'], hdr['RO_OFF'], hdr['DATA_OFF'], hdr['KEY_PAGE_OFF']
|
||
segs = [(0, te), (ro, da - ro), (da, ko - da)] # data 拉到键页前、bss 0:映像长 = KEY_PAGE_OFF,改写器算出同一 KEY_PAGE_OFF / PATCH_OFF
|
||
h = bytearray(0x100); h[0:4] = b'NSO0'
|
||
off = 0x100
|
||
for i, (mo, sz) in enumerate(segs): struct.pack_into('<III', h, 0x10 + 0x10 * i, off, mo, sz); off += sz
|
||
h[0x40:0x50] = bytes.fromhex(build_id)
|
||
struct.pack_into('<III', h, 0x60, *(sz for _, sz in segs))
|
||
with open(path, 'wb') as f:
|
||
f.write(h)
|
||
for mo, sz in segs: f.write(img[mo:mo + sz])
|
||
|
||
def check(name, out, app, pat):
|
||
with tempfile.TemporaryDirectory() as td:
|
||
env = dict(os.environ, DM_RULESET='touch-run-v4', DM_ABI='apple'); env.pop('DM_PRIVATE_GATES', None)
|
||
r = subprocess.run([sys.executable, os.path.join(HERE, 'patch_nso.py'), os.path.join(out, name), td, name], env=env, capture_output=True, text=True)
|
||
if r.returncode: print(r.stdout, r.stderr); return False
|
||
got = open(os.path.join(td, f'{name}.patched.bin'), 'rb').read()
|
||
ok = got.rstrip(b'\0') == bytes(pat).rstrip(b'\0')
|
||
if not ok:
|
||
n = min(len(got), len(pat)); bad = [o for o in range(0, n, 4) if got[o:o + 4] != pat[o:o + 4]]
|
||
print(f' patched.bin 不一致:{len(bad)} 字,首个 {[hex(x) for x in bad[:8]]}')
|
||
for t in ('fixups', 'gates', 'cuts'):
|
||
if f'dm/{name}.{t}.tsv' not in app: continue
|
||
same = open(os.path.join(td, f'{name}.{t}.tsv'), 'rb').read() == app[f'dm/{name}.{t}.tsv']
|
||
print(f' {t}.tsv {"一致" if same else "不一致"}'); ok &= same
|
||
return ok
|
||
|
||
def main():
|
||
a = [x for x in sys.argv[1:] if not x.startswith('--')]
|
||
if len(a) != 2: sys.exit('用法:ipa_unpatch.py <IPA 或 DmIOS.app 目录> <输出目录> [--check]')
|
||
app = read_app(a[0]); os.makedirs(a[1], exist_ok=True); ok = True
|
||
for name in MODS:
|
||
gates = []
|
||
for l in app[f'dm/{name}.gates.tsv'].decode().splitlines():
|
||
r, k, arg, orig = l.split(); gates.append((int(r, 16), k, int(arg), int(orig, 16)))
|
||
img, hdr, st, pat = restore(name, app, gates)
|
||
write_nso(os.path.join(a[1], name), img, hdr, BUILD_IDS.get(name, '00' * 16))
|
||
print(name, st, '多候选取规范编码', len(AMBIG)); AMBIG.clear()
|
||
if '--check' in sys.argv:
|
||
c = check(name, a[1], app, pat); ok &= c; print(f' {name} 苹果 ABI 重改写 {"与包内逐字节一致" if c else "不一致"}')
|
||
sys.exit(0 if ok else 1)
|
||
|
||
if __name__ == '__main__':
|
||
main()
|