diff options
Diffstat (limited to 'install.cpp')
-rw-r--r-- | install.cpp | 120 |
1 files changed, 106 insertions, 14 deletions
diff --git a/install.cpp b/install.cpp index fa7b73e56..6c8d827c2 100644 --- a/install.cpp +++ b/install.cpp @@ -23,9 +23,16 @@ #include <sys/wait.h> #include <unistd.h> +#include <chrono> +#include <string> #include <vector> +#include <android-base/parseint.h> +#include <android-base/stringprintf.h> +#include <android-base/strings.h> + #include "common.h" +#include "error_code.h" #include "install.h" #include "minui/minui.h" #include "minzip/SysUtil.h" @@ -38,6 +45,7 @@ extern RecoveryUI* ui; #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" #define PUBLIC_KEYS_FILE "/res/keys" +static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; // Default allocation of progress bar segments to operations static const int VERIFICATION_PROGRESS_TIME = 60; @@ -45,9 +53,64 @@ static const float VERIFICATION_PROGRESS_FRACTION = 0.25; static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; +// This function parses and returns the build.version.incremental +static int parse_build_number(std::string str) { + size_t pos = str.find("="); + if (pos != std::string::npos) { + std::string num_string = android::base::Trim(str.substr(pos+1)); + int build_number; + if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { + return build_number; + } + } + + LOGE("Failed to parse build number in %s.\n", str.c_str()); + return -1; +} + +// Read the build.version.incremental of src/tgt from the metadata and log it to last_install. +static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { + const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH); + if (meta_entry == nullptr) { + LOGE("Failed to find %s in update package.\n", METADATA_PATH); + return; + } + + std::string meta_data(meta_entry->uncompLen, '\0'); + if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) { + LOGE("Failed to read metadata in update package.\n"); + return; + } + + // Examples of the pre-build and post-build strings in metadata: + // pre-build-incremental=2943039 + // post-build-incremental=2951741 + std::vector<std::string> lines = android::base::Split(meta_data, "\n"); + for (const std::string& line : lines) { + std::string str = android::base::Trim(line); + if (android::base::StartsWith(str, "pre-build-incremental")){ + int source_build = parse_build_number(str); + if (source_build != -1) { + log_buffer.push_back(android::base::StringPrintf("source_build: %d", + source_build)); + } + } else if (android::base::StartsWith(str, "post-build-incremental")) { + int target_build = parse_build_number(str); + if (target_build != -1) { + log_buffer.push_back(android::base::StringPrintf("target_build: %d", + target_build)); + } + } + } +} + // If the package contains an update binary, extract it and run it. static int -try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { +try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, + std::vector<std::string>& log_buffer, int retry_count) +{ + read_source_target_build(zip, log_buffer); + const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { @@ -120,15 +183,19 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { // // - the name of the package zip file. // + // - an optional argument "retry" if this update is a retry of a failed + // update attempt. + // - const char* args[5]; + const char* args[6]; args[0] = binary; args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk char temp[16]; snprintf(temp, sizeof(temp), "%d", pipefd[1]); args[2] = temp; args[3] = path; - args[4] = NULL; + args[4] = retry_count > 0 ? "retry" : NULL; + args[5] = NULL; pid_t pid = fork(); if (pid == 0) { @@ -180,6 +247,10 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { ui->SetEnableReboot(true); } else if (strcmp(command, "retry_update") == 0) { retry_update = true; + } else if (strcmp(command, "log") == 0) { + // Save the logging request from updater and write to + // last_install later. + log_buffer.push_back(std::string(strtok(NULL, "\n"))); } else { LOGE("unknown command [%s]\n", command); } @@ -200,7 +271,8 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { } static int -really_install_package(const char *path, bool* wipe_cache, bool needs_mount) +really_install_package(const char *path, bool* wipe_cache, bool needs_mount, + std::vector<std::string>& log_buffer, int retry_count) { ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); ui->Print("Finding update package...\n"); @@ -226,6 +298,7 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount) return INSTALL_CORRUPT; } + // Load keys. std::vector<Certificate> loadedKeys; if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { LOGE("Failed to load keys\n"); @@ -234,31 +307,38 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount) } LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE); + // Verify package. ui->Print("Verifying update package...\n"); - + auto t0 = std::chrono::system_clock::now(); int err = verify_file(map.addr, map.length, loadedKeys); - LOGI("verify_file returned %d\n", err); + std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; + ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); if (err != VERIFY_SUCCESS) { LOGE("signature verification failed\n"); + log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); + sysReleaseMap(&map); return INSTALL_CORRUPT; } - /* Try to open the package. - */ + // Try to open the package. ZipArchive zip; err = mzOpenZipArchive(map.addr, map.length, &zip); if (err != 0) { LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); + log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); + sysReleaseMap(&map); return INSTALL_CORRUPT; } - /* Verify and install the contents of the package. - */ + // Verify and install the contents of the package. ui->Print("Installing update...\n"); + if (retry_count > 0) { + ui->Print("Retry attempt: %d\n", retry_count); + } ui->SetEnableReboot(false); - int result = try_update_binary(path, &zip, wipe_cache); + int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count); ui->SetEnableReboot(true); ui->Print("\n"); @@ -269,9 +349,10 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount) int install_package(const char* path, bool* wipe_cache, const char* install_file, - bool needs_mount) + bool needs_mount, int retry_count) { modified_flash = true; + auto start = std::chrono::system_clock::now(); FILE* install_log = fopen_path(install_file, "w"); if (install_log) { @@ -281,15 +362,26 @@ install_package(const char* path, bool* wipe_cache, const char* install_file, LOGE("failed to open last_install: %s\n", strerror(errno)); } int result; + std::vector<std::string> log_buffer; if (setup_install_mounts() != 0) { LOGE("failed to set up expected mounts for install; aborting\n"); result = INSTALL_ERROR; } else { - result = really_install_package(path, wipe_cache, needs_mount); + result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count); } - if (install_log) { + if (install_log != nullptr) { fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log); fputc('\n', install_log); + std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; + int count = static_cast<int>(duration.count()); + // Report the time spent to apply OTA update in seconds. + fprintf(install_log, "time_total: %d\n", count); + fprintf(install_log, "retry: %d\n", retry_count); + + for (const auto& s : log_buffer) { + fprintf(install_log, "%s\n", s.c_str()); + } + fclose(install_log); } return result; |