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