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