blob: b257780d5e3daaa1cb3db39c172ec93b2b4ba10e [file] [log] [blame]
Dees_Troya449a6f2013-04-07 17:50:11 -05001/*
bigbiffdf8436b2020-08-30 16:22:34 -04002 Copyright 2012-2020 TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
Dees_Troya449a6f2013-04-07 17:50:11 -05004
bigbiffdf8436b2020-08-30 16:22:34 -04005 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
Dees_Troya449a6f2013-04-07 17:50:11 -05009
bigbiffdf8436b2020-08-30 16:22:34 -040010 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
Dees_Troya449a6f2013-04-07 17:50:11 -050017*/
18
19#include <stdio.h>
20#include <stdlib.h>
Ethan Yonker74db1572015-10-28 12:44:49 -050021#include <string>
Dees_Troya449a6f2013-04-07 17:50:11 -050022#include <sys/stat.h>
23#include <sys/types.h>
24#include <time.h>
25#include <unistd.h>
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -060026#include <signal.h>
Mohd Faraza37d9302023-09-18 06:24:08 +000027#include <thread>
28#include <chrono>
29#include "recovery_utils/battery_utils.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050030#include "gui/twmsg.h"
Dees_Troya449a6f2013-04-07 17:50:11 -050031
32#include "cutils/properties.h"
Dees_Troya449a6f2013-04-07 17:50:11 -050033
34#ifdef ANDROID_RB_RESTART
35#include "cutils/android_reboot.h"
36#else
37#include <sys/reboot.h>
38#endif
39
40extern "C" {
41#include "gui/gui.h"
42}
Ethan Yonkerf1179622016-08-25 15:32:21 -050043#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050044#include "gui/gui.hpp"
45#include "gui/pages.hpp"
46#include "gui/objects.hpp"
Dees_Troya449a6f2013-04-07 17:50:11 -050047#include "twcommon.h"
48#include "twrp-functions.hpp"
49#include "data.hpp"
Captain Throwback00f0dcd2022-04-14 20:13:10 -040050
Dees_Troya449a6f2013-04-07 17:50:11 -050051#include "partitions.hpp"
Mohd Faraz0d9a62e2020-02-20 00:10:26 +053052#ifdef __ANDROID_API_N__
53#include <android-base/strings.h>
54#else
55#include <base/strings.h>
56#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050057#include "openrecoveryscript.hpp"
58#include "variables.h"
bigbiffdf8436b2020-08-30 16:22:34 -040059#include "startupArgs.hpp"
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040060#include "twrpAdbBuFifo.hpp"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050061#ifdef TW_USE_NEW_MINADBD
bigbiffd58ba182020-03-23 10:02:29 -040062// #include "minadbd/minadbd.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050063#else
64extern "C" {
Ethan Yonker84d61ce2017-05-10 16:11:35 -050065#include "minadbd21/adb.h"
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050066}
67#endif
Dees_Troya449a6f2013-04-07 17:50:11 -050068
bigbiffdf53b6c2022-01-17 19:31:52 -050069#ifdef TW_INCLUDE_CRYPTO
70#include "FsCrypt.h"
71#include "Decrypt.h"
72#endif
73
Ethan Yonker8373cfe2017-09-08 06:50:54 -050074//extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
Ethan Yonker534d4e02016-08-26 10:05:03 -050075
Dees_Troya449a6f2013-04-07 17:50:11 -050076TWPartitionManager PartitionManager;
77int Log_Offset;
Ethan Yonker6277c792014-09-15 14:54:30 -050078bool datamedia;
Dees_Troya449a6f2013-04-07 17:50:11 -050079
80static void Print_Prop(const char *key, const char *name, void *cookie) {
81 printf("%s=%s\n", key, name);
82}
83
bigbiff7ba75002020-04-11 20:47:09 -040084static void Decrypt_Page(bool SkipDecryption, bool datamedia) {
85 // Offer to decrypt if the device is encrypted
86 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
87 if (SkipDecryption) {
88 LOGINFO("Skipping decryption\n");
Captain Throwback89b56a12021-02-10 18:25:07 -050089 PartitionManager.Update_System_Details();
Mohd Faraz815e2402020-09-19 19:31:52 +053090 } else if (DataManager::GetIntValue(TW_CRYPTO_PWTYPE) != 0) {
bigbiff7ba75002020-04-11 20:47:09 -040091 LOGINFO("Is encrypted, do decrypt page first\n");
Mohd Faraze3948ec2020-08-14 01:40:59 +000092 if (DataManager::GetIntValue(TW_IS_FBE))
93 DataManager::SetValue("tw_crypto_user_id", "0");
bigbiff7ba75002020-04-11 20:47:09 -040094 if (gui_startPage("decrypt", 1, 1) != 0) {
95 LOGERR("Failed to start decrypt GUI page.\n");
bigbiff7ba75002020-04-11 20:47:09 -040096 }
97 }
98 } else if (datamedia) {
Captain Throwback89b56a12021-02-10 18:25:07 -050099 PartitionManager.Update_System_Details();
nielsdbed9852023-03-14 18:54:34 +0100100 if (tw_get_default_metadata(DataManager::GetCurrentStoragePath().c_str()) != 0) {
bigbiff7ba75002020-04-11 20:47:09 -0400101 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
102 } else {
103 LOGINFO("Got default contexts and file mode for storage files.\n");
104 }
105 }
106}
107
bigbiff22851b92021-09-01 16:46:57 -0400108static void process_fastbootd_mode() {
109 LOGINFO("starting fastboot\n");
Ctapchukaefc2ab2021-10-29 14:40:52 +0700110
111#ifdef TW_LOAD_VENDOR_MODULES
bigbiff850fa282021-10-09 12:37:29 -0400112 if (android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) {
113 PartitionManager.Unmap_Super_Devices();
114 }
bigbiff22851b92021-09-01 16:46:57 -0400115#endif
bigbiff850fa282021-10-09 12:37:29 -0400116
bigbiff22851b92021-09-01 16:46:57 -0400117 gui_msg(Msg("fastboot_console_msg=Entered Fastboot mode..."));
MD Raza9124c932021-12-07 22:05:08 +0000118 // Check for and run startup script if script exists
119 TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot");
120 TWFunc::check_and_run_script("/system/bin/postfastboot.sh", "fastboot");
bigbiff22851b92021-09-01 16:46:57 -0400121 if (gui_startPage("fastboot", 1, 1) != 0) {
122 LOGERR("Failed to start fastbootd page.\n");
123 }
124}
125
bigbiffdf8436b2020-08-30 16:22:34 -0400126static void process_recovery_mode(twrpAdbBuFifo* adb_bu_fifo, bool skip_decryption) {
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200127 char crash_prop_val[PROPERTY_VALUE_MAX];
128 int crash_counter;
bigbiff22851b92021-09-01 16:46:57 -0400129
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200130 property_get("twrp.crash_counter", crash_prop_val, "-1");
131 crash_counter = atoi(crash_prop_val) + 1;
132 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
133 property_set("twrp.crash_counter", crash_prop_val);
134
bigbiffdf8436b2020-08-30 16:22:34 -0400135 if (crash_counter == 0) {
136 property_list(Print_Prop, NULL);
137 printf("\n");
138 } else {
139 printf("twrp.crash_counter=%d\n", crash_counter);
140 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500141
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400142// We are doing this here to allow super partition to be set up prior to overriding properties
niels44e23462023-09-12 22:15:59 +0200143#if defined(TW_INCLUDE_LIBRESETPROP)
144 std::vector<std::string> build_date_props = {"ro.build.date.utc", "ro.bootimage.build.date.utc", "ro.vendor.build.date.utc", "ro.system.build.date.utc", "ro.system_ext.build.date.utc", "ro.product.build.date.utc", "ro.odm.build.date.utc"};
145 std::string val = "0";
146 for (auto prop : build_date_props) {
147 TWFunc::Property_Override(prop, val);
148 LOGINFO("Overriding %s with value: \"%s\"\n", prop.c_str(), val.c_str());
149 }
150#if defined(TW_OVERRIDE_SYSTEM_PROPS)
Captain Throwback1483a8e2022-01-03 09:52:06 -0500151 stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS));
152 string current_prop;
Captain Throwback2b9ea722021-12-31 21:35:52 -0500153
Captain Throwback1483a8e2022-01-03 09:52:06 -0500154 std::vector<std::string> partition_list;
155 partition_list.push_back (PartitionManager.Get_Android_Root_Path().c_str());
156#ifdef TW_OVERRIDE_PROPS_ADDITIONAL_PARTITIONS
157 std::vector<std::string> additional_partition_list = TWFunc::Split_String(TW_OVERRIDE_PROPS_ADDITIONAL_PARTITIONS, " ");
158 partition_list.insert(partition_list.end(), additional_partition_list.begin(), additional_partition_list.end());
159#endif
160 std::vector<std::string> build_prop_list = {"build.prop"};
161#ifdef TW_SYSTEM_BUILD_PROP_ADDITIONAL_PATHS
162 std::vector<std::string> additional_build_prop_list = TWFunc::Split_String(TW_SYSTEM_BUILD_PROP_ADDITIONAL_PATHS, ";");
163 build_prop_list.insert(build_prop_list.end(), additional_build_prop_list.begin(), additional_build_prop_list.end());
164#endif
165 while (getline(override_props, current_prop, ';')) {
166 string other_prop;
167 if (current_prop.find("=") != string::npos) {
168 other_prop = current_prop.substr(current_prop.find("=") + 1);
169 current_prop = current_prop.substr(0, current_prop.find("="));
170 } else {
171 other_prop = current_prop;
172 }
173 other_prop = android::base::Trim(other_prop);
174 current_prop = android::base::Trim(current_prop);
175
176 for (auto&& partition_mount_point:partition_list) {
Captain Throwback2b9ea722021-12-31 21:35:52 -0500177 for (auto&& prop_file:build_prop_list) {
Captain Throwback1483a8e2022-01-03 09:52:06 -0500178 string sys_val = TWFunc::Partition_Property_Get(other_prop, PartitionManager, partition_mount_point.c_str(), prop_file);
Captain Throwback2b9ea722021-12-31 21:35:52 -0500179 if (!sys_val.empty()) {
Captain Throwback1483a8e2022-01-03 09:52:06 -0500180 if (partition_mount_point == "/system_root") {
181 LOGINFO("Overriding %s with value: \"%s\" from property %s in /system/%s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str(),
182 prop_file.c_str());
183 } else {
184 LOGINFO("Overriding %s with value: \"%s\" from property %s in /%s/%s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str(),
185 partition_mount_point.c_str(), prop_file.c_str());
186 }
Captain Throwback2b9ea722021-12-31 21:35:52 -0500187 int error = TWFunc::Property_Override(current_prop, sys_val);
188 if (error) {
189 LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error);
190 }
Captain Throwback1483a8e2022-01-03 09:52:06 -0500191 if (partition_mount_point == partition_list.back()) {
192 PartitionManager.UnMount_By_Path(partition_mount_point, false);
193 }
194 goto exit;
Captain Throwback2b9ea722021-12-31 21:35:52 -0500195 } else {
Captain Throwback1483a8e2022-01-03 09:52:06 -0500196 if (partition_mount_point == "/system_root") {
197 LOGINFO("Unable to override property %s: property not found in /system/%s\n", current_prop.c_str(), prop_file.c_str());
198 } else {
199 LOGINFO("Unable to override property %s: property not found in /%s/%s\n", current_prop.c_str(), partition_mount_point.c_str(), prop_file.c_str());
200 }
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400201 }
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400202 }
Captain Throwback1483a8e2022-01-03 09:52:06 -0500203 PartitionManager.UnMount_By_Path(partition_mount_point, false);
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400204 }
Captain Throwback1483a8e2022-01-03 09:52:06 -0500205 exit:
206 continue;
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400207 }
niels44e23462023-09-12 22:15:59 +0200208#endif // defined(TW_OVERRIDE_SYSTEM_PROPS)
209#endif // defined(TW_INCLUDE_LIBRESETPROP)
Captain Throwbackdc0823c2020-09-06 13:07:23 -0400210
Dees_Troya449a6f2013-04-07 17:50:11 -0500211 // Check for and run startup script if script exists
bigbiffad58e1b2020-07-06 20:24:34 -0400212 TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot");
Captain Throwback3345ceb2021-12-10 15:31:48 -0500213 TWFunc::check_and_run_script("/system/bin/postrecoveryboot.sh", "recovery");
Dees_Troya449a6f2013-04-07 17:50:11 -0500214
215#ifdef TW_INCLUDE_INJECTTWRP
216 // Back up TWRP Ramdisk if needed:
217 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500218 LOGINFO("Backing up TWRP ramdisk...\n");
219 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200220 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500221 else {
222 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200223 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500224 }
225 LOGINFO("Backup of TWRP ramdisk done.\n");
226#endif
bigbiffdf53b6c2022-01-17 19:31:52 -0500227#ifdef TW_INCLUDE_CRYPTO
228 android::keystore::copySqliteDb();
229#endif
bigbiffdf8436b2020-08-30 16:22:34 -0400230 Decrypt_Page(skip_decryption, datamedia);
bigbiffdf53b6c2022-01-17 19:31:52 -0500231
bigbiff83298f52021-10-13 19:24:42 -0400232 // Check for and load custom theme if present
233 TWFunc::check_selinux_support();
234 gui_loadCustomResources();
Captain Throwback89b56a12021-02-10 18:25:07 -0500235 PartitionManager.Output_Partition_Logging();
Dees_Troya449a6f2013-04-07 17:50:11 -0500236
Vojtech Bocek2005a842014-03-11 19:40:34 +0100237 // Fixup the RTC clock on devices which require it
bigbiffadc599e2020-05-28 19:36:30 +0000238 if (crash_counter == 0)
239 TWFunc::Fixup_Time_On_Boot();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100240
epicXa721f952021-01-04 13:01:31 +0530241 DataManager::LoadTWRPFolderInfo();
Ian Macdonald1e0474a2020-09-09 02:43:13 +0200242 DataManager::ReadSettingsFile();
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500243
Dees_Troya449a6f2013-04-07 17:50:11 -0500244 // Run any outstanding OpenRecoveryScript
bigbiff25d25b92020-06-19 16:07:38 -0400245 std::string cacheDir = TWFunc::get_log_dir();
246 if (cacheDir == DATA_LOGS_DIR)
247 cacheDir = "/data/cache";
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500248 std::string orsFile = cacheDir + "/recovery/openrecoveryscript";
bigbiffdf8436b2020-08-30 16:22:34 -0400249 if ((DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 || skip_decryption) && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(orsFile))) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500250 OpenRecoveryScript::Run_OpenRecoveryScript();
251 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100252
Ethan Yonker6f49e112014-09-03 21:42:49 -0500253#ifdef TW_HAS_MTP
Ethan Yonker6f49e112014-09-03 21:42:49 -0500254 char mtp_crash_check[PROPERTY_VALUE_MAX];
255 property_get("mtp.crash_check", mtp_crash_check, "0");
Matt Mower653a1702017-02-16 10:38:52 -0600256 if (DataManager::GetIntValue("tw_mtp_enabled")
257 && !strcmp(mtp_crash_check, "0") && !crash_counter
258 && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) {
Ethan Yonker6f49e112014-09-03 21:42:49 -0500259 property_set("mtp.crash_check", "1");
Matt Mower653a1702017-02-16 10:38:52 -0600260 LOGINFO("Starting MTP\n");
261 if (!PartitionManager.Enable_MTP())
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600262 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600263 else
264 gui_msg("mtp_enabled=MTP Enabled");
Ethan Yonker6f49e112014-09-03 21:42:49 -0500265 property_set("mtp.crash_check", "0");
Matt Mower653a1702017-02-16 10:38:52 -0600266 } else if (strcmp(mtp_crash_check, "0")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500267 gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot.");
Ethan Yonker12055fa2014-09-04 08:06:53 -0500268 DataManager::SetValue("tw_mtp_enabled", 0);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -0600269 PartitionManager.Disable_MTP();
Matt Mower653a1702017-02-16 10:38:52 -0600270 } else if (crash_counter == 1) {
271 LOGINFO("TWRP crashed; disabling MTP as a precaution.\n");
272 PartitionManager.Disable_MTP();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400273 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500274#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400275
Ethan Yonker89583ef2015-08-26 09:01:59 -0500276#ifndef TW_OEM_BUILD
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500277 // Check if system has never been changed
Captain Throwback9d6feb52018-07-27 10:05:24 -0400278 TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500279 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500280 if (sys) {
bigbiffee7b7ff2020-03-23 15:08:27 -0400281 if (sys->Get_Super_Status()) {
bigbiff7ba75002020-04-11 20:47:09 -0400282#ifdef TW_INCLUDE_CRYPTO
bigbiff25d25b92020-06-19 16:07:38 -0400283 std::string recoveryLogDir(DATA_LOGS_DIR);
284 recoveryLogDir += "/recovery";
285 if (!TWFunc::Path_Exists(recoveryLogDir)) {
286 bool created = PartitionManager.Recreate_Logs_Dir();
287 if (!created)
288 LOGERR("Unable to create log directory for TWRP\n");
289 }
bigbiff7ba75002020-04-11 20:47:09 -0400290 DataManager::ReadSettingsFile();
291#endif
bigbiffee7b7ff2020-03-23 15:08:27 -0400292 } else {
293 if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) {
294 if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
295 DataManager::SetValue("tw_back", "main");
296 if (gui_startPage("system_readonly", 1, 1) != 0) {
297 LOGERR("Failed to start system_readonly GUI page.\n");
298 }
299 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) {
300 sys->Change_Mount_Read_Only(false);
301 if (ven)
302 ven->Change_Mount_Read_Only(false);
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500303 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400304 } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) {
305 // Do nothing, user selected to leave system read only
306 } else {
Ethan Yonker0e2c6572015-06-04 11:30:02 -0500307 sys->Change_Mount_Read_Only(false);
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500308 if (ven)
309 ven->Change_Mount_Read_Only(false);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500310 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500311 }
312 }
Ethan Yonker89583ef2015-08-26 09:01:59 -0500313#endif
bigbiff7ba75002020-04-11 20:47:09 -0400314
bigbiffaac58612020-08-30 11:18:39 -0400315 TWFunc::Update_Log_File();
316
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400317 adb_bu_fifo->threadAdbBuFifo();
318
Ethan Yonker89583ef2015-08-26 09:01:59 -0500319#ifndef TW_OEM_BUILD
Ethan Yonker9132d912015-02-02 10:32:49 -0600320 // Disable flashing of stock recovery
321 TWFunc::Disable_Stock_Recovery_Replace();
Ethan Yonker89583ef2015-08-26 09:01:59 -0500322#endif
bigbiffdf8436b2020-08-30 16:22:34 -0400323}
Dees_Troya449a6f2013-04-07 17:50:11 -0500324
bigbiffdf8436b2020-08-30 16:22:34 -0400325static void reboot() {
Ethan Yonker74db1572015-10-28 12:44:49 -0500326 gui_msg(Msg("rebooting=Rebooting..."));
bigbiffdf8436b2020-08-30 16:22:34 -0400327 TWFunc::Update_Log_File();
Dees_Troya449a6f2013-04-07 17:50:11 -0500328 string Reboot_Arg;
329 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
330 if (Reboot_Arg == "recovery")
331 TWFunc::tw_reboot(rb_recovery);
332 else if (Reboot_Arg == "poweroff")
333 TWFunc::tw_reboot(rb_poweroff);
334 else if (Reboot_Arg == "bootloader")
335 TWFunc::tw_reboot(rb_bootloader);
336 else if (Reboot_Arg == "download")
337 TWFunc::tw_reboot(rb_download);
mauronofrioe9a49ef2018-10-03 13:38:16 +0200338 else if (Reboot_Arg == "edl")
339 TWFunc::tw_reboot(rb_edl);
bigbiffdf8436b2020-08-30 16:22:34 -0400340 else if (Reboot_Arg == "fastboot")
341 TWFunc::tw_reboot(rb_fastboot);
Dees_Troya449a6f2013-04-07 17:50:11 -0500342 else
343 TWFunc::tw_reboot(rb_system);
bigbiffdf8436b2020-08-30 16:22:34 -0400344}
345
346int main(int argc, char **argv) {
347 // Recovery needs to install world-readable files, so clear umask
348 // set by init
349 umask(0);
bigbiffdf8436b2020-08-30 16:22:34 -0400350 Log_Offset = 0;
351
352 // Set up temporary log file (/tmp/recovery.log)
353 freopen(TMP_LOG_FILE, "a", stdout);
354 setbuf(stdout, NULL);
355 freopen(TMP_LOG_FILE, "a", stderr);
356 setbuf(stderr, NULL);
357
358 signal(SIGPIPE, SIG_IGN);
359
360 // Handle ADB sideload
361 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
362 property_set("ctl.stop", "adbd");
363#ifdef TW_USE_NEW_MINADBD
364 //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8
365 // minadbd_main();
366#else
367 adb_main(argv[2]);
368#endif
369 return 0;
370 }
371
372#ifdef RECOVERY_SDCARD_ON_DATA
373 datamedia = true;
374#endif
375
376 property_set("ro.twrp.boot", "1");
377 property_set("ro.twrp.version", TW_VERSION_STR);
378
DarthJabba97cd3abb2021-11-28 01:09:44 +0530379#ifdef TARGET_OTA_ASSERT_DEVICE
380 property_set("ro.twrp.target.devices", TARGET_OTA_ASSERT_DEVICE);
381#endif
382
bigbiffdf8436b2020-08-30 16:22:34 -0400383 time_t StartupTime = time(NULL);
384 printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, ctime(&StartupTime), getpid());
385
386 // Load default values to set DataManager constants and handle ifdefs
387 DataManager::SetDefaultValues();
Mohd Faraz88b4bab2023-01-01 02:43:01 +0530388 startupArgs startup;
389 startup.parse(&argc, &argv);
Mohd Farazfe8bcd42023-08-26 20:50:26 +0530390 android::base::SetProperty(TW_FASTBOOT_MODE_PROP, startup.Get_Fastboot_Mode() ? "1" : "0");
Mohd Faraz88b4bab2023-01-01 02:43:01 +0530391 printf("=> Linking mtab\n");
392 symlink("/proc/mounts", "/etc/mtab");
393 std::string fstab_filename = "/etc/twrp.fstab";
394 if (!TWFunc::Path_Exists(fstab_filename)) {
395 fstab_filename = "/etc/recovery.fstab";
396 }
397 printf("=> Processing %s\n", fstab_filename.c_str());
398 if (!PartitionManager.Process_Fstab(fstab_filename, 1, !startup.Get_Fastboot_Mode())) {
399 LOGERR("Failing out of recovery due to problem with fstab.\n");
400 return -1;
401 }
402
403#ifdef TW_LOAD_VENDOR_MODULES
micky38756f748d2023-08-15 15:32:11 +0200404 if (startup.Get_Fastboot_Mode()) {
micky3879aa71502023-08-15 15:36:00 +0200405 TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm");
Mohd Faraz88b4bab2023-01-01 02:43:01 +0530406 PartitionManager.Prepare_Super_Volume(PartitionManager.Find_Partition_By_Path("/vendor"));
micky3879aa71502023-08-15 15:36:00 +0200407 if(ven_dlkm) {
408 PartitionManager.Prepare_Super_Volume(ven_dlkm);
409 }
micky38756f748d2023-08-15 15:32:11 +0200410 }
Mohd Faraz88b4bab2023-01-01 02:43:01 +0530411#endif
412
Mohd Faraze7b5add2023-08-28 16:46:38 +0530413 printf("Starting the UI...\n");
414 gui_init();
415
Mohd Farazec370452023-08-28 16:51:35 +0530416 if (!startup.Get_Fastboot_Mode()) PartitionManager.Setup_Fstab_Partitions(true);
417
bigbiffdf8436b2020-08-30 16:22:34 -0400418 // Load up all the resources
419 gui_loadResources();
420
Mohd Faraza37d9302023-09-18 06:24:08 +0000421 std::string value;
422 static char charging = ' ';
423 static int lastVal = -1;
424
425 // Function to monitor battery in the background
426 auto monitorBatteryInBackground = [&]() {
427 while (true) {
Captain Throwback440525b2023-11-20 11:07:23 -0500428#ifdef TW_USE_LEGACY_BATTERY_SERVICES
429 char cap_s[4];
430#ifdef TW_CUSTOM_BATTERY_PATH
431 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
432 capacity_file += "/capacity";
433 FILE * cap = fopen(capacity_file.c_str(),"rt");
434#else
435 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
436#endif
437 if (cap) {
438 fgets(cap_s, 4, cap);
439 fclose(cap);
440 lastVal = atoi(cap_s);
441 if (lastVal > 100) lastVal = 101;
442 if (lastVal < 0) lastVal = 0;
443 }
444#ifdef TW_CUSTOM_BATTERY_PATH
445 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
446 status_file += "/status";
447 cap = fopen(status_file.c_str(),"rt");
448#else
449 cap = fopen("/sys/class/power_supply/battery/status","rt");
450#endif
451 if (cap) {
452 fgets(cap_s, 2, cap);
453 fclose(cap);
454 if (cap_s[0] == 'C')
455 charging = '+';
456 else
457 charging = ' ';
458 }
459#else
Mohd Faraza37d9302023-09-18 06:24:08 +0000460 auto battery_info = GetBatteryInfo();
461 if (battery_info.charging) {
462 charging = '+';
463 } else {
464 charging = ' ';
465 }
466 lastVal = battery_info.capacity;
Captain Throwback440525b2023-11-20 11:07:23 -0500467#endif
Mohd Faraza37d9302023-09-18 06:24:08 +0000468 // Format the value based on the background updates
469 value = std::to_string(lastVal) + "%" + charging;
470 DataManager::SetValue("tw_battery", value);
471
472 // Sleep for a specified interval (e.g., 1 second) before checking again
473 std::this_thread::sleep_for(std::chrono::seconds(1));
474 }
475 };
476
477 // Create a thread for battery monitoring
478 static std::thread battery_monitor(monitorBatteryInBackground);
479
bigbiffdf8436b2020-08-30 16:22:34 -0400480 twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo();
481 TWFunc::Clear_Bootloader_Message();
482
483 if (startup.Get_Fastboot_Mode()) {
bigbiff22851b92021-09-01 16:46:57 -0400484 process_fastbootd_mode();
bigbiffdf8436b2020-08-30 16:22:34 -0400485 delete adb_bu_fifo;
486 TWFunc::Update_Intent_File(startup.Get_Intent());
bigbiffdf8436b2020-08-30 16:22:34 -0400487 reboot();
488 return 0;
489 } else {
490 process_recovery_mode(adb_bu_fifo, startup.Should_Skip_Decryption());
491 }
492
Ian Macdonalda9910222020-10-27 18:02:21 +0100493 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
494 GUIConsole::Translate_Now();
495
epicX271bb3a2020-12-30 01:03:18 +0530496 TWFunc::checkforapp(); //Checking compatibility for TWRP app
497
bigbiffdf8436b2020-08-30 16:22:34 -0400498 // Launch the main GUI
499 gui_start();
500 delete adb_bu_fifo;
501 TWFunc::Update_Intent_File(startup.Get_Intent());
bigbiffdf8436b2020-08-30 16:22:34 -0400502 reboot();
Dees_Troya449a6f2013-04-07 17:50:11 -0500503
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200504 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500505}