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