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