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