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" |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 31 | #include "mtdutils/mounts.h" |
| 32 | #include "mtdutils/mtdutils.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 33 | #include "minzip/SysUtil.h" |
| 34 | #include "minzip/Zip.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 35 | #ifdef USE_OLD_VERIFIER |
Ethan Yonker | 4bf259f | 2016-08-29 11:50:34 -0500 | [diff] [blame] | 36 | #include "verifier24/verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 37 | #else |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 38 | #include "verifier.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 39 | #endif |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 40 | #include "variables.h" |
| 41 | #include "data.hpp" |
| 42 | #include "partitions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 43 | #include "twrpDigest.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 44 | #include "twrp-functions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 45 | #include "gui/gui.hpp" |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 46 | #include "gui/pages.hpp" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 47 | #include "legacy_property_service.h" |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 48 | #include "twinstall.h" |
| 49 | #include "installcommand.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 50 | extern "C" { |
| 51 | #include "gui/gui.h" |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 54 | #define AB_OTA "payload_properties.txt" |
| 55 | |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 56 | static const char* properties_path = "/dev/__properties__"; |
| 57 | static const char* properties_path_renamed = "/dev/__properties_kk__"; |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 58 | static bool legacy_props_env_initd = false; |
| 59 | static bool legacy_props_path_modified = false; |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 60 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 61 | enum zip_type { |
| 62 | UNKNOWN_ZIP_TYPE = 0, |
| 63 | UPDATE_BINARY_ZIP_TYPE, |
| 64 | AB_OTA_ZIP_TYPE, |
| 65 | TWRP_THEME_ZIP_TYPE |
| 66 | }; |
| 67 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 68 | // 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] | 69 | static int switch_to_legacy_properties() |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 70 | { |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 71 | if (!legacy_props_env_initd) { |
| 72 | if (legacy_properties_init() != 0) |
| 73 | return -1; |
| 74 | |
| 75 | char tmp[32]; |
| 76 | int propfd, propsz; |
| 77 | legacy_get_property_workspace(&propfd, &propsz); |
| 78 | sprintf(tmp, "%d,%d", dup(propfd), propsz); |
| 79 | setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1); |
| 80 | legacy_props_env_initd = true; |
| 81 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 82 | |
| 83 | if (TWFunc::Path_Exists(properties_path)) { |
| 84 | // 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] | 85 | if (rename(properties_path, properties_path_renamed) != 0) { |
| 86 | LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno)); |
| 87 | return -1; |
| 88 | } else { |
| 89 | legacy_props_path_modified = true; |
| 90 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 91 | } |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 92 | |
| 93 | return 0; |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 96 | static int switch_to_new_properties() |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 97 | { |
| 98 | if (TWFunc::Path_Exists(properties_path_renamed)) { |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 99 | if (rename(properties_path_renamed, properties_path) != 0) { |
| 100 | LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno)); |
| 101 | return -1; |
| 102 | } else { |
| 103 | legacy_props_path_modified = false; |
| 104 | } |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 105 | } |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 106 | |
| 107 | return 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 108 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 109 | |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 110 | static int Install_Theme(const char* path, ZipArchive *Zip) { |
| 111 | #ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds |
| 112 | mzCloseZipArchive(Zip); |
| 113 | return INSTALL_CORRUPT; |
| 114 | #else |
| 115 | const ZipEntry* xml_location = mzFindZipEntry(Zip, "ui.xml"); |
| 116 | |
| 117 | mzCloseZipArchive(Zip); |
| 118 | if (xml_location == NULL) { |
| 119 | return INSTALL_CORRUPT; |
| 120 | } |
| 121 | if (!PartitionManager.Mount_Settings_Storage(true)) |
| 122 | return INSTALL_ERROR; |
| 123 | string theme_path = DataManager::GetSettingsStoragePath(); |
| 124 | theme_path += "/TWRP/theme"; |
| 125 | if (!TWFunc::Path_Exists(theme_path)) { |
| 126 | if (!TWFunc::Recursive_Mkdir(theme_path)) { |
| 127 | return INSTALL_ERROR; |
| 128 | } |
| 129 | } |
| 130 | theme_path += "/ui.zip"; |
| 131 | if (TWFunc::copy_file(path, theme_path, 0644) != 0) { |
| 132 | return INSTALL_ERROR; |
| 133 | } |
| 134 | LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str()); |
| 135 | PageManager::RequestReload(); |
| 136 | return INSTALL_SUCCESS; |
| 137 | #endif |
| 138 | } |
| 139 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 140 | static int Prepare_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 141 | const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 142 | int binary_fd, ret_val; |
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 | if (binary_location == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 145 | return INSTALL_CORRUPT; |
| 146 | } |
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 | // Delete any existing updater |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 149 | if (TWFunc::Path_Exists(TMP_UPDATER_BINARY_PATH) && unlink(TMP_UPDATER_BINARY_PATH) != 0) { |
| 150 | LOGINFO("Unable to unlink '%s': %s\n", TMP_UPDATER_BINARY_PATH, strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 151 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 152 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 153 | binary_fd = creat(TMP_UPDATER_BINARY_PATH, 0755); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 154 | if (binary_fd < 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 155 | LOGERR("Could not create file for updater extract in '%s': %s\n", TMP_UPDATER_BINARY_PATH, strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 156 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 157 | return INSTALL_ERROR; |
| 158 | } |
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 | ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd); |
| 161 | close(binary_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 162 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 163 | if (!ret_val) { |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 164 | mzCloseZipArchive(Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 165 | LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME); |
| 166 | return INSTALL_ERROR; |
| 167 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 168 | |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 169 | // If exists, extract file_contexts from the zip file |
| 170 | const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts"); |
| 171 | if (selinx_contexts == NULL) { |
| 172 | mzCloseZipArchive(Zip); |
| 173 | LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n"); |
| 174 | } else { |
| 175 | string output_filename = "/file_contexts"; |
| 176 | LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str()); |
| 177 | // Delete any file_contexts |
| 178 | if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 179 | 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] | 180 | } |
| 181 | |
| 182 | int file_contexts_fd = creat(output_filename.c_str(), 0644); |
| 183 | if (file_contexts_fd < 0) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 184 | 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] | 185 | mzCloseZipArchive(Zip); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 186 | return INSTALL_ERROR; |
| 187 | } |
| 188 | |
| 189 | ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd); |
| 190 | close(file_contexts_fd); |
| 191 | |
| 192 | if (!ret_val) { |
| 193 | mzCloseZipArchive(Zip); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 194 | LOGERR("Could not extract '%s'\n", output_filename.c_str()); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 195 | return INSTALL_ERROR; |
| 196 | } |
| 197 | } |
| 198 | mzCloseZipArchive(Zip); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 199 | return INSTALL_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache, zip_type ztype) { |
| 203 | int ret_val, pipe_fd[2], status, zip_verify; |
| 204 | char buffer[1024]; |
| 205 | FILE* child_data; |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 206 | |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 207 | #ifndef TW_NO_LEGACY_PROPS |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 208 | /* Set legacy properties */ |
| 209 | if (switch_to_legacy_properties() != 0) { |
| 210 | LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n"); |
| 211 | } else { |
| 212 | LOGINFO("Legacy property environment initialized.\n"); |
| 213 | } |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 214 | #endif |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 215 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 216 | pipe(pipe_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 217 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 218 | std::vector<std::string> args; |
| 219 | if (ztype == UPDATE_BINARY_ZIP_TYPE) { |
| 220 | ret_val = update_binary_command(path, Zip, 0, pipe_fd[1], &args); |
| 221 | } else if (ztype == AB_OTA_ZIP_TYPE) { |
| 222 | ret_val = abupdate_binary_command(path, Zip, 0, pipe_fd[1], &args); |
| 223 | } else { |
| 224 | LOGERR("Unknown zip type %i\n", ztype); |
| 225 | ret_val = INSTALL_CORRUPT; |
| 226 | } |
| 227 | if (ret_val) { |
| 228 | close(pipe_fd[0]); |
| 229 | close(pipe_fd[1]); |
| 230 | return ret_val; |
| 231 | } |
| 232 | |
| 233 | // Convert the vector to a NULL-terminated char* array suitable for execv. |
| 234 | const char* chr_args[args.size() + 1]; |
| 235 | chr_args[args.size()] = NULL; |
| 236 | for (size_t i = 0; i < args.size(); i++) |
| 237 | chr_args[i] = args[i].c_str(); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 238 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 239 | pid_t pid = fork(); |
| 240 | if (pid == 0) { |
| 241 | close(pipe_fd[0]); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 242 | execve(chr_args[0], const_cast<char**>(chr_args), environ); |
| 243 | printf("E:Can't execute '%s': %s\n", chr_args[0], strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 244 | _exit(-1); |
| 245 | } |
| 246 | close(pipe_fd[1]); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 247 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 248 | *wipe_cache = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 249 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 250 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
| 251 | child_data = fdopen(pipe_fd[0], "r"); |
| 252 | while (fgets(buffer, sizeof(buffer), child_data) != NULL) { |
| 253 | char* command = strtok(buffer, " \n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 254 | if (command == NULL) { |
| 255 | continue; |
| 256 | } else if (strcmp(command, "progress") == 0) { |
| 257 | char* fraction_char = strtok(NULL, " \n"); |
| 258 | char* seconds_char = strtok(NULL, " \n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 259 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 260 | float fraction_float = strtof(fraction_char, NULL); |
| 261 | int seconds_float = strtol(seconds_char, NULL, 10); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 262 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 263 | if (zip_verify) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 264 | DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float); |
| 265 | else |
| 266 | DataManager::ShowProgress(fraction_float, seconds_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 267 | } else if (strcmp(command, "set_progress") == 0) { |
| 268 | char* fraction_char = strtok(NULL, " \n"); |
| 269 | float fraction_float = strtof(fraction_char, NULL); |
| 270 | DataManager::SetProgress(fraction_float); |
| 271 | } else if (strcmp(command, "ui_print") == 0) { |
| 272 | char* display_value = strtok(NULL, "\n"); |
| 273 | if (display_value) { |
| 274 | gui_print("%s", display_value); |
| 275 | } else { |
| 276 | gui_print("\n"); |
| 277 | } |
| 278 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 279 | *wipe_cache = 1; |
| 280 | } else if (strcmp(command, "clear_display") == 0) { |
| 281 | // Do nothing, not supported by TWRP |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 282 | } else if (strcmp(command, "log") == 0) { |
| 283 | printf("%s\n", strtok(NULL, "\n")); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 284 | } else { |
| 285 | LOGERR("unknown command [%s]\n", command); |
| 286 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 287 | } |
| 288 | fclose(child_data); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 289 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 290 | int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater"); |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 291 | |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 292 | #ifndef TW_NO_LEGACY_PROPS |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 293 | /* Unset legacy properties */ |
| 294 | if (legacy_props_path_modified) { |
| 295 | if (switch_to_new_properties() != 0) { |
| 296 | LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n"); |
| 297 | } else { |
| 298 | LOGINFO("Legacy property environment disabled.\n"); |
| 299 | } |
| 300 | } |
Matt Mower | 6883d73 | 2014-03-20 17:28:13 -0500 | [diff] [blame] | 301 | #endif |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 302 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 303 | if (waitrc != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 304 | return INSTALL_ERROR; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 305 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 306 | return INSTALL_SUCCESS; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 309 | extern "C" int TWinstall_zip(const char* path, int* wipe_cache) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 310 | int ret_val, zip_verify = 1; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 311 | ZipArchive Zip; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 312 | |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 313 | if (strcmp(path, "error") == 0) { |
| 314 | LOGERR("Failed to get adb sideload file: '%s'\n", path); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 315 | return INSTALL_CORRUPT; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 316 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 317 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 318 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path)); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 319 | if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 320 | gui_msg("check_for_md5=Checking for MD5 file..."); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 321 | twrpDigest md5sum; |
| 322 | md5sum.setfn(path); |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 323 | switch (md5sum.verify_md5digest()) { |
| 324 | case MD5_OK: |
| 325 | gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(path)); |
| 326 | break; |
| 327 | case MD5_NOT_FOUND: |
| 328 | gui_msg("no_md5=Skipping MD5 check: no MD5 file found"); |
| 329 | break; |
| 330 | case MD5_FILE_UNREADABLE: |
| 331 | LOGERR("Skipping MD5 check: MD5 file unreadable\n"); |
| 332 | break; |
| 333 | case MD5_MATCH_FAIL: // md5 did not match |
| 334 | LOGERR("Aborting zip install: MD5 verification failed\n"); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 335 | return INSTALL_CORRUPT; |
| 336 | } |
| 337 | } |
| 338 | |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 339 | #ifndef TW_OEM_BUILD |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 340 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 341 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 342 | DataManager::SetProgress(0); |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 343 | |
| 344 | MemMapping map; |
| 345 | if (sysMapFile(path, &map) != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 346 | gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path)); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 347 | return -1; |
| 348 | } |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 349 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 350 | if (zip_verify) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 351 | gui_msg("verify_zip_sig=Verifying zip signature..."); |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 352 | ret_val = verify_file(map.addr, map.length); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 353 | if (ret_val != VERIFY_SUCCESS) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 354 | LOGINFO("Zip signature verification failed: %i\n", ret_val); |
| 355 | gui_err("verify_zip_fail=Zip signature verification failed!"); |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 356 | sysReleaseMap(&map); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 357 | return -1; |
Ethan Yonker | 738be7a | 2014-12-10 11:40:43 -0600 | [diff] [blame] | 358 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 359 | gui_msg("verify_zip_done=Zip signature verified successfully."); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 360 | } |
| 361 | } |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 362 | ret_val = mzOpenZipArchive(map.addr, map.length, &Zip); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 363 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 364 | gui_err("zip_corrupt=Zip file is corrupt!"); |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 365 | sysReleaseMap(&map); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 366 | return INSTALL_CORRUPT; |
| 367 | } |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 368 | |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 369 | time_t start, stop; |
| 370 | time(&start); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 371 | const ZipEntry* file_location = mzFindZipEntry(&Zip, ASSUMED_UPDATE_BINARY_NAME); |
| 372 | if (file_location != NULL) { |
| 373 | LOGINFO("Update binary zip\n"); |
| 374 | ret_val = Prepare_Update_Binary(path, &Zip, wipe_cache); |
| 375 | if (ret_val == INSTALL_SUCCESS) |
| 376 | ret_val = Run_Update_Binary(path, &Zip, wipe_cache, UPDATE_BINARY_ZIP_TYPE); |
| 377 | } else { |
| 378 | file_location = mzFindZipEntry(&Zip, AB_OTA); |
| 379 | if (file_location != NULL) { |
| 380 | LOGINFO("AB zip\n"); |
| 381 | ret_val = Run_Update_Binary(path, &Zip, wipe_cache, AB_OTA_ZIP_TYPE); |
| 382 | } else { |
| 383 | file_location = mzFindZipEntry(&Zip, "ui.xml"); |
| 384 | if (file_location != NULL) { |
| 385 | LOGINFO("TWRP theme zip\n"); |
| 386 | ret_val = Install_Theme(path, &Zip); |
| 387 | } else { |
| 388 | mzCloseZipArchive(&Zip); |
| 389 | ret_val = INSTALL_CORRUPT; |
| 390 | } |
| 391 | } |
| 392 | } |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 393 | time(&stop); |
| 394 | int total_time = (int) difftime(stop, start); |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 395 | if (ret_val == INSTALL_CORRUPT) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 396 | gui_err("invalid_zip_format=Invalid zip file format!"); |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 397 | } else { |
| 398 | LOGINFO("Install took %i second(s).\n", total_time); |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 399 | } |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 400 | sysReleaseMap(&map); |
| 401 | return ret_val; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 402 | } |