Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2 | TWRP is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU General Public License as published by |
| 4 | the Free Software Foundation, either version 3 of the License, or |
| 5 | (at your option) any later version. |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 6 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 7 | TWRP 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. |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 11 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 12 | You should have received a copy of the GNU General Public License |
| 13 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <time.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "cutils/properties.h" |
| 25 | extern "C" { |
| 26 | #include "minadbd/adb.h" |
| 27 | #include "bootloader.h" |
| 28 | } |
| 29 | |
| 30 | #ifdef ANDROID_RB_RESTART |
| 31 | #include "cutils/android_reboot.h" |
| 32 | #else |
| 33 | #include <sys/reboot.h> |
| 34 | #endif |
| 35 | |
| 36 | extern "C" { |
| 37 | #include "gui/gui.h" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 38 | #include "set_metadata.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 39 | } |
| 40 | #include "twcommon.h" |
| 41 | #include "twrp-functions.hpp" |
| 42 | #include "data.hpp" |
| 43 | #include "partitions.hpp" |
| 44 | #include "openrecoveryscript.hpp" |
| 45 | #include "variables.h" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 46 | #include "twrpDU.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 47 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 48 | #ifdef HAVE_SELINUX |
| 49 | #include "selinux/label.h" |
| 50 | struct selabel_handle *selinux_handle; |
| 51 | #endif |
| 52 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 53 | TWPartitionManager PartitionManager; |
| 54 | int Log_Offset; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 55 | bool datamedia; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 56 | twrpDU du; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 57 | |
| 58 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 59 | printf("%s=%s\n", key, name); |
| 60 | } |
| 61 | |
| 62 | int main(int argc, char **argv) { |
| 63 | // Recovery needs to install world-readable files, so clear umask |
| 64 | // set by init |
| 65 | umask(0); |
| 66 | |
| 67 | Log_Offset = 0; |
| 68 | |
| 69 | // Set up temporary log file (/tmp/recovery.log) |
| 70 | freopen(TMP_LOG_FILE, "a", stdout); |
| 71 | setbuf(stdout, NULL); |
| 72 | freopen(TMP_LOG_FILE, "a", stderr); |
| 73 | setbuf(stderr, NULL); |
| 74 | |
| 75 | // Handle ADB sideload |
| 76 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 77 | adb_main(argv[2]); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 81 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 82 | datamedia = true; |
| 83 | #endif |
| 84 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 85 | char crash_prop_val[PROPERTY_VALUE_MAX]; |
| 86 | int crash_counter; |
| 87 | property_get("twrp.crash_counter", crash_prop_val, "-1"); |
| 88 | crash_counter = atoi(crash_prop_val) + 1; |
| 89 | snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter); |
| 90 | property_set("twrp.crash_counter", crash_prop_val); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 91 | property_set("ro.twrp.boot", "1"); |
| 92 | property_set("ro.twrp.version", TW_VERSION_STR); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 93 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 94 | time_t StartupTime = time(NULL); |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 95 | printf("Starting TWRP %s on %s (pid %d)", TW_VERSION_STR, ctime(&StartupTime), getpid()); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 96 | |
| 97 | // Load default values to set DataManager constants and handle ifdefs |
| 98 | DataManager::SetDefaultValues(); |
| 99 | printf("Starting the UI..."); |
| 100 | gui_init(); |
| 101 | printf("=> Linking mtab\n"); |
| 102 | symlink("/proc/mounts", "/etc/mtab"); |
Dees_Troy | 329383e | 2013-08-29 14:16:06 +0000 | [diff] [blame] | 103 | if (TWFunc::Path_Exists("/etc/twrp.fstab")) { |
| 104 | if (TWFunc::Path_Exists("/etc/recovery.fstab")) { |
| 105 | printf("Renaming regular /etc/recovery.fstab -> /etc/recovery.fstab.bak\n"); |
| 106 | rename("/etc/recovery.fstab", "/etc/recovery.fstab.bak"); |
| 107 | } |
| 108 | printf("Moving /etc/twrp.fstab -> /etc/recovery.fstab\n"); |
| 109 | rename("/etc/twrp.fstab", "/etc/recovery.fstab"); |
| 110 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 111 | printf("=> Processing recovery.fstab\n"); |
| 112 | if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) { |
| 113 | LOGERR("Failing out of recovery due to problem with recovery.fstab.\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | PartitionManager.Output_Partition_Logging(); |
| 117 | // Load up all the resources |
| 118 | gui_loadResources(); |
| 119 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 120 | #ifdef HAVE_SELINUX |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 121 | if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { |
| 122 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 123 | printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); |
| 124 | rename("/file_contexts", "/file_contexts.bak"); |
| 125 | } |
| 126 | printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); |
| 127 | rename("/prebuilt_file_contexts", "/file_contexts"); |
| 128 | } |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 129 | struct selinux_opt selinux_options[] = { |
| 130 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 131 | }; |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 132 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 133 | if (!selinux_handle) |
| 134 | printf("No file contexts for SELinux\n"); |
| 135 | else |
| 136 | printf("SELinux contexts loaded from /file_contexts\n"); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 137 | { // Check to ensure SELinux can be supported by the kernel |
| 138 | char *contexts = NULL; |
Dees Troy | 8d0eb13 | 2013-12-06 16:55:41 +0000 | [diff] [blame] | 139 | |
| 140 | if (PartitionManager.Mount_By_Path("/cache", true) && TWFunc::Path_Exists("/cache/recovery")) { |
| 141 | lgetfilecon("/cache/recovery", &contexts); |
| 142 | if (!contexts) { |
| 143 | lsetfilecon("/cache/recovery", "test"); |
| 144 | lgetfilecon("/cache/recovery", &contexts); |
| 145 | } |
| 146 | } else { |
| 147 | LOGINFO("Could not check /cache/recovery SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n"); |
| 148 | lgetfilecon("/sbin/teamwin", &contexts); |
| 149 | } |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 150 | if (!contexts) { |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 151 | gui_print_color("warning", "Kernel does not have support for reading SELinux contexts.\n"); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 152 | } else { |
| 153 | free(contexts); |
| 154 | gui_print("Full SELinux support is present.\n"); |
| 155 | } |
| 156 | } |
| 157 | #else |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 158 | gui_print_color("warning", "No SELinux support (no libselinux).\n"); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 161 | PartitionManager.Mount_By_Path("/cache", true); |
| 162 | |
| 163 | string Zip_File, Reboot_Value; |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 164 | bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false, Shutdown = false; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 165 | |
| 166 | { |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 167 | TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc"); |
| 168 | if (misc != NULL) { |
| 169 | if (misc->Current_File_System == "emmc") { |
| 170 | set_device_type('e'); |
| 171 | set_device_name(misc->Actual_Block_Device.c_str()); |
| 172 | } else if (misc->Current_File_System == "mtd") { |
| 173 | set_device_type('m'); |
| 174 | set_device_name(misc->MTD_Name.c_str()); |
| 175 | } else { |
| 176 | LOGERR("Unknown file system for /misc\n"); |
| 177 | } |
| 178 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 179 | get_args(&argc, &argv); |
| 180 | |
| 181 | int index, index2, len; |
| 182 | char* argptr; |
| 183 | char* ptr; |
| 184 | printf("Startup Commands: "); |
| 185 | for (index = 1; index < argc; index++) { |
| 186 | argptr = argv[index]; |
| 187 | printf(" '%s'", argv[index]); |
| 188 | len = strlen(argv[index]); |
| 189 | if (*argptr == '-') {argptr++; len--;} |
| 190 | if (*argptr == '-') {argptr++; len--;} |
| 191 | if (*argptr == 'u') { |
| 192 | ptr = argptr; |
| 193 | index2 = 0; |
| 194 | while (*ptr != '=' && *ptr != '\n') |
| 195 | ptr++; |
Kevin Steck | 7b69e9d | 2013-10-17 18:24:31 -0400 | [diff] [blame] | 196 | // skip the = before grabbing Zip_File |
| 197 | while (*ptr == '=') |
| 198 | ptr++; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 199 | if (*ptr) { |
| 200 | Zip_File = ptr; |
| 201 | } else |
| 202 | LOGERR("argument error specifying zip file\n"); |
| 203 | } else if (*argptr == 'w') { |
| 204 | if (len == 9) |
| 205 | Factory_Reset = true; |
| 206 | else if (len == 10) |
| 207 | Cache_Wipe = true; |
| 208 | } else if (*argptr == 'n') { |
| 209 | Perform_Backup = true; |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 210 | } else if (*argptr == 'p') { |
| 211 | Shutdown = true; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 212 | } else if (*argptr == 's') { |
| 213 | ptr = argptr; |
| 214 | index2 = 0; |
| 215 | while (*ptr != '=' && *ptr != '\n') |
| 216 | ptr++; |
| 217 | if (*ptr) { |
| 218 | Reboot_Value = *ptr; |
| 219 | } |
| 220 | } |
| 221 | } |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 222 | printf("\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 225 | if(crash_counter == 0) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 226 | property_list(Print_Prop, NULL); |
| 227 | printf("\n"); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 228 | } else { |
| 229 | printf("twrp.crash_counter=%d\n", crash_counter); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Check for and run startup script if script exists |
| 233 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 234 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
| 235 | |
| 236 | #ifdef TW_INCLUDE_INJECTTWRP |
| 237 | // Back up TWRP Ramdisk if needed: |
| 238 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 239 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 240 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 241 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 242 | else { |
| 243 | string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 244 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 245 | } |
| 246 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 247 | #endif |
| 248 | |
| 249 | bool Keep_Going = true; |
| 250 | if (Perform_Backup) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 251 | DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 252 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
| 253 | Keep_Going = false; |
| 254 | } |
| 255 | if (Keep_Going && !Zip_File.empty()) { |
| 256 | string ORSCommand = "install " + Zip_File; |
| 257 | |
| 258 | if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
| 259 | Keep_Going = false; |
| 260 | } |
| 261 | if (Keep_Going) { |
| 262 | if (Factory_Reset) { |
| 263 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 264 | Keep_Going = false; |
| 265 | } else if (Cache_Wipe) { |
| 266 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 267 | Keep_Going = false; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | TWFunc::Update_Log_File(); |
| 272 | // Offer to decrypt if the device is encrypted |
| 273 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 274 | LOGINFO("Is encrypted, do decrypt page first\n"); |
| 275 | if (gui_startPage("decrypt") != 0) { |
| 276 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 277 | } |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 278 | } else if (datamedia) { |
| 279 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
| 280 | LOGERR("Failed to get default contexts and file mode for storage files.\n"); |
| 281 | } else { |
| 282 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
| 283 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Read the settings file |
Ethan Yonker | 73da42c | 2014-09-03 11:30:33 -0500 | [diff] [blame] | 287 | #ifdef TW_HAS_MTP |
| 288 | // We unmount partitions sometimes during early boot which may override |
| 289 | // the default of MTP being enabled by auto toggling MTP off. This |
| 290 | // will force it back to enabled then get overridden by the settings |
| 291 | // file, assuming that an entry for tw_mtp_enabled is set. |
| 292 | DataManager::SetValue("tw_mtp_enabled", 1); |
| 293 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 294 | DataManager::ReadSettingsFile(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 295 | |
| 296 | // Fixup the RTC clock on devices which require it |
Vojtech Bocek | 67351dc | 2014-07-03 15:22:41 +0200 | [diff] [blame] | 297 | if(crash_counter == 0) |
| 298 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 299 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 300 | // Run any outstanding OpenRecoveryScript |
| 301 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) { |
| 302 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 303 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 304 | |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 305 | #ifdef TW_HAS_MTP |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 306 | // Enable MTP? |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 307 | char mtp_crash_check[PROPERTY_VALUE_MAX]; |
| 308 | property_get("mtp.crash_check", mtp_crash_check, "0"); |
| 309 | if (strcmp(mtp_crash_check, "0") == 0) { |
| 310 | property_set("mtp.crash_check", "1"); |
| 311 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 312 | if (DataManager::GetIntValue(TW_IS_DECRYPTED) != 0 && DataManager::GetIntValue("tw_mtp_enabled") == 1) { |
| 313 | LOGINFO("Enabling MTP during startup\n"); |
| 314 | if (!PartitionManager.Enable_MTP()) |
| 315 | PartitionManager.Disable_MTP(); |
| 316 | else |
| 317 | gui_print("MTP Enabled\n"); |
| 318 | } |
| 319 | } else if (DataManager::GetIntValue("tw_mtp_enabled") == 1) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 320 | LOGINFO("Enabling MTP during startup\n"); |
| 321 | if (!PartitionManager.Enable_MTP()) |
| 322 | PartitionManager.Disable_MTP(); |
| 323 | else |
| 324 | gui_print("MTP Enabled\n"); |
| 325 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 326 | property_set("mtp.crash_check", "0"); |
| 327 | } else { |
| 328 | gui_print_color("warning", "MTP Crashed, not starting MTP on boot.\n"); |
Ethan Yonker | 12055fa | 2014-09-04 08:06:53 -0500 | [diff] [blame] | 329 | DataManager::SetValue("tw_mtp_enabled", 0); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 330 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 331 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 332 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 333 | // Launch the main GUI |
| 334 | gui_start(); |
| 335 | |
| 336 | // Check for su to see if the device is rooted or not |
| 337 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 338 | // Disable flashing of stock recovery |
Dees Troy | c8f5899 | 2014-11-20 15:29:03 +0000 | [diff] [blame] | 339 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 340 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
| 341 | gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); |
| 342 | } |
| 343 | 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")) { |
| 344 | // Device doesn't have su installed |
| 345 | DataManager::SetValue("tw_busy", 1); |
| 346 | if (gui_startPage("installsu") != 0) { |
Dees Troy | f193f88 | 2013-09-11 14:56:20 +0000 | [diff] [blame] | 347 | LOGERR("Failed to start SuperSU install page.\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 348 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 349 | } |
| 350 | sync(); |
| 351 | PartitionManager.UnMount_By_Path("/system", false); |
| 352 | } |
| 353 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 354 | // Reboot |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 355 | TWFunc::Update_Intent_File(Reboot_Value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 356 | TWFunc::Update_Log_File(); |
| 357 | gui_print("Rebooting...\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 358 | string Reboot_Arg; |
| 359 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 360 | if (Reboot_Arg == "recovery") |
| 361 | TWFunc::tw_reboot(rb_recovery); |
| 362 | else if (Reboot_Arg == "poweroff") |
| 363 | TWFunc::tw_reboot(rb_poweroff); |
| 364 | else if (Reboot_Arg == "bootloader") |
| 365 | TWFunc::tw_reboot(rb_bootloader); |
| 366 | else if (Reboot_Arg == "download") |
| 367 | TWFunc::tw_reboot(rb_download); |
| 368 | else |
| 369 | TWFunc::tw_reboot(rb_system); |
| 370 | |
| 371 | #ifdef ANDROID_RB_RESTART |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 372 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 373 | #else |
| 374 | reboot(RB_AUTOBOOT); |
| 375 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 376 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 377 | } |