blob: 765cad34237f71801107025968e381a09e37e25f [file] [log] [blame]
Dees_Troya449a6f2013-04-07 17:50:11 -05001/*
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"
28extern "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
39extern "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_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;
56
57static void Print_Prop(const char *key, const char *name, void *cookie) {
58 printf("%s=%s\n", key, name);
59}
60
61int 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_Troy329383e2013-08-29 14:16:06 +000089 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_Troya449a6f2013-04-07 17:50:11 -050097 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_Troya95f55c2013-08-17 13:14:43 +0000106#ifdef HAVE_SELINUX
107 struct selinux_opt selinux_options[] = {
108 { SELABEL_OPT_PATH, "/file_contexts" }
109 };
110 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
111 if (!selinux_handle)
112 printf("No file contexts for SELinux\n");
113 else
114 printf("SELinux contexts loaded from /file_contexts\n");
115#endif
116
Dees_Troya449a6f2013-04-07 17:50:11 -0500117 PartitionManager.Mount_By_Path("/cache", true);
118
119 string Zip_File, Reboot_Value;
120 bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false;
121
122 {
Dees_Troy1669f892013-09-04 18:35:08 +0000123 TWPartition* misc = PartitionManager.Find_Partition_By_Path("/misc");
124 if (misc != NULL) {
125 if (misc->Current_File_System == "emmc") {
126 set_device_type('e');
127 set_device_name(misc->Actual_Block_Device.c_str());
128 } else if (misc->Current_File_System == "mtd") {
129 set_device_type('m');
130 set_device_name(misc->MTD_Name.c_str());
131 } else {
132 LOGERR("Unknown file system for /misc\n");
133 }
134 }
Dees_Troya449a6f2013-04-07 17:50:11 -0500135 get_args(&argc, &argv);
136
137 int index, index2, len;
138 char* argptr;
139 char* ptr;
140 printf("Startup Commands: ");
141 for (index = 1; index < argc; index++) {
142 argptr = argv[index];
143 printf(" '%s'", argv[index]);
144 len = strlen(argv[index]);
145 if (*argptr == '-') {argptr++; len--;}
146 if (*argptr == '-') {argptr++; len--;}
147 if (*argptr == 'u') {
148 ptr = argptr;
149 index2 = 0;
150 while (*ptr != '=' && *ptr != '\n')
151 ptr++;
152 if (*ptr) {
153 Zip_File = ptr;
154 } else
155 LOGERR("argument error specifying zip file\n");
156 } else if (*argptr == 'w') {
157 if (len == 9)
158 Factory_Reset = true;
159 else if (len == 10)
160 Cache_Wipe = true;
161 } else if (*argptr == 'n') {
162 Perform_Backup = true;
163 } else if (*argptr == 's') {
164 ptr = argptr;
165 index2 = 0;
166 while (*ptr != '=' && *ptr != '\n')
167 ptr++;
168 if (*ptr) {
169 Reboot_Value = *ptr;
170 }
171 }
172 }
173 }
174
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 char twrp_booted[PROPERTY_VALUE_MAX];
Dees_Troya449a6f2013-04-07 17:50:11 -0500176 property_get("ro.twrp.boot", twrp_booted, "0");
177 if (strcmp(twrp_booted, "0") == 0) {
178 property_list(Print_Prop, NULL);
179 printf("\n");
180 property_set("ro.twrp.boot", "1");
181 }
182
183 // Check for and run startup script if script exists
184 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
185 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
186
187#ifdef TW_INCLUDE_INJECTTWRP
188 // Back up TWRP Ramdisk if needed:
189 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troya449a6f2013-04-07 17:50:11 -0500190 LOGINFO("Backing up TWRP ramdisk...\n");
191 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200192 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troya449a6f2013-04-07 17:50:11 -0500193 else {
194 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200195 TWFunc::Exec_Cmd(injectcmd);
Dees_Troya449a6f2013-04-07 17:50:11 -0500196 }
197 LOGINFO("Backup of TWRP ramdisk done.\n");
198#endif
199
200 bool Keep_Going = true;
201 if (Perform_Backup) {
Dees Troyb21cc642013-09-10 17:36:41 +0000202 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troya449a6f2013-04-07 17:50:11 -0500203 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
204 Keep_Going = false;
205 }
206 if (Keep_Going && !Zip_File.empty()) {
207 string ORSCommand = "install " + Zip_File;
208
209 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
210 Keep_Going = false;
211 }
212 if (Keep_Going) {
213 if (Factory_Reset) {
214 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
215 Keep_Going = false;
216 } else if (Cache_Wipe) {
217 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
218 Keep_Going = false;
219 }
220 }
221
222 TWFunc::Update_Log_File();
223 // Offer to decrypt if the device is encrypted
224 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
225 LOGINFO("Is encrypted, do decrypt page first\n");
226 if (gui_startPage("decrypt") != 0) {
227 LOGERR("Failed to start decrypt GUI page.\n");
228 }
229 }
230
231 // Read the settings file
232 DataManager::ReadSettingsFile();
233 // Run any outstanding OpenRecoveryScript
234 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
235 OpenRecoveryScript::Run_OpenRecoveryScript();
236 }
237 // Launch the main GUI
238 gui_start();
239
240 // Check for su to see if the device is rooted or not
241 if (PartitionManager.Mount_By_Path("/system", false)) {
242 // Disable flashing of stock recovery
243 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
244 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
245 gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
246 }
247 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")) {
248 // Device doesn't have su installed
249 DataManager::SetValue("tw_busy", 1);
250 if (gui_startPage("installsu") != 0) {
Dees Troyf193f882013-09-11 14:56:20 +0000251 LOGERR("Failed to start SuperSU install page.\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500252 }
253 } else if (TWFunc::Check_su_Perms() > 0) {
254 // su perms are set incorrectly
Dees Troyf193f882013-09-11 14:56:20 +0000255 LOGINFO("Root permissions appear to be lost... fixing. (This will always happen on 4.3+ ROMs with SELinux.\n");
256 TWFunc::Fix_su_Perms();
Dees_Troya449a6f2013-04-07 17:50:11 -0500257 }
258 sync();
259 PartitionManager.UnMount_By_Path("/system", false);
260 }
261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 // Reboot
Dees_Troya449a6f2013-04-07 17:50:11 -0500263 TWFunc::Update_Intent_File(Reboot_Value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 TWFunc::Update_Log_File();
265 gui_print("Rebooting...\n");
Dees_Troya449a6f2013-04-07 17:50:11 -0500266 string Reboot_Arg;
267 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
268 if (Reboot_Arg == "recovery")
269 TWFunc::tw_reboot(rb_recovery);
270 else if (Reboot_Arg == "poweroff")
271 TWFunc::tw_reboot(rb_poweroff);
272 else if (Reboot_Arg == "bootloader")
273 TWFunc::tw_reboot(rb_bootloader);
274 else if (Reboot_Arg == "download")
275 TWFunc::tw_reboot(rb_download);
276 else
277 TWFunc::tw_reboot(rb_system);
278
279#ifdef ANDROID_RB_RESTART
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troya449a6f2013-04-07 17:50:11 -0500281#else
282 reboot(RB_AUTOBOOT);
283#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 return 0;
Dees_Troya449a6f2013-04-07 17:50:11 -0500285}