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" |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 27 | #include "bootloader_message/bootloader_message.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 28 | |
| 29 | #ifdef ANDROID_RB_RESTART |
| 30 | #include "cutils/android_reboot.h" |
| 31 | #else |
| 32 | #include <sys/reboot.h> |
| 33 | #endif |
| 34 | |
| 35 | extern "C" { |
| 36 | #include "gui/gui.h" |
| 37 | } |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 38 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 39 | #include "gui/gui.hpp" |
| 40 | #include "gui/pages.hpp" |
| 41 | #include "gui/objects.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 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" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 48 | #ifdef TW_USE_NEW_MINADBD |
| 49 | #include "adb.h" |
| 50 | #else |
| 51 | extern "C" { |
| 52 | #include "minadbd.old/adb.h" |
| 53 | } |
| 54 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 55 | |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 56 | #include <selinux/label.h> |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 57 | struct selabel_handle *selinux_handle; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 58 | |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 59 | extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */); |
| 60 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 61 | TWPartitionManager PartitionManager; |
| 62 | int Log_Offset; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 63 | bool datamedia; |
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 |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 88 | adb_server_main(0, DEFAULT_ADB_PORT, -1); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 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 | 0e2140e | 2016-08-10 21:04:26 +0200 | [diff] [blame] | 109 | printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, 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(); |
that | 0e2140e | 2016-08-10 21:04:26 +0200 | [diff] [blame] | 113 | printf("Starting the UI...\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 134 | if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { |
| 135 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 136 | printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); |
| 137 | rename("/file_contexts", "/file_contexts.bak"); |
| 138 | } |
| 139 | printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); |
| 140 | rename("/prebuilt_file_contexts", "/file_contexts"); |
| 141 | } |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 142 | struct selinux_opt selinux_options[] = { |
| 143 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 144 | }; |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 145 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 146 | if (!selinux_handle) |
| 147 | printf("No file contexts for SELinux\n"); |
| 148 | else |
| 149 | printf("SELinux contexts loaded from /file_contexts\n"); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 150 | { // Check to ensure SELinux can be supported by the kernel |
| 151 | char *contexts = NULL; |
Dees Troy | 8d0eb13 | 2013-12-06 16:55:41 +0000 | [diff] [blame] | 152 | |
Ethan Yonker | ff2b788 | 2016-12-10 09:04:41 -0600 | [diff] [blame] | 153 | if (PartitionManager.Mount_By_Path("/cache", false) && TWFunc::Path_Exists("/cache/recovery")) { |
Dees Troy | 8d0eb13 | 2013-12-06 16:55:41 +0000 | [diff] [blame] | 154 | lgetfilecon("/cache/recovery", &contexts); |
| 155 | if (!contexts) { |
| 156 | lsetfilecon("/cache/recovery", "test"); |
| 157 | lgetfilecon("/cache/recovery", &contexts); |
| 158 | } |
| 159 | } else { |
| 160 | LOGINFO("Could not check /cache/recovery SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n"); |
| 161 | lgetfilecon("/sbin/teamwin", &contexts); |
| 162 | } |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 163 | if (!contexts) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 164 | 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] | 165 | } else { |
| 166 | free(contexts); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 167 | gui_msg("full_selinux=Full SELinux support is present."); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 170 | |
Ethan Yonker | ff2b788 | 2016-12-10 09:04:41 -0600 | [diff] [blame] | 171 | PartitionManager.Mount_By_Path("/cache", false); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 172 | |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 173 | bool Shutdown = false; |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 174 | string Send_Intent = ""; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 175 | { |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 176 | TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc"); |
| 177 | if (misc != NULL) { |
| 178 | if (misc->Current_File_System == "emmc") { |
Matt Mower | 3626bdc | 2017-01-06 13:45:54 -0600 | [diff] [blame] | 179 | set_misc_device(misc->Actual_Block_Device); |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 180 | } else { |
Matt Mower | 3626bdc | 2017-01-06 13:45:54 -0600 | [diff] [blame] | 181 | LOGERR("Only emmc /misc is supported\n"); |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 184 | get_args(&argc, &argv); |
| 185 | |
| 186 | int index, index2, len; |
| 187 | char* argptr; |
| 188 | char* ptr; |
| 189 | printf("Startup Commands: "); |
| 190 | for (index = 1; index < argc; index++) { |
| 191 | argptr = argv[index]; |
| 192 | printf(" '%s'", argv[index]); |
| 193 | len = strlen(argv[index]); |
| 194 | if (*argptr == '-') {argptr++; len--;} |
| 195 | if (*argptr == '-') {argptr++; len--;} |
| 196 | if (*argptr == 'u') { |
| 197 | ptr = argptr; |
| 198 | index2 = 0; |
| 199 | while (*ptr != '=' && *ptr != '\n') |
| 200 | ptr++; |
Kevin Steck | 7b69e9d | 2013-10-17 18:24:31 -0400 | [diff] [blame] | 201 | // skip the = before grabbing Zip_File |
| 202 | while (*ptr == '=') |
| 203 | ptr++; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 204 | if (*ptr) { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 205 | string ORSCommand = "install "; |
| 206 | ORSCommand.append(ptr); |
| 207 | |
| 208 | if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
| 209 | break; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 210 | } else |
| 211 | LOGERR("argument error specifying zip file\n"); |
| 212 | } else if (*argptr == 'w') { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 213 | if (len == 9) { |
| 214 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 215 | break; |
| 216 | } else if (len == 10) { |
| 217 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 218 | break; |
| 219 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 220 | // Other 'w' items are wipe_ab and wipe_package_size which are related to bricking the device remotely. We will not bother to suppor these as having TWRP probably makes "bricking" the device in this manner useless |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 221 | } else if (*argptr == 'n') { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 222 | DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}")); |
| 223 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
| 224 | break; |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 225 | } else if (*argptr == 'p') { |
| 226 | Shutdown = true; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 227 | } else if (*argptr == 's') { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 228 | if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 229 | ptr = argptr + strlen("send_intent") + 1; |
| 230 | Send_Intent = *ptr; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 231 | } else if (strncmp(argptr, "security", strlen("security")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 232 | LOGINFO("Security update\n"); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 233 | } else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 234 | if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n")) |
| 235 | break; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 236 | } else if (strncmp(argptr, "stages", strlen("stages")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 237 | LOGINFO("ignoring stages command\n"); |
| 238 | } |
| 239 | } else if (*argptr == 'r') { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 240 | if (strncmp(argptr, "reason", strlen("reason")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 241 | ptr = argptr + strlen("reason") + 1; |
| 242 | gui_print("%s\n", ptr); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 246 | printf("\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 249 | if (crash_counter == 0) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 250 | property_list(Print_Prop, NULL); |
| 251 | printf("\n"); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 252 | } else { |
| 253 | printf("twrp.crash_counter=%d\n", crash_counter); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Check for and run startup script if script exists |
| 257 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 258 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
| 259 | |
| 260 | #ifdef TW_INCLUDE_INJECTTWRP |
| 261 | // Back up TWRP Ramdisk if needed: |
| 262 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 263 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 264 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 265 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 266 | else { |
| 267 | 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] | 268 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 269 | } |
| 270 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 271 | #endif |
| 272 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 273 | TWFunc::Update_Log_File(); |
| 274 | // Offer to decrypt if the device is encrypted |
| 275 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 276 | LOGINFO("Is encrypted, do decrypt page first\n"); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 277 | if (gui_startPage("decrypt", 1, 1) != 0) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 278 | LOGERR("Failed to start decrypt GUI page.\n"); |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 279 | } else { |
| 280 | // Check for and load custom theme if present |
| 281 | gui_loadCustomResources(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 282 | } |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 283 | } else if (datamedia) { |
| 284 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 285 | 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] | 286 | } else { |
| 287 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
| 288 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Read the settings file |
Ethan Yonker | 73da42c | 2014-09-03 11:30:33 -0500 | [diff] [blame] | 292 | #ifdef TW_HAS_MTP |
| 293 | // We unmount partitions sometimes during early boot which may override |
| 294 | // the default of MTP being enabled by auto toggling MTP off. This |
| 295 | // will force it back to enabled then get overridden by the settings |
| 296 | // file, assuming that an entry for tw_mtp_enabled is set. |
| 297 | DataManager::SetValue("tw_mtp_enabled", 1); |
| 298 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 299 | DataManager::ReadSettingsFile(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 300 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 301 | GUIConsole::Translate_Now(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 302 | |
| 303 | // Fixup the RTC clock on devices which require it |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 304 | if (crash_counter == 0) |
Vojtech Bocek | 67351dc | 2014-07-03 15:22:41 +0200 | [diff] [blame] | 305 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 306 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 307 | // Run any outstanding OpenRecoveryScript |
| 308 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) { |
| 309 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 310 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 311 | |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 312 | #ifdef TW_HAS_MTP |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 313 | // Enable MTP? |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 314 | char mtp_crash_check[PROPERTY_VALUE_MAX]; |
| 315 | property_get("mtp.crash_check", mtp_crash_check, "0"); |
| 316 | if (strcmp(mtp_crash_check, "0") == 0) { |
| 317 | property_set("mtp.crash_check", "1"); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 318 | 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] | 319 | LOGINFO("Enabling MTP during startup\n"); |
| 320 | if (!PartitionManager.Enable_MTP()) |
| 321 | PartitionManager.Disable_MTP(); |
| 322 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 323 | gui_msg("mtp_enabled=MTP Enabled"); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 324 | } else { |
| 325 | PartitionManager.Disable_MTP(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 326 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 327 | property_set("mtp.crash_check", "0"); |
| 328 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 329 | gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot."); |
Ethan Yonker | 12055fa | 2014-09-04 08:06:53 -0500 | [diff] [blame] | 330 | DataManager::SetValue("tw_mtp_enabled", 0); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 331 | PartitionManager.Disable_MTP(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 332 | } |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 333 | #else |
| 334 | PartitionManager.Disable_MTP(); |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 335 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 336 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 337 | #ifndef TW_OEM_BUILD |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 338 | // Check if system has never been changed |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 339 | TWPartition* sys = PartitionManager.Find_Partition_By_Path("/system"); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 340 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 341 | if (sys) { |
Ethan Yonker | 961d20e | 2015-06-29 14:00:03 -0500 | [diff] [blame] | 342 | if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) { |
| 343 | if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) { |
| 344 | DataManager::SetValue("tw_back", "main"); |
| 345 | if (gui_startPage("system_readonly", 1, 1) != 0) { |
| 346 | LOGERR("Failed to start system_readonly GUI page.\n"); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 347 | } |
Ethan Yonker | 961d20e | 2015-06-29 14:00:03 -0500 | [diff] [blame] | 348 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) { |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 349 | sys->Change_Mount_Read_Only(false); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 350 | if (ven) |
| 351 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 352 | } |
Ethan Yonker | 961d20e | 2015-06-29 14:00:03 -0500 | [diff] [blame] | 353 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) { |
| 354 | // Do nothing, user selected to leave system read only |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 355 | } else { |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 356 | sys->Change_Mount_Read_Only(false); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 357 | if (ven) |
| 358 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 359 | } |
| 360 | } |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 361 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 362 | // Launch the main GUI |
| 363 | gui_start(); |
| 364 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 365 | #ifndef TW_OEM_BUILD |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 366 | // Disable flashing of stock recovery |
| 367 | TWFunc::Disable_Stock_Recovery_Replace(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 368 | // Check for su to see if the device is rooted or not |
Matt Mower | 9546019 | 2015-11-18 10:31:33 -0600 | [diff] [blame] | 369 | 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] | 370 | // read /system/build.prop to get sdk version and do not offer to root if running M or higher (sdk version 23 == M) |
| 371 | string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk"); |
| 372 | int sdkver = 23; |
| 373 | if (!sdkverstr.empty()) { |
| 374 | sdkver = atoi(sdkverstr.c_str()); |
| 375 | } |
| 376 | 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] | 377 | // Device doesn't have su installed |
| 378 | DataManager::SetValue("tw_busy", 1); |
Ethan Yonker | fd0439e | 2015-01-14 11:08:13 -0600 | [diff] [blame] | 379 | if (gui_startPage("installsu", 1, 1) != 0) { |
Dees Troy | f193f88 | 2013-09-11 14:56:20 +0000 | [diff] [blame] | 380 | LOGERR("Failed to start SuperSU install page.\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 381 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 382 | } |
| 383 | sync(); |
| 384 | PartitionManager.UnMount_By_Path("/system", false); |
| 385 | } |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 386 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 387 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 388 | // Reboot |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 389 | TWFunc::Update_Intent_File(Send_Intent); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 390 | TWFunc::Update_Log_File(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 391 | gui_msg(Msg("rebooting=Rebooting...")); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 392 | string Reboot_Arg; |
| 393 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 394 | if (Reboot_Arg == "recovery") |
| 395 | TWFunc::tw_reboot(rb_recovery); |
| 396 | else if (Reboot_Arg == "poweroff") |
| 397 | TWFunc::tw_reboot(rb_poweroff); |
| 398 | else if (Reboot_Arg == "bootloader") |
| 399 | TWFunc::tw_reboot(rb_bootloader); |
| 400 | else if (Reboot_Arg == "download") |
| 401 | TWFunc::tw_reboot(rb_download); |
| 402 | else |
| 403 | TWFunc::tw_reboot(rb_system); |
| 404 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 405 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 406 | } |