From d26f95287a0efc6a3160624fa4e577891a2c9794 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 12 Mar 2018 21:18:52 -0700 Subject: tests: Add ApplyPatchModesTest.PatchModeEmmcTargetWithBsdiffPatch test. /system/bin/applypatch on device is expected to work with bsdiff based recovery-from-boot patch automatically. Adding a test to ensure that's always the case. Bug: 72731506 Test: Run recovery_component_test on marlin. Change-Id: I56283cd3ce7cf0215cc3bb3619b206fa01d552c4 Merged-In: I56283cd3ce7cf0215cc3bb3619b206fa01d552c4 (cherry picked from commit d612b23dfd58dbe5059ba53d8fd13cbb343b177c) --- tests/component/applypatch_test.cpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index b6d092557..61e06adb6 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "applypatch/applypatch.h" @@ -38,6 +39,8 @@ #include "otautil/cache_location.h" #include "otautil/print_sha1.h" +using namespace std::string_literals; + static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) { ASSERT_NE(nullptr, sha1); @@ -265,6 +268,54 @@ TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); } +// Ensures that applypatch works with a bsdiff based recovery-from-boot.p. +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithBsdiffPatch) { + std::string boot_img_file = from_testdata_base("boot.img"); + std::string boot_img_sha1; + size_t boot_img_size; + sha1sum(boot_img_file, &boot_img_sha1, &boot_img_size); + + std::string recovery_img_file = from_testdata_base("recovery.img"); + std::string recovery_img_sha1; + size_t recovery_img_size; + sha1sum(recovery_img_file, &recovery_img_sha1, &recovery_img_size); + + // Generate the bsdiff patch of recovery-from-boot.p. + std::string src_content; + ASSERT_TRUE(android::base::ReadFileToString(boot_img_file, &src_content)); + + std::string tgt_content; + ASSERT_TRUE(android::base::ReadFileToString(recovery_img_file, &tgt_content)); + + TemporaryFile patch_file; + ASSERT_EQ(0, + bsdiff::bsdiff(reinterpret_cast(src_content.data()), src_content.size(), + reinterpret_cast(tgt_content.data()), tgt_content.size(), + patch_file.path, nullptr)); + + // applypatch : + std::string src_file_arg = + "EMMC:" + boot_img_file + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string recovery_img_size_arg = std::to_string(recovery_img_size); + std::string patch_arg = boot_img_sha1 + ":" + patch_file.path; + std::vector args = { "applypatch", + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); + + // Double check the patched recovery image. + std::string tgt_file_sha1; + size_t tgt_file_size; + sha1sum(tgt_file.path, &tgt_file_sha1, &tgt_file_size); + ASSERT_EQ(recovery_img_size, tgt_file_size); + ASSERT_EQ(recovery_img_sha1, tgt_file_sha1); +} + TEST_F(ApplyPatchModesTest, PatchModeInvalidArgs) { // Invalid bonus file. ASSERT_NE(0, applypatch_modes(3, (const char* []){ "applypatch", "-b", "/doesntexist" })); -- cgit v1.2.3 From e3499f902e4118a890404863f9d6f5d59c121220 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 20 Mar 2018 10:33:42 -0700 Subject: tests: Split ApplyPatchModesTest.PatchModeEmmcTarget. We have been seeing flakiness in continuous test, but unable to reproduce locally. Break it down into smaller units to narrow down the cause. Bug: 67849209 Test: Run recovery_component_test on marlin. Change-Id: Ia24b0c5d137bad27d502575fcd18d3ca9c9828b6 --- tests/component/applypatch_test.cpp | 119 ++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 45 deletions(-) diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp index 61e06adb6..916028594 100644 --- a/tests/component/applypatch_test.cpp +++ b/tests/component/applypatch_test.cpp @@ -205,67 +205,96 @@ TEST_F(ApplyPatchModesTest, PatchModeEmmcTarget) { sha1sum(boot_img, &boot_img_sha1, &boot_img_size); std::string recovery_img = from_testdata_base("recovery.img"); - size_t size; + size_t recovery_img_size; std::string recovery_img_sha1; - sha1sum(recovery_img, &recovery_img_sha1, &size); - std::string recovery_img_size = std::to_string(size); + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); std::string bonus_file = from_testdata_base("bonus.file"); // applypatch -b : - TemporaryFile tmp1; - std::string src_file = + std::string src_file_arg = "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; - std::string tgt_file = "EMMC:" + std::string(tmp1.path); - std::string patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); - std::vector args = { - "applypatch", - "-b", - bonus_file.c_str(), - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch.c_str() - }; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string patch_arg = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); + std::vector args = { "applypatch", + "-b", + bonus_file.c_str(), + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); +} + +// Tests patching the EMMC target without a separate bonus file (i.e. recovery-from-boot patch has +// everything). +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithoutBonusFile) { + std::string boot_img = from_testdata_base("boot.img"); + size_t boot_img_size; + std::string boot_img_sha1; + sha1sum(boot_img, &boot_img_sha1, &boot_img_size); + + std::string recovery_img = from_testdata_base("recovery.img"); + size_t recovery_img_size; + std::string recovery_img_sha1; + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); // applypatch : - TemporaryFile tmp2; - patch = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); - tgt_file = "EMMC:" + std::string(tmp2.path); - std::vector args2 = { - "applypatch", - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch.c_str() - }; - ASSERT_EQ(0, applypatch_modes(args2.size(), args2.data())); + std::string src_file_arg = + "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; + std::string patch_arg = + boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot-with-bonus.p"); + std::vector args = { "applypatch", + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch_arg.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); +} + +TEST_F(ApplyPatchModesTest, PatchModeEmmcTargetWithMultiplePatches) { + std::string boot_img = from_testdata_base("boot.img"); + size_t boot_img_size; + std::string boot_img_sha1; + sha1sum(boot_img, &boot_img_sha1, &boot_img_size); + + std::string recovery_img = from_testdata_base("recovery.img"); + size_t recovery_img_size; + std::string recovery_img_sha1; + sha1sum(recovery_img, &recovery_img_sha1, &recovery_img_size); + std::string recovery_img_size_arg = std::to_string(recovery_img_size); + + std::string bonus_file = from_testdata_base("bonus.file"); // applypatch -b \ - // : : - TemporaryFile tmp3; - tgt_file = "EMMC:" + std::string(tmp3.path); + // : : : + std::string src_file_arg = + "EMMC:" + boot_img + ":" + std::to_string(boot_img_size) + ":" + boot_img_sha1; + TemporaryFile tgt_file; + std::string tgt_file_arg = "EMMC:"s + tgt_file.path; std::string bad_sha1_a = android::base::StringPrintf("%040x", rand()); std::string bad_sha1_b = android::base::StringPrintf("%040x", rand()); std::string patch1 = bad_sha1_a + ":" + from_testdata_base("recovery-from-boot.p"); std::string patch2 = boot_img_sha1 + ":" + from_testdata_base("recovery-from-boot.p"); std::string patch3 = bad_sha1_b + ":" + from_testdata_base("recovery-from-boot.p"); - std::vector args3 = { - "applypatch", - "-b", - bonus_file.c_str(), - src_file.c_str(), - tgt_file.c_str(), - recovery_img_sha1.c_str(), - recovery_img_size.c_str(), - patch1.c_str(), - patch2.c_str(), - patch3.c_str() - }; - ASSERT_EQ(0, applypatch_modes(args3.size(), args3.data())); + std::vector args = { "applypatch", + "-b", + bonus_file.c_str(), + src_file_arg.c_str(), + tgt_file_arg.c_str(), + recovery_img_sha1.c_str(), + recovery_img_size_arg.c_str(), + patch1.c_str(), + patch2.c_str(), + patch3.c_str() }; + ASSERT_EQ(0, applypatch_modes(args.size(), args.data())); } // Ensures that applypatch works with a bsdiff based recovery-from-boot.p. -- cgit v1.2.3 From b6e6ee758704a7044bb6cdf7a17f9e1d1f7ac195 Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Fri, 8 Dec 2017 13:19:23 -0800 Subject: f2fs: support f2fs by setting unmovable bit for package file This enables to use uncrypt for f2fs update-on-reboot. It requires kernel patch named: "f2fs: add an ioctl to disable GC for specific file" If any operation fails during uncrypt, please delete package file as soon as possible, and create the file again to move forward. IOWs, don't leave the package file for a long time. Bug: 70309376 Bug: 30170612 Change-Id: I3b4233e7da756f107be35364521699deaf2e7139 Signed-off-by: Jaegeuk Kim --- uncrypt/uncrypt.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 645faadbf..16036f9ce 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -172,10 +172,15 @@ static struct fstab* read_fstab() { return fstab; } -static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) { +static const char* find_block_device(const char* path, bool* encryptable, + bool* encrypted, bool* f2fs_fs) { // Look for a volume whose mount point is the prefix of path and // return its block device. Set encrypted if it's currently // encrypted. + + // ensure f2fs_fs is set to false first. + *f2fs_fs = false; + for (int i = 0; i < fstab->num_entries; ++i) { struct fstab_rec* v = &fstab->recs[i]; if (!v->mount_point) { @@ -192,6 +197,9 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* *encrypted = true; } } + if (strcmp(v->fs_type, "f2fs") == 0) { + *f2fs_fs = true; + } return v->blk_device; } } @@ -244,7 +252,7 @@ static int retry_fibmap(const int fd, const char* name, int* block, const int he } static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, - bool encrypted, int socket) { + bool encrypted, bool f2fs_fs, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; @@ -307,6 +315,31 @@ static int produce_block_map(const char* path, const char* map_file, const char* } } +// F2FS-specific ioctl +// It requires the below kernel commit merged in v4.16-rc1. +// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file") +// In android-4.4, +// 56ee1e817908 ("f2fs: updates on v4.16-rc1") +// In android-4.9, +// 2f17e34672a8 ("f2fs: updates on v4.16-rc1") +// In android-4.14, +// ce767d9a55bc ("f2fs: updates on v4.16-rc1") +#ifndef F2FS_IOC_SET_PIN_FILE +#ifndef F2FS_IOCTL_MAGIC +#define F2FS_IOCTL_MAGIC 0xf5 +#endif +#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) +#define F2FS_IOC_GET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 14, __u32) +#endif + if (f2fs_fs) { + int error = ioctl(fd, F2FS_IOC_SET_PIN_FILE); + // Don't break the old kernels which don't support it. + if (error && errno != ENOTTY && errno != ENOTSUP) { + PLOG(ERROR) << "Failed to set pin_file for f2fs: " << path << " on " << blk_dev; + return kUncryptIoctlError; + } + } + off64_t pos = 0; int last_progress = 0; while (pos < sb.st_size) { @@ -458,7 +491,8 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke bool encryptable; bool encrypted; - const char* blk_dev = find_block_device(path, &encryptable, &encrypted); + bool f2fs_fs; + const char* blk_dev = find_block_device(path, &encryptable, &encrypted, &f2fs_fs); if (blk_dev == nullptr) { LOG(ERROR) << "failed to find block device for " << path; return kUncryptBlockDeviceFindError; @@ -479,7 +513,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { LOG(INFO) << "writing block map " << map_file; - return produce_block_map(path, map_file, blk_dev, encrypted, socket); + return produce_block_map(path, map_file, blk_dev, encrypted, f2fs_fs, socket); } return 0; -- cgit v1.2.3