Merge "Disallow fastboot to modify locked DSU" into main
diff --git a/debuggerd/seccomp_policy/crash_dump.arm.policy b/debuggerd/seccomp_policy/crash_dump.arm.policy
index 8fd03c4..a70ab20 100644
--- a/debuggerd/seccomp_policy/crash_dump.arm.policy
+++ b/debuggerd/seccomp_policy/crash_dump.arm.policy
@@ -20,6 +20,7 @@
faccessat: 1
recvmsg: 1
recvfrom: 1
+setsockopt: 1
sysinfo: 1
process_vm_readv: 1
tgkill: 1
diff --git a/debuggerd/seccomp_policy/crash_dump.arm64.policy b/debuggerd/seccomp_policy/crash_dump.arm64.policy
index 8241f0e..adf8738 100644
--- a/debuggerd/seccomp_policy/crash_dump.arm64.policy
+++ b/debuggerd/seccomp_policy/crash_dump.arm64.policy
@@ -19,6 +19,7 @@
faccessat: 1
recvmsg: 1
recvfrom: 1
+setsockopt: 1
sysinfo: 1
process_vm_readv: 1
tgkill: 1
diff --git a/debuggerd/seccomp_policy/crash_dump.policy.def b/debuggerd/seccomp_policy/crash_dump.policy.def
index 0cb8e08..972a575 100644
--- a/debuggerd/seccomp_policy/crash_dump.policy.def
+++ b/debuggerd/seccomp_policy/crash_dump.policy.def
@@ -26,6 +26,7 @@
recvmsg: 1
recvfrom: 1
sysinfo: 1
+setsockopt: 1
process_vm_readv: 1
diff --git a/debuggerd/seccomp_policy/crash_dump.riscv64.policy b/debuggerd/seccomp_policy/crash_dump.riscv64.policy
index 281e231..94a5677 100644
--- a/debuggerd/seccomp_policy/crash_dump.riscv64.policy
+++ b/debuggerd/seccomp_policy/crash_dump.riscv64.policy
@@ -19,6 +19,7 @@
faccessat: 1
recvmsg: 1
recvfrom: 1
+setsockopt: 1
sysinfo: 1
process_vm_readv: 1
tgkill: 1
diff --git a/debuggerd/seccomp_policy/crash_dump.x86.policy b/debuggerd/seccomp_policy/crash_dump.x86.policy
index 8fd03c4..a70ab20 100644
--- a/debuggerd/seccomp_policy/crash_dump.x86.policy
+++ b/debuggerd/seccomp_policy/crash_dump.x86.policy
@@ -20,6 +20,7 @@
faccessat: 1
recvmsg: 1
recvfrom: 1
+setsockopt: 1
sysinfo: 1
process_vm_readv: 1
tgkill: 1
diff --git a/debuggerd/seccomp_policy/crash_dump.x86_64.policy b/debuggerd/seccomp_policy/crash_dump.x86_64.policy
index 281e231..94a5677 100644
--- a/debuggerd/seccomp_policy/crash_dump.x86_64.policy
+++ b/debuggerd/seccomp_policy/crash_dump.x86_64.policy
@@ -19,6 +19,7 @@
faccessat: 1
recvmsg: 1
recvfrom: 1
+setsockopt: 1
sysinfo: 1
process_vm_readv: 1
tgkill: 1
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index d7e3db6..21df729 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -1286,7 +1286,7 @@
return current_slot;
}
-static int get_slot_count() {
+static int get_slot_count(fastboot::IFastBootDriver* fb) {
std::string var;
int count = 0;
if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
@@ -1296,8 +1296,8 @@
return count;
}
-bool supports_AB() {
- return get_slot_count() >= 2;
+bool supports_AB(fastboot::IFastBootDriver* fb) {
+ return get_slot_count(fb) >= 2;
}
// Given a current slot, this returns what the 'other' slot is.
@@ -1309,7 +1309,7 @@
}
static std::string get_other_slot(const std::string& current_slot) {
- return get_other_slot(current_slot, get_slot_count());
+ return get_other_slot(current_slot, get_slot_count(fb));
}
static std::string get_other_slot(int count) {
@@ -1317,7 +1317,7 @@
}
static std::string get_other_slot() {
- return get_other_slot(get_current_slot(), get_slot_count());
+ return get_other_slot(get_current_slot(), get_slot_count(fb));
}
static std::string verify_slot(const std::string& slot_name, bool allow_all) {
@@ -1326,7 +1326,7 @@
if (allow_all) {
return "all";
} else {
- int count = get_slot_count();
+ int count = get_slot_count(fb);
if (count > 0) {
return "a";
} else {
@@ -1335,7 +1335,7 @@
}
}
- int count = get_slot_count();
+ int count = get_slot_count(fb);
if (count == 0) die("Device does not support slots");
if (slot == "other") {
@@ -1408,7 +1408,7 @@
slot.c_str());
}
if (has_slot == "yes") {
- for (int i = 0; i < get_slot_count(); i++) {
+ for (int i = 0; i < get_slot_count(fb); i++) {
do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
}
} else {
@@ -1529,7 +1529,7 @@
// Sets slot_override as the active slot. If slot_override is blank,
// set current slot as active instead. This clears slot-unbootable.
static void set_active(const std::string& slot_override) {
- if (!supports_AB()) return;
+ if (!supports_AB(fb)) return;
if (slot_override != "") {
fb->SetActive(slot_override);
@@ -1844,7 +1844,7 @@
fp_->secondary_slot = get_other_slot();
}
if (fp_->secondary_slot == "") {
- if (supports_AB()) {
+ if (supports_AB(fb)) {
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
}
fp_->skip_secondary = true;
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index 75b8d29..35deea7 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -29,10 +29,8 @@
#include <functional>
#include <string>
-#include "fastboot_driver.h"
#include "fastboot_driver_interface.h"
#include "filesystem.h"
-#include "super_flash_helper.h"
#include "task.h"
#include "util.h"
@@ -183,12 +181,12 @@
};
Result<NetworkSerial, FastbootError> ParseNetworkSerial(const std::string& serial);
-bool supports_AB();
std::string GetPartitionName(const ImageEntry& entry, const std::string& current_slot_);
void flash_partition_files(const std::string& partition, const std::vector<SparsePtr>& files);
int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp);
std::vector<SparsePtr> resparse_file(sparse_file* s, int64_t max_size);
+bool supports_AB(fastboot::IFastBootDriver* fb);
bool is_retrofit_device(fastboot::IFastBootDriver* fb);
bool is_logical(const std::string& partition);
void fb_perform_format(const std::string& partition, int skip_if_not_supported,
diff --git a/fastboot/fastboot_driver.h b/fastboot/fastboot_driver.h
index 30298cb..49cebd9 100644
--- a/fastboot/fastboot_driver.h
+++ b/fastboot/fastboot_driver.h
@@ -27,7 +27,6 @@
*/
#pragma once
#include <cstdlib>
-#include <deque>
#include <functional>
#include <limits>
#include <memory>
@@ -38,10 +37,8 @@
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <bootimg.h>
-#include <inttypes.h>
#include <sparse/sparse.h>
-#include "constants.h"
#include "fastboot_driver_interface.h"
#include "transport.h"
diff --git a/fastboot/fuzzy_fastboot/main.cpp b/fastboot/fuzzy_fastboot/main.cpp
index 43aaec9..79f3939 100644
--- a/fastboot/fuzzy_fastboot/main.cpp
+++ b/fastboot/fuzzy_fastboot/main.cpp
@@ -50,6 +50,7 @@
#include <gtest/gtest.h>
#include <sparse/sparse.h>
+#include "constants.h"
#include "fastboot_driver.h"
#include "usb.h"
@@ -928,8 +929,7 @@
ASSERT_TRUE(UsbStillAvailible()) << USB_PORT_GONE;
std::string resp;
- EXPECT_EQ(fb->GetVar("product", &resp), SUCCESS)
- << "Device is unresponsive to getvar command";
+ EXPECT_EQ(fb->GetVar("product", &resp), SUCCESS) << "Device is unresponsive to getvar command";
}
TEST_F(Fuzz, CommandTooLarge) {
@@ -985,11 +985,10 @@
TEST_F(Fuzz, SparseZeroBlkSize) {
// handcrafted malform sparse file with zero as block size
const std::vector<char> buf = {
- '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c', '\x00',
- '\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x00', '\x00', '\x00', '\x00', '\xc2', '\xca', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x10', '\x00', '\x00', '\x00', '\x11', '\x22', '\x33', '\x44'
- };
+ '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x01', '\x00',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xc2', '\xca', '\x00', '\x00', '\x01',
+ '\x00', '\x00', '\x00', '\x10', '\x00', '\x00', '\x00', '\x11', '\x22', '\x33', '\x44'};
ASSERT_EQ(DownloadCommand(buf.size()), SUCCESS) << "Device rejected download command";
ASSERT_EQ(SendBuffer(buf), SUCCESS) << "Downloading payload failed";
@@ -1004,13 +1003,10 @@
TEST_F(Fuzz, SparseVeryLargeBlkSize) {
// handcrafted sparse file with block size of ~4GB and divisible 4
const std::vector<char> buf = {
- '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00',
- '\x1c', '\x00', '\x0c', '\x00', '\xF0', '\xFF', '\xFF', '\xFF',
- '\x01', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x00', '\x00', '\x00', '\x00', '\xc3', '\xca', '\x00', '\x00',
- '\x01', '\x00', '\x00', '\x00', '\x0c', '\x00', '\x00', '\x00',
- '\x11', '\x22', '\x33', '\x44'
- };
+ '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c',
+ '\x00', '\xF0', '\xFF', '\xFF', '\xFF', '\x01', '\x00', '\x00', '\x00', '\x01', '\x00',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xc3', '\xca', '\x00', '\x00', '\x01',
+ '\x00', '\x00', '\x00', '\x0c', '\x00', '\x00', '\x00', '\x11', '\x22', '\x33', '\x44'};
ASSERT_EQ(DownloadCommand(buf.size()), SUCCESS) << "Device rejected download command";
ASSERT_EQ(SendBuffer(buf), SUCCESS) << "Downloading payload failed";
@@ -1021,11 +1017,10 @@
TEST_F(Fuzz, SparseTrimmed) {
// handcrafted malform sparse file which is trimmed
const std::vector<char> buf = {
- '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c', '\x00',
- '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x08', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x00', '\x00', '\x00', '\x00', '\xc1', '\xca', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x00', '\x00', '\x00', '\x80', '\x11', '\x22', '\x33', '\x44'
- };
+ '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c',
+ '\x00', '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x08', '\x00', '\x01', '\x00',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xc1', '\xca', '\x00', '\x00', '\x01',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x80', '\x11', '\x22', '\x33', '\x44'};
ASSERT_EQ(DownloadCommand(buf.size()), SUCCESS) << "Device rejected download command";
ASSERT_EQ(SendBuffer(buf), SUCCESS) << "Downloading payload failed";
@@ -1040,11 +1035,10 @@
TEST_F(Fuzz, SparseInvalidChurk) {
// handcrafted malform sparse file with invalid churk
const std::vector<char> buf = {
- '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c', '\x00',
- '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x08', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x00', '\x00', '\x00', '\x00', '\xc1', '\xca', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00',
- '\x10', '\x00', '\x00', '\x00', '\x11', '\x22', '\x33', '\x44'
- };
+ '\x3a', '\xff', '\x26', '\xed', '\x01', '\x00', '\x00', '\x00', '\x1c', '\x00', '\x0c',
+ '\x00', '\x00', '\x10', '\x00', '\x00', '\x00', '\x00', '\x08', '\x00', '\x01', '\x00',
+ '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\xc1', '\xca', '\x00', '\x00', '\x01',
+ '\x00', '\x00', '\x00', '\x10', '\x00', '\x00', '\x00', '\x11', '\x22', '\x33', '\x44'};
ASSERT_EQ(DownloadCommand(buf.size()), SUCCESS) << "Device rejected download command";
ASSERT_EQ(SendBuffer(buf), SUCCESS) << "Downloading payload failed";
@@ -1894,7 +1888,8 @@
if (!fastboot::FastBootTest::IsFastbootOverTcp()) {
printf("<Waiting for Device>\n");
const auto matcher = [](usb_ifc_info* info) -> int {
- return fastboot::FastBootTest::MatchFastboot(info, fastboot::FastBootTest::device_serial);
+ return fastboot::FastBootTest::MatchFastboot(info,
+ fastboot::FastBootTest::device_serial);
};
std::unique_ptr<Transport> transport;
while (!transport) {
diff --git a/fastboot/task.cpp b/fastboot/task.cpp
index f0eed0c..f13dd55 100644
--- a/fastboot/task.cpp
+++ b/fastboot/task.cpp
@@ -15,8 +15,7 @@
//
#include "task.h"
-#include <cstddef>
-#include <iostream>
+#include "fastboot_driver.h"
#include <android-base/logging.h>
#include <android-base/parseint.h>
@@ -130,6 +129,7 @@
// Send the data to the device.
flash_partition_files(super_name_, files);
}
+
std::string OptimizedFlashSuperTask::ToString() const {
return "optimized-flash-super";
}
@@ -165,7 +165,7 @@
LOG(INFO) << "super optimization is disabled";
return nullptr;
}
- if (!supports_AB()) {
+ if (!supports_AB(fp->fb)) {
LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
return nullptr;
}
@@ -218,17 +218,21 @@
auto s = helper->GetSparseLayout();
if (!s) return nullptr;
- // Remove images that we already flashed, just in case we have non-dynamic OS images.
+
+ // Remove tasks that are concatenated into this optimized task
auto remove_if_callback = [&](const auto& task) -> bool {
if (auto flash_task = task->AsFlashTask()) {
return helper->WillFlash(flash_task->GetPartitionAndSlot());
} else if (auto update_super_task = task->AsUpdateSuperTask()) {
return true;
} else if (auto reboot_task = task->AsRebootTask()) {
- return true;
+ if (reboot_task->GetTarget() == "fastboot") {
+ return true;
+ }
}
return false;
};
+
tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
return std::make_unique<OptimizedFlashSuperTask>(super_name, std::move(helper), std::move(s),
diff --git a/fastboot/task.h b/fastboot/task.h
index 6ebe381..a98c874 100644
--- a/fastboot/task.h
+++ b/fastboot/task.h
@@ -15,10 +15,8 @@
//
#pragma once
-#include <sstream>
#include <string>
-#include "fastboot_driver.h"
#include "super_flash_helper.h"
#include "util.h"
@@ -29,6 +27,7 @@
class FlashTask;
class RebootTask;
class UpdateSuperTask;
+class OptimizedFlashSuperTask;
class WipeTask;
class ResizeTask;
class Task {
@@ -40,6 +39,7 @@
virtual FlashTask* AsFlashTask() { return nullptr; }
virtual RebootTask* AsRebootTask() { return nullptr; }
virtual UpdateSuperTask* AsUpdateSuperTask() { return nullptr; }
+ virtual OptimizedFlashSuperTask* AsOptimizedFlashSuperTask() { return nullptr; }
virtual WipeTask* AsWipeTask() { return nullptr; }
virtual ResizeTask* AsResizeTask() { return nullptr; }
@@ -86,13 +86,13 @@
public:
OptimizedFlashSuperTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
SparsePtr sparse_layout, uint64_t super_size, const FlashingPlan* fp);
+ virtual OptimizedFlashSuperTask* AsOptimizedFlashSuperTask() override { return this; }
static std::unique_ptr<OptimizedFlashSuperTask> Initialize(
const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
static bool CanOptimize(const ImageSource* source,
const std::vector<std::unique_ptr<Task>>& tasks);
- using ImageEntry = std::pair<const Image*, std::string>;
void Run() override;
std::string ToString() const override;
diff --git a/fastboot/task_test.cpp b/fastboot/task_test.cpp
index 1ba3f4a..42be3cb 100644
--- a/fastboot/task_test.cpp
+++ b/fastboot/task_test.cpp
@@ -19,11 +19,10 @@
#include "fastboot_driver_mock.h"
#include <gtest/gtest.h>
-#include <fstream>
#include <iostream>
#include <memory>
-#include <unordered_map>
#include "android-base/strings.h"
+#include "gmock/gmock.h"
using android::base::Split;
using testing::_;
@@ -235,3 +234,111 @@
<< "size of fastboot-info task list: " << fastboot_info_tasks.size()
<< " size of hardcoded task list: " << hardcoded_tasks.size();
}
+
+TEST_F(ParseTest, CanOptimizeTest) {
+ if (!get_android_product_out()) {
+ GTEST_SKIP();
+ }
+
+ LocalImageSource s;
+ fp->source = &s;
+ fp->sparse_limit = std::numeric_limits<int64_t>::max();
+
+ fastboot::MockFastbootDriver fb;
+ fp->fb = &fb;
+ fp->should_optimize_flash_super = false;
+ fp->should_use_fastboot_info = true;
+
+ std::vector<std::pair<std::vector<std::string>, bool>> patternmatchtest = {
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ true},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ true},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ false},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "update-super", "flash product",
+ "flash system", "flash system_ext", "flash odm", "if-wipe erase userdata"},
+ false},
+ };
+
+ auto remove_if_callback = [&](const auto& task) -> bool { return !!task->AsResizeTask(); };
+
+ for (auto& test : patternmatchtest) {
+ std::vector<std::unique_ptr<Task>> tasks = ParseFastbootInfo(fp.get(), test.first);
+ tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
+ ASSERT_EQ(OptimizedFlashSuperTask::CanOptimize(fp->source, tasks), test.second);
+ }
+}
+// Note: this test is exclusively testing that optimized flash super pattern matches a given task
+// list and is able to optimized based on a correct sequence of tasks
+TEST_F(ParseTest, OptimizedFlashSuperPatternMatchTest) {
+ if (!get_android_product_out()) {
+ GTEST_SKIP();
+ }
+
+ LocalImageSource s;
+ fp->source = &s;
+ fp->sparse_limit = std::numeric_limits<int64_t>::max();
+
+ fastboot::MockFastbootDriver fb;
+ fp->fb = &fb;
+ fp->should_optimize_flash_super = true;
+ fp->should_use_fastboot_info = true;
+
+ ON_CALL(fb, GetVar("super-partition-name", _, _))
+ .WillByDefault(testing::Return(fastboot::BAD_ARG));
+
+ ON_CALL(fb, GetVar("slot-count", _, _))
+ .WillByDefault(testing::DoAll(testing::SetArgPointee<1>("2"),
+ testing::Return(fastboot::SUCCESS)));
+
+ ON_CALL(fb, GetVar("partition-size:super", _, _))
+ .WillByDefault(testing::DoAll(testing::SetArgPointee<1>("1000"),
+ testing::Return(fastboot::SUCCESS)));
+
+ std::vector<std::pair<std::vector<std::string>, bool>> patternmatchtest = {
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ true},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ true},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
+ "flash product", "flash system", "flash system_ext", "flash odm",
+ "if-wipe erase userdata"},
+ false},
+ {{"flash boot", "flash init_boot", "flash vendor_boot", "update-super", "flash product",
+ "flash system", "flash system_ext", "flash odm", "if-wipe erase userdata"},
+ false},
+ };
+
+ for (auto& test : patternmatchtest) {
+ std::vector<std::unique_ptr<Task>> tasks = ParseFastbootInfo(fp.get(), test.first);
+ // Check to make sure we have an optimized flash super task && no more dynamic partition
+ // flashing tasks
+ auto&& IsOptimized = [](const FlashingPlan* fp,
+ const std::vector<std::unique_ptr<Task>>& tasks) {
+ bool contains_optimized_task = false;
+ for (auto& task : tasks) {
+ if (auto optimized_task = task->AsOptimizedFlashSuperTask()) {
+ contains_optimized_task = true;
+ }
+ if (auto flash_task = task->AsFlashTask()) {
+ if (FlashTask::IsDynamicParitition(fp->source, flash_task)) {
+ return false;
+ }
+ }
+ }
+ return contains_optimized_task;
+ };
+ ASSERT_EQ(IsOptimized(fp.get(), tasks), test.second);
+ }
+}
diff --git a/fs_mgr/libsnapshot/Android.bp b/fs_mgr/libsnapshot/Android.bp
index 04b5a02..0131f73 100644
--- a/fs_mgr/libsnapshot/Android.bp
+++ b/fs_mgr/libsnapshot/Android.bp
@@ -446,6 +446,54 @@
],
}
+cc_binary {
+ name: "create_snapshot",
+ host_supported: true,
+ device_supported: false,
+
+ srcs: ["libsnapshot_cow/create_cow.cpp"],
+
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+
+ static_libs: [
+ "liblog",
+ "libbase",
+ "libext4_utils",
+ "libsnapshot_cow",
+ "libcrypto",
+ "libbrotli",
+ "libz",
+ "liblz4",
+ "libzstd",
+ "libgflags",
+ ],
+ shared_libs: [
+ ],
+
+ header_libs: [
+ "libstorage_literals_headers",
+ ],
+
+ dist: {
+ targets: [
+ "sdk",
+ "sdk-repo-platform-tools",
+ "sdk_repo",
+ ],
+ },
+ target: {
+ darwin: {
+ enabled: false,
+ },
+ windows: {
+ enabled: false,
+ },
+ },
+}
+
python_library_host {
name: "snapshot_proto_python",
srcs: [
diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
index c056a19..6bc8b9b 100644
--- a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
+++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
@@ -441,6 +441,7 @@
friend class FlashAfterUpdateTest;
friend class LockTestConsumer;
friend class SnapshotFuzzEnv;
+ friend class MapSnapshots;
friend struct AutoDeleteCowImage;
friend struct AutoDeleteSnapshot;
friend struct PartitionCowCreator;
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp
new file mode 100644
index 0000000..4e07fe3
--- /dev/null
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp
@@ -0,0 +1,359 @@
+#include <linux/types.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <condition_variable>
+#include <cstring>
+#include <future>
+#include <iostream>
+#include <limits>
+#include <mutex>
+#include <string>
+#include <thread>
+#include <unordered_map>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
+#include <ext4_utils/ext4_utils.h>
+#include <storage_literals/storage_literals.h>
+
+#include <android-base/chrono_utils.h>
+#include <android-base/scopeguard.h>
+#include <android-base/strings.h>
+
+#include <gflags/gflags.h>
+#include <libsnapshot/cow_writer.h>
+
+#include <openssl/sha.h>
+
+DEFINE_string(source, "", "Source partition image");
+DEFINE_string(target, "", "Target partition image");
+DEFINE_string(compression, "lz4",
+ "Compression algorithm. Default is set to lz4. Available options: lz4, zstd, gz");
+
+namespace android {
+namespace snapshot {
+
+using namespace android::storage_literals;
+using namespace android;
+using android::base::unique_fd;
+
+using android::snapshot::CreateCowWriter;
+using android::snapshot::ICowWriter;
+
+class CreateSnapshot {
+ public:
+ CreateSnapshot(const std::string& src_file, const std::string& target_file,
+ const std::string& patch_file, const std::string& compression);
+ bool CreateSnapshotPatch();
+
+ private:
+ /* source.img */
+ std::string src_file_;
+ /* target.img */
+ std::string target_file_;
+ /* snapshot-patch generated */
+ std::string patch_file_;
+
+ /*
+ * Active file which is being parsed by this instance.
+ * It will either be source.img or target.img.
+ */
+ std::string parsing_file_;
+ bool create_snapshot_patch_ = false;
+
+ const int kNumThreads = 6;
+ const size_t kBlockSizeToRead = 1_MiB;
+
+ std::unordered_map<std::string, int> source_block_hash_;
+ std::mutex source_block_hash_lock_;
+
+ std::unique_ptr<ICowWriter> writer_;
+ std::mutex write_lock_;
+
+ std::unique_ptr<uint8_t[]> zblock_;
+
+ std::string compression_ = "lz4";
+ unique_fd fd_;
+
+ const int BLOCK_SZ = 4_KiB;
+ void SHA256(const void* data, size_t length, uint8_t out[32]);
+ bool IsBlockAligned(uint64_t read_size) { return ((read_size & (BLOCK_SZ - 1)) == 0); }
+ bool ReadBlocks(off_t offset, const int skip_blocks, const uint64_t dev_sz);
+ std::string ToHexString(const uint8_t* buf, size_t len);
+
+ bool CreateSnapshotFile();
+ bool FindSourceBlockHash();
+ bool PrepareParse(std::string& parsing_file, const bool createSnapshot);
+ bool ParsePartition();
+ bool WriteSnapshot(const void* buffer, uint64_t block, std::string& block_hash);
+};
+
+void CreateSnapshotLogger(android::base::LogId, android::base::LogSeverity severity, const char*,
+ const char*, unsigned int, const char* message) {
+ if (severity == android::base::ERROR) {
+ fprintf(stderr, "%s\n", message);
+ } else {
+ fprintf(stdout, "%s\n", message);
+ }
+}
+
+CreateSnapshot::CreateSnapshot(const std::string& src_file, const std::string& target_file,
+ const std::string& patch_file, const std::string& compression)
+ : src_file_(src_file), target_file_(target_file), patch_file_(patch_file) {
+ if (!compression.empty()) {
+ compression_ = compression;
+ }
+}
+
+bool CreateSnapshot::PrepareParse(std::string& parsing_file, const bool createSnapshot) {
+ parsing_file_ = parsing_file;
+ create_snapshot_patch_ = createSnapshot;
+
+ if (createSnapshot) {
+ fd_.reset(open(patch_file_.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666));
+ if (fd_ < 0) {
+ PLOG(ERROR) << "Failed to open the snapshot-patch file: " << patch_file_;
+ return false;
+ }
+
+ zblock_ = std::make_unique<uint8_t[]>(BLOCK_SZ);
+ std::memset(zblock_.get(), 0, BLOCK_SZ);
+
+ CowOptions options;
+ options.compression = compression_;
+ options.num_compress_threads = 2;
+ options.batch_write = true;
+ options.cluster_ops = 600;
+ writer_ = CreateCowWriter(2, options, std::move(fd_));
+ }
+ return true;
+}
+
+/*
+ * Create per-block sha256 hash of source partition
+ */
+bool CreateSnapshot::FindSourceBlockHash() {
+ if (!PrepareParse(src_file_, false)) {
+ return false;
+ }
+ return ParsePartition();
+}
+
+/*
+ * Create snapshot file by comparing sha256 per block
+ * of target.img with the constructed per-block sha256 hash
+ * of source partition.
+ */
+bool CreateSnapshot::CreateSnapshotFile() {
+ if (!PrepareParse(target_file_, true)) {
+ return false;
+ }
+ return ParsePartition();
+}
+
+/*
+ * Creates snapshot patch file by comparing source.img and target.img
+ */
+bool CreateSnapshot::CreateSnapshotPatch() {
+ if (!FindSourceBlockHash()) {
+ return false;
+ }
+ return CreateSnapshotFile();
+}
+
+void CreateSnapshot::SHA256(const void* data, size_t length, uint8_t out[32]) {
+ SHA256_CTX c;
+ SHA256_Init(&c);
+ SHA256_Update(&c, data, length);
+ SHA256_Final(out, &c);
+}
+
+std::string CreateSnapshot::ToHexString(const uint8_t* buf, size_t len) {
+ char lookup[] = "0123456789abcdef";
+ std::string out(len * 2 + 1, '\0');
+ char* outp = out.data();
+ for (; len > 0; len--, buf++) {
+ *outp++ = (char)lookup[*buf >> 4];
+ *outp++ = (char)lookup[*buf & 0xf];
+ }
+ return out;
+}
+
+bool CreateSnapshot::WriteSnapshot(const void* buffer, uint64_t block, std::string& block_hash) {
+ if (std::memcmp(zblock_.get(), buffer, BLOCK_SZ) == 0) {
+ std::lock_guard<std::mutex> lock(write_lock_);
+ return writer_->AddZeroBlocks(block, 1);
+ }
+
+ auto iter = source_block_hash_.find(block_hash);
+ if (iter != source_block_hash_.end()) {
+ std::lock_guard<std::mutex> lock(write_lock_);
+ return writer_->AddCopy(block, iter->second, 1);
+ }
+ std::lock_guard<std::mutex> lock(write_lock_);
+ return writer_->AddRawBlocks(block, buffer, BLOCK_SZ);
+}
+
+bool CreateSnapshot::ReadBlocks(off_t offset, const int skip_blocks, const uint64_t dev_sz) {
+ unique_fd fd(TEMP_FAILURE_RETRY(open(parsing_file_.c_str(), O_RDONLY)));
+ if (fd < 0) {
+ LOG(ERROR) << "open failed: " << parsing_file_;
+ return false;
+ }
+
+ loff_t file_offset = offset;
+ const uint64_t read_sz = kBlockSizeToRead;
+ std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(read_sz);
+
+ while (true) {
+ size_t to_read = std::min((dev_sz - file_offset), read_sz);
+
+ if (!android::base::ReadFullyAtOffset(fd.get(), buffer.get(), to_read, file_offset)) {
+ LOG(ERROR) << "Failed to read block from block device: " << parsing_file_
+ << " at offset: " << file_offset << " read-size: " << to_read
+ << " block-size: " << dev_sz;
+ return false;
+ }
+
+ if (!IsBlockAligned(to_read)) {
+ LOG(ERROR) << "unable to parse the un-aligned request: " << to_read;
+ return false;
+ }
+
+ size_t num_blocks = to_read / BLOCK_SZ;
+ uint64_t buffer_offset = 0;
+ off_t foffset = file_offset;
+
+ while (num_blocks) {
+ const void* bufptr = (char*)buffer.get() + buffer_offset;
+ uint64_t blkindex = foffset / BLOCK_SZ;
+
+ uint8_t checksum[32];
+ SHA256(bufptr, BLOCK_SZ, checksum);
+ std::string hash = ToHexString(checksum, sizeof(checksum));
+
+ if (create_snapshot_patch_ && !WriteSnapshot(bufptr, blkindex, hash)) {
+ LOG(ERROR) << "WriteSnapshot failed for block: " << blkindex;
+ return false;
+ } else {
+ std::lock_guard<std::mutex> lock(source_block_hash_lock_);
+ {
+ if (source_block_hash_.count(hash) == 0) {
+ source_block_hash_[hash] = blkindex;
+ }
+ }
+ }
+ buffer_offset += BLOCK_SZ;
+ foffset += BLOCK_SZ;
+ num_blocks -= 1;
+ }
+
+ file_offset += (skip_blocks * kBlockSizeToRead);
+ if (file_offset >= dev_sz) {
+ break;
+ }
+ }
+
+ return true;
+}
+
+bool CreateSnapshot::ParsePartition() {
+ unique_fd fd(TEMP_FAILURE_RETRY(open(parsing_file_.c_str(), O_RDONLY)));
+ if (fd < 0) {
+ LOG(ERROR) << "open failed: " << parsing_file_;
+ return false;
+ }
+
+ uint64_t dev_sz = lseek(fd.get(), 0, SEEK_END);
+ if (!dev_sz) {
+ LOG(ERROR) << "Could not determine block device size: " << parsing_file_;
+ return false;
+ }
+
+ if (!IsBlockAligned(dev_sz)) {
+ LOG(ERROR) << "dev_sz: " << dev_sz << " is not block aligned";
+ return false;
+ }
+
+ int num_threads = kNumThreads;
+
+ std::vector<std::future<bool>> threads;
+ off_t start_offset = 0;
+ const int skip_blocks = num_threads;
+
+ while (num_threads) {
+ threads.emplace_back(std::async(std::launch::async, &CreateSnapshot::ReadBlocks, this,
+ start_offset, skip_blocks, dev_sz));
+ start_offset += kBlockSizeToRead;
+ num_threads -= 1;
+ if (start_offset >= dev_sz) {
+ break;
+ }
+ }
+
+ bool ret = true;
+ for (auto& t : threads) {
+ ret = t.get() && ret;
+ }
+
+ if (ret && create_snapshot_patch_ && !writer_->Finalize()) {
+ LOG(ERROR) << "Finzalize failed";
+ return false;
+ }
+
+ return ret;
+}
+
+} // namespace snapshot
+} // namespace android
+
+constexpr char kUsage[] = R"(
+NAME
+ create_snapshot - Create snapshot patches by comparing two partition images
+
+SYNOPSIS
+ create_snapshot --source=<source.img> --target=<target.img> --compression="<compression-algorithm"
+
+ source.img -> Source partition image
+ target.img -> Target partition image
+ compressoin -> compression algorithm. Default set to lz4. Supported types are gz, lz4, zstd.
+
+EXAMPLES
+
+ $ create_snapshot $SOURCE_BUILD/system.img $TARGET_BUILD/system.img
+ $ create_snapshot $SOURCE_BUILD/product.img $TARGET_BUILD/product.img --compression="zstd"
+
+)";
+
+int main(int argc, char* argv[]) {
+ android::base::InitLogging(argv, &android::snapshot::CreateSnapshotLogger);
+ ::gflags::SetUsageMessage(kUsage);
+ ::gflags::ParseCommandLineFlags(&argc, &argv, true);
+
+ if (FLAGS_source.empty() || FLAGS_target.empty()) {
+ LOG(INFO) << kUsage;
+ return 0;
+ }
+
+ std::string fname = android::base::Basename(FLAGS_target.c_str());
+ auto parts = android::base::Split(fname, ".");
+ std::string snapshotfile = parts[0] + ".patch";
+ android::snapshot::CreateSnapshot snapshot(FLAGS_source, FLAGS_target, snapshotfile,
+ FLAGS_compression);
+
+ if (!snapshot.CreateSnapshotPatch()) {
+ LOG(ERROR) << "Snapshot creation failed";
+ return -1;
+ }
+
+ LOG(INFO) << "Snapshot patch: " << snapshotfile << " created successfully";
+ return 0;
+}
diff --git a/fs_mgr/libsnapshot/snapshotctl.cpp b/fs_mgr/libsnapshot/snapshotctl.cpp
index 38eb719..1323b0b 100644
--- a/fs_mgr/libsnapshot/snapshotctl.cpp
+++ b/fs_mgr/libsnapshot/snapshotctl.cpp
@@ -17,14 +17,25 @@
#include <sysexits.h>
#include <chrono>
+#include <filesystem>
+#include <fstream>
+#include <future>
#include <iostream>
#include <map>
#include <sstream>
+#include <thread>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
+#include <android-base/chrono_utils.h>
+#include <android-base/parseint.h>
+#include <android-base/properties.h>
+#include <android-base/scopeguard.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
#include <fs_mgr.h>
#include <fs_mgr_dm_linear.h>
#include <fstab/fstab.h>
@@ -33,6 +44,8 @@
#include <libsnapshot/snapshot.h>
#include <storage_literals/storage_literals.h>
+#include "partition_cow_creator.h"
+
#ifdef SNAPSHOTCTL_USERDEBUG_OR_ENG
#include <BootControlClient.h>
#endif
@@ -56,13 +69,179 @@
" merge\n"
" Deprecated.\n"
" map\n"
- " Map all partitions at /dev/block/mapper\n";
+ " Map all partitions at /dev/block/mapper\n"
+ " map-snapshots <directory where snapshot patches are present>\n"
+ " Map all snapshots based on patches present in the directory\n"
+ " unmap-snapshots\n"
+ " Unmap all pre-created snapshots\n"
+ " delete-snapshots\n"
+ " Delete all pre-created snapshots\n";
return EX_USAGE;
}
namespace android {
namespace snapshot {
+class MapSnapshots {
+ public:
+ MapSnapshots(std::string path = "");
+ bool CreateSnapshotDevice(std::string& partition_name, std::string& patch);
+ bool InitiateThreadedSnapshotWrite(std::string& pname, std::string& snapshot_patch);
+ bool WaitForSnapshotWritesToComplete();
+ bool UnmapCowImagePath(std::string& name);
+ bool DeleteCowImage(std::string& name);
+
+ private:
+ std::optional<std::string> GetCowImagePath(std::string& name);
+ bool WriteSnapshotPatch(std::string cow_device, std::string patch);
+ std::unique_ptr<SnapshotManager::LockedFile> lock_;
+ std::unique_ptr<SnapshotManager> sm_;
+ std::vector<std::future<bool>> threads_;
+ std::string snapshot_dir_path_;
+};
+
+MapSnapshots::MapSnapshots(std::string path) {
+ sm_ = SnapshotManager::New();
+ if (!sm_) {
+ std::cout << "Failed to create snapshotmanager";
+ exit(1);
+ }
+ snapshot_dir_path_ = path + "/";
+ lock_ = sm_->LockExclusive();
+}
+
+bool MapSnapshots::CreateSnapshotDevice(std::string& partition_name, std::string& patchfile) {
+ std::string parsing_file = snapshot_dir_path_ + patchfile;
+
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(parsing_file.c_str(), O_RDONLY)));
+ if (fd < 0) {
+ LOG(ERROR) << "Failed to open file: " << parsing_file;
+ return false;
+ }
+
+ uint64_t dev_sz = lseek(fd.get(), 0, SEEK_END);
+ if (!dev_sz) {
+ LOG(ERROR) << "Could not determine block device size: " << parsing_file;
+ return false;
+ }
+
+ const int block_sz = 4_KiB;
+ dev_sz += block_sz - 1;
+ dev_sz &= ~(block_sz - 1);
+
+ SnapshotStatus status;
+ status.set_name(partition_name);
+ status.set_cow_file_size(dev_sz);
+ status.set_cow_partition_size(0);
+
+ PartitionCowCreator cow_creator;
+ cow_creator.using_snapuserd = true;
+
+ if (!sm_->CreateSnapshot(lock_.get(), &cow_creator, &status)) {
+ LOG(ERROR) << "CreateSnapshot failed";
+ return false;
+ }
+
+ if (!sm_->CreateCowImage(lock_.get(), partition_name)) {
+ LOG(ERROR) << "CreateCowImage failed";
+ return false;
+ }
+
+ return true;
+}
+
+std::optional<std::string> MapSnapshots::GetCowImagePath(std::string& name) {
+ auto cow_dev = sm_->MapCowImage(name, 5s);
+ if (!cow_dev.has_value()) {
+ LOG(ERROR) << "Failed to get COW device path";
+ return std::nullopt;
+ }
+
+ LOG(INFO) << "COW Device path: " << cow_dev.value();
+ return cow_dev;
+}
+
+bool MapSnapshots::WriteSnapshotPatch(std::string cow_device, std::string patch) {
+ std::string patch_file = snapshot_dir_path_ + patch;
+
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(patch_file.c_str(), O_RDONLY)));
+ if (fd < 0) {
+ LOG(ERROR) << "Failed to open file: " << patch_file;
+ return false;
+ }
+
+ uint64_t dev_sz = lseek(fd.get(), 0, SEEK_END);
+ if (!dev_sz) {
+ std::cout << "Could not determine block device size: " << patch_file;
+ return false;
+ }
+
+ android::base::unique_fd cfd(TEMP_FAILURE_RETRY(open(cow_device.c_str(), O_RDWR)));
+ if (cfd < 0) {
+ LOG(ERROR) << "Failed to open file: " << cow_device;
+ return false;
+ }
+
+ const uint64_t read_sz = 1_MiB;
+ std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(read_sz);
+ off_t file_offset = 0;
+
+ while (true) {
+ size_t to_read = std::min((dev_sz - file_offset), read_sz);
+ if (!android::base::ReadFullyAtOffset(fd.get(), buffer.get(), to_read, file_offset)) {
+ PLOG(ERROR) << "ReadFullyAtOffset failed";
+ return false;
+ }
+
+ if (!android::base::WriteFullyAtOffset(cfd, buffer.get(), to_read, file_offset)) {
+ PLOG(ERROR) << "WriteFullyAtOffset failed";
+ return false;
+ }
+ file_offset += to_read;
+ if (file_offset >= dev_sz) {
+ break;
+ }
+ }
+ fsync(cfd.get());
+ return true;
+}
+
+bool MapSnapshots::InitiateThreadedSnapshotWrite(std::string& pname, std::string& snapshot_patch) {
+ auto path = GetCowImagePath(pname);
+ if (!path.has_value()) {
+ return false;
+ }
+ threads_.emplace_back(std::async(std::launch::async, &MapSnapshots::WriteSnapshotPatch, this,
+ path.value(), snapshot_patch));
+ return true;
+}
+
+bool MapSnapshots::WaitForSnapshotWritesToComplete() {
+ bool ret = true;
+ for (auto& t : threads_) {
+ ret = t.get() && ret;
+ }
+
+ if (ret) {
+ LOG(INFO) << "Pre-created snapshots successfully copied";
+ } else {
+ LOG(ERROR) << "Snapshot copy failed";
+ }
+ return ret;
+}
+
+bool MapSnapshots::UnmapCowImagePath(std::string& name) {
+ return sm_->UnmapCowImage(name);
+}
+
+bool MapSnapshots::DeleteCowImage(std::string& name) {
+ if (!sm_->DeleteSnapshot(lock_.get(), name)) {
+ LOG(ERROR) << "Delete snapshot failed";
+ return false;
+ }
+ return true;
+}
+
bool DumpCmdHandler(int /*argc*/, char** argv) {
android::base::InitLogging(argv, &android::base::StderrLogger);
return SnapshotManager::New()->Dump(std::cout);
@@ -85,6 +264,121 @@
return false;
}
+bool GetVerityPartitions(std::vector<std::string>& partitions) {
+ auto& dm = android::dm::DeviceMapper::Instance();
+ auto dm_block_devices = dm.FindDmPartitions();
+ if (dm_block_devices.empty()) {
+ LOG(ERROR) << "No dm-enabled block device is found.";
+ return false;
+ }
+
+ for (auto& block_device : dm_block_devices) {
+ std::string dm_block_name = block_device.first;
+ std::string slot_suffix = fs_mgr_get_slot_suffix();
+ std::string partition = dm_block_name + slot_suffix;
+ partitions.push_back(partition);
+ }
+ return true;
+}
+
+bool UnMapPrecreatedSnapshots(int, char**) {
+ // Make sure we are root.
+ if (::getuid() != 0) {
+ LOG(ERROR) << "Not running as root. Try \"adb root\" first.";
+ return EXIT_FAILURE;
+ }
+
+ std::vector<std::string> partitions;
+ if (!GetVerityPartitions(partitions)) {
+ return false;
+ }
+
+ MapSnapshots snapshot;
+ for (auto partition : partitions) {
+ if (!snapshot.UnmapCowImagePath(partition)) {
+ LOG(ERROR) << "UnmapCowImagePath failed: " << partition;
+ }
+ }
+ return true;
+}
+
+bool DeletePrecreatedSnapshots(int, char**) {
+ // Make sure we are root.
+ if (::getuid() != 0) {
+ LOG(ERROR) << "Not running as root. Try \"adb root\" first.";
+ return EXIT_FAILURE;
+ }
+
+ std::vector<std::string> partitions;
+ if (!GetVerityPartitions(partitions)) {
+ return false;
+ }
+
+ MapSnapshots snapshot;
+ for (auto partition : partitions) {
+ if (!snapshot.DeleteCowImage(partition)) {
+ LOG(ERROR) << "DeleteCowImage failed: " << partition;
+ }
+ }
+ return true;
+}
+
+bool MapPrecreatedSnapshots(int argc, char** argv) {
+ android::base::InitLogging(argv, &android::base::StderrLogger);
+
+ // Make sure we are root.
+ if (::getuid() != 0) {
+ LOG(ERROR) << "Not running as root. Try \"adb root\" first.";
+ return EXIT_FAILURE;
+ }
+
+ if (argc < 3) {
+ std::cerr << " map-snapshots <directory location where snapshot patches are present>"
+ " Map all snapshots based on patches present in the directory\n";
+ return false;
+ }
+
+ std::string path = std::string(argv[2]);
+ std::vector<std::string> patchfiles;
+
+ for (const auto& entry : std::filesystem::directory_iterator(path)) {
+ if (android::base::EndsWith(entry.path().generic_string(), ".patch")) {
+ patchfiles.push_back(android::base::Basename(entry.path().generic_string()));
+ }
+ }
+ auto& dm = android::dm::DeviceMapper::Instance();
+ auto dm_block_devices = dm.FindDmPartitions();
+ if (dm_block_devices.empty()) {
+ LOG(ERROR) << "No dm-enabled block device is found.";
+ return false;
+ }
+
+ std::vector<std::pair<std::string, std::string>> partitions;
+ for (auto& patchfile : patchfiles) {
+ auto npos = patchfile.rfind(".patch");
+ auto dm_block_name = patchfile.substr(0, npos);
+ if (dm_block_devices.find(dm_block_name) != dm_block_devices.end()) {
+ std::string slot_suffix = fs_mgr_get_slot_suffix();
+ std::string partition = dm_block_name + slot_suffix;
+ partitions.push_back(std::make_pair(partition, patchfile));
+ }
+ }
+
+ MapSnapshots cow(path);
+ for (auto& pair : partitions) {
+ if (!cow.CreateSnapshotDevice(pair.first, pair.second)) {
+ LOG(ERROR) << "CreateSnapshotDevice failed for: " << pair.first;
+ return false;
+ }
+ if (!cow.InitiateThreadedSnapshotWrite(pair.first, pair.second)) {
+ LOG(ERROR) << "InitiateThreadedSnapshotWrite failed for: " << pair.first;
+ return false;
+ }
+ }
+
+ return cow.WaitForSnapshotWritesToComplete();
+}
+
#ifdef SNAPSHOTCTL_USERDEBUG_OR_ENG
bool CreateTestUpdate(SnapshotManager* sm) {
chromeos_update_engine::DeltaArchiveManifest manifest;
@@ -137,8 +431,8 @@
.block_device = fs_mgr_get_super_partition_name(target_slot_number),
.metadata_slot = {target_slot_number},
.partition_name = system_target_name,
- .partition_opener = &opener,
.timeout_ms = 10s,
+ .partition_opener = &opener,
};
auto writer = sm->OpenSnapshotWriter(clpp, std::nullopt);
if (!writer) {
@@ -211,6 +505,9 @@
{"test-blank-ota", TestOtaHandler},
#endif
{"unmap", UnmapCmdHandler},
+ {"map-snapshots", MapPrecreatedSnapshots},
+ {"unmap-snapshots", UnMapPrecreatedSnapshots},
+ {"delete-snapshots", DeletePrecreatedSnapshots},
// clang-format on
};
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index e2c77c3..f65bb20 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -111,8 +111,7 @@
break;
bits_size = res + 16;
bits = realloc(bits, bits_size * 2);
- if(bits == NULL)
- err(1, "failed to allocate buffer of size %d\n", (int)bits_size);
+ if (bits == NULL) err(1, "failed to allocate buffer of size %zd", bits_size);
}
res2 = 0;
switch(i) {