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