mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
235 lines
9.3 KiB
Python
235 lines
9.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Run or verify original Falcon boot parent (_nv028470rm) vectors in isolated private RAM."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
from _falcon_acr_oracle_elf import ( # noqa: E402
|
|
GPU,
|
|
STUB,
|
|
TEXT,
|
|
OracleUnicornHarness,
|
|
discover_rm_object,
|
|
load_relocated_rm_object,
|
|
write_or_check_json,
|
|
)
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Run or verify Falcon boot parent (_nv028470rm) oracle."
|
|
)
|
|
parser.add_argument("--rm-object", type=Path, default=None)
|
|
parser.add_argument("--check", action="store_true")
|
|
opt = parser.parse_args(argv)
|
|
|
|
loaded = load_relocated_rm_object(discover_rm_object(opt.rm_object))
|
|
falcon = GPU + 0x810000
|
|
selector = GPU + 0x820000
|
|
clock = GPU + 0x830000
|
|
bootmeta = GPU + 0x840000
|
|
bootcode = GPU + 0x850000
|
|
descriptor = GPU + 0x860000
|
|
image = GPU + 0x870000
|
|
sec2 = GPU + 0x880000
|
|
vm = GPU + 0x890000
|
|
va = 0x12340000
|
|
state = {"outcome": "success", "dmem_status": 0, "events": [], "copied_descriptors": []}
|
|
|
|
def on_custom(harness, emu, name):
|
|
x86 = harness.x86
|
|
if name == "read_register":
|
|
offset = emu.reg_read(x86.UC_X86_REG_RCX) & 0xFFFFFFFF
|
|
value = {4: 0xABCDE098, 0x108: 256}.get(offset, 0)
|
|
state["events"].append([5, offset, value])
|
|
harness.ret(value)
|
|
return True
|
|
if name == "write_register":
|
|
state["events"].append(
|
|
[
|
|
4,
|
|
emu.reg_read(x86.UC_X86_REG_RCX) & 0xFFFFFFFF,
|
|
emu.reg_read(x86.UC_X86_REG_R8) & 0xFFFFFFFF,
|
|
]
|
|
)
|
|
harness.ret()
|
|
return True
|
|
if name == "clock_gate":
|
|
state["events"].append([1])
|
|
harness.ret()
|
|
return True
|
|
if name == "reset_hold":
|
|
state["events"].append([2, emu.reg_read(x86.UC_X86_REG_RDX) & 255])
|
|
harness.ret()
|
|
return True
|
|
if name == "delay":
|
|
state["events"].append([3, emu.reg_read(x86.UC_X86_REG_RDI)])
|
|
harness.ret()
|
|
return True
|
|
if name == "create_vm":
|
|
state["events"].append([6])
|
|
if state["outcome"] != "vm_error":
|
|
emu.mem_write(falcon + 0x648, struct.pack("<Q", vm))
|
|
harness.ret(7 if state["outcome"] == "vm_error" else 0)
|
|
return True
|
|
if name == "map_image":
|
|
state["events"].append([7])
|
|
if state["outcome"] != "map_error":
|
|
emu.mem_write(emu.reg_read(x86.UC_X86_REG_R8), struct.pack("<Q", va))
|
|
harness.ret(8 if state["outcome"] == "map_error" else 0)
|
|
return True
|
|
if name == "bind_instance":
|
|
state["events"].append([8, emu.reg_read(x86.UC_X86_REG_RDX) & 0xFFFFFFFF])
|
|
harness.ret()
|
|
return True
|
|
if name == "dmem_copy":
|
|
address = emu.reg_read(x86.UC_X86_REG_RCX)
|
|
state["copied_descriptors"].append(bytes(emu.mem_read(address, 0x54)).hex())
|
|
state["events"].append([9, state["dmem_status"]])
|
|
harness.ret(state["dmem_status"])
|
|
return True
|
|
if name == "start":
|
|
state["events"].append([10])
|
|
harness.ret()
|
|
return True
|
|
if name == "wait_finish":
|
|
status = 0x65 if state["outcome"] == "timeout" else 0
|
|
state["events"].append([11, status])
|
|
if status == 0:
|
|
for off in (0x628, 0x638, 0x3B1):
|
|
emu.mem_write(falcon + off, bytes(1))
|
|
emu.mem_write(falcon + 0x648, bytes(8))
|
|
harness.ret(status)
|
|
return True
|
|
return False
|
|
|
|
harness = OracleUnicornHarness(loaded, custom_handler=on_custom)
|
|
for address in (falcon, selector, clock, bootmeta, bootcode, descriptor, image, sec2, vm):
|
|
harness.uc.mem_map(address, 0x2000)
|
|
harness.uc.mem_write(selector + 0x10, struct.pack("<H", 35))
|
|
harness.uc.mem_write(selector + 0x30, struct.pack("<H", 2))
|
|
harness.call("_nv005876rm", [falcon, selector])
|
|
harness.uc.mem_write(clock + 0x198, struct.pack("<Q", STUB + 0x9000))
|
|
harness.hooks[STUB + 0x9000] = "clock_gate"
|
|
harness.uc.mem_write(GPU + 0x2308, struct.pack("<Q", sec2))
|
|
harness.uc.mem_write(sec2 + 0xF78, struct.pack("<QQ", bootmeta, bootcode))
|
|
harness.uc.mem_write(
|
|
bootmeta, (ROOT / "analysis/sec2-580/11401d8-12.decoded").read_bytes()
|
|
)
|
|
harness.uc.mem_write(
|
|
bootcode, (ROOT / "analysis/sec2-580/11401d8-23.decoded").read_bytes()
|
|
)
|
|
for fn, hook_name in (
|
|
("_nv053907rm", "read_register"),
|
|
("_nv053912rm", "write_register"),
|
|
("_nv028467rm", "reset_hold"),
|
|
("_nv049598rm", "delay"),
|
|
("_nv028420rm", "create_vm"),
|
|
("_nv028379rm", "map_image"),
|
|
("_nv028759rm", "bind_instance"),
|
|
("_nv028457rm", "dmem_copy"),
|
|
("_nv028764rm", "start"),
|
|
("_nv028781rm", "wait_finish"),
|
|
("_nv046747rm", "diagnostic_leaf"),
|
|
):
|
|
harness.hooks[TEXT + harness.defined[fn]] = hook_name
|
|
|
|
expected_statuses = {"success": 0, "timeout": 0x65, "vm_error": 7, "map_error": 8}
|
|
cases = []
|
|
for privileged in (False, True):
|
|
for flag_a in (False, True):
|
|
for flag_b in (False, True):
|
|
for dmem_status in (0, 0x1F):
|
|
for outcome in ("success", "timeout", "vm_error", "map_error"):
|
|
state["dmem_status"] = dmem_status
|
|
state["outcome"] = outcome
|
|
state["events"] = []
|
|
state["copied_descriptors"] = []
|
|
for off, value in (
|
|
(0x3A5, privileged),
|
|
(0x3A7, flag_a),
|
|
(0x3AA, flag_b),
|
|
(0x581, 1),
|
|
(0x582, 0),
|
|
(0x628, 0),
|
|
(0x638, 0),
|
|
(0x3B1, 0),
|
|
):
|
|
harness.uc.mem_write(falcon + off, bytes([value]))
|
|
harness.uc.mem_write(falcon + 0x648, bytes(8))
|
|
harness.uc.mem_write(GPU + 0x1EA0, struct.pack("<Q", clock))
|
|
desc_buf = bytearray(0x54)
|
|
struct.pack_into("<I", desc_buf, 0x20, 1)
|
|
struct.pack_into("<I", desc_buf, 0x40, 0x2F00)
|
|
harness.uc.mem_write(descriptor, bytes(desc_buf))
|
|
status = harness.call(
|
|
"_nv028470rm", [GPU, falcon, descriptor, 1, 2, 1, 1, 1, image, 0]
|
|
)
|
|
if status != expected_statuses[outcome]:
|
|
raise SystemExit(
|
|
f"Unexpected status {status:#x} for outcome {outcome!r}"
|
|
)
|
|
fields = {
|
|
hex(off): int.from_bytes(harness.uc.mem_read(falcon + off, 1), "little")
|
|
for off in (0x3A5, 0x3A7, 0x3AA, 0x628, 0x638, 0x3B1)
|
|
}
|
|
cases.append(
|
|
{
|
|
"privileged": privileged,
|
|
"flag_a": flag_a,
|
|
"flag_b": flag_b,
|
|
"dmem_status": dmem_status,
|
|
"outcome": outcome,
|
|
"status": status,
|
|
"events": list(state["events"]),
|
|
"descriptor": bytes(harness.uc.mem_read(descriptor, 0x54)).hex(),
|
|
"copied_descriptors": list(state["copied_descriptors"]),
|
|
"fields": fields,
|
|
"vm_retained": int.from_bytes(
|
|
harness.uc.mem_read(falcon + 0x648, 8), "little"
|
|
)
|
|
!= 0,
|
|
}
|
|
)
|
|
|
|
report = {
|
|
"artifact_sha256": loaded["digest"],
|
|
"original_parent": "028470",
|
|
"original_instruction_helpers": [
|
|
"028502 cached bootloader access",
|
|
"028746 interrupt-mask helper",
|
|
"028672/674 register wrappers",
|
|
"028523/028579 fullIMEMtransfer",
|
|
],
|
|
"required_scripted_providers": [
|
|
"clock+198",
|
|
"reset028467",
|
|
"delay049598",
|
|
"VM028420",
|
|
"imageMap028379",
|
|
"bindInstance028759",
|
|
"DMEM028457",
|
|
"start028764",
|
|
"wait+teardown028781",
|
|
"physicalMMIO leafservices",
|
|
],
|
|
"cases": cases,
|
|
"physical_gpu_access": False,
|
|
"authenticated_hardware_boot_proven": False,
|
|
}
|
|
write_or_check_json(
|
|
ROOT / "reports/gpu-falcon-boot-parent-oracle.json",
|
|
report,
|
|
opt.check,
|
|
"Falcon boot parent oracle report",
|
|
)
|
|
print(len(cases), "actual028470 parent cases")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|