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