Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

refactor: share boot arguments and guest derivations

Lakrce6bf91a2add
diff --git a/scripts/vphoned/Daemon/GuestAPI.swift b/scripts/vphoned/Daemon/GuestAPI.swift
index 7f36a2de..94e5033f 100644
--- a/scripts/vphoned/Daemon/GuestAPI.swift
+++ b/scripts/vphoned/Daemon/GuestAPI.swift
@@ -27,9 +27,13 @@ enum GuestAPI {
static let binaryHash: String = {
guard let url = Bundle.main.executableURL,
let data = try? Data(contentsOf: url, options: .mappedIfSafe) else { return "unknown" }
- return SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
+ return sha256Hex(data)
}()
+ private static func sha256Hex(_ data: Data) -> String {
+ SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
+ }
+
static func health() -> [String: Any] {
let addresses = networkInfo()["addresses"] as? [String] ?? []
let ip = addresses.first(where: { $0.hasPrefix("en") && !$0.contains("127.0.0.1") })?
@@ -171,7 +175,7 @@ enum GuestAPI {
let cache = "/var/root/Library/Caches/vphoned"
let next = cache + ".next"
let data = try Data(contentsOf: URL(fileURLWithPath: next), options: .mappedIfSafe)
- let actual = SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
+ let actual = sha256Hex(data)
guard actual == expected else { throw GuestAPIError.invalidRequest("Update hash mismatch") }
guard chmod(next, 0o755) == 0 else { throw GuestAPIError.operationFailed("Could not make update executable") }
guard rename(next, cache) == 0 else { throw GuestAPIError.operationFailed("Could not install update") }
diff --git a/scripts/vphoned/vphoned_native.m b/scripts/vphoned/vphoned_native.m
index 8b9b2898..419800b7 100644
--- a/scripts/vphoned/vphoned_native.m
+++ b/scripts/vphoned/vphoned_native.m
@@ -5,6 +5,10 @@
#include <mach-o/dyld.h>
#include <unistd.h>
+static const char *cache = "/var/root/Library/Caches/vphoned";
+static const char *marker = "/var/root/Library/Caches/vphoned.api-v2";
+static const char *pending = "/var/root/Library/Caches/vphoned.api-v2.pending";
+
NSDictionary *vp_native_api_command(NSDictionary *message) {
NSString *type = message[@"t"];
if ([type isEqualToString:@"ipa_install"]) return vp_handle_custom_install(message);
@@ -14,11 +18,8 @@ NSDictionary *vp_native_api_command(NSDictionary *message) {
}
void vp_native_bootstrap_cached_binary(void) {
- static const char *cache = "/var/root/Library/Caches/vphoned";
// v1 cached binaries could fail before binding and put launchd in a crash
// loop. A v2 update gets one attempt; the bundled daemon is the fallback.
- static const char *marker = "/var/root/Library/Caches/vphoned.api-v2";
- static const char *pending = "/var/root/Library/Caches/vphoned.api-v2.pending";
if (access(cache, X_OK) != 0 || access(marker, R_OK) != 0) return;
NSData *binary = [NSData dataWithContentsOfFile:@(cache) options:NSDataReadingMappedIfSafe error:nil];
NSString *expected = [NSString stringWithContentsOfFile:@(marker) encoding:NSUTF8StringEncoding error:nil];
@@ -38,9 +39,6 @@ void vp_native_bootstrap_cached_binary(void) {
}
void vp_native_confirm_cached_binary(void) {
- static const char *cache = "/var/root/Library/Caches/vphoned";
- static const char *marker = "/var/root/Library/Caches/vphoned.api-v2";
- static const char *pending = "/var/root/Library/Caches/vphoned.api-v2.pending";
char current[4096];
uint32_t size = sizeof(current);
if (_NSGetExecutablePath(current, &size) != 0 || strcmp(current, cache) != 0) return;
diff --git a/sources/vphone-cli/VPhoneVirtualMachineLaunchCLI.swift b/sources/vphone-cli/VPhoneVirtualMachineLaunchCLI.swift
index 47d612be..cac4d671 100644
--- a/sources/vphone-cli/VPhoneVirtualMachineLaunchCLI.swift
+++ b/sources/vphone-cli/VPhoneVirtualMachineLaunchCLI.swift
@@ -49,11 +49,13 @@ struct VPhoneVirtualMachineLaunchCommand: ParsableCommand {
_ = try layout.stageVphoned(into: bundle)
}
- var args = ["--config", bundle.configURL.path]
- if dfu { args.append("--dfu") }
- if headless { args.append("--headless") }
- if let apiListen { args += ["--api-listen", apiListen] }
- if let kernelDebugPort { args += ["--kernel-debug-port", String(kernelDebugPort)] }
+ var boot = VPhoneBootCLI()
+ boot.config = bundle.configURL
+ boot.dfu = dfu
+ boot.headless = headless
+ boot.apiListen = apiListen
+ boot.kernelDebugPort = kernelDebugPort
+ let args = boot.bootArguments
if v.tracesInternals {
let (exe, spawned) = launcher.plan(args)