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> |
| 15 | #include <iostream> |
| 16 | #include <fstream> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 17 | #include "twrp-functions.hpp" |
| 18 | #include "partitions.hpp" |
| 19 | #include "common.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 20 | #include "data.hpp" |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 21 | #include "bootloader.h" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 22 | #include "variables.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 23 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 24 | extern "C" { |
| 25 | #include "libcrecovery/common.h" |
| 26 | } |
| 27 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | |
| 29 | /* Execute a command */ |
| 30 | |
| 31 | int TWFunc::Exec_Cmd(string cmd, string &result) { |
| 32 | FILE* exec; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 33 | char buffer[130]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 34 | int ret = 0; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 35 | exec = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 36 | if (!exec) return -1; |
| 37 | while(!feof(exec)) { |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 38 | memset(&buffer, 0, sizeof(buffer)); |
| 39 | if (fgets(buffer, 128, exec) != NULL) { |
| 40 | buffer[128] = '\n'; |
| 41 | buffer[129] = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 42 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 43 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 44 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 45 | ret = __pclose(exec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 49 | /* Checks md5 for a path |
| 50 | Return values: |
| 51 | -1 : MD5 does not exist |
| 52 | 0 : Failed |
| 53 | 1 : Success */ |
| 54 | int TWFunc::Check_MD5(string File) { |
| 55 | int ret; |
| 56 | string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK; |
| 57 | char line[255]; |
| 58 | size_t pos; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 59 | string result; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 60 | |
| 61 | MD5_File = File + ".md5"; |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 62 | if (Path_Exists(MD5_File)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 63 | DirPath = Get_Path(File); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 64 | MD5_File = Get_Filename(MD5_File); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 65 | Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "'"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 66 | Exec_Cmd(Command, result); |
| 67 | pos = result.find(":"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 68 | if (pos != string::npos) { |
| 69 | Filename = Get_Filename(File); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 70 | MD5_File_Filename = result.substr(0, pos); |
| 71 | OK = result.substr(pos + 2, result.size() - pos - 2); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 72 | if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) { |
| 73 | //MD5 is good, return 1 |
| 74 | ret = 1; |
| 75 | } else { |
| 76 | // MD5 is bad, return 0 |
| 77 | ret = 0; |
| 78 | } |
| 79 | } else { |
| 80 | // MD5 is bad, return 0 |
| 81 | ret = 0; |
| 82 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 83 | } else { |
| 84 | //No md5 file, return -1 |
| 85 | ret = -1; |
| 86 | } |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | // Returns "file.name" from a full /path/to/file.name |
| 92 | string TWFunc::Get_Filename(string Path) { |
| 93 | size_t pos = Path.find_last_of("/"); |
| 94 | if (pos != string::npos) { |
| 95 | string Filename; |
| 96 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 97 | return Filename; |
| 98 | } else |
| 99 | return Path; |
| 100 | } |
| 101 | |
| 102 | // Returns "/path/to/" from a full /path/to/file.name |
| 103 | string TWFunc::Get_Path(string Path) { |
| 104 | size_t pos = Path.find_last_of("/"); |
| 105 | if (pos != string::npos) { |
| 106 | string Pathonly; |
| 107 | Pathonly = Path.substr(0, pos + 1); |
| 108 | return Pathonly; |
| 109 | } else |
| 110 | return Path; |
| 111 | } |
| 112 | |
| 113 | // Returns "/path" from a full /path/to/file.name |
| 114 | string TWFunc::Get_Root_Path(string Path) { |
| 115 | string Local_Path = Path; |
| 116 | |
| 117 | // Make sure that we have a leading slash |
| 118 | if (Local_Path.substr(0, 1) != "/") |
| 119 | Local_Path = "/" + Local_Path; |
| 120 | |
| 121 | // Trim the path to get the root path only |
| 122 | size_t position = Local_Path.find("/", 2); |
| 123 | if (position != string::npos) { |
| 124 | Local_Path.resize(position); |
| 125 | } |
| 126 | return Local_Path; |
| 127 | } |
| 128 | |
| 129 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 130 | int need_libs = 0; |
| 131 | |
| 132 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 133 | return; |
| 134 | |
| 135 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 136 | return; |
| 137 | |
| 138 | ui_print("Installing HTC Dumlock to system...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 139 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 140 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 141 | ui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 142 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 143 | need_libs = 1; |
| 144 | } else |
| 145 | ui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 146 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 147 | ui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 149 | need_libs = 1; |
| 150 | } else |
| 151 | ui_print("dump_image is already installed, skipping...\n"); |
| 152 | if (need_libs) { |
| 153 | ui_print("Installing libs needed for flash_image and dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 154 | copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755); |
| 155 | copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755); |
| 156 | copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755); |
| 157 | copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 158 | } |
| 159 | ui_print("Installing HTC Dumlock app...\n"); |
| 160 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 161 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 162 | 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] | 163 | sync(); |
| 164 | ui_print("HTC Dumlock is installed.\n"); |
| 165 | } |
| 166 | |
| 167 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 168 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 169 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 170 | return; |
| 171 | |
| 172 | ui_print("Restoring original boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 173 | Exec_Cmd("htcdumlock restore", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 174 | ui_print("Original boot restored.\n"); |
| 175 | } |
| 176 | |
| 177 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 178 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 179 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 180 | return; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 181 | ui_print("Reflashing recovery to boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 182 | Exec_Cmd("htcdumlock recovery noreboot", status); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 183 | ui_print("Recovery is flashed to boot.\n"); |
| 184 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 185 | |
| 186 | int TWFunc::Recursive_Mkdir(string Path) { |
| 187 | string pathCpy = Path; |
| 188 | string wholePath; |
| 189 | size_t pos = pathCpy.find("/", 2); |
| 190 | |
| 191 | while (pos != string::npos) |
| 192 | { |
| 193 | wholePath = pathCpy.substr(0, pos); |
| 194 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) { |
| 195 | LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | pos = pathCpy.find("/", pos + 1); |
| 200 | } |
| 201 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 202 | return false; |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { |
| 207 | DIR* d; |
| 208 | struct dirent* de; |
| 209 | struct stat st; |
Dees_Troy | d9a9616 | 2012-12-20 17:44:35 +0000 | [diff] [blame] | 210 | char path2[4096], filename[4096]; |
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 | // Make a copy of path in case the data in the pointer gets overwritten later |
| 214 | strcpy(path2, Path.c_str()); |
| 215 | |
| 216 | d = opendir(path2); |
| 217 | if (d == NULL) |
| 218 | { |
| 219 | LOGE("error opening '%s'\n", path2); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 220 | LOGE("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | while ((de = readdir(d)) != NULL) |
| 225 | { |
| 226 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 227 | { |
| 228 | strcpy(filename, path2); |
| 229 | strcat(filename, "/"); |
| 230 | strcat(filename, de->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 231 | dutemp = Get_Folder_Size(filename, Display_Error); |
| 232 | dusize += dutemp; |
| 233 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 234 | } |
| 235 | else if (de->d_type == DT_REG) |
| 236 | { |
| 237 | strcpy(filename, path2); |
| 238 | strcat(filename, "/"); |
| 239 | strcat(filename, de->d_name); |
| 240 | stat(filename, &st); |
| 241 | dusize += (unsigned long long)(st.st_size); |
| 242 | } |
| 243 | } |
| 244 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 245 | return dusize; |
| 246 | } |
| 247 | |
| 248 | bool TWFunc::Path_Exists(string Path) { |
| 249 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 250 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 251 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 252 | return false; |
| 253 | else |
| 254 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 258 | string Display_Text; |
| 259 | |
| 260 | DataManager::GetValue(Read_Value, Display_Text); |
| 261 | if (Display_Text.empty()) |
| 262 | Display_Text = Default_Text; |
| 263 | |
| 264 | DataManager::SetValue("tw_operation", Display_Text); |
| 265 | DataManager::SetValue("tw_partition", ""); |
| 266 | } |
| 267 | |
| 268 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 269 | string Display_Text; |
| 270 | |
| 271 | DataManager::GetValue(Read_Value, Display_Text); |
| 272 | if (Display_Text.empty()) |
| 273 | Display_Text = Default_Text; |
| 274 | |
| 275 | DataManager::SetValue("tw_operation", Display_Text); |
| 276 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 280 | struct stat st; |
| 281 | |
| 282 | if (stat(Path.c_str(), &st) != 0) |
| 283 | return 0; |
| 284 | return st.st_size; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 288 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 289 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 290 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 291 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 292 | static const char *CACHE_ROOT = "/cache"; |
| 293 | static const char *SDCARD_ROOT = "/sdcard"; |
| 294 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 295 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
| 296 | |
| 297 | // close a file, log an error if the error indicator is set |
| 298 | void TWFunc::check_and_fclose(FILE *fp, const char *name) { |
| 299 | fflush(fp); |
| 300 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 301 | fclose(fp); |
| 302 | } |
| 303 | |
| 304 | void TWFunc::copy_log_file(const char* source, const char* destination, int append) { |
| 305 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 306 | if (log == NULL) { |
| 307 | LOGE("Can't open %s\n", destination); |
| 308 | } else { |
| 309 | FILE *tmplog = fopen(source, "r"); |
| 310 | if (tmplog != NULL) { |
| 311 | if (append) { |
| 312 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 313 | } |
| 314 | char buf[4096]; |
| 315 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 316 | if (append) { |
| 317 | tmplog_offset = ftell(tmplog); |
| 318 | } |
| 319 | check_and_fclose(tmplog, source); |
| 320 | } |
| 321 | check_and_fclose(log, destination); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 326 | // copy our log file to cache as well (for the system to read), and |
| 327 | // record any intent we were asked to communicate back to the system. |
| 328 | // this function is idempotent: call it as many times as you like. |
| 329 | void TWFunc::twfinish_recovery(const char *send_intent) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 330 | // By this point, we're ready to return to the main system... |
| 331 | if (send_intent != NULL) { |
| 332 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
| 333 | if (fp == NULL) { |
| 334 | LOGE("Can't open %s\n", INTENT_FILE); |
| 335 | } else { |
| 336 | fputs(send_intent, fp); |
| 337 | check_and_fclose(fp, INTENT_FILE); |
| 338 | } |
| 339 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 340 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 341 | // Copy logs to cache so the system can find out what happened. |
| 342 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 343 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 344 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 345 | chmod(LOG_FILE, 0600); |
| 346 | chown(LOG_FILE, 1000, 1000); // system user |
| 347 | chmod(LAST_LOG_FILE, 0640); |
| 348 | chmod(LAST_INSTALL_FILE, 0644); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 349 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 350 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 351 | struct bootloader_message boot; |
| 352 | memset(&boot, 0, sizeof(boot)); |
| 353 | set_bootloader_message(&boot); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 354 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 355 | // Remove the command file, so recovery won't repeat indefinitely. |
| 356 | if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
| 357 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 358 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 359 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 360 | PartitionManager.UnMount_By_Path("/cache", true); |
| 361 | sync(); // For good measure. |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 365 | int TWFunc::tw_reboot(RebootCommand command) |
| 366 | { |
| 367 | // Always force a sync before we reboot |
| 368 | sync(); |
| 369 | |
| 370 | switch (command) |
| 371 | { |
| 372 | case rb_current: |
| 373 | case rb_system: |
| 374 | twfinish_recovery("s"); |
| 375 | sync(); |
| 376 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
| 377 | return reboot(RB_AUTOBOOT); |
| 378 | case rb_recovery: |
| 379 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
| 380 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 381 | case rb_bootloader: |
| 382 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
| 383 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 384 | case rb_poweroff: |
| 385 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
| 386 | return reboot(RB_POWER_OFF); |
| 387 | case rb_download: |
| 388 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
| 389 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 390 | return 1; |
| 391 | default: |
| 392 | return -1; |
| 393 | } |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 398 | { |
| 399 | // Check for and run startup script if script exists |
| 400 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 401 | string result; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 402 | if (stat(script_file, &st) == 0) { |
| 403 | ui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 404 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 405 | TWFunc::Exec_Cmd(script_file, result); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 406 | ui_print("\nFinished running %s script.\n", display_name); |
| 407 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 408 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 409 | |
| 410 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 411 | DIR *d = opendir(path.c_str()); |
| 412 | int r = 0; |
| 413 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 414 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 415 | if (d == NULL) { |
| 416 | LOGE("Error opening '%s'\n", path.c_str()); |
| 417 | return -1; |
| 418 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 419 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 420 | if (d) { |
| 421 | struct dirent *p; |
| 422 | while (!r && (p = readdir(d))) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 423 | LOGI("checking :%s\n", p->d_name); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 424 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 425 | continue; |
| 426 | new_path = path + "/"; |
| 427 | new_path.append(p->d_name); |
| 428 | if (p->d_type == DT_DIR) { |
| 429 | r = removeDir(new_path, true); |
| 430 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 431 | if (p->d_type == DT_DIR) |
| 432 | r = rmdir(new_path.c_str()); |
| 433 | else |
| 434 | LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
| 435 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 436 | } 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] | 437 | r = unlink(new_path.c_str()); |
| 438 | if (!r) |
| 439 | LOGI("Unable to unlink '%s'\n", new_path.c_str()); |
| 440 | } |
| 441 | } |
| 442 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 443 | |
| 444 | if (!r) { |
| 445 | if (skipParent) |
| 446 | return 0; |
| 447 | else |
| 448 | r = rmdir(path.c_str()); |
| 449 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 450 | } |
| 451 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | int TWFunc::copy_file(string src, string dst, int mode) { |
| 455 | LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
| 456 | ifstream srcfile(src.c_str(), ios::binary); |
| 457 | ofstream dstfile(dst.c_str(), ios::binary); |
| 458 | dstfile << srcfile.rdbuf(); |
| 459 | srcfile.close(); |
| 460 | dstfile.close(); |
| 461 | return 0; |
| 462 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 463 | |
| 464 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 465 | struct stat st; |
| 466 | |
| 467 | stat(Path.c_str(), &st); |
| 468 | if (st.st_mode & S_IFDIR) |
| 469 | return DT_DIR; |
| 470 | else if (st.st_mode & S_IFBLK) |
| 471 | return DT_BLK; |
| 472 | else if (st.st_mode & S_IFCHR) |
| 473 | return DT_CHR; |
| 474 | else if (st.st_mode & S_IFIFO) |
| 475 | return DT_FIFO; |
| 476 | else if (st.st_mode & S_IFLNK) |
| 477 | return DT_LNK; |
| 478 | else if (st.st_mode & S_IFREG) |
| 479 | return DT_REG; |
| 480 | else if (st.st_mode & S_IFSOCK) |
| 481 | return DT_SOCK; |
| 482 | return DT_UNKNOWN; |
| 483 | } |