Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

fix: preserve seal patch selection

秋奈月7a361e472704
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index 5bd89a0e..a75dbcc1 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -56,8 +56,10 @@
> executable's cdhash, preserves the two metadata bytes and restores v1 sort
> order. The 27.0.1 (24A446) IPSWs for iPhone17,1–5 and iPhone18,1–5 all
> have root-hash type `1` in both SystemVolume entries; iPhone19,2 has `0x106`.
-> SHA-256 images leave both cloudOS restore files pristine and omit the
-> image-specific patch from the resolved plan and restore receipt. Unknown
+> SHA-256 images leave both cloudOS restore files pristine. The logical plan
+> and template key keep the user's selection stable; `PatchPlan.plist` records
+> the patch as not applicable and omits it from the effective plan and restore
+> receipt. Unknown
> hash types or malformed metadata abort before patching; discovery,
> signature coverage or uniqueness failures also abort the SHA3 patch.
>
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipeline.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
index 4aab1ae1..af69f2d8 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
@@ -194,19 +194,13 @@ public final class FirmwarePipeline {
// The preset resolves here rather than in init: its version gates need the
// two ProductVersions that were just read, and a conflict or a misnamed
// patch must stop the run before a single byte is written.
- notApplicablePatches = []
- var plan = try resolvePlan(iOSBase: baseVersion, cloudOS: cloudOSVersion)
- let sealPatch = RestoreRamDiskSealPatcher.patchIdentifier
- let sealSelected = plan?.isEnabled(sealPatch)
- ?? (baseVersion?.major == 27 && cloudOSVersion?.major == 26 && cloudOSVersion?.minor == 4)
- if variant != .less, sealSelected {
- let hashType = try RestoreRamDiskSealPatcher.systemVolumeHashType(in: restoreDir)
- if hashType == RestoreRamDiskSealPatcher.sha256Type {
- notApplicablePatches.insert(sealPatch)
- plan = plan?.skippingFirmwareInapplicable(sealPatch)
- log("[=] \(sealPatch): SystemVolume root hash uses SHA-256; restore tools stay pristine")
- }
- }
+ let plan = try resolvePlan(iOSBase: baseVersion, cloudOS: cloudOSVersion)
+ try resolveFirmwareApplicability(
+ restoreDir: restoreDir,
+ iOSBase: baseVersion,
+ cloudOS: cloudOSVersion,
+ plan: plan,
+ )
resolvedPlan = plan
let gate = plan.map { VPhonePatchGate(plan: $0) } ?? .unrestricted
@@ -227,6 +221,24 @@ public final class FirmwarePipeline {
return allRecords
}
+ func resolveFirmwareApplicability(
+ restoreDir: URL,
+ iOSBase: VPhoneVersion?,
+ cloudOS: VPhoneVersion?,
+ plan: VPhonePatchPlan?,
+ ) throws {
+ notApplicablePatches = []
+ let sealPatch = RestoreRamDiskSealPatcher.patchIdentifier
+ let sealSelected = plan?.isEnabled(sealPatch)
+ ?? (iOSBase?.major == 27 && cloudOS?.major == 26 && cloudOS?.minor == 4)
+ guard variant != .less, sealSelected else { return }
+ let hashType = try RestoreRamDiskSealPatcher.systemVolumeHashType(in: restoreDir)
+ if hashType == RestoreRamDiskSealPatcher.sha256Type {
+ notApplicablePatches.insert(sealPatch)
+ log("[=] \(sealPatch): SystemVolume root hash uses SHA-256; restore tools stay pristine")
+ }
+ }
+
/// Patch a kernelcache file in place with this VM's selection, without a
/// restore tree. For `cfw update-kernel`: the pristine kernelcache is kept
/// in `FirmwareOriginals`, and only the Preboot copy needs replacing, so the
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift
index e88304b2..3f6e0bb6 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Pipeline/FirmwarePipelineComponents.swift
@@ -214,14 +214,11 @@ extension FirmwarePipeline {
// 6. Restore ramdisk and trust cache
let sealPatch = RestoreRamDiskSealPatcher.patchIdentifier
- var includeSealPatch = variant != .less
+ let includeSealPatch = variant != .less
&& includeBootChain
&& isEnabled(sealPatch, fallback: baseIs27 && cloudIs264)
+ && !notApplicablePatches.contains(sealPatch)
let manifestURL = restoreDir.appendingPathComponent("BuildManifest.plist")
- if includeSealPatch, FileManager.default.fileExists(atPath: manifestURL.path) {
- let hashType = try RestoreRamDiskSealPatcher.systemVolumeHashType(in: restoreDir)
- includeSealPatch = hashType == RestoreRamDiskSealPatcher.sha3_256_4KType
- }
// Empty factories restore both originals when the patch is disabled.
let includeSealComponents = includeSealPatch || (baseIs27 && cloudIs264)
if includeSealComponents {
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Restore/RestoreRamDiskSealPatcherTests.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Restore/RestoreRamDiskSealPatcherTests.swift
index e0a7b540..63117f55 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Restore/RestoreRamDiskSealPatcherTests.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcherTests/Restore/RestoreRamDiskSealPatcherTests.swift
@@ -31,24 +31,8 @@ struct RestoreRamDiskSealPatcherTests {
}
}
- @Test
- func `SHA-256 skips the seal patch without changing the user's selection`() throws {
- let identifier = RestoreRamDiskSealPatcher.patchIdentifier
- let plan = try VPhonePatchPlan.resolve(
- preset: FirmwarePatchSetCatalog.standardPreset,
- patchSets: FirmwarePatchSetCatalog.bundled,
- iOSBase: VPhoneVersion("27.0.1"),
- cloudOS: VPhoneVersion("26.4"),
- )
- #expect(plan.isEnabled(identifier))
- let skipped = plan.skippingFirmwareInapplicable(identifier)
- #expect(!skipped.isEnabled(identifier))
- #expect(skipped.selection == plan.selection)
- #expect(skipped.enabled == plan.enabled.subtracting([identifier]))
- }
-
@Test(arguments: [UInt32(1), UInt32(0x106)])
- func `restore tool factories follow the SystemVolume hash type`(_ hashType: UInt32) throws {
+ func `root hash metadata changes applicability without rewriting the plan`(_ hashType: UInt32) throws {
let root = try rootHashFixture(hashType: hashType)
defer { try? FileManager.default.removeItem(at: root) }
let pipeline = FirmwarePipeline(
@@ -61,6 +45,15 @@ struct RestoreRamDiskSealPatcherTests {
iOSBase: VPhoneVersion("27.0.1"),
cloudOS: VPhoneVersion("26.4"),
))
+ try pipeline.resolveFirmwareApplicability(
+ restoreDir: root,
+ iOSBase: VPhoneVersion("27.0.1"),
+ cloudOS: VPhoneVersion("26.4"),
+ plan: plan,
+ )
+ let identifier = RestoreRamDiskSealPatcher.patchIdentifier
+ #expect(plan.isEnabled(identifier))
+ #expect(pipeline.notApplicablePatches == (hashType == 1 ? Set([identifier]) : []))
let components = try pipeline.buildComponentList(
restoreDir: root,
iOSBase: VPhoneVersion("27.0.1"),
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
index 379ae8f3..880ec4fc 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
@@ -86,8 +86,9 @@ public struct VPhoneVirtualMachinePatchPlan: Codable, Sendable, Hashable {
) {
presetIdentifier = plan.presetIdentifier
patchSets = plan.patchSets.map(\.identifier)
- enabledPatches = plan.enabled.sorted()
- self.notApplicablePatches = notApplicablePatches.sorted()
+ let notApplicable = notApplicablePatches.intersection(plan.enabled)
+ enabledPatches = plan.enabled.subtracting(notApplicable).sorted()
+ self.notApplicablePatches = notApplicable.sorted()
iOSBaseVersion = iOSBase?.description
cloudOSVersion = cloudOS?.description
parameters = plan.parameters
@@ -98,6 +99,10 @@ public struct VPhoneVirtualMachinePatchPlan: Codable, Sendable, Hashable {
enabledPatches.contains(identifier)
}
+ public var selectedPatches: Set<String> {
+ Set(enabledPatches).union(notApplicablePatches)
+ }
+
private enum CodingKeys: String, CodingKey {
case presetIdentifier = "Preset"
case patchSets = "PatchSets"
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
index df2005c5..c8f868d6 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
@@ -193,7 +193,7 @@ enum VPhoneMachineTemplateKeys {
cloudOSBuild: info.cloudOS.build,
patchPreset: plan.presetIdentifier,
bootChainPlanDigest: VPhoneMachineTemplateKey.planDigest(
- bootChainPatches: bootChainPatches(plan.enabledPatches),
+ bootChainPatches: bootChainPatches(plan.selectedPatches),
parameters: plan.parameters,
),
bundleSeries: series,
@@ -288,8 +288,14 @@ enum VPhoneMachineTemplateKeys {
allowed: selection.allowedPatches,
)
{
- let planned = VPhonePatchPresetStore.plan(forVM: template.url).map { Set($0.enabledPatches) }
- drifted = drift(of: bundle, wanted: wanted.enabled, planned: planned)
+ let recordedPlan = VPhonePatchPresetStore.plan(forVM: template.url)
+ let notApplicable = Set(recordedPlan?.notApplicablePatches ?? [])
+ let planned = recordedPlan.map { Set($0.enabledPatches) }
+ drifted = drift(
+ of: bundle,
+ wanted: wanted.enabled.subtracting(notApplicable),
+ planned: planned,
+ )
}
return template.staleReasons(currentSeries: currentSeries, freshKey: fresh, driftedPatches: drifted)
}
diff --git a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPlan.swift b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPlan.swift
index 1da48f7c..bd867f1a 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPlan.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhonePatchKit/PatchSet/VPhonePatchPlan.swift
@@ -31,18 +31,6 @@ public struct VPhonePatchPlan: Sendable, Hashable {
enabled.contains(identifier)
}
- public func skippingFirmwareInapplicable(_ identifier: String) -> VPhonePatchPlan {
- VPhonePatchPlan(
- presetIdentifier: presetIdentifier,
- patchSets: patchSets,
- selection: selection,
- enabled: enabled.subtracting([identifier]),
- skippedByVersion: skippedByVersion,
- droppedBootEssentials: droppedBootEssentials,
- parameters: parameters,
- )
- }
-
/// Whether the patch that emits `recordIdentifier` runs.
///
/// Record identifiers are sometimes built at runtime (`kernel-boot-sandbox_ext.3`,