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