Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Stop fw prepare when the two IPSWs are swapped or wrong

Lakr233bc44f4e207ab
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift
index a29a6018..d5f59bb9 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwarePreparer.swift
@@ -55,6 +55,7 @@ enum VPhoneFirmwarePreparer {
let cloud = try vphoneRunBlocking {
try await VPhoneIPSWCache.resolve(cloudOSSource, in: cacheDirectory)
}
+ try VPhoneIPSWCache.checkPair(iPhone: phone, cloudOS: cloud)
try checkIPhoneName(iPhoneSource, archive: phone)
print("[+] iPhone \(phone.version) (\(phone.build)); cloudOS \(cloud.version) (\(cloud.build))")
diff --git a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
index e8a54a10..1284c3a0 100644
--- a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneIPSWCache.swift
@@ -10,6 +10,10 @@ public enum VPhoneIPSWCache {
public let file: URL
public let version: String
public let build: String
+ /// `SupportedProductTypes`, such as `iPhone17,3`.
+ public let productTypes: [String]
+ /// Every build identity's `Info.DeviceClass`, such as `vresearch101ap`.
+ public let deviceClasses: Set<String>
}
public enum Error: Swift.Error, LocalizedError {
@@ -18,6 +22,9 @@ public enum VPhoneIPSWCache {
case invalidManifest(URL)
case unexpectedHTTP(URL, Int)
case incompleteDownload(URL, expected: Int64, actual: Int64)
+ case swappedSources(iPhone: URL, cloudOS: URL)
+ case notIPhoneSource(URL, productTypes: [String])
+ case notCloudOSSource(URL)
public var errorDescription: String? {
switch self {
@@ -27,6 +34,12 @@ public enum VPhoneIPSWCache {
case let .unexpectedHTTP(url, _): "Unable to download the IPSW from \(url). Try again later."
case let .incompleteDownload(url, expected, actual):
"The IPSW download from \(url) is incomplete (\(actual) of \(expected) bytes). Try again."
+ case let .swappedSources(iPhone, cloudOS):
+ "The iPhone and cloudOS IPSWs are swapped: \(iPhone.lastPathComponent) is a cloudOS IPSW and \(cloudOS.lastPathComponent) is an iPhone IPSW. Swap the two sources, then try again."
+ case let .notIPhoneSource(file, productTypes):
+ "\(file.lastPathComponent) is not an \(VPhoneIPSWCache.iPhoneProductType) IPSW; it is for \(productTypes.isEmpty ? "no listed product" : productTypes.joined(separator: ", ")). Choose an \(VPhoneIPSWCache.iPhoneProductType) IPSW as the iPhone source."
+ case let .notCloudOSSource(file):
+ "\(file.lastPathComponent) is not a cloudOS IPSW: it has no \(VPhoneIPSWCache.cloudOSDeviceClass) build identity. Choose a cloudOS IPSW as the cloudOS source."
}
}
}
@@ -98,7 +111,13 @@ public enum VPhoneIPSWCache {
try fm.moveItem(at: pending, to: cache)
try VPhoneHostFilePermissions.makeAccessible(at: cache)
try VPhoneHostFilePermissions.makeDirectoryAccessible(at: cacheDirectory)
- return Archive(file: cache, version: metadata.version, build: metadata.build)
+ return Archive(
+ file: cache,
+ version: metadata.version,
+ build: metadata.build,
+ productTypes: metadata.productTypes,
+ deviceClasses: metadata.deviceClasses,
+ )
}
public static func inspect(_ file: URL) throws -> Archive {
@@ -113,7 +132,43 @@ public enum VPhoneIPSWCache {
else {
throw Error.invalidManifest(file)
}
- return Archive(file: file, version: version, build: build)
+ let identities = plist["BuildIdentities"] as? [[String: Any]] ?? []
+ let deviceClasses = identities.compactMap { identity in
+ ((identity["Info"] as? [String: Any])?["DeviceClass"] as? String)?.lowercased()
+ }
+ return Archive(
+ file: file,
+ version: version,
+ build: build,
+ productTypes: plist["SupportedProductTypes"] as? [String] ?? [],
+ deviceClasses: Set(deviceClasses),
+ )
+ }
+
+ // MARK: - Pairing
+
+ /// The iPhone IPSW's product, and the cloudOS device class whose boot
+ /// chain matches the VM's DFU hardware. The restore tree needs both.
+ public static let iPhoneProductType = VPhoneFirmwareCatalog.device
+ public static let cloudOSDeviceClass = "vresearch101ap"
+
+ /// Checks each BuildManifest before anything is extracted, so a swapped
+ /// or wrong IPSW fails at once with the fix instead of deep in the merge.
+ public static func checkPair(iPhone: Archive, cloudOS: Archive) throws {
+ let iPhoneIsPhone = iPhone.productTypes.contains(iPhoneProductType)
+ let cloudOSIsCloudOS = cloudOS.deviceClasses.contains(cloudOSDeviceClass)
+ if !iPhoneIsPhone, !cloudOSIsCloudOS,
+ iPhone.deviceClasses.contains(cloudOSDeviceClass),
+ cloudOS.productTypes.contains(iPhoneProductType)
+ {
+ throw Error.swappedSources(iPhone: iPhone.file, cloudOS: cloudOS.file)
+ }
+ guard iPhoneIsPhone else {
+ throw Error.notIPhoneSource(iPhone.file, productTypes: iPhone.productTypes)
+ }
+ guard cloudOSIsCloudOS else {
+ throw Error.notCloudOSSource(cloudOS.file)
+ }
}
static func cacheName(for url: URL) -> String {
diff --git a/VPhoneKit/VPhoneArchiveKitTests/IPSWCacheTests.swift b/VPhoneKit/VPhoneArchiveKitTests/IPSWCacheTests.swift
index 34226656..5ac36c10 100644
--- a/VPhoneKit/VPhoneArchiveKitTests/IPSWCacheTests.swift
+++ b/VPhoneKit/VPhoneArchiveKitTests/IPSWCacheTests.swift
@@ -119,4 +119,51 @@ struct IPSWCacheTests {
atPath: cacheDir.appendingPathComponent(VPhoneIPSWCache.cacheName(for: url)).path,
))
}
+
+ // MARK: - Pairing
+
+ private func ipsw(in root: URL, _ name: String, productTypes: [String], deviceClasses: [String]) throws -> VPhoneIPSWCache.Archive {
+ let files = root.appendingPathComponent(name, isDirectory: true)
+ try FileManager.default.createDirectory(at: files, withIntermediateDirectories: true)
+ let manifest: [String: Any] = [
+ "ProductVersion": "27.0", "ProductBuildVersion": "24A435",
+ "SupportedProductTypes": productTypes,
+ "BuildIdentities": deviceClasses.map { ["Info": ["DeviceClass": $0]] },
+ ]
+ let data = try PropertyListSerialization.data(fromPropertyList: manifest, format: .xml, options: 0)
+ try data.write(to: files.appendingPathComponent("BuildManifest.plist"))
+ let archive = root.appendingPathComponent("\(name).ipsw")
+ try VPhoneArchiveWriter.create(archive: archive, from: files)
+ return try VPhoneIPSWCache.inspect(archive)
+ }
+
+ @Test func `pair check reads each manifest and names the mistake`() throws {
+ let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: root) }
+ let phone = try ipsw(in: root, "phone", productTypes: ["iPhone17,3"], deviceClasses: ["D47AP", "D47AP"])
+ let cloud = try ipsw(in: root, "cloud", productTypes: ["iProd99,1"], deviceClasses: ["vresearch101ap", "vphone600ap"])
+ let other = try ipsw(in: root, "other", productTypes: ["iPhone16,1"], deviceClasses: ["d83ap"])
+
+ #expect(phone.productTypes == ["iPhone17,3"])
+ #expect(phone.deviceClasses == ["d47ap"])
+ #expect(cloud.deviceClasses == ["vresearch101ap", "vphone600ap"])
+
+ try VPhoneIPSWCache.checkPair(iPhone: phone, cloudOS: cloud)
+ #expect {
+ try VPhoneIPSWCache.checkPair(iPhone: cloud, cloudOS: phone)
+ } throws: { error in
+ if case .swappedSources? = error as? VPhoneIPSWCache.Error { true } else { false }
+ }
+ #expect {
+ try VPhoneIPSWCache.checkPair(iPhone: other, cloudOS: cloud)
+ } throws: { error in
+ if case .notIPhoneSource? = error as? VPhoneIPSWCache.Error { true } else { false }
+ }
+ #expect {
+ try VPhoneIPSWCache.checkPair(iPhone: phone, cloudOS: phone)
+ } throws: { error in
+ if case .notCloudOSSource? = error as? VPhoneIPSWCache.Error { true } else { false }
+ }
+ }
}