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