blob: d28f7620269d012011169461edef35ffb8634a2c [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"
Dees_Troy812660f2012-09-20 09:55:17 -040043extern "C" {
Dees_Troy01b6d0c2013-01-22 01:41:09 +000044 #include "twinstall.h"
45 #include "gui/gui.h"
Ethan Yonkerd83587d2015-01-03 16:54:18 -060046 #include "cutils/properties.h"
Dees_Troy01b6d0c2013-01-22 01:41:09 +000047 int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy812660f2012-09-20 09:55:17 -040048}
49
Dees_Troy812660f2012-09-20 09:55:17 -040050#define SCRIPT_COMMAND_SIZE 512
51
52int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040053 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000054 LOGERR("Unable to mount /cache for OpenRecoveryScript support.\n");
Dees_Troy812660f2012-09-20 09:55:17 -040055 return 0;
56 }
57 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000058 LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040059 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050060 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040061 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050062 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040063 return 1;
64 }
65 return 0;
66}
67
Ethan Yonker03a42f62014-08-08 11:03:51 -050068int OpenRecoveryScript::copy_script_file(string filename) {
69 if (TWFunc::Path_Exists(filename)) {
70 LOGINFO("Script file found: '%s'\n", filename.c_str());
71 if (filename == SCRIPT_FILE_TMP)
72 return 1; // file is already in the right place
73 // Copy script file to /tmp
74 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
75 // Delete the old file
76 unlink(filename.c_str());
77 return 1;
78 }
79 return 0;
80}
81
Dees_Troy812660f2012-09-20 09:55:17 -040082int OpenRecoveryScript::run_script_file(void) {
83 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000084 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040085 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
86 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
87 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
88 char *val_start, *tok;
89
90 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000091 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000092 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040093 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
94 cindex = 0;
95 line_len = strlen(script_line);
96 if (line_len < 2)
97 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +000098 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -040099 for (i=0; i<line_len; i++) {
100 if ((int)script_line[i] == 32) {
101 cindex = i;
102 i = line_len;
103 }
104 }
105 memset(command, 0, sizeof(command));
106 memset(value, 0, sizeof(value));
107 if ((int)script_line[line_len - 1] == 10)
108 remove_nl = 2;
109 else
110 remove_nl = 1;
111 if (cindex != 0) {
112 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500113 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400114 val_start = script_line;
115 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400116 if ((int) *val_start == 32)
117 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400118 if ((int) *val_start == 51)
119 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400120 if ((int) *val_start == 32)
121 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400122 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000123 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400124 } else {
125 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000126 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400127 }
128 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400129 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000130 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000131 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400132 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400133 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400134 } else if (strcmp(command, "wipe") == 0) {
135 // Wipe
136 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 gui_print("-- Wiping Cache Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400138 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 gui_print("-- Cache Partition Wipe Complete!\n");
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_Troy2673cec2013-04-02 20:22:16 +0000141 gui_print("-- Wiping Dalvik Cache...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400142 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +0000143 gui_print("-- Dalvik Cache Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400144 } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 gui_print("-- Wiping Data Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400146 PartitionManager.Factory_Reset();
Dees_Troy2673cec2013-04-02 20:22:16 +0000147 gui_print("-- Data Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400148 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000149 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400150 ret_val = 1;
151 }
152 } else if (strcmp(command, "backup") == 0) {
153 // Backup
Dees_Troy6ed34b72013-01-25 15:01:29 +0000154 DataManager::SetValue("tw_action_text2", "Backing Up");
Dees_Troy812660f2012-09-20 09:55:17 -0400155 tok = strtok(value, " ");
156 strcpy(value1, tok);
157 tok = strtok(NULL, " ");
158 if (tok != NULL) {
159 memset(value2, 0, sizeof(value2));
160 strcpy(value2, tok);
161 line_len = strlen(tok);
162 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
163 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
164 remove_nl = 2;
165 else
166 remove_nl = 1;
167 } else
168 remove_nl = 0;
169 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000170 DataManager::SetValue(TW_BACKUP_NAME, value2);
Dees_Troy2673cec2013-04-02 20:22:16 +0000171 gui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400172 if (PartitionManager.Check_Backup_Name(true) != 0) {
173 ret_val = 1;
174 continue;
175 }
Dees_Troy812660f2012-09-20 09:55:17 -0400176 } else {
177 char empt[50];
178 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000179 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400180 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400181 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400182 } else if (strcmp(command, "restore") == 0) {
183 // Restore
Dees_Troy6ed34b72013-01-25 15:01:29 +0000184 DataManager::SetValue("tw_action_text2", "Restoring");
Dees_Troy812660f2012-09-20 09:55:17 -0400185 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000186 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000187 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400188
189 string val = value, restore_folder, restore_partitions;
190 size_t pos = val.find_last_of(" ");
191 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000192 restore_folder = value;
193 partitions[0] = '\0';
194 } else {
195 restore_folder = val.substr(0, pos);
196 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
197 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400198 }
Dees_Troy812660f2012-09-20 09:55:17 -0400199 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
201 gui_print("Restoring '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400202
203 if (folder_path[0] != '/') {
204 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000205 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600206 std::vector<PartitionList> Storage_List;
207
208 PartitionManager.Get_Partition_List("storage", &Storage_List);
209 int listSize = Storage_List.size();
210 for (int i = 0; i < listSize; i++) {
211 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
212 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000213 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
214 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600215 if (TWFunc::Path_Exists(backup_folder)) {
216 strcpy(folder_path, backup_folder);
217 break;
218 }
Dees_Troy812660f2012-09-20 09:55:17 -0400219 }
220 }
Dees_Troy812660f2012-09-20 09:55:17 -0400221 } else {
222 if (folder_path[strlen(folder_path) - 1] == '/')
223 strcat(folder_path, ".");
224 else
225 strcat(folder_path, "/.");
226 }
227 if (!TWFunc::Path_Exists(folder_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 gui_print("Unable to locate backup '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400229 ret_val = 1;
230 continue;
231 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000232 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400233
234 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500235 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000236 int is_encrypted = 0;
237 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400239 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500240 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400241
242 memset(value2, 0, sizeof(value2));
243 strcpy(value2, partitions);
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 gui_print("Setting restore options: '%s':\n", value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400245 line_len = strlen(value2);
246 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
248 Restore_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 gui_print("System\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
251 Restore_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 gui_print("Data\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
254 Restore_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 gui_print("Cache\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 gui_print("Recovery -- Not allowed to restore recovery\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000258 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000260 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000262 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
265 Restore_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000266 gui_print("Boot\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
268 Restore_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000269 gui_print("Android Secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
271 Restore_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 gui_print("SD-Ext\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400273 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000274 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000275 gui_print("MD5 check skip is on\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400276 }
277 }
278
Dees_Troya13d74f2013-03-24 08:54:55 -0500279 DataManager::SetValue("tw_restore_selected", Restore_List);
280 } else {
281 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400282 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000283 if (is_encrypted) {
284 LOGERR("Unable to use OpenRecoveryScript to restore an encrypted backup.\n");
285 ret_val = 1;
286 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 ret_val = 1;
288 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 gui_print("Restore complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400290 } else if (strcmp(command, "mount") == 0) {
291 // Mount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000292 DataManager::SetValue("tw_action_text2", "Mounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400293 if (value[0] != '/') {
294 strcpy(mount, "/");
295 strcat(mount, value);
296 } else
297 strcpy(mount, value);
298 if (PartitionManager.Mount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000299 gui_print("Mounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400300 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
301 // Unmount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000302 DataManager::SetValue("tw_action_text2", "Unmounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400303 if (value[0] != '/') {
304 strcpy(mount, "/");
305 strcat(mount, value);
306 } else
307 strcpy(mount, value);
308 if (PartitionManager.UnMount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 gui_print("Unmounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400310 } else if (strcmp(command, "set") == 0) {
311 // Set value
312 tok = strtok(value, " ");
313 strcpy(value1, tok);
314 tok = strtok(NULL, " ");
315 strcpy(value2, tok);
Dees_Troy2673cec2013-04-02 20:22:16 +0000316 gui_print("Setting '%s' to '%s'\n", value1, value2);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000317 DataManager::SetValue(value1, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400318 } else if (strcmp(command, "mkdir") == 0) {
319 // Make directory (recursive)
Dees_Troy6ed34b72013-01-25 15:01:29 +0000320 DataManager::SetValue("tw_action_text2", "Making Directory");
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 gui_print("Making directory (recursive): '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400322 if (TWFunc::Recursive_Mkdir(value)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGERR("Unable to create folder: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400324 ret_val = 1;
325 }
326 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500327 if (strlen(value) && strcmp(value, "recovery") == 0)
328 TWFunc::tw_reboot(rb_recovery);
329 else if (strlen(value) && strcmp(value, "poweroff") == 0)
330 TWFunc::tw_reboot(rb_poweroff);
331 else if (strlen(value) && strcmp(value, "bootloader") == 0)
332 TWFunc::tw_reboot(rb_bootloader);
333 else if (strlen(value) && strcmp(value, "download") == 0)
334 TWFunc::tw_reboot(rb_download);
335 else
336 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400337 } else if (strcmp(command, "cmd") == 0) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000338 DataManager::SetValue("tw_action_text2", "Running Command");
Dees_Troy812660f2012-09-20 09:55:17 -0400339 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200340 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400341 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000342 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400343 }
344 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000345 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000346 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000347 // ADB Sideload
Dees_Troy6ed34b72013-01-25 15:01:29 +0000348 DataManager::SetValue("tw_action_text2", "ADB Sideload");
349 install_cmd = -1;
350
351 int wipe_cache = 0;
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600352 string result;
353 pid_t sideload_child_pid;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000354
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600355 gui_print("Starting ADB sideload feature...\n");
356 ret_val = apply_from_adb("/", &sideload_child_pid);
357 if (ret_val != 0) {
358 if (ret_val == -2)
359 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000360 ret_val = 1; // failure
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600361 } else if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
362 if (wipe_cache)
363 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000364 } else {
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600365 ret_val = 1; // failure
Dees_Troy4fc00242013-01-17 19:22:12 +0000366 }
Ethan Yonkerd83587d2015-01-03 16:54:18 -0600367 sideload = 1; // Causes device to go to the home screen afterwards
368 if (sideload_child_pid != 0) {
369 LOGINFO("Signaling child sideload process to exit.\n");
370 struct stat st;
371 // Calling stat() on this magic filename signals the minadbd
372 // subprocess to shut down.
373 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
374 int status;
375 LOGINFO("Waiting for child sideload process to exit.\n");
376 waitpid(sideload_child_pid, &status, 0);
377 }
378 gui_print("Sideload finished.\n");
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600379 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0) {
380 ret_val = PartitionManager.Fix_Permissions();
381 if (ret_val != 0)
382 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500383 } else if (strcmp(command, "decrypt") == 0) {
384 if (*value) {
385 ret_val = PartitionManager.Decrypt_Device(value);
386 if (ret_val != 0)
387 ret_val = 1; // failure
388 } else {
389 LOGERR("No password provided.\n");
390 ret_val = 1; // failure
391 }
Dees_Troy812660f2012-09-20 09:55:17 -0400392 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400394 ret_val = 1;
395 }
396 }
397 fclose(fp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000398 gui_print("Done processing script file\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400399 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000400 LOGERR("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
Dees_Troy812660f2012-09-20 09:55:17 -0400401 return 1;
402 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000403 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000404 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400405 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
406 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200407 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400408 else {
409 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 +0200410 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400411 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000412 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400413 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000414 if (sideload)
415 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400416 return ret_val;
417}
418
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000419int OpenRecoveryScript::Insert_ORS_Command(string Command) {
420 ofstream ORSfile(SCRIPT_FILE_TMP);
421 if (ORSfile.is_open()) {
422 //if (Command.substr(Command.size() - 1, 1) != "\n")
423 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000424 LOGINFO("Inserting '%s'\n", Command.c_str());
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000425 ORSfile << Command.c_str();
426 ORSfile.close();
427 return 1;
428 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000429 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000430 return 0;
431}
432
Dees_Troy83a3b122012-10-01 14:16:43 -0400433int OpenRecoveryScript::Install_Command(string Zip) {
434 // Install zip
435 string ret_string;
436 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600437 std::vector<PartitionList> Storage_List;
438 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400439
440 PartitionManager.Mount_All_Storage();
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600441 PartitionManager.Get_Partition_List("storage", &Storage_List);
442 int listSize = Storage_List.size();
443 for (int i = 0; i < listSize; i++) {
444 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
445 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
446 if (TWFunc::Path_Exists(Full_Path)) {
447 Zip = Full_Path;
448 break;
Dees_Troy83a3b122012-10-01 14:16:43 -0400449 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600450 Full_Path = Zip;
451 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
452 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
453 if (!ret_string.empty()) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400454 Zip = ret_string;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600455 break;
456 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400457 }
458 }
459
460 if (!TWFunc::Path_Exists(Zip)) {
461 // zip file doesn't exist
Dees_Troy2673cec2013-04-02 20:22:16 +0000462 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400463 ret_val = 1;
464 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000465 gui_print("Installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400466 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
467 }
468 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000469 LOGERR("Error installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400470 ret_val = 1;
471 } else if (wipe_cache)
472 PartitionManager.Wipe_By_Path("/cache");
473
474 return ret_val;
475}
476
Dees_Troy812660f2012-09-20 09:55:17 -0400477string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
478 string Path = TWFunc::Get_Path(Zip);
479 string File = TWFunc::Get_Filename(Zip);
480 string pathCpy = Path;
481 string wholePath;
482 size_t pos = Path.find("/", 1);
483
484 while (pos != string::npos)
485 {
486 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500487 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600488 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400489 if (TWFunc::Path_Exists(wholePath))
490 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500491 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600492 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400493 if (TWFunc::Path_Exists(wholePath))
494 return wholePath;
495
496 pos = Path.find("/", pos + 1);
497 }
498 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400499}
500
501int OpenRecoveryScript::Backup_Command(string Options) {
502 char value1[SCRIPT_COMMAND_SIZE];
503 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400505
506 strcpy(value1, Options.c_str());
507
Dees_Troy6ed34b72013-01-25 15:01:29 +0000508 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
509 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400510
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 gui_print("Setting backup options:\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400512 line_len = Options.size();
513 for (i=0; i<line_len; i++) {
514 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 Backup_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000516 gui_print("System\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400517 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Backup_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000519 gui_print("Data\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400520 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 Backup_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 gui_print("Cache\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400523 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 Backup_List += "/recovery;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 gui_print("Recovery\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400526 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000527 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400528 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400530 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400532 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 Backup_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000534 gui_print("Boot\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400535 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500536 Backup_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000537 gui_print("Android Secure\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400538 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Backup_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000540 gui_print("SD-Ext\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400541 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000542 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 gui_print("Compression is on\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400544 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000545 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000546 gui_print("MD5 Generation is off\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400547 }
548 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 DataManager::SetValue("tw_backup_list", Backup_List);
Dees_Troy83a3b122012-10-01 14:16:43 -0400550 if (!PartitionManager.Run_Backup()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 LOGERR("Backup failed!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400552 return 1;
553 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 gui_print("Backup complete!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400555 return 0;
556}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000557
558void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
559 DataManager::SetValue("tw_back", "main");
560 DataManager::SetValue("tw_action", "openrecoveryscript");
561 DataManager::SetValue("tw_has_action2", "0");
562 DataManager::SetValue("tw_action2", "");
563 DataManager::SetValue("tw_action2_param", "");
564 DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript");
565 DataManager::SetValue("tw_action_text2", "");
566 DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete");
567 DataManager::SetValue("tw_has_cancel", 0);
568 DataManager::SetValue("tw_show_reboot", 0);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600569 if (gui_startPage("action_page", 0, 1) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000570 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000571 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000572}