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: |
bigbiff bigbiff | 731df79 | 2014-02-20 18:26:13 -0500 | [diff] [blame] | 82 | LOGERR("Exec_Cmd(): vfork failed: %d!\n", errno); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 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 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 277 | std::string TWFunc::Remove_Trailing_Slashes(const std::string& path, bool leaveLast) |
| 278 | { |
| 279 | std::string res; |
| 280 | size_t last_idx = 0, idx = 0; |
| 281 | |
| 282 | while(last_idx != std::string::npos) |
| 283 | { |
| 284 | if(last_idx != 0) |
| 285 | res += '/'; |
| 286 | |
| 287 | idx = path.find_first_of('/', last_idx); |
| 288 | if(idx == std::string::npos) { |
| 289 | res += path.substr(last_idx, idx); |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | res += path.substr(last_idx, idx-last_idx); |
| 294 | last_idx = path.find_first_not_of('/', idx); |
| 295 | } |
| 296 | |
| 297 | if(leaveLast) |
| 298 | res += '/'; |
| 299 | return res; |
| 300 | } |
| 301 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 302 | #ifndef BUILD_TWRPTAR_MAIN |
| 303 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 304 | // Returns "/path" from a full /path/to/file.name |
| 305 | string TWFunc::Get_Root_Path(string Path) { |
| 306 | string Local_Path = Path; |
| 307 | |
| 308 | // Make sure that we have a leading slash |
| 309 | if (Local_Path.substr(0, 1) != "/") |
| 310 | Local_Path = "/" + Local_Path; |
| 311 | |
| 312 | // Trim the path to get the root path only |
| 313 | size_t position = Local_Path.find("/", 2); |
| 314 | if (position != string::npos) { |
| 315 | Local_Path.resize(position); |
| 316 | } |
| 317 | return Local_Path; |
| 318 | } |
| 319 | |
| 320 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 321 | int need_libs = 0; |
| 322 | |
| 323 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 324 | return; |
| 325 | |
| 326 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 327 | return; |
| 328 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 329 | gui_print("Installing HTC Dumlock to system...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 330 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 331 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 332 | gui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 333 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 334 | need_libs = 1; |
| 335 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 336 | gui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 337 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 338 | gui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 339 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 340 | need_libs = 1; |
| 341 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 342 | gui_print("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 343 | if (need_libs) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 344 | gui_print("Installing libs needed for flash_image and dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 345 | copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755); |
| 346 | copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755); |
| 347 | copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755); |
| 348 | copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 349 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 350 | gui_print("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 351 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 352 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 353 | 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] | 354 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 355 | gui_print("HTC Dumlock is installed.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 359 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 360 | return; |
| 361 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 362 | gui_print("Restoring original boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 363 | Exec_Cmd("htcdumlock restore"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 364 | gui_print("Original boot restored.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 368 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 369 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 370 | gui_print("Reflashing recovery to boot...\n"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 371 | Exec_Cmd("htcdumlock recovery noreboot"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 372 | gui_print("Recovery is flashed to boot.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 373 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 374 | |
| 375 | int TWFunc::Recursive_Mkdir(string Path) { |
| 376 | string pathCpy = Path; |
| 377 | string wholePath; |
| 378 | size_t pos = pathCpy.find("/", 2); |
| 379 | |
| 380 | while (pos != string::npos) |
| 381 | { |
| 382 | wholePath = pathCpy.substr(0, pos); |
| 383 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 384 | 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] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | pos = pathCpy.find("/", pos + 1); |
| 389 | } |
| 390 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 391 | return false; |
| 392 | return true; |
| 393 | } |
| 394 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 395 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 396 | string Display_Text; |
| 397 | |
| 398 | DataManager::GetValue(Read_Value, Display_Text); |
| 399 | if (Display_Text.empty()) |
| 400 | Display_Text = Default_Text; |
| 401 | |
| 402 | DataManager::SetValue("tw_operation", Display_Text); |
| 403 | DataManager::SetValue("tw_partition", ""); |
| 404 | } |
| 405 | |
| 406 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 407 | string Display_Text; |
| 408 | |
| 409 | DataManager::GetValue(Read_Value, Display_Text); |
| 410 | if (Display_Text.empty()) |
| 411 | Display_Text = Default_Text; |
| 412 | |
| 413 | DataManager::SetValue("tw_operation", Display_Text); |
| 414 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 417 | void TWFunc::Copy_Log(string Source, string Destination) { |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 418 | PartitionManager.Mount_By_Path(Destination, false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 419 | FILE *destination_log = fopen(Destination.c_str(), "a"); |
| 420 | if (destination_log == NULL) { |
| 421 | 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] | 422 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 423 | FILE *source_log = fopen(Source.c_str(), "r"); |
| 424 | if (source_log != NULL) { |
| 425 | fseek(source_log, Log_Offset, SEEK_SET); |
| 426 | char buffer[4096]; |
| 427 | while (fgets(buffer, sizeof(buffer), source_log)) |
| 428 | fputs(buffer, destination_log); // Buffered write of log file |
| 429 | Log_Offset = ftell(source_log); |
| 430 | fflush(source_log); |
| 431 | fclose(source_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 432 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 433 | fflush(destination_log); |
| 434 | fclose(destination_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 435 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 438 | void TWFunc::Update_Log_File(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 439 | // Copy logs to cache so the system can find out what happened. |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 440 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 441 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 442 | LOGINFO("Recreating /cache/recovery folder.\n"); |
| 443 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 444 | LOGINFO("Unable to create /cache/recovery folder.\n"); |
| 445 | } |
| 446 | Copy_Log(TMP_LOG_FILE, "/cache/recovery/log"); |
| 447 | copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600); |
| 448 | chown("/cache/recovery/log", 1000, 1000); |
| 449 | chmod("/cache/recovery/log", 0600); |
| 450 | chmod("/cache/recovery/last_log", 0640); |
| 451 | } else { |
| 452 | LOGINFO("Failed to mount /cache for TWFunc::Update_Log_File\n"); |
| 453 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 454 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 455 | // Reset bootloader message |
| 456 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 457 | if (Part != NULL) { |
| 458 | struct bootloader_message boot; |
| 459 | memset(&boot, 0, sizeof(boot)); |
| 460 | if (Part->Current_File_System == "mtd") { |
| 461 | if (set_bootloader_message_mtd_name(&boot, Part->MTD_Name.c_str()) != 0) |
| 462 | LOGERR("Unable to set MTD bootloader message.\n"); |
| 463 | } else if (Part->Current_File_System == "emmc") { |
| 464 | if (set_bootloader_message_block_name(&boot, Part->Actual_Block_Device.c_str()) != 0) |
| 465 | LOGERR("Unable to set emmc bootloader message.\n"); |
| 466 | } else { |
| 467 | LOGERR("Unknown file system for /misc: '%s'\n", Part->Current_File_System.c_str()); |
| 468 | } |
| 469 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 470 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 471 | if (PartitionManager.Mount_By_Path("/cache", true)) { |
| 472 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 473 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 474 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 475 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 476 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 477 | sync(); |
| 478 | } |
| 479 | |
| 480 | void TWFunc::Update_Intent_File(string Intent) { |
| 481 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
| 482 | TWFunc::write_file("/cache/recovery/intent", Intent); |
| 483 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 487 | int TWFunc::tw_reboot(RebootCommand command) |
| 488 | { |
| 489 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 490 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 491 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 492 | switch (command) { |
| 493 | case rb_current: |
| 494 | case rb_system: |
| 495 | Update_Log_File(); |
| 496 | Update_Intent_File("s"); |
| 497 | sync(); |
| 498 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
| 499 | return reboot(RB_AUTOBOOT); |
| 500 | case rb_recovery: |
| 501 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
| 502 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 503 | case rb_bootloader: |
| 504 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
| 505 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 506 | case rb_poweroff: |
| 507 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 508 | #ifdef ANDROID_RB_POWEROFF |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 509 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 510 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 511 | return reboot(RB_POWER_OFF); |
| 512 | case rb_download: |
| 513 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
| 514 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 515 | default: |
| 516 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 517 | } |
| 518 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 522 | { |
| 523 | // Check for and run startup script if script exists |
| 524 | struct stat st; |
| 525 | if (stat(script_file, &st) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 526 | gui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 527 | 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] | 528 | TWFunc::Exec_Cmd(script_file); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 529 | gui_print("\nFinished running %s script.\n", display_name); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 530 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 531 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 532 | |
| 533 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 534 | DIR *d = opendir(path.c_str()); |
| 535 | int r = 0; |
| 536 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 537 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 538 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 539 | LOGERR("Error opening '%s'\n", path.c_str()); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 540 | return -1; |
| 541 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 542 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 543 | if (d) { |
| 544 | struct dirent *p; |
| 545 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 546 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 547 | continue; |
| 548 | new_path = path + "/"; |
| 549 | new_path.append(p->d_name); |
| 550 | if (p->d_type == DT_DIR) { |
| 551 | r = removeDir(new_path, true); |
| 552 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 553 | if (p->d_type == DT_DIR) |
| 554 | r = rmdir(new_path.c_str()); |
| 555 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 556 | 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] | 557 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 558 | } 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] | 559 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 560 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 561 | 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] | 562 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 566 | |
| 567 | if (!r) { |
| 568 | if (skipParent) |
| 569 | return 0; |
| 570 | else |
| 571 | r = rmdir(path.c_str()); |
| 572 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 573 | } |
| 574 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | int TWFunc::copy_file(string src, string dst, int mode) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 578 | 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] | 579 | ifstream srcfile(src.c_str(), ios::binary); |
| 580 | ofstream dstfile(dst.c_str(), ios::binary); |
| 581 | dstfile << srcfile.rdbuf(); |
| 582 | srcfile.close(); |
| 583 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 584 | if (chmod(dst.c_str(), mode) != 0) |
| 585 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 586 | return 0; |
| 587 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 588 | |
| 589 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 590 | struct stat st; |
| 591 | |
| 592 | stat(Path.c_str(), &st); |
| 593 | if (st.st_mode & S_IFDIR) |
| 594 | return DT_DIR; |
| 595 | else if (st.st_mode & S_IFBLK) |
| 596 | return DT_BLK; |
| 597 | else if (st.st_mode & S_IFCHR) |
| 598 | return DT_CHR; |
| 599 | else if (st.st_mode & S_IFIFO) |
| 600 | return DT_FIFO; |
| 601 | else if (st.st_mode & S_IFLNK) |
| 602 | return DT_LNK; |
| 603 | else if (st.st_mode & S_IFREG) |
| 604 | return DT_REG; |
| 605 | else if (st.st_mode & S_IFSOCK) |
| 606 | return DT_SOCK; |
| 607 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 608 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 609 | |
| 610 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 611 | ifstream file; |
| 612 | file.open(fn.c_str(), ios::in); |
| 613 | |
| 614 | if (file.is_open()) { |
| 615 | file >> results; |
| 616 | file.close(); |
| 617 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 618 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 619 | |
| 620 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 621 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 625 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 626 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 627 | file.open(fn.c_str(), ios::in); |
| 628 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 629 | while (getline(file, line)) |
| 630 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 631 | file.close(); |
| 632 | return 0; |
| 633 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 634 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | int TWFunc::write_file(string fn, string& line) { |
| 639 | FILE *file; |
| 640 | file = fopen(fn.c_str(), "w"); |
| 641 | if (file != NULL) { |
| 642 | fwrite(line.c_str(), line.size(), 1, file); |
| 643 | fclose(file); |
| 644 | return 0; |
| 645 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 646 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 651 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 652 | timespec temp; |
| 653 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 654 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 655 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 656 | } else { |
| 657 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 658 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 659 | } |
| 660 | return temp; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 661 | } |
| 662 | |
Vojtech Bocek | e5ffcd1 | 2014-02-06 21:17:32 +0100 | [diff] [blame] | 663 | int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end) |
| 664 | { |
| 665 | return ((end.tv_sec * 1000) + end.tv_nsec/1000000) - |
| 666 | ((start.tv_sec * 1000) + start.tv_nsec/1000000); |
| 667 | } |
| 668 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 669 | int TWFunc::drop_caches(void) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 670 | string file = "/proc/sys/vm/drop_caches"; |
| 671 | string value = "3"; |
| 672 | if (write_file(file, value) != 0) |
| 673 | return -1; |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int TWFunc::Check_su_Perms(void) { |
| 678 | struct stat st; |
| 679 | int ret = 0; |
| 680 | |
| 681 | if (!PartitionManager.Mount_By_Path("/system", false)) |
| 682 | return 0; |
| 683 | |
| 684 | // Check to ensure that perms are 6755 for all 3 file locations |
| 685 | if (stat("/system/bin/su", &st) == 0) { |
| 686 | 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) { |
| 687 | ret = 1; |
| 688 | } |
| 689 | } |
| 690 | if (stat("/system/xbin/su", &st) == 0) { |
| 691 | 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) { |
| 692 | ret += 2; |
| 693 | } |
| 694 | } |
| 695 | if (stat("/system/bin/.ext/.su", &st) == 0) { |
| 696 | 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) { |
| 697 | ret += 4; |
| 698 | } |
| 699 | } |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | bool TWFunc::Fix_su_Perms(void) { |
| 704 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 705 | return false; |
| 706 | |
Ethan Yonker | 0385f51 | 2014-02-06 14:33:02 -0600 | [diff] [blame] | 707 | string propvalue = System_Property_Get("ro.build.version.sdk"); |
| 708 | string su_perms = "6755"; |
| 709 | if (!propvalue.empty()) { |
| 710 | int sdk_version = atoi(propvalue.c_str()); |
| 711 | if (sdk_version >= 18) |
| 712 | su_perms = "0755"; |
| 713 | } |
| 714 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 715 | string file = "/system/bin/su"; |
| 716 | if (TWFunc::Path_Exists(file)) { |
| 717 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 718 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 719 | return false; |
| 720 | } |
Ethan Yonker | 0385f51 | 2014-02-06 14:33:02 -0600 | [diff] [blame] | 721 | if (tw_chmod(file, su_perms) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 722 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 723 | return false; |
| 724 | } |
| 725 | } |
| 726 | file = "/system/xbin/su"; |
| 727 | if (TWFunc::Path_Exists(file)) { |
| 728 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 729 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 730 | return false; |
| 731 | } |
Ethan Yonker | 0385f51 | 2014-02-06 14:33:02 -0600 | [diff] [blame] | 732 | if (tw_chmod(file, su_perms) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 733 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 734 | return false; |
| 735 | } |
| 736 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 737 | file = "/system/xbin/daemonsu"; |
| 738 | if (TWFunc::Path_Exists(file)) { |
| 739 | if (chown(file.c_str(), 0, 0) != 0) { |
| 740 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 741 | return false; |
| 742 | } |
Ethan Yonker | 0385f51 | 2014-02-06 14:33:02 -0600 | [diff] [blame] | 743 | if (tw_chmod(file, "0755") != 0) { |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 744 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 745 | return false; |
| 746 | } |
| 747 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 748 | file = "/system/bin/.ext/.su"; |
| 749 | if (TWFunc::Path_Exists(file)) { |
| 750 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 751 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 752 | return false; |
| 753 | } |
Ethan Yonker | 0385f51 | 2014-02-06 14:33:02 -0600 | [diff] [blame] | 754 | if (tw_chmod(file, su_perms) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 755 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 756 | return false; |
| 757 | } |
| 758 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 759 | file = "/system/etc/install-recovery.sh"; |
| 760 | if (TWFunc::Path_Exists(file)) { |
| 761 | if (chown(file.c_str(), 0, 0) != 0) { |
| 762 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 763 | return false; |
| 764 | } |
| 765 | if (tw_chmod(file, "0755") != 0) { |
| 766 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 767 | return false; |
| 768 | } |
| 769 | } |
| 770 | file = "/system/etc/init.d/99SuperSUDaemon"; |
| 771 | if (TWFunc::Path_Exists(file)) { |
| 772 | if (chown(file.c_str(), 0, 0) != 0) { |
| 773 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
| 774 | return false; |
| 775 | } |
| 776 | if (tw_chmod(file, "0755") != 0) { |
| 777 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
| 778 | return false; |
| 779 | } |
| 780 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 781 | file = "/system/app/Superuser.apk"; |
| 782 | if (TWFunc::Path_Exists(file)) { |
| 783 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 784 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | if (tw_chmod(file, "0644") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 788 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | } |
| 792 | sync(); |
| 793 | if (!PartitionManager.UnMount_By_Path("/system", true)) |
| 794 | return false; |
| 795 | return true; |
| 796 | } |
| 797 | |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 798 | int TWFunc::tw_chmod(const string& fn, const string& mode) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 799 | long mask = 0; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 800 | std::string::size_type n = mode.length(); |
| 801 | int cls = 0; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 802 | |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 803 | if(n == 3) |
| 804 | ++cls; |
| 805 | else if(n != 4) |
| 806 | { |
| 807 | LOGERR("TWFunc::tw_chmod used with %u long mode string (should be 3 or 4)!\n", mode.length()); |
| 808 | return -1; |
| 809 | } |
| 810 | |
| 811 | for (n = 0; n < mode.length(); ++n, ++cls) { |
| 812 | if (cls == 0) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 813 | if (mode[n] == '0') |
| 814 | continue; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 815 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 816 | mask |= S_ISVTX; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 817 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 818 | mask |= S_ISGID; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 819 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 820 | mask |= S_ISUID; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 821 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 822 | mask |= S_ISVTX; |
| 823 | mask |= S_ISUID; |
| 824 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 825 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 826 | mask |= S_ISGID; |
| 827 | mask |= S_ISUID; |
| 828 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 829 | else if (mode[n] == '7') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 830 | mask |= S_ISVTX; |
| 831 | mask |= S_ISGID; |
| 832 | mask |= S_ISUID; |
| 833 | } |
| 834 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 835 | else if (cls == 1) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 836 | if (mode[n] == '7') { |
| 837 | mask |= S_IRWXU; |
| 838 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 839 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 840 | mask |= S_IRUSR; |
| 841 | mask |= S_IWUSR; |
| 842 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 843 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 844 | mask |= S_IRUSR; |
| 845 | mask |= S_IXUSR; |
| 846 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 847 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 848 | mask |= S_IRUSR; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 849 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 850 | mask |= S_IWUSR; |
| 851 | mask |= S_IRUSR; |
| 852 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 853 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 854 | mask |= S_IWUSR; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 855 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 856 | mask |= S_IXUSR; |
| 857 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 858 | else if (cls == 2) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 859 | if (mode[n] == '7') { |
| 860 | mask |= S_IRWXG; |
| 861 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 862 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 863 | mask |= S_IRGRP; |
| 864 | mask |= S_IWGRP; |
| 865 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 866 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 867 | mask |= S_IRGRP; |
| 868 | mask |= S_IXGRP; |
| 869 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 870 | else if (mode[n] == '4') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 871 | mask |= S_IRGRP; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 872 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 873 | mask |= S_IWGRP; |
| 874 | mask |= S_IXGRP; |
| 875 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 876 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 877 | mask |= S_IWGRP; |
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_IXGRP; |
| 880 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 881 | else if (cls == 3) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 882 | if (mode[n] == '7') { |
| 883 | mask |= S_IRWXO; |
| 884 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 885 | else if (mode[n] == '6') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 886 | mask |= S_IROTH; |
| 887 | mask |= S_IWOTH; |
| 888 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 889 | else if (mode[n] == '5') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 890 | mask |= S_IROTH; |
| 891 | mask |= S_IXOTH; |
| 892 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 893 | else if (mode[n] == '4') |
| 894 | mask |= S_IROTH; |
| 895 | else if (mode[n] == '3') { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 896 | mask |= S_IWOTH; |
| 897 | mask |= S_IXOTH; |
| 898 | } |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 899 | else if (mode[n] == '2') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 900 | mask |= S_IWOTH; |
Vojtech Bocek | 37aeb8d | 2013-08-29 22:38:20 +0200 | [diff] [blame] | 901 | else if (mode[n] == '1') |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 902 | mask |= S_IXOTH; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (chmod(fn.c_str(), mask) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 907 | LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 908 | return -1; |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | bool TWFunc::Install_SuperSU(void) { |
| 915 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 916 | return false; |
| 917 | |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 918 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su"); |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 919 | if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 920 | LOGERR("Failed to copy su binary to /system/bin\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 921 | return false; |
| 922 | } |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 923 | if (!Path_Exists("/system/bin/.ext")) { |
| 924 | mkdir("/system/bin/.ext", 0777); |
| 925 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 926 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 927 | if (copy_file("/supersu/su", "/system/bin/.ext/su", 0755) != 0) { |
| 928 | LOGERR("Failed to copy su binary to /system/bin/.ext/su\n"); |
| 929 | return false; |
| 930 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 931 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 932 | if (copy_file("/supersu/su", "/system/xbin/daemonsu", 0755) != 0) { |
| 933 | LOGERR("Failed to copy su binary to /system/xbin/daemonsu\n"); |
| 934 | return false; |
| 935 | } |
| 936 | if (Path_Exists("/system/etc/init.d")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 937 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 938 | if (copy_file("/supersu/99SuperSUDaemon", "/system/etc/init.d/99SuperSUDaemon", 0755) != 0) { |
| 939 | LOGERR("Failed to copy 99SuperSUDaemon to /system/etc/init.d/99SuperSUDaemon\n"); |
| 940 | return false; |
| 941 | } |
| 942 | } else { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 943 | TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh"); |
Dees_Troy | a7939bb | 2013-08-29 20:21:12 +0000 | [diff] [blame] | 944 | if (copy_file("/supersu/install-recovery.sh", "/system/etc/install-recovery.sh", 0755) != 0) { |
| 945 | LOGERR("Failed to copy install-recovery.sh to /system/etc/install-recovery.sh\n"); |
| 946 | return false; |
| 947 | } |
| 948 | } |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 949 | if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 950 | LOGERR("Failed to copy Superuser app to /system/app\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 951 | return false; |
| 952 | } |
| 953 | if (!Fix_su_Perms()) |
| 954 | return false; |
| 955 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 956 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 957 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 958 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 959 | DIR* d; |
| 960 | |
| 961 | string Filename; |
| 962 | Restore_Path += "/"; |
| 963 | d = opendir(Restore_Path.c_str()); |
| 964 | if (d == NULL) { |
| 965 | LOGERR("Error opening '%s'\n", Restore_Path.c_str()); |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | struct dirent* de; |
| 970 | while ((de = readdir(d)) != NULL) { |
| 971 | Filename = Restore_Path; |
| 972 | Filename += de->d_name; |
| 973 | if (TWFunc::Get_File_Type(Filename) == 2) { |
| 974 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 975 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 976 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 977 | closedir(d); |
| 978 | return false; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | closedir(d); |
| 983 | return true; |
| 984 | } |
| 985 | |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 986 | string TWFunc::Get_Current_Date() { |
| 987 | string Current_Date; |
| 988 | time_t seconds = time(0); |
| 989 | struct tm *t = localtime(&seconds); |
| 990 | char timestamp[255]; |
| 991 | 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); |
| 992 | Current_Date = timestamp; |
| 993 | return Current_Date; |
| 994 | } |
| 995 | |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 996 | string TWFunc::System_Property_Get(string Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 997 | bool mount_state = PartitionManager.Is_Mounted_By_Path("/system"); |
| 998 | std::vector<string> buildprop; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 999 | string propvalue; |
| 1000 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 1001 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1002 | if (TWFunc::read_file("/system/build.prop", buildprop) != 0) { |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1003 | 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] | 1004 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1005 | if (!mount_state) |
| 1006 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1007 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1008 | } |
| 1009 | int line_count = buildprop.size(); |
| 1010 | int index; |
| 1011 | size_t start_pos = 0, end_pos; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1012 | string propname; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1013 | for (index = 0; index < line_count; index++) { |
| 1014 | end_pos = buildprop.at(index).find("=", start_pos); |
| 1015 | propname = buildprop.at(index).substr(start_pos, end_pos); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1016 | if (propname == Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1017 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1018 | if (!mount_state) |
| 1019 | PartitionManager.UnMount_By_Path("/system", false); |
| 1020 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 1023 | if (!mount_state) |
| 1024 | PartitionManager.UnMount_By_Path("/system", false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 1025 | return propvalue; |
| 1026 | } |
| 1027 | |
| 1028 | void TWFunc::Auto_Generate_Backup_Name() { |
| 1029 | string propvalue = System_Property_Get("ro.build.display.id"); |
| 1030 | if (propvalue.empty()) { |
| 1031 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 1032 | return; |
| 1033 | } |
| 1034 | string Backup_Name = Get_Current_Date(); |
| 1035 | Backup_Name += " " + propvalue; |
| 1036 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 1037 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 1038 | // Trailing spaces cause problems on some file systems, so remove them |
| 1039 | string space_check, space = " "; |
| 1040 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 1041 | while (space_check == space) { |
| 1042 | Backup_Name.resize(Backup_Name.size() - 1); |
| 1043 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 1044 | } |
| 1045 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 1046 | if (PartitionManager.Check_Backup_Name(false) != 0) { |
| 1047 | LOGINFO("Auto generated backup name '%s' contains invalid characters, using date instead.\n", Backup_Name.c_str()); |
| 1048 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 1049 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1050 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1051 | |
| 1052 | void TWFunc::Fixup_Time_On_Boot() |
| 1053 | { |
| 1054 | #ifdef QCOM_RTC_FIX |
| 1055 | // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC. |
| 1056 | // They never set it, it just ticks forward from 1970-01-01 00:00, |
| 1057 | // and then they have files /data/system/time/ats_* with 64bit offset |
| 1058 | // in miliseconds which, when added to the RTC, gives the correct time. |
| 1059 | // So, the time is: (offset_from_ats + value_from_RTC) |
| 1060 | // There are multiple ats files, they are for different systems? Bases? |
| 1061 | // Like, ats_1 is for modem and ats_2 is for TOD (time of day?). |
| 1062 | // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services |
| 1063 | |
| 1064 | static const char *paths[] = { "/data/system/time/", "/data/time/" }; |
| 1065 | |
| 1066 | DIR *d; |
| 1067 | FILE *f; |
| 1068 | uint64_t offset = 0; |
| 1069 | struct timeval tv; |
| 1070 | struct dirent *dt; |
| 1071 | std::string ats_path; |
| 1072 | |
| 1073 | |
| 1074 | // Don't fix the time of it already is over year 2000, it is likely already okay, either |
| 1075 | // because the RTC is fine or because the recovery already set it and then crashed |
| 1076 | gettimeofday(&tv, NULL); |
| 1077 | if(tv.tv_sec > 946684800) // timestamp of 2000-01-01 00:00:00 |
| 1078 | { |
| 1079 | LOGINFO("TWFunc::Fixup_Time: not fixing time, it seems to be already okay (after year 2000).\n"); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | if(!PartitionManager.Mount_By_Path("/data", false)) |
| 1084 | return; |
| 1085 | |
| 1086 | // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead |
| 1087 | // - it is the one for ATS_TOD (time of day?). |
| 1088 | // However, I never saw a device where the offset differs between ats files. |
| 1089 | for(size_t i = 0; i < (sizeof(paths)/sizeof(paths[0])); ++i) |
| 1090 | { |
| 1091 | DIR *d = opendir(paths[i]); |
| 1092 | if(!d) |
| 1093 | continue; |
| 1094 | |
| 1095 | while((dt = readdir(d))) |
| 1096 | { |
| 1097 | if(dt->d_type != DT_REG || strncmp(dt->d_name, "ats_", 4) != 0) |
| 1098 | continue; |
| 1099 | |
| 1100 | if(ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0) |
| 1101 | ats_path = std::string(paths[i]).append(dt->d_name); |
| 1102 | } |
| 1103 | |
| 1104 | closedir(d); |
| 1105 | } |
| 1106 | |
| 1107 | if(ats_path.empty()) |
| 1108 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1109 | LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving time as-is!\n"); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | f = fopen(ats_path.c_str(), "r"); |
| 1114 | if(!f) |
| 1115 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1116 | 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] | 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | if(fread(&offset, sizeof(offset), 1, f) != 1) |
| 1121 | { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1122 | 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] | 1123 | fclose(f); |
| 1124 | return; |
| 1125 | } |
| 1126 | fclose(f); |
| 1127 | |
| 1128 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), offset); |
| 1129 | |
| 1130 | gettimeofday(&tv, NULL); |
| 1131 | |
| 1132 | tv.tv_sec += offset/1000; |
| 1133 | tv.tv_usec += (offset%1000)*1000; |
| 1134 | |
| 1135 | while(tv.tv_usec >= 1000000) |
| 1136 | { |
| 1137 | ++tv.tv_sec; |
| 1138 | tv.tv_usec -= 1000000; |
| 1139 | } |
| 1140 | |
| 1141 | settimeofday(&tv, NULL); |
| 1142 | #endif |
| 1143 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1144 | |
| 1145 | #endif // ndef BUILD_TWRPTAR_MAIN |