"""Per-component memory layouts. Each TA runs in its own address space (HDCP and DTM are both based at 0x400100 on real hardware, so they cannot share a Unicorn instance). Layout choices are anchored to hardware observations where available: * ASD image spans 0xD00100..0xD33100, but live code touches 0xD42890 (callback vtable) and faults at 0xD5B000 (stack top) -> ASD RAM must extend to ~0xD5B000. We map 0xD00000..0xD60000 and place SP at 0xD5B000. * HDCP/DTM stack addresses are not known from hardware; we pick a region below the image and record it here so traces stay interpretable. """ from dataclasses import dataclass, field PAGE = 0x1000 def align_down(x, a=PAGE): return x & ~(a - 1) def align_up(x, a=PAGE): return (x + a - 1) & ~(a - 1) @dataclass class Region: name: str addr: int size: int perms: int = 7 # UC_PROT_ALL @property def end(self): return self.addr + self.size def contains(self, a): return self.addr <= a < self.end def __repr__(self): return f"<{self.name} {self.addr:#010x}..{self.end:#010x} ({self.size:#x})>" @dataclass class ComponentMap: name: str image_base: int regions: list = field(default_factory=list) stack_top: int = 0 scratch: int = 0 # emulator-owned scratch allocator base scratch_size: int = 0 def add(self, name, addr, size, perms=7): self.regions.append(Region(name, align_down(addr), align_up(size + (addr - align_down(addr))) , perms)) return self.regions[-1] def find(self, a): for r in self.regions: if r.contains(a): return r return None # Shared pseudo-VRAM window: the host-visible command/response buffer that the # driver writes and the TA reads (the real one lives in VRAM via the GFX ring). HOST_SHM_BASE = 0x2000_0000 HOST_SHM_SIZE = 0x0001_0000 # Magic return address used to detect "function returned" in run_function(). MAGIC_RET = 0x00FF_0000 # PSP MMIO aperture (C2PMSG mailboxes etc. at 0x3010900+). TAs genuinely read # these; leaving it unmapped makes benign register reads look like OOB faults # (found by fuzzing: ASD cmd 0x207 reads 0x30109EC = C2PMSG_59). MMIO_BASE = 0x0301_0000 MMIO_SIZE = 0x0001_0000 def _common(cm: ComponentMap, stack_base, stack_size, scratch_base, scratch_size): cm.add("stack", stack_base, stack_size) cm.stack_top = stack_base + stack_size cm.add("scratch", scratch_base, scratch_size) cm.scratch, cm.scratch_size = scratch_base, scratch_size cm.add("host_shm", HOST_SHM_BASE, HOST_SHM_SIZE) cm.add("mmio", MMIO_BASE, MMIO_SIZE) cm.add("magic_ret", MAGIC_RET, PAGE) return cm def hdcp_map(): cm = ComponentMap("hdcp", 0x400100) # TA header +0x0c = 0x408820 marks the end of the TA data region, so the # image window must extend past 0x408000 (found by fuzzing: cmd 0x11 reads # 0x4081dc legitimately). cm.add("image", 0x400000, 0xA000) # code+data+session table+bss return _common(cm, 0x4F0000, 0x10000, 0x600000, 0x20000) def dtm_map(): cm = ComponentMap("dtm", 0x400100) cm.add("image", 0x400000, 0x3000) return _common(cm, 0x4F0000, 0x10000, 0x600000, 0x10000) def asd_map(): cm = ComponentMap("asd", 0xD00100) # image + the extra RAM the live TA uses (vtable 0xD42890, stack ~0xD5B000) cm.add("image", 0xD00000, 0x34000) cm.add("ram", 0xD34000, 0x17000) # .. 0xD4B000 return _common(cm, 0xD4B000, 0x10000, 0xD70000, 0x20000) # SP top = 0xD5B000 def sys_drv_map(): cm = ComponentMap("sys_drv", 0xE00100) # TA header: +0x04 base 0xE00000, +0x1C bss 0xE27000, +0x0C data end 0xEB6020. # The old 0x28000 window stopped at 0xE28000 and put stack/scratch at # 0xE40000/0xE60000 -- INSIDE the real data region, so sub_E0CA2C (init) # faulted clearing 0xE575D4 and the RPC entry's init flag never got set. cm.add("image", 0xE00000, 0xB7000) # .. 0xEB7000 # generous scratch: the SVC 0x5D allocator hands out many blocks during init return _common(cm, 0xEC0000, 0x10000, 0xEE0000, 0x100000) def gcp_map(): cm = ComponentMap("gcp", 0x400100) # Layout MEASURED, not guessed: the firmware's own loader (bl_load_ta_image) was observed # placing GCP in psp_emu/harness/ta_layout_rule.py and confirmed on silicon # (fw/FINDING-GCP-FE09-...md Addendum 5): # code 0x400000-0x407000 R-X | data 0x407000-0x417000 RW | guard 0x417000 # stack 0x418000-0x41c000 (SP0 = 0x41c000) | guard | heap 0x41d000-0x421000 # The image window must cover code+data (TA header +0x0C = 0x416e64 -> 0x417000). cm.add("image", 0x400000, 0x17000) # .. 0x417000 cm.add("ta_heap", 0x41D000, 0x4000) # the real heap, per the loader # stack base/size are the MEASURED ones (SP top = 0x41c000). "scratch" is a HARNESS # convenience arena for buffers we allocate, so it is placed well clear of the TA layout # and sized generously rather than mirroring the 0x4000 TA heap. return _common(cm, 0x418000, 0x4000, 0x600000, 0x40000) def ras_map(): cm = ComponentMap("ras", 0x400100) # TA header: base 0x400000, data 0x405000, image_end 0x406000, bss end 0x4060F0 cm.add("image", 0x400000, 0x7000) # .. 0x407000 return _common(cm, 0x4F0000, 0x10000, 0x600000, 0x20000) def sos_map(): """sOS / bootloader layout. sos2.elf is a single flat PT_LOAD at va=0, size 0x18280, so IDB addresses are file offsets. But the *boot task* (CODE2, file 0x12000 onward) really executes at VA 0x200000 -- `sos_mmu_init` maps VA 0x200000 to bl_entry, and CODE2's literal pools reference VAs like 0x206068 (file 0x18068). Hence the relation recorded in psp_out/ATTACK-SURFACE.md: VA = FA + 0x1EE000. So we map the flat image at 0 *and* alias the CODE2 tail at 0x200000, which is what makes the boot task's literal-pool references resolve. Host-facing ring code (tee_cmd_dispatch FA 0x147C4 -> VA 0x2027C4) lives in that alias. Extra windows anchored to code we have read: * 0xB0000..0xC0000 -- sos_pcb_table_init maps 0xB0000; the VA-region table sits at 0xB2000 and the PCB array at 0xB2408. * 0x1000000..0x4000000 is the SMN/Syshub AXI aperture; not backed here (a harness can map it on demand) to keep the instance small. """ cm = ComponentMap("sos", 0x0) cm.add("image", 0x0, 0x40000) cm.add("pcb", 0xB0000, 0x10000) # varegion tbl 0xB2000 + PCB 0xB2408 cm.add("code2", 0x200000, 0x10000) # boot-task alias, VA = FA + 0x1EE000 # Device MMIO block. Wider than the shared MMIO_BASE window because sOS # touches it from the very first instruction of tee_cmd_dispatch # (bl_debug_gpio_pulse reads 0x32001E0). Extents follow the published # layout: CCP queues at 0x3001000 + q*0x1000, SMN slot registers at # 0x3220000, Syshub slot registers at 0x3230000. # (tiled around the shared MMIO_BASE window that _common adds at 0x3010000) cm.add("mmio_lo", 0x3000000, 0x10000) cm.add("mmio_hi", 0x3020000, 0x220000) return _common(cm, 0x40000, 0x10000, 0x300000, 0x80000) SOS_VA_BIAS = 0x1EE000 # VA = file-address + bias, for CODE2 (FA >= 0x12000) def sos_va(fa): """Translate an sos2.elf IDB (file) address to the VA it runs at.""" return fa + SOS_VA_BIAS if fa >= 0x12000 else fa MAPS = {"hdcp": hdcp_map, "dtm": dtm_map, "asd": asd_map, "sys_drv": sys_drv_map, "ras": ras_map, "sos": sos_map, "gcp": gcp_map}