Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 18 | |
| 19 | #include <ctype.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <limits.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 30 | #include "twcommon.h" |
Dees_Troy | b9d88ac | 2012-09-14 14:34:19 -0400 | [diff] [blame] | 31 | #include "mincrypt/rsa.h" |
| 32 | #include "mincrypt/sha.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 33 | #include "minui/minui.h" |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SELINUX |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 35 | #include "minzip/SysUtil.h" |
| 36 | #include "minzip/Zip.h" |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 37 | #else |
| 38 | #include "minzipold/SysUtil.h" |
| 39 | #include "minzipold/Zip.h" |
| 40 | #endif |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 41 | #include "mtdutils/mounts.h" |
| 42 | #include "mtdutils/mtdutils.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 43 | #include "verifier.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 44 | #include "variables.h" |
| 45 | #include "data.hpp" |
| 46 | #include "partitions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 47 | #include "twrpDigest.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 48 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 49 | extern "C" { |
| 50 | #include "gui/gui.h" |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 51 | #include "legacy_property_service.h" |
| 52 | } |
| 53 | |
| 54 | static const char* properties_path = "/dev/__properties__"; |
| 55 | static const char* properties_path_renamed = "/dev/__properties_kk__"; |
| 56 | |
| 57 | static void switch_to_legacy_properties() |
| 58 | { |
| 59 | char tmp[32]; |
| 60 | int propfd, propsz; |
| 61 | legacy_properties_init(); |
| 62 | legacy_get_property_workspace(&propfd, &propsz); |
| 63 | sprintf(tmp, "%d,%d", dup(propfd), propsz); |
| 64 | setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1); |
| 65 | |
| 66 | if (TWFunc::Path_Exists(properties_path)) { |
| 67 | // hide real properties so that the updater uses the envvar to find the legacy format properties |
| 68 | if (rename(properties_path, properties_path_renamed) != 0) |
| 69 | LOGERR("Renaming properties failed: %s (assertions in old installers may fail)\n", strerror(errno)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void switch_to_new_properties() |
| 74 | { |
| 75 | if (TWFunc::Path_Exists(properties_path_renamed)) { |
| 76 | if (rename(properties_path_renamed, properties_path) != 0) |
| 77 | LOGERR("Restoring properties failed: %s\n", strerror(errno)); |
| 78 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 79 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 80 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 81 | static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) { |
| 82 | const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME); |
| 83 | string Temp_Binary = "/tmp/updater"; |
| 84 | int binary_fd, ret_val, pipe_fd[2], status, zip_verify; |
| 85 | char buffer[1024]; |
| 86 | const char** args = (const char**)malloc(sizeof(char*) * 5); |
| 87 | FILE* child_data; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 88 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 89 | if (binary_location == NULL) { |
| 90 | mzCloseZipArchive(Zip); |
| 91 | return INSTALL_CORRUPT; |
| 92 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 93 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 94 | // Delete any existing updater |
| 95 | if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) { |
| 96 | LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str()); |
| 97 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 98 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 99 | binary_fd = creat(Temp_Binary.c_str(), 0755); |
| 100 | if (binary_fd < 0) { |
| 101 | mzCloseZipArchive(Zip); |
| 102 | LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str()); |
| 103 | return INSTALL_ERROR; |
| 104 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 105 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 106 | ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd); |
| 107 | close(binary_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 108 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 109 | if (!ret_val) { |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 110 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 111 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 112 | return INSTALL_ERROR; |
| 113 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 114 | |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 115 | // If exists, extract file_contexts from the zip file |
| 116 | const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts"); |
| 117 | if (selinx_contexts == NULL) { |
| 118 | mzCloseZipArchive(Zip); |
| 119 | LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n"); |
| 120 | } else { |
| 121 | string output_filename = "/file_contexts"; |
| 122 | LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str()); |
| 123 | // Delete any file_contexts |
| 124 | if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) { |
| 125 | LOGINFO("Unable to unlink '%s'\n", output_filename.c_str()); |
| 126 | } |
| 127 | |
| 128 | int file_contexts_fd = creat(output_filename.c_str(), 0644); |
| 129 | if (file_contexts_fd < 0) { |
| 130 | mzCloseZipArchive(Zip); |
| 131 | LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str()); |
| 132 | return INSTALL_ERROR; |
| 133 | } |
| 134 | |
| 135 | ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd); |
| 136 | close(file_contexts_fd); |
| 137 | |
| 138 | if (!ret_val) { |
| 139 | mzCloseZipArchive(Zip); |
| 140 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 141 | return INSTALL_ERROR; |
| 142 | } |
| 143 | } |
| 144 | mzCloseZipArchive(Zip); |
| 145 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 146 | pipe(pipe_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 147 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 148 | args[0] = Temp_Binary.c_str(); |
| 149 | args[1] = EXPAND(RECOVERY_API_VERSION); |
| 150 | char* temp = (char*)malloc(10); |
| 151 | sprintf(temp, "%d", pipe_fd[1]); |
| 152 | args[2] = temp; |
| 153 | args[3] = (char*)path; |
| 154 | args[4] = NULL; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 155 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 156 | pid_t pid = fork(); |
| 157 | if (pid == 0) { |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 158 | switch_to_legacy_properties(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 159 | close(pipe_fd[0]); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 160 | execve(Temp_Binary.c_str(), (char* const*)args, environ); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 161 | printf("E:Can't execute '%s'\n", Temp_Binary.c_str()); |
| 162 | _exit(-1); |
| 163 | } |
| 164 | close(pipe_fd[1]); |
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 | *wipe_cache = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 167 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 168 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
| 169 | child_data = fdopen(pipe_fd[0], "r"); |
| 170 | while (fgets(buffer, sizeof(buffer), child_data) != NULL) { |
| 171 | char* command = strtok(buffer, " \n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 172 | if (command == NULL) { |
| 173 | continue; |
| 174 | } else if (strcmp(command, "progress") == 0) { |
| 175 | char* fraction_char = strtok(NULL, " \n"); |
| 176 | char* seconds_char = strtok(NULL, " \n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 177 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 178 | float fraction_float = strtof(fraction_char, NULL); |
| 179 | int seconds_float = strtol(seconds_char, NULL, 10); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 180 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | if (zip_verify) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 182 | DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float); |
| 183 | else |
| 184 | DataManager::ShowProgress(fraction_float, seconds_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 185 | } else if (strcmp(command, "set_progress") == 0) { |
| 186 | char* fraction_char = strtok(NULL, " \n"); |
| 187 | float fraction_float = strtof(fraction_char, NULL); |
| 188 | DataManager::SetProgress(fraction_float); |
| 189 | } else if (strcmp(command, "ui_print") == 0) { |
| 190 | char* display_value = strtok(NULL, "\n"); |
| 191 | if (display_value) { |
| 192 | gui_print("%s", display_value); |
| 193 | } else { |
| 194 | gui_print("\n"); |
| 195 | } |
| 196 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 197 | *wipe_cache = 1; |
| 198 | } else if (strcmp(command, "clear_display") == 0) { |
| 199 | // Do nothing, not supported by TWRP |
| 200 | } else { |
| 201 | LOGERR("unknown command [%s]\n", command); |
| 202 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 203 | } |
| 204 | fclose(child_data); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 205 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 206 | waitpid(pid, &status, 0); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 207 | switch_to_new_properties(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 208 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 209 | LOGERR("Error executing updater binary in zip '%s'\n", path); |
| 210 | return INSTALL_ERROR; |
| 211 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 212 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 213 | return INSTALL_SUCCESS; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 216 | extern "C" int TWinstall_zip(const char* path, int* wipe_cache) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 217 | int ret_val, zip_verify, md5_return, key_count; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 218 | twrpDigest md5sum; |
| 219 | string strpath = path; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 220 | ZipArchive Zip; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 221 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 222 | gui_print("Installing '%s'...\nChecking for MD5 file...\n", path); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 223 | md5sum.setfn(strpath); |
| 224 | md5_return = md5sum.verify_md5digest(); |
| 225 | if (md5_return == -2) { |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 226 | // MD5 did not match. |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 227 | LOGERR("Zip MD5 does not match.\nUnable to install zip.\n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 228 | return INSTALL_CORRUPT; |
| 229 | } else if (md5_return == -1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 230 | gui_print("Skipping MD5 check: no MD5 file found.\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 231 | } else if (md5_return == 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 232 | gui_print("Zip MD5 matched.\n"); // MD5 found and matched. |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 233 | |
| 234 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 235 | DataManager::SetProgress(0); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 236 | if (zip_verify) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 237 | gui_print("Verifying zip signature...\n"); |
| 238 | ret_val = verify_file(path); |
| 239 | if (ret_val != VERIFY_SUCCESS) { |
| 240 | LOGERR("Zip signature verification failed: %i\n", ret_val); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 244 | ret_val = mzOpenZipArchive(path, &Zip); |
| 245 | if (ret_val != 0) { |
| 246 | LOGERR("Zip file is corrupt!\n", path); |
| 247 | return INSTALL_CORRUPT; |
| 248 | } |
| 249 | return Run_Update_Binary(path, &Zip, wipe_cache); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 250 | } |