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