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> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 27 | #include "gui/twmsg.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 28 | |
| 29 | #include "cutils/properties.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 30 | |
| 31 | #ifdef ANDROID_RB_RESTART |
| 32 | #include "cutils/android_reboot.h" |
| 33 | #else |
| 34 | #include <sys/reboot.h> |
| 35 | #endif |
| 36 | |
| 37 | extern "C" { |
| 38 | #include "gui/gui.h" |
| 39 | } |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 40 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 41 | #include "gui/gui.hpp" |
| 42 | #include "gui/pages.hpp" |
| 43 | #include "gui/objects.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 44 | #include "twcommon.h" |
| 45 | #include "twrp-functions.hpp" |
| 46 | #include "data.hpp" |
| 47 | #include "partitions.hpp" |
Mohd Faraz | 0d9a62e | 2020-02-20 00:10:26 +0530 | [diff] [blame] | 48 | #ifdef __ANDROID_API_N__ |
| 49 | #include <android-base/strings.h> |
| 50 | #else |
| 51 | #include <base/strings.h> |
| 52 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 53 | #include "openrecoveryscript.hpp" |
| 54 | #include "variables.h" |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 55 | #include "startupArgs.hpp" |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 56 | #include "twrpAdbBuFifo.hpp" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 57 | #ifdef TW_USE_NEW_MINADBD |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 58 | // #include "minadbd/minadbd.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 59 | #else |
| 60 | extern "C" { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 61 | #include "minadbd21/adb.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 62 | } |
| 63 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 64 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 65 | //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] | 66 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 67 | TWPartitionManager PartitionManager; |
| 68 | int Log_Offset; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 69 | bool datamedia; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 70 | |
| 71 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 72 | printf("%s=%s\n", key, name); |
| 73 | } |
| 74 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 75 | static void Decrypt_Page(bool SkipDecryption, bool datamedia) { |
| 76 | // Offer to decrypt if the device is encrypted |
| 77 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 78 | if (SkipDecryption) { |
| 79 | LOGINFO("Skipping decryption\n"); |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame^] | 80 | PartitionManager.Update_System_Details(); |
Mohd Faraz | 815e240 | 2020-09-19 19:31:52 +0530 | [diff] [blame] | 81 | } else if (DataManager::GetIntValue(TW_CRYPTO_PWTYPE) != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 82 | LOGINFO("Is encrypted, do decrypt page first\n"); |
Mohd Faraz | e3948ec | 2020-08-14 01:40:59 +0000 | [diff] [blame] | 83 | if (DataManager::GetIntValue(TW_IS_FBE)) |
| 84 | DataManager::SetValue("tw_crypto_user_id", "0"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 85 | if (gui_startPage("decrypt", 1, 1) != 0) { |
| 86 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 87 | } else { |
| 88 | // Check for and load custom theme if present |
| 89 | TWFunc::check_selinux_support(); |
| 90 | gui_loadCustomResources(); |
| 91 | } |
| 92 | } |
| 93 | } else if (datamedia) { |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame^] | 94 | PartitionManager.Update_System_Details(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 95 | TWFunc::check_selinux_support(); |
| 96 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
| 97 | LOGINFO("Failed to get default contexts and file mode for storage files.\n"); |
| 98 | } else { |
| 99 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 104 | static void process_recovery_mode(twrpAdbBuFifo* adb_bu_fifo, bool skip_decryption) { |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 105 | char crash_prop_val[PROPERTY_VALUE_MAX]; |
| 106 | int crash_counter; |
| 107 | property_get("twrp.crash_counter", crash_prop_val, "-1"); |
| 108 | crash_counter = atoi(crash_prop_val) + 1; |
| 109 | snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter); |
| 110 | property_set("twrp.crash_counter", crash_prop_val); |
| 111 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 112 | if (crash_counter == 0) { |
| 113 | property_list(Print_Prop, NULL); |
| 114 | printf("\n"); |
| 115 | } else { |
| 116 | printf("twrp.crash_counter=%d\n", crash_counter); |
| 117 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 118 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 119 | printf("=> Linking mtab\n"); |
| 120 | symlink("/proc/mounts", "/etc/mtab"); |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 121 | std::string fstab_filename = "/etc/twrp.fstab"; |
| 122 | if (!TWFunc::Path_Exists(fstab_filename)) { |
| 123 | fstab_filename = "/etc/recovery.fstab"; |
Dees_Troy | 329383e | 2013-08-29 14:16:06 +0000 | [diff] [blame] | 124 | } |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 125 | printf("=> Processing %s\n", fstab_filename.c_str()); |
bigbiff | 8da46fa | 2020-09-08 16:42:34 -0400 | [diff] [blame] | 126 | if (!PartitionManager.Process_Fstab(fstab_filename, 1)) { |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 127 | LOGERR("Failing out of recovery due to problem with fstab.\n"); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 128 | return; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 129 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 130 | |
Captain Throwback | dc0823c | 2020-09-06 13:07:23 -0400 | [diff] [blame] | 131 | // We are doing this here to allow super partition to be set up prior to overriding properties |
| 132 | #if defined(TW_INCLUDE_LIBRESETPROP) && defined(TW_OVERRIDE_SYSTEM_PROPS) |
| 133 | if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
| 134 | LOGERR("Unable to mount %s\n", PartitionManager.Get_Android_Root_Path().c_str()); |
| 135 | } else { |
| 136 | stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS)); |
| 137 | string current_prop; |
| 138 | while (getline(override_props, current_prop, ';')) { |
| 139 | string other_prop; |
| 140 | if (current_prop.find("=") != string::npos) { |
| 141 | other_prop = current_prop.substr(current_prop.find("=") + 1); |
| 142 | current_prop = current_prop.substr(0, current_prop.find("=")); |
| 143 | } else { |
| 144 | other_prop = current_prop; |
| 145 | } |
| 146 | other_prop = android::base::Trim(other_prop); |
| 147 | current_prop = android::base::Trim(current_prop); |
| 148 | string sys_val = TWFunc::System_Property_Get(other_prop, PartitionManager, PartitionManager.Get_Android_Root_Path().c_str()); |
| 149 | if (!sys_val.empty()) { |
| 150 | LOGINFO("Overriding %s with value: \"%s\" from system property %s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str()); |
| 151 | int error = TWFunc::Property_Override(current_prop, sys_val); |
| 152 | if (error) { |
| 153 | LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error); |
| 154 | } |
| 155 | } else { |
| 156 | LOGINFO("Not overriding %s with empty value from system property %s\n", current_prop.c_str(), other_prop.c_str()); |
| 157 | } |
| 158 | } |
| 159 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
| 160 | } |
| 161 | #endif |
| 162 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 163 | // Check for and run startup script if script exists |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 164 | TWFunc::check_and_run_script("/system/bin/runatboot.sh", "boot"); |
| 165 | TWFunc::check_and_run_script("/system/bin/postrecoveryboot.sh", "boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 166 | |
| 167 | #ifdef TW_INCLUDE_INJECTTWRP |
| 168 | // Back up TWRP Ramdisk if needed: |
| 169 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 170 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 171 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 172 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 173 | else { |
| 174 | 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] | 175 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 176 | } |
| 177 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 178 | #endif |
| 179 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 180 | Decrypt_Page(skip_decryption, datamedia); |
Captain Throwback | 89b56a1 | 2021-02-10 18:25:07 -0500 | [diff] [blame^] | 181 | PartitionManager.Output_Partition_Logging(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 182 | |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 183 | // Fixup the RTC clock on devices which require it |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 184 | if (crash_counter == 0) |
| 185 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 186 | |
Ian Macdonald | 1e0474a | 2020-09-09 02:43:13 +0200 | [diff] [blame] | 187 | DataManager::ReadSettingsFile(); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 188 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 189 | // Run any outstanding OpenRecoveryScript |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 190 | std::string cacheDir = TWFunc::get_log_dir(); |
| 191 | if (cacheDir == DATA_LOGS_DIR) |
| 192 | cacheDir = "/data/cache"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 193 | std::string orsFile = cacheDir + "/recovery/openrecoveryscript"; |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 194 | 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] | 195 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 196 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 197 | |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 198 | #ifdef TW_HAS_MTP |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 199 | char mtp_crash_check[PROPERTY_VALUE_MAX]; |
| 200 | property_get("mtp.crash_check", mtp_crash_check, "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 201 | if (DataManager::GetIntValue("tw_mtp_enabled") |
| 202 | && !strcmp(mtp_crash_check, "0") && !crash_counter |
| 203 | && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) { |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 204 | property_set("mtp.crash_check", "1"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 205 | LOGINFO("Starting MTP\n"); |
| 206 | if (!PartitionManager.Enable_MTP()) |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 207 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 208 | else |
| 209 | gui_msg("mtp_enabled=MTP Enabled"); |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 210 | property_set("mtp.crash_check", "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 211 | } else if (strcmp(mtp_crash_check, "0")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 212 | gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot."); |
Ethan Yonker | 12055fa | 2014-09-04 08:06:53 -0500 | [diff] [blame] | 213 | DataManager::SetValue("tw_mtp_enabled", 0); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 214 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 215 | } else if (crash_counter == 1) { |
| 216 | LOGINFO("TWRP crashed; disabling MTP as a precaution.\n"); |
| 217 | PartitionManager.Disable_MTP(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 218 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 219 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 220 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 221 | #ifndef TW_OEM_BUILD |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 222 | // Check if system has never been changed |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 223 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 224 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 225 | if (sys) { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 226 | if (sys->Get_Super_Status()) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 227 | #ifdef TW_INCLUDE_CRYPTO |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 228 | std::string recoveryLogDir(DATA_LOGS_DIR); |
| 229 | recoveryLogDir += "/recovery"; |
| 230 | if (!TWFunc::Path_Exists(recoveryLogDir)) { |
| 231 | bool created = PartitionManager.Recreate_Logs_Dir(); |
| 232 | if (!created) |
| 233 | LOGERR("Unable to create log directory for TWRP\n"); |
| 234 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 235 | DataManager::ReadSettingsFile(); |
| 236 | #endif |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 237 | } else { |
| 238 | if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) { |
| 239 | if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) { |
| 240 | DataManager::SetValue("tw_back", "main"); |
| 241 | if (gui_startPage("system_readonly", 1, 1) != 0) { |
| 242 | LOGERR("Failed to start system_readonly GUI page.\n"); |
| 243 | } |
| 244 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) { |
| 245 | sys->Change_Mount_Read_Only(false); |
| 246 | if (ven) |
| 247 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 248 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 249 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) { |
| 250 | // Do nothing, user selected to leave system read only |
| 251 | } else { |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 252 | sys->Change_Mount_Read_Only(false); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 253 | if (ven) |
| 254 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 255 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 258 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 259 | |
bigbiff | aac5861 | 2020-08-30 11:18:39 -0400 | [diff] [blame] | 260 | TWFunc::Update_Log_File(); |
| 261 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 262 | adb_bu_fifo->threadAdbBuFifo(); |
| 263 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 264 | #ifndef TW_OEM_BUILD |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 265 | // Disable flashing of stock recovery |
| 266 | TWFunc::Disable_Stock_Recovery_Replace(); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 267 | #endif |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 268 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 269 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 270 | static void reboot() { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 271 | gui_msg(Msg("rebooting=Rebooting...")); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 272 | TWFunc::Update_Log_File(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 273 | string Reboot_Arg; |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 274 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 275 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 276 | if (Reboot_Arg == "recovery") |
| 277 | TWFunc::tw_reboot(rb_recovery); |
| 278 | else if (Reboot_Arg == "poweroff") |
| 279 | TWFunc::tw_reboot(rb_poweroff); |
| 280 | else if (Reboot_Arg == "bootloader") |
| 281 | TWFunc::tw_reboot(rb_bootloader); |
| 282 | else if (Reboot_Arg == "download") |
| 283 | TWFunc::tw_reboot(rb_download); |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 284 | else if (Reboot_Arg == "edl") |
| 285 | TWFunc::tw_reboot(rb_edl); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 286 | else if (Reboot_Arg == "fastboot") |
| 287 | TWFunc::tw_reboot(rb_fastboot); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 288 | else |
| 289 | TWFunc::tw_reboot(rb_system); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | int main(int argc, char **argv) { |
| 293 | // Recovery needs to install world-readable files, so clear umask |
| 294 | // set by init |
| 295 | umask(0); |
| 296 | |
| 297 | Log_Offset = 0; |
| 298 | |
| 299 | // Set up temporary log file (/tmp/recovery.log) |
| 300 | freopen(TMP_LOG_FILE, "a", stdout); |
| 301 | setbuf(stdout, NULL); |
| 302 | freopen(TMP_LOG_FILE, "a", stderr); |
| 303 | setbuf(stderr, NULL); |
| 304 | |
| 305 | signal(SIGPIPE, SIG_IGN); |
| 306 | |
| 307 | // Handle ADB sideload |
| 308 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 309 | property_set("ctl.stop", "adbd"); |
| 310 | #ifdef TW_USE_NEW_MINADBD |
| 311 | //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8 |
| 312 | // minadbd_main(); |
| 313 | #else |
| 314 | adb_main(argv[2]); |
| 315 | #endif |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 320 | datamedia = true; |
| 321 | #endif |
| 322 | |
| 323 | property_set("ro.twrp.boot", "1"); |
| 324 | property_set("ro.twrp.version", TW_VERSION_STR); |
| 325 | |
| 326 | time_t StartupTime = time(NULL); |
| 327 | printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, ctime(&StartupTime), getpid()); |
| 328 | |
| 329 | // Load default values to set DataManager constants and handle ifdefs |
| 330 | DataManager::SetDefaultValues(); |
| 331 | printf("Starting the UI...\n"); |
| 332 | gui_init(); |
| 333 | |
| 334 | // Load up all the resources |
| 335 | gui_loadResources(); |
| 336 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 337 | startupArgs startup; |
| 338 | startup.parse(&argc, &argv); |
| 339 | twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo(); |
| 340 | TWFunc::Clear_Bootloader_Message(); |
| 341 | |
| 342 | if (startup.Get_Fastboot_Mode()) { |
| 343 | LOGINFO("starting fastboot\n"); |
| 344 | gui_msg(Msg("fastboot_console_msg=Entered Fastboot mode...")); |
| 345 | if (gui_startPage("fastboot", 1, 1) != 0) { |
| 346 | LOGERR("Failed to start fastbootd page.\n"); |
| 347 | } |
| 348 | delete adb_bu_fifo; |
| 349 | TWFunc::Update_Intent_File(startup.Get_Intent()); |
| 350 | |
| 351 | reboot(); |
| 352 | return 0; |
| 353 | } else { |
| 354 | process_recovery_mode(adb_bu_fifo, startup.Should_Skip_Decryption()); |
| 355 | } |
| 356 | |
Ian Macdonald | a991022 | 2020-10-27 18:02:21 +0100 | [diff] [blame] | 357 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 358 | GUIConsole::Translate_Now(); |
| 359 | |
epicX | 271bb3a | 2020-12-30 01:03:18 +0530 | [diff] [blame] | 360 | TWFunc::checkforapp(); //Checking compatibility for TWRP app |
| 361 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 362 | // Launch the main GUI |
| 363 | gui_start(); |
| 364 | delete adb_bu_fifo; |
| 365 | TWFunc::Update_Intent_File(startup.Get_Intent()); |
| 366 | |
| 367 | reboot(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 368 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 369 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 370 | } |