blob: bcd4acb2814437eb8e2d491a561bc389324d88a7 [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"
39extern "C" {
40#include "data.h"
41#include "twinstall.h"
42int TWinstall_zip(const char* path, int* wipe_cache);
43}
44
45static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript";
46static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript";
47#define SCRIPT_COMMAND_SIZE 512
48
49int OpenRecoveryScript::check_for_script_file(void) {
50 char exec[512];
51
52 if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) {
53 LOGE("Unable to mount /cache for OpenRecoveryScript support.\n");
54 return 0;
55 }
56 if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) {
57 LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE);
58 // Copy script file to /tmp
59 strcpy(exec, "cp ");
60 strcat(exec, SCRIPT_FILE_CACHE);
61 strcat(exec, " ");
62 strcat(exec, SCRIPT_FILE_TMP);
63 system(exec);
64 // Delete the file from /cache
65 strcpy(exec, "rm ");
66 strcat(exec, SCRIPT_FILE_CACHE);
67 system(exec);
68 return 1;
69 }
70 return 0;
71}
72
73int OpenRecoveryScript::run_script_file(void) {
74 FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
75 int ret_val = 0, cindex, line_len, i, remove_nl;
76 char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
77 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
78 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
79 char *val_start, *tok;
80
81 if (fp != NULL) {
82 while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
83 cindex = 0;
84 line_len = strlen(script_line);
85 if (line_len < 2)
86 continue; // there's a blank line or line is too short to contain a command
87 //ui_print("script line: '%s'\n", script_line);
88 for (i=0; i<line_len; i++) {
89 if ((int)script_line[i] == 32) {
90 cindex = i;
91 i = line_len;
92 }
93 }
94 memset(command, 0, sizeof(command));
95 memset(value, 0, sizeof(value));
96 if ((int)script_line[line_len - 1] == 10)
97 remove_nl = 2;
98 else
99 remove_nl = 1;
100 if (cindex != 0) {
101 strncpy(command, script_line, cindex);
102 LOGI("command is: '%s' and ", command);
103 val_start = script_line;
104 val_start += cindex + 1;
105 strncpy(value, val_start, line_len - cindex - remove_nl);
106 LOGI("value is: '%s'\n", value);
107 } else {
108 strncpy(command, script_line, line_len - remove_nl + 1);
109 ui_print("command is: '%s' and there is no value\n", command);
110 }
111 if (strcmp(command, "install") == 0) {
112 // Install zip
113 string ret_string;
114
115 PartitionManager.Mount_All_Storage();
116 if (value[0] != '/') {
117 // Relative path given
118 char full_path[SCRIPT_COMMAND_SIZE];
119
120 sprintf(full_path, "%s/%s", DataManager_GetCurrentStoragePath(), value);
121 LOGI("Full zip path: '%s'\n", full_path);
122 if (!TWFunc::Path_Exists(full_path)) {
123 ret_string = Locate_Zip_File(full_path, DataManager_GetCurrentStoragePath());
124 if (!ret_string.empty()) {
125 strcpy(full_path, ret_string.c_str());
126 } else if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
127 if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
128 LOGI("Zip file not found on external storage, trying internal...\n");
129 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0);
130 } else {
131 LOGI("Zip file not found on internal storage, trying external...\n");
132 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1);
133 }
134 sprintf(full_path, "%s/%s", DataManager_GetCurrentStoragePath(), value);
135 LOGI("Full zip path: '%s'\n", full_path);
136 ret_string = Locate_Zip_File(full_path, DataManager_GetCurrentStoragePath());
137 if (!ret_string.empty())
138 strcpy(full_path, ret_string.c_str());
139 }
140 }
141 strcpy(value, full_path);
142 } else {
143 // Full path given
144 if (!TWFunc::Path_Exists(value)) {
145 ret_string = Locate_Zip_File(value, DataManager_GetCurrentStoragePath());
146 if (!ret_string.empty())
147 strcpy(value, ret_string.c_str());
148 }
149 }
150 int wipe_cache = 0;
151 if (!TWFunc::Path_Exists(value)) {
152 // zip file doesn't exist
153 ui_print("Unable to locate zip file '%s'.\n", value);
154 ret_val = 1;
155 } else {
156 ui_print("Installing zip file '%s'\n", value);
157 ret_val = TWinstall_zip(value, &wipe_cache);
158 }
159 if (ret_val != 0) {
160 LOGE("Error installing zip file '%s'\n", value);
161 ret_val = 1;
162 } else if (wipe_cache)
163 PartitionManager.Wipe_By_Path("/cache");
164 } else if (strcmp(command, "wipe") == 0) {
165 // Wipe
166 if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
167 ui_print("-- Wiping Cache Partition...\n");
168 PartitionManager.Wipe_By_Path("/cache");
169 ui_print("-- Cache Partition Wipe Complete!\n");
170 } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
171 ui_print("-- Wiping Dalvik Cache...\n");
172 PartitionManager.Wipe_Dalvik_Cache();
173 ui_print("-- Dalvik Cache Wipe Complete!\n");
174 } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
175 ui_print("-- Wiping Data Partition...\n");
176 PartitionManager.Factory_Reset();
177 ui_print("-- Data Partition Wipe Complete!\n");
178 } else {
179 LOGE("Error with wipe command value: '%s'\n", value);
180 ret_val = 1;
181 }
182 } else if (strcmp(command, "backup") == 0) {
183 // Backup
184 tok = strtok(value, " ");
185 strcpy(value1, tok);
186 tok = strtok(NULL, " ");
187 if (tok != NULL) {
188 memset(value2, 0, sizeof(value2));
189 strcpy(value2, tok);
190 line_len = strlen(tok);
191 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) {
192 if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13)
193 remove_nl = 2;
194 else
195 remove_nl = 1;
196 } else
197 remove_nl = 0;
198 strncpy(value2, tok, line_len - remove_nl);
199 DataManager_SetStrValue(TW_BACKUP_NAME, value2);
200 ui_print("Backup folder set to '%s'\n", value2);
201 } else {
202 char empt[50];
203 strcpy(empt, "(Current Date)");
204 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
205 }
206
207 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0);
208 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0);
209 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0);
210 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0);
211 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0);
212 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0);
213 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0);
214 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0);
215 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0);
216 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
217 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
218 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0);
219 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0);
220
221 ui_print("Setting backup options:\n");
222 line_len = strlen(value1);
223 for (i=0; i<line_len; i++) {
224 if (value1[i] == 'S' || value1[i] == 's') {
225 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 1);
226 ui_print("System\n");
227 } else if (value1[i] == 'D' || value1[i] == 'd') {
228 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 1);
229 ui_print("Data\n");
230 } else if (value1[i] == 'C' || value1[i] == 'c') {
231 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 1);
232 ui_print("Cache\n");
233 } else if (value1[i] == 'R' || value1[i] == 'r') {
234 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 1);
235 ui_print("Recovery\n");
236 } else if (value1[i] == '1') {
237 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 1);
238 ui_print("%s\n", "Special1");
239 } else if (value1[i] == '2') {
240 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 1);
241 ui_print("%s\n", "Special2");
242 } else if (value1[i] == '3') {
243 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 1);
244 ui_print("%s\n", "Special3");
245 } else if (value1[i] == 'B' || value1[i] == 'b') {
246 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 1);
247 ui_print("Boot\n");
248 } else if (value1[i] == 'A' || value1[i] == 'a') {
249 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 1);
250 ui_print("Android Secure\n");
251 } else if (value1[i] == 'E' || value1[i] == 'e') {
252 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 1);
253 ui_print("SD-Ext\n");
254 } else if (value1[i] == 'O' || value1[i] == 'o') {
255 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 1);
256 ui_print("Compression is on\n");
257 } else if (value1[i] == 'M' || value1[i] == 'm') {
258 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 1);
259 ui_print("MD5 Generation is off\n");
260 }
261 }
262 if (!PartitionManager.Run_Backup()) {
263 ret_val = 1;
264 LOGE("Backup failed!\n");
265 } else
266 ui_print("Backup complete!\n");
267 } else if (strcmp(command, "restore") == 0) {
268 // Restore
269 PartitionManager.Mount_All_Storage();
270 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 0);
271
272 string val = value, restore_folder, restore_partitions;
273 size_t pos = val.find_last_of(" ");
274 if (pos == string::npos) {
275 ui_print("Malformed restore parameter: '%s'\n", value1);
276 ret_val = 1;
277 continue;
278 }
279 restore_folder = val.substr(0, pos);
280 char folder_path[512], partitions[512];
281 strcpy(folder_path, restore_folder.c_str());
282 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
283 strcpy(partitions, restore_partitions.c_str());
284 LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
285 ui_print("Restoring '%s'\n", folder_path);
286
287 if (folder_path[0] != '/') {
288 char backup_folder[512];
289 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
290 LOGI("Restoring relative path: '%s'\n", backup_folder);
291 if (!TWFunc::Path_Exists(backup_folder)) {
292 if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
293 if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
294 LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path);
295 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0);
296 } else {
297 LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path);
298 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1);
299 }
300 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
301 LOGI("2Restoring relative path: '%s'\n", backup_folder);
302 }
303 }
304 strcpy(folder_path, backup_folder);
305 } else {
306 if (folder_path[strlen(folder_path) - 1] == '/')
307 strcat(folder_path, ".");
308 else
309 strcat(folder_path, "/.");
310 }
311 if (!TWFunc::Path_Exists(folder_path)) {
312 ui_print("Unable to locate backup '%s'\n", folder_path);
313 ret_val = 1;
314 continue;
315 }
316 DataManager_SetStrValue("tw_restore", folder_path);
317
318 PartitionManager.Set_Restore_Files(folder_path);
319 if (strlen(partitions) != 0) {
320 int tw_restore_system = 0;
321 int tw_restore_data = 0;
322 int tw_restore_cache = 0;
323 int tw_restore_recovery = 0;
324 int tw_restore_boot = 0;
325 int tw_restore_andsec = 0;
326 int tw_restore_sdext = 0;
327 int tw_restore_sp1 = 0;
328 int tw_restore_sp2 = 0;
329 int tw_restore_sp3 = 0;
330
331 memset(value2, 0, sizeof(value2));
332 strcpy(value2, partitions);
333 ui_print("Setting restore options: '%s':\n", value2);
334 line_len = strlen(value2);
335 for (i=0; i<line_len; i++) {
336 if ((value2[i] == 'S' || value2[i] == 's') && DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) {
337 tw_restore_system = 1;
338 ui_print("System\n");
339 } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) {
340 tw_restore_data = 1;
341 ui_print("Data\n");
342 } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) {
343 tw_restore_cache = 1;
344 ui_print("Cache\n");
345 } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) {
346 tw_restore_recovery = 1;
347 ui_print("Recovery\n");
348 } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
349 tw_restore_sp1 = 1;
350 ui_print("%s\n", "Special1");
351 } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
352 tw_restore_sp2 = 1;
353 ui_print("%s\n", "Special2");
354 } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
355 tw_restore_sp3 = 1;
356 ui_print("%s\n", "Special3");
357 } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) {
358 tw_restore_boot = 1;
359 ui_print("Boot\n");
360 } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) {
361 tw_restore_andsec = 1;
362 ui_print("Android Secure\n");
363 } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) {
364 tw_restore_sdext = 1;
365 ui_print("SD-Ext\n");
366 } else if (value2[i] == 'M' || value2[i] == 'm') {
367 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1);
368 ui_print("MD5 check skip is on\n");
369 }
370 }
371
372 if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system)
373 DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0);
374 if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data)
375 DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0);
376 if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache)
377 DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0);
378 if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery)
379 DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0);
380 if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot)
381 DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0);
382 if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec)
383 DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0);
384 if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext)
385 DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0);
386 if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1)
387 DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0);
388 if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2)
389 DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0);
390 if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3)
391 DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0);
392 } else {
393 ui_print("No restore options set.\n");
394 ret_val = 1;
395 continue;
396 }
397 PartitionManager.Run_Restore(folder_path);
398 ui_print("Restore complete!\n");
399 } else if (strcmp(command, "mount") == 0) {
400 // Mount
401 if (value[0] != '/') {
402 strcpy(mount, "/");
403 strcat(mount, value);
404 } else
405 strcpy(mount, value);
406 if (PartitionManager.Mount_By_Path(mount, true))
407 ui_print("Mounted '%s'\n", mount);
408 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
409 // Unmount
410 if (value[0] != '/') {
411 strcpy(mount, "/");
412 strcat(mount, value);
413 } else
414 strcpy(mount, value);
415 if (PartitionManager.UnMount_By_Path(mount, true))
416 ui_print("Unmounted '%s'\n", mount);
417 } else if (strcmp(command, "set") == 0) {
418 // Set value
419 tok = strtok(value, " ");
420 strcpy(value1, tok);
421 tok = strtok(NULL, " ");
422 strcpy(value2, tok);
423 ui_print("Setting '%s' to '%s'\n", value1, value2);
424 DataManager_SetStrValue(value1, value2);
425 } else if (strcmp(command, "mkdir") == 0) {
426 // Make directory (recursive)
427 ui_print("Making directory (recursive): '%s'\n", value);
428 if (TWFunc::Recursive_Mkdir(value)) {
429 LOGE("Unable to create folder: '%s'\n", value);
430 ret_val = 1;
431 }
432 } else if (strcmp(command, "reboot") == 0) {
433 // Reboot
434 } else if (strcmp(command, "cmd") == 0) {
435 if (cindex != 0) {
436 system(value);
437 } else {
438 LOGE("No value given for cmd\n");
439 }
440 } else if (strcmp(command, "print") == 0) {
441 ui_print("%s\n", value);
442 } else {
443 LOGE("Unrecognized script command: '%s'\n", command);
444 ret_val = 1;
445 }
446 }
447 fclose(fp);
448 ui_print("Done processing script file\n");
449 } else {
450 LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
451 return 1;
452 }
453 return ret_val;
454}
455
456string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
457 string Path = TWFunc::Get_Path(Zip);
458 string File = TWFunc::Get_Filename(Zip);
459 string pathCpy = Path;
460 string wholePath;
461 size_t pos = Path.find("/", 1);
462
463 while (pos != string::npos)
464 {
465 pathCpy = Path.substr(pos, Path.size() - pos);
466 wholePath = pathCpy + "/" + File;
467 if (TWFunc::Path_Exists(wholePath))
468 return wholePath;
469 wholePath = Storage_Root + "/" + wholePath;
470 if (TWFunc::Path_Exists(wholePath))
471 return wholePath;
472
473 pos = Path.find("/", pos + 1);
474 }
475 return "";
476}