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> |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 15 | #include "cutils/android_reboot.h" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 16 | #include <iostream> |
| 17 | #include <fstream> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 18 | #include "twrp-functions.hpp" |
| 19 | #include "partitions.hpp" |
| 20 | #include "common.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 21 | #include "data.hpp" |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 22 | #include "bootloader.h" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 23 | #include "variables.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 24 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 25 | extern "C" { |
| 26 | #include "libcrecovery/common.h" |
| 27 | } |
| 28 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | |
| 30 | /* Execute a command */ |
| 31 | |
| 32 | int TWFunc::Exec_Cmd(string cmd, string &result) { |
| 33 | FILE* exec; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 34 | char buffer[130]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | int ret = 0; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 36 | exec = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | if (!exec) return -1; |
| 38 | while(!feof(exec)) { |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 39 | memset(&buffer, 0, sizeof(buffer)); |
| 40 | if (fgets(buffer, 128, exec) != NULL) { |
| 41 | buffer[128] = '\n'; |
| 42 | buffer[129] = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 43 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 44 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 46 | ret = __pclose(exec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 47 | return ret; |
| 48 | } |
| 49 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 50 | /* Checks md5 for a path |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 51 | Return values: |
| 52 | -1 : MD5 does not exist |
| 53 | 0 : Failed |
| 54 | 1 : Success */ |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 55 | int TWFunc::Check_MD5(string File) { |
| 56 | int ret; |
| 57 | string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK; |
| 58 | char line[255]; |
| 59 | size_t pos; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 60 | string result; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 61 | |
| 62 | MD5_File = File + ".md5"; |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 63 | if (Path_Exists(MD5_File)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 64 | DirPath = Get_Path(File); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 65 | MD5_File = Get_Filename(MD5_File); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 66 | Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "'"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 67 | Exec_Cmd(Command, result); |
| 68 | pos = result.find(":"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 69 | if (pos != string::npos) { |
| 70 | Filename = Get_Filename(File); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 71 | MD5_File_Filename = result.substr(0, pos); |
| 72 | OK = result.substr(pos + 2, result.size() - pos - 2); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 73 | if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) { |
| 74 | //MD5 is good, return 1 |
| 75 | ret = 1; |
| 76 | } else { |
| 77 | // MD5 is bad, return 0 |
| 78 | ret = 0; |
| 79 | } |
| 80 | } else { |
| 81 | // MD5 is bad, return 0 |
| 82 | ret = 0; |
| 83 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 84 | } else { |
| 85 | //No md5 file, return -1 |
| 86 | ret = -1; |
| 87 | } |
| 88 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 89 | return ret; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Returns "file.name" from a full /path/to/file.name |
| 93 | string TWFunc::Get_Filename(string Path) { |
| 94 | size_t pos = Path.find_last_of("/"); |
| 95 | if (pos != string::npos) { |
| 96 | string Filename; |
| 97 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 98 | return Filename; |
| 99 | } else |
| 100 | return Path; |
| 101 | } |
| 102 | |
| 103 | // Returns "/path/to/" from a full /path/to/file.name |
| 104 | string TWFunc::Get_Path(string Path) { |
| 105 | size_t pos = Path.find_last_of("/"); |
| 106 | if (pos != string::npos) { |
| 107 | string Pathonly; |
| 108 | Pathonly = Path.substr(0, pos + 1); |
| 109 | return Pathonly; |
| 110 | } else |
| 111 | return Path; |
| 112 | } |
| 113 | |
| 114 | // Returns "/path" from a full /path/to/file.name |
| 115 | string TWFunc::Get_Root_Path(string Path) { |
| 116 | string Local_Path = Path; |
| 117 | |
| 118 | // Make sure that we have a leading slash |
| 119 | if (Local_Path.substr(0, 1) != "/") |
| 120 | Local_Path = "/" + Local_Path; |
| 121 | |
| 122 | // Trim the path to get the root path only |
| 123 | size_t position = Local_Path.find("/", 2); |
| 124 | if (position != string::npos) { |
| 125 | Local_Path.resize(position); |
| 126 | } |
| 127 | return Local_Path; |
| 128 | } |
| 129 | |
| 130 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 131 | int need_libs = 0; |
| 132 | |
| 133 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 134 | return; |
| 135 | |
| 136 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 137 | return; |
| 138 | |
| 139 | ui_print("Installing HTC Dumlock to system...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 140 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 141 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 142 | ui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 143 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 144 | need_libs = 1; |
| 145 | } else |
| 146 | ui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 147 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 148 | ui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 149 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 150 | need_libs = 1; |
| 151 | } else |
| 152 | ui_print("dump_image is already installed, skipping...\n"); |
| 153 | if (need_libs) { |
| 154 | ui_print("Installing libs needed for flash_image and dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 155 | copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755); |
| 156 | copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755); |
| 157 | copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755); |
| 158 | copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 159 | } |
| 160 | ui_print("Installing HTC Dumlock app...\n"); |
| 161 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 162 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 163 | 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] | 164 | sync(); |
| 165 | ui_print("HTC Dumlock is installed.\n"); |
| 166 | } |
| 167 | |
| 168 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 169 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 170 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 171 | return; |
| 172 | |
| 173 | ui_print("Restoring original boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 174 | Exec_Cmd("htcdumlock restore", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 175 | ui_print("Original boot restored.\n"); |
| 176 | } |
| 177 | |
| 178 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 179 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 180 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 181 | return; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 182 | ui_print("Reflashing recovery to boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 183 | Exec_Cmd("htcdumlock recovery noreboot", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 184 | ui_print("Recovery is flashed to boot.\n"); |
| 185 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 186 | |
| 187 | int TWFunc::Recursive_Mkdir(string Path) { |
| 188 | string pathCpy = Path; |
| 189 | string wholePath; |
| 190 | size_t pos = pathCpy.find("/", 2); |
| 191 | |
| 192 | while (pos != string::npos) |
| 193 | { |
| 194 | wholePath = pathCpy.substr(0, pos); |
| 195 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) { |
| 196 | LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | pos = pathCpy.find("/", pos + 1); |
| 201 | } |
| 202 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 203 | return false; |
| 204 | return true; |
| 205 | } |
| 206 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 207 | 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] | 208 | DIR* d; |
| 209 | struct dirent* de; |
| 210 | struct stat st; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 211 | unsigned long long dusize = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 212 | unsigned long long dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 213 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 214 | d = opendir(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 215 | if (d == NULL) |
| 216 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 217 | LOGE("error opening '%s'\n", Path.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 218 | LOGE("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | while ((de = readdir(d)) != NULL) |
| 223 | { |
| 224 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 225 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 226 | dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 227 | dusize += dutemp; |
| 228 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 229 | } |
| 230 | else if (de->d_type == DT_REG) |
| 231 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 232 | stat((Path + "/" + de->d_name).c_str(), &st); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 233 | dusize += (unsigned long long)(st.st_size); |
| 234 | } |
| 235 | } |
| 236 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 237 | return dusize; |
| 238 | } |
| 239 | |
| 240 | bool TWFunc::Path_Exists(string Path) { |
| 241 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 242 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 243 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 244 | return false; |
| 245 | else |
| 246 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 250 | string Display_Text; |
| 251 | |
| 252 | DataManager::GetValue(Read_Value, Display_Text); |
| 253 | if (Display_Text.empty()) |
| 254 | Display_Text = Default_Text; |
| 255 | |
| 256 | DataManager::SetValue("tw_operation", Display_Text); |
| 257 | DataManager::SetValue("tw_partition", ""); |
| 258 | } |
| 259 | |
| 260 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 261 | string Display_Text; |
| 262 | |
| 263 | DataManager::GetValue(Read_Value, Display_Text); |
| 264 | if (Display_Text.empty()) |
| 265 | Display_Text = Default_Text; |
| 266 | |
| 267 | DataManager::SetValue("tw_operation", Display_Text); |
| 268 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 272 | struct stat st; |
| 273 | |
| 274 | if (stat(Path.c_str(), &st) != 0) |
| 275 | return 0; |
| 276 | return st.st_size; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 280 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 281 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 282 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 283 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 284 | static const char *CACHE_ROOT = "/cache"; |
| 285 | static const char *SDCARD_ROOT = "/sdcard"; |
| 286 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 287 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
| 288 | |
| 289 | // close a file, log an error if the error indicator is set |
| 290 | void TWFunc::check_and_fclose(FILE *fp, const char *name) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 291 | fflush(fp); |
| 292 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 293 | fclose(fp); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | 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] | 297 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 298 | if (log == NULL) { |
| 299 | LOGE("Can't open %s\n", destination); |
| 300 | } else { |
| 301 | FILE *tmplog = fopen(source, "r"); |
| 302 | if (tmplog != NULL) { |
| 303 | if (append) { |
| 304 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 305 | } |
| 306 | char buf[4096]; |
| 307 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 308 | if (append) { |
| 309 | tmplog_offset = ftell(tmplog); |
| 310 | } |
| 311 | check_and_fclose(tmplog, source); |
| 312 | } |
| 313 | check_and_fclose(log, destination); |
| 314 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 318 | // copy our log file to cache as well (for the system to read), and |
| 319 | // record any intent we were asked to communicate back to the system. |
| 320 | // this function is idempotent: call it as many times as you like. |
| 321 | void TWFunc::twfinish_recovery(const char *send_intent) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 322 | // By this point, we're ready to return to the main system... |
| 323 | if (send_intent != NULL) { |
| 324 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
| 325 | if (fp == NULL) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 326 | LOGE("Can't open %s\n", INTENT_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 327 | } else { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 328 | fputs(send_intent, fp); |
| 329 | check_and_fclose(fp, INTENT_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 330 | } |
| 331 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 332 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 333 | // Copy logs to cache so the system can find out what happened. |
| 334 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 335 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 336 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 337 | chmod(LOG_FILE, 0600); |
| 338 | chown(LOG_FILE, 1000, 1000); // system user |
| 339 | chmod(LAST_LOG_FILE, 0640); |
| 340 | chmod(LAST_INSTALL_FILE, 0644); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 341 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 342 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 343 | struct bootloader_message boot; |
| 344 | memset(&boot, 0, sizeof(boot)); |
| 345 | set_bootloader_message(&boot); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 346 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 347 | // Remove the command file, so recovery won't repeat indefinitely. |
| 348 | if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 349 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 350 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 351 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 352 | PartitionManager.UnMount_By_Path("/cache", true); |
| 353 | sync(); // For good measure. |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 357 | int TWFunc::tw_reboot(RebootCommand command) |
| 358 | { |
| 359 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 360 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 361 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 362 | switch (command) |
| 363 | { |
| 364 | case rb_current: |
| 365 | case rb_system: |
| 366 | twfinish_recovery("s"); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 367 | sync(); |
| 368 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 369 | return reboot(RB_AUTOBOOT); |
| 370 | case rb_recovery: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 371 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 372 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 373 | case rb_bootloader: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 374 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 375 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 376 | case rb_poweroff: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 377 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 378 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 379 | return reboot(RB_POWER_OFF); |
| 380 | case rb_download: |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 381 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
| 382 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 383 | return 1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 384 | default: |
| 385 | return -1; |
| 386 | } |
| 387 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 391 | { |
| 392 | // Check for and run startup script if script exists |
| 393 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 394 | string result; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 395 | if (stat(script_file, &st) == 0) { |
| 396 | ui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 397 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 398 | TWFunc::Exec_Cmd(script_file, result); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 399 | ui_print("\nFinished running %s script.\n", display_name); |
| 400 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 401 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 402 | |
| 403 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 404 | DIR *d = opendir(path.c_str()); |
| 405 | int r = 0; |
| 406 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 407 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 408 | if (d == NULL) { |
| 409 | LOGE("Error opening '%s'\n", path.c_str()); |
| 410 | return -1; |
| 411 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 412 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 413 | if (d) { |
| 414 | struct dirent *p; |
| 415 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 416 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 417 | continue; |
| 418 | new_path = path + "/"; |
| 419 | new_path.append(p->d_name); |
| 420 | if (p->d_type == DT_DIR) { |
| 421 | r = removeDir(new_path, true); |
| 422 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 423 | if (p->d_type == DT_DIR) |
| 424 | r = rmdir(new_path.c_str()); |
| 425 | else |
| 426 | LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
| 427 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 428 | } 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] | 429 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 430 | if (r != 0) { |
| 431 | LOGI("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
| 432 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 436 | |
| 437 | if (!r) { |
| 438 | if (skipParent) |
| 439 | return 0; |
| 440 | else |
| 441 | r = rmdir(path.c_str()); |
| 442 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 443 | } |
| 444 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | int TWFunc::copy_file(string src, string dst, int mode) { |
| 448 | LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
| 449 | ifstream srcfile(src.c_str(), ios::binary); |
| 450 | ofstream dstfile(dst.c_str(), ios::binary); |
| 451 | dstfile << srcfile.rdbuf(); |
| 452 | srcfile.close(); |
| 453 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 454 | if (chmod(dst.c_str(), mode) != 0) |
| 455 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 456 | return 0; |
| 457 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 458 | |
| 459 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 460 | struct stat st; |
| 461 | |
| 462 | stat(Path.c_str(), &st); |
| 463 | if (st.st_mode & S_IFDIR) |
| 464 | return DT_DIR; |
| 465 | else if (st.st_mode & S_IFBLK) |
| 466 | return DT_BLK; |
| 467 | else if (st.st_mode & S_IFCHR) |
| 468 | return DT_CHR; |
| 469 | else if (st.st_mode & S_IFIFO) |
| 470 | return DT_FIFO; |
| 471 | else if (st.st_mode & S_IFLNK) |
| 472 | return DT_LNK; |
| 473 | else if (st.st_mode & S_IFREG) |
| 474 | return DT_REG; |
| 475 | else if (st.st_mode & S_IFSOCK) |
| 476 | return DT_SOCK; |
| 477 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 478 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 479 | |
| 480 | int TWFunc::read_file(string fn, string& results) { |
| 481 | ifstream file; |
| 482 | file.open(fn.c_str(), ios::in); |
| 483 | if (file.is_open()) { |
| 484 | file >> results; |
| 485 | file.close(); |
| 486 | return 0; |
| 487 | } |
| 488 | LOGI("Cannot find file %s\n", fn.c_str()); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | int TWFunc::write_file(string fn, string& line) { |
| 493 | FILE *file; |
| 494 | file = fopen(fn.c_str(), "w"); |
| 495 | if (file != NULL) { |
| 496 | fwrite(line.c_str(), line.size(), 1, file); |
| 497 | fclose(file); |
| 498 | return 0; |
| 499 | } |
| 500 | LOGI("Cannot find file %s\n", fn.c_str()); |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 505 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 506 | timespec temp; |
| 507 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 508 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 509 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 510 | } else { |
| 511 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 512 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 513 | } |
| 514 | return temp; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | int TWFunc::drop_caches(void) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 518 | string file = "/proc/sys/vm/drop_caches"; |
| 519 | string value = "3"; |
| 520 | if (write_file(file, value) != 0) |
| 521 | return -1; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | int TWFunc::Check_su_Perms(void) { |
| 526 | struct stat st; |
| 527 | int ret = 0; |
| 528 | |
| 529 | if (!PartitionManager.Mount_By_Path("/system", false)) |
| 530 | return 0; |
| 531 | |
| 532 | // Check to ensure that perms are 6755 for all 3 file locations |
| 533 | if (stat("/system/bin/su", &st) == 0) { |
| 534 | 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) { |
| 535 | ret = 1; |
| 536 | } |
| 537 | } |
| 538 | if (stat("/system/xbin/su", &st) == 0) { |
| 539 | 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) { |
| 540 | ret += 2; |
| 541 | } |
| 542 | } |
| 543 | if (stat("/system/bin/.ext/.su", &st) == 0) { |
| 544 | 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) { |
| 545 | ret += 4; |
| 546 | } |
| 547 | } |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | bool TWFunc::Fix_su_Perms(void) { |
| 552 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 553 | return false; |
| 554 | |
| 555 | string file = "/system/bin/su"; |
| 556 | if (TWFunc::Path_Exists(file)) { |
| 557 | if (chown(file.c_str(), 0, 0) != 0) { |
| 558 | LOGE("Failed to chown '%s'\n", file.c_str()); |
| 559 | return false; |
| 560 | } |
| 561 | if (tw_chmod(file, "6755") != 0) { |
| 562 | LOGE("Failed to chmod '%s'\n", file.c_str()); |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | file = "/system/xbin/su"; |
| 567 | if (TWFunc::Path_Exists(file)) { |
| 568 | if (chown(file.c_str(), 0, 0) != 0) { |
| 569 | LOGE("Failed to chown '%s'\n", file.c_str()); |
| 570 | return false; |
| 571 | } |
| 572 | if (tw_chmod(file, "6755") != 0) { |
| 573 | LOGE("Failed to chmod '%s'\n", file.c_str()); |
| 574 | return false; |
| 575 | } |
| 576 | } |
| 577 | file = "/system/bin/.ext/.su"; |
| 578 | if (TWFunc::Path_Exists(file)) { |
| 579 | if (chown(file.c_str(), 0, 0) != 0) { |
| 580 | LOGE("Failed to chown '%s'\n", file.c_str()); |
| 581 | return false; |
| 582 | } |
| 583 | if (tw_chmod(file, "6755") != 0) { |
| 584 | LOGE("Failed to chmod '%s'\n", file.c_str()); |
| 585 | return false; |
| 586 | } |
| 587 | } |
| 588 | file = "/system/app/Superuser.apk"; |
| 589 | if (TWFunc::Path_Exists(file)) { |
| 590 | if (chown(file.c_str(), 0, 0) != 0) { |
| 591 | LOGE("Failed to chown '%s'\n", file.c_str()); |
| 592 | return false; |
| 593 | } |
| 594 | if (tw_chmod(file, "0644") != 0) { |
| 595 | LOGE("Failed to chmod '%s'\n", file.c_str()); |
| 596 | return false; |
| 597 | } |
| 598 | } |
| 599 | sync(); |
| 600 | if (!PartitionManager.UnMount_By_Path("/system", true)) |
| 601 | return false; |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | int TWFunc::tw_chmod(string fn, string mode) { |
| 606 | long mask = 0; |
| 607 | |
| 608 | for ( std::string::size_type n = 0; n < mode.length(); ++n) { |
| 609 | if (n == 0) { |
| 610 | if (mode[n] == '0') |
| 611 | continue; |
| 612 | if (mode[n] == '1') |
| 613 | mask |= S_ISVTX; |
| 614 | if (mode[n] == '2') |
| 615 | mask |= S_ISGID; |
| 616 | if (mode[n] == '4') |
| 617 | mask |= S_ISUID; |
| 618 | if (mode[n] == '5') { |
| 619 | mask |= S_ISVTX; |
| 620 | mask |= S_ISUID; |
| 621 | } |
| 622 | if (mode[n] == '6') { |
| 623 | mask |= S_ISGID; |
| 624 | mask |= S_ISUID; |
| 625 | } |
| 626 | if (mode[n] == '7') { |
| 627 | mask |= S_ISVTX; |
| 628 | mask |= S_ISGID; |
| 629 | mask |= S_ISUID; |
| 630 | } |
| 631 | } |
| 632 | else if (n == 1) { |
| 633 | if (mode[n] == '7') { |
| 634 | mask |= S_IRWXU; |
| 635 | } |
| 636 | if (mode[n] == '6') { |
| 637 | mask |= S_IRUSR; |
| 638 | mask |= S_IWUSR; |
| 639 | } |
| 640 | if (mode[n] == '5') { |
| 641 | mask |= S_IRUSR; |
| 642 | mask |= S_IXUSR; |
| 643 | } |
| 644 | if (mode[n] == '4') |
| 645 | mask |= S_IRUSR; |
| 646 | if (mode[n] == '3') { |
| 647 | mask |= S_IWUSR; |
| 648 | mask |= S_IRUSR; |
| 649 | } |
| 650 | if (mode[n] == '2') |
| 651 | mask |= S_IWUSR; |
| 652 | if (mode[n] == '1') |
| 653 | mask |= S_IXUSR; |
| 654 | } |
| 655 | else if (n == 2) { |
| 656 | if (mode[n] == '7') { |
| 657 | mask |= S_IRWXG; |
| 658 | } |
| 659 | if (mode[n] == '6') { |
| 660 | mask |= S_IRGRP; |
| 661 | mask |= S_IWGRP; |
| 662 | } |
| 663 | if (mode[n] == '5') { |
| 664 | mask |= S_IRGRP; |
| 665 | mask |= S_IXGRP; |
| 666 | } |
| 667 | if (mode[n] == '4') |
| 668 | mask |= S_IRGRP; |
| 669 | if (mode[n] == '3') { |
| 670 | mask |= S_IWGRP; |
| 671 | mask |= S_IXGRP; |
| 672 | } |
| 673 | if (mode[n] == '2') |
| 674 | mask |= S_IWGRP; |
| 675 | if (mode[n] == '1') |
| 676 | mask |= S_IXGRP; |
| 677 | } |
| 678 | else if (n == 3) { |
| 679 | if (mode[n] == '7') { |
| 680 | mask |= S_IRWXO; |
| 681 | } |
| 682 | if (mode[n] == '6') { |
| 683 | mask |= S_IROTH; |
| 684 | mask |= S_IWOTH; |
| 685 | } |
| 686 | if (mode[n] == '5') { |
| 687 | mask |= S_IROTH; |
| 688 | mask |= S_IXOTH; |
| 689 | } |
| 690 | if (mode[n] == '4') |
| 691 | mask |= S_IROTH; |
| 692 | if (mode[n] == '3') { |
| 693 | mask |= S_IWOTH; |
| 694 | mask |= S_IXOTH; |
| 695 | } |
| 696 | if (mode[n] == '2') |
| 697 | mask |= S_IWOTH; |
| 698 | if (mode[n] == '1') |
| 699 | mask |= S_IXOTH; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (chmod(fn.c_str(), mask) != 0) { |
| 704 | LOGE("Unable to chmod '%s' %l\n", fn.c_str(), mask); |
| 705 | return -1; |
| 706 | } |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | bool TWFunc::Install_SuperSU(void) { |
| 712 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 713 | return false; |
| 714 | |
| 715 | if (copy_file("/res/supersu/su", "/system/xbin/su", 0755) != 0) { |
| 716 | LOGE("Failed to copy su binary to /system/bin\n"); |
| 717 | return false; |
| 718 | } |
| 719 | if (copy_file("/res/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) { |
| 720 | LOGE("Failed to copy Superuser app to /system/app\n"); |
| 721 | return false; |
| 722 | } |
| 723 | if (!Fix_su_Perms()) |
| 724 | return false; |
| 725 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 726 | } |