blob: 0b71a42ebe44d2bb821e200f49644fc3c9dfdbf5 [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>
Dees_Troy812660f2012-09-20 09:55:17 -040031
32#include "twrp-functions.hpp"
33#include "partitions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy812660f2012-09-20 09:55:17 -040035#include "openrecoveryscript.hpp"
36#include "variables.h"
Dees_Troy4fc00242013-01-17 19:22:12 +000037#include "adb_install.h"
Dees_Troy6ed34b72013-01-25 15:01:29 +000038#include "data.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040039extern "C" {
Dees_Troy01b6d0c2013-01-22 01:41:09 +000040 #include "twinstall.h"
41 #include "gui/gui.h"
42 int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy812660f2012-09-20 09:55:17 -040043}
44
Dees_Troy812660f2012-09-20 09:55:17 -040045#define SCRIPT_COMMAND_SIZE 512
46
47int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040048 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000049 LOGERR("Unable to mount /cache for OpenRecoveryScript support.\n");
Dees_Troy812660f2012-09-20 09:55:17 -040050 return 0;
51 }
52 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000053 LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040054 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050055 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040056 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040058 return 1;
59 }
60 return 0;
61}
62
Ethan Yonker03a42f62014-08-08 11:03:51 -050063int OpenRecoveryScript::copy_script_file(string filename) {
64 if (TWFunc::Path_Exists(filename)) {
65 LOGINFO("Script file found: '%s'\n", filename.c_str());
66 if (filename == SCRIPT_FILE_TMP)
67 return 1; // file is already in the right place
68 // Copy script file to /tmp
69 TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
70 // Delete the old file
71 unlink(filename.c_str());
72 return 1;
73 }
74 return 0;
75}
76
Dees_Troy812660f2012-09-20 09:55:17 -040077int OpenRecoveryScript::run_script_file(void) {
78 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000079 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040080 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
81 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
82 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
83 char *val_start, *tok;
84
85 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000086 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000087 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040088 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
89 cindex = 0;
90 line_len = strlen(script_line);
91 if (line_len < 2)
92 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +000093 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -040094 for (i=0; i<line_len; i++) {
95 if ((int)script_line[i] == 32) {
96 cindex = i;
97 i = line_len;
98 }
99 }
100 memset(command, 0, sizeof(command));
101 memset(value, 0, sizeof(value));
102 if ((int)script_line[line_len - 1] == 10)
103 remove_nl = 2;
104 else
105 remove_nl = 1;
106 if (cindex != 0) {
107 strncpy(command, script_line, cindex);
Matt Mowere96c3c32014-09-19 12:14:37 -0500108 LOGINFO("command is: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400109 val_start = script_line;
110 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400111 if ((int) *val_start == 32)
112 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400113 if ((int) *val_start == 51)
114 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400115 if ((int) *val_start == 32)
116 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400117 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400119 } else {
120 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000121 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400122 }
123 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400124 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000125 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000126 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400127 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400128 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400129 } else if (strcmp(command, "wipe") == 0) {
130 // Wipe
131 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 gui_print("-- Wiping Cache Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400133 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 gui_print("-- Cache Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400135 } 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 +0000136 gui_print("-- Wiping Dalvik Cache...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400137 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +0000138 gui_print("-- Dalvik Cache Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400139 } 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 +0000140 gui_print("-- Wiping Data Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400141 PartitionManager.Factory_Reset();
Dees_Troy2673cec2013-04-02 20:22:16 +0000142 gui_print("-- Data Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400143 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000144 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400145 ret_val = 1;
146 }
147 } else if (strcmp(command, "backup") == 0) {
148 // Backup
Dees_Troy6ed34b72013-01-25 15:01:29 +0000149 DataManager::SetValue("tw_action_text2", "Backing Up");
Dees_Troy812660f2012-09-20 09:55:17 -0400150 tok = strtok(value, " ");
151 strcpy(value1, tok);
152 tok = strtok(NULL, " ");
153 if (tok != NULL) {
154 memset(value2, 0, sizeof(value2));
155 strcpy(value2, tok);
156 line_len = strlen(tok);
157 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
158 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
159 remove_nl = 2;
160 else
161 remove_nl = 1;
162 } else
163 remove_nl = 0;
164 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000165 DataManager::SetValue(TW_BACKUP_NAME, value2);
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 gui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400167 if (PartitionManager.Check_Backup_Name(true) != 0) {
168 ret_val = 1;
169 continue;
170 }
Dees_Troy812660f2012-09-20 09:55:17 -0400171 } else {
172 char empt[50];
173 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000174 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400175 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400176 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400177 } else if (strcmp(command, "restore") == 0) {
178 // Restore
Dees_Troy6ed34b72013-01-25 15:01:29 +0000179 DataManager::SetValue("tw_action_text2", "Restoring");
Dees_Troy812660f2012-09-20 09:55:17 -0400180 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000181 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000182 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400183
184 string val = value, restore_folder, restore_partitions;
185 size_t pos = val.find_last_of(" ");
186 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000187 restore_folder = value;
188 partitions[0] = '\0';
189 } else {
190 restore_folder = val.substr(0, pos);
191 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
192 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400193 }
Dees_Troy812660f2012-09-20 09:55:17 -0400194 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
196 gui_print("Restoring '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400197
198 if (folder_path[0] != '/') {
199 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000200 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600201 std::vector<PartitionList> Storage_List;
202
203 PartitionManager.Get_Partition_List("storage", &Storage_List);
204 int listSize = Storage_List.size();
205 for (int i = 0; i < listSize; i++) {
206 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
207 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000208 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
209 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600210 if (TWFunc::Path_Exists(backup_folder)) {
211 strcpy(folder_path, backup_folder);
212 break;
213 }
Dees_Troy812660f2012-09-20 09:55:17 -0400214 }
215 }
Dees_Troy812660f2012-09-20 09:55:17 -0400216 } else {
217 if (folder_path[strlen(folder_path) - 1] == '/')
218 strcat(folder_path, ".");
219 else
220 strcat(folder_path, "/.");
221 }
222 if (!TWFunc::Path_Exists(folder_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000223 gui_print("Unable to locate backup '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400224 ret_val = 1;
225 continue;
226 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000227 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400228
229 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500230 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000231 int is_encrypted = 0;
232 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500233 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400234 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500235 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400236
237 memset(value2, 0, sizeof(value2));
238 strcpy(value2, partitions);
Dees_Troy2673cec2013-04-02 20:22:16 +0000239 gui_print("Setting restore options: '%s':\n", value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400240 line_len = strlen(value2);
241 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500242 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
243 Restore_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 gui_print("System\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
246 Restore_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 gui_print("Data\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
249 Restore_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000250 gui_print("Cache\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 gui_print("Recovery -- Not allowed to restore recovery\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000253 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000255 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000257 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500259 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
260 Restore_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 gui_print("Boot\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
263 Restore_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 gui_print("Android Secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
266 Restore_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000267 gui_print("SD-Ext\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400268 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000269 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000270 gui_print("MD5 check skip is on\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400271 }
272 }
273
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 DataManager::SetValue("tw_restore_selected", Restore_List);
275 } else {
276 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400277 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000278 if (is_encrypted) {
279 LOGERR("Unable to use OpenRecoveryScript to restore an encrypted backup.\n");
280 ret_val = 1;
281 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 ret_val = 1;
283 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000284 gui_print("Restore complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400285 } else if (strcmp(command, "mount") == 0) {
286 // Mount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000287 DataManager::SetValue("tw_action_text2", "Mounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400288 if (value[0] != '/') {
289 strcpy(mount, "/");
290 strcat(mount, value);
291 } else
292 strcpy(mount, value);
293 if (PartitionManager.Mount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 gui_print("Mounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400295 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
296 // Unmount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000297 DataManager::SetValue("tw_action_text2", "Unmounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400298 if (value[0] != '/') {
299 strcpy(mount, "/");
300 strcat(mount, value);
301 } else
302 strcpy(mount, value);
303 if (PartitionManager.UnMount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 gui_print("Unmounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400305 } else if (strcmp(command, "set") == 0) {
306 // Set value
307 tok = strtok(value, " ");
308 strcpy(value1, tok);
309 tok = strtok(NULL, " ");
310 strcpy(value2, tok);
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 gui_print("Setting '%s' to '%s'\n", value1, value2);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000312 DataManager::SetValue(value1, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400313 } else if (strcmp(command, "mkdir") == 0) {
314 // Make directory (recursive)
Dees_Troy6ed34b72013-01-25 15:01:29 +0000315 DataManager::SetValue("tw_action_text2", "Making Directory");
Dees_Troy2673cec2013-04-02 20:22:16 +0000316 gui_print("Making directory (recursive): '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400317 if (TWFunc::Recursive_Mkdir(value)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000318 LOGERR("Unable to create folder: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400319 ret_val = 1;
320 }
321 } else if (strcmp(command, "reboot") == 0) {
Ethan Yonker03a42f62014-08-08 11:03:51 -0500322 if (strlen(value) && strcmp(value, "recovery") == 0)
323 TWFunc::tw_reboot(rb_recovery);
324 else if (strlen(value) && strcmp(value, "poweroff") == 0)
325 TWFunc::tw_reboot(rb_poweroff);
326 else if (strlen(value) && strcmp(value, "bootloader") == 0)
327 TWFunc::tw_reboot(rb_bootloader);
328 else if (strlen(value) && strcmp(value, "download") == 0)
329 TWFunc::tw_reboot(rb_download);
330 else
331 TWFunc::tw_reboot(rb_system);
Dees_Troy812660f2012-09-20 09:55:17 -0400332 } else if (strcmp(command, "cmd") == 0) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000333 DataManager::SetValue("tw_action_text2", "Running Command");
Dees_Troy812660f2012-09-20 09:55:17 -0400334 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200335 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400336 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400338 }
339 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000341 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000342 // ADB Sideload
Dees_Troy6ed34b72013-01-25 15:01:29 +0000343 DataManager::SetValue("tw_action_text2", "ADB Sideload");
344 install_cmd = -1;
345
346 int wipe_cache = 0;
347 string result, Sideload_File;
348
349 if (!PartitionManager.Mount_Current_Storage(true)) {
350 ret_val = 1; // failure
351 } else {
352 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
353 if (TWFunc::Path_Exists(Sideload_File)) {
354 unlink(Sideload_File.c_str());
355 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000356 gui_print("Starting ADB sideload feature...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000357 DataManager::SetValue("tw_has_cancel", 1);
358 DataManager::SetValue("tw_cancel_action", "adbsideloadcancel");
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 ret_val = apply_from_adb(Sideload_File.c_str());
Dees_Troy6ed34b72013-01-25 15:01:29 +0000360 DataManager::SetValue("tw_has_cancel", 0);
361 if (ret_val != 0)
362 ret_val = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +0000363 else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
364 if (wipe_cache)
365 PartitionManager.Wipe_By_Path("/cache");
366 } else {
367 ret_val = 1; // failure
368 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000369 sideload = 1; // Causes device to go to the home screen afterwards
Dees_Troy2673cec2013-04-02 20:22:16 +0000370 gui_print("Sideload finished.\n");
Dees_Troy4fc00242013-01-17 19:22:12 +0000371 }
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600372 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0) {
373 ret_val = PartitionManager.Fix_Permissions();
374 if (ret_val != 0)
375 ret_val = 1; // failure
Ethan Yonker03a42f62014-08-08 11:03:51 -0500376 } else if (strcmp(command, "decrypt") == 0) {
377 if (*value) {
378 ret_val = PartitionManager.Decrypt_Device(value);
379 if (ret_val != 0)
380 ret_val = 1; // failure
381 } else {
382 LOGERR("No password provided.\n");
383 ret_val = 1; // failure
384 }
Dees_Troy812660f2012-09-20 09:55:17 -0400385 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000386 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400387 ret_val = 1;
388 }
389 }
390 fclose(fp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000391 gui_print("Done processing script file\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400392 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGERR("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
Dees_Troy812660f2012-09-20 09:55:17 -0400394 return 1;
395 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000396 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000397 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400398 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
399 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200400 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400401 else {
402 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 +0200403 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400404 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000405 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400406 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000407 if (sideload)
408 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400409 return ret_val;
410}
411
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000412int OpenRecoveryScript::Insert_ORS_Command(string Command) {
413 ofstream ORSfile(SCRIPT_FILE_TMP);
414 if (ORSfile.is_open()) {
415 //if (Command.substr(Command.size() - 1, 1) != "\n")
416 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000417 LOGINFO("Inserting '%s'\n", Command.c_str());
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000418 ORSfile << Command.c_str();
419 ORSfile.close();
420 return 1;
421 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000422 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000423 return 0;
424}
425
Dees_Troy83a3b122012-10-01 14:16:43 -0400426int OpenRecoveryScript::Install_Command(string Zip) {
427 // Install zip
428 string ret_string;
429 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600430 std::vector<PartitionList> Storage_List;
431 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400432
433 PartitionManager.Mount_All_Storage();
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600434 PartitionManager.Get_Partition_List("storage", &Storage_List);
435 int listSize = Storage_List.size();
436 for (int i = 0; i < listSize; i++) {
437 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
438 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
439 if (TWFunc::Path_Exists(Full_Path)) {
440 Zip = Full_Path;
441 break;
Dees_Troy83a3b122012-10-01 14:16:43 -0400442 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600443 Full_Path = Zip;
444 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
445 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
446 if (!ret_string.empty()) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400447 Zip = ret_string;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600448 break;
449 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400450 }
451 }
452
453 if (!TWFunc::Path_Exists(Zip)) {
454 // zip file doesn't exist
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400456 ret_val = 1;
457 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 gui_print("Installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400459 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
460 }
461 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000462 LOGERR("Error installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400463 ret_val = 1;
464 } else if (wipe_cache)
465 PartitionManager.Wipe_By_Path("/cache");
466
467 return ret_val;
468}
469
Dees_Troy812660f2012-09-20 09:55:17 -0400470string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
471 string Path = TWFunc::Get_Path(Zip);
472 string File = TWFunc::Get_Filename(Zip);
473 string pathCpy = Path;
474 string wholePath;
475 size_t pos = Path.find("/", 1);
476
477 while (pos != string::npos)
478 {
479 pathCpy = Path.substr(pos, Path.size() - pos);
Matt Mowere96c3c32014-09-19 12:14:37 -0500480 wholePath = pathCpy + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600481 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400482 if (TWFunc::Path_Exists(wholePath))
483 return wholePath;
Matt Mowere96c3c32014-09-19 12:14:37 -0500484 wholePath = Storage_Root + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600485 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400486 if (TWFunc::Path_Exists(wholePath))
487 return wholePath;
488
489 pos = Path.find("/", pos + 1);
490 }
491 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400492}
493
494int OpenRecoveryScript::Backup_Command(string Options) {
495 char value1[SCRIPT_COMMAND_SIZE];
496 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400498
499 strcpy(value1, Options.c_str());
500
Dees_Troy6ed34b72013-01-25 15:01:29 +0000501 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
502 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400503
Dees_Troy2673cec2013-04-02 20:22:16 +0000504 gui_print("Setting backup options:\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400505 line_len = Options.size();
506 for (i=0; i<line_len; i++) {
507 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 Backup_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 gui_print("System\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400510 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 Backup_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 gui_print("Data\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400513 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 Backup_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 gui_print("Cache\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400516 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Backup_List += "/recovery;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000518 gui_print("Recovery\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400519 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000520 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400521 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400523 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400525 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 Backup_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000527 gui_print("Boot\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400528 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 Backup_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 gui_print("Android Secure\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400531 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 Backup_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000533 gui_print("SD-Ext\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400534 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000535 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000536 gui_print("Compression is on\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400537 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000538 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 gui_print("MD5 Generation is off\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400540 }
541 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500542 DataManager::SetValue("tw_backup_list", Backup_List);
Dees_Troy83a3b122012-10-01 14:16:43 -0400543 if (!PartitionManager.Run_Backup()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000544 LOGERR("Backup failed!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400545 return 1;
546 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000547 gui_print("Backup complete!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400548 return 0;
549}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000550
551void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
552 DataManager::SetValue("tw_back", "main");
553 DataManager::SetValue("tw_action", "openrecoveryscript");
554 DataManager::SetValue("tw_has_action2", "0");
555 DataManager::SetValue("tw_action2", "");
556 DataManager::SetValue("tw_action2_param", "");
557 DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript");
558 DataManager::SetValue("tw_action_text2", "");
559 DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete");
560 DataManager::SetValue("tw_has_cancel", 0);
561 DataManager::SetValue("tw_show_reboot", 0);
562 if (gui_startPage("action_page") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000564 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000565}