Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <vector> |
| 6 | #include <dirent.h> |
| 7 | #include <time.h> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 8 | #include <errno.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <sys/mount.h> |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 11 | #include <sys/reboot.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 12 | #include <sys/sendfile.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/vfs.h> |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 15 | #ifdef ANDROID_RB_POWEROFF |
| 16 | #include "cutils/android_reboot.h" |
| 17 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 18 | #include <iostream> |
| 19 | #include <fstream> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 20 | #include "twrp-functions.hpp" |
| 21 | #include "partitions.hpp" |
| 22 | #include "common.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 23 | #include "data.hpp" |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 24 | #include "bootloader.h" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 25 | #include "variables.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 26 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 27 | extern "C" { |
| 28 | #include "libcrecovery/common.h" |
| 29 | } |
| 30 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | |
| 32 | /* Execute a command */ |
| 33 | |
| 34 | int TWFunc::Exec_Cmd(string cmd, string &result) { |
| 35 | FILE* exec; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 36 | char buffer[130]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | int ret = 0; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 38 | exec = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 39 | if (!exec) return -1; |
| 40 | while(!feof(exec)) { |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 41 | memset(&buffer, 0, sizeof(buffer)); |
| 42 | if (fgets(buffer, 128, exec) != NULL) { |
| 43 | buffer[128] = '\n'; |
| 44 | buffer[129] = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 46 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 47 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 48 | ret = __pclose(exec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 49 | return ret; |
| 50 | } |
| 51 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 52 | // Returns "file.name" from a full /path/to/file.name |
| 53 | string 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 |
| 64 | string 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 |
| 75 | string 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 | |
| 90 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 91 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 100 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 101 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 102 | ui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 103 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 104 | need_libs = 1; |
| 105 | } else |
| 106 | ui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 107 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 108 | ui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 109 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 110 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 115 | 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_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 119 | } |
| 120 | ui_print("Installing HTC Dumlock app...\n"); |
| 121 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 122 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 123 | copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 124 | sync(); |
| 125 | ui_print("HTC Dumlock is installed.\n"); |
| 126 | } |
| 127 | |
| 128 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 129 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 130 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 131 | return; |
| 132 | |
| 133 | ui_print("Restoring original boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 134 | Exec_Cmd("htcdumlock restore", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 135 | ui_print("Original boot restored.\n"); |
| 136 | } |
| 137 | |
| 138 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 139 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 140 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 141 | return; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 142 | ui_print("Reflashing recovery to boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 143 | Exec_Cmd("htcdumlock recovery noreboot", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 144 | ui_print("Recovery is flashed to boot.\n"); |
| 145 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 146 | |
| 147 | int 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 Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 167 | unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 168 | DIR* d; |
| 169 | struct dirent* de; |
| 170 | struct stat st; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 171 | unsigned long long dusize = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 172 | unsigned long long dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 173 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 174 | d = opendir(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 175 | if (d == NULL) |
| 176 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 177 | LOGE("error opening '%s'\n", Path.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 178 | LOGE("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 179 | 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 Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 186 | dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 187 | dusize += dutemp; |
| 188 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 189 | } |
| 190 | else if (de->d_type == DT_REG) |
| 191 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 192 | stat((Path + "/" + de->d_name).c_str(), &st); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 193 | dusize += (unsigned long long)(st.st_size); |
| 194 | } |
| 195 | } |
| 196 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 197 | return dusize; |
| 198 | } |
| 199 | |
| 200 | bool TWFunc::Path_Exists(string Path) { |
| 201 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 202 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 203 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 204 | return false; |
| 205 | else |
| 206 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void 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 | |
| 220 | void 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_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | unsigned 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 240 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 241 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 242 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 243 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 244 | static const char *CACHE_ROOT = "/cache"; |
| 245 | static const char *SDCARD_ROOT = "/sdcard"; |
| 246 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 247 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
| 248 | |
| 249 | // close a file, log an error if the error indicator is set |
| 250 | void TWFunc::check_and_fclose(FILE *fp, const char *name) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 251 | fflush(fp); |
| 252 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 253 | fclose(fp); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void TWFunc::copy_log_file(const char* source, const char* destination, int append) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 257 | 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 275 | } |
| 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. |
| 281 | void TWFunc::twfinish_recovery(const char *send_intent) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 282 | // 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 286 | LOGE("Can't open %s\n", INTENT_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 287 | } else { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 288 | fputs(send_intent, fp); |
| 289 | check_and_fclose(fp, INTENT_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 290 | } |
| 291 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 292 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 293 | // 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 301 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 302 | // 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 306 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 307 | // Remove the command file, so recovery won't repeat indefinitely. |
| 308 | if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 309 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 310 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 311 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 312 | PartitionManager.UnMount_By_Path("/cache", true); |
| 313 | sync(); // For good measure. |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 317 | int TWFunc::tw_reboot(RebootCommand command) |
| 318 | { |
| 319 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 320 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 321 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 322 | switch (command) |
| 323 | { |
| 324 | case rb_current: |
| 325 | case rb_system: |
| 326 | twfinish_recovery("s"); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 327 | sync(); |
| 328 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 329 | return reboot(RB_AUTOBOOT); |
| 330 | case rb_recovery: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 331 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 332 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 333 | case rb_bootloader: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 334 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 335 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 336 | case rb_poweroff: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 337 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 338 | #ifdef ANDROID_RB_POWEROFF |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 339 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 340 | #endif |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 341 | return reboot(RB_POWER_OFF); |
| 342 | case rb_download: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 343 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 346 | default: |
| 347 | return -1; |
| 348 | } |
| 349 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 356 | string result; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 357 | if (stat(script_file, &st) == 0) { |
| 358 | ui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 359 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 360 | TWFunc::Exec_Cmd(script_file, result); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 361 | ui_print("\nFinished running %s script.\n", display_name); |
| 362 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 363 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 364 | |
| 365 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 366 | DIR *d = opendir(path.c_str()); |
| 367 | int r = 0; |
| 368 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 369 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 370 | if (d == NULL) { |
| 371 | LOGE("Error opening '%s'\n", path.c_str()); |
| 372 | return -1; |
| 373 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 374 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 375 | if (d) { |
| 376 | struct dirent *p; |
| 377 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 378 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 385 | 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 bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 390 | } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 391 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 392 | if (r != 0) { |
| 393 | LOGI("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
| 394 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 398 | |
| 399 | if (!r) { |
| 400 | if (skipParent) |
| 401 | return 0; |
| 402 | else |
| 403 | r = rmdir(path.c_str()); |
| 404 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 405 | } |
| 406 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | int 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 416 | if (chmod(dst.c_str(), mode) != 0) |
| 417 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 418 | return 0; |
| 419 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 420 | |
| 421 | unsigned 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_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 440 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 441 | |
| 442 | int TWFunc::read_file(string fn, string& results) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 443 | 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 | |
| 454 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 455 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 456 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 457 | file.open(fn.c_str(), ios::in); |
| 458 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 459 | while (getline(file, line)) |
| 460 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 461 | file.close(); |
| 462 | return 0; |
| 463 | } |
| 464 | LOGI("Cannot find file %s\n", fn.c_str()); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | int 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 | |
| 480 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 481 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 482 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | int TWFunc::drop_caches(void) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 494 | 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 | |
| 501 | int 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 | |
| 527 | bool 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 | |
| 581 | int 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 | |
| 687 | bool TWFunc::Install_SuperSU(void) { |
| 688 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 689 | return false; |
| 690 | |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 691 | if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 692 | LOGE("Failed to copy su binary to /system/bin\n"); |
| 693 | return false; |
| 694 | } |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 695 | if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 696 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 702 | } |