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