blob: 4d1b146fb7ef45ec12692671c388c7ea15ea7cb5 [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");
Adrian DCb206c132021-01-05 23:54:18 +0100159 PartitionManager.Update_System_Details();
Dees_Troy812660f2012-09-20 09:55:17 -0400160 } 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 -0400161 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy812660f2012-09-20 09:55:17 -0400162 } 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 -0400163 PartitionManager.Factory_Reset();
Dees_Troy812660f2012-09-20 09:55:17 -0400164 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400166 ret_val = 1;
167 }
Adrian DCf7a54e52021-01-05 22:34:21 +0100168 } else if (strcmp(command, "format") == 0) {
169 // Format
170 if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
171 PartitionManager.Format_Data();
172 } else {
173 LOGERR("Error with format command value: '%s'\n", value);
174 ret_val = 1;
175 }
Dees_Troy812660f2012-09-20 09:55:17 -0400176 } else if (strcmp(command, "backup") == 0) {
177 // Backup
Ethan Yonker74db1572015-10-28 12:44:49 -0500178 DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400179 tok = strtok(value, " ");
180 strcpy(value1, tok);
181 tok = strtok(NULL, " ");
182 if (tok != NULL) {
183 memset(value2, 0, sizeof(value2));
184 strcpy(value2, tok);
185 line_len = strlen(tok);
186 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
187 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
188 remove_nl = 2;
189 else
190 remove_nl = 1;
191 } else
192 remove_nl = 0;
193 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000194 DataManager::SetValue(TW_BACKUP_NAME, value2);
Matt Mower3c366972015-12-25 19:28:31 -0600195 gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2));
Ethan Yonker53796e72019-01-11 22:49:52 -0600196 if (PartitionManager.Check_Backup_Name(value2, true, true) != 0) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400197 ret_val = 1;
198 continue;
199 }
Dees_Troy812660f2012-09-20 09:55:17 -0400200 } else {
201 char empt[50];
202 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000203 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400204 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400205 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400206 } else if (strcmp(command, "restore") == 0) {
207 // Restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500208 DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400209 PartitionManager.Mount_All_Storage();
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400210 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000211 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400212
213 string val = value, restore_folder, restore_partitions;
214 size_t pos = val.find_last_of(" ");
215 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000216 restore_folder = value;
217 partitions[0] = '\0';
218 } else {
219 restore_folder = val.substr(0, pos);
220 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
221 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400222 }
Dees_Troy812660f2012-09-20 09:55:17 -0400223 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000224 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500225 gui_msg(Msg("restoring=Restoring {1}...")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400226
227 if (folder_path[0] != '/') {
228 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000229 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600230 std::vector<PartitionList> Storage_List;
231
232 PartitionManager.Get_Partition_List("storage", &Storage_List);
233 int listSize = Storage_List.size();
234 for (int i = 0; i < listSize; i++) {
235 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
236 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000237 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
238 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600239 if (TWFunc::Path_Exists(backup_folder)) {
240 strcpy(folder_path, backup_folder);
241 break;
242 }
Dees_Troy812660f2012-09-20 09:55:17 -0400243 }
244 }
Dees_Troy812660f2012-09-20 09:55:17 -0400245 } else {
246 if (folder_path[strlen(folder_path) - 1] == '/')
247 strcat(folder_path, ".");
248 else
249 strcat(folder_path, "/.");
250 }
251 if (!TWFunc::Path_Exists(folder_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500252 gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400253 ret_val = 1;
254 continue;
255 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000256 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400257
258 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500259 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000260 int is_encrypted = 0;
261 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400263 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400265
266 memset(value2, 0, sizeof(value2));
267 strcpy(value2, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500268 gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2));
Dees_Troy812660f2012-09-20 09:55:17 -0400269 line_len = strlen(value2);
270 for (i=0; i<line_len; i++) {
bigbiffd58ba182020-03-23 10:02:29 -0400271 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
272 Restore_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500273 gui_msg("system=System");
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
275 Restore_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500276 gui_msg("data=Data");
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
278 Restore_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500279 gui_msg("cache=Cache");
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500281 Restore_List += "/recovery;";
282 gui_msg("recovery=Recovery");
Dees_Troya13d74f2013-03-24 08:54:55 -0500283 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
284 Restore_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500285 gui_msg("boot=Boot");
Dees_Troya13d74f2013-03-24 08:54:55 -0500286 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
287 Restore_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500288 gui_msg("android_secure=Android Secure");
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
290 Restore_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500291 gui_msg("sdext=SD-EXT");
Dees_Troy812660f2012-09-20 09:55:17 -0400292 } else if (value2[i] == 'M' || value2[i] == 'm') {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400293 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 1);
294 gui_msg("digest_check_skip=Digest check skip is on");
Dees_Troy812660f2012-09-20 09:55:17 -0400295 }
296 }
297
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 DataManager::SetValue("tw_restore_selected", Restore_List);
299 } else {
300 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400301 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000302 if (is_encrypted) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500303 gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000304 ret_val = 1;
305 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500306 ret_val = 1;
307 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500308 gui_msg("done=Done.");
bigbiffce8f83c2015-12-12 18:30:21 -0500309 } else if (strcmp(command, "remountrw") == 0) {
310 ret_val = remountrw();
Dees_Troy812660f2012-09-20 09:55:17 -0400311 } else if (strcmp(command, "mount") == 0) {
312 // Mount
Ethan Yonker74db1572015-10-28 12:44:49 -0500313 DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400314 if (value[0] != '/') {
315 strcpy(mount, "/");
316 strcat(mount, value);
317 } else
318 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100319 if (!strcmp(mount, "/system"))
320 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400321 if (PartitionManager.Mount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500322 gui_msg(Msg("mounted=Mounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400323 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
324 // Unmount
Ethan Yonker74db1572015-10-28 12:44:49 -0500325 DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400326 if (value[0] != '/') {
327 strcpy(mount, "/");
328 strcat(mount, value);
329 } else
330 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100331 if (!strcmp(mount, "/system"))
332 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400333 if (PartitionManager.UnMount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500334 gui_msg(Msg("unmounted=Unounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400335 } else if (strcmp(command, "set") == 0) {
336 // Set value
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500337 size_t len = strlen(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400338 tok = strtok(value, " ");
339 strcpy(value1, tok);
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500340 if (len > strlen(value1) + 1) {
341 char *val2 = value + strlen(value1) + 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500342 gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500343 DataManager::SetValue(value1, val2);
344 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500345 gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500346 DataManager::SetValue(value1, "");
347 }
Dees_Troy812660f2012-09-20 09:55:17 -0400348 } else if (strcmp(command, "mkdir") == 0) {
349 // Make directory (recursive)
Ethan Yonker74db1572015-10-28 12:44:49 -0500350 DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}"));
351 gui_msg(Msg("making_dir2=Making directory: '{1}'")(value));
thatf1408b32016-01-03 11:09:15 +0100352 if (!TWFunc::Recursive_Mkdir(value)) {
353 // error message already displayed by Recursive_Mkdir
Dees_Troy812660f2012-09-20 09:55:17 -0400354 ret_val = 1;
355 }
356 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500357 if (strlen(value) && strcmp(value, "recovery") == 0)
358 TWFunc::tw_reboot(rb_recovery);
359 else if (strlen(value) && strcmp(value, "poweroff") == 0)
360 TWFunc::tw_reboot(rb_poweroff);
361 else if (strlen(value) && strcmp(value, "bootloader") == 0)
362 TWFunc::tw_reboot(rb_bootloader);
363 else if (strlen(value) && strcmp(value, "download") == 0)
364 TWFunc::tw_reboot(rb_download);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200365 else if (strlen(value) && strcmp(value, "edl") == 0)
366 TWFunc::tw_reboot(rb_edl);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500367 else
368 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400369 } else if (strcmp(command, "cmd") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500370 DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400371 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200372 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400373 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000374 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400375 }
376 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000377 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000378 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000379 // ADB Sideload
Ethan Yonker74db1572015-10-28 12:44:49 -0500380 DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}"));
Dees_Troy6ed34b72013-01-25 15:01:29 +0000381 install_cmd = -1;
382
383 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600384 string result;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000385
Ethan Yonker74db1572015-10-28 12:44:49 -0500386 gui_msg("start_sideload=Starting ADB sideload feature...");
bigbiffd58ba182020-03-23 10:02:29 -0400387
bigbiffd58ba182020-03-23 10:02:29 -0400388 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff673c7ae2020-12-02 19:44:56 -0500389 ret_val = twrp_sideload("/", &reboot_action);
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600390 if (ret_val != 0) {
391 if (ret_val == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -0500392 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 +0000393 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600394 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
395 if (wipe_cache)
396 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000397 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600398 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000399 }
bigbiff1f9e4842020-10-31 11:33:15 -0400400 PartitionManager.Unlock_Block_Partitions();
Adrian DCb206c132021-01-05 23:54:18 +0100401 PartitionManager.Update_System_Details();
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600402 sideload = 1; // Causes device to go to the home screen afterwards
bigbiff1f9e4842020-10-31 11:33:15 -0400403 pid_t sideload_child_pid = GetMiniAdbdPid();
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600404 if (sideload_child_pid != 0) {
405 LOGINFO("Signaling child sideload process to exit.\n");
406 struct stat st;
407 // Calling stat() on this magic filename signals the minadbd
408 // subprocess to shut down.
409 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
410 int status;
411 LOGINFO("Waiting for child sideload process to exit.\n");
412 waitpid(sideload_child_pid, &status, 0);
413 }
Dees Troy28a85d22015-04-28 02:03:16 +0000414 property_set("ctl.start", "adbd");
Ethan Yonker74db1572015-10-28 12:44:49 -0500415 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600416 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) {
417 ret_val = PartitionManager.Fix_Contexts();
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600418 if (ret_val != 0)
419 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500420 } else if (strcmp(command, "decrypt") == 0) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400421 // twrp cmd cannot decrypt a password with space, should decrypt on gui
Ethan Yonker03a42f62014-08-08 11:03:51 -0500422 if (*value) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400423 string tmp = value;
424 std::vector<string> args = TWFunc::Split_String(tmp, " ");
425
426 string pass = args[0];
427 string userid = "0";
428 if (args.size() > 1)
429 userid = args[1];
430
431 ret_val = PartitionManager.Decrypt_Device(pass, atoi(userid.c_str()));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500432 if (ret_val != 0)
Noah Jacobson81d638d2019-04-28 00:10:07 -0400433 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500434 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500435 gui_err("no_pwd=No password provided.");
Noah Jacobson81d638d2019-04-28 00:10:07 -0400436 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500437 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400438 } else if (strcmp(command, "listmounts") == 0) {
439 TWFunc::List_Mounts();
Dees_Troy812660f2012-09-20 09:55:17 -0400440 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000441 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400442 ret_val = 1;
443 }
444 }
445 fclose(fp);
that10ae24f2015-12-26 20:53:51 +0100446 unlink(SCRIPT_FILE_TMP);
Ethan Yonker74db1572015-10-28 12:44:49 -0500447 gui_msg("done_ors=Done processing script file");
Dees_Troy812660f2012-09-20 09:55:17 -0400448 } else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400449 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(
450 strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400451 return 1;
452 }
bigbiffce8f83c2015-12-12 18:30:21 -0500453
Noah Jacobson81d638d2019-04-28 00:10:07 -0400454 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 &&
455 DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500456 gui_msg("injecttwrp=Injecting TWRP into boot image...");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400457 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
458 if (Boot == NULL || Boot->Current_File_System != "emmc")
Noah Jacobson81d638d2019-04-28 00:10:07 -0400459 TWFunc::Exec_Cmd(
460 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400461 else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400462 string injectcmd =
463 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" +
464 Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200465 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400466 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500467 gui_msg("done=Done.");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400468 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000469 if (sideload)
Noah Jacobson81d638d2019-04-28 00:10:07 -0400470 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400471 return ret_val;
472}
473
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000474int OpenRecoveryScript::Insert_ORS_Command(string Command) {
xiaolu38c3aa72015-05-26 22:55:18 +0800475 ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000476 if (ORSfile.is_open()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000477 LOGINFO("Inserting '%s'\n", Command.c_str());
xiaolu38c3aa72015-05-26 22:55:18 +0800478 ORSfile << Command.c_str() << endl;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000479 ORSfile.close();
480 return 1;
481 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000483 return 0;
484}
485
Dees_Troy83a3b122012-10-01 14:16:43 -0400486int OpenRecoveryScript::Install_Command(string Zip) {
487 // Install zip
488 string ret_string;
489 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600490 std::vector<PartitionList> Storage_List;
491 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400492
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600493 if (Zip.substr(0, 1) == "@") {
494 // This is a special file that contains a map of blocks on the data partition
495 Full_Path = Zip.substr(1);
496 if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500497 LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str());
498 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600499 return 1;
500 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500501 LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str());
502 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600503 } else if (!TWFunc::Path_Exists(Zip)) {
504 PartitionManager.Mount_All_Storage();
505 PartitionManager.Get_Partition_List("storage", &Storage_List);
506 int listSize = Storage_List.size();
507 for (int i = 0; i < listSize; i++) {
508 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
509 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
510 if (TWFunc::Path_Exists(Full_Path)) {
511 Zip = Full_Path;
512 break;
513 }
514 Full_Path = Zip;
515 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
516 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
517 if (!ret_string.empty()) {
518 Zip = ret_string;
519 break;
520 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600521 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400522 }
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600523 if (!TWFunc::Path_Exists(Zip)) {
524 // zip file doesn't exist
525 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
526 ret_val = 1;
527 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500528 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400529 }
530
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600531 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
Dees_Troy83a3b122012-10-01 14:16:43 -0400532 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500533 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400534 ret_val = 1;
535 } else if (wipe_cache)
536 PartitionManager.Wipe_By_Path("/cache");
537
538 return ret_val;
539}
540
Dees_Troy812660f2012-09-20 09:55:17 -0400541string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
542 string Path = TWFunc::Get_Path(Zip);
543 string File = TWFunc::Get_Filename(Zip);
544 string pathCpy = Path;
545 string wholePath;
546 size_t pos = Path.find("/", 1);
547
548 while (pos != string::npos)
549 {
550 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500551 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600552 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400553 if (TWFunc::Path_Exists(wholePath))
554 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500555 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600556 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400557 if (TWFunc::Path_Exists(wholePath))
558 return wholePath;
559
560 pos = Path.find("/", pos + 1);
561 }
562 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400563}
564
565int OpenRecoveryScript::Backup_Command(string Options) {
566 char value1[SCRIPT_COMMAND_SIZE];
567 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400569
570 strcpy(value1, Options.c_str());
571
Dees_Troy6ed34b72013-01-25 15:01:29 +0000572 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400573 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400574
Ethan Yonker74db1572015-10-28 12:44:49 -0500575 gui_msg("select_backup_opt=Setting backup options:");
Dees_Troy83a3b122012-10-01 14:16:43 -0400576 line_len = Options.size();
577 for (i=0; i<line_len; i++) {
578 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
bigbiffd58ba182020-03-23 10:02:29 -0400579 Backup_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500580 gui_msg("system=System");
Dees_Troy83a3b122012-10-01 14:16:43 -0400581 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500582 Backup_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500583 gui_msg("data=Data");
Dees_Troy83a3b122012-10-01 14:16:43 -0400584 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500585 Backup_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500586 gui_msg("cache=Cache");
Dees_Troy83a3b122012-10-01 14:16:43 -0400587 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500588 Backup_List += "/recovery;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500589 gui_msg("recovery=Recovery");
Dees_Troy83a3b122012-10-01 14:16:43 -0400590 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400592 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400594 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000595 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400596 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500597 Backup_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500598 gui_msg("boot=Boot");
Dees_Troy83a3b122012-10-01 14:16:43 -0400599 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500600 Backup_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500601 gui_msg("android_secure=Android Secure");
Dees_Troy83a3b122012-10-01 14:16:43 -0400602 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500603 Backup_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500604 gui_msg("sdext=SD-EXT");
Dees_Troy83a3b122012-10-01 14:16:43 -0400605 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000606 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500607 gui_msg("compression_on=Compression is on");
Dees_Troy83a3b122012-10-01 14:16:43 -0400608 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400609 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 1);
610 gui_msg("digest_off=Digest Generation is off");
Dees_Troy83a3b122012-10-01 14:16:43 -0400611 }
612 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 DataManager::SetValue("tw_backup_list", Backup_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500614 if (!PartitionManager.Run_Backup(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500615 gui_err("backup_fail=Backup Failed");
Dees_Troy83a3b122012-10-01 14:16:43 -0400616 return 1;
617 }
Matt Mower3c366972015-12-25 19:28:31 -0600618 gui_msg("backup_complete=Backup Complete");
Dees_Troy83a3b122012-10-01 14:16:43 -0400619 return 0;
620}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000621
that10ae24f2015-12-26 20:53:51 +0100622// this is called by main()
Dees_Troy6ed34b72013-01-25 15:01:29 +0000623void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
624 DataManager::SetValue("tw_back", "main");
625 DataManager::SetValue("tw_action", "openrecoveryscript");
that10ae24f2015-12-26 20:53:51 +0100626 DataManager::SetValue("tw_action_param", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000627 DataManager::SetValue("tw_has_action2", "0");
628 DataManager::SetValue("tw_action2", "");
629 DataManager::SetValue("tw_action2_param", "");
Ethan Yonker89583ef2015-08-26 09:01:59 -0500630#ifdef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -0500631 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
632 DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500633#else
Ethan Yonker74db1572015-10-28 12:44:49 -0500634 DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript"));
635 DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500636#endif
637 DataManager::SetValue("tw_action_text2", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000638 DataManager::SetValue("tw_has_cancel", 0);
639 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600640 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000642 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000643}
that10ae24f2015-12-26 20:53:51 +0100644
645// this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript
646int OpenRecoveryScript::Run_OpenRecoveryScript_Action() {
647 int op_status = 1;
648 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
649 // that we converted to ORS commands during boot in recovery.cpp.
650 // Run those first.
651 int reboot = 0;
652 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
653 gui_msg("running_recovery_commands=Running Recovery Commands");
654 if (OpenRecoveryScript::run_script_file() == 0) {
655 reboot = 1;
656 op_status = 0;
657 }
658 }
659 // Check for the ORS file in /cache and attempt to run those commands.
660 if (OpenRecoveryScript::check_for_script_file()) {
661 gui_msg("running_ors=Running OpenRecoveryScript");
662 if (OpenRecoveryScript::run_script_file() == 0) {
663 reboot = 1;
664 op_status = 0;
665 }
666 }
667 if (reboot) {
668 // Disable stock recovery reflashing
669 TWFunc::Disable_Stock_Recovery_Replace();
670 usleep(2000000); // Sleep for 2 seconds before rebooting
671 TWFunc::tw_reboot(rb_system);
672 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
673 } else {
674 DataManager::SetValue("tw_page_done", 1);
675 }
676 return op_status;
677}
678
679// this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool
680void OpenRecoveryScript::Run_CLI_Command(const char* command) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400681 string tmp = command;
682 std::vector<string> parts =
683 TWFunc::Split_String(tmp, " "); // pats[0] is cmd, parts[1...] is args
684 string cmd_str = parts[0];
685
686 if (cmd_str == "runscript") {
687 if (parts.size() > 1) {
688 string filename = parts[1];
689 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
690 LOGINFO("Unable to copy script file\n");
691 } else {
692 OpenRecoveryScript::run_script_file();
693 }
that10ae24f2015-12-26 20:53:51 +0100694 } else {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400695 LOGINFO("Missing parameter: script file name\n");
that10ae24f2015-12-26 20:53:51 +0100696 }
Noah Jacobson81d638d2019-04-28 00:10:07 -0400697 } else if (cmd_str == "get") {
698 if (parts.size() > 1) {
699 string varname = parts[1];
700 string value;
701 DataManager::GetValue(varname, value);
702 gui_print("%s = %s\n", varname.c_str(), value.c_str());
703 } else {
704 LOGINFO("Missing parameter: var name\n");
705 }
706 } else if (cmd_str == "decrypt") {
707 // twrp cmd cannot decrypt a password with space, should decrypt on gui
708 if (parts.size() == 1) {
709 gui_err("no_pwd=No password provided.");
710 } else {
711 string pass = parts[1];
712 string userid = "0";
713 if (parts.size() > 2)
714 userid = parts[2];
715
716 gui_msg("decrypt_cmd=Attempting to decrypt data partition or user data via command line.");
717 if (PartitionManager.Decrypt_Device(pass, atoi(userid.c_str())) == 0) {
718 // set_page_done = 1; // done by singleaction_page anyway
719 std::string orsFile = TWFunc::get_log_dir() + "/openrecoveryscript";
720 if (TWFunc::Path_Exists(orsFile)) {
721 Run_OpenRecoveryScript_Action();
722 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500723 }
that10ae24f2015-12-26 20:53:51 +0100724 }
725 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
726 OpenRecoveryScript::run_script_file();
727 }
728
729 // let the GUI close the output fd and restart the command listener
730 call_after_cli_command();
731 LOGINFO("Done reading ORS command from command line\n");
732}
bigbiffce8f83c2015-12-12 18:30:21 -0500733
bigbiffce8f83c2015-12-12 18:30:21 -0500734int OpenRecoveryScript::remountrw(void)
735{
Captain Throwback9d6feb52018-07-27 10:05:24 -0400736 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500737 int op_status;
738 TWPartition* Part;
739
Captain Throwback9d6feb52018-07-27 10:05:24 -0400740 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500741 op_status = 1; // fail
742 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -0400743 Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500744 if (Part) {
745 DataManager::SetValue("tw_mount_system_ro", 0);
746 Part->Change_Mount_Read_Only(false);
747 }
748 if (remount_system) {
749 Part->Mount(true);
750 }
751 op_status = 0; // success
752 }
753
754 return op_status;
755}