Skip to content

Commit

Permalink
AWS backup
Browse files Browse the repository at this point in the history
  • Loading branch information
nirhen committed Oct 22, 2024
1 parent 151c680 commit b7d5c4a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 20 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ ltmain.sh
missing
mkinstalldirs
libtool
*Makefile
py-compile
stamp-h1
src/.libs
Expand Down
23 changes: 16 additions & 7 deletions src/asr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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--;
Expand All @@ -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;

}
17 changes: 17 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, ...)
Expand All @@ -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, ...)
Expand All @@ -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)
Expand Down
23 changes: 15 additions & 8 deletions src/dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/fdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/idevicerestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)) {
Expand Down Expand Up @@ -1510,14 +1520,18 @@ 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);

if (client->mode != MODE_RESTORE) {
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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/normal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
7 changes: 6 additions & 1 deletion src/recovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
7 changes: 6 additions & 1 deletion src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -5705,4 +5710,4 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit

restore_client_free(client);
return err;
}
}

0 comments on commit b7d5c4a

Please sign in to comment.