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 | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 41 | #include "gui/gui.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 42 | #ifndef BUILD_TWRPTAR_MAIN |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 43 | #include "data.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 44 | #include "partitions.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 45 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 46 | #include "bootloader.h" |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 47 | #include "cutils/properties.h" |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 48 | #include "cutils/android_reboot.h" |
| 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 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 275 | unsigned long TWFunc::Get_File_Size(const string& Path) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 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 | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 332 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 333 | { |
| 334 | timespec temp; |
| 335 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 336 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 337 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 338 | } else { |
| 339 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 340 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 341 | } |
| 342 | return temp; |
| 343 | } |
| 344 | |
| 345 | int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end) |
| 346 | { |
| 347 | return ((end.tv_sec * 1000) + end.tv_nsec/1000000) - |
| 348 | ((start.tv_sec * 1000) + start.tv_nsec/1000000); |
| 349 | } |
| 350 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 351 | #ifndef BUILD_TWRPTAR_MAIN |
| 352 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 353 | // Returns "/path" from a full /path/to/file.name |
| 354 | string TWFunc::Get_Root_Path(string Path) { |
| 355 | string Local_Path = Path; |
| 356 | |
| 357 | // Make sure that we have a leading slash |
| 358 | if (Local_Path.substr(0, 1) != "/") |
| 359 | Local_Path = "/" + Local_Path; |
| 360 | |
| 361 | // Trim the path to get the root path only |
| 362 | size_t position = Local_Path.find("/", 2); |
| 363 | if (position != string::npos) { |
| 364 | Local_Path.resize(position); |
| 365 | } |
| 366 | return Local_Path; |
| 367 | } |
| 368 | |
| 369 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 370 | int need_libs = 0; |
| 371 | |
| 372 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 373 | return; |
| 374 | |
| 375 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 376 | return; |
| 377 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 378 | gui_msg("install_dumlock=Installing HTC Dumlock to system..."); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 379 | copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 380 | if (!Path_Exists("/system/bin/flash_image")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 381 | LOGINFO("Installing flash_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 382 | copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 383 | need_libs = 1; |
| 384 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 385 | LOGINFO("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 386 | if (!Path_Exists("/system/bin/dump_image")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 387 | LOGINFO("Installing dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 388 | copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 389 | need_libs = 1; |
| 390 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 391 | LOGINFO("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 392 | if (need_libs) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 393 | LOGINFO("Installing libs needed for flash_image and dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 394 | copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644); |
| 395 | copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644); |
| 396 | copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644); |
| 397 | copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 398 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 399 | LOGINFO("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 400 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 401 | unlink("/data/app/com.teamwin.htcdumlock*"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 402 | 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] | 403 | sync(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 404 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 408 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 409 | return; |
| 410 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 411 | gui_msg("dumlock_restore=Restoring original boot..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 412 | Exec_Cmd("htcdumlock restore"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 413 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 417 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 418 | return; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 419 | gui_msg("dumlock_reflash=Reflashing recovery to boot..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 420 | Exec_Cmd("htcdumlock recovery noreboot"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 421 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 422 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 423 | |
| 424 | int TWFunc::Recursive_Mkdir(string Path) { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 425 | std::vector<std::string> parts = Split_String(Path, "/", true); |
| 426 | std::string cur_path; |
| 427 | for (size_t i = 0; i < parts.size(); ++i) { |
| 428 | cur_path += "/" + parts[i]; |
| 429 | if (!TWFunc::Path_Exists(cur_path)) { |
| 430 | if (mkdir(cur_path.c_str(), 0777)) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 431 | gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(cur_path)(strerror(errno))); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 432 | return false; |
| 433 | } else { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 434 | tw_set_default_metadata(cur_path.c_str()); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 435 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 436 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 437 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 438 | return true; |
| 439 | } |
| 440 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 441 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 442 | string Display_Text; |
| 443 | |
| 444 | DataManager::GetValue(Read_Value, Display_Text); |
| 445 | if (Display_Text.empty()) |
| 446 | Display_Text = Default_Text; |
| 447 | |
| 448 | DataManager::SetValue("tw_operation", Display_Text); |
| 449 | DataManager::SetValue("tw_partition", ""); |
| 450 | } |
| 451 | |
| 452 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 453 | string Display_Text; |
| 454 | |
| 455 | DataManager::GetValue(Read_Value, Display_Text); |
| 456 | if (Display_Text.empty()) |
| 457 | Display_Text = Default_Text; |
| 458 | |
| 459 | DataManager::SetValue("tw_operation", Display_Text); |
| 460 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 463 | void TWFunc::Copy_Log(string Source, string Destination) { |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 464 | PartitionManager.Mount_By_Path(Destination, false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 465 | FILE *destination_log = fopen(Destination.c_str(), "a"); |
| 466 | if (destination_log == NULL) { |
| 467 | 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] | 468 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 469 | FILE *source_log = fopen(Source.c_str(), "r"); |
| 470 | if (source_log != NULL) { |
| 471 | fseek(source_log, Log_Offset, SEEK_SET); |
| 472 | char buffer[4096]; |
| 473 | while (fgets(buffer, sizeof(buffer), source_log)) |
| 474 | fputs(buffer, destination_log); // Buffered write of log file |
| 475 | Log_Offset = ftell(source_log); |
| 476 | fflush(source_log); |
| 477 | fclose(source_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 478 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 479 | fflush(destination_log); |
| 480 | fclose(destination_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 481 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 484 | void TWFunc::Update_Log_File(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 485 | // Copy logs to cache so the system can find out what happened. |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 486 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 487 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 488 | LOGINFO("Recreating /cache/recovery folder.\n"); |
| 489 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 490 | LOGINFO("Unable to create /cache/recovery folder.\n"); |
| 491 | } |
| 492 | Copy_Log(TMP_LOG_FILE, "/cache/recovery/log"); |
| 493 | copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600); |
| 494 | chown("/cache/recovery/log", 1000, 1000); |
| 495 | chmod("/cache/recovery/log", 0600); |
| 496 | chmod("/cache/recovery/last_log", 0640); |
| 497 | } else { |
| 498 | LOGINFO("Failed to mount /cache for TWFunc::Update_Log_File\n"); |
| 499 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 500 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 501 | // Reset bootloader message |
| 502 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 503 | if (Part != NULL) { |
| 504 | struct bootloader_message boot; |
| 505 | memset(&boot, 0, sizeof(boot)); |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 506 | if (set_bootloader_message(&boot) != 0) |
| 507 | LOGERR("Unable to set bootloader message.\n"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 508 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 509 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 510 | if (PartitionManager.Mount_By_Path("/cache", true)) { |
| 511 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 512 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 513 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 514 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 515 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 516 | sync(); |
| 517 | } |
| 518 | |
| 519 | void TWFunc::Update_Intent_File(string Intent) { |
| 520 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
| 521 | TWFunc::write_file("/cache/recovery/intent", Intent); |
| 522 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 526 | int TWFunc::tw_reboot(RebootCommand command) |
| 527 | { |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 528 | DataManager::Flush(); |
| 529 | Update_Log_File(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 530 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 531 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 532 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 533 | switch (command) { |
| 534 | case rb_current: |
| 535 | case rb_system: |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 536 | Update_Intent_File("s"); |
| 537 | sync(); |
| 538 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 539 | #ifdef ANDROID_RB_PROPERTY |
| 540 | return property_set(ANDROID_RB_PROPERTY, "reboot,"); |
| 541 | #elif defined(ANDROID_RB_RESTART) |
| 542 | return android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 543 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 544 | return reboot(RB_AUTOBOOT); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 545 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 546 | case rb_recovery: |
| 547 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 548 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 549 | return property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 550 | #else |
| 551 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 552 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 553 | case rb_bootloader: |
| 554 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 555 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 556 | return property_set(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 557 | #else |
| 558 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 559 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 560 | case rb_poweroff: |
| 561 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 562 | #ifdef ANDROID_RB_PROPERTY |
| 563 | return property_set(ANDROID_RB_PROPERTY, "shutdown,"); |
| 564 | #elif defined(ANDROID_RB_POWEROFF) |
| 565 | return android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 566 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 567 | return reboot(RB_POWER_OFF); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 568 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 569 | case rb_download: |
| 570 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 571 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 572 | return property_set(ANDROID_RB_PROPERTY, "reboot,download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 573 | #else |
| 574 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 575 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 576 | default: |
| 577 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 578 | } |
| 579 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 583 | { |
| 584 | // Check for and run startup script if script exists |
| 585 | struct stat st; |
| 586 | if (stat(script_file, &st) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 587 | gui_msg(Msg("run_script=Running {1} script...")(display_name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 588 | 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] | 589 | TWFunc::Exec_Cmd(script_file); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 590 | gui_msg("done=Done."); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 591 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 592 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 593 | |
| 594 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 595 | DIR *d = opendir(path.c_str()); |
| 596 | int r = 0; |
| 597 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 598 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 599 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 600 | 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] | 601 | return -1; |
| 602 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 603 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 604 | if (d) { |
| 605 | struct dirent *p; |
| 606 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 607 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 608 | continue; |
| 609 | new_path = path + "/"; |
| 610 | new_path.append(p->d_name); |
| 611 | if (p->d_type == DT_DIR) { |
| 612 | r = removeDir(new_path, true); |
| 613 | if (!r) { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 614 | if (p->d_type == DT_DIR) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 615 | r = rmdir(new_path.c_str()); |
| 616 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 617 | 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] | 618 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 619 | } 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] | 620 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 621 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 622 | 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] | 623 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 627 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 628 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 629 | if (skipParent) |
| 630 | return 0; |
| 631 | else |
| 632 | r = rmdir(path.c_str()); |
| 633 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 634 | } |
| 635 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | int TWFunc::copy_file(string src, string dst, int mode) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 639 | 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] | 640 | ifstream srcfile(src.c_str(), ios::binary); |
| 641 | ofstream dstfile(dst.c_str(), ios::binary); |
| 642 | dstfile << srcfile.rdbuf(); |
| 643 | srcfile.close(); |
| 644 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 645 | if (chmod(dst.c_str(), mode) != 0) |
| 646 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 647 | return 0; |
| 648 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 649 | |
| 650 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 651 | struct stat st; |
| 652 | |
| 653 | stat(Path.c_str(), &st); |
| 654 | if (st.st_mode & S_IFDIR) |
| 655 | return DT_DIR; |
| 656 | else if (st.st_mode & S_IFBLK) |
| 657 | return DT_BLK; |
| 658 | else if (st.st_mode & S_IFCHR) |
| 659 | return DT_CHR; |
| 660 | else if (st.st_mode & S_IFIFO) |
| 661 | return DT_FIFO; |
| 662 | else if (st.st_mode & S_IFLNK) |
| 663 | return DT_LNK; |
| 664 | else if (st.st_mode & S_IFREG) |
| 665 | return DT_REG; |
| 666 | else if (st.st_mode & S_IFSOCK) |
| 667 | return DT_SOCK; |
| 668 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 669 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 670 | |
| 671 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 672 | ifstream file; |
| 673 | file.open(fn.c_str(), ios::in); |
| 674 | |
| 675 | if (file.is_open()) { |
| 676 | file >> results; |
| 677 | file.close(); |
| 678 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 679 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 680 | |
| 681 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 682 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 686 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 687 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 688 | file.open(fn.c_str(), ios::in); |
| 689 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 690 | while (getline(file, line)) |
| 691 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 692 | file.close(); |
| 693 | return 0; |
| 694 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 695 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 696 | return -1; |
| 697 | } |
| 698 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 699 | int TWFunc::read_file(string fn, uint64_t& results) { |
| 700 | ifstream file; |
| 701 | file.open(fn.c_str(), ios::in); |
| 702 | |
| 703 | if (file.is_open()) { |
| 704 | file >> results; |
| 705 | file.close(); |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 710 | return -1; |
| 711 | } |
| 712 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 713 | int TWFunc::write_file(string fn, string& line) { |
| 714 | FILE *file; |
| 715 | file = fopen(fn.c_str(), "w"); |
| 716 | if (file != NULL) { |
| 717 | fwrite(line.c_str(), line.size(), 1, file); |
| 718 | fclose(file); |
| 719 | return 0; |
| 720 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 721 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 722 | return -1; |
| 723 | } |
| 724 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 725 | bool TWFunc::Install_SuperSU(void) { |
| 726 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 727 | return false; |
| 728 | |
Ethan Yonker | e3e8829 | 2014-12-10 16:17:55 -0600 | [diff] [blame] | 729 | check_and_run_script("/supersu/install-supersu.sh", "SuperSU"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 730 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 731 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 732 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 733 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 734 | DIR* d; |
| 735 | |
| 736 | string Filename; |
| 737 | Restore_Path += "/"; |
| 738 | d = opendir(Restore_Path.c_str()); |
| 739 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 740 | 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] | 741 | return false; |
| 742 | } |
| 743 | |
| 744 | struct dirent* de; |
| 745 | while ((de = readdir(d)) != NULL) { |
| 746 | Filename = Restore_Path; |
| 747 | Filename += de->d_name; |
| 748 | if (TWFunc::Get_File_Type(Filename) == 2) { |
| 749 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 750 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 751 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 752 | closedir(d); |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | closedir(d); |
| 758 | return true; |
| 759 | } |
| 760 | |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 761 | string TWFunc::Get_Current_Date() { |
| 762 | string Current_Date; |
| 763 | time_t seconds = time(0); |
| 764 | struct tm *t = localtime(&seconds); |
| 765 | char timestamp[255]; |
| 766 | 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); |
| 767 | Current_Date = timestamp; |
| 768 | return Current_Date; |
| 769 | } |
| 770 | |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 771 | string TWFunc::System_Property_Get(string Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 772 | bool mount_state = PartitionManager.Is_Mounted_By_Path("/system"); |
| 773 | std::vector<string> buildprop; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 774 | string propvalue; |
| 775 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 776 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 777 | if (TWFunc::read_file("/system/build.prop", buildprop) != 0) { |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 778 | 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] | 779 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 780 | if (!mount_state) |
| 781 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 782 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 783 | } |
| 784 | int line_count = buildprop.size(); |
| 785 | int index; |
| 786 | size_t start_pos = 0, end_pos; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 787 | string propname; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 788 | for (index = 0; index < line_count; index++) { |
| 789 | end_pos = buildprop.at(index).find("=", start_pos); |
| 790 | propname = buildprop.at(index).substr(start_pos, end_pos); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 791 | if (propname == Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 792 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 793 | if (!mount_state) |
| 794 | PartitionManager.UnMount_By_Path("/system", false); |
| 795 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 798 | if (!mount_state) |
| 799 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 800 | return propvalue; |
| 801 | } |
| 802 | |
| 803 | void TWFunc::Auto_Generate_Backup_Name() { |
| 804 | string propvalue = System_Property_Get("ro.build.display.id"); |
| 805 | if (propvalue.empty()) { |
| 806 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 807 | return; |
| 808 | } |
| 809 | string Backup_Name = Get_Current_Date(); |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 810 | Backup_Name += "_" + propvalue; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 811 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 812 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 813 | // Trailing spaces cause problems on some file systems, so remove them |
| 814 | string space_check, space = " "; |
| 815 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 816 | while (space_check == space) { |
| 817 | Backup_Name.resize(Backup_Name.size() - 1); |
| 818 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 819 | } |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 820 | replace(Backup_Name.begin(), Backup_Name.end(), ' ', '_'); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 821 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 822 | if (PartitionManager.Check_Backup_Name(false) != 0) { |
| 823 | LOGINFO("Auto generated backup name '%s' contains invalid characters, using date instead.\n", Backup_Name.c_str()); |
| 824 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 825 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 826 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 827 | |
| 828 | void TWFunc::Fixup_Time_On_Boot() |
| 829 | { |
| 830 | #ifdef QCOM_RTC_FIX |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 831 | |
| 832 | LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 833 | |
| 834 | struct timeval tv; |
| 835 | uint64_t offset = 0; |
| 836 | std::string sepoch = "/sys/class/rtc/rtc0/since_epoch"; |
| 837 | |
| 838 | if (TWFunc::read_file(sepoch, offset) == 0) { |
| 839 | |
| 840 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str()); |
| 841 | |
| 842 | tv.tv_sec = offset; |
| 843 | tv.tv_usec = 0; |
| 844 | settimeofday(&tv, NULL); |
| 845 | |
| 846 | gettimeofday(&tv, NULL); |
| 847 | |
| 848 | if (tv.tv_sec > 1405209403) { // Anything older then 12 Jul 2014 23:56:43 GMT will do nicely thank you ;) |
| 849 | |
| 850 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 851 | return; |
| 852 | |
| 853 | } |
| 854 | |
| 855 | } else { |
| 856 | |
| 857 | LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str()); |
| 858 | |
| 859 | } |
| 860 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 861 | LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n"); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 862 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 863 | // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC. |
| 864 | // They never set it, it just ticks forward from 1970-01-01 00:00, |
| 865 | // and then they have files /data/system/time/ats_* with 64bit offset |
| 866 | // in miliseconds which, when added to the RTC, gives the correct time. |
| 867 | // So, the time is: (offset_from_ats + value_from_RTC) |
| 868 | // There are multiple ats files, they are for different systems? Bases? |
| 869 | // Like, ats_1 is for modem and ats_2 is for TOD (time of day?). |
| 870 | // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services |
| 871 | |
| 872 | static const char *paths[] = { "/data/system/time/", "/data/time/" }; |
| 873 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 874 | FILE *f; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 875 | DIR *d; |
| 876 | offset = 0; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 877 | struct dirent *dt; |
| 878 | std::string ats_path; |
| 879 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 880 | if(!PartitionManager.Mount_By_Path("/data", false)) |
| 881 | return; |
| 882 | |
| 883 | // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead |
| 884 | // - it is the one for ATS_TOD (time of day?). |
| 885 | // However, I never saw a device where the offset differs between ats files. |
| 886 | for(size_t i = 0; i < (sizeof(paths)/sizeof(paths[0])); ++i) |
| 887 | { |
| 888 | DIR *d = opendir(paths[i]); |
| 889 | if(!d) |
| 890 | continue; |
| 891 | |
| 892 | while((dt = readdir(d))) |
| 893 | { |
| 894 | if(dt->d_type != DT_REG || strncmp(dt->d_name, "ats_", 4) != 0) |
| 895 | continue; |
| 896 | |
| 897 | if(ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0) |
| 898 | ats_path = std::string(paths[i]).append(dt->d_name); |
| 899 | } |
| 900 | |
| 901 | closedir(d); |
| 902 | } |
| 903 | |
| 904 | if(ats_path.empty()) |
| 905 | { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 906 | LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n"); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 907 | return; |
| 908 | } |
| 909 | |
| 910 | f = fopen(ats_path.c_str(), "r"); |
| 911 | if(!f) |
| 912 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 913 | 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] | 914 | return; |
| 915 | } |
| 916 | |
| 917 | if(fread(&offset, sizeof(offset), 1, f) != 1) |
| 918 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 919 | 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] | 920 | fclose(f); |
| 921 | return; |
| 922 | } |
| 923 | fclose(f); |
| 924 | |
| 925 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), offset); |
| 926 | |
| 927 | gettimeofday(&tv, NULL); |
| 928 | |
| 929 | tv.tv_sec += offset/1000; |
| 930 | tv.tv_usec += (offset%1000)*1000; |
| 931 | |
| 932 | while(tv.tv_usec >= 1000000) |
| 933 | { |
| 934 | ++tv.tv_sec; |
| 935 | tv.tv_usec -= 1000000; |
| 936 | } |
| 937 | |
| 938 | settimeofday(&tv, NULL); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 939 | |
| 940 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 941 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 942 | #endif |
| 943 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 944 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 945 | std::vector<std::string> TWFunc::Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty) |
| 946 | { |
| 947 | std::vector<std::string> res; |
| 948 | size_t idx = 0, idx_last = 0; |
| 949 | |
| 950 | while(idx < str.size()) |
| 951 | { |
| 952 | idx = str.find_first_of(delimiter, idx_last); |
| 953 | if(idx == std::string::npos) |
| 954 | idx = str.size(); |
| 955 | |
| 956 | if(idx-idx_last != 0 || !removeEmpty) |
| 957 | res.push_back(str.substr(idx_last, idx-idx_last)); |
| 958 | |
| 959 | idx_last = idx + delimiter.size(); |
| 960 | } |
| 961 | |
| 962 | return res; |
| 963 | } |
| 964 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 965 | bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t uid, gid_t gid) |
| 966 | { |
| 967 | std::vector<std::string> parts = Split_String(path, "/"); |
| 968 | std::string cur_path; |
| 969 | struct stat info; |
| 970 | for(size_t i = 0; i < parts.size(); ++i) |
| 971 | { |
| 972 | cur_path += "/" + parts[i]; |
| 973 | if(stat(cur_path.c_str(), &info) < 0 || !S_ISDIR(info.st_mode)) |
| 974 | { |
| 975 | if(mkdir(cur_path.c_str(), mode) < 0) |
| 976 | return false; |
| 977 | chown(cur_path.c_str(), uid, gid); |
| 978 | } |
| 979 | } |
| 980 | return true; |
| 981 | } |
| 982 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 983 | int TWFunc::Set_Brightness(std::string brightness_value) |
| 984 | { |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 985 | int result = -1; |
| 986 | std::string secondary_brightness_file; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 987 | |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 988 | if (DataManager::GetIntValue("tw_has_brightnesss_file")) { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 989 | LOGINFO("TWFunc::Set_Brightness: Setting brightness control to %s\n", brightness_value.c_str()); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 990 | result = TWFunc::write_file(DataManager::GetStrValue("tw_brightness_file"), brightness_value); |
| 991 | DataManager::GetValue("tw_secondary_brightness_file", secondary_brightness_file); |
| 992 | if (!secondary_brightness_file.empty()) { |
| 993 | LOGINFO("TWFunc::Set_Brightness: Setting secondary brightness control to %s\n", brightness_value.c_str()); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 994 | TWFunc::write_file(secondary_brightness_file, brightness_value); |
| 995 | } |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 996 | } |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 997 | return result; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 998 | } |
| 999 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1000 | bool TWFunc::Toggle_MTP(bool enable) { |
| 1001 | #ifdef TW_HAS_MTP |
| 1002 | static int was_enabled = false; |
| 1003 | |
| 1004 | if (enable && was_enabled) { |
| 1005 | if (!PartitionManager.Enable_MTP()) |
| 1006 | PartitionManager.Disable_MTP(); |
| 1007 | } else { |
| 1008 | was_enabled = DataManager::GetIntValue("tw_mtp_enabled"); |
| 1009 | PartitionManager.Disable_MTP(); |
| 1010 | usleep(500); |
| 1011 | } |
| 1012 | return was_enabled; |
| 1013 | #else |
| 1014 | return false; |
| 1015 | #endif |
| 1016 | } |
| 1017 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 1018 | void TWFunc::SetPerformanceMode(bool mode) { |
| 1019 | if (mode) { |
| 1020 | property_set("recovery.perf.mode", "1"); |
| 1021 | } else { |
| 1022 | property_set("recovery.perf.mode", "0"); |
| 1023 | } |
| 1024 | // Some time for events to catch up to init handlers |
| 1025 | usleep(500000); |
| 1026 | } |
| 1027 | |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 1028 | std::string TWFunc::to_string(unsigned long value) { |
| 1029 | std::ostringstream os; |
| 1030 | os << value; |
| 1031 | return os.str(); |
| 1032 | } |
| 1033 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1034 | void TWFunc::Disable_Stock_Recovery_Replace(void) { |
| 1035 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 1036 | // Disable flashing of stock recovery |
| 1037 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
| 1038 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1039 | 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] | 1040 | sync(); |
| 1041 | } |
| 1042 | PartitionManager.UnMount_By_Path("/system", false); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1046 | unsigned long long TWFunc::IOCTL_Get_Block_Size(const char* block_device) { |
| 1047 | unsigned long block_device_size; |
| 1048 | int ret = 0; |
| 1049 | |
| 1050 | int fd = open(block_device, O_RDONLY); |
| 1051 | if (fd < 0) { |
| 1052 | LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", block_device, strerror(errno)); |
| 1053 | } else { |
| 1054 | ret = ioctl(fd, BLKGETSIZE, &block_device_size); |
| 1055 | close(fd); |
| 1056 | if (ret) { |
| 1057 | LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno)); |
| 1058 | } else { |
| 1059 | return (unsigned long long)(block_device_size) * 512LLU; |
| 1060 | } |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1065 | #endif // ndef BUILD_TWRPTAR_MAIN |