Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 21 | #include <string> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <vector> |
| 24 | #include <dirent.h> |
| 25 | #include <time.h> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 26 | #include <errno.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <sys/mount.h> |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 29 | #include <sys/reboot.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 30 | #include <sys/sendfile.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/vfs.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | #include <iostream> |
| 36 | #include <fstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 37 | #include <sstream> |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 38 | #include <algorithm> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 39 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "twcommon.h" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 41 | #ifndef BUILD_TWRPTAR_MAIN |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 42 | #include "data.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 43 | #include "partitions.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 44 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 45 | #include "bootloader.h" |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 46 | #include "cutils/properties.h" |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 47 | #include "cutils/android_reboot.h" |
| 48 | #include <sys/reboot.h> |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 49 | #endif // ndef BUILD_TWRPTAR_MAIN |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 50 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 51 | #include "openaes/inc/oaes_lib.h" |
| 52 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 53 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 54 | extern "C" { |
| 55 | #include "libcrecovery/common.h" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 56 | #include "set_metadata.h" |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 59 | /* Execute a command */ |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 60 | int TWFunc::Exec_Cmd(const string& cmd, string &result) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 61 | FILE* exec; |
| 62 | char buffer[130]; |
| 63 | int ret = 0; |
| 64 | exec = __popen(cmd.c_str(), "r"); |
| 65 | if (!exec) return -1; |
| 66 | while(!feof(exec)) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 67 | if (fgets(buffer, 128, exec) != NULL) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 69 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 70 | } |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 71 | ret = __pclose(exec); |
| 72 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 75 | int TWFunc::Exec_Cmd(const string& cmd) { |
| 76 | pid_t pid; |
| 77 | int status; |
| 78 | switch(pid = fork()) |
| 79 | { |
| 80 | case -1: |
bigbiff bigbiff | 731df79 | 2014-02-20 18:26:13 -0500 | [diff] [blame] | 81 | LOGERR("Exec_Cmd(): vfork failed: %d!\n", errno); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 82 | return -1; |
| 83 | case 0: // child |
| 84 | execl("/sbin/sh", "sh", "-c", cmd.c_str(), NULL); |
| 85 | _exit(127); |
| 86 | break; |
| 87 | default: |
| 88 | { |
| 89 | if (TWFunc::Wait_For_Child(pid, &status, cmd) != 0) |
| 90 | return -1; |
| 91 | else |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 97 | // Returns "file.name" from a full /path/to/file.name |
| 98 | string TWFunc::Get_Filename(string Path) { |
| 99 | size_t pos = Path.find_last_of("/"); |
| 100 | if (pos != string::npos) { |
| 101 | string Filename; |
| 102 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 103 | return Filename; |
| 104 | } else |
| 105 | return Path; |
| 106 | } |
| 107 | |
| 108 | // Returns "/path/to/" from a full /path/to/file.name |
| 109 | string TWFunc::Get_Path(string Path) { |
| 110 | size_t pos = Path.find_last_of("/"); |
| 111 | if (pos != string::npos) { |
| 112 | string Pathonly; |
| 113 | Pathonly = Path.substr(0, pos + 1); |
| 114 | return Pathonly; |
| 115 | } else |
| 116 | return Path; |
| 117 | } |
| 118 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 119 | int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) { |
| 120 | pid_t rc_pid; |
| 121 | |
| 122 | rc_pid = waitpid(pid, status, 0); |
| 123 | if (rc_pid > 0) { |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 124 | if (WIFSIGNALED(*status)) { |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 125 | LOGERR("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 126 | return -1; |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 127 | } else if (WEXITSTATUS(*status) == 0) { |
| 128 | LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success |
| 129 | } else { |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 130 | LOGERR("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | } else { // no PID returned |
| 134 | if (errno == ECHILD) |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 135 | LOGERR("%s no child process exist\n", Child_Name.c_str()); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 136 | else { |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 137 | LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | bool TWFunc::Path_Exists(string Path) { |
| 145 | // Check to see if the Path exists |
| 146 | struct stat st; |
| 147 | if (stat(Path.c_str(), &st) != 0) |
| 148 | return false; |
| 149 | else |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | int TWFunc::Get_File_Type(string fn) { |
| 154 | string::size_type i = 0; |
| 155 | int firstbyte = 0, secondbyte = 0; |
| 156 | char header[3]; |
| 157 | |
| 158 | ifstream f; |
| 159 | f.open(fn.c_str(), ios::in | ios::binary); |
| 160 | f.get(header, 3); |
| 161 | f.close(); |
| 162 | firstbyte = header[i] & 0xff; |
| 163 | secondbyte = header[++i] & 0xff; |
| 164 | |
| 165 | if (firstbyte == 0x1f && secondbyte == 0x8b) |
| 166 | return 1; // Compressed |
| 167 | else if (firstbyte == 0x4f && secondbyte == 0x41) |
| 168 | return 2; // Encrypted |
| 169 | else |
| 170 | return 0; // Unknown |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int TWFunc::Try_Decrypting_File(string fn, string password) { |
| 176 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 177 | OAES_CTX * ctx = NULL; |
| 178 | uint8_t _key_data[32] = ""; |
| 179 | FILE *f; |
| 180 | uint8_t buffer[4096]; |
| 181 | uint8_t *buffer_out = NULL; |
| 182 | uint8_t *ptr = NULL; |
| 183 | size_t read_len = 0, out_len = 0; |
| 184 | int firstbyte = 0, secondbyte = 0, key_len; |
| 185 | size_t _j = 0; |
| 186 | size_t _key_data_len = 0; |
| 187 | |
| 188 | // mostly kanged from OpenAES oaes.c |
| 189 | for( _j = 0; _j < 32; _j++ ) |
| 190 | _key_data[_j] = _j + 1; |
| 191 | _key_data_len = password.size(); |
| 192 | if( 16 >= _key_data_len ) |
| 193 | _key_data_len = 16; |
| 194 | else if( 24 >= _key_data_len ) |
| 195 | _key_data_len = 24; |
| 196 | else |
| 197 | _key_data_len = 32; |
| 198 | memcpy(_key_data, password.c_str(), password.size()); |
| 199 | |
| 200 | ctx = oaes_alloc(); |
| 201 | if (ctx == NULL) { |
| 202 | LOGERR("Failed to allocate OAES\n"); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | oaes_key_import_data(ctx, _key_data, _key_data_len); |
| 207 | |
| 208 | f = fopen(fn.c_str(), "rb"); |
| 209 | if (f == NULL) { |
| 210 | LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str()); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 211 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | read_len = fread(buffer, sizeof(uint8_t), 4096, f); |
| 215 | if (read_len <= 0) { |
| 216 | LOGERR("Read size during try decrypt failed\n"); |
| 217 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 218 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 219 | return -1; |
| 220 | } |
| 221 | if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) { |
| 222 | LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n"); |
| 223 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 224 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | buffer_out = (uint8_t *) calloc(out_len, sizeof(char)); |
| 228 | if (buffer_out == NULL) { |
| 229 | LOGERR("Failed to allocate output buffer for try decrypt.\n"); |
| 230 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 231 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) { |
| 235 | LOGERR("Failed to decrypt file '%s'\n", fn.c_str()); |
| 236 | fclose(f); |
| 237 | free(buffer_out); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 238 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 242 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 243 | if (out_len < 2) { |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 244 | LOGINFO("Successfully decrypted '%s' but read length too small.\n", fn.c_str()); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 245 | free(buffer_out); |
| 246 | return 1; // Decrypted successfully |
| 247 | } |
| 248 | ptr = buffer_out; |
| 249 | firstbyte = *ptr & 0xff; |
| 250 | ptr++; |
| 251 | secondbyte = *ptr & 0xff; |
| 252 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 253 | LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str()); |
| 254 | free(buffer_out); |
| 255 | return 3; // Compressed |
| 256 | } |
| 257 | if (out_len >= 262) { |
| 258 | ptr = buffer_out + 257; |
| 259 | if (strncmp((char*)ptr, "ustar", 5) == 0) { |
| 260 | LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str()); |
| 261 | free(buffer_out); |
| 262 | return 2; // Tar |
| 263 | } |
| 264 | } |
| 265 | free(buffer_out); |
| 266 | LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str()); |
| 267 | return 1; // Decrypted successfully |
| 268 | #else |
| 269 | LOGERR("Encrypted backup support not included.\n"); |
| 270 | return -1; |
| 271 | #endif |
| 272 | } |
| 273 | |
| 274 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 275 | struct stat st; |
| 276 | |
| 277 | if (stat(Path.c_str(), &st) != 0) |
| 278 | return 0; |
| 279 | return st.st_size; |
| 280 | } |
| 281 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 282 | std::string TWFunc::Remove_Trailing_Slashes(const std::string& path, bool leaveLast) |
| 283 | { |
| 284 | std::string res; |
| 285 | size_t last_idx = 0, idx = 0; |
| 286 | |
| 287 | while(last_idx != std::string::npos) |
| 288 | { |
| 289 | if(last_idx != 0) |
| 290 | res += '/'; |
| 291 | |
| 292 | idx = path.find_first_of('/', last_idx); |
| 293 | if(idx == std::string::npos) { |
| 294 | res += path.substr(last_idx, idx); |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | res += path.substr(last_idx, idx-last_idx); |
| 299 | last_idx = path.find_first_not_of('/', idx); |
| 300 | } |
| 301 | |
| 302 | if(leaveLast) |
| 303 | res += '/'; |
| 304 | return res; |
| 305 | } |
| 306 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 307 | vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) { |
| 308 | vector<string> res; |
| 309 | |
| 310 | if (in.empty() || del == '\0') |
| 311 | return res; |
| 312 | |
| 313 | string field; |
| 314 | istringstream f(in); |
| 315 | if (del == '\n') { |
| 316 | while(getline(f, field)) { |
| 317 | if (field.empty() && skip_empty) |
| 318 | continue; |
| 319 | res.push_back(field); |
| 320 | } |
| 321 | } else { |
| 322 | while(getline(f, field, del)) { |
| 323 | if (field.empty() && skip_empty) |
| 324 | continue; |
| 325 | res.push_back(field); |
| 326 | } |
| 327 | } |
| 328 | return res; |
| 329 | } |
| 330 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 331 | #ifndef BUILD_TWRPTAR_MAIN |
| 332 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 333 | // Returns "/path" from a full /path/to/file.name |
| 334 | string TWFunc::Get_Root_Path(string Path) { |
| 335 | string Local_Path = Path; |
| 336 | |
| 337 | // Make sure that we have a leading slash |
| 338 | if (Local_Path.substr(0, 1) != "/") |
| 339 | Local_Path = "/" + Local_Path; |
| 340 | |
| 341 | // Trim the path to get the root path only |
| 342 | size_t position = Local_Path.find("/", 2); |
| 343 | if (position != string::npos) { |
| 344 | Local_Path.resize(position); |
| 345 | } |
| 346 | return Local_Path; |
| 347 | } |
| 348 | |
| 349 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 350 | int need_libs = 0; |
| 351 | |
| 352 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 353 | return; |
| 354 | |
| 355 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 356 | return; |
| 357 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 358 | gui_print("Installing HTC Dumlock to system...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 359 | copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 360 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 361 | gui_print("Installing flash_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 362 | copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 363 | need_libs = 1; |
| 364 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 365 | gui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 366 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 367 | gui_print("Installing dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 368 | copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 369 | need_libs = 1; |
| 370 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 371 | gui_print("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 372 | if (need_libs) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 373 | gui_print("Installing libs needed for flash_image and dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 374 | copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644); |
| 375 | copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644); |
| 376 | copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644); |
| 377 | copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 378 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 379 | gui_print("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 380 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 381 | unlink("/data/app/com.teamwin.htcdumlock*"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 382 | copy_file(TWHTCD_PATH "HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 383 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 384 | gui_print("HTC Dumlock is installed.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 388 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 389 | return; |
| 390 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 391 | gui_print("Restoring original boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 392 | Exec_Cmd("htcdumlock restore"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 393 | gui_print("Original boot restored.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 397 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 398 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 399 | gui_print("Reflashing recovery to boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 400 | Exec_Cmd("htcdumlock recovery noreboot"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 401 | gui_print("Recovery is flashed to boot.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 402 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 403 | |
| 404 | int TWFunc::Recursive_Mkdir(string Path) { |
| 405 | string pathCpy = Path; |
| 406 | string wholePath; |
| 407 | size_t pos = pathCpy.find("/", 2); |
| 408 | |
| 409 | while (pos != string::npos) |
| 410 | { |
| 411 | wholePath = pathCpy.substr(0, pos); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 412 | if (!TWFunc::Path_Exists(wholePath)) { |
| 413 | if (mkdir(wholePath.c_str(), 0777)) { |
| 414 | LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
| 415 | return false; |
| 416 | } else { |
| 417 | tw_set_default_metadata(wholePath.c_str()); |
| 418 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | pos = pathCpy.find("/", pos + 1); |
| 422 | } |
| 423 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 424 | return false; |
| 425 | return true; |
| 426 | } |
| 427 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 428 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 429 | string Display_Text; |
| 430 | |
| 431 | DataManager::GetValue(Read_Value, Display_Text); |
| 432 | if (Display_Text.empty()) |
| 433 | Display_Text = Default_Text; |
| 434 | |
| 435 | DataManager::SetValue("tw_operation", Display_Text); |
| 436 | DataManager::SetValue("tw_partition", ""); |
| 437 | } |
| 438 | |
| 439 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 440 | string Display_Text; |
| 441 | |
| 442 | DataManager::GetValue(Read_Value, Display_Text); |
| 443 | if (Display_Text.empty()) |
| 444 | Display_Text = Default_Text; |
| 445 | |
| 446 | DataManager::SetValue("tw_operation", Display_Text); |
| 447 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 448 | } |
| 449 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 450 | void TWFunc::Copy_Log(string Source, string Destination) { |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 451 | PartitionManager.Mount_By_Path(Destination, false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 452 | FILE *destination_log = fopen(Destination.c_str(), "a"); |
| 453 | if (destination_log == NULL) { |
| 454 | LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 455 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 456 | FILE *source_log = fopen(Source.c_str(), "r"); |
| 457 | if (source_log != NULL) { |
| 458 | fseek(source_log, Log_Offset, SEEK_SET); |
| 459 | char buffer[4096]; |
| 460 | while (fgets(buffer, sizeof(buffer), source_log)) |
| 461 | fputs(buffer, destination_log); // Buffered write of log file |
| 462 | Log_Offset = ftell(source_log); |
| 463 | fflush(source_log); |
| 464 | fclose(source_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 465 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 466 | fflush(destination_log); |
| 467 | fclose(destination_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 468 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 471 | void TWFunc::Update_Log_File(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 472 | // Copy logs to cache so the system can find out what happened. |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 473 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 474 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 475 | LOGINFO("Recreating /cache/recovery folder.\n"); |
| 476 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 477 | LOGINFO("Unable to create /cache/recovery folder.\n"); |
| 478 | } |
| 479 | Copy_Log(TMP_LOG_FILE, "/cache/recovery/log"); |
| 480 | copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600); |
| 481 | chown("/cache/recovery/log", 1000, 1000); |
| 482 | chmod("/cache/recovery/log", 0600); |
| 483 | chmod("/cache/recovery/last_log", 0640); |
| 484 | } else { |
| 485 | LOGINFO("Failed to mount /cache for TWFunc::Update_Log_File\n"); |
| 486 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 487 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 488 | // Reset bootloader message |
| 489 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 490 | if (Part != NULL) { |
| 491 | struct bootloader_message boot; |
| 492 | memset(&boot, 0, sizeof(boot)); |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 493 | if (set_bootloader_message(&boot) != 0) |
| 494 | LOGERR("Unable to set bootloader message.\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 495 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 496 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 497 | if (PartitionManager.Mount_By_Path("/cache", true)) { |
| 498 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 499 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 500 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 501 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 502 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 503 | sync(); |
| 504 | } |
| 505 | |
| 506 | void TWFunc::Update_Intent_File(string Intent) { |
| 507 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
| 508 | TWFunc::write_file("/cache/recovery/intent", Intent); |
| 509 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 513 | int TWFunc::tw_reboot(RebootCommand command) |
| 514 | { |
| 515 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 516 | sync(); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 517 | Update_Log_File(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 518 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 519 | switch (command) { |
| 520 | case rb_current: |
| 521 | case rb_system: |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 522 | Update_Intent_File("s"); |
| 523 | sync(); |
| 524 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 525 | #ifdef ANDROID_RB_PROPERTY |
| 526 | return property_set(ANDROID_RB_PROPERTY, "reboot,"); |
| 527 | #elif defined(ANDROID_RB_RESTART) |
| 528 | return android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 529 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 530 | return reboot(RB_AUTOBOOT); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 531 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 532 | case rb_recovery: |
| 533 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 534 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 535 | return property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 536 | #else |
| 537 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 538 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 539 | case rb_bootloader: |
| 540 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 541 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 542 | return property_set(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 543 | #else |
| 544 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 545 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 546 | case rb_poweroff: |
| 547 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 548 | #ifdef ANDROID_RB_PROPERTY |
| 549 | return property_set(ANDROID_RB_PROPERTY, "shutdown,"); |
| 550 | #elif defined(ANDROID_RB_POWEROFF) |
| 551 | return android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 552 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 553 | return reboot(RB_POWER_OFF); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 554 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 555 | case rb_download: |
| 556 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 557 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 558 | return property_set(ANDROID_RB_PROPERTY, "reboot,download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 559 | #else |
| 560 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 561 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 562 | default: |
| 563 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 564 | } |
| 565 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 569 | { |
| 570 | // Check for and run startup script if script exists |
| 571 | struct stat st; |
| 572 | if (stat(script_file, &st) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 573 | gui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 574 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 575 | TWFunc::Exec_Cmd(script_file); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 576 | gui_print("\nFinished running %s script.\n", display_name); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 577 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 578 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 579 | |
| 580 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 581 | DIR *d = opendir(path.c_str()); |
| 582 | int r = 0; |
| 583 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 584 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 585 | if (d == NULL) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 586 | LOGERR("Error opening dir: '%s'\n", path.c_str()); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 587 | return -1; |
| 588 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 589 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 590 | if (d) { |
| 591 | struct dirent *p; |
| 592 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 593 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 594 | continue; |
| 595 | new_path = path + "/"; |
| 596 | new_path.append(p->d_name); |
| 597 | if (p->d_type == DT_DIR) { |
| 598 | r = removeDir(new_path, true); |
| 599 | if (!r) { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 600 | if (p->d_type == DT_DIR) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 601 | r = rmdir(new_path.c_str()); |
| 602 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 603 | LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 604 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 605 | } 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] | 606 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 607 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 608 | LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 609 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 613 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 614 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 615 | if (skipParent) |
| 616 | return 0; |
| 617 | else |
| 618 | r = rmdir(path.c_str()); |
| 619 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 620 | } |
| 621 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int TWFunc::copy_file(string src, string dst, int mode) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 625 | LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 626 | ifstream srcfile(src.c_str(), ios::binary); |
| 627 | ofstream dstfile(dst.c_str(), ios::binary); |
| 628 | dstfile << srcfile.rdbuf(); |
| 629 | srcfile.close(); |
| 630 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 631 | if (chmod(dst.c_str(), mode) != 0) |
| 632 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 633 | return 0; |
| 634 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 635 | |
| 636 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 637 | struct stat st; |
| 638 | |
| 639 | stat(Path.c_str(), &st); |
| 640 | if (st.st_mode & S_IFDIR) |
| 641 | return DT_DIR; |
| 642 | else if (st.st_mode & S_IFBLK) |
| 643 | return DT_BLK; |
| 644 | else if (st.st_mode & S_IFCHR) |
| 645 | return DT_CHR; |
| 646 | else if (st.st_mode & S_IFIFO) |
| 647 | return DT_FIFO; |
| 648 | else if (st.st_mode & S_IFLNK) |
| 649 | return DT_LNK; |
| 650 | else if (st.st_mode & S_IFREG) |
| 651 | return DT_REG; |
| 652 | else if (st.st_mode & S_IFSOCK) |
| 653 | return DT_SOCK; |
| 654 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 655 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 656 | |
| 657 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 658 | ifstream file; |
| 659 | file.open(fn.c_str(), ios::in); |
| 660 | |
| 661 | if (file.is_open()) { |
| 662 | file >> results; |
| 663 | file.close(); |
| 664 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 665 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 666 | |
| 667 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 668 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 672 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 673 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 674 | file.open(fn.c_str(), ios::in); |
| 675 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 676 | while (getline(file, line)) |
| 677 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 678 | file.close(); |
| 679 | return 0; |
| 680 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 681 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 682 | return -1; |
| 683 | } |
| 684 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 685 | int TWFunc::read_file(string fn, uint64_t& results) { |
| 686 | ifstream file; |
| 687 | file.open(fn.c_str(), ios::in); |
| 688 | |
| 689 | if (file.is_open()) { |
| 690 | file >> results; |
| 691 | file.close(); |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 696 | return -1; |
| 697 | } |
| 698 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 699 | int TWFunc::write_file(string fn, string& line) { |
| 700 | FILE *file; |
| 701 | file = fopen(fn.c_str(), "w"); |
| 702 | if (file != NULL) { |
| 703 | fwrite(line.c_str(), line.size(), 1, file); |
| 704 | fclose(file); |
| 705 | return 0; |
| 706 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 707 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 712 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 713 | timespec temp; |
| 714 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 715 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 716 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 717 | } else { |
| 718 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 719 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 720 | } |
| 721 | return temp; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 722 | } |
| 723 | |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 724 | int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end) |
| 725 | { |
| 726 | return ((end.tv_sec * 1000) + end.tv_nsec/1000000) - |
| 727 | ((start.tv_sec * 1000) + start.tv_nsec/1000000); |
| 728 | } |
| 729 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 730 | bool TWFunc::Install_SuperSU(void) { |
| 731 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 732 | return false; |
| 733 | |
Ethan Yonker | e3e8829 | 2014-12-10 16:17:55 -0600 | [diff] [blame] | 734 | check_and_run_script("/supersu/install-supersu.sh", "SuperSU"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 735 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 736 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 737 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 738 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 739 | DIR* d; |
| 740 | |
| 741 | string Filename; |
| 742 | Restore_Path += "/"; |
| 743 | d = opendir(Restore_Path.c_str()); |
| 744 | if (d == NULL) { |
| 745 | LOGERR("Error opening '%s'\n", Restore_Path.c_str()); |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | struct dirent* de; |
| 750 | while ((de = readdir(d)) != NULL) { |
| 751 | Filename = Restore_Path; |
| 752 | Filename += de->d_name; |
| 753 | if (TWFunc::Get_File_Type(Filename) == 2) { |
| 754 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 755 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 756 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 757 | closedir(d); |
| 758 | return false; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | closedir(d); |
| 763 | return true; |
| 764 | } |
| 765 | |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 766 | string TWFunc::Get_Current_Date() { |
| 767 | string Current_Date; |
| 768 | time_t seconds = time(0); |
| 769 | struct tm *t = localtime(&seconds); |
| 770 | char timestamp[255]; |
| 771 | sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec); |
| 772 | Current_Date = timestamp; |
| 773 | return Current_Date; |
| 774 | } |
| 775 | |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 776 | string TWFunc::System_Property_Get(string Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 777 | bool mount_state = PartitionManager.Is_Mounted_By_Path("/system"); |
| 778 | std::vector<string> buildprop; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 779 | string propvalue; |
| 780 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 781 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 782 | if (TWFunc::read_file("/system/build.prop", buildprop) != 0) { |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 783 | LOGINFO("Unable to open /system/build.prop for getting '%s'.\n", Prop_Name.c_str()); |
Vojtech Bocek | a2e7016 | 2013-09-17 17:05:10 +0200 | [diff] [blame] | 784 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 785 | if (!mount_state) |
| 786 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 787 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 788 | } |
| 789 | int line_count = buildprop.size(); |
| 790 | int index; |
| 791 | size_t start_pos = 0, end_pos; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 792 | string propname; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 793 | for (index = 0; index < line_count; index++) { |
| 794 | end_pos = buildprop.at(index).find("=", start_pos); |
| 795 | propname = buildprop.at(index).substr(start_pos, end_pos); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 796 | if (propname == Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 797 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 798 | if (!mount_state) |
| 799 | PartitionManager.UnMount_By_Path("/system", false); |
| 800 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 803 | if (!mount_state) |
| 804 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 805 | return propvalue; |
| 806 | } |
| 807 | |
| 808 | void TWFunc::Auto_Generate_Backup_Name() { |
| 809 | string propvalue = System_Property_Get("ro.build.display.id"); |
| 810 | if (propvalue.empty()) { |
| 811 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 812 | return; |
| 813 | } |
| 814 | string Backup_Name = Get_Current_Date(); |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 815 | Backup_Name += "_" + propvalue; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 816 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 817 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 818 | // Trailing spaces cause problems on some file systems, so remove them |
| 819 | string space_check, space = " "; |
| 820 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 821 | while (space_check == space) { |
| 822 | Backup_Name.resize(Backup_Name.size() - 1); |
| 823 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 824 | } |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 825 | replace(Backup_Name.begin(), Backup_Name.end(), ' ', '_'); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 826 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 827 | if (PartitionManager.Check_Backup_Name(false) != 0) { |
| 828 | LOGINFO("Auto generated backup name '%s' contains invalid characters, using date instead.\n", Backup_Name.c_str()); |
| 829 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 830 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 831 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 832 | |
| 833 | void TWFunc::Fixup_Time_On_Boot() |
| 834 | { |
| 835 | #ifdef QCOM_RTC_FIX |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 836 | |
| 837 | LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 838 | |
| 839 | struct timeval tv; |
| 840 | uint64_t offset = 0; |
| 841 | std::string sepoch = "/sys/class/rtc/rtc0/since_epoch"; |
| 842 | |
| 843 | if (TWFunc::read_file(sepoch, offset) == 0) { |
| 844 | |
| 845 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str()); |
| 846 | |
| 847 | tv.tv_sec = offset; |
| 848 | tv.tv_usec = 0; |
| 849 | settimeofday(&tv, NULL); |
| 850 | |
| 851 | gettimeofday(&tv, NULL); |
| 852 | |
| 853 | if (tv.tv_sec > 1405209403) { // Anything older then 12 Jul 2014 23:56:43 GMT will do nicely thank you ;) |
| 854 | |
| 855 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 856 | return; |
| 857 | |
| 858 | } |
| 859 | |
| 860 | } else { |
| 861 | |
| 862 | LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str()); |
| 863 | |
| 864 | } |
| 865 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 866 | LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n"); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 867 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 868 | // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC. |
| 869 | // They never set it, it just ticks forward from 1970-01-01 00:00, |
| 870 | // and then they have files /data/system/time/ats_* with 64bit offset |
| 871 | // in miliseconds which, when added to the RTC, gives the correct time. |
| 872 | // So, the time is: (offset_from_ats + value_from_RTC) |
| 873 | // There are multiple ats files, they are for different systems? Bases? |
| 874 | // Like, ats_1 is for modem and ats_2 is for TOD (time of day?). |
| 875 | // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services |
| 876 | |
| 877 | static const char *paths[] = { "/data/system/time/", "/data/time/" }; |
| 878 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 879 | FILE *f; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 880 | DIR *d; |
| 881 | offset = 0; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 882 | struct dirent *dt; |
| 883 | std::string ats_path; |
| 884 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 885 | if(!PartitionManager.Mount_By_Path("/data", false)) |
| 886 | return; |
| 887 | |
| 888 | // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead |
| 889 | // - it is the one for ATS_TOD (time of day?). |
| 890 | // However, I never saw a device where the offset differs between ats files. |
| 891 | for(size_t i = 0; i < (sizeof(paths)/sizeof(paths[0])); ++i) |
| 892 | { |
| 893 | DIR *d = opendir(paths[i]); |
| 894 | if(!d) |
| 895 | continue; |
| 896 | |
| 897 | while((dt = readdir(d))) |
| 898 | { |
| 899 | if(dt->d_type != DT_REG || strncmp(dt->d_name, "ats_", 4) != 0) |
| 900 | continue; |
| 901 | |
| 902 | if(ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0) |
| 903 | ats_path = std::string(paths[i]).append(dt->d_name); |
| 904 | } |
| 905 | |
| 906 | closedir(d); |
| 907 | } |
| 908 | |
| 909 | if(ats_path.empty()) |
| 910 | { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 911 | LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n"); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | |
| 915 | f = fopen(ats_path.c_str(), "r"); |
| 916 | if(!f) |
| 917 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 918 | LOGINFO("TWFunc::Fixup_Time: failed to open file %s\n", ats_path.c_str()); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
| 922 | if(fread(&offset, sizeof(offset), 1, f) != 1) |
| 923 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 924 | LOGINFO("TWFunc::Fixup_Time: failed load uint64 from file %s\n", ats_path.c_str()); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 925 | fclose(f); |
| 926 | return; |
| 927 | } |
| 928 | fclose(f); |
| 929 | |
| 930 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), offset); |
| 931 | |
| 932 | gettimeofday(&tv, NULL); |
| 933 | |
| 934 | tv.tv_sec += offset/1000; |
| 935 | tv.tv_usec += (offset%1000)*1000; |
| 936 | |
| 937 | while(tv.tv_usec >= 1000000) |
| 938 | { |
| 939 | ++tv.tv_sec; |
| 940 | tv.tv_usec -= 1000000; |
| 941 | } |
| 942 | |
| 943 | settimeofday(&tv, NULL); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 944 | |
| 945 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 946 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 947 | #endif |
| 948 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 949 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 950 | std::vector<std::string> TWFunc::Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty) |
| 951 | { |
| 952 | std::vector<std::string> res; |
| 953 | size_t idx = 0, idx_last = 0; |
| 954 | |
| 955 | while(idx < str.size()) |
| 956 | { |
| 957 | idx = str.find_first_of(delimiter, idx_last); |
| 958 | if(idx == std::string::npos) |
| 959 | idx = str.size(); |
| 960 | |
| 961 | if(idx-idx_last != 0 || !removeEmpty) |
| 962 | res.push_back(str.substr(idx_last, idx-idx_last)); |
| 963 | |
| 964 | idx_last = idx + delimiter.size(); |
| 965 | } |
| 966 | |
| 967 | return res; |
| 968 | } |
| 969 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 970 | bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t uid, gid_t gid) |
| 971 | { |
| 972 | std::vector<std::string> parts = Split_String(path, "/"); |
| 973 | std::string cur_path; |
| 974 | struct stat info; |
| 975 | for(size_t i = 0; i < parts.size(); ++i) |
| 976 | { |
| 977 | cur_path += "/" + parts[i]; |
| 978 | if(stat(cur_path.c_str(), &info) < 0 || !S_ISDIR(info.st_mode)) |
| 979 | { |
| 980 | if(mkdir(cur_path.c_str(), mode) < 0) |
| 981 | return false; |
| 982 | chown(cur_path.c_str(), uid, gid); |
| 983 | } |
| 984 | } |
| 985 | return true; |
| 986 | } |
| 987 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 988 | int TWFunc::Set_Brightness(std::string brightness_value) |
| 989 | { |
| 990 | |
| 991 | std::string brightness_file = DataManager::GetStrValue("tw_brightness_file");; |
| 992 | |
| 993 | if (brightness_file.compare("/nobrightness") != 0) { |
| 994 | std::string secondary_brightness_file = DataManager::GetStrValue("tw_secondary_brightness_file"); |
| 995 | LOGINFO("TWFunc::Set_Brightness: Setting brightness control to %s\n", brightness_value.c_str()); |
| 996 | int result = TWFunc::write_file(brightness_file, brightness_value); |
| 997 | if (secondary_brightness_file != "") { |
| 998 | LOGINFO("TWFunc::Set_Brightness: Setting SECONDARY brightness control to %s\n", brightness_value.c_str()); |
| 999 | TWFunc::write_file(secondary_brightness_file, brightness_value); |
| 1000 | } |
| 1001 | return result; |
| 1002 | } |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1006 | bool TWFunc::Toggle_MTP(bool enable) { |
| 1007 | #ifdef TW_HAS_MTP |
| 1008 | static int was_enabled = false; |
| 1009 | |
| 1010 | if (enable && was_enabled) { |
| 1011 | if (!PartitionManager.Enable_MTP()) |
| 1012 | PartitionManager.Disable_MTP(); |
| 1013 | } else { |
| 1014 | was_enabled = DataManager::GetIntValue("tw_mtp_enabled"); |
| 1015 | PartitionManager.Disable_MTP(); |
| 1016 | usleep(500); |
| 1017 | } |
| 1018 | return was_enabled; |
| 1019 | #else |
| 1020 | return false; |
| 1021 | #endif |
| 1022 | } |
| 1023 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 1024 | void TWFunc::SetPerformanceMode(bool mode) { |
| 1025 | if (mode) { |
| 1026 | property_set("recovery.perf.mode", "1"); |
| 1027 | } else { |
| 1028 | property_set("recovery.perf.mode", "0"); |
| 1029 | } |
| 1030 | // Some time for events to catch up to init handlers |
| 1031 | usleep(500000); |
| 1032 | } |
| 1033 | |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 1034 | std::string TWFunc::to_string(unsigned long value) { |
| 1035 | std::ostringstream os; |
| 1036 | os << value; |
| 1037 | return os.str(); |
| 1038 | } |
| 1039 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1040 | void TWFunc::Disable_Stock_Recovery_Replace(void) { |
| 1041 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 1042 | // Disable flashing of stock recovery |
| 1043 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
| 1044 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
| 1045 | gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); |
| 1046 | sync(); |
| 1047 | } |
| 1048 | PartitionManager.UnMount_By_Path("/system", false); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1052 | #endif // ndef BUILD_TWRPTAR_MAIN |