Add force_writable to MapPartitionOnDeviceMapper

In sideloading we will map source partitions with
force_writable = false.

Test: manual sideloading
Bug: 117101719

Change-Id: I7629b8ecce07a9bd2c9e24ac6b64617386067ca5
diff --git a/boot_control_android.cc b/boot_control_android.cc
index 8eafdce..12296d7 100644
--- a/boot_control_android.cc
+++ b/boot_control_android.cc
@@ -298,7 +298,7 @@
       super_device, builder.get(), target_slot);
 }
 
-// Unmap all partitions, and remap partitions.
+// Unmap all partitions, and remap partitions as writable.
 bool Remap(DynamicPartitionControlInterface* dynamic_control,
            const string& super_device,
            Slot target_slot,
@@ -318,6 +318,7 @@
               super_device,
               partition.name + target_suffix,
               target_slot,
+              true /* force writable */,
               &map_path)) {
         return false;
       }
diff --git a/boot_control_android_unittest.cc b/boot_control_android_unittest.cc
index 8185508..e98d2f4 100644
--- a/boot_control_android_unittest.cc
+++ b/boot_control_android_unittest.cc
@@ -306,16 +306,17 @@
 
   // Expect that MapPartitionOnDeviceMapper is called on target() metadata slot
   // with each partition in |partitions|.
-  void ExpectMap(const std::set<std::string>& partitions) {
+  void ExpectMap(const std::set<std::string>& partitions,
+                 bool force_writable = true) {
     // Error when MapPartitionOnDeviceMapper is called on unknown arguments.
-    ON_CALL(dynamicControl(), MapPartitionOnDeviceMapper(_, _, _, _))
+    ON_CALL(dynamicControl(), MapPartitionOnDeviceMapper(_, _, _, _, _))
         .WillByDefault(Return(false));
 
     for (const auto& partition : partitions) {
-      EXPECT_CALL(
-          dynamicControl(),
-          MapPartitionOnDeviceMapper(GetSuperDevice(), partition, target(), _))
-          .WillOnce(Invoke([this](auto, auto partition, auto, auto path) {
+      EXPECT_CALL(dynamicControl(),
+                  MapPartitionOnDeviceMapper(
+                      GetSuperDevice(), partition, target(), force_writable, _))
+          .WillOnce(Invoke([this](auto, auto partition, auto, auto, auto path) {
             auto it = mapped_devices_.find(partition);
             if (it != mapped_devices_.end()) {
               *path = it->second;
diff --git a/dynamic_partition_control_android.cc b/dynamic_partition_control_android.cc
index 5e2c2a4..38c6759 100644
--- a/dynamic_partition_control_android.cc
+++ b/dynamic_partition_control_android.cc
@@ -55,11 +55,12 @@
     const std::string& super_device,
     const std::string& target_partition_name,
     uint32_t slot,
+    bool force_writable,
     std::string* path) {
   if (!CreateLogicalPartition(super_device.c_str(),
                               slot,
                               target_partition_name,
-                              true /* force_writable */,
+                              force_writable,
                               std::chrono::milliseconds(kMapTimeoutMillis),
                               path)) {
     LOG(ERROR) << "Cannot map " << target_partition_name << " in "
@@ -67,7 +68,8 @@
     return false;
   }
   LOG(INFO) << "Succesfully mapped " << target_partition_name
-            << " to device mapper; device path at " << *path;
+            << " to device mapper (force_writable = " << force_writable
+            << "); device path at " << *path;
   mapped_devices_.insert(target_partition_name);
   return true;
 }
diff --git a/dynamic_partition_control_android.h b/dynamic_partition_control_android.h
index 945954d..91c9f87 100644
--- a/dynamic_partition_control_android.h
+++ b/dynamic_partition_control_android.h
@@ -33,6 +33,7 @@
   bool MapPartitionOnDeviceMapper(const std::string& super_device,
                                   const std::string& target_partition_name,
                                   uint32_t slot,
+                                  bool force_writable,
                                   std::string* path) override;
   bool UnmapPartitionOnDeviceMapper(const std::string& target_partition_name,
                                     bool wait) override;
diff --git a/dynamic_partition_control_interface.h b/dynamic_partition_control_interface.h
index 00ed026..95b01a1 100644
--- a/dynamic_partition_control_interface.h
+++ b/dynamic_partition_control_interface.h
@@ -46,6 +46,7 @@
       const std::string& super_device,
       const std::string& target_partition_name,
       uint32_t slot,
+      bool force_writable,
       std::string* path) = 0;
 
   // Unmap logical partition on device mapper. This is the reverse operation
diff --git a/mock_dynamic_partition_control.h b/mock_dynamic_partition_control.h
index ccbc7cf..6b233b3 100644
--- a/mock_dynamic_partition_control.h
+++ b/mock_dynamic_partition_control.h
@@ -27,9 +27,12 @@
 
 class MockDynamicPartitionControl : public DynamicPartitionControlInterface {
  public:
-  MOCK_METHOD4(
-      MapPartitionOnDeviceMapper,
-      bool(const std::string&, const std::string&, uint32_t, std::string*));
+  MOCK_METHOD5(MapPartitionOnDeviceMapper,
+               bool(const std::string&,
+                    const std::string&,
+                    uint32_t,
+                    bool,
+                    std::string*));
   MOCK_METHOD2(UnmapPartitionOnDeviceMapper, bool(const std::string&, bool));
   MOCK_METHOD0(Cleanup, void());
   MOCK_METHOD1(DeviceExists, bool(const std::string&));