blob: ad14bc22c64413a7658bf398056a35ed1264c605 [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"
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"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054#include "twinstall.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) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -050065 std::string orsFile = TWFunc::get_cache_dir() + "/recovery/openrecoveryscript";
66 if (!PartitionManager.Mount_By_Path(orsFile, false)) {
67 LOGINFO("Unable to mount %s for OpenRecoveryScript support.\n", TWFunc::get_cache_dir().c_str());
68 gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(TWFunc::get_cache_dir()));
Dees_Troy812660f2012-09-20 09:55:17 -040069 return 0;
70 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -050071 if (TWFunc::Path_Exists(orsFile)) {
72 LOGINFO("Script file found: '%s'\n", orsFile.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -040073 // Copy script file to /tmp
bigbiff bigbiff19874f12019-01-08 20:06:57 -050074 TWFunc::copy_file(orsFile, SCRIPT_FILE_TMP, 0755);
75 // Delete the file from cache
76 unlink(orsFile.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -040077 return 1;
78 }
79 return 0;
80}
81
Ethan Yonker03a42f62014-08-08 11:03:51 -050082int OpenRecoveryScript::copy_script_file(string filename) {
83 if (TWFunc::Path_Exists(filename)) {
84 LOGINFO("Script file found: '%s'\n", filename.c_str());
85 if (filename == SCRIPT_FILE_TMP)
86 return 1; // file is already in the right place
87 // Copy script file to /tmp
88 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
89 // Delete the old file
90 unlink(filename.c_str());
91 return 1;
92 }
93 return 0;
94}
95
Dees_Troy812660f2012-09-20 09:55:17 -040096int OpenRecoveryScript::run_script_file(void) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +000097 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040098 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
Matt Mowera8a89d12016-12-30 18:10:37 -060099 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
100 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
Dees_Troy812660f2012-09-20 09:55:17 -0400101 char *val_start, *tok;
102
Matt Mower06543e32017-01-06 15:25:26 -0600103 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy812660f2012-09-20 09:55:17 -0400104 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000105 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000106 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -0400107 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
108 cindex = 0;
109 line_len = strlen(script_line);
110 if (line_len < 2)
111 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +0000112 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -0400113 for (i=0; i<line_len; i++) {
114 if ((int)script_line[i] == 32) {
115 cindex = i;
116 i = line_len;
117 }
118 }
119 memset(command, 0, sizeof(command));
120 memset(value, 0, sizeof(value));
121 if ((int)script_line[line_len - 1] == 10)
Matt Mowera8a89d12016-12-30 18:10:37 -0600122 remove_nl = 2;
123 else
124 remove_nl = 1;
Dees_Troy812660f2012-09-20 09:55:17 -0400125 if (cindex != 0) {
126 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500127 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400128 val_start = script_line;
129 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400130 if ((int) *val_start == 32)
131 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400132 if ((int) *val_start == 51)
133 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400134 if ((int) *val_start == 32)
135 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400136 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400138 } else {
139 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400141 }
142 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400143 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000144 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troy83a3b122012-10-01 14:16:43 -0400145 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400146 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400147 } else if (strcmp(command, "wipe") == 0) {
148 // Wipe
149 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400150 PartitionManager.Wipe_By_Path("/cache");
dianlujitaob76a73a2020-04-30 20:33:20 +0800151 } else if (strcmp(value, "system") == 0 || strcmp(value, "/system") == 0 || strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0) {
152 PartitionManager.Wipe_By_Path("/system");
Dees_Troy812660f2012-09-20 09:55:17 -0400153 } 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 -0400154 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy812660f2012-09-20 09:55:17 -0400155 } 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 -0400156 PartitionManager.Factory_Reset();
Dees_Troy812660f2012-09-20 09:55:17 -0400157 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400159 ret_val = 1;
160 }
161 } else if (strcmp(command, "backup") == 0) {
162 // Backup
Ethan Yonker74db1572015-10-28 12:44:49 -0500163 DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400164 tok = strtok(value, " ");
165 strcpy(value1, tok);
166 tok = strtok(NULL, " ");
167 if (tok != NULL) {
168 memset(value2, 0, sizeof(value2));
169 strcpy(value2, tok);
170 line_len = strlen(tok);
171 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
172 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
173 remove_nl = 2;
174 else
175 remove_nl = 1;
176 } else
177 remove_nl = 0;
178 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000179 DataManager::SetValue(TW_BACKUP_NAME, value2);
Matt Mower3c366972015-12-25 19:28:31 -0600180 gui_msg(Msg("backup_folder_set=Backup folder set to '{1}'")(value2));
Ethan Yonker53796e72019-01-11 22:49:52 -0600181 if (PartitionManager.Check_Backup_Name(value2, true, true) != 0) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400182 ret_val = 1;
183 continue;
184 }
Dees_Troy812660f2012-09-20 09:55:17 -0400185 } else {
186 char empt[50];
187 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000188 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400189 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400190 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400191 } else if (strcmp(command, "restore") == 0) {
192 // Restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500193 DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400194 PartitionManager.Mount_All_Storage();
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400195 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000196 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400197
198 string val = value, restore_folder, restore_partitions;
199 size_t pos = val.find_last_of(" ");
200 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000201 restore_folder = value;
202 partitions[0] = '\0';
203 } else {
204 restore_folder = val.substr(0, pos);
205 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
206 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400207 }
Dees_Troy812660f2012-09-20 09:55:17 -0400208 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500210 gui_msg(Msg("restoring=Restoring {1}...")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400211
212 if (folder_path[0] != '/') {
213 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000214 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600215 std::vector<PartitionList> Storage_List;
216
217 PartitionManager.Get_Partition_List("storage", &Storage_List);
218 int listSize = Storage_List.size();
219 for (int i = 0; i < listSize; i++) {
220 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
221 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000222 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
223 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600224 if (TWFunc::Path_Exists(backup_folder)) {
225 strcpy(folder_path, backup_folder);
226 break;
227 }
Dees_Troy812660f2012-09-20 09:55:17 -0400228 }
229 }
Dees_Troy812660f2012-09-20 09:55:17 -0400230 } else {
231 if (folder_path[strlen(folder_path) - 1] == '/')
232 strcat(folder_path, ".");
233 else
234 strcat(folder_path, "/.");
235 }
236 if (!TWFunc::Path_Exists(folder_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500237 gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400238 ret_val = 1;
239 continue;
240 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000241 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400242
243 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000245 int is_encrypted = 0;
246 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400248 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400250
251 memset(value2, 0, sizeof(value2));
252 strcpy(value2, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500253 gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2));
Dees_Troy812660f2012-09-20 09:55:17 -0400254 line_len = strlen(value2);
255 for (i=0; i<line_len; i++) {
dianlujitaob76a73a2020-04-30 20:33:20 +0800256 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
257 Restore_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500258 gui_msg("system=System");
Dees_Troya13d74f2013-03-24 08:54:55 -0500259 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
260 Restore_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500261 gui_msg("data=Data");
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
263 Restore_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500264 gui_msg("cache=Cache");
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500266 Restore_List += "/recovery;";
267 gui_msg("recovery=Recovery");
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
269 Restore_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500270 gui_msg("boot=Boot");
Dees_Troya13d74f2013-03-24 08:54:55 -0500271 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
272 Restore_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500273 gui_msg("android_secure=Android Secure");
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
275 Restore_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500276 gui_msg("sdext=SD-EXT");
Dees_Troy812660f2012-09-20 09:55:17 -0400277 } else if (value2[i] == 'M' || value2[i] == 'm') {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400278 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 1);
279 gui_msg("digest_check_skip=Digest check skip is on");
Dees_Troy812660f2012-09-20 09:55:17 -0400280 }
281 }
282
Dees_Troya13d74f2013-03-24 08:54:55 -0500283 DataManager::SetValue("tw_restore_selected", Restore_List);
284 } else {
285 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400286 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000287 if (is_encrypted) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500288 gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000289 ret_val = 1;
290 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500291 ret_val = 1;
292 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500293 gui_msg("done=Done.");
bigbiffce8f83c2015-12-12 18:30:21 -0500294 } else if (strcmp(command, "remountrw") == 0) {
295 ret_val = remountrw();
Dees_Troy812660f2012-09-20 09:55:17 -0400296 } else if (strcmp(command, "mount") == 0) {
297 // Mount
Ethan Yonker74db1572015-10-28 12:44:49 -0500298 DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400299 if (value[0] != '/') {
300 strcpy(mount, "/");
301 strcat(mount, value);
302 } else
303 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100304 if (!strcmp(mount, "/system"))
305 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400306 if (PartitionManager.Mount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500307 gui_msg(Msg("mounted=Mounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400308 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
309 // Unmount
Ethan Yonker74db1572015-10-28 12:44:49 -0500310 DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400311 if (value[0] != '/') {
312 strcpy(mount, "/");
313 strcat(mount, value);
314 } else
315 strcpy(mount, value);
Chaosmaster3b3b6342020-01-25 22:51:54 +0100316 if (!strcmp(mount, "/system"))
317 strcpy(mount, PartitionManager.Get_Android_Root_Path().c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400318 if (PartitionManager.UnMount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500319 gui_msg(Msg("unmounted=Unounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400320 } else if (strcmp(command, "set") == 0) {
321 // Set value
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500322 size_t len = strlen(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400323 tok = strtok(value, " ");
324 strcpy(value1, tok);
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500325 if (len > strlen(value1) + 1) {
326 char *val2 = value + strlen(value1) + 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500327 gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500328 DataManager::SetValue(value1, val2);
329 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500330 gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500331 DataManager::SetValue(value1, "");
332 }
Dees_Troy812660f2012-09-20 09:55:17 -0400333 } else if (strcmp(command, "mkdir") == 0) {
334 // Make directory (recursive)
Ethan Yonker74db1572015-10-28 12:44:49 -0500335 DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}"));
336 gui_msg(Msg("making_dir2=Making directory: '{1}'")(value));
thatf1408b32016-01-03 11:09:15 +0100337 if (!TWFunc::Recursive_Mkdir(value)) {
338 // error message already displayed by Recursive_Mkdir
Dees_Troy812660f2012-09-20 09:55:17 -0400339 ret_val = 1;
340 }
341 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500342 if (strlen(value) && strcmp(value, "recovery") == 0)
343 TWFunc::tw_reboot(rb_recovery);
344 else if (strlen(value) && strcmp(value, "poweroff") == 0)
345 TWFunc::tw_reboot(rb_poweroff);
346 else if (strlen(value) && strcmp(value, "bootloader") == 0)
347 TWFunc::tw_reboot(rb_bootloader);
348 else if (strlen(value) && strcmp(value, "download") == 0)
349 TWFunc::tw_reboot(rb_download);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200350 else if (strlen(value) && strcmp(value, "edl") == 0)
351 TWFunc::tw_reboot(rb_edl);
Ethan Yonker03a42f62014-08-08 11:03:51 -0500352 else
353 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400354 } else if (strcmp(command, "cmd") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500355 DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400356 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200357 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400358 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400360 }
361 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000363 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000364 // ADB Sideload
Ethan Yonker74db1572015-10-28 12:44:49 -0500365 DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}"));
Dees_Troy6ed34b72013-01-25 15:01:29 +0000366 install_cmd = -1;
367
368 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600369 string result;
370 pid_t sideload_child_pid;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000371
Ethan Yonker74db1572015-10-28 12:44:49 -0500372 gui_msg("start_sideload=Starting ADB sideload feature...");
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600373 ret_val = apply_from_adb("/", &sideload_child_pid);
374 if (ret_val != 0) {
375 if (ret_val == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -0500376 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 +0000377 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600378 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
379 if (wipe_cache)
380 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000381 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600382 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000383 }
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600384 sideload = 1; // Causes device to go to the home screen afterwards
385 if (sideload_child_pid != 0) {
386 LOGINFO("Signaling child sideload process to exit.\n");
387 struct stat st;
388 // Calling stat() on this magic filename signals the minadbd
389 // subprocess to shut down.
390 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
391 int status;
392 LOGINFO("Waiting for child sideload process to exit.\n");
393 waitpid(sideload_child_pid, &status, 0);
394 }
Dees Troy28a85d22015-04-28 02:03:16 +0000395 property_set("ctl.start", "adbd");
Ethan Yonker74db1572015-10-28 12:44:49 -0500396 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600397 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0 || strcmp(command, "fixcontexts") == 0) {
398 ret_val = PartitionManager.Fix_Contexts();
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600399 if (ret_val != 0)
400 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500401 } else if (strcmp(command, "decrypt") == 0) {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400402 // twrp cmd cannot decrypt a password with space, should decrypt on gui
Ethan Yonker03a42f62014-08-08 11:03:51 -0500403 if (*value) {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400404 string tmp = value;
405 std::vector<string> args = TWFunc::Split_String(tmp, " ");
406
407 string pass = args[0];
408 string userid = "0";
409 if (args.size() > 1)
410 userid = args[1];
411
412 ret_val = PartitionManager.Decrypt_Device(pass, atoi(userid.c_str()));
Ethan Yonker03a42f62014-08-08 11:03:51 -0500413 if (ret_val != 0)
Noah Jacobson5a79f672019-04-28 00:10:07 -0400414 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500415 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500416 gui_err("no_pwd=No password provided.");
Noah Jacobson5a79f672019-04-28 00:10:07 -0400417 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500418 }
Dees_Troy812660f2012-09-20 09:55:17 -0400419 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000420 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400421 ret_val = 1;
422 }
423 }
424 fclose(fp);
that10ae24f2015-12-26 20:53:51 +0100425 unlink(SCRIPT_FILE_TMP);
Ethan Yonker74db1572015-10-28 12:44:49 -0500426 gui_msg("done_ors=Done processing script file");
Dees_Troy812660f2012-09-20 09:55:17 -0400427 } else {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400428 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(
429 strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400430 return 1;
431 }
bigbiffce8f83c2015-12-12 18:30:21 -0500432
Noah Jacobson5a79f672019-04-28 00:10:07 -0400433 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 &&
434 DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500435 gui_msg("injecttwrp=Injecting TWRP into boot image...");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400436 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
437 if (Boot == NULL || Boot->Current_File_System != "emmc")
Noah Jacobson5a79f672019-04-28 00:10:07 -0400438 TWFunc::Exec_Cmd(
439 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400440 else {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400441 string injectcmd =
442 "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" +
443 Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200444 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400445 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500446 gui_msg("done=Done.");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400447 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000448 if (sideload)
Noah Jacobson5a79f672019-04-28 00:10:07 -0400449 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400450 return ret_val;
451}
452
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000453int OpenRecoveryScript::Insert_ORS_Command(string Command) {
xiaolu38c3aa72015-05-26 22:55:18 +0800454 ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000455 if (ORSfile.is_open()) {
456 //if (Command.substr(Command.size() - 1, 1) != "\n")
457 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 LOGINFO("Inserting '%s'\n", Command.c_str());
xiaolu38c3aa72015-05-26 22:55:18 +0800459 ORSfile << Command.c_str() << endl;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000460 ORSfile.close();
461 return 1;
462 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000463 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000464 return 0;
465}
466
Dees_Troy83a3b122012-10-01 14:16:43 -0400467int OpenRecoveryScript::Install_Command(string Zip) {
468 // Install zip
469 string ret_string;
470 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600471 std::vector<PartitionList> Storage_List;
472 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400473
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600474 if (Zip.substr(0, 1) == "@") {
475 // This is a special file that contains a map of blocks on the data partition
476 Full_Path = Zip.substr(1);
477 if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500478 LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str());
479 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600480 return 1;
481 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500482 LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str());
483 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600484 } else if (!TWFunc::Path_Exists(Zip)) {
485 PartitionManager.Mount_All_Storage();
486 PartitionManager.Get_Partition_List("storage", &Storage_List);
487 int listSize = Storage_List.size();
488 for (int i = 0; i < listSize; i++) {
489 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
490 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
491 if (TWFunc::Path_Exists(Full_Path)) {
492 Zip = Full_Path;
493 break;
494 }
495 Full_Path = Zip;
496 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
497 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
498 if (!ret_string.empty()) {
499 Zip = ret_string;
500 break;
501 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600502 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400503 }
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600504 if (!TWFunc::Path_Exists(Zip)) {
505 // zip file doesn't exist
506 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
507 ret_val = 1;
508 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500509 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400510 }
511
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600512 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
Dees_Troy83a3b122012-10-01 14:16:43 -0400513 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500514 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400515 ret_val = 1;
516 } else if (wipe_cache)
517 PartitionManager.Wipe_By_Path("/cache");
518
519 return ret_val;
520}
521
Dees_Troy812660f2012-09-20 09:55:17 -0400522string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
523 string Path = TWFunc::Get_Path(Zip);
524 string File = TWFunc::Get_Filename(Zip);
525 string pathCpy = Path;
526 string wholePath;
527 size_t pos = Path.find("/", 1);
528
529 while (pos != string::npos)
530 {
531 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500532 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600533 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400534 if (TWFunc::Path_Exists(wholePath))
535 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500536 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600537 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400538 if (TWFunc::Path_Exists(wholePath))
539 return wholePath;
540
541 pos = Path.find("/", pos + 1);
542 }
543 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400544}
545
546int OpenRecoveryScript::Backup_Command(string Options) {
547 char value1[SCRIPT_COMMAND_SIZE];
548 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400550
551 strcpy(value1, Options.c_str());
552
Dees_Troy6ed34b72013-01-25 15:01:29 +0000553 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400554 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400555
Ethan Yonker74db1572015-10-28 12:44:49 -0500556 gui_msg("select_backup_opt=Setting backup options:");
Dees_Troy83a3b122012-10-01 14:16:43 -0400557 line_len = Options.size();
558 for (i=0; i<line_len; i++) {
559 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800560 Backup_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500561 gui_msg("system=System");
Dees_Troy83a3b122012-10-01 14:16:43 -0400562 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 Backup_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500564 gui_msg("data=Data");
Dees_Troy83a3b122012-10-01 14:16:43 -0400565 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500566 Backup_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500567 gui_msg("cache=Cache");
Dees_Troy83a3b122012-10-01 14:16:43 -0400568 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 Backup_List += "/recovery;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500570 gui_msg("recovery=Recovery");
Dees_Troy83a3b122012-10-01 14:16:43 -0400571 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000572 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400573 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000574 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400575 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000576 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400577 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500578 Backup_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500579 gui_msg("boot=Boot");
Dees_Troy83a3b122012-10-01 14:16:43 -0400580 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500581 Backup_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500582 gui_msg("android_secure=Android Secure");
Dees_Troy83a3b122012-10-01 14:16:43 -0400583 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500584 Backup_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500585 gui_msg("sdext=SD-EXT");
Dees_Troy83a3b122012-10-01 14:16:43 -0400586 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000587 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500588 gui_msg("compression_on=Compression is on");
Dees_Troy83a3b122012-10-01 14:16:43 -0400589 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400590 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 1);
591 gui_msg("digest_off=Digest Generation is off");
Dees_Troy83a3b122012-10-01 14:16:43 -0400592 }
593 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500594 DataManager::SetValue("tw_backup_list", Backup_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500595 if (!PartitionManager.Run_Backup(false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500596 gui_err("backup_fail=Backup Failed");
Dees_Troy83a3b122012-10-01 14:16:43 -0400597 return 1;
598 }
Matt Mower3c366972015-12-25 19:28:31 -0600599 gui_msg("backup_complete=Backup Complete");
Dees_Troy83a3b122012-10-01 14:16:43 -0400600 return 0;
601}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000602
that10ae24f2015-12-26 20:53:51 +0100603// this is called by main()
Dees_Troy6ed34b72013-01-25 15:01:29 +0000604void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
605 DataManager::SetValue("tw_back", "main");
606 DataManager::SetValue("tw_action", "openrecoveryscript");
that10ae24f2015-12-26 20:53:51 +0100607 DataManager::SetValue("tw_action_param", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000608 DataManager::SetValue("tw_has_action2", "0");
609 DataManager::SetValue("tw_action2", "");
610 DataManager::SetValue("tw_action2_param", "");
Ethan Yonker89583ef2015-08-26 09:01:59 -0500611#ifdef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -0500612 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
613 DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500614#else
Ethan Yonker74db1572015-10-28 12:44:49 -0500615 DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript"));
616 DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500617#endif
618 DataManager::SetValue("tw_action_text2", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000619 DataManager::SetValue("tw_has_cancel", 0);
620 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600621 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000622 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000623 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000624}
that10ae24f2015-12-26 20:53:51 +0100625
626// this is called by the "openrecoveryscript" GUI action called via action page from Run_OpenRecoveryScript
627int OpenRecoveryScript::Run_OpenRecoveryScript_Action() {
628 int op_status = 1;
629 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
630 // that we converted to ORS commands during boot in recovery.cpp.
631 // Run those first.
632 int reboot = 0;
633 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
634 gui_msg("running_recovery_commands=Running Recovery Commands");
635 if (OpenRecoveryScript::run_script_file() == 0) {
636 reboot = 1;
637 op_status = 0;
638 }
639 }
640 // Check for the ORS file in /cache and attempt to run those commands.
641 if (OpenRecoveryScript::check_for_script_file()) {
642 gui_msg("running_ors=Running OpenRecoveryScript");
643 if (OpenRecoveryScript::run_script_file() == 0) {
644 reboot = 1;
645 op_status = 0;
646 }
647 }
648 if (reboot) {
649 // Disable stock recovery reflashing
650 TWFunc::Disable_Stock_Recovery_Replace();
651 usleep(2000000); // Sleep for 2 seconds before rebooting
652 TWFunc::tw_reboot(rb_system);
653 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
654 } else {
655 DataManager::SetValue("tw_page_done", 1);
656 }
657 return op_status;
658}
659
660// this is called by the "twcmd" GUI action when a command is received via FIFO from the "twrp" command line tool
661void OpenRecoveryScript::Run_CLI_Command(const char* command) {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400662 string tmp = command;
663 std::vector<string> parts =
664 TWFunc::Split_String(tmp, " "); // pats[0] is cmd, parts[1...] is args
665 string cmd_str = parts[0];
666
667 if (cmd_str == "runscript") {
668 if (parts.size() > 1) {
669 string filename = parts[1];
670 if (OpenRecoveryScript::copy_script_file(filename) == 0) {
671 LOGINFO("Unable to copy script file\n");
672 } else {
673 OpenRecoveryScript::run_script_file();
674 }
that10ae24f2015-12-26 20:53:51 +0100675 } else {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400676 LOGINFO("Missing parameter: script file name\n");
that10ae24f2015-12-26 20:53:51 +0100677 }
Noah Jacobson5a79f672019-04-28 00:10:07 -0400678 } else if (cmd_str == "get") {
679 if (parts.size() > 1) {
680 string varname = parts[1];
681 string value;
682 DataManager::GetValue(varname, value);
683 gui_print("%s = %s\n", varname.c_str(), value.c_str());
684 } else {
685 LOGINFO("Missing parameter: var name\n");
686 }
687 } else if (cmd_str == "decrypt") {
688 // twrp cmd cannot decrypt a password with space, should decrypt on gui
689 if (parts.size() == 1) {
690 gui_err("no_pwd=No password provided.");
691 } else {
692 string pass = parts[1];
693 string userid = "0";
694 if (parts.size() > 2)
695 userid = parts[2];
696
697 gui_msg("decrypt_cmd=Attempting to decrypt data partition or user data via command line.");
698 if (PartitionManager.Decrypt_Device(pass, atoi(userid.c_str())) == 0) {
699 // set_page_done = 1; // done by singleaction_page anyway
700 std::string orsFile = TWFunc::get_cache_dir() + "/openrecoveryscript";
701 if (TWFunc::Path_Exists(orsFile)) {
702 Run_OpenRecoveryScript_Action();
703 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500704 }
that10ae24f2015-12-26 20:53:51 +0100705 }
706 } else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
707 OpenRecoveryScript::run_script_file();
708 }
709
710 // let the GUI close the output fd and restart the command listener
711 call_after_cli_command();
712 LOGINFO("Done reading ORS command from command line\n");
713}
bigbiffce8f83c2015-12-12 18:30:21 -0500714
bigbiffce8f83c2015-12-12 18:30:21 -0500715int OpenRecoveryScript::remountrw(void)
716{
Captain Throwback9d6feb52018-07-27 10:05:24 -0400717 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500718 int op_status;
719 TWPartition* Part;
720
Captain Throwback9d6feb52018-07-27 10:05:24 -0400721 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500722 op_status = 1; // fail
723 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -0400724 Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
bigbiffce8f83c2015-12-12 18:30:21 -0500725 if (Part) {
726 DataManager::SetValue("tw_mount_system_ro", 0);
727 Part->Change_Mount_Read_Only(false);
728 }
729 if (remount_system) {
730 Part->Mount(true);
731 }
732 op_status = 0; // success
733 }
734
735 return op_status;
736}