Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1 | #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> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 10 | #include <errno.h> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 11 | |
| 12 | #include "twrp-functions.hpp" |
| 13 | #include "partitions.hpp" |
| 14 | #include "common.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 15 | #include "data.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 16 | |
| 17 | /* Checks md5 for a path |
| 18 | Return values: |
| 19 | -1 : MD5 does not exist |
| 20 | 0 : Failed |
| 21 | 1 : Success */ |
| 22 | int TWFunc::Check_MD5(string File) { |
| 23 | int ret; |
| 24 | string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK; |
| 25 | char line[255]; |
| 26 | size_t pos; |
| 27 | |
| 28 | MD5_File = File + ".md5"; |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 29 | if (Path_Exists(MD5_File)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 30 | DirPath = Get_Path(File); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 31 | MD5_File = Get_Filename(MD5_File); |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 32 | Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output"; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 33 | system(Command.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 34 | FILE * cs = fopen("/tmp/md5output", "r"); |
| 35 | if (cs == NULL) { |
| 36 | LOGE("Unable to open md5 output file.\n"); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | fgets(line, sizeof(line), cs); |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 41 | fclose(cs); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 42 | |
| 43 | Sline = line; |
| 44 | pos = Sline.find(":"); |
| 45 | if (pos != string::npos) { |
| 46 | Filename = Get_Filename(File); |
| 47 | MD5_File_Filename = Sline.substr(0, pos); |
| 48 | OK = Sline.substr(pos + 2, Sline.size() - pos - 2); |
| 49 | if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) { |
| 50 | //MD5 is good, return 1 |
| 51 | ret = 1; |
| 52 | } else { |
| 53 | // MD5 is bad, return 0 |
| 54 | ret = 0; |
| 55 | } |
| 56 | } else { |
| 57 | // MD5 is bad, return 0 |
| 58 | ret = 0; |
| 59 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 60 | } else { |
| 61 | //No md5 file, return -1 |
| 62 | ret = -1; |
| 63 | } |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | // Returns "file.name" from a full /path/to/file.name |
| 69 | string TWFunc::Get_Filename(string Path) { |
| 70 | size_t pos = Path.find_last_of("/"); |
| 71 | if (pos != string::npos) { |
| 72 | string Filename; |
| 73 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 74 | return Filename; |
| 75 | } else |
| 76 | return Path; |
| 77 | } |
| 78 | |
| 79 | // Returns "/path/to/" from a full /path/to/file.name |
| 80 | string TWFunc::Get_Path(string Path) { |
| 81 | size_t pos = Path.find_last_of("/"); |
| 82 | if (pos != string::npos) { |
| 83 | string Pathonly; |
| 84 | Pathonly = Path.substr(0, pos + 1); |
| 85 | return Pathonly; |
| 86 | } else |
| 87 | return Path; |
| 88 | } |
| 89 | |
| 90 | // Returns "/path" from a full /path/to/file.name |
| 91 | string TWFunc::Get_Root_Path(string Path) { |
| 92 | string Local_Path = Path; |
| 93 | |
| 94 | // Make sure that we have a leading slash |
| 95 | if (Local_Path.substr(0, 1) != "/") |
| 96 | Local_Path = "/" + Local_Path; |
| 97 | |
| 98 | // Trim the path to get the root path only |
| 99 | size_t position = Local_Path.find("/", 2); |
| 100 | if (position != string::npos) { |
| 101 | Local_Path.resize(position); |
| 102 | } |
| 103 | return Local_Path; |
| 104 | } |
| 105 | |
| 106 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 107 | int need_libs = 0; |
| 108 | |
| 109 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 110 | return; |
| 111 | |
| 112 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 113 | return; |
| 114 | |
| 115 | ui_print("Installing HTC Dumlock to system...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 116 | system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock"); |
| 117 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 118 | ui_print("Installing flash_image...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 119 | system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 120 | need_libs = 1; |
| 121 | } else |
| 122 | ui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 123 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 124 | ui_print("Installing dump_image...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 125 | system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 126 | need_libs = 1; |
| 127 | } else |
| 128 | ui_print("dump_image is already installed, skipping...\n"); |
| 129 | if (need_libs) { |
| 130 | ui_print("Installing libs needed for flash_image and dump_image...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 131 | system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so"); |
| 132 | system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so"); |
| 133 | system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so"); |
| 134 | system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 135 | } |
| 136 | ui_print("Installing HTC Dumlock app...\n"); |
| 137 | mkdir("/data/app", 0777); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 138 | system("rm /data/app/com.teamwin.htcdumlock*"); |
| 139 | system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 140 | sync(); |
| 141 | ui_print("HTC Dumlock is installed.\n"); |
| 142 | } |
| 143 | |
| 144 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 145 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 146 | return; |
| 147 | |
| 148 | ui_print("Restoring original boot...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 149 | system("htcdumlock restore"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 150 | ui_print("Original boot restored.\n"); |
| 151 | } |
| 152 | |
| 153 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 154 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 155 | return; |
| 156 | |
| 157 | ui_print("Reflashing recovery to boot...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 158 | system("htcdumlock recovery noreboot"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 159 | ui_print("Recovery is flashed to boot.\n"); |
| 160 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 161 | |
| 162 | int TWFunc::Recursive_Mkdir(string Path) { |
| 163 | string pathCpy = Path; |
| 164 | string wholePath; |
| 165 | size_t pos = pathCpy.find("/", 2); |
| 166 | |
| 167 | while (pos != string::npos) |
| 168 | { |
| 169 | wholePath = pathCpy.substr(0, pos); |
| 170 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) { |
| 171 | LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | pos = pathCpy.find("/", pos + 1); |
| 176 | } |
| 177 | if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) |
| 178 | return false; |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { |
| 183 | DIR* d; |
| 184 | struct dirent* de; |
| 185 | struct stat st; |
| 186 | char path2[1024], filename[1024]; |
| 187 | unsigned long long dusize = 0; |
| 188 | |
| 189 | // Make a copy of path in case the data in the pointer gets overwritten later |
| 190 | strcpy(path2, Path.c_str()); |
| 191 | |
| 192 | d = opendir(path2); |
| 193 | if (d == NULL) |
| 194 | { |
| 195 | LOGE("error opening '%s'\n", path2); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | while ((de = readdir(d)) != NULL) |
| 200 | { |
| 201 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 202 | { |
| 203 | strcpy(filename, path2); |
| 204 | strcat(filename, "/"); |
| 205 | strcat(filename, de->d_name); |
| 206 | dusize += Get_Folder_Size(filename, Display_Error); |
| 207 | } |
| 208 | else if (de->d_type == DT_REG) |
| 209 | { |
| 210 | strcpy(filename, path2); |
| 211 | strcat(filename, "/"); |
| 212 | strcat(filename, de->d_name); |
| 213 | stat(filename, &st); |
| 214 | dusize += (unsigned long long)(st.st_size); |
| 215 | } |
| 216 | } |
| 217 | closedir(d); |
| 218 | |
| 219 | return dusize; |
| 220 | } |
| 221 | |
| 222 | bool TWFunc::Path_Exists(string Path) { |
| 223 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 224 | struct stat st; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 225 | |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 226 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 227 | return false; |
| 228 | else |
| 229 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 233 | string Display_Text; |
| 234 | |
| 235 | DataManager::GetValue(Read_Value, Display_Text); |
| 236 | if (Display_Text.empty()) |
| 237 | Display_Text = Default_Text; |
| 238 | |
| 239 | DataManager::SetValue("tw_operation", Display_Text); |
| 240 | DataManager::SetValue("tw_partition", ""); |
| 241 | } |
| 242 | |
| 243 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 244 | string Display_Text; |
| 245 | |
| 246 | DataManager::GetValue(Read_Value, Display_Text); |
| 247 | if (Display_Text.empty()) |
| 248 | Display_Text = Default_Text; |
| 249 | |
| 250 | DataManager::SetValue("tw_operation", Display_Text); |
| 251 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | unsigned long TWFunc::Get_File_Size(string Path) { |
| 255 | struct stat st; |
| 256 | |
| 257 | if (stat(Path.c_str(), &st) != 0) |
| 258 | return 0; |
| 259 | return st.st_size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 260 | } |