blob: a0194b3796f8cec380e2afbce069432a7fcfd3cb [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"
Dees_Troy2673cec2013-04-02 20:22:16 +000022#include "twcommon.h"
Dees_Troyb46a6842012-09-25 11:06:46 -040023#include "data.hpp"
Dees_Troy3477d712012-09-27 15:44:01 -040024#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000025#include "bootloader.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/* Execute a command */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032int TWFunc::Exec_Cmd(string cmd, string &result) {
33 FILE* exec;
Dees_Troyb05ddee2013-01-28 20:24:50 +000034 char buffer[130];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035 int ret = 0;
Dees_Troyb05ddee2013-01-28 20:24:50 +000036 exec = __popen(cmd.c_str(), "r");
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037 if (!exec) return -1;
38 while(!feof(exec)) {
Dees_Troyb05ddee2013-01-28 20:24:50 +000039 memset(&buffer, 0, sizeof(buffer));
40 if (fgets(buffer, 128, exec) != NULL) {
41 buffer[128] = '\n';
42 buffer[129] = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043 result += buffer;
Dees_Troyb05ddee2013-01-28 20:24:50 +000044 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045 }
Dees_Troyb05ddee2013-01-28 20:24:50 +000046 ret = __pclose(exec);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050047 return ret;
48}
49
Dees_Troy38bd7602012-09-14 13:33:53 -040050// Returns "file.name" from a full /path/to/file.name
51string TWFunc::Get_Filename(string Path) {
52 size_t pos = Path.find_last_of("/");
53 if (pos != string::npos) {
54 string Filename;
55 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
56 return Filename;
57 } else
58 return Path;
59}
60
61// Returns "/path/to/" from a full /path/to/file.name
62string TWFunc::Get_Path(string Path) {
63 size_t pos = Path.find_last_of("/");
64 if (pos != string::npos) {
65 string Pathonly;
66 Pathonly = Path.substr(0, pos + 1);
67 return Pathonly;
68 } else
69 return Path;
70}
71
72// Returns "/path" from a full /path/to/file.name
73string TWFunc::Get_Root_Path(string Path) {
74 string Local_Path = Path;
75
76 // Make sure that we have a leading slash
77 if (Local_Path.substr(0, 1) != "/")
78 Local_Path = "/" + Local_Path;
79
80 // Trim the path to get the root path only
81 size_t position = Local_Path.find("/", 2);
82 if (position != string::npos) {
83 Local_Path.resize(position);
84 }
85 return Local_Path;
86}
87
88void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -040089 int need_libs = 0;
90
91 if (!PartitionManager.Mount_By_Path("/system", true))
92 return;
93
94 if (!PartitionManager.Mount_By_Path("/data", true))
95 return;
96
Dees_Troy2673cec2013-04-02 20:22:16 +000097 gui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -050098 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -040099 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000100 gui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500101 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400102 need_libs = 1;
103 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000104 gui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400105 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000106 gui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500107 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400108 need_libs = 1;
109 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000110 gui_print("dump_image is already installed, skipping...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400111 if (need_libs) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000112 gui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500113 copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
114 copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
115 copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
116 copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400117 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 gui_print("Installing HTC Dumlock app...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500120 unlink("/data/app/com.teamwin.htcdumlock*");
121 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400122 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000123 gui_print("HTC Dumlock is installed.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400124}
125
126void TWFunc::htc_dumlock_restore_original_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500127 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400128 if (!PartitionManager.Mount_By_Path("/sdcard", true))
129 return;
130
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 gui_print("Restoring original boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500132 Exec_Cmd("htcdumlock restore", status);
Dees_Troy2673cec2013-04-02 20:22:16 +0000133 gui_print("Original boot restored.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400134}
135
136void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500137 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400138 if (!PartitionManager.Mount_By_Path("/sdcard", true))
139 return;
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 gui_print("Reflashing recovery to boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500141 Exec_Cmd("htcdumlock recovery noreboot", status);
Dees_Troy2673cec2013-04-02 20:22:16 +0000142 gui_print("Recovery is flashed to boot.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400143}
Dees_Troy43d8b002012-09-17 16:00:01 -0400144
145int TWFunc::Recursive_Mkdir(string Path) {
146 string pathCpy = Path;
147 string wholePath;
148 size_t pos = pathCpy.find("/", 2);
149
150 while (pos != string::npos)
151 {
152 wholePath = pathCpy.substr(0, pos);
153 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000154 LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
Dees_Troy43d8b002012-09-17 16:00:01 -0400155 return false;
156 }
157
158 pos = pathCpy.find("/", pos + 1);
159 }
160 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
161 return false;
162 return true;
163}
164
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100165unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400166 DIR* d;
167 struct dirent* de;
168 struct stat st;
Dees_Troy43d8b002012-09-17 16:00:01 -0400169 unsigned long long dusize = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500170 unsigned long long dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400171
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100172 d = opendir(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400173 if (d == NULL)
174 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000175 LOGERR("error opening '%s'\n", Path.c_str());
176 LOGERR("error: %s\n", strerror(errno));
Dees_Troy43d8b002012-09-17 16:00:01 -0400177 return 0;
178 }
179
180 while ((de = readdir(d)) != NULL)
181 {
182 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
183 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100184 dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500185 dusize += dutemp;
186 dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400187 }
188 else if (de->d_type == DT_REG)
189 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100190 stat((Path + "/" + de->d_name).c_str(), &st);
Dees_Troy43d8b002012-09-17 16:00:01 -0400191 dusize += (unsigned long long)(st.st_size);
192 }
193 }
194 closedir(d);
Dees_Troy43d8b002012-09-17 16:00:01 -0400195 return dusize;
196}
197
198bool TWFunc::Path_Exists(string Path) {
199 // Check to see if the Path exists
Dees_Troy7c2dec82012-09-26 09:49:14 -0400200 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400201 if (stat(Path.c_str(), &st) != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400202 return false;
203 else
204 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400205}
206
207void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
208 string Display_Text;
209
210 DataManager::GetValue(Read_Value, Display_Text);
211 if (Display_Text.empty())
212 Display_Text = Default_Text;
213
214 DataManager::SetValue("tw_operation", Display_Text);
215 DataManager::SetValue("tw_partition", "");
216}
217
218void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
219 string Display_Text;
220
221 DataManager::GetValue(Read_Value, Display_Text);
222 if (Display_Text.empty())
223 Display_Text = Default_Text;
224
225 DataManager::SetValue("tw_operation", Display_Text);
226 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400227}
228
229unsigned long TWFunc::Get_File_Size(string Path) {
230 struct stat st;
231
232 if (stat(Path.c_str(), &st) != 0)
233 return 0;
234 return st.st_size;
Dees_Troya58bead2012-09-27 09:49:29 -0400235}
236
Dees_Troy2673cec2013-04-02 20:22:16 +0000237void TWFunc::Copy_Log(string Source, string Destination) {
238 FILE *destination_log = fopen(Destination.c_str(), "a");
239 if (destination_log == NULL) {
240 LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600241 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 FILE *source_log = fopen(Source.c_str(), "r");
243 if (source_log != NULL) {
244 fseek(source_log, Log_Offset, SEEK_SET);
245 char buffer[4096];
246 while (fgets(buffer, sizeof(buffer), source_log))
247 fputs(buffer, destination_log); // Buffered write of log file
248 Log_Offset = ftell(source_log);
249 fflush(source_log);
250 fclose(source_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600251 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 fflush(destination_log);
253 fclose(destination_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600254 }
Dees_Troya58bead2012-09-27 09:49:29 -0400255}
256
Dees_Troy2673cec2013-04-02 20:22:16 +0000257void TWFunc::Update_Log_File(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500258 // Copy logs to cache so the system can find out what happened.
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 Copy_Log(TMP_LOG_FILE, "/cache/recovery/log");
260 copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600);
261 chown("/cache/recovery/log", 1000, 1000);
262 chmod("/cache/recovery/log", 0600);
263 chmod("/cache/recovery/last_log", 0640);
Dees_Troya58bead2012-09-27 09:49:29 -0400264
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 // Reset bootloader message
266 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
267 if (Part != NULL) {
268 struct bootloader_message boot;
269 memset(&boot, 0, sizeof(boot));
270 if (Part->Current_File_System == "mtd") {
271 if (set_bootloader_message_mtd_name(&boot, Part->MTD_Name.c_str()) != 0)
272 LOGERR("Unable to set MTD bootloader message.\n");
273 } else if (Part->Current_File_System == "emmc") {
274 if (set_bootloader_message_block_name(&boot, Part->Actual_Block_Device.c_str()) != 0)
275 LOGERR("Unable to set emmc bootloader message.\n");
276 } else {
277 LOGERR("Unknown file system for /misc: '%s'\n", Part->Current_File_System.c_str());
278 }
279 }
Dees_Troya58bead2012-09-27 09:49:29 -0400280
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 if (!PartitionManager.Mount_By_Path("/cache", true) || (unlink("/cache/recovery/command") && errno != ENOENT)) {
282 LOGINFO("Can't unlink %s\n", "/cache/recovery/command");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500283 }
Dees_Troya58bead2012-09-27 09:49:29 -0400284
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500285 PartitionManager.UnMount_By_Path("/cache", true);
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 sync();
287}
288
289void TWFunc::Update_Intent_File(string Intent) {
290 if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) {
291 TWFunc::write_file("/cache/recovery/intent", Intent);
292 }
Dees_Troya58bead2012-09-27 09:49:29 -0400293}
294
295// reboot: Reboot the system. Return -1 on error, no return on success
296int TWFunc::tw_reboot(RebootCommand command)
297{
298 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600299 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400300
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 switch (command) {
302 case rb_current:
303 case rb_system:
304 Update_Log_File();
305 Update_Intent_File("s");
306 sync();
307 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
308 return reboot(RB_AUTOBOOT);
309 case rb_recovery:
310 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
311 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
312 case rb_bootloader:
313 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
314 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
315 case rb_poweroff:
316 check_and_run_script("/sbin/poweroff.sh", "power off");
Dees_Troya4438782013-02-22 18:44:00 +0000317#ifdef ANDROID_RB_POWEROFF
Dees_Troy2673cec2013-04-02 20:22:16 +0000318 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
Dees_Troya4438782013-02-22 18:44:00 +0000319#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 return reboot(RB_POWER_OFF);
321 case rb_download:
322 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
323 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
324 default:
325 return -1;
Dees_Troy6ef66352013-02-21 08:26:57 -0600326 }
327 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400328}
329
330void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
331{
332 // Check for and run startup script if script exists
333 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 string result;
Dees_Troya58bead2012-09-27 09:49:29 -0400335 if (stat(script_file, &st) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 gui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500337 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
338 TWFunc::Exec_Cmd(script_file, result);
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 gui_print("\nFinished running %s script.\n", display_name);
Dees_Troya58bead2012-09-27 09:49:29 -0400340 }
Dees_Troy3477d712012-09-27 15:44:01 -0400341}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500342
343int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000344 DIR *d = opendir(path.c_str());
345 int r = 0;
346 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500347
Dees_Troyce675462013-01-09 19:48:21 +0000348 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000349 LOGERR("Error opening '%s'\n", path.c_str());
Dees_Troyce675462013-01-09 19:48:21 +0000350 return -1;
351 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500352
Dees_Troyce675462013-01-09 19:48:21 +0000353 if (d) {
354 struct dirent *p;
355 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000356 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
357 continue;
358 new_path = path + "/";
359 new_path.append(p->d_name);
360 if (p->d_type == DT_DIR) {
361 r = removeDir(new_path, true);
362 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500363 if (p->d_type == DT_DIR)
364 r = rmdir(new_path.c_str());
365 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000366 LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500367 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500368 } 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 +0000369 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000370 if (r != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000371 LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
Dees_Troye34c1332013-02-06 19:13:00 +0000372 }
Dees_Troyce675462013-01-09 19:48:21 +0000373 }
374 }
375 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500376
377 if (!r) {
378 if (skipParent)
379 return 0;
380 else
381 r = rmdir(path.c_str());
382 }
Dees_Troyce675462013-01-09 19:48:21 +0000383 }
384 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500385}
386
387int TWFunc::copy_file(string src, string dst, int mode) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500389 ifstream srcfile(src.c_str(), ios::binary);
390 ofstream dstfile(dst.c_str(), ios::binary);
391 dstfile << srcfile.rdbuf();
392 srcfile.close();
393 dstfile.close();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500394 if (chmod(dst.c_str(), mode) != 0)
395 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500396 return 0;
397}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000398
399unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
400 struct stat st;
401
402 stat(Path.c_str(), &st);
403 if (st.st_mode & S_IFDIR)
404 return DT_DIR;
405 else if (st.st_mode & S_IFBLK)
406 return DT_BLK;
407 else if (st.st_mode & S_IFCHR)
408 return DT_CHR;
409 else if (st.st_mode & S_IFIFO)
410 return DT_FIFO;
411 else if (st.st_mode & S_IFLNK)
412 return DT_LNK;
413 else if (st.st_mode & S_IFREG)
414 return DT_REG;
415 else if (st.st_mode & S_IFSOCK)
416 return DT_SOCK;
417 return DT_UNKNOWN;
Dees_Troye34c1332013-02-06 19:13:00 +0000418}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500419
420int TWFunc::read_file(string fn, string& results) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500421 ifstream file;
422 file.open(fn.c_str(), ios::in);
423 if (file.is_open()) {
424 file >> results;
425 file.close();
426 return 0;
427 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000428 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500429 return -1;
430}
431
432int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500433 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500434 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500435 file.open(fn.c_str(), ios::in);
436 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500437 while (getline(file, line))
438 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500439 file.close();
440 return 0;
441 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500443 return -1;
444}
445
446int TWFunc::write_file(string fn, string& line) {
447 FILE *file;
448 file = fopen(fn.c_str(), "w");
449 if (file != NULL) {
450 fwrite(line.c_str(), line.size(), 1, file);
451 fclose(file);
452 return 0;
453 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000454 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500455 return -1;
456}
457
458timespec TWFunc::timespec_diff(timespec& start, timespec& end)
459{
Dees_Troy6ef66352013-02-21 08:26:57 -0600460 timespec temp;
461 if ((end.tv_nsec-start.tv_nsec)<0) {
462 temp.tv_sec = end.tv_sec-start.tv_sec-1;
463 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
464 } else {
465 temp.tv_sec = end.tv_sec-start.tv_sec;
466 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
467 }
468 return temp;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500469}
470
471 int TWFunc::drop_caches(void) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600472 string file = "/proc/sys/vm/drop_caches";
473 string value = "3";
474 if (write_file(file, value) != 0)
475 return -1;
476 return 0;
477}
478
479int TWFunc::Check_su_Perms(void) {
480 struct stat st;
481 int ret = 0;
482
483 if (!PartitionManager.Mount_By_Path("/system", false))
484 return 0;
485
486 // Check to ensure that perms are 6755 for all 3 file locations
487 if (stat("/system/bin/su", &st) == 0) {
488 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) {
489 ret = 1;
490 }
491 }
492 if (stat("/system/xbin/su", &st) == 0) {
493 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) {
494 ret += 2;
495 }
496 }
497 if (stat("/system/bin/.ext/.su", &st) == 0) {
498 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) {
499 ret += 4;
500 }
501 }
502 return ret;
503}
504
505bool TWFunc::Fix_su_Perms(void) {
506 if (!PartitionManager.Mount_By_Path("/system", true))
507 return false;
508
509 string file = "/system/bin/su";
510 if (TWFunc::Path_Exists(file)) {
511 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600513 return false;
514 }
515 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000516 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600517 return false;
518 }
519 }
520 file = "/system/xbin/su";
521 if (TWFunc::Path_Exists(file)) {
522 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600524 return false;
525 }
526 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000527 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600528 return false;
529 }
530 }
531 file = "/system/bin/.ext/.su";
532 if (TWFunc::Path_Exists(file)) {
533 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000534 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600535 return false;
536 }
537 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000538 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600539 return false;
540 }
541 }
542 file = "/system/app/Superuser.apk";
543 if (TWFunc::Path_Exists(file)) {
544 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000545 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600546 return false;
547 }
548 if (tw_chmod(file, "0644") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000549 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600550 return false;
551 }
552 }
553 sync();
554 if (!PartitionManager.UnMount_By_Path("/system", true))
555 return false;
556 return true;
557}
558
559int TWFunc::tw_chmod(string fn, string mode) {
560 long mask = 0;
561
562 for ( std::string::size_type n = 0; n < mode.length(); ++n) {
563 if (n == 0) {
564 if (mode[n] == '0')
565 continue;
566 if (mode[n] == '1')
567 mask |= S_ISVTX;
568 if (mode[n] == '2')
569 mask |= S_ISGID;
570 if (mode[n] == '4')
571 mask |= S_ISUID;
572 if (mode[n] == '5') {
573 mask |= S_ISVTX;
574 mask |= S_ISUID;
575 }
576 if (mode[n] == '6') {
577 mask |= S_ISGID;
578 mask |= S_ISUID;
579 }
580 if (mode[n] == '7') {
581 mask |= S_ISVTX;
582 mask |= S_ISGID;
583 mask |= S_ISUID;
584 }
585 }
586 else if (n == 1) {
587 if (mode[n] == '7') {
588 mask |= S_IRWXU;
589 }
590 if (mode[n] == '6') {
591 mask |= S_IRUSR;
592 mask |= S_IWUSR;
593 }
594 if (mode[n] == '5') {
595 mask |= S_IRUSR;
596 mask |= S_IXUSR;
597 }
598 if (mode[n] == '4')
599 mask |= S_IRUSR;
600 if (mode[n] == '3') {
601 mask |= S_IWUSR;
602 mask |= S_IRUSR;
603 }
604 if (mode[n] == '2')
605 mask |= S_IWUSR;
606 if (mode[n] == '1')
607 mask |= S_IXUSR;
608 }
609 else if (n == 2) {
610 if (mode[n] == '7') {
611 mask |= S_IRWXG;
612 }
613 if (mode[n] == '6') {
614 mask |= S_IRGRP;
615 mask |= S_IWGRP;
616 }
617 if (mode[n] == '5') {
618 mask |= S_IRGRP;
619 mask |= S_IXGRP;
620 }
621 if (mode[n] == '4')
622 mask |= S_IRGRP;
623 if (mode[n] == '3') {
624 mask |= S_IWGRP;
625 mask |= S_IXGRP;
626 }
627 if (mode[n] == '2')
628 mask |= S_IWGRP;
629 if (mode[n] == '1')
630 mask |= S_IXGRP;
631 }
632 else if (n == 3) {
633 if (mode[n] == '7') {
634 mask |= S_IRWXO;
635 }
636 if (mode[n] == '6') {
637 mask |= S_IROTH;
638 mask |= S_IWOTH;
639 }
640 if (mode[n] == '5') {
641 mask |= S_IROTH;
642 mask |= S_IXOTH;
643 }
644 if (mode[n] == '4')
645 mask |= S_IROTH;
646 if (mode[n] == '3') {
647 mask |= S_IWOTH;
648 mask |= S_IXOTH;
649 }
650 if (mode[n] == '2')
651 mask |= S_IWOTH;
652 if (mode[n] == '1')
653 mask |= S_IXOTH;
654 }
655 }
656
657 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
Dees_Troy6ef66352013-02-21 08:26:57 -0600659 return -1;
660 }
661
662 return 0;
663}
664
665bool TWFunc::Install_SuperSU(void) {
666 if (!PartitionManager.Mount_By_Path("/system", true))
667 return false;
668
jt1134113ee732013-02-22 23:26:10 -0600669 if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGERR("Failed to copy su binary to /system/bin\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600671 return false;
672 }
jt1134113ee732013-02-22 23:26:10 -0600673 if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGERR("Failed to copy Superuser app to /system/app\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600675 return false;
676 }
677 if (!Fix_su_Perms())
678 return false;
679 return true;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500680}