Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2 | 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 6 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 7 | 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 11 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 12 | 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 18 | #include <string> |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <time.h> |
| 22 | #include <unistd.h> |
Ethan Yonker | f9f99bc | 2014-12-29 09:10:34 -0600 | [diff] [blame] | 23 | #include <signal.h> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 24 | #include "gui/twmsg.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 25 | |
| 26 | #include "cutils/properties.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 27 | #include "bootloader_message_twrp/include/bootloader_message_twrp/bootloader_message.h" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 28 | |
| 29 | #ifdef ANDROID_RB_RESTART |
| 30 | #include "cutils/android_reboot.h" |
| 31 | #else |
| 32 | #include <sys/reboot.h> |
| 33 | #endif |
| 34 | |
| 35 | extern "C" { |
| 36 | #include "gui/gui.h" |
| 37 | } |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 38 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 39 | #include "gui/gui.hpp" |
| 40 | #include "gui/pages.hpp" |
| 41 | #include "gui/objects.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 42 | #include "twcommon.h" |
| 43 | #include "twrp-functions.hpp" |
| 44 | #include "data.hpp" |
| 45 | #include "partitions.hpp" |
Mohd Faraz | 0d9a62e | 2020-02-20 00:10:26 +0530 | [diff] [blame] | 46 | #ifdef __ANDROID_API_N__ |
| 47 | #include <android-base/strings.h> |
| 48 | #else |
| 49 | #include <base/strings.h> |
| 50 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 51 | #include "openrecoveryscript.hpp" |
| 52 | #include "variables.h" |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 53 | #include "twrpAdbBuFifo.hpp" |
bigbiff | 32cbabe | 2020-04-12 19:16:19 -0400 | [diff] [blame] | 54 | #include "twrpApex.hpp" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 55 | #ifdef TW_USE_NEW_MINADBD |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 56 | // #include "minadbd/minadbd.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 57 | #else |
| 58 | extern "C" { |
Ethan Yonker | 84d61ce | 2017-05-10 16:11:35 -0500 | [diff] [blame] | 59 | #include "minadbd21/adb.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 60 | } |
| 61 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 62 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 63 | //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] | 64 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 65 | TWPartitionManager PartitionManager; |
| 66 | int Log_Offset; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 67 | bool datamedia; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 68 | |
| 69 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 70 | printf("%s=%s\n", key, name); |
| 71 | } |
| 72 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 73 | static void Decrypt_Page(bool SkipDecryption, bool datamedia) { |
| 74 | // Offer to decrypt if the device is encrypted |
| 75 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 76 | if (SkipDecryption) { |
| 77 | LOGINFO("Skipping decryption\n"); |
| 78 | } else { |
| 79 | LOGINFO("Is encrypted, do decrypt page first\n"); |
| 80 | if (gui_startPage("decrypt", 1, 1) != 0) { |
| 81 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 82 | } else { |
| 83 | // Check for and load custom theme if present |
| 84 | TWFunc::check_selinux_support(); |
| 85 | gui_loadCustomResources(); |
| 86 | } |
| 87 | } |
| 88 | } else if (datamedia) { |
| 89 | TWFunc::check_selinux_support(); |
| 90 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
| 91 | LOGINFO("Failed to get default contexts and file mode for storage files.\n"); |
| 92 | } else { |
| 93 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 98 | int main(int argc, char **argv) { |
| 99 | // Recovery needs to install world-readable files, so clear umask |
| 100 | // set by init |
| 101 | umask(0); |
| 102 | |
| 103 | Log_Offset = 0; |
| 104 | |
| 105 | // Set up temporary log file (/tmp/recovery.log) |
| 106 | freopen(TMP_LOG_FILE, "a", stdout); |
| 107 | setbuf(stdout, NULL); |
| 108 | freopen(TMP_LOG_FILE, "a", stderr); |
| 109 | setbuf(stderr, NULL); |
| 110 | |
Ethan Yonker | f9f99bc | 2014-12-29 09:10:34 -0600 | [diff] [blame] | 111 | signal(SIGPIPE, SIG_IGN); |
| 112 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 113 | // Handle ADB sideload |
| 114 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 115 | property_set("ctl.stop", "adbd"); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 116 | #ifdef TW_USE_NEW_MINADBD |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 117 | //adb_server_main(0, DEFAULT_ADB_PORT, -1); TODO fix this for android8 |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 118 | // minadbd_main(); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 119 | #else |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 120 | adb_main(argv[2]); |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 121 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 125 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 126 | datamedia = true; |
| 127 | #endif |
| 128 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 129 | char crash_prop_val[PROPERTY_VALUE_MAX]; |
| 130 | int crash_counter; |
| 131 | property_get("twrp.crash_counter", crash_prop_val, "-1"); |
| 132 | crash_counter = atoi(crash_prop_val) + 1; |
| 133 | snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter); |
| 134 | property_set("twrp.crash_counter", crash_prop_val); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 135 | property_set("ro.twrp.boot", "1"); |
| 136 | property_set("ro.twrp.version", TW_VERSION_STR); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 137 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 138 | time_t StartupTime = time(NULL); |
that | 0e2140e | 2016-08-10 21:04:26 +0200 | [diff] [blame] | 139 | printf("Starting TWRP %s-%s on %s (pid %d)\n", TW_VERSION_STR, TW_GIT_REVISION, ctime(&StartupTime), getpid()); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 140 | |
| 141 | // Load default values to set DataManager constants and handle ifdefs |
| 142 | DataManager::SetDefaultValues(); |
that | 0e2140e | 2016-08-10 21:04:26 +0200 | [diff] [blame] | 143 | printf("Starting the UI...\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 144 | gui_init(); |
| 145 | printf("=> Linking mtab\n"); |
| 146 | symlink("/proc/mounts", "/etc/mtab"); |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 147 | std::string fstab_filename = "/etc/twrp.fstab"; |
| 148 | if (!TWFunc::Path_Exists(fstab_filename)) { |
| 149 | fstab_filename = "/etc/recovery.fstab"; |
Dees_Troy | 329383e | 2013-08-29 14:16:06 +0000 | [diff] [blame] | 150 | } |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 151 | |
| 152 | // Begin SAR detection |
| 153 | { |
| 154 | TWPartitionManager SarPartitionManager; |
| 155 | printf("=> Processing %s for SAR-detection\n", fstab_filename.c_str()); |
| 156 | if (!SarPartitionManager.Process_Fstab(fstab_filename, 1, 1)) { |
| 157 | LOGERR("Failing out of recovery due to problem with fstab.\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | mkdir("/s", 0755); |
| 162 | |
| 163 | #if defined(AB_OTA_UPDATER) || defined(__ANDROID_API_Q__) |
| 164 | bool fallback_sar = true; |
| 165 | #else |
| 166 | bool fallback_sar = property_get_bool("ro.build.system_root_image", false); |
| 167 | #endif |
| 168 | |
| 169 | if(SarPartitionManager.Mount_By_Path("/s", false)) { |
| 170 | if (TWFunc::Path_Exists("/s/build.prop")) { |
| 171 | LOGINFO("SAR-DETECT: Non-SAR System detected\n"); |
| 172 | property_set("ro.twrp.sar", "0"); |
| 173 | rmdir("/system_root"); |
| 174 | } else if (TWFunc::Path_Exists("/s/system/build.prop")) { |
| 175 | LOGINFO("SAR-DETECT: SAR System detected\n"); |
| 176 | property_set("ro.twrp.sar", "1"); |
| 177 | } else { |
| 178 | LOGINFO("SAR-DETECT: No build.prop found, falling back to %s\n", fallback_sar ? "SAR" : "Non-SAR"); |
| 179 | property_set("ro.twrp.sar", fallback_sar ? "1" : "0"); |
| 180 | } |
Chaosmaster | 01a1799 | 2020-02-08 00:10:05 +0100 | [diff] [blame] | 181 | |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 182 | SarPartitionManager.UnMount_By_Path("/s", false); |
| 183 | } else { |
| 184 | LOGINFO("SAR-DETECT: Could not mount system partition, falling back to %s\n", fallback_sar ? "SAR":"Non-SAR"); |
| 185 | property_set("ro.twrp.sar", fallback_sar ? "1" : "0"); |
| 186 | } |
| 187 | |
| 188 | rmdir("/s"); |
| 189 | |
| 190 | TWFunc::check_and_run_script("/sbin/sarsetup.sh", "boot"); |
| 191 | } |
| 192 | // End SAR detection |
| 193 | |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 194 | printf("=> Processing %s\n", fstab_filename.c_str()); |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 195 | if (!PartitionManager.Process_Fstab(fstab_filename, 1, 0)) { |
Ethan Yonker | 53273ec | 2018-03-09 11:22:54 -0600 | [diff] [blame] | 196 | LOGERR("Failing out of recovery due to problem with fstab.\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | PartitionManager.Output_Partition_Logging(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 200 | #ifdef TW_INCLUDE_CRYPTO |
| 201 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 202 | #endif |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 203 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 204 | if (PartitionManager.Get_Super_Status()) { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 205 | PartitionManager.Setup_Super_Devices(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 206 | PartitionManager.Setup_Super_Partition(); |
| 207 | } else { |
| 208 | #ifdef TW_INCLUDE_CRYPTO |
| 209 | if (!PartitionManager.Get_Super_Status()) |
| 210 | PartitionManager.Decrypt_Data(); |
| 211 | #endif |
| 212 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 213 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 214 | // Load up all the resources |
| 215 | gui_loadResources(); |
| 216 | |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 217 | bool Shutdown = false; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 218 | bool SkipDecryption = false; |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 219 | string Send_Intent = ""; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 220 | { |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 221 | TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc"); |
| 222 | if (misc != NULL) { |
| 223 | if (misc->Current_File_System == "emmc") { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 224 | set_misc_device(misc->Actual_Block_Device.c_str()); |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 225 | } else { |
Matt Mower | 3626bdc | 2017-01-06 13:45:54 -0600 | [diff] [blame] | 226 | LOGERR("Only emmc /misc is supported\n"); |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 229 | get_args(&argc, &argv); |
| 230 | |
| 231 | int index, index2, len; |
| 232 | char* argptr; |
| 233 | char* ptr; |
| 234 | printf("Startup Commands: "); |
| 235 | for (index = 1; index < argc; index++) { |
| 236 | argptr = argv[index]; |
| 237 | printf(" '%s'", argv[index]); |
| 238 | len = strlen(argv[index]); |
| 239 | if (*argptr == '-') {argptr++; len--;} |
| 240 | if (*argptr == '-') {argptr++; len--;} |
| 241 | if (*argptr == 'u') { |
| 242 | ptr = argptr; |
| 243 | index2 = 0; |
| 244 | while (*ptr != '=' && *ptr != '\n') |
| 245 | ptr++; |
Kevin Steck | 7b69e9d | 2013-10-17 18:24:31 -0400 | [diff] [blame] | 246 | // skip the = before grabbing Zip_File |
| 247 | while (*ptr == '=') |
| 248 | ptr++; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 249 | if (*ptr) { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 250 | string ORSCommand = "install "; |
| 251 | ORSCommand.append(ptr); |
| 252 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 253 | // If we have a map of blocks we don't need to mount data. |
| 254 | SkipDecryption = *ptr == '@'; |
| 255 | |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 256 | if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
| 257 | break; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 258 | } else |
| 259 | LOGERR("argument error specifying zip file\n"); |
| 260 | } else if (*argptr == 'w') { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 261 | if (len == 9) { |
| 262 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 263 | break; |
| 264 | } else if (len == 10) { |
| 265 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 266 | break; |
| 267 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 268 | // 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_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 269 | } else if (*argptr == 'n') { |
Ethan Yonker | 3aa66be | 2015-12-22 12:30:18 -0600 | [diff] [blame] | 270 | DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}")); |
| 271 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
| 272 | break; |
Ethan Yonker | a167416 | 2014-11-06 08:35:10 -0600 | [diff] [blame] | 273 | } else if (*argptr == 'p') { |
| 274 | Shutdown = true; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 275 | } else if (*argptr == 's') { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 276 | if (strncmp(argptr, "send_intent", strlen("send_intent")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 277 | ptr = argptr + strlen("send_intent") + 1; |
| 278 | Send_Intent = *ptr; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 279 | } else if (strncmp(argptr, "security", strlen("security")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 280 | LOGINFO("Security update\n"); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 281 | } else if (strncmp(argptr, "sideload", strlen("sideload")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 282 | if (!OpenRecoveryScript::Insert_ORS_Command("sideload\n")) |
| 283 | break; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 284 | } else if (strncmp(argptr, "stages", strlen("stages")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 285 | LOGINFO("ignoring stages command\n"); |
| 286 | } |
| 287 | } else if (*argptr == 'r') { |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 288 | if (strncmp(argptr, "reason", strlen("reason")) == 0) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 289 | ptr = argptr + strlen("reason") + 1; |
| 290 | gui_print("%s\n", ptr); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 294 | printf("\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 297 | if (crash_counter == 0) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 298 | property_list(Print_Prop, NULL); |
| 299 | printf("\n"); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 300 | } else { |
| 301 | printf("twrp.crash_counter=%d\n", crash_counter); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Check for and run startup script if script exists |
| 305 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 306 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
| 307 | |
| 308 | #ifdef TW_INCLUDE_INJECTTWRP |
| 309 | // Back up TWRP Ramdisk if needed: |
| 310 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 311 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 312 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 313 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 314 | else { |
| 315 | 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] | 316 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 317 | } |
| 318 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 319 | #endif |
| 320 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 321 | if (!PartitionManager.Get_Super_Status()) |
| 322 | Decrypt_Page(SkipDecryption, datamedia); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 323 | |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 324 | // Fixup the RTC clock on devices which require it |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 325 | if (crash_counter == 0) |
| 326 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 327 | |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 328 | // Read the settings file |
| 329 | TWFunc::Update_Log_File(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 330 | |
| 331 | if (!PartitionManager.Get_Super_Status()) |
| 332 | DataManager::ReadSettingsFile(); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 333 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 334 | GUIConsole::Translate_Now(); |
| 335 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 336 | // Run any outstanding OpenRecoveryScript |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 337 | std::string cacheDir = TWFunc::get_log_dir(); |
| 338 | if (cacheDir == DATA_LOGS_DIR) |
| 339 | cacheDir = "/data/cache"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 340 | std::string orsFile = cacheDir + "/recovery/openrecoveryscript"; |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 341 | if ((DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 || SkipDecryption) && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(orsFile))) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 342 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 343 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 344 | |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 345 | #ifdef TW_HAS_MTP |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 346 | char mtp_crash_check[PROPERTY_VALUE_MAX]; |
| 347 | property_get("mtp.crash_check", mtp_crash_check, "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 348 | if (DataManager::GetIntValue("tw_mtp_enabled") |
| 349 | && !strcmp(mtp_crash_check, "0") && !crash_counter |
| 350 | && (!DataManager::GetIntValue(TW_IS_ENCRYPTED) || DataManager::GetIntValue(TW_IS_DECRYPTED))) { |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 351 | property_set("mtp.crash_check", "1"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 352 | LOGINFO("Starting MTP\n"); |
| 353 | if (!PartitionManager.Enable_MTP()) |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 354 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 355 | else |
| 356 | gui_msg("mtp_enabled=MTP Enabled"); |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 357 | property_set("mtp.crash_check", "0"); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 358 | } else if (strcmp(mtp_crash_check, "0")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 359 | gui_warn("mtp_crash=MTP Crashed, not starting MTP on boot."); |
Ethan Yonker | 12055fa | 2014-09-04 08:06:53 -0500 | [diff] [blame] | 360 | DataManager::SetValue("tw_mtp_enabled", 0); |
Ethan Yonker | df7abac | 2014-12-29 10:48:17 -0600 | [diff] [blame] | 361 | PartitionManager.Disable_MTP(); |
Matt Mower | 653a170 | 2017-02-16 10:38:52 -0600 | [diff] [blame] | 362 | } else if (crash_counter == 1) { |
| 363 | LOGINFO("TWRP crashed; disabling MTP as a precaution.\n"); |
| 364 | PartitionManager.Disable_MTP(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 365 | } |
Ethan Yonker | 6f49e11 | 2014-09-03 21:42:49 -0500 | [diff] [blame] | 366 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 367 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 368 | #ifndef TW_OEM_BUILD |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 369 | // Check if system has never been changed |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 370 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 371 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 372 | if (sys) { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 373 | if (sys->Get_Super_Status()) { |
| 374 | if (!PartitionManager.Prepare_All_Super_Volumes()) { |
| 375 | LOGERR("Unable to prepare super volumes.\n"); |
| 376 | } |
bigbiff | 32cbabe | 2020-04-12 19:16:19 -0400 | [diff] [blame] | 377 | sys->Mount(true); |
bigbiff | 86fabbe | 2020-06-22 17:07:27 -0400 | [diff] [blame] | 378 | if (ven) { |
| 379 | ven->Mount(true); |
| 380 | } |
bigbiff | 32cbabe | 2020-04-12 19:16:19 -0400 | [diff] [blame] | 381 | twrpApex apex; |
| 382 | if (!apex.loadApexImages()) { |
| 383 | LOGERR("Unable to load apex images from %s\n", APEX_DIR); |
| 384 | } |
| 385 | property_set("twrp.apex.loaded", "true"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 386 | #ifdef TW_INCLUDE_CRYPTO |
| 387 | PartitionManager.Decrypt_Data(); |
| 388 | Decrypt_Page(SkipDecryption, datamedia); |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 389 | std::string recoveryLogDir(DATA_LOGS_DIR); |
| 390 | recoveryLogDir += "/recovery"; |
| 391 | if (!TWFunc::Path_Exists(recoveryLogDir)) { |
| 392 | bool created = PartitionManager.Recreate_Logs_Dir(); |
| 393 | if (!created) |
| 394 | LOGERR("Unable to create log directory for TWRP\n"); |
| 395 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 396 | DataManager::ReadSettingsFile(); |
| 397 | #endif |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 398 | } else { |
| 399 | if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) { |
| 400 | if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) { |
| 401 | DataManager::SetValue("tw_back", "main"); |
| 402 | if (gui_startPage("system_readonly", 1, 1) != 0) { |
| 403 | LOGERR("Failed to start system_readonly GUI page.\n"); |
| 404 | } |
| 405 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 0) { |
| 406 | sys->Change_Mount_Read_Only(false); |
| 407 | if (ven) |
| 408 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 409 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 410 | } else if (DataManager::GetIntValue("tw_mount_system_ro") == 1) { |
| 411 | // Do nothing, user selected to leave system read only |
| 412 | } else { |
Ethan Yonker | 0e2c657 | 2015-06-04 11:30:02 -0500 | [diff] [blame] | 413 | sys->Change_Mount_Read_Only(false); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 414 | if (ven) |
| 415 | ven->Change_Mount_Read_Only(false); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 416 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 417 | } |
| 418 | } |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 419 | #endif |
Captain Throwback | b7cefd1 | 2020-09-05 15:59:01 -0400 | [diff] [blame^] | 420 | // We are doing this here to allow super partition to be set up prior to overriding properties |
| 421 | #if defined(TW_INCLUDE_LIBRESETPROP) && defined(TW_OVERRIDE_SYSTEM_PROPS) |
| 422 | if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
| 423 | LOGERR("Unable to mount %s\n", PartitionManager.Get_Android_Root_Path().c_str()); |
| 424 | } else { |
| 425 | stringstream override_props(EXPAND(TW_OVERRIDE_SYSTEM_PROPS)); |
| 426 | string current_prop; |
| 427 | while (getline(override_props, current_prop, ';')) { |
| 428 | string other_prop; |
| 429 | if (current_prop.find("=") != string::npos) { |
| 430 | other_prop = current_prop.substr(current_prop.find("=") + 1); |
| 431 | current_prop = current_prop.substr(0, current_prop.find("=")); |
| 432 | } else { |
| 433 | other_prop = current_prop; |
| 434 | } |
| 435 | other_prop = android::base::Trim(other_prop); |
| 436 | current_prop = android::base::Trim(current_prop); |
| 437 | string sys_val = TWFunc::System_Property_Get(other_prop, PartitionManager, PartitionManager.Get_Android_Root_Path().c_str()); |
| 438 | if (!sys_val.empty()) { |
| 439 | LOGINFO("Overriding %s with value: \"%s\" from system property %s\n", current_prop.c_str(), sys_val.c_str(), other_prop.c_str()); |
| 440 | int error = TWFunc::Property_Override(current_prop, sys_val); |
| 441 | if (error) { |
| 442 | LOGERR("Failed overriding property %s, error_code: %d\n", current_prop.c_str(), error); |
| 443 | } |
| 444 | } else { |
| 445 | LOGINFO("Not overriding %s with empty value from system property %s\n", current_prop.c_str(), other_prop.c_str()); |
| 446 | } |
| 447 | } |
| 448 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
| 449 | } |
| 450 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 451 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 452 | twrpAdbBuFifo *adb_bu_fifo = new twrpAdbBuFifo(); |
| 453 | adb_bu_fifo->threadAdbBuFifo(); |
| 454 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 455 | // Launch the main GUI |
| 456 | gui_start(); |
| 457 | |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 458 | #ifndef TW_OEM_BUILD |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 459 | // Disable flashing of stock recovery |
| 460 | TWFunc::Disable_Stock_Recovery_Replace(); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 461 | #endif |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 462 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 463 | // Reboot |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 464 | TWFunc::Update_Intent_File(Send_Intent); |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 465 | delete adb_bu_fifo; |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 466 | TWFunc::Update_Log_File(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 467 | gui_msg(Msg("rebooting=Rebooting...")); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 468 | string Reboot_Arg; |
| 469 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 470 | if (Reboot_Arg == "recovery") |
| 471 | TWFunc::tw_reboot(rb_recovery); |
| 472 | else if (Reboot_Arg == "poweroff") |
| 473 | TWFunc::tw_reboot(rb_poweroff); |
| 474 | else if (Reboot_Arg == "bootloader") |
| 475 | TWFunc::tw_reboot(rb_bootloader); |
| 476 | else if (Reboot_Arg == "download") |
| 477 | TWFunc::tw_reboot(rb_download); |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 478 | else if (Reboot_Arg == "edl") |
| 479 | TWFunc::tw_reboot(rb_edl); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 480 | else |
| 481 | TWFunc::tw_reboot(rb_system); |
| 482 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 483 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 484 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 485 | |