import serial, os, struct, sys, time, json, os.path, lzma, functools from proxy import * from tgtypes import * import malloc, adt def load_registers(): data = json.load(open(os.path.join(os.path.dirname(__file__), "..", "tools", "arm_regs.json"))) for reg in data: yield reg["name"], reg["enc"] globals().update(dict(load_registers())) class ProxyUtils(object): def __init__(self, p, heap_size=1024 * 1024 * 1024): self.iface = p.iface self.proxy = p self.base = p.get_base() self.ba_addr = p.get_bootargs() self.ba = self.iface.readstruct(self.ba_addr, BootArgs) # We allocate a 128MB heap, 128MB after the m1n1 heap, without telling it about it. # This frees up from having to coordinate memory management or free stuff after a Python # script runs, at the expense that if m1n1 ever uses more than 128MB of heap it will # clash with Python (m1n1 will normally not use *any* heap when running proxy ops though, # except when running very high-level operations like booting a kernel, so this should be # OK). self.heap_size = heap_size try: self.heap_base = p.heapblock_alloc(0) except ProxyRemoteError: # Compat with versions that don't have heapblock yet self.heap_base = (self.base + ((self.ba.top_of_kernel_data + 0xffff) & ~0xffff) - self.ba.phys_base) self.heap_base += 128 * 1024 * 1024 # We leave 128MB for m1n1 heap self.heap_top = self.heap_base + self.heap_size self.heap = malloc.Heap(self.heap_base, self.heap_top) self.proxy.heap = self.heap self.malloc = self.heap.malloc self.memalign = self.heap.memalign self.free = self.heap.free self.code_buffer = self.malloc(0x10000) self.adt_data = None self.adt = LazyADT(self) def mrs(self, reg, silent=False, call=None): if call is None: call = self.proxy.call op0, op1, CRn, CRm, op2 = reg op = (((op0 & 1) << 19) | (op1 << 16) | (CRn << 12) | (CRm << 8) | (op2 << 5) | 0xd5300000) func = struct.pack("