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); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 65 | chdir(DirPath.c_str()); |
| 66 | Command = "/sbin/busybox md5sum -c " + MD5_File; |
| 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 | |
| 89 | return ret; |
| 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 | |
| 207 | unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { |
| 208 | DIR* d; |
| 209 | struct dirent* de; |
| 210 | struct stat st; |
Dees_Troy | d9a9616 | 2012-12-20 17:44:35 +0000 | [diff] [blame] | 211 | char path2[4096], filename[4096]; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 212 | unsigned long long dusize = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 213 | unsigned long long dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 214 | // Make a copy of path in case the data in the pointer gets overwritten later |
| 215 | strcpy(path2, Path.c_str()); |
| 216 | |
| 217 | d = opendir(path2); |
| 218 | if (d == NULL) |
| 219 | { |
| 220 | LOGE("error opening '%s'\n", path2); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 221 | LOGE("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | while ((de = readdir(d)) != NULL) |
| 226 | { |
| 227 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 228 | { |
| 229 | strcpy(filename, path2); |
| 230 | strcat(filename, "/"); |
| 231 | strcat(filename, de->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 232 | dutemp = Get_Folder_Size(filename, Display_Error); |
| 233 | dusize += dutemp; |
| 234 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 235 | } |
| 236 | else if (de->d_type == DT_REG) |
| 237 | { |
| 238 | strcpy(filename, path2); |
| 239 | strcat(filename, "/"); |
| 240 | strcat(filename, de->d_name); |
| 241 | stat(filename, &st); |
| 242 | dusize += (unsigned long long)(st.st_size); |
| 243 | } |
| 244 | } |
| 245 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 246 | return dusize; |
| 247 | } |
| 248 | |
| 249 | bool TWFunc::Path_Exists(string Path) { |
| 250 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 251 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 252 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 253 | return false; |
| 254 | else |
| 255 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 259 | string Display_Text; |
| 260 | |
| 261 | DataManager::GetValue(Read_Value, Display_Text); |
| 262 | if (Display_Text.empty()) |
| 263 | Display_Text = Default_Text; |
| 264 | |
| 265 | DataManager::SetValue("tw_operation", Display_Text); |
| 266 | DataManager::SetValue("tw_partition", ""); |
| 267 | } |
| 268 | |
| 269 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 270 | string Display_Text; |
| 271 | |
| 272 | DataManager::GetValue(Read_Value, Display_Text); |
| 273 | if (Display_Text.empty()) |
| 274 | Display_Text = Default_Text; |
| 275 | |
| 276 | DataManager::SetValue("tw_operation", Display_Text); |
| 277 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 281 | struct stat st; |
| 282 | |
| 283 | if (stat(Path.c_str(), &st) != 0) |
| 284 | return 0; |
| 285 | return st.st_size; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 289 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 290 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 291 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 292 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 293 | static const char *CACHE_ROOT = "/cache"; |
| 294 | static const char *SDCARD_ROOT = "/sdcard"; |
| 295 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 296 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
| 297 | |
| 298 | // close a file, log an error if the error indicator is set |
| 299 | void TWFunc::check_and_fclose(FILE *fp, const char *name) { |
| 300 | fflush(fp); |
| 301 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 302 | fclose(fp); |
| 303 | } |
| 304 | |
| 305 | void TWFunc::copy_log_file(const char* source, const char* destination, int append) { |
| 306 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 307 | if (log == NULL) { |
| 308 | LOGE("Can't open %s\n", destination); |
| 309 | } else { |
| 310 | FILE *tmplog = fopen(source, "r"); |
| 311 | if (tmplog != NULL) { |
| 312 | if (append) { |
| 313 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 314 | } |
| 315 | char buf[4096]; |
| 316 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 317 | if (append) { |
| 318 | tmplog_offset = ftell(tmplog); |
| 319 | } |
| 320 | check_and_fclose(tmplog, source); |
| 321 | } |
| 322 | check_and_fclose(log, destination); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 327 | // copy our log file to cache as well (for the system to read), and |
| 328 | // record any intent we were asked to communicate back to the system. |
| 329 | // this function is idempotent: call it as many times as you like. |
| 330 | void TWFunc::twfinish_recovery(const char *send_intent) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 331 | // By this point, we're ready to return to the main system... |
| 332 | if (send_intent != NULL) { |
| 333 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
| 334 | if (fp == NULL) { |
| 335 | LOGE("Can't open %s\n", INTENT_FILE); |
| 336 | } else { |
| 337 | fputs(send_intent, fp); |
| 338 | check_and_fclose(fp, INTENT_FILE); |
| 339 | } |
| 340 | } |
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 | // Copy logs to cache so the system can find out what happened. |
| 343 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 344 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 345 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 346 | chmod(LOG_FILE, 0600); |
| 347 | chown(LOG_FILE, 1000, 1000); // system user |
| 348 | chmod(LAST_LOG_FILE, 0640); |
| 349 | chmod(LAST_INSTALL_FILE, 0644); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 350 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 351 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 352 | struct bootloader_message boot; |
| 353 | memset(&boot, 0, sizeof(boot)); |
| 354 | set_bootloader_message(&boot); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 355 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 356 | // Remove the command file, so recovery won't repeat indefinitely. |
| 357 | if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
| 358 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 359 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 360 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 361 | PartitionManager.UnMount_By_Path("/cache", true); |
| 362 | sync(); // For good measure. |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 366 | int TWFunc::tw_reboot(RebootCommand command) |
| 367 | { |
| 368 | // Always force a sync before we reboot |
| 369 | sync(); |
| 370 | |
| 371 | switch (command) |
| 372 | { |
| 373 | case rb_current: |
| 374 | case rb_system: |
| 375 | twfinish_recovery("s"); |
| 376 | sync(); |
| 377 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
| 378 | return reboot(RB_AUTOBOOT); |
| 379 | case rb_recovery: |
| 380 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
| 381 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 382 | case rb_bootloader: |
| 383 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
| 384 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 385 | case rb_poweroff: |
| 386 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
| 387 | return reboot(RB_POWER_OFF); |
| 388 | case rb_download: |
| 389 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
| 390 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 391 | return 1; |
| 392 | default: |
| 393 | return -1; |
| 394 | } |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 399 | { |
| 400 | // Check for and run startup script if script exists |
| 401 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 402 | string result; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 403 | if (stat(script_file, &st) == 0) { |
| 404 | ui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 405 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 406 | TWFunc::Exec_Cmd(script_file, result); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 407 | ui_print("\nFinished running %s script.\n", display_name); |
| 408 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 409 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 410 | |
| 411 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 412 | DIR *d = opendir(path.c_str()); |
| 413 | int r = 0; |
| 414 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 415 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 416 | if (d == NULL) { |
| 417 | LOGE("Error opening '%s'\n", path.c_str()); |
| 418 | return -1; |
| 419 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 420 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 421 | if (d) { |
| 422 | struct dirent *p; |
| 423 | while (!r && (p = readdir(d))) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 424 | LOGI("checking :%s\n", p->d_name); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 425 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 426 | continue; |
| 427 | new_path = path + "/"; |
| 428 | new_path.append(p->d_name); |
| 429 | if (p->d_type == DT_DIR) { |
| 430 | r = removeDir(new_path, true); |
| 431 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 432 | if (p->d_type == DT_DIR) |
| 433 | r = rmdir(new_path.c_str()); |
| 434 | else |
| 435 | LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
| 436 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 437 | } 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] | 438 | r = unlink(new_path.c_str()); |
| 439 | if (!r) |
| 440 | LOGI("Unable to unlink '%s'\n", new_path.c_str()); |
| 441 | } |
| 442 | } |
| 443 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 444 | |
| 445 | if (!r) { |
| 446 | if (skipParent) |
| 447 | return 0; |
| 448 | else |
| 449 | r = rmdir(path.c_str()); |
| 450 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 451 | } |
| 452 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | int TWFunc::copy_file(string src, string dst, int mode) { |
| 456 | LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
| 457 | ifstream srcfile(src.c_str(), ios::binary); |
| 458 | ofstream dstfile(dst.c_str(), ios::binary); |
| 459 | dstfile << srcfile.rdbuf(); |
| 460 | srcfile.close(); |
| 461 | dstfile.close(); |
| 462 | return 0; |
| 463 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 464 | |
| 465 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 466 | struct stat st; |
| 467 | |
| 468 | stat(Path.c_str(), &st); |
| 469 | if (st.st_mode & S_IFDIR) |
| 470 | return DT_DIR; |
| 471 | else if (st.st_mode & S_IFBLK) |
| 472 | return DT_BLK; |
| 473 | else if (st.st_mode & S_IFCHR) |
| 474 | return DT_CHR; |
| 475 | else if (st.st_mode & S_IFIFO) |
| 476 | return DT_FIFO; |
| 477 | else if (st.st_mode & S_IFLNK) |
| 478 | return DT_LNK; |
| 479 | else if (st.st_mode & S_IFREG) |
| 480 | return DT_REG; |
| 481 | else if (st.st_mode & S_IFSOCK) |
| 482 | return DT_SOCK; |
| 483 | return DT_UNKNOWN; |
| 484 | } |