Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 1 | /* OpenRecoveryScript class for TWRP |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 15 | * 02110-1301, USA. |
| 16 | * |
| 17 | * The code was written from scratch by Dees_Troy dees_troy at |
| 18 | * yahoo |
| 19 | * |
| 20 | * Copyright (c) 2012 |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/vfs.h> |
| 28 | #include <unistd.h> |
| 29 | #include <vector> |
| 30 | #include <dirent.h> |
| 31 | #include <time.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | #include "twrp-functions.hpp" |
| 35 | #include "partitions.hpp" |
| 36 | #include "common.h" |
| 37 | #include "openrecoveryscript.hpp" |
| 38 | #include "variables.h" |
| 39 | extern "C" { |
| 40 | #include "data.h" |
| 41 | #include "twinstall.h" |
| 42 | int TWinstall_zip(const char* path, int* wipe_cache); |
| 43 | } |
| 44 | |
| 45 | static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript"; |
| 46 | static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript"; |
| 47 | #define SCRIPT_COMMAND_SIZE 512 |
| 48 | |
| 49 | int OpenRecoveryScript::check_for_script_file(void) { |
| 50 | char exec[512]; |
| 51 | |
| 52 | if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { |
| 53 | LOGE("Unable to mount /cache for OpenRecoveryScript support.\n"); |
| 54 | return 0; |
| 55 | } |
| 56 | if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { |
| 57 | LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE); |
| 58 | // Copy script file to /tmp |
| 59 | strcpy(exec, "cp "); |
| 60 | strcat(exec, SCRIPT_FILE_CACHE); |
| 61 | strcat(exec, " "); |
| 62 | strcat(exec, SCRIPT_FILE_TMP); |
| 63 | system(exec); |
| 64 | // Delete the file from /cache |
| 65 | strcpy(exec, "rm "); |
| 66 | strcat(exec, SCRIPT_FILE_CACHE); |
| 67 | system(exec); |
| 68 | return 1; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int OpenRecoveryScript::run_script_file(void) { |
| 74 | FILE *fp = fopen(SCRIPT_FILE_TMP, "r"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 75 | int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 76 | char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE], |
| 77 | value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE], |
| 78 | value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE]; |
| 79 | char *val_start, *tok; |
| 80 | |
| 81 | if (fp != NULL) { |
Dees_Troy | 3ac71a5 | 2012-10-01 19:46:11 -0400 | [diff] [blame] | 82 | DataManager_SetIntValue(TW_SIMULATE_ACTIONS, 0); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 83 | while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) { |
| 84 | cindex = 0; |
| 85 | line_len = strlen(script_line); |
| 86 | if (line_len < 2) |
| 87 | continue; // there's a blank line or line is too short to contain a command |
| 88 | //ui_print("script line: '%s'\n", script_line); |
| 89 | for (i=0; i<line_len; i++) { |
| 90 | if ((int)script_line[i] == 32) { |
| 91 | cindex = i; |
| 92 | i = line_len; |
| 93 | } |
| 94 | } |
| 95 | memset(command, 0, sizeof(command)); |
| 96 | memset(value, 0, sizeof(value)); |
| 97 | if ((int)script_line[line_len - 1] == 10) |
| 98 | remove_nl = 2; |
| 99 | else |
| 100 | remove_nl = 1; |
| 101 | if (cindex != 0) { |
| 102 | strncpy(command, script_line, cindex); |
| 103 | LOGI("command is: '%s' and ", command); |
| 104 | val_start = script_line; |
| 105 | val_start += cindex + 1; |
| 106 | strncpy(value, val_start, line_len - cindex - remove_nl); |
| 107 | LOGI("value is: '%s'\n", value); |
| 108 | } else { |
| 109 | strncpy(command, script_line, line_len - remove_nl + 1); |
| 110 | ui_print("command is: '%s' and there is no value\n", command); |
| 111 | } |
| 112 | if (strcmp(command, "install") == 0) { |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 113 | // Install Zip |
| 114 | ret_val = Install_Command(value); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 115 | install_cmd = -1; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 116 | } else if (strcmp(command, "wipe") == 0) { |
| 117 | // Wipe |
| 118 | if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) { |
| 119 | ui_print("-- Wiping Cache Partition...\n"); |
| 120 | PartitionManager.Wipe_By_Path("/cache"); |
| 121 | ui_print("-- Cache Partition Wipe Complete!\n"); |
| 122 | } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) { |
| 123 | ui_print("-- Wiping Dalvik Cache...\n"); |
| 124 | PartitionManager.Wipe_Dalvik_Cache(); |
| 125 | ui_print("-- Dalvik Cache Wipe Complete!\n"); |
| 126 | } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) { |
| 127 | ui_print("-- Wiping Data Partition...\n"); |
| 128 | PartitionManager.Factory_Reset(); |
| 129 | ui_print("-- Data Partition Wipe Complete!\n"); |
| 130 | } else { |
| 131 | LOGE("Error with wipe command value: '%s'\n", value); |
| 132 | ret_val = 1; |
| 133 | } |
| 134 | } else if (strcmp(command, "backup") == 0) { |
| 135 | // Backup |
| 136 | tok = strtok(value, " "); |
| 137 | strcpy(value1, tok); |
| 138 | tok = strtok(NULL, " "); |
| 139 | if (tok != NULL) { |
| 140 | memset(value2, 0, sizeof(value2)); |
| 141 | strcpy(value2, tok); |
| 142 | line_len = strlen(tok); |
| 143 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) { |
| 144 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) |
| 145 | remove_nl = 2; |
| 146 | else |
| 147 | remove_nl = 1; |
| 148 | } else |
| 149 | remove_nl = 0; |
| 150 | strncpy(value2, tok, line_len - remove_nl); |
| 151 | DataManager_SetStrValue(TW_BACKUP_NAME, value2); |
| 152 | ui_print("Backup folder set to '%s'\n", value2); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 153 | if (PartitionManager.Check_Backup_Name(true) != 0) { |
| 154 | ret_val = 1; |
| 155 | continue; |
| 156 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 157 | } else { |
| 158 | char empt[50]; |
| 159 | strcpy(empt, "(Current Date)"); |
| 160 | DataManager_SetStrValue(TW_BACKUP_NAME, empt); |
| 161 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 162 | ret_val = Backup_Command(value1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 163 | } else if (strcmp(command, "restore") == 0) { |
| 164 | // Restore |
| 165 | PartitionManager.Mount_All_Storage(); |
| 166 | DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 0); |
| 167 | |
| 168 | string val = value, restore_folder, restore_partitions; |
| 169 | size_t pos = val.find_last_of(" "); |
| 170 | if (pos == string::npos) { |
| 171 | ui_print("Malformed restore parameter: '%s'\n", value1); |
| 172 | ret_val = 1; |
| 173 | continue; |
| 174 | } |
| 175 | restore_folder = val.substr(0, pos); |
| 176 | char folder_path[512], partitions[512]; |
| 177 | strcpy(folder_path, restore_folder.c_str()); |
| 178 | restore_partitions = val.substr(pos + 1, val.size() - pos - 1); |
| 179 | strcpy(partitions, restore_partitions.c_str()); |
| 180 | LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions); |
| 181 | ui_print("Restoring '%s'\n", folder_path); |
| 182 | |
| 183 | if (folder_path[0] != '/') { |
| 184 | char backup_folder[512]; |
| 185 | sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path); |
| 186 | LOGI("Restoring relative path: '%s'\n", backup_folder); |
| 187 | if (!TWFunc::Path_Exists(backup_folder)) { |
| 188 | if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 189 | if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
| 190 | LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path); |
| 191 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 192 | } else { |
| 193 | LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path); |
| 194 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 195 | } |
| 196 | sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path); |
| 197 | LOGI("2Restoring relative path: '%s'\n", backup_folder); |
| 198 | } |
| 199 | } |
| 200 | strcpy(folder_path, backup_folder); |
| 201 | } else { |
| 202 | if (folder_path[strlen(folder_path) - 1] == '/') |
| 203 | strcat(folder_path, "."); |
| 204 | else |
| 205 | strcat(folder_path, "/."); |
| 206 | } |
| 207 | if (!TWFunc::Path_Exists(folder_path)) { |
| 208 | ui_print("Unable to locate backup '%s'\n", folder_path); |
| 209 | ret_val = 1; |
| 210 | continue; |
| 211 | } |
| 212 | DataManager_SetStrValue("tw_restore", folder_path); |
| 213 | |
| 214 | PartitionManager.Set_Restore_Files(folder_path); |
| 215 | if (strlen(partitions) != 0) { |
| 216 | int tw_restore_system = 0; |
| 217 | int tw_restore_data = 0; |
| 218 | int tw_restore_cache = 0; |
| 219 | int tw_restore_recovery = 0; |
| 220 | int tw_restore_boot = 0; |
| 221 | int tw_restore_andsec = 0; |
| 222 | int tw_restore_sdext = 0; |
| 223 | int tw_restore_sp1 = 0; |
| 224 | int tw_restore_sp2 = 0; |
| 225 | int tw_restore_sp3 = 0; |
| 226 | |
| 227 | memset(value2, 0, sizeof(value2)); |
| 228 | strcpy(value2, partitions); |
| 229 | ui_print("Setting restore options: '%s':\n", value2); |
| 230 | line_len = strlen(value2); |
| 231 | for (i=0; i<line_len; i++) { |
| 232 | if ((value2[i] == 'S' || value2[i] == 's') && DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) { |
| 233 | tw_restore_system = 1; |
| 234 | ui_print("System\n"); |
| 235 | } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) { |
| 236 | tw_restore_data = 1; |
| 237 | ui_print("Data\n"); |
| 238 | } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) { |
| 239 | tw_restore_cache = 1; |
| 240 | ui_print("Cache\n"); |
| 241 | } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) { |
| 242 | tw_restore_recovery = 1; |
| 243 | ui_print("Recovery\n"); |
| 244 | } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) { |
| 245 | tw_restore_sp1 = 1; |
| 246 | ui_print("%s\n", "Special1"); |
| 247 | } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) { |
| 248 | tw_restore_sp2 = 1; |
| 249 | ui_print("%s\n", "Special2"); |
| 250 | } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) { |
| 251 | tw_restore_sp3 = 1; |
| 252 | ui_print("%s\n", "Special3"); |
| 253 | } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) { |
| 254 | tw_restore_boot = 1; |
| 255 | ui_print("Boot\n"); |
| 256 | } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) { |
| 257 | tw_restore_andsec = 1; |
| 258 | ui_print("Android Secure\n"); |
| 259 | } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) { |
| 260 | tw_restore_sdext = 1; |
| 261 | ui_print("SD-Ext\n"); |
| 262 | } else if (value2[i] == 'M' || value2[i] == 'm') { |
| 263 | DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1); |
| 264 | ui_print("MD5 check skip is on\n"); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system) |
| 269 | DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0); |
| 270 | if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data) |
| 271 | DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0); |
| 272 | if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache) |
| 273 | DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0); |
| 274 | if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery) |
| 275 | DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0); |
| 276 | if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot) |
| 277 | DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0); |
| 278 | if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec) |
| 279 | DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0); |
| 280 | if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext) |
| 281 | DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0); |
| 282 | if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1) |
| 283 | DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0); |
| 284 | if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2) |
| 285 | DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0); |
| 286 | if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3) |
| 287 | DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0); |
| 288 | } else { |
| 289 | ui_print("No restore options set.\n"); |
| 290 | ret_val = 1; |
| 291 | continue; |
| 292 | } |
| 293 | PartitionManager.Run_Restore(folder_path); |
| 294 | ui_print("Restore complete!\n"); |
| 295 | } else if (strcmp(command, "mount") == 0) { |
| 296 | // Mount |
| 297 | if (value[0] != '/') { |
| 298 | strcpy(mount, "/"); |
| 299 | strcat(mount, value); |
| 300 | } else |
| 301 | strcpy(mount, value); |
| 302 | if (PartitionManager.Mount_By_Path(mount, true)) |
| 303 | ui_print("Mounted '%s'\n", mount); |
| 304 | } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { |
| 305 | // Unmount |
| 306 | if (value[0] != '/') { |
| 307 | strcpy(mount, "/"); |
| 308 | strcat(mount, value); |
| 309 | } else |
| 310 | strcpy(mount, value); |
| 311 | if (PartitionManager.UnMount_By_Path(mount, true)) |
| 312 | ui_print("Unmounted '%s'\n", mount); |
| 313 | } else if (strcmp(command, "set") == 0) { |
| 314 | // Set value |
| 315 | tok = strtok(value, " "); |
| 316 | strcpy(value1, tok); |
| 317 | tok = strtok(NULL, " "); |
| 318 | strcpy(value2, tok); |
| 319 | ui_print("Setting '%s' to '%s'\n", value1, value2); |
| 320 | DataManager_SetStrValue(value1, value2); |
| 321 | } else if (strcmp(command, "mkdir") == 0) { |
| 322 | // Make directory (recursive) |
| 323 | ui_print("Making directory (recursive): '%s'\n", value); |
| 324 | if (TWFunc::Recursive_Mkdir(value)) { |
| 325 | LOGE("Unable to create folder: '%s'\n", value); |
| 326 | ret_val = 1; |
| 327 | } |
| 328 | } else if (strcmp(command, "reboot") == 0) { |
| 329 | // Reboot |
| 330 | } else if (strcmp(command, "cmd") == 0) { |
| 331 | if (cindex != 0) { |
| 332 | system(value); |
| 333 | } else { |
| 334 | LOGE("No value given for cmd\n"); |
| 335 | } |
| 336 | } else if (strcmp(command, "print") == 0) { |
| 337 | ui_print("%s\n", value); |
| 338 | } else { |
| 339 | LOGE("Unrecognized script command: '%s'\n", command); |
| 340 | ret_val = 1; |
| 341 | } |
| 342 | } |
| 343 | fclose(fp); |
| 344 | ui_print("Done processing script file\n"); |
| 345 | } else { |
| 346 | LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP); |
| 347 | return 1; |
| 348 | } |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 349 | if (install_cmd && DataManager_GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager_GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 350 | ui_print("Injecting TWRP into boot image...\n"); |
| 351 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 352 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
| 353 | system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 354 | else { |
| 355 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
| 356 | system(injectcmd.c_str()); |
| 357 | } |
| 358 | ui_print("TWRP injection complete.\n"); |
| 359 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 360 | return ret_val; |
| 361 | } |
| 362 | |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 363 | int OpenRecoveryScript::Install_Command(string Zip) { |
| 364 | // Install zip |
| 365 | string ret_string; |
| 366 | int ret_val = 0, wipe_cache = 0; |
| 367 | |
| 368 | PartitionManager.Mount_All_Storage(); |
| 369 | if (Zip.substr(0, 1) != "/") { |
| 370 | // Relative path given |
| 371 | string Full_Path; |
| 372 | |
| 373 | Full_Path = DataManager_GetCurrentStoragePath(); |
| 374 | Full_Path += "/" + Zip; |
| 375 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
| 376 | if (!TWFunc::Path_Exists(Full_Path)) { |
| 377 | ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); |
| 378 | if (!ret_string.empty()) { |
| 379 | Full_Path = ret_string; |
| 380 | } else if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 381 | if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
| 382 | LOGI("Zip file not found on external storage, trying internal...\n"); |
| 383 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 384 | } else { |
| 385 | LOGI("Zip file not found on internal storage, trying external...\n"); |
| 386 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 387 | } |
| 388 | Full_Path = DataManager_GetCurrentStoragePath(); |
| 389 | Full_Path += "/" + Zip; |
| 390 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
| 391 | ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); |
| 392 | if (!ret_string.empty()) |
| 393 | Full_Path = ret_string; |
| 394 | } |
| 395 | } |
| 396 | Zip = Full_Path; |
| 397 | } else { |
| 398 | // Full path given |
| 399 | if (!TWFunc::Path_Exists(Zip)) { |
| 400 | ret_string = Locate_Zip_File(Zip, DataManager_GetCurrentStoragePath()); |
| 401 | if (!ret_string.empty()) |
| 402 | Zip = ret_string; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (!TWFunc::Path_Exists(Zip)) { |
| 407 | // zip file doesn't exist |
| 408 | ui_print("Unable to locate zip file '%s'.\n", Zip.c_str()); |
| 409 | ret_val = 1; |
| 410 | } else { |
| 411 | ui_print("Installing zip file '%s'\n", Zip.c_str()); |
| 412 | ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache); |
| 413 | } |
| 414 | if (ret_val != 0) { |
| 415 | LOGE("Error installing zip file '%s'\n", Zip.c_str()); |
| 416 | ret_val = 1; |
| 417 | } else if (wipe_cache) |
| 418 | PartitionManager.Wipe_By_Path("/cache"); |
| 419 | |
| 420 | return ret_val; |
| 421 | } |
| 422 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 423 | string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) { |
| 424 | string Path = TWFunc::Get_Path(Zip); |
| 425 | string File = TWFunc::Get_Filename(Zip); |
| 426 | string pathCpy = Path; |
| 427 | string wholePath; |
| 428 | size_t pos = Path.find("/", 1); |
| 429 | |
| 430 | while (pos != string::npos) |
| 431 | { |
| 432 | pathCpy = Path.substr(pos, Path.size() - pos); |
| 433 | wholePath = pathCpy + "/" + File; |
| 434 | if (TWFunc::Path_Exists(wholePath)) |
| 435 | return wholePath; |
| 436 | wholePath = Storage_Root + "/" + wholePath; |
| 437 | if (TWFunc::Path_Exists(wholePath)) |
| 438 | return wholePath; |
| 439 | |
| 440 | pos = Path.find("/", pos + 1); |
| 441 | } |
| 442 | return ""; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int OpenRecoveryScript::Backup_Command(string Options) { |
| 446 | char value1[SCRIPT_COMMAND_SIZE]; |
| 447 | int line_len, i; |
| 448 | |
| 449 | strcpy(value1, Options.c_str()); |
| 450 | |
| 451 | DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0); |
| 452 | DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0); |
| 453 | DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0); |
| 454 | DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 455 | DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0); |
| 456 | DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0); |
| 457 | DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0); |
| 458 | DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0); |
| 459 | DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0); |
| 460 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); |
| 461 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); |
| 462 | DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0); |
| 463 | DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0); |
| 464 | |
| 465 | ui_print("Setting backup options:\n"); |
| 466 | line_len = Options.size(); |
| 467 | for (i=0; i<line_len; i++) { |
| 468 | if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") { |
| 469 | DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 1); |
| 470 | ui_print("System\n"); |
| 471 | } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") { |
| 472 | DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 1); |
| 473 | ui_print("Data\n"); |
| 474 | } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") { |
| 475 | DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 1); |
| 476 | ui_print("Cache\n"); |
| 477 | } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") { |
| 478 | DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 1); |
| 479 | ui_print("Recovery\n"); |
| 480 | } else if (Options.substr(i, 1) == "1") { |
| 481 | DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 1); |
| 482 | ui_print("%s\n", "Special1"); |
| 483 | } else if (Options.substr(i, 1) == "2") { |
| 484 | DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 1); |
| 485 | ui_print("%s\n", "Special2"); |
| 486 | } else if (Options.substr(i, 1) == "3") { |
| 487 | DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 1); |
| 488 | ui_print("%s\n", "Special3"); |
| 489 | } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") { |
| 490 | DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 1); |
| 491 | ui_print("Boot\n"); |
| 492 | } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") { |
| 493 | DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 1); |
| 494 | ui_print("Android Secure\n"); |
| 495 | } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") { |
| 496 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 1); |
| 497 | ui_print("SD-Ext\n"); |
| 498 | } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") { |
| 499 | DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 1); |
| 500 | ui_print("Compression is on\n"); |
| 501 | } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") { |
| 502 | DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 1); |
| 503 | ui_print("MD5 Generation is off\n"); |
| 504 | } |
| 505 | } |
| 506 | if (!PartitionManager.Run_Backup()) { |
| 507 | LOGE("Backup failed!\n"); |
| 508 | return 1; |
| 509 | } |
| 510 | ui_print("Backup complete!\n"); |
| 511 | return 0; |
| 512 | } |