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