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