Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 2 | Copyright 2012-2020 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 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> |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 40 | #include <selinux/label.h> |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 41 | |
| 42 | #include <android-base/strings.h> |
| 43 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 44 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 45 | #include "twcommon.h" |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 46 | #include "gui/gui.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 47 | #ifndef BUILD_TWRPTAR_MAIN |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 48 | #include "data.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 49 | #include "partitions.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 50 | #include "variables.h" |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 51 | #include "bootloader_message/include/bootloader_message/bootloader_message.h" |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 52 | #include "cutils/properties.h" |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 53 | #include "cutils/android_reboot.h" |
| 54 | #include <sys/reboot.h> |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 55 | #endif // ndef BUILD_TWRPTAR_MAIN |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 56 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 57 | #include "openaes/inc/oaes_lib.h" |
| 58 | #endif |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 59 | #include "set_metadata.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 60 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 61 | extern "C" { |
| 62 | #include "libcrecovery/common.h" |
| 63 | } |
| 64 | |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 65 | #ifdef TW_INCLUDE_LIBRESETPROP |
bigbiff | a05d32c | 2021-12-25 15:43:34 -0500 | [diff] [blame] | 66 | #include <resetprop.hpp> |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 69 | struct selabel_handle *selinux_handle; |
| 70 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 71 | /* Execute a command */ |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 72 | int TWFunc::Exec_Cmd(const string& cmd, string &result, bool combine_stderr) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 73 | FILE* exec; |
| 74 | char buffer[130]; |
| 75 | int ret = 0; |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 76 | std::string popen_cmd = cmd; |
| 77 | if (combine_stderr) |
| 78 | popen_cmd = cmd + " 2>&1"; |
| 79 | exec = __popen(popen_cmd.c_str(), "r"); |
| 80 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 81 | while (!feof(exec)) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 82 | if (fgets(buffer, 128, exec) != NULL) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 83 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 84 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 85 | } |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 86 | ret = __pclose(exec); |
| 87 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 90 | int TWFunc::Exec_Cmd(const string& cmd, bool Show_Errors) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 91 | pid_t pid; |
| 92 | int status; |
| 93 | switch(pid = fork()) |
| 94 | { |
| 95 | case -1: |
bigbiff bigbiff | 731df79 | 2014-02-20 18:26:13 -0500 | [diff] [blame] | 96 | LOGERR("Exec_Cmd(): vfork failed: %d!\n", errno); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 97 | return -1; |
| 98 | case 0: // child |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 99 | execl("/system/bin/sh", "sh", "-c", cmd.c_str(), NULL); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 100 | _exit(127); |
| 101 | break; |
| 102 | default: |
| 103 | { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 104 | if (TWFunc::Wait_For_Child(pid, &status, cmd, Show_Errors) != 0) |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 105 | return -1; |
| 106 | else |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 112 | // Returns "file.name" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 113 | string TWFunc::Get_Filename(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 114 | size_t pos = Path.find_last_of("/"); |
| 115 | if (pos != string::npos) { |
| 116 | string Filename; |
| 117 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 118 | return Filename; |
| 119 | } else |
| 120 | return Path; |
| 121 | } |
| 122 | |
| 123 | // Returns "/path/to/" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 124 | string TWFunc::Get_Path(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 125 | size_t pos = Path.find_last_of("/"); |
| 126 | if (pos != string::npos) { |
| 127 | string Pathonly; |
| 128 | Pathonly = Path.substr(0, pos + 1); |
| 129 | return Pathonly; |
| 130 | } else |
| 131 | return Path; |
| 132 | } |
| 133 | |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 134 | int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 135 | pid_t rc_pid; |
| 136 | |
| 137 | rc_pid = waitpid(pid, status, 0); |
| 138 | if (rc_pid > 0) { |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 139 | if (WIFSIGNALED(*status)) { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 140 | if (Show_Errors) |
| 141 | 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] | 142 | return -1; |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 143 | } else if (WEXITSTATUS(*status) == 0) { |
| 144 | LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success |
| 145 | } else { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 146 | if (Show_Errors) |
| 147 | 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] | 148 | return -1; |
| 149 | } |
| 150 | } else { // no PID returned |
| 151 | if (errno == ECHILD) |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 152 | LOGERR("%s no child process exist\n", Child_Name.c_str()); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 153 | else { |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 154 | LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
Ethan Yonker | ddb63e2 | 2017-01-19 14:01:57 -0600 | [diff] [blame] | 161 | int TWFunc::Wait_For_Child_Timeout(pid_t pid, int *status, const string& Child_Name, int timeout) { |
| 162 | pid_t retpid = waitpid(pid, status, WNOHANG); |
| 163 | for (; retpid == 0 && timeout; --timeout) { |
| 164 | sleep(1); |
| 165 | retpid = waitpid(pid, status, WNOHANG); |
| 166 | } |
| 167 | if (retpid == 0 && timeout == 0) { |
| 168 | LOGERR("%s took too long, killing process\n", Child_Name.c_str()); |
| 169 | kill(pid, SIGKILL); |
Ethan Yonker | ddb63e2 | 2017-01-19 14:01:57 -0600 | [diff] [blame] | 170 | for (timeout = 5; retpid == 0 && timeout; --timeout) { |
| 171 | sleep(1); |
| 172 | retpid = waitpid(pid, status, WNOHANG); |
| 173 | } |
| 174 | if (retpid) |
| 175 | LOGINFO("Child process killed successfully\n"); |
| 176 | else |
| 177 | LOGINFO("Child process took too long to kill, may be a zombie process\n"); |
| 178 | return -1; |
| 179 | } else if (retpid > 0) { |
| 180 | if (WIFSIGNALED(*status)) { |
| 181 | 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 |
| 182 | return -1; |
| 183 | } |
| 184 | } else if (retpid < 0) { // no PID returned |
| 185 | if (errno == ECHILD) |
| 186 | LOGERR("%s no child process exist\n", Child_Name.c_str()); |
| 187 | else { |
| 188 | LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno); |
| 189 | return -1; |
| 190 | } |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 195 | bool TWFunc::Path_Exists(string Path) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 196 | struct stat st; |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 197 | return stat(Path.c_str(), &st) == 0; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 198 | } |
| 199 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 200 | Archive_Type TWFunc::Get_File_Type(string fn) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 201 | string::size_type i = 0; |
| 202 | int firstbyte = 0, secondbyte = 0; |
| 203 | char header[3]; |
| 204 | |
| 205 | ifstream f; |
| 206 | f.open(fn.c_str(), ios::in | ios::binary); |
| 207 | f.get(header, 3); |
| 208 | f.close(); |
| 209 | firstbyte = header[i] & 0xff; |
| 210 | secondbyte = header[++i] & 0xff; |
| 211 | |
| 212 | if (firstbyte == 0x1f && secondbyte == 0x8b) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 213 | return COMPRESSED; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 214 | else if (firstbyte == 0x4f && secondbyte == 0x41) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 215 | return ENCRYPTED; |
| 216 | return UNCOMPRESSED; // default |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int TWFunc::Try_Decrypting_File(string fn, string password) { |
| 220 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 221 | OAES_CTX * ctx = NULL; |
| 222 | uint8_t _key_data[32] = ""; |
| 223 | FILE *f; |
| 224 | uint8_t buffer[4096]; |
| 225 | uint8_t *buffer_out = NULL; |
| 226 | uint8_t *ptr = NULL; |
| 227 | size_t read_len = 0, out_len = 0; |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 228 | int firstbyte = 0, secondbyte = 0; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 229 | size_t _j = 0; |
| 230 | size_t _key_data_len = 0; |
| 231 | |
| 232 | // mostly kanged from OpenAES oaes.c |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 233 | for ( _j = 0; _j < 32; _j++ ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 234 | _key_data[_j] = _j + 1; |
| 235 | _key_data_len = password.size(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 236 | if ( 16 >= _key_data_len ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 237 | _key_data_len = 16; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 238 | else if ( 24 >= _key_data_len ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 239 | _key_data_len = 24; |
| 240 | else |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 241 | _key_data_len = 32; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 242 | memcpy(_key_data, password.c_str(), password.size()); |
| 243 | |
| 244 | ctx = oaes_alloc(); |
| 245 | if (ctx == NULL) { |
| 246 | LOGERR("Failed to allocate OAES\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | oaes_key_import_data(ctx, _key_data, _key_data_len); |
| 251 | |
| 252 | f = fopen(fn.c_str(), "rb"); |
| 253 | if (f == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 254 | 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] | 255 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 256 | return -1; |
| 257 | } |
| 258 | read_len = fread(buffer, sizeof(uint8_t), 4096, f); |
| 259 | if (read_len <= 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 260 | LOGERR("Read size during try decrypt failed: %s\n", strerror(errno)); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 261 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 262 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 263 | return -1; |
| 264 | } |
| 265 | if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) { |
| 266 | LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n"); |
| 267 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 268 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | buffer_out = (uint8_t *) calloc(out_len, sizeof(char)); |
| 272 | if (buffer_out == NULL) { |
| 273 | LOGERR("Failed to allocate output buffer for try decrypt.\n"); |
| 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 | return -1; |
| 277 | } |
| 278 | if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) { |
| 279 | LOGERR("Failed to decrypt file '%s'\n", fn.c_str()); |
| 280 | fclose(f); |
| 281 | free(buffer_out); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 282 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 286 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 287 | if (out_len < 2) { |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 288 | 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] | 289 | free(buffer_out); |
| 290 | return 1; // Decrypted successfully |
| 291 | } |
| 292 | ptr = buffer_out; |
| 293 | firstbyte = *ptr & 0xff; |
| 294 | ptr++; |
| 295 | secondbyte = *ptr & 0xff; |
| 296 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 297 | LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str()); |
| 298 | free(buffer_out); |
| 299 | return 3; // Compressed |
| 300 | } |
| 301 | if (out_len >= 262) { |
| 302 | ptr = buffer_out + 257; |
| 303 | if (strncmp((char*)ptr, "ustar", 5) == 0) { |
| 304 | LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str()); |
| 305 | free(buffer_out); |
| 306 | return 2; // Tar |
| 307 | } |
| 308 | } |
| 309 | free(buffer_out); |
| 310 | LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str()); |
| 311 | return 1; // Decrypted successfully |
| 312 | #else |
| 313 | LOGERR("Encrypted backup support not included.\n"); |
| 314 | return -1; |
| 315 | #endif |
| 316 | } |
| 317 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 318 | unsigned long TWFunc::Get_File_Size(const string& Path) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 319 | struct stat st; |
| 320 | |
| 321 | if (stat(Path.c_str(), &st) != 0) |
| 322 | return 0; |
| 323 | return st.st_size; |
| 324 | } |
| 325 | |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 326 | std::string TWFunc::Remove_Beginning_Slash(const std::string& path) { |
| 327 | std::string res; |
| 328 | size_t pos = path.find_first_of("/"); |
| 329 | if (pos != std::string::npos) { |
| 330 | res = path.substr(pos+1); |
| 331 | } |
| 332 | return res; |
| 333 | } |
| 334 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 335 | std::string TWFunc::Remove_Trailing_Slashes(const std::string& path, bool leaveLast) |
| 336 | { |
| 337 | std::string res; |
| 338 | size_t last_idx = 0, idx = 0; |
| 339 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 340 | while (last_idx != std::string::npos) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 341 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 342 | if (last_idx != 0) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 343 | res += '/'; |
| 344 | |
| 345 | idx = path.find_first_of('/', last_idx); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 346 | if (idx == std::string::npos) { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 347 | res += path.substr(last_idx, idx); |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | res += path.substr(last_idx, idx-last_idx); |
| 352 | last_idx = path.find_first_not_of('/', idx); |
| 353 | } |
| 354 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 355 | if (leaveLast) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 356 | res += '/'; |
| 357 | return res; |
| 358 | } |
| 359 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 360 | void TWFunc::Strip_Quotes(char* &str) { |
| 361 | if (strlen(str) > 0 && str[0] == '\"') |
| 362 | str++; |
| 363 | if (strlen(str) > 0 && str[strlen(str)-1] == '\"') |
| 364 | str[strlen(str)-1] = 0; |
| 365 | } |
| 366 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 367 | vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) { |
| 368 | vector<string> res; |
| 369 | |
| 370 | if (in.empty() || del == '\0') |
| 371 | return res; |
| 372 | |
| 373 | string field; |
| 374 | istringstream f(in); |
| 375 | if (del == '\n') { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 376 | while (getline(f, field)) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 377 | if (field.empty() && skip_empty) |
| 378 | continue; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 379 | res.push_back(field); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 380 | } |
| 381 | } else { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 382 | while (getline(f, field, del)) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 383 | if (field.empty() && skip_empty) |
| 384 | continue; |
| 385 | res.push_back(field); |
| 386 | } |
| 387 | } |
| 388 | return res; |
| 389 | } |
| 390 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 391 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 392 | { |
| 393 | timespec temp; |
| 394 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 395 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 396 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 397 | } else { |
| 398 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 399 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 400 | } |
| 401 | return temp; |
| 402 | } |
| 403 | |
| 404 | int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end) |
| 405 | { |
| 406 | return ((end.tv_sec * 1000) + end.tv_nsec/1000000) - |
| 407 | ((start.tv_sec * 1000) + start.tv_nsec/1000000); |
| 408 | } |
| 409 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 410 | #ifndef BUILD_TWRPTAR_MAIN |
| 411 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 412 | // Returns "/path" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 413 | string TWFunc::Get_Root_Path(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 414 | string Local_Path = Path; |
| 415 | |
| 416 | // Make sure that we have a leading slash |
| 417 | if (Local_Path.substr(0, 1) != "/") |
| 418 | Local_Path = "/" + Local_Path; |
| 419 | |
| 420 | // Trim the path to get the root path only |
| 421 | size_t position = Local_Path.find("/", 2); |
| 422 | if (position != string::npos) { |
| 423 | Local_Path.resize(position); |
| 424 | } |
| 425 | return Local_Path; |
| 426 | } |
| 427 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 428 | int TWFunc::Recursive_Mkdir(string Path) { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 429 | std::vector<std::string> parts = Split_String(Path, "/", true); |
| 430 | std::string cur_path; |
| 431 | for (size_t i = 0; i < parts.size(); ++i) { |
| 432 | cur_path += "/" + parts[i]; |
| 433 | if (!TWFunc::Path_Exists(cur_path)) { |
| 434 | if (mkdir(cur_path.c_str(), 0777)) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 435 | gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(cur_path)(strerror(errno))); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 436 | return false; |
| 437 | } else { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 438 | tw_set_default_metadata(cur_path.c_str()); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 439 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 440 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 441 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 442 | return true; |
| 443 | } |
| 444 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 445 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 446 | string Display_Text; |
| 447 | |
| 448 | DataManager::GetValue(Read_Value, Display_Text); |
| 449 | if (Display_Text.empty()) |
| 450 | Display_Text = Default_Text; |
| 451 | |
| 452 | DataManager::SetValue("tw_operation", Display_Text); |
| 453 | DataManager::SetValue("tw_partition", ""); |
| 454 | } |
| 455 | |
| 456 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 457 | string Display_Text; |
| 458 | |
| 459 | DataManager::GetValue(Read_Value, Display_Text); |
| 460 | if (Display_Text.empty()) |
| 461 | Display_Text = Default_Text; |
| 462 | |
| 463 | DataManager::SetValue("tw_operation", Display_Text); |
| 464 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 465 | } |
| 466 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 467 | void TWFunc::Copy_Log(string Source, string Destination) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 468 | int logPipe[2]; |
| 469 | int pigz_pid; |
| 470 | int destination_fd; |
| 471 | std::string destLogBuffer; |
| 472 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 473 | PartitionManager.Mount_By_Path(Destination, false); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 474 | |
| 475 | size_t extPos = Destination.find(".gz"); |
| 476 | std::string uncompressedLog(Destination); |
| 477 | uncompressedLog.replace(extPos, Destination.length(), ""); |
| 478 | |
| 479 | if (Path_Exists(Destination)) { |
| 480 | Archive_Type type = Get_File_Type(Destination); |
| 481 | if (type == COMPRESSED) { |
| 482 | std::string destFileBuffer; |
| 483 | std::string getCompressedContents = "pigz -c -d " + Destination; |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 484 | if (Exec_Cmd(getCompressedContents, destFileBuffer, false) < 0) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 485 | LOGINFO("Unable to get destination logfile contents.\n"); |
| 486 | return; |
| 487 | } |
| 488 | destLogBuffer.append(destFileBuffer); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 489 | } |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 490 | } else if (Path_Exists(uncompressedLog)) { |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 491 | std::ifstream uncompressedIfs(uncompressedLog.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 492 | std::stringstream uncompressedSS; |
| 493 | uncompressedSS << uncompressedIfs.rdbuf(); |
| 494 | uncompressedIfs.close(); |
| 495 | std::string uncompressedLogBuffer(uncompressedSS.str()); |
| 496 | destLogBuffer.append(uncompressedLogBuffer); |
| 497 | std::remove(uncompressedLog.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 498 | } |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 499 | |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 500 | std::ifstream ifs(Source.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 501 | std::stringstream ss; |
| 502 | ss << ifs.rdbuf(); |
| 503 | std::string srcLogBuffer(ss.str()); |
| 504 | ifs.close(); |
| 505 | |
| 506 | if (pipe(logPipe) < 0) { |
| 507 | LOGINFO("Unable to open pipe to write to persistent log file: %s\n", Destination.c_str()); |
| 508 | } |
| 509 | |
| 510 | destination_fd = open(Destination.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 511 | |
| 512 | pigz_pid = fork(); |
| 513 | if (pigz_pid < 0) { |
| 514 | LOGINFO("fork() failed\n"); |
| 515 | close(destination_fd); |
| 516 | close(logPipe[0]); |
| 517 | close(logPipe[1]); |
| 518 | } else if (pigz_pid == 0) { |
| 519 | close(logPipe[1]); |
| 520 | dup2(logPipe[0], fileno(stdin)); |
| 521 | dup2(destination_fd, fileno(stdout)); |
| 522 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 523 | close(destination_fd); |
| 524 | close(logPipe[0]); |
| 525 | _exit(-1); |
| 526 | } |
| 527 | } else { |
| 528 | close(logPipe[0]); |
| 529 | if (write(logPipe[1], destLogBuffer.c_str(), destLogBuffer.size()) < 0) { |
| 530 | LOGINFO("Unable to append to persistent log: %s\n", Destination.c_str()); |
| 531 | close(logPipe[1]); |
| 532 | close(destination_fd); |
| 533 | return; |
| 534 | } |
| 535 | if (write(logPipe[1], srcLogBuffer.c_str(), srcLogBuffer.size()) < 0) { |
| 536 | LOGINFO("Unable to append to persistent log: %s\n", Destination.c_str()); |
| 537 | close(logPipe[1]); |
| 538 | close(destination_fd); |
| 539 | return; |
| 540 | } |
| 541 | close(logPipe[1]); |
| 542 | } |
| 543 | close(destination_fd); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 546 | void TWFunc::Update_Log_File(void) { |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 547 | std::string recoveryDir = get_log_dir() + "recovery/"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 548 | |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 549 | if (get_log_dir() == CACHE_LOGS_DIR) { |
| 550 | if (!PartitionManager.Mount_By_Path(CACHE_LOGS_DIR, false)) { |
| 551 | LOGINFO("Failed to mount %s for TWFunc::Update_Log_File\n", CACHE_LOGS_DIR); |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 552 | } |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 553 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 554 | |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 555 | if (!TWFunc::Path_Exists(recoveryDir)) { |
| 556 | LOGINFO("Recreating %s folder.\n", recoveryDir.c_str()); |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 557 | if (!Create_Dir_Recursive(recoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, 0, 0)) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 558 | LOGINFO("Unable to create %s folder.\n", recoveryDir.c_str()); |
| 559 | } |
| 560 | } |
| 561 | |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 562 | std::string logCopy = recoveryDir + "log.gz"; |
| 563 | std::string lastLogCopy = recoveryDir + "last_log.gz"; |
Ethan Yonker | 5f3b8f0 | 2021-08-16 15:10:10 -0500 | [diff] [blame] | 564 | copy_file(logCopy, lastLogCopy, 0600); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 565 | Copy_Log(TMP_LOG_FILE, logCopy); |
| 566 | chown(logCopy.c_str(), 1000, 1000); |
| 567 | chmod(logCopy.c_str(), 0600); |
| 568 | chmod(lastLogCopy.c_str(), 0640); |
| 569 | |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 570 | if (get_log_dir() == CACHE_LOGS_DIR) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 571 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 572 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 573 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 574 | } |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 575 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 576 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 577 | sync(); |
| 578 | } |
| 579 | |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 580 | void TWFunc::Clear_Bootloader_Message() { |
| 581 | std::string err; |
| 582 | if (!clear_bootloader_message(&err)) { |
Ian Macdonald | d485182 | 2020-11-02 08:52:32 +0100 | [diff] [blame] | 583 | LOGINFO("%s\n", err.c_str()); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 587 | void TWFunc::Update_Intent_File(string Intent) { |
| 588 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 589 | TWFunc::write_to_file("/cache/recovery/intent", Intent); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 590 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 594 | int TWFunc::tw_reboot(RebootCommand command) |
| 595 | { |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 596 | DataManager::Flush(); |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 597 | Update_Log_File(); |
| 598 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 599 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 600 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 601 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 602 | switch (command) { |
| 603 | case rb_current: |
| 604 | case rb_system: |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 605 | Update_Intent_File("s"); |
| 606 | sync(); |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 607 | check_and_run_script("/system/bin/rebootsystem.sh", "reboot system"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 608 | #ifdef ANDROID_RB_PROPERTY |
| 609 | return property_set(ANDROID_RB_PROPERTY, "reboot,"); |
| 610 | #elif defined(ANDROID_RB_RESTART) |
| 611 | return android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 612 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 613 | return reboot(RB_AUTOBOOT); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 614 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 615 | case rb_recovery: |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 616 | check_and_run_script("/system/bin/rebootrecovery.sh", "reboot recovery"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 617 | return property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 618 | case rb_bootloader: |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 619 | check_and_run_script("/system/bin/rebootbootloader.sh", "reboot bootloader"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 620 | return property_set(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 621 | case rb_poweroff: |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 622 | check_and_run_script("/system/bin/poweroff.sh", "power off"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 623 | #ifdef ANDROID_RB_PROPERTY |
| 624 | return property_set(ANDROID_RB_PROPERTY, "shutdown,"); |
| 625 | #elif defined(ANDROID_RB_POWEROFF) |
| 626 | return android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 627 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 628 | return reboot(RB_POWER_OFF); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 629 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 630 | case rb_download: |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 631 | check_and_run_script("/system/bin/rebootdownload.sh", "reboot download"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 632 | return property_set(ANDROID_RB_PROPERTY, "reboot,download"); |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 633 | case rb_edl: |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 634 | check_and_run_script("/system/bin/rebootedl.sh", "reboot edl"); |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 635 | return property_set(ANDROID_RB_PROPERTY, "reboot,edl"); |
bigbiff | df8436b | 2020-08-30 16:22:34 -0400 | [diff] [blame] | 636 | case rb_fastboot: |
| 637 | return property_set(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 638 | default: |
| 639 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 640 | } |
| 641 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 645 | { |
| 646 | // Check for and run startup script if script exists |
| 647 | struct stat st; |
| 648 | if (stat(script_file, &st) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 649 | gui_msg(Msg("run_script=Running {1} script...")(display_name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 650 | 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] | 651 | TWFunc::Exec_Cmd(script_file); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 652 | gui_msg("done=Done."); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 653 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 654 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 655 | |
| 656 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 657 | DIR *d = opendir(path.c_str()); |
| 658 | int r = 0; |
| 659 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 660 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 661 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 662 | 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] | 663 | return -1; |
| 664 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 665 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 666 | if (d) { |
| 667 | struct dirent *p; |
| 668 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 669 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 670 | continue; |
| 671 | new_path = path + "/"; |
| 672 | new_path.append(p->d_name); |
| 673 | if (p->d_type == DT_DIR) { |
| 674 | r = removeDir(new_path, true); |
| 675 | if (!r) { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 676 | if (p->d_type == DT_DIR) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 677 | r = rmdir(new_path.c_str()); |
| 678 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 679 | 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] | 680 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 681 | } 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] | 682 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 683 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 684 | 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] | 685 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 689 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 690 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 691 | if (skipParent) |
| 692 | return 0; |
| 693 | else |
| 694 | r = rmdir(path.c_str()); |
| 695 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 696 | } |
| 697 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 698 | } |
| 699 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 700 | int TWFunc::copy_file(string src, string dst, int mode, bool mount_paths) { |
| 701 | if (mount_paths) { |
| 702 | PartitionManager.Mount_By_Path(src, false); |
| 703 | PartitionManager.Mount_By_Path(dst, false); |
| 704 | } |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 705 | if (!Path_Exists(src)) { |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 706 | LOGINFO("Path %s does not exist. Unable to copy file to %s\n", src.c_str(), dst.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 707 | return -1; |
| 708 | } |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 709 | std::ifstream srcfile(src.c_str(), ios::binary); |
| 710 | std::ofstream dstfile(dst.c_str(), ios::binary); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 711 | dstfile << srcfile.rdbuf(); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 712 | if (dstfile.bad()) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 713 | LOGINFO("Unable to copy file %s to %s\n", src.c_str(), dst.c_str()); |
| 714 | return -1; |
| 715 | } |
| 716 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 717 | srcfile.close(); |
| 718 | dstfile.close(); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 719 | if (chmod(dst.c_str(), mode) != 0) { |
| 720 | LOGERR("Unable to chmod file: %s. Error: %s\n", dst.c_str(), strerror(errno)); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 721 | return -1; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 722 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 723 | return 0; |
| 724 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 725 | |
| 726 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 727 | struct stat st; |
| 728 | |
| 729 | stat(Path.c_str(), &st); |
| 730 | if (st.st_mode & S_IFDIR) |
| 731 | return DT_DIR; |
| 732 | else if (st.st_mode & S_IFBLK) |
| 733 | return DT_BLK; |
| 734 | else if (st.st_mode & S_IFCHR) |
| 735 | return DT_CHR; |
| 736 | else if (st.st_mode & S_IFIFO) |
| 737 | return DT_FIFO; |
| 738 | else if (st.st_mode & S_IFLNK) |
| 739 | return DT_LNK; |
| 740 | else if (st.st_mode & S_IFREG) |
| 741 | return DT_REG; |
| 742 | else if (st.st_mode & S_IFSOCK) |
| 743 | return DT_SOCK; |
| 744 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 745 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 746 | |
| 747 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 748 | ifstream file; |
| 749 | file.open(fn.c_str(), ios::in); |
| 750 | |
| 751 | if (file.is_open()) { |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 752 | std::string line; |
| 753 | while (std::getline(file, line)) { |
| 754 | results += line; |
| 755 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 756 | file.close(); |
| 757 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 758 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 759 | |
| 760 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 761 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 765 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 766 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 767 | file.open(fn.c_str(), ios::in); |
| 768 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 769 | while (getline(file, line)) |
| 770 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 771 | file.close(); |
| 772 | return 0; |
| 773 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 774 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 775 | return -1; |
| 776 | } |
| 777 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 778 | int TWFunc::read_file(string fn, uint64_t& results) { |
| 779 | ifstream file; |
| 780 | file.open(fn.c_str(), ios::in); |
| 781 | |
| 782 | if (file.is_open()) { |
| 783 | file >> results; |
| 784 | file.close(); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 789 | return -1; |
| 790 | } |
| 791 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 792 | bool TWFunc::write_to_file(const string& fn, const string& line) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 793 | FILE *file; |
| 794 | file = fopen(fn.c_str(), "w"); |
| 795 | if (file != NULL) { |
| 796 | fwrite(line.c_str(), line.size(), 1, file); |
| 797 | fclose(file); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 798 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 799 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 800 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 801 | return false; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 802 | } |
| 803 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 804 | bool TWFunc::write_to_file(const string& fn, const std::vector<string> lines) { |
| 805 | FILE *file; |
| 806 | file = fopen(fn.c_str(), "a+"); |
| 807 | if (file != NULL) { |
| 808 | for (auto&& line: lines) { |
| 809 | fwrite(line.c_str(), line.size(), 1, file); |
| 810 | fwrite("\n", sizeof(char), 1, file); |
| 811 | } |
| 812 | fclose(file); |
| 813 | return true; |
| 814 | } |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 819 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 820 | DIR* d; |
| 821 | |
| 822 | string Filename; |
| 823 | Restore_Path += "/"; |
| 824 | d = opendir(Restore_Path.c_str()); |
| 825 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 826 | 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] | 827 | return false; |
| 828 | } |
| 829 | |
| 830 | struct dirent* de; |
| 831 | while ((de = readdir(d)) != NULL) { |
| 832 | Filename = Restore_Path; |
| 833 | Filename += de->d_name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 834 | if (TWFunc::Get_File_Type(Filename) == ENCRYPTED) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 835 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 836 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 837 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 838 | closedir(d); |
| 839 | return false; |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | closedir(d); |
| 844 | return true; |
| 845 | } |
| 846 | |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 847 | string TWFunc::Get_Current_Date() { |
| 848 | string Current_Date; |
| 849 | time_t seconds = time(0); |
| 850 | struct tm *t = localtime(&seconds); |
| 851 | char timestamp[255]; |
| 852 | 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); |
| 853 | Current_Date = timestamp; |
| 854 | return Current_Date; |
| 855 | } |
| 856 | |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 857 | string TWFunc::System_Property_Get(string Prop_Name) { |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame^] | 858 | return System_Property_Get(Prop_Name, PartitionManager, PartitionManager.Get_Android_Root_Path(), ""); |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 859 | } |
| 860 | |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame^] | 861 | string TWFunc::System_Property_Get(string Prop_Name, TWPartitionManager &PartitionManager, string Mount_Point, string prop_file_name) { |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 862 | bool mount_state = PartitionManager.Is_Mounted_By_Path(Mount_Point); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 863 | std::vector<string> buildprop; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 864 | string propvalue; |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 865 | if (!PartitionManager.Mount_By_Path(Mount_Point, true)) |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 866 | return propvalue; |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame^] | 867 | string prop_file = Mount_Point + "/system/" + prop_file_name; |
| 868 | if (!TWFunc::Path_Exists(prop_file)) { |
| 869 | LOGINFO("Unable to locate file: %s\n", prop_file.c_str()); |
| 870 | return propvalue; |
| 871 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 872 | if (TWFunc::read_file(prop_file, buildprop) != 0) { |
Captain Throwback | 2b9ea72 | 2021-12-31 21:35:52 -0500 | [diff] [blame^] | 873 | LOGINFO("Unable to open %s for getting '%s'.\n", prop_file_name.c_str(), Prop_Name.c_str()); |
Vojtech Bocek | a2e7016 | 2013-09-17 17:05:10 +0200 | [diff] [blame] | 874 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 875 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 876 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 877 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 878 | } |
| 879 | int line_count = buildprop.size(); |
| 880 | int index; |
| 881 | size_t start_pos = 0, end_pos; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 882 | string propname; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 883 | for (index = 0; index < line_count; index++) { |
| 884 | end_pos = buildprop.at(index).find("=", start_pos); |
| 885 | propname = buildprop.at(index).substr(start_pos, end_pos); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 886 | if (propname == Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 887 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 888 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 889 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 890 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 893 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 894 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 895 | return propvalue; |
| 896 | } |
| 897 | |
| 898 | void TWFunc::Auto_Generate_Backup_Name() { |
| 899 | string propvalue = System_Property_Get("ro.build.display.id"); |
| 900 | if (propvalue.empty()) { |
| 901 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 902 | return; |
| 903 | } |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 904 | else { |
| 905 | //remove periods from build display so it doesn't confuse the extension code |
| 906 | propvalue.erase(remove(propvalue.begin(), propvalue.end(), '.'), propvalue.end()); |
| 907 | } |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 908 | string Backup_Name = Get_Current_Date(); |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 909 | Backup_Name += "_" + propvalue; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 910 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 911 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 912 | // Trailing spaces cause problems on some file systems, so remove them |
| 913 | string space_check, space = " "; |
| 914 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 915 | while (space_check == space) { |
| 916 | Backup_Name.resize(Backup_Name.size() - 1); |
| 917 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 918 | } |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 919 | replace(Backup_Name.begin(), Backup_Name.end(), ' ', '_'); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 920 | if (PartitionManager.Check_Backup_Name(Backup_Name, false, true) != 0) { |
| 921 | LOGINFO("Auto generated backup name '%s' is not valid, using date instead.\n", Backup_Name.c_str()); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 922 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 923 | } else { |
| 924 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 925 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 926 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 927 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 928 | void TWFunc::Fixup_Time_On_Boot(const string& time_paths /* = "" */) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 929 | { |
| 930 | #ifdef QCOM_RTC_FIX |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 931 | static bool fixed = false; |
| 932 | if (fixed) |
| 933 | return; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 934 | |
| 935 | LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 936 | |
| 937 | struct timeval tv; |
| 938 | uint64_t offset = 0; |
| 939 | std::string sepoch = "/sys/class/rtc/rtc0/since_epoch"; |
| 940 | |
| 941 | if (TWFunc::read_file(sepoch, offset) == 0) { |
| 942 | |
| 943 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str()); |
| 944 | |
| 945 | tv.tv_sec = offset; |
| 946 | tv.tv_usec = 0; |
| 947 | settimeofday(&tv, NULL); |
| 948 | |
| 949 | gettimeofday(&tv, NULL); |
| 950 | |
Phoenix591 | 46b05f2 | 2018-02-03 06:41:08 +0000 | [diff] [blame] | 951 | 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] | 952 | |
| 953 | 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] | 954 | fixed = true; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 955 | return; |
| 956 | |
| 957 | } |
| 958 | |
| 959 | } else { |
| 960 | |
| 961 | LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str()); |
| 962 | |
| 963 | } |
| 964 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 965 | LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n"); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 966 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 967 | // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC. |
| 968 | // They never set it, it just ticks forward from 1970-01-01 00:00, |
| 969 | // and then they have files /data/system/time/ats_* with 64bit offset |
| 970 | // in miliseconds which, when added to the RTC, gives the correct time. |
| 971 | // So, the time is: (offset_from_ats + value_from_RTC) |
| 972 | // There are multiple ats files, they are for different systems? Bases? |
| 973 | // Like, ats_1 is for modem and ats_2 is for TOD (time of day?). |
| 974 | // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services |
| 975 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 976 | std::vector<std::string> paths; // space separated list of paths |
| 977 | if (time_paths.empty()) { |
Mauronofrio Matarrese | 2dab70d | 2019-03-05 02:22:26 +0100 | [diff] [blame] | 978 | paths = Split_String("/data/system/time/ /data/time/ /data/vendor/time/", " "); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 979 | if (!PartitionManager.Mount_By_Path("/data", false)) |
| 980 | return; |
| 981 | } else { |
| 982 | // When specific path(s) are used, Fixup_Time needs those |
| 983 | // partitions to already be mounted! |
| 984 | paths = Split_String(time_paths, " "); |
| 985 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 986 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 987 | FILE *f; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 988 | offset = 0; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 989 | struct dirent *dt; |
| 990 | std::string ats_path; |
| 991 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 992 | // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead |
| 993 | // - it is the one for ATS_TOD (time of day?). |
| 994 | // However, I never saw a device where the offset differs between ats files. |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 995 | for (size_t i = 0; i < paths.size(); ++i) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 996 | { |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 997 | DIR *d = opendir(paths[i].c_str()); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 998 | if (!d) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 999 | continue; |
| 1000 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1001 | while ((dt = readdir(d))) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1002 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1003 | 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] | 1004 | continue; |
| 1005 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1006 | if (ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0) |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1007 | ats_path = paths[i] + dt->d_name; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | closedir(d); |
| 1011 | } |
| 1012 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1013 | if (ats_path.empty()) { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1014 | LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n"); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1015 | } else if ((f = fopen(ats_path.c_str(), "r")) == NULL) { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1016 | 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] | 1017 | } else if (fread(&offset, sizeof(offset), 1, f) != 1) { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1018 | 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] | 1019 | fclose(f); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1020 | } else { |
| 1021 | fclose(f); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1022 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1023 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), (unsigned long long) offset); |
| 1024 | DataManager::SetValue("tw_qcom_ats_offset", (unsigned long long) offset, 1); |
| 1025 | fixed = true; |
| 1026 | } |
| 1027 | |
| 1028 | if (!fixed) { |
| 1029 | // Failed to get offset from ats file, check twrp settings |
| 1030 | unsigned long long value; |
| 1031 | if (DataManager::GetValue("tw_qcom_ats_offset", value) < 0) { |
| 1032 | return; |
| 1033 | } else { |
| 1034 | offset = (uint64_t) value; |
| 1035 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from twrp setting file, offset %llu\n", (unsigned long long) offset); |
| 1036 | // Do not consider the settings file as a definitive answer, keep fixed=false so next run will try ats files again |
| 1037 | } |
| 1038 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1039 | |
| 1040 | gettimeofday(&tv, NULL); |
| 1041 | |
| 1042 | tv.tv_sec += offset/1000; |
Phoenix591 | e444d11 | 2018-02-03 07:23:54 +0000 | [diff] [blame] | 1043 | #ifdef TW_CLOCK_OFFSET |
| 1044 | // Some devices are even quirkier and have ats files that are offset from the actual time |
| 1045 | tv.tv_sec = tv.tv_sec + TW_CLOCK_OFFSET; |
| 1046 | #endif |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1047 | tv.tv_usec += (offset%1000)*1000; |
| 1048 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1049 | while (tv.tv_usec >= 1000000) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1050 | { |
| 1051 | ++tv.tv_sec; |
| 1052 | tv.tv_usec -= 1000000; |
| 1053 | } |
| 1054 | |
| 1055 | settimeofday(&tv, NULL); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1056 | |
| 1057 | 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] | 1058 | #endif |
| 1059 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1060 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1061 | std::vector<std::string> TWFunc::Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty) |
| 1062 | { |
| 1063 | std::vector<std::string> res; |
| 1064 | size_t idx = 0, idx_last = 0; |
| 1065 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1066 | while (idx < str.size()) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1067 | { |
| 1068 | idx = str.find_first_of(delimiter, idx_last); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1069 | if (idx == std::string::npos) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1070 | idx = str.size(); |
| 1071 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1072 | if (idx-idx_last != 0 || !removeEmpty) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1073 | res.push_back(str.substr(idx_last, idx-idx_last)); |
| 1074 | |
| 1075 | idx_last = idx + delimiter.size(); |
| 1076 | } |
| 1077 | |
| 1078 | return res; |
| 1079 | } |
| 1080 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1081 | bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t uid, gid_t gid) |
| 1082 | { |
| 1083 | std::vector<std::string> parts = Split_String(path, "/"); |
| 1084 | std::string cur_path; |
| 1085 | struct stat info; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1086 | for (size_t i = 0; i < parts.size(); ++i) |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1087 | { |
| 1088 | cur_path += "/" + parts[i]; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1089 | 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] | 1090 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1091 | if (mkdir(cur_path.c_str(), mode) < 0) |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1092 | return false; |
| 1093 | chown(cur_path.c_str(), uid, gid); |
| 1094 | } |
| 1095 | } |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1099 | int TWFunc::Set_Brightness(std::string brightness_value) |
| 1100 | { |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1101 | int result = -1; |
| 1102 | std::string secondary_brightness_file; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1103 | |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1104 | if (DataManager::GetIntValue("tw_has_brightnesss_file")) { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1105 | 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] | 1106 | result = TWFunc::write_to_file(DataManager::GetStrValue("tw_brightness_file"), brightness_value); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1107 | DataManager::GetValue("tw_secondary_brightness_file", secondary_brightness_file); |
| 1108 | if (!secondary_brightness_file.empty()) { |
| 1109 | 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] | 1110 | TWFunc::write_to_file(secondary_brightness_file, brightness_value); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1111 | } |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1112 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 1113 | return result ? 0 : -1; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1116 | bool TWFunc::Toggle_MTP(bool enable) { |
| 1117 | #ifdef TW_HAS_MTP |
| 1118 | static int was_enabled = false; |
| 1119 | |
| 1120 | if (enable && was_enabled) { |
| 1121 | if (!PartitionManager.Enable_MTP()) |
| 1122 | PartitionManager.Disable_MTP(); |
| 1123 | } else { |
| 1124 | was_enabled = DataManager::GetIntValue("tw_mtp_enabled"); |
| 1125 | PartitionManager.Disable_MTP(); |
| 1126 | usleep(500); |
| 1127 | } |
| 1128 | return was_enabled; |
| 1129 | #else |
| 1130 | return false; |
| 1131 | #endif |
| 1132 | } |
| 1133 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 1134 | void TWFunc::SetPerformanceMode(bool mode) { |
| 1135 | if (mode) { |
| 1136 | property_set("recovery.perf.mode", "1"); |
| 1137 | } else { |
| 1138 | property_set("recovery.perf.mode", "0"); |
| 1139 | } |
| 1140 | // Some time for events to catch up to init handlers |
| 1141 | usleep(500000); |
| 1142 | } |
| 1143 | |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 1144 | std::string TWFunc::to_string(unsigned long value) { |
| 1145 | std::ostringstream os; |
| 1146 | os << value; |
| 1147 | return os.str(); |
| 1148 | } |
| 1149 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1150 | void TWFunc::Disable_Stock_Recovery_Replace(void) { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1151 | if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) { |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1152 | // Disable flashing of stock recovery |
| 1153 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
| 1154 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1155 | 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] | 1156 | sync(); |
| 1157 | } |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1158 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1162 | unsigned long long TWFunc::IOCTL_Get_Block_Size(const char* block_device) { |
| 1163 | unsigned long block_device_size; |
| 1164 | int ret = 0; |
| 1165 | |
| 1166 | int fd = open(block_device, O_RDONLY); |
| 1167 | if (fd < 0) { |
| 1168 | LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", block_device, strerror(errno)); |
| 1169 | } else { |
| 1170 | ret = ioctl(fd, BLKGETSIZE, &block_device_size); |
| 1171 | close(fd); |
| 1172 | if (ret) { |
| 1173 | LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno)); |
| 1174 | } else { |
| 1175 | return (unsigned long long)(block_device_size) * 512LLU; |
| 1176 | } |
| 1177 | } |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 1181 | void TWFunc::copy_kernel_log(string curr_storage) { |
| 1182 | std::string dmesgDst = curr_storage + "/dmesg.log"; |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1183 | std::string dmesgCmd = "/system/bin/dmesg"; |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 1184 | |
| 1185 | std::string result; |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1186 | Exec_Cmd(dmesgCmd, result, false); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1187 | write_to_file(dmesgDst, result); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 1188 | gui_msg(Msg("copy_kernel_log=Copied kernel log to {1}")(dmesgDst)); |
| 1189 | tw_set_default_metadata(dmesgDst.c_str()); |
| 1190 | } |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1191 | |
Captain Throwback | 5297893 | 2021-09-05 15:11:07 -0400 | [diff] [blame] | 1192 | void TWFunc::copy_logcat(string curr_storage) { |
| 1193 | std::string logcatDst = curr_storage + "/logcat.txt"; |
| 1194 | std::string logcatCmd = "logcat -d"; |
| 1195 | |
| 1196 | std::string result; |
| 1197 | Exec_Cmd(logcatCmd, result, false); |
| 1198 | write_to_file(logcatDst, result); |
| 1199 | gui_msg(Msg("copy_logcat=Copied logcat to {1}")(logcatDst)); |
| 1200 | tw_set_default_metadata(logcatDst.c_str()); |
| 1201 | } |
| 1202 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1203 | bool TWFunc::isNumber(string strtocheck) { |
| 1204 | int num = 0; |
| 1205 | std::istringstream iss(strtocheck); |
| 1206 | |
| 1207 | if (!(iss >> num).fail()) |
| 1208 | return true; |
| 1209 | else |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
| 1213 | int TWFunc::stream_adb_backup(string &Restore_Name) { |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1214 | string cmd = "/system/bin/bu --twrp stream " + Restore_Name; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1215 | LOGINFO("stream_adb_backup: %s\n", cmd.c_str()); |
| 1216 | int ret = TWFunc::Exec_Cmd(cmd); |
| 1217 | if (ret != 0) |
| 1218 | return -1; |
| 1219 | return ret; |
| 1220 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1221 | |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1222 | std::string TWFunc::get_log_dir() { |
| 1223 | if (PartitionManager.Find_Partition_By_Path(CACHE_LOGS_DIR) == NULL) { |
| 1224 | if (PartitionManager.Find_Partition_By_Path(DATA_LOGS_DIR) == NULL) { |
bigbiff | aac5861 | 2020-08-30 11:18:39 -0400 | [diff] [blame] | 1225 | LOGINFO("Unable to find a directory to store TWRP logs."); |
| 1226 | return ""; |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 1227 | } else { |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1228 | return DATA_LOGS_DIR; |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 1229 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1230 | } |
| 1231 | else { |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1232 | return CACHE_LOGS_DIR; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | void TWFunc::check_selinux_support() { |
| 1237 | if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { |
| 1238 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 1239 | printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); |
| 1240 | rename("/file_contexts", "/file_contexts.bak"); |
| 1241 | } |
| 1242 | printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); |
| 1243 | rename("/prebuilt_file_contexts", "/file_contexts"); |
| 1244 | } |
| 1245 | struct selinux_opt selinux_options[] = { |
| 1246 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 1247 | }; |
| 1248 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
| 1249 | if (!selinux_handle) |
| 1250 | printf("No file contexts for SELinux\n"); |
| 1251 | else |
| 1252 | printf("SELinux contexts loaded from /file_contexts\n"); |
| 1253 | { // Check to ensure SELinux can be supported by the kernel |
| 1254 | char *contexts = NULL; |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1255 | std::string cacheDir = TWFunc::get_log_dir(); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1256 | std::string se_context_check = cacheDir + "recovery/"; |
| 1257 | int ret = 0; |
| 1258 | |
bigbiff | 25d25b9 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1259 | if (cacheDir == CACHE_LOGS_DIR) { |
| 1260 | PartitionManager.Mount_By_Path(CACHE_LOGS_DIR, false); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1261 | } |
| 1262 | if (TWFunc::Path_Exists(se_context_check)) { |
| 1263 | ret = lgetfilecon(se_context_check.c_str(), &contexts); |
Makornthawat Emery | abc299c | 2019-03-29 13:45:22 +0000 | [diff] [blame] | 1264 | if (ret < 0) { |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1265 | LOGINFO("Could not check %s SELinux contexts, using /system/bin/teamwin instead which may be inaccurate.\n", se_context_check.c_str()); |
| 1266 | lgetfilecon("/system/bin/teamwin", &contexts); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | if (ret < 0) { |
| 1270 | gui_warn("no_kernel_selinux=Kernel does not have support for reading SELinux contexts."); |
| 1271 | } else { |
| 1272 | free(contexts); |
| 1273 | gui_msg("full_selinux=Full SELinux support is present."); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1277 | |
| 1278 | bool TWFunc::Is_TWRP_App_In_System() { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 1279 | LOGINFO("checking for twrp app\n"); |
| 1280 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
| 1281 | if (!sys->Get_Super_Status()) { |
Captain Throwback | 31c1492 | 2020-12-21 11:28:34 -0500 | [diff] [blame] | 1282 | bool is_system_mounted = true; |
| 1283 | if(!PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path())) { |
| 1284 | is_system_mounted = false; |
| 1285 | PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1286 | } |
Captain Throwback | 31c1492 | 2020-12-21 11:28:34 -0500 | [diff] [blame] | 1287 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 1288 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
| 1289 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 1290 | string install_path = base_path + "/priv-app"; |
| 1291 | if (!TWFunc::Path_Exists(install_path)) |
| 1292 | install_path = base_path + "/app"; |
| 1293 | install_path += "/twrpapp"; |
| 1294 | if (TWFunc::Path_Exists(install_path)) { |
| 1295 | LOGINFO("App found at '%s'\n", install_path.c_str()); |
| 1296 | DataManager::SetValue("tw_app_installed_in_system", 1); |
| 1297 | return true; |
| 1298 | } |
| 1299 | if (!is_system_mounted) |
| 1300 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 1301 | DataManager::SetValue("tw_app_installed_in_system", 0); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1302 | } |
| 1303 | DataManager::SetValue("tw_app_installed_in_system", 0); |
| 1304 | return false; |
| 1305 | } |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 1306 | |
epicX | 271bb3a | 2020-12-30 01:03:18 +0530 | [diff] [blame] | 1307 | void TWFunc::checkforapp(){ |
| 1308 | |
| 1309 | string sdkverstr = System_Property_Get("ro.build.version.sdk"); |
| 1310 | int sdkver = 0; |
| 1311 | if (!sdkverstr.empty()) { |
| 1312 | sdkver = atoi(sdkverstr.c_str()); |
| 1313 | } |
| 1314 | if (sdkver <= 13) { |
| 1315 | if (sdkver == 0) |
| 1316 | LOGINFO("Unable to read sdk version from build prop\n"); |
| 1317 | else |
| 1318 | LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver); |
| 1319 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1320 | goto exit; |
| 1321 | } |
| 1322 | if (Is_TWRP_App_In_System()) { |
| 1323 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1324 | goto exit; |
| 1325 | } |
| 1326 | if (PartitionManager.Mount_By_Path("/data", false)) { |
| 1327 | const char parent_path[] = "/data/app"; |
| 1328 | const char app_prefix[] = "me.twrp.twrpapp-"; |
| 1329 | DIR *d = opendir(parent_path); |
| 1330 | if (d) { |
| 1331 | struct dirent *p; |
| 1332 | while ((p = readdir(d))) { |
| 1333 | if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix))) |
| 1334 | continue; |
| 1335 | closedir(d); |
| 1336 | LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name); |
| 1337 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1338 | goto exit; |
| 1339 | } |
| 1340 | closedir(d); |
| 1341 | } |
| 1342 | } else { |
| 1343 | LOGINFO("Data partition cannot be mounted during app check\n"); |
| 1344 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1345 | } |
| 1346 | |
| 1347 | LOGINFO("App not installed\n"); |
| 1348 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1349 | exit: |
| 1350 | return; |
| 1351 | |
| 1352 | } |
| 1353 | |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 1354 | int TWFunc::Property_Override(string Prop_Name, string Prop_Value) { |
| 1355 | #ifdef TW_INCLUDE_LIBRESETPROP |
| 1356 | return setprop(Prop_Name.c_str(), Prop_Value.c_str(), false); |
| 1357 | #else |
| 1358 | return -2; |
| 1359 | #endif |
| 1360 | } |
| 1361 | |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 1362 | void TWFunc::List_Mounts() { |
| 1363 | std::vector<std::string> mounts; |
| 1364 | read_file("/proc/mounts", mounts); |
| 1365 | LOGINFO("Mounts:\n"); |
| 1366 | for (auto&& mount: mounts) { |
| 1367 | LOGINFO("%s\n", mount.c_str()); |
| 1368 | } |
| 1369 | } |
| 1370 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1371 | #ifdef TW_INCLUDE_CRYPTO |
| 1372 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 1373 | bool TWFunc::Get_Encryption_Policy(struct fscrypt_policy_v1 &policy, std::string path) { |
| 1374 | #else |
| 1375 | bool TWFunc::Get_Encryption_Policy(struct fscrypt_policy_v2 &policy, std::string path) { |
| 1376 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1377 | if (!TWFunc::Path_Exists(path)) { |
| 1378 | LOGERR("Unable to find %s to get policy\n", path.c_str()); |
| 1379 | return false; |
| 1380 | } |
| 1381 | if (!fscrypt_policy_get_struct(path.c_str(), &policy)) { |
| 1382 | LOGERR("No policy set for path %s\n", path.c_str()); |
| 1383 | return false; |
| 1384 | } |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1388 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 1389 | bool TWFunc::Set_Encryption_Policy(std::string path, struct fscrypt_policy_v1 &policy) { |
| 1390 | #else |
| 1391 | bool TWFunc::Set_Encryption_Policy(std::string path, struct fscrypt_policy_v2 &policy) { |
| 1392 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1393 | if (!TWFunc::Path_Exists(path)) { |
| 1394 | LOGERR("unable to find %s to set policy\n", path.c_str()); |
| 1395 | return false; |
| 1396 | } |
| 1397 | uint8_t binary_policy[FS_KEY_DESCRIPTOR_SIZE]; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1398 | char policy_hex[FSCRYPT_KEY_IDENTIFIER_HEX_SIZE]; |
| 1399 | bytes_to_hex(binary_policy, FS_KEY_DESCRIPTOR_SIZE, policy_hex); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1400 | if (!fscrypt_policy_set_struct(path.c_str(), &policy)) { |
| 1401 | LOGERR("unable to set policy for path: %s\n", path.c_str()); |
| 1402 | return false; |
| 1403 | } |
| 1404 | return true; |
| 1405 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 1406 | #endif |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1407 | |
| 1408 | string TWFunc::Check_For_TwrpFolder() { |
| 1409 | string oldFolder = ""; |
| 1410 | vector<string> customTWRPFolders; |
| 1411 | string mainPath = DataManager::GetCurrentStoragePath(); |
| 1412 | DIR* d; |
| 1413 | struct dirent* de; |
| 1414 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1415 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED)) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1416 | goto exit; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | d = opendir(mainPath.c_str()); |
| 1421 | if (d == NULL) { |
| 1422 | goto exit; |
| 1423 | } |
| 1424 | |
| 1425 | while ((de = readdir(d)) != NULL) { |
| 1426 | string name = de->d_name; |
| 1427 | string fullPath = mainPath + '/' + name; |
| 1428 | unsigned char type = de->d_type; |
| 1429 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1430 | if (name == "." || name == "..") continue; |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1431 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1432 | if (type == DT_UNKNOWN) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1433 | type = Get_D_Type_From_Stat(fullPath); |
| 1434 | } |
| 1435 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1436 | if (type == DT_DIR && Path_Exists(fullPath + '/' + TW_SETTINGS_FILE)) { |
| 1437 | if ('/' + name == TW_DEFAULT_RECOVERY_FOLDER) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1438 | oldFolder = name; |
| 1439 | } else { |
| 1440 | customTWRPFolders.push_back(name); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | closedir(d); |
| 1446 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1447 | if (oldFolder == "" && customTWRPFolders.empty()) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1448 | LOGINFO("No recovery folder found. Using default folder.\n"); |
| 1449 | goto exit; |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1450 | } else if (customTWRPFolders.empty()) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1451 | LOGINFO("No custom recovery folder found. Using TWRP as default.\n"); |
| 1452 | goto exit; |
| 1453 | } else { |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1454 | if (customTWRPFolders.size() > 1) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1455 | LOGINFO("More than one custom recovery folder found. Using first one from the list.\n"); |
| 1456 | } else { |
| 1457 | LOGINFO("One custom recovery folder found.\n"); |
| 1458 | } |
| 1459 | string customPath = '/' + customTWRPFolders.at(0); |
| 1460 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1461 | if (Path_Exists(mainPath + TW_DEFAULT_RECOVERY_FOLDER)) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1462 | string oldBackupFolder = mainPath + TW_DEFAULT_RECOVERY_FOLDER + "/BACKUPS/" + DataManager::GetStrValue("device_id"); |
| 1463 | string newBackupFolder = mainPath + customPath + "/BACKUPS/" + DataManager::GetStrValue("device_id"); |
| 1464 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1465 | if (Path_Exists(oldBackupFolder)) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1466 | vector<string> backups; |
| 1467 | d = opendir(oldBackupFolder.c_str()); |
| 1468 | |
| 1469 | if (d != NULL) { |
| 1470 | while ((de = readdir(d)) != NULL) { |
| 1471 | string name = de->d_name; |
| 1472 | unsigned char type = de->d_type; |
| 1473 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1474 | if (name == "." || name == "..") continue; |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1475 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1476 | if (type == DT_UNKNOWN) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1477 | type = Get_D_Type_From_Stat(mainPath + '/' + name); |
| 1478 | } |
| 1479 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1480 | if (type == DT_DIR) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1481 | backups.push_back(name); |
| 1482 | } |
| 1483 | } |
| 1484 | closedir(d); |
| 1485 | } |
| 1486 | |
epicX | 11e9083 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1487 | for (auto it = backups.begin(); it != backups.end(); it++) { |
epicX | a721f95 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1488 | Exec_Cmd("mv -f \"" + oldBackupFolder + '/' + *it + "\" \"" + newBackupFolder + '/' + *it + (Path_Exists(newBackupFolder + '/' + *it) ? "_new\"" : "\"")); |
| 1489 | } |
| 1490 | } |
| 1491 | Exec_Cmd("rm -rf \"" + mainPath + TW_DEFAULT_RECOVERY_FOLDER + '\"'); |
| 1492 | } |
| 1493 | |
| 1494 | return customPath; |
| 1495 | } |
| 1496 | |
| 1497 | exit: |
| 1498 | return TW_DEFAULT_RECOVERY_FOLDER; |
| 1499 | } |
zhenyolka | ee02585 | 2021-11-08 19:05:30 +0300 | [diff] [blame] | 1500 | |
| 1501 | bool TWFunc::Check_Xml_Format(const std::string filename) { |
| 1502 | std::string buffer(' ', 4); |
| 1503 | std::string abx_hdr("ABX\x00", 4); |
| 1504 | std::ifstream File; |
| 1505 | File.open(filename); |
| 1506 | if (File.is_open()) { |
| 1507 | File.get(&buffer[0], buffer.size()); |
| 1508 | File.close(); |
| 1509 | // Android Binary Xml start from these bytes |
| 1510 | if(!buffer.compare(0, abx_hdr.size(), abx_hdr)) |
| 1511 | return false; // bad format, not possible to parse |
| 1512 | } |
| 1513 | return true; // good format, possible to parse |
| 1514 | } |
| 1515 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1516 | #endif // ndef BUILD_TWRPTAR_MAIN |