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