Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2 | Copyright 2016 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" |
| 54 | #include "adbbu/libtwadbbu.hpp" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 55 | #include "twinstall.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 56 | extern "C" { |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 57 | #include "gui/gui.h" |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 58 | #include "cutils/properties.h" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 59 | } |
| 60 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 61 | OpenRecoveryScript::VoidFunction OpenRecoveryScript::call_after_cli_command; |
| 62 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 63 | #define SCRIPT_COMMAND_SIZE 512 |
| 64 | |
| 65 | int OpenRecoveryScript::check_for_script_file(void) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 66 | if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 67 | LOGINFO("Unable to mount /cache for OpenRecoveryScript support.\n"); |
| 68 | gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(SCRIPT_FILE_CACHE)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 72 | LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 73 | // Copy script file to /tmp |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 74 | TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 75 | // Delete the file from /cache |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 76 | unlink(SCRIPT_FILE_CACHE); |
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 | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 145 | PartitionManager.Mount_All_Storage(); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 146 | ret_val = Install_Command(value); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 147 | install_cmd = -1; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 148 | } else if (strcmp(command, "wipe") == 0) { |
| 149 | // Wipe |
| 150 | if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) { |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 151 | PartitionManager.Wipe_By_Path("/cache"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 152 | } else if (strcmp(value, "system") == 0 || strcmp(value, "/system") == 0) { |
| 153 | PartitionManager.Wipe_By_Path("/system"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 154 | } 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] | 155 | PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 156 | } 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] | 157 | PartitionManager.Factory_Reset(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 158 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 159 | LOGERR("Error with wipe command value: '%s'\n", value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 160 | ret_val = 1; |
| 161 | } |
| 162 | } else if (strcmp(command, "backup") == 0) { |
| 163 | // Backup |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 164 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 165 | tok = strtok(value, " "); |
| 166 | strcpy(value1, tok); |
| 167 | tok = strtok(NULL, " "); |
| 168 | if (tok != NULL) { |
| 169 | memset(value2, 0, sizeof(value2)); |
| 170 | strcpy(value2, tok); |
| 171 | line_len = strlen(tok); |
| 172 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) { |
| 173 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) |
| 174 | remove_nl = 2; |
| 175 | else |
| 176 | remove_nl = 1; |
| 177 | } else |
| 178 | remove_nl = 0; |
| 179 | strncpy(value2, tok, line_len - remove_nl); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 180 | DataManager::SetValue(TW_BACKUP_NAME, value2); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 181 | gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2)); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 182 | if (PartitionManager.Check_Backup_Name(true) != 0) { |
| 183 | ret_val = 1; |
| 184 | continue; |
| 185 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 186 | } else { |
| 187 | char empt[50]; |
| 188 | strcpy(empt, "(Current Date)"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 189 | DataManager::SetValue(TW_BACKUP_NAME, empt); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 190 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 191 | ret_val = Backup_Command(value1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 192 | } else if (strcmp(command, "restore") == 0) { |
| 193 | // Restore |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 194 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 195 | PartitionManager.Mount_All_Storage(); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 196 | DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 197 | char folder_path[512], partitions[512]; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 198 | |
| 199 | string val = value, restore_folder, restore_partitions; |
| 200 | size_t pos = val.find_last_of(" "); |
| 201 | if (pos == string::npos) { |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 202 | restore_folder = value; |
| 203 | partitions[0] = '\0'; |
| 204 | } else { |
| 205 | restore_folder = val.substr(0, pos); |
| 206 | restore_partitions = val.substr(pos + 1, val.size() - pos - 1); |
| 207 | strcpy(partitions, restore_partitions.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 208 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 209 | strcpy(folder_path, restore_folder.c_str()); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 210 | LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 211 | gui_msg(Msg("restoring=Restoring {1}...")(folder_path)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 212 | |
| 213 | if (folder_path[0] != '/') { |
| 214 | char backup_folder[512]; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 215 | string folder_var; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 216 | std::vector<PartitionList> Storage_List; |
| 217 | |
| 218 | PartitionManager.Get_Partition_List("storage", &Storage_List); |
| 219 | int listSize = Storage_List.size(); |
| 220 | for (int i = 0; i < listSize; i++) { |
| 221 | if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) { |
| 222 | DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 223 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var); |
| 224 | sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path); |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 225 | if (TWFunc::Path_Exists(backup_folder)) { |
| 226 | strcpy(folder_path, backup_folder); |
| 227 | break; |
| 228 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 229 | } |
| 230 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 231 | } else { |
| 232 | if (folder_path[strlen(folder_path) - 1] == '/') |
| 233 | strcat(folder_path, "."); |
| 234 | else |
| 235 | strcat(folder_path, "/."); |
| 236 | } |
| 237 | if (!TWFunc::Path_Exists(folder_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 238 | 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] | 239 | ret_val = 1; |
| 240 | continue; |
| 241 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 242 | DataManager::SetValue("tw_restore", folder_path); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 243 | |
| 244 | PartitionManager.Set_Restore_Files(folder_path); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 245 | string Partition_List; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 246 | int is_encrypted = 0; |
| 247 | DataManager::GetValue("tw_restore_encrypted", is_encrypted); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 248 | DataManager::GetValue("tw_restore_list", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 249 | if (strlen(partitions) != 0) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 250 | string Restore_List; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 251 | |
| 252 | memset(value2, 0, sizeof(value2)); |
| 253 | strcpy(value2, partitions); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 254 | gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 255 | line_len = strlen(value2); |
| 256 | for (i=0; i<line_len; i++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 257 | if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) { |
| 258 | Restore_List += "/system;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 259 | gui_msg("system=System"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 260 | } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) { |
| 261 | Restore_List += "/data;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 262 | gui_msg("data=Data"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 263 | } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) { |
| 264 | Restore_List += "/cache;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 265 | gui_msg("cache=Cache"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 266 | } 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] | 267 | Restore_List += "/recovery;"; |
| 268 | gui_msg("recovery=Recovery"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 269 | } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) { |
| 270 | Restore_List += "/boot;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 271 | gui_msg("boot=Boot"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 272 | } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) { |
| 273 | Restore_List += "/and-sec;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 274 | gui_msg("android_secure=Android Secure"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 275 | } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) { |
| 276 | Restore_List += "/sd-ext;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 277 | gui_msg("sdext=SD-EXT"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 278 | } else if (value2[i] == 'M' || value2[i] == 'm') { |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 279 | DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 1); |
| 280 | gui_msg("digest_check_skip=Digest check skip is on"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 284 | DataManager::SetValue("tw_restore_selected", Restore_List); |
| 285 | } else { |
| 286 | DataManager::SetValue("tw_restore_selected", Partition_List); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 287 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 288 | if (is_encrypted) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 289 | 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] | 290 | ret_val = 1; |
| 291 | } else if (!PartitionManager.Run_Restore(folder_path)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 292 | ret_val = 1; |
| 293 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 294 | gui_msg("done=Done."); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 295 | } else if (strncmp(command, "adbbackup", 9) == 0) { |
| 296 | ret_val = Backup_ADB_Command(value); |
| 297 | if (ret_val == 1) { |
| 298 | twadbbu::Write_TWERROR(); |
| 299 | gui_err("adbbackup_error=Error with ADB Backup. Quitting..."); |
| 300 | } |
| 301 | } else if (strcmp(command, "adbrestore") == 0) { |
| 302 | LOGINFO("running adb restore\n"); |
| 303 | ret_val = Restore_ADB_Backup(); |
| 304 | } else if (strcmp(command, "remountrw") == 0) { |
| 305 | ret_val = remountrw(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 306 | } else if (strcmp(command, "mount") == 0) { |
| 307 | // Mount |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 308 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}")); |
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.Mount_By_Path(mount, true)) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 315 | gui_msg(Msg("mounted=Mounted '{1}'")(mount)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 316 | } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { |
| 317 | // Unmount |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 318 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 319 | if (value[0] != '/') { |
| 320 | strcpy(mount, "/"); |
| 321 | strcat(mount, value); |
| 322 | } else |
| 323 | strcpy(mount, value); |
| 324 | if (PartitionManager.UnMount_By_Path(mount, true)) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 325 | gui_msg(Msg("unmounted=Unounted '{1}'")(mount)); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 326 | } else if (strcmp(command, "set") == 0) { |
| 327 | // Set value |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 328 | size_t len = strlen(value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 329 | tok = strtok(value, " "); |
| 330 | strcpy(value1, tok); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 331 | if (len > strlen(value1) + 1) { |
| 332 | char *val2 = value + strlen(value1) + 1; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 333 | gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2)); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 334 | DataManager::SetValue(value1, val2); |
| 335 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 336 | gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1)); |
Ethan Yonker | 6511c4d | 2015-06-17 16:52:29 -0500 | [diff] [blame] | 337 | DataManager::SetValue(value1, ""); |
| 338 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 339 | } else if (strcmp(command, "mkdir") == 0) { |
| 340 | // Make directory (recursive) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 341 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}")); |
| 342 | gui_msg(Msg("making_dir2=Making directory: '{1}'")(value)); |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 343 | if (!TWFunc::Recursive_Mkdir(value)) { |
| 344 | // error message already displayed by Recursive_Mkdir |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 345 | ret_val = 1; |
| 346 | } |
| 347 | } else if (strcmp(command, "reboot") == 0) { |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 348 | if (strlen(value) && strcmp(value, "recovery") == 0) |
| 349 | TWFunc::tw_reboot(rb_recovery); |
| 350 | else if (strlen(value) && strcmp(value, "poweroff") == 0) |
| 351 | TWFunc::tw_reboot(rb_poweroff); |
| 352 | else if (strlen(value) && strcmp(value, "bootloader") == 0) |
| 353 | TWFunc::tw_reboot(rb_bootloader); |
| 354 | else if (strlen(value) && strcmp(value, "download") == 0) |
| 355 | TWFunc::tw_reboot(rb_download); |
| 356 | else |
| 357 | TWFunc::tw_reboot(rb_system); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 358 | } else if (strcmp(command, "cmd") == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 359 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}")); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 360 | if (cindex != 0) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 361 | TWFunc::Exec_Cmd(value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 362 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 363 | LOGERR("No value given for cmd\n"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 364 | } |
| 365 | } else if (strcmp(command, "print") == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 366 | gui_print("%s\n", value); |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 367 | } else if (strcmp(command, "sideload") == 0) { |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 368 | // ADB Sideload |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 369 | DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}")); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 370 | install_cmd = -1; |
| 371 | |
| 372 | int wipe_cache = 0; |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 373 | string result; |
| 374 | pid_t sideload_child_pid; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 375 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 376 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 377 | ret_val = apply_from_adb("/", &sideload_child_pid); |
| 378 | if (ret_val != 0) { |
| 379 | if (ret_val == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 380 | 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] | 381 | ret_val = 1; // failure |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 382 | } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 383 | if (wipe_cache) |
| 384 | PartitionManager.Wipe_By_Path("/cache"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 385 | } else { |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 386 | ret_val = 1; // failure |
Dees_Troy | 4fc0024 | 2013-01-17 19:22:12 +0000 | [diff] [blame] | 387 | } |
Ethan Yonker | d83587d | 2015-01-03 16:54:18 -0600 | [diff] [blame] | 388 | sideload = 1; // Causes device to go to the home screen afterwards |
| 389 | if (sideload_child_pid != 0) { |
| 390 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 391 | struct stat st; |
| 392 | // Calling stat() on this magic filename signals the minadbd |
| 393 | // subprocess to shut down. |
| 394 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 395 | int status; |
| 396 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 397 | waitpid(sideload_child_pid, &status, 0); |
| 398 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 399 | property_set("ctl.start", "adbd"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 400 | gui_msg("done=Done."); |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 401 | } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) { |
| 402 | ret_val = PartitionManager.Fix_Contexts(); |
Ethan Yonker | aae6e8c | 2014-02-11 21:37:36 -0600 | [diff] [blame] | 403 | if (ret_val != 0) |
| 404 | ret_val = 1; // failure |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 405 | } else if (strcmp(command, "decrypt") == 0) { |
| 406 | if (*value) { |
| 407 | ret_val = PartitionManager.Decrypt_Device(value); |
| 408 | if (ret_val != 0) |
| 409 | ret_val = 1; // failure |
| 410 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 411 | gui_err("no_pwd=No password provided."); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 412 | ret_val = 1; // failure |
| 413 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 414 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 415 | LOGERR("Unrecognized script command: '%s'\n", command); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 416 | ret_val = 1; |
| 417 | } |
| 418 | } |
| 419 | fclose(fp); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 420 | unlink(SCRIPT_FILE_TMP); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 421 | gui_msg("done_ors=Done processing script file"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 422 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 423 | 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] | 424 | return 1; |
| 425 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 426 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 427 | 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] | 428 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 429 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 430 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 431 | 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] | 432 | else { |
| 433 | 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] | 434 | TWFunc::Exec_Cmd(injectcmd.c_str()); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 435 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 436 | gui_msg("done=Done."); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 437 | } |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 438 | if (sideload) |
| 439 | ret_val = 1; // Forces booting to the home page after sideload |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 440 | return ret_val; |
| 441 | } |
| 442 | |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 443 | int OpenRecoveryScript::Insert_ORS_Command(string Command) { |
xiaolu | 38c3aa7 | 2015-05-26 22:55:18 +0800 | [diff] [blame] | 444 | ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out); |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 445 | if (ORSfile.is_open()) { |
| 446 | //if (Command.substr(Command.size() - 1, 1) != "\n") |
| 447 | // Command += "\n"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 448 | LOGINFO("Inserting '%s'\n", Command.c_str()); |
xiaolu | 38c3aa7 | 2015-05-26 22:55:18 +0800 | [diff] [blame] | 449 | ORSfile << Command.c_str() << endl; |
Dees_Troy | 01b6d0c | 2013-01-22 01:41:09 +0000 | [diff] [blame] | 450 | ORSfile.close(); |
| 451 | return 1; |
| 452 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 453 | 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] | 454 | return 0; |
| 455 | } |
| 456 | |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 457 | int OpenRecoveryScript::Install_Command(string Zip) { |
| 458 | // Install zip |
| 459 | string ret_string; |
| 460 | int ret_val = 0, wipe_cache = 0; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 461 | std::vector<PartitionList> Storage_List; |
| 462 | string Full_Path; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 463 | |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 464 | if (Zip.substr(0, 1) == "@") { |
| 465 | // This is a special file that contains a map of blocks on the data partition |
| 466 | Full_Path = Zip.substr(1); |
| 467 | 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] | 468 | LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str()); |
| 469 | 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] | 470 | return 1; |
| 471 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 472 | LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str()); |
| 473 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip)); |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 474 | } else if (!TWFunc::Path_Exists(Zip)) { |
| 475 | PartitionManager.Mount_All_Storage(); |
| 476 | PartitionManager.Get_Partition_List("storage", &Storage_List); |
| 477 | int listSize = Storage_List.size(); |
| 478 | for (int i = 0; i < listSize; i++) { |
| 479 | if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) { |
| 480 | Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip; |
| 481 | if (TWFunc::Path_Exists(Full_Path)) { |
| 482 | Zip = Full_Path; |
| 483 | break; |
| 484 | } |
| 485 | Full_Path = Zip; |
| 486 | LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str()); |
| 487 | ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point); |
| 488 | if (!ret_string.empty()) { |
| 489 | Zip = ret_string; |
| 490 | break; |
| 491 | } |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 492 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 493 | } |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 494 | if (!TWFunc::Path_Exists(Zip)) { |
| 495 | // zip file doesn't exist |
| 496 | gui_print("Unable to locate zip file '%s'.\n", Zip.c_str()); |
| 497 | ret_val = 1; |
| 498 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 499 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip)); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Ethan Yonker | aee144c | 2015-02-11 16:49:19 -0600 | [diff] [blame] | 502 | ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 503 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 504 | 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] | 505 | ret_val = 1; |
| 506 | } else if (wipe_cache) |
| 507 | PartitionManager.Wipe_By_Path("/cache"); |
| 508 | |
| 509 | return ret_val; |
| 510 | } |
| 511 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 512 | int OpenRecoveryScript::Backup_ADB_Command(std::string Options) { |
| 513 | std::vector<std::string> args; |
| 514 | std::string Backup_List; |
| 515 | bool adbbackup = true, ret = false; |
| 516 | std::string rmopt = "--"; |
| 517 | |
| 518 | std::replace(Options.begin(), Options.end(), ':', ' '); |
| 519 | args = TWFunc::Split_String(Options, " "); |
| 520 | |
| 521 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 522 | DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 523 | |
| 524 | if (args[1].compare("--twrp") != 0) { |
| 525 | gui_err("twrp_adbbu_option=--twrp option is required to enable twrp adb backup"); |
| 526 | sleep(2); |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | for (unsigned i = 2; i < args.size(); i++) { |
| 531 | int compress; |
| 532 | |
| 533 | std::string::size_type size = args[i].find(rmopt); |
| 534 | if (size != std::string::npos) |
| 535 | args[i].erase(size, rmopt.length()); |
| 536 | |
| 537 | if (args[i].compare("compress") == 0) { |
| 538 | gui_msg("compression_on=Compression is on"); |
| 539 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1); |
| 540 | continue; |
| 541 | } |
| 542 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, compress); |
| 543 | gui_print("%s\n", args[i].c_str()); |
| 544 | std::string path; |
| 545 | path = "/" + args[i]; |
| 546 | TWPartition* part = PartitionManager.Find_Partition_By_Path(path); |
| 547 | if (part) { |
| 548 | Backup_List += path; |
| 549 | Backup_List += ";"; |
| 550 | } |
| 551 | else { |
| 552 | gui_msg(Msg(msg::kError, "partition_not_found=path: {1} not found in partition list")(path)); |
| 553 | return 1; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (Backup_List.empty()) { |
| 558 | DataManager::GetValue("tw_backup_list", Backup_List); |
| 559 | if (Backup_List.empty()) { |
| 560 | gui_err("no_partition_selected=No partitions selected for backup."); |
| 561 | return 1; |
| 562 | } |
| 563 | } |
| 564 | else |
| 565 | DataManager::SetValue("tw_backup_list", Backup_List); |
| 566 | |
| 567 | ret = PartitionManager.Run_Backup(adbbackup); |
| 568 | DataManager::SetValue(TW_BACKUP_NAME, gui_lookup("auto_generate", "(Auto Generate)")); |
| 569 | if (!ret) { |
| 570 | gui_err("backup_fail=Backup failed"); |
| 571 | return 1; |
| 572 | } |
| 573 | gui_msg("backup_complete=Backup Complete"); |
| 574 | sleep(2); //give time for user to see messages on console |
| 575 | return 0; |
| 576 | } |
| 577 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 578 | string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) { |
| 579 | string Path = TWFunc::Get_Path(Zip); |
| 580 | string File = TWFunc::Get_Filename(Zip); |
| 581 | string pathCpy = Path; |
| 582 | string wholePath; |
| 583 | size_t pos = Path.find("/", 1); |
| 584 | |
| 585 | while (pos != string::npos) |
| 586 | { |
| 587 | pathCpy = Path.substr(pos, Path.size() - pos); |
Matt Mower | e96c3c3 | 2014-09-19 12:14:37 -0500 | [diff] [blame] | 588 | wholePath = pathCpy + File; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 589 | LOGINFO("Looking for zip at '%s'\n", wholePath.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 590 | if (TWFunc::Path_Exists(wholePath)) |
| 591 | return wholePath; |
Matt Mower | e96c3c3 | 2014-09-19 12:14:37 -0500 | [diff] [blame] | 592 | wholePath = Storage_Root + wholePath; |
Ethan Yonker | d7adf29 | 2014-02-15 16:43:23 -0600 | [diff] [blame] | 593 | LOGINFO("Looking for zip at '%s'\n", wholePath.c_str()); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 594 | if (TWFunc::Path_Exists(wholePath)) |
| 595 | return wholePath; |
| 596 | |
| 597 | pos = Path.find("/", pos + 1); |
| 598 | } |
| 599 | return ""; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | int OpenRecoveryScript::Backup_Command(string Options) { |
| 603 | char value1[SCRIPT_COMMAND_SIZE]; |
| 604 | int line_len, i; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 605 | string Backup_List; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 606 | |
| 607 | strcpy(value1, Options.c_str()); |
| 608 | |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 609 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 610 | DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 611 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 612 | gui_msg("select_backup_opt=Setting backup options:"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 613 | line_len = Options.size(); |
| 614 | for (i=0; i<line_len; i++) { |
| 615 | if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 616 | Backup_List += "/system;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 617 | gui_msg("system=System"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 618 | } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 619 | Backup_List += "/data;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 620 | gui_msg("data=Data"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 621 | } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 622 | Backup_List += "/cache;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 623 | gui_msg("cache=Cache"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 624 | } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 625 | Backup_List += "/recovery;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 626 | gui_msg("recovery=Recovery"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 627 | } else if (Options.substr(i, 1) == "1") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 628 | gui_print("%s\n", "Special1 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 629 | } else if (Options.substr(i, 1) == "2") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 630 | gui_print("%s\n", "Special2 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 631 | } else if (Options.substr(i, 1) == "3") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 632 | gui_print("%s\n", "Special3 -- No Longer Supported..."); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 633 | } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 634 | Backup_List += "/boot;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 635 | gui_msg("boot=Boot"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 636 | } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 637 | Backup_List += "/and-sec;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 638 | gui_msg("android_secure=Android Secure"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 639 | } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 640 | Backup_List += "/sd-ext;"; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 641 | gui_msg("sdext=SD-EXT"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 642 | } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") { |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 643 | DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 644 | gui_msg("compression_on=Compression is on"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 645 | } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") { |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 646 | DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 1); |
| 647 | gui_msg("digest_off=Digest Generation is off"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 648 | } |
| 649 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 650 | DataManager::SetValue("tw_backup_list", Backup_List); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 651 | if (!PartitionManager.Run_Backup(false)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 652 | gui_err("backup_fail=Backup Failed"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 653 | return 1; |
| 654 | } |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 655 | gui_msg("backup_complete=Backup Complete"); |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 656 | return 0; |
| 657 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 658 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 659 | // this is called by main() |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 660 | void OpenRecoveryScript::Run_OpenRecoveryScript(void) { |
| 661 | DataManager::SetValue("tw_back", "main"); |
| 662 | DataManager::SetValue("tw_action", "openrecoveryscript"); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 663 | DataManager::SetValue("tw_action_param", ""); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 664 | DataManager::SetValue("tw_has_action2", "0"); |
| 665 | DataManager::SetValue("tw_action2", ""); |
| 666 | DataManager::SetValue("tw_action2_param", ""); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 667 | #ifdef TW_OEM_BUILD |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 668 | DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands")); |
| 669 | 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] | 670 | #else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 671 | DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript")); |
| 672 | DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete")); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 673 | #endif |
| 674 | DataManager::SetValue("tw_action_text2", ""); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 675 | DataManager::SetValue("tw_has_cancel", 0); |
| 676 | DataManager::SetValue("tw_show_reboot", 0); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 677 | if (gui_startPage("action_page", 0, 1) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 678 | LOGERR("Failed to load OpenRecoveryScript GUI page.\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 679 | } |
Dees_Troy | 6bdcbb1 | 2013-01-26 14:07:43 +0000 | [diff] [blame] | 680 | } |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 681 | |
| 682 | // this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript |
| 683 | int OpenRecoveryScript::Run_OpenRecoveryScript_Action() { |
| 684 | int op_status = 1; |
| 685 | // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands |
| 686 | // that we converted to ORS commands during boot in recovery.cpp. |
| 687 | // Run those first. |
| 688 | int reboot = 0; |
| 689 | if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) { |
| 690 | gui_msg("running_recovery_commands=Running Recovery Commands"); |
| 691 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 692 | reboot = 1; |
| 693 | op_status = 0; |
| 694 | } |
| 695 | } |
| 696 | // Check for the ORS file in /cache and attempt to run those commands. |
| 697 | if (OpenRecoveryScript::check_for_script_file()) { |
| 698 | gui_msg("running_ors=Running OpenRecoveryScript"); |
| 699 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 700 | reboot = 1; |
| 701 | op_status = 0; |
| 702 | } |
| 703 | } |
| 704 | if (reboot) { |
| 705 | // Disable stock recovery reflashing |
| 706 | TWFunc::Disable_Stock_Recovery_Replace(); |
| 707 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 708 | TWFunc::tw_reboot(rb_system); |
| 709 | usleep(5000000); // Sleep for 5 seconds to allow reboot to occur |
| 710 | } else { |
| 711 | DataManager::SetValue("tw_page_done", 1); |
| 712 | } |
| 713 | return op_status; |
| 714 | } |
| 715 | |
| 716 | // this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool |
| 717 | void OpenRecoveryScript::Run_CLI_Command(const char* command) { |
| 718 | if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) { |
| 719 | const char* filename = command + 10; |
| 720 | if (OpenRecoveryScript::copy_script_file(filename) == 0) { |
| 721 | LOGINFO("Unable to copy script file\n"); |
| 722 | } else { |
| 723 | OpenRecoveryScript::run_script_file(); |
| 724 | } |
| 725 | } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) { |
| 726 | const char* varname = command + 4; |
| 727 | string value; |
| 728 | DataManager::GetValue(varname, value); |
| 729 | gui_print("%s = %s\n", varname, value.c_str()); |
| 730 | } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) { |
| 731 | const char* pass = command + 8; |
| 732 | gui_msg("decrypt_cmd=Attempting to decrypt data partition via command line."); |
| 733 | if (PartitionManager.Decrypt_Device(pass) == 0) { |
| 734 | // set_page_done = 1; // done by singleaction_page anyway |
| 735 | } |
| 736 | } else if (OpenRecoveryScript::Insert_ORS_Command(command)) { |
| 737 | OpenRecoveryScript::run_script_file(); |
| 738 | } |
| 739 | |
| 740 | // let the GUI close the output fd and restart the command listener |
| 741 | call_after_cli_command(); |
| 742 | LOGINFO("Done reading ORS command from command line\n"); |
| 743 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 744 | |
| 745 | int OpenRecoveryScript::Restore_ADB_Backup(void) { |
| 746 | bool breakloop = false; |
| 747 | int partition_count = 0; |
| 748 | std::string Restore_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 749 | struct AdbBackupFileTrailer adbmd5; |
| 750 | struct PartitionSettings part_settings; |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 751 | int adb_control_twrp_fd; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 752 | int adb_control_bu_fd, ret = 0; |
| 753 | char cmd[512]; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 754 | |
| 755 | part_settings.total_restore_size = 0; |
| 756 | |
| 757 | PartitionManager.Mount_All_Storage(); |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 758 | DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 759 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 760 | LOGINFO("opening TW_ADB_BU_CONTROL\n"); |
| 761 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 762 | if (adb_control_bu_fd < 0) { |
| 763 | LOGERR("Error opening TW_ADB_BU_CONTROL\n"); |
| 764 | return -1; |
| 765 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 766 | LOGINFO("opening TW_ADB_TWRP_CONTROL\n"); |
| 767 | adb_control_twrp_fd = open(TW_ADB_TWRP_CONTROL, O_RDONLY | O_NONBLOCK); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 768 | if (adb_control_twrp_fd < 0) { |
| 769 | LOGERR("Error opening TW_ADB_TWRP_CONTROL\n"); |
| 770 | close(adb_control_bu_fd); |
| 771 | return -1; |
| 772 | } |
| 773 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 774 | memset(&adbmd5, 0, sizeof(adbmd5)); |
| 775 | |
| 776 | while (!breakloop) { |
| 777 | memset(&cmd, 0, sizeof(cmd)); |
| 778 | if (read(adb_control_twrp_fd, cmd, sizeof(cmd)) > 0) { |
| 779 | struct AdbBackupControlType cmdstruct; |
| 780 | |
| 781 | memset(&cmdstruct, 0, sizeof(cmdstruct)); |
| 782 | memcpy(&cmdstruct, cmd, sizeof(cmdstruct)); |
| 783 | std::string cmdstr(cmdstruct.type); |
| 784 | std::string cmdtype = cmdstr.substr(0, sizeof(cmdstruct.type) - 1); |
| 785 | if (cmdstr.substr(0, sizeof(cmdstruct.type) - 1) == TWSTREAMHDR) { |
| 786 | struct AdbBackupStreamHeader twhdr; |
| 787 | memcpy(&twhdr, cmd, sizeof(cmd)); |
| 788 | LOGINFO("ADB Partition count: %" PRIu64 "\n", twhdr.partition_count); |
| 789 | LOGINFO("ADB version: %" PRIu64 "\n", twhdr.version); |
| 790 | if (twhdr.version != ADB_BACKUP_VERSION) { |
| 791 | LOGERR("Incompatible adb backup version!\n"); |
| 792 | breakloop = false; |
| 793 | break; |
| 794 | } |
| 795 | partition_count = twhdr.partition_count; |
| 796 | } |
| 797 | else if (cmdtype == MD5TRAILER) { |
| 798 | LOGINFO("Restoring MD5TRAILER\n"); |
| 799 | memcpy(&adbmd5, cmd, sizeof(cmd)); |
| 800 | } |
| 801 | else if (cmdtype == TWMD5) { |
| 802 | struct AdbBackupFileTrailer md5check; |
| 803 | LOGINFO("Restoring TWMD5\n"); |
| 804 | |
| 805 | memset(&md5check, 0, sizeof(md5check)); |
| 806 | memcpy(&md5check, cmd, sizeof(cmd)); |
| 807 | if (strcmp(md5check.md5, adbmd5.md5) != 0) { |
| 808 | LOGERR("md5 doesn't match!\n"); |
| 809 | LOGERR("file md5: %s\n", adbmd5.md5); |
| 810 | LOGERR("check md5: %s\n", md5check.md5); |
| 811 | breakloop = true; |
| 812 | ret = 1; |
| 813 | break; |
| 814 | } |
| 815 | else { |
| 816 | LOGINFO("adbrestore md5 matches\n"); |
| 817 | LOGINFO("adbmd5.md5: %s\n", adbmd5.md5); |
| 818 | LOGINFO("md5check.md5: %s\n", md5check.md5); |
| 819 | } |
| 820 | } |
| 821 | else if (cmdtype == TWENDADB) { |
| 822 | LOGINFO("received TWENDADB\n"); |
| 823 | breakloop = true; |
| 824 | break; |
| 825 | } |
| 826 | else { |
| 827 | struct twfilehdr twimghdr; |
| 828 | memcpy(&twimghdr, cmd, sizeof(cmd)); |
| 829 | std::string cmdstr(twimghdr.type); |
| 830 | Restore_Name = twimghdr.name; |
| 831 | part_settings.total_restore_size = twimghdr.size; |
| 832 | if (cmdtype == TWIMG) { |
| 833 | LOGINFO("ADB Type: %s\n", twimghdr.type); |
| 834 | LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str()); |
| 835 | LOGINFO("ADB Restore_size: %" PRIu64 "\n", part_settings.total_restore_size); |
| 836 | string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed"; |
| 837 | LOGINFO("ADB compression: %s\n", compression.c_str()); |
| 838 | std::string Backup_FileName; |
| 839 | std::size_t pos = Restore_Name.find_last_of("/"); |
| 840 | std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size()); |
| 841 | pos = path.find_first_of("."); |
| 842 | path = path.substr(0, pos); |
| 843 | if (path.substr(0,1).compare("//")) { |
| 844 | path = path.substr(1, path.size()); |
| 845 | } |
| 846 | |
| 847 | pos = Restore_Name.find_last_of("/"); |
| 848 | Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size()); |
| 849 | part_settings.Part = PartitionManager.Find_Partition_By_Path(path); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 850 | part_settings.Backup_Folder = path; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 851 | part_settings.partition_count = partition_count; |
| 852 | part_settings.adbbackup = true; |
| 853 | part_settings.adb_compression = twimghdr.compressed; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 854 | part_settings.PM_Method = PM_RESTORE; |
| 855 | ProgressTracking progress(part_settings.total_restore_size); |
| 856 | part_settings.progress = &progress; |
| 857 | if (!PartitionManager.Restore_Partition(&part_settings)) { |
| 858 | LOGERR("ADB Restore failed.\n"); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 859 | close(adb_control_twrp_fd); |
| 860 | close(adb_control_bu_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 861 | return 1; |
| 862 | } |
| 863 | } |
| 864 | else if (cmdtype == TWFN) { |
| 865 | LOGINFO("ADB Type: %s\n", twimghdr.type); |
| 866 | LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str()); |
| 867 | LOGINFO("ADB Restore_size: %" PRIi64 "\n", part_settings.total_restore_size); |
| 868 | string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed"; |
| 869 | LOGINFO("ADB compression: %s\n", compression.c_str()); |
| 870 | std::string Backup_FileName; |
| 871 | std::size_t pos = Restore_Name.find_last_of("/"); |
| 872 | std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size()); |
| 873 | pos = path.find_first_of("."); |
| 874 | path = path.substr(0, pos); |
| 875 | if (path.substr(0,1).compare("//")) { |
| 876 | path = path.substr(1, path.size()); |
| 877 | } |
| 878 | |
| 879 | pos = Restore_Name.find_last_of("/"); |
| 880 | Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size()); |
| 881 | pos = Restore_Name.find_last_of("/"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 882 | part_settings.Part = PartitionManager.Find_Partition_By_Path(path); |
| 883 | |
| 884 | if (path.compare("/system") == 0) { |
| 885 | if (part_settings.Part->Is_Read_Only()) { |
| 886 | struct AdbBackupControlType twerror; |
| 887 | strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header)); |
| 888 | strncpy(twerror.type, TWERROR, sizeof(twerror.type)); |
| 889 | memset(twerror.space, 0, sizeof(twerror.space)); |
| 890 | twerror.crc = crc32(0L, Z_NULL, 0); |
| 891 | twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror)); |
| 892 | if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) { |
| 893 | LOGERR("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno)); |
| 894 | } |
| 895 | gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name)); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 896 | close(adb_control_twrp_fd); |
| 897 | close(adb_control_bu_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 898 | return 1; |
| 899 | |
| 900 | } |
| 901 | } |
| 902 | part_settings.partition_count = partition_count; |
| 903 | part_settings.adbbackup = true; |
| 904 | part_settings.adb_compression = twimghdr.compressed; |
bigbiff bigbiff | b5ecaad | 2017-03-20 18:53:53 -0400 | [diff] [blame] | 905 | part_settings.Part->Set_Backup_FileName(part_settings.Part->Get_Backup_Name() + "." + part_settings.Part->Current_File_System + ".win"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 906 | part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings); |
| 907 | part_settings.PM_Method = PM_RESTORE; |
| 908 | ProgressTracking progress(part_settings.total_restore_size); |
| 909 | part_settings.progress = &progress; |
| 910 | if (!PartitionManager.Restore_Partition(&part_settings)) { |
| 911 | LOGERR("ADB Restore failed.\n"); |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 912 | close(adb_control_twrp_fd); |
| 913 | close(adb_control_bu_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 914 | return 1; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
Matt Mower | 06543e3 | 2017-01-06 15:25:26 -0600 | [diff] [blame] | 920 | close(adb_control_twrp_fd); |
| 921 | close(adb_control_bu_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 922 | gui_msg("restore_complete=Restore Complete"); |
| 923 | |
| 924 | if (!twadbbu::Write_TWENDADB()) |
| 925 | ret = 1; |
| 926 | sleep(2); //give time for user to see messages on console |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | int OpenRecoveryScript::remountrw(void) |
| 931 | { |
| 932 | bool remount_system = PartitionManager.Is_Mounted_By_Path("/system"); |
| 933 | int op_status; |
| 934 | TWPartition* Part; |
| 935 | |
| 936 | if (!PartitionManager.UnMount_By_Path("/system", true)) { |
| 937 | op_status = 1; // fail |
| 938 | } else { |
| 939 | Part = PartitionManager.Find_Partition_By_Path("/system"); |
| 940 | if (Part) { |
| 941 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 942 | Part->Change_Mount_Read_Only(false); |
| 943 | } |
| 944 | if (remount_system) { |
| 945 | Part->Mount(true); |
| 946 | } |
| 947 | op_status = 0; // success |
| 948 | } |
| 949 | |
| 950 | return op_status; |
| 951 | } |