Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift
10.4 KB
Raw Blame
// FirmwarePipelineDiscovery.swift — Locating the Restore directory, firmware files
// and manifest versions.
//
// Owns the filesystem half of the pipeline: picking the newest `*Restore*` directory,
// reading `ProductVersion` out of the base and cloudOS manifests, and resolving a
// component's glob patterns to a concrete file URL.
//
// Split out of FirmwarePipeline.swift. `compareRestoreDirectories` and
// `parseRestoreDirectoryName` stay `private` and live here with their only caller,
// so nothing was widened by the move.

import Darwin
import Foundation
import VPhoneCoreKit
import VPhonePatchKit

extension FirmwarePipeline {
    // MARK: - File Discovery

    /// Find the `*Restore*` subdirectory inside the VM directory.
    /// Mirrors Python `find_restore_dir`.
    public func findRestoreDirectory() throws -> URL {
        let fm = FileManager.default
        let contents = try fm.contentsOfDirectory(
            at: vmDirectory,
            includingPropertiesForKeys: [.isDirectoryKey, .contentModificationDateKey],
        )
        .filter { (try? $0.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true }
        .filter { $0.lastPathComponent.contains("Restore") }
        .sorted(by: compareRestoreDirectories)

        guard let restoreDir = contents.first else {
            throw PatcherError.fileNotFound("Restore directory in \(vmDirectory.path). Run vphone-cli fw prepare first.")
        }
        return restoreDir
    }

    /// The research board kernelcache the VM's mix is built from, as shipped:
    /// its `RestoreKernelCache`, from the originals once `fw patch` has put it
    /// aside. Nil when the restore tree has vphone600 components.
    public func researchBoardKernelcache() throws -> KernelCacheMixer.Input? {
        let restoreDir = try findRestoreDirectory()
        guard Self.researchBoardStandsIn(restoreDir: restoreDir) else { return nil }
        let file = try restoreDir.appending(path: Self.manifestComponentPath("RestoreKernelCache", in: restoreDir))
        let stashed = originalURL(for: file).flatMap { FileManager.default.fileExists(atPath: $0.path) ? $0 : nil }
        return try KernelCacheMixer.Input(
            kernelcache: Data(contentsOf: stashed ?? file),
            guest: readGuestDevice(restoreDir).family.rawValue,
            restoreTree: restoreDir,
        )
    }

    /// The guest family the preserved iPhone manifest names (iPhone when none).
    static func guestFamily(restoreDir: URL) -> String {
        let manifest = restoreDir.appendingPathComponent("iPhone-BuildManifest.plist")
        return (VPhoneGuestDevice.detect(buildManifestAt: manifest) ?? .default).family.rawValue
    }

    /// `ProductVersion` from a manifest in `restoreDir`, or nil if absent/unreadable.
    static func readProductVersion(_ restoreDir: URL, manifest: String) -> String? {
        let url = restoreDir.appendingPathComponent(manifest)
        guard let data = try? Data(contentsOf: url, options: .mappedIfSafe),
              let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
              let dict = plist as? [String: Any],
              let version = dict["ProductVersion"] as? String
        else { return nil }
        return version
    }

    /// The version of the OTA a device without an IPSW takes its userland
    /// from, which `fw prepare` records beside the base's manifest: the
    /// userland, not the restored IPSW, is what the guest patches meet.
    public static let otaVersionFileName = "OTA-SystemVersion.plist"

    /// iPhone base version: the staged OTA's when there is one, else
    /// `iPhone-BuildManifest.plist`'s, preserved by fw_prepare.
    static func readBaseProductVersion(_ restoreDir: URL) -> String? {
        readProductVersion(restoreDir, manifest: otaVersionFileName)
            ?? readProductVersion(restoreDir, manifest: "iPhone-BuildManifest.plist")
    }

    /// The device whose IPSW supplied the guest OS: the one `fw prepare`
    /// recorded in the VM's configuration when that IPSW covers it, otherwise
    /// the first known model the preserved manifest names. iPhone17,3 when it
    /// names none.
    func readGuestDevice(_ restoreDir: URL) -> VPhoneGuestDevice {
        let recorded = try? VPhoneVirtualMachineManifest.load(from: vmDirectory.appendingPathComponent("config.plist"))
        // An Apple TV 4K has no IPSW: it is built over an Apple TV HD restore
        // and takes its userland from an OTA, so the HD's manifest never lists
        // it. Its own device tree, placed in the restore tree, presents it.
        if let guest = recorded?.guestDevice, guest.restoreBase != nil {
            return guest
        }
        return VPhoneGuestDevice.detect(
            buildManifestAt: restoreDir.appendingPathComponent("iPhone-BuildManifest.plist"),
            preferring: recorded?.guestProductType,
        ) ?? .default
    }

    /// cloudOS/kernel version (the live `BuildManifest.plist`).
    static func readCloudOSProductVersion(_ restoreDir: URL) -> String? {
        readProductVersion(restoreDir, manifest: "BuildManifest.plist")
    }

    static func manifestComponentPath(_ component: String, in restoreDir: URL) throws -> String {
        let url = restoreDir.appendingPathComponent("BuildManifest.plist")
        let data = try Data(contentsOf: url, options: .mappedIfSafe)
        guard let root = try PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
              let identities = root["BuildIdentities"] as? [[String: Any]]
        else {
            throw PatcherError.invalidFormat("BuildManifest.plist has no BuildIdentities")
        }
        let paths = Set(identities.compactMap { identity -> String? in
            guard let manifest = identity["Manifest"] as? [String: Any],
                  let entry = manifest[component] as? [String: Any],
                  let info = entry["Info"] as? [String: Any]
            else { return nil }
            return info["Path"] as? String
        })
        guard paths.count == 1, let path = paths.first else {
            throw PatcherError.invalidFormat(
                "BuildManifest.plist names \(paths.count) distinct \(component) paths, expected one",
            )
        }
        guard CustomFirmwareDaemons.isPlainRelativePath(path) else {
            throw PatcherError.invalidFormat("BuildManifest.plist has unsafe \(component) path: \(path)")
        }
        return path
    }

    /// Dotted `ProductVersion` >= major.minor, compared numerically. nil is false.
    static func productVersionAtLeast(_ version: String?, _ major: Int, _ minor: Int) -> Bool {
        guard let parts = version?.split(separator: ".").compactMap({ Int($0) }),
              let vMajor = parts.first else { return false }
        return vMajor != major ? vMajor > major : (parts.count > 1 ? parts[1] : 0) >= minor
    }

    private func compareRestoreDirectories(_ lhs: URL, _ rhs: URL) -> Bool {
        let leftName = lhs.lastPathComponent
        let rightName = rhs.lastPathComponent

        if let left = parseRestoreDirectoryName(leftName),
           let right = parseRestoreDirectoryName(rightName)
        {
            if left.version != right.version {
                return left.version.lexicographicallyPrecedes(right.version, by: >)
            }
            if left.build != right.build {
                return left.build.compare(right.build, options: .numeric) == .orderedDescending
            }
        }

        let leftDate = (try? lhs.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate)
            ?? .distantPast
        let rightDate = (try? rhs.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate)
            ?? .distantPast
        if leftDate != rightDate {
            return leftDate > rightDate
        }
        return leftName > rightName
    }

    private func parseRestoreDirectoryName(_ name: String) -> (version: [Int], build: String)? {
        let pattern = #"_([0-9]+(?:\.[0-9]+)*)_([0-9A-Za-z]+)_Restore$"#
        guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil }
        let range = NSRange(name.startIndex..., in: name)
        guard let match = regex.firstMatch(in: name, range: range),
              match.numberOfRanges == 3,
              let versionRange = Range(match.range(at: 1), in: name),
              let buildRange = Range(match.range(at: 2), in: name)
        else { return nil }

        let version = name[versionRange]
            .split(separator: ".")
            .compactMap { Int($0) }
        let build = String(name[buildRange])
        guard !version.isEmpty else { return nil }
        return (version, build)
    }

