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