blob: dfdf93b900f3ae4ff0fa83c5271cb3a681ee8305 [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"
Mohd Faraz0d9a62e2020-02-20 00:10:26 +053046#ifdef __ANDROID_API_N__
47#include <android-base/strings.h>
48#else
49#include <base/strings.h>
50#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050051#include "openrecoveryscript.hpp"
52#include "variables.h"
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040053#include "twrpAdbBuFifo.hpp"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050054#ifdef TW_USE_NEW_MINADBD
bigbiffd58ba182020-03-23 10:02:29 -040055// #include "minadbd/minadbd.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050056#else
57extern "C" {
Ethan Yonker84d61ce2017-05-10 16:11:35 -050058#include "minadbd21/adb.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050059}
60#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050061
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062//extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
Ethan Yonker534d4e02016-08-26 10:05:03 -050063
Dees_Troya449a6f2013-04-07 17:50:11 -050064TWPartitionManager PartitionManager;
65int Log_Offset;
Ethan Yonker6277c792014-09-15 14:54:30 -050066bool datamedia;
Dees_Troya449a6f2013-04-07 17:50:11 -050067
68static void Print_Prop(const char *key, const char *name, void *cookie) {
69 printf("%s=%s\n", key, name);
70}
71
bigbiff7ba75002020-04-11 20:47:09 -040072static void Decrypt_Page(bool SkipDecryption, bool datamedia) {
73 // Offer to decrypt if the device is encrypted
74 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
75 if (SkipDecryption) {
76 LOGINFO("Skipping decryption\n");
77 } else {
78 LOGINFO("Is encrypted, do decrypt page first\n");
Mohd Faraze3948ec2020-08-14 01:40:59 +000079 if (DataManager::GetIntValue(TW_IS_FBE))
80 DataManager::SetValue("tw_crypto_user_id", "0");
bigbiff7ba75002020-04-11 20:47:09 -040081 if (gui_startPage("decrypt", 1, 1) != 0) {
82 LOGERR("Failed to start decrypt GUI page.\n");
83 } else {
84 // Check for and load custom theme if present
85 TWFunc::check_selinux_support();
86 gui_loadCustomResources();
87 }
88 }
89 } else if (datamedia) {
90 TWFunc::check_selinux_support();
91 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
92 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
93 } else {
94 LOGINFO("Got default contexts and file mode for storage files.\n");
95 }
96 }
97}
98
Dees_Troya449a6f2013-04-07 17:50:11 -050099int main(int argc, char **argv) {
100 // Recovery needs to install world-readable files, so clear umask
101 // set by init
102 umask(0);
103
104 Log_Offset = 0;
105
106 // Set up temporary log file (/tmp/recovery.log)
107 freopen(TMP_LOG_FILE, "a", stdout);
108 setbuf(stdout, NULL);
109 freopen(TMP_LOG_FILE, "a", stderr);
110 setbuf(stderr, NULL);
111
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -0600112 signal(SIGPIPE, SIG_IGN);
113
Dees_Troya449a6f2013-04-07 17:50:11 -0500114 // Handle ADB sideload
115 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
thatcc8ddca2015-01-03 01:59:36 +0100116 property_set("ctl.stop", "adbd");
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500117#ifdef TW_USE_NEW_MINADBD
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500118 //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8
bigbiffd58ba182020-03-23 10:02:29 -0400119 // minadbd_main();
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500120#else
Dees_Troya449a6f2013-04-07 17:50:11 -0500121 adb_main(argv[2]);
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500122#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500123 return 0;
124 }
125
Ethan Yonker6277c792014-09-15 14:54:30 -0500126#ifdef RECOVERY_SDCARD_ON_DATA
127 datamedia = true;
128#endif
129
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200130 char crash_prop_val[PROPERTY_VALUE_MAX];
131 int crash_counter;
132 property_get("twrp.crash_counter", crash_prop_val, "-1");
133 crash_counter = atoi(crash_prop_val) + 1;
134 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
135 property_set("twrp.crash_counter", crash_prop_val);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400136 property_set("ro.twrp.boot", "1");
137 property_set("ro.twrp.version", TW_VERSION_STR);
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200138
Dees_Troya449a6f2013-04-07 17:50:11 -0500139 time_t StartupTime = time(NULL);
that0e2140e2016-08-10 21:04:26 +0200140 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 -0500141
142 // Load default values to set DataManager constants and handle ifdefs
143 DataManager::SetDefaultValues();
that0e2140e2016-08-10 21:04:26 +0200144 printf("Starting the UI...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500145 gui_init();
146 printf("=> Linking mtab\n");
147 symlink("/proc/mounts", "/etc/mtab");
Ethan Yonker53273ec2018-03-09 11:22:54 -0600148 std::string fstab_filename = "/etc/twrp.fstab";
149 if (!TWFunc::Path_Exists(fstab_filename)) {
150 fstab_filename = "/etc/recovery.fstab";
Dees_Troy329383e2013-08-29 14:16:06 +0000151 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100152
153 // Begin SAR detection
154 {
155 TWPartitionManager SarPartitionManager;
156 printf("=> Processing %s for SAR-detection\n", fstab_filename.c_str());
157 if (!SarPartitionManager.Process_Fstab(fstab_filename, 1, 1)) {
158 LOGERR("Failing out of recovery due to problem with fstab.\n");
159 return -1;
160 }
161
162 mkdir("/s", 0755);
163
164#if defined(AB_OTA_UPDATER) || defined(__ANDROID_API_Q__)
165 bool fallback_sar = true;
166#else
167 bool fallback_sar = property_get_bool("ro.build.system_root_image", false);
168#endif
169
170 if(SarPartitionManager.Mount_By_Path("/s", false)) {
171 if (TWFunc::Path_Exists("/s/build.prop")) {
172 LOGINFO("SAR-DETECT: Non-SAR System detected\n");
173 property_set("ro.twrp.sar", "0");
174 rmdir("/system_root");
175 } else if (TWFunc::Path_Exists("/s/system/build.prop")) {
176 LOGINFO("SAR-DETECT: SAR System detected\n");
177 property_set("ro.twrp.sar", "1");
178 } else {
179 LOGINFO("SAR-DETECT: No build.prop found, falling back to %s\n", fallback_sar ? "SAR" : "Non-SAR");
180 property_set("ro.twrp.sar", fallback_sar ? "1" : "0");
181 }
Chaosmaster01a17992020-02-08 00:10:05 +0100182
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100183 SarPartitionManager.UnMount_By_Path("/s", false);
184 } else {
185 LOGINFO("SAR-DETECT: Could not mount system partition, falling back to %s\n", fallback_sar ? "SAR":"Non-SAR");
186 property_set("ro.twrp.sar", fallback_sar ? "1" : "0");
187 }
188
189 rmdir("/s");
190
bigbiffad58e1b2020-07-06 20:24:34 -0400191 TWFunc::check_and_run_script("/system/bin/sarsetup.sh", "boot");
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100192 }
193 // End SAR detection
194
Ethan Yonker53273ec2018-03-09 11:22:54 -0600195 printf("=> Processing %s\n", fstab_filename.c_str());
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100196 if (!PartitionManager.Process_Fstab(fstab_filename, 1, 0)) {
Ethan Yonker53273ec2018-03-09 11:22:54 -0600197 LOGERR("Failing out of recovery due to problem with fstab.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500198 return -1;
199 }
200 PartitionManager.Output_Partition_Logging();
bigbiffee7b7ff2020-03-23 15:08:27 -0400201
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400202// We are doing this here to allow super partition to be set up prior to overriding properties
203#if defined(TW_INCLUDE_LIBRESETPROP) && defined(TW_OVERRIDE_SYSTEM_PROPS)
204 if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
205 LOGERR("Unable to mount %s\n", PartitionManager.Get_Android_Root_Path().c_str());
206 } else {
207 stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS));
208 string current_prop;
209 while (getline(override_props, current_prop, ';')) {
210 string other_prop;
211 if (current_prop.find("=") != string::npos) {
212 other_prop = current_prop.substr(current_prop.find("=") + 1);
213 current_prop = current_prop.substr(0, current_prop.find("="));
214 } else {
215 other_prop = current_prop;
216 }
217 other_prop = android::base::Trim(other_prop);
218 current_prop = android::base::Trim(current_prop);
219 string sys_val = TWFunc::System_Property_Get(other_prop, PartitionManager, PartitionManager.Get_Android_Root_Path().c_str());
220 if (!sys_val.empty()) {
221 LOGINFO("Overriding %s with value: \"%s\" from system property %s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str());
222 int error = TWFunc::Property_Override(current_prop, sys_val);
223 if (error) {
224 LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error);
225 }
226 } else {
227 LOGINFO("Not overriding %s with empty value from system property %s\n", current_prop.c_str(), other_prop.c_str());
228 }
229 }
230 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
231 }
232#endif
233
Dees_Troya449a6f2013-04-07 17:50:11 -0500234 // Load up all the resources
235 gui_loadResources();
236
Matt Mower23d8aae2017-01-06 14:30:33 -0600237 bool Shutdown = false;
bigbiffd58ba182020-03-23 10:02:29 -0400238 bool SkipDecryption = false;
Ethan Yonkerb5236502016-11-19 22:24:59 -0600239 string Send_Intent = "";
Dees_Troya449a6f2013-04-07 17:50:11 -0500240 {
Dees_Troy1669f892013-09-04 18:35:08 +0000241 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
242 if (misc != NULL) {
243 if (misc->Current_File_System == "emmc") {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500244 set_misc_device(misc->Actual_Block_Device.c_str());
Dees_Troy1669f892013-09-04 18:35:08 +0000245 } else {
Matt Mower3626bdc2017-01-06 13:45:54 -0600246 LOGERR("Only emmc /misc is supported\n");
Dees_Troy1669f892013-09-04 18:35:08 +0000247 }
248 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500249 get_args(&argc, &argv);
250
251 int index, index2, len;
252 char* argptr;
253 char* ptr;
254 printf("Startup Commands: ");
255 for (index = 1; index < argc; index++) {
256 argptr = argv[index];
257 printf(" '%s'", argv[index]);
258 len = strlen(argv[index]);
259 if (*argptr == '-') {argptr++; len--;}
260 if (*argptr == '-') {argptr++; len--;}
261 if (*argptr == 'u') {
262 ptr = argptr;
263 index2 = 0;
264 while (*ptr != '=' && *ptr != '\n')
265 ptr++;
Kevin Steck7b69e9d2013-10-17 18:24:31 -0400266 // skip the = before grabbing Zip_File
267 while (*ptr == '=')
268 ptr++;
Dees_Troya449a6f2013-04-07 17:50:11 -0500269 if (*ptr) {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600270 string ORSCommand = "install ";
271 ORSCommand.append(ptr);
272
bigbiffd58ba182020-03-23 10:02:29 -0400273 // If we have a map of blocks we don't need to mount data.
274 SkipDecryption = *ptr == '@';
275
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600276 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
277 break;
Dees_Troya449a6f2013-04-07 17:50:11 -0500278 } else
279 LOGERR("argument error specifying zip file\n");
280 } else if (*argptr == 'w') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600281 if (len == 9) {
282 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
283 break;
284 } else if (len == 10) {
285 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
286 break;
287 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600288 // 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 -0500289 } else if (*argptr == 'n') {
Ethan Yonker3aa66be2015-12-22 12:30:18 -0600290 DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}"));
291 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
292 break;
Ethan Yonkera1674162014-11-06 08:35:10 -0600293 } else if (*argptr == 'p') {
294 Shutdown = true;
Dees_Troya449a6f2013-04-07 17:50:11 -0500295 } else if (*argptr == 's') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600296 if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600297 ptr = argptr + strlen("send_intent") + 1;
298 Send_Intent = *ptr;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600299 } else if (strncmp(argptr, "security", strlen("security")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600300 LOGINFO("Security update\n");
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600301 } else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600302 if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n"))
303 break;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600304 } else if (strncmp(argptr, "stages", strlen("stages")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600305 LOGINFO("ignoring stages command\n");
306 }
307 } else if (*argptr == 'r') {
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600308 if (strncmp(argptr, "reason", strlen("reason")) == 0) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600309 ptr = argptr + strlen("reason") + 1;
310 gui_print("%s\n", ptr);
Dees_Troya449a6f2013-04-07 17:50:11 -0500311 }
312 }
313 }
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200314 printf("\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500315 }
316
Matt Mowera8a89d12016-12-30 18:10:37 -0600317 if (crash_counter == 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500318 property_list(Print_Prop, NULL);
319 printf("\n");
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200320 } else {
321 printf("twrp.crash_counter=%d\n", crash_counter);
Dees_Troya449a6f2013-04-07 17:50:11 -0500322 }
323
324 // Check for and run startup script if script exists
bigbiffad58e1b2020-07-06 20:24:34 -0400325 TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot");
326 TWFunc::check_and_run_script("/system/bin/postrecoveryboot.sh", "boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500327
328#ifdef TW_INCLUDE_INJECTTWRP
329 // Back up TWRP Ramdisk if needed:
330 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500331 LOGINFO("Backing up TWRP ramdisk...\n");
332 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200333 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500334 else {
335 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200336 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500337 }
338 LOGINFO("Backup of TWRP ramdisk done.\n");
339#endif
340
Mohd Faraze3948ec2020-08-14 01:40:59 +0000341 Decrypt_Page(SkipDecryption, datamedia);
Dees_Troya449a6f2013-04-07 17:50:11 -0500342
Vojtech Bocek2005a842014-03-11 19:40:34 +0100343 // Fixup the RTC clock on devices which require it
bigbiffadc599e2020-05-28 19:36:30 +0000344 if (crash_counter == 0)
345 TWFunc::Fixup_Time_On_Boot();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100346
bigbiff7ba75002020-04-11 20:47:09 -0400347 if (!PartitionManager.Get_Super_Status())
348 DataManager::ReadSettingsFile();
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500349 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
350 GUIConsole::Translate_Now();
351
Dees_Troya449a6f2013-04-07 17:50:11 -0500352 // Run any outstanding OpenRecoveryScript
bigbiff25d25b92020-06-19 16:07:38 -0400353 std::string cacheDir = TWFunc::get_log_dir();
354 if (cacheDir == DATA_LOGS_DIR)
355 cacheDir = "/data/cache";
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500356 std::string orsFile = cacheDir + "/recovery/openrecoveryscript";
bigbiffadc599e2020-05-28 19:36:30 +0000357 if ((DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 || SkipDecryption) && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(orsFile))) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500358 OpenRecoveryScript::Run_OpenRecoveryScript();
359 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100360
Ethan Yonker6f49e112014-09-03 21:42:49 -0500361#ifdef TW_HAS_MTP
Ethan Yonker6f49e112014-09-03 21:42:49 -0500362 char mtp_crash_check[PROPERTY_VALUE_MAX];
363 property_get("mtp.crash_check", mtp_crash_check, "0");
Matt Mower653a1702017-02-16 10:38:52 -0600364 if (DataManager::GetIntValue("tw_mtp_enabled")
365 && !strcmp(mtp_crash_check, "0") && !crash_counter
366 && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) {
Ethan Yonker6f49e112014-09-03 21:42:49 -0500367 property_set("mtp.crash_check", "1");
Matt Mower653a1702017-02-16 10:38:52 -0600368 LOGINFO("Starting MTP\n");
369 if (!PartitionManager.Enable_MTP())
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600370 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600371 else
372 gui_msg("mtp_enabled=MTP Enabled");
Ethan Yonker6f49e112014-09-03 21:42:49 -0500373 property_set("mtp.crash_check", "0");
Matt Mower653a1702017-02-16 10:38:52 -0600374 } else if (strcmp(mtp_crash_check, "0")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500375 gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot.");
Ethan Yonker12055fa2014-09-04 08:06:53 -0500376 DataManager::SetValue("tw_mtp_enabled", 0);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600377 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600378 } else if (crash_counter == 1) {
379 LOGINFO("TWRP crashed; disabling MTP as a precaution.\n");
380 PartitionManager.Disable_MTP();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400381 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500382#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400383
Ethan Yonker89583ef2015-08-26 09:01:59 -0500384#ifndef TW_OEM_BUILD
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500385 // Check if system has never been changed
Captain Throwback9d6feb52018-07-27 10:05:24 -0400386 TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500387 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500388 if (sys) {
bigbiffee7b7ff2020-03-23 15:08:27 -0400389 if (sys->Get_Super_Status()) {
bigbiff7ba75002020-04-11 20:47:09 -0400390#ifdef TW_INCLUDE_CRYPTO
bigbiff25d25b92020-06-19 16:07:38 -0400391 std::string recoveryLogDir(DATA_LOGS_DIR);
392 recoveryLogDir += "/recovery";
393 if (!TWFunc::Path_Exists(recoveryLogDir)) {
394 bool created = PartitionManager.Recreate_Logs_Dir();
395 if (!created)
396 LOGERR("Unable to create log directory for TWRP\n");
397 }
bigbiff7ba75002020-04-11 20:47:09 -0400398 DataManager::ReadSettingsFile();
399#endif
bigbiffee7b7ff2020-03-23 15:08:27 -0400400 } else {
401 if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) {
402 if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
403 DataManager::SetValue("tw_back", "main");
404 if (gui_startPage("system_readonly", 1, 1) != 0) {
405 LOGERR("Failed to start system_readonly GUI page.\n");
406 }
407 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) {
408 sys->Change_Mount_Read_Only(false);
409 if (ven)
410 ven->Change_Mount_Read_Only(false);
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500411 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400412 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) {
413 // Do nothing, user selected to leave system read only
414 } else {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500415 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500416 if (ven)
417 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500418 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500419 }
420 }
Ethan Yonker89583ef2015-08-26 09:01:59 -0500421#endif
bigbiff7ba75002020-04-11 20:47:09 -0400422
bigbiffaac58612020-08-30 11:18:39 -0400423 TWFunc::Update_Log_File();
424
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400425 twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo();
426 adb_bu_fifo->threadAdbBuFifo();
427
Dees_Troya449a6f2013-04-07 17:50:11 -0500428 // Launch the main GUI
429 gui_start();
430
Ethan Yonker89583ef2015-08-26 09:01:59 -0500431#ifndef TW_OEM_BUILD
Ethan Yonker9132d912015-02-02 10:32:49 -0600432 // Disable flashing of stock recovery
433 TWFunc::Disable_Stock_Recovery_Replace();
Ethan Yonker89583ef2015-08-26 09:01:59 -0500434#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500435
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200436 // Reboot
Ethan Yonkerb5236502016-11-19 22:24:59 -0600437 TWFunc::Update_Intent_File(Send_Intent);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400438 delete adb_bu_fifo;
bigbiff25d25b92020-06-19 16:07:38 -0400439 TWFunc::Update_Log_File();
Ethan Yonker74db1572015-10-28 12:44:49 -0500440 gui_msg(Msg("rebooting=Rebooting..."));
Dees_Troya449a6f2013-04-07 17:50:11 -0500441 string Reboot_Arg;
442 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
443 if (Reboot_Arg == "recovery")
444 TWFunc::tw_reboot(rb_recovery);
445 else if (Reboot_Arg == "poweroff")
446 TWFunc::tw_reboot(rb_poweroff);
447 else if (Reboot_Arg == "bootloader")
448 TWFunc::tw_reboot(rb_bootloader);
449 else if (Reboot_Arg == "download")
450 TWFunc::tw_reboot(rb_download);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200451 else if (Reboot_Arg == "edl")
452 TWFunc::tw_reboot(rb_edl);
Dees_Troya449a6f2013-04-07 17:50:11 -0500453 else
454 TWFunc::tw_reboot(rb_system);
455
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200456 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500457}
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500458