    /// Find a firmware file by trying glob-style patterns under `baseDir`.
    /// Mirrors Python `find_file`.
    func findFile(in baseDir: URL, patterns: [String], label: String) throws -> URL {
        let fm = FileManager.default
        for pattern in patterns {
            if pattern.contains("*") || pattern.contains("?") || pattern.contains("[") {
                var matches: [URL] = []
                if !pattern.contains("/") {
                    let urls = try fm.contentsOfDirectory(at: baseDir, includingPropertiesForKeys: [.isRegularFileKey])
                    for url in urls where fnmatch(pattern, url.lastPathComponent, 0) == 0 {
                        matches.append(url)
                    }
                } else {
                    let enumerator = fm.enumerator(at: baseDir, includingPropertiesForKeys: [.isRegularFileKey])
                    while let url = enumerator?.nextObject() as? URL {
                        guard url.path.hasPrefix(baseDir.path + "/") else { continue }
                        let rel = String(url.path.dropFirst(baseDir.path.count + 1))
                        if fnmatch(pattern, rel, 0) == 0 {
                            matches.append(url)
                        }
                    }
                }
                if let first = matches.sorted(by: { $0.path < $1.path }).first {
                    return first
                }
            } else {
                let candidate = baseDir.appendingPathComponent(pattern)
                if fm.fileExists(atPath: candidate.path) {
                    return candidate
                }
            }
        }
        let searched = patterns.map { baseDir.appendingPathComponent($0).path }.joined(separator: "\n    ")
        throw PatcherError.fileNotFound("\(label). Looked in:\n    \(searched)")
    }
}