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> |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 33 | #include <iostream> |
| 34 | #include <fstream> |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 35 | |
| 36 | #include "twrp-functions.hpp" |
| 37 | #include "partitions.hpp" |
| 38 | #include "common.h" |
| 39 | #include "openrecoveryscript.hpp" |
| 40 | #include "variables.h" |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 41 | #include "adb_install.h" |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 42 | #include "data.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 43 | extern "C" { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 44 | #include "twinstall.h" |
| 45 | #include "gui/gui.h" |
| 46 | int TWinstall_zip(const char* path, int* wipe_cache); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 49 | extern RecoveryUI* ui; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 50 | #define SCRIPT_COMMAND_SIZE 512 |
| 51 | |
| 52 | int OpenRecoveryScript::check_for_script_file(void) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 53 | if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { |
| 54 | LOGE("Unable to mount /cache for OpenRecoveryScript support.\n"); |
| 55 | return 0; |
| 56 | } |
| 57 | if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { |
| 58 | LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE); |
| 59 | // Copy script file to /tmp |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 60 | TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 61 | // Delete the file from /cache |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 62 | unlink(SCRIPT_FILE_CACHE); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int OpenRecoveryScript::run_script_file(void) { |
| 69 | FILE *fp = fopen(SCRIPT_FILE_TMP, "r"); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 70 | int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 71 | char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE], |
| 72 | value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE], |
| 73 | value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE]; |
| 74 | char *val_start, *tok; |
| 75 | |
| 76 | if (fp != NULL) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 77 | DataManager::SetValue(TW_SIMULATE_ACTIONS, 0); |
Dees_Troy | 6bdcbb1 | 2013-01-26 14:07:43 +0000 | [diff] [blame] | 78 | DataManager::SetValue("ui_progress", 0); // Reset the progress bar |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 79 | while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) { |
| 80 | cindex = 0; |
| 81 | line_len = strlen(script_line); |
| 82 | if (line_len < 2) |
| 83 | continue; // there's a blank line or line is too short to contain a command |
| 84 | //ui_print("script line: '%s'\n", script_line); |
| 85 | for (i=0; i<line_len; i++) { |
| 86 | if ((int)script_line[i] == 32) { |
| 87 | cindex = i; |
| 88 | i = line_len; |
| 89 | } |
| 90 | } |
| 91 | memset(command, 0, sizeof(command)); |
| 92 | memset(value, 0, sizeof(value)); |
| 93 | if ((int)script_line[line_len - 1] == 10) |
| 94 | remove_nl = 2; |
| 95 | else |
| 96 | remove_nl = 1; |
| 97 | if (cindex != 0) { |
| 98 | strncpy(command, script_line, cindex); |
| 99 | LOGI("command is: '%s' and ", command); |
| 100 | val_start = script_line; |
| 101 | val_start += cindex + 1; |
| 102 | strncpy(value, val_start, line_len - cindex - remove_nl); |
| 103 | LOGI("value is: '%s'\n", value); |
| 104 | } else { |
| 105 | strncpy(command, script_line, line_len - remove_nl + 1); |
| 106 | ui_print("command is: '%s' and there is no value\n", command); |
| 107 | } |
| 108 | if (strcmp(command, "install") == 0) { |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 109 | // Install Zip |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 110 | DataManager::SetValue("tw_action_text2", "Installing Zip"); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 111 | PartitionManager.Mount_All_Storage(); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 112 | ret_val = Install_Command(value); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 113 | install_cmd = -1; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 114 | } else if (strcmp(command, "wipe") == 0) { |
| 115 | // Wipe |
| 116 | if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) { |
| 117 | ui_print("-- Wiping Cache Partition...\n"); |
| 118 | PartitionManager.Wipe_By_Path("/cache"); |
| 119 | ui_print("-- Cache Partition Wipe Complete!\n"); |
| 120 | } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) { |
| 121 | ui_print("-- Wiping Dalvik Cache...\n"); |
| 122 | PartitionManager.Wipe_Dalvik_Cache(); |
| 123 | ui_print("-- Dalvik Cache Wipe Complete!\n"); |
| 124 | } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) { |
| 125 | ui_print("-- Wiping Data Partition...\n"); |
| 126 | PartitionManager.Factory_Reset(); |
| 127 | ui_print("-- Data Partition Wipe Complete!\n"); |
| 128 | } else { |
| 129 | LOGE("Error with wipe command value: '%s'\n", value); |
| 130 | ret_val = 1; |
| 131 | } |
| 132 | } else if (strcmp(command, "backup") == 0) { |
| 133 | // Backup |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 134 | DataManager::SetValue("tw_action_text2", "Backing Up"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 135 | tok = strtok(value, " "); |
| 136 | strcpy(value1, tok); |
| 137 | tok = strtok(NULL, " "); |
| 138 | if (tok != NULL) { |
| 139 | memset(value2, 0, sizeof(value2)); |
| 140 | strcpy(value2, tok); |
| 141 | line_len = strlen(tok); |
| 142 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) { |
| 143 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) |
| 144 | remove_nl = 2; |
| 145 | else |
| 146 | remove_nl = 1; |
| 147 | } else |
| 148 | remove_nl = 0; |
| 149 | strncpy(value2, tok, line_len - remove_nl); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 150 | DataManager::SetValue(TW_BACKUP_NAME, value2); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 151 | ui_print("Backup folder set to '%s'\n", value2); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 152 | if (PartitionManager.Check_Backup_Name(true) != 0) { |
| 153 | ret_val = 1; |
| 154 | continue; |
| 155 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 156 | } else { |
| 157 | char empt[50]; |
| 158 | strcpy(empt, "(Current Date)"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 159 | DataManager::SetValue(TW_BACKUP_NAME, empt); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 160 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 161 | ret_val = Backup_Command(value1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 162 | } else if (strcmp(command, "restore") == 0) { |
| 163 | // Restore |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 164 | DataManager::SetValue("tw_action_text2", "Restoring"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 165 | PartitionManager.Mount_All_Storage(); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 166 | DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 167 | char folder_path[512], partitions[512]; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 168 | |
| 169 | string val = value, restore_folder, restore_partitions; |
| 170 | size_t pos = val.find_last_of(" "); |
| 171 | if (pos == string::npos) { |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 172 | restore_folder = value; |
| 173 | partitions[0] = '\0'; |
| 174 | } else { |
| 175 | restore_folder = val.substr(0, pos); |
| 176 | restore_partitions = val.substr(pos + 1, val.size() - pos - 1); |
| 177 | strcpy(partitions, restore_partitions.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 178 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 179 | strcpy(folder_path, restore_folder.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 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]; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 185 | string folder_var; |
| 186 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var); |
| 187 | sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 188 | LOGI("Restoring relative path: '%s'\n", backup_folder); |
| 189 | if (!TWFunc::Path_Exists(backup_folder)) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 190 | if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 191 | if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 192 | LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 193 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 194 | } else { |
| 195 | LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 196 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 197 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 198 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var); |
| 199 | sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 200 | LOGI("2Restoring relative path: '%s'\n", backup_folder); |
| 201 | } |
| 202 | } |
| 203 | strcpy(folder_path, backup_folder); |
| 204 | } else { |
| 205 | if (folder_path[strlen(folder_path) - 1] == '/') |
| 206 | strcat(folder_path, "."); |
| 207 | else |
| 208 | strcat(folder_path, "/."); |
| 209 | } |
| 210 | if (!TWFunc::Path_Exists(folder_path)) { |
| 211 | ui_print("Unable to locate backup '%s'\n", folder_path); |
| 212 | ret_val = 1; |
| 213 | continue; |
| 214 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 215 | DataManager::SetValue("tw_restore", folder_path); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 216 | |
| 217 | PartitionManager.Set_Restore_Files(folder_path); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 218 | string Partition_List; |
| 219 | DataManager::GetValue("tw_restore_list", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 220 | if (strlen(partitions) != 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 221 | string Restore_List; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 222 | |
| 223 | memset(value2, 0, sizeof(value2)); |
| 224 | strcpy(value2, partitions); |
| 225 | ui_print("Setting restore options: '%s':\n", value2); |
| 226 | line_len = strlen(value2); |
| 227 | for (i=0; i<line_len; i++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 228 | if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) { |
| 229 | Restore_List += "/system;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 230 | ui_print("System\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 231 | } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) { |
| 232 | Restore_List += "/data;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 233 | ui_print("Data\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 234 | } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) { |
| 235 | Restore_List += "/cache;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 236 | ui_print("Cache\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 237 | } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) { |
| 238 | ui_print("Recovery -- Not allowed to restore recovery\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 239 | } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 240 | ui_print("%s\n", "Special1 -- No Longer Supported..."); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 241 | } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 242 | ui_print("%s\n", "Special2 -- No Longer Supported..."); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 243 | } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 244 | ui_print("%s\n", "Special3 -- No Longer Supported..."); |
| 245 | } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) { |
| 246 | Restore_List += "/boot;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 247 | ui_print("Boot\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 248 | } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) { |
| 249 | Restore_List += "/and-sec;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 250 | ui_print("Android Secure\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 251 | } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) { |
| 252 | Restore_List += "/sd-ext;"; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 253 | ui_print("SD-Ext\n"); |
| 254 | } else if (value2[i] == 'M' || value2[i] == 'm') { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 255 | DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 256 | ui_print("MD5 check skip is on\n"); |
| 257 | } |
| 258 | } |
| 259 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 260 | DataManager::SetValue("tw_restore_selected", Restore_List); |
| 261 | } else { |
| 262 | DataManager::SetValue("tw_restore_selected", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 263 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 264 | if (!PartitionManager.Run_Restore(folder_path)) |
| 265 | ret_val = 1; |
| 266 | else |
| 267 | ui_print("Restore complete!\n"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 268 | } else if (strcmp(command, "mount") == 0) { |
| 269 | // Mount |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 270 | DataManager::SetValue("tw_action_text2", "Mounting"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 271 | if (value[0] != '/') { |
| 272 | strcpy(mount, "/"); |
| 273 | strcat(mount, value); |
| 274 | } else |
| 275 | strcpy(mount, value); |
| 276 | if (PartitionManager.Mount_By_Path(mount, true)) |
| 277 | ui_print("Mounted '%s'\n", mount); |
| 278 | } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { |
| 279 | // Unmount |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 280 | DataManager::SetValue("tw_action_text2", "Unmounting"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 281 | if (value[0] != '/') { |
| 282 | strcpy(mount, "/"); |
| 283 | strcat(mount, value); |
| 284 | } else |
| 285 | strcpy(mount, value); |
| 286 | if (PartitionManager.UnMount_By_Path(mount, true)) |
| 287 | ui_print("Unmounted '%s'\n", mount); |
| 288 | } else if (strcmp(command, "set") == 0) { |
| 289 | // Set value |
| 290 | tok = strtok(value, " "); |
| 291 | strcpy(value1, tok); |
| 292 | tok = strtok(NULL, " "); |
| 293 | strcpy(value2, tok); |
| 294 | ui_print("Setting '%s' to '%s'\n", value1, value2); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 295 | DataManager::SetValue(value1, value2); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 296 | } else if (strcmp(command, "mkdir") == 0) { |
| 297 | // Make directory (recursive) |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 298 | DataManager::SetValue("tw_action_text2", "Making Directory"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 299 | ui_print("Making directory (recursive): '%s'\n", value); |
| 300 | if (TWFunc::Recursive_Mkdir(value)) { |
| 301 | LOGE("Unable to create folder: '%s'\n", value); |
| 302 | ret_val = 1; |
| 303 | } |
| 304 | } else if (strcmp(command, "reboot") == 0) { |
| 305 | // Reboot |
| 306 | } else if (strcmp(command, "cmd") == 0) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 307 | DataManager::SetValue("tw_action_text2", "Running Command"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 308 | if (cindex != 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 309 | string status; |
| 310 | TWFunc::Exec_Cmd(value, status); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 311 | } else { |
| 312 | LOGE("No value given for cmd\n"); |
| 313 | } |
| 314 | } else if (strcmp(command, "print") == 0) { |
| 315 | ui_print("%s\n", value); |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 316 | } else if (strcmp(command, "sideload") == 0) { |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 317 | // ADB Sideload |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 318 | DataManager::SetValue("tw_action_text2", "ADB Sideload"); |
| 319 | install_cmd = -1; |
| 320 | |
| 321 | int wipe_cache = 0; |
| 322 | string result, Sideload_File; |
| 323 | |
| 324 | if (!PartitionManager.Mount_Current_Storage(true)) { |
| 325 | ret_val = 1; // failure |
| 326 | } else { |
| 327 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
| 328 | if (TWFunc::Path_Exists(Sideload_File)) { |
| 329 | unlink(Sideload_File.c_str()); |
| 330 | } |
| 331 | ui_print("Starting ADB sideload feature...\n"); |
| 332 | DataManager::SetValue("tw_has_cancel", 1); |
| 333 | DataManager::SetValue("tw_cancel_action", "adbsideloadcancel"); |
| 334 | ret_val = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str()); |
| 335 | DataManager::SetValue("tw_has_cancel", 0); |
| 336 | if (ret_val != 0) |
| 337 | ret_val = 1; // failure |
| 338 | else if (wipe_cache) |
| 339 | PartitionManager.Wipe_By_Path("/cache"); |
| 340 | sideload = 1; // Causes device to go to the home screen afterwards |
| 341 | ui_print("Sideload finished.\n"); |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 342 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 343 | } else { |
| 344 | LOGE("Unrecognized script command: '%s'\n", command); |
| 345 | ret_val = 1; |
| 346 | } |
| 347 | } |
| 348 | fclose(fp); |
| 349 | ui_print("Done processing script file\n"); |
| 350 | } else { |
| 351 | LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP); |
| 352 | return 1; |
| 353 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 354 | 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] | 355 | string status; |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 356 | ui_print("Injecting TWRP into boot image...\n"); |
| 357 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 358 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 359 | 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] | 360 | else { |
| 361 | 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] | 362 | TWFunc::Exec_Cmd(injectcmd.c_str(), status); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 363 | } |
| 364 | ui_print("TWRP injection complete.\n"); |
| 365 | } |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 366 | if (sideload) |
| 367 | ret_val = 1; // Forces booting to the home page after sideload |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 368 | return ret_val; |
| 369 | } |
| 370 | |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 371 | int OpenRecoveryScript::Insert_ORS_Command(string Command) { |
| 372 | ofstream ORSfile(SCRIPT_FILE_TMP); |
| 373 | if (ORSfile.is_open()) { |
| 374 | //if (Command.substr(Command.size() - 1, 1) != "\n") |
| 375 | // Command += "\n"; |
| 376 | LOGI("Inserting '%s'\n", Command.c_str()); |
| 377 | ORSfile << Command.c_str(); |
| 378 | ORSfile.close(); |
| 379 | return 1; |
| 380 | } |
| 381 | LOGE("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP); |
| 382 | return 0; |
| 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 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 395 | Full_Path = DataManager::GetCurrentStoragePath(); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 396 | Full_Path += "/" + Zip; |
| 397 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
| 398 | if (!TWFunc::Path_Exists(Full_Path)) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 399 | ret_string = Locate_Zip_File(Full_Path, DataManager::GetCurrentStoragePath()); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 400 | if (!ret_string.empty()) { |
| 401 | Full_Path = ret_string; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 402 | } else if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 403 | if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 404 | LOGI("Zip file not found on external storage, trying internal...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 405 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 406 | } else { |
| 407 | LOGI("Zip file not found on internal storage, trying external...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 408 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 409 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 410 | Full_Path = DataManager::GetCurrentStoragePath(); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 411 | Full_Path += "/" + Zip; |
| 412 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 413 | ret_string = Locate_Zip_File(Full_Path, DataManager::GetCurrentStoragePath()); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 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)) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 422 | ret_string = Locate_Zip_File(Zip, DataManager::GetCurrentStoragePath()); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 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; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 470 | string Backup_List; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 471 | |
| 472 | strcpy(value1, Options.c_str()); |
| 473 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 474 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0); |
| 475 | DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 476 | |
| 477 | ui_print("Setting backup options:\n"); |
| 478 | line_len = Options.size(); |
| 479 | for (i=0; i<line_len; i++) { |
| 480 | if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 481 | Backup_List += "/system;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 482 | ui_print("System\n"); |
| 483 | } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 484 | Backup_List += "/data;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 485 | ui_print("Data\n"); |
| 486 | } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 487 | Backup_List += "/cache;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 488 | ui_print("Cache\n"); |
| 489 | } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 490 | Backup_List += "/recovery;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 491 | ui_print("Recovery\n"); |
| 492 | } else if (Options.substr(i, 1) == "1") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 493 | ui_print("%s\n", "Special1 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 494 | } else if (Options.substr(i, 1) == "2") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 495 | ui_print("%s\n", "Special2 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 496 | } else if (Options.substr(i, 1) == "3") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 497 | ui_print("%s\n", "Special3 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 498 | } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 499 | Backup_List += "/boot;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 500 | ui_print("Boot\n"); |
| 501 | } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 502 | Backup_List += "/and-sec;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 503 | ui_print("Android Secure\n"); |
| 504 | } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 505 | Backup_List += "/sd-ext;"; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 506 | ui_print("SD-Ext\n"); |
| 507 | } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 508 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 509 | ui_print("Compression is on\n"); |
| 510 | } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 511 | DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 512 | ui_print("MD5 Generation is off\n"); |
| 513 | } |
| 514 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame^] | 515 | DataManager::SetValue("tw_backup_list", Backup_List); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 516 | if (!PartitionManager.Run_Backup()) { |
| 517 | LOGE("Backup failed!\n"); |
| 518 | return 1; |
| 519 | } |
| 520 | ui_print("Backup complete!\n"); |
| 521 | return 0; |
| 522 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 523 | |
| 524 | void OpenRecoveryScript::Run_OpenRecoveryScript(void) { |
| 525 | DataManager::SetValue("tw_back", "main"); |
| 526 | DataManager::SetValue("tw_action", "openrecoveryscript"); |
| 527 | DataManager::SetValue("tw_has_action2", "0"); |
| 528 | DataManager::SetValue("tw_action2", ""); |
| 529 | DataManager::SetValue("tw_action2_param", ""); |
| 530 | DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript"); |
| 531 | DataManager::SetValue("tw_action_text2", ""); |
| 532 | DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete"); |
| 533 | DataManager::SetValue("tw_has_cancel", 0); |
| 534 | DataManager::SetValue("tw_show_reboot", 0); |
| 535 | if (gui_startPage("action_page") != 0) { |
| 536 | LOGE("Failed to load OpenRecoveryScript GUI page.\n"); |
| 537 | } |
Dees_Troy | 6bdcbb1 | 2013-01-26 14:07:43 +0000 | [diff] [blame] | 538 | } |