Guest User

Untitled

a guest
Jun 21st, 2026
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. # SPDX-License-Identifier: MIT
  2. from m1n1.trace import Tracer
  3. from m1n1.trace.dart import DARTTracer
  4. from m1n1.utils import *
  5. from m1n1.proxyutils import RegMonitor
  6. hv.p.hv_set_time_stealing(0, 1)
  7.  
  8. # Usage
  9. #
  10. # 2023/12/25: Only tested on J293AP (AVD revision "Viola"/V3).
  11. # Should work on all baseline M1s.
  12. #
  13. # 1. Start tracer under the hypervisor
  14. #
  15. # 2. Send over the bitstream(s) to the target machine
  16. # Supported formats: .264, .265, .ivf (unless you want to add a demuxer to avid/codecs)
  17. #
  18. # 3. The tracer is purposely not activated at boot. As of 13.5, it takes ~2 HEVC runs
  19. # to get to the login screen - it's probably decoding the login screen. By "activate",
  20. # I mean the tracer variable "outdir" is NULLed s.t. the tracer will not save the
  21. # traced data and merely log the IPC transactions.
  22. #
  23. # [cpu0] [AVDTracer@/arm-io/avd] sent fw command at 0x108eb30
  24. # 00000000 00000801 00030007 0050c000 000002a8 00000003 01091a70 01091a78 00000001
  25. # 00000020 0050caa4 01091af0 01091bc0 01091c50 0050c2a4 0050c210 0050c28c 00000000
  26. # 00000040 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  27. # [cpu0] [0xfffffe00149d9984] MMIO: W.4 0x269098054 (avd[0], offset 0x1098054) = 0x108eb30
  28. # [cpu7] [0xfffffe00149d99b0] MMIO: R.4 0x269098048 (avd[0], offset 0x1098048) = 0x9
  29. #
  30. # 4. To save the trace contents, break into the hypervisor console and set
  31. #
  32. # >>> tracer.outdir = "matrix_1080X512"
  33. #
  34. # The output data dir will become 'data/[inferred codec name]/$outdir'.
  35. # The data dir will be created if it does not exist. The directory structure is
  36. # meant to look like this:
  37. #
  38. # proxyclient/
  39. # data/
  40. # h264/*
  41. # h265/*
  42. # vp9/*
  43. #
  44. # 5. After the directory name is configured, trigger avd from the target machine:
  45. #
  46. # ffmpeg -hwaccel videotoolbox -i matrix_1080X512.ivf
  47. #
  48. # Or you can access VT directly yourself with some Obj-C (but why would you want to do that..)
  49. # Though I may need to do that to test some sekrit undocumented features.
  50. # The input bitstream is hopefully the matching one as the directory name just set.
  51. #
  52. # 6. If all goes well (i.e. the bitstream is decoding on AVD), the tracer will save:
  53. #
  54. # >> ~/m1n1/proxyclient $ ls data/*
  55. # data/vp9/matrix_1080X512:
  56. # frame.2023-12-17T21:17:47.519048.00004000.bin probs.2023-12-17T21:17:47.519048.00004000.00004000.bin
  57. # frame.2023-12-17T21:17:47.578537.000bc000.bin probs.2023-12-17T21:17:47.578537.000bc000.0000c000.bin
  58. # frame.2023-12-17T21:17:47.633768.00174000.bin probs.2023-12-17T21:17:47.633768.00174000.00014000.bin
  59. # frame.2023-12-17T21:17:47.688067.0022c000.bin probs.2023-12-17T21:17:47.688067.0022c000.0001c000.bin
  60. #
  61. # The "frame" is the macOS source frame_params struct, and this directory is the
  62. # one intended to be supplied to all the avid tools (e.g. emulator, differs).
  63. # For VP9 (and presumably AV1) the tracer will additionally save the "probs" blob.
  64. # You can also bypass FairPlay encryption and save the coded bitstream, but that's
  65. # an exercise left for the reader.
  66. #
  67. # 7. Copy the data directory over to `avd/data/*` & have fun :D
  68. #
  69. # python3 avd_emu.py -i frame.2023-12-17T21:17:47.519048.00004000.bin # emulate single fp
  70. # python3 avd_emu.py -d vp9/matrix_1080X512 -a # emulate all using trace dir name
  71. # python3 tools/test.py -m vp9 -d vp9/matrix_1080X512 -e -a # test against emulated output
  72. #
  73. # Optionally save the firmware to run on the emulator via
  74. # >>> tracer.save_firmware("data/fw.bin")
  75.  
  76. import datetime
  77. import os
  78. import struct
  79. import time
  80.  
  81. class AVDTracer(Tracer):
  82. DEFAULT_MODE = TraceMode.SYNC
  83.  
  84. def __init__(self, hv, dev_path, dart_tracer, verbose=False):
  85. super().__init__(hv, verbose=verbose, ident=type(self).__name__ + "@" + dev_path)
  86. self.dev = hv.adt[dev_path]
  87. self.dart_tracer = dart_tracer
  88. self.base = self.dev.get_reg(0)[0] # 0x268000000
  89. self.p = hv.p
  90. self.u = hv.u
  91. self.dart = dart_tracer.dart
  92.  
  93. mon = RegMonitor(hv.u)
  94. AVD_REGS = [
  95. #(0x1000000, 0x4000, "unk0"),
  96. #(0x1010000, 0x4000, "dart"),
  97. #(0x1002000, 0x1000, "unk2"),
  98. (0x1070000, 0x4000, "piodma"),
  99. (0x1088000, 0x4000, "sram"),
  100. (0x108c000, 0xc000, "cmd"),
  101. #(0x1098000, 0x4000, "mbox"),
  102. #(0x10a3000, 0x1000, "unka"),
  103. (0x1100000, 0xc000, "config"),
  104. (0x110c000, 0x4000, "dma"),
  105. #(0x1400000, 0x4000, "wrap"),
  106. ]
  107. #for (offset, size, name) in AVD_REGS: mon.add(self.base + offset, size, name=name)
  108. self.mon = mon
  109.  
  110. iomon = RegMonitor(hv.u, ascii=True)
  111. iomon1 = RegMonitor(hv.u, ascii=True)
  112. def readmem_iova(addr, size, readfn=None):
  113. try:
  114. return dart_tracer.dart.ioread(0, addr, size)
  115. except Exception as e:
  116. print(e)
  117. return None
  118. iomon.readmem = readmem_iova
  119. def readmem_iova(addr, size, readfn=None):
  120. try:
  121. return dart_tracer.dart.ioread(1, addr, size)
  122. except Exception as e:
  123. print(e)
  124. return None
  125. iomon1.readmem = readmem_iova
  126. self.iomon = iomon
  127. self.iomon1 = iomon1
  128. self.state_active = False
  129. self.outdir = ""
  130.  
  131. def avd_r32(self, off): return self.p.read32(self.base + off)
  132. def avd_w32(self, off, x): return self.p.write32(self.base + off, x)
  133. def avd_r64(self, off): return self.p.read64(self.base + off)
  134. def avd_w64(self, off, x): return self.p.write64(self.base + off, x)
  135.  
  136. def start(self):
  137. self.hv.trace_range(irange(self.dev.get_reg(0)[0], self.dev.get_reg(0)[1]), mode=TraceMode.SYNC)
  138. self.hv.trace_range(irange(self.base + 0x1080000, 0x20000), False)
  139. self.hv.add_tracer(irange(self.base + 0x10a0054, 4), "avd-mbox-54", TraceMode.SYNC, self.evt_rw_hook, self.w_AVD_MBOX_0054)
  140. self.hv.add_tracer(irange(self.base + 0x10a0064, 4), "avd-mbox-64", TraceMode.SYNC, self.r_AVD_MBOX_0064, self.evt_rw_hook)
  141.  
  142. def poll(self):
  143. self.mon.poll()
  144. self.iomon.poll()
  145. self.iomon1.poll()
  146.  
  147. def evt_rw_hook(self, x):
  148. self.poll()
  149.  
  150. def w_AVD_MBOX_0054(self, x):
  151. if ((x.data >= 0x1080000) and (x.data <= 0x10a0000)):
  152. self.log("Sent fw command at 0x%x" % (x.data))
  153. self.poll()
  154. cmd = self.read_regs(self.base + x.data, 0x60)
  155. chexdump32(cmd)
  156.  
  157. opcode = struct.unpack("<I", cmd[:4])[0] & 0xf
  158. if (opcode == 0):
  159. self.log("Command start")
  160. self.state_active = True
  161. self.access_idx = 0
  162.  
  163. elif (opcode == 1):
  164. frame_params_iova = self.p.read32(self.base + x.data + 0x8)
  165. if (self.outdir) and (frame_params_iova != 0x0):
  166. t = datetime.datetime.now().isoformat()
  167. frame_params = self.dart.ioread(1, frame_params_iova, 0xbc000)
  168.  
  169. word = self.p.read32(self.base + x.data)
  170. if (word & 0x400) == 0x400: # h264
  171. name = "h264"
  172. elif (word & 0x800) == 0x800: # vp9
  173. name = "vp9"
  174. else:
  175. name = "h265"
  176. outdir = os.path.join("data", name, self.outdir)
  177. os.makedirs(outdir, exist_ok=True)
  178. open(os.path.join(outdir, f'frame.{t}.{frame_params_iova:08x}.bin'), "wb").write(frame_params)
  179.  
  180. if (word & 0x800) == 0x800: # save probs for vp9
  181. iova = [0x4000, 0xc000, 0x14000, 0x1c000][self.access_idx % 4]
  182. open(os.path.join(outdir, f'probs.{t}.{frame_params_iova:08x}.{iova:08x}.bin'), "wb").write(self.dart.ioread(0, iova, 0x4000))
  183. self.access_idx += 1
  184.  
  185. elif (opcode == 2):
  186. self.log("Command end")
  187. self.state_active = False
  188. self.access_idx = 0
  189.  
  190. def r_AVD_MBOX_0064(self, x):
  191. if ((x.data >= 0x1080000) and (x.data <= 0x10a0000)):
  192. self.log("Received fw command at 0x%x" % (x.data))
  193. cmd = self.read_regs(self.base + x.data, 0x60)
  194. chexdump32(cmd)
  195. self.poll()
  196.  
  197. def read_regs(self, addr, size):
  198. scratch = self.u.malloc(size)
  199. p.memcpy32(scratch, addr, size)
  200. return self.p.iface.readmem(scratch, size)
  201.  
  202. def read_iova(self, start, end, stream=0):
  203. data = b''
  204. for i in range((end - start) // 0x4000):
  205. try:
  206. d = self.dart_tracer.dart.ioread(stream, start + (i * 0x4000), 0x4000)
  207. except:
  208. d = b'\0' * 0x4000
  209. data += d
  210. return data
  211.  
  212. def save_firmware(self, path="fw.bin"):
  213. firmware = self.read_regs(self.base + 0x1080000, 0x10000)
  214. open(path, "wb").write(firmware)
  215.  
  216. p.pmgr_adt_clocks_enable('/arm-io/dart-avd0')
  217. p.pmgr_adt_clocks_enable('/arm-io/avd0')
  218. dart_tracer = DARTTracer(hv, "/arm-io/dart-avd0", verbose=0)
  219. dart_tracer.start()
  220. dart = dart_tracer.dart
  221. tracer = AVDTracer(hv, '/arm-io/avd0', dart_tracer, verbose=3)
  222. tracer.start()
Advertisement
Add Comment
Please, Sign In to add comment