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> |
| 18 | #include <string.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <time.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "cutils/properties.h" |
| 25 | extern "C" { |
| 26 | #include "minadbd/adb.h" |
| 27 | #include "bootloader.h" |
| 28 | } |
| 29 | |
| 30 | #ifdef ANDROID_RB_RESTART |
| 31 | #include "cutils/android_reboot.h" |
| 32 | #else |
| 33 | #include <sys/reboot.h> |
| 34 | #endif |
| 35 | |
| 36 | extern "C" { |
| 37 | #include "gui/gui.h" |
| 38 | } |
| 39 | #include "twcommon.h" |
| 40 | #include "twrp-functions.hpp" |
| 41 | #include "data.hpp" |
| 42 | #include "partitions.hpp" |
| 43 | #include "openrecoveryscript.hpp" |
| 44 | #include "variables.h" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 45 | #include "twrpDU.hpp" |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 46 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SELINUX |
| 48 | #include "selinux/label.h" |
| 49 | struct selabel_handle *selinux_handle; |
| 50 | #endif |
| 51 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 52 | TWPartitionManager PartitionManager; |
| 53 | int Log_Offset; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 54 | twrpDU du; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 55 | |
| 56 | static void Print_Prop(const char *key, const char *name, void *cookie) { |
| 57 | printf("%s=%s\n", key, name); |
| 58 | } |
| 59 | |
| 60 | int main(int argc, char **argv) { |
| 61 | // Recovery needs to install world-readable files, so clear umask |
| 62 | // set by init |
| 63 | umask(0); |
| 64 | |
| 65 | Log_Offset = 0; |
| 66 | |
| 67 | // Set up temporary log file (/tmp/recovery.log) |
| 68 | freopen(TMP_LOG_FILE, "a", stdout); |
| 69 | setbuf(stdout, NULL); |
| 70 | freopen(TMP_LOG_FILE, "a", stderr); |
| 71 | setbuf(stderr, NULL); |
| 72 | |
| 73 | // Handle ADB sideload |
| 74 | if (argc == 3 && strcmp(argv[1], "--adbd") == 0) { |
| 75 | adb_main(argv[2]); |
| 76 | return 0; |
| 77 | } |
| 78 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 79 | char crash_prop_val[PROPERTY_VALUE_MAX]; |
| 80 | int crash_counter; |
| 81 | property_get("twrp.crash_counter", crash_prop_val, "-1"); |
| 82 | crash_counter = atoi(crash_prop_val) + 1; |
| 83 | snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter); |
| 84 | property_set("twrp.crash_counter", crash_prop_val); |
| 85 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 86 | time_t StartupTime = time(NULL); |
| 87 | printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&StartupTime)); |
| 88 | |
| 89 | // Load default values to set DataManager constants and handle ifdefs |
| 90 | DataManager::SetDefaultValues(); |
| 91 | printf("Starting the UI..."); |
| 92 | gui_init(); |
| 93 | printf("=> Linking mtab\n"); |
| 94 | symlink("/proc/mounts", "/etc/mtab"); |
Dees_Troy | 329383e | 2013-08-29 14:16:06 +0000 | [diff] [blame] | 95 | if (TWFunc::Path_Exists("/etc/twrp.fstab")) { |
| 96 | if (TWFunc::Path_Exists("/etc/recovery.fstab")) { |
| 97 | printf("Renaming regular /etc/recovery.fstab -> /etc/recovery.fstab.bak\n"); |
| 98 | rename("/etc/recovery.fstab", "/etc/recovery.fstab.bak"); |
| 99 | } |
| 100 | printf("Moving /etc/twrp.fstab -> /etc/recovery.fstab\n"); |
| 101 | rename("/etc/twrp.fstab", "/etc/recovery.fstab"); |
| 102 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 103 | printf("=> Processing recovery.fstab\n"); |
| 104 | if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) { |
| 105 | LOGERR("Failing out of recovery due to problem with recovery.fstab.\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | PartitionManager.Output_Partition_Logging(); |
| 109 | // Load up all the resources |
| 110 | gui_loadResources(); |
| 111 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 112 | #ifdef HAVE_SELINUX |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 113 | if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { |
| 114 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 115 | printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); |
| 116 | rename("/file_contexts", "/file_contexts.bak"); |
| 117 | } |
| 118 | printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); |
| 119 | rename("/prebuilt_file_contexts", "/file_contexts"); |
| 120 | } |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 121 | struct selinux_opt selinux_options[] = { |
| 122 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 123 | }; |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 124 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 125 | if (!selinux_handle) |
| 126 | printf("No file contexts for SELinux\n"); |
| 127 | else |
| 128 | printf("SELinux contexts loaded from /file_contexts\n"); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 129 | { // Check to ensure SELinux can be supported by the kernel |
| 130 | char *contexts = NULL; |
Dees Troy | 8d0eb13 | 2013-12-06 16:55:41 +0000 | [diff] [blame] | 131 | |
| 132 | if (PartitionManager.Mount_By_Path("/cache", true) && TWFunc::Path_Exists("/cache/recovery")) { |
| 133 | lgetfilecon("/cache/recovery", &contexts); |
| 134 | if (!contexts) { |
| 135 | lsetfilecon("/cache/recovery", "test"); |
| 136 | lgetfilecon("/cache/recovery", &contexts); |
| 137 | } |
| 138 | } else { |
| 139 | LOGINFO("Could not check /cache/recovery SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n"); |
| 140 | lgetfilecon("/sbin/teamwin", &contexts); |
| 141 | } |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 142 | if (!contexts) { |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 143 | gui_print_color("warning", "Kernel does not have support for reading SELinux contexts.\n"); |
Dees Troy | 995e88c | 2013-11-26 21:39:14 +0000 | [diff] [blame] | 144 | } else { |
| 145 | free(contexts); |
| 146 | gui_print("Full SELinux support is present.\n"); |
| 147 | } |
| 148 | } |
| 149 | #else |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 150 | gui_print_color("warning", "No SELinux support (no libselinux).\n"); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 153 | PartitionManager.Mount_By_Path("/cache", true); |
| 154 | |
| 155 | string Zip_File, Reboot_Value; |
| 156 | bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false; |
| 157 | |
| 158 | { |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 159 | TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc"); |
| 160 | if (misc != NULL) { |
| 161 | if (misc->Current_File_System == "emmc") { |
| 162 | set_device_type('e'); |
| 163 | set_device_name(misc->Actual_Block_Device.c_str()); |
| 164 | } else if (misc->Current_File_System == "mtd") { |
| 165 | set_device_type('m'); |
| 166 | set_device_name(misc->MTD_Name.c_str()); |
| 167 | } else { |
| 168 | LOGERR("Unknown file system for /misc\n"); |
| 169 | } |
| 170 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 171 | get_args(&argc, &argv); |
| 172 | |
| 173 | int index, index2, len; |
| 174 | char* argptr; |
| 175 | char* ptr; |
| 176 | printf("Startup Commands: "); |
| 177 | for (index = 1; index < argc; index++) { |
| 178 | argptr = argv[index]; |
| 179 | printf(" '%s'", argv[index]); |
| 180 | len = strlen(argv[index]); |
| 181 | if (*argptr == '-') {argptr++; len--;} |
| 182 | if (*argptr == '-') {argptr++; len--;} |
| 183 | if (*argptr == 'u') { |
| 184 | ptr = argptr; |
| 185 | index2 = 0; |
| 186 | while (*ptr != '=' && *ptr != '\n') |
| 187 | ptr++; |
Kevin Steck | 7b69e9d | 2013-10-17 18:24:31 -0400 | [diff] [blame] | 188 | // skip the = before grabbing Zip_File |
| 189 | while (*ptr == '=') |
| 190 | ptr++; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 191 | if (*ptr) { |
| 192 | Zip_File = ptr; |
| 193 | } else |
| 194 | LOGERR("argument error specifying zip file\n"); |
| 195 | } else if (*argptr == 'w') { |
| 196 | if (len == 9) |
| 197 | Factory_Reset = true; |
| 198 | else if (len == 10) |
| 199 | Cache_Wipe = true; |
| 200 | } else if (*argptr == 'n') { |
| 201 | Perform_Backup = true; |
| 202 | } else if (*argptr == 's') { |
| 203 | ptr = argptr; |
| 204 | index2 = 0; |
| 205 | while (*ptr != '=' && *ptr != '\n') |
| 206 | ptr++; |
| 207 | if (*ptr) { |
| 208 | Reboot_Value = *ptr; |
| 209 | } |
| 210 | } |
| 211 | } |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 212 | printf("\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 215 | if(crash_counter == 0) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 216 | property_list(Print_Prop, NULL); |
| 217 | printf("\n"); |
Vojtech Bocek | 0fc1573 | 2014-07-04 01:09:50 +0200 | [diff] [blame] | 218 | } else { |
| 219 | printf("twrp.crash_counter=%d\n", crash_counter); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Check for and run startup script if script exists |
| 223 | TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot"); |
| 224 | TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot"); |
| 225 | |
| 226 | #ifdef TW_INCLUDE_INJECTTWRP |
| 227 | // Back up TWRP Ramdisk if needed: |
| 228 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 229 | LOGINFO("Backing up TWRP ramdisk...\n"); |
| 230 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 231 | TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 232 | else { |
| 233 | 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] | 234 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 235 | } |
| 236 | LOGINFO("Backup of TWRP ramdisk done.\n"); |
| 237 | #endif |
| 238 | |
| 239 | bool Keep_Going = true; |
| 240 | if (Perform_Backup) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 241 | DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 242 | if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) |
| 243 | Keep_Going = false; |
| 244 | } |
| 245 | if (Keep_Going && !Zip_File.empty()) { |
| 246 | string ORSCommand = "install " + Zip_File; |
| 247 | |
| 248 | if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) |
| 249 | Keep_Going = false; |
| 250 | } |
| 251 | if (Keep_Going) { |
| 252 | if (Factory_Reset) { |
| 253 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) |
| 254 | Keep_Going = false; |
| 255 | } else if (Cache_Wipe) { |
| 256 | if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) |
| 257 | Keep_Going = false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | TWFunc::Update_Log_File(); |
| 262 | // Offer to decrypt if the device is encrypted |
| 263 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) { |
| 264 | LOGINFO("Is encrypted, do decrypt page first\n"); |
| 265 | if (gui_startPage("decrypt") != 0) { |
| 266 | LOGERR("Failed to start decrypt GUI page.\n"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Read the settings file |
| 271 | DataManager::ReadSettingsFile(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 272 | |
| 273 | // Fixup the RTC clock on devices which require it |
Vojtech Bocek | 67351dc | 2014-07-03 15:22:41 +0200 | [diff] [blame] | 274 | if(crash_counter == 0) |
| 275 | TWFunc::Fixup_Time_On_Boot(); |
Vojtech Bocek | 2005a84 | 2014-03-11 19:40:34 +0100 | [diff] [blame] | 276 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 277 | // Run any outstanding OpenRecoveryScript |
| 278 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) { |
| 279 | OpenRecoveryScript::Run_OpenRecoveryScript(); |
| 280 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 281 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 282 | // Launch the main GUI |
| 283 | gui_start(); |
| 284 | |
| 285 | // Check for su to see if the device is rooted or not |
| 286 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 287 | // Disable flashing of stock recovery |
dhacker29 | 30f100e | 2014-07-15 21:33:29 -0400 | [diff] [blame] | 288 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p") && TWFunc::Path_Exists("/system/etc/install-recovery.sh")) { |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 289 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
| 290 | gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); |
| 291 | } |
| 292 | if (TWFunc::Path_Exists("/supersu/su") && !TWFunc::Path_Exists("/system/bin/su") && !TWFunc::Path_Exists("/system/xbin/su") && !TWFunc::Path_Exists("/system/bin/.ext/.su")) { |
| 293 | // Device doesn't have su installed |
| 294 | DataManager::SetValue("tw_busy", 1); |
| 295 | if (gui_startPage("installsu") != 0) { |
Dees Troy | f193f88 | 2013-09-11 14:56:20 +0000 | [diff] [blame] | 296 | LOGERR("Failed to start SuperSU install page.\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 297 | } |
| 298 | } else if (TWFunc::Check_su_Perms() > 0) { |
| 299 | // su perms are set incorrectly |
Dees Troy | f193f88 | 2013-09-11 14:56:20 +0000 | [diff] [blame] | 300 | LOGINFO("Root permissions appear to be lost... fixing. (This will always happen on 4.3+ ROMs with SELinux.\n"); |
| 301 | TWFunc::Fix_su_Perms(); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 302 | } |
| 303 | sync(); |
| 304 | PartitionManager.UnMount_By_Path("/system", false); |
| 305 | } |
| 306 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 307 | // Reboot |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 308 | TWFunc::Update_Intent_File(Reboot_Value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 309 | TWFunc::Update_Log_File(); |
| 310 | gui_print("Rebooting...\n"); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 311 | string Reboot_Arg; |
| 312 | DataManager::GetValue("tw_reboot_arg", Reboot_Arg); |
| 313 | if (Reboot_Arg == "recovery") |
| 314 | TWFunc::tw_reboot(rb_recovery); |
| 315 | else if (Reboot_Arg == "poweroff") |
| 316 | TWFunc::tw_reboot(rb_poweroff); |
| 317 | else if (Reboot_Arg == "bootloader") |
| 318 | TWFunc::tw_reboot(rb_bootloader); |
| 319 | else if (Reboot_Arg == "download") |
| 320 | TWFunc::tw_reboot(rb_download); |
| 321 | else |
| 322 | TWFunc::tw_reboot(rb_system); |
| 323 | |
| 324 | #ifdef ANDROID_RB_RESTART |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 325 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 326 | #else |
| 327 | reboot(RB_AUTOBOOT); |
| 328 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 329 | return 0; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 330 | } |