summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2026-11-27 03:11:51 +0100
committerGravatar Nikias Bassen2026-11-27 03:11:51 +0100
commited14f0693d80947f539c2a7597999fb90ee1dcf9 (patch)
treefcab6a818c79173ec4dcb0ebcf8ff33330e641ad
parent784d3d7069cfd9165013542bdc89c107cfc2af2e (diff)
downloadidevicerestore-ed14f0693d80947f539c2a7597999fb90ee1dcf9.tar.gz
idevicerestore-ed14f0693d80947f539c2a7597999fb90ee1dcf9.tar.bz2
Re-detect device mode after long-running preparation stepsHEADmaster
The device can reboot while idevicerestore is busy with slow steps such as downloading the IPSW (-l) or extracting the filesystem. E.g. a device in Recovery mode may time out and reboot to Normal mode. client->mode then ends up as MODE_UNKNOWN, and later calls like get_ap_nonce() fail with the unhelpful "Device is in an invalid state" error. Add resync_device_mode(), which waits up to 60 seconds for the device to reappear and logs when the mode changed. Call it after the on-demand IPSW download and after the filesystem extraction. If the device returned in Normal mode, refresh the cached product/build version, which is factored out into refresh_device_version().
-rw-r--r--src/idevicerestore.c70
1 files changed, 60 insertions, 10 deletions
diff --git a/src/idevicerestore.c b/src/idevicerestore.c
index b55e585..825e24c 100644
--- a/src/idevicerestore.c
+++ b/src/idevicerestore.c
@@ -361,6 +361,54 @@ static void irecv_event_cb(const irecv_device_event_t* event, void *userdata)
int build_identity_check_components_in_ipsw(plist_t build_identity, ipsw_archive_t ipsw);
+static void refresh_device_version(struct idevicerestore_client_t* client)
+{
+ plist_t pver = normal_get_lockdown_value(client, NULL, "ProductVersion");
+ if (pver) {
+ free(client->device_version);
+ client->device_version = NULL;
+ plist_get_string_val(pver, &client->device_version);
+ plist_free(pver);
+ }
+ pver = normal_get_lockdown_value(client, NULL, "BuildVersion");
+ if (pver) {
+ free(client->device_build);
+ client->device_build = NULL;
+ plist_get_string_val(pver, &client->device_build);
+ plist_free(pver);
+ }
+}
+
+/* The device can reboot (e.g. recovery mode timeout) during long operations such
+ * as the IPSW download. Wait for it to reappear and pick up its current mode. */
+static int resync_device_mode(struct idevicerestore_client_t* client, const struct idevicerestore_mode_t** known_mode)
+{
+ time_t start = time(NULL);
+ mutex_lock(&client->device_event_mutex);
+ while (client->mode == MODE_UNKNOWN && !(client->flags & FLAG_QUIT)) {
+ if (time(NULL) - start >= 60) {
+ break;
+ }
+ cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 1000);
+ }
+ if (client->mode == MODE_UNKNOWN || (client->flags & FLAG_QUIT)) {
+ mutex_unlock(&client->device_event_mutex);
+ logger(LL_ERROR, "Device is no longer connected. Please make sure it is attached and try again.\n");
+ return -1;
+ }
+ const struct idevicerestore_mode_t* mode = client->mode;
+ mutex_unlock(&client->device_event_mutex);
+
+ if (mode != *known_mode) {
+ logger(LL_NOTICE, "Device mode changed from %s to %s\n", (*known_mode)->string, mode->string);
+ *known_mode = mode;
+ if (mode == MODE_NORMAL) {
+ refresh_device_version(client);
+ }
+ }
+ return 0;
+}
+
int idevicerestore_start(struct idevicerestore_client_t* client)
{
int tss_enabled = 0;
@@ -527,16 +575,7 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
}
if (client->mode == MODE_NORMAL) {
- plist_t pver = normal_get_lockdown_value(client, NULL, "ProductVersion");
- if (pver) {
- plist_get_string_val(pver, &client->device_version);
- plist_free(pver);
- }
- pver = normal_get_lockdown_value(client, NULL, "BuildVersion");
- if (pver) {
- plist_get_string_val(pver, &client->device_build);
- plist_free(pver);
- }
+ refresh_device_version(client);
}
logger(LL_INFO, "Device Product Version: %s\n", (client->device_version) ? client->device_version : "N/A");
logger(LL_INFO, "Device Product Build: %s\n", (client->device_build) ? client->device_build : "N/A");
@@ -575,6 +614,8 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
#endif
}
+ const struct idevicerestore_mode_t* detected_mode = client->mode;
+
if (client->flags & FLAG_LATEST) {
char *fwurl = NULL;
unsigned char fwsha1[20];
@@ -673,6 +714,10 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
}
free(ipsw);
}
+ // the download can take long enough for the device to have rebooted
+ if (resync_device_mode(client, &detected_mode) < 0) {
+ return -1;
+ }
}
idevicerestore_progress(client, RESTORE_STEP_DETECT, 0.6);
@@ -1198,6 +1243,11 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
return -1;
}
client->filesystem = tmpf;
+
+ // the extraction can take long enough for the device to have rebooted
+ if (resync_device_mode(client, &detected_mode) < 0) {
+ return -1;
+ }
}
}