blob: ccf0540a13e408028a37872e14f2e791ebdb46b7 [file] [log] [blame]
Dees_Troy38bd7602012-09-14 13:33:53 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/stat.h>
5#include <sys/vfs.h>
6#include <unistd.h>
7#include <vector>
8#include <dirent.h>
9#include <time.h>
10
11#include "twrp-functions.hpp"
12#include "partitions.hpp"
13#include "common.h"
14extern "C" {
15 #include "extra-functions.h"
16 int __system(const char *command);
17}
18
19/* Checks md5 for a path
20 Return values:
21 -1 : MD5 does not exist
22 0 : Failed
23 1 : Success */
24int TWFunc::Check_MD5(string File) {
25 int ret;
26 string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK;
27 char line[255];
28 size_t pos;
29
30 MD5_File = File + ".md5";
31 if (access(MD5_File.c_str(), F_OK ) != -1) {
32 DirPath = Get_Path(File);
33 chdir(DirPath.c_str());
34 MD5_File = Get_Filename(MD5_File);
35 Command = "/sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output";
36 __system(Command.c_str());
37 FILE * cs = fopen("/tmp/md5output", "r");
38 if (cs == NULL) {
39 LOGE("Unable to open md5 output file.\n");
40 return 0;
41 }
42
43 fgets(line, sizeof(line), cs);
44
45 Sline = line;
46 pos = Sline.find(":");
47 if (pos != string::npos) {
48 Filename = Get_Filename(File);
49 MD5_File_Filename = Sline.substr(0, pos);
50 OK = Sline.substr(pos + 2, Sline.size() - pos - 2);
51 if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) {
52 //MD5 is good, return 1
53 ret = 1;
54 } else {
55 // MD5 is bad, return 0
56 ret = 0;
57 }
58 } else {
59 // MD5 is bad, return 0
60 ret = 0;
61 }
62 fclose(cs);
63 } else {
64 //No md5 file, return -1
65 ret = -1;
66 }
67
68 return ret;
69}
70
71// Returns "file.name" from a full /path/to/file.name
72string TWFunc::Get_Filename(string Path) {
73 size_t pos = Path.find_last_of("/");
74 if (pos != string::npos) {
75 string Filename;
76 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
77 return Filename;
78 } else
79 return Path;
80}
81
82// Returns "/path/to/" from a full /path/to/file.name
83string TWFunc::Get_Path(string Path) {
84 size_t pos = Path.find_last_of("/");
85 if (pos != string::npos) {
86 string Pathonly;
87 Pathonly = Path.substr(0, pos + 1);
88 return Pathonly;
89 } else
90 return Path;
91}
92
93// Returns "/path" from a full /path/to/file.name
94string TWFunc::Get_Root_Path(string Path) {
95 string Local_Path = Path;
96
97 // Make sure that we have a leading slash
98 if (Local_Path.substr(0, 1) != "/")
99 Local_Path = "/" + Local_Path;
100
101 // Trim the path to get the root path only
102 size_t position = Local_Path.find("/", 2);
103 if (position != string::npos) {
104 Local_Path.resize(position);
105 }
106 return Local_Path;
107}
108
109void TWFunc::install_htc_dumlock(void) {
110 struct statfs fs1, fs2;
111 int need_libs = 0;
112
113 if (!PartitionManager.Mount_By_Path("/system", true))
114 return;
115
116 if (!PartitionManager.Mount_By_Path("/data", true))
117 return;
118
119 ui_print("Installing HTC Dumlock to system...\n");
120 __system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock");
121 if (statfs("/system/bin/flash_image", &fs1) != 0) {
122 ui_print("Installing flash_image...\n");
123 __system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image");
124 need_libs = 1;
125 } else
126 ui_print("flash_image is already installed, skipping...\n");
127 if (statfs("/system/bin/dump_image", &fs2) != 0) {
128 ui_print("Installing dump_image...\n");
129 __system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image");
130 need_libs = 1;
131 } else
132 ui_print("dump_image is already installed, skipping...\n");
133 if (need_libs) {
134 ui_print("Installing libs needed for flash_image and dump_image...\n");
135 __system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so");
136 __system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so");
137 __system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so");
138 __system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so");
139 }
140 ui_print("Installing HTC Dumlock app...\n");
141 mkdir("/data/app", 0777);
142 __system("rm /data/app/com.teamwin.htcdumlock*");
143 __system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk");
144 sync();
145 ui_print("HTC Dumlock is installed.\n");
146}
147
148void TWFunc::htc_dumlock_restore_original_boot(void) {
149 if (!PartitionManager.Mount_By_Path("/sdcard", true))
150 return;
151
152 ui_print("Restoring original boot...\n");
153 __system("htcdumlock restore");
154 ui_print("Original boot restored.\n");
155}
156
157void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
158 if (!PartitionManager.Mount_By_Path("/sdcard", true))
159 return;
160
161 ui_print("Reflashing recovery to boot...\n");
162 __system("htcdumlock recovery noreboot");
163 ui_print("Recovery is flashed to boot.\n");
164}