blob: edf4898b1db5e6d6243ddb548f8a15b20c421288 [file] [log] [blame]
Dees_Troy38bd7602012-09-14 13:33:53 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
Dees_Troy38bd7602012-09-14 13:33:53 -04004#include <unistd.h>
5#include <vector>
6#include <dirent.h>
7#include <time.h>
Dees_Troy43d8b002012-09-17 16:00:01 -04008#include <errno.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009#include <fcntl.h>
10#include <sys/mount.h>
Dees_Troya58bead2012-09-27 09:49:29 -040011#include <sys/reboot.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050012#include <sys/sendfile.h>
13#include <sys/stat.h>
14#include <sys/vfs.h>
15#include <iostream>
16#include <fstream>
Dees_Troy38bd7602012-09-14 13:33:53 -040017#include "twrp-functions.hpp"
18#include "partitions.hpp"
19#include "common.h"
Dees_Troyb46a6842012-09-25 11:06:46 -040020#include "data.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040021#include "bootloader.h"
Dees_Troy3477d712012-09-27 15:44:01 -040022#include "variables.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040023
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024
25/* Execute a command */
26
27int TWFunc::Exec_Cmd(string cmd, string &result) {
28 FILE* exec;
29 char buffer[128];
30 int ret = 0;
31 exec = popen(cmd.c_str(), "r");
32 if (!exec) return -1;
33 while(!feof(exec)) {
34 if (fgets(buffer, 128, exec) != NULL)
35 result += buffer;
36 }
37 ret = pclose(exec);
38 return ret;
39}
40
Dees_Troy38bd7602012-09-14 13:33:53 -040041/* Checks md5 for a path
42 Return values:
43 -1 : MD5 does not exist
44 0 : Failed
45 1 : Success */
46int TWFunc::Check_MD5(string File) {
47 int ret;
48 string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK;
49 char line[255];
50 size_t pos;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050051 string result;
Dees_Troy38bd7602012-09-14 13:33:53 -040052
53 MD5_File = File + ".md5";
Dees_Troy2a923582012-09-20 12:13:34 -040054 if (Path_Exists(MD5_File)) {
Dees_Troy38bd7602012-09-14 13:33:53 -040055 DirPath = Get_Path(File);
Dees_Troy38bd7602012-09-14 13:33:53 -040056 MD5_File = Get_Filename(MD5_File);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057 chdir(DirPath.c_str());
58 Command = "/sbin/busybox md5sum -c " + MD5_File;
59 Exec_Cmd(Command, result);
60 pos = result.find(":");
Dees_Troy38bd7602012-09-14 13:33:53 -040061 if (pos != string::npos) {
62 Filename = Get_Filename(File);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050063 MD5_File_Filename = result.substr(0, pos);
64 OK = result.substr(pos + 2, result.size() - pos - 2);
Dees_Troy38bd7602012-09-14 13:33:53 -040065 if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) {
66 //MD5 is good, return 1
67 ret = 1;
68 } else {
69 // MD5 is bad, return 0
70 ret = 0;
71 }
72 } else {
73 // MD5 is bad, return 0
74 ret = 0;
75 }
Dees_Troy38bd7602012-09-14 13:33:53 -040076 } else {
77 //No md5 file, return -1
78 ret = -1;
79 }
80
81 return ret;
82}
83
84// Returns "file.name" from a full /path/to/file.name
85string TWFunc::Get_Filename(string Path) {
86 size_t pos = Path.find_last_of("/");
87 if (pos != string::npos) {
88 string Filename;
89 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
90 return Filename;
91 } else
92 return Path;
93}
94
95// Returns "/path/to/" from a full /path/to/file.name
96string TWFunc::Get_Path(string Path) {
97 size_t pos = Path.find_last_of("/");
98 if (pos != string::npos) {
99 string Pathonly;
100 Pathonly = Path.substr(0, pos + 1);
101 return Pathonly;
102 } else
103 return Path;
104}
105
106// Returns "/path" from a full /path/to/file.name
107string TWFunc::Get_Root_Path(string Path) {
108 string Local_Path = Path;
109
110 // Make sure that we have a leading slash
111 if (Local_Path.substr(0, 1) != "/")
112 Local_Path = "/" + Local_Path;
113
114 // Trim the path to get the root path only
115 size_t position = Local_Path.find("/", 2);
116 if (position != string::npos) {
117 Local_Path.resize(position);
118 }
119 return Local_Path;
120}
121
122void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 int need_libs = 0;
124
125 if (!PartitionManager.Mount_By_Path("/system", true))
126 return;
127
128 if (!PartitionManager.Mount_By_Path("/data", true))
129 return;
130
131 ui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500132 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400133 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400134 ui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500135 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400136 need_libs = 1;
137 } else
138 ui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400139 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400140 ui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500141 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400142 need_libs = 1;
143 } else
144 ui_print("dump_image is already installed, skipping...\n");
145 if (need_libs) {
146 ui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500147 copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
148 copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
149 copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
150 copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400151 }
152 ui_print("Installing HTC Dumlock app...\n");
153 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500154 unlink("/data/app/com.teamwin.htcdumlock*");
155 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400156 sync();
157 ui_print("HTC Dumlock is installed.\n");
158}
159
160void TWFunc::htc_dumlock_restore_original_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500161 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400162 if (!PartitionManager.Mount_By_Path("/sdcard", true))
163 return;
164
165 ui_print("Restoring original boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500166 Exec_Cmd("htcdumlock restore", status);
Dees_Troy38bd7602012-09-14 13:33:53 -0400167 ui_print("Original boot restored.\n");
168}
169
170void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500171 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400172 if (!PartitionManager.Mount_By_Path("/sdcard", true))
173 return;
Dees_Troy38bd7602012-09-14 13:33:53 -0400174 ui_print("Reflashing recovery to boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500175 Exec_Cmd("htcdumlock recovery noreboot", status);
Dees_Troy38bd7602012-09-14 13:33:53 -0400176 ui_print("Recovery is flashed to boot.\n");
177}
Dees_Troy43d8b002012-09-17 16:00:01 -0400178
179int TWFunc::Recursive_Mkdir(string Path) {
180 string pathCpy = Path;
181 string wholePath;
182 size_t pos = pathCpy.find("/", 2);
183
184 while (pos != string::npos)
185 {
186 wholePath = pathCpy.substr(0, pos);
187 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
188 LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
189 return false;
190 }
191
192 pos = pathCpy.find("/", pos + 1);
193 }
194 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
195 return false;
196 return true;
197}
198
199unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) {
200 DIR* d;
201 struct dirent* de;
202 struct stat st;
Dees_Troyd9a96162012-12-20 17:44:35 +0000203 char path2[4096], filename[4096];
Dees_Troy43d8b002012-09-17 16:00:01 -0400204 unsigned long long dusize = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500205 unsigned long long dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400206 // Make a copy of path in case the data in the pointer gets overwritten later
207 strcpy(path2, Path.c_str());
208
209 d = opendir(path2);
210 if (d == NULL)
211 {
212 LOGE("error opening '%s'\n", path2);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500213 LOGE("error: %s\n", strerror(errno));
Dees_Troy43d8b002012-09-17 16:00:01 -0400214 return 0;
215 }
216
217 while ((de = readdir(d)) != NULL)
218 {
219 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
220 {
221 strcpy(filename, path2);
222 strcat(filename, "/");
223 strcat(filename, de->d_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500224 dutemp = Get_Folder_Size(filename, Display_Error);
225 dusize += dutemp;
226 dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400227 }
228 else if (de->d_type == DT_REG)
229 {
230 strcpy(filename, path2);
231 strcat(filename, "/");
232 strcat(filename, de->d_name);
233 stat(filename, &st);
234 dusize += (unsigned long long)(st.st_size);
235 }
236 }
237 closedir(d);
Dees_Troy43d8b002012-09-17 16:00:01 -0400238 return dusize;
239}
240
241bool TWFunc::Path_Exists(string Path) {
242 // Check to see if the Path exists
Dees_Troy7c2dec82012-09-26 09:49:14 -0400243 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400244 if (stat(Path.c_str(), &st) != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400245 return false;
246 else
247 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400248}
249
250void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
251 string Display_Text;
252
253 DataManager::GetValue(Read_Value, Display_Text);
254 if (Display_Text.empty())
255 Display_Text = Default_Text;
256
257 DataManager::SetValue("tw_operation", Display_Text);
258 DataManager::SetValue("tw_partition", "");
259}
260
261void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
262 string Display_Text;
263
264 DataManager::GetValue(Read_Value, Display_Text);
265 if (Display_Text.empty())
266 Display_Text = Default_Text;
267
268 DataManager::SetValue("tw_operation", Display_Text);
269 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400270}
271
272unsigned long TWFunc::Get_File_Size(string Path) {
273 struct stat st;
274
275 if (stat(Path.c_str(), &st) != 0)
276 return 0;
277 return st.st_size;
Dees_Troya58bead2012-09-27 09:49:29 -0400278}
279
280static const char *COMMAND_FILE = "/cache/recovery/command";
281static const char *INTENT_FILE = "/cache/recovery/intent";
282static const char *LOG_FILE = "/cache/recovery/log";
283static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
284static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
285static const char *CACHE_ROOT = "/cache";
286static const char *SDCARD_ROOT = "/sdcard";
287static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
288static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
289
290// close a file, log an error if the error indicator is set
291void TWFunc::check_and_fclose(FILE *fp, const char *name) {
292 fflush(fp);
293 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
294 fclose(fp);
295}
296
297void TWFunc::copy_log_file(const char* source, const char* destination, int append) {
298 FILE *log = fopen_path(destination, append ? "a" : "w");
299 if (log == NULL) {
300 LOGE("Can't open %s\n", destination);
301 } else {
302 FILE *tmplog = fopen(source, "r");
303 if (tmplog != NULL) {
304 if (append) {
305 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
306 }
307 char buf[4096];
308 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
309 if (append) {
310 tmplog_offset = ftell(tmplog);
311 }
312 check_and_fclose(tmplog, source);
313 }
314 check_and_fclose(log, destination);
315 }
316}
317
318// clear the recovery command and prepare to boot a (hopefully working) system,
319// copy our log file to cache as well (for the system to read), and
320// record any intent we were asked to communicate back to the system.
321// this function is idempotent: call it as many times as you like.
322void TWFunc::twfinish_recovery(const char *send_intent) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500323 // By this point, we're ready to return to the main system...
324 if (send_intent != NULL) {
325 FILE *fp = fopen_path(INTENT_FILE, "w");
326 if (fp == NULL) {
327 LOGE("Can't open %s\n", INTENT_FILE);
328 } else {
329 fputs(send_intent, fp);
330 check_and_fclose(fp, INTENT_FILE);
331 }
332 }
Dees_Troya58bead2012-09-27 09:49:29 -0400333
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 // Copy logs to cache so the system can find out what happened.
335 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
336 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
337 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
338 chmod(LOG_FILE, 0600);
339 chown(LOG_FILE, 1000, 1000); // system user
340 chmod(LAST_LOG_FILE, 0640);
341 chmod(LAST_INSTALL_FILE, 0644);
Dees_Troya58bead2012-09-27 09:49:29 -0400342
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343 // Reset to normal system boot so recovery won't cycle indefinitely.
344 struct bootloader_message boot;
345 memset(&boot, 0, sizeof(boot));
346 set_bootloader_message(&boot);
Dees_Troya58bead2012-09-27 09:49:29 -0400347
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500348 // Remove the command file, so recovery won't repeat indefinitely.
349 if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) {
350 LOGW("Can't unlink %s\n", COMMAND_FILE);
351 }
Dees_Troya58bead2012-09-27 09:49:29 -0400352
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500353 PartitionManager.UnMount_By_Path("/cache", true);
354 sync(); // For good measure.
Dees_Troya58bead2012-09-27 09:49:29 -0400355}
356
357// reboot: Reboot the system. Return -1 on error, no return on success
358int TWFunc::tw_reboot(RebootCommand command)
359{
360 // Always force a sync before we reboot
361 sync();
362
363 switch (command)
364 {
365 case rb_current:
366 case rb_system:
367 twfinish_recovery("s");
368 sync();
369 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
370 return reboot(RB_AUTOBOOT);
371 case rb_recovery:
372 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
373 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
374 case rb_bootloader:
375 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
376 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
377 case rb_poweroff:
378 check_and_run_script("/sbin/poweroff.sh", "power off");
379 return reboot(RB_POWER_OFF);
380 case rb_download:
381 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
382 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
383 return 1;
384 default:
385 return -1;
386 }
387 return -1;
388}
389
390void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
391{
392 // Check for and run startup script if script exists
393 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500394 string result;
Dees_Troya58bead2012-09-27 09:49:29 -0400395 if (stat(script_file, &st) == 0) {
396 ui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500397 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
398 TWFunc::Exec_Cmd(script_file, result);
Dees_Troya58bead2012-09-27 09:49:29 -0400399 ui_print("\nFinished running %s script.\n", display_name);
400 }
Dees_Troy3477d712012-09-27 15:44:01 -0400401}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500402
403int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000404 DIR *d = opendir(path.c_str());
405 int r = 0;
406 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500407
Dees_Troyce675462013-01-09 19:48:21 +0000408 if (d == NULL) {
409 LOGE("Error opening '%s'\n", path.c_str());
410 return -1;
411 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500412
Dees_Troyce675462013-01-09 19:48:21 +0000413 if (d) {
414 struct dirent *p;
415 while (!r && (p = readdir(d))) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500416 LOGI("checking :%s\n", p->d_name);
Dees_Troyce675462013-01-09 19:48:21 +0000417 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
418 continue;
419 new_path = path + "/";
420 new_path.append(p->d_name);
421 if (p->d_type == DT_DIR) {
422 r = removeDir(new_path, true);
423 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500424 if (p->d_type == DT_DIR)
425 r = rmdir(new_path.c_str());
426 else
427 LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
428 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500429 } 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 +0000430 r = unlink(new_path.c_str());
431 if (!r)
432 LOGI("Unable to unlink '%s'\n", new_path.c_str());
433 }
434 }
435 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500436
437 if (!r) {
438 if (skipParent)
439 return 0;
440 else
441 r = rmdir(path.c_str());
442 }
Dees_Troyce675462013-01-09 19:48:21 +0000443 }
444 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500445}
446
447int TWFunc::copy_file(string src, string dst, int mode) {
448 LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str());
449 ifstream srcfile(src.c_str(), ios::binary);
450 ofstream dstfile(dst.c_str(), ios::binary);
451 dstfile << srcfile.rdbuf();
452 srcfile.close();
453 dstfile.close();
454 return 0;
455}