bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [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 | |
| 17 | #include "twinstall/install.h" |
| 18 | |
| 19 | #include <ctype.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <limits.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <atomic> |
| 31 | #include <chrono> |
| 32 | #include <condition_variable> |
| 33 | #include <functional> |
| 34 | #include <limits> |
| 35 | #include <mutex> |
| 36 | #include <thread> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include <android-base/file.h> |
| 40 | #include <android-base/logging.h> |
| 41 | #include <android-base/parsedouble.h> |
| 42 | #include <android-base/parseint.h> |
| 43 | #include <android-base/properties.h> |
| 44 | #include <android-base/stringprintf.h> |
| 45 | #include <android-base/strings.h> |
| 46 | #include <android-base/unique_fd.h> |
| 47 | #include <vintf/VintfObjectRecovery.h> |
| 48 | |
| 49 | #include "twinstall/package.h" |
| 50 | #include "twinstall/verifier.h" |
| 51 | #include "twinstall/wipe_data.h" |
| 52 | #include "otautil/error_code.h" |
| 53 | #include "otautil/paths.h" |
| 54 | #include "otautil/roots.h" |
| 55 | #include "otautil/sysutil.h" |
| 56 | #include "otautil/thermalutil.h" |
| 57 | #include "private/setup_commands.h" |
| 58 | |
| 59 | using namespace std::chrono_literals; |
| 60 | |
| 61 | static constexpr int kRecoveryApiVersion = 3; |
| 62 | // Assert the version defined in code and in Android.mk are consistent. |
| 63 | static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); |
| 64 | |
| 65 | // Default allocation of progress bar segments to operations |
| 66 | // static constexpr int VERIFICATION_PROGRESS_TIME = 60; |
| 67 | // static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 68 | |
| 69 | static std::condition_variable finish_log_temperature; |
| 70 | |
| 71 | bool ReadMetadataFromPackage(ZipArchiveHandle zip, std::map<std::string, std::string>* metadata) { |
| 72 | CHECK(metadata != nullptr); |
| 73 | |
| 74 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
| 75 | ZipString path(METADATA_PATH); |
| 76 | ZipEntry entry; |
| 77 | if (FindEntry(zip, path, &entry) != 0) { |
| 78 | LOG(ERROR) << "Failed to find " << METADATA_PATH; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | uint32_t length = entry.uncompressed_length; |
| 83 | std::string metadata_string(length, '\0'); |
| 84 | int32_t err = |
| 85 | ExtractToMemory(zip, &entry, reinterpret_cast<uint8_t*>(&metadata_string[0]), length); |
| 86 | if (err != 0) { |
| 87 | LOG(ERROR) << "Failed to extract " << METADATA_PATH << ": " << ErrorCodeString(err); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | for (const std::string& line : android::base::Split(metadata_string, "\n")) { |
| 92 | size_t eq = line.find('='); |
| 93 | if (eq != std::string::npos) { |
| 94 | metadata->emplace(android::base::Trim(line.substr(0, eq)), |
| 95 | android::base::Trim(line.substr(eq + 1))); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Gets the value for the given key in |metadata|. Returns an emtpy string if the key isn't |
| 103 | // present. |
| 104 | static std::string get_value(const std::map<std::string, std::string>& metadata, |
| 105 | const std::string& key) { |
| 106 | const auto& it = metadata.find(key); |
| 107 | return (it == metadata.end()) ? "" : it->second; |
| 108 | } |
| 109 | |
| 110 | static std::string OtaTypeToString(OtaType type) { |
| 111 | switch (type) { |
| 112 | case OtaType::AB: |
| 113 | return "AB"; |
| 114 | case OtaType::BLOCK: |
| 115 | return "BLOCK"; |
| 116 | case OtaType::BRICK: |
| 117 | return "BRICK"; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
| 122 | static void ReadSourceTargetBuild(const std::map<std::string, std::string>& metadata, |
| 123 | std::vector<std::string>* log_buffer) { |
| 124 | // Examples of the pre-build and post-build strings in metadata: |
| 125 | // pre-build-incremental=2943039 |
| 126 | // post-build-incremental=2951741 |
| 127 | auto source_build = get_value(metadata, "pre-build-incremental"); |
| 128 | if (!source_build.empty()) { |
| 129 | log_buffer->push_back("source_build: " + source_build); |
| 130 | } |
| 131 | |
| 132 | auto target_build = get_value(metadata, "post-build-incremental"); |
| 133 | if (!target_build.empty()) { |
| 134 | log_buffer->push_back("target_build: " + target_build); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Checks the build version, fingerprint and timestamp in the metadata of the A/B package. |
| 139 | // Downgrading is not allowed unless explicitly enabled in the package and only for |
| 140 | // incremental packages. |
| 141 | static int CheckAbSpecificMetadata(const std::map<std::string, std::string>& metadata) { |
| 142 | // Incremental updates should match the current build. |
| 143 | auto device_pre_build = android::base::GetProperty("ro.build.version.incremental", ""); |
| 144 | auto pkg_pre_build = get_value(metadata, "pre-build-incremental"); |
| 145 | if (!pkg_pre_build.empty() && pkg_pre_build != device_pre_build) { |
| 146 | LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " |
| 147 | << device_pre_build; |
| 148 | return INSTALL_ERROR; |
| 149 | } |
| 150 | |
| 151 | auto device_fingerprint = android::base::GetProperty("ro.build.fingerprint", ""); |
| 152 | auto pkg_pre_build_fingerprint = get_value(metadata, "pre-build"); |
| 153 | if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != device_fingerprint) { |
| 154 | LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected " |
| 155 | << device_fingerprint; |
| 156 | return INSTALL_ERROR; |
| 157 | } |
| 158 | |
| 159 | // Check for downgrade version. |
| 160 | // int64_t build_timestamp = |
| 161 | // android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max()); |
| 162 | // int64_t pkg_post_timestamp = 0; |
| 163 | // We allow to full update to the same version we are running, in case there |
| 164 | // is a problem with the current copy of that version. |
| 165 | auto pkg_post_timestamp_string = get_value(metadata, "post-timestamp"); |
| 166 | // if (pkg_post_timestamp_string.empty() || |
| 167 | // !android::base::ParseInt(pkg_post_timestamp_string, &pkg_post_timestamp) || |
| 168 | // pkg_post_timestamp < build_timestamp) { |
| 169 | // if (get_value(metadata, "ota-downgrade") != "yes") { |
| 170 | // LOG(ERROR) << "Update package is older than the current build, expected a build " |
| 171 | // "newer than timestamp " |
| 172 | // << build_timestamp << " but package has timestamp " << pkg_post_timestamp |
| 173 | // << " and downgrade not allowed."; |
| 174 | // return INSTALL_ERROR; |
| 175 | // } |
| 176 | // if (pkg_pre_build_fingerprint.empty()) { |
| 177 | // LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed."; |
| 178 | // return INSTALL_ERROR; |
| 179 | // } |
| 180 | // } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int CheckPackageMetadata(const std::map<std::string, std::string>& metadata, OtaType ota_type) { |
| 186 | auto package_ota_type = get_value(metadata, "ota-type"); |
| 187 | auto expected_ota_type = OtaTypeToString(ota_type); |
| 188 | if (ota_type != OtaType::AB && ota_type != OtaType::BRICK) { |
| 189 | LOG(INFO) << "Skip package metadata check for ota type " << expected_ota_type; |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | if (package_ota_type != expected_ota_type) { |
| 194 | LOG(ERROR) << "Unexpected ota package type, expects " << expected_ota_type << ", actual " |
| 195 | << package_ota_type; |
| 196 | return INSTALL_ERROR; |
| 197 | } |
| 198 | |
| 199 | auto device = android::base::GetProperty("ro.product.device", ""); |
| 200 | auto pkg_device = get_value(metadata, "pre-device"); |
| 201 | if (pkg_device != device || pkg_device.empty()) { |
| 202 | LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << device; |
| 203 | return INSTALL_ERROR; |
| 204 | } |
| 205 | |
| 206 | // We allow the package to not have any serialno; and we also allow it to carry multiple serial |
| 207 | // numbers split by "|"; e.g. serialno=serialno1|serialno2|serialno3 ... We will fail the |
| 208 | // verification if the device's serialno doesn't match any of these carried numbers. |
| 209 | auto pkg_serial_no = get_value(metadata, "serialno"); |
| 210 | if (!pkg_serial_no.empty()) { |
| 211 | auto device_serial_no = android::base::GetProperty("ro.serialno", ""); |
| 212 | bool serial_number_match = false; |
| 213 | for (const auto& number : android::base::Split(pkg_serial_no, "|")) { |
| 214 | if (device_serial_no == android::base::Trim(number)) { |
| 215 | serial_number_match = true; |
| 216 | } |
| 217 | } |
| 218 | if (!serial_number_match) { |
| 219 | LOG(ERROR) << "Package is for serial " << pkg_serial_no; |
| 220 | return INSTALL_ERROR; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (ota_type == OtaType::AB) { |
| 225 | return CheckAbSpecificMetadata(metadata); |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int SetUpAbUpdateCommands(const std::string& package, ZipArchiveHandle zip, int status_fd, |
| 232 | std::vector<std::string>* cmd) { |
| 233 | CHECK(cmd != nullptr); |
| 234 | |
| 235 | // For A/B updates we extract the payload properties to a buffer and obtain the RAW payload offset |
| 236 | // in the zip file. |
| 237 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 238 | ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES); |
| 239 | ZipEntry properties_entry; |
| 240 | if (FindEntry(zip, property_name, &properties_entry) != 0) { |
| 241 | LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD_PROPERTIES; |
| 242 | return INSTALL_CORRUPT; |
| 243 | } |
| 244 | uint32_t properties_entry_length = properties_entry.uncompressed_length; |
| 245 | std::vector<uint8_t> payload_properties(properties_entry_length); |
| 246 | int32_t err = |
| 247 | ExtractToMemory(zip, &properties_entry, payload_properties.data(), properties_entry_length); |
| 248 | if (err != 0) { |
| 249 | LOG(ERROR) << "Failed to extract " << AB_OTA_PAYLOAD_PROPERTIES << ": " << ErrorCodeString(err); |
| 250 | return INSTALL_CORRUPT; |
| 251 | } |
| 252 | |
| 253 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
| 254 | ZipString payload_name(AB_OTA_PAYLOAD); |
| 255 | ZipEntry payload_entry; |
| 256 | if (FindEntry(zip, payload_name, &payload_entry) != 0) { |
| 257 | LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD; |
| 258 | return INSTALL_CORRUPT; |
| 259 | } |
| 260 | long payload_offset = payload_entry.offset; |
| 261 | *cmd = { |
| 262 | "/system/bin/update_engine_sideload", |
| 263 | "--payload=file://" + package, |
| 264 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 265 | "--headers=" + std::string(payload_properties.begin(), payload_properties.end()), |
| 266 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 267 | }; |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int SetUpNonAbUpdateCommands(const std::string& package, ZipArchiveHandle zip, int retry_count, |
| 272 | int status_fd, std::vector<std::string>* cmd) { |
| 273 | CHECK(cmd != nullptr); |
| 274 | |
| 275 | // In non-A/B updates we extract the update binary from the package. |
| 276 | ZipString binary_name(UPDATE_BINARY_NAME); |
| 277 | ZipEntry binary_entry; |
| 278 | if (FindEntry(zip, binary_name, &binary_entry) != 0) { |
| 279 | LOG(ERROR) << "Failed to find update binary " << UPDATE_BINARY_NAME; |
| 280 | return INSTALL_CORRUPT; |
| 281 | } |
| 282 | |
| 283 | LOG(ERROR) << "SetupNonAbUpdateCommands::here1"; |
| 284 | const std::string binary_path = Paths::Get().temporary_update_binary(); |
| 285 | unlink(binary_path.c_str()); |
| 286 | LOG(ERROR) << "SetupNonAbUpdateCommands::here2"; |
| 287 | android::base::unique_fd fd( |
| 288 | open(binary_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0755)); |
| 289 | if (fd == -1) { |
| 290 | PLOG(ERROR) << "Failed to create " << binary_path; |
| 291 | return INSTALL_ERROR; |
| 292 | } |
| 293 | LOG(ERROR) << "SetupNonAbUpdateCommands::here3"; |
| 294 | |
| 295 | int32_t error = ExtractEntryToFile(zip, &binary_entry, fd); |
| 296 | if (error != 0) { |
| 297 | LOG(ERROR) << "Failed to extract " << UPDATE_BINARY_NAME << ": " << ErrorCodeString(error); |
| 298 | return INSTALL_ERROR; |
| 299 | } |
| 300 | LOG(ERROR) << "SetupNonAbUpdateCommands::here4"; |
| 301 | |
| 302 | // When executing the update binary contained in the package, the arguments passed are: |
| 303 | // - the version number for this interface |
| 304 | // - an FD to which the program can write in order to update the progress bar. |
| 305 | // - the name of the package zip file. |
| 306 | // - an optional argument "retry" if this update is a retry of a failed update attempt. |
| 307 | *cmd = { |
| 308 | binary_path, |
| 309 | std::to_string(kRecoveryApiVersion), |
| 310 | std::to_string(status_fd), |
| 311 | package, |
| 312 | }; |
| 313 | if (retry_count > 0) { |
| 314 | cmd->push_back("retry"); |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static void log_max_temperature(int* max_temperature, const std::atomic<bool>& logger_finished) { |
| 320 | CHECK(max_temperature != nullptr); |
| 321 | std::mutex mtx; |
| 322 | std::unique_lock<std::mutex> lck(mtx); |
| 323 | while (!logger_finished.load() && |
| 324 | finish_log_temperature.wait_for(lck, 20s) == std::cv_status::timeout) { |
| 325 | *max_temperature = std::max(*max_temperature, GetMaxValueFromThermalZone()); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // If the package contains an update binary, extract it and run it. |
| 330 | static int try_update_binary(const std::string& package, ZipArchiveHandle zip, bool* wipe_cache, |
| 331 | std::vector<std::string>* log_buffer, int retry_count, |
| 332 | int* max_temperature) { |
| 333 | std::map<std::string, std::string> metadata; |
| 334 | |
| 335 | if (!ReadMetadataFromPackage(zip, &metadata)) { |
| 336 | LOG(ERROR) << "Failed to parse metadata in the zip file"; |
| 337 | // return INSTALL_CORRUPT; |
| 338 | } |
| 339 | |
| 340 | bool is_ab = android::base::GetBoolProperty("ro.build.ab_update", false); |
| 341 | // Verifies against the metadata in the package first. |
| 342 | // if (int check_status = is_ab ? CheckPackageMetadata(metadata, OtaType::AB) : 0; |
| 343 | // check_status != 0) { |
| 344 | // log_buffer->push_back(android::base::StringPrintf("error: %d", kUpdateBinaryCommandFailure)); |
| 345 | // return check_status; |
| 346 | // } |
| 347 | |
| 348 | ReadSourceTargetBuild(metadata, log_buffer); |
| 349 | LOG(ERROR) << "try_update_binary::here1"; |
| 350 | // The updater in child process writes to the pipe to communicate with recovery. |
| 351 | android::base::unique_fd pipe_read, pipe_write; |
| 352 | // Explicitly disable O_CLOEXEC using 0 as the flags (last) parameter to Pipe |
| 353 | // so that the child updater process will recieve a non-closed fd. |
| 354 | if (!android::base::Pipe(&pipe_read, &pipe_write, 0)) { |
| 355 | PLOG(ERROR) << "Failed to create pipe for updater-recovery communication"; |
| 356 | return INSTALL_CORRUPT; |
| 357 | } |
| 358 | |
| 359 | // The updater-recovery communication protocol. |
| 360 | // |
| 361 | // progress <frac> <secs> |
| 362 | // fill up the next <frac> part of of the progress bar over <secs> seconds. If <secs> is |
| 363 | // zero, use `set_progress` commands to manually control the progress of this segment of the |
| 364 | // bar. |
| 365 | // |
| 366 | // set_progress <frac> |
| 367 | // <frac> should be between 0.0 and 1.0; sets the progress bar within the segment defined by |
| 368 | // the most recent progress command. |
| 369 | // |
| 370 | // ui_print <string> |
| 371 | // display <string> on the screen. |
| 372 | // |
| 373 | // wipe_cache |
| 374 | // a wipe of cache will be performed following a successful installation. |
| 375 | // |
| 376 | // clear_display |
| 377 | // turn off the text display. |
| 378 | // |
| 379 | // enable_reboot |
| 380 | // packages can explicitly request that they want the user to be able to reboot during |
| 381 | // installation (useful for debugging packages that don't exit). |
| 382 | // |
| 383 | // retry_update |
| 384 | // updater encounters some issue during the update. It requests a reboot to retry the same |
| 385 | // package automatically. |
| 386 | // |
| 387 | // log <string> |
| 388 | // updater requests logging the string (e.g. cause of the failure). |
| 389 | // |
| 390 | |
| 391 | std::vector<std::string> args; |
| 392 | |
| 393 | is_ab = false; |
| 394 | ZipString binary_name(UPDATE_BINARY_NAME); |
| 395 | ZipEntry binary_entry; |
| 396 | if (FindEntry(zip, binary_name, &binary_entry) != 0) { |
| 397 | LOG(ERROR) << "Failed to find update binary " << UPDATE_BINARY_NAME; |
| 398 | is_ab = true; |
| 399 | } |
| 400 | |
| 401 | if (int update_status = |
| 402 | is_ab ? SetUpAbUpdateCommands(package, zip, pipe_write.get(), &args) |
| 403 | : SetUpNonAbUpdateCommands(package, zip, retry_count, pipe_write.get(), &args); |
| 404 | update_status != 0) { |
| 405 | log_buffer->push_back(android::base::StringPrintf("error: %d", kUpdateBinaryCommandFailure)); |
| 406 | // return update_status; |
| 407 | } |
| 408 | |
| 409 | pid_t pid = fork(); |
| 410 | if (pid == -1) { |
| 411 | PLOG(ERROR) << "Failed to fork update binary"; |
| 412 | log_buffer->push_back(android::base::StringPrintf("error: %d", kForkUpdateBinaryFailure)); |
| 413 | return INSTALL_ERROR; |
| 414 | } |
| 415 | |
| 416 | if (pid == 0) { |
| 417 | umask(022); |
| 418 | pipe_read.reset(); |
| 419 | |
| 420 | // Convert the std::string vector to a NULL-terminated char* vector suitable for execv. |
| 421 | auto chr_args = StringVectorToNullTerminatedArray(args); |
| 422 | execv(chr_args[0], chr_args.data()); |
| 423 | // We shouldn't use LOG/PLOG in the forked process, since they may cause the child process to |
| 424 | // hang. This deadlock results from an improperly copied mutex in the ui functions. |
| 425 | // (Bug: 34769056) |
| 426 | fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno)); |
| 427 | _exit(EXIT_FAILURE); |
| 428 | } |
| 429 | pipe_write.reset(); |
| 430 | |
| 431 | std::atomic<bool> logger_finished(false); |
| 432 | std::thread temperature_logger(log_max_temperature, max_temperature, std::ref(logger_finished)); |
| 433 | |
| 434 | *wipe_cache = false; |
| 435 | bool retry_update = false; |
| 436 | |
| 437 | char buffer[1024]; |
| 438 | FILE* from_child = android::base::Fdopen(std::move(pipe_read), "r"); |
| 439 | while (fgets(buffer, sizeof(buffer), from_child) != nullptr) { |
| 440 | std::string line(buffer); |
| 441 | size_t space = line.find_first_of(" \n"); |
| 442 | std::string command(line.substr(0, space)); |
| 443 | if (command.empty()) continue; |
| 444 | |
| 445 | // Get rid of the leading and trailing space and/or newline. |
| 446 | std::string args = space == std::string::npos ? "" : android::base::Trim(line.substr(space)); |
| 447 | |
| 448 | if (command == "progress") { |
| 449 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 450 | double fraction; |
| 451 | int seconds; |
| 452 | if (tokens.size() == 2 && android::base::ParseDouble(tokens[0].c_str(), &fraction) && |
| 453 | android::base::ParseInt(tokens[1], &seconds)) { |
| 454 | // ui->ShowProgress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), seconds); |
| 455 | } else { |
| 456 | LOG(ERROR) << "invalid \"progress\" parameters: " << line; |
| 457 | } |
| 458 | } else if (command == "set_progress") { |
| 459 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 460 | double fraction; |
| 461 | if (tokens.size() == 1 && android::base::ParseDouble(tokens[0].c_str(), &fraction)) { |
| 462 | // ui->SetProgress(fraction); |
| 463 | } else { |
| 464 | LOG(ERROR) << "invalid \"set_progress\" parameters: " << line; |
| 465 | } |
| 466 | } else if (command == "ui_print") { |
| 467 | // ui->PrintOnScreenOnly("%s\n", args.c_str()); |
| 468 | fflush(stdout); |
| 469 | } else if (command == "wipe_cache") { |
| 470 | *wipe_cache = true; |
| 471 | } else if (command == "clear_display") { |
| 472 | // ui->SetBackground(RecoveryUI::NONE); |
| 473 | } else if (command == "enable_reboot") { |
| 474 | // packages can explicitly request that they want the user |
| 475 | // to be able to reboot during installation (useful for |
| 476 | // debugging packages that don't exit). |
| 477 | // ui->SetEnableReboot(true); |
| 478 | } else if (command == "retry_update") { |
| 479 | retry_update = true; |
| 480 | } else if (command == "log") { |
| 481 | if (!args.empty()) { |
| 482 | // Save the logging request from updater and write to last_install later. |
| 483 | log_buffer->push_back(args); |
| 484 | } else { |
| 485 | LOG(ERROR) << "invalid \"log\" parameters: " << line; |
| 486 | } |
| 487 | } else { |
| 488 | LOG(ERROR) << "unknown command [" << command << "]"; |
| 489 | } |
| 490 | } |
| 491 | fclose(from_child); |
| 492 | |
| 493 | int status; |
| 494 | waitpid(pid, &status, 0); |
| 495 | |
| 496 | logger_finished.store(true); |
| 497 | finish_log_temperature.notify_one(); |
| 498 | temperature_logger.join(); |
| 499 | |
| 500 | if (retry_update) { |
| 501 | return INSTALL_RETRY; |
| 502 | } |
| 503 | if (WIFEXITED(status)) { |
| 504 | if (WEXITSTATUS(status) != EXIT_SUCCESS) { |
| 505 | LOG(ERROR) << "Error in " << package << " (status " << WEXITSTATUS(status) << ")"; |
| 506 | return INSTALL_ERROR; |
| 507 | } |
| 508 | } else if (WIFSIGNALED(status)) { |
| 509 | LOG(ERROR) << "Error in " << package << " (killed by signal " << WTERMSIG(status) << ")"; |
| 510 | return INSTALL_ERROR; |
| 511 | } else { |
| 512 | LOG(FATAL) << "Invalid status code " << status; |
| 513 | } |
| 514 | |
| 515 | return INSTALL_SUCCESS; |
| 516 | } |
| 517 | |
| 518 | // Verifes the compatibility info in a Treble-compatible package. Returns true directly if the |
| 519 | // entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA |
| 520 | // package. |
| 521 | // bool verify_package_compatibility(ZipArchiveHandle package_zip __unused) { |
| 522 | // LOG(INFO) << "Verifying package compatibility..."; |
| 523 | |
| 524 | // static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip"; |
| 525 | // ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY); |
| 526 | // ZipEntry compatibility_entry; |
| 527 | // if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) { |
| 528 | // LOG(INFO) << "Package doesn't contain " << COMPATIBILITY_ZIP_ENTRY << " entry"; |
| 529 | // return true; |
| 530 | // } |
| 531 | |
| 532 | // std::string zip_content(compatibility_entry.uncompressed_length, '\0'); |
| 533 | // int32_t ret; |
| 534 | // if ((ret = ExtractToMemory(package_zip, &compatibility_entry, |
| 535 | // reinterpret_cast<uint8_t*>(&zip_content[0]), |
| 536 | // compatibility_entry.uncompressed_length)) != 0) { |
| 537 | // LOG(ERROR) << "Failed to read " << COMPATIBILITY_ZIP_ENTRY << ": " << ErrorCodeString(ret); |
| 538 | // return false; |
| 539 | // } |
| 540 | |
| 541 | // ZipArchiveHandle zip_handle; |
| 542 | // ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())), |
| 543 | // zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle); |
| 544 | // if (ret != 0) { |
| 545 | // LOG(ERROR) << "Failed to OpenArchiveFromMemory: " << ErrorCodeString(ret); |
| 546 | // return false; |
| 547 | // } |
| 548 | |
| 549 | // // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents. |
| 550 | // void* cookie; |
| 551 | // ret = StartIteration(zip_handle, &cookie, nullptr, nullptr); |
| 552 | // if (ret != 0) { |
| 553 | // LOG(ERROR) << "Failed to start iterating zip entries: " << ErrorCodeString(ret); |
| 554 | // CloseArchive(zip_handle); |
| 555 | // return false; |
| 556 | // } |
| 557 | // std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 558 | |
| 559 | // std::vector<std::string> compatibility_info; |
| 560 | // ZipEntry info_entry; |
| 561 | // ZipString info_name; |
| 562 | // while (Next(cookie, &info_entry, &info_name) == 0) { |
| 563 | // std::string content(info_entry.uncompressed_length, '\0'); |
| 564 | // int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]), |
| 565 | // info_entry.uncompressed_length); |
| 566 | // if (ret != 0) { |
| 567 | // LOG(ERROR) << "Failed to read " << info_name.name << ": " << ErrorCodeString(ret); |
| 568 | // CloseArchive(zip_handle); |
| 569 | // return false; |
| 570 | // } |
| 571 | // compatibility_info.emplace_back(std::move(content)); |
| 572 | // } |
| 573 | // CloseArchive(zip_handle); |
| 574 | |
| 575 | // // VintfObjectRecovery::CheckCompatibility returns zero on success. |
| 576 | // std::string err; |
| 577 | // int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err); |
| 578 | // if (result == 0) { |
| 579 | // return true; |
| 580 | // } |
| 581 | |
| 582 | // LOG(ERROR) << "Failed to verify package compatibility (result " << result << "): " << err; |
| 583 | // return false; |
| 584 | // } |
| 585 | |
| 586 | static int really_install_package(const std::string& path, bool* wipe_cache __unused, bool needs_mount __unused, |
| 587 | std::vector<std::string>* log_buffer __unused, int retry_count __unused, |
| 588 | int* max_temperature __unused) { |
| 589 | // ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 590 | // ui->Print("Finding update package...\n"); |
| 591 | // Give verification half the progress bar... |
| 592 | // ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 593 | // ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
| 594 | LOG(INFO) << "Update location: " << path; |
| 595 | |
| 596 | // Map the update package into memory. |
| 597 | // ui->Print("Opening update package...\n"); |
| 598 | if (needs_mount) { |
| 599 | if (path[0] == '@') { |
| 600 | ensure_path_mounted(path.substr(1)); |
| 601 | } else { |
| 602 | ensure_path_mounted(path); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | auto package = Package::CreateMemoryPackage( |
| 607 | path); |
| 608 | if (!package) { |
| 609 | log_buffer->push_back(android::base::StringPrintf("error: %d", kMapFileFailure)); |
| 610 | return INSTALL_CORRUPT; |
| 611 | } |
| 612 | |
| 613 | // Verify package. |
| 614 | if (!verify_package(package.get())) { |
| 615 | log_buffer->push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); |
| 616 | return INSTALL_CORRUPT; |
| 617 | } |
| 618 | |
| 619 | // Try to open the package. |
| 620 | ZipArchiveHandle zip = package->GetZipArchiveHandle(); |
| 621 | if (!zip) { |
| 622 | log_buffer->push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); |
| 623 | return INSTALL_CORRUPT; |
| 624 | } |
| 625 | LOG(ERROR) << "really_install_package::here1"; |
| 626 | |
| 627 | // Additionally verify the compatibility of the package if it's a fresh install. |
| 628 | if (retry_count == 0 && !verify_package_compatibility(zip)) { |
| 629 | log_buffer->push_back(android::base::StringPrintf("error: %d", kPackageCompatibilityFailure)); |
| 630 | return INSTALL_CORRUPT; |
| 631 | } |
| 632 | |
| 633 | // Verify and install the contents of the package. |
| 634 | // ui->Print("Installing update...\n"); |
| 635 | // if (retry_count > 0) { |
| 636 | // ui->Print("Retry attempt: %d\n", retry_count); |
| 637 | // } |
| 638 | // ui->SetEnableReboot(false); |
| 639 | LOG(ERROR) << "really_install_package::here2"; |
| 640 | int result = |
| 641 | try_update_binary(path, zip, wipe_cache, log_buffer, retry_count, max_temperature); |
| 642 | // ui->SetEnableReboot(true); |
| 643 | // ui->Print("\n"); |
| 644 | return result; |
| 645 | } |
| 646 | |
| 647 | int install_package(const std::string& path, bool should_wipe_cache, bool needs_mount, |
| 648 | int retry_count) { |
| 649 | CHECK(!path.empty()); |
| 650 | |
| 651 | auto start = std::chrono::system_clock::now(); |
| 652 | |
| 653 | int start_temperature = GetMaxValueFromThermalZone(); |
| 654 | int max_temperature = start_temperature; |
| 655 | |
| 656 | int result; |
| 657 | std::vector<std::string> log_buffer; |
| 658 | // if (setup_install_mounts() != 0) { |
| 659 | // LOG(ERROR) << "failed to set up expected mounts for install; aborting"; |
| 660 | // result = INSTALL_ERROR; |
| 661 | // } else { |
| 662 | bool updater_wipe_cache = false; |
| 663 | result = really_install_package(path, &updater_wipe_cache, needs_mount, &log_buffer, |
| 664 | retry_count, &max_temperature); |
| 665 | should_wipe_cache = should_wipe_cache || updater_wipe_cache; |
| 666 | // } |
| 667 | |
| 668 | // Measure the time spent to apply OTA update in seconds. |
| 669 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; |
| 670 | int time_total = static_cast<int>(duration.count()); |
| 671 | |
| 672 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 673 | // Skip logging the uncrypt_status on devices without /cache. |
| 674 | if (has_cache) { |
| 675 | static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; |
| 676 | if (ensure_path_mounted(UNCRYPT_STATUS) != 0) { |
| 677 | LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS; |
| 678 | } else { |
| 679 | std::string uncrypt_status; |
| 680 | if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) { |
| 681 | PLOG(WARNING) << "failed to read uncrypt status"; |
| 682 | } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) { |
| 683 | LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status; |
| 684 | } else { |
| 685 | log_buffer.push_back(android::base::Trim(uncrypt_status)); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // The first two lines need to be the package name and install result. |
| 691 | std::vector<std::string> log_header = { |
| 692 | path, |
| 693 | result == INSTALL_SUCCESS ? "1" : "0", |
| 694 | "time_total: " + std::to_string(time_total), |
| 695 | "retry: " + std::to_string(retry_count), |
| 696 | }; |
| 697 | |
| 698 | int end_temperature = GetMaxValueFromThermalZone(); |
| 699 | max_temperature = std::max(end_temperature, max_temperature); |
| 700 | if (start_temperature > 0) { |
| 701 | log_buffer.push_back("temperature_start: " + std::to_string(start_temperature)); |
| 702 | } |
| 703 | if (end_temperature > 0) { |
| 704 | log_buffer.push_back("temperature_end: " + std::to_string(end_temperature)); |
| 705 | } |
| 706 | if (max_temperature > 0) { |
| 707 | log_buffer.push_back("temperature_max: " + std::to_string(max_temperature)); |
| 708 | } |
| 709 | |
| 710 | std::string log_content = |
| 711 | android::base::Join(log_header, "\n") + "\n" + android::base::Join(log_buffer, "\n") + "\n"; |
| 712 | const std::string& install_file = Paths::Get().temporary_install_file(); |
| 713 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 714 | PLOG(ERROR) << "failed to write " << install_file; |
| 715 | } |
| 716 | |
| 717 | // Write a copy into last_log. |
| 718 | LOG(INFO) << log_content; |
| 719 | |
| 720 | if (result == INSTALL_SUCCESS && should_wipe_cache) { |
| 721 | if (!WipeCache(nullptr)) { |
| 722 | result = INSTALL_ERROR; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | return result; |
| 727 | } |
| 728 | |
| 729 | bool verify_package(Package* package) { |
| 730 | static constexpr const char* CERTIFICATE_ZIP_FILE = "/system/etc/security/otacerts.zip"; |
| 731 | std::vector<Certificate> loaded_keys = LoadKeysFromZipfile(CERTIFICATE_ZIP_FILE); |
| 732 | if (loaded_keys.empty()) { |
| 733 | LOG(ERROR) << "Failed to load keys"; |
| 734 | return false; |
| 735 | } |
| 736 | LOG(INFO) << loaded_keys.size() << " key(s) loaded from " << CERTIFICATE_ZIP_FILE; |
| 737 | |
| 738 | // Verify package. |
| 739 | // ui->Print("Verifying update package...\n"); |
| 740 | // auto t0 = std::chrono::system_clock::now(); |
| 741 | int err = verify_file(package, loaded_keys); |
| 742 | // if(err != VERIFY_SUCCESS) { |
| 743 | |
| 744 | // } |
| 745 | // std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; |
| 746 | // ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); |
| 747 | if (err != VERIFY_SUCCESS) { |
| 748 | LOG(ERROR) << "Signature verification failed"; |
| 749 | LOG(ERROR) << "error: " << kZipVerificationFailure; |
| 750 | // return false; |
| 751 | } |
| 752 | return true; |
| 753 | } |