1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include <vector>
#include <dirent.h>
#include <time.h>
#include <errno.h>
#include <sys/reboot.h>
#include "twrp-functions.hpp"
#include "partitions.hpp"
#include "common.h"
#include "data.hpp"
#include "bootloader.h"
#include "variables.h"
/* Checks md5 for a path
Return values:
-1 : MD5 does not exist
0 : Failed
1 : Success */
int TWFunc::Check_MD5(string File) {
int ret;
string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK;
char line[255];
size_t pos;
MD5_File = File + ".md5";
if (Path_Exists(MD5_File)) {
DirPath = Get_Path(File);
MD5_File = Get_Filename(MD5_File);
Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output";
system(Command.c_str());
FILE * cs = fopen("/tmp/md5output", "r");
if (cs == NULL) {
LOGE("Unable to open md5 output file.\n");
return 0;
}
fgets(line, sizeof(line), cs);
fclose(cs);
Sline = line;
pos = Sline.find(":");
if (pos != string::npos) {
Filename = Get_Filename(File);
MD5_File_Filename = Sline.substr(0, pos);
OK = Sline.substr(pos + 2, Sline.size() - pos - 2);
if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) {
//MD5 is good, return 1
ret = 1;
} else {
// MD5 is bad, return 0
ret = 0;
}
} else {
// MD5 is bad, return 0
ret = 0;
}
} else {
//No md5 file, return -1
ret = -1;
}
return ret;
}
// Returns "file.name" from a full /path/to/file.name
string TWFunc::Get_Filename(string Path) {
size_t pos = Path.find_last_of("/");
if (pos != string::npos) {
string Filename;
Filename = Path.substr(pos + 1, Path.size() - pos - 1);
return Filename;
} else
return Path;
}
// Returns "/path/to/" from a full /path/to/file.name
string TWFunc::Get_Path(string Path) {
size_t pos = Path.find_last_of("/");
if (pos != string::npos) {
string Pathonly;
Pathonly = Path.substr(0, pos + 1);
return Pathonly;
} else
return Path;
}
// Returns "/path" from a full /path/to/file.name
string TWFunc::Get_Root_Path(string Path) {
string Local_Path = Path;
// Make sure that we have a leading slash
if (Local_Path.substr(0, 1) != "/")
Local_Path = "/" + Local_Path;
// Trim the path to get the root path only
size_t position = Local_Path.find("/", 2);
if (position != string::npos) {
Local_Path.resize(position);
}
return Local_Path;
}
void TWFunc::install_htc_dumlock(void) {
int need_libs = 0;
if (!PartitionManager.Mount_By_Path("/system", true))
return;
if (!PartitionManager.Mount_By_Path("/data", true))
return;
ui_print("Installing HTC Dumlock to system...\n");
system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock");
if (!Path_Exists("/system/bin/flash_image")) {
ui_print("Installing flash_image...\n");
system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image");
need_libs = 1;
} else
ui_print("flash_image is already installed, skipping...\n");
if (!Path_Exists("/system/bin/dump_image")) {
ui_print("Installing dump_image...\n");
system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image");
need_libs = 1;
} else
ui_print("dump_image is already installed, skipping...\n");
if (need_libs) {
ui_print("Installing libs needed for flash_image and dump_image...\n");
system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so");
system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so");
system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so");
system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so");
}
ui_print("Installing HTC Dumlock app...\n");
mkdir("/data/app", 0777);
system("rm /data/app/com.teamwin.htcdumlock*");
system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk");
sync();
ui_print("HTC Dumlock is installed.\n");
}
void TWFunc::htc_dumlock_restore_original_boot(void) {
if (!PartitionManager.Mount_By_Path("/sdcard", true))
return;
ui_print("Restoring original boot...\n");
system("htcdumlock restore");
ui_print("Original boot restored.\n");
}
void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
if (!PartitionManager.Mount_By_Path("/sdcard", true))
return;
ui_print("Reflashing recovery to boot...\n");
system("htcdumlock recovery noreboot");
ui_print("Recovery is flashed to boot.\n");
}
int TWFunc::Recursive_Mkdir(string Path) {
string pathCpy = Path;
string wholePath;
size_t pos = pathCpy.find("/", 2);
while (pos != string::npos)
{
wholePath = pathCpy.substr(0, pos);
if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
return false;
}
pos = pathCpy.find("/", pos + 1);
}
if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
return false;
return true;
}
unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) {
DIR* d;
struct dirent* de;
struct stat st;
char path2[1024], filename[1024];
unsigned long long dusize = 0;
// Make a copy of path in case the data in the pointer gets overwritten later
strcpy(path2, Path.c_str());
d = opendir(path2);
if (d == NULL)
{
LOGE("error opening '%s'\n", path2);
return 0;
}
while ((de = readdir(d)) != NULL)
{
if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
{
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
dusize += Get_Folder_Size(filename, Display_Error);
}
else if (de->d_type == DT_REG)
{
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
stat(filename, &st);
dusize += (unsigned long long)(st.st_size);
}
}
closedir(d);
return dusize;
}
bool TWFunc::Path_Exists(string Path) {
// Check to see if the Path exists
struct stat st;
if (stat(Path.c_str(), &st) != 0)
return false;
else
return true;
}
void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
string Display_Text;
DataManager::GetValue(Read_Value, Display_Text);
if (Display_Text.empty())
Display_Text = Default_Text;
DataManager::SetValue("tw_operation", Display_Text);
DataManager::SetValue("tw_partition", "");
}
void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
string Display_Text;
DataManager::GetValue(Read_Value, Display_Text);
if (Display_Text.empty())
Display_Text = Default_Text;
DataManager::SetValue("tw_operation", Display_Text);
DataManager::SetValue("tw_partition", Partition_Name);
}
unsigned long TWFunc::Get_File_Size(string Path) {
struct stat st;
if (stat(Path.c_str(), &st) != 0)
return 0;
return st.st_size;
}
static const char *COMMAND_FILE = "/cache/recovery/command";
static const char *INTENT_FILE = "/cache/recovery/intent";
static const char *LOG_FILE = "/cache/recovery/log";
static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
static const char *CACHE_ROOT = "/cache";
static const char *SDCARD_ROOT = "/sdcard";
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
// close a file, log an error if the error indicator is set
void TWFunc::check_and_fclose(FILE *fp, const char *name) {
fflush(fp);
if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
fclose(fp);
}
void TWFunc::copy_log_file(const char* source, const char* destination, int append) {
FILE *log = fopen_path(destination, append ? "a" : "w");
if (log == NULL) {
LOGE("Can't open %s\n", destination);
} else {
FILE *tmplog = fopen(source, "r");
if (tmplog != NULL) {
if (append) {
fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
}
char buf[4096];
while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
if (append) {
tmplog_offset = ftell(tmplog);
}
check_and_fclose(tmplog, source);
}
check_and_fclose(log, destination);
}
}
// clear the recovery command and prepare to boot a (hopefully working) system,
// copy our log file to cache as well (for the system to read), and
// record any intent we were asked to communicate back to the system.
// this function is idempotent: call it as many times as you like.
void TWFunc::twfinish_recovery(const char *send_intent) {
// By this point, we're ready to return to the main system...
if (send_intent != NULL) {
FILE *fp = fopen_path(INTENT_FILE, "w");
if (fp == NULL) {
LOGE("Can't open %s\n", INTENT_FILE);
} else {
fputs(send_intent, fp);
check_and_fclose(fp, INTENT_FILE);
}
}
// Copy logs to cache so the system can find out what happened.
copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
chmod(LOG_FILE, 0600);
chown(LOG_FILE, 1000, 1000); // system user
chmod(LAST_LOG_FILE, 0640);
chmod(LAST_INSTALL_FILE, 0644);
// Reset to normal system boot so recovery won't cycle indefinitely.
struct bootloader_message boot;
memset(&boot, 0, sizeof(boot));
set_bootloader_message(&boot);
// Remove the command file, so recovery won't repeat indefinitely.
if (system("mount /cache") != 0 ||
(unlink(COMMAND_FILE) && errno != ENOENT)) {
LOGW("Can't unlink %s\n", COMMAND_FILE);
}
system("umount /cache");
sync(); // For good measure.
}
// reboot: Reboot the system. Return -1 on error, no return on success
int TWFunc::tw_reboot(RebootCommand command)
{
// Always force a sync before we reboot
sync();
switch (command)
{
case rb_current:
case rb_system:
twfinish_recovery("s");
sync();
check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
return reboot(RB_AUTOBOOT);
case rb_recovery:
check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
case rb_bootloader:
check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
case rb_poweroff:
check_and_run_script("/sbin/poweroff.sh", "power off");
return reboot(RB_POWER_OFF);
case rb_download:
check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
return 1;
default:
return -1;
}
return -1;
}
void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
{
// Check for and run startup script if script exists
struct stat st;
if (stat(script_file, &st) == 0) {
ui_print("Running %s script...\n", display_name);
char command[255];
strcpy(command, "chmod 755 ");
strcat(command, script_file);
system(command);
system(script_file);
ui_print("\nFinished running %s script.\n", display_name);
}
}
void TWFunc::Output_Version(void) {
string Path, Command;
char version[255];
Path = DataManager::GetSettingsStoragePath();
if (!PartitionManager.Mount_By_Path(Path, false)) {
LOGI("Unable to mount '%s' to write version number.\n", Path.c_str());
return;
}
Path += "/TWRP/.version";
if (Path_Exists(Path)) {
Command = "rm -f " + Path;
system(Command.c_str());
}
FILE *fp = fopen(Path.c_str(), "w");
if (fp == NULL) {
LOGE("Unable to open '%s'.\n", Path.c_str());
return;
}
strcpy(version, TW_VERSION_STR);
fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
fclose(fp);
sync();
}
|