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