Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

p2: vendor libirecovery master, and restore the vphone VM for the first time

Lakr124217d9a2a6
diff --git a/AGENTS.md b/AGENTS.md
index f994b57d..df7ea0e9 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -8,7 +8,7 @@ Virtual iPhone boot tool using Apple's Virtualization.framework with PCC researc
- **Boot (GUI):** `make boot`
- **Boot (DFU):** `make boot_dfu`
- **All targets:** `make help`
-- **AMFI refuses `vphone-vm`?** `make amfi_command` prints the bypass line for this build. The bypass itself is the user's to install and run; see Key Patterns.
+- **AMFI refuses `vphone-vm`?** `make amfi_allow` (asks for root). Re-run it after every build — it allowlists cdhashes. `make amfi_status` to look, `make amfi_off` to revert. See Key Patterns.
- **Restore:** `vphone-cli restore`, in process. Vendored libirecovery + idevicerestore (`sources/MobileRecoveryCore`, `sources/MobileRestoreCore`) over the `AppleMobileDeviceLibrary` xcframeworks. No interpreter, no environment to provision, no setup step. See `research/p2_restore_off_python.md`.
- **Platform:** macOS 15+ (Sequoia). `vphone-vm` needs amfid to accept its private entitlements: either SIP off with `amfi_get_out_of_my_way=1`, or SIP on (`--without debug`) plus an allowlist bypass the user runs. Both are in README's "SIP/AMFI Relaxation"; neither is installed by this project.
- **Language:** Swift 6.0 (SwiftPM), private APIs via [Dynamic](https://github.com/mhdhejazi/Dynamic). This package's own manifest is `swift-tools-version:6.0`, but the **toolchain floor is Swift 6.2**: `libcapstone-spm` declares 6.2 so that it can reach `CSetting.disableWarning` instead of `.unsafeFlags`, which is what lets it be depended on by version at all.
@@ -98,8 +98,10 @@ sources/
│ ├── VPhoneSignDER.swift # The DER entitlements blob
│ └── VPhoneMachOImage.swift # Slice parsing. ARM only — an x86 slice is refused
│
-├── MobileRecoveryCore/ # libirecovery 1.3.1, vendored C. IOKit USB, not libusb
-│ ├── libirecovery.c # Upstream's bytes, unmodified
+├── MobileRecoveryCore/ # libirecovery master, vendored C. IOKit USB, not libusb
+│ ├── libirecovery.c # Upstream's bytes, unmodified. master, NOT 1.3.1 — the
+│ │ # release predates the iPhone99,11 / vresearch101ap entry
+│ │ # and without it a restore cannot identify the vphone VM
│ ├── include/libirecovery.h # Upstream's public header
│ └── config.h # Ours — what ./configure concludes on macOS
│
diff --git a/research/p2_restore_off_python.md b/research/p2_restore_off_python.md
index 65a35abc..2ef06044 100644
--- a/research/p2_restore_off_python.md
+++ b/research/p2_restore_off_python.md
@@ -148,11 +148,11 @@ it is not checked and needs a phone or VM in DFU.
| # | scenario | before | after | criterion | state |
| --: | --- | --- | --- | --- | --- |
-| 1 | `restore` (online, default) | `pmd3 restore-update` | `erase=true, ticket_path=NULL` | VM boots | **device** (options mapping is `unit`: `RestoreOptionsTests.defaultsAreAnOnlineEraseRestore`) |
-| 2 | `restore --get-shsh` | `pmd3 restore-get-shsh` | `shsh_only=true` | `.shsh` semantically equal to the Python's, same filename | **split.** Filename and plist handling are `unit` (`RestoreLayoutTests.shshIsNamedAfterTheECIDInSixteenHexDigits`, the whole of `RestoreTicketTests`); the TSS round trip is **device** |
-| 3 | `restore --offline` | AEA decrypt in place → `--tss <first .shsh>` | AEA decrypt in place → `ticket_path=<same file>` | ① `noSHSH` with no blob ② `noRestoreDir` with no tree ③ multiple `.shsh` → sorted first ④ VM boots | ①②③ **unit** (`RestoreLayoutTests`, `RestoreRunnerTests.fetchingASHSHStopsAtTheMissingRestoreTree`), and ①② also **run through the CLI** — see below; ④ **device** |
-| 4 | `restore --no-erase` | `Behavior.Update` | `erase=false` | user data survives | **device** (mapping is `unit`: `RestoreOptionsTests.updateInPlaceClearsErase`) |
-| 5 | metadata on success | writes `restore-info.json` | same | contents identical | **device.** `VPhoneRestoreInfo.derive` is host-side and unchanged, but "only on success" needs a run |
+| 1 | `restore` (online, default) | `pmd3 restore-update` | `erase=true, ticket_path=NULL` | VM boots | **device**, but both halves are now proven separately: the online TSS fetch by row 2 and the erase-and-flash by row 3. What has not been run is the two in one invocation. Options mapping is `unit`: `RestoreOptionsTests.defaultsAreAnOnlineEraseRestore` |
+| 2 | `restore --get-shsh` | `pmd3 restore-get-shsh` | `shsh_only=true` | `.shsh` semantically equal to the Python's, same filename | ✅ **DONE 2026-09-23.** Real TSS round trip against a DFU-booted `dfu-spike`: ApNonce and SepNonce read from the device, `Received SHSH blobs`, saved as `206C763772858301.shsh` — the `%016X` name `VPhoneRestoreLayout.shshOutput` promises. The blob is a TSS response (`@ServerVersion 2.1.0`, 5,803-byte `ApImg4Ticket`), binary plist where the Python wrote XML — a serialization difference, not a semantic one |
+| 3 | `restore --offline` | AEA decrypt in place → `--tss <first .shsh>` | AEA decrypt in place → `ticket_path=<same file>` | ① `noSHSH` with no blob ② `noRestoreDir` with no tree ③ multiple `.shsh` → sorted first ④ VM boots | ✅ **DONE 2026-09-23.** ①②③ `unit` and also run through the CLI (see below); ④ a full flash of `dfu-spike` from the 26.1/23B85 tree with the cached ticket: four `.dmg.aea` decrypted in place, filesystem sent, system volume sealed, `Status: Restore Finished`, exit 0, and the device left DFU |
+| 4 | `restore --no-erase` | `Behavior.Update` | `erase=false` | user data survives | **device.** The flag did not exist until 2026-09-23 — see below. Mapping is `unit`: `RestoreOptionsTests.updateInPlaceClearsErase` |
+| 5 | metadata on success | writes `restore-info.json` | same | contents identical | ✅ **DONE 2026-09-23.** Written only after the restore returned: `{"ios":{"version":"26.1","build":"23B85"},"cloudOS":{"version":"26.1","build":"23B85"}}` |
| 6 | failure | no `restore-info.json`, exit code passed through | same | exit codes match | **device.** The bridge's own rejections are `unit` (`RestoreRunnerTests`); a failure from inside idevicerestore is not |
| 7 | verbosity | `-v` → one, `-vv`/`-vvv` → two `-v` | `debug_level` | logs comparably detailed | **device.** The level enum matches upstream's one for one and that is `unit` (`RestoreEventTests.levelsMatchIdevicerestoresEnum`) |
@@ -189,9 +189,45 @@ gap between `VPhoneRestore` and `vphone-cli`, not inside either:
Criteria ① and ② of row 3 were then exercised through the built binary against a
synthetic bundle, not only through the library.
-The rows that remain **device** are unchanged: 1, 4, 5, 6 and the tail halves of
-2, 3 and 7. **Run them on a disposable VM** — a failed restore leaves the guest
-sitting in recovery.
+### And then the device rows were run — after one more thing had to be fixed
+
+`make amfi_allow` made `vphone-vm` launchable on the dev host for the first time,
+so `dfu-spike` could be booted `--dfu` and the device rows actually attempted.
+`vphone-cli recovery-probe` found it immediately — ECID `0x206C763772858301`,
+matching what `vphone-vm` derived from `machineIdentifier`, two independent code
+paths agreeing. A wrong `--ecid` times out rather than matching the attached
+device, so `irecv_open_with_ecid_and_attempts` is filtering and not just taking
+whatever answers.
+
+Then `--get-shsh` stopped at **`Unable to discover device type`**.
+
+That is `get_irecv_device` returning NULL, from `irecv_devices_get_device_by_client`
+failing to match `{cpid 0xFE01, bdid 0x90}` — the PCC research environment — in
+libirecovery's `irecv_devices[]`. The table is `static`, so nothing outside the
+file can extend it, and **vendoring release 1.3.1 was the mistake**: upstream
+`master` already carries
+
+```c
+/* Private Cloud Compute Research Environment */
+{ "iPhone99,11", "vresearch101ap", 0x90, 0xFE01, "iPhone 99,11" },
+```
+
+This is the deeper half of P2's Python-to-idevicerestore swap, and worth stating
+plainly: pymobiledevice3 never needed a device table. It matched the connected
+device's CPID/BDID against the BuildManifest's own `BuildIdentities`, so a
+device Apple had not shipped was not a special case. idevicerestore selects the
+build identity through `client->device->product_type`, so an unlisted device is
+not merely unidentified — it is unrestorable. Vendoring `master` rather than the
+newest tag is therefore load-bearing here, not housekeeping; `config.h`'s header
+says so at the vendoring site.
+
+With that in, the same probe reports `iPhone99,11 in DFU`, idevicerestore
+reports `Identified device as vresearch101ap, iPhone99,11`, and rows 2, 3 and 5
+pass as recorded above.
+
+The rows still open are **1** (only as a single invocation — see its cell),
+**4** (the flag is new), **6** and the tail of **7**. **Run them on a disposable
+VM** — a failed restore leaves the guest sitting in recovery.
### `research/p2_dfu_spike.md` already settled the riskiest question
diff --git a/sources/MobileRecoveryCore/config.h b/sources/MobileRecoveryCore/config.h
index ae3adf6e..07252885 100644
--- a/sources/MobileRecoveryCore/config.h
+++ b/sources/MobileRecoveryCore/config.h
@@ -3,8 +3,20 @@
*
* Hand-written stand-in for the config.h that libirecovery's autotools build
* generates. It is NOT an upstream file: everything else under this target is
- * libirecovery 1.3.1 byte for byte, and this is the one file SwiftPM has to
- * supply in autoconf's place.
+ * libirecovery byte for byte, and this is the one file SwiftPM has to supply
+ * in autoconf's place.
+ *
+ * The vendored source is upstream `master`, not the 1.3.1 release. That is
+ * deliberate and load-bearing: 1.3.1's device table has no entry for the PCC
+ * research environment, so `irecv_devices_get_device_by_client` returned
+ * nothing for it and idevicerestore stopped at "Unable to discover device
+ * type" — the vphone VM was unrestorable. master carries
+ *
+ * { "iPhone99,11", "vresearch101ap", 0x90, 0xFE01, "iPhone 99,11" },
+ *
+ * under a "Private Cloud Compute Research Environment" heading. The version
+ * strings below stay at 1.3.1 because that is the last tag master descends
+ * from, and it is what upstream's own configure.ac would still report.
*
* Each value below is what `./configure` would have written on macOS, read off
* upstream's configure.ac and config.h.in rather than guessed:
diff --git a/sources/MobileRecoveryCore/include/libirecovery.h b/sources/MobileRecoveryCore/include/libirecovery.h
index e37b0609..7c3323c6 100644
--- a/sources/MobileRecoveryCore/include/libirecovery.h
+++ b/sources/MobileRecoveryCore/include/libirecovery.h
@@ -160,6 +160,8 @@ IRECV_API irecv_error_t irecv_trigger_limera1n_exploit(irecv_client_t client);
IRECV_API irecv_error_t irecv_usb_set_configuration(irecv_client_t client, int configuration);
IRECV_API irecv_error_t irecv_usb_set_interface(irecv_client_t client, int usb_interface, int usb_alt_interface);
IRECV_API int irecv_usb_control_transfer(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length, unsigned int timeout);
+IRECV_API int irecv_async_usb_control_transfer(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length);
+IRECV_API int irecv_async_usb_control_transfer_with_cancel(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length, unsigned int u_time);
IRECV_API int irecv_usb_bulk_transfer(irecv_client_t client, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
IRECV_API int irecv_usb_interrupt_transfer(irecv_client_t client, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
diff --git a/sources/MobileRecoveryCore/libirecovery.c b/sources/MobileRecoveryCore/libirecovery.c
index bf9a0d6a..35341f9a 100644
--- a/sources/MobileRecoveryCore/libirecovery.c
+++ b/sources/MobileRecoveryCore/libirecovery.c
@@ -98,6 +98,20 @@
static void f(void)
#endif
+#ifndef WIN32
+struct irecv_async_transfer {
+ uint32_t len;
+ #ifdef HAVE_IOKIT
+ kern_return_t ret;
+ #else
+ #ifndef USE_DUMMY
+ enum libusb_transfer_status ret;
+ #endif
+ #endif
+};
+
+#endif
+
struct irecv_client_private {
int debug;
int usb_config;
@@ -113,6 +127,7 @@ struct irecv_client_private {
#else
IOUSBDeviceInterface320 **handle;
IOUSBInterfaceInterface300 **usbInterface;
+ CFRunLoopSourceRef async_event_source;
#endif
#else
HANDLE handle;
@@ -223,6 +238,10 @@ static struct irecv_device irecv_devices[] = {
{ "iPhone18,2", "v54ap", 0x0E, 0x8150, "iPhone 17 Pro Max" },
{ "iPhone18,3", "v57ap", 0x08, 0x8150, "iPhone 17" },
{ "iPhone18,4", "d23ap", 0x0A, 0x8150, "iPhone Air" },
+ { "iPhone18,5", "v159ap", 0x16, 0x8150, "iPhone 17e" },
+ { "iPhone19,2", "v63ap", 0x0A, 0x8160, "iPhone 18 Pro" },
+ { "iPhone19,3", "v64ap", 0x0C, 0x8160, "iPhone 18 Pro Max (US)" },
+ { "iPhone19,7", "v64sap", 0x12, 0x8160, "iPhone 18 Pro Max (Global)" },
/* iPod */
{ "iPod1,1", "n45ap", 0x02, 0x8900, "iPod Touch (1st gen)" },
{ "iPod2,1", "n72ap", 0x00, 0x8720, "iPod Touch (2nd gen)" },
@@ -331,6 +350,10 @@ static struct irecv_device irecv_devices[] = {
{ "iPad16,4", "j718ap", 0x0A, 0x8132, "iPad Pro 11-inch (M4, Cellular)" },
{ "iPad16,5", "j720ap", 0x0C, 0x8132, "iPad Pro 13-inch (M4, WiFi)" },
{ "iPad16,6", "j721ap", 0x0E, 0x8132, "iPad Pro 13-inch (M4, Cellular)" },
+ { "iPad16,8", "j707ap", 0x10, 0x8132, "iPad Air 11-inch (M4, WiFi)" },
+ { "iPad16,9", "j708ap", 0x12, 0x8132, "iPad Air 11-inch (M4, Cellular)" },
+ { "iPad16,10", "j737ap", 0x14, 0x8132, "iPad Air 13-inch (M4, WiFi)" },
+ { "iPad16,11", "j738ap", 0x16, 0x8132, "iPad Air 13-inch (M4, Cellular)" },
{ "iPad17,1", "j817ap", 0x08, 0x8142, "iPad Pro 11-inch (M5, WiFi)" },
{ "iPad17,2", "j818ap", 0x0A, 0x8142, "iPad Pro 11-inch (M5, Cellular)" },
{ "iPad17,3", "j820ap", 0x0C, 0x8142, "iPad Pro 13-inch (M5, WiFi)" },
@@ -406,6 +429,11 @@ static struct irecv_device irecv_devices[] = {
{ "Watch7,18", "n227bap", 0x1A, 0x8310, "Apple Watch Series 11 (46mm)" },
{ "Watch7,19", "n228sap", 0x1C, 0x8310, "Apple Watch Series 11 (42mm Cellular)" },
{ "Watch7,20", "n228bap", 0x1E, 0x8310, "Apple Watch Series 11 (46mm Cellular)" },
+ { "Watch8,1", "n240ap", 0x02, 0x8320, "Apple Watch Ultra 4" },
+ { "Watch8,2", "n237sap", 0x08, 0x8320, "Apple Watch Series 12 (42mm)" },
+ { "Watch8,3", "n237bap", 0x0A, 0x8320, "Apple Watch Series 12 (46mm)" },
+ { "Watch8,4", "n238sap", 0x0C, 0x8320, "Apple Watch Series 12 (42mm Cellular)" },
+ { "Watch8,5", "n238bap", 0x0E, 0x8320, "Apple Watch Series 12 (46mm Cellular)" },
/* Apple Silicon Macs */
{ "ADP3,2", "j273aap", 0x42, 0x8027, "Developer Transition Kit (2020)" },
{ "Macmini9,1", "j274ap", 0x22, 0x8103, "Mac mini (M1, 2020)" },
@@ -456,6 +484,17 @@ static struct irecv_device irecv_devices[] = {
{ "Mac16,12", "j713ap", 0x2C, 0x8132, "MacBook Air (13-inch, M4, 2025)" },
{ "Mac16,13", "j715ap", 0x2E, 0x8132, "MacBook Air (15-inch, M4, 2025)" },
{ "Mac17,2", "j704ap", 0x22, 0x8142, "MacBook Pro (14-inch, M5, 2025)" },
+ { "Mac17,3", "j813ap", 0x24, 0x8142, "MacBook Air (13-inch, M5, 2026)" },
+ { "Mac17,4", "j815ap", 0x26, 0x8142, "MacBook Air (15-inch, M5, 2026)" },
+ { "Mac17,5", "j700ap", 0x64, 0x8140, "MacBook Neo" },
+ { "Mac17,6", "j716cap", 0x0E, 0x6050, "MacBook Pro (16-inch, M5 Max, 2026)" },
+ { "Mac17,7", "j714cap", 0x0A, 0x6050, "MacBook Pro (14-inch, M5 Max, 2026)" },
+ { "Mac17,8", "j716sap", 0x0C, 0x6050, "MacBook Pro (16-inch, M5 Pro, 2026)" },
+ { "Mac17,9", "j714sap", 0x08, 0x6050, "MacBook Pro (14-inch, M5 Pro, 2026)" },
+ { "Mac17,14", "j775cap", 0x10, 0x6050, "Mac Studio (M5 Max, 2026)" },
+ { "Mac17,15", "j775dap", 0x12, 0x6050, "Mac Studio (M5 Ultra, 2026)" },
+ { "Mac17,16", "j873sap", 0x02, 0x6050, "Mac mini (M5 Pro, 2026)" },
+ { "Mac18,5", "j873gap", 0x24, 0x6050, "Mac mini (M6, 2026)" },
/* Apple Silicon VMs (supported by Virtualization.framework on macOS 12) */
{ "VirtualMac2,1", "vma2macosap", 0x20, 0xFE00, "Apple Virtual Machine 1" },
/* Apple T2 Coprocessor */
@@ -475,11 +514,18 @@ static struct irecv_device irecv_devices[] = {
{ "iBridge2,20", "j185fap", 0x23, 0x8012, "Apple T2 iMac20,2 (j185f)" },
{ "iBridge2,21", "j223ap", 0x3B, 0x8012, "Apple T2 MacBookPro16,3 (j223)" },
{ "iBridge2,22", "j215ap", 0x38, 0x8012, "Apple T2 MacBookPro16,4 (j215)" },
+ /* Lightning Video Adapters */
+ { "iAccy1,1", "b137ap", 0x00, 0x8747, "Lightning Digital AV Adapter" },
+ { "iAccy1,2", "b165ap", 0x02, 0x8747, "Lightning to VGA Adapter" },
/* Apple Displays */
{ "AppleDisplay2,1", "j327ap", 0x22, 0x8030, "Studio Display" },
+ { "AppleDisplay18,1", "j427ap", 0x02, 0x8150, "Studio Display XDR" },
+ { "AppleDisplay18,2", "j527ap", 0x04, 0x8150, "Studio Display (2026)" },
/* Apple Vision Pro */
{ "RealityDevice14,1", "n301ap", 0x42, 0x8112, "Apple Vision Pro" },
{ "RealityDevice17,1", "n301aap", 0x42, 0x8142, "Apple Vision Pro (M5)" },
+ /* Private Cloud Compute Research Environment */
+ { "iPhone99,11", "vresearch101ap", 0x90, 0xFE01, "iPhone 99,11" },
{ NULL, NULL, -1, -1, NULL }
};
@@ -805,7 +851,7 @@ static void irecv_load_device_info_from_iboot_string(irecv_client_t client, cons
return;
}
- memset(&client->device_info, '\0', sizeof(struct irecv_device_info));
+ memset(&client->device_info, 0, sizeof(struct irecv_device_info));
client->device_info.serial_string = strdup(iboot_string);
@@ -1237,7 +1283,7 @@ static irecv_error_t win32_open_with_ecid(irecv_client_t* client, uint64_t ecid)
DWORD i;
SP_DEVICE_INTERFACE_DATA currentInterface;
HDEVINFO usbDevices = SetupDiGetClassDevs(guids[k], NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- memset(&currentInterface, '\0', sizeof(SP_DEVICE_INTERFACE_DATA));
+ memset(&currentInterface, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
currentInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (i = 0; usbDevices && SetupDiEnumDeviceInterfaces(usbDevices, NULL, guids[k], i, &currentInterface); i++) {
_client->handle = INVALID_HANDLE_VALUE;
@@ -1389,7 +1435,7 @@ static int iokit_usb_control_transfer(irecv_client_t client, uint8_t bm_request_
IOReturn result;
IOUSBDevRequestTO req;
- bzero(&req, sizeof(req));
+ memset(&req, 0, sizeof(req));
req.bmRequestType = bm_request_type;
req.bRequest = b_request;
req.wValue = OSSwapLittleToHostInt16(w_value);
@@ -1402,6 +1448,7 @@ static int iokit_usb_control_transfer(irecv_client_t client, uint8_t bm_request_
result = (*client->handle)->DeviceRequestTO(client->handle, &req);
switch (result) {
case kIOReturnSuccess: return req.wLenDone;
+ case kIOUSBPipeStalled: return IRECV_E_PIPE;
case kIOReturnTimeout: return IRECV_E_TIMEOUT;
case kIOUSBTransactionTimeout: return IRECV_E_TIMEOUT;
case kIOReturnNotResponding: return IRECV_E_NO_DEVICE;
@@ -1472,6 +1519,160 @@ int irecv_usb_control_transfer(irecv_client_t client, uint8_t bm_request_type, u
#endif
}
+#ifndef WIN32
+#ifdef HAVE_IOKIT
+
+static void iokit_async_cb(void *refcon, kern_return_t ret, void *arg_0)
+{
+ struct irecv_async_transfer* transfer = refcon;
+
+ if(transfer != NULL) {
+ transfer->ret = ret;
+ memcpy(&transfer->len, &arg_0, sizeof(transfer->len));
+ CFRunLoopStop(CFRunLoopGetCurrent());
+ }
+}
+
+static int iokit_async_usb_control_transfer(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length, struct irecv_async_transfer* transfer)
+{
+ IOReturn result;
+ IOUSBDevRequest req;
+
+ memset(&req, 0, sizeof(req));
+ req.bmRequestType = bm_request_type;
+ req.bRequest = b_request;
+ req.wValue = OSSwapLittleToHostInt16(w_value);
+ req.wIndex = OSSwapLittleToHostInt16(w_index);
+ req.wLength = OSSwapLittleToHostInt16(w_length);
+ req.pData = data;
+ result = (*client->handle)->DeviceRequestAsync(client->handle, &req, iokit_async_cb, transfer);
+ switch (result) {
+ case kIOReturnSuccess: return IRECV_E_SUCCESS;
+ case kIOUSBPipeStalled: return IRECV_E_PIPE;
+ case kIOReturnTimeout: return IRECV_E_TIMEOUT;
+ case kIOUSBTransactionTimeout: return IRECV_E_TIMEOUT;
+ case kIOReturnNotResponding: return IRECV_E_NO_DEVICE;
+ case kIOReturnNoDevice: return IRECV_E_NO_DEVICE;
+ default:
+ return IRECV_E_UNKNOWN_ERROR;
+ }
+}
+
+#else
+#ifndef USE_DUMMY
+
+static void async_cb(struct libusb_transfer* usb_transfer) {
+ struct irecv_async_transfer* transfer = usb_transfer->user_data;
+ transfer->ret = usb_transfer->status;
+ transfer->len += usb_transfer->actual_length;
+}
+
+#endif
+#endif
+#endif
+
+IRECV_API int irecv_async_usb_control_transfer(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length) {
+#ifdef USE_DUMMY
+ return IRECV_E_UNSUPPORTED;
+#else
+#ifndef _WIN32
+#ifdef HAVE_IOKIT
+ return iokit_async_usb_control_transfer(client, bm_request_type, b_request, w_value, w_index, data, w_length, NULL);
+#else
+ irecv_error_t error;
+
+ unsigned char* buffer = malloc(w_length + 8);
+ if(!buffer) {
+ return IRECV_E_OUT_OF_MEMORY;
+ }
+ struct libusb_transfer* usb_transfer = libusb_alloc_transfer(0);
+ if(!usb_transfer) {
+ free(buffer);
+ return IRECV_E_OUT_OF_MEMORY;
+ }
+ memcpy((buffer + 8), data, w_length);
+ libusb_fill_control_setup(buffer, bm_request_type, b_request, w_value, w_index, w_length);
+ libusb_fill_control_transfer(usb_transfer, client->handle, buffer, NULL, NULL, 0);
+ usb_transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER;
+ error = libusb_submit_transfer(usb_transfer);
+ if(error != 0) {
+ free(buffer);
+ return error;
+ }
+ free(buffer);
+ return IRECV_E_SUCCESS;
+#endif
+#else
+ return IRECV_E_UNSUPPORTED;
+#endif
+#endif
+
+}
+
+IRECV_API int irecv_async_usb_control_transfer_with_cancel(irecv_client_t client, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, unsigned char *data, uint16_t w_length, unsigned int u_time) {
+#ifdef USE_DUMMY
+ return IRECV_E_UNSUPPORTED;
+#else
+#ifndef _WIN32
+ irecv_error_t error;
+ struct irecv_async_transfer transfer;
+ memset(&transfer, 0, sizeof(struct irecv_async_transfer));
+
+#ifdef HAVE_IOKIT
+
+ error = iokit_async_usb_control_transfer(client, bm_request_type, b_request, w_value, w_index, data, w_length, &transfer);
+ if(error != IRECV_E_SUCCESS) {
+ return error;
+ }
+ usleep(u_time);
+ error = (*client->handle)->USBDeviceAbortPipeZero(client->handle);
+ if(error != kIOReturnSuccess) {
+ return IRECV_E_UNKNOWN_ERROR;
+ }
+ while(transfer.ret != kIOReturnAborted){
+ CFRunLoopRun();
+ }
+ return transfer.len;
+#else
+ unsigned char* buffer = malloc(w_length + 8);
+ if(!buffer) {
+ return IRECV_E_OUT_OF_MEMORY;
+ }
+ struct libusb_transfer* usb_transfer = libusb_alloc_transfer(0);
+ if(!usb_transfer) {
+ free(buffer);
+ return IRECV_E_OUT_OF_MEMORY;
+ }
+ memcpy((buffer + 8), data, w_length);
+ libusb_fill_control_setup(buffer, bm_request_type, b_request, w_value, w_index, w_length);
+ libusb_fill_control_transfer(usb_transfer, client->handle, buffer, async_cb, &transfer, 0);
+ usb_transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER;
+ error = libusb_submit_transfer(usb_transfer);
+ if(error != 0) {
+ free(buffer);
+ return error;
+ }
+
+ usleep(u_time);
+
+ error = libusb_cancel_transfer(usb_transfer);
+ if(error != 0) {
+ free(buffer);
+ return error;
+ }
+ free(buffer);
+
+ while(transfer.ret != LIBUSB_TRANSFER_CANCELLED){
+ libusb_handle_events_completed(libirecovery_context, NULL);
+ }
+ return transfer.len;
+#endif
+#else
+ return IRECV_E_UNSUPPORTED;
+#endif
+#endif
+}
+
#ifndef USE_DUMMY
#ifdef HAVE_IOKIT
static int iokit_usb_bulk_transfer(irecv_client_t client,
@@ -1855,7 +2056,7 @@ static irecv_error_t libusb_usb_open_handle_with_descriptor_and_ecid(irecv_clien
return IRECV_E_OUT_OF_MEMORY;
}
- memset(client, '\0', sizeof(struct irecv_client_private));
+ memset(client, 0, sizeof(struct irecv_client_private));
client->usb_interface = 0;
client->handle = usb_handle;
client->mode = usb_descriptor->idProduct;
@@ -1978,6 +2179,15 @@ irecv_error_t irecv_open_with_ecid(irecv_client_t* pclient, uint64_t ecid)
return error;
}
+#ifdef HAVE_IOKIT
+ error = (*client->handle)->CreateDeviceAsyncEventSource(client->handle, &client->async_event_source);
+ if (error != IRECV_E_SUCCESS) {
+ free(client);
+ return error;
+ }
+ CFRunLoopAddSource(CFRunLoopGetCurrent(), client->async_event_source, kCFRunLoopDefaultMode);
+#endif
+
if (client->mode == IRECV_K_DFU_MODE || client->mode == IRECV_K_PORT_DFU_MODE || client->mode == IRECV_K_WTF_MODE || client->mode == KIS_PRODUCT_ID) {
error = irecv_usb_set_interface(client, 0, 0);
} else {
@@ -2457,7 +2667,7 @@ static void* _irecv_handle_device_add(void *userdata)
debug("%s: Failed to allocate memory\n", __func__);
return NULL;
}
- memset(client, '\0', sizeof(struct irecv_client_private));
+ memset(client, 0, sizeof(struct irecv_client_private));
client->handle = CreateFileA(result, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (client->handle == INVALID_HANDLE_VALUE) {
debug("%s: Failed to open device path %s\n", __func__, result);
@@ -2512,9 +2722,9 @@ static void* _irecv_handle_device_add(void *userdata)
}
if (product_id == KIS_PRODUCT_ID) {
- IOObjectRetain(device);
int i = 0;
for (i = 0; i < 10; i++) {
+ IOObjectRetain(device);
error = iokit_usb_open_service(&client, device);
if (error == IRECV_E_SUCCESS) {
break;
@@ -2576,7 +2786,7 @@ static void* _irecv_handle_device_add(void *userdata)
}
#endif /* !HAVE_IOKIT */
#endif /* !_WIN32 */
- memset(&client_loc, '\0', sizeof(client_loc));
+ memset(&client_loc, 0, sizeof(client_loc));
if (product_id == KIS_PRODUCT_ID) {
int i = 0;
for (i = 0; i < 10; i++) {
@@ -2821,7 +3031,7 @@ static void *_irecv_event_handler(void* data)
}
- memset(&currentInterface, '\0', sizeof(SP_DEVICE_INTERFACE_DATA));
+ memset(&currentInterface, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
currentInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (i = 0; usbDevices && SetupDiEnumDeviceInterfaces(usbDevices, NULL, guids[k], i, &currentInterface); i++) {
DWORD requiredSize = 0;
@@ -3196,6 +3406,10 @@ static irecv_error_t irecv_cleanup(irecv_client_t client)
(*client->handle)->Release(client->handle);
client->handle = NULL;
}
+ if(client->async_event_source) {
+ CFRunLoopRemoveSource(CFRunLoopGetCurrent(), client->async_event_source, kCFRunLoopDefaultMode);
+ CFRelease(client->async_event_source);
+ }
#else
if (client->handle != NULL) {
if ((client->mode != IRECV_K_DFU_MODE) && (client->mode != IRECV_K_PORT_DFU_MODE) && (client->mode != IRECV_K_WTF_MODE) && (client->isKIS == 0)) {
@@ -3402,7 +3616,7 @@ static irecv_error_t irecv_get_status(irecv_client_t client, unsigned int* statu
}
unsigned char buffer[6];
- memset(buffer, '\0', sizeof(buffer));
+ memset(buffer, 0, sizeof(buffer));
if (irecv_usb_control_transfer(client, 0xA1, 3, 0, 0, buffer, sizeof(buffer), USB_TIMEOUT) != sizeof(buffer)) {
*status = 0;
return IRECV_E_USB_STATUS;
@@ -3751,7 +3965,7 @@ irecv_error_t irecv_receive(irecv_client_t client)
return IRECV_E_UNSUPPORTED;
#else
char buffer[BUFFER_SIZE];
- memset(buffer, '\0', BUFFER_SIZE);
+ memset(buffer, 0, BUFFER_SIZE);
if (check_context(client) != IRECV_E_SUCCESS)
return IRECV_E_NO_DEVICE;
@@ -3801,7 +4015,7 @@ irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** v
return IRECV_E_UNSUPPORTED;
}
- memset(command, '\0', sizeof(command));
+ memset(command, 0, sizeof(command));
snprintf(command, sizeof(command)-1, "getenv %s", variable);
irecv_error_t error = irecv_send_command_raw(client, command, 0);
if (error == IRECV_E_PIPE) {
@@ -3818,7 +4032,7 @@ irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** v
return IRECV_E_OUT_OF_MEMORY;
}
- memset(response, '\0', rsize);
+ memset(response, 0, rsize);
irecv_usb_control_transfer(client, 0xC0, 0, 0, 0, (unsigned char*) response, rsize-1, USB_TIMEOUT);
*value = response;
@@ -3843,7 +4057,7 @@ irecv_error_t irecv_getret(irecv_client_t client, unsigned int* value)
return IRECV_E_OUT_OF_MEMORY;
}
- memset(response, '\0', rsize);
+ memset(response, 0, rsize);
irecv_usb_control_transfer(client, 0xC0, 0, 0, 0, (unsigned char*) response, rsize-1, USB_TIMEOUT);
*value = (unsigned int) *response;
@@ -3906,7 +4120,7 @@ irecv_error_t irecv_trigger_limera1n_exploit(irecv_client_t client)
#ifdef HAVE_IOKIT
IOReturn result;
IOUSBDevRequestTO req;
- bzero(&req, sizeof(req));
+ memset(&req, 0, sizeof(req));
req.bmRequestType = 0x21;
req.bRequest = 2;
@@ -4008,7 +4222,7 @@ irecv_error_t irecv_setenv(irecv_client_t client, const char* variable, const ch
return IRECV_E_UNKNOWN_ERROR;
}
- memset(command, '\0', sizeof(command));
+ memset(command, 0, sizeof(command));
snprintf(command, sizeof(command)-1, "setenv %s %s", variable, value);
irecv_error_t error = irecv_send_command_raw(client, command, 0);
if (error != IRECV_E_SUCCESS) {
@@ -4033,7 +4247,7 @@ irecv_error_t irecv_setenv_np(irecv_client_t client, const char* variable, const
return IRECV_E_UNKNOWN_ERROR;
}
- memset(command, '\0', sizeof(command));
+ memset(command, 0, sizeof(command));
snprintf(command, sizeof(command)-1, "setenvnp %s %s", variable, value);
irecv_error_t error = irecv_send_command_raw(client, command, 0);
if (error != IRECV_E_SUCCESS) {