blob: e319a9c61d81baeb209f762295b8f62ee3b75854 [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"
38#include "common.h"
39#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_Troy4fc00242013-01-17 19:22:12 +000049extern RecoveryUI* ui;
Dees_Troy812660f2012-09-20 09:55:17 -040050#define SCRIPT_COMMAND_SIZE 512
51
52int OpenRecoveryScript::check_for_script_file(void) {
Dees_Troy812660f2012-09-20 09:55:17 -040053 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
54 LOGE("Unable to mount /cache for OpenRecoveryScript support.\n");
55 return 0;
56 }
57 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
58 LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
59 // Copy script file to /tmp
bigbiff bigbiff9c754052013-01-09 09:09:08 -050060 TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755);
Dees_Troy812660f2012-09-20 09:55:17 -040061 // Delete the file from /cache
bigbiff bigbiff9c754052013-01-09 09:09:08 -050062 unlink(SCRIPT_FILE_CACHE);
Dees_Troy812660f2012-09-20 09:55:17 -040063 return 1;
64 }
65 return 0;
66}
67
68int OpenRecoveryScript::run_script_file(void) {
69 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
Dees_Troy4bc09ae2013-01-18 17:00:54 +000070 int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
Dees_Troy812660f2012-09-20 09:55:17 -040071 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
72 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
73 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
74 char *val_start, *tok;
75
76 if (fp != NULL) {
Dees_Troy6ed34b72013-01-25 15:01:29 +000077 DataManager::SetValue(TW_SIMULATE_ACTIONS, 0);
Dees_Troy6bdcbb12013-01-26 14:07:43 +000078 DataManager::SetValue("ui_progress", 0); // Reset the progress bar
Dees_Troy812660f2012-09-20 09:55:17 -040079 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
80 cindex = 0;
81 line_len = strlen(script_line);
82 if (line_len < 2)
83 continue; // there's a blank line or line is too short to contain a command
84 //ui_print("script line: '%s'\n", script_line);
85 for (i=0; i<line_len; i++) {
86 if ((int)script_line[i] == 32) {
87 cindex = i;
88 i = line_len;
89 }
90 }
91 memset(command, 0, sizeof(command));
92 memset(value, 0, sizeof(value));
93 if ((int)script_line[line_len - 1] == 10)
94 remove_nl = 2;
95 else
96 remove_nl = 1;
97 if (cindex != 0) {
98 strncpy(command, script_line, cindex);
99 LOGI("command is: '%s' and ", command);
100 val_start = script_line;
101 val_start += cindex + 1;
102 strncpy(value, val_start, line_len - cindex - remove_nl);
103 LOGI("value is: '%s'\n", value);
104 } else {
105 strncpy(command, script_line, line_len - remove_nl + 1);
106 ui_print("command is: '%s' and there is no value\n", command);
107 }
108 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400109 // Install Zip
Dees_Troy6ed34b72013-01-25 15:01:29 +0000110 DataManager::SetValue("tw_action_text2", "Installing Zip");
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000111 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400112 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400113 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400114 } else if (strcmp(command, "wipe") == 0) {
115 // Wipe
116 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
117 ui_print("-- Wiping Cache Partition...\n");
118 PartitionManager.Wipe_By_Path("/cache");
119 ui_print("-- Cache Partition Wipe Complete!\n");
120 } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
121 ui_print("-- Wiping Dalvik Cache...\n");
122 PartitionManager.Wipe_Dalvik_Cache();
123 ui_print("-- Dalvik Cache Wipe Complete!\n");
124 } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
125 ui_print("-- Wiping Data Partition...\n");
126 PartitionManager.Factory_Reset();
127 ui_print("-- Data Partition Wipe Complete!\n");
128 } else {
129 LOGE("Error with wipe command value: '%s'\n", value);
130 ret_val = 1;
131 }
132 } else if (strcmp(command, "backup") == 0) {
133 // Backup
Dees_Troy6ed34b72013-01-25 15:01:29 +0000134 DataManager::SetValue("tw_action_text2", "Backing Up");
Dees_Troy812660f2012-09-20 09:55:17 -0400135 tok = strtok(value, " ");
136 strcpy(value1, tok);
137 tok = strtok(NULL, " ");
138 if (tok != NULL) {
139 memset(value2, 0, sizeof(value2));
140 strcpy(value2, tok);
141 line_len = strlen(tok);
142 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
143 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
144 remove_nl = 2;
145 else
146 remove_nl = 1;
147 } else
148 remove_nl = 0;
149 strncpy(value2, tok, line_len - remove_nl);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000150 DataManager::SetValue(TW_BACKUP_NAME, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400151 ui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400152 if (PartitionManager.Check_Backup_Name(true) != 0) {
153 ret_val = 1;
154 continue;
155 }
Dees_Troy812660f2012-09-20 09:55:17 -0400156 } else {
157 char empt[50];
158 strcpy(empt, "(Current Date)");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000159 DataManager::SetValue(TW_BACKUP_NAME, empt);
Dees_Troy812660f2012-09-20 09:55:17 -0400160 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400161 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400162 } else if (strcmp(command, "restore") == 0) {
163 // Restore
Dees_Troy6ed34b72013-01-25 15:01:29 +0000164 DataManager::SetValue("tw_action_text2", "Restoring");
Dees_Troy812660f2012-09-20 09:55:17 -0400165 PartitionManager.Mount_All_Storage();
Dees_Troy6ed34b72013-01-25 15:01:29 +0000166 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000167 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400168
169 string val = value, restore_folder, restore_partitions;
170 size_t pos = val.find_last_of(" ");
171 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000172 restore_folder = value;
173 partitions[0] = '\0';
174 } else {
175 restore_folder = val.substr(0, pos);
176 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
177 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400178 }
Dees_Troy812660f2012-09-20 09:55:17 -0400179 strcpy(folder_path, restore_folder.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400180 LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
181 ui_print("Restoring '%s'\n", folder_path);
182
183 if (folder_path[0] != '/') {
184 char backup_folder[512];
Dees_Troy6ed34b72013-01-25 15:01:29 +0000185 string folder_var;
186 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
187 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400188 LOGI("Restoring relative path: '%s'\n", backup_folder);
189 if (!TWFunc::Path_Exists(backup_folder)) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000190 if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) {
191 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
Dees_Troy812660f2012-09-20 09:55:17 -0400192 LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000193 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy812660f2012-09-20 09:55:17 -0400194 } else {
195 LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000196 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troy812660f2012-09-20 09:55:17 -0400197 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000198 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, folder_var);
199 sprintf(backup_folder, "%s/%s", folder_var.c_str(), folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400200 LOGI("2Restoring relative path: '%s'\n", backup_folder);
201 }
202 }
203 strcpy(folder_path, backup_folder);
204 } else {
205 if (folder_path[strlen(folder_path) - 1] == '/')
206 strcat(folder_path, ".");
207 else
208 strcat(folder_path, "/.");
209 }
210 if (!TWFunc::Path_Exists(folder_path)) {
211 ui_print("Unable to locate backup '%s'\n", folder_path);
212 ret_val = 1;
213 continue;
214 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000215 DataManager::SetValue("tw_restore", folder_path);
Dees_Troy812660f2012-09-20 09:55:17 -0400216
217 PartitionManager.Set_Restore_Files(folder_path);
218 if (strlen(partitions) != 0) {
219 int tw_restore_system = 0;
220 int tw_restore_data = 0;
221 int tw_restore_cache = 0;
222 int tw_restore_recovery = 0;
223 int tw_restore_boot = 0;
224 int tw_restore_andsec = 0;
225 int tw_restore_sdext = 0;
226 int tw_restore_sp1 = 0;
227 int tw_restore_sp2 = 0;
228 int tw_restore_sp3 = 0;
229
230 memset(value2, 0, sizeof(value2));
231 strcpy(value2, partitions);
232 ui_print("Setting restore options: '%s':\n", value2);
233 line_len = strlen(value2);
234 for (i=0; i<line_len; i++) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000235 if ((value2[i] == 'S' || value2[i] == 's') && DataManager::GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400236 tw_restore_system = 1;
237 ui_print("System\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000238 } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager::GetIntValue(TW_RESTORE_DATA_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400239 tw_restore_data = 1;
240 ui_print("Data\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000241 } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager::GetIntValue(TW_RESTORE_CACHE_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400242 tw_restore_cache = 1;
243 ui_print("Cache\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000244 } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager::GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400245 tw_restore_recovery = 1;
246 ui_print("Recovery\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000247 } else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400248 tw_restore_sp1 = 1;
249 ui_print("%s\n", "Special1");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000250 } else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400251 tw_restore_sp2 = 1;
252 ui_print("%s\n", "Special2");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000253 } else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400254 tw_restore_sp3 = 1;
255 ui_print("%s\n", "Special3");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000256 } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager::GetIntValue(TW_RESTORE_BOOT_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400257 tw_restore_boot = 1;
258 ui_print("Boot\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000259 } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager::GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400260 tw_restore_andsec = 1;
261 ui_print("Android Secure\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000262 } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager::GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) {
Dees_Troy812660f2012-09-20 09:55:17 -0400263 tw_restore_sdext = 1;
264 ui_print("SD-Ext\n");
265 } else if (value2[i] == 'M' || value2[i] == 'm') {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000266 DataManager::SetValue(TW_SKIP_MD5_CHECK_VAR, 1);
Dees_Troy812660f2012-09-20 09:55:17 -0400267 ui_print("MD5 check skip is on\n");
268 }
269 }
270
Dees_Troy6ed34b72013-01-25 15:01:29 +0000271 if (DataManager::GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system)
272 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, 0);
273 if (DataManager::GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data)
274 DataManager::SetValue(TW_RESTORE_DATA_VAR, 0);
275 if (DataManager::GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache)
276 DataManager::SetValue(TW_RESTORE_CACHE_VAR, 0);
277 if (DataManager::GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery)
278 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, 0);
279 if (DataManager::GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot)
280 DataManager::SetValue(TW_RESTORE_BOOT_VAR, 0);
281 if (DataManager::GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec)
282 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, 0);
283 if (DataManager::GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext)
284 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, 0);
285 if (DataManager::GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1)
286 DataManager::SetValue(TW_RESTORE_SP1_VAR, 0);
287 if (DataManager::GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2)
288 DataManager::SetValue(TW_RESTORE_SP2_VAR, 0);
289 if (DataManager::GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3)
290 DataManager::SetValue(TW_RESTORE_SP3_VAR, 0);
Dees_Troy812660f2012-09-20 09:55:17 -0400291 }
292 PartitionManager.Run_Restore(folder_path);
293 ui_print("Restore complete!\n");
294 } else if (strcmp(command, "mount") == 0) {
295 // Mount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000296 DataManager::SetValue("tw_action_text2", "Mounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400297 if (value[0] != '/') {
298 strcpy(mount, "/");
299 strcat(mount, value);
300 } else
301 strcpy(mount, value);
302 if (PartitionManager.Mount_By_Path(mount, true))
303 ui_print("Mounted '%s'\n", mount);
304 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
305 // Unmount
Dees_Troy6ed34b72013-01-25 15:01:29 +0000306 DataManager::SetValue("tw_action_text2", "Unmounting");
Dees_Troy812660f2012-09-20 09:55:17 -0400307 if (value[0] != '/') {
308 strcpy(mount, "/");
309 strcat(mount, value);
310 } else
311 strcpy(mount, value);
312 if (PartitionManager.UnMount_By_Path(mount, true))
313 ui_print("Unmounted '%s'\n", mount);
314 } else if (strcmp(command, "set") == 0) {
315 // Set value
316 tok = strtok(value, " ");
317 strcpy(value1, tok);
318 tok = strtok(NULL, " ");
319 strcpy(value2, tok);
320 ui_print("Setting '%s' to '%s'\n", value1, value2);
Dees_Troy6ed34b72013-01-25 15:01:29 +0000321 DataManager::SetValue(value1, value2);
Dees_Troy812660f2012-09-20 09:55:17 -0400322 } else if (strcmp(command, "mkdir") == 0) {
323 // Make directory (recursive)
Dees_Troy6ed34b72013-01-25 15:01:29 +0000324 DataManager::SetValue("tw_action_text2", "Making Directory");
Dees_Troy812660f2012-09-20 09:55:17 -0400325 ui_print("Making directory (recursive): '%s'\n", value);
326 if (TWFunc::Recursive_Mkdir(value)) {
327 LOGE("Unable to create folder: '%s'\n", value);
328 ret_val = 1;
329 }
330 } else if (strcmp(command, "reboot") == 0) {
331 // Reboot
332 } 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) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500335 string status;
336 TWFunc::Exec_Cmd(value, status);
Dees_Troy812660f2012-09-20 09:55:17 -0400337 } else {
338 LOGE("No value given for cmd\n");
339 }
340 } else if (strcmp(command, "print") == 0) {
341 ui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000342 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000343 // ADB Sideload
Dees_Troy6ed34b72013-01-25 15:01:29 +0000344 DataManager::SetValue("tw_action_text2", "ADB Sideload");
345 install_cmd = -1;
346
347 int wipe_cache = 0;
348 string result, Sideload_File;
349
350 if (!PartitionManager.Mount_Current_Storage(true)) {
351 ret_val = 1; // failure
352 } else {
353 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
354 if (TWFunc::Path_Exists(Sideload_File)) {
355 unlink(Sideload_File.c_str());
356 }
357 ui_print("Starting ADB sideload feature...\n");
358 DataManager::SetValue("tw_has_cancel", 1);
359 DataManager::SetValue("tw_cancel_action", "adbsideloadcancel");
360 ret_val = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
361 DataManager::SetValue("tw_has_cancel", 0);
362 if (ret_val != 0)
363 ret_val = 1; // failure
364 else if (wipe_cache)
365 PartitionManager.Wipe_By_Path("/cache");
366 sideload = 1; // Causes device to go to the home screen afterwards
367 ui_print("Sideload finished.\n");
Dees_Troy4fc00242013-01-17 19:22:12 +0000368 }
Dees_Troy812660f2012-09-20 09:55:17 -0400369 } else {
370 LOGE("Unrecognized script command: '%s'\n", command);
371 ret_val = 1;
372 }
373 }
374 fclose(fp);
375 ui_print("Done processing script file\n");
376 } else {
377 LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
378 return 1;
379 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000380 if (install_cmd && DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500381 string status;
Dees_Troy06b4fe92012-10-16 11:43:20 -0400382 ui_print("Injecting TWRP into boot image...\n");
383 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
384 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500385 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", status);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400386 else {
387 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500388 TWFunc::Exec_Cmd(injectcmd.c_str(), status);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400389 }
390 ui_print("TWRP injection complete.\n");
391 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000392 if (sideload)
393 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400394 return ret_val;
395}
396
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000397int OpenRecoveryScript::Insert_ORS_Command(string Command) {
398 ofstream ORSfile(SCRIPT_FILE_TMP);
399 if (ORSfile.is_open()) {
400 //if (Command.substr(Command.size() - 1, 1) != "\n")
401 // Command += "\n";
402 LOGI("Inserting '%s'\n", Command.c_str());
403 ORSfile << Command.c_str();
404 ORSfile.close();
405 return 1;
406 }
407 LOGE("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
408 return 0;
409}
410
Dees_Troy83a3b122012-10-01 14:16:43 -0400411int OpenRecoveryScript::Install_Command(string Zip) {
412 // Install zip
413 string ret_string;
414 int ret_val = 0, wipe_cache = 0;
415
416 PartitionManager.Mount_All_Storage();
417 if (Zip.substr(0, 1) != "/") {
418 // Relative path given
419 string Full_Path;
420
Dees_Troy6ed34b72013-01-25 15:01:29 +0000421 Full_Path = DataManager::GetCurrentStoragePath();
Dees_Troy83a3b122012-10-01 14:16:43 -0400422 Full_Path += "/" + Zip;
423 LOGI("Full zip path: '%s'\n", Full_Path.c_str());
424 if (!TWFunc::Path_Exists(Full_Path)) {
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;
Dees_Troy6ed34b72013-01-25 15:01:29 +0000428 } else if (DataManager::GetIntValue(TW_HAS_DUAL_STORAGE)) {
429 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400430 LOGI("Zip file not found on external storage, trying internal...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000431 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400432 } else {
433 LOGI("Zip file not found on internal storage, trying external...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +0000434 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400435 }
Dees_Troy6ed34b72013-01-25 15:01:29 +0000436 Full_Path = DataManager::GetCurrentStoragePath();
Dees_Troy83a3b122012-10-01 14:16:43 -0400437 Full_Path += "/" + Zip;
438 LOGI("Full zip path: '%s'\n", Full_Path.c_str());
Dees_Troy6ed34b72013-01-25 15:01:29 +0000439 ret_string = Locate_Zip_File(Full_Path, DataManager::GetCurrentStoragePath());
Dees_Troy83a3b122012-10-01 14:16:43 -0400440 if (!ret_string.empty())
441 Full_Path = ret_string;
442 }
443 }
444 Zip = Full_Path;
445 } else {
446 // Full path given
447 if (!TWFunc::Path_Exists(Zip)) {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000448 ret_string = Locate_Zip_File(Zip, DataManager::GetCurrentStoragePath());
Dees_Troy83a3b122012-10-01 14:16:43 -0400449 if (!ret_string.empty())
450 Zip = ret_string;
451 }
452 }
453
454 if (!TWFunc::Path_Exists(Zip)) {
455 // zip file doesn't exist
456 ui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
457 ret_val = 1;
458 } else {
459 ui_print("Installing zip file '%s'\n", Zip.c_str());
460 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
461 }
462 if (ret_val != 0) {
463 LOGE("Error installing zip file '%s'\n", Zip.c_str());
464 ret_val = 1;
465 } else if (wipe_cache)
466 PartitionManager.Wipe_By_Path("/cache");
467
468 return ret_val;
469}
470
Dees_Troy812660f2012-09-20 09:55:17 -0400471string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
472 string Path = TWFunc::Get_Path(Zip);
473 string File = TWFunc::Get_Filename(Zip);
474 string pathCpy = Path;
475 string wholePath;
476 size_t pos = Path.find("/", 1);
477
478 while (pos != string::npos)
479 {
480 pathCpy = Path.substr(pos, Path.size() - pos);
481 wholePath = pathCpy + "/" + File;
482 if (TWFunc::Path_Exists(wholePath))
483 return wholePath;
484 wholePath = Storage_Root + "/" + wholePath;
485 if (TWFunc::Path_Exists(wholePath))
486 return wholePath;
487
488 pos = Path.find("/", pos + 1);
489 }
490 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400491}
492
493int OpenRecoveryScript::Backup_Command(string Options) {
494 char value1[SCRIPT_COMMAND_SIZE];
495 int line_len, i;
496
497 strcpy(value1, Options.c_str());
498
Dees_Troy6ed34b72013-01-25 15:01:29 +0000499 DataManager::SetValue(TW_BACKUP_SYSTEM_VAR, 0);
500 DataManager::SetValue(TW_BACKUP_DATA_VAR, 0);
501 DataManager::SetValue(TW_BACKUP_CACHE_VAR, 0);
502 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
503 DataManager::SetValue(TW_BACKUP_SP1_VAR, 0);
504 DataManager::SetValue(TW_BACKUP_SP2_VAR, 0);
505 DataManager::SetValue(TW_BACKUP_SP3_VAR, 0);
506 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
507 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
508 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
509 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
510 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
511 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 0);
Dees_Troy83a3b122012-10-01 14:16:43 -0400512
513 ui_print("Setting backup options:\n");
514 line_len = Options.size();
515 for (i=0; i<line_len; i++) {
516 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000517 DataManager::SetValue(TW_BACKUP_SYSTEM_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400518 ui_print("System\n");
519 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000520 DataManager::SetValue(TW_BACKUP_DATA_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400521 ui_print("Data\n");
522 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000523 DataManager::SetValue(TW_BACKUP_CACHE_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400524 ui_print("Cache\n");
525 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000526 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400527 ui_print("Recovery\n");
528 } else if (Options.substr(i, 1) == "1") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000529 DataManager::SetValue(TW_BACKUP_SP1_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400530 ui_print("%s\n", "Special1");
531 } else if (Options.substr(i, 1) == "2") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000532 DataManager::SetValue(TW_BACKUP_SP2_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400533 ui_print("%s\n", "Special2");
534 } else if (Options.substr(i, 1) == "3") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000535 DataManager::SetValue(TW_BACKUP_SP3_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400536 ui_print("%s\n", "Special3");
537 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000538 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400539 ui_print("Boot\n");
540 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000541 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400542 ui_print("Android Secure\n");
543 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000544 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400545 ui_print("SD-Ext\n");
546 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000547 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400548 ui_print("Compression is on\n");
549 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
Dees_Troy6ed34b72013-01-25 15:01:29 +0000550 DataManager::SetValue(TW_SKIP_MD5_GENERATE_VAR, 1);
Dees_Troy83a3b122012-10-01 14:16:43 -0400551 ui_print("MD5 Generation is off\n");
552 }
553 }
554 if (!PartitionManager.Run_Backup()) {
555 LOGE("Backup failed!\n");
556 return 1;
557 }
558 ui_print("Backup complete!\n");
559 return 0;
560}
Dees_Troy6ed34b72013-01-25 15:01:29 +0000561
562void OpenRecoveryScript::Run_OpenRecoveryScript(void) {
563 DataManager::SetValue("tw_back", "main");
564 DataManager::SetValue("tw_action", "openrecoveryscript");
565 DataManager::SetValue("tw_has_action2", "0");
566 DataManager::SetValue("tw_action2", "");
567 DataManager::SetValue("tw_action2_param", "");
568 DataManager::SetValue("tw_action_text1", "Running OpenRecoveryScript");
569 DataManager::SetValue("tw_action_text2", "");
570 DataManager::SetValue("tw_complete_text1", "OpenRecoveryScript Complete");
571 DataManager::SetValue("tw_has_cancel", 0);
572 DataManager::SetValue("tw_show_reboot", 0);
573 if (gui_startPage("action_page") != 0) {
574 LOGE("Failed to load OpenRecoveryScript GUI page.\n");
575 }
Dees_Troy6bdcbb12013-01-26 14:07:43 +0000576}