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