Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Start new machines' SEP from OpenSEP instead of AVPSEPBooter

Jackyb537d3fc2f5a
diff --git a/AGENTS.md b/AGENTS.md
index 4bbedc3c..1969a445 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -57,6 +57,7 @@ The `VPhone` scheme puts host programs in `VPhone.bundle/Contents/MacOS` and eve
### Key Patterns
- New machines boot **OpenAVB**, not Apple's AVPBooter: `VPhone.bundle/Contents/Resources/OpenAVB/OpenAVB.bin`, checked in prebuilt under `VPhoneExecutable/VPhoneVirtualization/Resources/OpenAVB/` from a pinned OpenAVB commit plus any patches in `patches/` beside it (none at present). Rebuild or check it only with `Build/UpdateOpenAVB.sh` (Homebrew LLVM/LLD, outside the repo). `fw patch` skips AVPBooter on an OpenAVB machine; `vphone-cli restore` uploads the iBSS identity first (`MobileRestoreCore/Recovery/openavb.c`); `vm config --rom` switches ROM. See `Research/Firmware/openavb_boot_rom.md`.
+- New machines start the SEP from **OpenSEP**, not Apple's AVPSEPBooter: `VPhone.bundle/Contents/Resources/OpenSEP/OpenSEP.bin`, checked in prebuilt under `VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/` from a pinned OpenSEP commit (`vresearch1` platform) plus any patches in `patches/` beside it. Rebuild or check it only with `Build/UpdateOpenSEP.sh`. Neither SEP ROM is patched; `vm config --seprom` switches it without a restore. See `Research/Firmware/opensep_sep_rom.md`.
- `vphone-cli` is the unentitled entry point. `VPhoneGuestLaunchPlanner` resolves `vphone-vm` beside the running executable, checks its entitlements, probes AMFI, and reports the exact allowlist command on refusal. It never obtains root itself.
- `vphone-escalator` manages AMFI cdhash admission only. It writes amfid heap state, not executable code. Root authorization belongs to the user or to `vphone-launchpad`; neither `vphone-cli` nor the bundle has an SMJobBless or sudo password flow.
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index 2bd5f621..827e74a0 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -3209,3 +3209,16 @@ USB device left running, SEP not ready after a warm reset) were carried as
patches at first and are now upstream (owngoal-dev/OpenAVB#1 and #2).
`Firmware/openavb_boot_rom.md` describes
each one and how it was found.
+
+## OpenSEP replaces AVPSEPBooter on new machines (2026-10-18)
+
+New machines start the SEP from OpenSEP (`Resources/OpenSEP/OpenSEP.bin`,
+built from `owngoal-dev/OpenSEP` at `39ff3d7` for its `vresearch1` platform,
+with no carried patches) instead of Virtualization.framework's
+`AVPSEPBooter.vresearch1.bin`. Apple's SEP ROM was never patched and OpenSEP is
+not either, so no patch, preset or plan changes: there is no SEP ROM component
+in `fw patch`. The SEP firmware (`sep-firmware.vphone600.RELEASE.im4p`, sent as
+`RestoreSEP` and loaded by iBoot) is unchanged and unpatched. Machines created
+before keep Apple's SEP ROM; `vm config --seprom opensep|apple` switches one
+without a restore. `Firmware/opensep_sep_rom.md` has the contract and the
+validation.
diff --git a/Research/Firmware/firmware_manifest_and_origins.md b/Research/Firmware/firmware_manifest_and_origins.md
index 0c62ccc0..5a24aead 100644
--- a/Research/Firmware/firmware_manifest_and_origins.md
+++ b/Research/Firmware/firmware_manifest_and_origins.md
@@ -74,6 +74,7 @@ Key distinctions:
| Component | Source Identity | File | Patches Applied |
| ------------- | ----------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------ |
| **AVPBooter** | PCC vresearch1 | `AVPBooter*.bin` (vm dir) | DGST validation bypass (`mov x0, #0`); none on machines booting OpenAVB |
+| **SEP ROM** | vresearch1 | `AVPSEPBooter*.bin`, or `OpenSEP.bin` on new machines (vm dir) | Not patched (`opensep_sep_rom.md`) |
| **iBSS** | PROD (vresearch101ap release) | `Firmware/dfu/iBSS.vresearch101.RELEASE.im4p` | Serial labels + image4 callback bypass |
| **iBEC** | PROD (vresearch101ap release) | `Firmware/dfu/iBEC.vresearch101.RELEASE.im4p` | Serial labels + image4 callback + boot-args |
| **LLB** | PROD (vresearch101ap release) | `Firmware/all_flash/LLB.vresearch101.RELEASE.im4p` | Serial labels + image4 callback + boot-args + rootfs + panic (6 patches) |
diff --git a/Research/Firmware/opensep_sep_rom.md b/Research/Firmware/opensep_sep_rom.md
new file mode 100644
index 00000000..42ce1f3e
--- /dev/null
+++ b/Research/Firmware/opensep_sep_rom.md
@@ -0,0 +1,117 @@
+# OpenSEP SEP ROM
+
+New machines start their SEP core from
+[OpenSEP](https://github.com/owngoal-dev/OpenSEP), an open-source SEP boot ROM
+compatible with AVPSEPBooter, instead of Apple's `AVPSEPBooter.vresearch1.bin`
+from Virtualization.framework. It goes with [OpenAVB](openavb_boot_rom.md) on
+the AP side: with both, nothing Apple wrote runs before iBoot and SEPOS. This
+page is vphone's side of OpenSEP's host contract (`docs/external-contract.md`
+in its repository): what the ROM expects, what vphone does about it, and how it
+was validated.
+
+## Where the ROM lives
+
+| What | Where |
+| --- | --- |
+| Checked-in image and licence | `VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/` |
+| Rebuild and check | `VPhoneExecutable/VPhoneVirtualization/Build/UpdateOpenSEP.sh` (Homebrew LLVM and LLD, outside the repository) |
+| In the bundle | `VPhone.bundle/Contents/Resources/OpenSEP/OpenSEP.bin`, admitted by `ValidateBundle.sh` |
+| In a machine | `<vm>/OpenSEP.bin`, named by `romImages.avpSEPBooter` in `config.plist` |
+| Recognition | `VPhoneSEPROM` (VPhoneCoreKit): an image containing `AVPSEPBooter-` (Apple's version string) is Apple's, anything else OpenSEP |
+
+OpenSEP prints nothing and carries no string of its own, so recognition is the
+reverse of OpenAVB's: Apple's ROM is the one with a marker. `ValidateBundle.sh`
+refuses a bundled `OpenSEP.bin` that carries Apple's marker or exceeds the SEP
+ROM window (`0x29000` bytes, the size of Apple's image).
+
+`vm new` copies the bundle's OpenSEP (`VPhoneBundleOperations.defaultSEPROMSource`).
+A machine cloned from a template gets the creating bundle's OpenSEP too
+(`VPhoneVirtualMachineCreator.cloneFromTemplate`), as it gets OpenAVB: the SEP
+ROM is not part of a template's key, and a machine changes SEP ROM without a
+restore (see Validation). `vm new --seprom apple` still creates a machine on
+Apple's ROM, and
+`vm config --seprom opensep|apple|<file>` switches an existing machine from its
+next start; the ROM it replaces stays in the folder. `vm info` prints
+`seprom:` and its JSON carries `sepROM`. The PCC GPU recovery machine
+(`VPhonePCCGPURecovery`) keeps both of Apple's ROMs.
+
+## The build
+
+`make PLATFORM=vresearch1 build/OpenSEP.bin` at the pinned commit, with no
+patches. `vresearch1` is OpenSEP's default and the row of its handoff table
+that stands in for Virtualization.framework's AVPSEPBooter: the SEPOS runtime
+is entered at the runtime region's base (`0x06000000`) with the handoff pointer
+in `x0`, `x1` zero, and the firmware building its own translation tables. The
+other row, `t8110`, is for the iphone-cli QEMU machine and is not built here.
+OpenSEP's `docs/platforms.md` cites the AVPSEPBooter addresses each value of
+the `vresearch1` row was read from (macOS 26's image, SHA-256 `86d008eb…`, the
+one this Mac's Virtualization.framework ships).
+
+The image is 147496 bytes and builds byte for byte the same from a fresh clone
+(`UpdateOpenSEP.sh` with no argument).
+
+## What changes on the boot chain
+
+Nothing vphone patches. Apple's AVPSEPBooter was never patched either, and
+`fw patch` has no SEP ROM component. The SEP firmware itself
+(`sep-firmware.vphone600.RELEASE.im4p`, sent as `RestoreSEP` during a restore
+and loaded by iBoot at boot, `sepfw-load-at-boot = 1`) stays as it was.
+
+- **Mailbox.** OpenSEP serves the same mailbox protocol as AVPSEPBooter: the
+ startup events (`0xd2`), the session opcodes the AP's ROM and iBoot send,
+ and opcode `0x06`, with which the AP kernel's `AppleSEPManager` publishes
+ the SEP firmware's dart-sep IOVA. OpenAVB's SEP bootstrap
+ (`OpenBootROM: SEP bootstrap complete`, and in DFU `DFU SEP finalized`) talks
+ to it unchanged.
+- **Research seed.** OpenSEP answers the seed requests (opcodes `0x03`/`0x04`,
+ `0x21`/`0x22`) with the constant `0xdeadbeef` pattern
+ (`sep_fill_research_seed_pattern`, `src/protocol/protocol.c`), so the seed is
+ the same on every machine. The per-machine secret is the one SEPOS keeps in
+ `SEPStorage` ([machine identity](../Host/machine_identity_and_clone.md)),
+ which neither ROM touches.
+- **Image trust.** OpenSEP evaluates the SEP firmware manifest's certificate
+ chain against the Apple DDI Secure Boot Root CA - G1 it carries, logs the
+ result and goes on: its build does not set
+ `OPEN_SECURE_ROM_ENFORCE_IMAGE_TRUST`. The `vphone600` firmware is not
+ encrypted, so OpenSEP's table of iPhone14,4 firmware keys is never used here.
+- **Warm boot.** OpenSEP's reset path checks a warm-boot word at
+ `0x2_3D2E_4804` that only the t8110 runtime writes. OpenSEP's own docs say
+ the path is unexercised on `vresearch1`; guest reboots, which restart the SEP
+ with the AP, come back (see Validation), so the SEP starts cold there.
+
+## Validation
+
+All on 2026-10-18, iPhone17,3 iOS 26.6.2 / cloudOS 26.4 (23E5207q), on a
+bundle with this change installed beside the default (`--keep-default`).
+
+- Restore: a machine created with `vm create --no-template` (`OpenAVB.bin` and
+ `OpenSEP.bin` in the folder) restores through OpenAVB's DFU:
+ `Sending RestoreSEP (3299082 bytes)`, `AppleSEPBooter: SEP accepted IMG4` in
+ the restore ramdisk, `Restore Finished`. CFW installs. The pre-CFW boot after
+ the restore panics with `initproc failed to start … libSystem.B.dylib`, as
+ it does on Apple's ROMs (the guest has no CFW yet).
+- Boot and setup: the setup boot passes (settle 62 s, its reboot 9 s); the
+ guest unlocks and SpringBoard runs.
+- Guest reboots: `system.reboot` (`force: true`) comes back in 12, 12 and 13 s,
+ three times out of three, with no `SEP RX timeout` or SEP panic on the
+ console.
+- Switching: that machine, restored on OpenSEP, boots on Apple's SEP ROM after
+ `vm config --seprom apple` (unlocks, SpringBoard runs) and again on OpenSEP
+ after `--seprom opensep`. A machine cloned from a template built on Apple's
+ SEP ROM boots on OpenSEP after `--seprom opensep` and comes back from a
+ reboot in 12 s. With the template clone change, a fresh clone of that
+ template gets `OpenSEP.bin` (`vm info --json`: `"sepROM":"opensep"`) and
+ passes its first-boot check.
+- Unit tests: `SEPROMTests` (VPhoneCoreKitTests) pin the recognition, the
+ naming in a new machine, the switch in both directions and the report, and
+ check the framework's image is recognised as Apple's.
+
+To check the image after an update: `UpdateOpenSEP.sh` with no argument
+rebuilds the pinned commit and compares byte for byte. To check a machine:
+`vphone-cli vm info <vm>` prints `seprom:`.
+
+## Upstream
+
+No patches are carried. If one is needed, it goes in
+`Resources/OpenSEP/patches/` as a `git format-patch` file until upstream takes
+it; `UpdateOpenSEP.sh` refuses a patch that no longer applies.
diff --git a/Research/Host/machine_identity_and_clone.md b/Research/Host/machine_identity_and_clone.md
index ceb8c326..adbc26d2 100644
--- a/Research/Host/machine_identity_and_clone.md
+++ b/Research/Host/machine_identity_and_clone.md
@@ -77,7 +77,9 @@ The boot chain already ignores the ECID in its tickets
TXM and SPTM from Preboot through the same callback.
- The SEP firmware is checked by the unpatched AVPSEPBooter, which still logs
`AppleSEPBooter: SEP accepted IMG4` under a new ECID: the virtual SEP ROM
- does not enforce it.
+ does not enforce it. OpenSEP, which new machines start the SEP from, logs its
+ chain evaluation and does not enforce it either
+ ([OpenSEP SEP ROM](../Firmware/opensep_sep_rom.md)).
A new-identity clone's serial log shows the new ECID in `USB_SERIAL_NUMBER`.
Its error and failure lines, normalized, are identical to those of a clone that
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
index 26af9301..ddd516f1 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Host/VPhonePCCGPURecovery.swift
@@ -35,15 +35,15 @@ enum VPhonePCCGPURecovery {
// The restore runs over the virtual USB device, so the machine needs no
// NIC. With the default nat one, the Mac's shared vmnet network and its
// DHCP service came up between the end of the restore and the stop.
- // Apple's ROM: this restores Apple-signed PCC firmware, which OpenAVB has
- // not been tried with.
+ // Apple's ROMs: this restores Apple-signed PCC firmware, which OpenAVB
+ // and OpenSEP have not been tried with.
let vm = try VPhoneBundleOperations.create(.init(
name: name,
cpuCount: 8,
memoryMB: 8192,
diskSizeGB: 64,
romSource: VPhoneBootROM.appleSource,
- sepromSource: VPhoneBundleOperations.defaultSEPROMSource(),
+ sepromSource: VPhoneSEPROM.appleSource,
networkConfig: .init(mode: .off, macAddress: ""),
), in: library)
var diskMayBeAttached = false
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneBootROMOption.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneBootROMOption.swift
index d6862037..7318ed6c 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneBootROMOption.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneBootROMOption.swift
@@ -53,3 +53,36 @@ enum VPhoneBootROMOption {
return "\(kind?.displayName ?? "unreadable") (\(name))"
}
}
+
+// MARK: - --seprom
+
+/// `--seprom` of `vm new` and `vm config`: `opensep`, `apple`, or a ROM file.
+enum VPhoneSEPROMOption {
+ static var help: ArgumentHelp {
+ "SEP ROM: opensep (the bundle's OpenSEP, the default for new machines), apple (Virtualization.framework's AVPSEPBooter), or a ROM file"
+ }
+
+ /// What `vm new` copies into the new machine.
+ static func source(for argument: String?) -> URL {
+ switch argument?.lowercased() {
+ case nil, VPhoneSEPROM.openSEP.rawValue: VPhoneSEPROM.openSEPSource()
+ case VPhoneSEPROM.apple.rawValue: VPhoneSEPROM.appleSource
+ default: URL(fileURLWithPath: argument ?? "")
+ }
+ }
+
+ /// Makes `argument` the SEP ROM of an existing machine. Neither ROM is
+ /// patched, so `apple` is a fresh copy from the framework either way.
+ static func install(_ argument: String, in bundle: VPhoneBundle) throws -> VPhoneBundle {
+ try VPhoneBundleOperations.installSEPROM(from: source(for: argument), in: bundle)
+ }
+
+ /// `OpenSEP (OpenSEP.bin)`, for listings.
+ static func describe(_ bundle: VPhoneBundle) -> String {
+ guard let name = bundle.manifest.romImages?.avpSEPBooter else {
+ return "Virtualization.framework built-in"
+ }
+ let kind = VPhoneSEPROM.kind(ofMachineAt: bundle.url)
+ return "\(kind?.displayName ?? "unreadable") (\(name))"
+ }
+}
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift
index 7ca1e33d..54bcee57 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCommand.swift
@@ -164,6 +164,7 @@ struct VPhoneVirtualMachineInfoCommand: ParsableCommand {
print("unlock: \(report.unlocksAtStartup ? "on" : "off")")
print("location: \(report.syncsHostLocation ? "on" : "off")")
print("rom: \(VPhoneBootROMOption.describe(bundle))")
+ print("seprom: \(VPhoneSEPROMOption.describe(bundle))")
if let snapshots = try? VPhoneMachineSnapshots.list(of: bundle), !snapshots.isEmpty {
print("snapshots: \(snapshots.count)")
}
@@ -198,7 +199,7 @@ struct VPhoneVirtualMachineNewCommand: ParsableCommand {
@Option(name: .shortAndLong, help: "Memory (MB)") var memory: UInt64 = 8192
@Option(name: .shortAndLong, help: "Disk size (GB)") var diskSize: UInt64 = 64
@Option(name: .shortAndLong, help: VPhoneBootROMOption.help) var rom: String?
- @Option(name: .shortAndLong, help: "AVPSEPBooter ROM (default: framework built-in)") var seprom: String?
+ @Option(name: .shortAndLong, help: VPhoneSEPROMOption.help) var seprom: String?
func run() throws {
let spec = VPhoneBundleOperations.NewBundleConfiguration(
@@ -207,7 +208,7 @@ struct VPhoneVirtualMachineNewCommand: ParsableCommand {
memoryMB: memory,
diskSizeGB: diskSize,
romSource: VPhoneBootROMOption.source(for: rom),
- sepromSource: seprom.map { URL(fileURLWithPath: $0) } ?? VPhoneBundleOperations.defaultSEPROMSource(),
+ sepromSource: VPhoneSEPROMOption.source(for: seprom),
)
let bundle = try VPhoneBundleOperations.create(spec, in: lib.library)
print("created \(bundle.url.path)")
@@ -219,7 +220,7 @@ struct VPhoneVirtualMachineNewCommand: ParsableCommand {
struct VPhoneVirtualMachineConfigCommand: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "config",
- abstract: "Edit VM manifest fields (cpu/memory/network/startup/rom)",
+ abstract: "Edit VM manifest fields (cpu/memory/network/startup/rom/seprom)",
discussion: """
Network settings are read when the VM starts, so a change applies from the next launch.
@@ -254,6 +255,10 @@ struct VPhoneVirtualMachineConfigCommand: ParsableCommand {
AVPBooter (the machine's own patched copy when it has one), --rom <file> any other ROM. \
Applies from the next start, without a restore: both ROMs load the same patched LLB. \
Restoring a machine that boots OpenAVB needs a bundle with OpenAVB support.
+
+ --seprom opensep makes the bundle's OpenSEP the SEP ROM, --seprom apple Virtualization.framework's \
+ AVPSEPBooter, --seprom <file> any other SEP ROM. Applies from the next start, without a \
+ restore: both load the SEP firmware the restore installed.
""",
)
@@ -288,6 +293,8 @@ struct VPhoneVirtualMachineConfigCommand: ParsableCommand {
var syncHostLocation: String?
@Option(name: .long, help: VPhoneBootROMOption.help)
var rom: String?
+ @Option(name: .long, help: VPhoneSEPROMOption.help)
+ var seprom: String?
func run() throws {
let name = try VPhoneVirtualMachineSelection.resolveExisting(name, in: lib.library)
@@ -303,9 +310,14 @@ struct VPhoneVirtualMachineConfigCommand: ParsableCommand {
unlocksAtStartup: unlocks,
syncsHostLocation: syncsLocation,
)
+ var current = updated
if let rom {
- let installed = try VPhoneBootROMOption.install(rom, in: updated)
- print("boot ROM: \(VPhoneBootROMOption.describe(installed)), from the next start")
+ current = try VPhoneBootROMOption.install(rom, in: current)
+ print("boot ROM: \(VPhoneBootROMOption.describe(current)), from the next start")
+ }
+ if let seprom {
+ current = try VPhoneSEPROMOption.install(seprom, in: current)
+ print("SEP ROM: \(VPhoneSEPROMOption.describe(current)), from the next start")
}
let m = updated.manifest
print(
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift
index 81afc08b..f16e1d2f 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift
@@ -440,10 +440,12 @@ public struct VPhoneVirtualMachineCreator {
let clone = try VPhoneMachineTemplates.cloneMachine(from: template, to: options.name, in: library)
outputs.add(clone.url)
do {
- // The ROM is not part of a template's key: it changes without a
- // restore. A template built before OpenAVB still starts Apple's,
- // so the machine gets this bundle's ROM, as a `vm new` would.
- try VPhoneBundleOperations.installBootROM(from: VPhoneBundleOperations.defaultROMSource(), in: clone)
+ // The ROMs are not part of a template's key: they change without a
+ // restore. A template built before OpenAVB or OpenSEP still starts
+ // Apple's, so the machine gets this bundle's ROMs, as a `vm new`
+ // would.
+ let booted = try VPhoneBundleOperations.installBootROM(from: VPhoneBundleOperations.defaultROMSource(), in: clone)
+ try VPhoneBundleOperations.installSEPROM(from: VPhoneBundleOperations.defaultSEPROMSource(), in: booted)
try applySettings(options)
} catch {
try? FileManager.default.removeItem(at: clone.url)
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
index 2523e891..83071d9e 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh
@@ -116,6 +116,13 @@ fi
"$root/VPhoneExecutable/VPhoneVirtualization/Resources/OpenAVB/APSL-2.0.txt" \
"$resources/OpenAVB/"
+# The SEP ROM new machines copy, with its licence. Checked in prebuilt; see
+# Resources/OpenSEP/README.md.
+/bin/mkdir -p "$resources/OpenSEP"
+/bin/cp "$root/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/OpenSEP.bin" \
+ "$root/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/LICENSE" \
+ "$resources/OpenSEP/"
+
# Patch presets. Prewritten plists, read by vphone-cli at patch time to decide
# which declared patches apply. Nothing generates or edits these.
/bin/mkdir -p "$resources/patches_presets"
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/UpdateOpenSEP.sh b/VPhoneExecutable/VPhoneVirtualization/Build/UpdateOpenSEP.sh
new file mode 100755
index 00000000..e36478e1
--- /dev/null
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/UpdateOpenSEP.sh
@@ -0,0 +1,68 @@
+#!/bin/zsh
+# vphone-tier: build
+#
+# Rebuild Resources/OpenSEP/OpenSEP.bin from OpenSEP's source, with any
+# patches in Resources/OpenSEP/patches applied in order.
+#
+# UpdateOpenSEP.sh build the pinned commit and check it matches
+# the checked-in image byte for byte
+# UpdateOpenSEP.sh <commit> build another commit and replace the image and
+# LICENSE with what it builds
+#
+# A patch upstream has taken is deleted from patches/ when moving past it.
+#
+# Nothing here runs during the VPhone build: the image is checked in so the
+# bundle needs no cross toolchain. The clone and the build happen in a
+# temporary folder outside this repository. Needs git and
+# `brew install llvm lld`; OpenSEP's `make verify` also wants uv and ripgrep,
+# and is not run here. The build is `make build/OpenSEP.bin`, whose platform
+# is vresearch1 (Virtualization.framework's AVPSEPBooter). After replacing the
+# image, update the commit, toolchain, SHA-256 and size in
+# Resources/OpenSEP/README.md and the pinned commit below, then restore and
+# boot a machine with it.
+set -euo pipefail
+
+repository="https://github.com/owngoal-dev/OpenSEP.git"
+pinned="39ff3d7572059668077388dd907b63308ddf645e"
+
+root="$(cd "${0:a:h}/../../.." && pwd)"
+destination="$root/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP"
+commit="${1:-$pinned}"
+llvm="${LLVM_PREFIX:-/opt/homebrew/opt/llvm/bin}"
+
+for tool in "$llvm/clang" "$llvm/llvm-objcopy" /opt/homebrew/bin/ld.lld; do
+ [[ -x "$tool" ]] || { print -u2 "Missing $tool (brew install llvm lld)"; exit 1; }
+done
+
+work="$(/usr/bin/mktemp -d)"
+trap '/bin/rm -rf "$work"' EXIT
+/usr/bin/git clone --quiet "$repository" "$work/OpenSEP"
+/usr/bin/git -C "$work/OpenSEP" checkout --quiet --detach "$commit"
+for patch in "$destination"/patches/*.patch(N); do
+ /usr/bin/git -C "$work/OpenSEP" apply --whitespace=nowarn "$patch" || {
+ print -u2 "${patch:t} does not apply to $commit; refresh it, or delete it if upstream has it"
+ exit 1
+ }
+ print "patch ${patch:t}"
+done
+/usr/bin/make -C "$work/OpenSEP" LLVM_PREFIX="$llvm" PLATFORM=vresearch1 build/OpenSEP.bin >/dev/null
+
+built="$work/OpenSEP/build/OpenSEP.bin"
+print "commit $(/usr/bin/git -C "$work/OpenSEP" rev-parse HEAD)"
+print "sha256 $(/usr/bin/shasum -a 256 "$built" | /usr/bin/awk '{print $1}')"
+print "size $(/usr/bin/stat -f %z "$built")"
+print "clang $("$llvm/clang" --version | /usr/bin/head -1)"
+print "ld.lld $(/opt/homebrew/bin/ld.lld --version)"
+
+if (( $# == 0 )); then
+ /usr/bin/cmp "$built" "$destination/OpenSEP.bin" || {
+ print -u2 "The pinned commit does not build the checked-in OpenSEP.bin with this toolchain."
+ exit 1
+ }
+ print "OpenSEP.bin matches $pinned"
+else
+ /bin/cp "$built" "$destination/OpenSEP.bin"
+ /bin/cp "$work/OpenSEP/LICENSE" "$destination/LICENSE"
+ /bin/chmod 644 "$destination/OpenSEP.bin" "$destination/LICENSE"
+ print "Replaced $destination/OpenSEP.bin; update README.md and the pinned commit."
+fi
diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
index 67991deb..4fd9a509 100755
--- a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
+++ b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh
@@ -105,6 +105,20 @@ done
print -u2 "Contents/Resources/OpenAVB/OpenAVB.bin is not an OpenAVB image"
exit 1
}
+# The SEP ROM new machines copy, and its licence. The SEP ROM window is
+# 0x29000 bytes. Apple's AVPSEPBooter carries its version string and OpenSEP
+# none, which is how a machine's SEP ROM is told apart (VPhoneSEPROM).
+for name in OpenSEP.bin LICENSE; do
+ [[ -s "$resources/OpenSEP/$name" ]] || { print -u2 "Missing Contents/Resources/OpenSEP/$name"; exit 1; }
+done
+(( $(/usr/bin/stat -f %z "$resources/OpenSEP/OpenSEP.bin") <= 0x29000 )) || {
+ print -u2 "OpenSEP.bin is larger than the 0x29000-byte SEP ROM window"
+ exit 1
+}
+if /usr/bin/grep -q 'AVPSEPBooter-' "$resources/OpenSEP/OpenSEP.bin"; then
+ print -u2 "Contents/Resources/OpenSEP/OpenSEP.bin is Apple's AVPSEPBooter, not OpenSEP"
+ exit 1
+fi
for name in vphoned vphoned-tvos launchdhook-vphone.dylib SystemHook-vphone.dylib libcamfix.dylib \
libvcamcaptured.dylib libmisfix.dylib libhapticsfix.dylib libbatteryhealthfix.dylib libdevicehubfix.dylib libdevicename.dylib \
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/LICENSE b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/LICENSE
new file mode 100644
index 00000000..be4a1b6a
--- /dev/null
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2026 owngoal-dev
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/OpenSEP.bin b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/OpenSEP.bin
new file mode 100644
index 00000000..70a69774
Binary files /dev/null and b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/OpenSEP.bin differ
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/README.md b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/README.md
new file mode 100644
index 00000000..f1519f72
--- /dev/null
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/README.md
@@ -0,0 +1,44 @@
+# OpenSEP
+
+`OpenSEP.bin` is the SEP boot ROM new machines run instead of Apple's
+`AVPSEPBooter.vresearch1.bin`. It is
+[OpenSEP](https://github.com/owngoal-dev/OpenSEP), an open-source SEP boot ROM
+compatible with AVPSEPBooter, built from source for the `vresearch1` platform.
+`StageBundle.sh` copies the image and its licence to
+`VPhone.bundle/Contents/Resources/OpenSEP/`, and `vm new` copies the image into
+each new machine folder.
+
+| | |
+| --- | --- |
+| Source | `https://github.com/owngoal-dev/OpenSEP` |
+| Commit | `39ff3d7572059668077388dd907b63308ddf645e` |
+| Patches | `patches/*.patch`, applied in order (none) |
+| Target | `make PLATFORM=vresearch1 build/OpenSEP.bin` |
+| Toolchain | Homebrew LLVM 23.1.3 (`clang`, `llvm-objcopy`), Homebrew LLD 23.1.3 |
+| SHA-256 | `fe3f8de4334dd00ba6862bd95cd86d9bea7534e47592432a601d815edfb911d6` |
+| Size | 147496 bytes (the SEP ROM window is `0x29000`, 167936) |
+
+The image is checked in rather than built by the `VPhone` scheme, so building
+the bundle needs no cross toolchain. To move to another commit, or to check
+that this file is what the commit builds, run
+`VPhoneExecutable/VPhoneVirtualization/Build/UpdateOpenSEP.sh` (see its
+header). It clones and builds outside the repository and needs
+`brew install llvm lld`.
+
+## Patches
+
+None. If vphone needs a change upstream has not taken yet, put it in
+`patches/` as a `git format-patch` file. `UpdateOpenSEP.sh` applies patches in
+order and refuses one that no longer applies. Delete a patch once the pin
+moves past it.
+
+## Licence
+
+`LICENSE` is OpenSEP's licence as published at that commit: MIT. The image
+also carries two pieces of public Apple data, as OpenSEP's README describes:
+the Apple DDI Secure Boot Root CA - G1 certificate, and a table of published
+per-build SEP firmware keys for iPhone14,4 that a `vresearch1` machine does not
+use (its `vphone600` SEP firmware is not encrypted).
+
+What the ROM expects from the host (memory map, mailbox, SEP firmware image)
+and how vphone uses it is described in `Research/Firmware/opensep_sep_rom.md`.
diff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
index e363caf6..0e4d1b71 100644
--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift
@@ -26,16 +26,14 @@ public enum VPhoneBundleOperations {
}
}
- private static let frameworkResources = URL(fileURLWithPath:
- "/System/Library/Frameworks/Virtualization.framework/Versions/A/Resources")
-
/// The boot ROM new machines get: this bundle's OpenAVB.
public static func defaultROMSource() -> URL {
VPhoneBootROM.openAVBSource()
}
+ /// The SEP ROM new machines get: this bundle's OpenSEP.
public static func defaultSEPROMSource() -> URL {
- frameworkResources.appendingPathComponent("AVPSEPBooter.vresearch1.bin")
+ VPhoneSEPROM.openSEPSource()
}
/// Public because `vm import` validates the name it is about to place into
@@ -79,19 +77,19 @@ public enum VPhoneBundleOperations {
// SEP storage: 512 KB of initialized zero bytes.
try Data(count: 512 * 1024).write(to: dir.appendingPathComponent("SEPStorage"))
- // ROMs. The boot ROM keeps the name of its kind, so `fw patch`
- // finds an Apple ROM to patch and leaves OpenAVB alone.
+ // ROMs. Each keeps the name of its kind, so `fw patch` finds an
+ // Apple boot ROM to patch and leaves OpenAVB alone.
let romName = try bootROMKind(of: spec.romSource).fileName
+ let sepromName = try sepROMKind(of: spec.sepromSource).fileName
try fm.copyItem(at: spec.romSource, to: dir.appendingPathComponent(romName))
- try fm.copyItem(at: spec.sepromSource, to: dir.appendingPathComponent("AVPSEPBooter.vresearch1.bin"))
+ try fm.copyItem(at: spec.sepromSource, to: dir.appendingPathComponent(sepromName))
// Manifest.
let manifest = VPhoneVirtualMachineManifest(
cpuCount: spec.cpuCount,
memorySize: spec.memoryMB * 1024 * 1024,
networkConfig: spec.networkConfig,
- romImages: .init(avpBooter: romName,
- avpSEPBooter: "AVPSEPBooter.vresearch1.bin"),
+ romImages: .init(avpBooter: romName, avpSEPBooter: sepromName),
)
try manifest.write(to: dir.appendingPathComponent("config.plist"))
@@ -130,11 +128,53 @@ public enum VPhoneBundleOperations {
/// identity, which bundles from before OpenAVB do not.
@discardableResult
public static func installBootROM(from source: URL, in bundle: VPhoneBundle) throws -> VPhoneBundle {
- let kind = try bootROMKind(of: source)
+ let name = try bootROMKind(of: source).fileName
+ try placeROM(from: source, named: name, in: bundle)
+ let images = VPhoneVirtualMachineManifest.ROMImages(
+ avpBooter: name,
+ avpSEPBooter: bundle.manifest.romImages?.avpSEPBooter
+ ?? VPhoneVirtualMachineManifest.ROMImages.default.avpSEPBooter,
+ )
+ return try write(romImages: images, to: bundle)
+ }
+
+ // MARK: - SEP ROM
+
+ /// The kind of the SEP ROM at `url`, refusing a file that cannot be read.
+ static func sepROMKind(of url: URL) throws -> VPhoneSEPROM {
+ guard let kind = VPhoneSEPROM.kind(at: url) else {
+ throw CocoaError(.fileReadNoSuchFile, userInfo: [NSFilePathErrorKey: url.path])
+ }
+ return kind
+ }
+
+ /// Makes `source` the machine's SEP ROM, as ``installBootROM(from:in:)``
+ /// does for the boot ROM: copied in under the name of its kind and named by
+ /// `romImages.avpSEPBooter`, from the next start. The ROM it replaces
+ /// stays in the folder.
+ ///
+ /// Neither SEP ROM is patched, and both boot the SEP firmware the restore
+ /// installed, so a restored machine can change SEP ROM without another
+ /// restore.
+ @discardableResult
+ public static func installSEPROM(from source: URL, in bundle: VPhoneBundle) throws -> VPhoneBundle {
+ let name = try sepROMKind(of: source).fileName
+ try placeROM(from: source, named: name, in: bundle)
+ let images = VPhoneVirtualMachineManifest.ROMImages(
+ avpBooter: bundle.manifest.romImages?.avpBooter
+ ?? VPhoneVirtualMachineManifest.ROMImages.default.avpBooter,
+ avpSEPBooter: name,
+ )
+ return try write(romImages: images, to: bundle)
+ }
+
+ /// Copies `source` into the folder as `name`, unless it is already that
+ /// file, replacing an earlier copy in one step.
+ private static func placeROM(from source: URL, named name: String, in bundle: VPhoneBundle) throws {
let fm = FileManager.default
- let destination = bundle.url.appendingPathComponent(kind.fileName)
+ let destination = bundle.url.appendingPathComponent(name)
if source.standardizedFileURL.path != destination.standardizedFileURL.path {
- let staged = bundle.url.appendingPathComponent(".\(kind.fileName).\(UUID().uuidString)")
+ let staged = bundle.url.appendingPathComponent(".\(name).\(UUID().uuidString)")
try fm.copyItem(at: source, to: staged)
do {
if fm.fileExists(atPath: destination.path) {
@@ -148,13 +188,10 @@ public enum VPhoneBundleOperations {
}
}
try VPhoneHostFilePermissions.makeAccessible(at: destination)
+ }
- let images = VPhoneVirtualMachineManifest.ROMImages(
- avpBooter: kind.fileName,
- avpSEPBooter: bundle.manifest.romImages?.avpSEPBooter
- ?? VPhoneVirtualMachineManifest.ROMImages.default.avpSEPBooter,
- )
- let updated = bundle.manifest.updating(romImages: images)
+ private static func write(romImages: VPhoneVirtualMachineManifest.ROMImages, to bundle: VPhoneBundle) throws -> VPhoneBundle {
+ let updated = bundle.manifest.updating(romImages: romImages)
try updated.write(to: bundle.configURL)
try VPhoneHostFilePermissions.makeAccessible(at: bundle.configURL)
return VPhoneBundle(url: bundle.url, manifest: updated)
diff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleReport.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleReport.swift
index 555f2daf..00e912f5 100644
--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleReport.swift
+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleReport.swift
@@ -30,6 +30,9 @@ public struct VPhoneBundleReport: Codable, Equatable, Sendable {
/// manifest names none (the framework's built-in ROM) or it cannot be
/// read. Added with OpenAVB.
public let bootROM: String?
+ /// The SEP ROM the machine starts its SEP from: `opensep` or `apple`, nil
+ /// as for `bootROM`. Added with OpenSEP.
+ public let sepROM: String?
public init(bundle: VPhoneBundle) {
name = bundle.name
@@ -48,5 +51,6 @@ public struct VPhoneBundleReport: Codable, Equatable, Sendable {
templateMatch = source.map { VPhoneMachineTemplates.match(of: $0, in: library) }
syncsHostLocation = bundle.manifest.sharesHostLocation
bootROM = VPhoneBootROM.kind(ofMachineAt: bundle.url)?.rawValue
+ sepROM = VPhoneSEPROM.kind(ofMachineAt: bundle.url)?.rawValue
}
}
diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneSEPROM.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneSEPROM.swift
new file mode 100644
index 00000000..31a25965
--- /dev/null
+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneSEPROM.swift
@@ -0,0 +1,86 @@
+import Foundation
+
+// MARK: - VPhoneSEPROM
+
+/// The SEP boot ROM a machine starts its SEP core from: the file
+/// `romImages.avpSEPBooter` names.
+///
+/// New machines run OpenSEP, an open-source ROM compatible with AVPSEPBooter
+/// that ships in `VPhone.bundle/Contents/Resources/OpenSEP/`. Machines created
+/// before it keep Apple's `AVPSEPBooter.vresearch1.bin`. Neither is patched:
+/// both load the personalised SEP firmware the restore sends and hand over to
+/// it (see `Research/Firmware/opensep_sep_rom.md`).
+public enum VPhoneSEPROM: String, CaseIterable, Sendable {
+ case openSEP = "opensep"
+ case apple
+
+ /// The file name a machine folder keeps this ROM under.
+ public var fileName: String {
+ switch self {
+ case .openSEP: "OpenSEP.bin"
+ case .apple: "AVPSEPBooter.vresearch1.bin"
+ }
+ }
+
+ public var displayName: String {
+ switch self {
+ case .openSEP: "OpenSEP"
+ case .apple: "AVPSEPBooter"
+ }
+ }
+
+ // MARK: - Recognition
+
+ /// The version string Apple's SEP ROM carries (`AVPSEPBooter-9…` in the
+ /// macOS 26 image). OpenSEP prints nothing and carries no string of its
+ /// own, so the rule is the other way round from `VPhoneBootROM`: a ROM
+ /// with Apple's version string is Apple's, and any other is OpenSEP.
+ static let appleMarker = Data("AVPSEPBooter-".utf8)
+
+ /// Which SEP ROM these bytes are.
+ public static func kind(of image: Data) -> VPhoneSEPROM {
+ image.range(of: appleMarker) != nil ? .apple : .openSEP
+ }
+
+ /// The SEP ROM at `url`, or nil when it cannot be read.
+ public static func kind(at url: URL) -> VPhoneSEPROM? {
+ guard let image = try? Data(contentsOf: url, options: .mappedIfSafe) else { return nil }
+ return kind(of: image)
+ }
+
+ /// The SEP ROM a machine folder boots: what its manifest names, or nil
+ /// when the manifest names none (vphone-vm then uses the framework's
+ /// built-in ROM) or the file cannot be read.
+ public static func kind(ofMachineAt url: URL) -> VPhoneSEPROM? {
+ guard let manifest = try? VPhoneVirtualMachineManifest.load(from: url.appendingPathComponent("config.plist")),
+ let name = manifest.romImages?.avpSEPBooter,
+ VPhoneVirtualMachineManifest.isPlainFileName(name)
+ else { return nil }
+ return kind(at: url.appendingPathComponent(name))
+ }
+
+ // MARK: - Sources
+
+ /// Apple's SEP ROM, from Virtualization.framework.
+ public static let appleSource = URL(fileURLWithPath:
+ "/System/Library/Frameworks/Virtualization.framework/Versions/A/Resources/AVPSEPBooter.vresearch1.bin")
+
+ /// The OpenSEP image in this bundle's resources. A source build outside
+ /// the bundle reads the checked-in copy.
+ public static func openSEPSource(resources: VPhoneResources = .resolve()) -> URL {
+ let bundled = resources.base.appendingPathComponent("OpenSEP/OpenSEP.bin")
+ if FileManager.default.fileExists(atPath: bundled.path) {
+ return bundled
+ }
+ return resources.base.appendingPathComponent(
+ "VPhoneExecutable/VPhoneVirtualization/Resources/OpenSEP/OpenSEP.bin")
+ }
+
+ /// Where `vm new` copies this ROM from.
+ public func source(resources: VPhoneResources = .resolve()) -> URL {
+ switch self {
+ case .openSEP: Self.openSEPSource(resources: resources)
+ case .apple: Self.appleSource
+ }
+ }
+}
diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/BootROMTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/BootROMTests.swift
index d0c2ce60..9057e1e1 100644
--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/BootROMTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/BootROMTests.swift
@@ -94,7 +94,7 @@ struct BootROMTests {
let switched = try VPhoneBundleOperations.installBootROM(from: rom(.openAVB, in: root), in: apple)
#expect(switched.manifest.romImages?.avpBooter == "OpenAVB.bin")
- #expect(switched.manifest.romImages?.avpSEPBooter == "AVPSEPBooter.vresearch1.bin")
+ #expect(switched.manifest.romImages?.avpSEPBooter == apple.manifest.romImages?.avpSEPBooter)
#expect(try VPhoneVirtualMachineManifest.load(from: switched.configURL).romImages?.avpBooter == "OpenAVB.bin")
#expect(VPhoneBootROM.kind(ofMachineAt: switched.url) == .openAVB)
// The Apple ROM, with whatever fw patch did to it, is still there.
diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/SEPROMTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/SEPROMTests.swift
new file mode 100644
index 00000000..ae014d14
--- /dev/null
+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/SEPROMTests.swift
@@ -0,0 +1,143 @@
+import Foundation
+import Testing
+@testable import VPhoneCoreKit
+
+/// Which SEP ROM a machine starts its SEP from, and how it gets there.
+///
+/// New machines copy OpenSEP and keep it as `OpenSEP.bin`; Apple's keeps
+/// `AVPSEPBooter.vresearch1.bin`. Changing SEP ROM only repoints
+/// `romImages.avpSEPBooter`, leaves the boot ROM alone and keeps the other SEP
+/// ROM in the folder.
+struct SEPROMTests {
+ // MARK: - Fixtures
+
+ private func makeRoot() throws -> URL {
+ let dir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ return dir
+ }
+
+ /// A SEP ROM image of `kind`: Apple's carries its version string.
+ private func seprom(_ kind: VPhoneSEPROM, in dir: URL, tag: UInt8 = 0) throws -> URL {
+ var bytes = Data([0x1F, 0x20, 0x03, 0xD5, tag])
+ if kind == .apple {
+ bytes.append(Data("....AVPSEPBooter-9.120.2\0".utf8))
+ }
+ let url = dir.appendingPathComponent(UUID().uuidString + ".bin")
+ try bytes.write(to: url)
+ return url
+ }
+
+ private func newMachine(seprom: URL, in root: URL) throws -> VPhoneBundle {
+ let rom = root.appendingPathComponent("rom.bin")
+ try Data([0x01]).write(to: rom)
+ return try VPhoneBundleOperations.create(
+ .init(name: "phone", cpuCount: 2, memoryMB: 2048, diskSizeGB: 1, romSource: rom, sepromSource: seprom),
+ in: VPhoneLibrary(root: root.appendingPathComponent("library")),
+ )
+ }
+
+ // MARK: - Recognition
+
+ @Test func `Apple's SEP ROM is recognised by its version string and anything else is OpenSEP`() throws {
+ #expect(VPhoneSEPROM.kind(of: Data("xxAVPSEPBooter-9.120.2\0".utf8)) == .apple)
+ #expect(VPhoneSEPROM.kind(of: Data("Apple DDI Secure Boot Root CA - G1".utf8)) == .openSEP)
+ #expect(VPhoneSEPROM.kind(of: Data()) == .openSEP)
+ #expect(VPhoneSEPROM.kind(at: URL(fileURLWithPath: "/nonexistent/seprom.bin")) == nil)
+ }
+
+ @Test func `the checked-in OpenSEP image is recognised as OpenSEP and fits the SEP ROM window`() throws {
+ let repository = URL(fileURLWithPath: #filePath)
+ .deletingLastPathComponent().deletingLastPathComponent()
+ .deletingLastPathComponent().deletingLastPathComponent()
+ let image = VPhoneSEPROM.openSEPSource(resources: VPhoneResources(base: repository))
+ #expect(image.path.hasSuffix("Resources/OpenSEP/OpenSEP.bin"))
+ #expect(VPhoneSEPROM.kind(at: image) == .openSEP)
+ let size = try Data(contentsOf: image).count
+ #expect(size > 0 && size <= 0x29000)
+ }
+
+ @Test func `the framework's SEP ROM is recognised as Apple's`() throws {
+ try #require(FileManager.default.fileExists(atPath: VPhoneSEPROM.appleSource.path))
+ #expect(VPhoneSEPROM.kind(at: VPhoneSEPROM.appleSource) == .apple)
+ }
+
+ // MARK: - vm new
+
+ @Test func `a new machine keeps each SEP ROM under the name of its kind`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+
+ let openSEP = try newMachine(seprom: seprom(.openSEP, in: root), in: root)
+ #expect(openSEP.manifest.romImages?.avpSEPBooter == "OpenSEP.bin")
+ #expect(FileManager.default.fileExists(atPath: openSEP.url.appendingPathComponent("OpenSEP.bin").path))
+ #expect(!FileManager.default.fileExists(atPath: openSEP.url.appendingPathComponent("AVPSEPBooter.vresearch1.bin").path))
+ #expect(VPhoneSEPROM.kind(ofMachineAt: openSEP.url) == .openSEP)
+ #expect(VPhoneBundleReport(bundle: openSEP).sepROM == "opensep")
+
+ let other = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: other) }
+ let apple = try newMachine(seprom: seprom(.apple, in: other), in: other)
+ #expect(apple.manifest.romImages?.avpSEPBooter == "AVPSEPBooter.vresearch1.bin")
+ #expect(VPhoneSEPROM.kind(ofMachineAt: apple.url) == .apple)
+ #expect(VPhoneBundleReport(bundle: apple).sepROM == "apple")
+ }
+
+ @Test func `a new machine refuses a SEP ROM it cannot read`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+ #expect(throws: (any Error).self) {
+ try newMachine(seprom: root.appendingPathComponent("missing.bin"), in: root)
+ }
+ #expect(!FileManager.default.fileExists(atPath: root.appendingPathComponent("library/phone").path))
+ }
+
+ // MARK: - Changing SEP ROM
+
+ @Test func `changing SEP ROM repoints the manifest and keeps the boot ROM and the other SEP ROM`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+ let apple = try newMachine(seprom: seprom(.apple, in: root, tag: 7), in: root)
+ let bootROM = apple.manifest.romImages?.avpBooter
+ let appleImage = try Data(contentsOf: apple.url.appendingPathComponent("AVPSEPBooter.vresearch1.bin"))
+
+ let switched = try VPhoneBundleOperations.installSEPROM(from: seprom(.openSEP, in: root), in: apple)
+ #expect(switched.manifest.romImages?.avpSEPBooter == "OpenSEP.bin")
+ #expect(switched.manifest.romImages?.avpBooter == bootROM)
+ #expect(try VPhoneVirtualMachineManifest.load(from: switched.configURL).romImages?.avpSEPBooter == "OpenSEP.bin")
+ #expect(VPhoneSEPROM.kind(ofMachineAt: switched.url) == .openSEP)
+ #expect(try Data(contentsOf: apple.url.appendingPathComponent("AVPSEPBooter.vresearch1.bin")) == appleImage)
+
+ let kept = switched.url.appendingPathComponent("AVPSEPBooter.vresearch1.bin")
+ let back = try VPhoneBundleOperations.installSEPROM(from: kept, in: switched)
+ #expect(back.manifest.romImages?.avpSEPBooter == "AVPSEPBooter.vresearch1.bin")
+ #expect(back.manifest.romImages?.avpBooter == bootROM)
+ #expect(try Data(contentsOf: kept) == appleImage)
+ #expect(VPhoneSEPROM.kind(ofMachineAt: back.url) == .apple)
+ }
+
+ @Test func `changing boot ROM leaves the SEP ROM alone`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+ let machine = try newMachine(seprom: seprom(.openSEP, in: root), in: root)
+ let openAVB = root.appendingPathComponent("openavb.bin")
+ try Data("OpenBootROM: SEP bootstrap complete\n".utf8).write(to: openAVB)
+
+ let switched = try VPhoneBundleOperations.installBootROM(from: openAVB, in: machine)
+ #expect(switched.manifest.romImages?.avpBooter == "OpenAVB.bin")
+ #expect(switched.manifest.romImages?.avpSEPBooter == "OpenSEP.bin")
+ #expect(VPhoneSEPROM.kind(ofMachineAt: switched.url) == .openSEP)
+ }
+
+ @Test func `installing OpenSEP again replaces the copy in the folder`() throws {
+ let root = try makeRoot()
+ defer { try? FileManager.default.removeItem(at: root) }
+ let machine = try newMachine(seprom: seprom(.openSEP, in: root, tag: 1), in: root)
+ let newer = try seprom(.openSEP, in: root, tag: 2)
+
+ try VPhoneBundleOperations.installSEPROM(from: newer, in: machine)
+ #expect(try Data(contentsOf: machine.url.appendingPathComponent("OpenSEP.bin")) == Data(contentsOf: newer))
+ let leftovers = try FileManager.default.contentsOfDirectory(atPath: machine.url.path).filter { $0.hasPrefix(".") }
+ #expect(leftovers.isEmpty)
+ }
+}