blob: dc152cc3ac3dc435084cf8313bfda954e0f9e26d [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
18#define UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
19
20#include "update_engine/dynamic_partition_control_interface.h"
21
22#include <memory>
23#include <set>
24#include <string>
25
26namespace chromeos_update_engine {
27
28class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
29 public:
30 DynamicPartitionControlAndroid() = default;
31 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070032 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong537802d2018-08-15 13:15:42 -070033 bool MapPartitionOnDeviceMapper(const std::string& super_device,
34 const std::string& target_partition_name,
35 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070036 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070037 std::string* path) override;
Yifan Hong537802d2018-08-15 13:15:42 -070038 void Cleanup() override;
39 bool DeviceExists(const std::string& path) override;
40 android::dm::DmDeviceState GetState(const std::string& name) override;
41 bool GetDmDevicePathByName(const std::string& name,
42 std::string* path) override;
43 std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -070044 const std::string& super_device, uint32_t source_slot) override;
45
46 bool PreparePartitionsForUpdate(uint32_t source_slot,
47 uint32_t target_slot,
48 const BootControlInterface::PartitionMetadata&
49 partition_metadata) override;
50 bool GetDeviceDir(std::string* path) override;
51
52 protected:
53 // These functions are exposed for testing.
54
55 // Unmap logical partition on device mapper. This is the reverse operation
56 // of MapPartitionOnDeviceMapper.
57 // Returns true if unmapped successfully.
58 virtual bool UnmapPartitionOnDeviceMapper(
59 const std::string& target_partition_name);
60
61 // Retrieve metadata from |super_device| at slot |source_slot|.
62 //
63 // If |target_slot| != kInvalidSlot, before returning the metadata, this
64 // function modifies the metadata so that during updates, the metadata can be
65 // written to |target_slot|. In particular, on retrofit devices, the returned
66 // metadata automatically includes block devices at |target_slot|.
67 //
68 // If |target_slot| == kInvalidSlot, this function returns metadata at
69 // |source_slot| without modifying it. This is the same as
70 // LoadMetadataBuilder(const std::string&, uint32_t).
71 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080072 const std::string& super_device,
73 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070074 uint32_t target_slot);
75
76 // Write metadata |builder| to |super_device| at slot |target_slot|.
77 virtual bool StoreMetadata(const std::string& super_device,
78 android::fs_mgr::MetadataBuilder* builder,
79 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070080
81 private:
Yifan Hongc049f932019-07-23 15:06:05 -070082 friend class DynamicPartitionControlAndroidTest;
83
Yifan Hong537802d2018-08-15 13:15:42 -070084 std::set<std::string> mapped_devices_;
85
86 void CleanupInternal(bool wait);
Yifan Hong8546a712019-03-28 14:42:53 -070087 bool MapPartitionInternal(const std::string& super_device,
88 const std::string& target_partition_name,
89 uint32_t slot,
90 bool force_writable,
91 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -070092
Yifan Hong012508e2019-07-22 18:30:40 -070093 // Update |builder| according to |partition_metadata|, assuming the device
94 // does not have Virtual A/B.
95 bool UpdatePartitionMetadata(
96 android::fs_mgr::MetadataBuilder* builder,
97 uint32_t target_slot,
98 const BootControlInterface::PartitionMetadata& partition_metadata);
99
Yifan Hong537802d2018-08-15 13:15:42 -0700100 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
101};
102
103} // namespace chromeos_update_engine
104
105#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_