blob: b67631ea886d0aad32262319721b793536d52e50 [file] [log] [blame]
Dees_Troya449a6f2013-04-07 17:50:11 -05001/*
Dees_Troya449a6f2013-04-07 17:50:11 -05002
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003ccdd
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_Troya449a6f2013-04-07 17:50:11 -05008
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05009 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_Troya449a6f2013-04-07 17:50:11 -050013
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050014 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_Troya449a6f2013-04-07 17:50:11 -050016*/
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"
27extern "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
38extern "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 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;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050056twrpDU du;
Dees_Troya449a6f2013-04-07 17:50:11 -050057
58static void Print_Prop(const char *key, const char *name, void *cookie) {
59 printf("%s=%s\n", key, name);
60}
61
62int 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
Vojtech Bocek0fc15732014-07-04 01:09:50 +020081 char crash_prop_val[PROPERTY_VALUE_MAX];
82 int crash_counter;
83 property_get("twrp.crash_counter", crash_prop_val, "-1");
84 crash_counter = atoi(crash_prop_val) + 1;
85 snprintf(crash_prop_val, sizeof(crash_prop_val), "%d", crash_counter);
86 property_set("twrp.crash_counter", crash_prop_val);
87
Dees_Troya449a6f2013-04-07 17:50:11 -050088 time_t StartupTime = time(NULL);
89 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&StartupTime));
90
91 // Load default values to set DataManager constants and handle ifdefs
92 DataManager::SetDefaultValues();
93 printf("Starting the UI...");
94 gui_init();
95 printf("=> Linking mtab\n");
96 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy329383e2013-08-29 14:16:06 +000097 if (TWFunc::Path_Exists("/etc/twrp.fstab")) {
98 if (TWFunc::Path_Exists("/etc/recovery.fstab")) {
99 printf("Renaming regular /etc/recovery.fstab -> /etc/recovery.fstab.bak\n");
100 rename("/etc/recovery.fstab", "/etc/recovery.fstab.bak");
101 }
102 printf("Moving /etc/twrp.fstab -> /etc/recovery.fstab\n");
103 rename("/etc/twrp.fstab", "/etc/recovery.fstab");
104 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500105 printf("=> Processing recovery.fstab\n");
106 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
107 LOGERR("Failing out of recovery due to problem with recovery.fstab.\n");
108 return -1;
109 }
110 PartitionManager.Output_Partition_Logging();
111 // Load up all the resources
112 gui_loadResources();
113
Dees_Troya95f55c2013-08-17 13:14:43 +0000114#ifdef HAVE_SELINUX
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400115 if (TWFunc::Path_Exists("/prebuilt_file_contexts")) {
116 if (TWFunc::Path_Exists("/file_contexts")) {
117 printf("Renaming regular /file_contexts -> /file_contexts.bak\n");
118 rename("/file_contexts", "/file_contexts.bak");
119 }
120 printf("Moving /prebuilt_file_contexts -> /file_contexts\n");
121 rename("/prebuilt_file_contexts", "/file_contexts");
122 }
Dees_Troya95f55c2013-08-17 13:14:43 +0000123 struct selinux_opt selinux_options[] = {
124 { SELABEL_OPT_PATH, "/file_contexts" }
125 };
bigbiff bigbiffc49d7062013-10-11 20:28:00 -0400126 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
Dees_Troya95f55c2013-08-17 13:14:43 +0000127 if (!selinux_handle)
128 printf("No file contexts for SELinux\n");
129 else
130 printf("SELinux contexts loaded from /file_contexts\n");
Dees Troy995e88c2013-11-26 21:39:14 +0000131 { // Check to ensure SELinux can be supported by the kernel
132 char *contexts = NULL;
Dees Troy8d0eb132013-12-06 16:55:41 +0000133
134 if (PartitionManager.Mount_By_Path("/cache", true) && TWFunc::Path_Exists("/cache/recovery")) {
135 lgetfilecon("/cache/recovery", &contexts);
136 if (!contexts) {
137 lsetfilecon("/cache/recovery", "test");
138 lgetfilecon("/cache/recovery", &contexts);
139 }
140 } else {
141 LOGINFO("Could not check /cache/recovery SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n");
142 lgetfilecon("/sbin/teamwin", &contexts);
143 }
Dees Troy995e88c2013-11-26 21:39:14 +0000144 if (!contexts) {
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500145 gui_print_color("warning", "Kernel does not have support for reading SELinux contexts.\n");
Dees Troy995e88c2013-11-26 21:39:14 +0000146 } else {
147 free(contexts);
148 gui_print("Full SELinux support is present.\n");
149 }
150 }
151#else
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500152 gui_print_color("warning", "No SELinux support (no libselinux).\n");
Dees_Troya95f55c2013-08-17 13:14:43 +0000153#endif
154
Dees_Troya449a6f2013-04-07 17:50:11 -0500155 PartitionManager.Mount_By_Path("/cache", true);
156
157 string Zip_File, Reboot_Value;
158 bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false;
159
160 {
Dees_Troy1669f892013-09-04 18:35:08 +0000161 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
162 if (misc != NULL) {
163 if (misc->Current_File_System == "emmc") {
164 set_device_type('e');
165 set_device_name(misc->Actual_Block_Device.c_str());
166 } else if (misc->Current_File_System == "mtd") {
167 set_device_type('m');
168 set_device_name(misc->MTD_Name.c_str());
169 } else {
170 LOGERR("Unknown file system for /misc\n");
171 }
172 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500173 get_args(&argc, &argv);
174
175 int index, index2, len;
176 char* argptr;
177 char* ptr;
178 printf("Startup Commands: ");
179 for (index = 1; index < argc; index++) {
180 argptr = argv[index];
181 printf(" '%s'", argv[index]);
182 len = strlen(argv[index]);
183 if (*argptr == '-') {argptr++; len--;}
184 if (*argptr == '-') {argptr++; len--;}
185 if (*argptr == 'u') {
186 ptr = argptr;
187 index2 = 0;
188 while (*ptr != '=' && *ptr != '\n')
189 ptr++;
Kevin Steck7b69e9d2013-10-17 18:24:31 -0400190 // skip the = before grabbing Zip_File
191 while (*ptr == '=')
192 ptr++;
Dees_Troya449a6f2013-04-07 17:50:11 -0500193 if (*ptr) {
194 Zip_File = ptr;
195 } else
196 LOGERR("argument error specifying zip file\n");
197 } else if (*argptr == 'w') {
198 if (len == 9)
199 Factory_Reset = true;
200 else if (len == 10)
201 Cache_Wipe = true;
202 } else if (*argptr == 'n') {
203 Perform_Backup = true;
204 } else if (*argptr == 's') {
205 ptr = argptr;
206 index2 = 0;
207 while (*ptr != '=' && *ptr != '\n')
208 ptr++;
209 if (*ptr) {
210 Reboot_Value = *ptr;
211 }
212 }
213 }
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200214 printf("\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500215 }
216
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200217 if(crash_counter == 0) {
Dees_Troya449a6f2013-04-07 17:50:11 -0500218 property_list(Print_Prop, NULL);
219 printf("\n");
Vojtech Bocek0fc15732014-07-04 01:09:50 +0200220 } else {
221 printf("twrp.crash_counter=%d\n", crash_counter);
Dees_Troya449a6f2013-04-07 17:50:11 -0500222 }
223
224 // Check for and run startup script if script exists
225 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
226 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
227
228#ifdef TW_INCLUDE_INJECTTWRP
229 // Back up TWRP Ramdisk if needed:
230 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500231 LOGINFO("Backing up TWRP ramdisk...\n");
232 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200233 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500234 else {
235 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200236 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500237 }
238 LOGINFO("Backup of TWRP ramdisk done.\n");
239#endif
240
241 bool Keep_Going = true;
242 if (Perform_Backup) {
Dees Troyb21cc642013-09-10 17:36:41 +0000243 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troya449a6f2013-04-07 17:50:11 -0500244 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
245 Keep_Going = false;
246 }
247 if (Keep_Going && !Zip_File.empty()) {
248 string ORSCommand = "install " + Zip_File;
249
250 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
251 Keep_Going = false;
252 }
253 if (Keep_Going) {
254 if (Factory_Reset) {
255 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
256 Keep_Going = false;
257 } else if (Cache_Wipe) {
258 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
259 Keep_Going = false;
260 }
261 }
262
263 TWFunc::Update_Log_File();
264 // Offer to decrypt if the device is encrypted
265 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
266 LOGINFO("Is encrypted, do decrypt page first\n");
267 if (gui_startPage("decrypt") != 0) {
268 LOGERR("Failed to start decrypt GUI page.\n");
269 }
270 }
271
272 // Read the settings file
273 DataManager::ReadSettingsFile();
Vojtech Bocek2005a842014-03-11 19:40:34 +0100274
275 // Fixup the RTC clock on devices which require it
276 TWFunc::Fixup_Time_On_Boot();
277
Dees_Troya449a6f2013-04-07 17:50:11 -0500278 // Run any outstanding OpenRecoveryScript
279 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
280 OpenRecoveryScript::Run_OpenRecoveryScript();
281 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100282
Dees_Troya449a6f2013-04-07 17:50:11 -0500283 // Launch the main GUI
284 gui_start();
285
286 // Check for su to see if the device is rooted or not
287 if (PartitionManager.Mount_By_Path("/system", false)) {
288 // Disable flashing of stock recovery
289 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
290 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
291 gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
292 }
293 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")) {
294 // Device doesn't have su installed
295 DataManager::SetValue("tw_busy", 1);
296 if (gui_startPage("installsu") != 0) {
Dees Troyf193f882013-09-11 14:56:20 +0000297 LOGERR("Failed to start SuperSU install page.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500298 }
299 } else if (TWFunc::Check_su_Perms() > 0) {
300 // su perms are set incorrectly
Dees Troyf193f882013-09-11 14:56:20 +0000301 LOGINFO("Root permissions appear to be lost... fixing. (This will always happen on 4.3+ ROMs with SELinux.\n");
302 TWFunc::Fix_su_Perms();
Dees_Troya449a6f2013-04-07 17:50:11 -0500303 }
304 sync();
305 PartitionManager.UnMount_By_Path("/system", false);
306 }
307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 // Reboot
Dees_Troya449a6f2013-04-07 17:50:11 -0500309 TWFunc::Update_Intent_File(Reboot_Value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200310 TWFunc::Update_Log_File();
311 gui_print("Rebooting...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500312 string Reboot_Arg;
313 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
314 if (Reboot_Arg == "recovery")
315 TWFunc::tw_reboot(rb_recovery);
316 else if (Reboot_Arg == "poweroff")
317 TWFunc::tw_reboot(rb_poweroff);
318 else if (Reboot_Arg == "bootloader")
319 TWFunc::tw_reboot(rb_bootloader);
320 else if (Reboot_Arg == "download")
321 TWFunc::tw_reboot(rb_download);
322 else
323 TWFunc::tw_reboot(rb_system);
324
325#ifdef ANDROID_RB_RESTART
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200326 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troya449a6f2013-04-07 17:50:11 -0500327#else
328 reboot(RB_AUTOBOOT);
329#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200330 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500331}