blob: 5145b2678219cbba9fc7de2c1e63132521605c1d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
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_Troy38bd7602012-09-14 13:33:53 -040019#include <stdio.h>
20#include <stdlib.h>
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060021#include <string>
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include <unistd.h>
23#include <vector>
24#include <dirent.h>
25#include <time.h>
Dees_Troy43d8b002012-09-17 16:00:01 -040026#include <errno.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <fcntl.h>
28#include <sys/mount.h>
Dees_Troya58bead2012-09-27 09:49:29 -040029#include <sys/reboot.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <sys/sendfile.h>
31#include <sys/stat.h>
32#include <sys/vfs.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000033#include <sys/types.h>
34#include <sys/wait.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035#include <iostream>
36#include <fstream>
Dees_Troy83bd4832013-05-04 12:39:56 +000037#include <sstream>
Dees_Troy38bd7602012-09-14 13:33:53 -040038#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060040#ifndef BUILD_TWRPTAR_MAIN
Dees_Troyb46a6842012-09-25 11:06:46 -040041#include "data.hpp"
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060042#include "partitions.hpp"
Dees_Troy3477d712012-09-27 15:44:01 -040043#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000044#include "bootloader.h"
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060045#ifdef ANDROID_RB_POWEROFF
46 #include "cutils/android_reboot.h"
47#endif
48#endif // ndef BUILD_TWRPTAR_MAIN
Dees_Troy83bd4832013-05-04 12:39:56 +000049#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
50 #include "openaes/inc/oaes_lib.h"
51#endif
Dees_Troy38bd7602012-09-14 13:33:53 -040052
Dees_Troyb05ddee2013-01-28 20:24:50 +000053extern "C" {
54 #include "libcrecovery/common.h"
55}
56
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057/* Execute a command */
Vojtech Bocek05534202013-09-11 08:11:56 +020058int TWFunc::Exec_Cmd(const string& cmd, string &result) {
Dees_Troy29a06352013-08-24 12:06:47 +000059 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 bigbiff9c754052013-01-09 09:09:08 -050069 result += buffer;
Dees_Troyb05ddee2013-01-28 20:24:50 +000070 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050071 }
Dees_Troy29a06352013-08-24 12:06:47 +000072 ret = __pclose(exec);
73 return ret;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050074}
75
Vojtech Bocek05534202013-09-11 08:11:56 +020076int 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_Troy38bd7602012-09-14 13:33:53 -040098// Returns "file.name" from a full /path/to/file.name
99string 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
110string 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 Yonkeraf2897c2014-02-10 13:07:14 -0600120int 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
145bool 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
154int 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
176int 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
269unsigned 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_Troy38bd7602012-09-14 13:33:53 -0400279// Returns "/path" from a full /path/to/file.name
280string 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
295void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400296 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_Troy2673cec2013-04-02 20:22:16 +0000304 gui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500305 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400306 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000307 gui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500308 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400309 need_libs = 1;
310 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 gui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400312 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 gui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500314 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400315 need_libs = 1;
316 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000317 gui_print("dump_image is already installed, skipping...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400318 if (need_libs) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000319 gui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500320 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_Troy38bd7602012-09-14 13:33:53 -0400324 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000325 gui_print("Installing HTC Dumlock app...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400326 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327 unlink("/data/app/com.teamwin.htcdumlock*");
328 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400329 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000330 gui_print("HTC Dumlock is installed.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400331}
332
333void TWFunc::htc_dumlock_restore_original_boot(void) {
334 if (!PartitionManager.Mount_By_Path("/sdcard", true))
335 return;
336
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 gui_print("Restoring original boot...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200338 Exec_Cmd("htcdumlock restore");
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 gui_print("Original boot restored.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400340}
341
342void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
343 if (!PartitionManager.Mount_By_Path("/sdcard", true))
344 return;
Dees_Troy2673cec2013-04-02 20:22:16 +0000345 gui_print("Reflashing recovery to boot...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200346 Exec_Cmd("htcdumlock recovery noreboot");
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 gui_print("Recovery is flashed to boot.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400348}
Dees_Troy43d8b002012-09-17 16:00:01 -0400349
350int 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_Troy2673cec2013-04-02 20:22:16 +0000359 LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
Dees_Troy43d8b002012-09-17 16:00:01 -0400360 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_Troyb46a6842012-09-25 11:06:46 -0400370void 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
381void 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_Troy7c2dec82012-09-26 09:49:14 -0400390}
391
Dees_Troy2673cec2013-04-02 20:22:16 +0000392void TWFunc::Copy_Log(string Source, string Destination) {
Dees Troy9d7fdf52013-09-19 20:49:25 +0000393 PartitionManager.Mount_By_Path(Destination, false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000394 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_Troy6ef66352013-02-21 08:26:57 -0600397 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000398 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_Troy6ef66352013-02-21 08:26:57 -0600407 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 fflush(destination_log);
409 fclose(destination_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600410 }
Dees_Troya58bead2012-09-27 09:49:29 -0400411}
412
Dees_Troy2673cec2013-04-02 20:22:16 +0000413void TWFunc::Update_Log_File(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500414 // Copy logs to cache so the system can find out what happened.
Dees Troy9d7fdf52013-09-19 20:49:25 +0000415 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_Troya58bead2012-09-27 09:49:29 -0400429
Dees_Troy2673cec2013-04-02 20:22:16 +0000430 // 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_Troya58bead2012-09-27 09:49:29 -0400445
Dees Troy9d7fdf52013-09-19 20:49:25 +0000446 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 bigbiff9c754052013-01-09 09:09:08 -0500450 }
Dees_Troya58bead2012-09-27 09:49:29 -0400451
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 sync();
453}
454
455void 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_Troya58bead2012-09-27 09:49:29 -0400459}
460
461// reboot: Reboot the system. Return -1 on error, no return on success
462int TWFunc::tw_reboot(RebootCommand command)
463{
464 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600465 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400466
Dees_Troy2673cec2013-04-02 20:22:16 +0000467 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_Troya4438782013-02-22 18:44:00 +0000483#ifdef ANDROID_RB_POWEROFF
Dees_Troy2673cec2013-04-02 20:22:16 +0000484 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
Dees_Troya4438782013-02-22 18:44:00 +0000485#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000486 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_Troy6ef66352013-02-21 08:26:57 -0600492 }
493 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400494}
495
496void 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_Troy2673cec2013-04-02 20:22:16 +0000501 gui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500502 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Vojtech Bocek05534202013-09-11 08:11:56 +0200503 TWFunc::Exec_Cmd(script_file);
Dees_Troy2673cec2013-04-02 20:22:16 +0000504 gui_print("\nFinished running %s script.\n", display_name);
Dees_Troya58bead2012-09-27 09:49:29 -0400505 }
Dees_Troy3477d712012-09-27 15:44:01 -0400506}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507
508int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000509 DIR *d = opendir(path.c_str());
510 int r = 0;
511 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500512
Dees_Troyce675462013-01-09 19:48:21 +0000513 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000514 LOGERR("Error opening '%s'\n", path.c_str());
Dees_Troyce675462013-01-09 19:48:21 +0000515 return -1;
516 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500517
Dees_Troyce675462013-01-09 19:48:21 +0000518 if (d) {
519 struct dirent *p;
520 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000521 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 bigbiff9c754052013-01-09 09:09:08 -0500528 if (p->d_type == DT_DIR)
529 r = rmdir(new_path.c_str());
530 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500532 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500533 } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +0000534 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000535 if (r != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000536 LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
Dees_Troye34c1332013-02-06 19:13:00 +0000537 }
Dees_Troyce675462013-01-09 19:48:21 +0000538 }
539 }
540 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500541
542 if (!r) {
543 if (skipParent)
544 return 0;
545 else
546 r = rmdir(path.c_str());
547 }
Dees_Troyce675462013-01-09 19:48:21 +0000548 }
549 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500550}
551
552int TWFunc::copy_file(string src, string dst, int mode) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000553 LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500554 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 bigbiff8a68c312013-02-10 14:28:30 -0500559 if (chmod(dst.c_str(), mode) != 0)
560 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500561 return 0;
562}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000563
564unsigned 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_Troye34c1332013-02-06 19:13:00 +0000583}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500584
585int TWFunc::read_file(string fn, string& results) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 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 bigbiffcdcfee42013-02-27 21:11:26 -0500593 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594
595 LOGINFO("Cannot find file %s\n", fn.c_str());
596 return -1;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500597}
598
599int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500600 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500601 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500602 file.open(fn.c_str(), ios::in);
603 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500604 while (getline(file, line))
605 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500606 file.close();
607 return 0;
608 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000609 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500610 return -1;
611}
612
613int 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_Troy2673cec2013-04-02 20:22:16 +0000621 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500622 return -1;
623}
624
625timespec TWFunc::timespec_diff(timespec& start, timespec& end)
626{
Dees_Troy6ef66352013-02-21 08:26:57 -0600627 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 bigbiff8a68c312013-02-10 14:28:30 -0500636}
637
Vojtech Boceke5ffcd12014-02-06 21:17:32 +0100638int32_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 Bocekfafb0c52013-07-25 22:53:02 +0200644int TWFunc::drop_caches(void) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600645 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
652int 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
678bool TWFunc::Fix_su_Perms(void) {
679 if (!PartitionManager.Mount_By_Path("/system", true))
680 return false;
681
Ethan Yonker0385f512014-02-06 14:33:02 -0600682 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_Troy6ef66352013-02-21 08:26:57 -0600690 string file = "/system/bin/su";
691 if (TWFunc::Path_Exists(file)) {
692 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000693 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600694 return false;
695 }
Ethan Yonker0385f512014-02-06 14:33:02 -0600696 if (tw_chmod(file, su_perms) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000697 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600698 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_Troy2673cec2013-04-02 20:22:16 +0000704 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600705 return false;
706 }
Ethan Yonker0385f512014-02-06 14:33:02 -0600707 if (tw_chmod(file, su_perms) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600709 return false;
710 }
711 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000712 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 Yonker0385f512014-02-06 14:33:02 -0600718 if (tw_chmod(file, "0755") != 0) {
Dees_Troya7939bb2013-08-29 20:21:12 +0000719 LOGERR("Failed to chmod '%s'\n", file.c_str());
720 return false;
721 }
722 }
Dees_Troy6ef66352013-02-21 08:26:57 -0600723 file = "/system/bin/.ext/.su";
724 if (TWFunc::Path_Exists(file)) {
725 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000726 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600727 return false;
728 }
Ethan Yonker0385f512014-02-06 14:33:02 -0600729 if (tw_chmod(file, su_perms) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000730 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600731 return false;
732 }
733 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000734 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_Troy6ef66352013-02-21 08:26:57 -0600756 file = "/system/app/Superuser.apk";
757 if (TWFunc::Path_Exists(file)) {
758 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000759 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600760 return false;
761 }
762 if (tw_chmod(file, "0644") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000763 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600764 return false;
765 }
766 }
767 sync();
768 if (!PartitionManager.UnMount_By_Path("/system", true))
769 return false;
770 return true;
771}
772
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200773int TWFunc::tw_chmod(const string& fn, const string& mode) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600774 long mask = 0;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200775 std::string::size_type n = mode.length();
776 int cls = 0;
Dees_Troy6ef66352013-02-21 08:26:57 -0600777
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200778 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_Troy6ef66352013-02-21 08:26:57 -0600788 if (mode[n] == '0')
789 continue;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200790 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600791 mask |= S_ISVTX;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200792 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600793 mask |= S_ISGID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200794 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600795 mask |= S_ISUID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200796 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600797 mask |= S_ISVTX;
798 mask |= S_ISUID;
799 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200800 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600801 mask |= S_ISGID;
802 mask |= S_ISUID;
803 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200804 else if (mode[n] == '7') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600805 mask |= S_ISVTX;
806 mask |= S_ISGID;
807 mask |= S_ISUID;
808 }
809 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200810 else if (cls == 1) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600811 if (mode[n] == '7') {
812 mask |= S_IRWXU;
813 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200814 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600815 mask |= S_IRUSR;
816 mask |= S_IWUSR;
817 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200818 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600819 mask |= S_IRUSR;
820 mask |= S_IXUSR;
821 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200822 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600823 mask |= S_IRUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200824 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600825 mask |= S_IWUSR;
826 mask |= S_IRUSR;
827 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200828 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600829 mask |= S_IWUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200830 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600831 mask |= S_IXUSR;
832 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200833 else if (cls == 2) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600834 if (mode[n] == '7') {
835 mask |= S_IRWXG;
836 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200837 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600838 mask |= S_IRGRP;
839 mask |= S_IWGRP;
840 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200841 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600842 mask |= S_IRGRP;
843 mask |= S_IXGRP;
844 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200845 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600846 mask |= S_IRGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200847 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600848 mask |= S_IWGRP;
849 mask |= S_IXGRP;
850 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200851 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600852 mask |= S_IWGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200853 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600854 mask |= S_IXGRP;
855 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200856 else if (cls == 3) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600857 if (mode[n] == '7') {
858 mask |= S_IRWXO;
859 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200860 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600861 mask |= S_IROTH;
862 mask |= S_IWOTH;
863 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200864 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600865 mask |= S_IROTH;
866 mask |= S_IXOTH;
867 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200868 else if (mode[n] == '4')
869 mask |= S_IROTH;
870 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600871 mask |= S_IWOTH;
872 mask |= S_IXOTH;
873 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200874 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600875 mask |= S_IWOTH;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200876 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600877 mask |= S_IXOTH;
878 }
879 }
880
881 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
Dees_Troy6ef66352013-02-21 08:26:57 -0600883 return -1;
884 }
885
886 return 0;
887}
888
889bool TWFunc::Install_SuperSU(void) {
890 if (!PartitionManager.Mount_By_Path("/system", true))
891 return false;
892
Vojtech Bocek05534202013-09-11 08:11:56 +0200893 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su");
jt1134113ee732013-02-22 23:26:10 -0600894 if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 LOGERR("Failed to copy su binary to /system/bin\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600896 return false;
897 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000898 if (!Path_Exists("/system/bin/.ext")) {
899 mkdir("/system/bin/.ext", 0777);
900 }
Vojtech Bocek05534202013-09-11 08:11:56 +0200901 TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su");
Dees_Troya7939bb2013-08-29 20:21:12 +0000902 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 Bocek05534202013-09-11 08:11:56 +0200906 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu");
Dees_Troya7939bb2013-08-29 20:21:12 +0000907 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 Bocek05534202013-09-11 08:11:56 +0200912 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon");
Dees_Troya7939bb2013-08-29 20:21:12 +0000913 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 Bocek05534202013-09-11 08:11:56 +0200918 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh");
Dees_Troya7939bb2013-08-29 20:21:12 +0000919 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 }
jt1134113ee732013-02-22 23:26:10 -0600924 if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000925 LOGERR("Failed to copy Superuser app to /system/app\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600926 return false;
927 }
928 if (!Fix_su_Perms())
929 return false;
930 return true;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500931}
Dees_Troy83bd4832013-05-04 12:39:56 +0000932
Dees_Troy83bd4832013-05-04 12:39:56 +0000933bool 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 Troyb21cc642013-09-10 17:36:41 +0000961string 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 Yonkerb5557892014-02-07 21:43:20 -0600971string TWFunc::System_Property_Get(string Prop_Name) {
Dees Troyb21cc642013-09-10 17:36:41 +0000972 bool mount_state = PartitionManager.Is_Mounted_By_Path("/system");
973 std::vector<string> buildprop;
Ethan Yonkerb5557892014-02-07 21:43:20 -0600974 string propvalue;
975 if (!PartitionManager.Mount_By_Path("/system", true))
976 return propvalue;
Dees Troyb21cc642013-09-10 17:36:41 +0000977 if (TWFunc::read_file("/system/build.prop", buildprop) != 0) {
Ethan Yonkerb5557892014-02-07 21:43:20 -0600978 LOGINFO("Unable to open /system/build.prop for getting '%s'.\n", Prop_Name.c_str());
Vojtech Boceka2e70162013-09-17 17:05:10 +0200979 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
Dees Troyb21cc642013-09-10 17:36:41 +0000980 if (!mount_state)
981 PartitionManager.UnMount_By_Path("/system", false);
Ethan Yonkerb5557892014-02-07 21:43:20 -0600982 return propvalue;
Dees Troyb21cc642013-09-10 17:36:41 +0000983 }
984 int line_count = buildprop.size();
985 int index;
986 size_t start_pos = 0, end_pos;
Ethan Yonkerb5557892014-02-07 21:43:20 -0600987 string propname;
Dees Troyb21cc642013-09-10 17:36:41 +0000988 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 Yonkerb5557892014-02-07 21:43:20 -0600991 if (propname == Prop_Name) {
Dees Troyb21cc642013-09-10 17:36:41 +0000992 propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size());
Ethan Yonkerb5557892014-02-07 21:43:20 -0600993 if (!mount_state)
994 PartitionManager.UnMount_By_Path("/system", false);
995 return propvalue;
Dees Troyb21cc642013-09-10 17:36:41 +0000996 }
997 }
Dees Troyb21cc642013-09-10 17:36:41 +0000998 if (!mount_state)
999 PartitionManager.UnMount_By_Path("/system", false);
Ethan Yonkerb5557892014-02-07 21:43:20 -06001000 return propvalue;
1001}
1002
1003void 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 Bocek05534202013-09-11 08:11:56 +02001021}
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +01001022
1023void 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 Yonkeraf2897c2014-02-10 13:07:14 -06001115
1116#endif // ndef BUILD_TWRPTAR_MAIN