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