From a233a89d99b673eccaf072f9d0f452afa1c725c7 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 17 Apr 2017 16:46:05 -0700 Subject: Add tests for update_binary_command(). Expose update_binary_command() through private/install.h for testing purpose. Also make minor clean-ups to install.cpp: a) adding more verbose logging on ExtractToMemory failures; b) update_binary_command() taking std::string instead of const char*; c) moving a few macro and global constants into update_binary_command(). Bug: 37300957 Test: recovery_component_test on marlin Test: Build new recovery and adb sideload on angler and sailfish. Change-Id: Ib2d9068af3fee038f01c90940ccaeb0a7da374fc Merged-In: Ib2d9068af3fee038f01c90940ccaeb0a7da374fc (cherry picked from commit bc4b1fe4c4305ebf0fbfc891b9b508c14b5c8ef8) --- install.cpp | 162 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 80 insertions(+), 82 deletions(-) (limited to 'install.cpp') diff --git a/install.cpp b/install.cpp index 6dcd3565e..4fb809996 100644 --- a/install.cpp +++ b/install.cpp @@ -57,9 +57,6 @@ using namespace std::chrono_literals; -#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" -static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; -static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; #define PUBLIC_KEYS_FILE "/res/keys" static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; @@ -136,13 +133,11 @@ static void read_source_target_build(ZipArchiveHandle zip, std::vector* cmd); +// Extract the update binary from the open zip archive |zip| located at |path| and store into |cmd| +// the command line that should be called. The |status_fd| is the file descriptor the child process +// should use to report back the progress of the update. +int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count, + int status_fd, std::vector* cmd); #ifdef AB_OTA_UPDATER @@ -224,87 +219,90 @@ static int check_newer_ab_build(ZipArchiveHandle zip) { return 0; } -static int -update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, - int status_fd, std::vector* cmd) -{ - int ret = check_newer_ab_build(zip); - if (ret) { - return ret; - } +int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count, + int status_fd, std::vector* cmd) { + CHECK(cmd != nullptr); + int ret = check_newer_ab_build(zip); + if (ret != 0) { + return ret; + } - // For A/B updates we extract the payload properties to a buffer and obtain - // the RAW payload offset in the zip file. - ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES); - ZipEntry properties_entry; - if (FindEntry(zip, property_name, &properties_entry) != 0) { - LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES; - return INSTALL_CORRUPT; - } - std::vector payload_properties( - properties_entry.uncompressed_length); - if (ExtractToMemory(zip, &properties_entry, payload_properties.data(), - properties_entry.uncompressed_length) != 0) { - LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES; - return INSTALL_CORRUPT; - } + // For A/B updates we extract the payload properties to a buffer and obtain the RAW payload offset + // in the zip file. + static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; + ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES); + ZipEntry properties_entry; + if (FindEntry(zip, property_name, &properties_entry) != 0) { + LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD_PROPERTIES; + return INSTALL_CORRUPT; + } + uint32_t properties_entry_length = properties_entry.uncompressed_length; + std::vector payload_properties(properties_entry_length); + int32_t err = + ExtractToMemory(zip, &properties_entry, payload_properties.data(), properties_entry_length); + if (err != 0) { + LOG(ERROR) << "Failed to extract " << AB_OTA_PAYLOAD_PROPERTIES << ": " << ErrorCodeString(err); + return INSTALL_CORRUPT; + } - ZipString payload_name(AB_OTA_PAYLOAD); - ZipEntry payload_entry; - if (FindEntry(zip, payload_name, &payload_entry) != 0) { - LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD; - return INSTALL_CORRUPT; - } - long payload_offset = payload_entry.offset; - *cmd = { - "/sbin/update_engine_sideload", - android::base::StringPrintf("--payload=file://%s", path), - android::base::StringPrintf("--offset=%ld", payload_offset), - "--headers=" + std::string(payload_properties.begin(), - payload_properties.end()), - android::base::StringPrintf("--status_fd=%d", status_fd), - }; - return 0; + static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; + ZipString payload_name(AB_OTA_PAYLOAD); + ZipEntry payload_entry; + if (FindEntry(zip, payload_name, &payload_entry) != 0) { + LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD; + return INSTALL_CORRUPT; + } + long payload_offset = payload_entry.offset; + *cmd = { + "/sbin/update_engine_sideload", + "--payload=file://" + path, + android::base::StringPrintf("--offset=%ld", payload_offset), + "--headers=" + std::string(payload_properties.begin(), payload_properties.end()), + android::base::StringPrintf("--status_fd=%d", status_fd), + }; + return 0; } #else // !AB_OTA_UPDATER -static int -update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, - int status_fd, std::vector* cmd) -{ - // On traditional updates we extract the update binary from the package. - ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME); - ZipEntry binary_entry; - if (FindEntry(zip, binary_name, &binary_entry) != 0) { - return INSTALL_CORRUPT; - } +int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count, + int status_fd, std::vector* cmd) { + CHECK(cmd != nullptr); - const char* binary = "/tmp/update_binary"; - unlink(binary); - int fd = creat(binary, 0755); - if (fd < 0) { - PLOG(ERROR) << "Can't make " << binary; - return INSTALL_ERROR; - } - int error = ExtractEntryToFile(zip, &binary_entry, fd); - close(fd); + // On traditional updates we extract the update binary from the package. + static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary"; + ZipString binary_name(UPDATE_BINARY_NAME); + ZipEntry binary_entry; + if (FindEntry(zip, binary_name, &binary_entry) != 0) { + LOG(ERROR) << "Failed to find update binary " << UPDATE_BINARY_NAME; + return INSTALL_CORRUPT; + } - if (error != 0) { - LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME - << " : " << ErrorCodeString(error); - return INSTALL_ERROR; - } + const char* binary = "/tmp/update_binary"; + unlink(binary); + int fd = creat(binary, 0755); + if (fd == -1) { + PLOG(ERROR) << "Failed to create " << binary; + return INSTALL_ERROR; + } - *cmd = { - binary, - EXPAND(RECOVERY_API_VERSION), // defined in Android.mk - std::to_string(status_fd), - path, - }; - if (retry_count > 0) - cmd->push_back("retry"); - return 0; + int32_t error = ExtractEntryToFile(zip, &binary_entry, fd); + close(fd); + if (error != 0) { + LOG(ERROR) << "Failed to extract " << UPDATE_BINARY_NAME << ": " << ErrorCodeString(error); + return INSTALL_ERROR; + } + + *cmd = { + binary, + EXPAND(RECOVERY_API_VERSION), // defined in Android.mk + std::to_string(status_fd), + path, + }; + if (retry_count > 0) { + cmd->push_back("retry"); + } + return 0; } #endif // !AB_OTA_UPDATER -- cgit v1.2.3