blob: b68e321b9237c99798e443705b84435a7104be40 [file] [log] [blame]
Dees_Troya449a6f2013-04-07 17:50:11 -05001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 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_Troya449a6f2013-04-07 17:50:11 -05006
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05007 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_Troya449a6f2013-04-07 17:50:11 -050011
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050012 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_Troya449a6f2013-04-07 17:50:11 -050014*/
15
16#include <stdio.h>
17#include <stdlib.h>
Ethan Yonker74db1572015-10-28 12:44:49 -050018#include <string>
Dees_Troya449a6f2013-04-07 17:50:11 -050019#include <sys/stat.h>
20#include <sys/types.h>
21#include <time.h>
22#include <unistd.h>
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -060023#include <signal.h>
Ethan Yonker74db1572015-10-28 12:44:49 -050024#include "gui/twmsg.h"
Dees_Troya449a6f2013-04-07 17:50:11 -050025
26#include "cutils/properties.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050027#include "bootloader_message_twrp/include/bootloader_message_twrp/bootloader_message.h"
Dees_Troya449a6f2013-04-07 17:50:11 -050028
29#ifdef ANDROID_RB_RESTART
30#include "cutils/android_reboot.h"
31#else
32#include <sys/reboot.h>
33#endif
34
35extern "C" {
36#include "gui/gui.h"
37}
Ethan Yonkerf1179622016-08-25 15:32:21 -050038#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050039#include "gui/gui.hpp"
40#include "gui/pages.hpp"
41#include "gui/objects.hpp"
Dees_Troya449a6f2013-04-07 17:50:11 -050042#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 Yonkerc798c9c2015-10-09 11:15:26 -050048#ifdef TW_USE_NEW_MINADBD
Ethan Yonker8373cfe2017-09-08 06:50:54 -050049#include "minadbd/minadbd.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050050#else
51extern "C" {
Ethan Yonker84d61ce2017-05-10 16:11:35 -050052#include "minadbd21/adb.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050053}
54#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050055
Matt Mower87413642017-01-17 21:14:46 -060056#include <selinux/label.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000057struct selabel_handle *selinux_handle;
Dees_Troya95f55c2013-08-17 13:14:43 +000058
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059//extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
Ethan Yonker534d4e02016-08-26 10:05:03 -050060
Dees_Troya449a6f2013-04-07 17:50:11 -050061TWPartitionManager PartitionManager;
62int Log_Offset;
Ethan Yonker6277c792014-09-15 14:54:30 -050063bool datamedia;
Dees_Troya449a6f2013-04-07 17:50:11 -050064
65static void Print_Prop(const char *key, const char *name, void *cookie) {
66 printf("%s=%s\n", key, name);
67}
68
69int 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 Yonkerf9f99bc2014-12-29 09:10:34 -060082 signal(SIGPIPE, SIG_IGN);
83
Dees_Troya449a6f2013-04-07 17:50:11 -050084 // Handle ADB sideload
85 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
thatcc8ddca2015-01-03 01:59:36 +010086 property_set("ctl.stop", "adbd");
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050087#ifdef TW_USE_NEW_MINADBD
Ethan Yonker8373cfe2017-09-08 06:50:54 -050088 //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8
89 minadbd_main();
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050090#else
Dees_Troya449a6f2013-04-07 17:50:11 -050091 adb_main(argv[2]);
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050092#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050093 return 0;
94 }
95
Ethan Yonker6277c792014-09-15 14:54:30 -050096#ifdef RECOVERY_SDCARD_ON_DATA
97 datamedia = true;
98#endif
99
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200100 char crash_prop_val[PROPERTY_VALUE_MAX];
101 int crash_counter;
102 property_get("twrp.crash_counter", crash_prop_val, "-1");
103 crash_counter = atoi(crash_prop_val) + 1;
104 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
105 property_set("twrp.crash_counter", crash_prop_val);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400106 property_set("ro.twrp.boot", "1");
107 property_set("ro.twrp.version", TW_VERSION_STR);
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200108
Dees_Troya449a6f2013-04-07 17:50:11 -0500109 time_t StartupTime = time(NULL);
that0e2140e2016-08-10 21:04:26 +0200110 printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, ctime(&StartupTime), getpid());
Dees_Troya449a6f2013-04-07 17:50:11 -0500111
112 // Load default values to set DataManager constants and handle ifdefs
113 DataManager::SetDefaultValues();
that0e2140e2016-08-10 21:04:26 +0200114 printf("Starting the UI...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500115 gui_init();
116 printf("=> Linking mtab\n");
117 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy329383e2013-08-29 14:16:06 +0000118 if (TWFunc::Path_Exists("/etc/twrp.fstab")) {
119 if (TWFunc::Path_Exists("/etc/recovery.fstab")) {
120 printf("Renaming regular /etc/recovery.fstab -> /etc/recovery.fstab.bak\n");
121 rename("/etc/recovery.fstab", "/etc/recovery.fstab.bak");
122 }
123 printf("Moving /etc/twrp.fstab -> /etc/recovery.fstab\n");
124 rename("/etc/twrp.fstab", "/etc/recovery.fstab");
125 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500126 printf("=> Processing recovery.fstab\n");
127 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
128 LOGERR("Failing out of recovery due to problem with recovery.fstab.\n");
129 return -1;
130 }
131 PartitionManager.Output_Partition_Logging();
132 // Load up all the resources
133 gui_loadResources();
134
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400135 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_Troya95f55c2013-08-17 13:14:43 +0000143 struct selinux_opt selinux_options[] = {
144 { SELABEL_OPT_PATH, "/file_contexts" }
145 };
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400146 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
Dees_Troya95f55c2013-08-17 13:14:43 +0000147 if (!selinux_handle)
148 printf("No file contexts for SELinux\n");
149 else
150 printf("SELinux contexts loaded from /file_contexts\n");
Dees Troy995e88c2013-11-26 21:39:14 +0000151 { // Check to ensure SELinux can be supported by the kernel
152 char *contexts = NULL;
Dees Troy8d0eb132013-12-06 16:55:41 +0000153
Ethan Yonkerff2b7882016-12-10 09:04:41 -0600154 if (PartitionManager.Mount_By_Path("/cache", false) && TWFunc::Path_Exists("/cache/recovery")) {
Dees Troy8d0eb132013-12-06 16:55:41 +0000155 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 Troy995e88c2013-11-26 21:39:14 +0000164 if (!contexts) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500165 gui_warn("no_kernel_selinux=Kernel does not have support for reading SELinux contexts.");
Dees Troy995e88c2013-11-26 21:39:14 +0000166 } else {
167 free(contexts);
Ethan Yonker74db1572015-10-28 12:44:49 -0500168 gui_msg("full_selinux=Full SELinux support is present.");
Dees Troy995e88c2013-11-26 21:39:14 +0000169 }
170 }
Dees_Troya95f55c2013-08-17 13:14:43 +0000171
Ethan Yonkerff2b7882016-12-10 09:04:41 -0600172 PartitionManager.Mount_By_Path("/cache", false);
Dees_Troya449a6f2013-04-07 17:50:11 -0500173
Matt Mower23d8aae2017-01-06 14:30:33 -0600174 bool Shutdown = false;
Ethan Yonkerb5236502016-11-19 22:24:59 -0600175 string Send_Intent = "";
Dees_Troya449a6f2013-04-07 17:50:11 -0500176 {
Dees_Troy1669f892013-09-04 18:35:08 +0000177 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
178 if (misc != NULL) {
179 if (misc->Current_File_System == "emmc") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500180 set_misc_device(misc->Actual_Block_Device.c_str());
Dees_Troy1669f892013-09-04 18:35:08 +0000181 } else {
Matt Mower3626bdc2017-01-06 13:45:54 -0600182 LOGERR("Only emmc /misc is supported\n");
Dees_Troy1669f892013-09-04 18:35:08 +0000183 }
184 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500185 get_args(&argc, &argv);
186
187 int index, index2, len;
188 char* argptr;
189 char* ptr;
190 printf("Startup Commands: ");
191 for (index = 1; index < argc; index++) {
192 argptr = argv[index];
193 printf(" '%s'", argv[index]);
194 len = strlen(argv[index]);
195 if (*argptr == '-') {argptr++; len--;}
196 if (*argptr == '-') {argptr++; len--;}
197 if (*argptr == 'u') {
198 ptr = argptr;
199 index2 = 0;
200 while (*ptr != '=' && *ptr != '\n')
201 ptr++;
Kevin Steck7b69e9d2013-10-17 18:24:31 -0400202 // skip the = before grabbing Zip_File
203 while (*ptr == '=')
204 ptr++;
Dees_Troya449a6f2013-04-07 17:50:11 -0500205 if (*ptr) {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600206 string ORSCommand = "install ";
207 ORSCommand.append(ptr);
208
209 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
210 break;
Dees_Troya449a6f2013-04-07 17:50:11 -0500211 } else
212 LOGERR("argument error specifying zip file\n");
213 } else if (*argptr == 'w') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600214 if (len == 9) {
215 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
216 break;
217 } else if (len == 10) {
218 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
219 break;
220 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600221 // 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_Troya449a6f2013-04-07 17:50:11 -0500222 } else if (*argptr == 'n') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600223 DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}"));
224 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
225 break;
Ethan Yonkera1674162014-11-06 08:35:10 -0600226 } else if (*argptr == 'p') {
227 Shutdown = true;
Dees_Troya449a6f2013-04-07 17:50:11 -0500228 } else if (*argptr == 's') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600229 if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600230 ptr = argptr + strlen("send_intent") + 1;
231 Send_Intent = *ptr;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600232 } else if (strncmp(argptr, "security", strlen("security")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600233 LOGINFO("Security update\n");
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600234 } else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600235 if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n"))
236 break;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600237 } else if (strncmp(argptr, "stages", strlen("stages")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600238 LOGINFO("ignoring stages command\n");
239 }
240 } else if (*argptr == 'r') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600241 if (strncmp(argptr, "reason", strlen("reason")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600242 ptr = argptr + strlen("reason") + 1;
243 gui_print("%s\n", ptr);
Dees_Troya449a6f2013-04-07 17:50:11 -0500244 }
245 }
246 }
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200247 printf("\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500248 }
249
Matt Mowera8a89d12016-12-30 18:10:37 -0600250 if (crash_counter == 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500251 property_list(Print_Prop, NULL);
252 printf("\n");
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200253 } else {
254 printf("twrp.crash_counter=%d\n", crash_counter);
Dees_Troya449a6f2013-04-07 17:50:11 -0500255 }
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_Troya449a6f2013-04-07 17:50:11 -0500264 LOGINFO("Backing up TWRP ramdisk...\n");
265 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200266 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500267 else {
268 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200269 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500270 }
271 LOGINFO("Backup of TWRP ramdisk done.\n");
272#endif
273
Dees_Troya449a6f2013-04-07 17:50:11 -0500274 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 Yonkerfd0439e2015-01-14 11:08:13 -0600278 if (gui_startPage("decrypt", 1, 1) != 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500279 LOGERR("Failed to start decrypt GUI page.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600280 } else {
281 // Check for and load custom theme if present
282 gui_loadCustomResources();
Dees_Troya449a6f2013-04-07 17:50:11 -0500283 }
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600284 } else if (datamedia) {
285 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -0600286 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600287 } else {
288 LOGINFO("Got default contexts and file mode for storage files.\n");
289 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500290 }
291
292 // Read the settings file
293 DataManager::ReadSettingsFile();
Ethan Yonker74db1572015-10-28 12:44:49 -0500294 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
295 GUIConsole::Translate_Now();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100296
297 // Fixup the RTC clock on devices which require it
Matt Mowera8a89d12016-12-30 18:10:37 -0600298 if (crash_counter == 0)
Vojtech Bocek67351dc2014-07-03 15:22:41 +0200299 TWFunc::Fixup_Time_On_Boot();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100300
Dees_Troya449a6f2013-04-07 17:50:11 -0500301 // Run any outstanding OpenRecoveryScript
302 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
303 OpenRecoveryScript::Run_OpenRecoveryScript();
304 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100305
Ethan Yonker6f49e112014-09-03 21:42:49 -0500306#ifdef TW_HAS_MTP
Ethan Yonker6f49e112014-09-03 21:42:49 -0500307 char mtp_crash_check[PROPERTY_VALUE_MAX];
308 property_get("mtp.crash_check", mtp_crash_check, "0");
Matt Mower653a1702017-02-16 10:38:52 -0600309 if (DataManager::GetIntValue("tw_mtp_enabled")
310 && !strcmp(mtp_crash_check, "0") && !crash_counter
311 && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) {
Ethan Yonker6f49e112014-09-03 21:42:49 -0500312 property_set("mtp.crash_check", "1");
Matt Mower653a1702017-02-16 10:38:52 -0600313 LOGINFO("Starting MTP\n");
314 if (!PartitionManager.Enable_MTP())
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600315 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600316 else
317 gui_msg("mtp_enabled=MTP Enabled");
Ethan Yonker6f49e112014-09-03 21:42:49 -0500318 property_set("mtp.crash_check", "0");
Matt Mower653a1702017-02-16 10:38:52 -0600319 } else if (strcmp(mtp_crash_check, "0")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500320 gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot.");
Ethan Yonker12055fa2014-09-04 08:06:53 -0500321 DataManager::SetValue("tw_mtp_enabled", 0);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600322 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600323 } else if (crash_counter == 1) {
324 LOGINFO("TWRP crashed; disabling MTP as a precaution.\n");
325 PartitionManager.Disable_MTP();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400326 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500327#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400328
Ethan Yonker89583ef2015-08-26 09:01:59 -0500329#ifndef TW_OEM_BUILD
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500330 // Check if system has never been changed
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500331 TWPartition* sys = PartitionManager.Find_Partition_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500332 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500333 if (sys) {
Ethan Yonker961d20e2015-06-29 14:00:03 -0500334 if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) {
335 if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
336 DataManager::SetValue("tw_back", "main");
337 if (gui_startPage("system_readonly", 1, 1) != 0) {
338 LOGERR("Failed to start system_readonly GUI page.\n");
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500339 }
Ethan Yonker961d20e2015-06-29 14:00:03 -0500340 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500341 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500342 if (ven)
343 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500344 }
Ethan Yonker961d20e2015-06-29 14:00:03 -0500345 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) {
346 // Do nothing, user selected to leave system read only
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500347 } else {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500348 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500349 if (ven)
350 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500351 }
352 }
Ethan Yonker89583ef2015-08-26 09:01:59 -0500353#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500354 // Launch the main GUI
355 gui_start();
356
Ethan Yonker89583ef2015-08-26 09:01:59 -0500357#ifndef TW_OEM_BUILD
Ethan Yonker9132d912015-02-02 10:32:49 -0600358 // Disable flashing of stock recovery
359 TWFunc::Disable_Stock_Recovery_Replace();
Dees_Troya449a6f2013-04-07 17:50:11 -0500360 // Check for su to see if the device is rooted or not
Matt Mower95460192015-11-18 10:31:33 -0600361 if (DataManager::GetIntValue("tw_mount_system_ro") == 0 && PartitionManager.Mount_By_Path("/system", false)) {
Ethan Yonker051f4762016-01-17 17:54:31 -0600362 // read /system/build.prop to get sdk version and do not offer to root if running M or higher (sdk version 23 == M)
363 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
364 int sdkver = 23;
365 if (!sdkverstr.empty()) {
366 sdkver = atoi(sdkverstr.c_str());
367 }
368 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_Troya449a6f2013-04-07 17:50:11 -0500369 // Device doesn't have su installed
370 DataManager::SetValue("tw_busy", 1);
Ethan Yonkerfd0439e2015-01-14 11:08:13 -0600371 if (gui_startPage("installsu", 1, 1) != 0) {
Dees Troyf193f882013-09-11 14:56:20 +0000372 LOGERR("Failed to start SuperSU install page.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500373 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500374 }
375 sync();
376 PartitionManager.UnMount_By_Path("/system", false);
377 }
Ethan Yonker89583ef2015-08-26 09:01:59 -0500378#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500379
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 // Reboot
Ethan Yonkerb5236502016-11-19 22:24:59 -0600381 TWFunc::Update_Intent_File(Send_Intent);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 TWFunc::Update_Log_File();
Ethan Yonker74db1572015-10-28 12:44:49 -0500383 gui_msg(Msg("rebooting=Rebooting..."));
Dees_Troya449a6f2013-04-07 17:50:11 -0500384 string Reboot_Arg;
385 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
386 if (Reboot_Arg == "recovery")
387 TWFunc::tw_reboot(rb_recovery);
388 else if (Reboot_Arg == "poweroff")
389 TWFunc::tw_reboot(rb_poweroff);
390 else if (Reboot_Arg == "bootloader")
391 TWFunc::tw_reboot(rb_bootloader);
392 else if (Reboot_Arg == "download")
393 TWFunc::tw_reboot(rb_download);
394 else
395 TWFunc::tw_reboot(rb_system);
396
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500398}