Run a virtual iPhone on an Apple Silicon Mac.
Swift
cli: Accept a preset file path for --preset
zqxwce810407cb7581
diff --git a/Skills/authoring-patch-sets/SKILL.md b/Skills/authoring-patch-sets/SKILL.mdindex 4c1c6fb0..75e62dca 100644--- a/Skills/authoring-patch-sets/SKILL.md+++ b/Skills/authoring-patch-sets/SKILL.md@@ -193,7 +193,12 @@ Presets are prewritten and reviewed. Nothing in the tool writes one; a VM recordonly which boxes its owner changed, and those compose on top.`standard` is what every VM gets. Any other preset must be asked for with-`--preset`. The shipped presets name every set but the restore image and differ only in their+`--preset`, which takes a preset name or a path to a preset plist (a value+with a `/` or a `.plist` suffix). A preset given as a file is copied into the+VM as `PatchPreset.plist`, so later steps — root `cfw install` included —+resolve it without the original; naming a preset again removes the copy. Its+identifier may not be a shipped one, nor contain `/` or end in `.plist`. Under+root the file must be a single-link regular file owned by the sudo user. The shipped presets name every set but the restore image and differ only in theirselection:```xml@@ -220,11 +225,12 @@ fails if they drift.### What a VM Records-A VM carries three patch records, each with its own writers:+A VM carries up to four patch records, each with its own writers:| File | What it says | Written by || --- | --- | --- || `<vm>/PatchSelection.plist` | what the owner wants: a preset plus the boxes they changed | `fw set-patches`; `vm create` (preset only); `fw patch` (rewrites it with what it ran, `--preset` included) |+| `<vm>/PatchPreset.plist` | the preset, when `--preset` named a file | `vm create`, `fw set-patches`, `fw patch` || `<vm>/PatchPlan.plist` | what the last `fw patch` resolved | `fw patch` || `<vm>/PatchReceipt.plist` | what is live in the guest, part by part | each step that puts patched bytes into the guest |diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swiftindex 9f20bda0..70b02dab 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePatchWorkspace.swift@@ -41,7 +41,9 @@ public struct FirmwarePatchWorkspace {do {let entries = try source.entries()treeNames = entries.filter { $0.contains("Restore") || $0 == VPhoneBundleOperations.firmwareOriginalsDirectoryName }- let roots = entries.filter { $0 == "config.plist" || $0 == "PatchSelection.plist" || $0 == "OpenAVB.bin" || Self.isAppleROM($0) }+ let roots = entries.filter {+ $0 == "config.plist" || $0 == "PatchSelection.plist" || $0 == Self.presetFileName || $0 == "OpenAVB.bin" || Self.isAppleROM($0)+ }for name in treeNames + roots {try Self.copyInput(name, from: source, into: privateMachine, owner: owner.uid)}@@ -51,6 +53,9 @@ public struct FirmwarePatchWorkspace {}}+ /// A VM's copy of a preset given to `--preset` as a file.+ public static let presetFileName = "PatchPreset.plist"+static func isAppleROM(_ name: String) -> Bool {name.hasPrefix("AVPBooter") && name.hasSuffix(".bin")}@@ -85,11 +90,15 @@ public struct FirmwarePatchWorkspace {public func publish() throws {let names = try privateMachine.entries().filter {treeNames.contains($0) || $0 == VPhoneBundleOperations.firmwareOriginalsDirectoryName- || $0 == "PatchSelection.plist" || $0 == "PatchPlan.plist" || Self.isAppleROM($0)+ || $0 == "PatchSelection.plist" || $0 == "PatchPlan.plist" || $0 == Self.presetFileName || Self.isAppleROM($0)}for name in names {try publish(name, from: privateMachine, to: source, relative: name)}+ // A named --preset dropped the VM's preset copy.+ if !names.contains(Self.presetFileName), try source.status(Self.presetFileName) != nil {+ try source.removeItem(Self.presetFileName)+ }}func publish(_ name: String, from input: VPhoneConfinedDirectory, to output: VPhoneConfinedDirectory, relative: String) throws {diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swiftindex ae7a6ff6..0128db3c 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneCustomFirmwareInstaller.swift@@ -870,7 +870,10 @@ struct VPhoneCustomFirmwareInstaller {?? VPhoneVirtualMachinePatchSelection()let plan = readPatchPlan(in: bundleDirectory)if let pristine = try firmwareOriginalsKernelcache(in: bundleDirectory),- let preset = VPhonePatchPresetStore.preset(named: selection.presetIdentifier),+ let preset = VPhonePatchPresetStore.preset(+ named: selection.presetIdentifier,+ vmCopy: try? bundleDirectory.readData(VPhonePatchPresetStore.vmPresetFileName),+ ),!preset.patchSets.contains(where: {if case .external = $0 {true@@ -2320,7 +2323,10 @@ struct VPhoneCustomFirmwareInstaller {.flatMap { try? PropertyListDecoder().decode(VPhoneVirtualMachinePatchSelection.self, from: $0) }?? VPhoneVirtualMachinePatchSelection()guard- let preset = VPhonePatchPresetStore.preset(named: plan.presetIdentifier),+ let preset = VPhonePatchPresetStore.preset(+ named: plan.presetIdentifier,+ vmCopy: try? bundleDirectory.readData(VPhonePatchPresetStore.vmPresetFileName),+ ),let resolved = try? VPhonePatchPlan.resolve(preset: preset,patchSets: FirmwarePatchSetCatalog.bundled,diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swiftindex b743db9d..b3dc4747 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift@@ -358,7 +358,7 @@ struct VPhoneFirmwarePatchCommand: ParsableCommand {@Argument(help: "VM name") var name: String?@Option(name: .customLong("preset"),- help: "Patch preset to apply. Defaults to the VM's recorded choice, or standard.",+ help: "Patch preset name, or path to a preset plist, to apply. Defaults to the VM's recorded choice, or standard.",)var preset: String?@Flag(name: .shortAndLong, help: "Suppress per-component progress") var quiet = false@@ -392,15 +392,8 @@ struct VPhoneFirmwarePatchCommand: ParsableCommand {// An explicit --preset wins and is remembered, so `cfw install` and a// later re-patch agree without the flag being repeated.var selection = VPhonePatchPresetStore.selection(forVM: directory)- if let preset {- selection.presetIdentifier = preset- }- guard let resolved = VPhonePatchPresetStore.preset(named: selection.presetIdentifier) else {- let available = VPhonePatchPresetStore.availablePresets().map(\.identifier)- throw ValidationError(- "Unknown patch preset '\(selection.presetIdentifier)'. Available: \(available.joined(separator: ", "))",- )- }+ let (resolved, presetFile) = try VPhonePatchPresetStore.resolve(preset ?? selection.presetIdentifier, forVM: directory)+ selection.presetIdentifier = resolved.identifier// The restore image patches merge and reseal the system image, which// needs root; nothing else here does.@@ -445,6 +438,9 @@ struct VPhoneFirmwarePatchCommand: ParsableCommand {let records = try pipeline.patchAll()if let plan = pipeline.resolvedPlan {+ if preset != nil {+ try VPhonePatchPresetStore.writePresetFile(presetFile, forVM: directory)+ }try VPhonePatchPresetStore.write(selection, forVM: directory)try VPhonePatchPresetStore.write(VPhoneVirtualMachinePatchPlan(@@ -479,7 +475,7 @@ struct VPhoneFirmwarePatchesCommand: ParsableCommand {@OptionGroup var lib: VPhoneLibraryOption@Argument(help: "VM name, to report what it is set to") var name: String?- @Option(name: .customLong("preset"), help: "Report against this preset instead of the VM's choice")+ @Option(name: .customLong("preset"), help: "Report against this preset name or preset plist path instead of the VM's choice")var preset: String?@Flag(name: .customLong("json"), help: "Emit machine-readable output for a UI")var json = false@@ -489,23 +485,22 @@ struct VPhoneFirmwarePatchesCommand: ParsableCommand {var selection = VPhoneVirtualMachinePatchSelection()var vmName: String?var machine: VPhonePatchCatalogReport.MachineRecords?+ var directory: URL?if let name {let resolvedName = try VPhoneVirtualMachineSelection.resolveExisting(name, in: lib.library)let bundle = try lib.library.bundle(named: resolvedName)vmName = resolvedName+ directory = bundle.urlselection = VPhonePatchPresetStore.selection(forVM: bundle.url)machine = .read(bundle)}- if let preset {- selection.presetIdentifier = preset- }+ let active = try VPhonePatchPresetStore.resolve(preset ?? selection.presetIdentifier, forVM: directory).preset+ selection.presetIdentifier = active.identifier- let presets = VPhonePatchPresetStore.availablePresets()- guard let active = presets.first(where: { $0.identifier == selection.presetIdentifier }) else {- throw ValidationError(- "Unknown patch preset '\(selection.presetIdentifier)'."- + " Available: \(presets.map(\.identifier).joined(separator: ", "))",- )+ // A VM's own preset is listed too, so a picker can show what it is set to.+ var presets = VPhonePatchPresetStore.availablePresets()+ if !presets.contains(where: { $0.identifier == active.identifier }) {+ presets.append(active)}let report = VPhonePatchCatalogReport(@@ -570,7 +565,7 @@ struct VPhoneFirmwareSetPatchesCommand: ParsableCommand {@Argument(help: "VM name") var name: String?@Option(name: .customLong("preset"),- help: "Preset the choice composes onto. Defaults to the VM's recorded preset.",+ help: "Preset name, or path to a preset plist, the choice composes onto. Defaults to the VM's recorded preset.",)var preset: String?@Option(@@ -589,15 +584,8 @@ struct VPhoneFirmwareSetPatchesCommand: ParsableCommand {let bundle = try lib.library.bundle(named: name)var selection = VPhonePatchPresetStore.selection(forVM: bundle.url)- if let preset {- selection.presetIdentifier = preset- }- guard let resolved = VPhonePatchPresetStore.preset(named: selection.presetIdentifier) else {- let available = VPhonePatchPresetStore.availablePresets().map(\.identifier)- throw ValidationError(- "Unknown patch preset '\(selection.presetIdentifier)'. Available: \(available.joined(separator: ", "))",- )- }+ let (resolved, presetFile) = try VPhonePatchPresetStore.resolve(preset ?? selection.presetIdentifier, forVM: bundle.url)+ selection.presetIdentifier = resolved.identifierlet declarations = FirmwarePatchSetCatalog.allDeclarationslet declared = Set(declarations.map(\.identifier))@@ -629,6 +617,9 @@ struct VPhoneFirmwareSetPatchesCommand: ParsableCommand {))}+ if preset != nil {+ try VPhonePatchPresetStore.writePresetFile(presetFile, forVM: bundle.url)+ }try VPhonePatchPresetStore.write(selection, forVM: bundle.url)try VPhoneHostFilePermissions.makeAccessible(at: bundle.url)print("[fw set-patches] \(name): preset \(selection.presetIdentifier)"diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swiftindex 14e57da2..1d1af535 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift@@ -119,7 +119,7 @@ enum VPhoneFirmwarePreparer {// configuration with the GPU disabled must not restore another PCC VM// merely to recover a driver that will never be installed.let selection = VPhonePatchPresetStore.selection(forVM: bundle.url)- guard let preset = VPhonePatchPresetStore.preset(named: selection.presetIdentifier) else {+ guard let preset = VPhonePatchPresetStore.preset(named: selection.presetIdentifier, forVM: bundle.url) else {let available = VPhonePatchPresetStore.availablePresets().map(\.identifier)throw ValidationError("Unknown patch preset '\(selection.presetIdentifier)'. Available: \(available.joined(separator: ", "))")}diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swiftindex 880ec4fc..a29fcb19 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift@@ -1,6 +1,6 @@// VPhonePatchPresetStore.swift — Finding presets, and remembering a VM's choice.//-// Three files matter here.+// Four files matter here.//// `VPhone.bundle/Contents/Resources/patches_presets/*.plist` are the presets the// bundle ships. They are prewritten and reviewed; nothing in the tool writes one.@@ -13,7 +13,11 @@// boot chain is patched. `cfw install` reads it so the guest half of the install// applies the same selection the boot chain was built with, without resolving the// preset a second time against versions that may since have changed.+//+// `<vm>/PatchPreset.plist` is a preset given to `--preset` as a file, kept so+// later steps, root ones included, resolve it without the original.+import ArgumentParserimport FirmwarePatcherimport Foundationimport VPhoneCoreKit@@ -266,15 +270,7 @@ public enum VPhonePatchPresetStore {/// outside it. A user preset cannot shadow a shipped one — an identifier/// already claimed is ignored, so nothing can redefine `standard`.public static func availablePresets() -> [VPhonePatchPreset] {- var presets: [VPhonePatchPreset] = []- if let directory = presetsDirectory(),- let shipped = try? VPhonePatchPreset.readAll(fromDirectory: directory),- !shipped.isEmpty- {- presets = shipped- } else {- presets = FirmwarePatchSetCatalog.builtInPresets- }+ var presets = shippedPresets()let claimed = Set(presets.map(\.identifier))if let user = try? VPhonePatchPreset.readAll(fromDirectory: userPresetsDirectory()) {presets.append(contentsOf: user.filter { !claimed.contains($0.identifier) })@@ -288,11 +284,131 @@ public enum VPhonePatchPresetStore {.appendingPathComponent(VPhonePatchPreset.resourceDirectoryName, isDirectory: true)}+ static func shippedPresets() -> [VPhonePatchPreset] {+ if let directory = presetsDirectory(),+ let shipped = try? VPhonePatchPreset.readAll(fromDirectory: directory),+ !shipped.isEmpty+ {+ return shipped+ }+ return FirmwarePatchSetCatalog.builtInPresets+ }+/// The preset with this identifier, or nil.public static func preset(named identifier: String) -> VPhonePatchPreset? {availablePresets().first { $0.identifier == identifier }}+ /// The preset with this identifier for one VM: its own copy+ /// (``vmPresetFileName``) when that is the one named, else ``preset(named:)``.+ public static func preset(named identifier: String, forVM directory: URL) -> VPhonePatchPreset? {+ preset(named: identifier, vmCopy: vmCopyData(forVM: directory))+ }++ /// As ``preset(named:forVM:)``, from the VM copy's bytes, for a reader+ /// that opens the VM folder itself.+ public static func preset(named identifier: String, vmCopy data: Data?) -> VPhonePatchPreset? {+ vmCopy(named: identifier, data: data) ?? preset(named: identifier)+ }++ private static func vmCopyData(forVM directory: URL) -> Data? {+ try? Data(contentsOf: directory.appendingPathComponent(vmPresetFileName))+ }++ /// The VM's copy, when it is the preset named. Never a shipped identifier.+ private static func vmCopy(named identifier: String, data: Data?) -> VPhonePatchPreset? {+ guard let data, let copy = try? VPhonePatchPreset.decode(data), copy.identifier == identifier,+ !shippedPresets().contains(where: { $0.identifier == identifier })+ else { return nil }+ return copy+ }++ // MARK: Preset Files++ /// A preset given to `--preset` as a file, kept in the VM so every later+ /// step resolves the same preset without the original file.+ public static let vmPresetFileName = FirmwarePatchWorkspace.presetFileName++ /// `--preset` names a file when it has a path separator or a `.plist` suffix.+ public static func isPresetFile(_ argument: String) -> Bool {+ argument.contains("/") || argument.hasSuffix(".plist")+ }++ /// A `--preset` value: a preset file, or a name resolved for `directory`+ /// when given. `file` is the preset the VM keeps a copy of: one read from a+ /// file, or the VM's existing copy when the name is its identifier.+ public static func resolve(+ _ argument: String,+ forVM directory: URL? = nil,+ ) throws -> (preset: VPhonePatchPreset, file: VPhonePatchPreset?) {+ if isPresetFile(argument) {+ let preset = try readPresetFile(argument)+ return (preset, preset)+ }+ if let directory, let copy = vmCopy(named: argument, data: vmCopyData(forVM: directory)) {+ return (copy, copy)+ }+ guard let found = preset(named: argument) else {+ let available = availablePresets().map(\.identifier)+ throw ValidationError("Unknown patch preset '\(argument)'. Available: \(available.joined(separator: ", "))")+ }+ return (found, nil)+ }++ /// Reads a preset file. Under root only a single-link regular file owned by+ /// the sudo user is opened, never a link, so root opens nothing the+ /// invoking user could not.+ static func readPresetFile(_ path: String) throws -> VPhonePatchPreset {+ let url = URL(fileURLWithPath: (path as NSString).expandingTildeInPath)+ let data: Data+ if geteuid() == 0 {+ guard let user = VPhoneInvokingUser.current else {+ throw ValidationError("Root reads a preset file only under sudo; name a preset instead.")+ }+ let descriptor = open(url.path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)+ guard descriptor >= 0 else {+ throw ValidationError("Cannot open preset file \(url.path): \(String(cString: strerror(errno)))")+ }+ let handle = FileHandle(fileDescriptor: descriptor, closeOnDealloc: true)+ var info = stat()+ guard fstat(descriptor, &info) == 0, info.st_mode & S_IFMT == S_IFREG,+ info.st_nlink == 1, info.st_uid == user.uid+ else {+ throw ValidationError("Preset file \(url.path) must be a regular file owned by the invoking user.")+ }+ data = try handle.readToEnd() ?? Data()+ } else {+ data = try Data(contentsOf: url)+ }+ let preset: VPhonePatchPreset+ do {+ preset = try VPhonePatchPreset.decode(data)+ } catch {+ throw ValidationError("Preset file \(url.path) is not a valid preset: \(error)")+ }+ // The VM records the identifier, and reading it back must not take it for a path.+ if isPresetFile(preset.identifier) {+ throw ValidationError("Preset file \(url.path) has the identifier '\(preset.identifier)'; an identifier has no '/' and no .plist suffix.")+ }+ // As for ~/.vphone/patches_presets: nothing can redefine a shipped preset.+ if shippedPresets().contains(where: { $0.identifier == preset.identifier }) {+ throw ValidationError("Preset file \(url.path) uses the identifier '\(preset.identifier)' of a shipped preset; give it its own.")+ }+ return preset+ }++ /// Keeps `file` as the VM's preset copy, or removes the copy when nil.+ public static func writePresetFile(_ file: VPhonePatchPreset?, forVM directory: URL) throws {+ let url = directory.appendingPathComponent(vmPresetFileName)+ guard let file else {+ if FileManager.default.fileExists(atPath: url.path) {+ try FileManager.default.removeItem(at: url)+ }+ return+ }+ try file.encodedPlist().write(to: url, options: .atomic)+ }+/// `Contents/Resources/patches_presets` beside the running executable.static func presetsDirectory() -> URL? {// Not argv[0]: a `vphone-cli` found on PATH gets a bare name there, whichdiff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VPhoneCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VPhoneCommand.swiftindex 73102b15..74a9bed6 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VPhoneCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VPhoneCommand.swift@@ -47,12 +47,13 @@ struct PatchFirmwareCommand: ParsableCommand {@Option(name: .customLong("preset"),- help: "Patch preset to apply. Defaults to standard. Individual patches are selected per VM by `fw patch`, not here.",+ help: "Patch preset name, or path to a preset plist, to apply. Defaults to standard. Individual patches are selected per VM by `fw patch`, not here.",)var preset: String = VPhonePatchPreset.standardIdentifiermutating func run() throws {- guard let resolved = FirmwarePatchSetCatalog.builtInPresets.first(where: { $0.identifier == preset })+ let file = try VPhonePatchPresetStore.isPresetFile(preset) ? VPhonePatchPresetStore.readPresetFile(preset) : nil+ guard let resolved = file ?? FirmwarePatchSetCatalog.builtInPresets.first(where: { $0.identifier == preset })else {let available = FirmwarePatchSetCatalog.builtInPresets.map(\.identifier)throw ValidationError(diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swiftindex 4c1e3987..5d285c98 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift@@ -42,16 +42,12 @@ enum VPhoneMachineTemplateKeys {/// Resolves the preset against the two versions the way `fw patch` will.static func plan(- preset identifier: String,+ preset: VPhonePatchPreset,iOSVersion: String,cloudOSVersion: String,blocked: [String] = [],allowed: [String] = [],) throws -> VPhonePatchPlan {- guard let preset = VPhonePatchPresetStore.preset(named: identifier) else {- let available = VPhonePatchPresetStore.availablePresets().map(\.identifier)- throw ValidationError("Unknown patch preset '\(identifier)'. Available: \(available.joined(separator: ", "))")- }let pipeline = FirmwarePipeline(vmDirectory: FileManager.default.temporaryDirectory,variant: .jb,@@ -61,7 +57,7 @@ enum VPhoneMachineTemplateKeys {allowedPatches: Set(allowed),)guard let plan = try pipeline.resolvePlan(iOSBase: VPhoneVersion(iOSVersion), cloudOS: VPhoneVersion(cloudOSVersion)) else {- throw ValidationError("Patch preset '\(identifier)' did not resolve.")+ throw ValidationError("Patch preset '\(preset.identifier)' did not resolve.")}return plan}@@ -70,7 +66,7 @@ enum VPhoneMachineTemplateKeys {device: String,ios: VPhoneRestoreInfo.OSVersion,cloudOS: VPhoneRestoreInfo.OSVersion,- preset: String,+ preset: VPhonePatchPreset,blocked: [String] = [],allowed: [String] = [],diskSizeGB: UInt64,@@ -277,11 +273,14 @@ enum VPhoneMachineTemplateKeys {static func staleReasons(_ template: VPhoneMachineTemplate) -> [String] {let key = template.keylet selection = VPhonePatchPresetStore.selection(forVM: template.url)+ guard let preset = VPhonePatchPresetStore.preset(named: key.patchPreset, forVM: template.url) else {+ return template.staleReasons(currentSeries: currentSeries, freshKey: nil)+ }let fresh = try? self.key(device: key.device,ios: .init(version: key.iOSVersion, build: key.iOSBuild),cloudOS: .init(version: key.cloudOSVersion, build: key.cloudOSBuild),- preset: key.patchPreset,+ preset: preset,blocked: selection.blockedPatches,allowed: selection.allowedPatches,diskSizeGB: key.diskSizeGB,@@ -291,7 +290,7 @@ enum VPhoneMachineTemplateKeys {var drifted: [String] = []if let bundle = try? template.bundle(),let wanted = try? plan(- preset: key.patchPreset,+ preset: preset,iOSVersion: key.iOSVersion,cloudOSVersion: key.cloudOSVersion,blocked: selection.blockedPatches,diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swiftindex af346216..04fcb455 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateCommand.swift@@ -64,7 +64,7 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {@Option(name: .shortAndLong, help: "Disk size (GB, default 64)") var diskSize: UInt64?@Option(name: .customLong("preset"),- help: "Patch preset for the new VM. Defaults to standard; run `fw patches` to see what each one applies.",+ help: "Patch preset name, or path to a preset plist, for the new VM. Defaults to standard; run `fw patches` to see what each one applies.",)var preset: String?@Option(name: .long, help: "CPU cores (default 8)") var cpu: UInt?@@ -114,10 +114,14 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {/// offline trim and the setup boot of a template write the System volume,/// which would break that system's seal.private var customisesRestoredSystem: Bool {- VPhonePatchPresetStore.preset(named: preset ?? defaultPreset)?.patchSets+ (try? resolvedPreset().preset)?.patchSets.contains { $0.identifier == FirmwareRestoreImagePatchSet.identifier } ?? false}+ private func resolvedPreset() throws -> (preset: VPhonePatchPreset, file: VPhonePatchPreset?) {+ try VPhonePatchPresetStore.resolve(preset ?? defaultPreset)+ }+/// 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. An/// Apple Watch takes the watch preset, an iPhone XS Max `minimal`: its iOS 18@@ -195,6 +199,7 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {if let device, VPhoneGuestDevice.named(device) == nil {throw ValidationError("vphone runs \(VPhoneGuestDevice.known.map(\.productType).joined(separator: ", ")) guests, not \(device).")}+ let patchPreset = try resolvedPreset()let networkMode = try network.map(VPhoneVirtualMachineConfigCommand.parseMode)let unlocks = try unlockAtStartup.map { try VPhoneVirtualMachineConfigCommand.parseSwitch($0, option: "--unlock-at-startup") }let templateUse: VPhoneVirtualMachineCreator.TemplateUse = if let template {@@ -238,7 +243,8 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {device: selectedDevice,otaSource: otaSource,otaKey: otaKey,- patchPreset: preset ?? defaultPreset,+ patchPreset: patchPreset.preset.identifier,+ patchPresetFile: patchPreset.file,cpuCount: cpu ?? 8,memoryMB: memory ?? 8192,diskSizeGB: diskSize ?? 64,@@ -249,7 +255,7 @@ struct VPhoneVirtualMachineCreateCommand: ParsableCommand {template: templateUse,templateRequest: VPhoneMachineTemplateRequest(device: device,- patchPreset: preset,+ patchPreset: preset == nil ? nil : patchPreset.preset.identifier,diskSizeGB: diskSize,slimming: slimming.request.isEmpty ? nil : slimming.resolve(),),diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateOptions.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateOptions.swiftindex 3578e26c..3f9363e6 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateOptions.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreateOptions.swift@@ -31,6 +31,8 @@ public extension VPhoneVirtualMachineCreator {/// Which patch preset the new VM is built with. Individual patches are/// turned on or off per VM afterwards, through its patch selection.public var patchPreset: String+ /// The preset when `--preset` named a file; kept in the machine.+ public var patchPresetFile: VPhonePatchPreset?public var cpuCount: UIntpublic var memoryMB: UInt64public var diskSizeGB: UInt64@@ -61,6 +63,7 @@ public extension VPhoneVirtualMachineCreator {otaSource: String? = nil,otaKey: String? = nil,patchPreset: String = VPhonePatchPreset.standardIdentifier,+ patchPresetFile: VPhonePatchPreset? = nil,cpuCount: UInt = 8,memoryMB: UInt64 = 8192,diskSizeGB: UInt64 = 64,@@ -82,6 +85,7 @@ public extension VPhoneVirtualMachineCreator {self.otaSource = otaSourceself.otaKey = otaKeyself.patchPreset = patchPreset+ self.patchPresetFile = patchPresetFileself.cpuCount = cpuCountself.memoryMB = memoryMBself.diskSizeGB = diskSizeGBdiff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swiftindex 588d939c..a5c685ed 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineCreator.swift@@ -190,6 +190,7 @@ public struct VPhoneVirtualMachineCreator {let v = options.verbosity// `fw prepare` stages what the selection needs (the GPU driver, the seal// tool), so the preset has to be on record before it runs.+ try VPhonePatchPresetStore.writePresetFile(options.patchPresetFile, forVM: bundleURL)try VPhonePatchPresetStore.write(VPhoneVirtualMachinePatchSelection(presetIdentifier: options.patchPreset),forVM: bundleURL,@@ -255,6 +256,7 @@ public struct VPhoneVirtualMachineCreator {guard slimming.problems.isEmpty else {throw ValidationError("Cannot build this template: \(slimming.problems.joined(separator: "; ")). Pass --trim none.")}+ let preset = try options.patchPresetFile ?? VPhonePatchPresetStore.resolve(options.patchPreset).preset// Resolved as vm template find resolves it: from the IPSWs when they// are local or cached, else from a template recorded with the same// sources. Nothing is downloaded unless no template matches and one@@ -271,7 +273,7 @@ public struct VPhoneVirtualMachineCreator {device: builds.device,ios: builds.ios,cloudOS: builds.cloudOS,- preset: options.patchPreset,+ preset: preset,diskSizeGB: options.diskSizeGB,slimming: slimming,)@@ -296,7 +298,7 @@ public struct VPhoneVirtualMachineCreator {device: sources.device.productType,ios: .init(version: sources.phone.version, build: sources.phone.build),cloudOS: .init(version: sources.cloud.version, build: sources.cloud.build),- preset: options.patchPreset,+ preset: preset,diskSizeGB: options.diskSizeGB,slimming: slimming,)@@ -525,7 +527,7 @@ public struct VPhoneVirtualMachineCreator {verbosity v: VPhoneVerbosity,) throws {trace("in-process FirmwarePipeline.patchAll variant=jb preset=\(presetIdentifier)", v)- guard let preset = VPhonePatchPresetStore.preset(named: presetIdentifier) else {+ guard let preset = VPhonePatchPresetStore.preset(named: presetIdentifier, forVM: bundleURL) else {let available = VPhonePatchPresetStore.availablePresets().map(\.identifier)throw ValidationError("Unknown patch preset '\(presetIdentifier)'. Available: \(available.joined(separator: ", "))",diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTemplateCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTemplateCommand.swiftindex c7ac6a92..77534abf 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTemplateCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTemplateCommand.swift@@ -253,7 +253,7 @@ struct VPhoneVirtualMachineTemplateFindCommand: ParsableCommand {var ipswCache: String?@Option(help: ArgumentHelp("Guest device, when the IPSW covers several models", valueName: "product-type"))var device: String?- @Option(name: .customLong("preset"), help: "Patch preset (default standard)") var preset: String?+ @Option(name: .customLong("preset"), help: "Patch preset name or preset plist path (default standard)") var preset: String?@Option(name: .shortAndLong, help: "Disk size (GB, default 64)") var diskSize: UInt64?@Option(name: .customLong("block"), help: ArgumentHelp("A patch the preset turns on, turned off", valueName: "id"))var blocked: [String] = []@@ -277,7 +277,7 @@ struct VPhoneVirtualMachineTemplateFindCommand: ParsableCommand {device: builds.device,ios: builds.ios,cloudOS: builds.cloudOS,- preset: preset ?? VPhonePatchPreset.standardIdentifier,+ preset: VPhonePatchPresetStore.resolve(preset ?? VPhonePatchPreset.standardIdentifier).preset,blocked: blocked,allowed: allowed,diskSizeGB: diskSize ?? 64,