Skip to content

Commit

Permalink
Add support for iOS 18 restore process
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Jun 24, 2024
1 parent de1d17d commit 28c1dab
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 131 deletions.
15 changes: 9 additions & 6 deletions src/asr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@

#define ASR_VERSION 1
#define ASR_STREAM_ID 1
#define ASR_PORT 12345
#define ASR_BUFFER_SIZE 65536
#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

int asr_open_with_timeout(idevice_t device, asr_client_t* asr)
int asr_open_with_timeout(idevice_t device, asr_client_t* asr, uint16_t port)
{
int i = 0;
int attempts = 10;
Expand All @@ -61,9 +60,13 @@ int asr_open_with_timeout(idevice_t device, asr_client_t* asr)
return -1;
}

debug("Connecting to ASR\n");
if (port == 0) {
port = ASR_DEFAULT_PORT;
}
debug("Connecting to ASR on port %u\n", port);

for (i = 1; i <= attempts; i++) {
device_error = idevice_connect(device, ASR_PORT, &connection);
device_error = idevice_connect(device, port, &connection);
if (device_error == IDEVICE_E_SUCCESS) {
break;
}
Expand Down Expand Up @@ -358,7 +361,7 @@ int asr_send_payload(asr_client_t asr, ipsw_file_handle_t file)
sendsize += 20;
}
if (asr_send_buffer(asr, data, sendsize) < 0) {
error("ERROR: Unable to send filesystem payload\n");
error("Unable to send filesystem payload chunk, retrying...\n");
retry--;
continue;
}
Expand All @@ -374,5 +377,5 @@ int asr_send_payload(asr_client_t asr, ipsw_file_handle_t file)
}

free(data);
return 0;
return (i == 0) ? 0 : -1;
}
4 changes: 3 additions & 1 deletion src/asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern "C" {

#include <libimobiledevice/libimobiledevice.h>

#define ASR_DEFAULT_PORT 12345

typedef void (*asr_progress_cb_t)(double, void*);

struct asr_client {
Expand All @@ -44,7 +46,7 @@ typedef struct asr_client *asr_client_t;
struct ipsw_file_handle;
typedef struct ipsw_file_handle* ipsw_file_handle_t;

int asr_open_with_timeout(idevice_t device, asr_client_t* asr);
int asr_open_with_timeout(idevice_t device, asr_client_t* asr, uint16_t port);
void asr_set_progress_callback(asr_client_t asr, asr_progress_cb_t, void* userdata);
int asr_send(asr_client_t asr, plist_t data);
int asr_receive(asr_client_t asr, plist_t* data);
Expand Down
36 changes: 29 additions & 7 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <libimobiledevice-glue/thread.h>

#ifdef WIN32
#include <windows.h>
Expand Down Expand Up @@ -79,17 +80,31 @@ static int info_disabled = 0;
static int error_disabled = 0;
static int debug_disabled = 0;

static mutex_t log_mutex;
static thread_once_t init_once = THREAD_ONCE_INIT;

static void _log_init(void)
{
printf("******** _log_init ********\n");
mutex_init(&log_mutex);
}

void info(const char* format, ...)
{
if (info_disabled) return;
thread_once(&init_once, _log_init);
mutex_lock(&log_mutex);
va_list vargs;
va_start(vargs, format);
vfprintf((info_stream) ? info_stream : stdout, format, vargs);
va_end(vargs);
mutex_unlock(&log_mutex);
}

void error(const char* format, ...)
{
thread_once(&init_once, _log_init);
mutex_lock(&log_mutex);
va_list vargs, vargs2;
va_start(vargs, format);
va_copy(vargs2, vargs);
Expand All @@ -99,6 +114,7 @@ void error(const char* format, ...)
vfprintf((error_stream) ? error_stream : stderr, format, vargs2);
}
va_end(vargs2);
mutex_unlock(&log_mutex);
}

void debug(const char* format, ...)
Expand All @@ -107,10 +123,13 @@ void debug(const char* format, ...)
if (!idevicerestore_debug) {
return;
}
thread_once(&init_once, _log_init);
mutex_lock(&log_mutex);
va_list vargs;
va_start(vargs, format);
vfprintf((debug_stream) ? debug_stream : stderr, format, vargs);
va_end(vargs);
mutex_unlock(&log_mutex);
}

void idevicerestore_set_info_stream(FILE* strm)
Expand Down Expand Up @@ -227,9 +246,9 @@ void debug_plist(plist_t plist) {
char* data = NULL;
plist_to_xml(plist, &data, &size);
if (size <= MAX_PRINT_LEN)
info("%s:printing %i bytes plist:\n%s", __FILE__, size, data);
info("printing %i bytes plist:\n%s", size, data);
else
info("%s:supressed printing %i bytes plist...\n", __FILE__, size);
info("supressed printing %i bytes plist...\n", size);
free(data);
}

Expand All @@ -239,13 +258,13 @@ void print_progress_bar(double progress) {
int i = 0;
if(progress < 0) return;
if(progress > 100) progress = 100;
info("\r[");
fprintf((info_stream) ? info_stream : stdout, "\r[");
for(i = 0; i < 50; i++) {
if(i < progress / 2) info("=");
else info(" ");
if(i < progress / 2) fprintf((info_stream) ? info_stream : stdout, "=");
else fprintf((info_stream) ? info_stream : stdout, " ");
}
info("] %5.1f%%", progress);
if(progress >= 100) info("\n");
fprintf((info_stream) ? info_stream : stdout, "] %5.1f%%", progress);
if(progress >= 100) fprintf((info_stream) ? info_stream : stdout, "\n");
fflush((info_stream) ? info_stream : stdout);
#endif
}
Expand Down Expand Up @@ -464,6 +483,8 @@ char *get_temp_filename(const char *prefix)

void idevicerestore_progress(struct idevicerestore_client_t* client, int step, double progress)
{
thread_once(&init_once, _log_init);
mutex_lock(&log_mutex);
if(client && client->progress_cb) {
client->progress_cb(step, progress, client->progress_cb_data);
} else {
Expand All @@ -472,6 +493,7 @@ void idevicerestore_progress(struct idevicerestore_client_t* client, int step, d
print_progress_bar(100.0 * progress);
}
}
mutex_unlock(&log_mutex);
}

#ifndef HAVE_STRSEP
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ struct idevicerestore_client_t {
char* restore_variant;
char* filesystem;
int delete_fs;
int async_err;
};

extern struct idevicerestore_mode_t idevicerestore_modes[];
Expand Down
5 changes: 3 additions & 2 deletions src/idevicerestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,11 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
}
}

info("DONE\n");

if (result == 0) {
info("DONE\n");
idevicerestore_progress(client, RESTORE_NUM_STEPS-1, 1.0);
} else {
info("RESTORE FAILED\n");
}

if (build_identity_needs_free)
Expand Down
Loading

0 comments on commit 28c1dab

Please sign in to comment.