From 5609bc8b34745b70d18916aa3b74f3272648e490 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 20 Jun 2018 00:30:48 -0700 Subject: applypatch: Refactor applypatch(). applypatch() was initially designed for file-based OTA, operating on individual files. It was later extended to allow patching eMMC targets as a whole, in favor of block-based updates. As we have deprecated file-based OTA since Oreo, part of the code in applypatch() has become obsolete. This CL refactors the related functions, by removing the obsolete logic and focusing on eMMC targets. Since this CL substantially changes applypatch APIs, it adds new functions to avoid unintentionally mixing them together. In particular, it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`, and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()` and `CheckPartition()`. It also replaces the old Edify functions `apply_patch()` and `apply_patch_check()` with `patch_partition()` and `patch_partition_check()` respectively. This CL requires matching changes to OTA generation script (in the same topic). Bug: 110106408 Test: Run recovery_unit_test and recovery_component_test on marlin. Test: `m dist` with non-A/B target. Verify /system/bin/install-recovery.sh on device. Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE. Verify /system/bin/install-recovery.sh on device. Test: Install an incremental OTA with the new updater and scripts. Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f --- applypatch/applypatch_modes.cpp | 57 +++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 33 deletions(-) (limited to 'applypatch/applypatch_modes.cpp') diff --git a/applypatch/applypatch_modes.cpp b/applypatch/applypatch_modes.cpp index f130ea222..b46659808 100644 --- a/applypatch/applypatch_modes.cpp +++ b/applypatch/applypatch_modes.cpp @@ -35,56 +35,48 @@ #include "applypatch/applypatch.h" #include "edify/expr.h" -static int CheckMode(const std::string& target) { - return applypatch_check(target, {}); +static int CheckMode(const std::string& target_emmc) { + std::string err; + auto target = Partition::Parse(target_emmc, &err); + if (!target) { + LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err; + return 2; + } + return CheckPartition(target) ? 0 : 1; } static int FlashMode(const std::string& target_emmc, const std::string& source_file) { - std::vector pieces = android::base::Split(target_emmc, ":"); - if (pieces.size() != 4 || pieces[0] != "EMMC") { + std::string err; + auto target = Partition::Parse(target_emmc, &err); + if (!target) { + LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err; return 2; } - size_t target_size; - if (!android::base::ParseUint(pieces[2], &target_size) || target_size == 0) { - LOG(ERROR) << "Failed to parse \"" << pieces[2] << "\" as byte count"; - return 1; - } - return applypatch_flash(source_file.c_str(), target_emmc.c_str(), pieces[3].c_str(), target_size); + return FlashPartition(target, source_file) ? 0 : 1; } static int PatchMode(const std::string& target_emmc, const std::string& source_emmc, const std::string& patch_file, const std::string& bonus_file) { - std::vector target_pieces = android::base::Split(target_emmc, ":"); - if (target_pieces.size() != 4 || target_pieces[0] != "EMMC") { + std::string err; + auto target = Partition::Parse(target_emmc, &err); + if (!target) { + LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err; return 2; } - size_t target_size; - if (!android::base::ParseUint(target_pieces[2], &target_size) || target_size == 0) { - LOG(ERROR) << "Failed to parse \"" << target_pieces[2] << "\" as byte count"; - return 1; - } - - std::vector source_pieces = android::base::Split(source_emmc, ":"); - if (source_pieces.size() != 4 || source_pieces[0] != "EMMC") { + auto source = Partition::Parse(source_emmc, &err); + if (!source) { + LOG(ERROR) << "Failed to parse source \"" << source_emmc << "\": " << err; return 2; } - size_t source_size; - if (!android::base::ParseUint(source_pieces[2], &source_size) || source_size == 0) { - LOG(ERROR) << "Failed to parse \"" << source_pieces[2] << "\" as byte count"; - return 1; - } - - std::string contents; - if (!android::base::ReadFileToString(patch_file, &contents)) { + std::string patch_contents; + if (!android::base::ReadFileToString(patch_file, &patch_contents)) { PLOG(ERROR) << "Failed to read patch file \"" << patch_file << "\""; return 1; } - std::vector> patches; - patches.push_back(std::make_unique(Value::Type::BLOB, std::move(contents))); - std::vector sha1s{ source_pieces[3] }; + Value patch(Value::Type::BLOB, std::move(patch_contents)); std::unique_ptr bonus; if (!bonus_file.empty()) { std::string bonus_contents; @@ -95,8 +87,7 @@ static int PatchMode(const std::string& target_emmc, const std::string& source_e bonus = std::make_unique(Value::Type::BLOB, std::move(bonus_contents)); } - return applypatch(source_emmc.c_str(), target_emmc.c_str(), target_pieces[3].c_str(), target_size, - sha1s, patches, bonus.get()); + return PatchPartition(target, source, patch, bonus.get()) ? 0 : 1; } static void Usage() { -- cgit v1.2.3