blob: 23641f7f3fbe6d5932ef56b65ec33eaf3ab43857 [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);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400201 if (PartitionManager.Check_Backup_Name(true) != 0) {
202 ret_val = 1;
203 continue;
204 }
Dees_Troy812660f2012-09-20 09:55:17 -0400205 } else {
206 char empt[50];
207 strcpy(empt, "(Current Date)");
208 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
209 }
210
211 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0);
212 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0);
213 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0);
214 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0);
215 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0);
216 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0);
217 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0);
218 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0);
219 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0);
220 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
221 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0);
222 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0);
223 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0);
224
225 ui_print("Setting backup options:\n");
226 line_len = strlen(value1);
227 for (i=0; i<line_len; i++) {
228 if (value1[i] == 'S' || value1[i] == 's') {
229 DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 1);
230 ui_print("System\n");
231 } else if (value1[i] == 'D' || value1[i] == 'd') {
232 DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 1);
233 ui_print("Data\n");
234 } else if (value1[i] == 'C' || value1[i] == 'c') {
235 DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 1);
236 ui_print("Cache\n");
237 } else if (value1[i] == 'R' || value1[i] == 'r') {
238 DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 1);
239 ui_print("Recovery\n");
240 } else if (value1[i] == '1') {
241 DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 1);
242 ui_print("%s\n", "Special1");
243 } else if (value1[i] == '2') {
244 DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 1);
245 ui_print("%s\n", "Special2");
246 } else if (value1[i] == '3') {
247 DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 1);
248 ui_print("%s\n", "Special3");
249 } else if (value1[i] == 'B' || value1[i] == 'b') {
250 DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 1);
251 ui_print("Boot\n");
252 } else if (value1[i] == 'A' || value1[i] == 'a') {
253 DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 1);
254 ui_print("Android Secure\n");
255 } else if (value1[i] == 'E' || value1[i] == 'e') {
256 DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 1);
257 ui_print("SD-Ext\n");
258 } else if (value1[i] == 'O' || value1[i] == 'o') {
259 DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 1);
260 ui_print("Compression is on\n");
261 } else if (value1[i] == 'M' || value1[i] == 'm') {
262 DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 1);
263 ui_print("MD5 Generation is off\n");
264 }
265 }
266 if (!PartitionManager.Run_Backup()) {
267 ret_val = 1;
268 LOGE("Backup failed!\n");
269 } else
270 ui_print("Backup complete!\n");
271 } else if (strcmp(command, "restore") == 0) {
272 // Restore
273 PartitionManager.Mount_All_Storage();
274 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 0);
275
276 string val = value, restore_folder, restore_partitions;
277 size_t pos = val.find_last_of(" ");
278 if (pos == string::npos) {
279 ui_print("Malformed restore parameter: '%s'\n", value1);
280 ret_val = 1;
281 continue;
282 }
283 restore_folder = val.substr(0, pos);
284 char folder_path[512], partitions[512];
285 strcpy(folder_path, restore_folder.c_str());
286 restore_partitions = val.substr(pos + 1, val.size() - pos - 1);
287 strcpy(partitions, restore_partitions.c_str());
288 LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions);
289 ui_print("Restoring '%s'\n", folder_path);
290
291 if (folder_path[0] != '/') {
292 char backup_folder[512];
293 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
294 LOGI("Restoring relative path: '%s'\n", backup_folder);
295 if (!TWFunc::Path_Exists(backup_folder)) {
296 if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) {
297 if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) {
298 LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path);
299 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0);
300 } else {
301 LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path);
302 DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1);
303 }
304 sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path);
305 LOGI("2Restoring relative path: '%s'\n", backup_folder);
306 }
307 }
308 strcpy(folder_path, backup_folder);
309 } else {
310 if (folder_path[strlen(folder_path) - 1] == '/')
311 strcat(folder_path, ".");
312 else
313 strcat(folder_path, "/.");
314 }
315 if (!TWFunc::Path_Exists(folder_path)) {
316 ui_print("Unable to locate backup '%s'\n", folder_path);
317 ret_val = 1;
318 continue;
319 }
320 DataManager_SetStrValue("tw_restore", folder_path);
321
322 PartitionManager.Set_Restore_Files(folder_path);
323 if (strlen(partitions) != 0) {
324 int tw_restore_system = 0;
325 int tw_restore_data = 0;
326 int tw_restore_cache = 0;
327 int tw_restore_recovery = 0;
328 int tw_restore_boot = 0;
329 int tw_restore_andsec = 0;
330 int tw_restore_sdext = 0;
331 int tw_restore_sp1 = 0;
332 int tw_restore_sp2 = 0;
333 int tw_restore_sp3 = 0;
334
335 memset(value2, 0, sizeof(value2));
336 strcpy(value2, partitions);
337 ui_print("Setting restore options: '%s':\n", value2);
338 line_len = strlen(value2);
339 for (i=0; i<line_len; i++) {
340 if ((value2[i] == 'S' || value2[i] == 's') && DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) {
341 tw_restore_system = 1;
342 ui_print("System\n");
343 } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) {
344 tw_restore_data = 1;
345 ui_print("Data\n");
346 } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) {
347 tw_restore_cache = 1;
348 ui_print("Cache\n");
349 } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) {
350 tw_restore_recovery = 1;
351 ui_print("Recovery\n");
352 } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
353 tw_restore_sp1 = 1;
354 ui_print("%s\n", "Special1");
355 } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
356 tw_restore_sp2 = 1;
357 ui_print("%s\n", "Special2");
358 } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
359 tw_restore_sp3 = 1;
360 ui_print("%s\n", "Special3");
361 } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) {
362 tw_restore_boot = 1;
363 ui_print("Boot\n");
364 } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) {
365 tw_restore_andsec = 1;
366 ui_print("Android Secure\n");
367 } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) {
368 tw_restore_sdext = 1;
369 ui_print("SD-Ext\n");
370 } else if (value2[i] == 'M' || value2[i] == 'm') {
371 DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1);
372 ui_print("MD5 check skip is on\n");
373 }
374 }
375
376 if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system)
377 DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0);
378 if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data)
379 DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0);
380 if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache)
381 DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0);
382 if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery)
383 DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0);
384 if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot)
385 DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0);
386 if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec)
387 DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0);
388 if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext)
389 DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0);
390 if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1)
391 DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0);
392 if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2)
393 DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0);
394 if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3)
395 DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0);
396 } else {
397 ui_print("No restore options set.\n");
398 ret_val = 1;
399 continue;
400 }
401 PartitionManager.Run_Restore(folder_path);
402 ui_print("Restore complete!\n");
403 } else if (strcmp(command, "mount") == 0) {
404 // Mount
405 if (value[0] != '/') {
406 strcpy(mount, "/");
407 strcat(mount, value);
408 } else
409 strcpy(mount, value);
410 if (PartitionManager.Mount_By_Path(mount, true))
411 ui_print("Mounted '%s'\n", mount);
412 } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
413 // Unmount
414 if (value[0] != '/') {
415 strcpy(mount, "/");
416 strcat(mount, value);
417 } else
418 strcpy(mount, value);
419 if (PartitionManager.UnMount_By_Path(mount, true))
420 ui_print("Unmounted '%s'\n", mount);
421 } else if (strcmp(command, "set") == 0) {
422 // Set value
423 tok = strtok(value, " ");
424 strcpy(value1, tok);
425 tok = strtok(NULL, " ");
426 strcpy(value2, tok);
427 ui_print("Setting '%s' to '%s'\n", value1, value2);
428 DataManager_SetStrValue(value1, value2);
429 } else if (strcmp(command, "mkdir") == 0) {
430 // Make directory (recursive)
431 ui_print("Making directory (recursive): '%s'\n", value);
432 if (TWFunc::Recursive_Mkdir(value)) {
433 LOGE("Unable to create folder: '%s'\n", value);
434 ret_val = 1;
435 }
436 } else if (strcmp(command, "reboot") == 0) {
437 // Reboot
438 } else if (strcmp(command, "cmd") == 0) {
439 if (cindex != 0) {
440 system(value);
441 } else {
442 LOGE("No value given for cmd\n");
443 }
444 } else if (strcmp(command, "print") == 0) {
445 ui_print("%s\n", value);
446 } else {
447 LOGE("Unrecognized script command: '%s'\n", command);
448 ret_val = 1;
449 }
450 }
451 fclose(fp);
452 ui_print("Done processing script file\n");
453 } else {
454 LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP);
455 return 1;
456 }
457 return ret_val;
458}
459
460string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) {
461 string Path = TWFunc::Get_Path(Zip);
462 string File = TWFunc::Get_Filename(Zip);
463 string pathCpy = Path;
464 string wholePath;
465 size_t pos = Path.find("/", 1);
466
467 while (pos != string::npos)
468 {
469 pathCpy = Path.substr(pos, Path.size() - pos);
470 wholePath = pathCpy + "/" + File;
471 if (TWFunc::Path_Exists(wholePath))
472 return wholePath;
473 wholePath = Storage_Root + "/" + wholePath;
474 if (TWFunc::Path_Exists(wholePath))
475 return wholePath;
476
477 pos = Path.find("/", pos + 1);
478 }
479 return "";
480}