From b7d5c4a88d5b4b93183c768f7ee907cf2fd1a5d8 Mon Sep 17 00:00:00 2001 From: nir Date: Tue, 22 Oct 2024 15:22:20 +0000 Subject: [PATCH] AWS backup --- .gitignore | 1 - src/asr.c | 23 ++++++++++++++++------- src/common.c | 17 +++++++++++++++++ src/dfu.c | 23 +++++++++++++++-------- src/fdr.c | 2 +- src/idevicerestore.c | 20 +++++++++++++++++++- src/normal.c | 10 ++++++++++ src/recovery.c | 7 ++++++- src/restore.c | 7 ++++++- 9 files changed, 90 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index bdfaa099..e8c1b827 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,6 @@ ltmain.sh missing mkinstalldirs libtool -*Makefile py-compile stamp-h1 src/.libs diff --git a/src/asr.c b/src/asr.c index 15396c4a..0caf3918 100644 --- a/src/asr.c +++ b/src/asr.c @@ -44,8 +44,8 @@ #define ASR_FEC_SLICE_STRIDE 40 #define ASR_PACKETS_PER_FEC 25 #define ASR_PAYLOAD_PACKET_SIZE 1450 -#define ASR_PAYLOAD_CHUNK_SIZE 131072 -#define ASR_CHECKSUM_CHUNK_SIZE 131072 +#define ASR_PAYLOAD_CHUNK_SIZE 32768 +#define ASR_CHECKSUM_CHUNK_SIZE 32768 int asr_open_with_timeout(idevice_t device, asr_client_t* asr, uint16_t port) { @@ -354,9 +354,11 @@ int asr_handle_oob_data_request(asr_client_t asr, plist_t packet, ipsw_file_hand free(oob_data); return 0; } - +int g_count = 0 ; int asr_send_payload(asr_client_t asr, ipsw_file_handle_t file) { + g_count++; + info("asr_send_payload small packets") ; char *data = NULL; uint64_t i, length, bytes = 0; double progress = 0; @@ -375,18 +377,20 @@ int asr_send_payload(asr_client_t asr, ipsw_file_handle_t file) if (i < ASR_PAYLOAD_CHUNK_SIZE) { size = i; } - + //info("\r\nipsw_file_read"); if (ipsw_file_read(file, data, size) != (int64_t)size) { - error("Error reading filesystem\n"); + info("Error reading filesystem\n"); retry--; continue; } - + //info("ipsw_file_read read done"); sendsize = size; if (asr->checksum_chunks) { sha1((unsigned char*)data, size, (unsigned char*)(data+size)); sendsize += 20; } + + // info("\r\nasr_send_buffer"); if (asr_send_buffer(asr, data, sendsize) < 0) { error("Unable to send filesystem payload chunk, retrying...\n"); retry--; @@ -401,8 +405,13 @@ int asr_send_payload(asr_client_t asr, ipsw_file_handle_t file) } i -= size; + if(g_count%20==0) + Sleep(1) ; + //info("again"); } - + info("\r\nfree"); free(data); + info("\r\nreturn"); return (i == 0) ? 0 : -1; + } diff --git a/src/common.c b/src/common.c index c7be4f66..4e7d71b0 100644 --- a/src/common.c +++ b/src/common.c @@ -79,6 +79,15 @@ static FILE* debug_stream = NULL; static int info_disabled = 0; static int error_disabled = 0; static int debug_disabled = 0; +void printTime() +{ + time_t rawtime; + struct tm * timeinfo; + time(&rawtime); + timeinfo = localtime(&rawtime); + // Print current time in human-readable format + printf("\nCurrent time: %02d:%02d:%02d\n %d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); +} static mutex_t log_mutex; static thread_once_t init_once = THREAD_ONCE_INIT; @@ -97,8 +106,11 @@ void info(const char* format, ...) va_start(vargs, format); vfprintf((info_stream) ? info_stream : stdout, format, vargs); va_end(vargs); + printTime() ; fflush(info_stream?info_stream:stdout); + mutex_unlock(&log_mutex); + } void error(const char* format, ...) @@ -114,8 +126,11 @@ void error(const char* format, ...) vfprintf((error_stream) ? error_stream : stderr, format, vargs2); } va_end(vargs2); + printTime() ; + fflush(error_stream?error_stream:stderr); mutex_unlock(&log_mutex); + } void debug(const char* format, ...) @@ -130,8 +145,10 @@ void debug(const char* format, ...) va_start(vargs, format); vfprintf((debug_stream) ? debug_stream : stderr, format, vargs); va_end(vargs); + printTime(); fflush(debug_stream?debug_stream:stderr); mutex_unlock(&log_mutex); + } void idevicerestore_set_info_stream(FILE* strm) diff --git a/src/dfu.c b/src/dfu.c index e6b45afa..c1990381 100644 --- a/src/dfu.c +++ b/src/dfu.c @@ -466,19 +466,16 @@ int dfu_enter_recovery(struct idevicerestore_client_t* client, plist_t build_ide } dfu_client_free(client); + int tries = 3; if (client->build_major > 8) { /* reconnect */ debug("Waiting for device to disconnect...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 10000); - if (client->mode != MODE_UNKNOWN || (client->flags & FLAG_QUIT)) { - mutex_unlock(&client->device_event_mutex); - if (!(client->flags & FLAG_QUIT)) { - error("ERROR: Device did not disconnect. Possibly invalid iBSS. Reset device and try again.\n"); - } - return -1; + tries = 3 ; + while(tries-- && ((client->mode != MODE_DFU && client->mode != MODE_RECOVERY) || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); } - debug("Waiting for device to reconnect...\n"); - cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 10000); if ((client->mode != MODE_DFU && client->mode != MODE_RECOVERY) || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); if (!(client->flags & FLAG_QUIT)) { @@ -645,6 +642,11 @@ int dfu_enter_recovery(struct idevicerestore_client_t* client, plist_t build_ide debug("Waiting for device to disconnect...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 10000); + tries = 3 ; + while(tries-- && (client->mode != MODE_UNKNOWN || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode != MODE_UNKNOWN || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); if (!(client->flags & FLAG_QUIT)) { @@ -653,6 +655,11 @@ int dfu_enter_recovery(struct idevicerestore_client_t* client, plist_t build_ide return -1; } debug("Waiting for device to reconnect in recovery mode...\n"); + tries = 3 ; + while(tries-- && (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 10000); if (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); diff --git a/src/fdr.c b/src/fdr.c index ca9b7c75..9e30ac65 100644 --- a/src/fdr.c +++ b/src/fdr.c @@ -142,7 +142,7 @@ int fdr_poll_and_handle_message(fdr_client_t fdr) return -1; } - device_error = idevice_connection_receive_timeout(fdr->connection, (char *)&cmd, sizeof(cmd), &bytes, 20000); + device_error = idevice_connection_receive_timeout(fdr->connection, (char *)&cmd, sizeof(cmd), &bytes, 40000); #ifdef HAVE_IDEVICE_E_TIMEOUT if (device_error == IDEVICE_E_TIMEOUT || (device_error == IDEVICE_E_SUCCESS && bytes != sizeof(cmd))) #else diff --git a/src/idevicerestore.c b/src/idevicerestore.c index b8bb1d06..1f5d9ec8 100644 --- a/src/idevicerestore.c +++ b/src/idevicerestore.c @@ -1436,6 +1436,11 @@ int idevicerestore_start(struct idevicerestore_client_t* client) debug("Waiting for device to disconnect...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 60000); + int tries = 3 ; + while(tries-- && (client->mode != MODE_UNKNOWN || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode != MODE_UNKNOWN || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); @@ -1447,6 +1452,11 @@ int idevicerestore_start(struct idevicerestore_client_t* client) recovery_client_free(client); debug("Waiting for device to reconnect in recovery mode...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 60000); + tries = 3 ; + while(tries-- && (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); if (!(client->flags & FLAG_QUIT)) { @@ -1510,7 +1520,6 @@ int idevicerestore_start(struct idevicerestore_client_t* client) plist_free(client->tss); return -2; } - recovery_client_free(client); } idevicerestore_progress(client, RESTORE_STEP_PREPARE, 0.9); @@ -1518,6 +1527,11 @@ int idevicerestore_start(struct idevicerestore_client_t* client) mutex_lock(&client->device_event_mutex); info("Waiting for device to enter restore mode...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 180000); + int tries = 13 ; + while(tries-- && (client->mode != MODE_RESTORE || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode != MODE_RESTORE || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); error("ERROR: Device failed to enter restore mode.\n"); @@ -1727,9 +1741,12 @@ void plain_progress_cb(int step, double step_progress, void* userdata) { printf("progress: %u %f\n", step, step_progress); fflush(stdout); + fflush(stderr); } int main(int argc, char* argv[]) { + //setbuf(stdout, NULL); + //setbuf(stderr, NULL); int opt = 0; int optindex = 0; char* ipsw = NULL; @@ -1967,6 +1984,7 @@ int main(int argc, char* argv[]) { irecv_device_t get_irecv_device(struct idevicerestore_client_t *client) { + info("get_irecv_device"); int mode = _MODE_UNKNOWN; if (client->mode) { diff --git a/src/normal.c b/src/normal.c index 9e460803..b61c63d0 100644 --- a/src/normal.c +++ b/src/normal.c @@ -260,6 +260,11 @@ int normal_enter_recovery(struct idevicerestore_client_t* client) mutex_lock(&client->device_event_mutex); debug("DEBUG: Waiting for device to disconnect...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 60000); + int tries = 3 ; + while(tries-- && (client->mode == MODE_NORMAL || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode == MODE_NORMAL || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); error("ERROR: Failed to place device in recovery mode\n"); @@ -268,6 +273,11 @@ int normal_enter_recovery(struct idevicerestore_client_t* client) debug("DEBUG: Waiting for device to connect in recovery mode...\n"); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 60000); + tries = 3 ; + while(tries-- && (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode != MODE_RECOVERY || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); error("ERROR: Failed to enter recovery mode\n"); diff --git a/src/recovery.c b/src/recovery.c index c4513502..8031b493 100644 --- a/src/recovery.c +++ b/src/recovery.c @@ -234,7 +234,12 @@ int recovery_enter_restore(struct idevicerestore_client_t* client, plist_t build } debug("DEBUG: Waiting for device to disconnect...\n"); - cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 30000); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 40000); + int tries = 13 ; + while(tries-- && (client->mode == MODE_RECOVERY || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode == MODE_RECOVERY || (client->flags & FLAG_QUIT)) { mutex_unlock(&client->device_event_mutex); error("ERROR: Failed to place device in restore mode\n"); diff --git a/src/restore.c b/src/restore.c index 2daeea2b..e51edc44 100644 --- a/src/restore.c +++ b/src/restore.c @@ -364,6 +364,11 @@ int restore_reboot(struct idevicerestore_client_t* client) restored_client_free(client->restore->client); cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 30000); + int tries = 3 ; + while(tries-- && (client->mode == MODE_RECOVERY || (client->flags & FLAG_QUIT))) { + debug("cond_wait_timeout retry\n"); + cond_wait_timeout(&client->device_event_cond, &client->device_event_mutex, 3000); + } if (client->mode == MODE_RESTORE) { mutex_unlock(&client->device_event_mutex); return -1; @@ -5705,4 +5710,4 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit restore_client_free(client); return err; -} +} \ No newline at end of file