Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 1 | /* |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 2 | Copyright 2012-2020 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 4 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 5 | 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 9 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 10 | 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 21 | #include <string> |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
| 25 | #include <unistd.h> |
Ethan Yonker | f9f99bc | 2014-12-29 09:10:34 -0600 | [diff] [blame] | 26 | #include <signal.h> |
Mohd Faraz | a37d930 | 2023-09-18 06:24:08 +0000 | [diff] [blame^] | 27 | #include <thread> |
| 28 | #include <chrono> |
| 29 | #include "recovery_utils/battery_utils.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 30 | #include "gui/twmsg.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 31 | |
| 32 | #include "cutils/properties.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 33 | |
| 34 | #ifdef ANDROID_RB_RESTART |
| 35 | #include "cutils/android_reboot.h" |
| 36 | #else |
| 37 | #include <sys/reboot.h> |
| 38 | #endif |
| 39 | |
| 40 | extern "C" { |
| 41 | #include "gui/gui.h" |
| 42 | } |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 43 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 44 | #include "gui/gui.hpp" |
| 45 | #include "gui/pages.hpp" |
| 46 | #include "gui/objects.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 47 | #include "twcommon.h" |
| 48 | #include "twrp-functions.hpp" |
| 49 | #include "data.hpp" |
Captain Throwback | 00f0dcd | 2022-04-14 20:13:10 -0400 | [diff] [blame] | 50 | |
| 51 | #ifdef TW_LOAD_VENDOR_MODULES |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 52 | #include "kernel_module_loader.hpp" |
Captain Throwback | 00f0dcd | 2022-04-14 20:13:10 -0400 | [diff] [blame] | 53 | #endif |
| 54 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 55 | #include "partitions.hpp" |
Mohd Faraz | 0d9a62e | 2020-02-20 00:10:26 +0530 | [diff] [blame] | 56 | #ifdef __ANDROID_API_N__ |
| 57 | #include <android-base/strings.h> |
| 58 | #else |
| 59 | #include <base/strings.h> |
| 60 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 61 | #include "openrecoveryscript.hpp" |
| 62 | #include "variables.h" |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 63 | #include "startupArgs.hpp" |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 64 | #include "twrpAdbBuFifo.hpp" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 65 | #ifdef TW_USE_NEW_MINADBD |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 66 | // #include "minadbd/minadbd.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 67 | #else |
| 68 | extern "C" { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 69 | #include "minadbd21/adb.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 70 | } |
| 71 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 72 | |
bigbiff | df53b6c | 2022-01-17 19:31:52 -0500 | [diff] [blame] | 73 | #ifdef TW_INCLUDE_CRYPTO |
| 74 | #include "FsCrypt.h" |
| 75 | #include "Decrypt.h" |
| 76 | #endif |
| 77 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 78 | //extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */); |
Ethan Yonker | 534d4e0 | 2016-08-26 10:05:03 -0500 | [diff] [blame] | 79 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 80 | TWPartitionManager PartitionManager; |
| 81 | int Log_Offset; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 82 | bool datamedia; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 83 | |
| 84 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 85 | printf("%s=%s\n", key, name); |
| 86 | } |
| 87 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 88 | static void Decrypt_Page(bool SkipDecryption, bool datamedia) { |
| 89 | // Offer to decrypt if the device is encrypted |
| 90 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 91 | if (SkipDecryption) { |
| 92 | LOGINFO("Skipping decryption\n"); |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame] | 93 | PartitionManager.Update_System_Details(); |
Mohd Faraz | 815e240 | 2020-09-19 19:31:52 +0530 | [diff] [blame] | 94 | } else if (DataManager::GetIntValue(TW_CRYPTO_PWTYPE) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 95 | LOGINFO("Is encrypted, do decrypt page first\n"); |
Mohd Faraz | e3948ec | 2020-08-14 01:40:59 +0000 | [diff] [blame] | 96 | if (DataManager::GetIntValue(TW_IS_FBE)) |
| 97 | DataManager::SetValue("tw_crypto_user_id", "0"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 98 | if (gui_startPage("decrypt", 1, 1) != 0) { |
| 99 | LOGERR("Failed to start decrypt GUI page.\n"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } else if (datamedia) { |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame] | 103 | PartitionManager.Update_System_Details(); |
niels | dbed985 | 2023-03-14 18:54:34 +0100 | [diff] [blame] | 104 | if (tw_get_default_metadata(DataManager::GetCurrentStoragePath().c_str()) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 105 | LOGINFO("Failed to get default contexts and file mode for storage files.\n"); |
| 106 | } else { |
| 107 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 112 | static void process_fastbootd_mode() { |
| 113 | LOGINFO("starting fastboot\n"); |
Ctapchuk | aefc2ab | 2021-10-29 14:40:52 +0700 | [diff] [blame] | 114 | |
| 115 | #ifdef TW_LOAD_VENDOR_MODULES |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 116 | if (android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) { |
| 117 | PartitionManager.Unmap_Super_Devices(); |
| 118 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 119 | #endif |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 120 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 121 | gui_msg(Msg("fastboot_console_msg=Entered Fastboot mode...")); |
MD Raza | 9124c93 | 2021-12-07 22:05:08 +0000 | [diff] [blame] | 122 | // Check for and run startup script if script exists |
| 123 | TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot"); |
| 124 | TWFunc::check_and_run_script("/system/bin/postfastboot.sh", "fastboot"); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 125 | if (gui_startPage("fastboot", 1, 1) != 0) { |
| 126 | LOGERR("Failed to start fastbootd page.\n"); |
| 127 | } |
| 128 | } |
| 129 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 130 | static void process_recovery_mode(twrpAdbBuFifo* adb_bu_fifo, bool skip_decryption) { |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 131 | char crash_prop_val[PROPERTY_VALUE_MAX]; |
| 132 | int crash_counter; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 133 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 134 | property_get("twrp.crash_counter", crash_prop_val, "-1"); |
| 135 | crash_counter = atoi(crash_prop_val) + 1; |
| 136 | snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter); |
| 137 | property_set("twrp.crash_counter", crash_prop_val); |
| 138 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 139 | if (crash_counter == 0) { |
| 140 | property_list(Print_Prop, NULL); |
| 141 | printf("\n"); |
| 142 | } else { |
| 143 | printf("twrp.crash_counter=%d\n", crash_counter); |
| 144 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 145 | |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 146 | // We are doing this here to allow super partition to be set up prior to overriding properties |
niels | 44e2346 | 2023-09-12 22:15:59 +0200 | [diff] [blame] | 147 | #if defined(TW_INCLUDE_LIBRESETPROP) |
| 148 | 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"}; |
| 149 | std::string val = "0"; |
| 150 | for (auto prop : build_date_props) { |
| 151 | TWFunc::Property_Override(prop, val); |
| 152 | LOGINFO("Overriding %s with value: \"%s\"\n", prop.c_str(), val.c_str()); |
| 153 | } |
| 154 | #if defined(TW_OVERRIDE_SYSTEM_PROPS) |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 155 | stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS)); |
| 156 | string current_prop; |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame] | 157 | |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 158 | std::vector<std::string> partition_list; |
| 159 | partition_list.push_back (PartitionManager.Get_Android_Root_Path().c_str()); |
| 160 | #ifdef TW_OVERRIDE_PROPS_ADDITIONAL_PARTITIONS |
| 161 | std::vector<std::string> additional_partition_list = TWFunc::Split_String(TW_OVERRIDE_PROPS_ADDITIONAL_PARTITIONS, " "); |
| 162 | partition_list.insert(partition_list.end(), additional_partition_list.begin(), additional_partition_list.end()); |
| 163 | #endif |
| 164 | std::vector<std::string> build_prop_list = {"build.prop"}; |
| 165 | #ifdef TW_SYSTEM_BUILD_PROP_ADDITIONAL_PATHS |
| 166 | std::vector<std::string> additional_build_prop_list = TWFunc::Split_String(TW_SYSTEM_BUILD_PROP_ADDITIONAL_PATHS, ";"); |
| 167 | build_prop_list.insert(build_prop_list.end(), additional_build_prop_list.begin(), additional_build_prop_list.end()); |
| 168 | #endif |
| 169 | while (getline(override_props, current_prop, ';')) { |
| 170 | string other_prop; |
| 171 | if (current_prop.find("=") != string::npos) { |
| 172 | other_prop = current_prop.substr(current_prop.find("=") + 1); |
| 173 | current_prop = current_prop.substr(0, current_prop.find("=")); |
| 174 | } else { |
| 175 | other_prop = current_prop; |
| 176 | } |
| 177 | other_prop = android::base::Trim(other_prop); |
| 178 | current_prop = android::base::Trim(current_prop); |
| 179 | |
| 180 | for (auto&& partition_mount_point:partition_list) { |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame] | 181 | for (auto&& prop_file:build_prop_list) { |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 182 | string sys_val = TWFunc::Partition_Property_Get(other_prop, PartitionManager, partition_mount_point.c_str(), prop_file); |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame] | 183 | if (!sys_val.empty()) { |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 184 | if (partition_mount_point == "/system_root") { |
| 185 | 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(), |
| 186 | prop_file.c_str()); |
| 187 | } else { |
| 188 | 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(), |
| 189 | partition_mount_point.c_str(), prop_file.c_str()); |
| 190 | } |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame] | 191 | int error = TWFunc::Property_Override(current_prop, sys_val); |
| 192 | if (error) { |
| 193 | LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error); |
| 194 | } |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 195 | if (partition_mount_point == partition_list.back()) { |
| 196 | PartitionManager.UnMount_By_Path(partition_mount_point, false); |
| 197 | } |
| 198 | goto exit; |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame] | 199 | } else { |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 200 | if (partition_mount_point == "/system_root") { |
| 201 | LOGINFO("Unable to override property %s: property not found in /system/%s\n", current_prop.c_str(), prop_file.c_str()); |
| 202 | } else { |
| 203 | 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()); |
| 204 | } |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 205 | } |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 206 | } |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 207 | PartitionManager.UnMount_By_Path(partition_mount_point, false); |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 208 | } |
Captain Throwback | 1483a8e | 2022-01-03 09:52:06 -0500 | [diff] [blame] | 209 | exit: |
| 210 | continue; |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 211 | } |
niels | 44e2346 | 2023-09-12 22:15:59 +0200 | [diff] [blame] | 212 | #endif // defined(TW_OVERRIDE_SYSTEM_PROPS) |
| 213 | #endif // defined(TW_INCLUDE_LIBRESETPROP) |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 214 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 215 | // Check for and run startup script if script exists |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 216 | TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot"); |
Captain Throwback | 3345ceb | 2021-12-10 15:31:48 -0500 | [diff] [blame] | 217 | TWFunc::check_and_run_script("/system/bin/postrecoveryboot.sh", "recovery"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 218 | |
| 219 | #ifdef TW_INCLUDE_INJECTTWRP |
| 220 | // Back up TWRP Ramdisk if needed: |
| 221 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 222 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 223 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 224 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 225 | else { |
| 226 | string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 227 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 228 | } |
| 229 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 230 | #endif |
bigbiff | df53b6c | 2022-01-17 19:31:52 -0500 | [diff] [blame] | 231 | #ifdef TW_INCLUDE_CRYPTO |
| 232 | android::keystore::copySqliteDb(); |
| 233 | #endif |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 234 | Decrypt_Page(skip_decryption, datamedia); |
bigbiff | df53b6c | 2022-01-17 19:31:52 -0500 | [diff] [blame] | 235 | |
bigbiff | 83298f5 | 2021-10-13 19:24:42 -0400 | [diff] [blame] | 236 | // Check for and load custom theme if present |
| 237 | TWFunc::check_selinux_support(); |
| 238 | gui_loadCustomResources(); |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame] | 239 | PartitionManager.Output_Partition_Logging(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 240 | |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 241 | // Fixup the RTC clock on devices which require it |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 242 | if (crash_counter == 0) |
| 243 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 244 | |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 245 | DataManager::LoadTWRPFolderInfo(); |
Ian Macdonald | 1e0474a | 2020-09-09 02:43:13 +0200 | [diff] [blame] | 246 | DataManager::ReadSettingsFile(); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 247 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 248 | // Run any outstanding OpenRecoveryScript |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 249 | std::string cacheDir = TWFunc::get_log_dir(); |
| 250 | if (cacheDir == DATA_LOGS_DIR) |
| 251 | cacheDir = "/data/cache"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 252 | std::string orsFile = cacheDir + "/recovery/openrecoveryscript"; |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 253 | if ((DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 || skip_decryption) && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(orsFile))) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 254 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 255 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 256 | |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 257 | #ifdef TW_HAS_MTP |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 258 | char mtp_crash_check[PROPERTY_VALUE_MAX]; |
| 259 | property_get("mtp.crash_check", mtp_crash_check, "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 260 | if (DataManager::GetIntValue("tw_mtp_enabled") |
| 261 | && !strcmp(mtp_crash_check, "0") && !crash_counter |
| 262 | && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) { |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 263 | property_set("mtp.crash_check", "1"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 264 | LOGINFO("Starting MTP\n"); |
| 265 | if (!PartitionManager.Enable_MTP()) |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 266 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 267 | else |
| 268 | gui_msg("mtp_enabled=MTP Enabled"); |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 269 | property_set("mtp.crash_check", "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 270 | } else if (strcmp(mtp_crash_check, "0")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 271 | gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot."); |
Ethan Yonker | 12055fa | 2014-09-04 08:06:53 -0500 | [diff] [blame] | 272 | DataManager::SetValue("tw_mtp_enabled", 0); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 273 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 274 | } else if (crash_counter == 1) { |
| 275 | LOGINFO("TWRP crashed; disabling MTP as a precaution.\n"); |
| 276 | PartitionManager.Disable_MTP(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 277 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 278 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 279 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 280 | #ifndef TW_OEM_BUILD |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 281 | // Check if system has never been changed |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 282 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 283 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 284 | if (sys) { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 285 | if (sys->Get_Super_Status()) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 286 | #ifdef TW_INCLUDE_CRYPTO |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 287 | std::string recoveryLogDir(DATA_LOGS_DIR); |
| 288 | recoveryLogDir += "/recovery"; |
| 289 | if (!TWFunc::Path_Exists(recoveryLogDir)) { |
| 290 | bool created = PartitionManager.Recreate_Logs_Dir(); |
| 291 | if (!created) |
| 292 | LOGERR("Unable to create log directory for TWRP\n"); |
| 293 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 294 | DataManager::ReadSettingsFile(); |
| 295 | #endif |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 296 | } else { |
| 297 | if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) { |
| 298 | if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) { |
| 299 | DataManager::SetValue("tw_back", "main"); |
| 300 | if (gui_startPage("system_readonly", 1, 1) != 0) { |
| 301 | LOGERR("Failed to start system_readonly GUI page.\n"); |
| 302 | } |
| 303 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) { |
| 304 | sys->Change_Mount_Read_Only(false); |
| 305 | if (ven) |
| 306 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 307 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 308 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) { |
| 309 | // Do nothing, user selected to leave system read only |
| 310 | } else { |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 311 | sys->Change_Mount_Read_Only(false); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 312 | if (ven) |
| 313 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 314 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 315 | } |
| 316 | } |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 317 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 318 | |
bigbiff | aac5861 | 2020-08-30 11:18:39 -0400 | [diff] [blame] | 319 | TWFunc::Update_Log_File(); |
| 320 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 321 | adb_bu_fifo->threadAdbBuFifo(); |
| 322 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 323 | #ifndef TW_OEM_BUILD |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 324 | // Disable flashing of stock recovery |
| 325 | TWFunc::Disable_Stock_Recovery_Replace(); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 326 | #endif |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 327 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 328 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 329 | static void reboot() { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 330 | gui_msg(Msg("rebooting=Rebooting...")); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 331 | TWFunc::Update_Log_File(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 332 | string Reboot_Arg; |
| 333 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 334 | if (Reboot_Arg == "recovery") |
| 335 | TWFunc::tw_reboot(rb_recovery); |
| 336 | else if (Reboot_Arg == "poweroff") |
| 337 | TWFunc::tw_reboot(rb_poweroff); |
| 338 | else if (Reboot_Arg == "bootloader") |
| 339 | TWFunc::tw_reboot(rb_bootloader); |
| 340 | else if (Reboot_Arg == "download") |
| 341 | TWFunc::tw_reboot(rb_download); |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 342 | else if (Reboot_Arg == "edl") |
| 343 | TWFunc::tw_reboot(rb_edl); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 344 | else if (Reboot_Arg == "fastboot") |
| 345 | TWFunc::tw_reboot(rb_fastboot); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 346 | else |
| 347 | TWFunc::tw_reboot(rb_system); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int main(int argc, char **argv) { |
| 351 | // Recovery needs to install world-readable files, so clear umask |
| 352 | // set by init |
| 353 | umask(0); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 354 | Log_Offset = 0; |
| 355 | |
| 356 | // Set up temporary log file (/tmp/recovery.log) |
| 357 | freopen(TMP_LOG_FILE, "a", stdout); |
| 358 | setbuf(stdout, NULL); |
| 359 | freopen(TMP_LOG_FILE, "a", stderr); |
| 360 | setbuf(stderr, NULL); |
| 361 | |
| 362 | signal(SIGPIPE, SIG_IGN); |
| 363 | |
| 364 | // Handle ADB sideload |
| 365 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 366 | property_set("ctl.stop", "adbd"); |
| 367 | #ifdef TW_USE_NEW_MINADBD |
| 368 | //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8 |
| 369 | // minadbd_main(); |
| 370 | #else |
| 371 | adb_main(argv[2]); |
| 372 | #endif |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 377 | datamedia = true; |
| 378 | #endif |
| 379 | |
| 380 | property_set("ro.twrp.boot", "1"); |
| 381 | property_set("ro.twrp.version", TW_VERSION_STR); |
| 382 | |
DarthJabba9 | 7cd3abb | 2021-11-28 01:09:44 +0530 | [diff] [blame] | 383 | #ifdef TARGET_OTA_ASSERT_DEVICE |
| 384 | property_set("ro.twrp.target.devices", TARGET_OTA_ASSERT_DEVICE); |
| 385 | #endif |
| 386 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 387 | time_t StartupTime = time(NULL); |
| 388 | printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, ctime(&StartupTime), getpid()); |
| 389 | |
| 390 | // Load default values to set DataManager constants and handle ifdefs |
| 391 | DataManager::SetDefaultValues(); |
Mohd Faraz | 88b4bab | 2023-01-01 02:43:01 +0530 | [diff] [blame] | 392 | startupArgs startup; |
| 393 | startup.parse(&argc, &argv); |
| 394 | printf("=> Linking mtab\n"); |
| 395 | symlink("/proc/mounts", "/etc/mtab"); |
| 396 | std::string fstab_filename = "/etc/twrp.fstab"; |
| 397 | if (!TWFunc::Path_Exists(fstab_filename)) { |
| 398 | fstab_filename = "/etc/recovery.fstab"; |
| 399 | } |
| 400 | printf("=> Processing %s\n", fstab_filename.c_str()); |
| 401 | if (!PartitionManager.Process_Fstab(fstab_filename, 1, !startup.Get_Fastboot_Mode())) { |
| 402 | LOGERR("Failing out of recovery due to problem with fstab.\n"); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | #ifdef TW_LOAD_VENDOR_MODULES |
micky387 | 56f748d | 2023-08-15 15:32:11 +0200 | [diff] [blame] | 407 | if (startup.Get_Fastboot_Mode()) { |
micky387 | 9aa7150 | 2023-08-15 15:36:00 +0200 | [diff] [blame] | 408 | TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm"); |
Captain Throwback | f209818 | 2023-08-16 21:55:51 -0400 | [diff] [blame] | 409 | android::base::SetProperty("ro.twrp.fastbootd", "1"); |
Mohd Faraz | 88b4bab | 2023-01-01 02:43:01 +0530 | [diff] [blame] | 410 | PartitionManager.Prepare_Super_Volume(PartitionManager.Find_Partition_By_Path("/vendor")); |
micky387 | 9aa7150 | 2023-08-15 15:36:00 +0200 | [diff] [blame] | 411 | if(ven_dlkm) { |
| 412 | PartitionManager.Prepare_Super_Volume(ven_dlkm); |
| 413 | } |
micky387 | 56f748d | 2023-08-15 15:32:11 +0200 | [diff] [blame] | 414 | } |
Mohd Faraz | 88b4bab | 2023-01-01 02:43:01 +0530 | [diff] [blame] | 415 | KernelModuleLoader::Load_Vendor_Modules(); |
| 416 | #endif |
| 417 | |
Mohd Faraz | e7b5add | 2023-08-28 16:46:38 +0530 | [diff] [blame] | 418 | printf("Starting the UI...\n"); |
| 419 | gui_init(); |
| 420 | |
Mohd Faraz | ec37045 | 2023-08-28 16:51:35 +0530 | [diff] [blame] | 421 | if (!startup.Get_Fastboot_Mode()) PartitionManager.Setup_Fstab_Partitions(true); |
| 422 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 423 | // Load up all the resources |
| 424 | gui_loadResources(); |
| 425 | |
Mohd Faraz | a37d930 | 2023-09-18 06:24:08 +0000 | [diff] [blame^] | 426 | std::string value; |
| 427 | static char charging = ' '; |
| 428 | static int lastVal = -1; |
| 429 | |
| 430 | // Function to monitor battery in the background |
| 431 | auto monitorBatteryInBackground = [&]() { |
| 432 | while (true) { |
| 433 | auto battery_info = GetBatteryInfo(); |
| 434 | if (battery_info.charging) { |
| 435 | charging = '+'; |
| 436 | } else { |
| 437 | charging = ' '; |
| 438 | } |
| 439 | lastVal = battery_info.capacity; |
| 440 | // Format the value based on the background updates |
| 441 | value = std::to_string(lastVal) + "%" + charging; |
| 442 | DataManager::SetValue("tw_battery", value); |
| 443 | |
| 444 | // Sleep for a specified interval (e.g., 1 second) before checking again |
| 445 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 446 | } |
| 447 | }; |
| 448 | |
| 449 | // Create a thread for battery monitoring |
| 450 | static std::thread battery_monitor(monitorBatteryInBackground); |
| 451 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 452 | twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo(); |
| 453 | TWFunc::Clear_Bootloader_Message(); |
| 454 | |
| 455 | if (startup.Get_Fastboot_Mode()) { |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 456 | process_fastbootd_mode(); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 457 | delete adb_bu_fifo; |
| 458 | TWFunc::Update_Intent_File(startup.Get_Intent()); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 459 | reboot(); |
| 460 | return 0; |
| 461 | } else { |
| 462 | process_recovery_mode(adb_bu_fifo, startup.Should_Skip_Decryption()); |
| 463 | } |
| 464 | |
Ian Macdonald | a991022 | 2020-10-27 18:02:21 +0100 | [diff] [blame] | 465 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 466 | GUIConsole::Translate_Now(); |
| 467 | |
epicX | 271bb3a | 2020-12-30 01:03:18 +0530 | [diff] [blame] | 468 | TWFunc::checkforapp(); //Checking compatibility for TWRP app |
| 469 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 470 | // Launch the main GUI |
| 471 | gui_start(); |
| 472 | delete adb_bu_fifo; |
| 473 | TWFunc::Update_Intent_File(startup.Get_Intent()); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 474 | reboot(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 475 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 476 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 477 | } |