Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

[Patchless] Add AMFI preflight test

Maximilian Paß8188761e24b2
diff --git a/Makefile b/Makefile
index 8983fe1a..604d0ec6 100644
--- a/Makefile
+++ b/Makefile
@@ -239,8 +239,8 @@ amfidont_allow_vphone: bundle
boot_host_preflight: build
zsh $(SCRIPTS)/boot_host_preflight.sh
-boot_binary_check: $(BINARY)
- @zsh $(SCRIPTS)/boot_host_preflight.sh --assert-bootable
+define BOOT_BINARY_CHECK
+ @zsh $(SCRIPTS)/boot_host_preflight.sh $(1)
@tmp_log="$$(mktemp -t vphone-boot-preflight.XXXXXX)"; \
set +e; \
"$(CURDIR)/$(BINARY)" --help >"$$tmp_log" 2>&1; \
@@ -258,12 +258,19 @@ boot_binary_check: $(BINARY)
exit $$rc; \
fi; \
rm -f "$$tmp_log"
+endef
+
+boot_binary_check_less: $(BINARY)
+ $(call BOOT_BINARY_CHECK,--assert-bootable --less)
+
+boot_binary_check: $(BINARY)
+ $(call BOOT_BINARY_CHECK,--assert-bootable)
boot: bundle vphoned boot_binary_check
cd $(VM_DIR) && "$(CURDIR)/$(BUNDLE_BIN)" \
--config ./config.plist
-boot_less: bundle vphoned boot_binary_check
+boot_less: bundle vphoned boot_binary_check_less
cd $(VM_DIR) && "$(CURDIR)/$(BUNDLE_BIN)" \
--config ./config.plist --variant less
diff --git a/README.md b/README.md
index 565f348e..777f3c4e 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,8 @@ See [research/0_binary_patch_comparison.md](./research/0_binary_patch_comparison
**Configure SIP/AMFI** — required for private Virtualization.framework entitlements and unsigned binary workflows.
+The Patchless variant is currently only supported by Option 1.
+
Boot into Recovery (long press power button), open Terminal, then choose one setup path:
- **Option 1: Fully disable SIP + AMFI boot-arg (most permissive)**
diff --git a/scripts/boot_host_preflight.sh b/scripts/boot_host_preflight.sh
old mode 100644
new mode 100755
index ae4d0d26..64d57e45
--- a/scripts/boot_host_preflight.sh
+++ b/scripts/boot_host_preflight.sh
@@ -9,6 +9,7 @@ PROJECT_ROOT="${SCRIPT_DIR:h}"
ASSERT_BOOTABLE=0
QUIET=0
+LESS=0
while [[ $# -gt 0 ]]; do
case "$1" in
@@ -20,6 +21,10 @@ while [[ $# -gt 0 ]]; do
QUIET=1
shift
;;
+ --less)
+ LESS=1
+ shift
+ ;;
*)
echo "Unknown option: $1" >&2
exit 2
@@ -123,6 +128,20 @@ if (( ASSERT_BOOTABLE == 1 )); then
fi
fi
+if (( LESS == 1 && ASSERT_BOOTABLE == 1 )); then
+ case " $CURRENT_BOOT_ARGS " in
+ *" amfi_get_out_of_my_way=0x1 "*)
+ ;;
+ *)
+ (( QUIET == 0 )) && {
+ echo ""
+ echo "Error: The patchless variant requires a disabled AMFI." >&2
+ }
+ exit 3
+ ;;
+ esac
+fi
+
print_section "Entitlements"
if [[ -f "$RELEASE_BIN" ]]; then
codesign -d --entitlements :- "$RELEASE_BIN" 2>/dev/null || true
diff --git a/scripts/setup_tools.sh b/scripts/setup_tools.sh
old mode 100644
new mode 100755
index 54375b7d..07175739
--- a/scripts/setup_tools.sh
+++ b/scripts/setup_tools.sh
@@ -25,7 +25,7 @@ ensure_repo_submodule() {
# ── Brew packages ──────────────────────────────────────────────
-echo "[1/4] Checking brew packages..."
+echo "[1/5] Checking brew packages..."
BREW_PACKAGES=(aria2 gnu-tar openssl@3 ldid-procursus sshpass)
BREW_MISSING=()
@@ -45,7 +45,7 @@ fi
# ── Trustcache ─────────────────────────────────────────────────
-echo "[2/4] trustcache"
+echo "[2/5] trustcache"
TRUSTCACHE_BIN="$TOOLS_PREFIX/bin/trustcache"
if [[ -x "$TRUSTCACHE_BIN" ]]; then
@@ -74,7 +74,7 @@ fi
# ── insert_dylib ───────────────────────────────────────────────
-echo "[3/4] insert_dylib"
+echo "[3/5] insert_dylib"
INSERT_DYLIB_BIN="$TOOLS_PREFIX/bin/insert_dylib"
if [[ -x "$INSERT_DYLIB_BIN" ]]; then
@@ -90,8 +90,30 @@ fi
# ── Python venv ────────────────────────────────────────────────
-echo "[4/4] Python venv"
+echo "[4/5] Python venv"
zsh "$SCRIPT_DIR/setup_venv.sh"
+# ── APFS sealvolume ────────────────────────────────────────────────
+
+echo "[5/5] apfs sealvolume"
+TMP_DIR="$(mktemp -d)"
+ipsw download appledb \
+ --os macOS \
+ --build 25D2140 \
+ --pattern "094-33864-054.dmg" \
+ --output "$TMP_DIR"
+
+RAMDISK_IM4P="$TMP_DIR/25D2140__MacOS/094-33864-054.dmg"
+RAMDISK="$TMP_DIR/ramdisk.dmg"
+ipsw img4 im4p extract --output "$RAMDISK" "$RAMDISK_IM4P"
+
+DEVICE=$(hdiutil attach -readonly -nobrowse "$RAMDISK" | awk '/Apple_APFS/ {print $1; exit}')
+MOUNT=$(mount | grep "$DEVICE" | awk '{print $3}')
+cp "$MOUNT/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs_sealvolume" \
+ "$TOOLS_PREFIX/apfs_sealvolume"
+hdiutil detach "$DEVICE" >/dev/null 2>&1 || true
+echo "Downloaded: $TOOLS_PREFIX/apfs_sealvolume"
+
+
echo ""
echo "All tools installed."
diff --git a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift
index 241190a6..25c49d14 100644
--- a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift
+++ b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift
@@ -6,9 +6,8 @@
// 2. With the OS, AppOS, and SystemOS images, attach them and copy them to a target image
// 3. Create trustcache for resulting image
// 4. Create mtree for resulting image
-// 5. Download apfs_sealvolume
-// 6. Generate digest.db and SystemVolume root_hash
-// 7. Join mtree and digest.db to Ap,SystemVolumeCanonicalMetadata
+// 5. Generate digest.db and SystemVolume root_hash
+// 6. Join mtree and digest.db to Ap,SystemVolumeCanonicalMetadata
import Foundation
import CryptoKit
@@ -75,8 +74,7 @@ public final class CryptexFilesystemPatcher: Patcher {
let mtreePath = try createMtree(filesystem: unencryptedImage)
print("Creating DigestDB and Root Hash")
- let sealvolume = try extractSealvolume()
- let (digestDbPath, rootHashPath) = try createDigestAndHash(sealvolume: sealvolume, filesystem: unencryptedImage, mtree: mtreePath)
+ let (digestDbPath, rootHashPath) = try createDigestAndHash(filesystem: unencryptedImage, mtree: mtreePath)
let metadataPath = try compressCanonicalMetadata(mtree: mtreePath, digestDb: digestDbPath)
let rootHashContainer = try wrapRootHash(rootHashPath)
@@ -341,7 +339,7 @@ public final class CryptexFilesystemPatcher: Patcher {
return im4pPath
}
- func createDigestAndHash(sealvolume: URL, filesystem: URL, mtree: URL) throws -> (URL, URL) {
+ func createDigestAndHash(filesystem: URL, mtree: URL) throws -> (URL, URL) {
let (device, mount) = try attachImage(path: filesystem)
defer { try? detachImage(deviceNode: device) }
@@ -368,6 +366,7 @@ public final class CryptexFilesystemPatcher: Patcher {
FileManager.default.createFile(atPath: mtreeRemapPath.path, contents: remapContent.data(using: .utf8))
try unmount(mount: mount)
+ let sealvolume = self.vphoneCliDirectory.appending(path: ".tools/apfs_sealvolume")
_ = try runProcess(sealvolume.path, [
"-R", mtreeRemapPath.path,
"-U", digestDbPath.path, // Save digest records
@@ -414,35 +413,6 @@ public final class CryptexFilesystemPatcher: Patcher {
throw FirmwareManifest.ManifestError.fileNotFound("metadata")
}
- func extractSealvolume() throws -> URL {
- // We cannot execute iOS binaries on macOS, therefore, we have to download a macOS ramdisk
- let tmpDir = try createTmpDir()
- _ = try runProcess("/opt/homebrew/bin/ipsw", [
- "download", "appledb", "--os", "macOS", "--build", "25D2140",
- "--pattern", "094-33864-054.dmg",
- "--output", tmpDir.path
- ])
- let ramdiskIm4pPath = tmpDir.appending(path: "25D2140__MacOS/094-33864-054.dmg")
- let ramdiskPath = tmpDir.appending(path: "ramdisk.dmg")
- try extractIm4pContainer(ramdiskIm4pPath, output: ramdiskPath)
-
- let (device, mount) = try attachImage(path: ramdiskPath, readonly: true)
- defer { try? detachImage(deviceNode: device) }
-
- let sourcePath = URL.init(filePath: mount).appending(path: "System/Library/Filesystems/apfs.fs/Contents/Resources/apfs_sealvolume")
- let targetPath = tmpDir.appending(path: "apfs_sealvolume")
- try FileManager.default.copyItem(at: sourcePath, to: targetPath)
- return targetPath
- }
-
- func extractIm4pContainer(_ container: URL, output: URL) throws {
- _ = try runProcess("/opt/homebrew/bin/ipsw", [
- "img4", "im4p", "extract",
- "--output", output.path,
- container.path
- ])
- }
-
func createMtree(filesystem: URL) throws -> URL {
let (device, mount) = try attachImage(path: filesystem, readonly: true)
defer { try? detachImage(deviceNode: device) }