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