blob: 8e95555f75ebdc9534dea7471c6416add7815dea [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>
33
34#include "twrp-functions.hpp"
35#include "partitions.hpp"
36#include "common.h"
37#include "openrecoveryscript.hpp"
38#include "variables.h"
Dees_Troy4fc00242013-01-17 19:22:12 +000039#include "adb_install.h"
Dees_Troy812660f2012-09-20 09:55:17 -040040extern "C" {
41#include "data.h"
42#include "twinstall.h"
Dees_Troy4bc09ae2013-01-18 17:00:54 +000043#include "gui/gui.h"
Dees_Troy812660f2012-09-20 09:55:17 -040044int TWinstall_zip(const char* path, int* wipe_cache);
45}
46
Dees_Troy4fc00242013-01-17 19:22:12 +000047extern RecoveryUI* ui;
Dees_Troy812660f2012-09-20 09:55:17 -040048static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript";
49static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript";
50#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_Troy3ac71a52012-10-01 19:46:11 -040077 DataManager_SetIntValue(TW_SIMULATE_ACTIONS, 0);
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
83 //ui_print("script line: '%s'\n", script_line);
84 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);
98 LOGI("command is: '%s' and ", command);
99 val_start = script_line;
100 val_start += cindex + 1;
101 strncpy(value, val_start, line_len - cindex - remove_nl);
102 LOGI("value is: '%s'\n", value);
103 } else {
104 strncpy(command, script_line, line_len - remove_nl + 1);
105 ui_print("command is: '%s' and there is no value\n", command);
106 }
107 if (strcmp(command, "install") == 0) {
Dees_Troy83a3b122012-10-01 14:16:43 -0400108 // Install Zip
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000109 PartitionManager.Mount_All_Storage();
Dees_Troy83a3b122012-10-01 14:16:43 -0400110 ret_val = Install_Command(value);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400111 install_cmd = -1;
Dees_Troy812660f2012-09-20 09:55:17 -0400112 } else if (strcmp(command, "wipe") == 0) {
113 // Wipe
114 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
115 ui_print("-- Wiping Cache Partition...\n");
116 PartitionManager.Wipe_By_Path("/cache");
117 ui_print("-- Cache Partition Wipe Complete!\n");
118 } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
119 ui_print("-- Wiping Dalvik Cache...\n");
120 PartitionManager.Wipe_Dalvik_Cache();
121 ui_print("-- Dalvik Cache Wipe Complete!\n");
122 } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
123 ui_print("-- Wiping Data Partition...\n");
124 PartitionManager.Factory_Reset();
125 ui_print("-- Data Partition Wipe Complete!\n");
126 } else {
127 LOGE("Error with wipe command value: '%s'\n", value);
128 ret_val = 1;
129 }
130 } else if (strcmp(command, "backup") == 0) {
131 // Backup
132 tok = strtok(value, " ");
133 strcpy(value1, tok);
134 tok = strtok(NULL, " ");
135 if (tok != NULL) {
136 memset(value2, 0, sizeof(value2));
137 strcpy(value2, tok);
138 line_len = strlen(tok);
139 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
140 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
141 remove_nl = 2;
142 else
143 remove_nl = 1;
144 } else
145 remove_nl = 0;
146 strncpy(value2, tok, line_len - remove_nl);
147 DataManager_SetStrValue(TW_BACKUP_NAME, value2);
148 ui_print("Backup folder set to '%s'\n", value2);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400149 if (PartitionManager.Check_Backup_Name(true) != 0) {
150 ret_val = 1;
151 continue;
152 }
Dees_Troy812660f2012-09-20 09:55:17 -0400153 } else {
154 char empt[50];
155 strcpy(empt, "(Current Date)");
156 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
157 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400158 ret_val = Backup_Command(value1);
Dees_Troy812660f2012-09-20 09:55:17 -0400159 } else if (strcmp(command, "restore") == 0) {
160 // Restore
161 PartitionManager.Mount_All_Storage();
162 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 0);
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000163 char folder_path[512], partitions[512];
Dees_Troy812660f2012-09-20 09:55:17 -0400164
165 string val = value, restore_folder, restore_partitions;
166 size_t pos = val.find_last_of(" ");
167 if (pos == string::npos) {
Dees_Troydc8bc1b2013-01-17 01:39:28 +0000168 restore_folder = value;
169 partitions[0] = '\0';
170 } else {
171 restore_folder = val.substr(0, pos);
172 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
173 strcpy(partitions, restore_partitions.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400174 }
Dees_Troy812660f2012-09-20 09:55:17 -0400175 strcpy(folder_path, restore_folder.c_str());
Dees_Troy812660f2012-09-20 09:55:17 -0400176 LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
177 ui_print("Restoring '%s'\n", folder_path);
178
179 if (folder_path[0] != '/') {
180 char backup_folder[512];
181 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
182 LOGI("Restoring relative path: '%s'\n", backup_folder);
183 if (!TWFunc::Path_Exists(backup_folder)) {
184 if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
185 if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
186 LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path);
187 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0);
188 } else {
189 LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path);
190 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1);
191 }
192 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
193 LOGI("2Restoring relative path: '%s'\n", backup_folder);
194 }
195 }
196 strcpy(folder_path, backup_folder);
197 } else {
198 if (folder_path[strlen(folder_path) - 1] == '/')
199 strcat(folder_path, ".");
200 else
201 strcat(folder_path, "/.");
202 }
203 if (!TWFunc::Path_Exists(folder_path)) {
204 ui_print("Unable to locate backup '%s'\n", folder_path);
205 ret_val = 1;
206 continue;
207 }
208 DataManager_SetStrValue("tw_restore", folder_path);
209
210 PartitionManager.Set_Restore_Files(folder_path);
211 if (strlen(partitions) != 0) {
212 int tw_restore_system = 0;
213 int tw_restore_data = 0;
214 int tw_restore_cache = 0;
215 int tw_restore_recovery = 0;
216 int tw_restore_boot = 0;
217 int tw_restore_andsec = 0;
218 int tw_restore_sdext = 0;
219 int tw_restore_sp1 = 0;
220 int tw_restore_sp2 = 0;
221 int tw_restore_sp3 = 0;
222
223 memset(value2, 0, sizeof(value2));
224 strcpy(value2, partitions);
225 ui_print("Setting restore options: '%s':\n", value2);
226 line_len = strlen(value2);
227 for (i=0; i<line_len; i++) {
228 if ((value2[i] == 'S' || value2[i] == 's') && DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) {
229 tw_restore_system = 1;
230 ui_print("System\n");
231 } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) {
232 tw_restore_data = 1;
233 ui_print("Data\n");
234 } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) {
235 tw_restore_cache = 1;
236 ui_print("Cache\n");
237 } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) {
238 tw_restore_recovery = 1;
239 ui_print("Recovery\n");
240 } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
241 tw_restore_sp1 = 1;
242 ui_print("%s\n", "Special1");
243 } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
244 tw_restore_sp2 = 1;
245 ui_print("%s\n", "Special2");
246 } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
247 tw_restore_sp3 = 1;
248 ui_print("%s\n", "Special3");
249 } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) {
250 tw_restore_boot = 1;
251 ui_print("Boot\n");
252 } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) {
253 tw_restore_andsec = 1;
254 ui_print("Android Secure\n");
255 } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) {
256 tw_restore_sdext = 1;
257 ui_print("SD-Ext\n");
258 } else if (value2[i] == 'M' || value2[i] == 'm') {
259 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1);
260 ui_print("MD5 check skip is on\n");
261 }
262 }
263
264 if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system)
265 DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0);
266 if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data)
267 DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0);
268 if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache)
269 DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0);
270 if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery)
271 DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0);
272 if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot)
273 DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0);
274 if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec)
275 DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0);
276 if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext)
277 DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0);
278 if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1)
279 DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0);
280 if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2)
281 DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0);
282 if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3)
283 DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0);
Dees_Troy812660f2012-09-20 09:55:17 -0400284 }
285 PartitionManager.Run_Restore(folder_path);
286 ui_print("Restore complete!\n");
287 } else if (strcmp(command, "mount") == 0) {
288 // Mount
289 if (value[0] != '/') {
290 strcpy(mount, "/");
291 strcat(mount, value);
292 } else
293 strcpy(mount, value);
294 if (PartitionManager.Mount_By_Path(mount, true))
295 ui_print("Mounted '%s'\n", mount);
296 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
297 // Unmount
298 if (value[0] != '/') {
299 strcpy(mount, "/");
300 strcat(mount, value);
301 } else
302 strcpy(mount, value);
303 if (PartitionManager.UnMount_By_Path(mount, true))
304 ui_print("Unmounted '%s'\n", mount);
305 } 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);
311 ui_print("Setting '%s' to '%s'\n", value1, value2);
312 DataManager_SetStrValue(value1, value2);
313 } else if (strcmp(command, "mkdir") == 0) {
314 // Make directory (recursive)
315 ui_print("Making directory (recursive): '%s'\n", value);
316 if (TWFunc::Recursive_Mkdir(value)) {
317 LOGE("Unable to create folder: '%s'\n", value);
318 ret_val = 1;
319 }
320 } else if (strcmp(command, "reboot") == 0) {
321 // Reboot
322 } else if (strcmp(command, "cmd") == 0) {
323 if (cindex != 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324 string status;
325 TWFunc::Exec_Cmd(value, status);
Dees_Troy812660f2012-09-20 09:55:17 -0400326 } else {
327 LOGE("No value given for cmd\n");
328 }
329 } else if (strcmp(command, "print") == 0) {
330 ui_print("%s\n", value);
Dees_Troy4fc00242013-01-17 19:22:12 +0000331 } else if (strcmp(command, "sideload") == 0) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000332 // ADB Sideload
333 DataManager_SetStrValue("tw_page_done", "0");
334 DataManager_SetStrValue("tw_back", "main");
335 DataManager_SetStrValue("tw_action", "adbsideload");
336 DataManager_SetStrValue("tw_has_action2", "0");
337 DataManager_SetStrValue("tw_action2", "");
338 DataManager_SetStrValue("tw_action2_param", "");
339 DataManager_SetStrValue("tw_action_text1", "ADB Sideload");
340 DataManager_SetStrValue("tw_action_text2", "Usage: adb sideload filename.zip");
341 DataManager_SetStrValue("tw_complete_text1", "ADB Sideload Complete");
342 DataManager_SetIntValue("tw_has_cancel", 1);
343 DataManager_SetIntValue("tw_show_reboot", 1);
344 DataManager_SetStrValue("tw_cancel_action", "adbsideloadcancel");
345 DataManager_SetStrValue("tw_cancel_param", "");
346 if (gui_startPage("action_page") != 0) {
347 LOGE("Failed to load sideload GUI page.\n");
Dees_Troy4fc00242013-01-17 19:22:12 +0000348 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000349 DataManager_SetIntValue("tw_page_done", 1);
350 gui_console_only();
351 sideload = 1; // Causes device to go to the home screen afterwards
352 ui_print("Sideload finished.\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400353 } else {
354 LOGE("Unrecognized script command: '%s'\n", command);
355 ret_val = 1;
356 }
357 }
358 fclose(fp);
359 ui_print("Done processing script file\n");
360 } else {
361 LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
362 return 1;
363 }
Dees_Troy06b4fe92012-10-16 11:43:20 -0400364 if (install_cmd && DataManager_GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager_GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500365 string status;
Dees_Troy06b4fe92012-10-16 11:43:20 -0400366 ui_print("Injecting TWRP into boot image...\n");
367 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
368 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500369 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", status);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400370 else {
371 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 -0500372 TWFunc::Exec_Cmd(injectcmd.c_str(), status);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400373 }
374 ui_print("TWRP injection complete.\n");
375 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000376 if (sideload)
377 ret_val = 1; // Forces booting to the home page after sideload
Dees_Troy812660f2012-09-20 09:55:17 -0400378 return ret_val;
379}
380
Dees_Troy83a3b122012-10-01 14:16:43 -0400381int OpenRecoveryScript::Install_Command(string Zip) {
382 // Install zip
383 string ret_string;
384 int ret_val = 0, wipe_cache = 0;
385
386 PartitionManager.Mount_All_Storage();
387 if (Zip.substr(0, 1) != "/") {
388 // Relative path given
389 string Full_Path;
390
391 Full_Path = DataManager_GetCurrentStoragePath();
392 Full_Path += "/" + Zip;
393 LOGI("Full zip path: '%s'\n", Full_Path.c_str());
394 if (!TWFunc::Path_Exists(Full_Path)) {
395 ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath());
396 if (!ret_string.empty()) {
397 Full_Path = ret_string;
398 } else if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
399 if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
400 LOGI("Zip file not found on external storage, trying internal...\n");
401 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0);
402 } else {
403 LOGI("Zip file not found on internal storage, trying external...\n");
404 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1);
405 }
406 Full_Path = DataManager_GetCurrentStoragePath();
407 Full_Path += "/" + Zip;
408 LOGI("Full zip path: '%s'\n", Full_Path.c_str());
409 ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath());
410 if (!ret_string.empty())
411 Full_Path = ret_string;
412 }
413 }
414 Zip = Full_Path;
415 } else {
416 // Full path given
417 if (!TWFunc::Path_Exists(Zip)) {
418 ret_string = Locate_Zip_File(Zip, DataManager_GetCurrentStoragePath());
419 if (!ret_string.empty())
420 Zip = ret_string;
421 }
422 }
423
424 if (!TWFunc::Path_Exists(Zip)) {
425 // zip file doesn't exist
426 ui_print("Unable to locate zip file '%s'.\n", Zip.c_str());
427 ret_val = 1;
428 } else {
429 ui_print("Installing zip file '%s'\n", Zip.c_str());
430 ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache);
431 }
432 if (ret_val != 0) {
433 LOGE("Error installing zip file '%s'\n", Zip.c_str());
434 ret_val = 1;
435 } else if (wipe_cache)
436 PartitionManager.Wipe_By_Path("/cache");
437
438 return ret_val;
439}
440
Dees_Troy812660f2012-09-20 09:55:17 -0400441string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
442 string Path = TWFunc::Get_Path(Zip);
443 string File = TWFunc::Get_Filename(Zip);
444 string pathCpy = Path;
445 string wholePath;
446 size_t pos = Path.find("/", 1);
447
448 while (pos != string::npos)
449 {
450 pathCpy = Path.substr(pos, Path.size() - pos);
451 wholePath = pathCpy + "/" + File;
452 if (TWFunc::Path_Exists(wholePath))
453 return wholePath;
454 wholePath = Storage_Root + "/" + wholePath;
455 if (TWFunc::Path_Exists(wholePath))
456 return wholePath;
457
458 pos = Path.find("/", pos + 1);
459 }
460 return "";
Dees_Troy83a3b122012-10-01 14:16:43 -0400461}
462
463int OpenRecoveryScript::Backup_Command(string Options) {
464 char value1[SCRIPT_COMMAND_SIZE];
465 int line_len, i;
466
467 strcpy(value1, Options.c_str());
468
469 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0);
470 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0);
471 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0);
472 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0);
473 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0);
474 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0);
475 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0);
476 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0);
477 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0);
478 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
479 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
480 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0);
481 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0);
482
483 ui_print("Setting backup options:\n");
484 line_len = Options.size();
485 for (i=0; i<line_len; i++) {
486 if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") {
487 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 1);
488 ui_print("System\n");
489 } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") {
490 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 1);
491 ui_print("Data\n");
492 } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") {
493 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 1);
494 ui_print("Cache\n");
495 } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") {
496 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 1);
497 ui_print("Recovery\n");
498 } else if (Options.substr(i, 1) == "1") {
499 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 1);
500 ui_print("%s\n", "Special1");
501 } else if (Options.substr(i, 1) == "2") {
502 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 1);
503 ui_print("%s\n", "Special2");
504 } else if (Options.substr(i, 1) == "3") {
505 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 1);
506 ui_print("%s\n", "Special3");
507 } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") {
508 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 1);
509 ui_print("Boot\n");
510 } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") {
511 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 1);
512 ui_print("Android Secure\n");
513 } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") {
514 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 1);
515 ui_print("SD-Ext\n");
516 } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") {
517 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 1);
518 ui_print("Compression is on\n");
519 } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") {
520 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 1);
521 ui_print("MD5 Generation is off\n");
522 }
523 }
524 if (!PartitionManager.Run_Backup()) {
525 LOGE("Backup failed!\n");
526 return 1;
527 }
528 ui_print("Backup complete!\n");
529 return 0;
530}