Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "cutils/properties.h" |
| 28 | extern "C" { |
| 29 | #include "minadbd/adb.h" |
| 30 | #include "bootloader.h" |
| 31 | } |
| 32 | |
| 33 | #ifdef ANDROID_RB_RESTART |
| 34 | #include "cutils/android_reboot.h" |
| 35 | #else |
| 36 | #include <sys/reboot.h> |
| 37 | #endif |
| 38 | |
| 39 | extern "C" { |
| 40 | #include "gui/gui.h" |
| 41 | } |
| 42 | #include "twcommon.h" |
| 43 | #include "twrp-functions.hpp" |
| 44 | #include "data.hpp" |
| 45 | #include "partitions.hpp" |
| 46 | #include "openrecoveryscript.hpp" |
| 47 | #include "variables.h" |
| 48 | |
| 49 | TWPartitionManager PartitionManager; |
| 50 | int Log_Offset; |
| 51 | |
| 52 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 53 | printf("%s=%s\n", key, name); |
| 54 | } |
| 55 | |
| 56 | int main(int argc, char **argv) { |
| 57 | // Recovery needs to install world-readable files, so clear umask |
| 58 | // set by init |
| 59 | umask(0); |
| 60 | |
| 61 | Log_Offset = 0; |
| 62 | |
| 63 | // Set up temporary log file (/tmp/recovery.log) |
| 64 | freopen(TMP_LOG_FILE, "a", stdout); |
| 65 | setbuf(stdout, NULL); |
| 66 | freopen(TMP_LOG_FILE, "a", stderr); |
| 67 | setbuf(stderr, NULL); |
| 68 | |
| 69 | // Handle ADB sideload |
| 70 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 71 | adb_main(argv[2]); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | time_t StartupTime = time(NULL); |
| 76 | printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&StartupTime)); |
| 77 | |
| 78 | // Load default values to set DataManager constants and handle ifdefs |
| 79 | DataManager::SetDefaultValues(); |
| 80 | printf("Starting the UI..."); |
| 81 | gui_init(); |
| 82 | printf("=> Linking mtab\n"); |
| 83 | symlink("/proc/mounts", "/etc/mtab"); |
| 84 | printf("=> Processing recovery.fstab\n"); |
| 85 | if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) { |
| 86 | LOGERR("Failing out of recovery due to problem with recovery.fstab.\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | PartitionManager.Output_Partition_Logging(); |
| 90 | // Load up all the resources |
| 91 | gui_loadResources(); |
| 92 | |
| 93 | PartitionManager.Mount_By_Path("/cache", true); |
| 94 | |
| 95 | string Zip_File, Reboot_Value; |
| 96 | bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false; |
| 97 | |
| 98 | { |
| 99 | get_args(&argc, &argv); |
| 100 | |
| 101 | int index, index2, len; |
| 102 | char* argptr; |
| 103 | char* ptr; |
| 104 | printf("Startup Commands: "); |
| 105 | for (index = 1; index < argc; index++) { |
| 106 | argptr = argv[index]; |
| 107 | printf(" '%s'", argv[index]); |
| 108 | len = strlen(argv[index]); |
| 109 | if (*argptr == '-') {argptr++; len--;} |
| 110 | if (*argptr == '-') {argptr++; len--;} |
| 111 | if (*argptr == 'u') { |
| 112 | ptr = argptr; |
| 113 | index2 = 0; |
| 114 | while (*ptr != '=' && *ptr != '\n') |
| 115 | ptr++; |
| 116 | if (*ptr) { |
| 117 | Zip_File = ptr; |
| 118 | } else |
| 119 | LOGERR("argument error specifying zip file\n"); |
| 120 | } else if (*argptr == 'w') { |
| 121 | if (len == 9) |
| 122 | Factory_Reset = true; |
| 123 | else if (len == 10) |
| 124 | Cache_Wipe = true; |
| 125 | } else if (*argptr == 'n') { |
| 126 | Perform_Backup = true; |
| 127 | } else if (*argptr == 's') { |
| 128 | ptr = argptr; |
| 129 | index2 = 0; |
| 130 | while (*ptr != '=' && *ptr != '\n') |
| 131 | ptr++; |
| 132 | if (*ptr) { |
| 133 | Reboot_Value = *ptr; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | char twrp_booted[PROPERTY_VALUE_MAX]; |
| 140 | property_get("ro.twrp.boot", twrp_booted, "0"); |
| 141 | if (strcmp(twrp_booted, "0") == 0) { |
| 142 | property_list(Print_Prop, NULL); |
| 143 | printf("\n"); |
| 144 | property_set("ro.twrp.boot", "1"); |
| 145 | } |
| 146 | |
| 147 | // Check for and run startup script if script exists |
| 148 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 149 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
| 150 | |
| 151 | #ifdef TW_INCLUDE_INJECTTWRP |
| 152 | // Back up TWRP Ramdisk if needed: |
| 153 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 154 | string result; |
| 155 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 156 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
| 157 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img", result); |
| 158 | else { |
| 159 | string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device; |
| 160 | TWFunc::Exec_Cmd(injectcmd, result); |
| 161 | } |
| 162 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 163 | #endif |
| 164 | |
| 165 | bool Keep_Going = true; |
| 166 | if (Perform_Backup) { |
| 167 | DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)"); |
| 168 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
| 169 | Keep_Going = false; |
| 170 | } |
| 171 | if (Keep_Going && !Zip_File.empty()) { |
| 172 | string ORSCommand = "install " + Zip_File; |
| 173 | |
| 174 | if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
| 175 | Keep_Going = false; |
| 176 | } |
| 177 | if (Keep_Going) { |
| 178 | if (Factory_Reset) { |
| 179 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 180 | Keep_Going = false; |
| 181 | } else if (Cache_Wipe) { |
| 182 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 183 | Keep_Going = false; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | TWFunc::Update_Log_File(); |
| 188 | // Offer to decrypt if the device is encrypted |
| 189 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 190 | LOGINFO("Is encrypted, do decrypt page first\n"); |
| 191 | if (gui_startPage("decrypt") != 0) { |
| 192 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Read the settings file |
| 197 | DataManager::ReadSettingsFile(); |
| 198 | // Run any outstanding OpenRecoveryScript |
| 199 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) { |
| 200 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 201 | } |
| 202 | // Launch the main GUI |
| 203 | gui_start(); |
| 204 | |
| 205 | // Check for su to see if the device is rooted or not |
| 206 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 207 | // Disable flashing of stock recovery |
| 208 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
| 209 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
| 210 | gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); |
| 211 | } |
| 212 | if (TWFunc::Path_Exists("/supersu/su") && !TWFunc::Path_Exists("/system/bin/su") && !TWFunc::Path_Exists("/system/xbin/su") && !TWFunc::Path_Exists("/system/bin/.ext/.su")) { |
| 213 | // Device doesn't have su installed |
| 214 | DataManager::SetValue("tw_busy", 1); |
| 215 | if (gui_startPage("installsu") != 0) { |
| 216 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 217 | } |
| 218 | } else if (TWFunc::Check_su_Perms() > 0) { |
| 219 | // su perms are set incorrectly |
| 220 | DataManager::SetValue("tw_busy", 1); |
| 221 | if (gui_startPage("fixsu") != 0) { |
| 222 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 223 | } |
| 224 | } |
| 225 | sync(); |
| 226 | PartitionManager.UnMount_By_Path("/system", false); |
| 227 | } |
| 228 | |
| 229 | // Reboot |
| 230 | TWFunc::Update_Intent_File(Reboot_Value); |
| 231 | TWFunc::Update_Log_File(); |
| 232 | gui_print("Rebooting...\n"); |
| 233 | string Reboot_Arg; |
| 234 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 235 | if (Reboot_Arg == "recovery") |
| 236 | TWFunc::tw_reboot(rb_recovery); |
| 237 | else if (Reboot_Arg == "poweroff") |
| 238 | TWFunc::tw_reboot(rb_poweroff); |
| 239 | else if (Reboot_Arg == "bootloader") |
| 240 | TWFunc::tw_reboot(rb_bootloader); |
| 241 | else if (Reboot_Arg == "download") |
| 242 | TWFunc::tw_reboot(rb_download); |
| 243 | else |
| 244 | TWFunc::tw_reboot(rb_system); |
| 245 | |
| 246 | #ifdef ANDROID_RB_RESTART |
| 247 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 248 | #else |
| 249 | reboot(RB_AUTOBOOT); |
| 250 | #endif |
| 251 | return 0; |
| 252 | } |