blob: 036d6ca81506f80656cd8a350af351c318c520db [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002 Copyright 2003 to 2017 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"
bigbiffd58ba182020-03-23 10:02:29 -040047#include "install/adb_install.h"
Dees_Troy6ed34b72013-01-25 15:01:29 +000048#include "data.hpp"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060049#include "fuse_sideload.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050050#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050051#include "gui/pages.hpp"
52#include "orscmd/orscmd.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050053#include "twinstall.h"
bigbiff1f9e4842020-10-31 11:33:15 -040054#include "twinstall/adb_install.h"
Dees_Troy812660f2012-09-20 09:55:17 -040055extern "C" {
Dees_Troy01b6d0c2013-01-22 01:41:09 +000056 #include "gui/gui.h"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060057 #include "cutils/properties.h"
Dees_Troy812660f2012-09-20 09:55:17 -040058}
59
that10ae24f2015-12-26 20:53:51 +010060OpenRecoveryScript::VoidFunction OpenRecoveryScript::call_after_cli_command;
61
Dees_Troy812660f2012-09-20 09:55:17 -040062#define SCRIPT_COMMAND_SIZE 512
63
64int OpenRecoveryScript::check_for_script_file(void) {
bigbiff25d25b92020-06-19 16:07:38 -040065 std::string logDir = TWFunc::get_log_dir();
66 std::string orsFile;
67 if (logDir == DATA_LOGS_DIR)
68 orsFile = "/data/cache";
69 else
70 orsFile = logDir;
71 orsFile += "/recovery/openrecoveryscript";
bigbiff bigbiff19874f12019-01-08 20:06:57 -050072 if (!PartitionManager.Mount_By_Path(orsFile, false)) {
bigbiff25d25b92020-06-19 16:07:38 -040073 LOGINFO("Unable to mount %s for OpenRecoveryScript support.\n", logDir.c_str());
74 gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(logDir.c_str()));
Dees_Troy812660f2012-09-20 09:55:17 -040075 return 0;
76 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -050077 if (TWFunc::Path_Exists(orsFile)) {
78 LOGINFO("Script file found: '%s'\n", orsFile.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -040079 // Copy script file to /tmp
bigbiff bigbiff19874f12019-01-08 20:06:57 -050080 TWFunc::copy_file(orsFile, SCRIPT_FILE_TMP, 0755);
81 // Delete the file from cache
82 unlink(orsFile.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -040083 return 1;
84 }
85 return 0;
86}
87
Ethan Yonker03a42f62014-08-08 11:03:51 -050088int OpenRecoveryScript::copy_script_file(string filename) {
89 if (TWFunc::Path_Exists(filename)) {
90 LOGINFO("Script file found: '%s'\n", filename.c_str());
91 if (filename == SCRIPT_FILE_TMP)
92 return 1; // file is already in the right place
93 // Copy script file to /tmp
94 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
95 // Delete the old file
96 unlink(filename.c_str());
97 return 1;
98 }
99 return 0;
100}
101
Dees_Troy812660f2012-09-20 09:55:17 -0400102int OpenRecoveryScript::run_script_file(void) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000103 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -0400104 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
Matt Mowera8a89d12016-12-30 18:10:37 -0600105 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
106 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
Dees_Troy812660f2012-09-20 09:55:17 -0400107 char *val_start, *tok;
108
Matt Mower06543e32017-01-06 15:25:26 -0600109 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy812660f2012-09-20 09:55:17 -0400110 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000111 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000112 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -0400113 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
114 cindex = 0;
115 line_len = strlen(script_line);
116 if (line_len < 2)
117 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -0400119 for (i=0; i<line_len; i++) {
120 if ((int)script_line[i] == 32) {
121 cindex = i;
122 i = line_len;
123 }
124 }
125 memset(command, 0, sizeof(command));
126 memset(value, 0, sizeof(value));
127 if ((int)script_line[line_len - 1] == 10)
Matt Mowera8a89d12016-12-30 18:10:37 -0600128 remove_nl = 2;
129 else
130 remove_nl = 1;
Dees_Troy812660f2012-09-20 09:55:17 -0400131 if (cindex != 0) {
132 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500133 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400134 val_start = script_line;
135 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400136 if ((int) *val_start == 32)
137 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400138 if ((int) *val_start == 51)
139 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400140 if ((int) *val_start == 32)
141 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400142 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000143 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400144 } else {
145 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000146 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400147 }
148 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400149 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000150 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troy83a3b122012-10-01 14:16:43 -0400151 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400152 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400153 } else if (strcmp(command, "wipe") == 0) {
154 // Wipe
155 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400156 PartitionManager.Wipe_By_Path("/cache");
dianlujitaob76a73a2020-04-30 20:33:20 +0800157 } else if (strcmp(value, "system") == 0 || strcmp(value, "/system") == 0 || strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0) {
158 PartitionManager.Wipe_By_Path("/system");
Dees_Troy812660f2012-09-20 09:55:17 -0400159 } 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 -0400160 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy812660f2012-09-20 09:55:17 -0400161 } 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 -0400162 PartitionManager.Factory_Reset();
Dees_Troy812660f2012-09-20 09:55:17 -0400163 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400165 ret_val = 1;
166 }
167 } else if (strcmp(command, "backup") == 0) {
168 // Backup
Ethan Yonker74db1572015-10-28 12:44:49 -0500169 DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400170 tok = strtok(value, " ");
171 strcpy(value1, tok);
172 tok = strtok(NULL, " ");
173 if (tok != NULL) {
174 memset(value2, 0, sizeof(value2));
175 strcpy(value2, tok);
176 line_len = strlen(tok);
177 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
178 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
179 remove_nl = 2;
180 else
181 remove_nl = 1;
182 } else
183 remove_nl = 0;
184 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000185 DataManager::SetValue(TW_BACKUP_NAME, value2);
Matt Mower3c366972015-12-25 19:28:31 -0600186 gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2));
Ethan Yonker53796e72019-01-11 22:49:52 -0600187 if (PartitionManager.Check_Backup_Name(value2, true, true) != 0) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400188 ret_val = 1;
189 continue;
190 }
Dees_Troy812660f2012-09-20 09:55:17 -0400191 } else {
192 char empt[50];
193 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000194 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400195 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400196 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400197 } else if (strcmp(command, "restore") == 0) {
198 // Restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500199 DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400200 PartitionManager.Mount_All_Storage();
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400201 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000202 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400203
204 string val = value, restore_folder, restore_partitions;
205 size_t pos = val.find_last_of(" ");
206 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000207 restore_folder = value;
208 partitions[0] = '\0';
209 } else {
210 restore_folder = val.substr(0, pos);
211 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
212 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400213 }
Dees_Troy812660f2012-09-20 09:55:17 -0400214 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000215 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500216 gui_msg(Msg("restoring=Restoring {1}...")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400217
218 if (folder_path[0] != '/') {
219 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000220 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600221 std::vector<PartitionList> Storage_List;
222
223 PartitionManager.Get_Partition_List("storage", &Storage_List);
224 int listSize = Storage_List.size();
225 for (int i = 0; i < listSize; i++) {
226 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
227 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000228 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
229 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600230 if (TWFunc::Path_Exists(backup_folder)) {
231 strcpy(folder_path, backup_folder);
232 break;
233 }
Dees_Troy812660f2012-09-20 09:55:17 -0400234 }
235 }
Dees_Troy812660f2012-09-20 09:55:17 -0400236 } else {
237 if (folder_path[strlen(folder_path) - 1] == '/')
238 strcat(folder_path, ".");
239 else
240 strcat(folder_path, "/.");
241 }
242 if (!TWFunc::Path_Exists(folder_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500243 gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400244 ret_val = 1;
245 continue;
246 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000247 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400248
249 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000251 int is_encrypted = 0;
252 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400254 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400256
257 memset(value2, 0, sizeof(value2));
258 strcpy(value2, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500259 gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2));
Dees_Troy812660f2012-09-20 09:55:17 -0400260 line_len = strlen(value2);
261 for (i=0; i<line_len; i++) {
bigbiffd58ba182020-03-23 10:02:29 -0400262 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
263 Restore_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500264 gui_msg("system=System");
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
266 Restore_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500267 gui_msg("data=Data");
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
269 Restore_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500270 gui_msg("cache=Cache");
Dees_Troya13d74f2013-03-24 08:54:55 -0500271 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500272 Restore_List += "/recovery;";
273 gui_msg("recovery=Recovery");
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
275 Restore_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500276 gui_msg("boot=Boot");
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
278 Restore_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500279 gui_msg("android_secure=Android Secure");
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
281 Restore_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500282 gui_msg("sdext=SD-EXT");
Dees_Troy812660f2012-09-20 09:55:17 -0400283 } else if (value2[i] == 'M' || value2[i] == 'm') {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400284 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 1);
285 gui_msg("digest_check_skip=Digest check skip is on");
Dees_Troy812660f2012-09-20 09:55:17 -0400286 }
287 }
288
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 DataManager::SetValue("tw_restore_selected", Restore_List);
290 } else {
291 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400292 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000293 if (is_encrypted) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500294 gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000295 ret_val = 1;
296 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 ret_val = 1;
298 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500299 gui_msg("done=Done.");
bigbiffce8f83c2015-12-12 18:30:21 -0500300 } else if (strcmp(command, "remountrw") == 0) {
301 ret_val = remountrw();
Dees_Troy812660f2012-09-20 09:55:17 -0400302 } else if (strcmp(command, "mount") == 0) {
303 // Mount
Ethan Yonker74db1572015-10-28 12:44:49 -0500304 DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400305 if (value[0] != '/') {
306 strcpy(mount, "/");
307 strcat(mount, value);
308 } else
309 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100310 if (!strcmp(mount, "/system"))
311 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400312 if (PartitionManager.Mount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500313 gui_msg(Msg("mounted=Mounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400314 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
315 // Unmount
Ethan Yonker74db1572015-10-28 12:44:49 -0500316 DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400317 if (value[0] != '/') {
318 strcpy(mount, "/");
319 strcat(mount, value);
320 } else
321 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100322 if (!strcmp(mount, "/system"))
323 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400324 if (PartitionManager.UnMount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500325 gui_msg(Msg("unmounted=Unounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400326 } else if (strcmp(command, "set") == 0) {
327 // Set value
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500328 size_t len = strlen(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400329 tok = strtok(value, " ");
330 strcpy(value1, tok);
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500331 if (len > strlen(value1) + 1) {
332 char *val2 = value + strlen(value1) + 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500333 gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500334 DataManager::SetValue(value1, val2);
335 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500336 gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500337 DataManager::SetValue(value1, "");
338 }
Dees_Troy812660f2012-09-20 09:55:17 -0400339 } else if (strcmp(command, "mkdir") == 0) {
340 // Make directory (recursive)
Ethan Yonker74db1572015-10-28 12:44:49 -0500341 DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}"));
342 gui_msg(Msg("making_dir2=Making directory: '{1}'")(value));
thatf1408b32016-01-03 11:09:15 +0100343 if (!TWFunc::Recursive_Mkdir(value)) {
344 // error message already displayed by Recursive_Mkdir
Dees_Troy812660f2012-09-20 09:55:17 -0400345 ret_val = 1;
346 }
347 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500348 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);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200356 else if (strlen(value) && strcmp(value, "edl") == 0)
357 TWFunc::tw_reboot(rb_edl);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500358 else
359 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400360 } else if (strcmp(command, "cmd") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500361 DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400362 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200363 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400364 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000365 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400366 }
367 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000368 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000369 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000370 // ADB Sideload
Ethan Yonker74db1572015-10-28 12:44:49 -0500371 DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}"));
Dees_Troy6ed34b72013-01-25 15:01:29 +0000372 install_cmd = -1;
373
374 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600375 string result;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000376
Ethan Yonker74db1572015-10-28 12:44:49 -0500377 gui_msg("start_sideload=Starting ADB sideload feature...");
bigbiffd58ba182020-03-23 10:02:29 -0400378
bigbiffd58ba182020-03-23 10:02:29 -0400379 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
380 ret_val = ApplyFromAdb("/", &reboot_action);
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600381 if (ret_val != 0) {
382 if (ret_val == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -0500383 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 +0000384 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600385 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
386 if (wipe_cache)
387 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000388 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600389 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000390 }
bigbiff1f9e4842020-10-31 11:33:15 -0400391 PartitionManager.Unlock_Block_Partitions();
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600392 sideload = 1; // Causes device to go to the home screen afterwards
bigbiff1f9e4842020-10-31 11:33:15 -0400393 pid_t sideload_child_pid = GetMiniAdbdPid();
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600394 if (sideload_child_pid != 0) {
395 LOGINFO("Signaling child sideload process to exit.\n");
396 struct stat st;
397 // Calling stat() on this magic filename signals the minadbd
398 // subprocess to shut down.
399 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
400 int status;
401 LOGINFO("Waiting for child sideload process to exit.\n");
402 waitpid(sideload_child_pid, &status, 0);
403 }
Dees Troy28a85d22015-04-28 02:03:16 +0000404 property_set("ctl.start", "adbd");
Ethan Yonker74db1572015-10-28 12:44:49 -0500405 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600406 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) {
407 ret_val = PartitionManager.Fix_Contexts();
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600408 if (ret_val != 0)
409 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500410 } else if (strcmp(command, "decrypt") == 0) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400411 // twrp cmd cannot decrypt a password with space, should decrypt on gui
Ethan Yonker03a42f62014-08-08 11:03:51 -0500412 if (*value) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400413 string tmp = value;
414 std::vector<string> args = TWFunc::Split_String(tmp, " ");
415
416 string pass = args[0];
417 string userid = "0";
418 if (args.size() > 1)
419 userid = args[1];
420
421 ret_val = PartitionManager.Decrypt_Device(pass, atoi(userid.c_str()));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500422 if (ret_val != 0)
Noah Jacobson81d638d2019-04-28 00:10:07 -0400423 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500424 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500425 gui_err("no_pwd=No password provided.");
Noah Jacobson81d638d2019-04-28 00:10:07 -0400426 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500427 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400428 } else if (strcmp(command, "listmounts") == 0) {
429 TWFunc::List_Mounts();
Dees_Troy812660f2012-09-20 09:55:17 -0400430 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400432 ret_val = 1;
433 }
434 }
435 fclose(fp);
that10ae24f2015-12-26 20:53:51 +0100436 unlink(SCRIPT_FILE_TMP);
Ethan Yonker74db1572015-10-28 12:44:49 -0500437 gui_msg("done_ors=Done processing script file");
Dees_Troy812660f2012-09-20 09:55:17 -0400438 } else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400439 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(
440 strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400441 return 1;
442 }
bigbiffce8f83c2015-12-12 18:30:21 -0500443
Noah Jacobson81d638d2019-04-28 00:10:07 -0400444 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 &&
445 DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500446 gui_msg("injecttwrp=Injecting TWRP into boot image...");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400447 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
448 if (Boot == NULL || Boot->Current_File_System != "emmc")
Noah Jacobson81d638d2019-04-28 00:10:07 -0400449 TWFunc::Exec_Cmd(
450 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400451 else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400452 string injectcmd =
453 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" +
454 Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200455 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400456 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500457 gui_msg("done=Done.");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400458 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000459 if (sideload)
Noah Jacobson81d638d2019-04-28 00:10:07 -0400460 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400461 return ret_val;
462}
463
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000464int OpenRecoveryScript::Insert_ORS_Command(string Command) {
xiaolu38c3aa72015-05-26 22:55:18 +0800465 ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000466 if (ORSfile.is_open()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000467 LOGINFO("Inserting '%s'\n", Command.c_str());
xiaolu38c3aa72015-05-26 22:55:18 +0800468 ORSfile << Command.c_str() << endl;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000469 ORSfile.close();
470 return 1;
471 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000472 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000473 return 0;
474}
475
Dees_Troy83a3b122012-10-01 14:16:43 -0400476int OpenRecoveryScript::Install_Command(string Zip) {
477 // Install zip
478 string ret_string;
479 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600480 std::vector<PartitionList> Storage_List;
481 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400482
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600483 if (Zip.substr(0, 1) == "@") {
484 // This is a special file that contains a map of blocks on the data partition
485 Full_Path = Zip.substr(1);
486 if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500487 LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str());
488 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600489 return 1;
490 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500491 LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str());
492 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600493 } else if (!TWFunc::Path_Exists(Zip)) {
494 PartitionManager.Mount_All_Storage();
495 PartitionManager.Get_Partition_List("storage", &Storage_List);
496 int listSize = Storage_List.size();
497 for (int i = 0; i < listSize; i++) {
498 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
499 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
500 if (TWFunc::Path_Exists(Full_Path)) {
501 Zip = Full_Path;
502 break;
503 }
504 Full_Path = Zip;
505 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
506 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
507 if (!ret_string.empty()) {
508 Zip = ret_string;
509 break;
510 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600511 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400512 }
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600513 if (!TWFunc::Path_Exists(Zip)) {
514 // zip file doesn't exist
515 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
516 ret_val = 1;
517 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500518 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400519 }
520
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600521 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
Dees_Troy83a3b122012-10-01 14:16:43 -0400522 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500523 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400524 ret_val = 1;
525 } else if (wipe_cache)
526 PartitionManager.Wipe_By_Path("/cache");
527
528 return ret_val;
529}
530
Dees_Troy812660f2012-09-20 09:55:17 -0400531string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
532 string Path = TWFunc::Get_Path(Zip);
533 string File = TWFunc::Get_Filename(Zip);
534 string pathCpy = Path;
535 string wholePath;
536 size_t pos = Path.find("/", 1);
537
538 while (pos != string::npos)
539 {
540 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500541 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600542 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400543 if (TWFunc::Path_Exists(wholePath))
544 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500545 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600546 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400547 if (TWFunc::Path_Exists(wholePath))
548 return wholePath;
549
550 pos = Path.find("/", pos + 1);
551 }
552 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400553}
554
555int OpenRecoveryScript::Backup_Command(string Options) {
556 char value1[SCRIPT_COMMAND_SIZE];
557 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500558 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400559
560 strcpy(value1, Options.c_str());
561
Dees_Troy6ed34b72013-01-25 15:01:29 +0000562 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400563 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400564
Ethan Yonker74db1572015-10-28 12:44:49 -0500565 gui_msg("select_backup_opt=Setting backup options:");
Dees_Troy83a3b122012-10-01 14:16:43 -0400566 line_len = Options.size();
567 for (i=0; i<line_len; i++) {
568 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
bigbiffd58ba182020-03-23 10:02:29 -0400569 Backup_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500570 gui_msg("system=System");
Dees_Troy83a3b122012-10-01 14:16:43 -0400571 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500572 Backup_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500573 gui_msg("data=Data");
Dees_Troy83a3b122012-10-01 14:16:43 -0400574 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500575 Backup_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500576 gui_msg("cache=Cache");
Dees_Troy83a3b122012-10-01 14:16:43 -0400577 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500578 Backup_List += "/recovery;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500579 gui_msg("recovery=Recovery");
Dees_Troy83a3b122012-10-01 14:16:43 -0400580 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400582 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000583 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400584 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400586 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500587 Backup_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500588 gui_msg("boot=Boot");
Dees_Troy83a3b122012-10-01 14:16:43 -0400589 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500590 Backup_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500591 gui_msg("android_secure=Android Secure");
Dees_Troy83a3b122012-10-01 14:16:43 -0400592 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500593 Backup_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500594 gui_msg("sdext=SD-EXT");
Dees_Troy83a3b122012-10-01 14:16:43 -0400595 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000596 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500597 gui_msg("compression_on=Compression is on");
Dees_Troy83a3b122012-10-01 14:16:43 -0400598 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400599 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 1);
600 gui_msg("digest_off=Digest Generation is off");
Dees_Troy83a3b122012-10-01 14:16:43 -0400601 }
602 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500603 DataManager::SetValue("tw_backup_list", Backup_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500604 if (!PartitionManager.Run_Backup(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500605 gui_err("backup_fail=Backup Failed");
Dees_Troy83a3b122012-10-01 14:16:43 -0400606 return 1;
607 }
Matt Mower3c366972015-12-25 19:28:31 -0600608 gui_msg("backup_complete=Backup Complete");
Dees_Troy83a3b122012-10-01 14:16:43 -0400609 return 0;
610}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000611
that10ae24f2015-12-26 20:53:51 +0100612// this is called by main()
Dees_Troy6ed34b72013-01-25 15:01:29 +0000613void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
614 DataManager::SetValue("tw_back", "main");
615 DataManager::SetValue("tw_action", "openrecoveryscript");
that10ae24f2015-12-26 20:53:51 +0100616 DataManager::SetValue("tw_action_param", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000617 DataManager::SetValue("tw_has_action2", "0");
618 DataManager::SetValue("tw_action2", "");
619 DataManager::SetValue("tw_action2_param", "");
Ethan Yonker89583ef2015-08-26 09:01:59 -0500620#ifdef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -0500621 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
622 DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500623#else
Ethan Yonker74db1572015-10-28 12:44:49 -0500624 DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript"));
625 DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500626#endif
627 DataManager::SetValue("tw_action_text2", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000628 DataManager::SetValue("tw_has_cancel", 0);
629 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600630 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000631 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000632 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000633}
that10ae24f2015-12-26 20:53:51 +0100634
635// this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript
636int OpenRecoveryScript::Run_OpenRecoveryScript_Action() {
637 int op_status = 1;
638 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
639 // that we converted to ORS commands during boot in recovery.cpp.
640 // Run those first.
641 int reboot = 0;
642 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
643 gui_msg("running_recovery_commands=Running Recovery Commands");
644 if (OpenRecoveryScript::run_script_file() == 0) {
645 reboot = 1;
646 op_status = 0;
647 }
648 }
649 // Check for the ORS file in /cache and attempt to run those commands.
650 if (OpenRecoveryScript::check_for_script_file()) {
651 gui_msg("running_ors=Running OpenRecoveryScript");
652 if (OpenRecoveryScript::run_script_file() == 0) {
653 reboot = 1;
654 op_status = 0;
655 }
656 }
657 if (reboot) {
658 // Disable stock recovery reflashing
659 TWFunc::Disable_Stock_Recovery_Replace();
660 usleep(2000000); // Sleep for 2 seconds before rebooting
661 TWFunc::tw_reboot(rb_system);
662 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
663 } else {
664 DataManager::SetValue("tw_page_done", 1);
665 }
666 return op_status;
667}
668
669// this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool
670void OpenRecoveryScript::Run_CLI_Command(const char* command) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400671 string tmp = command;
672 std::vector<string> parts =
673 TWFunc::Split_String(tmp, " "); // pats[0] is cmd, parts[1...] is args
674 string cmd_str = parts[0];
675
676 if (cmd_str == "runscript") {
677 if (parts.size() > 1) {
678 string filename = parts[1];
679 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
680 LOGINFO("Unable to copy script file\n");
681 } else {
682 OpenRecoveryScript::run_script_file();
683 }
that10ae24f2015-12-26 20:53:51 +0100684 } else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400685 LOGINFO("Missing parameter: script file name\n");
that10ae24f2015-12-26 20:53:51 +0100686 }
Noah Jacobson81d638d2019-04-28 00:10:07 -0400687 } else if (cmd_str == "get") {
688 if (parts.size() > 1) {
689 string varname = parts[1];
690 string value;
691 DataManager::GetValue(varname, value);
692 gui_print("%s = %s\n", varname.c_str(), value.c_str());
693 } else {
694 LOGINFO("Missing parameter: var name\n");
695 }
696 } else if (cmd_str == "decrypt") {
697 // twrp cmd cannot decrypt a password with space, should decrypt on gui
698 if (parts.size() == 1) {
699 gui_err("no_pwd=No password provided.");
700 } else {
701 string pass = parts[1];
702 string userid = "0";
703 if (parts.size() > 2)
704 userid = parts[2];
705
706 gui_msg("decrypt_cmd=Attempting to decrypt data partition or user data via command line.");
707 if (PartitionManager.Decrypt_Device(pass, atoi(userid.c_str())) == 0) {
708 // set_page_done = 1; // done by singleaction_page anyway
709 std::string orsFile = TWFunc::get_log_dir() + "/openrecoveryscript";
710 if (TWFunc::Path_Exists(orsFile)) {
711 Run_OpenRecoveryScript_Action();
712 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500713 }
that10ae24f2015-12-26 20:53:51 +0100714 }
715 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
716 OpenRecoveryScript::run_script_file();
717 }
718
719 // let the GUI close the output fd and restart the command listener
720 call_after_cli_command();
721 LOGINFO("Done reading ORS command from command line\n");
722}
bigbiffce8f83c2015-12-12 18:30:21 -0500723
bigbiffce8f83c2015-12-12 18:30:21 -0500724int OpenRecoveryScript::remountrw(void)
725{
Captain Throwback9d6feb52018-07-27 10:05:24 -0400726 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500727 int op_status;
728 TWPartition* Part;
729
Captain Throwback9d6feb52018-07-27 10:05:24 -0400730 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500731 op_status = 1; // fail
732 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -0400733 Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500734 if (Part) {
735 DataManager::SetValue("tw_mount_system_ro", 0);
736 Part->Change_Mount_Read_Only(false);
737 }
738 if (remount_system) {
739 Part->Mount(true);
740 }
741 op_status = 0; // success
742 }
743
744 return op_status;
745}