Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 2 | Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 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" |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 33 | #include "mtdutils/mounts.h" |
| 34 | #include "mtdutils/mtdutils.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 35 | #include "minzip/SysUtil.h" |
| 36 | #include "minzip/Zip.h" |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 37 | #include "verifier.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 38 | #include "variables.h" |
| 39 | #include "data.hpp" |
| 40 | #include "partitions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 41 | #include "twrpDigest.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 42 | #include "twrp-functions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 43 | #include "gui/gui.hpp" |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 44 | #include "gui/pages.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 45 | extern "C" { |
| 46 | #include "gui/gui.h" |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 47 | #include "legacy_property_service.h" |
| 48 | } |
| 49 | |
| 50 | static const char* properties_path = "/dev/__properties__"; |
| 51 | static const char* properties_path_renamed = "/dev/__properties_kk__"; |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 52 | static bool legacy_props_env_initd = false; |
| 53 | static bool legacy_props_path_modified = false; |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 54 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 55 | // to support pre-KitKat update-binaries that expect properties in the legacy format |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 56 | static int switch_to_legacy_properties() |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 57 | { |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 58 | if (!legacy_props_env_initd) { |
| 59 | if (legacy_properties_init() != 0) |
| 60 | return -1; |
| 61 | |
| 62 | char tmp[32]; |
| 63 | int propfd, propsz; |
| 64 | legacy_get_property_workspace(&propfd, &propsz); |
| 65 | sprintf(tmp, "%d,%d", dup(propfd), propsz); |
| 66 | setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1); |
| 67 | legacy_props_env_initd = true; |
| 68 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 69 | |
| 70 | if (TWFunc::Path_Exists(properties_path)) { |
| 71 | // hide real properties so that the updater uses the envvar to find the legacy format properties |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 72 | if (rename(properties_path, properties_path_renamed) != 0) { |
| 73 | LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno)); |
| 74 | return -1; |
| 75 | } else { |
| 76 | legacy_props_path_modified = true; |
| 77 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 78 | } |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 79 | |
| 80 | return 0; |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 83 | static int switch_to_new_properties() |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 84 | { |
| 85 | if (TWFunc::Path_Exists(properties_path_renamed)) { |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 86 | if (rename(properties_path_renamed, properties_path) != 0) { |
| 87 | LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno)); |
| 88 | return -1; |
| 89 | } else { |
| 90 | legacy_props_path_modified = false; |
| 91 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 92 | } |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 93 | |
| 94 | return 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 95 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 96 | |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 97 | static int Install_Theme(const char* path, ZipArchive *Zip) { |
| 98 | #ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds |
| 99 | mzCloseZipArchive(Zip); |
| 100 | return INSTALL_CORRUPT; |
| 101 | #else |
| 102 | const ZipEntry* xml_location = mzFindZipEntry(Zip, "ui.xml"); |
| 103 | |
| 104 | mzCloseZipArchive(Zip); |
| 105 | if (xml_location == NULL) { |
| 106 | return INSTALL_CORRUPT; |
| 107 | } |
| 108 | if (!PartitionManager.Mount_Settings_Storage(true)) |
| 109 | return INSTALL_ERROR; |
| 110 | string theme_path = DataManager::GetSettingsStoragePath(); |
| 111 | theme_path += "/TWRP/theme"; |
| 112 | if (!TWFunc::Path_Exists(theme_path)) { |
| 113 | if (!TWFunc::Recursive_Mkdir(theme_path)) { |
| 114 | return INSTALL_ERROR; |
| 115 | } |
| 116 | } |
| 117 | theme_path += "/ui.zip"; |
| 118 | if (TWFunc::copy_file(path, theme_path, 0644) != 0) { |
| 119 | return INSTALL_ERROR; |
| 120 | } |
| 121 | LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str()); |
| 122 | PageManager::RequestReload(); |
| 123 | return INSTALL_SUCCESS; |
| 124 | #endif |
| 125 | } |
| 126 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 127 | static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) { |
| 128 | const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 129 | string Temp_Binary = "/tmp/updater"; // Note: AOSP names it /tmp/update_binary (yes, with "_") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 130 | int binary_fd, ret_val, pipe_fd[2], status, zip_verify; |
| 131 | char buffer[1024]; |
| 132 | const char** args = (const char**)malloc(sizeof(char*) * 5); |
| 133 | FILE* child_data; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 134 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 135 | if (binary_location == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 136 | return INSTALL_CORRUPT; |
| 137 | } |
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 | // Delete any existing updater |
| 140 | if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 141 | LOGINFO("Unable to unlink '%s': %s\n", Temp_Binary.c_str(), strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 142 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 143 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 144 | binary_fd = creat(Temp_Binary.c_str(), 0755); |
| 145 | if (binary_fd < 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 146 | LOGERR("Could not create file for updater extract in '%s': %s\n", Temp_Binary.c_str(), strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 147 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 148 | return INSTALL_ERROR; |
| 149 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 150 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 151 | ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd); |
| 152 | close(binary_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 153 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 154 | if (!ret_val) { |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 155 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 156 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 157 | return INSTALL_ERROR; |
| 158 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 159 | |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 160 | // If exists, extract file_contexts from the zip file |
| 161 | const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts"); |
| 162 | if (selinx_contexts == NULL) { |
| 163 | mzCloseZipArchive(Zip); |
| 164 | LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n"); |
| 165 | } else { |
| 166 | string output_filename = "/file_contexts"; |
| 167 | LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str()); |
| 168 | // Delete any file_contexts |
| 169 | if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 170 | LOGINFO("Unable to unlink '%s': %s\n", output_filename.c_str(), strerror(errno)); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int file_contexts_fd = creat(output_filename.c_str(), 0644); |
| 174 | if (file_contexts_fd < 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 175 | LOGERR("Could not extract to '%s': %s\n", output_filename.c_str(), strerror(errno)); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 176 | mzCloseZipArchive(Zip); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 177 | return INSTALL_ERROR; |
| 178 | } |
| 179 | |
| 180 | ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd); |
| 181 | close(file_contexts_fd); |
| 182 | |
| 183 | if (!ret_val) { |
| 184 | mzCloseZipArchive(Zip); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 185 | LOGERR("Could not extract '%s'\n", output_filename.c_str()); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 186 | return INSTALL_ERROR; |
| 187 | } |
| 188 | } |
| 189 | mzCloseZipArchive(Zip); |
| 190 | |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 191 | #ifndef TW_NO_LEGACY_PROPS |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 192 | /* Set legacy properties */ |
| 193 | if (switch_to_legacy_properties() != 0) { |
| 194 | LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n"); |
| 195 | } else { |
| 196 | LOGINFO("Legacy property environment initialized.\n"); |
| 197 | } |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 198 | #endif |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 199 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 200 | pipe(pipe_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 201 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 202 | args[0] = Temp_Binary.c_str(); |
| 203 | args[1] = EXPAND(RECOVERY_API_VERSION); |
| 204 | char* temp = (char*)malloc(10); |
| 205 | sprintf(temp, "%d", pipe_fd[1]); |
| 206 | args[2] = temp; |
| 207 | args[3] = (char*)path; |
| 208 | args[4] = NULL; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 209 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 210 | pid_t pid = fork(); |
| 211 | if (pid == 0) { |
| 212 | close(pipe_fd[0]); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 213 | execve(Temp_Binary.c_str(), (char* const*)args, environ); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 214 | printf("E:Can't execute '%s': %s\n", Temp_Binary.c_str(), strerror(errno)); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 215 | free(temp); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 216 | _exit(-1); |
| 217 | } |
| 218 | close(pipe_fd[1]); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 219 | free(temp); |
| 220 | temp = NULL; |
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 | *wipe_cache = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 223 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 224 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
| 225 | child_data = fdopen(pipe_fd[0], "r"); |
| 226 | while (fgets(buffer, sizeof(buffer), child_data) != NULL) { |
| 227 | char* command = strtok(buffer, " \n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | if (command == NULL) { |
| 229 | continue; |
| 230 | } else if (strcmp(command, "progress") == 0) { |
| 231 | char* fraction_char = strtok(NULL, " \n"); |
| 232 | char* seconds_char = strtok(NULL, " \n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 233 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 234 | float fraction_float = strtof(fraction_char, NULL); |
| 235 | int seconds_float = strtol(seconds_char, NULL, 10); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 236 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 237 | if (zip_verify) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 238 | DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float); |
| 239 | else |
| 240 | DataManager::ShowProgress(fraction_float, seconds_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 241 | } else if (strcmp(command, "set_progress") == 0) { |
| 242 | char* fraction_char = strtok(NULL, " \n"); |
| 243 | float fraction_float = strtof(fraction_char, NULL); |
| 244 | DataManager::SetProgress(fraction_float); |
| 245 | } else if (strcmp(command, "ui_print") == 0) { |
| 246 | char* display_value = strtok(NULL, "\n"); |
| 247 | if (display_value) { |
| 248 | gui_print("%s", display_value); |
| 249 | } else { |
| 250 | gui_print("\n"); |
| 251 | } |
| 252 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 253 | *wipe_cache = 1; |
| 254 | } else if (strcmp(command, "clear_display") == 0) { |
| 255 | // Do nothing, not supported by TWRP |
| 256 | } else { |
| 257 | LOGERR("unknown command [%s]\n", command); |
| 258 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 259 | } |
| 260 | fclose(child_data); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 261 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 262 | int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater"); |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 263 | |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 264 | #ifndef TW_NO_LEGACY_PROPS |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 265 | /* Unset legacy properties */ |
| 266 | if (legacy_props_path_modified) { |
| 267 | if (switch_to_new_properties() != 0) { |
| 268 | LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n"); |
| 269 | } else { |
| 270 | LOGINFO("Legacy property environment disabled.\n"); |
| 271 | } |
| 272 | } |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 273 | #endif |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 274 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 275 | if (waitrc != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 276 | return INSTALL_ERROR; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 277 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 278 | return INSTALL_SUCCESS; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 281 | extern "C" int TWinstall_zip(const char* path, int* wipe_cache) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 282 | int ret_val, zip_verify = 1; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 283 | ZipArchive Zip; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 284 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 285 | if (strcmp(path, "error") == 0) { |
| 286 | LOGERR("Failed to get adb sideload file: '%s'\n", path); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 287 | return INSTALL_CORRUPT; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 288 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 289 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 290 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path)); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 291 | if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 292 | gui_msg("check_for_md5=Checking for MD5 file..."); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 293 | twrpDigest md5sum; |
| 294 | md5sum.setfn(path); |
| 295 | int md5_return = md5sum.verify_md5digest(); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 296 | if (md5_return == -2) { // md5 did not match |
| 297 | LOGERR("Aborting zip install\n"); |
| 298 | return INSTALL_CORRUPT; |
| 299 | } |
| 300 | } |
| 301 | |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 302 | #ifndef TW_OEM_BUILD |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 303 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 304 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 305 | DataManager::SetProgress(0); |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 306 | |
| 307 | MemMapping map; |
| 308 | if (sysMapFile(path, &map) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 309 | gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path)); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 310 | return -1; |
| 311 | } |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 312 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 313 | if (zip_verify) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 314 | gui_msg("verify_zip_sig=Verifying zip signature..."); |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 315 | ret_val = verify_file(map.addr, map.length); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 316 | if (ret_val != VERIFY_SUCCESS) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 317 | LOGINFO("Zip signature verification failed: %i\n", ret_val); |
| 318 | gui_err("verify_zip_fail=Zip signature verification failed!"); |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 319 | sysReleaseMap(&map); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 320 | return -1; |
Ethan Yonker | 738be7a | 2014-12-10 11:40:43 -0600 | [diff] [blame] | 321 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 322 | gui_msg("verify_zip_done=Zip signature verified successfully."); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 323 | } |
| 324 | } |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 325 | ret_val = mzOpenZipArchive(map.addr, map.length, &Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 326 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 327 | gui_err("zip_corrupt=Zip file is corrupt!"); |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 328 | sysReleaseMap(&map); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 329 | return INSTALL_CORRUPT; |
| 330 | } |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 331 | ret_val = Run_Update_Binary(path, &Zip, wipe_cache); |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 332 | if (ret_val == INSTALL_CORRUPT) { |
| 333 | // If no updater binary is found, check for ui.xml |
| 334 | ret_val = Install_Theme(path, &Zip); |
| 335 | if (ret_val == INSTALL_CORRUPT) |
| 336 | gui_msg(Msg(msg::kError, "no_updater_binary=Could not find '{1}' in the zip file.")(ASSUMED_UPDATE_BINARY_NAME)); |
| 337 | } |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 338 | sysReleaseMap(&map); |
| 339 | return ret_val; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 340 | } |