blob: a2a42cc9101b7271c4bee2344a3c4f557610173e [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
18#define UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
Yifan Hong537802d2018-08-15 13:15:42 -070019
Yifan Hong537802d2018-08-15 13:15:42 -070020#include <memory>
21#include <set>
22#include <string>
Tianjie99d570d2020-06-04 14:57:19 -070023#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070024
Yifan Hong3a1a5612019-11-05 16:34:32 -080025#include <base/files/file_util.h>
Yifan Hong2c62c132019-10-24 14:53:40 -070026#include <libsnapshot/auto_device.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070027#include <libsnapshot/snapshot.h>
Kelvin Zhang34618522020-09-28 09:21:02 -040028#include <libsnapshot/snapshot_writer.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070029
Yifan Hong15726b92019-11-05 19:06:48 -080030#include "update_engine/common/dynamic_partition_control_interface.h"
31
Yifan Hong537802d2018-08-15 13:15:42 -070032namespace chromeos_update_engine {
33
34class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
35 public:
Yifan Hong420db9b2019-07-23 20:50:33 -070036 DynamicPartitionControlAndroid();
Yifan Hong537802d2018-08-15 13:15:42 -070037 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070038 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070039 FeatureFlag GetVirtualAbFeatureFlag() override;
Kelvin Zhangda1b3142020-09-24 17:09:02 -040040 FeatureFlag GetVirtualAbCompressionFeatureFlag() override;
Yifan Hongf5261562020-03-10 10:28:10 -070041 bool OptimizeOperation(const std::string& partition_name,
42 const InstallOperation& operation,
43 InstallOperation* optimized) override;
Yifan Hong537802d2018-08-15 13:15:42 -070044 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070045
Yifan Hongf0f4a912019-09-26 17:51:33 -070046 bool PreparePartitionsForUpdate(uint32_t source_slot,
47 uint32_t target_slot,
48 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080049 bool update,
50 uint64_t* required_size) override;
Yifan Hong7b3910a2020-03-24 17:47:32 -070051 bool FinishUpdate(bool powerwash_required) override;
Yifan Hong90965502020-02-19 15:22:47 -080052 std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
53 BootControlInterface* boot_control,
54 PrefsInterface* prefs,
55 CleanupPreviousUpdateActionDelegateInterface* delegate) override;
Yifan Hong012508e2019-07-22 18:30:40 -070056
Yifan Hong6a6d0f12020-03-11 13:20:52 -070057 bool ResetUpdate(PrefsInterface* prefs) override;
58
Tianjie99d570d2020-06-04 14:57:19 -070059 bool ListDynamicPartitionsForSlot(
60 uint32_t current_slot, std::vector<std::string>* partitions) override;
61
Tianjie24f96092020-06-30 12:26:25 -070062 bool VerifyExtentsForUntouchedPartitions(
63 uint32_t source_slot,
64 uint32_t target_slot,
65 const std::vector<std::string>& partitions) override;
66
Tianjie99d570d2020-06-04 14:57:19 -070067 bool GetDeviceDir(std::string* path) override;
68
Yifan Hong3a1a5612019-11-05 16:34:32 -080069 // Return the device for partition |partition_name| at slot |slot|.
70 // |current_slot| should be set to the current active slot.
71 // Note: this function is only used by BootControl*::GetPartitionDevice.
72 // Other callers should prefer BootControl*::GetPartitionDevice over
73 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
74 bool GetPartitionDevice(const std::string& partition_name,
75 uint32_t slot,
76 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -070077 bool not_in_payload,
78 std::string* device,
79 bool* is_dynamic);
80
81 bool GetPartitionDevice(const std::string& partition_name,
82 uint32_t slot,
83 uint32_t current_slot,
Yifan Hong3a1a5612019-11-05 16:34:32 -080084 std::string* device);
85
Kelvin Zhang34618522020-09-28 09:21:02 -040086 // Partition name is expected to be unsuffixed. e.g. system, vendor
87 // Return an interface to write to a snapshoted partition.
88 std::unique_ptr<android::snapshot::ISnapshotWriter> OpenCowWriter(
89 const std::string& unsuffixed_partition_name,
90 const std::optional<std::string>& source_path,
91 bool is_append) override;
Kelvin Zhangc82511c2020-11-06 16:01:24 -050092 FileDescriptorPtr OpenCowReader(const std::string& unsuffixed_partition_name,
93 const std::optional<std::string>&,
94 bool is_append = false) override;
Kelvin Zhang34618522020-09-28 09:21:02 -040095
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -040096 bool UnmapAllPartitions() override;
97
Yifan Hong012508e2019-07-22 18:30:40 -070098 protected:
99 // These functions are exposed for testing.
100
101 // Unmap logical partition on device mapper. This is the reverse operation
102 // of MapPartitionOnDeviceMapper.
103 // Returns true if unmapped successfully.
104 virtual bool UnmapPartitionOnDeviceMapper(
105 const std::string& target_partition_name);
106
Tianjie24f96092020-06-30 12:26:25 -0700107 // Retrieves metadata from |super_device| at slot |slot|.
108 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
109 const std::string& super_device, uint32_t slot);
110
111 // Retrieves metadata from |super_device| at slot |source_slot|. And modifies
112 // the metadata so that during updates, the metadata can be written to
113 // |target_slot|. In particular, on retrofit devices, the returned metadata
114 // automatically includes block devices at |target_slot|.
Yifan Hong012508e2019-07-22 18:30:40 -0700115 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800116 const std::string& super_device,
117 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -0700118 uint32_t target_slot);
119
120 // Write metadata |builder| to |super_device| at slot |target_slot|.
121 virtual bool StoreMetadata(const std::string& super_device,
122 android::fs_mgr::MetadataBuilder* builder,
123 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700124
Yifan Hong3a1a5612019-11-05 16:34:32 -0800125 // Map logical partition on device-mapper.
126 // |super_device| is the device path of the physical partition ("super").
127 // |target_partition_name| is the identifier used in metadata; for example,
128 // "vendor_a"
129 // |slot| is the selected slot to mount; for example, 0 for "_a".
130 // Returns true if mapped successfully; if so, |path| is set to the device
131 // path of the mapped logical partition.
132 virtual bool MapPartitionOnDeviceMapper(
133 const std::string& super_device,
134 const std::string& target_partition_name,
135 uint32_t slot,
136 bool force_writable,
137 std::string* path);
138
139 // Return true if a static partition exists at device path |path|.
140 virtual bool DeviceExists(const std::string& path);
141
142 // Returns the current state of the underlying device mapper device
143 // with given name.
144 // One of INVALID, SUSPENDED or ACTIVE.
145 virtual android::dm::DmDeviceState GetState(const std::string& name);
146
147 // Returns the path to the device mapper device node in '/dev' corresponding
148 // to 'name'. If the device does not exist, false is returned, and the path
149 // parameter is not set.
150 virtual bool GetDmDevicePathByName(const std::string& name,
151 std::string* path);
152
Yifan Hong3a1a5612019-11-05 16:34:32 -0800153 // Return the name of the super partition (which stores super partition
154 // metadata) for a given slot.
155 virtual std::string GetSuperPartitionName(uint32_t slot);
156
Yifan Hong6eec9952019-12-04 13:12:01 -0800157 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
158
Yifan Hongbae27842019-10-24 16:56:12 -0700159 // Allow mock objects to override this to test recovery mode.
160 virtual bool IsRecovery();
161
Yifan Hong29692902020-03-26 12:47:05 -0700162 // Determine path for system_other partition.
163 // |source_slot| should be current slot.
164 // |target_slot| should be "other" slot.
165 // |partition_name_suffix| should be "system" + suffix(|target_slot|).
166 // Return true and set |path| if successful.
167 // Set |path| to empty if no need to erase system_other.
168 // Set |should_unmap| to true if path needs to be unmapped later.
169 //
170 // Note: system_other cannot use GetPartitionDevice or
171 // GetDynamicPartitionDevice because:
172 // - super partition metadata may be loaded from the source slot
173 // - UPDATED flag needs to be check to skip erasing if partition is not
174 // created by flashing tools
175 // - Snapshots from previous update attempts should not be used.
176 virtual bool GetSystemOtherPath(uint32_t source_slot,
177 uint32_t target_slot,
178 const std::string& partition_name_suffix,
179 std::string* path,
180 bool* should_unmap);
181
182 // Returns true if any entry in the fstab file in |path| has AVB enabled,
183 // false if not enabled, and nullopt for any error.
184 virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path);
185
186 // Returns true if system_other has AVB enabled, false if not enabled, and
187 // nullopt for any error.
188 virtual std::optional<bool> IsAvbEnabledOnSystemOther();
189
190 // Erase system_other partition that may contain system_other.img.
191 // After the update, the content of system_other may be corrupted but with
192 // valid AVB footer. If the update is rolled back and factory data reset is
193 // triggered, system_b fails to be mapped with verity errors (see
194 // b/152444348). Erase the system_other so that mapping system_other is
195 // skipped.
196 virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot,
197 uint32_t target_slot);
198
Yifan Hong302fa702020-04-16 09:48:29 -0700199 // Helper for PreparePartitionsForUpdate. Used for devices with dynamic
200 // partitions updating without snapshots.
201 // If |delete_source| is set, source partitions are deleted before resizing
202 // target partitions (using DeleteSourcePartitions).
203 virtual bool PrepareDynamicPartitionsForUpdate(
204 uint32_t source_slot,
205 uint32_t target_slot,
206 const DeltaArchiveManifest& manifest,
207 bool delete_source);
208
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400209 bool MapAllPartitions() override;
210
Yifan Hong537802d2018-08-15 13:15:42 -0700211 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700212 friend class DynamicPartitionControlAndroidTest;
Yifan Hong302fa702020-04-16 09:48:29 -0700213 friend class SnapshotPartitionTestP;
Yifan Hongc049f932019-07-23 15:06:05 -0700214
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400215 std::optional<base::FilePath> GetSuperDevice();
216
Yifan Hong8546a712019-03-28 14:42:53 -0700217 bool MapPartitionInternal(const std::string& super_device,
218 const std::string& target_partition_name,
219 uint32_t slot,
220 bool force_writable,
221 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700222
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700223 // Update |builder| according to |partition_metadata|.
224 // - In Android mode, this is only called when the device
225 // does not have Virtual A/B.
226 // - When sideloading, this maybe called as a fallback path if CoW cannot
227 // be created.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700228 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
229 uint32_t target_slot,
230 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700231
Yifan Hong420db9b2019-07-23 20:50:33 -0700232 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
233 // Virtual A/B update.
234 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
235 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800236 const DeltaArchiveManifest& manifest,
237 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700238
Yifan Hong3a1a5612019-11-05 16:34:32 -0800239 enum class DynamicPartitionDeviceStatus {
240 SUCCESS,
241 ERROR,
242 TRY_STATIC,
243 };
244
245 // Return SUCCESS and path in |device| if partition is dynamic.
246 // Return ERROR if any error.
247 // Return TRY_STATIC if caller should resolve the partition as a static
248 // partition instead.
249 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
250 const base::FilePath& device_dir,
251 const std::string& partition_name_suffix,
252 uint32_t slot,
253 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -0700254 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -0800255 std::string* device);
256
257 // Return true if |partition_name_suffix| is a block device of
258 // super partition metadata slot |slot|.
259 bool IsSuperBlockDevice(const base::FilePath& device_dir,
260 uint32_t current_slot,
261 const std::string& partition_name_suffix);
262
Yifan Hongbae27842019-10-24 16:56:12 -0700263 // If sideloading a full OTA, delete source partitions from |builder|.
264 bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder,
265 uint32_t source_slot,
266 const DeltaArchiveManifest& manifest);
267
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700268 // Returns true if metadata is expected to be mounted, false otherwise.
269 // Note that it returns false on non-Virtual A/B devices.
270 //
271 // Almost all functions of SnapshotManager depends on metadata being mounted.
272 // - In Android mode for Virtual A/B devices, assume it is mounted. If not,
273 // let caller fails when calling into SnapshotManager.
274 // - In recovery for Virtual A/B devices, it is possible that metadata is not
275 // formatted, hence it cannot be mounted. Caller should not call into
276 // SnapshotManager.
277 // - On non-Virtual A/B devices, updates do not depend on metadata partition.
278 // Caller should not call into SnapshotManager.
279 //
280 // This function does NOT mount metadata partition. Use EnsureMetadataMounted
281 // to mount metadata partition.
282 bool ExpectMetadataMounted();
283
284 // Ensure /metadata is mounted. Returns true if successful, false otherwise.
285 //
286 // Note that this function returns true on non-Virtual A/B devices without
287 // doing anything.
288 bool EnsureMetadataMounted();
289
Yifan Hongf6f75c22020-07-31 15:20:25 -0700290 // Set boolean flags related to target build. This includes flags like
291 // target_supports_snapshot_ and is_target_dynamic_.
292 bool SetTargetBuildVars(const DeltaArchiveManifest& manifest);
293
Yifan Hong420db9b2019-07-23 20:50:33 -0700294 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700295 const FeatureFlag dynamic_partitions_;
296 const FeatureFlag virtual_ab_;
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400297 const FeatureFlag virtual_ab_compression_;
Yifan Hongf9cb4492020-04-15 13:00:20 -0700298 std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700299 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700300 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800301 // Whether the target partitions should be loaded as dynamic partitions. Set
302 // by PreparePartitionsForUpdate() per each update.
303 bool is_target_dynamic_ = false;
Yifan Hong6eec9952019-12-04 13:12:01 -0800304 uint32_t source_slot_ = UINT32_MAX;
305 uint32_t target_slot_ = UINT32_MAX;
Yifan Hong420db9b2019-07-23 20:50:33 -0700306
Yifan Hong537802d2018-08-15 13:15:42 -0700307 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
308};
309
310} // namespace chromeos_update_engine
311
Amin Hassaniec7bc112020-10-29 16:47:58 -0700312#endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_CONTROL_ANDROID_H_