Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Grow the merged image before sealing it, and show why a seal fails

Maximilian Paß35cbb33e30ea
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
index c115289f..74b2ff1a 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcher.swift
@@ -105,6 +105,10 @@ public final class CryptexFilesystemPatcher: BufferedPatcher {
let mtreePath = try createMtree(filesystem: unencryptedImage)
print("Creating digest database and root hash…")
+ // The merged image was shrunk to its minimum size. With a remap,
+ // apfs_sealvolume copies the whole fsroot tree first and fails with
+ // ENOSPC (exit 65) in the space that is left.
+ try growImage(dmg: unencryptedImage, by: 2 << 30)
let (digestDbPath, rootHashPath) = try createDigestAndHash(
filesystem: unencryptedImage,
mtree: mtreePath,
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
index 29e47502..73ffb409 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherDiskImage.swift
@@ -133,6 +133,15 @@ extension CryptexFilesystemPatcher {
])
}
+ /// Add `bytes` to the image's size, for a tool that needs room on a
+ /// volume `shrinkImage` left at its minimum.
+ func growImage(dmg: URL, by bytes: Int) throws {
+ let size = try FileManager.default.attributesOfItem(atPath: dmg.path)[.size] as? Int ?? 0
+ _ = try runProcess("/usr/sbin/diskutil", [
+ "image", "resize", "-size", String(size + bytes), dmg.path,
+ ])
+ }
+
func unmount(mount: String) throws {
_ = try runProcess("/usr/sbin/diskutil", ["unmount", mount])
}
diff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
index ff38466a..d0a7f21b 100644
--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/CryptexFilesystem/CryptexFilesystemPatcherSealing.swift
@@ -99,12 +99,21 @@ extension CryptexFilesystemPatcher {
try unmount(mount: mount)
let sealvolume = try identifyApfsSealvolume()
FileManager.default.createFile(atPath: sealLogPath.path, contents: nil)
- _ = try runProcess(sealvolume.path, [
- "-R", mtreeRemapPath.path,
- "-U", digestDbPath.path, // Save digest records
- "-M", rootHashPath.path, // Save root hash
- device,
- ], output: sealLogPath)
+ do {
+ _ = try runProcess(sealvolume.path, [
+ "-R", mtreeRemapPath.path,
+ "-U", digestDbPath.path, // Save digest records
+ "-M", rootHashPath.path, // Save root hash
+ device,
+ ], output: sealLogPath)
+ } catch {
+ // apfs_sealvolume says why it rejects a volume only in its log
+ // (exit 65 = EX_DATAERR), which goes with the temporary directory.
+ let log = (try? String(contentsOf: sealLogPath, encoding: .utf8)) ?? ""
+ print("apfs_sealvolume log (last 60 lines):")
+ print(log.split(separator: "\n", omittingEmptySubsequences: false).suffix(60).joined(separator: "\n"))
+ throw error
+ }
return (digestDbPath, rootHashPath)
}