// If the package contains an update binary, extract it and run it. static int try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { mzCloseZipArchive(zip); return INSTALL_CORRUPT; } fprintf(stderr, "try_update_binary(path(%s))\n",path); radio_diff = mzFindZipEntry(zip, RADIO_DIFF_NAME); if (radio_diff == NULL) { fprintf(stderr, "%s not found\n", RADIO_DIFF_NAME); } else { char* diff_file = RADIO_DIFF_OUTPUT; int fd_diff = creat(diff_file, 0777); fprintf(stderr, "%s found\n", RADIO_DIFF_NAME); if (fd_diff < 0) { fprintf(stderr, "Can't make %s\n", diff_file); } else { bool ok_diff = mzExtractZipEntryToFile(zip, radio_diff, fd_diff); close(fd_diff); if (!ok_diff) { fprintf(stderr, "Can't copy %s\n", RADIO_DIFF_NAME); } } } char* binary = "/tmp/update_binary"; unlink(binary); int fd = creat(binary, 0755); if (fd < 0) { mzCloseZipArchive(zip); LOGE("Can't make %s\n", binary); return INSTALL_ERROR; } bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); close(fd); mzCloseZipArchive(zip); if (!ok) { LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); return INSTALL_ERROR; } int pipefd[2]; pipe(pipefd); // When executing the update binary contained in the package, the // arguments passed are: // // - the version number for this interface // // - an fd to which the program can write in order to update the // progress bar. The program can write single-line commands: // // progress <frac> <secs> // fill up the next <frac> part of of the progress bar // over <secs> seconds. If <secs> is zero, use // set_progress commands to manually control the // progress of this segment of the bar // // set_progress <frac> // <frac> should be between 0.0 and 1.0; sets the // progress bar within the segment defined by the most // recent progress command. // // firmware <"hboot"|"radio"> <filename> // arrange to install the contents of <filename> in the // given partition on reboot. // // (API v2: <filename> may start with "PACKAGE:" to // indicate taking a file from the OTA package.) // // (API v3: this command no longer exists.) // // ui_print <string> // display <string> on the screen. // // - the name of the package zip file. // char** args = malloc(sizeof(char*) * 5); args[0] = binary; args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk args[2] = malloc(10); sprintf(args[2], "%d", pipefd[1]); args[3] = (char*)path; args[4] = NULL; pid_t pid = fork(); if (pid == 0) { close(pipefd[0]); execv(binary, args); fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); _exit(-1); } close(pipefd[1]); *wipe_cache = 0; char buffer[1024]; FILE* from_child = fdopen(pipefd[0], "r"); while (fgets(buffer, sizeof(buffer), from_child) != NULL) { char* command = strtok(buffer, " \n"); if (command == NULL) { continue; } else if (strcmp(command, "progress") == 0) { char* fraction_s = strtok(NULL, " \n"); char* seconds_s = strtok(NULL, " \n"); float fraction = strtof(fraction_s, NULL); int seconds = strtol(seconds_s, NULL, 10); ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); } else if (strcmp(command, "set_progress") == 0) { char* fraction_s = strtok(NULL, " \n"); float fraction = strtof(fraction_s, NULL); ui_set_progress(fraction); } else if (strcmp(command, "ui_print") == 0) { char* str = strtok(NULL, "\n"); if (str) { ui_print("%s", str); } else { ui_print("\n"); } } else if (strcmp(command, "wipe_cache") == 0) { *wipe_cache = 1; } else { LOGE("unknown command [%s]\n", command); } } fclose(from_child); int status; waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); return INSTALL_ERROR; } return INSTALL_SUCCESS; }
int nandroid_back_exe() { SetDataState("Starting", "backup", 0, 0); if (ensure_path_mounted(SDCARD_ROOT) != 0) { ui_print("-- Could not mount: %s.\n-- Aborting.\n",SDCARD_ROOT); SetDataState("Unable to mount", "/sdcard", 1, 1); return 1; } // Create backup folder struct tm *t; char timestamp[64]; char image_dir[255]; char exe[255]; time_t start, stop; time_t seconds; seconds = time(0); t = localtime(&seconds); sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec); // make time stamp sprintf(image_dir,"%s/%s/%s/", backup_folder, device_id, timestamp); // for backup folder if (recursive_mkdir(image_dir)) { LOGE("Unable to create folder: '%s'\n", backup_folder); SetDataState("Backup failed", image_dir, 1, 1); return -1; } // Record the start time time(&start); // Prepare operation ui_print("\n[BACKUP STARTED]\n"); ui_print(" * Backup Folder: %s\n", backup_folder); ui_print(" * Verifying filesystems...\n"); verifyFst(); createFstab(); ui_print(" * Verifying partition sizes...\n"); updateUsedSized(); unsigned long long sdc_free = sdcext.sze - sdcext.used; // Compute totals int total = 0; unsigned long long total_img_bytes = 0, total_file_bytes = 0; CalculateBackupDetails(&total, &total_img_bytes, &total_file_bytes); unsigned long long total_bytes = total_img_bytes + total_file_bytes; if (total == 0 || total_bytes == 0) { LOGE("Unable to compute target usage (%d partitions, %llu bytes)\n", total, total_bytes); SetDataState("Backup failed", image_dir, 1, 1); return -1; } ui_print(" * Total number of partition to back up: %d\n", total); ui_print(" * Total size of all data, in KB: %llu\n", total_bytes / 1024); ui_print(" * Available space on the SD card, in KB: %llu\n", sdc_free / 1024); // We can't verify sufficient space on devices where sdcard is a portion of the data partition #ifndef RECOVERY_SDCARD_ON_DATA // Verify space if (sdc_free < (total_bytes + 0x2000000)) // We require at least 32MB of additional space { LOGE("Insufficient space on SDCARD. Required space is %lluKB, available %lluKB\n", (total_bytes + 0x2000000) / 1024, sdc_free / 1024); SetDataState("Backup failed", image_dir, 1, 1); return -1; } #else ui_print(" * This device does not support verifying available free space.\n"); #endif // Prepare progress bar... unsigned long long img_bytes_remaining = total_img_bytes; unsigned long long file_bytes_remaining = total_file_bytes; unsigned long img_byte_time = 0, file_byte_time = 0; struct stat st; ui_set_progress(0.0); // SYSTEM if (phx_do_backup(VAR_BACKUP_SYSTEM_VAR, &sys, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // DATA if (phx_do_backup(VAR_BACKUP_DATA_VAR, &dat, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // BOOT if (phx_do_backup(VAR_BACKUP_BOOT_VAR, &boo, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // RECOVERY if (phx_do_backup(VAR_BACKUP_RECOVERY_VAR, &rec, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // CACHE if (phx_do_backup(VAR_BACKUP_CACHE_VAR, &cac, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // SP1 if (phx_do_backup(VAR_BACKUP_SP1_VAR, &sp1, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // SP2 if (phx_do_backup(VAR_BACKUP_SP2_VAR, &sp2, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // SP3 if (phx_do_backup(VAR_BACKUP_SP3_VAR, &sp3, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // ANDROID-SECURE if (stat(ase.dev, &st) ==0) if (phx_do_backup(VAR_BACKUP_ANDSEC_VAR, &ase, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; // SD-EXT if (stat(sde.dev, &st) ==0) if (phx_do_backup(VAR_BACKUP_SDEXT_VAR, &sde, image_dir, total_img_bytes, total_file_bytes, &img_bytes_remaining, &file_bytes_remaining, &img_byte_time, &file_byte_time)) return 1; ui_print(" * Verifying filesystems...\n"); verifyFst(); createFstab(); ui_print(" * Verifying partition sizes...\n"); updateUsedSized(); time(&stop); // Average BPS unsigned long int img_bps = total_img_bytes / img_byte_time; unsigned long int file_bps = total_file_bytes / file_byte_time; LOGI("img_bps = %lu total_img_bytes = %llu img_byte_time = %lu\n", img_bps, total_img_bytes, img_byte_time); ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024))); ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024))); if (DataManager_GetIntValue(VAR_SKIP_MD5_GENERATE_VAR) == 1) { // If we're skipping MD5 generation, our BPS is faster by about 1.65 file_bps = (unsigned long) (file_bps / 1.65); img_bps = (unsigned long) (img_bps / 1.65); } img_bps += (DataManager_GetIntValue(VAR_BACKUP_AVG_IMG_RATE) * 4); img_bps /= 5; if (DataManager_GetIntValue(VAR_USE_COMPRESSION_VAR)) file_bps += (DataManager_GetIntValue(VAR_BACKUP_AVG_FILE_COMP_RATE) * 4); else file_bps += (DataManager_GetIntValue(VAR_BACKUP_AVG_FILE_RATE) * 4); file_bps /= 5; DataManager_SetIntValue(VAR_BACKUP_AVG_IMG_RATE, img_bps); if (DataManager_GetIntValue(VAR_USE_COMPRESSION_VAR)) DataManager_SetIntValue(VAR_BACKUP_AVG_FILE_COMP_RATE, file_bps); else DataManager_SetIntValue(VAR_BACKUP_AVG_FILE_RATE, file_bps); int total_time = (int) difftime(stop, start); unsigned long long new_sdc_free = (sdcext.sze - sdcext.used) / (1024 * 1024); sdc_free /= (1024 * 1024); ui_print("[%lu MB TOTAL BACKED UP TO SDCARD]\n",(unsigned long) (sdc_free - new_sdc_free)); ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end SetDataState("Backup Succeeded", "", 0, 1); return 0; }
// If the package contains an update binary, extract it and run it. static int try_update_binary(const char *path, ZipArchive *zip) { const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { const ZipEntry* update_script_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME); if (update_script_entry != NULL) { ui_print("Amend scripting (update-script) is no longer supported.\n"); ui_print("Amend scripting was deprecated by Google in Android 1.5.\n"); ui_print("It was necessary to remove it when upgrading to the ClockworkMod 3.0 Gingerbread based recovery.\n"); ui_print("Please switch to Edify scripting (updater-script and update-binary) to create working update zip packages.\n"); return INSTALL_UPDATE_BINARY_MISSING; } mzCloseZipArchive(zip); return INSTALL_UPDATE_BINARY_MISSING; } char* binary = "/tmp/update_binary"; unlink(binary); int fd = creat(binary, 0755); if (fd < 0) { mzCloseZipArchive(zip); LOGE("Can't make %s\n", binary); return 1; } bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); close(fd); if (!ok) { LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); mzCloseZipArchive(zip); return 1; } int pipefd[2]; pipe(pipefd); // When executing the update binary contained in the package, the // arguments passed are: // // - the version number for this interface // // - an fd to which the program can write in order to update the // progress bar. The program can write single-line commands: // // progress <frac> <secs> // fill up the next <frac> part of of the progress bar // over <secs> seconds. If <secs> is zero, use // set_progress commands to manually control the // progress of this segment of the bar // // set_progress <frac> // <frac> should be between 0.0 and 1.0; sets the // progress bar within the segment defined by the most // recent progress command. // // firmware <"hboot"|"radio"> <filename> // arrange to install the contents of <filename> in the // given partition on reboot. // // (API v2: <filename> may start with "PACKAGE:" to // indicate taking a file from the OTA package.) // // (API v3: this command no longer exists.) // // ui_print <string> // display <string> on the screen. // // - the name of the package zip file. // char** args = malloc(sizeof(char*) * 5); args[0] = binary; args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk args[2] = malloc(10); sprintf(args[2], "%d", pipefd[1]); args[3] = (char*)path; args[4] = NULL; pid_t pid = fork(); if (pid == 0) { setenv("UPDATE_PACKAGE", path, 1); close(pipefd[0]); execv(binary, args); fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); _exit(-1); } close(pipefd[1]); char* firmware_type = NULL; char* firmware_filename = NULL; char buffer[1024]; FILE* from_child = fdopen(pipefd[0], "r"); while (fgets(buffer, sizeof(buffer), from_child) != NULL) { char* command = strtok(buffer, " \n"); if (command == NULL) { continue; } else if (strcmp(command, "progress") == 0) { char* fraction_s = strtok(NULL, " \n"); char* seconds_s = strtok(NULL, " \n"); float fraction = strtof(fraction_s, NULL); int seconds = strtol(seconds_s, NULL, 10); ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); } else if (strcmp(command, "set_progress") == 0) { char* fraction_s = strtok(NULL, " \n"); float fraction = strtof(fraction_s, NULL); ui_set_progress(fraction); } else if (strcmp(command, "firmware") == 0) { char* type = strtok(NULL, " \n"); char* filename = strtok(NULL, " \n"); if (type != NULL && filename != NULL) { if (firmware_type != NULL) { LOGE("ignoring attempt to do multiple firmware updates"); } else { firmware_type = strdup(type); firmware_filename = strdup(filename); } } } else if (strcmp(command, "ui_print") == 0) { char* str = strtok(NULL, "\n"); if (str) { ui_print("%s", str); } else { ui_print("\n"); } } else { LOGE("unknown command [%s]\n", command); } } fclose(from_child); int status; waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); mzCloseZipArchive(zip); return INSTALL_ERROR; } if (firmware_type != NULL) { int ret = handle_firmware_update(firmware_type, firmware_filename, zip); mzCloseZipArchive(zip); return ret; } mzCloseZipArchive(zip); return INSTALL_SUCCESS; }
int phx_do_backup(const char* enableVar, struct dInfo* mnt, const char* image_dir, unsigned long long img_bytes, unsigned long long file_bytes, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long* img_byte_time, unsigned long* file_byte_time) { // Check if this partition is being backed up... if (!DataManager_GetIntValue(enableVar)) return 0; // Let's calculate the percentage of the bar this section is expected to take unsigned long int img_bps = DataManager_GetIntValue(VAR_BACKUP_AVG_IMG_RATE); unsigned long int file_bps; if (DataManager_GetIntValue(VAR_USE_COMPRESSION_VAR)) file_bps = DataManager_GetIntValue(VAR_BACKUP_AVG_FILE_COMP_RATE); else file_bps = DataManager_GetIntValue(VAR_BACKUP_AVG_FILE_RATE); if (DataManager_GetIntValue(VAR_SKIP_MD5_GENERATE_VAR) == 1) { // If we're skipping MD5 generation, our BPS is faster by about 1.65 file_bps = (unsigned long) (file_bps * 1.65); img_bps = (unsigned long) (img_bps * 1.65); } // We know the speed for both, how far into the whole backup are we, based on time unsigned long total_time = (img_bytes / img_bps) + (file_bytes / file_bps); unsigned long remain_time = (*img_bytes_remaining / img_bps) + (*file_bytes_remaining / file_bps); float pos = (total_time - remain_time) / (float) total_time; ui_set_progress(pos); LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time); // Determine how much we're about to process unsigned long long blocksize = get_backup_size(mnt); // And get the time unsigned long section_time; if (mnt->backup == image) section_time = blocksize / img_bps; else section_time = blocksize / file_bps; // Set the position pos = section_time / (float) total_time; ui_show_progress(pos, section_time); time_t start, stop; time(&start); if (phx_backup(*mnt, image_dir) == 1) { // did the backup process return an error ? 0 = no error SetDataState("Backup failed", mnt->mnt, 1, 1); ui_print("-- Error occured, check recovery.log. Aborting.\n"); //oh noes! abort abort! return 1; } time(&stop); LOGI("Partition Backup time: %d\n", (int) difftime(stop, start)); // Now, decrement out byte counts if (mnt->backup == image) { *img_bytes_remaining -= blocksize; *img_byte_time += (int) difftime(stop, start); } else { *file_bytes_remaining -= blocksize; *file_byte_time += (int) difftime(stop, start); } return 0; }