blob: ecab6fa7b587e104ff45d74947855ae13cacf6c0 [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();
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050038
Yifan Hong186bb682019-07-23 14:04:39 -070039 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070040 FeatureFlag GetVirtualAbFeatureFlag() override;
Kelvin Zhangda1b3142020-09-24 17:09:02 -040041 FeatureFlag GetVirtualAbCompressionFeatureFlag() override;
Yifan Hongf5261562020-03-10 10:28:10 -070042 bool OptimizeOperation(const std::string& partition_name,
43 const InstallOperation& operation,
44 InstallOperation* optimized) override;
Yifan Hong537802d2018-08-15 13:15:42 -070045 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070046
Yifan Hongf0f4a912019-09-26 17:51:33 -070047 bool PreparePartitionsForUpdate(uint32_t source_slot,
48 uint32_t target_slot,
49 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080050 bool update,
51 uint64_t* required_size) override;
Yifan Hong7b3910a2020-03-24 17:47:32 -070052 bool FinishUpdate(bool powerwash_required) override;
Yifan Hong90965502020-02-19 15:22:47 -080053 std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
54 BootControlInterface* boot_control,
55 PrefsInterface* prefs,
56 CleanupPreviousUpdateActionDelegateInterface* delegate) override;
Yifan Hong012508e2019-07-22 18:30:40 -070057
Yifan Hong6a6d0f12020-03-11 13:20:52 -070058 bool ResetUpdate(PrefsInterface* prefs) override;
59
Tianjie99d570d2020-06-04 14:57:19 -070060 bool ListDynamicPartitionsForSlot(
61 uint32_t current_slot, std::vector<std::string>* partitions) override;
62
Tianjie24f96092020-06-30 12:26:25 -070063 bool VerifyExtentsForUntouchedPartitions(
64 uint32_t source_slot,
65 uint32_t target_slot,
66 const std::vector<std::string>& partitions) override;
67
Tianjie99d570d2020-06-04 14:57:19 -070068 bool GetDeviceDir(std::string* path) override;
69
Yifan Hong3a1a5612019-11-05 16:34:32 -080070 // Return the device for partition |partition_name| at slot |slot|.
71 // |current_slot| should be set to the current active slot.
72 // Note: this function is only used by BootControl*::GetPartitionDevice.
73 // Other callers should prefer BootControl*::GetPartitionDevice over
74 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050075 std::optional<PartitionDevice> GetPartitionDevice(
76 const std::string& partition_name,
77 uint32_t slot,
78 uint32_t current_slot,
79 bool not_in_payload = false);
80 // Deprecated, please use GetPartitionDevice(string, uint32_t, uint32_t);
81 // TODO(zhangkelvin) Remove below deprecated APIs.
Yifan Hong3a1a5612019-11-05 16:34:32 -080082 bool GetPartitionDevice(const std::string& partition_name,
83 uint32_t slot,
84 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -070085 bool not_in_payload,
86 std::string* device,
87 bool* is_dynamic);
88
89 bool GetPartitionDevice(const std::string& partition_name,
90 uint32_t slot,
91 uint32_t current_slot,
Yifan Hong3a1a5612019-11-05 16:34:32 -080092 std::string* device);
93
Kelvin Zhang34618522020-09-28 09:21:02 -040094 // Partition name is expected to be unsuffixed. e.g. system, vendor
95 // Return an interface to write to a snapshoted partition.
96 std::unique_ptr<android::snapshot::ISnapshotWriter> OpenCowWriter(
97 const std::string& unsuffixed_partition_name,
98 const std::optional<std::string>& source_path,
99 bool is_append) override;
Kelvin Zhangc82511c2020-11-06 16:01:24 -0500100 FileDescriptorPtr OpenCowReader(const std::string& unsuffixed_partition_name,
101 const std::optional<std::string>&,
102 bool is_append = false) override;
Kelvin Zhang34618522020-09-28 09:21:02 -0400103
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400104 bool UnmapAllPartitions() override;
105
Kelvin Zhangeb9de162020-11-16 15:47:28 -0500106 bool IsDynamicPartition(const std::string& part_name) override;
107
Yifan Hong012508e2019-07-22 18:30:40 -0700108 protected:
109 // These functions are exposed for testing.
110
111 // Unmap logical partition on device mapper. This is the reverse operation
112 // of MapPartitionOnDeviceMapper.
113 // Returns true if unmapped successfully.
114 virtual bool UnmapPartitionOnDeviceMapper(
115 const std::string& target_partition_name);
116
Tianjie24f96092020-06-30 12:26:25 -0700117 // Retrieves metadata from |super_device| at slot |slot|.
118 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
119 const std::string& super_device, uint32_t slot);
120
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500121 // Retrieves metadata from |super_device| at slot |source_slot|. And
122 // modifies the metadata so that during updates, the metadata can be written
123 // to |target_slot|. In particular, on retrofit devices, the returned
124 // metadata automatically includes block devices at |target_slot|.
Yifan Hong012508e2019-07-22 18:30:40 -0700125 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800126 const std::string& super_device,
127 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -0700128 uint32_t target_slot);
129
130 // Write metadata |builder| to |super_device| at slot |target_slot|.
131 virtual bool StoreMetadata(const std::string& super_device,
132 android::fs_mgr::MetadataBuilder* builder,
133 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700134
Yifan Hong3a1a5612019-11-05 16:34:32 -0800135 // Map logical partition on device-mapper.
136 // |super_device| is the device path of the physical partition ("super").
137 // |target_partition_name| is the identifier used in metadata; for example,
138 // "vendor_a"
139 // |slot| is the selected slot to mount; for example, 0 for "_a".
140 // Returns true if mapped successfully; if so, |path| is set to the device
141 // path of the mapped logical partition.
142 virtual bool MapPartitionOnDeviceMapper(
143 const std::string& super_device,
144 const std::string& target_partition_name,
145 uint32_t slot,
146 bool force_writable,
147 std::string* path);
148
149 // Return true if a static partition exists at device path |path|.
150 virtual bool DeviceExists(const std::string& path);
151
152 // Returns the current state of the underlying device mapper device
153 // with given name.
154 // One of INVALID, SUSPENDED or ACTIVE.
155 virtual android::dm::DmDeviceState GetState(const std::string& name);
156
157 // Returns the path to the device mapper device node in '/dev' corresponding
158 // to 'name'. If the device does not exist, false is returned, and the path
159 // parameter is not set.
160 virtual bool GetDmDevicePathByName(const std::string& name,
161 std::string* path);
162
Yifan Hong3a1a5612019-11-05 16:34:32 -0800163 // Return the name of the super partition (which stores super partition
164 // metadata) for a given slot.
165 virtual std::string GetSuperPartitionName(uint32_t slot);
166
Yifan Hong6eec9952019-12-04 13:12:01 -0800167 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
168
Yifan Hongbae27842019-10-24 16:56:12 -0700169 // Allow mock objects to override this to test recovery mode.
170 virtual bool IsRecovery();
171
Yifan Hong29692902020-03-26 12:47:05 -0700172 // Determine path for system_other partition.
173 // |source_slot| should be current slot.
174 // |target_slot| should be "other" slot.
175 // |partition_name_suffix| should be "system" + suffix(|target_slot|).
176 // Return true and set |path| if successful.
177 // Set |path| to empty if no need to erase system_other.
178 // Set |should_unmap| to true if path needs to be unmapped later.
179 //
180 // Note: system_other cannot use GetPartitionDevice or
181 // GetDynamicPartitionDevice because:
182 // - super partition metadata may be loaded from the source slot
183 // - UPDATED flag needs to be check to skip erasing if partition is not
184 // created by flashing tools
185 // - Snapshots from previous update attempts should not be used.
186 virtual bool GetSystemOtherPath(uint32_t source_slot,
187 uint32_t target_slot,
188 const std::string& partition_name_suffix,
189 std::string* path,
190 bool* should_unmap);
191
192 // Returns true if any entry in the fstab file in |path| has AVB enabled,
193 // false if not enabled, and nullopt for any error.
194 virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path);
195
196 // Returns true if system_other has AVB enabled, false if not enabled, and
197 // nullopt for any error.
198 virtual std::optional<bool> IsAvbEnabledOnSystemOther();
199
200 // Erase system_other partition that may contain system_other.img.
201 // After the update, the content of system_other may be corrupted but with
202 // valid AVB footer. If the update is rolled back and factory data reset is
203 // triggered, system_b fails to be mapped with verity errors (see
204 // b/152444348). Erase the system_other so that mapping system_other is
205 // skipped.
206 virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot,
207 uint32_t target_slot);
208
Yifan Hong302fa702020-04-16 09:48:29 -0700209 // Helper for PreparePartitionsForUpdate. Used for devices with dynamic
210 // partitions updating without snapshots.
211 // If |delete_source| is set, source partitions are deleted before resizing
212 // target partitions (using DeleteSourcePartitions).
213 virtual bool PrepareDynamicPartitionsForUpdate(
214 uint32_t source_slot,
215 uint32_t target_slot,
216 const DeltaArchiveManifest& manifest,
217 bool delete_source);
218
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400219 bool MapAllPartitions() override;
220
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500221 void SetSourceSlot(uint32_t slot) { source_slot_ = slot; }
222 void SetTargetSlot(uint32_t slot) { target_slot_ = slot; }
223
Yifan Hong537802d2018-08-15 13:15:42 -0700224 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700225 friend class DynamicPartitionControlAndroidTest;
Yifan Hong302fa702020-04-16 09:48:29 -0700226 friend class SnapshotPartitionTestP;
Yifan Hongc049f932019-07-23 15:06:05 -0700227
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400228 std::optional<base::FilePath> GetSuperDevice();
229
Yifan Hong8546a712019-03-28 14:42:53 -0700230 bool MapPartitionInternal(const std::string& super_device,
231 const std::string& target_partition_name,
232 uint32_t slot,
233 bool force_writable,
234 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700235
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700236 // Update |builder| according to |partition_metadata|.
237 // - In Android mode, this is only called when the device
238 // does not have Virtual A/B.
239 // - When sideloading, this maybe called as a fallback path if CoW cannot
240 // be created.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700241 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
242 uint32_t target_slot,
243 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700244
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500245 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions
246 // for Virtual A/B update.
Yifan Hong420db9b2019-07-23 20:50:33 -0700247 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
248 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800249 const DeltaArchiveManifest& manifest,
250 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700251
Yifan Hong3a1a5612019-11-05 16:34:32 -0800252 enum class DynamicPartitionDeviceStatus {
253 SUCCESS,
254 ERROR,
255 TRY_STATIC,
256 };
257
258 // Return SUCCESS and path in |device| if partition is dynamic.
259 // Return ERROR if any error.
260 // Return TRY_STATIC if caller should resolve the partition as a static
261 // partition instead.
262 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
263 const base::FilePath& device_dir,
264 const std::string& partition_name_suffix,
265 uint32_t slot,
266 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -0700267 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -0800268 std::string* device);
269
270 // Return true if |partition_name_suffix| is a block device of
271 // super partition metadata slot |slot|.
272 bool IsSuperBlockDevice(const base::FilePath& device_dir,
273 uint32_t current_slot,
274 const std::string& partition_name_suffix);
275
Yifan Hongbae27842019-10-24 16:56:12 -0700276 // If sideloading a full OTA, delete source partitions from |builder|.
277 bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder,
278 uint32_t source_slot,
279 const DeltaArchiveManifest& manifest);
280
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700281 // Returns true if metadata is expected to be mounted, false otherwise.
282 // Note that it returns false on non-Virtual A/B devices.
283 //
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500284 // Almost all functions of SnapshotManager depends on metadata being
285 // mounted.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700286 // - In Android mode for Virtual A/B devices, assume it is mounted. If not,
287 // let caller fails when calling into SnapshotManager.
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500288 // - In recovery for Virtual A/B devices, it is possible that metadata is
289 // not
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700290 // formatted, hence it cannot be mounted. Caller should not call into
291 // SnapshotManager.
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500292 // - On non-Virtual A/B devices, updates do not depend on metadata
293 // partition.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700294 // Caller should not call into SnapshotManager.
295 //
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500296 // This function does NOT mount metadata partition. Use
297 // EnsureMetadataMounted to mount metadata partition.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700298 bool ExpectMetadataMounted();
299
300 // Ensure /metadata is mounted. Returns true if successful, false otherwise.
301 //
302 // Note that this function returns true on non-Virtual A/B devices without
303 // doing anything.
304 bool EnsureMetadataMounted();
305
Yifan Hongf6f75c22020-07-31 15:20:25 -0700306 // Set boolean flags related to target build. This includes flags like
307 // target_supports_snapshot_ and is_target_dynamic_.
308 bool SetTargetBuildVars(const DeltaArchiveManifest& manifest);
309
Yifan Hong420db9b2019-07-23 20:50:33 -0700310 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700311 const FeatureFlag dynamic_partitions_;
312 const FeatureFlag virtual_ab_;
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400313 const FeatureFlag virtual_ab_compression_;
Yifan Hongf9cb4492020-04-15 13:00:20 -0700314 std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700315 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700316 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800317 // Whether the target partitions should be loaded as dynamic partitions. Set
318 // by PreparePartitionsForUpdate() per each update.
319 bool is_target_dynamic_ = false;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500320
Yifan Hong6eec9952019-12-04 13:12:01 -0800321 uint32_t source_slot_ = UINT32_MAX;
322 uint32_t target_slot_ = UINT32_MAX;
Kelvin Zhangeb9de162020-11-16 15:47:28 -0500323 std::vector<std::string> dynamic_partition_list_;
Yifan Hong420db9b2019-07-23 20:50:33 -0700324
Yifan Hong537802d2018-08-15 13:15:42 -0700325 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
326};
327
328} // namespace chromeos_update_engine
329
Amin Hassaniec7bc112020-10-29 16:47:58 -0700330#endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_CONTROL_ANDROID_H_