Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Record vphone firmware in bootstrap dpkg status

Lakrd308fb9956af
diff --git a/Research/vphoned_http_api.md b/Research/vphoned_http_api.md
index db01e00e..0a5abd3c 100644
--- a/Research/vphoned_http_api.md
+++ b/Research/vphoned_http_api.md
@@ -44,7 +44,14 @@ to start the daemon; a launchd start error is returned as
`service_start_warning` while the installed bootstrap remains available.
RootHide's plist gets a physical daemon path and `__Patched`
marker before launchd reads it. This is a manual payload install: no maintainer
-script runs and the dpkg database is not changed. The reply includes the tag,
+script runs. For this minimal vphone bootstrap, vphoned writes a real installed
+`firmware` record with the guest iOS version to the selected root's
+`Library/dpkg/status`; Irisin's installed list, resolver, and helper then read
+the same record. If the status already contains firmware from another
+bootstrap, vphoned preserves it. A vphoned-owned record is updated after an
+iOS version change when vphoned starts. `POST /v1/bootstrap/firmware` (RPC `bootstrap.firmware`)
+repairs the record for a bootstrap already identified by the completion marker
+without running another install. The reply includes the tag,
bootstrap path, registration record, and launchd status. A successful bootstrap
writes `.vphoned-boostrap-completed` beside the running vphoned binary; later
requests refuse to bootstrap again when that marker exists.
diff --git a/VPhoneDaemon/Daemon/GuestAPI.swift b/VPhoneDaemon/Daemon/GuestAPI.swift
index 98f900df..6558e6bb 100644
--- a/VPhoneDaemon/Daemon/GuestAPI.swift
+++ b/VPhoneDaemon/Daemon/GuestAPI.swift
@@ -205,6 +205,8 @@ enum GuestAPI {
)
case "bootstrap.status":
return GuestIrisinInstaller.status()
+ case "bootstrap.firmware":
+ return try GuestIrisinInstaller.repairFirmwareRecord()
case "input.touch":
guard let phase = (params["phase"] as? String).flatMap(TouchPhase.init(rawValue:)) else {
throw GuestAPIError.invalidRequest("phase must be down, move or up")
diff --git a/VPhoneDaemon/Daemon/GuestHyperTextHandler.swift b/VPhoneDaemon/Daemon/GuestHyperTextHandler.swift
index aedcd8bf..bae7bcdc 100644
--- a/VPhoneDaemon/Daemon/GuestHyperTextHandler.swift
+++ b/VPhoneDaemon/Daemon/GuestHyperTextHandler.swift
@@ -176,6 +176,7 @@ final class GuestHyperTextHandler: ChannelInboundHandler, RemovableChannelHandle
case (.POST, "/v1/apps/install"): "apps.install"
case (.POST, "/v1/bootstrap/install"): "bootstrap.install"
case (.GET, "/v1/bootstrap/status"): "bootstrap.status"
+ case (.POST, "/v1/bootstrap/firmware"): "bootstrap.firmware"
case (.POST, "/v1/input/touch"): "input.touch"
case (.POST, "/v1/input/hid"): "input.hid"
case (.GET, "/v1/location"): "location.current"
diff --git a/VPhoneDaemon/Daemon/GuestIrisinInstaller.swift b/VPhoneDaemon/Daemon/GuestIrisinInstaller.swift
index d16c652e..8de5f91c 100644
--- a/VPhoneDaemon/Daemon/GuestIrisinInstaller.swift
+++ b/VPhoneDaemon/Daemon/GuestIrisinInstaller.swift
@@ -143,6 +143,8 @@ enum GuestIrisinInstaller {
guard status["loaded"] as? Bool == true else {
throw GuestAPIError.operationFailed("Irisin daemon is not loaded")
}
+ setProgress(["phase": "firmware", "layout": layout, "tag": release.tag])
+ let firmware = try ensureFirmwareRecord(root: root)
let marker = ["tag": release.tag, "layout": layout, "jbroot": root]
try JSONSerialization.data(withJSONObject: marker).write(to: completionMarker, options: .atomic)
for entry in replaced {
@@ -158,8 +160,9 @@ enum GuestIrisinInstaller {
"registration": registration,
"service_load": loaded,
"service_status": status,
+ "firmware_version": firmware.version,
"maintainer_scripts_executed": false,
- "dpkg_database_updated": false,
+ "dpkg_database_updated": firmware.updated,
]
if let started { result["service_start"] = started }
if let startWarning { result["service_start_warning"] = startWarning }
@@ -183,6 +186,96 @@ enum GuestIrisinInstaller {
}
}
+ static func repairFirmwareRecord() throws -> [String: Any] {
+ installLock.lock()
+ defer { installLock.unlock() }
+ let data = try Data(contentsOf: completionMarker)
+ guard let marker = try JSONSerialization.jsonObject(with: data) as? [String: String],
+ let layout = marker["layout"], let root = marker["jbroot"],
+ layout == "rootless" || layout == "roothide",
+ root == (try bootstrapRoot(layout: layout, detected: nil)),
+ isDirectory(root)
+ else { throw GuestAPIError.operationFailed("No valid completed vphoned bootstrap was found") }
+ let firmware = try ensureFirmwareRecord(root: root)
+ return ["layout": layout, "jbroot": root,
+ "firmware_version": firmware.version, "dpkg_database_updated": firmware.updated]
+ }
+
+ static func refreshFirmwareOnStartup() {
+ guard itemExists(completionMarker) else { return }
+ do {
+ _ = try repairFirmwareRecord()
+ } catch {
+ NSLog("vphoned: could not refresh bootstrap firmware record: %@", String(describing: error))
+ }
+ }
+
+ /// This vphone bootstrap has no firmware maintainer script. Write the
+ /// virtual package into the same dpkg status file Irisin and its helper read.
+ private static func ensureFirmwareRecord(root: String) throws -> (version: String, updated: Bool) {
+ let os = ProcessInfo.processInfo.operatingSystemVersion
+ let version = "\(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"
+ let database = URL(fileURLWithPath: root, isDirectory: true)
+ .appendingPathComponent("Library/dpkg", isDirectory: true)
+ try FileManager.default.createDirectory(at: database, withIntermediateDirectories: true,
+ attributes: [.posixPermissions: 0o755])
+ let statusURL = database.appendingPathComponent("status")
+ var info = stat()
+ let statusResult = lstat(statusURL.path, &info)
+ if statusResult != 0, errno != ENOENT {
+ throw GuestAPIError.operationFailed("Could not inspect dpkg status")
+ }
+ if statusResult == 0, info.st_mode & mode_t(S_IFMT) != mode_t(S_IFREG) {
+ throw GuestAPIError.operationFailed("dpkg status is not a regular file")
+ }
+ let existing = statusResult == 0
+ ? try String(contentsOf: statusURL, encoding: .utf8)
+ : ""
+ var paragraphs = existing.replacingOccurrences(of: "\r\n", with: "\n")
+ .components(separatedBy: "\n\n")
+ .filter { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
+ let matches = paragraphs.indices.filter { index in
+ paragraphs[index].split(separator: "\n").contains("Package: firmware")
+ }
+ guard matches.count <= 1 else {
+ throw GuestAPIError.operationFailed("dpkg status contains duplicate firmware records")
+ }
+ if let index = matches.first {
+ let lines = paragraphs[index].split(separator: "\n").map(String.init)
+ let oldVersion = lines.first(where: { $0.hasPrefix("Version: ") })
+ .map { String($0.dropFirst("Version: ".count)) } ?? ""
+ guard lines.contains("Status: install ok installed") else {
+ throw GuestAPIError.operationFailed("Existing firmware record is not installed")
+ }
+ if !lines.contains("Maintainer: vphoned") {
+ return (oldVersion, false)
+ }
+ if oldVersion == version { return (version, false) }
+ var updated = lines.map {
+ $0.hasPrefix("Version: ") ? "Version: \(version)" : $0
+ }
+ if oldVersion.isEmpty { updated.append("Version: \(version)") }
+ paragraphs[index] = updated.joined(separator: "\n")
+ } else {
+ paragraphs.append("""
+ Package: firmware
+ Essential: yes
+ Status: install ok installed
+ Priority: required
+ Section: System
+ Installed-Size: 0
+ Maintainer: vphoned
+ Architecture: all
+ Version: \(version)
+ Description: virtual package for this vphone iOS firmware
+ """)
+ }
+ try (paragraphs.joined(separator: "\n\n") + "\n\n")
+ .write(to: statusURL, atomically: true, encoding: .utf8)
+ try FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: statusURL.path)
+ return (version, true)
+ }
+
// MARK: - Release and payload
private static func bootstrapRoot(layout: String, detected: String?) throws -> String {
diff --git a/VPhoneDaemon/Daemon/main.swift b/VPhoneDaemon/Daemon/main.swift
index 96229525..0fd8b045 100644
--- a/VPhoneDaemon/Daemon/main.swift
+++ b/VPhoneDaemon/Daemon/main.swift
@@ -7,6 +7,7 @@ import VphonedNative
vp_native_bootstrap_cached_binary()
vp_vcam_start()
+GuestIrisinInstaller.refreshFirmwareOnStartup()
let group = MultiThreadedEventLoopGroup(numberOfThreads: 2)
let filePool = NIOThreadPool(numberOfThreads: 2)
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
index 5c7e44a1..a13b039f 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
@@ -1,6 +1,10 @@
{
"sourceLanguage": "en",
"strings": {
+ "Recording firmware version…": { "localizations": {
+ "en": { "stringUnit": { "state": "translated", "value": "Recording firmware version…" } },
+ "zh-Hans": { "stringUnit": { "state": "translated", "value": "正在登记固件版本…" } }
+ } },
"Install Bootstrap…": { "localizations": {
"en": { "stringUnit": { "state": "translated", "value": "Install Bootstrap…" } },
"zh-Hans": { "stringUnit": { "state": "translated", "value": "安装引导环境…" } }
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuConnect.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuConnect.swift
index 4c9b1a48..7482539e 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuConnect.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuConnect.swift
@@ -211,6 +211,8 @@ extension VPhoneMenuController {
label.stringValue = VPhoneLocalization.text("Extracting Irisin…")
case "installing":
label.stringValue = VPhoneLocalization.text("Registering Irisin…")
+ case "firmware":
+ label.stringValue = VPhoneLocalization.text("Recording firmware version…")
default: break
}
}