blob: 04851aaf1606683dbcaf17f07e8b2a02dcacb04f [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");
89 printf("=> Processing recovery.fstab\n");
90 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
91 LOGERR("Failing out of recovery due to problem with recovery.fstab.\n");
92 return -1;
93 }
94 PartitionManager.Output_Partition_Logging();
95 // Load up all the resources
96 gui_loadResources();
97
Dees_Troya95f55c2013-08-17 13:14:43 +000098#ifdef HAVE_SELINUX
99 struct selinux_opt selinux_options[] = {
100 { SELABEL_OPT_PATH, "/file_contexts" }
101 };
102 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
103 if (!selinux_handle)
104 printf("No file contexts for SELinux\n");
105 else
106 printf("SELinux contexts loaded from /file_contexts\n");
107#endif
108
Dees_Troya449a6f2013-04-07 17:50:11 -0500109 PartitionManager.Mount_By_Path("/cache", true);
110
111 string Zip_File, Reboot_Value;
112 bool Cache_Wipe = false, Factory_Reset = false, Perform_Backup = false;
113
114 {
115 get_args(&argc, &argv);
116
117 int index, index2, len;
118 char* argptr;
119 char* ptr;
120 printf("Startup Commands: ");
121 for (index = 1; index < argc; index++) {
122 argptr = argv[index];
123 printf(" '%s'", argv[index]);
124 len = strlen(argv[index]);
125 if (*argptr == '-') {argptr++; len--;}
126 if (*argptr == '-') {argptr++; len--;}
127 if (*argptr == 'u') {
128 ptr = argptr;
129 index2 = 0;
130 while (*ptr != '=' && *ptr != '\n')
131 ptr++;
132 if (*ptr) {
133 Zip_File = ptr;
134 } else
135 LOGERR("argument error specifying zip file\n");
136 } else if (*argptr == 'w') {
137 if (len == 9)
138 Factory_Reset = true;
139 else if (len == 10)
140 Cache_Wipe = true;
141 } else if (*argptr == 'n') {
142 Perform_Backup = true;
143 } else if (*argptr == 's') {
144 ptr = argptr;
145 index2 = 0;
146 while (*ptr != '=' && *ptr != '\n')
147 ptr++;
148 if (*ptr) {
149 Reboot_Value = *ptr;
150 }
151 }
152 }
153 }
154
155 char twrp_booted[PROPERTY_VALUE_MAX];
156 property_get("ro.twrp.boot", twrp_booted, "0");
157 if (strcmp(twrp_booted, "0") == 0) {
158 property_list(Print_Prop, NULL);
159 printf("\n");
160 property_set("ro.twrp.boot", "1");
161 }
162
163 // Check for and run startup script if script exists
164 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
165 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
166
167#ifdef TW_INCLUDE_INJECTTWRP
168 // Back up TWRP Ramdisk if needed:
169 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
170 string result;
171 LOGINFO("Backing up TWRP ramdisk...\n");
172 if (Boot == NULL || Boot->Current_File_System != "emmc")
173 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img", result);
174 else {
175 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
176 TWFunc::Exec_Cmd(injectcmd, result);
177 }
178 LOGINFO("Backup of TWRP ramdisk done.\n");
179#endif
180
181 bool Keep_Going = true;
182 if (Perform_Backup) {
183 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
184 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
185 Keep_Going = false;
186 }
187 if (Keep_Going && !Zip_File.empty()) {
188 string ORSCommand = "install " + Zip_File;
189
190 if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
191 Keep_Going = false;
192 }
193 if (Keep_Going) {
194 if (Factory_Reset) {
195 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
196 Keep_Going = false;
197 } else if (Cache_Wipe) {
198 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
199 Keep_Going = false;
200 }
201 }
202
203 TWFunc::Update_Log_File();
204 // Offer to decrypt if the device is encrypted
205 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) != 0) {
206 LOGINFO("Is encrypted, do decrypt page first\n");
207 if (gui_startPage("decrypt") != 0) {
208 LOGERR("Failed to start decrypt GUI page.\n");
209 }
210 }
211
212 // Read the settings file
213 DataManager::ReadSettingsFile();
214 // Run any outstanding OpenRecoveryScript
215 if (DataManager::GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
216 OpenRecoveryScript::Run_OpenRecoveryScript();
217 }
218 // Launch the main GUI
219 gui_start();
220
221 // Check for su to see if the device is rooted or not
222 if (PartitionManager.Mount_By_Path("/system", false)) {
223 // Disable flashing of stock recovery
224 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
225 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
226 gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
227 }
228 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")) {
229 // Device doesn't have su installed
230 DataManager::SetValue("tw_busy", 1);
231 if (gui_startPage("installsu") != 0) {
232 LOGERR("Failed to start decrypt GUI page.\n");
233 }
234 } else if (TWFunc::Check_su_Perms() > 0) {
235 // su perms are set incorrectly
236 DataManager::SetValue("tw_busy", 1);
237 if (gui_startPage("fixsu") != 0) {
238 LOGERR("Failed to start decrypt GUI page.\n");
239 }
240 }
241 sync();
242 PartitionManager.UnMount_By_Path("/system", false);
243 }
244
245 // Reboot
246 TWFunc::Update_Intent_File(Reboot_Value);
247 TWFunc::Update_Log_File();
248 gui_print("Rebooting...\n");
249 string Reboot_Arg;
250 DataManager::GetValue("tw_reboot_arg", Reboot_Arg);
251 if (Reboot_Arg == "recovery")
252 TWFunc::tw_reboot(rb_recovery);
253 else if (Reboot_Arg == "poweroff")
254 TWFunc::tw_reboot(rb_poweroff);
255 else if (Reboot_Arg == "bootloader")
256 TWFunc::tw_reboot(rb_bootloader);
257 else if (Reboot_Arg == "download")
258 TWFunc::tw_reboot(rb_download);
259 else
260 TWFunc::tw_reboot(rb_system);
261
262#ifdef ANDROID_RB_RESTART
263 android_reboot(ANDROID_RB_RESTART, 0, 0);
264#else
265 reboot(RB_AUTOBOOT);
266#endif
267 return 0;
268}