blob: 53aab7f66c65038c4b567599c4a04b34606da527 [file] [log] [blame]
Dees_Troya449a6f2013-04-07 17:50:11 -05001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 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_Troya449a6f2013-04-07 17:50:11 -05006
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05007 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_Troya449a6f2013-04-07 17:50:11 -050011
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050012 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_Troya449a6f2013-04-07 17:50:11 -050014*/
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>
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -060023#include <signal.h>
Dees_Troya449a6f2013-04-07 17:50:11 -050024
25#include "cutils/properties.h"
26extern "C" {
27#include "minadbd/adb.h"
28#include "bootloader.h"
29}
30
31#ifdef ANDROID_RB_RESTART
32#include "cutils/android_reboot.h"
33#else
34#include <sys/reboot.h>
35#endif
36
37extern "C" {
38#include "gui/gui.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060039#include "set_metadata.h"
Dees_Troya449a6f2013-04-07 17:50:11 -050040}
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 bigbiff34684ff2013-12-01 21:03:45 -050047#include "twrpDU.hpp"
Dees_Troya449a6f2013-04-07 17:50:11 -050048
Dees_Troya95f55c2013-08-17 13:14:43 +000049#ifdef HAVE_SELINUX
50#include "selinux/label.h"
51struct selabel_handle *selinux_handle;
52#endif
53
Dees_Troya449a6f2013-04-07 17:50:11 -050054TWPartitionManager PartitionManager;
55int Log_Offset;
Ethan Yonker6277c792014-09-15 14:54:30 -050056bool datamedia;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050057twrpDU du;
Dees_Troya449a6f2013-04-07 17:50:11 -050058
59static void Print_Prop(const char *key, const char *name, void *cookie) {
60 printf("%s=%s\n", key, name);
61}
62
63int main(int argc, char **argv) {
64 // Recovery needs to install world-readable files, so clear umask
65 // set by init
66 umask(0);
67
68 Log_Offset = 0;
69
70 // Set up temporary log file (/tmp/recovery.log)
71 freopen(TMP_LOG_FILE, "a", stdout);
72 setbuf(stdout, NULL);
73 freopen(TMP_LOG_FILE, "a", stderr);
74 setbuf(stderr, NULL);
75
Ethan Yonkerf9f99bc2014-12-29 09:10:34 -060076 signal(SIGPIPE, SIG_IGN);
77
Dees_Troya449a6f2013-04-07 17:50:11 -050078 // Handle ADB sideload
79 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
80 adb_main(argv[2]);
81 return 0;
82 }
83
Ethan Yonker6277c792014-09-15 14:54:30 -050084#ifdef RECOVERY_SDCARD_ON_DATA
85 datamedia = true;
86#endif
87
Vojtech Bocek0fc15732014-07-04 01:09:50 +020088 char crash_prop_val[PROPERTY_VALUE_MAX];
89 int crash_counter;
90 property_get("twrp.crash_counter", crash_prop_val, "-1");
91 crash_counter = atoi(crash_prop_val) + 1;
92 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
93 property_set("twrp.crash_counter", crash_prop_val);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040094 property_set("ro.twrp.boot", "1");
95 property_set("ro.twrp.version", TW_VERSION_STR);
Vojtech Bocek0fc15732014-07-04 01:09:50 +020096
Dees_Troya449a6f2013-04-07 17:50:11 -050097 time_t StartupTime = time(NULL);
Ethan Yonkera1674162014-11-06 08:35:10 -060098 printf("Starting TWRP %s on %s (pid %d)", TW_VERSION_STR, ctime(&StartupTime), getpid());
Dees_Troya449a6f2013-04-07 17:50:11 -050099
100 // Load default values to set DataManager constants and handle ifdefs
101 DataManager::SetDefaultValues();
102 printf("Starting the UI...");
103 gui_init();
104 printf("=> Linking mtab\n");
105 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy329383e2013-08-29 14:16:06 +0000106 if (TWFunc::Path_Exists("/etc/twrp.fstab")) {
107 if (TWFunc::Path_Exists("/etc/recovery.fstab")) {
108 printf("Renaming regular /etc/recovery.fstab -> /etc/recovery.fstab.bak\n");
109 rename("/etc/recovery.fstab", "/etc/recovery.fstab.bak");
110 }
111 printf("Moving /etc/twrp.fstab -> /etc/recovery.fstab\n");
112 rename("/etc/twrp.fstab", "/etc/recovery.fstab");
113 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500114 printf("=> Processing recovery.fstab\n");
115 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
116 LOGERR("Failing out of recovery due to problem with recovery.fstab.\n");
117 return -1;
118 }
119 PartitionManager.Output_Partition_Logging();
120 // Load up all the resources
121 gui_loadResources();
122
Dees_Troya95f55c2013-08-17 13:14:43 +0000123#ifdef HAVE_SELINUX
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400124 if (TWFunc::Path_Exists("/prebuilt_file_contexts")) {
125 if (TWFunc::Path_Exists("/file_contexts")) {
126 printf("Renaming regular /file_contexts -> /file_contexts.bak\n");
127 rename("/file_contexts", "/file_contexts.bak");
128 }
129 printf("Moving /prebuilt_file_contexts -> /file_contexts\n");
130 rename("/prebuilt_file_contexts", "/file_contexts");
131 }
Dees_Troya95f55c2013-08-17 13:14:43 +0000132 struct selinux_opt selinux_options[] = {
133 { SELABEL_OPT_PATH, "/file_contexts" }
134 };
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400135 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
Dees_Troya95f55c2013-08-17 13:14:43 +0000136 if (!selinux_handle)
137 printf("No file contexts for SELinux\n");
138 else
139 printf("SELinux contexts loaded from /file_contexts\n");
Dees Troy995e88c2013-11-26 21:39:14 +0000140 { // Check to ensure SELinux can be supported by the kernel
141 char *contexts = NULL;
Dees Troy8d0eb132013-12-06 16:55:41 +0000142
143 if (PartitionManager.Mount_By_Path("/cache", true) && TWFunc::Path_Exists("/cache/recovery")) {
144 lgetfilecon("/cache/recovery", &contexts);
145 if (!contexts) {
146 lsetfilecon("/cache/recovery", "test");
147 lgetfilecon("/cache/recovery", &contexts);
148 }
149 } else {
150 LOGINFO("Could not check /cache/recovery SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n");
151 lgetfilecon("/sbin/teamwin", &contexts);
152 }
Dees Troy995e88c2013-11-26 21:39:14 +0000153 if (!contexts) {
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500154 gui_print_color("warning", "Kernel does not have support for reading SELinux contexts.\n");
Dees Troy995e88c2013-11-26 21:39:14 +0000155 } else {
156 free(contexts);
157 gui_print("Full SELinux support is present.\n");
158 }
159 }
160#else
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500161 gui_print_color("warning", "No SELinux support (no libselinux).\n");
Dees_Troya95f55c2013-08-17 13:14:43 +0000162#endif
163
Dees_Troya449a6f2013-04-07 17:50:11 -0500164 PartitionManager.Mount_By_Path("/cache", true);
165
166 string Zip_File, Reboot_Value;
Ethan Yonkera1674162014-11-06 08:35:10 -0600167 bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false, Shutdown = false;
Dees_Troya449a6f2013-04-07 17:50:11 -0500168
169 {
Dees_Troy1669f892013-09-04 18:35:08 +0000170 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
171 if (misc != NULL) {
172 if (misc->Current_File_System == "emmc") {
173 set_device_type('e');
174 set_device_name(misc->Actual_Block_Device.c_str());
175 } else if (misc->Current_File_System == "mtd") {
176 set_device_type('m');
177 set_device_name(misc->MTD_Name.c_str());
178 } else {
179 LOGERR("Unknown file system for /misc\n");
180 }
181 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500182 get_args(&argc, &argv);
183
184 int index, index2, len;
185 char* argptr;
186 char* ptr;
187 printf("Startup Commands: ");
188 for (index = 1; index < argc; index++) {
189 argptr = argv[index];
190 printf(" '%s'", argv[index]);
191 len = strlen(argv[index]);
192 if (*argptr == '-') {argptr++; len--;}
193 if (*argptr == '-') {argptr++; len--;}
194 if (*argptr == 'u') {
195 ptr = argptr;
196 index2 = 0;
197 while (*ptr != '=' && *ptr != '\n')
198 ptr++;
Kevin Steck7b69e9d2013-10-17 18:24:31 -0400199 // skip the = before grabbing Zip_File
200 while (*ptr == '=')
201 ptr++;
Dees_Troya449a6f2013-04-07 17:50:11 -0500202 if (*ptr) {
203 Zip_File = ptr;
204 } else
205 LOGERR("argument error specifying zip file\n");
206 } else if (*argptr == 'w') {
207 if (len == 9)
208 Factory_Reset = true;
209 else if (len == 10)
210 Cache_Wipe = true;
211 } else if (*argptr == 'n') {
212 Perform_Backup = true;
Ethan Yonkera1674162014-11-06 08:35:10 -0600213 } else if (*argptr == 'p') {
214 Shutdown = true;
Dees_Troya449a6f2013-04-07 17:50:11 -0500215 } else if (*argptr == 's') {
216 ptr = argptr;
217 index2 = 0;
218 while (*ptr != '=' && *ptr != '\n')
219 ptr++;
220 if (*ptr) {
221 Reboot_Value = *ptr;
222 }
223 }
224 }
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200225 printf("\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500226 }
227
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200228 if(crash_counter == 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500229 property_list(Print_Prop, NULL);
230 printf("\n");
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200231 } else {
232 printf("twrp.crash_counter=%d\n", crash_counter);
Dees_Troya449a6f2013-04-07 17:50:11 -0500233 }
234
235 // Check for and run startup script if script exists
236 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
237 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
238
239#ifdef TW_INCLUDE_INJECTTWRP
240 // Back up TWRP Ramdisk if needed:
241 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500242 LOGINFO("Backing up TWRP ramdisk...\n");
243 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200244 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500245 else {
246 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200247 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500248 }
249 LOGINFO("Backup of TWRP ramdisk done.\n");
250#endif
251
252 bool Keep_Going = true;
253 if (Perform_Backup) {
Dees Troyb21cc642013-09-10 17:36:41 +0000254 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troya449a6f2013-04-07 17:50:11 -0500255 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
256 Keep_Going = false;
257 }
258 if (Keep_Going && !Zip_File.empty()) {
259 string ORSCommand = "install " + Zip_File;
260
261 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
262 Keep_Going = false;
263 }
264 if (Keep_Going) {
265 if (Factory_Reset) {
266 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
267 Keep_Going = false;
268 } else if (Cache_Wipe) {
269 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
270 Keep_Going = false;
271 }
272 }
273
274 TWFunc::Update_Log_File();
275 // Offer to decrypt if the device is encrypted
276 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
277 LOGINFO("Is encrypted, do decrypt page first\n");
278 if (gui_startPage("decrypt") != 0) {
279 LOGERR("Failed to start decrypt GUI page.\n");
280 }
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600281 } else if (datamedia) {
282 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
283 LOGERR("Failed to get default contexts and file mode for storage files.\n");
284 } else {
285 LOGINFO("Got default contexts and file mode for storage files.\n");
286 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500287 }
288
289 // Read the settings file
Ethan Yonker73da42c2014-09-03 11:30:33 -0500290#ifdef TW_HAS_MTP
291 // We unmount partitions sometimes during early boot which may override
292 // the default of MTP being enabled by auto toggling MTP off. This
293 // will force it back to enabled then get overridden by the settings
294 // file, assuming that an entry for tw_mtp_enabled is set.
295 DataManager::SetValue("tw_mtp_enabled", 1);
296#endif
Dees_Troya449a6f2013-04-07 17:50:11 -0500297 DataManager::ReadSettingsFile();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100298
299 // Fixup the RTC clock on devices which require it
Vojtech Bocek67351dc2014-07-03 15:22:41 +0200300 if(crash_counter == 0)
301 TWFunc::Fixup_Time_On_Boot();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100302
Dees_Troya449a6f2013-04-07 17:50:11 -0500303 // Run any outstanding OpenRecoveryScript
304 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
305 OpenRecoveryScript::Run_OpenRecoveryScript();
306 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100307
Ethan Yonker6f49e112014-09-03 21:42:49 -0500308#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400309 // Enable MTP?
Ethan Yonker6f49e112014-09-03 21:42:49 -0500310 char mtp_crash_check[PROPERTY_VALUE_MAX];
311 property_get("mtp.crash_check", mtp_crash_check, "0");
312 if (strcmp(mtp_crash_check, "0") == 0) {
313 property_set("mtp.crash_check", "1");
314 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
315 if (DataManager::GetIntValue(TW_IS_DECRYPTED) != 0 && DataManager::GetIntValue("tw_mtp_enabled") == 1) {
316 LOGINFO("Enabling MTP during startup\n");
317 if (!PartitionManager.Enable_MTP())
318 PartitionManager.Disable_MTP();
319 else
320 gui_print("MTP Enabled\n");
321 }
322 } else if (DataManager::GetIntValue("tw_mtp_enabled") == 1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400323 LOGINFO("Enabling MTP during startup\n");
324 if (!PartitionManager.Enable_MTP())
325 PartitionManager.Disable_MTP();
326 else
327 gui_print("MTP Enabled\n");
328 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500329 property_set("mtp.crash_check", "0");
330 } else {
331 gui_print_color("warning", "MTP Crashed, not starting MTP on boot.\n");
Ethan Yonker12055fa2014-09-04 08:06:53 -0500332 DataManager::SetValue("tw_mtp_enabled", 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400333 }
Ethan Yonker6f49e112014-09-03 21:42:49 -0500334#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400335
Dees_Troya449a6f2013-04-07 17:50:11 -0500336 // Launch the main GUI
337 gui_start();
338
339 // Check for su to see if the device is rooted or not
340 if (PartitionManager.Mount_By_Path("/system", false)) {
341 // Disable flashing of stock recovery
Dees Troyc8f58992014-11-20 15:29:03 +0000342 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500343 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
344 gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
345 }
346 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")) {
347 // Device doesn't have su installed
348 DataManager::SetValue("tw_busy", 1);
349 if (gui_startPage("installsu") != 0) {
Dees Troyf193f882013-09-11 14:56:20 +0000350 LOGERR("Failed to start SuperSU install page.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500351 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500352 }
353 sync();
354 PartitionManager.UnMount_By_Path("/system", false);
355 }
356
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 // Reboot
Dees_Troya449a6f2013-04-07 17:50:11 -0500358 TWFunc::Update_Intent_File(Reboot_Value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 TWFunc::Update_Log_File();
360 gui_print("Rebooting...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500361 string Reboot_Arg;
362 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
363 if (Reboot_Arg == "recovery")
364 TWFunc::tw_reboot(rb_recovery);
365 else if (Reboot_Arg == "poweroff")
366 TWFunc::tw_reboot(rb_poweroff);
367 else if (Reboot_Arg == "bootloader")
368 TWFunc::tw_reboot(rb_bootloader);
369 else if (Reboot_Arg == "download")
370 TWFunc::tw_reboot(rb_download);
371 else
372 TWFunc::tw_reboot(rb_system);
373
374#ifdef ANDROID_RB_RESTART
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200375 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troya449a6f2013-04-07 17:50:11 -0500376#else
377 reboot(RB_AUTOBOOT);
378#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500380}