def one_example(dir, example): sys.path.append(dir) y = importlib.import_module(dir + "." + example) sys.path.pop(-1) nm, ms = y.example() for i, j in enumerate(ms): if len(ms) == 1: fn = "_output/" + nm else: fn = "_output/" + nm + ".%02d" % i print(i, j, fn) if j.bits <= 8: ncol = 8 else: ncol = 8 for a, b in ((".asm", False), # (".pil", True) ): listing.Listing( j, ncol=ncol, fn=fn + a, pil=b, align_blank=True, align_xxx=True, compact_xxx=True, ) sys.stdout.flush()
def __init__(self, octets, output_file="/tmp/_", **kwargs): self.exp_ins = ExpDisass() m = mem.ByteMem(0, len(octets)) for adr, octet in enumerate(octets): m[adr] = octet self.cx = mcs51.I8032() self.cx.m.map(m, 0) self.cx.vectors(( "RESET", "RI_TI", )) self.common() self.distinct() code.lcmt_flows(self.cx.m) listing.Listing(self.cx.m, fn=output_file, align_blank=True, align_xxx=True, ncol=4, **kwargs)
def output(pj): code.lcmt_flows(pj) listing.Listing(pj)
def output(pj): code.lcmt_flows(pj) listing.Listing(pj, "HP3455A_Inguard.lst")
if pj.pg == 4: lexer(pj) cpu.vectors() # decompile.analyse(pj, cpu) str_len_args(pj, 0xd8a5, 3, 2) str_len_args(pj, 0x9ffe, 1, 2) error_arg(pj, 0xc3fe, 1, errors) num_arg(pj, 0xd37d, 1) num_arg(pj, 0xd37d, 2) num_arg(pj, 0xd392, 1) num_arg(pj, 0xd392, 2) num_arg(pj, 0xd3c6, 1) num_arg(pj, 0xd3c6, 2) str_len_args(pj, 0xd8ea, 3, 2) if pj.pg == 2: str_len_args(pj, 0x44ee, 3, 2) # decompile.mopup(pj, cpu) listing.Listing(pj) ####################################################################### # fcea is array index multiplicator # A is always 0x01 # X points to six bytes: {00 01 00 00 xx xx} where xxxx is stride # Stack contains base + index # result in X
def do_one(fn, alist): print("--------------", fn) m = mem.byte_mem(0x000, 0x800) orig_src = dict() labels = dict() def load_xx(m): fi = open(fn) for i in fi: if i[0] != ' ': continue j = i.strip() if j == "": continue j = i.split('*', 1)[0].rstrip() if len(j) < 15: continue if j[8] != "-": continue if not j[14].isdigit(): continue if j[14:18] == ' ': continue a = int(j[14:18], 8) d = int(j[20:23], 8) orig_src[a] = j m.wr(a, d) lb = j[25:30].strip() if lb != "": labels[a] = lb fi.close() load_xx(m) pj = pyreveng.Job(m, "HP-9411") dx = hp_nanoproc.hp_nanoproc() if True: for i in alist: pj.todo(i, dx.disass) pj.apct = "%04o" pj.apct = "%03x" while pj.run(): pass def fixit(i, a, b): if i == a.lo: # Check if there are instructions our disass don't know if a.mne != b[31:34]: print(a.im, b) a.lcmt += b + "\n" for i, j in orig_src.items(): x = pj.t.find_range(i, i + 1) if len(x[1]) > 0: fixit(i, x[1][0], j) elif len(x[0]) > 0: fixit(i, x[0][0], j) else: x = pj.add(i, i + 1, "origsrc") x.lcmt += j + "\n" # print(x) for i in labels: pj.set_label(i, labels[i]) if True: code.lcmt_flows(pj) listing.Listing(pj, fn="/tmp/_.HP" + fn, fmt="o", ncol=1)
0x4e56, 0x4eb9, ): # print("0x%x" % i, "0x%x" % cx.m.bu16(i)) cx.m.set_line_comment(i, "(discovered)") cx.disass(i) stop = False break if stop: break return cx def example(): m0 = mem.Stackup((FILENAME,), nextto=__file__) cx = m200_file(m0) return "R1K_M200", (cx.m,) ####################################################################### if __name__ == '__main__': import sys if len(sys.argv) == 3: mb = mem.Stackup((sys.argv[1],)) cx = m200_file(mb) listing.Listing(cx.m, fn=sys.argv[2], ncol=8, leaf_width=72) exit(0) listing.Example(example)
""" Run all examples """ from __future__ import print_function import glob, os, sys, importlib from pyreveng import code, listing if len(sys.argv) == 1: l = glob.glob("*/example*.py") else: l = glob.glob(sys.argv[1] + "/example*.py") try: os.mkdir("_output") except: pass l.sort() for i in l: j = i.split("/") k = j[1].replace(".py", "") print(j[0], k) y = importlib.import_module(j[0] + "." + k) pj, cx = y.setup() y.task(pj, cx) code.lcmt_flows(pj) listing.Listing(pj, ncol=8, fn="_output/" + pj.name + ".txt")
return cx ####################################################################### if __name__ == '__main__': import sys import glob if len(sys.argv) == 5 and sys.argv[1] == "-AutoArchaeologist": cx = r1k_experiment(sys.argv[3], sys.argv[2]) listing.Listing( cx.m, fn=sys.argv[4], ncol=6, lo=0x10, hi=cx.hi_adr, charset=False, ) exit(0) if len(sys.argv) == 2: cx = r1k_experiment(sys.argv[1], sys.argv[1]) listing.Listing( cx.m, fn="/tmp/_exp", ncol=6, lo=0x10, hi=cx.hi_adr, charset=False, )
def disassemble_file(input_file, output_file="/tmp/_", verbose=True, **kwargs): ''' Disassemble a single file ''' cx = m68020.m68020() try: ident = load_dfs_file.load_dfs_file(cx.m, input_file) except load_dfs_file.LoadError as err: if verbose: print(input_file, err) return high = 0 low = 1<<32 for i, j in cx.m.gaps(): high = max(high, j) low = min(low, i) tryout = [] tryout.append("all") kind = { 0x00000000: "kernel", 0x00010000: "fs", 0x00020000: "program", 0x00054000: "bootblock", 0x00070000: "resha", 0x80000000: "ioc", 0xe0004000: "enp100", }.get(low) print(input_file, "0x%x" % low, kind, ident) tryout.append("kind." + kind) tryout.append("ident." + ident) if kind == "ioc": # IOC consists of four quite individual parts for a in range(4): tryout.append("kind.ioc_part_%d" % a) b = 0x80000000 + 0x2000 * a i = hashlib.sha256(cx.m.bytearray(b, 0x2000)).hexdigest()[:16] tryout.append("ident." + i) contrib = [] cx.m.set_block_comment(low, "R1000.Disassembly modules:") for i in tryout: try: contrib.append(importlib.import_module(i)) except ModuleNotFoundError: cx.m.set_block_comment(low, " no " + i) print(" no", i) continue print(" import", i) cx.m.set_block_comment(low, " import " + i) for turnus in range(4): i = "round_%d" % turnus for j in contrib: k = getattr(j, i, None) if k: k(cx) listing.Listing( cx.m, fn=output_file, align_blank=True, align_xxx=True, ncol=8, lo=low, hi=high, **kwargs ) return cx
cx.m.set_block_comment(0x100, " " * 4 + i) cx.m.set_block_comment(0x100, " ") for i, j in ( ("Early macro", explain.early_macro_events), ("Late macro", explain.late_macro_events), ("Micro", explain.micro_events), ): for x, y in j.items(): cx.m.set_block_comment(x, i + " event: " + y) cx.m.set_label(x, y) return cx ####################################################################### if __name__ == '__main__': import sys import glob if len(sys.argv) == 5 and sys.argv[1] == "-AutoArchaeologist": cx = r1k_experiment(sys.argv[3], sys.argv[2]) listing.Listing(cx.m, fn=sys.argv[4], ncol=1) exit(0) cx = r1k_microcode() listing.Listing(cx.m, fn="/tmp/_ucode", ncol=1, charset=False)