blob: 28a0f0cc7249c22afb17cc87f1ac5d734ba38392 [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>
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040038#include <cctype>
bigbiff74a6d0d2015-02-14 20:49:44 -050039#include <algorithm>
bigbiff bigbiff19874f12019-01-08 20:06:57 -050040#include <selinux/label.h>
Dees_Troy38bd7602012-09-14 13:33:53 -040041#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000042#include "twcommon.h"
Ethan Yonker472f5062016-02-25 13:47:30 -060043#include "gui/gui.hpp"
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060044#ifndef BUILD_TWRPTAR_MAIN
Dees_Troyb46a6842012-09-25 11:06:46 -040045#include "data.hpp"
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060046#include "partitions.hpp"
Dees_Troy3477d712012-09-27 15:44:01 -040047#include "variables.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050048#include "bootloader_message_twrp/include/bootloader_message_twrp/bootloader_message.h"
Tom Hite5a926722014-09-15 01:31:03 +000049#include "cutils/properties.h"
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -050050#include "cutils/android_reboot.h"
51#include <sys/reboot.h>
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060052#endif // ndef BUILD_TWRPTAR_MAIN
Dees_Troy83bd4832013-05-04 12:39:56 +000053#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
54 #include "openaes/inc/oaes_lib.h"
55#endif
Ethan Yonkerf1179622016-08-25 15:32:21 -050056#include "set_metadata.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040057
Dees_Troyb05ddee2013-01-28 20:24:50 +000058extern "C" {
59 #include "libcrecovery/common.h"
60}
61
bigbiff bigbiff19874f12019-01-08 20:06:57 -050062struct selabel_handle *selinux_handle;
63
bigbiff bigbiff9c754052013-01-09 09:09:08 -050064/* Execute a command */
Vojtech Bocek05534202013-09-11 08:11:56 +020065int TWFunc::Exec_Cmd(const string& cmd, string &result) {
Dees_Troy29a06352013-08-24 12:06:47 +000066 FILE* exec;
67 char buffer[130];
68 int ret = 0;
69 exec = __popen(cmd.c_str(), "r");
70 if (!exec) return -1;
Matt Mowera8a89d12016-12-30 18:10:37 -060071 while (!feof(exec)) {
Dees_Troy29a06352013-08-24 12:06:47 +000072 if (fgets(buffer, 128, exec) != NULL) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073 result += buffer;
Dees_Troyb05ddee2013-01-28 20:24:50 +000074 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075 }
Dees_Troy29a06352013-08-24 12:06:47 +000076 ret = __pclose(exec);
77 return ret;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078}
79
Ethan Yonker7e941582019-03-22 08:18:21 -050080int TWFunc::Exec_Cmd(const string& cmd, bool Show_Errors) {
Vojtech Bocek05534202013-09-11 08:11:56 +020081 pid_t pid;
82 int status;
83 switch(pid = fork())
84 {
85 case -1:
bigbiff bigbiff731df792014-02-20 18:26:13 -050086 LOGERR("Exec_Cmd(): vfork failed: %d!\n", errno);
Vojtech Bocek05534202013-09-11 08:11:56 +020087 return -1;
88 case 0: // child
89 execl("/sbin/sh", "sh", "-c", cmd.c_str(), NULL);
90 _exit(127);
91 break;
92 default:
93 {
Ethan Yonker7e941582019-03-22 08:18:21 -050094 if (TWFunc::Wait_For_Child(pid, &status, cmd, Show_Errors) != 0)
Vojtech Bocek05534202013-09-11 08:11:56 +020095 return -1;
96 else
97 return 0;
98 }
99 }
100}
101
Dees_Troy38bd7602012-09-14 13:33:53 -0400102// Returns "file.name" from a full /path/to/file.name
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103string TWFunc::Get_Filename(const string& Path) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400104 size_t pos = Path.find_last_of("/");
105 if (pos != string::npos) {
106 string Filename;
107 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
108 return Filename;
109 } else
110 return Path;
111}
112
113// Returns "/path/to/" from a full /path/to/file.name
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600114string TWFunc::Get_Path(const string& Path) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400115 size_t pos = Path.find_last_of("/");
116 if (pos != string::npos) {
117 string Pathonly;
118 Pathonly = Path.substr(0, pos + 1);
119 return Pathonly;
120 } else
121 return Path;
122}
123
Ethan Yonker7e941582019-03-22 08:18:21 -0500124int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors) {
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600125 pid_t rc_pid;
126
127 rc_pid = waitpid(pid, status, 0);
128 if (rc_pid > 0) {
Vojtech Bocek0fdcbec2015-03-20 15:36:40 +0100129 if (WIFSIGNALED(*status)) {
Ethan Yonker7e941582019-03-22 08:18:21 -0500130 if (Show_Errors)
131 gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600132 return -1;
Vojtech Bocek0fdcbec2015-03-20 15:36:40 +0100133 } else if (WEXITSTATUS(*status) == 0) {
134 LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success
135 } else {
Ethan Yonker7e941582019-03-22 08:18:21 -0500136 if (Show_Errors)
137 gui_msg(Msg(msg::kError, "pid_error={1} process ended with ERROR: {2}")(Child_Name)(WEXITSTATUS(*status))); // Graceful exit, but there was an error
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600138 return -1;
139 }
140 } else { // no PID returned
141 if (errno == ECHILD)
that2252d242015-04-03 22:33:04 +0200142 LOGERR("%s no child process exist\n", Child_Name.c_str());
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600143 else {
that2252d242015-04-03 22:33:04 +0200144 LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600145 return -1;
146 }
147 }
148 return 0;
149}
150
Ethan Yonkerddb63e22017-01-19 14:01:57 -0600151int TWFunc::Wait_For_Child_Timeout(pid_t pid, int *status, const string& Child_Name, int timeout) {
152 pid_t retpid = waitpid(pid, status, WNOHANG);
153 for (; retpid == 0 && timeout; --timeout) {
154 sleep(1);
155 retpid = waitpid(pid, status, WNOHANG);
156 }
157 if (retpid == 0 && timeout == 0) {
158 LOGERR("%s took too long, killing process\n", Child_Name.c_str());
159 kill(pid, SIGKILL);
Ethan Yonkerddb63e22017-01-19 14:01:57 -0600160 for (timeout = 5; retpid == 0 && timeout; --timeout) {
161 sleep(1);
162 retpid = waitpid(pid, status, WNOHANG);
163 }
164 if (retpid)
165 LOGINFO("Child process killed successfully\n");
166 else
167 LOGINFO("Child process took too long to kill, may be a zombie process\n");
168 return -1;
169 } else if (retpid > 0) {
170 if (WIFSIGNALED(*status)) {
171 gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination
172 return -1;
173 }
174 } else if (retpid < 0) { // no PID returned
175 if (errno == ECHILD)
176 LOGERR("%s no child process exist\n", Child_Name.c_str());
177 else {
178 LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno);
179 return -1;
180 }
181 }
182 return 0;
183}
184
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600185bool TWFunc::Path_Exists(string Path) {
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600186 struct stat st;
187 if (stat(Path.c_str(), &st) != 0)
188 return false;
189 else
190 return true;
191}
192
bigbiffce8f83c2015-12-12 18:30:21 -0500193Archive_Type TWFunc::Get_File_Type(string fn) {
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600194 string::size_type i = 0;
195 int firstbyte = 0, secondbyte = 0;
196 char header[3];
197
198 ifstream f;
199 f.open(fn.c_str(), ios::in | ios::binary);
200 f.get(header, 3);
201 f.close();
202 firstbyte = header[i] & 0xff;
203 secondbyte = header[++i] & 0xff;
204
205 if (firstbyte == 0x1f && secondbyte == 0x8b)
bigbiffce8f83c2015-12-12 18:30:21 -0500206 return COMPRESSED;
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600207 else if (firstbyte == 0x4f && secondbyte == 0x41)
bigbiffce8f83c2015-12-12 18:30:21 -0500208 return ENCRYPTED;
209 return UNCOMPRESSED; // default
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600210}
211
212int TWFunc::Try_Decrypting_File(string fn, string password) {
213#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
214 OAES_CTX * ctx = NULL;
215 uint8_t _key_data[32] = "";
216 FILE *f;
217 uint8_t buffer[4096];
218 uint8_t *buffer_out = NULL;
219 uint8_t *ptr = NULL;
220 size_t read_len = 0, out_len = 0;
Matt Mower23d8aae2017-01-06 14:30:33 -0600221 int firstbyte = 0, secondbyte = 0;
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600222 size_t _j = 0;
223 size_t _key_data_len = 0;
224
225 // mostly kanged from OpenAES oaes.c
Matt Mowera8a89d12016-12-30 18:10:37 -0600226 for ( _j = 0; _j < 32; _j++ )
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600227 _key_data[_j] = _j + 1;
228 _key_data_len = password.size();
Matt Mowera8a89d12016-12-30 18:10:37 -0600229 if ( 16 >= _key_data_len )
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600230 _key_data_len = 16;
Matt Mowera8a89d12016-12-30 18:10:37 -0600231 else if ( 24 >= _key_data_len )
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600232 _key_data_len = 24;
233 else
Matt Mowera8a89d12016-12-30 18:10:37 -0600234 _key_data_len = 32;
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600235 memcpy(_key_data, password.c_str(), password.size());
236
237 ctx = oaes_alloc();
238 if (ctx == NULL) {
239 LOGERR("Failed to allocate OAES\n");
240 return -1;
241 }
242
243 oaes_key_import_data(ctx, _key_data, _key_data_len);
244
245 f = fopen(fn.c_str(), "rb");
246 if (f == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500247 LOGERR("Failed to open '%s' to try decrypt: %s\n", fn.c_str(), strerror(errno));
Matt Mower13a8f0b2015-09-26 15:40:03 -0500248 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600249 return -1;
250 }
251 read_len = fread(buffer, sizeof(uint8_t), 4096, f);
252 if (read_len <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500253 LOGERR("Read size during try decrypt failed: %s\n", strerror(errno));
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600254 fclose(f);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500255 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600256 return -1;
257 }
258 if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) {
259 LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n");
260 fclose(f);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500261 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600262 return -1;
263 }
264 buffer_out = (uint8_t *) calloc(out_len, sizeof(char));
265 if (buffer_out == NULL) {
266 LOGERR("Failed to allocate output buffer for try decrypt.\n");
267 fclose(f);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500268 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600269 return -1;
270 }
271 if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) {
272 LOGERR("Failed to decrypt file '%s'\n", fn.c_str());
273 fclose(f);
274 free(buffer_out);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500275 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600276 return 0;
277 }
278 fclose(f);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500279 oaes_free(&ctx);
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600280 if (out_len < 2) {
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500281 LOGINFO("Successfully decrypted '%s' but read length too small.\n", fn.c_str());
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600282 free(buffer_out);
283 return 1; // Decrypted successfully
284 }
285 ptr = buffer_out;
286 firstbyte = *ptr & 0xff;
287 ptr++;
288 secondbyte = *ptr & 0xff;
289 if (firstbyte == 0x1f && secondbyte == 0x8b) {
290 LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str());
291 free(buffer_out);
292 return 3; // Compressed
293 }
294 if (out_len >= 262) {
295 ptr = buffer_out + 257;
296 if (strncmp((char*)ptr, "ustar", 5) == 0) {
297 LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str());
298 free(buffer_out);
299 return 2; // Tar
300 }
301 }
302 free(buffer_out);
303 LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str());
304 return 1; // Decrypted successfully
305#else
306 LOGERR("Encrypted backup support not included.\n");
307 return -1;
308#endif
309}
310
Ethan Yonker472f5062016-02-25 13:47:30 -0600311unsigned long TWFunc::Get_File_Size(const string& Path) {
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600312 struct stat st;
313
314 if (stat(Path.c_str(), &st) != 0)
315 return 0;
316 return st.st_size;
317}
318
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100319std::string TWFunc::Remove_Trailing_Slashes(const std::string& path, bool leaveLast)
320{
321 std::string res;
322 size_t last_idx = 0, idx = 0;
323
Matt Mowera8a89d12016-12-30 18:10:37 -0600324 while (last_idx != std::string::npos)
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100325 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600326 if (last_idx != 0)
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100327 res += '/';
328
329 idx = path.find_first_of('/', last_idx);
Matt Mowera8a89d12016-12-30 18:10:37 -0600330 if (idx == std::string::npos) {
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100331 res += path.substr(last_idx, idx);
332 break;
333 }
334
335 res += path.substr(last_idx, idx-last_idx);
336 last_idx = path.find_first_not_of('/', idx);
337 }
338
Matt Mowera8a89d12016-12-30 18:10:37 -0600339 if (leaveLast)
Vojtech Bocek05f87d62014-03-11 22:08:23 +0100340 res += '/';
341 return res;
342}
343
Matt Mower2416a502016-04-12 19:54:46 -0500344void TWFunc::Strip_Quotes(char* &str) {
345 if (strlen(str) > 0 && str[0] == '\"')
346 str++;
347 if (strlen(str) > 0 && str[strlen(str)-1] == '\"')
348 str[strlen(str)-1] = 0;
349}
350
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500351vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) {
352 vector<string> res;
353
354 if (in.empty() || del == '\0')
355 return res;
356
357 string field;
358 istringstream f(in);
359 if (del == '\n') {
Matt Mowera8a89d12016-12-30 18:10:37 -0600360 while (getline(f, field)) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500361 if (field.empty() && skip_empty)
362 continue;
Matt Mowera8a89d12016-12-30 18:10:37 -0600363 res.push_back(field);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500364 }
365 } else {
Matt Mowera8a89d12016-12-30 18:10:37 -0600366 while (getline(f, field, del)) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500367 if (field.empty() && skip_empty)
368 continue;
369 res.push_back(field);
370 }
371 }
372 return res;
373}
374
Ethan Yonker472f5062016-02-25 13:47:30 -0600375timespec TWFunc::timespec_diff(timespec& start, timespec& end)
376{
377 timespec temp;
378 if ((end.tv_nsec-start.tv_nsec)<0) {
379 temp.tv_sec = end.tv_sec-start.tv_sec-1;
380 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
381 } else {
382 temp.tv_sec = end.tv_sec-start.tv_sec;
383 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
384 }
385 return temp;
386}
387
388int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end)
389{
390 return ((end.tv_sec * 1000) + end.tv_nsec/1000000) -
391 ((start.tv_sec * 1000) + start.tv_nsec/1000000);
392}
393
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600394#ifndef BUILD_TWRPTAR_MAIN
395
Dees_Troy38bd7602012-09-14 13:33:53 -0400396// Returns "/path" from a full /path/to/file.name
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600397string TWFunc::Get_Root_Path(const string& Path) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400398 string Local_Path = Path;
399
400 // Make sure that we have a leading slash
401 if (Local_Path.substr(0, 1) != "/")
402 Local_Path = "/" + Local_Path;
403
404 // Trim the path to get the root path only
405 size_t position = Local_Path.find("/", 2);
406 if (position != string::npos) {
407 Local_Path.resize(position);
408 }
409 return Local_Path;
410}
411
412void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400413 int need_libs = 0;
414
Captain Throwback9d6feb52018-07-27 10:05:24 -0400415 if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true))
Dees_Troy38bd7602012-09-14 13:33:53 -0400416 return;
417
418 if (!PartitionManager.Mount_By_Path("/data", true))
419 return;
420
Ethan Yonker74db1572015-10-28 12:44:49 -0500421 gui_msg("install_dumlock=Installing HTC Dumlock to system...");
Dees Troy3454ade2015-01-20 19:21:04 +0000422 copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400423 if (!Path_Exists("/system/bin/flash_image")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500424 LOGINFO("Installing flash_image...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000425 copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400426 need_libs = 1;
427 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500428 LOGINFO("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400429 if (!Path_Exists("/system/bin/dump_image")) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500430 LOGINFO("Installing dump_image...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000431 copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400432 need_libs = 1;
433 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500434 LOGINFO("dump_image is already installed, skipping...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400435 if (need_libs) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500436 LOGINFO("Installing libs needed for flash_image and dump_image...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000437 copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644);
438 copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644);
439 copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644);
440 copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644);
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500442 LOGINFO("Installing HTC Dumlock app...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400443 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500444 unlink("/data/app/com.teamwin.htcdumlock*");
Dees Troy3454ade2015-01-20 19:21:04 +0000445 copy_file(TWHTCD_PATH "HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400446 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500447 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -0400448}
449
450void TWFunc::htc_dumlock_restore_original_boot(void) {
451 if (!PartitionManager.Mount_By_Path("/sdcard", true))
452 return;
453
Ethan Yonker74db1572015-10-28 12:44:49 -0500454 gui_msg("dumlock_restore=Restoring original boot...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200455 Exec_Cmd("htcdumlock restore");
Ethan Yonker74db1572015-10-28 12:44:49 -0500456 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -0400457}
458
459void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
460 if (!PartitionManager.Mount_By_Path("/sdcard", true))
461 return;
Ethan Yonker74db1572015-10-28 12:44:49 -0500462 gui_msg("dumlock_reflash=Reflashing recovery to boot...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200463 Exec_Cmd("htcdumlock recovery noreboot");
Ethan Yonker74db1572015-10-28 12:44:49 -0500464 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -0400465}
Dees_Troy43d8b002012-09-17 16:00:01 -0400466
467int TWFunc::Recursive_Mkdir(string Path) {
thatf1408b32016-01-03 11:09:15 +0100468 std::vector<std::string> parts = Split_String(Path, "/", true);
469 std::string cur_path;
470 for (size_t i = 0; i < parts.size(); ++i) {
471 cur_path += "/" + parts[i];
472 if (!TWFunc::Path_Exists(cur_path)) {
473 if (mkdir(cur_path.c_str(), 0777)) {
Matt Mower3c366972015-12-25 19:28:31 -0600474 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(cur_path)(strerror(errno)));
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600475 return false;
476 } else {
thatf1408b32016-01-03 11:09:15 +0100477 tw_set_default_metadata(cur_path.c_str());
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600478 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400479 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400480 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400481 return true;
482}
483
Dees_Troyb46a6842012-09-25 11:06:46 -0400484void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
485 string Display_Text;
486
487 DataManager::GetValue(Read_Value, Display_Text);
488 if (Display_Text.empty())
489 Display_Text = Default_Text;
490
491 DataManager::SetValue("tw_operation", Display_Text);
492 DataManager::SetValue("tw_partition", "");
493}
494
495void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
496 string Display_Text;
497
498 DataManager::GetValue(Read_Value, Display_Text);
499 if (Display_Text.empty())
500 Display_Text = Default_Text;
501
502 DataManager::SetValue("tw_operation", Display_Text);
503 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400504}
505
Dees_Troy2673cec2013-04-02 20:22:16 +0000506void TWFunc::Copy_Log(string Source, string Destination) {
Dees Troy9d7fdf52013-09-19 20:49:25 +0000507 PartitionManager.Mount_By_Path(Destination, false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000508 FILE *destination_log = fopen(Destination.c_str(), "a");
509 if (destination_log == NULL) {
510 LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600511 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 FILE *source_log = fopen(Source.c_str(), "r");
513 if (source_log != NULL) {
514 fseek(source_log, Log_Offset, SEEK_SET);
515 char buffer[4096];
516 while (fgets(buffer, sizeof(buffer), source_log))
517 fputs(buffer, destination_log); // Buffered write of log file
518 Log_Offset = ftell(source_log);
519 fflush(source_log);
520 fclose(source_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600521 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 fflush(destination_log);
523 fclose(destination_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600524 }
Dees_Troya58bead2012-09-27 09:49:29 -0400525}
526
Dees_Troy2673cec2013-04-02 20:22:16 +0000527void TWFunc::Update_Log_File(void) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500528 std::string recoveryDir = get_cache_dir() + "recovery/";
529
530 if (get_cache_dir() == NON_AB_CACHE_DIR) {
531 if (!PartitionManager.Mount_By_Path(NON_AB_CACHE_DIR, false)) {
532 LOGINFO("Failed to mount %s for TWFunc::Update_Log_File\n", NON_AB_CACHE_DIR);
Dees Troy9d7fdf52013-09-19 20:49:25 +0000533 }
Dees Troy9d7fdf52013-09-19 20:49:25 +0000534 }
Dees_Troya58bead2012-09-27 09:49:29 -0400535
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500536 if (!TWFunc::Path_Exists(recoveryDir)) {
537 LOGINFO("Recreating %s folder.\n", recoveryDir.c_str());
bigbiff bigbiffe4bdb152019-03-23 18:33:17 -0400538 if (!Create_Dir_Recursive(recoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, 0, 0)) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500539 LOGINFO("Unable to create %s folder.\n", recoveryDir.c_str());
540 }
541 }
542
543 std::string logCopy = recoveryDir + "log";
544 std::string lastLogCopy = recoveryDir + "last_log";
545 copy_file(logCopy, lastLogCopy, 600);
546 Copy_Log(TMP_LOG_FILE, logCopy);
547 chown(logCopy.c_str(), 1000, 1000);
548 chmod(logCopy.c_str(), 0600);
549 chmod(lastLogCopy.c_str(), 0640);
550
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 // Reset bootloader message
552 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
553 if (Part != NULL) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500554 std::string err;
555 if (!clear_bootloader_message((void*)&err)) {
Ethan Yonkerb5236502016-11-19 22:24:59 -0600556 if (err == "no misc device set") {
557 LOGINFO("%s\n", err.c_str());
558 } else {
559 LOGERR("%s\n", err.c_str());
560 }
561 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000562 }
Dees_Troya58bead2012-09-27 09:49:29 -0400563
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500564 if (get_cache_dir() == NON_AB_CACHE_DIR) {
565 if (PartitionManager.Mount_By_Path("/cache", false)) {
566 if (unlink("/cache/recovery/command") && errno != ENOENT) {
567 LOGINFO("Can't unlink %s\n", "/cache/recovery/command");
568 }
Dees Troy9d7fdf52013-09-19 20:49:25 +0000569 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500570 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000571 sync();
572}
573
574void TWFunc::Update_Intent_File(string Intent) {
575 if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600576 TWFunc::write_to_file("/cache/recovery/intent", Intent);
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 }
Dees_Troya58bead2012-09-27 09:49:29 -0400578}
579
580// reboot: Reboot the system. Return -1 on error, no return on success
581int TWFunc::tw_reboot(RebootCommand command)
582{
Ethan Yonkerfe916112016-03-14 14:54:37 -0500583 DataManager::Flush();
584 Update_Log_File();
Dees_Troya58bead2012-09-27 09:49:29 -0400585 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600586 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400587
Dees_Troy2673cec2013-04-02 20:22:16 +0000588 switch (command) {
589 case rb_current:
590 case rb_system:
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 Update_Intent_File("s");
592 sync();
593 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500594#ifdef ANDROID_RB_PROPERTY
595 return property_set(ANDROID_RB_PROPERTY, "reboot,");
596#elif defined(ANDROID_RB_RESTART)
597 return android_reboot(ANDROID_RB_RESTART, 0, 0);
598#else
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 return reboot(RB_AUTOBOOT);
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500600#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000601 case rb_recovery:
602 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600603#ifdef ANDROID_RB_PROPERTY
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500604 return property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600605#else
606 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
607#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 case rb_bootloader:
609 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600610#ifdef ANDROID_RB_PROPERTY
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500611 return property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600612#else
613 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
614#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000615 case rb_poweroff:
616 check_and_run_script("/sbin/poweroff.sh", "power off");
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500617#ifdef ANDROID_RB_PROPERTY
618 return property_set(ANDROID_RB_PROPERTY, "shutdown,");
619#elif defined(ANDROID_RB_POWEROFF)
620 return android_reboot(ANDROID_RB_POWEROFF, 0, 0);
621#else
Dees_Troy2673cec2013-04-02 20:22:16 +0000622 return reboot(RB_POWER_OFF);
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500623#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000624 case rb_download:
625 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600626#ifdef ANDROID_RB_PROPERTY
Ethan Yonkerbc2bc6b2015-03-24 21:37:52 -0500627 return property_set(ANDROID_RB_PROPERTY, "reboot,download");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600628#else
629 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
630#endif
mauronofrioe9a49ef2018-10-03 13:38:16 +0200631 case rb_edl:
632 check_and_run_script("/sbin/rebootedl.sh", "reboot edl");
633#ifdef ANDROID_RB_PROPERTY
634 return property_set(ANDROID_RB_PROPERTY, "reboot,edl");
635#else
636 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "edl");
637#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000638 default:
639 return -1;
Dees_Troy6ef66352013-02-21 08:26:57 -0600640 }
641 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400642}
643
644void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
645{
646 // Check for and run startup script if script exists
647 struct stat st;
648 if (stat(script_file, &st) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500649 gui_msg(Msg("run_script=Running {1} script...")(display_name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500650 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Vojtech Bocek05534202013-09-11 08:11:56 +0200651 TWFunc::Exec_Cmd(script_file);
Ethan Yonker74db1572015-10-28 12:44:49 -0500652 gui_msg("done=Done.");
Dees_Troya58bead2012-09-27 09:49:29 -0400653 }
Dees_Troy3477d712012-09-27 15:44:01 -0400654}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500655
656int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000657 DIR *d = opendir(path.c_str());
658 int r = 0;
659 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500660
Dees_Troyce675462013-01-09 19:48:21 +0000661 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500662 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(path)(strerror(errno)));
Dees_Troyce675462013-01-09 19:48:21 +0000663 return -1;
664 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500665
Dees_Troyce675462013-01-09 19:48:21 +0000666 if (d) {
667 struct dirent *p;
668 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000669 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
670 continue;
671 new_path = path + "/";
672 new_path.append(p->d_name);
673 if (p->d_type == DT_DIR) {
674 r = removeDir(new_path, true);
675 if (!r) {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500676 if (p->d_type == DT_DIR)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500677 r = rmdir(new_path.c_str());
678 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000679 LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500680 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500681 } 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 +0000682 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000683 if (r != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
Dees_Troye34c1332013-02-06 19:13:00 +0000685 }
Dees_Troyce675462013-01-09 19:48:21 +0000686 }
687 }
688 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500689
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500690 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500691 if (skipParent)
692 return 0;
693 else
694 r = rmdir(path.c_str());
695 }
Dees_Troyce675462013-01-09 19:48:21 +0000696 }
697 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500698}
699
700int TWFunc::copy_file(string src, string dst, int mode) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000701 LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500702 ifstream srcfile(src.c_str(), ios::binary);
703 ofstream dstfile(dst.c_str(), ios::binary);
704 dstfile << srcfile.rdbuf();
705 srcfile.close();
706 dstfile.close();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500707 if (chmod(dst.c_str(), mode) != 0)
708 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500709 return 0;
710}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000711
712unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
713 struct stat st;
714
715 stat(Path.c_str(), &st);
716 if (st.st_mode & S_IFDIR)
717 return DT_DIR;
718 else if (st.st_mode & S_IFBLK)
719 return DT_BLK;
720 else if (st.st_mode & S_IFCHR)
721 return DT_CHR;
722 else if (st.st_mode & S_IFIFO)
723 return DT_FIFO;
724 else if (st.st_mode & S_IFLNK)
725 return DT_LNK;
726 else if (st.st_mode & S_IFREG)
727 return DT_REG;
728 else if (st.st_mode & S_IFSOCK)
729 return DT_SOCK;
730 return DT_UNKNOWN;
Dees_Troye34c1332013-02-06 19:13:00 +0000731}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500732
733int TWFunc::read_file(string fn, string& results) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200734 ifstream file;
735 file.open(fn.c_str(), ios::in);
736
737 if (file.is_open()) {
738 file >> results;
739 file.close();
740 return 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500741 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200742
743 LOGINFO("Cannot find file %s\n", fn.c_str());
744 return -1;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500745}
746
747int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500748 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500749 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500750 file.open(fn.c_str(), ios::in);
751 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500752 while (getline(file, line))
753 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500754 file.close();
755 return 0;
756 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000757 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500758 return -1;
759}
760
xNUTxe85f02d2014-07-18 01:30:58 +0200761int TWFunc::read_file(string fn, uint64_t& results) {
762 ifstream file;
763 file.open(fn.c_str(), ios::in);
764
765 if (file.is_open()) {
766 file >> results;
767 file.close();
768 return 0;
769 }
770
771 LOGINFO("Cannot find file %s\n", fn.c_str());
772 return -1;
773}
774
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600775int TWFunc::write_to_file(const string& fn, const string& line) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500776 FILE *file;
777 file = fopen(fn.c_str(), "w");
778 if (file != NULL) {
779 fwrite(line.c_str(), line.size(), 1, file);
780 fclose(file);
781 return 0;
782 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500784 return -1;
785}
786
Dees_Troy83bd4832013-05-04 12:39:56 +0000787bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) {
788 DIR* d;
789
790 string Filename;
791 Restore_Path += "/";
792 d = opendir(Restore_Path.c_str());
793 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500794 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Path)(strerror(errno)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000795 return false;
796 }
797
798 struct dirent* de;
799 while ((de = readdir(d)) != NULL) {
800 Filename = Restore_Path;
801 Filename += de->d_name;
bigbiffce8f83c2015-12-12 18:30:21 -0500802 if (TWFunc::Get_File_Type(Filename) == ENCRYPTED) {
Dees_Troy83bd4832013-05-04 12:39:56 +0000803 if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) {
804 DataManager::SetValue("tw_restore_password", ""); // Clear the bad password
805 DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask
806 closedir(d);
807 return false;
808 }
809 }
810 }
811 closedir(d);
812 return true;
813}
814
Dees Troyb21cc642013-09-10 17:36:41 +0000815string TWFunc::Get_Current_Date() {
816 string Current_Date;
817 time_t seconds = time(0);
818 struct tm *t = localtime(&seconds);
819 char timestamp[255];
820 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);
821 Current_Date = timestamp;
822 return Current_Date;
823}
824
Ethan Yonkerb5557892014-02-07 21:43:20 -0600825string TWFunc::System_Property_Get(string Prop_Name) {
Captain Throwback9d6feb52018-07-27 10:05:24 -0400826 bool mount_state = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Dees Troyb21cc642013-09-10 17:36:41 +0000827 std::vector<string> buildprop;
Ethan Yonkerb5557892014-02-07 21:43:20 -0600828 string propvalue;
Captain Throwback9d6feb52018-07-27 10:05:24 -0400829 if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true))
Ethan Yonkerb5557892014-02-07 21:43:20 -0600830 return propvalue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600831 string prop_file = "/system/build.prop";
832 if (!TWFunc::Path_Exists(prop_file))
Captain Throwback9d6feb52018-07-27 10:05:24 -0400833 prop_file = PartitionManager.Get_Android_Root_Path() + "/system/build.prop"; // for devices with system as a root file system (e.g. Pixel)
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600834 if (TWFunc::read_file(prop_file, buildprop) != 0) {
Captain Throwback9d6feb52018-07-27 10:05:24 -0400835 LOGINFO("Unable to open build.prop for getting '%s'.\n", Prop_Name.c_str());
Vojtech Boceka2e70162013-09-17 17:05:10 +0200836 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
Dees Troyb21cc642013-09-10 17:36:41 +0000837 if (!mount_state)
Captain Throwback9d6feb52018-07-27 10:05:24 -0400838 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
Ethan Yonkerb5557892014-02-07 21:43:20 -0600839 return propvalue;
Dees Troyb21cc642013-09-10 17:36:41 +0000840 }
841 int line_count = buildprop.size();
842 int index;
843 size_t start_pos = 0, end_pos;
Ethan Yonkerb5557892014-02-07 21:43:20 -0600844 string propname;
Dees Troyb21cc642013-09-10 17:36:41 +0000845 for (index = 0; index < line_count; index++) {
846 end_pos = buildprop.at(index).find("=", start_pos);
847 propname = buildprop.at(index).substr(start_pos, end_pos);
Ethan Yonkerb5557892014-02-07 21:43:20 -0600848 if (propname == Prop_Name) {
Dees Troyb21cc642013-09-10 17:36:41 +0000849 propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size());
Ethan Yonkerb5557892014-02-07 21:43:20 -0600850 if (!mount_state)
Captain Throwback9d6feb52018-07-27 10:05:24 -0400851 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
Ethan Yonkerb5557892014-02-07 21:43:20 -0600852 return propvalue;
Dees Troyb21cc642013-09-10 17:36:41 +0000853 }
854 }
Dees Troyb21cc642013-09-10 17:36:41 +0000855 if (!mount_state)
Captain Throwback9d6feb52018-07-27 10:05:24 -0400856 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
Ethan Yonkerb5557892014-02-07 21:43:20 -0600857 return propvalue;
858}
859
860void TWFunc::Auto_Generate_Backup_Name() {
861 string propvalue = System_Property_Get("ro.build.display.id");
862 if (propvalue.empty()) {
863 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
864 return;
865 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400866 else {
867 //remove periods from build display so it doesn't confuse the extension code
868 propvalue.erase(remove(propvalue.begin(), propvalue.end(), '.'), propvalue.end());
869 }
Ethan Yonkerb5557892014-02-07 21:43:20 -0600870 string Backup_Name = Get_Current_Date();
bigbiff74a6d0d2015-02-14 20:49:44 -0500871 Backup_Name += "_" + propvalue;
Ethan Yonkerb5557892014-02-07 21:43:20 -0600872 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
873 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
874 // Trailing spaces cause problems on some file systems, so remove them
875 string space_check, space = " ";
876 space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
877 while (space_check == space) {
878 Backup_Name.resize(Backup_Name.size() - 1);
879 space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
880 }
bigbiff74a6d0d2015-02-14 20:49:44 -0500881 replace(Backup_Name.begin(), Backup_Name.end(), ' ', '_');
Ethan Yonkerb5557892014-02-07 21:43:20 -0600882 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker92d48e02014-02-26 12:05:55 -0600883 if (PartitionManager.Check_Backup_Name(false) != 0) {
884 LOGINFO("Auto generated backup name '%s' contains invalid characters, using date instead.\n", Backup_Name.c_str());
885 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
886 }
Vojtech Bocek05534202013-09-11 08:11:56 +0200887}
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100888
nkk7198fc3992017-12-16 16:26:42 +0200889void TWFunc::Fixup_Time_On_Boot(const string& time_paths /* = "" */)
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100890{
891#ifdef QCOM_RTC_FIX
nkk7198fc3992017-12-16 16:26:42 +0200892 static bool fixed = false;
893 if (fixed)
894 return;
xNUTxe85f02d2014-07-18 01:30:58 +0200895
896 LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str());
897
898 struct timeval tv;
899 uint64_t offset = 0;
900 std::string sepoch = "/sys/class/rtc/rtc0/since_epoch";
901
902 if (TWFunc::read_file(sepoch, offset) == 0) {
903
904 LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str());
905
906 tv.tv_sec = offset;
907 tv.tv_usec = 0;
908 settimeofday(&tv, NULL);
909
910 gettimeofday(&tv, NULL);
911
Phoenix59146b05f22018-02-03 06:41:08 +0000912 if (tv.tv_sec > 1517600000) { // Anything older then 2 Feb 2018 19:33:20 GMT will do nicely thank you ;)
xNUTxe85f02d2014-07-18 01:30:58 +0200913
914 LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str());
nkk7198fc3992017-12-16 16:26:42 +0200915 fixed = true;
xNUTxe85f02d2014-07-18 01:30:58 +0200916 return;
917
918 }
919
920 } else {
921
922 LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str());
923
924 }
925
Ethan Yonker9132d912015-02-02 10:32:49 -0600926 LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n");
xNUTxe85f02d2014-07-18 01:30:58 +0200927
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100928 // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC.
929 // They never set it, it just ticks forward from 1970-01-01 00:00,
930 // and then they have files /data/system/time/ats_* with 64bit offset
931 // in miliseconds which, when added to the RTC, gives the correct time.
932 // So, the time is: (offset_from_ats + value_from_RTC)
933 // There are multiple ats files, they are for different systems? Bases?
934 // Like, ats_1 is for modem and ats_2 is for TOD (time of day?).
935 // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services
936
nkk7198fc3992017-12-16 16:26:42 +0200937 std::vector<std::string> paths; // space separated list of paths
938 if (time_paths.empty()) {
Mauronofrio Matarrese2dab70d2019-03-05 02:22:26 +0100939 paths = Split_String("/data/system/time/ /data/time/ /data/vendor/time/", " ");
nkk7198fc3992017-12-16 16:26:42 +0200940 if (!PartitionManager.Mount_By_Path("/data", false))
941 return;
942 } else {
943 // When specific path(s) are used, Fixup_Time needs those
944 // partitions to already be mounted!
945 paths = Split_String(time_paths, " ");
946 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100947
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100948 FILE *f;
xNUTxe85f02d2014-07-18 01:30:58 +0200949 offset = 0;
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100950 struct dirent *dt;
951 std::string ats_path;
952
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100953 // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead
954 // - it is the one for ATS_TOD (time of day?).
955 // However, I never saw a device where the offset differs between ats files.
nkk7198fc3992017-12-16 16:26:42 +0200956 for (size_t i = 0; i < paths.size(); ++i)
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100957 {
nkk7198fc3992017-12-16 16:26:42 +0200958 DIR *d = opendir(paths[i].c_str());
Matt Mowera8a89d12016-12-30 18:10:37 -0600959 if (!d)
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100960 continue;
961
Matt Mowera8a89d12016-12-30 18:10:37 -0600962 while ((dt = readdir(d)))
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100963 {
Matt Mowera8a89d12016-12-30 18:10:37 -0600964 if (dt->d_type != DT_REG || strncmp(dt->d_name, "ats_", 4) != 0)
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100965 continue;
966
Matt Mowera8a89d12016-12-30 18:10:37 -0600967 if (ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0)
nkk7198fc3992017-12-16 16:26:42 +0200968 ats_path = paths[i] + dt->d_name;
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100969 }
970
971 closedir(d);
972 }
973
nkk7198fc3992017-12-16 16:26:42 +0200974 if (ats_path.empty()) {
xNUTxe85f02d2014-07-18 01:30:58 +0200975 LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n");
nkk7198fc3992017-12-16 16:26:42 +0200976 } else if ((f = fopen(ats_path.c_str(), "r")) == NULL) {
Dees Troy3e254b92014-03-06 20:24:54 +0000977 LOGINFO("TWFunc::Fixup_Time: failed to open file %s\n", ats_path.c_str());
nkk7198fc3992017-12-16 16:26:42 +0200978 } else if (fread(&offset, sizeof(offset), 1, f) != 1) {
Dees Troy3e254b92014-03-06 20:24:54 +0000979 LOGINFO("TWFunc::Fixup_Time: failed load uint64 from file %s\n", ats_path.c_str());
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100980 fclose(f);
nkk7198fc3992017-12-16 16:26:42 +0200981 } else {
982 fclose(f);
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +0100983
nkk7198fc3992017-12-16 16:26:42 +0200984 LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), (unsigned long long) offset);
985 DataManager::SetValue("tw_qcom_ats_offset", (unsigned long long) offset, 1);
986 fixed = true;
987 }
988
989 if (!fixed) {
990 // Failed to get offset from ats file, check twrp settings
991 unsigned long long value;
992 if (DataManager::GetValue("tw_qcom_ats_offset", value) < 0) {
993 return;
994 } else {
995 offset = (uint64_t) value;
996 LOGINFO("TWFunc::Fixup_Time: Setting time offset from twrp setting file, offset %llu\n", (unsigned long long) offset);
997 // Do not consider the settings file as a definitive answer, keep fixed=false so next run will try ats files again
998 }
999 }
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +01001000
1001 gettimeofday(&tv, NULL);
1002
1003 tv.tv_sec += offset/1000;
Phoenix591e444d112018-02-03 07:23:54 +00001004#ifdef TW_CLOCK_OFFSET
1005// Some devices are even quirkier and have ats files that are offset from the actual time
1006 tv.tv_sec = tv.tv_sec + TW_CLOCK_OFFSET;
1007#endif
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +01001008 tv.tv_usec += (offset%1000)*1000;
1009
Matt Mowera8a89d12016-12-30 18:10:37 -06001010 while (tv.tv_usec >= 1000000)
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +01001011 {
1012 ++tv.tv_sec;
1013 tv.tv_usec -= 1000000;
1014 }
1015
1016 settimeofday(&tv, NULL);
xNUTxe85f02d2014-07-18 01:30:58 +02001017
1018 LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str());
Vojtech Bocekd0e38bc2014-02-03 23:36:57 +01001019#endif
1020}
Ethan Yonkeraf2897c2014-02-10 13:07:14 -06001021
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001022std::vector<std::string> TWFunc::Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty)
1023{
1024 std::vector<std::string> res;
1025 size_t idx = 0, idx_last = 0;
1026
Matt Mowera8a89d12016-12-30 18:10:37 -06001027 while (idx < str.size())
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001028 {
1029 idx = str.find_first_of(delimiter, idx_last);
Matt Mowera8a89d12016-12-30 18:10:37 -06001030 if (idx == std::string::npos)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001031 idx = str.size();
1032
Matt Mowera8a89d12016-12-30 18:10:37 -06001033 if (idx-idx_last != 0 || !removeEmpty)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +01001034 res.push_back(str.substr(idx_last, idx-idx_last));
1035
1036 idx_last = idx + delimiter.size();
1037 }
1038
1039 return res;
1040}
1041
Vojtech Bocek03fd6c52014-03-13 18:46:34 +01001042bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t uid, gid_t gid)
1043{
1044 std::vector<std::string> parts = Split_String(path, "/");
1045 std::string cur_path;
1046 struct stat info;
Matt Mowera8a89d12016-12-30 18:10:37 -06001047 for (size_t i = 0; i < parts.size(); ++i)
Vojtech Bocek03fd6c52014-03-13 18:46:34 +01001048 {
1049 cur_path += "/" + parts[i];
Matt Mowera8a89d12016-12-30 18:10:37 -06001050 if (stat(cur_path.c_str(), &info) < 0 || !S_ISDIR(info.st_mode))
Vojtech Bocek03fd6c52014-03-13 18:46:34 +01001051 {
Matt Mowera8a89d12016-12-30 18:10:37 -06001052 if (mkdir(cur_path.c_str(), mode) < 0)
Vojtech Bocek03fd6c52014-03-13 18:46:34 +01001053 return false;
1054 chown(cur_path.c_str(), uid, gid);
1055 }
1056 }
1057 return true;
1058}
1059
xNUTxe85f02d2014-07-18 01:30:58 +02001060int TWFunc::Set_Brightness(std::string brightness_value)
1061{
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +09001062 int result = -1;
1063 std::string secondary_brightness_file;
xNUTxe85f02d2014-07-18 01:30:58 +02001064
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +09001065 if (DataManager::GetIntValue("tw_has_brightnesss_file")) {
xNUTxe85f02d2014-07-18 01:30:58 +02001066 LOGINFO("TWFunc::Set_Brightness: Setting brightness control to %s\n", brightness_value.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001067 result = TWFunc::write_to_file(DataManager::GetStrValue("tw_brightness_file"), brightness_value);
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +09001068 DataManager::GetValue("tw_secondary_brightness_file", secondary_brightness_file);
1069 if (!secondary_brightness_file.empty()) {
1070 LOGINFO("TWFunc::Set_Brightness: Setting secondary brightness control to %s\n", brightness_value.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001071 TWFunc::write_to_file(secondary_brightness_file, brightness_value);
xNUTxe85f02d2014-07-18 01:30:58 +02001072 }
xNUTxe85f02d2014-07-18 01:30:58 +02001073 }
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +09001074 return result;
xNUTxe85f02d2014-07-18 01:30:58 +02001075}
1076
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001077bool TWFunc::Toggle_MTP(bool enable) {
1078#ifdef TW_HAS_MTP
1079 static int was_enabled = false;
1080
1081 if (enable && was_enabled) {
1082 if (!PartitionManager.Enable_MTP())
1083 PartitionManager.Disable_MTP();
1084 } else {
1085 was_enabled = DataManager::GetIntValue("tw_mtp_enabled");
1086 PartitionManager.Disable_MTP();
1087 usleep(500);
1088 }
1089 return was_enabled;
1090#else
1091 return false;
1092#endif
1093}
1094
Tom Hite5a926722014-09-15 01:31:03 +00001095void TWFunc::SetPerformanceMode(bool mode) {
1096 if (mode) {
1097 property_set("recovery.perf.mode", "1");
1098 } else {
1099 property_set("recovery.perf.mode", "0");
1100 }
1101 // Some time for events to catch up to init handlers
1102 usleep(500000);
1103}
1104
Jenkins1710bf22014-10-02 20:22:21 -04001105std::string TWFunc::to_string(unsigned long value) {
1106 std::ostringstream os;
1107 os << value;
1108 return os.str();
1109}
1110
Ethan Yonker9132d912015-02-02 10:32:49 -06001111void TWFunc::Disable_Stock_Recovery_Replace(void) {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001112 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001113 // Disable flashing of stock recovery
1114 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
1115 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
Ethan Yonker74db1572015-10-28 12:44:49 -05001116 gui_msg("rename_stock=Renamed stock recovery file in /system to prevent the stock ROM from replacing TWRP.");
Ethan Yonker9132d912015-02-02 10:32:49 -06001117 sync();
1118 }
Captain Throwback9d6feb52018-07-27 10:05:24 -04001119 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
Ethan Yonker9132d912015-02-02 10:32:49 -06001120 }
1121}
1122
Ethan Yonker483e9f42016-01-11 22:21:18 -06001123unsigned long long TWFunc::IOCTL_Get_Block_Size(const char* block_device) {
1124 unsigned long block_device_size;
1125 int ret = 0;
1126
1127 int fd = open(block_device, O_RDONLY);
1128 if (fd < 0) {
1129 LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", block_device, strerror(errno));
1130 } else {
1131 ret = ioctl(fd, BLKGETSIZE, &block_device_size);
1132 close(fd);
1133 if (ret) {
1134 LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno));
1135 } else {
1136 return (unsigned long long)(block_device_size) * 512LLU;
1137 }
1138 }
1139 return 0;
1140}
1141
bigbiff bigbiffbad332a2016-07-29 21:18:13 -04001142void TWFunc::copy_kernel_log(string curr_storage) {
1143 std::string dmesgDst = curr_storage + "/dmesg.log";
1144 std::string dmesgCmd = "/sbin/dmesg";
1145
1146 std::string result;
1147 Exec_Cmd(dmesgCmd, result);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001148 write_to_file(dmesgDst, result);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -04001149 gui_msg(Msg("copy_kernel_log=Copied kernel log to {1}")(dmesgDst));
1150 tw_set_default_metadata(dmesgDst.c_str());
1151}
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001152
1153bool TWFunc::isNumber(string strtocheck) {
1154 int num = 0;
1155 std::istringstream iss(strtocheck);
1156
1157 if (!(iss >> num).fail())
1158 return true;
1159 else
1160 return false;
1161}
1162
1163int TWFunc::stream_adb_backup(string &Restore_Name) {
1164 string cmd = "/sbin/bu --twrp stream " + Restore_Name;
1165 LOGINFO("stream_adb_backup: %s\n", cmd.c_str());
1166 int ret = TWFunc::Exec_Cmd(cmd);
1167 if (ret != 0)
1168 return -1;
1169 return ret;
1170}
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001171
1172std::string TWFunc::get_cache_dir() {
1173 if (PartitionManager.Find_Partition_By_Path(NON_AB_CACHE_DIR) == NULL) {
bigbiff bigbiffe4bdb152019-03-23 18:33:17 -04001174 if (PartitionManager.Find_Partition_By_Path(NON_AB_CACHE_DIR) == NULL) {
1175 if (PartitionManager.Find_Partition_By_Path(PERSIST_CACHE_DIR) == NULL) {
1176 LOGINFO("Unable to find a directory to store TWRP logs.");
1177 return "";
1178 }
1179 return PERSIST_CACHE_DIR;
1180 } else {
1181 return AB_CACHE_DIR;
1182 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001183 }
1184 else {
1185 return NON_AB_CACHE_DIR;
1186 }
1187}
1188
1189void TWFunc::check_selinux_support() {
1190 if (TWFunc::Path_Exists("/prebuilt_file_contexts")) {
1191 if (TWFunc::Path_Exists("/file_contexts")) {
1192 printf("Renaming regular /file_contexts -> /file_contexts.bak\n");
1193 rename("/file_contexts", "/file_contexts.bak");
1194 }
1195 printf("Moving /prebuilt_file_contexts -> /file_contexts\n");
1196 rename("/prebuilt_file_contexts", "/file_contexts");
1197 }
1198 struct selinux_opt selinux_options[] = {
1199 { SELABEL_OPT_PATH, "/file_contexts" }
1200 };
1201 selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
1202 if (!selinux_handle)
1203 printf("No file contexts for SELinux\n");
1204 else
1205 printf("SELinux contexts loaded from /file_contexts\n");
1206 { // Check to ensure SELinux can be supported by the kernel
1207 char *contexts = NULL;
1208 std::string cacheDir = TWFunc::get_cache_dir();
1209 std::string se_context_check = cacheDir + "recovery/";
1210 int ret = 0;
1211
1212 if (cacheDir == NON_AB_CACHE_DIR) {
1213 PartitionManager.Mount_By_Path(NON_AB_CACHE_DIR, false);
1214 }
1215 if (TWFunc::Path_Exists(se_context_check)) {
1216 ret = lgetfilecon(se_context_check.c_str(), &contexts);
Makornthawat Emeryabc299c2019-03-29 13:45:22 +00001217 if (ret < 0) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001218 LOGINFO("Could not check %s SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n", se_context_check.c_str());
1219 lgetfilecon("/sbin/teamwin", &contexts);
1220 }
1221 }
1222 if (ret < 0) {
1223 gui_warn("no_kernel_selinux=Kernel does not have support for reading SELinux contexts.");
1224 } else {
1225 free(contexts);
1226 gui_msg("full_selinux=Full SELinux support is present.");
1227 }
1228 }
1229}
Ethan Yonkeraf2897c2014-02-10 13:07:14 -06001230#endif // ndef BUILD_TWRPTAR_MAIN