Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 2 | Copyright 2003 to 2017 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 18 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 19 | #define __STDC_FORMAT_MACROS 1 |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/vfs.h> |
| 25 | #include <unistd.h> |
| 26 | #include <vector> |
| 27 | #include <dirent.h> |
| 28 | #include <time.h> |
| 29 | #include <errno.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 30 | #include <inttypes.h> |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <fstream> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | #include <iterator> |
| 36 | #include <algorithm> |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <sys/wait.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 39 | #include <zlib.h> |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 40 | |
| 41 | #include "twrp-functions.hpp" |
| 42 | #include "partitions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 43 | #include "twcommon.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 44 | #include "openrecoveryscript.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 45 | #include "progresstracking.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 46 | #include "variables.h" |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 47 | #include "adb_install.h" |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 48 | #include "data.hpp" |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 49 | #include "adb_install.h" |
| 50 | #include "fuse_sideload.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 51 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 52 | #include "gui/pages.hpp" |
| 53 | #include "orscmd/orscmd.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 54 | #include "twinstall.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 55 | extern "C" { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 56 | #include "gui/gui.h" |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 57 | #include "cutils/properties.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 58 | } |
| 59 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 60 | OpenRecoveryScript::VoidFunction OpenRecoveryScript::call_after_cli_command; |
| 61 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 62 | #define SCRIPT_COMMAND_SIZE 512 |
| 63 | |
| 64 | int OpenRecoveryScript::check_for_script_file(void) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 65 | std::string orsFile = TWFunc::get_cache_dir() + "/recovery/openrecoveryscript"; |
| 66 | if (!PartitionManager.Mount_By_Path(orsFile, false)) { |
| 67 | LOGINFO("Unable to mount %s for OpenRecoveryScript support.\n", TWFunc::get_cache_dir().c_str()); |
| 68 | gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(TWFunc::get_cache_dir())); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 69 | return 0; |
| 70 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 71 | if (TWFunc::Path_Exists(orsFile)) { |
| 72 | LOGINFO("Script file found: '%s'\n", orsFile.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 73 | // Copy script file to /tmp |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 74 | TWFunc::copy_file(orsFile, SCRIPT_FILE_TMP, 0755); |
| 75 | // Delete the file from cache |
| 76 | unlink(orsFile.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 77 | return 1; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 82 | int OpenRecoveryScript::copy_script_file(string filename) { |
| 83 | if (TWFunc::Path_Exists(filename)) { |
| 84 | LOGINFO("Script file found: '%s'\n", filename.c_str()); |
| 85 | if (filename == SCRIPT_FILE_TMP) |
| 86 | return 1; // file is already in the right place |
| 87 | // Copy script file to /tmp |
| 88 | TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755); |
| 89 | // Delete the old file |
| 90 | unlink(filename.c_str()); |
| 91 | return 1; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 96 | int OpenRecoveryScript::run_script_file(void) { |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 97 | 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] | 98 | char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE], |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 99 | value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE], |
| 100 | value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE]; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 101 | char *val_start, *tok; |
| 102 | |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 103 | FILE *fp = fopen(SCRIPT_FILE_TMP, "r"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 104 | if (fp != NULL) { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 105 | DataManager::SetValue(TW_SIMULATE_ACTIONS, 0); |
Dees_Troy | 6bdcbb1 | 2013-01-26 14:07:43 +0000 | [diff] [blame] | 106 | DataManager::SetValue("ui_progress", 0); // Reset the progress bar |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 107 | while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) { |
| 108 | cindex = 0; |
| 109 | line_len = strlen(script_line); |
| 110 | if (line_len < 2) |
| 111 | continue; // there's a blank line or line is too short to contain a command |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 112 | //gui_print("script line: '%s'\n", script_line); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 113 | for (i=0; i<line_len; i++) { |
| 114 | if ((int)script_line[i] == 32) { |
| 115 | cindex = i; |
| 116 | i = line_len; |
| 117 | } |
| 118 | } |
| 119 | memset(command, 0, sizeof(command)); |
| 120 | memset(value, 0, sizeof(value)); |
| 121 | if ((int)script_line[line_len - 1] == 10) |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 122 | remove_nl = 2; |
| 123 | else |
| 124 | remove_nl = 1; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 125 | if (cindex != 0) { |
| 126 | strncpy(command, script_line, cindex); |
Matt Mower | e96c3c3 | 2014-09-19 12:14:37 -0500 | [diff] [blame] | 127 | LOGINFO("command is: '%s'\n", command); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 128 | val_start = script_line; |
| 129 | val_start += cindex + 1; |
bigbiff bigbiff | c03121d | 2013-09-09 19:14:35 -0400 | [diff] [blame] | 130 | if ((int) *val_start == 32) |
| 131 | val_start++; //get rid of space |
bigbiff bigbiff | 72e9554 | 2013-08-29 21:01:02 -0400 | [diff] [blame] | 132 | if ((int) *val_start == 51) |
| 133 | val_start++; //get rid of = at the beginning |
bigbiff bigbiff | a6c5f4e | 2013-09-17 20:01:14 -0400 | [diff] [blame] | 134 | if ((int) *val_start == 32) |
| 135 | val_start++; //get rid of space |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 136 | strncpy(value, val_start, line_len - cindex - remove_nl); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 137 | LOGINFO("value is: '%s'\n", value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 138 | } else { |
| 139 | strncpy(command, script_line, line_len - remove_nl + 1); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 140 | gui_print("command is: '%s' and there is no value\n", command); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 141 | } |
| 142 | if (strcmp(command, "install") == 0) { |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 143 | // Install Zip |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 144 | DataManager::SetValue("tw_action_text2", "Installing Zip"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 145 | ret_val = Install_Command(value); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 146 | install_cmd = -1; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 147 | } else if (strcmp(command, "wipe") == 0) { |
| 148 | // Wipe |
| 149 | if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 150 | PartitionManager.Wipe_By_Path("/cache"); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 151 | } else if (strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0 || strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0) { |
| 152 | PartitionManager.Wipe_By_Path(PartitionManager.Get_Android_Root_Path()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 153 | } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 154 | PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 155 | } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 156 | PartitionManager.Factory_Reset(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 157 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 158 | LOGERR("Error with wipe command value: '%s'\n", value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 159 | ret_val = 1; |
| 160 | } |
| 161 | } else if (strcmp(command, "backup") == 0) { |
| 162 | // Backup |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 163 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 164 | tok = strtok(value, " "); |
| 165 | strcpy(value1, tok); |
| 166 | tok = strtok(NULL, " "); |
| 167 | if (tok != NULL) { |
| 168 | memset(value2, 0, sizeof(value2)); |
| 169 | strcpy(value2, tok); |
| 170 | line_len = strlen(tok); |
| 171 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) { |
| 172 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) |
| 173 | remove_nl = 2; |
| 174 | else |
| 175 | remove_nl = 1; |
| 176 | } else |
| 177 | remove_nl = 0; |
| 178 | strncpy(value2, tok, line_len - remove_nl); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 179 | DataManager::SetValue(TW_BACKUP_NAME, value2); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 180 | gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2)); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 181 | if (PartitionManager.Check_Backup_Name(true) != 0) { |
| 182 | ret_val = 1; |
| 183 | continue; |
| 184 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 185 | } else { |
| 186 | char empt[50]; |
| 187 | strcpy(empt, "(Current Date)"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 188 | DataManager::SetValue(TW_BACKUP_NAME, empt); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 189 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 190 | ret_val = Backup_Command(value1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 191 | } else if (strcmp(command, "restore") == 0) { |
| 192 | // Restore |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 193 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 194 | PartitionManager.Mount_All_Storage(); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 195 | DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 196 | char folder_path[512], partitions[512]; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 197 | |
| 198 | string val = value, restore_folder, restore_partitions; |
| 199 | size_t pos = val.find_last_of(" "); |
| 200 | if (pos == string::npos) { |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 201 | restore_folder = value; |
| 202 | partitions[0] = '\0'; |
| 203 | } else { |
| 204 | restore_folder = val.substr(0, pos); |
| 205 | restore_partitions = val.substr(pos + 1, val.size() - pos - 1); |
| 206 | strcpy(partitions, restore_partitions.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 207 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 208 | strcpy(folder_path, restore_folder.c_str()); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 209 | LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 210 | gui_msg(Msg("restoring=Restoring {1}...")(folder_path)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 211 | |
| 212 | if (folder_path[0] != '/') { |
| 213 | char backup_folder[512]; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 214 | string folder_var; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 215 | std::vector<PartitionList> Storage_List; |
| 216 | |
| 217 | PartitionManager.Get_Partition_List("storage", &Storage_List); |
| 218 | int listSize = Storage_List.size(); |
| 219 | for (int i = 0; i < listSize; i++) { |
| 220 | if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) { |
| 221 | DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 222 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var); |
| 223 | sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path); |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 224 | if (TWFunc::Path_Exists(backup_folder)) { |
| 225 | strcpy(folder_path, backup_folder); |
| 226 | break; |
| 227 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 230 | } else { |
| 231 | if (folder_path[strlen(folder_path) - 1] == '/') |
| 232 | strcat(folder_path, "."); |
| 233 | else |
| 234 | strcat(folder_path, "/."); |
| 235 | } |
| 236 | if (!TWFunc::Path_Exists(folder_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 237 | gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 238 | ret_val = 1; |
| 239 | continue; |
| 240 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 241 | DataManager::SetValue("tw_restore", folder_path); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 242 | |
| 243 | PartitionManager.Set_Restore_Files(folder_path); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 244 | string Partition_List; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 245 | int is_encrypted = 0; |
| 246 | DataManager::GetValue("tw_restore_encrypted", is_encrypted); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 247 | DataManager::GetValue("tw_restore_list", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 248 | if (strlen(partitions) != 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 249 | string Restore_List; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 250 | |
| 251 | memset(value2, 0, sizeof(value2)); |
| 252 | strcpy(value2, partitions); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 253 | gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 254 | line_len = strlen(value2); |
| 255 | for (i=0; i<line_len; i++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 256 | if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) { |
| 257 | Restore_List += "/system;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 258 | gui_msg("system=System"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 259 | } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) { |
| 260 | Restore_List += "/data;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 261 | gui_msg("data=Data"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 262 | } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) { |
| 263 | Restore_List += "/cache;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 264 | gui_msg("cache=Cache"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 265 | } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 266 | Restore_List += "/recovery;"; |
| 267 | gui_msg("recovery=Recovery"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 268 | } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) { |
| 269 | Restore_List += "/boot;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 270 | gui_msg("boot=Boot"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 271 | } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) { |
| 272 | Restore_List += "/and-sec;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 273 | gui_msg("android_secure=Android Secure"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 274 | } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) { |
| 275 | Restore_List += "/sd-ext;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 276 | gui_msg("sdext=SD-EXT"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 277 | } else if (value2[i] == 'M' || value2[i] == 'm') { |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 278 | DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 1); |
| 279 | gui_msg("digest_check_skip=Digest check skip is on"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 283 | DataManager::SetValue("tw_restore_selected", Restore_List); |
| 284 | } else { |
| 285 | DataManager::SetValue("tw_restore_selected", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 286 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 287 | if (is_encrypted) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 288 | gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup."); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 289 | ret_val = 1; |
| 290 | } else if (!PartitionManager.Run_Restore(folder_path)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 291 | ret_val = 1; |
| 292 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 293 | gui_msg("done=Done."); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 294 | } else if (strcmp(command, "remountrw") == 0) { |
| 295 | ret_val = remountrw(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 296 | } else if (strcmp(command, "mount") == 0) { |
| 297 | // Mount |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 298 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 299 | if (value[0] != '/') { |
| 300 | strcpy(mount, "/"); |
| 301 | strcat(mount, value); |
| 302 | } else |
| 303 | strcpy(mount, value); |
| 304 | if (PartitionManager.Mount_By_Path(mount, true)) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 305 | gui_msg(Msg("mounted=Mounted '{1}'")(mount)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 306 | } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { |
| 307 | // Unmount |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 308 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 309 | if (value[0] != '/') { |
| 310 | strcpy(mount, "/"); |
| 311 | strcat(mount, value); |
| 312 | } else |
| 313 | strcpy(mount, value); |
| 314 | if (PartitionManager.UnMount_By_Path(mount, true)) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 315 | gui_msg(Msg("unmounted=Unounted '{1}'")(mount)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 316 | } else if (strcmp(command, "set") == 0) { |
| 317 | // Set value |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 318 | size_t len = strlen(value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 319 | tok = strtok(value, " "); |
| 320 | strcpy(value1, tok); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 321 | if (len > strlen(value1) + 1) { |
| 322 | char *val2 = value + strlen(value1) + 1; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 323 | gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2)); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 324 | DataManager::SetValue(value1, val2); |
| 325 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 326 | gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1)); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 327 | DataManager::SetValue(value1, ""); |
| 328 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 329 | } else if (strcmp(command, "mkdir") == 0) { |
| 330 | // Make directory (recursive) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 331 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}")); |
| 332 | gui_msg(Msg("making_dir2=Making directory: '{1}'")(value)); |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 333 | if (!TWFunc::Recursive_Mkdir(value)) { |
| 334 | // error message already displayed by Recursive_Mkdir |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 335 | ret_val = 1; |
| 336 | } |
| 337 | } else if (strcmp(command, "reboot") == 0) { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 338 | if (strlen(value) && strcmp(value, "recovery") == 0) |
| 339 | TWFunc::tw_reboot(rb_recovery); |
| 340 | else if (strlen(value) && strcmp(value, "poweroff") == 0) |
| 341 | TWFunc::tw_reboot(rb_poweroff); |
| 342 | else if (strlen(value) && strcmp(value, "bootloader") == 0) |
| 343 | TWFunc::tw_reboot(rb_bootloader); |
| 344 | else if (strlen(value) && strcmp(value, "download") == 0) |
| 345 | TWFunc::tw_reboot(rb_download); |
| 346 | else |
| 347 | TWFunc::tw_reboot(rb_system); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 348 | } else if (strcmp(command, "cmd") == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 349 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 350 | if (cindex != 0) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 351 | TWFunc::Exec_Cmd(value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 352 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 353 | LOGERR("No value given for cmd\n"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 354 | } |
| 355 | } else if (strcmp(command, "print") == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 356 | gui_print("%s\n", value); |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 357 | } else if (strcmp(command, "sideload") == 0) { |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 358 | // ADB Sideload |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 359 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}")); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 360 | install_cmd = -1; |
| 361 | |
| 362 | int wipe_cache = 0; |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 363 | string result; |
| 364 | pid_t sideload_child_pid; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 365 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 366 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 367 | ret_val = apply_from_adb("/", &sideload_child_pid); |
| 368 | if (ret_val != 0) { |
| 369 | if (ret_val == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 370 | gui_err("need_new_adb=You need adb 1.0.32 or newer to sideload to this device."); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 371 | ret_val = 1; // failure |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 372 | } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 373 | if (wipe_cache) |
| 374 | PartitionManager.Wipe_By_Path("/cache"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 375 | } else { |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 376 | ret_val = 1; // failure |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 377 | } |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 378 | sideload = 1; // Causes device to go to the home screen afterwards |
| 379 | if (sideload_child_pid != 0) { |
| 380 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 381 | struct stat st; |
| 382 | // Calling stat() on this magic filename signals the minadbd |
| 383 | // subprocess to shut down. |
| 384 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 385 | int status; |
| 386 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 387 | waitpid(sideload_child_pid, &status, 0); |
| 388 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 389 | property_set("ctl.start", "adbd"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 390 | gui_msg("done=Done."); |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 391 | } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) { |
| 392 | ret_val = PartitionManager.Fix_Contexts(); |
Ethan Yonker | aae6e8c | 2014-02-11 21:37:36 -0600 | [diff] [blame] | 393 | if (ret_val != 0) |
| 394 | ret_val = 1; // failure |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 395 | } else if (strcmp(command, "decrypt") == 0) { |
| 396 | if (*value) { |
| 397 | ret_val = PartitionManager.Decrypt_Device(value); |
| 398 | if (ret_val != 0) |
| 399 | ret_val = 1; // failure |
| 400 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 401 | gui_err("no_pwd=No password provided."); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 402 | ret_val = 1; // failure |
| 403 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 404 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 405 | LOGERR("Unrecognized script command: '%s'\n", command); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 406 | ret_val = 1; |
| 407 | } |
| 408 | } |
| 409 | fclose(fp); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 410 | unlink(SCRIPT_FILE_TMP); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 411 | gui_msg("done_ors=Done processing script file"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 412 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 413 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(strerror(errno))); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 414 | return 1; |
| 415 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 416 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 417 | if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 418 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 419 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 420 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 421 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 422 | else { |
| 423 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 424 | TWFunc::Exec_Cmd(injectcmd.c_str()); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 425 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 426 | gui_msg("done=Done."); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 427 | } |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 428 | if (sideload) |
| 429 | ret_val = 1; // Forces booting to the home page after sideload |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 430 | return ret_val; |
| 431 | } |
| 432 | |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 433 | int OpenRecoveryScript::Insert_ORS_Command(string Command) { |
xiaolu | 38c3aa7 | 2015-05-26 22:55:18 +0800 | [diff] [blame] | 434 | ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out); |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 435 | if (ORSfile.is_open()) { |
| 436 | //if (Command.substr(Command.size() - 1, 1) != "\n") |
| 437 | // Command += "\n"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 438 | LOGINFO("Inserting '%s'\n", Command.c_str()); |
xiaolu | 38c3aa7 | 2015-05-26 22:55:18 +0800 | [diff] [blame] | 439 | ORSfile << Command.c_str() << endl; |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 440 | ORSfile.close(); |
| 441 | return 1; |
| 442 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 443 | LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP); |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 447 | int OpenRecoveryScript::Install_Command(string Zip) { |
| 448 | // Install zip |
| 449 | string ret_string; |
| 450 | int ret_val = 0, wipe_cache = 0; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 451 | std::vector<PartitionList> Storage_List; |
| 452 | string Full_Path; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 453 | |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 454 | if (Zip.substr(0, 1) == "@") { |
| 455 | // This is a special file that contains a map of blocks on the data partition |
| 456 | Full_Path = Zip.substr(1); |
| 457 | if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 458 | LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str()); |
| 459 | gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip)); |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 460 | return 1; |
| 461 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 462 | LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str()); |
| 463 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip)); |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 464 | } else if (!TWFunc::Path_Exists(Zip)) { |
| 465 | PartitionManager.Mount_All_Storage(); |
| 466 | PartitionManager.Get_Partition_List("storage", &Storage_List); |
| 467 | int listSize = Storage_List.size(); |
| 468 | for (int i = 0; i < listSize; i++) { |
| 469 | if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) { |
| 470 | Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip; |
| 471 | if (TWFunc::Path_Exists(Full_Path)) { |
| 472 | Zip = Full_Path; |
| 473 | break; |
| 474 | } |
| 475 | Full_Path = Zip; |
| 476 | LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str()); |
| 477 | ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point); |
| 478 | if (!ret_string.empty()) { |
| 479 | Zip = ret_string; |
| 480 | break; |
| 481 | } |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 482 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 483 | } |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 484 | if (!TWFunc::Path_Exists(Zip)) { |
| 485 | // zip file doesn't exist |
| 486 | gui_print("Unable to locate zip file '%s'.\n", Zip.c_str()); |
| 487 | ret_val = 1; |
| 488 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 489 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip)); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 492 | ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 493 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 494 | gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip)); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 495 | ret_val = 1; |
| 496 | } else if (wipe_cache) |
| 497 | PartitionManager.Wipe_By_Path("/cache"); |
| 498 | |
| 499 | return ret_val; |
| 500 | } |
| 501 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 502 | string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) { |
| 503 | string Path = TWFunc::Get_Path(Zip); |
| 504 | string File = TWFunc::Get_Filename(Zip); |
| 505 | string pathCpy = Path; |
| 506 | string wholePath; |
| 507 | size_t pos = Path.find("/", 1); |
| 508 | |
| 509 | while (pos != string::npos) |
| 510 | { |
| 511 | pathCpy = Path.substr(pos, Path.size() - pos); |
Matt Mower | e96c3c3 | 2014-09-19 12:14:37 -0500 | [diff] [blame] | 512 | wholePath = pathCpy + File; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 513 | LOGINFO("Looking for zip at '%s'\n", wholePath.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 514 | if (TWFunc::Path_Exists(wholePath)) |
| 515 | return wholePath; |
Matt Mower | e96c3c3 | 2014-09-19 12:14:37 -0500 | [diff] [blame] | 516 | wholePath = Storage_Root + wholePath; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 517 | LOGINFO("Looking for zip at '%s'\n", wholePath.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 518 | if (TWFunc::Path_Exists(wholePath)) |
| 519 | return wholePath; |
| 520 | |
| 521 | pos = Path.find("/", pos + 1); |
| 522 | } |
| 523 | return ""; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | int OpenRecoveryScript::Backup_Command(string Options) { |
| 527 | char value1[SCRIPT_COMMAND_SIZE]; |
| 528 | int line_len, i; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 529 | string Backup_List; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 530 | |
| 531 | strcpy(value1, Options.c_str()); |
| 532 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 533 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 534 | DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 535 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 536 | gui_msg("select_backup_opt=Setting backup options:"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 537 | line_len = Options.size(); |
| 538 | for (i=0; i<line_len; i++) { |
| 539 | if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 540 | Backup_List += "/system;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 541 | gui_msg("system=System"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 542 | } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 543 | Backup_List += "/data;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 544 | gui_msg("data=Data"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 545 | } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 546 | Backup_List += "/cache;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 547 | gui_msg("cache=Cache"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 548 | } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 549 | Backup_List += "/recovery;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 550 | gui_msg("recovery=Recovery"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 551 | } else if (Options.substr(i, 1) == "1") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 552 | gui_print("%s\n", "Special1 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 553 | } else if (Options.substr(i, 1) == "2") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 554 | gui_print("%s\n", "Special2 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 555 | } else if (Options.substr(i, 1) == "3") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 556 | gui_print("%s\n", "Special3 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 557 | } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 558 | Backup_List += "/boot;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 559 | gui_msg("boot=Boot"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 560 | } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 561 | Backup_List += "/and-sec;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 562 | gui_msg("android_secure=Android Secure"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 563 | } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 564 | Backup_List += "/sd-ext;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 565 | gui_msg("sdext=SD-EXT"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 566 | } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 567 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 568 | gui_msg("compression_on=Compression is on"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 569 | } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") { |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 570 | DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 1); |
| 571 | gui_msg("digest_off=Digest Generation is off"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 572 | } |
| 573 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 574 | DataManager::SetValue("tw_backup_list", Backup_List); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 575 | if (!PartitionManager.Run_Backup(false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 576 | gui_err("backup_fail=Backup Failed"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 577 | return 1; |
| 578 | } |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 579 | gui_msg("backup_complete=Backup Complete"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 580 | return 0; |
| 581 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 582 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 583 | // this is called by main() |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 584 | void OpenRecoveryScript::Run_OpenRecoveryScript(void) { |
| 585 | DataManager::SetValue("tw_back", "main"); |
| 586 | DataManager::SetValue("tw_action", "openrecoveryscript"); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 587 | DataManager::SetValue("tw_action_param", ""); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 588 | DataManager::SetValue("tw_has_action2", "0"); |
| 589 | DataManager::SetValue("tw_action2", ""); |
| 590 | DataManager::SetValue("tw_action2_param", ""); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 591 | #ifdef TW_OEM_BUILD |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 592 | DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands")); |
| 593 | DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete")); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 594 | #else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 595 | DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript")); |
| 596 | DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete")); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 597 | #endif |
| 598 | DataManager::SetValue("tw_action_text2", ""); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 599 | DataManager::SetValue("tw_has_cancel", 0); |
| 600 | DataManager::SetValue("tw_show_reboot", 0); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 601 | if (gui_startPage("action_page", 0, 1) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 602 | LOGERR("Failed to load OpenRecoveryScript GUI page.\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 603 | } |
Dees_Troy | 6bdcbb1 | 2013-01-26 14:07:43 +0000 | [diff] [blame] | 604 | } |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 605 | |
| 606 | // this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript |
| 607 | int OpenRecoveryScript::Run_OpenRecoveryScript_Action() { |
| 608 | int op_status = 1; |
| 609 | // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands |
| 610 | // that we converted to ORS commands during boot in recovery.cpp. |
| 611 | // Run those first. |
| 612 | int reboot = 0; |
| 613 | if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) { |
| 614 | gui_msg("running_recovery_commands=Running Recovery Commands"); |
| 615 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 616 | reboot = 1; |
| 617 | op_status = 0; |
| 618 | } |
| 619 | } |
| 620 | // Check for the ORS file in /cache and attempt to run those commands. |
| 621 | if (OpenRecoveryScript::check_for_script_file()) { |
| 622 | gui_msg("running_ors=Running OpenRecoveryScript"); |
| 623 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 624 | reboot = 1; |
| 625 | op_status = 0; |
| 626 | } |
| 627 | } |
| 628 | if (reboot) { |
| 629 | // Disable stock recovery reflashing |
| 630 | TWFunc::Disable_Stock_Recovery_Replace(); |
| 631 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 632 | TWFunc::tw_reboot(rb_system); |
| 633 | usleep(5000000); // Sleep for 5 seconds to allow reboot to occur |
| 634 | } else { |
| 635 | DataManager::SetValue("tw_page_done", 1); |
| 636 | } |
| 637 | return op_status; |
| 638 | } |
| 639 | |
| 640 | // this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool |
| 641 | void OpenRecoveryScript::Run_CLI_Command(const char* command) { |
| 642 | if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) { |
| 643 | const char* filename = command + 10; |
| 644 | if (OpenRecoveryScript::copy_script_file(filename) == 0) { |
| 645 | LOGINFO("Unable to copy script file\n"); |
| 646 | } else { |
| 647 | OpenRecoveryScript::run_script_file(); |
| 648 | } |
| 649 | } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) { |
| 650 | const char* varname = command + 4; |
| 651 | string value; |
| 652 | DataManager::GetValue(varname, value); |
| 653 | gui_print("%s = %s\n", varname, value.c_str()); |
| 654 | } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) { |
| 655 | const char* pass = command + 8; |
| 656 | gui_msg("decrypt_cmd=Attempting to decrypt data partition via command line."); |
| 657 | if (PartitionManager.Decrypt_Device(pass) == 0) { |
| 658 | // set_page_done = 1; // done by singleaction_page anyway |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 659 | std::string orsFile = TWFunc::get_cache_dir() + "/openrecoveryscript"; |
| 660 | if (TWFunc::Path_Exists(orsFile)) { |
| 661 | Run_OpenRecoveryScript_Action(); |
| 662 | } |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 663 | } |
| 664 | } else if (OpenRecoveryScript::Insert_ORS_Command(command)) { |
| 665 | OpenRecoveryScript::run_script_file(); |
| 666 | } |
| 667 | |
| 668 | // let the GUI close the output fd and restart the command listener |
| 669 | call_after_cli_command(); |
| 670 | LOGINFO("Done reading ORS command from command line\n"); |
| 671 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 672 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 673 | int OpenRecoveryScript::remountrw(void) |
| 674 | { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 675 | bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path()); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 676 | int op_status; |
| 677 | TWPartition* Part; |
| 678 | |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 679 | if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 680 | op_status = 1; // fail |
| 681 | } else { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 682 | Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 683 | if (Part) { |
| 684 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 685 | Part->Change_Mount_Read_Only(false); |
| 686 | } |
| 687 | if (remount_system) { |
| 688 | Part->Mount(true); |
| 689 | } |
| 690 | op_status = 0; // success |
| 691 | } |
| 692 | |
| 693 | return op_status; |
| 694 | } |