blob: a79f41ac0f8bdbf8c65779922c20936ca24dc8d9 [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
Yifan Hong537802d2018-08-15 13:15:42 -070020#include <memory>
21#include <set>
22#include <string>
23
Yifan Hong3a1a5612019-11-05 16:34:32 -080024#include <base/files/file_util.h>
Yifan Hong2c62c132019-10-24 14:53:40 -070025#include <libsnapshot/auto_device.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070026#include <libsnapshot/snapshot.h>
27
Yifan Hong15726b92019-11-05 19:06:48 -080028#include "update_engine/common/dynamic_partition_control_interface.h"
29
Yifan Hong537802d2018-08-15 13:15:42 -070030namespace chromeos_update_engine {
31
32class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
33 public:
Yifan Hong420db9b2019-07-23 20:50:33 -070034 DynamicPartitionControlAndroid();
Yifan Hong537802d2018-08-15 13:15:42 -070035 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070036 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070037 FeatureFlag GetVirtualAbFeatureFlag() override;
Yifan Hong6eec9952019-12-04 13:12:01 -080038 bool ShouldSkipOperation(const std::string& partition_name,
39 const InstallOperation& operation) override;
Yifan Hong537802d2018-08-15 13:15:42 -070040 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070041
Yifan Hongf0f4a912019-09-26 17:51:33 -070042 bool PreparePartitionsForUpdate(uint32_t source_slot,
43 uint32_t target_slot,
44 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080045 bool update,
46 uint64_t* required_size) override;
Yifan Honga33bca42019-09-03 20:29:45 -070047 bool FinishUpdate() override;
Yifan Hong012508e2019-07-22 18:30:40 -070048
Yifan Hong3a1a5612019-11-05 16:34:32 -080049 // Return the device for partition |partition_name| at slot |slot|.
50 // |current_slot| should be set to the current active slot.
51 // Note: this function is only used by BootControl*::GetPartitionDevice.
52 // Other callers should prefer BootControl*::GetPartitionDevice over
53 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
54 bool GetPartitionDevice(const std::string& partition_name,
55 uint32_t slot,
56 uint32_t current_slot,
57 std::string* device);
58
Yifan Hong012508e2019-07-22 18:30:40 -070059 protected:
60 // These functions are exposed for testing.
61
62 // Unmap logical partition on device mapper. This is the reverse operation
63 // of MapPartitionOnDeviceMapper.
64 // Returns true if unmapped successfully.
65 virtual bool UnmapPartitionOnDeviceMapper(
66 const std::string& target_partition_name);
67
68 // Retrieve metadata from |super_device| at slot |source_slot|.
69 //
70 // If |target_slot| != kInvalidSlot, before returning the metadata, this
71 // function modifies the metadata so that during updates, the metadata can be
72 // written to |target_slot|. In particular, on retrofit devices, the returned
73 // metadata automatically includes block devices at |target_slot|.
74 //
75 // If |target_slot| == kInvalidSlot, this function returns metadata at
76 // |source_slot| without modifying it. This is the same as
77 // LoadMetadataBuilder(const std::string&, uint32_t).
78 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080079 const std::string& super_device,
80 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070081 uint32_t target_slot);
82
83 // Write metadata |builder| to |super_device| at slot |target_slot|.
84 virtual bool StoreMetadata(const std::string& super_device,
85 android::fs_mgr::MetadataBuilder* builder,
86 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070087
Yifan Hong3a1a5612019-11-05 16:34:32 -080088 // Map logical partition on device-mapper.
89 // |super_device| is the device path of the physical partition ("super").
90 // |target_partition_name| is the identifier used in metadata; for example,
91 // "vendor_a"
92 // |slot| is the selected slot to mount; for example, 0 for "_a".
93 // Returns true if mapped successfully; if so, |path| is set to the device
94 // path of the mapped logical partition.
95 virtual bool MapPartitionOnDeviceMapper(
96 const std::string& super_device,
97 const std::string& target_partition_name,
98 uint32_t slot,
99 bool force_writable,
100 std::string* path);
101
102 // Return true if a static partition exists at device path |path|.
103 virtual bool DeviceExists(const std::string& path);
104
105 // Returns the current state of the underlying device mapper device
106 // with given name.
107 // One of INVALID, SUSPENDED or ACTIVE.
108 virtual android::dm::DmDeviceState GetState(const std::string& name);
109
110 // Returns the path to the device mapper device node in '/dev' corresponding
111 // to 'name'. If the device does not exist, false is returned, and the path
112 // parameter is not set.
113 virtual bool GetDmDevicePathByName(const std::string& name,
114 std::string* path);
115
116 // Retrieve metadata from |super_device| at slot |source_slot|.
117 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
118 const std::string& super_device, uint32_t source_slot);
119
120 // Return a possible location for devices listed by name.
121 virtual bool GetDeviceDir(std::string* path);
122
123 // Return the name of the super partition (which stores super partition
124 // metadata) for a given slot.
125 virtual std::string GetSuperPartitionName(uint32_t slot);
126
Yifan Hong6eec9952019-12-04 13:12:01 -0800127 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
128
Yifan Hong537802d2018-08-15 13:15:42 -0700129 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700130 friend class DynamicPartitionControlAndroidTest;
131
Yifan Hong02513dc2019-10-30 11:23:04 -0700132 void CleanupInternal();
Yifan Hong8546a712019-03-28 14:42:53 -0700133 bool MapPartitionInternal(const std::string& super_device,
134 const std::string& target_partition_name,
135 uint32_t slot,
136 bool force_writable,
137 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700138
Yifan Hong012508e2019-07-22 18:30:40 -0700139 // Update |builder| according to |partition_metadata|, assuming the device
140 // does not have Virtual A/B.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700141 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
142 uint32_t target_slot,
143 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700144
Yifan Hong420db9b2019-07-23 20:50:33 -0700145 // Helper for PreparePartitionsForUpdate. Used for dynamic partitions without
146 // Virtual A/B update.
147 bool PrepareDynamicPartitionsForUpdate(uint32_t source_slot,
148 uint32_t target_slot,
149 const DeltaArchiveManifest& manifest);
150
151 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
152 // Virtual A/B update.
153 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
154 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800155 const DeltaArchiveManifest& manifest,
156 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700157
Yifan Hong3a1a5612019-11-05 16:34:32 -0800158 enum class DynamicPartitionDeviceStatus {
159 SUCCESS,
160 ERROR,
161 TRY_STATIC,
162 };
163
164 // Return SUCCESS and path in |device| if partition is dynamic.
165 // Return ERROR if any error.
166 // Return TRY_STATIC if caller should resolve the partition as a static
167 // partition instead.
168 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
169 const base::FilePath& device_dir,
170 const std::string& partition_name_suffix,
171 uint32_t slot,
172 uint32_t current_slot,
173 std::string* device);
174
175 // Return true if |partition_name_suffix| is a block device of
176 // super partition metadata slot |slot|.
177 bool IsSuperBlockDevice(const base::FilePath& device_dir,
178 uint32_t current_slot,
179 const std::string& partition_name_suffix);
180
Yifan Hong420db9b2019-07-23 20:50:33 -0700181 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700182 const FeatureFlag dynamic_partitions_;
183 const FeatureFlag virtual_ab_;
Yifan Hong420db9b2019-07-23 20:50:33 -0700184 std::unique_ptr<android::snapshot::SnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700185 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700186 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800187 // Whether the target partitions should be loaded as dynamic partitions. Set
188 // by PreparePartitionsForUpdate() per each update.
189 bool is_target_dynamic_ = false;
Yifan Hong6eec9952019-12-04 13:12:01 -0800190 uint32_t source_slot_ = UINT32_MAX;
191 uint32_t target_slot_ = UINT32_MAX;
Yifan Hong420db9b2019-07-23 20:50:33 -0700192
Yifan Hong537802d2018-08-15 13:15:42 -0700193 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
194};
195
196} // namespace chromeos_update_engine
197
198#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_