blob: 87118bc677cbd71ce157578373587b4a278423d1 [file] [log] [blame]
Dees_Troy812660f2012-09-20 09:55:17 -04001/* OpenRecoveryScript class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
28#include <unistd.h>
29#include <vector>
30#include <dirent.h>
31#include <time.h>
32#include <errno.h>
Dees_Troy01b6d0c2013-01-22 01:41:09 +000033#include <iostream>
34#include <fstream>
Dees_Troy812660f2012-09-20 09:55:17 -040035
36#include "twrp-functions.hpp"
37#include "partitions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "twcommon.h"
Dees_Troy812660f2012-09-20 09:55:17 -040039#include "openrecoveryscript.hpp"
40#include "variables.h"
Dees_Troy4fc00242013-01-17 19:22:12 +000041#include "adb_install.h"
Dees_Troy6ed34b72013-01-25 15:01:29 +000042#include "data.hpp"
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"
46 int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy812660f2012-09-20 09:55:17 -040047}
48
Dees_Troy812660f2012-09-20 09:55:17 -040049#define SCRIPT_COMMAND_SIZE 512
50
51int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040052 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000053 LOGERR("Unable to mount /cache for OpenRecoveryScript support.\n");
Dees_Troy812660f2012-09-20 09:55:17 -040054 return 0;
55 }
56 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +000057 LOGINFO("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040058 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040060 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050061 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040062 return 1;
63 }
64 return 0;
65}
66
67int OpenRecoveryScript::run_script_file(void) {
68 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000069 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040070 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
71 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
72 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
73 char *val_start, *tok;
74
75 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000076 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000077 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040078 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
79 cindex = 0;
80 line_len = strlen(script_line);
81 if (line_len < 2)
82 continue; // there's a blank line or line is too short to contain a command
Dees_Troy2673cec2013-04-02 20:22:16 +000083 //gui_print("script line: '%s'\n", script_line);
Dees_Troy812660f2012-09-20 09:55:17 -040084 for (i=0; i<line_len; i++) {
85 if ((int)script_line[i] == 32) {
86 cindex = i;
87 i = line_len;
88 }
89 }
90 memset(command, 0, sizeof(command));
91 memset(value, 0, sizeof(value));
92 if ((int)script_line[line_len - 1] == 10)
93 remove_nl = 2;
94 else
95 remove_nl = 1;
96 if (cindex != 0) {
97 strncpy(command, script_line, cindex);
Dees_Troy2673cec2013-04-02 20:22:16 +000098 LOGINFO("command is: '%s' and ", command);
Dees_Troy812660f2012-09-20 09:55:17 -040099 val_start = script_line;
100 val_start += cindex + 1;
bigbiff bigbiffc03121d2013-09-09 19:14:35 -0400101 if ((int) *val_start == 32)
102 val_start++; //get rid of space
bigbiff bigbiff72e95542013-08-29 21:01:02 -0400103 if ((int) *val_start == 51)
104 val_start++; //get rid of = at the beginning
bigbiff bigbiffa6c5f4e2013-09-17 20:01:14 -0400105 if ((int) *val_start == 32)
106 val_start++; //get rid of space
Dees_Troy812660f2012-09-20 09:55:17 -0400107 strncpy(value, val_start, line_len - cindex - remove_nl);
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 LOGINFO("value is: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400109 } else {
110 strncpy(command, script_line, line_len - remove_nl + 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 gui_print("command is: '%s' and there is no value\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400112 }
113 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400114 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000115 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000116 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400117 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400118 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400119 } else if (strcmp(command, "wipe") == 0) {
120 // Wipe
121 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000122 gui_print("-- Wiping Cache Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400123 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 gui_print("-- Cache Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400125 } 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 +0000126 gui_print("-- Wiping Dalvik Cache...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400127 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 gui_print("-- Dalvik Cache Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400129 } 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 +0000130 gui_print("-- Wiping Data Partition...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400131 PartitionManager.Factory_Reset();
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 gui_print("-- Data Partition Wipe Complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400133 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 LOGERR("Error with wipe command value: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400135 ret_val = 1;
136 }
137 } else if (strcmp(command, "backup") == 0) {
138 // Backup
Dees_Troy6ed34b72013-01-25 15:01:29 +0000139 DataManager::SetValue("tw_action_text2", "Backing Up");
Dees_Troy812660f2012-09-20 09:55:17 -0400140 tok = strtok(value, " ");
141 strcpy(value1, tok);
142 tok = strtok(NULL, " ");
143 if (tok != NULL) {
144 memset(value2, 0, sizeof(value2));
145 strcpy(value2, tok);
146 line_len = strlen(tok);
147 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
148 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
149 remove_nl = 2;
150 else
151 remove_nl = 1;
152 } else
153 remove_nl = 0;
154 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000155 DataManager::SetValue(TW_BACKUP_NAME, value2);
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 gui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400157 if (PartitionManager.Check_Backup_Name(true) != 0) {
158 ret_val = 1;
159 continue;
160 }
Dees_Troy812660f2012-09-20 09:55:17 -0400161 } else {
162 char empt[50];
163 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000164 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400165 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400166 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400167 } else if (strcmp(command, "restore") == 0) {
168 // Restore
Dees_Troy6ed34b72013-01-25 15:01:29 +0000169 DataManager::SetValue("tw_action_text2", "Restoring");
Dees_Troy812660f2012-09-20 09:55:17 -0400170 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000171 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000172 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400173
174 string val = value, restore_folder, restore_partitions;
175 size_t pos = val.find_last_of(" ");
176 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000177 restore_folder = value;
178 partitions[0] = '\0';
179 } else {
180 restore_folder = val.substr(0, pos);
181 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
182 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400183 }
Dees_Troy812660f2012-09-20 09:55:17 -0400184 strcpy(folder_path, restore_folder.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +0000185 LOGINFO("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
186 gui_print("Restoring '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400187
188 if (folder_path[0] != '/') {
189 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000190 string folder_var;
191 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
192 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 LOGINFO("Restoring relative path: '%s'\n", backup_folder);
Dees_Troy812660f2012-09-20 09:55:17 -0400194 if (!TWFunc::Path_Exists(backup_folder)) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000195 if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) {
196 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGINFO("Backup folder '%s' not found on external storage, trying internal...\n", folder_path);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000198 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy812660f2012-09-20 09:55:17 -0400199 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 LOGINFO("Backup folder '%s' not found on internal storage, trying external...\n", folder_path);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000201 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troy812660f2012-09-20 09:55:17 -0400202 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000203 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
204 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Dees_Troy2673cec2013-04-02 20:22:16 +0000205 LOGINFO("2Restoring relative path: '%s'\n", backup_folder);
Dees_Troy812660f2012-09-20 09:55:17 -0400206 }
207 }
208 strcpy(folder_path, backup_folder);
209 } else {
210 if (folder_path[strlen(folder_path) - 1] == '/')
211 strcat(folder_path, ".");
212 else
213 strcat(folder_path, "/.");
214 }
215 if (!TWFunc::Path_Exists(folder_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 gui_print("Unable to locate backup '%s'\n", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400217 ret_val = 1;
218 continue;
219 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000220 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400221
222 PartitionManager.Set_Restore_Files(folder_path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500223 string Partition_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000224 int is_encrypted = 0;
225 DataManager::GetValue("tw_restore_encrypted", is_encrypted);
Dees_Troya13d74f2013-03-24 08:54:55 -0500226 DataManager::GetValue("tw_restore_list", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400227 if (strlen(partitions) != 0) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500228 string Restore_List;
Dees_Troy812660f2012-09-20 09:55:17 -0400229
230 memset(value2, 0, sizeof(value2));
231 strcpy(value2, partitions);
Dees_Troy2673cec2013-04-02 20:22:16 +0000232 gui_print("Setting restore options: '%s':\n", value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400233 line_len = strlen(value2);
234 for (i=0; i<line_len; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500235 if ((value2[i] == 'S' || value2[i] == 's') && Partition_List.find("/system;") != string::npos) {
236 Restore_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 gui_print("System\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 } else if ((value2[i] == 'D' || value2[i] == 'd') && Partition_List.find("/data;") != string::npos) {
239 Restore_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 gui_print("Data\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 } else if ((value2[i] == 'C' || value2[i] == 'c') && Partition_List.find("/cache;") != string::npos) {
242 Restore_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000243 gui_print("Cache\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 } else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000245 gui_print("Recovery -- Not allowed to restore recovery\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000246 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000248 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000250 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 } else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
253 Restore_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 gui_print("Boot\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 } else if ((value2[i] == 'A' || value2[i] == 'a') && Partition_List.find("/and-sec;") != string::npos) {
256 Restore_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 gui_print("Android Secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 } else if ((value2[i] == 'E' || value2[i] == 'e') && Partition_List.find("/sd-ext;") != string::npos) {
259 Restore_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 gui_print("SD-Ext\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400261 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000262 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 gui_print("MD5 check skip is on\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400264 }
265 }
266
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 DataManager::SetValue("tw_restore_selected", Restore_List);
268 } else {
269 DataManager::SetValue("tw_restore_selected", Partition_List);
Dees_Troy812660f2012-09-20 09:55:17 -0400270 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000271 if (is_encrypted) {
272 LOGERR("Unable to use OpenRecoveryScript to restore an encrypted backup.\n");
273 ret_val = 1;
274 } else if (!PartitionManager.Run_Restore(folder_path))
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 ret_val = 1;
276 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000277 gui_print("Restore complete!\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400278 } else if (strcmp(command, "mount") == 0) {
279 // Mount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000280 DataManager::SetValue("tw_action_text2", "Mounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400281 if (value[0] != '/') {
282 strcpy(mount, "/");
283 strcat(mount, value);
284 } else
285 strcpy(mount, value);
286 if (PartitionManager.Mount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 gui_print("Mounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400288 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
289 // Unmount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000290 DataManager::SetValue("tw_action_text2", "Unmounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400291 if (value[0] != '/') {
292 strcpy(mount, "/");
293 strcat(mount, value);
294 } else
295 strcpy(mount, value);
296 if (PartitionManager.UnMount_By_Path(mount, true))
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 gui_print("Unmounted '%s'\n", mount);
Dees_Troy812660f2012-09-20 09:55:17 -0400298 } else if (strcmp(command, "set") == 0) {
299 // Set value
300 tok = strtok(value, " ");
301 strcpy(value1, tok);
302 tok = strtok(NULL, " ");
303 strcpy(value2, tok);
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 gui_print("Setting '%s' to '%s'\n", value1, value2);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000305 DataManager::SetValue(value1, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400306 } else if (strcmp(command, "mkdir") == 0) {
307 // Make directory (recursive)
Dees_Troy6ed34b72013-01-25 15:01:29 +0000308 DataManager::SetValue("tw_action_text2", "Making Directory");
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 gui_print("Making directory (recursive): '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400310 if (TWFunc::Recursive_Mkdir(value)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 LOGERR("Unable to create folder: '%s'\n", value);
Dees_Troy812660f2012-09-20 09:55:17 -0400312 ret_val = 1;
313 }
314 } else if (strcmp(command, "reboot") == 0) {
315 // Reboot
316 } else if (strcmp(command, "cmd") == 0) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000317 DataManager::SetValue("tw_action_text2", "Running Command");
Dees_Troy812660f2012-09-20 09:55:17 -0400318 if (cindex != 0) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200319 TWFunc::Exec_Cmd(value);
Dees_Troy812660f2012-09-20 09:55:17 -0400320 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 LOGERR("No value given for cmd\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400322 }
323 } else if (strcmp(command, "print") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 gui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000325 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000326 // ADB Sideload
Dees_Troy6ed34b72013-01-25 15:01:29 +0000327 DataManager::SetValue("tw_action_text2", "ADB Sideload");
328 install_cmd = -1;
329
330 int wipe_cache = 0;
331 string result, Sideload_File;
332
333 if (!PartitionManager.Mount_Current_Storage(true)) {
334 ret_val = 1; // failure
335 } else {
336 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
337 if (TWFunc::Path_Exists(Sideload_File)) {
338 unlink(Sideload_File.c_str());
339 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 gui_print("Starting ADB sideload feature...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000341 DataManager::SetValue("tw_has_cancel", 1);
342 DataManager::SetValue("tw_cancel_action", "adbsideloadcancel");
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 ret_val = apply_from_adb(Sideload_File.c_str());
Dees_Troy6ed34b72013-01-25 15:01:29 +0000344 DataManager::SetValue("tw_has_cancel", 0);
345 if (ret_val != 0)
346 ret_val = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
348 if (wipe_cache)
349 PartitionManager.Wipe_By_Path("/cache");
350 } else {
351 ret_val = 1; // failure
352 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000353 sideload = 1; // Causes device to go to the home screen afterwards
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 gui_print("Sideload finished.\n");
Dees_Troy4fc00242013-01-17 19:22:12 +0000355 }
Dees_Troy812660f2012-09-20 09:55:17 -0400356 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGERR("Unrecognized script command: '%s'\n", command);
Dees_Troy812660f2012-09-20 09:55:17 -0400358 ret_val = 1;
359 }
360 }
361 fclose(fp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 gui_print("Done processing script file\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400363 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000364 LOGERR("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
Dees_Troy812660f2012-09-20 09:55:17 -0400365 return 1;
366 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000367 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000368 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400369 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
370 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200371 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400372 else {
373 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 +0200374 TWFunc::Exec_Cmd(injectcmd.c_str());
Dees_Troy06b4fe92012-10-16 11:43:20 -0400375 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000376 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400377 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000378 if (sideload)
379 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400380 return ret_val;
381}
382
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000383int OpenRecoveryScript::Insert_ORS_Command(string Command) {
384 ofstream ORSfile(SCRIPT_FILE_TMP);
385 if (ORSfile.is_open()) {
386 //if (Command.substr(Command.size() - 1, 1) != "\n")
387 // Command += "\n";
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 LOGINFO("Inserting '%s'\n", Command.c_str());
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000389 ORSfile << Command.c_str();
390 ORSfile.close();
391 return 1;
392 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGERR("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000394 return 0;
395}
396
Dees_Troy83a3b122012-10-01 14:16:43 -0400397int OpenRecoveryScript::Install_Command(string Zip) {
398 // Install zip
399 string ret_string;
400 int ret_val = 0, wipe_cache = 0;
401
402 PartitionManager.Mount_All_Storage();
403 if (Zip.substr(0, 1) != "/") {
404 // Relative path given
405 string Full_Path;
406
Dees_Troy6ed34b72013-01-25 15:01:29 +0000407 Full_Path = DataManager::GetCurrentStoragePath();
Dees_Troy83a3b122012-10-01 14:16:43 -0400408 Full_Path += "/" + Zip;
Dees_Troy2673cec2013-04-02 20:22:16 +0000409 LOGINFO("Full zip path: '%s'\n", Full_Path.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400410 if (!TWFunc::Path_Exists(Full_Path)) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000411 ret_string = Locate_Zip_File(Full_Path, DataManager::GetCurrentStoragePath());
Dees_Troy83a3b122012-10-01 14:16:43 -0400412 if (!ret_string.empty()) {
413 Full_Path = ret_string;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000414 } else if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) {
415 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000416 LOGINFO("Zip file not found on external storage, trying internal...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000417 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400418 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000419 LOGINFO("Zip file not found on internal storage, trying external...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000420 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400421 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000422 Full_Path = DataManager::GetCurrentStoragePath();
Dees_Troy83a3b122012-10-01 14:16:43 -0400423 Full_Path += "/" + Zip;
Dees_Troy2673cec2013-04-02 20:22:16 +0000424 LOGINFO("Full zip path: '%s'\n", Full_Path.c_str());
Dees_Troy6ed34b72013-01-25 15:01:29 +0000425 ret_string = Locate_Zip_File(Full_Path, DataManager::GetCurrentStoragePath());
Dees_Troy83a3b122012-10-01 14:16:43 -0400426 if (!ret_string.empty())
427 Full_Path = ret_string;
428 }
429 }
430 Zip = Full_Path;
431 } else {
432 // Full path given
433 if (!TWFunc::Path_Exists(Zip)) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000434 ret_string = Locate_Zip_File(Zip, DataManager::GetCurrentStoragePath());
Dees_Troy83a3b122012-10-01 14:16:43 -0400435 if (!ret_string.empty())
436 Zip = ret_string;
437 }
438 }
439
440 if (!TWFunc::Path_Exists(Zip)) {
441 // zip file doesn't exist
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 gui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400443 ret_val = 1;
444 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000445 gui_print("Installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400446 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
447 }
448 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 LOGERR("Error installing zip file '%s'\n", Zip.c_str());
Dees_Troy83a3b122012-10-01 14:16:43 -0400450 ret_val = 1;
451 } else if (wipe_cache)
452 PartitionManager.Wipe_By_Path("/cache");
453
454 return ret_val;
455}
456
Dees_Troy812660f2012-09-20 09:55:17 -0400457string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
458 string Path = TWFunc::Get_Path(Zip);
459 string File = TWFunc::Get_Filename(Zip);
460 string pathCpy = Path;
461 string wholePath;
462 size_t pos = Path.find("/", 1);
463
464 while (pos != string::npos)
465 {
466 pathCpy = Path.substr(pos, Path.size() - pos);
467 wholePath = pathCpy + "/" + File;
468 if (TWFunc::Path_Exists(wholePath))
469 return wholePath;
470 wholePath = Storage_Root + "/" + wholePath;
471 if (TWFunc::Path_Exists(wholePath))
472 return wholePath;
473
474 pos = Path.find("/", pos + 1);
475 }
476 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400477}
478
479int OpenRecoveryScript::Backup_Command(string Options) {
480 char value1[SCRIPT_COMMAND_SIZE];
481 int line_len, i;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 string Backup_List;
Dees_Troy83a3b122012-10-01 14:16:43 -0400483
484 strcpy(value1, Options.c_str());
485
Dees_Troy6ed34b72013-01-25 15:01:29 +0000486 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
487 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400488
Dees_Troy2673cec2013-04-02 20:22:16 +0000489 gui_print("Setting backup options:\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400490 line_len = Options.size();
491 for (i=0; i<line_len; i++) {
492 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Backup_List += "/system;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000494 gui_print("System\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400495 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 Backup_List += "/data;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000497 gui_print("Data\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400498 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 Backup_List += "/cache;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000500 gui_print("Cache\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400501 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 Backup_List += "/recovery;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000503 gui_print("Recovery\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400504 } else if (Options.substr(i, 1) == "1") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000505 gui_print("%s\n", "Special1 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400506 } else if (Options.substr(i, 1) == "2") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000507 gui_print("%s\n", "Special2 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400508 } else if (Options.substr(i, 1) == "3") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 gui_print("%s\n", "Special3 -- No Longer Supported...");
Dees_Troy83a3b122012-10-01 14:16:43 -0400510 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 Backup_List += "/boot;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 gui_print("Boot\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400513 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 Backup_List += "/and-sec;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 gui_print("Android Secure\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400516 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Backup_List += "/sd-ext;";
Dees_Troy2673cec2013-04-02 20:22:16 +0000518 gui_print("SD-Ext\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400519 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000520 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000521 gui_print("Compression is on\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400522 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000523 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 gui_print("MD5 Generation is off\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400525 }
526 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 DataManager::SetValue("tw_backup_list", Backup_List);
Dees_Troy83a3b122012-10-01 14:16:43 -0400528 if (!PartitionManager.Run_Backup()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 LOGERR("Backup failed!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400530 return 1;
531 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000532 gui_print("Backup complete!\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400533 return 0;
534}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000535
536void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
537 DataManager::SetValue("tw_back", "main");
538 DataManager::SetValue("tw_action", "openrecoveryscript");
539 DataManager::SetValue("tw_has_action2", "0");
540 DataManager::SetValue("tw_action2", "");
541 DataManager::SetValue("tw_action2_param", "");
542 DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript");
543 DataManager::SetValue("tw_action_text2", "");
544 DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete");
545 DataManager::SetValue("tw_has_cancel", 0);
546 DataManager::SetValue("tw_show_reboot", 0);
547 if (gui_startPage("action_page") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 LOGERR("Failed to load OpenRecoveryScript GUI page.\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000549 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000550}