Run a virtual iPhone on an Apple Silicon Mac.
Swift
fix(gitignore): anchor lib/ so it stops swallowing cfw-kit/lib
Lakr4f1475295790
diff --git a/.gitignore b/.gitignoreindex 5938b17b..c9474966 100644--- a/.gitignore+++ b/.gitignore@@ -107,8 +107,12 @@ dist/downloads/eggs/.eggs/-lib/-lib64/+# Anchored to the root. Unanchored, these match a directory named lib at ANY+# depth, which silently swallowed cfw-kit/lib/ -- the shared library every+# script in the kit sources. They come from the Python packaging template and+# were only ever meant to be the setuptools build output here.+/lib/+/lib64/parts/sdist/var/diff --git a/cfw-kit/lib/base_stages.sh b/cfw-kit/lib/base_stages.shnew file mode 100644index 00000000..fd1a162c--- /dev/null+++ b/cfw-kit/lib/base_stages.sh@@ -0,0 +1,267 @@+#!/bin/zsh+# base_stages.sh — the stages BOTH variants need, as functions.+#+# This is the "what does it take to boot at all" set. Nothing here installs a+# daemon, opens a port, or touches launchd.plist. Both vanilla and jb call+# these; jb then layers its own stages on top.+#+# Every function assumes lib/common.sh is already sourced and init_paths ran.++# ── Version detection ───────────────────────────────────────────+# Sets IOS_VERSION and DSC_DIR once, explicitly. Several stages key off+# IOS_VERSION; having one of them set it as a side effect made the stage order+# load-bearing in a way nothing enforced. Call this right after stage_cryptex —+# SystemVersion.plist only exists on the volume once the Cryptex is in place.+detect_ios_version() {+ IOS_VERSION=$(read_ios_version)+ DSC_DIR="$MNT1/System/Cryptexes/OS/System/Library/Caches/com.apple.dyld"+ echo " [*] userland version: ${IOS_VERSION:-unknown}"+ [[ -n "$IOS_VERSION" ]] \+ || warn "could not read ProductVersion — every version-gated fix below will be skipped"+}++# ── Cryptex SystemOS + AppOS ────────────────────────────────────+# Without this the hybrid firmware has a boot chain and no userland.+stage_cryptex() {+ echo ""+ echo "[base] Installing Cryptex (SystemOS + AppOS)..."++ echo " Mounting rootfs rw..."+ mount_vol s1 "$MNT1"++ local os_count=0 app_count=0+ [[ -d "$MNT1/System/Cryptexes/OS" ]] && os_count=$(/bin/ls "$MNT1/System/Cryptexes/OS/" | /usr/bin/wc -l | tr -d ' ')+ [[ -d "$MNT1/System/Cryptexes/App" ]] && app_count=$(/bin/ls "$MNT1/System/Cryptexes/App/" | /usr/bin/wc -l | tr -d ' ')++ if [[ "${os_count:-0}" -gt 0 && "${app_count:-0}" -gt 0 ]]; then+ echo " [*] Cryptexes already installed (OS=${os_count}, App=${app_count}), skipping copy"+ _link_dyld+ echo " [+] Cryptex skipped (already present)"+ return+ fi++ local sysos_dmg="$TEMP_DIR/CryptexSystemOS.dmg"+ local appos_dmg="$TEMP_DIR/CryptexAppOS.dmg"+ local mnt_sysos="$TEMP_DIR/mnt_sysos"+ local mnt_appos="$TEMP_DIR/mnt_appos"++ if [[ ! -f "$sysos_dmg" ]]; then+ echo " Extracting AEA key..."+ local aea_key+ aea_key=$(ipsw fw aea --key "$RESTORE_DIR/$CRYPTEX_SYSOS")+ echo " Decrypting SystemOS..."+ aea decrypt -i "$RESTORE_DIR/$CRYPTEX_SYSOS" -o "$sysos_dmg" -key-value "$aea_key"+ else+ echo " Using cached SystemOS DMG"+ fi++ if [[ ! -f "$appos_dmg" ]]; then+ cp "$RESTORE_DIR/$CRYPTEX_APPOS" "$appos_dmg"+ else+ echo " Using cached AppOS DMG"+ fi++ safe_detach "$mnt_sysos"+ safe_detach "$mnt_appos"+ mkdir -p "$mnt_sysos" "$mnt_appos"+ assert_mount_under_vm "$mnt_sysos" "SystemOS mountpoint"+ assert_mount_under_vm "$mnt_appos" "AppOS mountpoint"++ echo " Mounting SystemOS..."+ host_hdiutil attach -mountpoint "$mnt_sysos" "$sysos_dmg" -nobrowse -owners off \+ || die "Failed to mount SystemOS DMG. Run 'sudo -v' in a terminal and retry."+ echo " Mounting AppOS..."+ host_hdiutil attach -mountpoint "$mnt_appos" "$appos_dmg" -nobrowse -owners off \+ || die "Failed to mount AppOS DMG. Run 'sudo -v' in a terminal and retry."++ /bin/rm -rf $MNT1/System/Cryptexes/App $MNT1/System/Cryptexes/OS+ /bin/mkdir -p $MNT1/System/Cryptexes/App $MNT1/System/Cryptexes/OS+ /bin/chmod 0755 $MNT1/System/Cryptexes/App $MNT1/System/Cryptexes/OS++ echo " Copying Cryptexes..."+ cp -R "$mnt_sysos/." "$MNT1/System/Cryptexes/OS"+ cp -R "$mnt_appos/." "$MNT1/System/Cryptexes/App"++ echo " Creating dyld symlinks..."+ _link_dyld++ echo " Unmounting Cryptex DMGs..."+ safe_detach "$mnt_sysos"+ safe_detach "$mnt_appos"++ echo " [+] Cryptex installed"+}++_link_dyld() {+ /bin/ln -sf ../../../System/Cryptexes/OS/System/Library/Caches/com.apple.dyld \+ $MNT1/System/Library/Caches/com.apple.dyld+ /bin/ln -sf ../../../../System/Cryptexes/OS/System/DriverKit/System/Library/dyld \+ $MNT1/System/DriverKit/System/Library/dyld+}++# ── Version-gated display / boot fixes ──────────────────────────+# NOT optional extras. Without the matching one the guest either shows a black+# screen on the host VZ view or panics at pid 1. Keyed off the installed+# userland version, exactly as upstream does.+#+# $1 = "1" to also apply the lsd embedded-registration gate. That one is the+# only genuinely JB-only patch in this stage: upstream's own comment+# (cfw_install.sh:348-351) says it exists so the iOS-27 `vpregister` first-boot+# tool can register JB app bundles, because uicache's+# registerApplicationDictionary is a no-op stub on 27. With no such tool+# installed it opens a path nothing walks.+#+# Reads IOS_VERSION / DSC_DIR; detect_ios_version must have run.+stage_display_and_boot_fixes() {+ local want_lsd_reg="${1:-0}"+ : "${IOS_VERSION?detect_ios_version has not run}"+ : "${DSC_DIR:?detect_ios_version has not run}"++ case "$IOS_VERSION" in+ 26.0*|18.*)+ echo " [*] Patching IOMobileFramebuffer SwapEnd payload size (-> 0x560)..."+ [[ -d "$DSC_DIR" ]] || die "dyld cache dir missing: $DSC_DIR"+ cfw_py patch-iomfb-swapend "$DSC_DIR" --target-size 0x560+ ;;+ 27.*)+ echo " [*] Forcing IOMobileFramebuffer present onto the kern (method-5) path..."+ [[ -d "$DSC_DIR" ]] || die "dyld cache dir missing: $DSC_DIR"+ cfw_py patch-iomfb-force-kern "$DSC_DIR"+ ;;+ esac++ case "$IOS_VERSION" in+ 27.*)+ if [[ -d "$DSC_DIR" ]]; then+ # 27's cache + 512 MiB maxSlide overflows the 6 GiB shared region+ # -> dyld cannot map libSystem -> launchd (pid 1) panics.+ echo " [*] Checking dyld cache maxSlide vs kernel shared region..."+ cfw_py patch-dsc-maxslide "$DSC_DIR"+ # libxpc LWCR self-check crash-loops every daemon that pins an+ # entitlement peer-requirement under our code-signing setup.+ echo " [*] Patching libxpc LWCR self-check..."+ cfw_py patch-xpc-lwcr "$DSC_DIR"+ # missing MAC sysctl -> launchd abort.+ echo " [*] Patching os_lockdown_mode_enabled..."+ cfw_py patch-lockdown-mode "$DSC_DIR"++ if [[ "$want_lsd_reg" == "1" ]]; then+ echo " [*] Patching lsd embedded-registration gate..."+ cfw_py patch-lsd-embedded-reg "$DSC_DIR"+ else+ echo " [*] skip lsd embedded-registration gate (no JB registration tool)"+ fi+ fi+ ;;+ *)+ if [[ "${FORCE_DSC_MAXSLIDE:-0}" == "1" && -d "$DSC_DIR" ]]; then+ echo " [*] Forcing dyld cache maxSlide=0 (opt-in FORCE_DSC_MAXSLIDE=1)..."+ cfw_py patch-dsc-maxslide "$DSC_DIR" --force+ fi+ ;;+ esac+}++# ── seputil + gigalocker ────────────────────────────────────────+stage_seputil() {+ echo ""+ echo "[base] Patching seputil..."+ patch_rootfs_binary "usr/libexec/seputil" patch-seputil "com.apple.seputil"++ # diskimagesiod DDI mount-gate. NOT an extra and NOT jailbreak-specific:+ # upstream applies it in the plain regular variant too (cfw_install.sh:407),+ # because on a 27 userland MobileStorageMounter's waitForDAMount otherwise+ # hangs forever on the 26.4 hybrid. Version-gated exactly as upstream: on a+ # version-matched userland the native wait completes and forcing it to+ # return early could race the real mount.+ #+ # Entitlements (embedded sandbox profile + private DA/apfs) must survive the+ # re-sign, so this cannot use patch_rootfs_binary.+ case "$IOS_VERSION" in+ 27.*)+ echo " Patching diskimagesiod (DDI auto-mount, iOS $IOS_VERSION)..."+ if ! [[ -e "$MNT1/usr/libexec/diskimagesiod.bak" ]]; then+ /bin/cp "$MNT1/usr/libexec/diskimagesiod" "$MNT1/usr/libexec/diskimagesiod.bak"+ fi+ ldid -e "$MNT1/usr/libexec/diskimagesiod.bak" > "$TEMP_DIR/diskimagesiod.ent.plist"+ cp "$MNT1/usr/libexec/diskimagesiod.bak" "$TEMP_DIR/diskimagesiod"+ cfw_py patch-diskimagesiod "$TEMP_DIR/diskimagesiod"+ ldid_sign_ent "$TEMP_DIR/diskimagesiod" "$TEMP_DIR/diskimagesiod.ent.plist" "com.apple.diskimagesiod"+ cp -R "$TEMP_DIR/diskimagesiod" "$MNT1/usr/libexec/diskimagesiod"+ /bin/chmod 0755 "$MNT1/usr/libexec/diskimagesiod"+ ;;+ esac++ echo " Renaming gigalocker..."+ mount_vol s3 "$MNT3"+ mv "$MNT3"/*.gl(N) "$MNT3/AA.gl" 2>/dev/null || true++ echo " [+] seputil patched"+}++# ── Paravirtual GPU bundle ──────────────────────────────────────+# Without it: backboardd crash loop / black SpringBoard.+# Provenance of the two binaries in here: see ../paravirt-gpu-provenance-kit.+stage_gpu() {+ echo ""+ echo "[base] Installing AppleParavirtGPUMetalIOGPUFamily..."++ local gpu_tar="$INPUT_DIR/custom/AppleParavirtGPUMetalIOGPUFamily.tar"+ [[ -f "$gpu_tar" ]] || die "GPU bundle missing: $gpu_tar"++ cp -R "$gpu_tar" "$MNT1"+ "$TAR" --preserve-permissions --no-overwrite-dir --warning=no-unknown-keyword \+ -xf $MNT1/AppleParavirtGPUMetalIOGPUFamily.tar -C $MNT1++ local bundle="$MNT1/System/Library/Extensions/AppleParavirtGPUMetalIOGPUFamily.bundle"+ find $bundle -name '._*' -delete 2>/dev/null || true+ /usr/sbin/chown -R 0:0 $bundle+ /bin/chmod 0755 $bundle+ /bin/chmod 0755 $bundle/libAppleParavirtCompilerPluginIOGPUFamily.dylib+ /bin/chmod 0755 $bundle/AppleParavirtGPUMetalIOGPUFamily+ /bin/chmod 0755 $bundle/_CodeSignature+ /bin/chmod 0644 $bundle/_CodeSignature/CodeResources+ /bin/chmod 0644 $bundle/Info.plist+ /bin/rm -f $MNT1/AppleParavirtGPUMetalIOGPUFamily.tar++ echo " [+] GPU driver installed"+}++# ── Activation bypass ───────────────────────────────────────────+# Without it the guest sits on the activation screen forever.+stage_mobileactivationd() {+ echo ""+ echo "[base] Patching mobileactivationd..."+ patch_rootfs_binary "usr/libexec/mobileactivationd" patch-mobileactivationd+ echo " [+] mobileactivationd patched"+}++# ── launchd cache validation ────────────────────────────────────+# NOPs the cache validation check in launchd_cache_loader. Its ONLY purpose is+# to let launchd accept a MODIFIED /System/Library/xpc/launchd.plist+# (research/0_binary_patch_comparison.md row 2: "Allow modified launchd.plist").+#+# So it is NOT part of the base set: neither vanilla nor jb-with-an-empty-slot+# touches launchd.plist, and on a stock plist the stock validation passes.+# A userland flavour that injects a daemon into launchd.plist — which is what+# upstream cfw_install_jb.sh:459-471 does — MUST declare+# USERLAND_MODIFIES_LAUNCHD_PLIST=1, or the guest will boot with a plist+# launchd refuses to load.+stage_launchd_cache_loader() {+ echo ""+ echo "[base] Patching launchd_cache_loader (modified launchd.plist allowed)..."+ patch_rootfs_binary "usr/libexec/launchd_cache_loader" \+ patch-launchd-cache-loader "com.apple.launchd_cache_loader"+ echo " [+] launchd_cache_loader patched"+}++# ── Teardown ────────────────────────────────────────────────────+stage_unmount() {+ echo ""+ echo "[*] Unmounting image volumes..."+ # Deliberately NOT umount -f: if something still holds a file on the volume+ # we want to hear about it, not paper over it.+ umount "$MNT1" 2>/dev/null || warn "could not unmount $MNT1 (still busy?)"+ umount "$MNT3" 2>/dev/null || true+ /bin/rm -rf "$TEMP_DIR/mnt_sysos" "$TEMP_DIR/mnt_appos"+}diff --git a/cfw-kit/lib/common.sh b/cfw-kit/lib/common.shnew file mode 100644index 00000000..3ab74cac--- /dev/null+++ b/cfw-kit/lib/common.sh@@ -0,0 +1,218 @@+#!/bin/zsh+# common.sh — shared helpers for the custom-firmware-kit variant installers.+#+# Every function here is lifted from vphone-cli's scripts/cfw_install.sh so the+# two variants behave identically to the upstream installer on the code paths+# they keep. Do not "improve" them here — if upstream changes, re-sync.+#+# Contract expected by the callers (same as upstream cfw_install.sh):+# - running as root, VM powered off+# - CFW_HOST_CONTAINER set to the attached APFS container (e.g. disk4)+# - cwd = the VM directory, $1 = the VM directory+# - VPHONE_REPO points at a vphone-cli checkout (for patchers/ and resources/)++# ── Restore caller's PATH — Nix /etc/zshenv resets PATH on zsh startup ─+[[ -n "${_VPHONE_PATH:-}" ]] && export PATH="$_VPHONE_PATH"++die() {+ echo "[-] $*" >&2+ exit 1+}++warn() { echo "[!] $*" >&2; }++# ── Repo location ───────────────────────────────────────────────+# The kit does not fork the Python patchers; it calls the repo's cfw.py.+resolve_repo() {+ local candidate="${VPHONE_REPO:-}"+ if [[ -z "$candidate" ]]; then+ # The kit now lives inside the checkout, so the enclosing directory is+ # the answer in the normal case. The other two are kept for running it+ # from outside a checkout, which is how it was developed.+ for c in "${KIT_DIR:h}" "$HOME/Documents/GitHub/Lakr233/vphone-cli" "${KIT_DIR:h}/vphone-cli"; do+ [[ -f "$c/scripts/patchers/cfw.py" ]] && candidate="$c" && break+ done+ fi+ [[ -n "$candidate" ]] || die "VPHONE_REPO unset and no vphone-cli checkout found. Pass --repo <path>."+ [[ -f "$candidate/scripts/patchers/cfw.py" ]] \+ || die "Not a vphone-cli checkout (no scripts/patchers/cfw.py): $candidate"+ echo "${candidate:a}"+}++# ── Python resolver — prefer project venv over whatever is in PATH ─+resolve_python3() {+ if [[ -n "${VPHONE_PYTHON:-}" ]]; then+ echo "$VPHONE_PYTHON"+ return+ fi+ local venv_py="$REPO_DIR/.venv/bin/python3"+ if [[ -x "$venv_py" ]]; then+ echo "$venv_py"+ else+ command -v python3 || true+ fi+}++cfw_py() { "$PYTHON3" "$REPO_DIR/scripts/patchers/cfw.py" "$@"; }++# ── Signing ─────────────────────────────────────────────────────+require_signing_tools() {+ command -v ldid &>/dev/null \+ || die "ldid not found (brew install ldid-procursus). Run: make setup_tools"+}++ldid_sign() {+ local file="$1" bundle_id="${2:-}"+ local args=(-S -M "-K$VM_DIR/$CFW_INPUT/signcert.p12")+ [[ -n "$bundle_id" ]] && args+=("-I$bundle_id")+ ldid "${args[@]}" "$file"+}++# Like ldid_sign but re-applies an entitlements plist (for binaries whose+# entitlements must survive the re-sign).+ldid_sign_ent() {+ local file="$1" ent="$2" bundle_id="${3:-}"+ local args=("-S$ent" -M "-K$VM_DIR/$CFW_INPUT/signcert.p12")+ [[ -n "$bundle_id" ]] && args+=("-I$bundle_id")+ ldid "${args[@]}" "$file"+}++# ── Host image helpers ──────────────────────────────────────────+host_hdiutil() {+ local rc+ [[ -n "${SUDO_ASKPASS:-}" ]] && { sudo -A hdiutil "$@"; return; }+ hdiutil "$@" && return 0+ rc=$?+ if sudo -n true 2>/dev/null; then+ sudo hdiutil "$@"+ return+ fi+ return "$rc"+}++safe_detach() {+ local mnt="$1"+ if mount | grep -Fq " on $mnt "; then+ host_hdiutil detach -force "$mnt" 2>/dev/null || true+ fi+}++assert_mount_under_vm() {+ local mnt="$1" label="${2:-mountpoint}"+ local abs_vm abs_mnt+ abs_vm="$(cd "$VM_DIR" && pwd -P)"+ abs_mnt="$(cd "$mnt" && pwd -P)"+ case "$abs_mnt/" in+ "$abs_vm/"*) ;;+ *) die "Unsafe ${label}: ${abs_mnt} (must be inside ${abs_vm})" ;;+ esac+}++mount_vol() { # mount_vol <slice, e.g. s1> <mountpoint> [opts]+ local dev="/dev/${CFW_HOST_CONTAINER}$1" mnt="$2" opts="${3:-rw}"+ /bin/mkdir -p "$mnt"+ /sbin/mount | /usr/bin/grep -q " on $mnt " && return 0+ /sbin/mount_apfs -o "$opts" "$dev" "$mnt" 2>/dev/null || true+ /sbin/mount | /usr/bin/grep -q " on $mnt " || die "mount failed: $dev -> $mnt"+}++find_restore_dir() {+ for dir in "$VM_DIR"/iPhone*_Restore; do+ [[ -f "$dir/BuildManifest.plist" ]] && echo "$dir" && return+ done+ die "No restore directory found in $VM_DIR"+}++setup_cfw_input() {+ [[ -d "$VM_DIR/$CFW_INPUT" ]] && return+ local archive+ for search_dir in "$REPO_DIR/scripts/resources" "$REPO_DIR/scripts" "$VM_DIR"; do+ archive="$search_dir/$CFW_ARCHIVE"+ if [[ -f "$archive" ]]; then+ echo " Extracting $CFW_ARCHIVE..."+ "$TAR" --zstd --warning=no-unknown-keyword -xf "$archive" -C "$VM_DIR"+ return+ fi+ done+ die "Neither $CFW_INPUT/ nor $CFW_ARCHIVE found"+}++# ── Preflight ───────────────────────────────────────────────────+# Everything that can fail is checked BEFORE the first write to the volume.+# This is the only cheap protection against a half-modified guest volume:+# the installer is streaming and there is no snapshot to roll back to.+preflight() {+ local fatal=0++ command -v ipsw >/dev/null 2>&1 \+ || { warn "'ipsw' not found. Install: brew install blacktop/tap/ipsw"; fatal=1; }+ command -v aea >/dev/null 2>&1 \+ || { warn "'aea' not found (requires macOS 12+)"; fatal=1; }+ command -v ldid >/dev/null 2>&1 \+ || { warn "'ldid' not found. Install: brew install ldid-procursus"; fatal=1; }++ # GNU tar: macOS bsdtar lacks --no-overwrite-dir / --warning=.+ if [[ ! -x "$TAR" ]]; then+ warn "GNU tar not found at '$TAR'. Install: brew install gnu-tar"; fatal=1+ fi+ # --zstd in GNU tar spawns an external zstd(1); it is NOT linked in.+ if [[ -x "$TAR" ]] && ! command -v zstd >/dev/null 2>&1; then+ warn "'zstd' not found — GNU tar's --zstd shells out to it. Install: brew install zstd"+ fatal=1+ fi++ [[ -x "$PYTHON3" ]] || { warn "python3 not found (tried: $PYTHON3)"; fatal=1; }+ if [[ -x "$PYTHON3" ]]; then+ local py_err+ py_err="$("$PYTHON3" -c "import capstone, keystone" 2>&1)" || {+ warn "Missing Python deps (using $PYTHON3): ${py_err}"+ warn " Fix: source $REPO_DIR/.venv/bin/activate && pip install capstone keystone-engine"+ fatal=1+ }+ fi++ # Every cfw.py subcommand this variant will call must exist up front.+ local sub+ for sub in "${REQUIRED_CFW_SUBCOMMANDS[@]}"; do+ grep -q "\"$sub\"" "$REPO_DIR/scripts/patchers/cfw.py" \+ || { warn "cfw.py has no '$sub' subcommand — repo too old or too new for this kit"; fatal=1; }+ done++ (( fatal == 0 )) || die "Preflight failed — nothing was written."+ echo "[+] preflight OK"+}++# ── Volume layout (set by the caller after sourcing) ────────────+init_paths() {+ : "${CFW_HOST_CONTAINER:?CFW_HOST_CONTAINER unset — run via run.sh or cfw_install_host.sh}"+ HOST_MNT="${CFW_HOST_MNT:-/private/tmp/cfwhost}"+ MNT1="$HOST_MNT/mnt1" # disk1s1 (System / rootfs)+ MNT3="$HOST_MNT/mnt3" # disk1s3+ TAR="$(command -v gtar 2>/dev/null || echo /opt/homebrew/bin/gtar)"+ mkdir -p "$HOST_MNT"+}++# Read the installed userland version off the mounted rootfs. Every+# version-gated patch below keys off this, exactly as upstream does.+read_ios_version() {+ /usr/bin/plutil -extract ProductVersion raw -o - \+ "$MNT1/System/Library/CoreServices/SystemVersion.plist" 2>/dev/null || true+}++# Patch a rootfs binary from its pristine .bak, sign it, put it back.+# Upstream does this inline in four places; same semantics, one helper.+patch_rootfs_binary() { # <relpath> <cfw.py subcommand> [bundle_id]+ local rel="$1" sub="$2" bundle_id="${3:-}"+ local live="$MNT1/$rel" bak="$MNT1/$rel.bak" work="$TEMP_DIR/${rel:t}"++ [[ -e "$live" || -e "$bak" ]] || die "missing on volume: /$rel"+ if ! [[ -e "$bak" ]]; then+ echo " Creating backup of /$rel..."+ /bin/cp "$live" "$bak"+ fi+ /bin/cp "$bak" "$work"+ cfw_py "$sub" "$work"+ ldid_sign "$work" "$bundle_id"+ /bin/cp -R "$work" "$live"+ /bin/chmod 0755 "$live"+}