blob: 76ee93a1e4f6aca171d0eb5c8a502f57d7c5c6ff [file] [log] [blame]
Dees_Troy38bd7602012-09-14 13:33:53 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
Dees_Troy38bd7602012-09-14 13:33:53 -04004#include <unistd.h>
5#include <vector>
6#include <dirent.h>
7#include <time.h>
Dees_Troy43d8b002012-09-17 16:00:01 -04008#include <errno.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009#include <fcntl.h>
10#include <sys/mount.h>
Dees_Troya58bead2012-09-27 09:49:29 -040011#include <sys/reboot.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050012#include <sys/sendfile.h>
13#include <sys/stat.h>
14#include <sys/vfs.h>
Dees_Troya4438782013-02-22 18:44:00 +000015#ifdef ANDROID_RB_POWEROFF
16 #include "cutils/android_reboot.h"
17#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050018#include <iostream>
19#include <fstream>
Dees_Troy38bd7602012-09-14 13:33:53 -040020#include "twrp-functions.hpp"
21#include "partitions.hpp"
22#include "common.h"
Dees_Troyb46a6842012-09-25 11:06:46 -040023#include "data.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040024#include "bootloader.h"
Dees_Troy3477d712012-09-27 15:44:01 -040025#include "variables.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040026
Dees_Troyb05ddee2013-01-28 20:24:50 +000027extern "C" {
28 #include "libcrecovery/common.h"
29}
30
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031
32/* Execute a command */
33
34int TWFunc::Exec_Cmd(string cmd, string &result) {
35 FILE* exec;
Dees_Troyb05ddee2013-01-28 20:24:50 +000036 char buffer[130];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037 int ret = 0;
Dees_Troyb05ddee2013-01-28 20:24:50 +000038 exec = __popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -050039 if (!exec) return -1;
40 while(!feof(exec)) {
Dees_Troyb05ddee2013-01-28 20:24:50 +000041 memset(&buffer, 0, sizeof(buffer));
42 if (fgets(buffer, 128, exec) != NULL) {
43 buffer[128] = '\n';
44 buffer[129] = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045 result += buffer;
Dees_Troyb05ddee2013-01-28 20:24:50 +000046 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050047 }
Dees_Troyb05ddee2013-01-28 20:24:50 +000048 ret = __pclose(exec);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050049 return ret;
50}
51
Dees_Troy38bd7602012-09-14 13:33:53 -040052// Returns "file.name" from a full /path/to/file.name
53string TWFunc::Get_Filename(string Path) {
54 size_t pos = Path.find_last_of("/");
55 if (pos != string::npos) {
56 string Filename;
57 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
58 return Filename;
59 } else
60 return Path;
61}
62
63// Returns "/path/to/" from a full /path/to/file.name
64string TWFunc::Get_Path(string Path) {
65 size_t pos = Path.find_last_of("/");
66 if (pos != string::npos) {
67 string Pathonly;
68 Pathonly = Path.substr(0, pos + 1);
69 return Pathonly;
70 } else
71 return Path;
72}
73
74// Returns "/path" from a full /path/to/file.name
75string TWFunc::Get_Root_Path(string Path) {
76 string Local_Path = Path;
77
78 // Make sure that we have a leading slash
79 if (Local_Path.substr(0, 1) != "/")
80 Local_Path = "/" + Local_Path;
81
82 // Trim the path to get the root path only
83 size_t position = Local_Path.find("/", 2);
84 if (position != string::npos) {
85 Local_Path.resize(position);
86 }
87 return Local_Path;
88}
89
90void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -040091 int need_libs = 0;
92
93 if (!PartitionManager.Mount_By_Path("/system", true))
94 return;
95
96 if (!PartitionManager.Mount_By_Path("/data", true))
97 return;
98
99 ui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500100 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400101 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400102 ui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500103 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400104 need_libs = 1;
105 } else
106 ui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400107 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400108 ui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500109 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400110 need_libs = 1;
111 } else
112 ui_print("dump_image is already installed, skipping...\n");
113 if (need_libs) {
114 ui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500115 copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
116 copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
117 copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
118 copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 }
120 ui_print("Installing HTC Dumlock app...\n");
121 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500122 unlink("/data/app/com.teamwin.htcdumlock*");
123 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 sync();
125 ui_print("HTC Dumlock is installed.\n");
126}
127
128void TWFunc::htc_dumlock_restore_original_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500129 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 if (!PartitionManager.Mount_By_Path("/sdcard", true))
131 return;
132
133 ui_print("Restoring original boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500134 Exec_Cmd("htcdumlock restore", status);
Dees_Troy38bd7602012-09-14 13:33:53 -0400135 ui_print("Original boot restored.\n");
136}
137
138void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500139 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400140 if (!PartitionManager.Mount_By_Path("/sdcard", true))
141 return;
Dees_Troy38bd7602012-09-14 13:33:53 -0400142 ui_print("Reflashing recovery to boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500143 Exec_Cmd("htcdumlock recovery noreboot", status);
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 ui_print("Recovery is flashed to boot.\n");
145}
Dees_Troy43d8b002012-09-17 16:00:01 -0400146
147int TWFunc::Recursive_Mkdir(string Path) {
148 string pathCpy = Path;
149 string wholePath;
150 size_t pos = pathCpy.find("/", 2);
151
152 while (pos != string::npos)
153 {
154 wholePath = pathCpy.substr(0, pos);
155 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
156 LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
157 return false;
158 }
159
160 pos = pathCpy.find("/", pos + 1);
161 }
162 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
163 return false;
164 return true;
165}
166
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100167unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400168 DIR* d;
169 struct dirent* de;
170 struct stat st;
Dees_Troy43d8b002012-09-17 16:00:01 -0400171 unsigned long long dusize = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500172 unsigned long long dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400173
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100174 d = opendir(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400175 if (d == NULL)
176 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100177 LOGE("error opening '%s'\n", Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500178 LOGE("error: %s\n", strerror(errno));
Dees_Troy43d8b002012-09-17 16:00:01 -0400179 return 0;
180 }
181
182 while ((de = readdir(d)) != NULL)
183 {
184 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
185 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100186 dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500187 dusize += dutemp;
188 dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400189 }
190 else if (de->d_type == DT_REG)
191 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100192 stat((Path + "/" + de->d_name).c_str(), &st);
Dees_Troy43d8b002012-09-17 16:00:01 -0400193 dusize += (unsigned long long)(st.st_size);
194 }
195 }
196 closedir(d);
Dees_Troy43d8b002012-09-17 16:00:01 -0400197 return dusize;
198}
199
200bool TWFunc::Path_Exists(string Path) {
201 // Check to see if the Path exists
Dees_Troy7c2dec82012-09-26 09:49:14 -0400202 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400203 if (stat(Path.c_str(), &st) != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400204 return false;
205 else
206 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400207}
208
209void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
210 string Display_Text;
211
212 DataManager::GetValue(Read_Value, Display_Text);
213 if (Display_Text.empty())
214 Display_Text = Default_Text;
215
216 DataManager::SetValue("tw_operation", Display_Text);
217 DataManager::SetValue("tw_partition", "");
218}
219
220void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
221 string Display_Text;
222
223 DataManager::GetValue(Read_Value, Display_Text);
224 if (Display_Text.empty())
225 Display_Text = Default_Text;
226
227 DataManager::SetValue("tw_operation", Display_Text);
228 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400229}
230
231unsigned long TWFunc::Get_File_Size(string Path) {
232 struct stat st;
233
234 if (stat(Path.c_str(), &st) != 0)
235 return 0;
236 return st.st_size;
Dees_Troya58bead2012-09-27 09:49:29 -0400237}
238
239static const char *COMMAND_FILE = "/cache/recovery/command";
240static const char *INTENT_FILE = "/cache/recovery/intent";
241static const char *LOG_FILE = "/cache/recovery/log";
242static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
243static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
244static const char *CACHE_ROOT = "/cache";
245static const char *SDCARD_ROOT = "/sdcard";
246static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
247static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
248
249// close a file, log an error if the error indicator is set
250void TWFunc::check_and_fclose(FILE *fp, const char *name) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600251 fflush(fp);
252 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
253 fclose(fp);
Dees_Troya58bead2012-09-27 09:49:29 -0400254}
255
256void TWFunc::copy_log_file(const char* source, const char* destination, int append) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600257 FILE *log = fopen_path(destination, append ? "a" : "w");
258 if (log == NULL) {
259 LOGE("Can't open %s\n", destination);
260 } else {
261 FILE *tmplog = fopen(source, "r");
262 if (tmplog != NULL) {
263 if (append) {
264 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
265 }
266 char buf[4096];
267 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
268 if (append) {
269 tmplog_offset = ftell(tmplog);
270 }
271 check_and_fclose(tmplog, source);
272 }
273 check_and_fclose(log, destination);
274 }
Dees_Troya58bead2012-09-27 09:49:29 -0400275}
276
277// clear the recovery command and prepare to boot a (hopefully working) system,
278// copy our log file to cache as well (for the system to read), and
279// record any intent we were asked to communicate back to the system.
280// this function is idempotent: call it as many times as you like.
281void TWFunc::twfinish_recovery(const char *send_intent) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282 // By this point, we're ready to return to the main system...
283 if (send_intent != NULL) {
284 FILE *fp = fopen_path(INTENT_FILE, "w");
285 if (fp == NULL) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600286 LOGE("Can't open %s\n", INTENT_FILE);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500287 } else {
Dees_Troy6ef66352013-02-21 08:26:57 -0600288 fputs(send_intent, fp);
289 check_and_fclose(fp, INTENT_FILE);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500290 }
291 }
Dees_Troya58bead2012-09-27 09:49:29 -0400292
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500293 // Copy logs to cache so the system can find out what happened.
294 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
295 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
296 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
297 chmod(LOG_FILE, 0600);
298 chown(LOG_FILE, 1000, 1000); // system user
299 chmod(LAST_LOG_FILE, 0640);
300 chmod(LAST_INSTALL_FILE, 0644);
Dees_Troya58bead2012-09-27 09:49:29 -0400301
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500302 // Reset to normal system boot so recovery won't cycle indefinitely.
303 struct bootloader_message boot;
304 memset(&boot, 0, sizeof(boot));
305 set_bootloader_message(&boot);
Dees_Troya58bead2012-09-27 09:49:29 -0400306
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500307 // Remove the command file, so recovery won't repeat indefinitely.
308 if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600309 LOGW("Can't unlink %s\n", COMMAND_FILE);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500310 }
Dees_Troya58bead2012-09-27 09:49:29 -0400311
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500312 PartitionManager.UnMount_By_Path("/cache", true);
313 sync(); // For good measure.
Dees_Troya58bead2012-09-27 09:49:29 -0400314}
315
316// reboot: Reboot the system. Return -1 on error, no return on success
317int TWFunc::tw_reboot(RebootCommand command)
318{
319 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600320 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400321
Dees_Troy6ef66352013-02-21 08:26:57 -0600322 switch (command)
323 {
324 case rb_current:
325 case rb_system:
326 twfinish_recovery("s");
Dees_Troya58bead2012-09-27 09:49:29 -0400327 sync();
328 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
Dees_Troy6ef66352013-02-21 08:26:57 -0600329 return reboot(RB_AUTOBOOT);
330 case rb_recovery:
Dees_Troya58bead2012-09-27 09:49:29 -0400331 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
Dees_Troy6ef66352013-02-21 08:26:57 -0600332 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
333 case rb_bootloader:
Dees_Troya58bead2012-09-27 09:49:29 -0400334 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
Dees_Troy6ef66352013-02-21 08:26:57 -0600335 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
336 case rb_poweroff:
Dees_Troya58bead2012-09-27 09:49:29 -0400337 check_and_run_script("/sbin/poweroff.sh", "power off");
Dees_Troya4438782013-02-22 18:44:00 +0000338#ifdef ANDROID_RB_POWEROFF
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500339 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
Dees_Troya4438782013-02-22 18:44:00 +0000340#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600341 return reboot(RB_POWER_OFF);
342 case rb_download:
Dees_Troya58bead2012-09-27 09:49:29 -0400343 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
344 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
345 return 1;
Dees_Troy6ef66352013-02-21 08:26:57 -0600346 default:
347 return -1;
348 }
349 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400350}
351
352void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
353{
354 // Check for and run startup script if script exists
355 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500356 string result;
Dees_Troya58bead2012-09-27 09:49:29 -0400357 if (stat(script_file, &st) == 0) {
358 ui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500359 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
360 TWFunc::Exec_Cmd(script_file, result);
Dees_Troya58bead2012-09-27 09:49:29 -0400361 ui_print("\nFinished running %s script.\n", display_name);
362 }
Dees_Troy3477d712012-09-27 15:44:01 -0400363}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500364
365int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000366 DIR *d = opendir(path.c_str());
367 int r = 0;
368 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500369
Dees_Troyce675462013-01-09 19:48:21 +0000370 if (d == NULL) {
371 LOGE("Error opening '%s'\n", path.c_str());
372 return -1;
373 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500374
Dees_Troyce675462013-01-09 19:48:21 +0000375 if (d) {
376 struct dirent *p;
377 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000378 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
379 continue;
380 new_path = path + "/";
381 new_path.append(p->d_name);
382 if (p->d_type == DT_DIR) {
383 r = removeDir(new_path, true);
384 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500385 if (p->d_type == DT_DIR)
386 r = rmdir(new_path.c_str());
387 else
388 LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
389 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500390 } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +0000391 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000392 if (r != 0) {
393 LOGI("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
394 }
Dees_Troyce675462013-01-09 19:48:21 +0000395 }
396 }
397 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500398
399 if (!r) {
400 if (skipParent)
401 return 0;
402 else
403 r = rmdir(path.c_str());
404 }
Dees_Troyce675462013-01-09 19:48:21 +0000405 }
406 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500407}
408
409int TWFunc::copy_file(string src, string dst, int mode) {
410 LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str());
411 ifstream srcfile(src.c_str(), ios::binary);
412 ofstream dstfile(dst.c_str(), ios::binary);
413 dstfile << srcfile.rdbuf();
414 srcfile.close();
415 dstfile.close();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500416 if (chmod(dst.c_str(), mode) != 0)
417 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500418 return 0;
419}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000420
421unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
422 struct stat st;
423
424 stat(Path.c_str(), &st);
425 if (st.st_mode & S_IFDIR)
426 return DT_DIR;
427 else if (st.st_mode & S_IFBLK)
428 return DT_BLK;
429 else if (st.st_mode & S_IFCHR)
430 return DT_CHR;
431 else if (st.st_mode & S_IFIFO)
432 return DT_FIFO;
433 else if (st.st_mode & S_IFLNK)
434 return DT_LNK;
435 else if (st.st_mode & S_IFREG)
436 return DT_REG;
437 else if (st.st_mode & S_IFSOCK)
438 return DT_SOCK;
439 return DT_UNKNOWN;
Dees_Troye34c1332013-02-06 19:13:00 +0000440}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500441
442int TWFunc::read_file(string fn, string& results) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500443 ifstream file;
444 file.open(fn.c_str(), ios::in);
445 if (file.is_open()) {
446 file >> results;
447 file.close();
448 return 0;
449 }
450 LOGI("Cannot find file %s\n", fn.c_str());
451 return -1;
452}
453
454int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500455 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500456 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500457 file.open(fn.c_str(), ios::in);
458 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500459 while (getline(file, line))
460 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500461 file.close();
462 return 0;
463 }
464 LOGI("Cannot find file %s\n", fn.c_str());
465 return -1;
466}
467
468int TWFunc::write_file(string fn, string& line) {
469 FILE *file;
470 file = fopen(fn.c_str(), "w");
471 if (file != NULL) {
472 fwrite(line.c_str(), line.size(), 1, file);
473 fclose(file);
474 return 0;
475 }
476 LOGI("Cannot find file %s\n", fn.c_str());
477 return -1;
478}
479
480timespec TWFunc::timespec_diff(timespec& start, timespec& end)
481{
Dees_Troy6ef66352013-02-21 08:26:57 -0600482 timespec temp;
483 if ((end.tv_nsec-start.tv_nsec)<0) {
484 temp.tv_sec = end.tv_sec-start.tv_sec-1;
485 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
486 } else {
487 temp.tv_sec = end.tv_sec-start.tv_sec;
488 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
489 }
490 return temp;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500491}
492
493 int TWFunc::drop_caches(void) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600494 string file = "/proc/sys/vm/drop_caches";
495 string value = "3";
496 if (write_file(file, value) != 0)
497 return -1;
498 return 0;
499}
500
501int TWFunc::Check_su_Perms(void) {
502 struct stat st;
503 int ret = 0;
504
505 if (!PartitionManager.Mount_By_Path("/system", false))
506 return 0;
507
508 // Check to ensure that perms are 6755 for all 3 file locations
509 if (stat("/system/bin/su", &st) == 0) {
510 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
511 ret = 1;
512 }
513 }
514 if (stat("/system/xbin/su", &st) == 0) {
515 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
516 ret += 2;
517 }
518 }
519 if (stat("/system/bin/.ext/.su", &st) == 0) {
520 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
521 ret += 4;
522 }
523 }
524 return ret;
525}
526
527bool TWFunc::Fix_su_Perms(void) {
528 if (!PartitionManager.Mount_By_Path("/system", true))
529 return false;
530
531 string file = "/system/bin/su";
532 if (TWFunc::Path_Exists(file)) {
533 if (chown(file.c_str(), 0, 0) != 0) {
534 LOGE("Failed to chown '%s'\n", file.c_str());
535 return false;
536 }
537 if (tw_chmod(file, "6755") != 0) {
538 LOGE("Failed to chmod '%s'\n", file.c_str());
539 return false;
540 }
541 }
542 file = "/system/xbin/su";
543 if (TWFunc::Path_Exists(file)) {
544 if (chown(file.c_str(), 0, 0) != 0) {
545 LOGE("Failed to chown '%s'\n", file.c_str());
546 return false;
547 }
548 if (tw_chmod(file, "6755") != 0) {
549 LOGE("Failed to chmod '%s'\n", file.c_str());
550 return false;
551 }
552 }
553 file = "/system/bin/.ext/.su";
554 if (TWFunc::Path_Exists(file)) {
555 if (chown(file.c_str(), 0, 0) != 0) {
556 LOGE("Failed to chown '%s'\n", file.c_str());
557 return false;
558 }
559 if (tw_chmod(file, "6755") != 0) {
560 LOGE("Failed to chmod '%s'\n", file.c_str());
561 return false;
562 }
563 }
564 file = "/system/app/Superuser.apk";
565 if (TWFunc::Path_Exists(file)) {
566 if (chown(file.c_str(), 0, 0) != 0) {
567 LOGE("Failed to chown '%s'\n", file.c_str());
568 return false;
569 }
570 if (tw_chmod(file, "0644") != 0) {
571 LOGE("Failed to chmod '%s'\n", file.c_str());
572 return false;
573 }
574 }
575 sync();
576 if (!PartitionManager.UnMount_By_Path("/system", true))
577 return false;
578 return true;
579}
580
581int TWFunc::tw_chmod(string fn, string mode) {
582 long mask = 0;
583
584 for ( std::string::size_type n = 0; n < mode.length(); ++n) {
585 if (n == 0) {
586 if (mode[n] == '0')
587 continue;
588 if (mode[n] == '1')
589 mask |= S_ISVTX;
590 if (mode[n] == '2')
591 mask |= S_ISGID;
592 if (mode[n] == '4')
593 mask |= S_ISUID;
594 if (mode[n] == '5') {
595 mask |= S_ISVTX;
596 mask |= S_ISUID;
597 }
598 if (mode[n] == '6') {
599 mask |= S_ISGID;
600 mask |= S_ISUID;
601 }
602 if (mode[n] == '7') {
603 mask |= S_ISVTX;
604 mask |= S_ISGID;
605 mask |= S_ISUID;
606 }
607 }
608 else if (n == 1) {
609 if (mode[n] == '7') {
610 mask |= S_IRWXU;
611 }
612 if (mode[n] == '6') {
613 mask |= S_IRUSR;
614 mask |= S_IWUSR;
615 }
616 if (mode[n] == '5') {
617 mask |= S_IRUSR;
618 mask |= S_IXUSR;
619 }
620 if (mode[n] == '4')
621 mask |= S_IRUSR;
622 if (mode[n] == '3') {
623 mask |= S_IWUSR;
624 mask |= S_IRUSR;
625 }
626 if (mode[n] == '2')
627 mask |= S_IWUSR;
628 if (mode[n] == '1')
629 mask |= S_IXUSR;
630 }
631 else if (n == 2) {
632 if (mode[n] == '7') {
633 mask |= S_IRWXG;
634 }
635 if (mode[n] == '6') {
636 mask |= S_IRGRP;
637 mask |= S_IWGRP;
638 }
639 if (mode[n] == '5') {
640 mask |= S_IRGRP;
641 mask |= S_IXGRP;
642 }
643 if (mode[n] == '4')
644 mask |= S_IRGRP;
645 if (mode[n] == '3') {
646 mask |= S_IWGRP;
647 mask |= S_IXGRP;
648 }
649 if (mode[n] == '2')
650 mask |= S_IWGRP;
651 if (mode[n] == '1')
652 mask |= S_IXGRP;
653 }
654 else if (n == 3) {
655 if (mode[n] == '7') {
656 mask |= S_IRWXO;
657 }
658 if (mode[n] == '6') {
659 mask |= S_IROTH;
660 mask |= S_IWOTH;
661 }
662 if (mode[n] == '5') {
663 mask |= S_IROTH;
664 mask |= S_IXOTH;
665 }
666 if (mode[n] == '4')
667 mask |= S_IROTH;
668 if (mode[n] == '3') {
669 mask |= S_IWOTH;
670 mask |= S_IXOTH;
671 }
672 if (mode[n] == '2')
673 mask |= S_IWOTH;
674 if (mode[n] == '1')
675 mask |= S_IXOTH;
676 }
677 }
678
679 if (chmod(fn.c_str(), mask) != 0) {
680 LOGE("Unable to chmod '%s' %l\n", fn.c_str(), mask);
681 return -1;
682 }
683
684 return 0;
685}
686
687bool TWFunc::Install_SuperSU(void) {
688 if (!PartitionManager.Mount_By_Path("/system", true))
689 return false;
690
jt1134113ee732013-02-22 23:26:10 -0600691 if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600692 LOGE("Failed to copy su binary to /system/bin\n");
693 return false;
694 }
jt1134113ee732013-02-22 23:26:10 -0600695 if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600696 LOGE("Failed to copy Superuser app to /system/app\n");
697 return false;
698 }
699 if (!Fix_su_Perms())
700 return false;
701 return true;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500702}