blob: 96d995895fb01ee4b2b02e7990ee2118a7082b4a [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"
bigbifff62d2492020-05-20 10:15:34 -040046
Mohd Faraze8f7ea32020-06-12 20:38:58 +053047#if SDK_VERSION >= 24
bigbifff62d2492020-05-20 10:15:34 -040048#include <android-base/strings.h>
Mohd Faraze8f7ea32020-06-12 20:38:58 +053049#elif SDK_VERSION == 23
50#include <base/strings.h>
51#else
52#include <strings.h>
Mohd Faraz0d9a62e2020-02-20 00:10:26 +053053#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050054#include "openrecoveryscript.hpp"
55#include "variables.h"
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040056#include "twrpAdbBuFifo.hpp"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050057#ifdef TW_USE_NEW_MINADBD
Ethan Yonker8373cfe2017-09-08 06:50:54 -050058#include "minadbd/minadbd.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050059#else
60extern "C" {
Ethan Yonker84d61ce2017-05-10 16:11:35 -050061#include "minadbd21/adb.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050062}
63#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050064
Ethan Yonker8373cfe2017-09-08 06:50:54 -050065//extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
Ethan Yonker534d4e02016-08-26 10:05:03 -050066
Dees_Troya449a6f2013-04-07 17:50:11 -050067TWPartitionManager PartitionManager;
68int Log_Offset;
Ethan Yonker6277c792014-09-15 14:54:30 -050069bool datamedia;
Dees_Troya449a6f2013-04-07 17:50:11 -050070
71static void Print_Prop(const char *key, const char *name, void *cookie) {
72 printf("%s=%s\n", key, name);
73}
74
75int main(int argc, char **argv) {
76 // Recovery needs to install world-readable files, so clear umask
77 // set by init
78 umask(0);
79
80 Log_Offset = 0;
81
82 // Set up temporary log file (/tmp/recovery.log)
83 freopen(TMP_LOG_FILE, "a", stdout);
84 setbuf(stdout, NULL);
85 freopen(TMP_LOG_FILE, "a", stderr);
86 setbuf(stderr, NULL);
87
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -060088 signal(SIGPIPE, SIG_IGN);
89
Dees_Troya449a6f2013-04-07 17:50:11 -050090 // Handle ADB sideload
91 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
thatcc8ddca2015-01-03 01:59:36 +010092 property_set("ctl.stop", "adbd");
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050093#ifdef TW_USE_NEW_MINADBD
Ethan Yonker8373cfe2017-09-08 06:50:54 -050094 //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8
95 minadbd_main();
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050096#else
Dees_Troya449a6f2013-04-07 17:50:11 -050097 adb_main(argv[2]);
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050098#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050099 return 0;
100 }
101
Ethan Yonker6277c792014-09-15 14:54:30 -0500102#ifdef RECOVERY_SDCARD_ON_DATA
103 datamedia = true;
104#endif
105
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200106 char crash_prop_val[PROPERTY_VALUE_MAX];
107 int crash_counter;
108 property_get("twrp.crash_counter", crash_prop_val, "-1");
109 crash_counter = atoi(crash_prop_val) + 1;
110 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
111 property_set("twrp.crash_counter", crash_prop_val);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400112 property_set("ro.twrp.boot", "1");
113 property_set("ro.twrp.version", TW_VERSION_STR);
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200114
Dees_Troya449a6f2013-04-07 17:50:11 -0500115 time_t StartupTime = time(NULL);
that0e2140e2016-08-10 21:04:26 +0200116 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 -0500117
118 // Load default values to set DataManager constants and handle ifdefs
119 DataManager::SetDefaultValues();
that0e2140e2016-08-10 21:04:26 +0200120 printf("Starting the UI...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500121 gui_init();
122 printf("=> Linking mtab\n");
123 symlink("/proc/mounts", "/etc/mtab");
Ethan Yonker53273ec2018-03-09 11:22:54 -0600124 std::string fstab_filename = "/etc/twrp.fstab";
125 if (!TWFunc::Path_Exists(fstab_filename)) {
126 fstab_filename = "/etc/recovery.fstab";
Dees_Troy329383e2013-08-29 14:16:06 +0000127 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100128
129 // Begin SAR detection
130 {
131 TWPartitionManager SarPartitionManager;
132 printf("=> Processing %s for SAR-detection\n", fstab_filename.c_str());
133 if (!SarPartitionManager.Process_Fstab(fstab_filename, 1, 1)) {
134 LOGERR("Failing out of recovery due to problem with fstab.\n");
135 return -1;
136 }
137
138 mkdir("/s", 0755);
139
140#if defined(AB_OTA_UPDATER) || defined(__ANDROID_API_Q__)
141 bool fallback_sar = true;
142#else
143 bool fallback_sar = property_get_bool("ro.build.system_root_image", false);
144#endif
145
146 if(SarPartitionManager.Mount_By_Path("/s", false)) {
147 if (TWFunc::Path_Exists("/s/build.prop")) {
148 LOGINFO("SAR-DETECT: Non-SAR System detected\n");
Chaosmaster5738d3d2020-05-20 22:16:37 +0200149 property_set("ro.twrp.sar", "false");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100150 rmdir("/system_root");
151 } else if (TWFunc::Path_Exists("/s/system/build.prop")) {
152 LOGINFO("SAR-DETECT: SAR System detected\n");
Chaosmaster5738d3d2020-05-20 22:16:37 +0200153 property_set("ro.twrp.sar", "true");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100154 } else {
155 LOGINFO("SAR-DETECT: No build.prop found, falling back to %s\n", fallback_sar ? "SAR" : "Non-SAR");
Chaosmaster5738d3d2020-05-20 22:16:37 +0200156 property_set("ro.twrp.sar", fallback_sar ? "true" : "false");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100157 }
Chaosmaster01a17992020-02-08 00:10:05 +0100158
159// We are doing this here during SAR-detection, since we are mounting the system-partition anyway
160// This way we don't need to remount it later, just for overriding properties
161#if defined(TW_INCLUDE_LIBRESETPROP) && defined(TW_OVERRIDE_SYSTEM_PROPS)
162 stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS));
163 string current_prop;
164 while (getline(override_props, current_prop, ';')) {
165 string other_prop;
166 if (current_prop.find("=") != string::npos) {
167 other_prop = current_prop.substr(current_prop.find("=") + 1);
168 current_prop = current_prop.substr(0, current_prop.find("="));
169 } else {
170 other_prop = current_prop;
171 }
Mohd Faraz0d9a62e2020-02-20 00:10:26 +0530172 other_prop = android::base::Trim(other_prop);
173 current_prop = android::base::Trim(current_prop);
Chaosmaster01a17992020-02-08 00:10:05 +0100174 string sys_val = TWFunc::System_Property_Get(other_prop, SarPartitionManager, "/s");
175 if (!sys_val.empty()) {
176 LOGINFO("Overriding %s with value: \"%s\" from system property %s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str());
177 int error = TWFunc::Property_Override(current_prop, sys_val);
178 if (error) {
179 LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error);
180 }
181 } else {
182 LOGINFO("Not overriding %s with empty value from system property %s\n", current_prop.c_str(), other_prop.c_str());
183 }
184 }
185#endif
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100186 SarPartitionManager.UnMount_By_Path("/s", false);
187 } else {
188 LOGINFO("SAR-DETECT: Could not mount system partition, falling back to %s\n", fallback_sar ? "SAR":"Non-SAR");
Chaosmaster5738d3d2020-05-20 22:16:37 +0200189 property_set("ro.twrp.sar", fallback_sar ? "true" : "false");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100190 }
191
192 rmdir("/s");
193
Captain Throwbackaaedfa82021-01-08 11:29:39 -0500194 TWFunc::check_and_run_script("/sbin/sarsetup.sh", "SAR");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100195 }
196 // End SAR detection
197
Ethan Yonker53273ec2018-03-09 11:22:54 -0600198 printf("=> Processing %s\n", fstab_filename.c_str());
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100199 if (!PartitionManager.Process_Fstab(fstab_filename, 1, 0)) {
Ethan Yonker53273ec2018-03-09 11:22:54 -0600200 LOGERR("Failing out of recovery due to problem with fstab.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500201 return -1;
202 }
203 PartitionManager.Output_Partition_Logging();
204 // Load up all the resources
205 gui_loadResources();
206
Matt Mower23d8aae2017-01-06 14:30:33 -0600207 bool Shutdown = false;
Ethan Yonkerb5236502016-11-19 22:24:59 -0600208 string Send_Intent = "";
Dees_Troya449a6f2013-04-07 17:50:11 -0500209 {
Dees_Troy1669f892013-09-04 18:35:08 +0000210 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
211 if (misc != NULL) {
212 if (misc->Current_File_System == "emmc") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500213 set_misc_device(misc->Actual_Block_Device.c_str());
Dees_Troy1669f892013-09-04 18:35:08 +0000214 } else {
Matt Mower3626bdc2017-01-06 13:45:54 -0600215 LOGERR("Only emmc /misc is supported\n");
Dees_Troy1669f892013-09-04 18:35:08 +0000216 }
217 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500218 get_args(&argc, &argv);
219
220 int index, index2, len;
221 char* argptr;
222 char* ptr;
223 printf("Startup Commands: ");
224 for (index = 1; index < argc; index++) {
225 argptr = argv[index];
226 printf(" '%s'", argv[index]);
227 len = strlen(argv[index]);
228 if (*argptr == '-') {argptr++; len--;}
229 if (*argptr == '-') {argptr++; len--;}
230 if (*argptr == 'u') {
231 ptr = argptr;
232 index2 = 0;
233 while (*ptr != '=' && *ptr != '\n')
234 ptr++;
Kevin Steck7b69e9d2013-10-17 18:24:31 -0400235 // skip the = before grabbing Zip_File
236 while (*ptr == '=')
237 ptr++;
Dees_Troya449a6f2013-04-07 17:50:11 -0500238 if (*ptr) {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600239 string ORSCommand = "install ";
240 ORSCommand.append(ptr);
241
242 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
243 break;
Dees_Troya449a6f2013-04-07 17:50:11 -0500244 } else
245 LOGERR("argument error specifying zip file\n");
246 } else if (*argptr == 'w') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600247 if (len == 9) {
248 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
249 break;
250 } else if (len == 10) {
251 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
252 break;
253 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600254 // 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 -0500255 } else if (*argptr == 'n') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600256 DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}"));
257 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
258 break;
Ethan Yonkera1674162014-11-06 08:35:10 -0600259 } else if (*argptr == 'p') {
260 Shutdown = true;
Dees_Troya449a6f2013-04-07 17:50:11 -0500261 } else if (*argptr == 's') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600262 if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600263 ptr = argptr + strlen("send_intent") + 1;
264 Send_Intent = *ptr;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600265 } else if (strncmp(argptr, "security", strlen("security")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600266 LOGINFO("Security update\n");
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600267 } else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600268 if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n"))
269 break;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600270 } else if (strncmp(argptr, "stages", strlen("stages")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600271 LOGINFO("ignoring stages command\n");
272 }
273 } else if (*argptr == 'r') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600274 if (strncmp(argptr, "reason", strlen("reason")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600275 ptr = argptr + strlen("reason") + 1;
276 gui_print("%s\n", ptr);
Dees_Troya449a6f2013-04-07 17:50:11 -0500277 }
278 }
279 }
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200280 printf("\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500281 }
282
Matt Mowera8a89d12016-12-30 18:10:37 -0600283 if (crash_counter == 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500284 property_list(Print_Prop, NULL);
285 printf("\n");
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200286 } else {
287 printf("twrp.crash_counter=%d\n", crash_counter);
Dees_Troya449a6f2013-04-07 17:50:11 -0500288 }
289
290 // Check for and run startup script if script exists
291 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
292 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
293
294#ifdef TW_INCLUDE_INJECTTWRP
295 // Back up TWRP Ramdisk if needed:
296 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500297 LOGINFO("Backing up TWRP ramdisk...\n");
298 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200299 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500300 else {
301 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200302 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500303 }
304 LOGINFO("Backup of TWRP ramdisk done.\n");
305#endif
306
Dees_Troya449a6f2013-04-07 17:50:11 -0500307 // Offer to decrypt if the device is encrypted
308 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
Captain Throwbackf8d0e212019-07-04 14:26:39 -0400309 LOGINFO("Is encrypted, do decrypt page first\n");
Noah Jacobson5a79f672019-04-28 00:10:07 -0400310 if (DataManager::GetIntValue(TW_IS_FBE))
311 DataManager::SetValue("tw_crypto_user_id", "0");
312 if (gui_startPage("decrypt", 1, 1) != 0) {
313 LOGERR("Failed to start decrypt GUI page.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -0600314 } else {
Captain Throwbackf8d0e212019-07-04 14:26:39 -0400315 // Check for and load custom theme if present
316 TWFunc::check_selinux_support();
317 gui_loadCustomResources();
Dees_Troya449a6f2013-04-07 17:50:11 -0500318 }
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600319 } else if (datamedia) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500320 TWFunc::check_selinux_support();
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600321 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -0600322 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600323 } else {
324 LOGINFO("Got default contexts and file mode for storage files.\n");
325 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500326 }
327
Vojtech Bocek2005a842014-03-11 19:40:34 +0100328 // Fixup the RTC clock on devices which require it
Noah Jacobson5a79f672019-04-28 00:10:07 -0400329 if (crash_counter == 0) TWFunc::Fixup_Time_On_Boot();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100330
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500331 // Read the settings file
332 TWFunc::Update_Log_File();
epicX98053c32021-01-04 13:01:31 +0530333 DataManager::LoadTWRPFolderInfo();
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500334 DataManager::ReadSettingsFile();
335 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
336 GUIConsole::Translate_Now();
337
Dees_Troya449a6f2013-04-07 17:50:11 -0500338 // Run any outstanding OpenRecoveryScript
bigbiff349ea552020-06-19 16:07:38 -0400339 std::string cacheDir = TWFunc::get_log_dir();
340 if (cacheDir == DATA_LOGS_DIR)
341 cacheDir = "/data/cache";
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500342 std::string orsFile = cacheDir + "/recovery/openrecoveryscript";
Captain Throwbackf8d0e212019-07-04 14:26:39 -0400343
Noah Jacobson5a79f672019-04-28 00:10:07 -0400344 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP) ||
345 (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && TWFunc::Path_Exists(orsFile))) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500346 OpenRecoveryScript::Run_OpenRecoveryScript();
347 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100348
Ethan Yonker6f49e112014-09-03 21:42:49 -0500349#ifdef TW_HAS_MTP
Ethan Yonker6f49e112014-09-03 21:42:49 -0500350 char mtp_crash_check[PROPERTY_VALUE_MAX];
351 property_get("mtp.crash_check", mtp_crash_check, "0");
Matt Mower653a1702017-02-16 10:38:52 -0600352 if (DataManager::GetIntValue("tw_mtp_enabled")
353 && !strcmp(mtp_crash_check, "0") && !crash_counter
354 && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) {
Ethan Yonker6f49e112014-09-03 21:42:49 -0500355 property_set("mtp.crash_check", "1");
Matt Mower653a1702017-02-16 10:38:52 -0600356 LOGINFO("Starting MTP\n");
357 if (!PartitionManager.Enable_MTP())
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600358 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600359 else
360 gui_msg("mtp_enabled=MTP Enabled");
Ethan Yonker6f49e112014-09-03 21:42:49 -0500361 property_set("mtp.crash_check", "0");
Matt Mower653a1702017-02-16 10:38:52 -0600362 } else if (strcmp(mtp_crash_check, "0")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500363 gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot.");
Ethan Yonker12055fa2014-09-04 08:06:53 -0500364 DataManager::SetValue("tw_mtp_enabled", 0);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600365 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600366 } else if (crash_counter == 1) {
367 LOGINFO("TWRP crashed; disabling MTP as a precaution.\n");
368 PartitionManager.Disable_MTP();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400369 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500370#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400371
Ethan Yonker89583ef2015-08-26 09:01:59 -0500372#ifndef TW_OEM_BUILD
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500373 // Check if system has never been changed
Captain Throwback9d6feb52018-07-27 10:05:24 -0400374 TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500375 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400376
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500377 if (sys) {
Ethan Yonker961d20e2015-06-29 14:00:03 -0500378 if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) {
379 if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
380 DataManager::SetValue("tw_back", "main");
381 if (gui_startPage("system_readonly", 1, 1) != 0) {
382 LOGERR("Failed to start system_readonly GUI page.\n");
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500383 }
Ethan Yonker961d20e2015-06-29 14:00:03 -0500384 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500385 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500386 if (ven)
387 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500388 }
Ethan Yonker961d20e2015-06-29 14:00:03 -0500389 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) {
390 // Do nothing, user selected to leave system read only
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500391 } else {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500392 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500393 if (ven)
394 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500395 }
396 }
Ethan Yonker89583ef2015-08-26 09:01:59 -0500397#endif
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400398 twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo();
399 adb_bu_fifo->threadAdbBuFifo();
400
epicX9d930a22020-12-29 00:39:36 +0530401 TWFunc::checkforapp(); //Checking compatibility for TWRP app
402
Dees_Troya449a6f2013-04-07 17:50:11 -0500403 // Launch the main GUI
404 gui_start();
405
Ethan Yonker89583ef2015-08-26 09:01:59 -0500406#ifndef TW_OEM_BUILD
Ethan Yonker9132d912015-02-02 10:32:49 -0600407 // Disable flashing of stock recovery
408 TWFunc::Disable_Stock_Recovery_Replace();
Ethan Yonker89583ef2015-08-26 09:01:59 -0500409#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500410
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 // Reboot
Ethan Yonkerb5236502016-11-19 22:24:59 -0600412 TWFunc::Update_Intent_File(Send_Intent);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400413 delete adb_bu_fifo;
bigbiff349ea552020-06-19 16:07:38 -0400414 TWFunc::Update_Log_File();
Ethan Yonker74db1572015-10-28 12:44:49 -0500415 gui_msg(Msg("rebooting=Rebooting..."));
Dees_Troya449a6f2013-04-07 17:50:11 -0500416 string Reboot_Arg;
417 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
418 if (Reboot_Arg == "recovery")
419 TWFunc::tw_reboot(rb_recovery);
420 else if (Reboot_Arg == "poweroff")
421 TWFunc::tw_reboot(rb_poweroff);
422 else if (Reboot_Arg == "bootloader")
423 TWFunc::tw_reboot(rb_bootloader);
424 else if (Reboot_Arg == "download")
425 TWFunc::tw_reboot(rb_download);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200426 else if (Reboot_Arg == "edl")
427 TWFunc::tw_reboot(rb_edl);
Dees_Troya449a6f2013-04-07 17:50:11 -0500428 else
429 TWFunc::tw_reboot(rb_system);
430
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500432}
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500433