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