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