Implement IMGDIFF operation in the client.
This operation is used to apply update on gzipped data with much smaller
diff data than bsdiff.
update_engine only calls ApplyImagePatch() with all the data, the actuall
implementation of the imgdiff format is in bootable/recovery/applypatch.
Test: mma & added unittest
Bug: 26628339
Change-Id: I2b8a097aa68727b06be101cc06a3a896b835a815
diff --git a/Android.mk b/Android.mk
index d529f70..34fe4dd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -143,6 +143,8 @@
update_metadata-protos \
libxz-host \
libbz \
+ libimgpatch \
+ libz \
$(ue_update_metadata_protos_exported_static_libraries)
ue_libpayload_consumer_exported_shared_libraries := \
libcrypto-host \
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index f490c08..074e724 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -26,6 +26,7 @@
#include <string>
#include <vector>
+#include <applypatch/imgpatch.h>
#include <base/files/file_util.h>
#include <base/format_macros.h>
#include <base/strings/string_util.h>
@@ -677,6 +678,9 @@
case InstallOperation::SOURCE_BSDIFF:
op_result = PerformSourceBsdiffOperation(op);
break;
+ case InstallOperation::IMGDIFF:
+ op_result = PerformImgdiffOperation(op);
+ break;
default:
op_result = false;
}
@@ -1232,6 +1236,57 @@
return true;
}
+bool DeltaPerformer::PerformImgdiffOperation(
+ const InstallOperation& operation) {
+ // Since we delete data off the beginning of the buffer as we use it,
+ // the data we need should be exactly at the beginning of the buffer.
+ TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
+ TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
+
+ uint64_t src_blocks = GetBlockCount(operation.src_extents());
+ brillo::Blob src_data(src_blocks * block_size_);
+
+ ssize_t bytes_read = 0;
+ for (const Extent& extent : operation.src_extents()) {
+ ssize_t bytes_read_this_iteration = 0;
+ ssize_t bytes_to_read = extent.num_blocks() * block_size_;
+ TEST_AND_RETURN_FALSE(utils::PReadAll(source_fd_,
+ &src_data[bytes_read],
+ bytes_to_read,
+ extent.start_block() * block_size_,
+ &bytes_read_this_iteration));
+ TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes_to_read);
+ bytes_read += bytes_read_this_iteration;
+ }
+
+ if (operation.has_src_sha256_hash()) {
+ brillo::Blob src_hash;
+ TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(src_data, &src_hash));
+ TEST_AND_RETURN_FALSE(ValidateSourceHash(src_hash, operation));
+ }
+
+ vector<Extent> target_extents(operation.dst_extents().begin(),
+ operation.dst_extents().end());
+ DirectExtentWriter writer;
+ TEST_AND_RETURN_FALSE(writer.Init(target_fd_, target_extents, block_size_));
+ TEST_AND_RETURN_FALSE(
+ ApplyImagePatch(src_data.data(),
+ src_data.size(),
+ buffer_.data(),
+ operation.data_length(),
+ [](const unsigned char* data, ssize_t len, void* token) {
+ return reinterpret_cast<ExtentWriter*>(token)
+ ->Write(data, len)
+ ? len
+ : 0;
+ },
+ &writer) == 0);
+ TEST_AND_RETURN_FALSE(writer.End());
+
+ DiscardBuffer(true, buffer_.size());
+ return true;
+}
+
bool DeltaPerformer::ExtractSignatureMessageFromOperation(
const InstallOperation& operation) {
if (operation.type() != InstallOperation::REPLACE ||
diff --git a/payload_consumer/delta_performer.h b/payload_consumer/delta_performer.h
index 0a60020..03f30b2 100644
--- a/payload_consumer/delta_performer.h
+++ b/payload_consumer/delta_performer.h
@@ -256,6 +256,7 @@
bool PerformBsdiffOperation(const InstallOperation& operation);
bool PerformSourceCopyOperation(const InstallOperation& operation);
bool PerformSourceBsdiffOperation(const InstallOperation& operation);
+ bool PerformImgdiffOperation(const InstallOperation& operation);
// Extracts the payload signature message from the blob on the |operation| if
// the offset matches the one specified by the manifest. Returns whether the
diff --git a/payload_consumer/delta_performer_unittest.cc b/payload_consumer/delta_performer_unittest.cc
index 98a378b..d637a7e 100644
--- a/payload_consumer/delta_performer_unittest.cc
+++ b/payload_consumer/delta_performer_unittest.cc
@@ -84,6 +84,60 @@
0x00, 0x00, 0x59, 0x5a,
};
+// Gzipped 'abc', generated with:
+// echo -n abc | minigzip | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
+const uint8_t kSourceGzippedData[] = {
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0x4c,
+ 0x4a, 0x06, 0x00, 0xc2, 0x41, 0x24, 0x35, 0x03, 0x00, 0x00, 0x00,
+};
+
+// Gzipped 'def', generated with:
+// echo -n def | minigzip | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
+const uint8_t kTargetGzippedData[] = {
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0x49,
+ 0x4d, 0x03, 0x00, 0x61, 0xe1, 0xc4, 0x0c, 0x03, 0x00, 0x00, 0x00,
+};
+
+// Imgdiff data, generated with:
+// echo -n abc | minigzip > abc && truncate -s 4096 abc
+// echo -n def | minigzip > def && truncate -s 4096 def
+// imgdiff abc def patch && hexdump -v -e '" " 12/1 "0x%02x, " "\n"' patch
+const uint8_t kImgdiffData[] = {
+ 0x49, 0x4d, 0x47, 0x44, 0x49, 0x46, 0x46, 0x32, 0x03, 0x00, 0x00, 0x00,
+ 0x03, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0xf1, 0xff,
+ 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x0f,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x42, 0x53, 0x44, 0x49, 0x46, 0x46, 0x34, 0x30, 0x2a, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x5a,
+ 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xc3, 0xc8, 0xfb, 0x1f,
+ 0x00, 0x00, 0x01, 0x40, 0x00, 0x5c, 0x00, 0x20, 0x00, 0x30, 0xcd, 0x34,
+ 0x12, 0x34, 0x54, 0x60, 0x5c, 0xce, 0x2e, 0xe4, 0x8a, 0x70, 0xa1, 0x21,
+ 0x87, 0x91, 0xf6, 0x3e, 0x42, 0x5a, 0x68, 0x39, 0x17, 0x72, 0x45, 0x38,
+ 0x50, 0x90, 0x00, 0x00, 0x00, 0x00, 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41,
+ 0x59, 0x26, 0x53, 0x59, 0x42, 0x3c, 0xb0, 0xf9, 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x07, 0x00, 0x20, 0x00, 0x21, 0x98, 0x19, 0x84, 0x61, 0x77, 0x24,
+ 0x53, 0x85, 0x09, 0x04, 0x23, 0xcb, 0x0f, 0x90, 0x42, 0x53, 0x44, 0x49,
+ 0x46, 0x46, 0x34, 0x30, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x0f, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26,
+ 0x53, 0x59, 0x6f, 0x02, 0x77, 0xf3, 0x00, 0x00, 0x07, 0x40, 0x41, 0xe0,
+ 0x10, 0xc0, 0x00, 0x00, 0x02, 0x20, 0x00, 0x20, 0x00, 0x21, 0x29, 0xa3,
+ 0x10, 0x86, 0x03, 0x84, 0x04, 0xae, 0x5f, 0x17, 0x72, 0x45, 0x38, 0x50,
+ 0x90, 0x6f, 0x02, 0x77, 0xf3, 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59,
+ 0x26, 0x53, 0x59, 0x71, 0x62, 0xbd, 0xa7, 0x00, 0x00, 0x20, 0x40, 0x32,
+ 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x80, 0x00, 0x48, 0x20, 0x00,
+ 0x30, 0xc0, 0x02, 0xa5, 0x19, 0xa5, 0x92, 0x6f, 0xc2, 0x5d, 0xac, 0x0e,
+ 0x17, 0x72, 0x45, 0x38, 0x50, 0x90, 0x71, 0x62, 0xbd, 0xa7, 0x42, 0x5a,
+ 0x68, 0x39, 0x17, 0x72, 0x45, 0x38, 0x50, 0x90, 0x00, 0x00, 0x00, 0x00,
+};
+
} // namespace
class DeltaPerformerTest : public ::testing::Test {
@@ -476,6 +530,33 @@
EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path, true));
}
+TEST_F(DeltaPerformerTest, ImgdiffOperationTest) {
+ brillo::Blob imgdiff_data(std::begin(kImgdiffData), std::end(kImgdiffData));
+
+ AnnotatedOperation aop;
+ *(aop.op.add_src_extents()) = ExtentForRange(0, 1);
+ *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
+ aop.op.set_data_offset(0);
+ aop.op.set_data_length(imgdiff_data.size());
+ aop.op.set_type(InstallOperation::IMGDIFF);
+
+ brillo::Blob payload_data = GeneratePayload(imgdiff_data, {aop}, false);
+
+ string source_path;
+ EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX", &source_path, nullptr));
+ ScopedPathUnlinker path_unlinker(source_path);
+ brillo::Blob source_data(std::begin(kSourceGzippedData),
+ std::end(kSourceGzippedData));
+ source_data.resize(4096); // block size
+ EXPECT_TRUE(utils::WriteFile(
+ source_path.c_str(), source_data.data(), source_data.size()));
+
+ brillo::Blob target_data(std::begin(kTargetGzippedData),
+ std::end(kTargetGzippedData));
+ target_data.resize(4096); // block size
+ EXPECT_EQ(target_data, ApplyPayload(payload_data, source_path, true));
+}
+
TEST_F(DeltaPerformerTest, SourceHashMismatchTest) {
brillo::Blob expected_data = {'f', 'o', 'o'};
brillo::Blob actual_data = {'b', 'a', 'r'};
diff --git a/update_engine.gyp b/update_engine.gyp
index 07e6f6c..9d0f9ac 100644
--- a/update_engine.gyp
+++ b/update_engine.gyp
@@ -133,6 +133,7 @@
'exported_deps': [
'libcrypto',
'libcurl',
+ 'libimgpatch',
'libssl',
'xz-embedded',
],