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