blob: b31a7605fa8a39a4e150ce3bd4eceb1b041e1983 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy812660f2012-09-20 09:55:17 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
25#include <vector>
26#include <dirent.h>
27#include <time.h>
28#include <errno.h>
Dees_Troy01b6d0c2013-01-22 01:41:09 +000029#include <iostream>
30#include <fstream>
Ethan Yonkerd83587d2015-01-03 16:54:18 -060031#include <sys/types.h>
32#include <sys/wait.h>
Dees_Troy812660f2012-09-20 09:55:17 -040033
34#include "twrp-functions.hpp"
35#include "partitions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy812660f2012-09-20 09:55:17 -040037#include "openrecoveryscript.hpp"
38#include "variables.h"
Dees_Troy4fc00242013-01-17 19:22:12 +000039#include "adb_install.h"
Dees_Troy6ed34b72013-01-25 15:01:29 +000040#include "data.hpp"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060041#include "adb_install.h"
42#include "fuse_sideload.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050043#include "gui/gui.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040044extern "C" {
Dees_Troy01b6d0c2013-01-22 01:41:09 +000045 #include "twinstall.h"
46 #include "gui/gui.h"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060047 #include "cutils/properties.h"
Dees_Troy01b6d0c2013-01-22 01:41:09 +000048 int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy812660f2012-09-20 09:55:17 -040049}
50
Dees_Troy812660f2012-09-20 09:55:17 -040051#define SCRIPT_COMMAND_SIZE 512
52
53int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040054 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
Ethan Yonker74db1572015-10-28 12:44:49 -050055 LOGINFO("Unable to mount /cache for OpenRecoveryScript support.\n");
56 gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(SCRIPT_FILE_CACHE));
Dees_Troy812660f2012-09-20 09:55:17 -040057 return 0;
58 }
59 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000060 LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040061 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050062 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040063 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050064 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040065 return 1;
66 }
67 return 0;
68}
69
Ethan Yonker03a42f62014-08-08 11:03:51 -050070int OpenRecoveryScript::copy_script_file(string filename) {
71 if (TWFunc::Path_Exists(filename)) {
72 LOGINFO("Script file found: '%s'\n", filename.c_str());
73 if (filename == SCRIPT_FILE_TMP)
74 return 1; // file is already in the right place
75 // Copy script file to /tmp
76 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
77 // Delete the old file
78 unlink(filename.c_str());
79 return 1;
80 }
81 return 0;
82}
83
Dees_Troy812660f2012-09-20 09:55:17 -040084int OpenRecoveryScript::run_script_file(void) {
85 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000086 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040087 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
88 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
89 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
90 char *val_start, *tok;
91
92 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000093 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000094 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040095 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
96 cindex = 0;
97 line_len = strlen(script_line);
98 if (line_len < 2)
99 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +0000100 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -0400101 for (i=0; i<line_len; i++) {
102 if ((int)script_line[i] == 32) {
103 cindex = i;
104 i = line_len;
105 }
106 }
107 memset(command, 0, sizeof(command));
108 memset(value, 0, sizeof(value));
109 if ((int)script_line[line_len - 1] == 10)
110 remove_nl = 2;
111 else
112 remove_nl = 1;
113 if (cindex != 0) {
114 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500115 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400116 val_start = script_line;
117 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400118 if ((int) *val_start == 32)
119 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400120 if ((int) *val_start == 51)
121 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400122 if ((int) *val_start == 32)
123 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400124 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400126 } else {
127 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400129 }
130 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400131 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000132 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000133 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400134 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400135 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400136 } else if (strcmp(command, "wipe") == 0) {
137 // Wipe
138 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400139 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy812660f2012-09-20 09:55:17 -0400140 } 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 -0400141 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy812660f2012-09-20 09:55:17 -0400142 } 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 -0400143 PartitionManager.Factory_Reset();
Dees_Troy812660f2012-09-20 09:55:17 -0400144 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400146 ret_val = 1;
147 }
148 } else if (strcmp(command, "backup") == 0) {
149 // Backup
Ethan Yonker74db1572015-10-28 12:44:49 -0500150 DataManager::SetValue("tw_action_text2", gui_parse_text("{@backing}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400151 tok = strtok(value, " ");
152 strcpy(value1, tok);
153 tok = strtok(NULL, " ");
154 if (tok != NULL) {
155 memset(value2, 0, sizeof(value2));
156 strcpy(value2, tok);
157 line_len = strlen(tok);
158 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
159 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
160 remove_nl = 2;
161 else
162 remove_nl = 1;
163 } else
164 remove_nl = 0;
165 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000166 DataManager::SetValue(TW_BACKUP_NAME, value2);
Ethan Yonker74db1572015-10-28 12:44:49 -0500167 gui_msg(Msg("backup_folder=Backup folder set to '{1}'")(value2));
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400168 if (PartitionManager.Check_Backup_Name(true) != 0) {
169 ret_val = 1;
170 continue;
171 }
Dees_Troy812660f2012-09-20 09:55:17 -0400172 } else {
173 char empt[50];
174 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000175 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400176 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400177 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400178 } else if (strcmp(command, "restore") == 0) {
179 // Restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500180 DataManager::SetValue("tw_action_text2", gui_parse_text("{@restore}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400181 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000182 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000183 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400184
185 string val = value, restore_folder, restore_partitions;
186 size_t pos = val.find_last_of(" ");
187 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000188 restore_folder = value;
189 partitions[0] = '\0';
190 } else {
191 restore_folder = val.substr(0, pos);
192 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
193 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400194 }
Dees_Troy812660f2012-09-20 09:55:17 -0400195 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000196 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500197 gui_msg(Msg("restoring=Restoring {1}...")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400198
199 if (folder_path[0] != '/') {
200 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000201 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600202 std::vector<PartitionList> Storage_List;
203
204 PartitionManager.Get_Partition_List("storage", &Storage_List);
205 int listSize = Storage_List.size();
206 for (int i = 0; i < listSize; i++) {
207 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
208 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000209 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
210 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600211 if (TWFunc::Path_Exists(backup_folder)) {
212 strcpy(folder_path, backup_folder);
213 break;
214 }
Dees_Troy812660f2012-09-20 09:55:17 -0400215 }
216 }
Dees_Troy812660f2012-09-20 09:55:17 -0400217 } else {
218 if (folder_path[strlen(folder_path) - 1] == '/')
219 strcat(folder_path, ".");
220 else
221 strcat(folder_path, "/.");
222 }
223 if (!TWFunc::Path_Exists(folder_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500224 gui_msg(Msg(msg::kError, "locate_backup_err=Unable to locate backup '{1}'")(folder_path));
Dees_Troy812660f2012-09-20 09:55:17 -0400225 ret_val = 1;
226 continue;
227 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000228 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400229
230 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000232 int is_encrypted = 0;
233 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500234 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400235 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500236 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400237
238 memset(value2, 0, sizeof(value2));
239 strcpy(value2, partitions);
Ethan Yonker74db1572015-10-28 12:44:49 -0500240 gui_msg(Msg("set_restore_opt=Setting restore options: '{1}':")(value2));
Dees_Troy812660f2012-09-20 09:55:17 -0400241 line_len = strlen(value2);
242 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
244 Restore_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500245 gui_msg("system=System");
Dees_Troya13d74f2013-03-24 08:54:55 -0500246 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
247 Restore_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500248 gui_msg("data=Data");
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
250 Restore_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500251 gui_msg("cache=Cache");
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500253 Restore_List += "/recovery;";
254 gui_msg("recovery=Recovery");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000255 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000257 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000259 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
262 Restore_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500263 gui_msg("boot=Boot");
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
265 Restore_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500266 gui_msg("android_secure=Android Secure");
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
268 Restore_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500269 gui_msg("sdext=SD-EXT");
Dees_Troy812660f2012-09-20 09:55:17 -0400270 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000271 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500272 gui_msg("md5_check_skip=MD5 check skip is on");
Dees_Troy812660f2012-09-20 09:55:17 -0400273 }
274 }
275
Dees_Troya13d74f2013-03-24 08:54:55 -0500276 DataManager::SetValue("tw_restore_selected", Restore_List);
277 } else {
278 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400279 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000280 if (is_encrypted) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500281 gui_err("ors_encrypt_restore_err=Unable to use OpenRecoveryScript to restore an encrypted backup.");
Dees_Troy83bd4832013-05-04 12:39:56 +0000282 ret_val = 1;
283 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 ret_val = 1;
285 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500286 gui_msg("done=Done.");
Dees_Troy812660f2012-09-20 09:55:17 -0400287 } else if (strcmp(command, "mount") == 0) {
288 // Mount
Ethan Yonker74db1572015-10-28 12:44:49 -0500289 DataManager::SetValue("tw_action_text2", gui_parse_text("{@mounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400290 if (value[0] != '/') {
291 strcpy(mount, "/");
292 strcat(mount, value);
293 } else
294 strcpy(mount, value);
295 if (PartitionManager.Mount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500296 gui_msg(Msg("mounted=Mounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400297 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
298 // Unmount
Ethan Yonker74db1572015-10-28 12:44:49 -0500299 DataManager::SetValue("tw_action_text2", gui_parse_text("{@unmounting}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400300 if (value[0] != '/') {
301 strcpy(mount, "/");
302 strcat(mount, value);
303 } else
304 strcpy(mount, value);
305 if (PartitionManager.UnMount_By_Path(mount, true))
Ethan Yonker74db1572015-10-28 12:44:49 -0500306 gui_msg(Msg("unmounted=Unounted '{1}'")(mount));
Dees_Troy812660f2012-09-20 09:55:17 -0400307 } else if (strcmp(command, "set") == 0) {
308 // Set value
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500309 size_t len = strlen(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400310 tok = strtok(value, " ");
311 strcpy(value1, tok);
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500312 if (len > strlen(value1) + 1) {
313 char *val2 = value + strlen(value1) + 1;
Ethan Yonker74db1572015-10-28 12:44:49 -0500314 gui_msg(Msg("setting=Setting '{1}' to '{2}'")(value1)(val2));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500315 DataManager::SetValue(value1, val2);
316 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500317 gui_msg(Msg("setting_empty=Setting '{1}' to empty")(value1));
Ethan Yonker6511c4d2015-06-17 16:52:29 -0500318 DataManager::SetValue(value1, "");
319 }
Dees_Troy812660f2012-09-20 09:55:17 -0400320 } else if (strcmp(command, "mkdir") == 0) {
321 // Make directory (recursive)
Ethan Yonker74db1572015-10-28 12:44:49 -0500322 DataManager::SetValue("tw_action_text2", gui_parse_text("{@making_dir1}"));
323 gui_msg(Msg("making_dir2=Making directory: '{1}'")(value));
Dees_Troy812660f2012-09-20 09:55:17 -0400324 if (TWFunc::Recursive_Mkdir(value)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500325 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(value)(strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400326 ret_val = 1;
327 }
328 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500329 if (strlen(value) && strcmp(value, "recovery") == 0)
330 TWFunc::tw_reboot(rb_recovery);
331 else if (strlen(value) && strcmp(value, "poweroff") == 0)
332 TWFunc::tw_reboot(rb_poweroff);
333 else if (strlen(value) && strcmp(value, "bootloader") == 0)
334 TWFunc::tw_reboot(rb_bootloader);
335 else if (strlen(value) && strcmp(value, "download") == 0)
336 TWFunc::tw_reboot(rb_download);
337 else
338 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400339 } else if (strcmp(command, "cmd") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500340 DataManager::SetValue("tw_action_text2", gui_parse_text("{@running_command}"));
Dees_Troy812660f2012-09-20 09:55:17 -0400341 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200342 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400343 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000344 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400345 }
346 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000348 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000349 // ADB Sideload
Ethan Yonker74db1572015-10-28 12:44:49 -0500350 DataManager::SetValue("tw_action_text2", gui_parse_text("{@sideload}"));
Dees_Troy6ed34b72013-01-25 15:01:29 +0000351 install_cmd = -1;
352
353 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600354 string result;
355 pid_t sideload_child_pid;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000356
Ethan Yonker74db1572015-10-28 12:44:49 -0500357 gui_msg("start_sideload=Starting ADB sideload feature...");
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600358 ret_val = apply_from_adb("/", &sideload_child_pid);
359 if (ret_val != 0) {
360 if (ret_val == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -0500361 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 +0000362 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600363 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
364 if (wipe_cache)
365 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000366 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600367 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000368 }
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600369 sideload = 1; // Causes device to go to the home screen afterwards
370 if (sideload_child_pid != 0) {
371 LOGINFO("Signaling child sideload process to exit.\n");
372 struct stat st;
373 // Calling stat() on this magic filename signals the minadbd
374 // subprocess to shut down.
375 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
376 int status;
377 LOGINFO("Waiting for child sideload process to exit.\n");
378 waitpid(sideload_child_pid, &status, 0);
379 }
Dees Troy28a85d22015-04-28 02:03:16 +0000380 property_set("ctl.start", "adbd");
Ethan Yonker74db1572015-10-28 12:44:49 -0500381 gui_msg("done=Done.");
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600382 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0) {
383 ret_val = PartitionManager.Fix_Permissions();
384 if (ret_val != 0)
385 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500386 } else if (strcmp(command, "decrypt") == 0) {
387 if (*value) {
388 ret_val = PartitionManager.Decrypt_Device(value);
389 if (ret_val != 0)
390 ret_val = 1; // failure
391 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500392 gui_err("no_pwd=No password provided.");
Ethan Yonker03a42f62014-08-08 11:03:51 -0500393 ret_val = 1; // failure
394 }
Dees_Troy812660f2012-09-20 09:55:17 -0400395 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000396 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400397 ret_val = 1;
398 }
399 }
400 fclose(fp);
Ethan Yonker74db1572015-10-28 12:44:49 -0500401 gui_msg("done_ors=Done processing script file");
Dees_Troy812660f2012-09-20 09:55:17 -0400402 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500403 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(SCRIPT_FILE_TMP)(strerror(errno)));
Dees_Troy812660f2012-09-20 09:55:17 -0400404 return 1;
405 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000406 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500407 gui_msg("injecttwrp=Injecting TWRP into boot image...");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400408 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
409 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200410 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400411 else {
412 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200413 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400414 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500415 gui_msg("done=Done.");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400416 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000417 if (sideload)
418 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400419 return ret_val;
420}
421
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000422int OpenRecoveryScript::Insert_ORS_Command(string Command) {
xiaolu38c3aa72015-05-26 22:55:18 +0800423 ofstream ORSfile(SCRIPT_FILE_TMP, ios_base::app | ios_base::out);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000424 if (ORSfile.is_open()) {
425 //if (Command.substr(Command.size() - 1, 1) != "\n")
426 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000427 LOGINFO("Inserting '%s'\n", Command.c_str());
xiaolu38c3aa72015-05-26 22:55:18 +0800428 ORSfile << Command.c_str() << endl;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000429 ORSfile.close();
430 return 1;
431 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000432 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000433 return 0;
434}
435
Dees_Troy83a3b122012-10-01 14:16:43 -0400436int OpenRecoveryScript::Install_Command(string Zip) {
437 // Install zip
438 string ret_string;
439 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600440 std::vector<PartitionList> Storage_List;
441 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400442
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600443 if (Zip.substr(0, 1) == "@") {
444 // This is a special file that contains a map of blocks on the data partition
445 Full_Path = Zip.substr(1);
446 if (!PartitionManager.Mount_By_Path(Full_Path, true) || !TWFunc::Path_Exists(Full_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500447 LOGINFO("Unable to install via mapped zip '%s'\n", Full_Path.c_str());
448 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600449 return 1;
450 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500451 LOGINFO("Installing mapped zip file '%s'\n", Full_Path.c_str());
452 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600453 } else if (!TWFunc::Path_Exists(Zip)) {
454 PartitionManager.Mount_All_Storage();
455 PartitionManager.Get_Partition_List("storage", &Storage_List);
456 int listSize = Storage_List.size();
457 for (int i = 0; i < listSize; i++) {
458 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
459 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
460 if (TWFunc::Path_Exists(Full_Path)) {
461 Zip = Full_Path;
462 break;
463 }
464 Full_Path = Zip;
465 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
466 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
467 if (!ret_string.empty()) {
468 Zip = ret_string;
469 break;
470 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600471 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400472 }
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600473 if (!TWFunc::Path_Exists(Zip)) {
474 // zip file doesn't exist
475 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
476 ret_val = 1;
477 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500478 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400479 }
480
Ethan Yonkeraee144c2015-02-11 16:49:19 -0600481 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
Dees_Troy83a3b122012-10-01 14:16:43 -0400482 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500483 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(Zip));
Dees_Troy83a3b122012-10-01 14:16:43 -0400484 ret_val = 1;
485 } else if (wipe_cache)
486 PartitionManager.Wipe_By_Path("/cache");
487
488 return ret_val;
489}
490
Dees_Troy812660f2012-09-20 09:55:17 -0400491string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
492 string Path = TWFunc::Get_Path(Zip);
493 string File = TWFunc::Get_Filename(Zip);
494 string pathCpy = Path;
495 string wholePath;
496 size_t pos = Path.find("/", 1);
497
498 while (pos != string::npos)
499 {
500 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500501 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600502 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400503 if (TWFunc::Path_Exists(wholePath))
504 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500505 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600506 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400507 if (TWFunc::Path_Exists(wholePath))
508 return wholePath;
509
510 pos = Path.find("/", pos + 1);
511 }
512 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400513}
514
515int OpenRecoveryScript::Backup_Command(string Options) {
516 char value1[SCRIPT_COMMAND_SIZE];
517 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400519
520 strcpy(value1, Options.c_str());
521
Dees_Troy6ed34b72013-01-25 15:01:29 +0000522 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
523 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400524
Ethan Yonker74db1572015-10-28 12:44:49 -0500525 gui_msg("select_backup_opt=Setting backup options:");
Dees_Troy83a3b122012-10-01 14:16:43 -0400526 line_len = Options.size();
527 for (i=0; i<line_len; i++) {
528 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 Backup_List += "/system;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500530 gui_msg("system=System");
Dees_Troy83a3b122012-10-01 14:16:43 -0400531 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 Backup_List += "/data;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500533 gui_msg("data=Data");
Dees_Troy83a3b122012-10-01 14:16:43 -0400534 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 Backup_List += "/cache;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500536 gui_msg("cache=Cache");
Dees_Troy83a3b122012-10-01 14:16:43 -0400537 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 Backup_List += "/recovery;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500539 gui_msg("recovery=Recovery");
Dees_Troy83a3b122012-10-01 14:16:43 -0400540 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400542 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400544 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000545 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400546 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500547 Backup_List += "/boot;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500548 gui_msg("boot=Boot");
Dees_Troy83a3b122012-10-01 14:16:43 -0400549 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 Backup_List += "/and-sec;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500551 gui_msg("android_secure=Android Secure");
Dees_Troy83a3b122012-10-01 14:16:43 -0400552 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500553 Backup_List += "/sd-ext;";
Ethan Yonker74db1572015-10-28 12:44:49 -0500554 gui_msg("sdext=SD-EXT");
Dees_Troy83a3b122012-10-01 14:16:43 -0400555 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000556 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500557 gui_msg("compression_on=Compression is on");
Dees_Troy83a3b122012-10-01 14:16:43 -0400558 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000559 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500560 gui_msg("md5_off=MD5 Generation is off");
Dees_Troy83a3b122012-10-01 14:16:43 -0400561 }
562 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 DataManager::SetValue("tw_backup_list", Backup_List);
Dees_Troy83a3b122012-10-01 14:16:43 -0400564 if (!PartitionManager.Run_Backup()) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500565 gui_err("backup_fail=Backup Failed");
Dees_Troy83a3b122012-10-01 14:16:43 -0400566 return 1;
567 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500568 gui_msg("backup_complete=Backup complete!");
Dees_Troy83a3b122012-10-01 14:16:43 -0400569 return 0;
570}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000571
572void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
573 DataManager::SetValue("tw_back", "main");
574 DataManager::SetValue("tw_action", "openrecoveryscript");
575 DataManager::SetValue("tw_has_action2", "0");
576 DataManager::SetValue("tw_action2", "");
577 DataManager::SetValue("tw_action2_param", "");
Ethan Yonker89583ef2015-08-26 09:01:59 -0500578#ifdef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -0500579 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
580 DataManager::SetValue("tw_complete_text1", gui_lookup("recovery_commands_complete", "Recovery Commands Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500581#else
Ethan Yonker74db1572015-10-28 12:44:49 -0500582 DataManager::SetValue("tw_action_text1", gui_lookup("running_ors", "Running OpenRecoveryScript"));
583 DataManager::SetValue("tw_complete_text1", gui_lookup("ors_complete", "OpenRecoveryScript Complete"));
Ethan Yonker89583ef2015-08-26 09:01:59 -0500584#endif
585 DataManager::SetValue("tw_action_text2", "");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000586 DataManager::SetValue("tw_has_cancel", 0);
587 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600588 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000589 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000590 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000591}