The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Alex Deymo | 53c107f | 2016-08-12 13:43:04 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <limits.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <sys/stat.h> |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
| 25 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 27 | #include <chrono> |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 28 | #include <limits> |
| 29 | #include <map> |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 30 | #include <string> |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 33 | #include <android-base/parseint.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/strings.h> |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | #include "common.h" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 39 | #include "error_code.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | #include "install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | #include "minui/minui.h" |
| 42 | #include "minzip/SysUtil.h" |
| 43 | #include "minzip/Zip.h" |
| 44 | #include "mtdutils/mounts.h" |
| 45 | #include "mtdutils/mtdutils.h" |
| 46 | #include "roots.h" |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 47 | #include "ui.h" |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 48 | #include "verifier.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 50 | extern RecoveryUI* ui; |
| 51 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 52 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 53 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 54 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 55 | #define PUBLIC_KEYS_FILE "/res/keys" |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 56 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 57 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 58 | // Default allocation of progress bar segments to operations |
| 59 | static const int VERIFICATION_PROGRESS_TIME = 60; |
| 60 | static const float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 61 | static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; |
| 62 | static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; |
| 63 | |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 64 | // This function parses and returns the build.version.incremental |
| 65 | static int parse_build_number(std::string str) { |
| 66 | size_t pos = str.find("="); |
| 67 | if (pos != std::string::npos) { |
| 68 | std::string num_string = android::base::Trim(str.substr(pos+1)); |
| 69 | int build_number; |
| 70 | if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { |
| 71 | return build_number; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | LOGE("Failed to parse build number in %s.\n", str.c_str()); |
| 76 | return -1; |
| 77 | } |
| 78 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 79 | bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 80 | const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH); |
| 81 | if (meta_entry == nullptr) { |
| 82 | LOGE("Failed to find %s in update package.\n", METADATA_PATH); |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 83 | return false; |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 86 | meta_data->resize(meta_entry->uncompLen, '\0'); |
| 87 | if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 88 | LOGE("Failed to read metadata in update package.\n"); |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
| 95 | static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { |
| 96 | std::string meta_data; |
| 97 | if (!read_metadata_from_package(zip, &meta_data)) { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 100 | // Examples of the pre-build and post-build strings in metadata: |
| 101 | // pre-build-incremental=2943039 |
| 102 | // post-build-incremental=2951741 |
| 103 | std::vector<std::string> lines = android::base::Split(meta_data, "\n"); |
| 104 | for (const std::string& line : lines) { |
| 105 | std::string str = android::base::Trim(line); |
| 106 | if (android::base::StartsWith(str, "pre-build-incremental")){ |
| 107 | int source_build = parse_build_number(str); |
| 108 | if (source_build != -1) { |
| 109 | log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 110 | source_build)); |
| 111 | } |
| 112 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 113 | int target_build = parse_build_number(str); |
| 114 | if (target_build != -1) { |
| 115 | log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 116 | target_build)); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 122 | // Extract the update binary from the open zip archive |zip| located at |path| |
| 123 | // and store into |cmd| the command line that should be called. The |status_fd| |
| 124 | // is the file descriptor the child process should use to report back the |
| 125 | // progress of the update. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 126 | static int |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 127 | update_binary_command(const char* path, ZipArchive* zip, int retry_count, |
| 128 | int status_fd, std::vector<std::string>* cmd); |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 129 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 130 | #ifdef AB_OTA_UPDATER |
| 131 | |
| 132 | // Parses the metadata of the OTA package in |zip| and checks whether we are |
| 133 | // allowed to accept this A/B package. Downgrading is not allowed unless |
| 134 | // explicitly enabled in the package and only for incremental packages. |
| 135 | static int check_newer_ab_build(ZipArchive* zip) |
| 136 | { |
| 137 | std::string metadata_str; |
| 138 | if (!read_metadata_from_package(zip, &metadata_str)) { |
| 139 | return INSTALL_CORRUPT; |
| 140 | } |
| 141 | std::map<std::string, std::string> metadata; |
| 142 | for (const std::string& line : android::base::Split(metadata_str, "\n")) { |
| 143 | size_t eq = line.find('='); |
| 144 | if (eq != std::string::npos) { |
| 145 | metadata[line.substr(0, eq)] = line.substr(eq + 1); |
| 146 | } |
| 147 | } |
| 148 | char value[PROPERTY_VALUE_MAX]; |
| 149 | |
| 150 | property_get("ro.product.device", value, ""); |
| 151 | const std::string& pkg_device = metadata["pre-device"]; |
| 152 | if (pkg_device != value || pkg_device.empty()) { |
| 153 | LOGE("Package is for product %s but expected %s\n", |
| 154 | pkg_device.c_str(), value); |
| 155 | return INSTALL_ERROR; |
| 156 | } |
| 157 | |
| 158 | // We allow the package to not have any serialno, but if it has a non-empty |
| 159 | // value it should match. |
| 160 | property_get("ro.serialno", value, ""); |
| 161 | const std::string& pkg_serial_no = metadata["serialno"]; |
| 162 | if (!pkg_serial_no.empty() && pkg_serial_no != value) { |
| 163 | LOGE("Package is for serial %s\n", pkg_serial_no.c_str()); |
| 164 | return INSTALL_ERROR; |
| 165 | } |
| 166 | |
| 167 | if (metadata["ota-type"] != "AB") { |
| 168 | LOGE("Package is not A/B\n"); |
| 169 | return INSTALL_ERROR; |
| 170 | } |
| 171 | |
| 172 | // Incremental updates should match the current build. |
| 173 | property_get("ro.build.version.incremental", value, ""); |
| 174 | const std::string& pkg_pre_build = metadata["pre-build-incremental"]; |
| 175 | if (!pkg_pre_build.empty() && pkg_pre_build != value) { |
| 176 | LOGE("Package is for source build %s but expected %s\n", |
| 177 | pkg_pre_build.c_str(), value); |
| 178 | return INSTALL_ERROR; |
| 179 | } |
| 180 | property_get("ro.build.fingerprint", value, ""); |
| 181 | const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; |
| 182 | if (!pkg_pre_build_fingerprint.empty() && |
| 183 | pkg_pre_build_fingerprint != value) { |
| 184 | LOGE("Package is for source build %s but expected %s\n", |
| 185 | pkg_pre_build_fingerprint.c_str(), value); |
| 186 | return INSTALL_ERROR; |
| 187 | } |
| 188 | |
| 189 | // Check for downgrade version. |
| 190 | int64_t build_timestampt = property_get_int64( |
| 191 | "ro.build.date.utc", std::numeric_limits<int64_t>::max()); |
| 192 | int64_t pkg_post_timespampt = 0; |
| 193 | // We allow to full update to the same version we are running, in case there |
| 194 | // is a problem with the current copy of that version. |
| 195 | if (metadata["post-timestamp"].empty() || |
| 196 | !android::base::ParseInt(metadata["post-timestamp"].c_str(), |
| 197 | &pkg_post_timespampt) || |
| 198 | pkg_post_timespampt < build_timestampt) { |
| 199 | if (metadata["ota-downgrade"] != "yes") { |
| 200 | LOGE("Update package is older than the current build, expected a " |
| 201 | "build newer than timestamp %" PRIu64 " but package has " |
| 202 | "timestamp %" PRIu64 " and downgrade not allowed.\n", |
| 203 | build_timestampt, pkg_post_timespampt); |
| 204 | return INSTALL_ERROR; |
| 205 | } |
| 206 | if (pkg_pre_build_fingerprint.empty()) { |
| 207 | LOGE("Downgrade package must have a pre-build version set, not " |
| 208 | "allowed.\n"); |
| 209 | return INSTALL_ERROR; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int |
| 217 | update_binary_command(const char* path, ZipArchive* zip, int retry_count, |
| 218 | int status_fd, std::vector<std::string>* cmd) |
| 219 | { |
| 220 | int ret = check_newer_ab_build(zip); |
| 221 | if (ret) { |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | // For A/B updates we extract the payload properties to a buffer and obtain |
| 226 | // the RAW payload offset in the zip file. |
| 227 | const ZipEntry* properties_entry = |
| 228 | mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES); |
| 229 | if (!properties_entry) { |
| 230 | LOGE("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 231 | return INSTALL_CORRUPT; |
| 232 | } |
| 233 | std::vector<unsigned char> payload_properties( |
| 234 | mzGetZipEntryUncompLen(properties_entry)); |
| 235 | if (!mzExtractZipEntryToBuffer(zip, properties_entry, |
| 236 | payload_properties.data())) { |
| 237 | LOGE("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 238 | return INSTALL_CORRUPT; |
| 239 | } |
| 240 | |
| 241 | const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD); |
| 242 | if (!payload_entry) { |
| 243 | LOGE("Can't find %s\n", AB_OTA_PAYLOAD); |
| 244 | return INSTALL_CORRUPT; |
| 245 | } |
| 246 | long payload_offset = mzGetZipEntryOffset(payload_entry); |
| 247 | *cmd = { |
| 248 | "/sbin/update_engine_sideload", |
| 249 | android::base::StringPrintf("--payload=file://%s", path), |
| 250 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 251 | "--headers=" + std::string(payload_properties.begin(), |
| 252 | payload_properties.end()), |
| 253 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 254 | }; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | #else // !AB_OTA_UPDATER |
| 259 | |
| 260 | static int |
| 261 | update_binary_command(const char* path, ZipArchive* zip, int retry_count, |
| 262 | int status_fd, std::vector<std::string>* cmd) |
| 263 | { |
| 264 | // On traditional updates we extract the update binary from the package. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 265 | const ZipEntry* binary_entry = |
| 266 | mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); |
| 267 | if (binary_entry == NULL) { |
| 268 | return INSTALL_CORRUPT; |
| 269 | } |
| 270 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 271 | const char* binary = "/tmp/update_binary"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 272 | unlink(binary); |
| 273 | int fd = creat(binary, 0755); |
| 274 | if (fd < 0) { |
| 275 | LOGE("Can't make %s\n", binary); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 276 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 277 | } |
| 278 | bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); |
| 279 | close(fd); |
| 280 | |
| 281 | if (!ok) { |
| 282 | LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 283 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 286 | *cmd = { |
| 287 | binary, |
| 288 | EXPAND(RECOVERY_API_VERSION), // defined in Android.mk |
| 289 | std::to_string(status_fd), |
| 290 | path, |
| 291 | }; |
| 292 | if (retry_count > 0) |
| 293 | cmd->push_back("retry"); |
| 294 | return 0; |
| 295 | } |
| 296 | #endif // !AB_OTA_UPDATER |
| 297 | |
| 298 | // If the package contains an update binary, extract it and run it. |
| 299 | static int |
| 300 | try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, |
| 301 | std::vector<std::string>& log_buffer, int retry_count) |
| 302 | { |
| 303 | read_source_target_build(zip, log_buffer); |
| 304 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 305 | int pipefd[2]; |
| 306 | pipe(pipefd); |
| 307 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 308 | std::vector<std::string> args; |
| 309 | int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args); |
| 310 | mzCloseZipArchive(zip); |
| 311 | if (ret) { |
| 312 | close(pipefd[0]); |
| 313 | close(pipefd[1]); |
| 314 | return ret; |
| 315 | } |
| 316 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 317 | // When executing the update binary contained in the package, the |
| 318 | // arguments passed are: |
| 319 | // |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 320 | // - the version number for this interface |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 321 | // |
| 322 | // - an fd to which the program can write in order to update the |
| 323 | // progress bar. The program can write single-line commands: |
| 324 | // |
| 325 | // progress <frac> <secs> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 326 | // fill up the next <frac> part of of the progress bar |
| 327 | // over <secs> seconds. If <secs> is zero, use |
| 328 | // set_progress commands to manually control the |
Tao Bao | b07e1f3 | 2015-04-10 16:14:52 -0700 | [diff] [blame] | 329 | // progress of this segment of the bar. |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 330 | // |
| 331 | // set_progress <frac> |
| 332 | // <frac> should be between 0.0 and 1.0; sets the |
| 333 | // progress bar within the segment defined by the most |
| 334 | // recent progress command. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 335 | // |
| 336 | // firmware <"hboot"|"radio"> <filename> |
| 337 | // arrange to install the contents of <filename> in the |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 338 | // given partition on reboot. |
| 339 | // |
| 340 | // (API v2: <filename> may start with "PACKAGE:" to |
| 341 | // indicate taking a file from the OTA package.) |
| 342 | // |
| 343 | // (API v3: this command no longer exists.) |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 344 | // |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 345 | // ui_print <string> |
| 346 | // display <string> on the screen. |
| 347 | // |
Tao Bao | b07e1f3 | 2015-04-10 16:14:52 -0700 | [diff] [blame] | 348 | // wipe_cache |
| 349 | // a wipe of cache will be performed following a successful |
| 350 | // installation. |
| 351 | // |
| 352 | // clear_display |
| 353 | // turn off the text display. |
| 354 | // |
| 355 | // enable_reboot |
| 356 | // packages can explicitly request that they want the user |
| 357 | // to be able to reboot during installation (useful for |
| 358 | // debugging packages that don't exit). |
| 359 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 360 | // - the name of the package zip file. |
| 361 | // |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 362 | // - an optional argument "retry" if this update is a retry of a failed |
| 363 | // update attempt. |
| 364 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 365 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 366 | // Convert the vector to a NULL-terminated char* array suitable for execv. |
| 367 | const char* chr_args[args.size() + 1]; |
| 368 | chr_args[args.size()] = NULL; |
| 369 | for (size_t i = 0; i < args.size(); i++) { |
| 370 | chr_args[i] = args[i].c_str(); |
| 371 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 372 | |
| 373 | pid_t pid = fork(); |
| 374 | if (pid == 0) { |
Alistair Strachan | 027429a | 2013-07-17 10:41:49 -0700 | [diff] [blame] | 375 | umask(022); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 376 | close(pipefd[0]); |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 377 | execv(chr_args[0], const_cast<char**>(chr_args)); |
| 378 | fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 379 | _exit(-1); |
| 380 | } |
| 381 | close(pipefd[1]); |
| 382 | |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 383 | *wipe_cache = false; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 384 | bool retry_update = false; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 385 | |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 386 | char buffer[1024]; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 387 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 388 | while (fgets(buffer, sizeof(buffer), from_child) != NULL) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 389 | char* command = strtok(buffer, " \n"); |
| 390 | if (command == NULL) { |
| 391 | continue; |
| 392 | } else if (strcmp(command, "progress") == 0) { |
| 393 | char* fraction_s = strtok(NULL, " \n"); |
| 394 | char* seconds_s = strtok(NULL, " \n"); |
| 395 | |
| 396 | float fraction = strtof(fraction_s, NULL); |
| 397 | int seconds = strtol(seconds_s, NULL, 10); |
| 398 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 399 | ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 400 | } else if (strcmp(command, "set_progress") == 0) { |
| 401 | char* fraction_s = strtok(NULL, " \n"); |
| 402 | float fraction = strtof(fraction_s, NULL); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 403 | ui->SetProgress(fraction); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 404 | } else if (strcmp(command, "ui_print") == 0) { |
| 405 | char* str = strtok(NULL, "\n"); |
| 406 | if (str) { |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 407 | ui->PrintOnScreenOnly("%s", str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 408 | } else { |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 409 | ui->PrintOnScreenOnly("\n"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 410 | } |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 411 | fflush(stdout); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 412 | } else if (strcmp(command, "wipe_cache") == 0) { |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 413 | *wipe_cache = true; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 414 | } else if (strcmp(command, "clear_display") == 0) { |
| 415 | ui->SetBackground(RecoveryUI::NONE); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 416 | } else if (strcmp(command, "enable_reboot") == 0) { |
| 417 | // packages can explicitly request that they want the user |
| 418 | // to be able to reboot during installation (useful for |
| 419 | // debugging packages that don't exit). |
| 420 | ui->SetEnableReboot(true); |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 421 | } else if (strcmp(command, "retry_update") == 0) { |
| 422 | retry_update = true; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 423 | } else if (strcmp(command, "log") == 0) { |
| 424 | // Save the logging request from updater and write to |
| 425 | // last_install later. |
| 426 | log_buffer.push_back(std::string(strtok(NULL, "\n"))); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 427 | } else { |
| 428 | LOGE("unknown command [%s]\n", command); |
| 429 | } |
| 430 | } |
| 431 | fclose(from_child); |
| 432 | |
| 433 | int status; |
| 434 | waitpid(pid, &status, 0); |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 435 | if (retry_update) { |
| 436 | return INSTALL_RETRY; |
| 437 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 438 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 439 | LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 440 | return INSTALL_ERROR; |
| 441 | } |
| 442 | |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 443 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 446 | static int |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 447 | really_install_package(const char *path, bool* wipe_cache, bool needs_mount, |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 448 | std::vector<std::string>& log_buffer, int retry_count) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 449 | { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 450 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 451 | ui->Print("Finding update package...\n"); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 452 | // Give verification half the progress bar... |
| 453 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 454 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 455 | LOGI("Update location: %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 456 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 457 | // Map the update package into memory. |
| 458 | ui->Print("Opening update package...\n"); |
| 459 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 460 | if (path && needs_mount) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 461 | if (path[0] == '@') { |
| 462 | ensure_path_mounted(path+1); |
| 463 | } else { |
| 464 | ensure_path_mounted(path); |
| 465 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 468 | MemMapping map; |
| 469 | if (sysMapFile(path, &map) != 0) { |
| 470 | LOGE("failed to map file\n"); |
| 471 | return INSTALL_CORRUPT; |
| 472 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 473 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 474 | // Verify package. |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 475 | if (!verify_package(map.addr, map.length)) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 476 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 477 | sysReleaseMap(&map); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 478 | return INSTALL_CORRUPT; |
| 479 | } |
| 480 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 481 | // Try to open the package. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 482 | ZipArchive zip; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 483 | int err = mzOpenZipArchive(map.addr, map.length, &zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 484 | if (err != 0) { |
| 485 | LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 486 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); |
| 487 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 488 | sysReleaseMap(&map); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 489 | return INSTALL_CORRUPT; |
| 490 | } |
| 491 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 492 | // Verify and install the contents of the package. |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 493 | ui->Print("Installing update...\n"); |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 494 | if (retry_count > 0) { |
| 495 | ui->Print("Retry attempt: %d\n", retry_count); |
| 496 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 497 | ui->SetEnableReboot(false); |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 498 | int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 499 | ui->SetEnableReboot(true); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 500 | ui->Print("\n"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 501 | |
| 502 | sysReleaseMap(&map); |
| 503 | |
| 504 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 505 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 506 | |
| 507 | int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 508 | install_package(const char* path, bool* wipe_cache, const char* install_file, |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 509 | bool needs_mount, int retry_count) |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 510 | { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 511 | modified_flash = true; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 512 | auto start = std::chrono::system_clock::now(); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 513 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 514 | FILE* install_log = fopen_path(install_file, "w"); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 515 | if (install_log) { |
| 516 | fputs(path, install_log); |
| 517 | fputc('\n', install_log); |
| 518 | } else { |
| 519 | LOGE("failed to open last_install: %s\n", strerror(errno)); |
| 520 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 521 | int result; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 522 | std::vector<std::string> log_buffer; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 523 | if (setup_install_mounts() != 0) { |
| 524 | LOGE("failed to set up expected mounts for install; aborting\n"); |
| 525 | result = INSTALL_ERROR; |
| 526 | } else { |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 527 | result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 528 | } |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 529 | if (install_log != nullptr) { |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 530 | fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log); |
| 531 | fputc('\n', install_log); |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 532 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; |
| 533 | int count = static_cast<int>(duration.count()); |
| 534 | // Report the time spent to apply OTA update in seconds. |
| 535 | fprintf(install_log, "time_total: %d\n", count); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 536 | fprintf(install_log, "retry: %d\n", retry_count); |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 537 | |
| 538 | for (const auto& s : log_buffer) { |
| 539 | fprintf(install_log, "%s\n", s.c_str()); |
| 540 | } |
| 541 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 542 | fclose(install_log); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 543 | } |
| 544 | return result; |
| 545 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 546 | |
| 547 | bool verify_package(const unsigned char* package_data, size_t package_size) { |
| 548 | std::vector<Certificate> loadedKeys; |
| 549 | if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { |
| 550 | LOGE("Failed to load keys\n"); |
| 551 | return false; |
| 552 | } |
| 553 | LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE); |
| 554 | |
| 555 | // Verify package. |
| 556 | ui->Print("Verifying update package...\n"); |
| 557 | auto t0 = std::chrono::system_clock::now(); |
| 558 | int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys); |
| 559 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; |
| 560 | ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); |
| 561 | if (err != VERIFY_SUCCESS) { |
| 562 | LOGE("Signature verification failed\n"); |
| 563 | LOGE("error: %d\n", kZipVerificationFailure); |
| 564 | return false; |
| 565 | } |
| 566 | return true; |
| 567 | } |