Run a virtual iPhone on an Apple Silicon Mac.
Swift
Apple Watch guests
Maximilian Paß94f283ea5856
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOBuildPlatform.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOBuildPlatform.swiftindex 97ab9847..971c8a1a 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOBuildPlatform.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOBuildPlatform.swift@@ -2,7 +2,8 @@//// dyld refuses to load a library into a process of another platform. An// Apple TV guest's processes are tvOS, so an iOS-built library a TV guest-// loads, the paravirtual GPU driver, is marked tvOS before it is installed.+// loads, the paravirtual GPU driver, is marked tvOS before it is installed;+// on a watch guest, watchOS.// The code signature no longer matches afterwards: the caller signs.import Foundation@@ -10,6 +11,7 @@ import Foundationpublic enum MachOBuildPlatform {public static let iOS: UInt32 = 2public static let tvOS: UInt32 = 3+ public static let watchOS: UInt32 = 4public enum Error: Swift.Error, Equatable {case notThin64BitMachOdiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOFatSlice.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOFatSlice.swiftnew file mode 100644index 00000000..12fb45d5--- /dev/null+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Binary/MachOFatSlice.swift@@ -0,0 +1,51 @@+// MachOFatSlice.swift — take one architecture out of a universal Mach-O.+//+// watchOS ships its binaries for arm64_32 and arm64e in one file. The VM's+// kernel runs the arm64e slice, which is the one the patchers understand, so a+// watch binary is thinned to it before it is patched and signed.++import Foundation+import VPhonePatchKit++public enum MachOFatSlice {+ public static let arm64e = (cpuType: UInt32(0x0100_000C), cpuSubtype: UInt32(2))++ private static let fatMagic: UInt32 = 0xCAFE_BABE++ /// Whether `data` is a universal (32-bit fat header) Mach-O.+ public static func isFat(_ data: Data) -> Bool {+ data.count >= 8 && u32be(data, 0) == fatMagic+ }++ /// The slice for `cpuType`/`cpuSubtype` (capability bits ignored) of a+ /// universal Mach-O.+ public static func slice(+ _ data: Data,+ cpuType: UInt32 = arm64e.cpuType,+ cpuSubtype: UInt32 = arm64e.cpuSubtype,+ ) throws -> Data {+ guard isFat(data) else {+ throw PatcherError.invalidFormat("not a universal Mach-O")+ }+ let count = Int(u32be(data, 4))+ guard 8 + count * 20 <= data.count else {+ throw PatcherError.invalidFormat("fat header declares \(count) architectures it does not have")+ }+ for index in 0 ..< count {+ let entry = 8 + index * 20+ guard u32be(data, entry) == cpuType, u32be(data, entry + 4) & 0x00FF_FFFF == cpuSubtype else { continue }+ let offset = Int(u32be(data, entry + 8))+ let size = Int(u32be(data, entry + 12))+ guard offset + size <= data.count else {+ throw PatcherError.invalidFormat("fat slice \(index) is out of bounds")+ }+ return data.subdata(in: data.startIndex + offset ..< data.startIndex + offset + size)+ }+ throw PatcherError.invalidFormat("no slice for CPU type 0x\(String(cpuType, radix: 16)) subtype \(cpuSubtype)")+ }++ private static func u32be(_ data: Data, _ offset: Int) -> UInt32 {+ let start = data.startIndex + offset+ return data[start ..< start + 4].reduce(0) { $0 << 8 | UInt32($1) }+ }+}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swiftindex 2742d54b..6a5c109c 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift@@ -2,6 +2,9 @@//// Merge the cryptex filesystems inside the main OS filesystem.//+// A watchOS image has no cryptexes: its dyld caches are in the OS image, and+// only the guest payload is added before the seal.+//// 1. Collect the AppOS and SystemOS Cryptex from the iPhone BuildManifest// 2. With the OS, AppOS, and SystemOS images, attach them and copy them to a target image// 3. Create trustcache for resulting image@@ -32,6 +35,8 @@ public final class CryptexFilesystemPatcher: BufferedPatcher {var buildManiest: Datavar rebuiltData: Data?+ /// The guest's `VPhoneGuestDevice.Family`, from the preserved iPhone manifest.+ lazy var guestFamily = VPhoneGuestDevice.Family(rawValue: FirmwarePipeline.guestFamily(restoreDir: restoreDir)) ?? .iPhonevar tmpDirectories: [URL] = []var attachedDevices: Set<String> = []@@ -153,14 +158,16 @@ public final class CryptexFilesystemPatcher: BufferedPatcher {let (targetDevice, targetMount) = try attachImage(path: targetImagePath, forceRW: true)defer { try? detachImage(deviceNode: targetDevice) }- print("- Merging App OS cryptex…")- try copyCryptex(targetMount: targetMount, appOS: true)+ if guestFamily != .watch {+ print("- Merging App OS cryptex…")+ try copyCryptex(targetMount: targetMount, appOS: true)- print("- Merging System OS cryptex…")- try copyCryptex(targetMount: targetMount, systemOS: true)+ print("- Merging System OS cryptex…")+ try copyCryptex(targetMount: targetMount, systemOS: true)- print("- Fixing dyld cache…")- try addDyldSymlinks(targetMount: targetMount)+ print("- Fixing dyld cache…")+ try addDyldSymlinks(targetMount: targetMount)+ }if gate.isEnabled(FirmwareRestoreImagePatchSet.gpuDriver) {print("- Fixing GPU driver…")diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swiftindex 5e8b294e..4b7b11d1 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherGuestPayload.swift@@ -34,6 +34,7 @@ extension CryptexFilesystemPatcher {func patchLaunchdCacheLoader(targetMount: String) throws {let target = URL(filePath: targetMount)let launchdCacheLoaderPath = target.appending(path: "/usr/libexec/launchd_cache_loader")+ try thinToArm64e(launchdCacheLoaderPath)// Patched in place with no `.bak` to restore from, so this is the call// site that needs the port's idempotence. No re-attestation: the sign// below replaces the whole signature anyway.@@ -95,7 +96,7 @@ extension CryptexFilesystemPatcher {/// that VM. vphoned is cross-compiled at build time now/// by the Xcode guest target and staged into the bundle.func stageVphoned(to vphonedBin: URL) throws {- let prebuilt = try VPhoneGuestBinaries.resolve("vphoned")+ let prebuilt = try VPhoneGuestBinaries.resolve(VPhoneResources.vphonedName(for: guestFamily))try FileManager.default.copyItem(at: prebuilt, to: vphonedBin)}@@ -114,6 +115,9 @@ extension CryptexFilesystemPatcher {try FileManager.default.copyItem(at: staged, to: bundle)// Clean AppleDouble files if the host copy created any.try deleteAppleDoubleFiles(under: bundle)+ if guestFamily == .watch {+ try retargetGPUBundle(bundle, to: MachOBuildPlatform.watchOS)+ }try chownRecursively(uid: 0, gid: 0, at: bundle)for path in [bundle,@@ -137,6 +141,33 @@ extension CryptexFilesystemPatcher {}}+ /// The driver and its compiler plugin are built for iOS, and dyld refuses a+ /// library of another platform than the process's: on a watch they say+ /// watchOS, are signed again and the bundle sealed again.+ func retargetGPUBundle(_ bundle: URL, to platform: UInt32) throws {+ for name in ["AppleParavirtGPUMetalIOGPUFamily", "libAppleParavirtCompilerPluginIOGPUFamily.dylib"] {+ let file = bundle.appending(path: name)+ var data = try Data(contentsOf: file)+ try MachOBuildPlatform.set(platform, in: &data)+ try data.write(to: file)+ try VPhoneSigner.sign(+ fileAt: file,+ options: .init(identifier: nil, entitlements: nil, mergesExisting: true, style: .appleAdHoc),+ )+ }+ // No --identifier: codesign keeps the one the bundle already carries.+ _ = try runProcess("/usr/bin/codesign", ["-f", "-s", "-", "--timestamp=none", bundle.path])+ print(" [+] GPU driver retargeted to \(platform == MachOBuildPlatform.watchOS ? "watchOS" : "platform \(platform)")")+ }++ /// A watch's binaries are universal (arm64_32 and arm64e); the VM runs the+ /// arm64e slice, which is the one the patchers read. Keeps a thin binary.+ func thinToArm64e(_ file: URL) throws {+ let data = try Data(contentsOf: file)+ guard MachOFatSlice.isFat(data) else { return }+ try MachOFatSlice.slice(data).write(to: file)+ }+func patchMobileActivation(targetMount: String) throws {let target = URL(filePath: targetMount)let mobileActivationdPath = target.appending(path: "/usr/libexec/mobileactivationd")diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swiftindex 9c7db356..a7439d9e 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/DeviceTree/DeviceTreeGuestDevicePatches.swift@@ -62,6 +62,8 @@ extension DeviceTreePatcher {static let iPadIdentityPatch = "devicetree-cfw-ipad_identity"static let iPadAudioPatch = "devicetree-cfw-ipad_audio"static let tvAppArchitecturePatch = "devicetree-cfw-tv_app_architecture"+ static let watchPresentationPatch = "devicetree-cfw-watch_presentation"+ static let watchDriverKitPatch = "devicetree-cfw-watch_driverkit"static let hapticsPatch = "devicetree-cfw-product_haptics_node"static let microphoneArrayPatch = "devicetree-cfw-product_audio_microphone_array"@@ -172,6 +174,12 @@ extension DeviceTreePatcher {action: .set(architecture), patchID: tvAppArchitecturePatch))}+ if let idiom = value(sourceProduct, "artwork-device-idiom"),+ String(decoding: idiom.prefix { $0 != 0 }, as: UTF8.self) == "watch"+ {+ edits += watchEdits(product: product, value: { value(sourceProduct, $0) })+ }+for (name, patchID) in copiedProductProperties {let action: GuestEdit.Action = value(sourceProduct, name).map { .set($0) } ?? .removeedits.append(GuestEdit(nodePath: product, property: name, action: action, patchID: patchID))@@ -207,6 +215,53 @@ extension DeviceTreePatcher {return edits}+ // MARK: - Watch++ /// `/product` properties a watch's display is described by, beyond those+ /// every board copies.+ static let watchDisplayProperties = ["artwork-display-gamut", "artwork-dynamic-displaymode", "framebuffer-identifier"]++ /// The gestalt variant tags MobileGestalt reads in place of syscfg on the+ /// VM, and the board's `/product` property each stands for. vphone600's+ /// carry the D47's values; a watch whose tags still describe a phone shows+ /// no UI ("unsupported screen class"). A property the board lacks is a+ /// boolean it does not have: 0.+ static let watchVariantTags: [(tag: String, property: String)] = [+ ("tag-ards", "artwork-device-subtype"),+ ("tag-arsf", "artwork-scale-factor"),+ ("tag-codf", "compatible-device-fallback"),+ ("tag-tprt", "sub-product-type"),+ ("tag-crid", "chrome-identifier"),+ ("tag-dcrd", "display-corner-radius"),+ ("tag-fbid", "framebuffer-identifier"),+ ("tag-prde", "product-description"),+ ("tag-isnl", "island-notch-location"),+ ("tag-waco", "watch-companion"),+ ]++ static let watchVariantNode = ["device-tree", "product", "vphone600-gestalt-variants", "d47vm2025vre-variant"]++ /// The edits a watch board needs besides the common copy: its display+ /// properties, the variant tags, and `supports-driverkit`. The VM's touch+ /// screen is a USB device whose HID driver is a dext, and launchd starts+ /// driverkitd only on a device tree that says so; a real watch has no USB+ /// touch screen, so N227b's tree does not.+ static func watchEdits(product: [String], value: (String) -> Data?) -> [GuestEdit] {+ var edits: [GuestEdit] = []+ for name in watchDisplayProperties {+ let action: GuestEdit.Action = value(name).map { .set($0) } ?? .remove+ edits.append(GuestEdit(nodePath: product, property: name, action: action, patchID: watchPresentationPatch))+ }+ let absent = Data(count: 4)+ for (tag, property) in watchVariantTags {+ edits.append(GuestEdit(nodePath: watchVariantNode, property: tag,+ action: .set(value(property) ?? absent), patchID: watchPresentationPatch))+ }+ edits.append(GuestEdit(nodePath: product, property: "supports-driverkit",+ action: .set(Data([1, 0, 0, 0])), patchID: watchDriverKitPatch))+ return edits+ }+/// `compatible` with the board first, so `hw.model` reads it, and/// VPHONE600AP kept for the platform expert's AppleVMApple1IO match.static func compatible(board: Data) -> Data {diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swiftindex 7196d5dd..d10896cf 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareDeviceTreePatchSet.swift@@ -222,6 +222,16 @@ public enum FirmwareDeviceTreePatchSet {"TV app architecture","Copies app-macho-architecture from a TV board's own device tree into the installed tree, so its userland sees the donor's architecture. Restore, iPhone and iPad trees are unchanged.",),+ property(+ "devicetree-cfw-watch_presentation",+ "Watch display and gestalt variant",+ "Copies a watch board's display gamut, display mode and framebuffer identifier, and rewrites the gestalt variant tags MobileGestalt reads in place of syscfg from the board's values, so the watch UI finds its screen class.",+ ),+ property(+ "devicetree-cfw-watch_driverkit",+ "Watch DriverKit",+ "Sets supports-driverkit on a watch guest, so launchd starts driverkitd for the dext that drives the VM's USB touch screen.",+ ),],provides: ["vphone.devicetree"],)diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwarePatchSetCatalog.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwarePatchSetCatalog.swiftindex d0d8c17a..15f35b28 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwarePatchSetCatalog.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwarePatchSetCatalog.swift@@ -363,6 +363,38 @@ public enum FirmwarePatchSetCatalog {),)+ /// The device tree patches ``watchOSPreset`` keeps: the board presentation+ /// of its own tree, the watch display and DriverKit, and what no VM has.+ public static let watchOSDeviceTreePatches: Set<String> = [+ "devicetree-cfw-serial_number",+ "devicetree-cfw-ipad_artwork",+ "devicetree-cfw-ipad_product",+ "devicetree-cfw-ipad_buttons",+ "devicetree-cfw-ipad_identity",+ "devicetree-cfw-ipad_audio",+ "devicetree-cfw-watch_presentation",+ "devicetree-cfw-watch_driverkit",+ "devicetree-cfw-product_haptics_node",+ "devicetree-cfw-product_audio_microphone_array",+ ]++ /// ``minimalPreset`` for an Apple Watch: its device tree presents the watch+ /// board, and activation stays stock. The shipped `watchos.plist` must+ /// match this.+ public static let watchOSPreset = VPhonePatchPreset(+ identifier: VPhonePatchPreset.watchOSIdentifier,+ title: "Apple Watch",+ summary: "Minimal for an Apple Watch guest: the watch board's device tree, and a resealed system with vphoned and the GPU driver restored onto a stock kernel and TXM.",+ patchSets: minimalPreset.patchSets,+ selection: .allow(+ minimalBootChainPatches+ .union(watchOSDeviceTreePatches)+ .union(FirmwareResearchBoardPatchSet.manifest.patches.map(\.identifier))+ .union(FirmwareRestoreImagePatchSet.manifest.patches.map(\.identifier))+ .subtracting([FirmwareRestoreImagePatchSet.mobileActivation]),+ ),+ )+/// The presets the bundle ships.- public static let builtInPresets: [VPhonePatchPreset] = [standardPreset, experimentalPreset, tvOSPreset, tvOS4KPreset, minimalPreset]+ public static let builtInPresets: [VPhonePatchPreset] = [standardPreset, experimentalPreset, tvOSPreset, tvOS4KPreset, minimalPreset, watchOSPreset]}diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareRestoreImagePatchSet.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareRestoreImagePatchSet.swiftindex 17ce738e..b53a049c 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareRestoreImagePatchSet.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/PatchSets/FirmwareRestoreImagePatchSet.swift@@ -8,9 +8,10 @@// chain, kernel and TXM stay stock, and nothing writes the System volume after the// restore, because that would break the seal.//-// Only the `minimal` preset names this set. Every patch in it is boot-essential for-// that preset, and naming it nowhere else keeps `standard` from counting them as-// dropped.+// Only the `minimal` preset and its watch form, `watchos`, name this set. Every+// patch in it is boot-essential for them, and naming it nowhere else keeps+// `standard` from counting them as dropped. A watchOS image has no cryptexes:+// for a watch the merge only reseals the image with the guest payload.import Foundationimport VPhonePatchKitdiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swiftindex be127344..6581f49a 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/PatchSets/FirmwarePatchSetCatalogTests.swift@@ -137,13 +137,17 @@ struct FirmwarePatchSetCatalogTests {// presets drop exactly the iOS ones an Apple TV has no port of.let tvPresets = [VPhonePatchPreset.tvOSIdentifier, VPhonePatchPreset.tvOS4KIdentifier]// `minimal` drops every boot chain patch but iBEC's and LLB's:- // its system is customised before the restore instead.+ // its system is customised before the restore instead. The+ // watch preset drops activation besides, which pairing needs.+ let restoreImagePresets = [VPhonePatchPreset.minimalIdentifier, VPhonePatchPreset.watchOSIdentifier]let expected = if tvPresets.contains(preset.identifier) {Set(plan.droppedBootEssentials).isSubset(of: FirmwarePatchSetCatalog.tvOSBlockedPatches)- } else if preset.identifier == VPhonePatchPreset.minimalIdentifier {+ } else if restoreImagePresets.contains(preset.identifier) {Set(plan.droppedBootEssentials).isDisjoint(with: FirmwarePatchSetCatalog.minimalBootChainPatches)&& plan.droppedBootEssentials.allSatisfy {FirmwareBootChainPatchSet.manifest.patches.map(\.identifier).contains($0)+ || (preset.identifier == VPhonePatchPreset.watchOSIdentifier+ && $0 == FirmwareRestoreImagePatchSet.mobileActivation)}} else {plan.droppedBootEssentials.isEmpty@@ -172,10 +176,11 @@ struct FirmwarePatchSetCatalogTests {}@Test- func `Only minimal names the restore image, and patches only iBEC, LLB, the device tree and the research board besides`() throws {+ func `Only minimal and its watch form name the restore image, and patch only iBEC, LLB, the device tree and the research board besides`() throws {for preset in FirmwarePatchSetCatalog.builtInPresets {let namesRestoreImage = preset.patchSets.map(\.identifier).contains(FirmwareRestoreImagePatchSet.identifier)- #expect(namesRestoreImage == (preset.identifier == VPhonePatchPreset.minimalIdentifier), "\(preset.identifier)")+ let restoreImagePresets = [VPhonePatchPreset.minimalIdentifier, VPhonePatchPreset.watchOSIdentifier]+ #expect(namesRestoreImage == restoreImagePresets.contains(preset.identifier), "\(preset.identifier)")}let plan = try VPhonePatchPlan.resolve(preset: FirmwarePatchSetCatalog.minimalPreset,@@ -188,6 +193,15 @@ struct FirmwarePatchSetCatalogTests {let researchBoard = Set(FirmwareResearchBoardPatchSet.manifest.patches.map(\.identifier))#expect(plan.enabled == restoreImage.union(FirmwarePatchSetCatalog.minimalDeviceTreePatches).union(bootChain).union(researchBoard))#expect(plan.droppedBootEssentials.allSatisfy { !$0.hasPrefix("ibec-") && !$0.hasPrefix("llb-") })++ let watch = try VPhonePatchPlan.resolve(+ preset: FirmwarePatchSetCatalog.watchOSPreset,+ patchSets: FirmwarePatchSetCatalog.bundled,+ iOSBase: VPhoneVersion("26.4"),+ cloudOS: VPhoneVersion("26.4"),+ )+ #expect(watch.enabled == restoreImage.subtracting([FirmwareRestoreImagePatchSet.mobileActivation])+ .union(FirmwarePatchSetCatalog.watchOSDeviceTreePatches).union(bootChain).union(researchBoard))}@Testdiff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swiftindex a31f2d7a..7281ccbc 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift@@ -100,12 +100,17 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {}/// An Apple TV takes the TV preset; one whose userland comes from an OTA- /// takes the arm64e one, with the GPU driver and the userland swap.+ /// takes the arm64e one, with the GPU driver and the userland swap. An+ /// Apple Watch takes the watch preset.private var defaultPreset: String {- guard let guest = VPhoneGuestDevice.named(device), guest.family == .appleTV else {- return VPhonePatchPreset.standardIdentifier+ switch VPhoneGuestDevice.named(device) {+ case let guest? where guest.family == .appleTV:+ guest.restoreBase == nil ? VPhonePatchPreset.tvOSIdentifier : VPhonePatchPreset.tvOS4KIdentifier+ case let guest? where guest.family == .watch:+ VPhonePatchPreset.watchOSIdentifier+ default:+ VPhonePatchPreset.standardIdentifier}- return guest.restoreBase == nil ? VPhonePatchPreset.tvOSIdentifier : VPhonePatchPreset.tvOS4KIdentifier}func validate() throws {diff --git a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPreset.swift b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPreset.swiftindex 96ffc1c3..83be7547 100644--- a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPreset.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPreset.swift@@ -86,6 +86,10 @@ public struct VPhonePatchPreset: Sendable, Hashable, Codable, Identifiable {/// userland taken from the device's OTA.public static let tvOS4KIdentifier = "tvos-4k"+ /// The identifier an Apple Watch guest gets: ``minimalIdentifier`` for a+ /// watch, which presents its own board.+ public static let watchOSIdentifier = "watchos"+/// Whether this is the preset a VM gets by default.public var isStandard: Bool {identifier == Self.standardIdentifierdiff --git a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.shindex 4a87090e..7792c2d1 100755--- a/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh+++ b/VPhoneExecutable/VPhoneVirtualization/Build/StageBundle.sh@@ -71,6 +71,8 @@ fi# libraries only into a tvOS-platform process, so this copy says tvOS.daemon_sdk="$(/usr/bin/otool -l "$guest/vphoned" | /usr/bin/awk '/LC_BUILD_VERSION/ { found = 1 } found && $1 == "sdk" { print $2; exit }')"/usr/bin/xcrun vtool -set-build-version tvos 15.0 "$daemon_sdk" -replace -output "$guest/vphoned-tvos" "$guest/vphoned"+# The same for watch guests: watchOS 8 is iOS 15's release.+/usr/bin/xcrun vtool -set-build-version watchos 8.0 "$daemon_sdk" -replace -output "$guest/vphoned-watchos" "$guest/vphoned"/bin/cp "$root/VPhoneDaemon/Configuration/vphoned.plist" "$guest/vphoned.plist"/bin/cp "$guest_products/launchhook/launchdhook-vphone.dylib" "$guest/launchdhook-vphone.dylib"/bin/cp "$guest_products/systemhook/SystemHook-vphone.dylib" "$guest/SystemHook-vphone.dylib"@@ -168,6 +170,7 @@ compatibility_library="$(/usr/bin/xcrun swift-stdlib-tool --print \/usr/bin/codesign --force --sign - "$macos/vphone-cli"/usr/bin/codesign --force --sign - --entitlements "$root/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements" "$guest/vphoned"/usr/bin/codesign --force --sign - --entitlements "$root/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements" "$guest/vphoned-tvos"+/usr/bin/codesign --force --sign - --entitlements "$root/VPhoneDaemon/Configuration/VPhoneDaemon.entitlements" "$guest/vphoned-watchos"/usr/bin/codesign --force --sign - "$macos/vphone-escalator"/usr/bin/codesign --force --sign - "$macos/libswiftCompatibilitySpan.vphone.dylib"/usr/bin/codesign --force --sign - --entitlements "$root/VPhoneExecutable/VPhoneVirtualization/Resources/VPhoneVirtualization.entitlements" "$macos/vphone-vm"diff --git a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.shindex 9a1bdd47..cd894067 100755--- a/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh+++ b/VPhoneExecutable/VPhoneVirtualization/Build/ValidateBundle.sh@@ -144,7 +144,7 @@ if /usr/bin/grep -q 'AVPSEPBooter-' "$resources/OpenSEP/OpenSEP.bin"; thenexit 1fi-for name in vphoned vphoned-tvos launchdhook-vphone.dylib SystemHook-vphone.dylib libcamfix.dylib \+for name in vphoned vphoned-tvos vphoned-watchos launchdhook-vphone.dylib SystemHook-vphone.dylib libcamfix.dylib \libvcamcaptured.dylib libmisfix.dylib libhapticsfix.dylib libbatteryhealthfix.dylib libdevicehubfix.dylib libdevicename.dylib \libprefsfix.dylib libsigninfix.dylib libvphonegyro.dylib libvphoneaccelerometer.dylib libvphoneattitude.dylib libAppleParavirtCompilerPluginIOGPUFamily.dylib; dorequire_signed_macho "$guest/$name"@@ -204,6 +204,9 @@ while IFS= read -r file; do# tvOS dyld refuses an iOS-platform daemon on an Apple TV guest.[[ "$platform" == TVOS ]] || { print -u2 "Non-tvOS binary: ${file#$bundle/}"; exit 1; };;+ "$guest/vphoned-watchos")+ [[ "$platform" == WATCHOS ]] || { print -u2 "Non-watchOS binary: ${file#$bundle/}"; exit 1; }+ ;;"$guest/"*)[[ "$platform" == IOS ]] || { print -u2 "Non-iOS binary in guest-resources: ${file#$bundle/}"; exit 1; };;@@ -227,7 +230,7 @@ vm_entitlements="$(/usr/bin/codesign -d --entitlements - --xml "$macos/vphone-vmprint -u2 "Guest daemon entitlements leaked into vphone-vm"exit 1}-for daemon in vphoned vphoned-tvos; do+for daemon in vphoned vphoned-tvos vphoned-watchos; dodaemon_entitlements="$(/usr/bin/codesign -d --entitlements - --xml "$guest/$daemon" 2>/dev/null)"[[ "$daemon_entitlements" == *'com.apple.CommCenter.fine-grained'* &&"$daemon_entitlements" != *'com.apple.private.virtualization'* ]] || {diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/patches_presets/watchos.plist b/VPhoneExecutable/VPhoneVirtualization/Resources/patches_presets/watchos.plistnew file mode 100644index 00000000..31392ace--- /dev/null+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/patches_presets/watchos.plist@@ -0,0 +1,75 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0">+<dict>+ <key>Identifier</key>+ <string>watchos</string>+ <key>PatchSets</key>+ <array>+ <dict>+ <key>Identifier</key>+ <string>com.vphone.patchset.bootchain</string>+ <key>Kind</key>+ <string>Bundled</string>+ </dict>+ <dict>+ <key>Identifier</key>+ <string>com.vphone.patchset.research.board</string>+ <key>Kind</key>+ <string>Bundled</string>+ </dict>+ <dict>+ <key>Identifier</key>+ <string>com.vphone.patchset.devicetree</string>+ <key>Kind</key>+ <string>Bundled</string>+ </dict>+ <dict>+ <key>Identifier</key>+ <string>com.vphone.patchset.restore.image</string>+ <key>Kind</key>+ <string>Bundled</string>+ </dict>+ </array>+ <key>Selection</key>+ <dict>+ <key>Kind</key>+ <string>Allow</string>+ <key>Patches</key>+ <array>+ <string>devicetree-boot-vresearch101_conversion</string>+ <string>devicetree-cfw-ipad_artwork</string>+ <string>devicetree-cfw-ipad_audio</string>+ <string>devicetree-cfw-ipad_buttons</string>+ <string>devicetree-cfw-ipad_identity</string>+ <string>devicetree-cfw-ipad_product</string>+ <string>devicetree-cfw-product_audio_microphone_array</string>+ <string>devicetree-cfw-product_haptics_node</string>+ <string>devicetree-cfw-serial_number</string>+ <string>devicetree-cfw-watch_driverkit</string>+ <string>devicetree-cfw-watch_presentation</string>+ <string>filesystem-boot-cryptex_merge</string>+ <string>filesystem-boot-gpu_driver</string>+ <string>filesystem-boot-launchd_cache_loader_unsecure_cache_gate</string>+ <string>filesystem-boot-vphoned</string>+ <string>ibec-boot-boot_args</string>+ <string>ibec-boot-bootx_precondition</string>+ <string>ibec-boot-image4_callback</string>+ <string>ibec-cfw-serial_label</string>+ <string>kernel-boot-vresearch101_kext_mix</string>+ <string>llb-boot-boot_args</string>+ <string>llb-boot-image4_callback</string>+ <string>llb-boot-panic_bypass</string>+ <string>llb-boot-rootfs</string>+ <string>llb-cfw-display_scale</string>+ <string>llb-cfw-serial_label</string>+ <string>manifest-boot-digest_rehash</string>+ <string>sep-boot-vresearch101_sepd_dispatch</string>+ </array>+ </dict>+ <key>Summary</key>+ <string>Minimal for an Apple Watch guest: the watch board's device tree, and a resealed system with vphoned and the GPU driver restored onto a stock kernel and TXM.</string>+ <key>Title</key>+ <string>Apple Watch</string>+</dict>+</plist>diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swiftindex df6d1a9b..17aa2124 100644--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift@@ -458,6 +458,15 @@ public enum VPhoneFirmwareCatalog {]),]+ /// Apple Watch releases. The watch boots the research board kernel of the+ /// cloudOS its kernelcache recipe is for, rebuilt with the watchOS kexts:+ /// 26.4 (23E244) for watchOS 26.4.+ static let watchReleases: [(devices: [String], releases: [(version: String, url: String)])] = [+ (devices: ["Watch7,18"], releases: [+ ("26.4", "https://updates.cdn-apple.com/2026SpringFCS/fullrestores/122-09130/486B2E2B-DEDB-4DCF-80F0-7A84ED51B074/Watch7,18_26.4_23T240_Restore.ipsw"),+ ]),+ ]+/// Full OTAs of devices without an IPSW, oldest first. Each has a/// `tvReleases` entry of its `baseVersion` for its `restoreBase`. The keys/// are AppleDB's.@@ -498,7 +507,7 @@ public enum VPhoneFirmwareCatalog {/// The URL of the base device's IPSW `release` is restored over.public static func restoreBaseURL(for release: VPhoneOTARelease) -> String? {guard let base = VPhoneGuestDevice.named(release.productType)?.restoreBase else { return nil }- let releases = (iPhoneReleases + iPadReleases + tvReleases).first { $0.devices.contains(base.productType) }?.releases+ let releases = (iPhoneReleases + iPadReleases + tvReleases + watchReleases).first { $0.devices.contains(base.productType) }?.releasesreturn releases?.first { $0.version == release.baseVersion }?.url}@@ -506,6 +515,7 @@ public enum VPhoneFirmwareCatalog {/// or another model's releases with the cloudOS an iPhone17,3 of that/// release uses. A cellular iPad gets its Wi-Fi model's; an Apple TV gets/// cloudOS 26.4, the only kernel its TPRO compatibility patch covers; a+ /// watch gets the research board cloudOS its kernelcache recipe covers; a/// device without an IPSW gets one per OTA for it, named for the OTA: the/// base's IPSW when the OTA is of the same version, else the OTA itself,/// which `fw prepare` restores over its base's IPSW; an unknown device@@ -514,7 +524,7 @@ public enum VPhoneFirmwareCatalog {guard let guest = VPhoneGuestDevice.named(device) else { return [] }guard guest.presentsBoard else { return pairings }let restored = guest.restoreBase ?? guest- var releases = (iPhoneReleases + iPadReleases + tvReleases).first { $0.devices.contains(restored.productType) }?.releases ?? []+ var releases = (iPhoneReleases + iPadReleases + tvReleases + watchReleases).first { $0.devices.contains(restored.productType) }?.releases ?? []if restored != guest {releases = otaReleases.filter { $0.productType == guest.productType }.compactMap { ota inguard let base = releases.first(where: { $0.version == ota.baseVersion }) else { return nil }@@ -522,9 +532,11 @@ public enum VPhoneFirmwareCatalog {}}return releases.map { release in- let cloudOS = guest.family == .appleTV- ? VPhoneCloudOSOption(name: "cloudOS 26.4", url: cloud264)- : recommendedCloudOS(forVersion: release.version)+ let cloudOS = switch guest.family {+ case .appleTV: VPhoneCloudOSOption(name: "cloudOS 26.4", url: cloud264)+ case .watch: researchBoardCloudOS(forVersion: release.version) ?? recommendedCloudOS(forVersion: release.version)+ case .iPhone, .iPad: recommendedCloudOS(forVersion: release.version)+ }return VPhoneFirmwarePairing(device: guest.productType,iosName: "\(guest.family.osName) \(release.version)",diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swiftindex a02096a6..17333076 100644--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareIndex.swift@@ -59,9 +59,9 @@ public enum VPhoneFirmwareIndex {// MARK: - Queries/// Every released iOS restore URL for one device identifier, newest first.- /// AppleDB files an iPad's releases under iPadOS.+ /// AppleDB files an iPad's releases under iPadOS, a watch's under watchOS.public static func restoreURLs(forDevice device: String) async throws -> [String] {- try await releases(os: device.hasPrefix("iPad") ? "iPadOS" : "iOS", device: device)+ try await releases(os: device.hasPrefix("iPad") ? "iPadOS" : device.hasPrefix("Watch") ? "watchOS" : "iOS", device: device).map(\.url.absoluteString)}diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwarePicker.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwarePicker.swiftindex 978bd7ff..f5f9ab43 100644--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwarePicker.swift+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwarePicker.swift@@ -101,6 +101,7 @@ public enum VPhoneFirmwarePicker {let os = switch VPhoneGuestDevice.named(pairings.first?.device)?.family {case .iPad: "an iPadOS"case .appleTV: "a tvOS"+ case .watch: "a watchOS"case .iPhone, nil: "an iPhone"}let idx = try choose(diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneResources.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneResources.swiftindex 5c51beff..7638cccf 100644--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneResources.swift+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneResources.swift@@ -107,9 +107,9 @@ public struct VPhoneResources: Sendable {vphoned(for: .iPhone)}- /// The vphoned a guest of this family runs. Apple TV takes the same arm64- /// daemon marked as a tvOS executable: tvOS dyld refuses to load its- /// system libraries into an iOS-platform process.+ /// The vphoned a guest of this family runs. Apple TV and Apple Watch take+ /// the same arm64 daemon marked as a tvOS or watchOS executable: their+ /// dyld refuses to load its system libraries into an iOS-platform process.public func vphoned(for family: VPhoneGuestDevice.Family) -> URL {let name = Self.vphonedName(for: family)let bundled = guestResources.appendingPathComponent(name)@@ -121,7 +121,11 @@ public struct VPhoneResources: Sendable {}public static func vphonedName(for family: VPhoneGuestDevice.Family) -> String {- family == .appleTV ? "vphoned-tvos" : "vphoned"+ switch family {+ case .appleTV: "vphoned-tvos"+ case .watch: "vphoned-watchos"+ case .iPhone, .iPad: "vphoned"+ }}public var gpuCompilerPlugin: URL {diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swiftindex e10c2e05..6656915c 100644--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDevice.swift@@ -19,12 +19,14 @@ public struct VPhoneGuestDevice: Sendable, Equatable {case iPhonecase iPadcase appleTV+ case watchpublic var osName: String {switch self {case .iPhone: "iOS"case .iPad: "iPadOS"case .appleTV: "tvOS"+ case .watch: "watchOS"}}}@@ -171,6 +173,18 @@ public struct VPhoneGuestDevice: Sendable, Equatable {screen: .init(width: 3840, height: 2160, pixelsPerInch: 144, scale: 2.0),)+ /// Apple Watch Series 11 (GPS), 46 mm: 416x496 at 330 ppi, 208x248 points.+ /// The research board's kernel boots it with the watchOS kexts of its IPSW+ /// (the `watch` kernelcache recipe); N227b's device tree supplies the watch+ /// idiom and 2x identity.+ public static let watch7_18 = VPhoneGuestDevice(+ family: .watch,+ productType: "Watch7,18",+ deviceClass: "n227bap",+ productName: "Apple Watch Series 11 (GPS)",+ screen: .init(width: 416, height: 496, pixelsPerInch: 330, scale: 2.0),+ )+/// Whether the guest's userland is plain arm64 rather than arm64e: only/// the Apple TV HD's. It has no paravirtual GPU driver to load, and its/// dyld needs the TV TPRO patches.@@ -197,6 +211,7 @@ public struct VPhoneGuestDevice: Sendable, Equatable {iPad16_3, iPad16_5,iPad17_1, iPad17_3,appleTV5_3, appleTV14_1,+ watch7_18,]/// Cellular models that ship in the same IPSW as a known Wi-Fi model and map@@ -231,7 +246,7 @@ public struct VPhoneGuestDevice: Sendable, Equatable {public func restoreTreeName(version: String, build: String) -> String {switch family {case .iPhone: "\(productType)_\(version)_\(build)_Restore"- case .iPad, .appleTV: "iPhoneOS_\(productType)_\(version)_\(build)_Restore"+ case .iPad, .appleTV, .watch: "iPhoneOS_\(productType)_\(version)_\(build)_Restore"}}