Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Show the build and download state of an Apple TV 4K OTA pairing

Jackybb9b629a96da
diff --git a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneOTAArchive.swift b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneOTAArchive.swift
index 695890ca..56b52b00 100644
--- a/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneOTAArchive.swift
+++ b/VPhoneKit/VPhoneArchiveKit/Transfer/VPhoneOTAArchive.swift
@@ -518,7 +518,7 @@ public enum VPhoneOTAArchive {
}
let fm = FileManager.default
- let directory = cacheDirectory.appendingPathComponent("ota", isDirectory: true)
+ let directory = cacheDirectory.appendingPathComponent(VPhoneOTARelease.cacheDirectoryName, isDirectory: true)
try fm.createDirectory(at: directory, withIntermediateDirectories: true)
try VPhoneHostFilePermissions.makeDirectoryAccessible(at: directory)
let cache = directory.appendingPathComponent(name)
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift
index 47819a33..9223523b 100644
--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift
+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneFirmwareCatalog.swift
@@ -67,6 +67,9 @@ public struct VPhoneOTARelease: Sendable, Equatable {
self.baseVersion = baseVersion ?? version
}
+ /// The IPSW cache's folder for OTAs.
+ public static let cacheDirectoryName = "ota"
+
/// The name the IPSW cache keeps it under, in its `ota` folder.
public var fileName: String {
"\(productType)_\(version)_\(build)_OTA.aea"
@@ -526,8 +529,15 @@ public enum VPhoneFirmwareCatalog {
public static var report: VPhoneFirmwareCatalogReport {
func entries(_ pairings: [VPhoneFirmwarePairing]) -> [VPhoneFirmwareCatalogReport.Entry] {
pairings.map {
- .init(
- ios: .init(name: $0.iosName, url: $0.iosURL),
+ let ota = otaRelease(source: $0.iosURL)
+ let base = ota.flatMap(restoreBaseURL(for:)).map { ($0 as NSString).lastPathComponent }
+ return .init(
+ ios: .init(
+ name: $0.iosName,
+ url: $0.iosURL,
+ build: ota?.build,
+ files: ota.map { ["\(VPhoneOTARelease.cacheDirectoryName)/\($0.fileName)"] + (base.map { [$0] } ?? []) },
+ ),
recommendedCloudOS: .init(name: $0.cloudosName, url: $0.cloudosURL),
)
}
@@ -559,8 +569,14 @@ public struct VPhoneFirmwareCatalogReport: Codable, Equatable, Sendable {
public struct Firmware: Codable, Equatable, Sendable {
public let name: String
public let url: String
- public init(name: String, url: String) {
- self.name = name; self.url = url
+ /// The build, when `url`'s file name does not carry it: an OTA's.
+ public let build: String?
+ /// The files `fw prepare` needs in the IPSW cache, relative to it, when
+ /// `url`'s file alone is not all: an OTA in `ota/` and the base IPSW it
+ /// is restored over. Absent for an IPSW.
+ public let files: [String]?
+ public init(name: String, url: String, build: String? = nil, files: [String]? = nil) {
+ self.name = name; self.url = url; self.build = build; self.files = files
}
}
diff --git a/VPhoneKit/VPhoneCoreKitTests/Firmware/FirmwareCatalogReportTests.swift b/VPhoneKit/VPhoneCoreKitTests/Firmware/FirmwareCatalogReportTests.swift
index 5b36362e..8db3fc09 100644
--- a/VPhoneKit/VPhoneCoreKitTests/Firmware/FirmwareCatalogReportTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/Firmware/FirmwareCatalogReportTests.swift
@@ -16,6 +16,18 @@ struct FirmwareCatalogReportTests {
}
}
+ @Test func `an OTA pairing names its build and the files it needs`() throws {
+ let tv4K = try #require(VPhoneFirmwareCatalog.report.devices.first { $0.productType == "AppleTV14,1" })
+ let ipsw = try #require(tv4K.pairings.first { $0.ios.name == "tvOS 26.6" })
+ let ota = try #require(tv4K.pairings.first { $0.ios.name == "tvOS 27.0" })
+ #expect(ipsw.ios.build == nil && ipsw.ios.files == nil)
+ #expect(ota.ios.build == "24J361")
+ #expect(ota.ios.files == ["ota/AppleTV14,1_27.0_24J361_OTA.aea", "AppleTV5,3_26.6_23L773_Restore.ipsw"])
+ // An IPSW's entry encodes as before, so an older Launchpad reads it unchanged.
+ let json = try #require(String(data: JSONEncoder().encode(ipsw), encoding: .utf8))
+ #expect(!json.contains("build") && !json.contains("files"))
+ }
+
@Test func `lists every curated guest`() {
let report = VPhoneFirmwareCatalog.report
#expect(report.devices.map(\.productType) == VPhoneGuestDevice.known.map(\.productType))
diff --git a/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift b/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift
index 18905671..d4bdc9a2 100644
--- a/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift
+++ b/VPhoneLaunchpad/Tests/MachineProductTypeTests.swift
@@ -47,6 +47,16 @@ struct MachineProductTypeTests {
expect(machine.isPad && machine.osName == "iPadOS", "iPad guest")
machine.guestProductType = VPhoneLaunchpadMachine.guestProductType(in: tv)
expect(machine.isAppleTV && !machine.isPad && machine.osName == "tvOS", "Apple TV guest")
- print("Machine product type tests passed: config, restore trees, OS name")
+
+ // An OTA pairing's build comes from the bundle; an IPSW's from its name.
+ let catalog = #"""
+ {"device":"iPhone17,3","pairings":[],"devices":[{"productType":"AppleTV14,1","name":"Apple TV 4K (3rd generation)","family":"appleTV","pairings":[
+ {"ios":{"name":"tvOS 26.6","url":"https://example.com/AppleTV5,3_26.6_23L773_Restore.ipsw"},"recommendedCloudOS":{"name":"cloudOS 26.4","url":"https://example.com/c"}},
+ {"ios":{"name":"tvOS 27.0","url":"https://example.com/9bf3ad6a.aea","build":"24J361","files":["ota/AppleTV14,1_27.0_24J361_OTA.aea","AppleTV5,3_26.6_23L773_Restore.ipsw"]},"recommendedCloudOS":{"name":"cloudOS 26.4","url":"https://example.com/c"}}]}]}
+ """#
+ let tv4K = try JSONDecoder().decode(VPhoneLaunchpadFirmwareCatalog.self, from: Data(catalog.utf8)).guests[0]
+ expect(tv4K.pairings.map(\.build) == ["23L773", "24J361"], "OTA build from the bundle")
+ expect(tv4K.pairings[0].ios.files == nil && tv4K.pairings[1].ios.files?.count == 2, "OTA files")
+ print("Machine product type tests passed: config, restore trees, OS name, catalog builds")
}
}
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift
index 20a0a851..334aa991 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachine.swift
@@ -222,6 +222,11 @@ nonisolated struct VPhoneLaunchpadFirmwareCatalog: Decodable, Sendable {
struct Image: Decodable, Hashable, Sendable {
let name: String
let url: String
+ /// The build, from a bundle that knows `url`'s name does not carry it.
+ var build: String?
+ /// The files the firmware needs in the IPSW cache, relative to it, from
+ /// a bundle that lists them: an OTA and the IPSW it is restored over.
+ var files: [String]?
}
struct Pairing: Decodable, Hashable, Identifiable, Sendable {
@@ -232,9 +237,13 @@ nonisolated struct VPhoneLaunchpadFirmwareCatalog: Decodable, Sendable {
ios.url
}
- /// The build from an IPSW name such as `iPhone17,3_27.0_24A435_Restore.ipsw`
- /// or `iPad_Pro_M4_27.0.1_24A446_Restore.ipsw`: the field before `Restore`.
+ /// The build the bundle gives, else the one from an IPSW name such as
+ /// `iPhone17,3_27.0_24A435_Restore.ipsw` or
+ /// `iPad_Pro_M4_27.0.1_24A446_Restore.ipsw`: the field before `Restore`.
var build: String {
+ if let build = ios.build {
+ return build
+ }
let fields = (ios.url as NSString).lastPathComponent.split(separator: "_")
return fields.count >= 4 ? String(fields[fields.count - 2]) : ""
}
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift
index 4e72f7ea..536fb50e 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadNewMachineView.swift
@@ -335,6 +335,10 @@ struct VPhoneLaunchpadNewMachineView: View {
/// Whether the pairing's iOS or iPadOS IPSW is downloaded, under the name
/// `fw prepare` gives it or its own.
private func isDownloaded(_ pairing: VPhoneLaunchpadFirmwareCatalog.Pairing) -> Bool {
+ // An OTA pairing needs the OTA and the IPSW it is restored over.
+ if let files = pairing.ios.files {
+ return !files.isEmpty && files.allSatisfy(downloaded.contains)
+ }
guard let url = URL(string: pairing.ios.url) else {
return false
}
@@ -345,7 +349,17 @@ struct VPhoneLaunchpadNewMachineView: View {
guard let scan = try? await VPhoneLaunchpadIPSWCache.scan(libraryRoots: model.machines.roots, machineFolders: []) else {
return
}
- downloaded = Set(scan.ipsws.filter { !$0.isDownloading }.map(\.name))
+ var names = Set(scan.ipsws.filter { !$0.isDownloading }.map(\.name))
+ // OTAs are in each cache's `ota` folder, under the names pairings list.
+ for directory in scan.cacheDirectories {
+ let ota = directory.appendingPathComponent("ota", isDirectory: true)
+ for name in (try? FileManager.default.contentsOfDirectory(atPath: ota.path)) ?? []
+ where !name.hasPrefix(".") && name.lowercased().hasSuffix(".aea")
+ {
+ names.insert("ota/\(name)")
+ }
+ }
+ downloaded = names
}
private func placeholderPicker(_ title: LocalizedStringKey) -> some View {