blob: f3805f0e280906bef8a3175493ddec1561fb6e31 [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>
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>
28
Yifan Hong15726b92019-11-05 19:06:48 -080029#include "update_engine/common/dynamic_partition_control_interface.h"
30
Yifan Hong537802d2018-08-15 13:15:42 -070031namespace chromeos_update_engine {
32
33class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
34 public:
Yifan Hong420db9b2019-07-23 20:50:33 -070035 DynamicPartitionControlAndroid();
Yifan Hong537802d2018-08-15 13:15:42 -070036 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070037 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070038 FeatureFlag GetVirtualAbFeatureFlag() override;
Kelvin Zhangda1b3142020-09-24 17:09:02 -040039 FeatureFlag GetVirtualAbCompressionFeatureFlag() override;
Yifan Hongf5261562020-03-10 10:28:10 -070040 bool OptimizeOperation(const std::string& partition_name,
41 const InstallOperation& operation,
42 InstallOperation* optimized) override;
Yifan Hong537802d2018-08-15 13:15:42 -070043 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070044
Yifan Hongf0f4a912019-09-26 17:51:33 -070045 bool PreparePartitionsForUpdate(uint32_t source_slot,
46 uint32_t target_slot,
47 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080048 bool update,
49 uint64_t* required_size) override;
Yifan Hong7b3910a2020-03-24 17:47:32 -070050 bool FinishUpdate(bool powerwash_required) override;
Yifan Hong90965502020-02-19 15:22:47 -080051 std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
52 BootControlInterface* boot_control,
53 PrefsInterface* prefs,
54 CleanupPreviousUpdateActionDelegateInterface* delegate) override;
Yifan Hong012508e2019-07-22 18:30:40 -070055
Yifan Hong6a6d0f12020-03-11 13:20:52 -070056 bool ResetUpdate(PrefsInterface* prefs) override;
57
Tianjie99d570d2020-06-04 14:57:19 -070058 bool ListDynamicPartitionsForSlot(
59 uint32_t current_slot, std::vector<std::string>* partitions) override;
60
Tianjie24f96092020-06-30 12:26:25 -070061 bool VerifyExtentsForUntouchedPartitions(
62 uint32_t source_slot,
63 uint32_t target_slot,
64 const std::vector<std::string>& partitions) override;
65
Tianjie99d570d2020-06-04 14:57:19 -070066 bool GetDeviceDir(std::string* path) override;
67
Yifan Hong3a1a5612019-11-05 16:34:32 -080068 // Return the device for partition |partition_name| at slot |slot|.
69 // |current_slot| should be set to the current active slot.
70 // Note: this function is only used by BootControl*::GetPartitionDevice.
71 // Other callers should prefer BootControl*::GetPartitionDevice over
72 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
73 bool GetPartitionDevice(const std::string& partition_name,
74 uint32_t slot,
75 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -070076 bool not_in_payload,
77 std::string* device,
78 bool* is_dynamic);
79
80 bool GetPartitionDevice(const std::string& partition_name,
81 uint32_t slot,
82 uint32_t current_slot,
Yifan Hong3a1a5612019-11-05 16:34:32 -080083 std::string* device);
84
Yifan Hong012508e2019-07-22 18:30:40 -070085 protected:
86 // These functions are exposed for testing.
87
88 // Unmap logical partition on device mapper. This is the reverse operation
89 // of MapPartitionOnDeviceMapper.
90 // Returns true if unmapped successfully.
91 virtual bool UnmapPartitionOnDeviceMapper(
92 const std::string& target_partition_name);
93
Tianjie24f96092020-06-30 12:26:25 -070094 // Retrieves metadata from |super_device| at slot |slot|.
95 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
96 const std::string& super_device, uint32_t slot);
97
98 // Retrieves metadata from |super_device| at slot |source_slot|. And modifies
99 // the metadata so that during updates, the metadata can be written to
100 // |target_slot|. In particular, on retrofit devices, the returned metadata
101 // automatically includes block devices at |target_slot|.
Yifan Hong012508e2019-07-22 18:30:40 -0700102 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800103 const std::string& super_device,
104 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -0700105 uint32_t target_slot);
106
107 // Write metadata |builder| to |super_device| at slot |target_slot|.
108 virtual bool StoreMetadata(const std::string& super_device,
109 android::fs_mgr::MetadataBuilder* builder,
110 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700111
Yifan Hong3a1a5612019-11-05 16:34:32 -0800112 // Map logical partition on device-mapper.
113 // |super_device| is the device path of the physical partition ("super").
114 // |target_partition_name| is the identifier used in metadata; for example,
115 // "vendor_a"
116 // |slot| is the selected slot to mount; for example, 0 for "_a".
117 // Returns true if mapped successfully; if so, |path| is set to the device
118 // path of the mapped logical partition.
119 virtual bool MapPartitionOnDeviceMapper(
120 const std::string& super_device,
121 const std::string& target_partition_name,
122 uint32_t slot,
123 bool force_writable,
124 std::string* path);
125
126 // Return true if a static partition exists at device path |path|.
127 virtual bool DeviceExists(const std::string& path);
128
129 // Returns the current state of the underlying device mapper device
130 // with given name.
131 // One of INVALID, SUSPENDED or ACTIVE.
132 virtual android::dm::DmDeviceState GetState(const std::string& name);
133
134 // Returns the path to the device mapper device node in '/dev' corresponding
135 // to 'name'. If the device does not exist, false is returned, and the path
136 // parameter is not set.
137 virtual bool GetDmDevicePathByName(const std::string& name,
138 std::string* path);
139
Yifan Hong3a1a5612019-11-05 16:34:32 -0800140 // Return the name of the super partition (which stores super partition
141 // metadata) for a given slot.
142 virtual std::string GetSuperPartitionName(uint32_t slot);
143
Yifan Hong6eec9952019-12-04 13:12:01 -0800144 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
145
Yifan Hongbae27842019-10-24 16:56:12 -0700146 // Allow mock objects to override this to test recovery mode.
147 virtual bool IsRecovery();
148
Yifan Hong29692902020-03-26 12:47:05 -0700149 // Determine path for system_other partition.
150 // |source_slot| should be current slot.
151 // |target_slot| should be "other" slot.
152 // |partition_name_suffix| should be "system" + suffix(|target_slot|).
153 // Return true and set |path| if successful.
154 // Set |path| to empty if no need to erase system_other.
155 // Set |should_unmap| to true if path needs to be unmapped later.
156 //
157 // Note: system_other cannot use GetPartitionDevice or
158 // GetDynamicPartitionDevice because:
159 // - super partition metadata may be loaded from the source slot
160 // - UPDATED flag needs to be check to skip erasing if partition is not
161 // created by flashing tools
162 // - Snapshots from previous update attempts should not be used.
163 virtual bool GetSystemOtherPath(uint32_t source_slot,
164 uint32_t target_slot,
165 const std::string& partition_name_suffix,
166 std::string* path,
167 bool* should_unmap);
168
169 // Returns true if any entry in the fstab file in |path| has AVB enabled,
170 // false if not enabled, and nullopt for any error.
171 virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path);
172
173 // Returns true if system_other has AVB enabled, false if not enabled, and
174 // nullopt for any error.
175 virtual std::optional<bool> IsAvbEnabledOnSystemOther();
176
177 // Erase system_other partition that may contain system_other.img.
178 // After the update, the content of system_other may be corrupted but with
179 // valid AVB footer. If the update is rolled back and factory data reset is
180 // triggered, system_b fails to be mapped with verity errors (see
181 // b/152444348). Erase the system_other so that mapping system_other is
182 // skipped.
183 virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot,
184 uint32_t target_slot);
185
Yifan Hong302fa702020-04-16 09:48:29 -0700186 // Helper for PreparePartitionsForUpdate. Used for devices with dynamic
187 // partitions updating without snapshots.
188 // If |delete_source| is set, source partitions are deleted before resizing
189 // target partitions (using DeleteSourcePartitions).
190 virtual bool PrepareDynamicPartitionsForUpdate(
191 uint32_t source_slot,
192 uint32_t target_slot,
193 const DeltaArchiveManifest& manifest,
194 bool delete_source);
195
Yifan Hong537802d2018-08-15 13:15:42 -0700196 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700197 friend class DynamicPartitionControlAndroidTest;
Yifan Hong302fa702020-04-16 09:48:29 -0700198 friend class SnapshotPartitionTestP;
Yifan Hongc049f932019-07-23 15:06:05 -0700199
Yifan Hongbae27842019-10-24 16:56:12 -0700200 void UnmapAllPartitions();
Yifan Hong8546a712019-03-28 14:42:53 -0700201 bool MapPartitionInternal(const std::string& super_device,
202 const std::string& target_partition_name,
203 uint32_t slot,
204 bool force_writable,
205 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700206
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700207 // Update |builder| according to |partition_metadata|.
208 // - In Android mode, this is only called when the device
209 // does not have Virtual A/B.
210 // - When sideloading, this maybe called as a fallback path if CoW cannot
211 // be created.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700212 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
213 uint32_t target_slot,
214 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700215
Yifan Hong420db9b2019-07-23 20:50:33 -0700216 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
217 // Virtual A/B update.
218 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
219 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800220 const DeltaArchiveManifest& manifest,
221 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700222
Yifan Hong3a1a5612019-11-05 16:34:32 -0800223 enum class DynamicPartitionDeviceStatus {
224 SUCCESS,
225 ERROR,
226 TRY_STATIC,
227 };
228
229 // Return SUCCESS and path in |device| if partition is dynamic.
230 // Return ERROR if any error.
231 // Return TRY_STATIC if caller should resolve the partition as a static
232 // partition instead.
233 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
234 const base::FilePath& device_dir,
235 const std::string& partition_name_suffix,
236 uint32_t slot,
237 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -0700238 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -0800239 std::string* device);
240
241 // Return true if |partition_name_suffix| is a block device of
242 // super partition metadata slot |slot|.
243 bool IsSuperBlockDevice(const base::FilePath& device_dir,
244 uint32_t current_slot,
245 const std::string& partition_name_suffix);
246
Yifan Hongbae27842019-10-24 16:56:12 -0700247 // If sideloading a full OTA, delete source partitions from |builder|.
248 bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder,
249 uint32_t source_slot,
250 const DeltaArchiveManifest& manifest);
251
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700252 // Returns true if metadata is expected to be mounted, false otherwise.
253 // Note that it returns false on non-Virtual A/B devices.
254 //
255 // Almost all functions of SnapshotManager depends on metadata being mounted.
256 // - In Android mode for Virtual A/B devices, assume it is mounted. If not,
257 // let caller fails when calling into SnapshotManager.
258 // - In recovery for Virtual A/B devices, it is possible that metadata is not
259 // formatted, hence it cannot be mounted. Caller should not call into
260 // SnapshotManager.
261 // - On non-Virtual A/B devices, updates do not depend on metadata partition.
262 // Caller should not call into SnapshotManager.
263 //
264 // This function does NOT mount metadata partition. Use EnsureMetadataMounted
265 // to mount metadata partition.
266 bool ExpectMetadataMounted();
267
268 // Ensure /metadata is mounted. Returns true if successful, false otherwise.
269 //
270 // Note that this function returns true on non-Virtual A/B devices without
271 // doing anything.
272 bool EnsureMetadataMounted();
273
Yifan Hongf6f75c22020-07-31 15:20:25 -0700274 // Set boolean flags related to target build. This includes flags like
275 // target_supports_snapshot_ and is_target_dynamic_.
276 bool SetTargetBuildVars(const DeltaArchiveManifest& manifest);
277
Yifan Hong420db9b2019-07-23 20:50:33 -0700278 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700279 const FeatureFlag dynamic_partitions_;
280 const FeatureFlag virtual_ab_;
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400281 const FeatureFlag virtual_ab_compression_;
Yifan Hongf9cb4492020-04-15 13:00:20 -0700282 std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700283 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700284 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800285 // Whether the target partitions should be loaded as dynamic partitions. Set
286 // by PreparePartitionsForUpdate() per each update.
287 bool is_target_dynamic_ = false;
Yifan Hong6eec9952019-12-04 13:12:01 -0800288 uint32_t source_slot_ = UINT32_MAX;
289 uint32_t target_slot_ = UINT32_MAX;
Yifan Hong420db9b2019-07-23 20:50:33 -0700290
Yifan Hong537802d2018-08-15 13:15:42 -0700291 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
292};
293
294} // namespace chromeos_update_engine
295
296#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_