Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 1 | |
| 2 | #include <ctype.h> |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <limits.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/wait.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <stdio.h> |
| 12 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 13 | #include "twcommon.h" |
Dees_Troy | b9d88ac | 2012-09-14 14:34:19 -0400 | [diff] [blame] | 14 | #include "mincrypt/rsa.h" |
| 15 | #include "mincrypt/sha.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 16 | #include "minui/minui.h" |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 17 | #ifdef HAVE_SELINUX |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 18 | #include "minzip/SysUtil.h" |
| 19 | #include "minzip/Zip.h" |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 20 | #else |
| 21 | #include "minzipold/SysUtil.h" |
| 22 | #include "minzipold/Zip.h" |
| 23 | #endif |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 24 | #include "mtdutils/mounts.h" |
| 25 | #include "mtdutils/mtdutils.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 26 | #include "verifier.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 27 | #include "variables.h" |
| 28 | #include "data.hpp" |
| 29 | #include "partitions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 30 | #include "twrpDigest.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 31 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 32 | extern "C" { |
| 33 | #include "gui/gui.h" |
| 34 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 35 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 36 | static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) { |
| 37 | const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME); |
| 38 | string Temp_Binary = "/tmp/updater"; |
| 39 | int binary_fd, ret_val, pipe_fd[2], status, zip_verify; |
| 40 | char buffer[1024]; |
| 41 | const char** args = (const char**)malloc(sizeof(char*) * 5); |
| 42 | FILE* child_data; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 43 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 44 | if (binary_location == NULL) { |
| 45 | mzCloseZipArchive(Zip); |
| 46 | return INSTALL_CORRUPT; |
| 47 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 48 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 49 | // Delete any existing updater |
| 50 | if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) { |
| 51 | LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str()); |
| 52 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 53 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 54 | binary_fd = creat(Temp_Binary.c_str(), 0755); |
| 55 | if (binary_fd < 0) { |
| 56 | mzCloseZipArchive(Zip); |
| 57 | LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str()); |
| 58 | return INSTALL_ERROR; |
| 59 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 60 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 61 | ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd); |
| 62 | close(binary_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 63 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 64 | if (!ret_val) { |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 65 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 66 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 67 | return INSTALL_ERROR; |
| 68 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 69 | |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 70 | // If exists, extract file_contexts from the zip file |
| 71 | const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts"); |
| 72 | if (selinx_contexts == NULL) { |
| 73 | mzCloseZipArchive(Zip); |
| 74 | LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n"); |
| 75 | } else { |
| 76 | string output_filename = "/file_contexts"; |
| 77 | LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str()); |
| 78 | // Delete any file_contexts |
| 79 | if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) { |
| 80 | LOGINFO("Unable to unlink '%s'\n", output_filename.c_str()); |
| 81 | } |
| 82 | |
| 83 | int file_contexts_fd = creat(output_filename.c_str(), 0644); |
| 84 | if (file_contexts_fd < 0) { |
| 85 | mzCloseZipArchive(Zip); |
| 86 | LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str()); |
| 87 | return INSTALL_ERROR; |
| 88 | } |
| 89 | |
| 90 | ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd); |
| 91 | close(file_contexts_fd); |
| 92 | |
| 93 | if (!ret_val) { |
| 94 | mzCloseZipArchive(Zip); |
| 95 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 96 | return INSTALL_ERROR; |
| 97 | } |
| 98 | } |
| 99 | mzCloseZipArchive(Zip); |
| 100 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 101 | pipe(pipe_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 102 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 103 | args[0] = Temp_Binary.c_str(); |
| 104 | args[1] = EXPAND(RECOVERY_API_VERSION); |
| 105 | char* temp = (char*)malloc(10); |
| 106 | sprintf(temp, "%d", pipe_fd[1]); |
| 107 | args[2] = temp; |
| 108 | args[3] = (char*)path; |
| 109 | args[4] = NULL; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 110 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 111 | pid_t pid = fork(); |
| 112 | if (pid == 0) { |
| 113 | close(pipe_fd[0]); |
| 114 | execv(Temp_Binary.c_str(), (char* const*)args); |
| 115 | printf("E:Can't execute '%s'\n", Temp_Binary.c_str()); |
| 116 | _exit(-1); |
| 117 | } |
| 118 | close(pipe_fd[1]); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 119 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 120 | *wipe_cache = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 121 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 122 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
| 123 | child_data = fdopen(pipe_fd[0], "r"); |
| 124 | while (fgets(buffer, sizeof(buffer), child_data) != NULL) { |
| 125 | char* command = strtok(buffer, " \n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 126 | if (command == NULL) { |
| 127 | continue; |
| 128 | } else if (strcmp(command, "progress") == 0) { |
| 129 | char* fraction_char = strtok(NULL, " \n"); |
| 130 | char* seconds_char = strtok(NULL, " \n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | float fraction_float = strtof(fraction_char, NULL); |
| 133 | int seconds_float = strtol(seconds_char, NULL, 10); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 134 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 135 | if (zip_verify) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 136 | DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float); |
| 137 | else |
| 138 | DataManager::ShowProgress(fraction_float, seconds_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | } else if (strcmp(command, "set_progress") == 0) { |
| 140 | char* fraction_char = strtok(NULL, " \n"); |
| 141 | float fraction_float = strtof(fraction_char, NULL); |
| 142 | DataManager::SetProgress(fraction_float); |
| 143 | } else if (strcmp(command, "ui_print") == 0) { |
| 144 | char* display_value = strtok(NULL, "\n"); |
| 145 | if (display_value) { |
| 146 | gui_print("%s", display_value); |
| 147 | } else { |
| 148 | gui_print("\n"); |
| 149 | } |
| 150 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 151 | *wipe_cache = 1; |
| 152 | } else if (strcmp(command, "clear_display") == 0) { |
| 153 | // Do nothing, not supported by TWRP |
| 154 | } else { |
| 155 | LOGERR("unknown command [%s]\n", command); |
| 156 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 157 | } |
| 158 | fclose(child_data); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 159 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | waitpid(pid, &status, 0); |
| 161 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 162 | LOGERR("Error executing updater binary in zip '%s'\n", path); |
| 163 | return INSTALL_ERROR; |
| 164 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 165 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 166 | return INSTALL_SUCCESS; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 169 | extern "C" int TWinstall_zip(const char* path, int* wipe_cache) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 170 | int ret_val, zip_verify, md5_return, key_count; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 171 | twrpDigest md5sum; |
| 172 | string strpath = path; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 173 | ZipArchive Zip; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 174 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 175 | gui_print("Installing '%s'...\nChecking for MD5 file...\n", path); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 176 | md5sum.setfn(strpath); |
| 177 | md5_return = md5sum.verify_md5digest(); |
| 178 | if (md5_return == -2) { |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 179 | // MD5 did not match. |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 180 | LOGERR("Zip MD5 does not match.\nUnable to install zip.\n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 181 | return INSTALL_CORRUPT; |
| 182 | } else if (md5_return == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 183 | gui_print("Skipping MD5 check: no MD5 file found.\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 184 | } else if (md5_return == 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 185 | gui_print("Zip MD5 matched.\n"); // MD5 found and matched. |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 186 | |
| 187 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 188 | DataManager::SetProgress(0); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 189 | if (zip_verify) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 190 | gui_print("Verifying zip signature...\n"); |
| 191 | ret_val = verify_file(path); |
| 192 | if (ret_val != VERIFY_SUCCESS) { |
| 193 | LOGERR("Zip signature verification failed: %i\n", ret_val); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 197 | ret_val = mzOpenZipArchive(path, &Zip); |
| 198 | if (ret_val != 0) { |
| 199 | LOGERR("Zip file is corrupt!\n", path); |
| 200 | return INSTALL_CORRUPT; |
| 201 | } |
| 202 | return Run_Update_Binary(path, &Zip, wipe_cache); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 203 | } |