blob: 0147027418783b3a918ff7cdc0fdfeee9b95127b [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
63int OpenRecoveryScript::run_script_file(void) {
64 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000065 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040066 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
67 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
68 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
69 char *val_start, *tok;
70
71 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000072 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000073 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040074 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
75 cindex = 0;
76 line_len = strlen(script_line);
77 if (line_len < 2)
78 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +000079 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -040080 for (i=0; i<line_len; i++) {
81 if ((int)script_line[i] == 32) {
82 cindex = i;
83 i = line_len;
84 }
85 }
86 memset(command, 0, sizeof(command));
87 memset(value, 0, sizeof(value));
88 if ((int)script_line[line_len - 1] == 10)
89 remove_nl = 2;
90 else
91 remove_nl = 1;
92 if (cindex != 0) {
93 strncpy(command, script_line, cindex);
Dees_Troy2673cec2013-04-02 20:22:16 +000094 LOGINFO("command is: '%s' and ", command);
Dees_Troy812660f2012-09-20 09:55:17 -040095 val_start = script_line;
96 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -040097 if ((int) *val_start == 32)
98 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -040099 if ((int) *val_start == 51)
100 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400101 if ((int) *val_start == 32)
102 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400103 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400105 } else {
106 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000107 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400108 }
109 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400110 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000111 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000112 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400113 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400114 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400115 } else if (strcmp(command, "wipe") == 0) {
116 // Wipe
117 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 gui_print("-- Wiping Cache Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400119 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy2673cec2013-04-02 20:22:16 +0000120 gui_print("-- Cache Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400121 } 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 +0000122 gui_print("-- Wiping Dalvik Cache...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400123 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 gui_print("-- Dalvik Cache Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400125 } 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 +0000126 gui_print("-- Wiping Data Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400127 PartitionManager.Factory_Reset();
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 gui_print("-- Data Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400129 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000130 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400131 ret_val = 1;
132 }
133 } else if (strcmp(command, "backup") == 0) {
134 // Backup
Dees_Troy6ed34b72013-01-25 15:01:29 +0000135 DataManager::SetValue("tw_action_text2", "Backing Up");
Dees_Troy812660f2012-09-20 09:55:17 -0400136 tok = strtok(value, " ");
137 strcpy(value1, tok);
138 tok = strtok(NULL, " ");
139 if (tok != NULL) {
140 memset(value2, 0, sizeof(value2));
141 strcpy(value2, tok);
142 line_len = strlen(tok);
143 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
144 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
145 remove_nl = 2;
146 else
147 remove_nl = 1;
148 } else
149 remove_nl = 0;
150 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000151 DataManager::SetValue(TW_BACKUP_NAME, value2);
Dees_Troy2673cec2013-04-02 20:22:16 +0000152 gui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400153 if (PartitionManager.Check_Backup_Name(true) != 0) {
154 ret_val = 1;
155 continue;
156 }
Dees_Troy812660f2012-09-20 09:55:17 -0400157 } else {
158 char empt[50];
159 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000160 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400161 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400162 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400163 } else if (strcmp(command, "restore") == 0) {
164 // Restore
Dees_Troy6ed34b72013-01-25 15:01:29 +0000165 DataManager::SetValue("tw_action_text2", "Restoring");
Dees_Troy812660f2012-09-20 09:55:17 -0400166 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000167 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000168 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400169
170 string val = value, restore_folder, restore_partitions;
171 size_t pos = val.find_last_of(" ");
172 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000173 restore_folder = value;
174 partitions[0] = '\0';
175 } else {
176 restore_folder = val.substr(0, pos);
177 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
178 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400179 }
Dees_Troy812660f2012-09-20 09:55:17 -0400180 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000181 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
182 gui_print("Restoring '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400183
184 if (folder_path[0] != '/') {
185 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000186 string folder_var;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600187 std::vector<PartitionList> Storage_List;
188
189 PartitionManager.Get_Partition_List("storage", &Storage_List);
190 int listSize = Storage_List.size();
191 for (int i = 0; i < listSize; i++) {
192 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
193 DataManager::SetValue("tw_storage_path", Storage_List.at(i).Mount_Point);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000194 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
195 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600196 if (TWFunc::Path_Exists(backup_folder)) {
197 strcpy(folder_path, backup_folder);
198 break;
199 }
Dees_Troy812660f2012-09-20 09:55:17 -0400200 }
201 }
Dees_Troy812660f2012-09-20 09:55:17 -0400202 } else {
203 if (folder_path[strlen(folder_path) - 1] == '/')
204 strcat(folder_path, ".");
205 else
206 strcat(folder_path, "/.");
207 }
208 if (!TWFunc::Path_Exists(folder_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 gui_print("Unable to locate backup '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400210 ret_val = 1;
211 continue;
212 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000213 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400214
215 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500216 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000217 int is_encrypted = 0;
218 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400220 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500221 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400222
223 memset(value2, 0, sizeof(value2));
224 strcpy(value2, partitions);
Dees_Troy2673cec2013-04-02 20:22:16 +0000225 gui_print("Setting restore options: '%s':\n", value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400226 line_len = strlen(value2);
227 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500228 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
229 Restore_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 gui_print("System\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
232 Restore_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000233 gui_print("Data\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500234 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
235 Restore_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000236 gui_print("Cache\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500237 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 gui_print("Recovery -- Not allowed to restore recovery\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000239 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000241 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000243 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
246 Restore_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 gui_print("Boot\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
249 Restore_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000250 gui_print("Android Secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
252 Restore_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 gui_print("SD-Ext\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400254 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000255 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 gui_print("MD5 check skip is on\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400257 }
258 }
259
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 DataManager::SetValue("tw_restore_selected", Restore_List);
261 } else {
262 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400263 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000264 if (is_encrypted) {
265 LOGERR("Unable to use OpenRecoveryScript to restore an encrypted backup.\n");
266 ret_val = 1;
267 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 ret_val = 1;
269 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000270 gui_print("Restore complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400271 } else if (strcmp(command, "mount") == 0) {
272 // Mount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000273 DataManager::SetValue("tw_action_text2", "Mounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400274 if (value[0] != '/') {
275 strcpy(mount, "/");
276 strcat(mount, value);
277 } else
278 strcpy(mount, value);
279 if (PartitionManager.Mount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 gui_print("Mounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400281 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
282 // Unmount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000283 DataManager::SetValue("tw_action_text2", "Unmounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400284 if (value[0] != '/') {
285 strcpy(mount, "/");
286 strcat(mount, value);
287 } else
288 strcpy(mount, value);
289 if (PartitionManager.UnMount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 gui_print("Unmounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400291 } else if (strcmp(command, "set") == 0) {
292 // Set value
293 tok = strtok(value, " ");
294 strcpy(value1, tok);
295 tok = strtok(NULL, " ");
296 strcpy(value2, tok);
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 gui_print("Setting '%s' to '%s'\n", value1, value2);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000298 DataManager::SetValue(value1, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400299 } else if (strcmp(command, "mkdir") == 0) {
300 // Make directory (recursive)
Dees_Troy6ed34b72013-01-25 15:01:29 +0000301 DataManager::SetValue("tw_action_text2", "Making Directory");
Dees_Troy2673cec2013-04-02 20:22:16 +0000302 gui_print("Making directory (recursive): '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400303 if (TWFunc::Recursive_Mkdir(value)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGERR("Unable to create folder: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400305 ret_val = 1;
306 }
307 } else if (strcmp(command, "reboot") == 0) {
308 // Reboot
309 } else if (strcmp(command, "cmd") == 0) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000310 DataManager::SetValue("tw_action_text2", "Running Command");
Dees_Troy812660f2012-09-20 09:55:17 -0400311 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200312 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400313 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400315 }
316 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000317 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000318 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000319 // ADB Sideload
Dees_Troy6ed34b72013-01-25 15:01:29 +0000320 DataManager::SetValue("tw_action_text2", "ADB Sideload");
321 install_cmd = -1;
322
323 int wipe_cache = 0;
324 string result, Sideload_File;
325
326 if (!PartitionManager.Mount_Current_Storage(true)) {
327 ret_val = 1; // failure
328 } else {
329 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
330 if (TWFunc::Path_Exists(Sideload_File)) {
331 unlink(Sideload_File.c_str());
332 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 gui_print("Starting ADB sideload feature...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000334 DataManager::SetValue("tw_has_cancel", 1);
335 DataManager::SetValue("tw_cancel_action", "adbsideloadcancel");
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 ret_val = apply_from_adb(Sideload_File.c_str());
Dees_Troy6ed34b72013-01-25 15:01:29 +0000337 DataManager::SetValue("tw_has_cancel", 0);
338 if (ret_val != 0)
339 ret_val = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
341 if (wipe_cache)
342 PartitionManager.Wipe_By_Path("/cache");
343 } else {
344 ret_val = 1; // failure
345 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000346 sideload = 1; // Causes device to go to the home screen afterwards
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 gui_print("Sideload finished.\n");
Dees_Troy4fc00242013-01-17 19:22:12 +0000348 }
Ethan Yonkeraae6e8c2014-02-11 21:37:36 -0600349 } else if (strcmp(command, "fixperms") == 0 || strcmp(command, "fixpermissions") == 0) {
350 ret_val = PartitionManager.Fix_Permissions();
351 if (ret_val != 0)
352 ret_val = 1; // failure
Dees_Troy812660f2012-09-20 09:55:17 -0400353 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400355 ret_val = 1;
356 }
357 }
358 fclose(fp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 gui_print("Done processing script file\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400360 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000361 LOGERR("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
Dees_Troy812660f2012-09-20 09:55:17 -0400362 return 1;
363 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000364 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000365 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400366 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
367 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200368 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400369 else {
370 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 +0200371 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400372 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000373 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400374 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000375 if (sideload)
376 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400377 return ret_val;
378}
379
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000380int OpenRecoveryScript::Insert_ORS_Command(string Command) {
381 ofstream ORSfile(SCRIPT_FILE_TMP);
382 if (ORSfile.is_open()) {
383 //if (Command.substr(Command.size() - 1, 1) != "\n")
384 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000385 LOGINFO("Inserting '%s'\n", Command.c_str());
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000386 ORSfile << Command.c_str();
387 ORSfile.close();
388 return 1;
389 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000390 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000391 return 0;
392}
393
Dees_Troy83a3b122012-10-01 14:16:43 -0400394int OpenRecoveryScript::Install_Command(string Zip) {
395 // Install zip
396 string ret_string;
397 int ret_val = 0, wipe_cache = 0;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600398 std::vector<PartitionList> Storage_List;
399 string Full_Path;
Dees_Troy83a3b122012-10-01 14:16:43 -0400400
401 PartitionManager.Mount_All_Storage();
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600402 PartitionManager.Get_Partition_List("storage", &Storage_List);
403 int listSize = Storage_List.size();
404 for (int i = 0; i < listSize; i++) {
405 if (PartitionManager.Is_Mounted_By_Path(Storage_List.at(i).Mount_Point)) {
406 Full_Path = Storage_List.at(i).Mount_Point + "/" + Zip;
407 if (TWFunc::Path_Exists(Full_Path)) {
408 Zip = Full_Path;
409 break;
Dees_Troy83a3b122012-10-01 14:16:43 -0400410 }
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600411 Full_Path = Zip;
412 LOGINFO("Trying to find zip '%s' on '%s'...\n", Full_Path.c_str(), Storage_List.at(i).Mount_Point.c_str());
413 ret_string = Locate_Zip_File(Full_Path, Storage_List.at(i).Mount_Point);
414 if (!ret_string.empty()) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400415 Zip = ret_string;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600416 break;
417 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400418 }
419 }
420
421 if (!TWFunc::Path_Exists(Zip)) {
422 // zip file doesn't exist
Dees_Troy2673cec2013-04-02 20:22:16 +0000423 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400424 ret_val = 1;
425 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000426 gui_print("Installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400427 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
428 }
429 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000430 LOGERR("Error installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400431 ret_val = 1;
432 } else if (wipe_cache)
433 PartitionManager.Wipe_By_Path("/cache");
434
435 return ret_val;
436}
437
Dees_Troy812660f2012-09-20 09:55:17 -0400438string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
439 string Path = TWFunc::Get_Path(Zip);
440 string File = TWFunc::Get_Filename(Zip);
441 string pathCpy = Path;
442 string wholePath;
443 size_t pos = Path.find("/", 1);
444
445 while (pos != string::npos)
446 {
447 pathCpy = Path.substr(pos, Path.size() - pos);
448 wholePath = pathCpy + "/" + File;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600449 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400450 if (TWFunc::Path_Exists(wholePath))
451 return wholePath;
452 wholePath = Storage_Root + "/" + wholePath;
Ethan Yonkerd7adf292014-02-15 16:43:23 -0600453 LOGINFO("Looking for zip at '%s'\n", wholePath.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400454 if (TWFunc::Path_Exists(wholePath))
455 return wholePath;
456
457 pos = Path.find("/", pos + 1);
458 }
459 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400460}
461
462int OpenRecoveryScript::Backup_Command(string Options) {
463 char value1[SCRIPT_COMMAND_SIZE];
464 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400466
467 strcpy(value1, Options.c_str());
468
Dees_Troy6ed34b72013-01-25 15:01:29 +0000469 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
470 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400471
Dees_Troy2673cec2013-04-02 20:22:16 +0000472 gui_print("Setting backup options:\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400473 line_len = Options.size();
474 for (i=0; i<line_len; i++) {
475 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Backup_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000477 gui_print("System\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400478 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 Backup_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000480 gui_print("Data\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400481 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Backup_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000483 gui_print("Cache\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400484 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500485 Backup_List += "/recovery;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000486 gui_print("Recovery\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400487 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400489 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000490 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400491 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000492 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400493 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500494 Backup_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000495 gui_print("Boot\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400496 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 Backup_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 gui_print("Android Secure\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400499 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500500 Backup_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000501 gui_print("SD-Ext\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400502 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000503 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000504 gui_print("Compression is on\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400505 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000506 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000507 gui_print("MD5 Generation is off\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400508 }
509 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 DataManager::SetValue("tw_backup_list", Backup_List);
Dees_Troy83a3b122012-10-01 14:16:43 -0400511 if (!PartitionManager.Run_Backup()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 LOGERR("Backup failed!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400513 return 1;
514 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 gui_print("Backup complete!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400516 return 0;
517}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000518
519void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
520 DataManager::SetValue("tw_back", "main");
521 DataManager::SetValue("tw_action", "openrecoveryscript");
522 DataManager::SetValue("tw_has_action2", "0");
523 DataManager::SetValue("tw_action2", "");
524 DataManager::SetValue("tw_action2_param", "");
525 DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript");
526 DataManager::SetValue("tw_action_text2", "");
527 DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete");
528 DataManager::SetValue("tw_has_cancel", 0);
529 DataManager::SetValue("tw_show_reboot", 0);
530 if (gui_startPage("action_page") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000532 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000533}