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