/* OpenRecoveryScript class for TWRP * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 and * only version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. * * The code was written from scratch by Dees_Troy dees_troy at * yahoo * * Copyright (c) 2012 */ #include #include #include #include #include #include #include #include #include #include #include "twrp-functions.hpp" #include "partitions.hpp" #include "common.h" #include "openrecoveryscript.hpp" #include "variables.h" #include "adb_install.h" extern "C" { #include "data.h" #include "twinstall.h" #include "gui/gui.h" int TWinstall_zip(const char* path, int* wipe_cache); } extern RecoveryUI* ui; static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript"; static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript"; #define SCRIPT_COMMAND_SIZE 512 int OpenRecoveryScript::check_for_script_file(void) { if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { LOGE("Unable to mount /cache for OpenRecoveryScript support.\n"); return 0; } if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE); // Copy script file to /tmp TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755); // Delete the file from /cache unlink(SCRIPT_FILE_CACHE); return 1; } return 0; } int OpenRecoveryScript::run_script_file(void) { FILE *fp = fopen(SCRIPT_FILE_TMP, "r"); int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0; char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE], value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE], value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE]; char *val_start, *tok; if (fp != NULL) { DataManager_SetIntValue(TW_SIMULATE_ACTIONS, 0); while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) { cindex = 0; line_len = strlen(script_line); if (line_len < 2) continue; // there's a blank line or line is too short to contain a command //ui_print("script line: '%s'\n", script_line); for (i=0; i 0) { tw_restore_system = 1; ui_print("System\n"); } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) { tw_restore_data = 1; ui_print("Data\n"); } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) { tw_restore_cache = 1; ui_print("Cache\n"); } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) { tw_restore_recovery = 1; ui_print("Recovery\n"); } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) { tw_restore_sp1 = 1; ui_print("%s\n", "Special1"); } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) { tw_restore_sp2 = 1; ui_print("%s\n", "Special2"); } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) { tw_restore_sp3 = 1; ui_print("%s\n", "Special3"); } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) { tw_restore_boot = 1; ui_print("Boot\n"); } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) { tw_restore_andsec = 1; ui_print("Android Secure\n"); } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) { tw_restore_sdext = 1; ui_print("SD-Ext\n"); } else if (value2[i] == 'M' || value2[i] == 'm') { DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1); ui_print("MD5 check skip is on\n"); } } if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system) DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data) DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache) DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery) DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot) DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec) DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext) DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1) DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2) DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0); if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3) DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0); } PartitionManager.Run_Restore(folder_path); ui_print("Restore complete!\n"); } else if (strcmp(command, "mount") == 0) { // Mount if (value[0] != '/') { strcpy(mount, "/"); strcat(mount, value); } else strcpy(mount, value); if (PartitionManager.Mount_By_Path(mount, true)) ui_print("Mounted '%s'\n", mount); } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { // Unmount if (value[0] != '/') { strcpy(mount, "/"); strcat(mount, value); } else strcpy(mount, value); if (PartitionManager.UnMount_By_Path(mount, true)) ui_print("Unmounted '%s'\n", mount); } else if (strcmp(command, "set") == 0) { // Set value tok = strtok(value, " "); strcpy(value1, tok); tok = strtok(NULL, " "); strcpy(value2, tok); ui_print("Setting '%s' to '%s'\n", value1, value2); DataManager_SetStrValue(value1, value2); } else if (strcmp(command, "mkdir") == 0) { // Make directory (recursive) ui_print("Making directory (recursive): '%s'\n", value); if (TWFunc::Recursive_Mkdir(value)) { LOGE("Unable to create folder: '%s'\n", value); ret_val = 1; } } else if (strcmp(command, "reboot") == 0) { // Reboot } else if (strcmp(command, "cmd") == 0) { if (cindex != 0) { string status; TWFunc::Exec_Cmd(value, status); } else { LOGE("No value given for cmd\n"); } } else if (strcmp(command, "print") == 0) { ui_print("%s\n", value); } else if (strcmp(command, "sideload") == 0) { // ADB Sideload DataManager_SetStrValue("tw_page_done", "0"); DataManager_SetStrValue("tw_back", "main"); DataManager_SetStrValue("tw_action", "adbsideload"); DataManager_SetStrValue("tw_has_action2", "0"); DataManager_SetStrValue("tw_action2", ""); DataManager_SetStrValue("tw_action2_param", ""); DataManager_SetStrValue("tw_action_text1", "ADB Sideload"); DataManager_SetStrValue("tw_action_text2", "Usage: adb sideload filename.zip"); DataManager_SetStrValue("tw_complete_text1", "ADB Sideload Complete"); DataManager_SetIntValue("tw_has_cancel", 1); DataManager_SetIntValue("tw_show_reboot", 1); DataManager_SetStrValue("tw_cancel_action", "adbsideloadcancel"); DataManager_SetStrValue("tw_cancel_param", ""); if (gui_startPage("action_page") != 0) { LOGE("Failed to load sideload GUI page.\n"); } DataManager_SetIntValue("tw_page_done", 1); gui_console_only(); sideload = 1; // Causes device to go to the home screen afterwards ui_print("Sideload finished.\n"); } else { LOGE("Unrecognized script command: '%s'\n", command); ret_val = 1; } } fclose(fp); ui_print("Done processing script file\n"); } else { LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP); return 1; } if (install_cmd && DataManager_GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager_GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { string status; ui_print("Injecting TWRP into boot image...\n"); TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); if (Boot == NULL || Boot->Current_File_System != "emmc") TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", status); else { string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; TWFunc::Exec_Cmd(injectcmd.c_str(), status); } ui_print("TWRP injection complete.\n"); } if (sideload) ret_val = 1; // Forces booting to the home page after sideload return ret_val; } int OpenRecoveryScript::Install_Command(string Zip) { // Install zip string ret_string; int ret_val = 0, wipe_cache = 0; PartitionManager.Mount_All_Storage(); if (Zip.substr(0, 1) != "/") { // Relative path given string Full_Path; Full_Path = DataManager_GetCurrentStoragePath(); Full_Path += "/" + Zip; LOGI("Full zip path: '%s'\n", Full_Path.c_str()); if (!TWFunc::Path_Exists(Full_Path)) { ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); if (!ret_string.empty()) { Full_Path = ret_string; } else if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) { if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) { LOGI("Zip file not found on external storage, trying internal...\n"); DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0); } else { LOGI("Zip file not found on internal storage, trying external...\n"); DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1); } Full_Path = DataManager_GetCurrentStoragePath(); Full_Path += "/" + Zip; LOGI("Full zip path: '%s'\n", Full_Path.c_str()); ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); if (!ret_string.empty()) Full_Path = ret_string; } } Zip = Full_Path; } else { // Full path given if (!TWFunc::Path_Exists(Zip)) { ret_string = Locate_Zip_File(Zip, DataManager_GetCurrentStoragePath()); if (!ret_string.empty()) Zip = ret_string; } } if (!TWFunc::Path_Exists(Zip)) { // zip file doesn't exist ui_print("Unable to locate zip file '%s'.\n", Zip.c_str()); ret_val = 1; } else { ui_print("Installing zip file '%s'\n", Zip.c_str()); ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache); } if (ret_val != 0) { LOGE("Error installing zip file '%s'\n", Zip.c_str()); ret_val = 1; } else if (wipe_cache) PartitionManager.Wipe_By_Path("/cache"); return ret_val; } string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) { string Path = TWFunc::Get_Path(Zip); string File = TWFunc::Get_Filename(Zip); string pathCpy = Path; string wholePath; size_t pos = Path.find("/", 1); while (pos != string::npos) { pathCpy = Path.substr(pos, Path.size() - pos); wholePath = pathCpy + "/" + File; if (TWFunc::Path_Exists(wholePath)) return wholePath; wholePath = Storage_Root + "/" + wholePath; if (TWFunc::Path_Exists(wholePath)) return wholePath; pos = Path.find("/", pos + 1); } return ""; } int OpenRecoveryScript::Backup_Command(string Options) { char value1[SCRIPT_COMMAND_SIZE]; int line_len, i; strcpy(value1, Options.c_str()); DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0); DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0); DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0); DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0); DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0); DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0); DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0); DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0); DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0); DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0); DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0); ui_print("Setting backup options:\n"); line_len = Options.size(); for (i=0; i