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