blob: 0d2268cd16ab12880a60a1e21c7272c835d3344d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiffce8f83c2015-12-12 18:30:21 -05002 Copyright 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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_Troy812660f2012-09-20 09:55:17 -040018
bigbiffce8f83c2015-12-12 18:30:21 -050019#define __STDC_FORMAT_MACROS 1
Dees_Troy812660f2012-09-20 09:55:17 -040020#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>
bigbiffce8f83c2015-12-12 18:30:21 -050030#include <inttypes.h>
Dees_Troy01b6d0c2013-01-22 01:41:09 +000031#include <iostream>
32#include <fstream>
bigbiffce8f83c2015-12-12 18:30:21 -050033#include <sstream>
34#include <string>
35#include <iterator>
36#include <algorithm>
Ethan Yonkerd83587d2015-01-03 16:54:18 -060037#include <sys/types.h>
38#include <sys/wait.h>
bigbiffce8f83c2015-12-12 18:30:21 -050039#include <zlib.h>
Dees_Troy812660f2012-09-20 09:55:17 -040040
41#include "twrp-functions.hpp"
42#include "partitions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000043#include "twcommon.h"
Dees_Troy812660f2012-09-20 09:55:17 -040044#include "openrecoveryscript.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050045#include "progresstracking.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040046#include "variables.h"
Dees_Troy4fc00242013-01-17 19:22:12 +000047#include "adb_install.h"
Dees_Troy6ed34b72013-01-25 15:01:29 +000048#include "data.hpp"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060049#include "adb_install.h"
50#include "fuse_sideload.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050051#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050052#include "gui/pages.hpp"
53#include "orscmd/orscmd.h"
54#include "adbbu/libtwadbbu.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040055extern "C" {
Dees_Troy01b6d0c2013-01-22 01:41:09 +000056 #include "twinstall.h"
57 #include "gui/gui.h"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060058 #include "cutils/properties.h"
Dees_Troy01b6d0c2013-01-22 01:41:09 +000059 int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy812660f2012-09-20 09:55:17 -040060}
61
that10ae24f2015-12-26 20:53:51 +010062OpenRecoveryScript::VoidFunction OpenRecoveryScript::call_after_cli_command;
63
Dees_Troy812660f2012-09-20 09:55:17 -040064#define SCRIPT_COMMAND_SIZE 512
65
66int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040067 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -050068 LOGINFO("Unable to mount /cache for OpenRecoveryScript support.\n");
69 gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(SCRIPT_FILE_CACHE));
Dees_Troy812660f2012-09-20 09:55:17 -040070 return 0;
71 }
72 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000073 LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040074 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040076 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050077 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040078 return 1;
79 }
80 return 0;
81}
82
Ethan Yonker03a42f62014-08-08 11:03:51 -050083int OpenRecoveryScript::copy_script_file(string filename) {
84 if (TWFunc::Path_Exists(filename)) {
85 LOGINFO("Script file found: '%s'\n", filename.c_str());
86 if (filename == SCRIPT_FILE_TMP)
87 return 1; // file is already in the right place
88 // Copy script file to /tmp
89 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
90 // Delete the old file
91 unlink(filename.c_str());
92 return 1;
93 }
94 return 0;
95}
96
Dees_Troy812660f2012-09-20 09:55:17 -040097int OpenRecoveryScript::run_script_file(void) {
98 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000099 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -0400100 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
101 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
102 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
103 char *val_start, *tok;
104
105 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000106 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000107 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -0400108 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
109 cindex = 0;
110 line_len = strlen(script_line);
111 if (line_len < 2)
112 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +0000113 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -0400114 for (i=0; i<line_len; i++) {
115 if ((int)script_line[i] == 32) {
116 cindex = i;
117 i = line_len;
118 }
119 }
120 memset(command, 0, sizeof(command));
121 memset(value, 0, sizeof(value));
122 if ((int)script_line[line_len - 1] == 10)
123 remove_nl = 2;
124 else
125 remove_nl = 1;
126 if (cindex != 0) {
127 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500128 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400129 val_start = script_line;
130 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400131 if ((int) *val_start == 32)
132 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400133 if ((int) *val_start == 51)
134 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400135 if ((int) *val_start == 32)
136 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400137 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000138 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400139 } else {
140 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000141 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400142 }
143 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400144 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000145 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000146 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400147 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400148 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400149 } else if (strcmp(command, "wipe") == 0) {
150 // Wipe
151 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400152 PartitionManager.Wipe_By_Path("/cache");
bigbiffce8f83c2015-12-12 18:30:21 -0500153 } else if (strcmp(value, "system") == 0 || strcmp(value, "/system") == 0) {
154 PartitionManager.Wipe_By_Path("/system");
Dees_Troy812660f2012-09-20 09:55:17 -0400155 } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400156 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy812660f2012-09-20 09:55:17 -0400157 } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400158 PartitionManager.Factory_Reset();
Dees_Troy812660f2012-09-20 09:55:17 -0400159 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400161 ret_val = 1;
162 }
163 } else if (strcmp(command, "backup") == 0) {
164 // Backup
Ethan Yonker74db1572015-10-28 12:44:49 -0500165 DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400166 tok = strtok(value, " ");
167 strcpy(value1, tok);
168 tok = strtok(NULL, " ");
169 if (tok != NULL) {
170 memset(value2, 0, sizeof(value2));
171 strcpy(value2, tok);
172 line_len = strlen(tok);
173 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
174 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
175 remove_nl = 2;
176 else
177 remove_nl = 1;
178 } else
179 remove_nl = 0;
180 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000181 DataManager::SetValue(TW_BACKUP_NAME, value2);
Matt Mower3c366972015-12-25 19:28:31 -0600182 gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2));
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400183 if (PartitionManager.Check_Backup_Name(true) != 0) {
184 ret_val = 1;
185 continue;
186 }
Dees_Troy812660f2012-09-20 09:55:17 -0400187 } else {
188 char empt[50];
189 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000190 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400191 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400192 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400193 } else if (strcmp(command, "restore") == 0) {
194 // Restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500195 DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400196 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000197 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000198 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400199
200 string val = value, restore_folder, restore_partitions;
201 size_t pos = val.find_last_of(" ");
202 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000203 restore_folder = value;
204 partitions[0] = '\0';
205 } else {
206 restore_folder = val.substr(0, pos);
207 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
208 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400209 }
Dees_Troy812660f2012-09-20 09:55:17 -0400210 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000211 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500212 gui_msg(Msg("restoring=Restoring {1}...")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400213
214 if (folder_path[0] != '/') {
215 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000216 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600217 std::vector<PartitionList> Storage_List;
218
219 PartitionManager.Get_Partition_List("storage", &Storage_List);
220 int listSize = Storage_List.size();
221 for (int i = 0; i < listSize; i++) {
222 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
223 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000224 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
225 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600226 if (TWFunc::Path_Exists(backup_folder)) {
227 strcpy(folder_path, backup_folder);
228 break;
229 }
Dees_Troy812660f2012-09-20 09:55:17 -0400230 }
231 }
Dees_Troy812660f2012-09-20 09:55:17 -0400232 } else {
233 if (folder_path[strlen(folder_path) - 1] == '/')
234 strcat(folder_path, ".");
235 else
236 strcat(folder_path, "/.");
237 }
238 if (!TWFunc::Path_Exists(folder_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500239 gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400240 ret_val = 1;
241 continue;
242 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000243 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400244
245 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500246 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 int is_encrypted = 0;
248 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400250 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400252
253 memset(value2, 0, sizeof(value2));
254 strcpy(value2, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500255 gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2));
Dees_Troy812660f2012-09-20 09:55:17 -0400256 line_len = strlen(value2);
257 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
259 Restore_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500260 gui_msg("system=System");
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
262 Restore_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500263 gui_msg("data=Data");
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
265 Restore_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500266 gui_msg("cache=Cache");
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500268 Restore_List += "/recovery;";
269 gui_msg("recovery=Recovery");
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
271 Restore_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500272 gui_msg("boot=Boot");
Dees_Troya13d74f2013-03-24 08:54:55 -0500273 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
274 Restore_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500275 gui_msg("android_secure=Android Secure");
Dees_Troya13d74f2013-03-24 08:54:55 -0500276 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
277 Restore_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500278 gui_msg("sdext=SD-EXT");
Dees_Troy812660f2012-09-20 09:55:17 -0400279 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000280 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500281 gui_msg("md5_check_skip=MD5 check skip is on");
Dees_Troy812660f2012-09-20 09:55:17 -0400282 }
283 }
284
Dees_Troya13d74f2013-03-24 08:54:55 -0500285 DataManager::SetValue("tw_restore_selected", Restore_List);
286 } else {
287 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400288 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000289 if (is_encrypted) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500290 gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000291 ret_val = 1;
292 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500293 ret_val = 1;
294 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500295 gui_msg("done=Done.");
bigbiffce8f83c2015-12-12 18:30:21 -0500296 } else if (strncmp(command, "adbbackup", 9) == 0) {
297 ret_val = Backup_ADB_Command(value);
298 if (ret_val == 1) {
299 twadbbu::Write_TWERROR();
300 gui_err("adbbackup_error=Error with ADB Backup. Quitting...");
301 }
302 } else if (strcmp(command, "adbrestore") == 0) {
303 LOGINFO("running adb restore\n");
304 ret_val = Restore_ADB_Backup();
305 } else if (strcmp(command, "remountrw") == 0) {
306 ret_val = remountrw();
Dees_Troy812660f2012-09-20 09:55:17 -0400307 } else if (strcmp(command, "mount") == 0) {
308 // Mount
Ethan Yonker74db1572015-10-28 12:44:49 -0500309 DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400310 if (value[0] != '/') {
311 strcpy(mount, "/");
312 strcat(mount, value);
313 } else
314 strcpy(mount, value);
315 if (PartitionManager.Mount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500316 gui_msg(Msg("mounted=Mounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400317 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
318 // Unmount
Ethan Yonker74db1572015-10-28 12:44:49 -0500319 DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400320 if (value[0] != '/') {
321 strcpy(mount, "/");
322 strcat(mount, value);
323 } else
324 strcpy(mount, value);
325 if (PartitionManager.UnMount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500326 gui_msg(Msg("unmounted=Unounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400327 } else if (strcmp(command, "set") == 0) {
328 // Set value
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500329 size_t len = strlen(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400330 tok = strtok(value, " ");
331 strcpy(value1, tok);
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500332 if (len > strlen(value1) + 1) {
333 char *val2 = value + strlen(value1) + 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500334 gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500335 DataManager::SetValue(value1, val2);
336 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500337 gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500338 DataManager::SetValue(value1, "");
339 }
Dees_Troy812660f2012-09-20 09:55:17 -0400340 } else if (strcmp(command, "mkdir") == 0) {
341 // Make directory (recursive)
Ethan Yonker74db1572015-10-28 12:44:49 -0500342 DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}"));
343 gui_msg(Msg("making_dir2=Making directory: '{1}'")(value));
thatf1408b32016-01-03 11:09:15 +0100344 if (!TWFunc::Recursive_Mkdir(value)) {
345 // error message already displayed by Recursive_Mkdir
Dees_Troy812660f2012-09-20 09:55:17 -0400346 ret_val = 1;
347 }
348 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500349 if (strlen(value) && strcmp(value, "recovery") == 0)
350 TWFunc::tw_reboot(rb_recovery);
351 else if (strlen(value) && strcmp(value, "poweroff") == 0)
352 TWFunc::tw_reboot(rb_poweroff);
353 else if (strlen(value) && strcmp(value, "bootloader") == 0)
354 TWFunc::tw_reboot(rb_bootloader);
355 else if (strlen(value) && strcmp(value, "download") == 0)
356 TWFunc::tw_reboot(rb_download);
357 else
358 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400359 } else if (strcmp(command, "cmd") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500360 DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400361 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200362 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400363 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000364 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400365 }
366 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000368 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000369 // ADB Sideload
Ethan Yonker74db1572015-10-28 12:44:49 -0500370 DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}"));
Dees_Troy6ed34b72013-01-25 15:01:29 +0000371 install_cmd = -1;
372
373 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600374 string result;
375 pid_t sideload_child_pid;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000376
Ethan Yonker74db1572015-10-28 12:44:49 -0500377 gui_msg("start_sideload=Starting ADB sideload feature...");
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600378 ret_val = apply_from_adb("/", &sideload_child_pid);
379 if (ret_val != 0) {
380 if (ret_val == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -0500381 gui_err("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000382 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600383 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
384 if (wipe_cache)
385 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000386 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600387 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000388 }
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600389 sideload = 1; // Causes device to go to the home screen afterwards
390 if (sideload_child_pid != 0) {
391 LOGINFO("Signaling child sideload process to exit.\n");
392 struct stat st;
393 // Calling stat() on this magic filename signals the minadbd
394 // subprocess to shut down.
395 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
396 int status;
397 LOGINFO("Waiting for child sideload process to exit.\n");
398 waitpid(sideload_child_pid, &status, 0);
399 }
Dees Troy28a85d22015-04-28 02:03:16 +0000400 property_set("ctl.start", "adbd");
Ethan Yonker74db1572015-10-28 12:44:49 -0500401 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600402 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) {
403 ret_val = PartitionManager.Fix_Contexts();
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600404 if (ret_val != 0)
405 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500406 } else if (strcmp(command, "decrypt") == 0) {
407 if (*value) {
408 ret_val = PartitionManager.Decrypt_Device(value);
409 if (ret_val != 0)
410 ret_val = 1; // failure
411 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500412 gui_err("no_pwd=No password provided.");
Ethan Yonker03a42f62014-08-08 11:03:51 -0500413 ret_val = 1; // failure
414 }
Dees_Troy812660f2012-09-20 09:55:17 -0400415 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000416 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400417 ret_val = 1;
418 }
419 }
420 fclose(fp);
that10ae24f2015-12-26 20:53:51 +0100421 unlink(SCRIPT_FILE_TMP);
Ethan Yonker74db1572015-10-28 12:44:49 -0500422 gui_msg("done_ors=Done processing script file");
Dees_Troy812660f2012-09-20 09:55:17 -0400423 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500424 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400425 return 1;
426 }
bigbiffce8f83c2015-12-12 18:30:21 -0500427
Dees_Troy6ed34b72013-01-25 15:01:29 +0000428 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500429 gui_msg("injecttwrp=Injecting TWRP into boot image...");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400430 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
431 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200432 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400433 else {
434 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200435 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400436 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500437 gui_msg("done=Done.");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400438 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000439 if (sideload)
440 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400441 return ret_val;
442}
443
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000444int OpenRecoveryScript::Insert_ORS_Command(string Command) {
xiaolu38c3aa72015-05-26 22:55:18 +0800445 ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000446 if (ORSfile.is_open()) {
447 //if (Command.substr(Command.size() - 1, 1) != "\n")
448 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 LOGINFO("Inserting '%s'\n", Command.c_str());
xiaolu38c3aa72015-05-26 22:55:18 +0800450 ORSfile << Command.c_str() << endl;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000451 ORSfile.close();
452 return 1;
453 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000454 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000455 return 0;
456}
457
Dees_Troy83a3b122012-10-01 14:16:43 -0400458int OpenRecoveryScript::Install_Command(string Zip) {
459 // Install zip
460 string ret_string;
461 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600462 std::vector<PartitionList> Storage_List;
463 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400464
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600465 if (Zip.substr(0, 1) == "@") {
466 // This is a special file that contains a map of blocks on the data partition
467 Full_Path = Zip.substr(1);
468 if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500469 LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str());
470 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600471 return 1;
472 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500473 LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str());
474 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600475 } else if (!TWFunc::Path_Exists(Zip)) {
476 PartitionManager.Mount_All_Storage();
477 PartitionManager.Get_Partition_List("storage", &Storage_List);
478 int listSize = Storage_List.size();
479 for (int i = 0; i < listSize; i++) {
480 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
481 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
482 if (TWFunc::Path_Exists(Full_Path)) {
483 Zip = Full_Path;
484 break;
485 }
486 Full_Path = Zip;
487 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
488 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
489 if (!ret_string.empty()) {
490 Zip = ret_string;
491 break;
492 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600493 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400494 }
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600495 if (!TWFunc::Path_Exists(Zip)) {
496 // zip file doesn't exist
497 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
498 ret_val = 1;
499 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500500 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400501 }
502
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600503 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
Dees_Troy83a3b122012-10-01 14:16:43 -0400504 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500505 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400506 ret_val = 1;
507 } else if (wipe_cache)
508 PartitionManager.Wipe_By_Path("/cache");
509
510 return ret_val;
511}
512
bigbiffce8f83c2015-12-12 18:30:21 -0500513int OpenRecoveryScript::Backup_ADB_Command(std::string Options) {
514 std::vector<std::string> args;
515 std::string Backup_List;
516 bool adbbackup = true, ret = false;
517 std::string rmopt = "--";
518
519 std::replace(Options.begin(), Options.end(), ':', ' ');
520 args = TWFunc::Split_String(Options, " ");
521
522 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
523 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
524
525 if (args[1].compare("--twrp") != 0) {
526 gui_err("twrp_adbbu_option=--twrp option is required to enable twrp adb backup");
527 sleep(2);
528 return 1;
529 }
530
531 for (unsigned i = 2; i < args.size(); i++) {
532 int compress;
533
534 std::string::size_type size = args[i].find(rmopt);
535 if (size != std::string::npos)
536 args[i].erase(size, rmopt.length());
537
538 if (args[i].compare("compress") == 0) {
539 gui_msg("compression_on=Compression is on");
540 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
541 continue;
542 }
543 DataManager::GetValue(TW_USE_COMPRESSION_VAR, compress);
544 gui_print("%s\n", args[i].c_str());
545 std::string path;
546 path = "/" + args[i];
547 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
548 if (part) {
549 Backup_List += path;
550 Backup_List += ";";
551 }
552 else {
553 gui_msg(Msg(msg::kError, "partition_not_found=path: {1} not found in partition list")(path));
554 return 1;
555 }
556 }
557
558 if (Backup_List.empty()) {
559 DataManager::GetValue("tw_backup_list", Backup_List);
560 if (Backup_List.empty()) {
561 gui_err("no_partition_selected=No partitions selected for backup.");
562 return 1;
563 }
564 }
565 else
566 DataManager::SetValue("tw_backup_list", Backup_List);
567
568 ret = PartitionManager.Run_Backup(adbbackup);
569 DataManager::SetValue(TW_BACKUP_NAME, gui_lookup("auto_generate", "(Auto Generate)"));
570 if (!ret) {
571 gui_err("backup_fail=Backup failed");
572 return 1;
573 }
574 gui_msg("backup_complete=Backup Complete");
575 sleep(2); //give time for user to see messages on console
576 return 0;
577}
578
Dees_Troy812660f2012-09-20 09:55:17 -0400579string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
580 string Path = TWFunc::Get_Path(Zip);
581 string File = TWFunc::Get_Filename(Zip);
582 string pathCpy = Path;
583 string wholePath;
584 size_t pos = Path.find("/", 1);
585
586 while (pos != string::npos)
587 {
588 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500589 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600590 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400591 if (TWFunc::Path_Exists(wholePath))
592 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500593 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600594 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400595 if (TWFunc::Path_Exists(wholePath))
596 return wholePath;
597
598 pos = Path.find("/", pos + 1);
599 }
600 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400601}
602
603int OpenRecoveryScript::Backup_Command(string Options) {
604 char value1[SCRIPT_COMMAND_SIZE];
605 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500606 string Backup_List;
bigbiffce8f83c2015-12-12 18:30:21 -0500607 bool adbbackup = false;
Dees_Troy83a3b122012-10-01 14:16:43 -0400608
609 strcpy(value1, Options.c_str());
610
Dees_Troy6ed34b72013-01-25 15:01:29 +0000611 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
612 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400613
Ethan Yonker74db1572015-10-28 12:44:49 -0500614 gui_msg("select_backup_opt=Setting backup options:");
Dees_Troy83a3b122012-10-01 14:16:43 -0400615 line_len = Options.size();
616 for (i=0; i<line_len; i++) {
617 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500618 Backup_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500619 gui_msg("system=System");
Dees_Troy83a3b122012-10-01 14:16:43 -0400620 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500621 Backup_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500622 gui_msg("data=Data");
Dees_Troy83a3b122012-10-01 14:16:43 -0400623 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500624 Backup_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500625 gui_msg("cache=Cache");
Dees_Troy83a3b122012-10-01 14:16:43 -0400626 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500627 Backup_List += "/recovery;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500628 gui_msg("recovery=Recovery");
Dees_Troy83a3b122012-10-01 14:16:43 -0400629 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400631 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000632 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400633 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000634 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400635 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500636 Backup_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500637 gui_msg("boot=Boot");
Dees_Troy83a3b122012-10-01 14:16:43 -0400638 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500639 Backup_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg("android_secure=Android Secure");
Dees_Troy83a3b122012-10-01 14:16:43 -0400641 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500642 Backup_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500643 gui_msg("sdext=SD-EXT");
Dees_Troy83a3b122012-10-01 14:16:43 -0400644 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000645 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg("compression_on=Compression is on");
Dees_Troy83a3b122012-10-01 14:16:43 -0400647 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000648 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500649 gui_msg("md5_off=MD5 Generation is off");
Dees_Troy83a3b122012-10-01 14:16:43 -0400650 }
651 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500652 DataManager::SetValue("tw_backup_list", Backup_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500653 if (!PartitionManager.Run_Backup(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500654 gui_err("backup_fail=Backup Failed");
Dees_Troy83a3b122012-10-01 14:16:43 -0400655 return 1;
656 }
Matt Mower3c366972015-12-25 19:28:31 -0600657 gui_msg("backup_complete=Backup Complete");
Dees_Troy83a3b122012-10-01 14:16:43 -0400658 return 0;
659}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000660
that10ae24f2015-12-26 20:53:51 +0100661// this is called by main()
Dees_Troy6ed34b72013-01-25 15:01:29 +0000662void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
663 DataManager::SetValue("tw_back", "main");
664 DataManager::SetValue("tw_action", "openrecoveryscript");
that10ae24f2015-12-26 20:53:51 +0100665 DataManager::SetValue("tw_action_param", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000666 DataManager::SetValue("tw_has_action2", "0");
667 DataManager::SetValue("tw_action2", "");
668 DataManager::SetValue("tw_action2_param", "");
Ethan Yonker89583ef2015-08-26 09:01:59 -0500669#ifdef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -0500670 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
671 DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500672#else
Ethan Yonker74db1572015-10-28 12:44:49 -0500673 DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript"));
674 DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500675#endif
676 DataManager::SetValue("tw_action_text2", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000677 DataManager::SetValue("tw_has_cancel", 0);
678 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600679 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000680 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000681 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000682}
that10ae24f2015-12-26 20:53:51 +0100683
684// this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript
685int OpenRecoveryScript::Run_OpenRecoveryScript_Action() {
686 int op_status = 1;
687 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
688 // that we converted to ORS commands during boot in recovery.cpp.
689 // Run those first.
690 int reboot = 0;
691 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
692 gui_msg("running_recovery_commands=Running Recovery Commands");
693 if (OpenRecoveryScript::run_script_file() == 0) {
694 reboot = 1;
695 op_status = 0;
696 }
697 }
698 // Check for the ORS file in /cache and attempt to run those commands.
699 if (OpenRecoveryScript::check_for_script_file()) {
700 gui_msg("running_ors=Running OpenRecoveryScript");
701 if (OpenRecoveryScript::run_script_file() == 0) {
702 reboot = 1;
703 op_status = 0;
704 }
705 }
706 if (reboot) {
707 // Disable stock recovery reflashing
708 TWFunc::Disable_Stock_Recovery_Replace();
709 usleep(2000000); // Sleep for 2 seconds before rebooting
710 TWFunc::tw_reboot(rb_system);
711 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
712 } else {
713 DataManager::SetValue("tw_page_done", 1);
714 }
715 return op_status;
716}
717
718// this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool
719void OpenRecoveryScript::Run_CLI_Command(const char* command) {
720 if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
721 const char* filename = command + 10;
722 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
723 LOGINFO("Unable to copy script file\n");
724 } else {
725 OpenRecoveryScript::run_script_file();
726 }
727 } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) {
728 const char* varname = command + 4;
729 string value;
730 DataManager::GetValue(varname, value);
731 gui_print("%s = %s\n", varname, value.c_str());
732 } else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) {
733 const char* pass = command + 8;
734 gui_msg("decrypt_cmd=Attempting to decrypt data partition via command line.");
735 if (PartitionManager.Decrypt_Device(pass) == 0) {
736 // set_page_done = 1; // done by singleaction_page anyway
737 }
738 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
739 OpenRecoveryScript::run_script_file();
740 }
741
742 // let the GUI close the output fd and restart the command listener
743 call_after_cli_command();
744 LOGINFO("Done reading ORS command from command line\n");
745}
bigbiffce8f83c2015-12-12 18:30:21 -0500746
747int OpenRecoveryScript::Restore_ADB_Backup(void) {
748 bool breakloop = false;
749 int partition_count = 0;
750 std::string Restore_Name;
751 std::size_t pos = 0;
752 struct AdbBackupFileTrailer adbmd5;
753 struct PartitionSettings part_settings;
754 int adb_control_twrp_fd, adb_write_fd, systemro;
755 int adb_control_bu_fd, ret = 0;
756 char cmd[512];
757 int orsfd = open(ORS_OUTPUT_FILE, O_WRONLY);
758
759 part_settings.total_restore_size = 0;
760
761 PartitionManager.Mount_All_Storage();
762 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
763 LOGINFO("opening TW_ADB_BU_CONTROL\n");
764 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
765 LOGINFO("opening TW_ADB_TWRP_CONTROL\n");
766 adb_control_twrp_fd = open(TW_ADB_TWRP_CONTROL, O_RDONLY | O_NONBLOCK);
767 memset(&adbmd5, 0, sizeof(adbmd5));
768
769 while (!breakloop) {
770 memset(&cmd, 0, sizeof(cmd));
771 if (read(adb_control_twrp_fd, cmd, sizeof(cmd)) > 0) {
772 struct AdbBackupControlType cmdstruct;
773
774 memset(&cmdstruct, 0, sizeof(cmdstruct));
775 memcpy(&cmdstruct, cmd, sizeof(cmdstruct));
776 std::string cmdstr(cmdstruct.type);
777 std::string cmdtype = cmdstr.substr(0, sizeof(cmdstruct.type) - 1);
778 if (cmdstr.substr(0, sizeof(cmdstruct.type) - 1) == TWSTREAMHDR) {
779 struct AdbBackupStreamHeader twhdr;
780 memcpy(&twhdr, cmd, sizeof(cmd));
781 LOGINFO("ADB Partition count: %" PRIu64 "\n", twhdr.partition_count);
782 LOGINFO("ADB version: %" PRIu64 "\n", twhdr.version);
783 if (twhdr.version != ADB_BACKUP_VERSION) {
784 LOGERR("Incompatible adb backup version!\n");
785 breakloop = false;
786 break;
787 }
788 partition_count = twhdr.partition_count;
789 }
790 else if (cmdtype == MD5TRAILER) {
791 LOGINFO("Restoring MD5TRAILER\n");
792 memcpy(&adbmd5, cmd, sizeof(cmd));
793 }
794 else if (cmdtype == TWMD5) {
795 struct AdbBackupFileTrailer md5check;
796 LOGINFO("Restoring TWMD5\n");
797
798 memset(&md5check, 0, sizeof(md5check));
799 memcpy(&md5check, cmd, sizeof(cmd));
800 if (strcmp(md5check.md5, adbmd5.md5) != 0) {
801 LOGERR("md5 doesn't match!\n");
802 LOGERR("file md5: %s\n", adbmd5.md5);
803 LOGERR("check md5: %s\n", md5check.md5);
804 breakloop = true;
805 ret = 1;
806 break;
807 }
808 else {
809 LOGINFO("adbrestore md5 matches\n");
810 LOGINFO("adbmd5.md5: %s\n", adbmd5.md5);
811 LOGINFO("md5check.md5: %s\n", md5check.md5);
812 }
813 }
814 else if (cmdtype == TWENDADB) {
815 LOGINFO("received TWENDADB\n");
816 breakloop = true;
817 break;
818 }
819 else {
820 struct twfilehdr twimghdr;
821 memcpy(&twimghdr, cmd, sizeof(cmd));
822 std::string cmdstr(twimghdr.type);
823 Restore_Name = twimghdr.name;
824 part_settings.total_restore_size = twimghdr.size;
825 if (cmdtype == TWIMG) {
826 LOGINFO("ADB Type: %s\n", twimghdr.type);
827 LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str());
828 LOGINFO("ADB Restore_size: %" PRIu64 "\n", part_settings.total_restore_size);
829 string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed";
830 LOGINFO("ADB compression: %s\n", compression.c_str());
831 std::string Backup_FileName;
832 std::size_t pos = Restore_Name.find_last_of("/");
833 std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size());
834 pos = path.find_first_of(".");
835 path = path.substr(0, pos);
836 if (path.substr(0,1).compare("//")) {
837 path = path.substr(1, path.size());
838 }
839
840 pos = Restore_Name.find_last_of("/");
841 Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size());
842 part_settings.Part = PartitionManager.Find_Partition_By_Path(path);
843 part_settings.Restore_Name = path;
844 part_settings.partition_count = partition_count;
845 part_settings.adbbackup = true;
846 part_settings.adb_compression = twimghdr.compressed;
847 part_settings.Backup_FileName = Backup_FileName;
848 part_settings.PM_Method = PM_RESTORE;
849 ProgressTracking progress(part_settings.total_restore_size);
850 part_settings.progress = &progress;
851 if (!PartitionManager.Restore_Partition(&part_settings)) {
852 LOGERR("ADB Restore failed.\n");
853 return 1;
854 }
855 }
856 else if (cmdtype == TWFN) {
857 LOGINFO("ADB Type: %s\n", twimghdr.type);
858 LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str());
859 LOGINFO("ADB Restore_size: %" PRIi64 "\n", part_settings.total_restore_size);
860 string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed";
861 LOGINFO("ADB compression: %s\n", compression.c_str());
862 std::string Backup_FileName;
863 std::size_t pos = Restore_Name.find_last_of("/");
864 std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size());
865 pos = path.find_first_of(".");
866 path = path.substr(0, pos);
867 if (path.substr(0,1).compare("//")) {
868 path = path.substr(1, path.size());
869 }
870
871 pos = Restore_Name.find_last_of("/");
872 Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size());
873 pos = Restore_Name.find_last_of("/");
874 part_settings.Restore_Name = Restore_Name.substr(0, pos);
875 part_settings.Part = PartitionManager.Find_Partition_By_Path(path);
876
877 if (path.compare("/system") == 0) {
878 if (part_settings.Part->Is_Read_Only()) {
879 struct AdbBackupControlType twerror;
880 strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header));
881 strncpy(twerror.type, TWERROR, sizeof(twerror.type));
882 memset(twerror.space, 0, sizeof(twerror.space));
883 twerror.crc = crc32(0L, Z_NULL, 0);
884 twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror));
885 if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) {
886 LOGERR("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno));
887 }
888 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name));
889 return 1;
890
891 }
892 }
893 part_settings.partition_count = partition_count;
894 part_settings.adbbackup = true;
895 part_settings.adb_compression = twimghdr.compressed;
896 part_settings.Backup_FileName = Backup_FileName;
897 part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings);
898 part_settings.PM_Method = PM_RESTORE;
899 ProgressTracking progress(part_settings.total_restore_size);
900 part_settings.progress = &progress;
901 if (!PartitionManager.Restore_Partition(&part_settings)) {
902 LOGERR("ADB Restore failed.\n");
903 return 1;
904 }
905 }
906 }
907 }
908 }
909 gui_msg("restore_complete=Restore Complete");
910
911 if (!twadbbu::Write_TWENDADB())
912 ret = 1;
913 sleep(2); //give time for user to see messages on console
914 return ret;
915}
916
917int OpenRecoveryScript::remountrw(void)
918{
919 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
920 int op_status;
921 TWPartition* Part;
922
923 if (!PartitionManager.UnMount_By_Path("/system", true)) {
924 op_status = 1; // fail
925 } else {
926 Part = PartitionManager.Find_Partition_By_Path("/system");
927 if (Part) {
928 DataManager::SetValue("tw_mount_system_ro", 0);
929 Part->Change_Mount_Read_Only(false);
930 }
931 if (remount_system) {
932 Part->Mount(true);
933 }
934 op_status = 0; // success
935 }
936
937 return op_status;
938}