blob: cebca07f1d440460151495ccb8262786f0cc3ff2 [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>
Kelvin Zhang91ad6622021-03-01 13:46:17 -050023#include <string_view>
Tianjie99d570d2020-06-04 14:57:19 -070024#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070025
Yifan Hong3a1a5612019-11-05 16:34:32 -080026#include <base/files/file_util.h>
Yifan Hong2c62c132019-10-24 14:53:40 -070027#include <libsnapshot/auto_device.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070028#include <libsnapshot/snapshot.h>
Kelvin Zhang34618522020-09-28 09:21:02 -040029#include <libsnapshot/snapshot_writer.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070030
Yifan Hong15726b92019-11-05 19:06:48 -080031#include "update_engine/common/dynamic_partition_control_interface.h"
32
Yifan Hong537802d2018-08-15 13:15:42 -070033namespace chromeos_update_engine {
34
35class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
36 public:
Kelvin Zhang91ad6622021-03-01 13:46:17 -050037 // A directory where all partitions mapped by VABC is expected to be found.
38 // Per earlier discussion with VAB team, this directory is unlikely to change.
39 // So we declare it as a constant here.
40 static constexpr std::string_view VABC_DEVICE_DIR = "/dev/block/mapper/";
Kelvin Zhangebd115e2021-03-08 16:10:25 -050041 explicit DynamicPartitionControlAndroid(uint32_t source_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070042 ~DynamicPartitionControlAndroid();
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050043
Yifan Hong186bb682019-07-23 14:04:39 -070044 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070045 FeatureFlag GetVirtualAbFeatureFlag() override;
Kelvin Zhangda1b3142020-09-24 17:09:02 -040046 FeatureFlag GetVirtualAbCompressionFeatureFlag() override;
Yifan Hongf5261562020-03-10 10:28:10 -070047 bool OptimizeOperation(const std::string& partition_name,
48 const InstallOperation& operation,
49 InstallOperation* optimized) override;
Yifan Hong537802d2018-08-15 13:15:42 -070050 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070051
Yifan Hongf0f4a912019-09-26 17:51:33 -070052 bool PreparePartitionsForUpdate(uint32_t source_slot,
53 uint32_t target_slot,
54 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080055 bool update,
56 uint64_t* required_size) override;
Yifan Hong7b3910a2020-03-24 17:47:32 -070057 bool FinishUpdate(bool powerwash_required) override;
Yifan Hong90965502020-02-19 15:22:47 -080058 std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
59 BootControlInterface* boot_control,
60 PrefsInterface* prefs,
61 CleanupPreviousUpdateActionDelegateInterface* delegate) override;
Yifan Hong012508e2019-07-22 18:30:40 -070062
Yifan Hong6a6d0f12020-03-11 13:20:52 -070063 bool ResetUpdate(PrefsInterface* prefs) override;
64
Tianjie99d570d2020-06-04 14:57:19 -070065 bool ListDynamicPartitionsForSlot(
Tianjie3a55fc22021-02-13 16:02:22 -080066 uint32_t slot,
67 uint32_t current_slot,
68 std::vector<std::string>* partitions) override;
Tianjie99d570d2020-06-04 14:57:19 -070069
Tianjie24f96092020-06-30 12:26:25 -070070 bool VerifyExtentsForUntouchedPartitions(
71 uint32_t source_slot,
72 uint32_t target_slot,
73 const std::vector<std::string>& partitions) override;
74
Tianjie99d570d2020-06-04 14:57:19 -070075 bool GetDeviceDir(std::string* path) override;
76
Yifan Hong3a1a5612019-11-05 16:34:32 -080077 // Return the device for partition |partition_name| at slot |slot|.
78 // |current_slot| should be set to the current active slot.
79 // Note: this function is only used by BootControl*::GetPartitionDevice.
80 // Other callers should prefer BootControl*::GetPartitionDevice over
81 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050082 std::optional<PartitionDevice> GetPartitionDevice(
83 const std::string& partition_name,
84 uint32_t slot,
85 uint32_t current_slot,
Kelvin Zhang66a9ebb2021-01-25 13:35:10 -050086 bool not_in_payload);
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050087 // Deprecated, please use GetPartitionDevice(string, uint32_t, uint32_t);
88 // TODO(zhangkelvin) Remove below deprecated APIs.
Yifan Hong3a1a5612019-11-05 16:34:32 -080089 bool GetPartitionDevice(const std::string& partition_name,
90 uint32_t slot,
91 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -070092 bool not_in_payload,
93 std::string* device,
94 bool* is_dynamic);
95
96 bool GetPartitionDevice(const std::string& partition_name,
97 uint32_t slot,
98 uint32_t current_slot,
Yifan Hong3a1a5612019-11-05 16:34:32 -080099 std::string* device);
100
Kelvin Zhang34618522020-09-28 09:21:02 -0400101 // Partition name is expected to be unsuffixed. e.g. system, vendor
102 // Return an interface to write to a snapshoted partition.
103 std::unique_ptr<android::snapshot::ISnapshotWriter> OpenCowWriter(
104 const std::string& unsuffixed_partition_name,
105 const std::optional<std::string>& source_path,
106 bool is_append) override;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800107 std::unique_ptr<FileDescriptor> OpenCowFd(
108 const std::string& unsuffixed_partition_name,
109 const std::optional<std::string>&,
110 bool is_append = false) override;
Kelvin Zhang34618522020-09-28 09:21:02 -0400111
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700112 bool MapAllPartitions() override;
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400113 bool UnmapAllPartitions() override;
114
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500115 bool IsDynamicPartition(const std::string& part_name, uint32_t slot) override;
Kelvin Zhangeb9de162020-11-16 15:47:28 -0500116
Yifan Hongb0cbd392021-02-04 11:11:45 -0800117 bool UpdateUsesSnapshotCompression() override;
118
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700119 std::optional<base::FilePath> GetSuperDevice();
120
Yifan Hong012508e2019-07-22 18:30:40 -0700121 protected:
122 // These functions are exposed for testing.
123
124 // Unmap logical partition on device mapper. This is the reverse operation
125 // of MapPartitionOnDeviceMapper.
126 // Returns true if unmapped successfully.
127 virtual bool UnmapPartitionOnDeviceMapper(
128 const std::string& target_partition_name);
129
Tianjie24f96092020-06-30 12:26:25 -0700130 // Retrieves metadata from |super_device| at slot |slot|.
131 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
132 const std::string& super_device, uint32_t slot);
133
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500134 // Retrieves metadata from |super_device| at slot |source_slot|. And
135 // modifies the metadata so that during updates, the metadata can be written
136 // to |target_slot|. In particular, on retrofit devices, the returned
137 // metadata automatically includes block devices at |target_slot|.
Yifan Hong012508e2019-07-22 18:30:40 -0700138 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800139 const std::string& super_device,
140 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -0700141 uint32_t target_slot);
142
143 // Write metadata |builder| to |super_device| at slot |target_slot|.
144 virtual bool StoreMetadata(const std::string& super_device,
145 android::fs_mgr::MetadataBuilder* builder,
146 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700147
Yifan Hong3a1a5612019-11-05 16:34:32 -0800148 // Map logical partition on device-mapper.
149 // |super_device| is the device path of the physical partition ("super").
150 // |target_partition_name| is the identifier used in metadata; for example,
151 // "vendor_a"
152 // |slot| is the selected slot to mount; for example, 0 for "_a".
153 // Returns true if mapped successfully; if so, |path| is set to the device
154 // path of the mapped logical partition.
155 virtual bool MapPartitionOnDeviceMapper(
156 const std::string& super_device,
157 const std::string& target_partition_name,
158 uint32_t slot,
159 bool force_writable,
160 std::string* path);
161
162 // Return true if a static partition exists at device path |path|.
163 virtual bool DeviceExists(const std::string& path);
164
165 // Returns the current state of the underlying device mapper device
166 // with given name.
167 // One of INVALID, SUSPENDED or ACTIVE.
168 virtual android::dm::DmDeviceState GetState(const std::string& name);
169
170 // Returns the path to the device mapper device node in '/dev' corresponding
171 // to 'name'. If the device does not exist, false is returned, and the path
172 // parameter is not set.
173 virtual bool GetDmDevicePathByName(const std::string& name,
174 std::string* path);
175
Yifan Hong3a1a5612019-11-05 16:34:32 -0800176 // Return the name of the super partition (which stores super partition
177 // metadata) for a given slot.
178 virtual std::string GetSuperPartitionName(uint32_t slot);
179
Yifan Hong6eec9952019-12-04 13:12:01 -0800180 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
181
Yifan Hongbae27842019-10-24 16:56:12 -0700182 // Allow mock objects to override this to test recovery mode.
183 virtual bool IsRecovery();
184
Yifan Hong29692902020-03-26 12:47:05 -0700185 // Determine path for system_other partition.
186 // |source_slot| should be current slot.
187 // |target_slot| should be "other" slot.
188 // |partition_name_suffix| should be "system" + suffix(|target_slot|).
189 // Return true and set |path| if successful.
190 // Set |path| to empty if no need to erase system_other.
191 // Set |should_unmap| to true if path needs to be unmapped later.
192 //
193 // Note: system_other cannot use GetPartitionDevice or
194 // GetDynamicPartitionDevice because:
195 // - super partition metadata may be loaded from the source slot
196 // - UPDATED flag needs to be check to skip erasing if partition is not
197 // created by flashing tools
198 // - Snapshots from previous update attempts should not be used.
199 virtual bool GetSystemOtherPath(uint32_t source_slot,
200 uint32_t target_slot,
201 const std::string& partition_name_suffix,
202 std::string* path,
203 bool* should_unmap);
204
205 // Returns true if any entry in the fstab file in |path| has AVB enabled,
206 // false if not enabled, and nullopt for any error.
207 virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path);
208
209 // Returns true if system_other has AVB enabled, false if not enabled, and
210 // nullopt for any error.
211 virtual std::optional<bool> IsAvbEnabledOnSystemOther();
212
213 // Erase system_other partition that may contain system_other.img.
214 // After the update, the content of system_other may be corrupted but with
215 // valid AVB footer. If the update is rolled back and factory data reset is
216 // triggered, system_b fails to be mapped with verity errors (see
217 // b/152444348). Erase the system_other so that mapping system_other is
218 // skipped.
219 virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot,
220 uint32_t target_slot);
221
Yifan Hong302fa702020-04-16 09:48:29 -0700222 // Helper for PreparePartitionsForUpdate. Used for devices with dynamic
223 // partitions updating without snapshots.
224 // If |delete_source| is set, source partitions are deleted before resizing
225 // target partitions (using DeleteSourcePartitions).
226 virtual bool PrepareDynamicPartitionsForUpdate(
227 uint32_t source_slot,
228 uint32_t target_slot,
229 const DeltaArchiveManifest& manifest,
230 bool delete_source);
231
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500232 void SetSourceSlot(uint32_t slot) { source_slot_ = slot; }
233 void SetTargetSlot(uint32_t slot) { target_slot_ = slot; }
234
Yifan Hong537802d2018-08-15 13:15:42 -0700235 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700236 friend class DynamicPartitionControlAndroidTest;
Yifan Hong302fa702020-04-16 09:48:29 -0700237 friend class SnapshotPartitionTestP;
Yifan Hongc049f932019-07-23 15:06:05 -0700238
Yifan Hong8546a712019-03-28 14:42:53 -0700239 bool MapPartitionInternal(const std::string& super_device,
240 const std::string& target_partition_name,
241 uint32_t slot,
242 bool force_writable,
243 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700244
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700245 // Update |builder| according to |partition_metadata|.
246 // - In Android mode, this is only called when the device
247 // does not have Virtual A/B.
248 // - When sideloading, this maybe called as a fallback path if CoW cannot
249 // be created.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700250 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
251 uint32_t target_slot,
252 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700253
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500254 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions
255 // for Virtual A/B update.
Yifan Hong420db9b2019-07-23 20:50:33 -0700256 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
257 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800258 const DeltaArchiveManifest& manifest,
259 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700260
Yifan Hong3a1293a2021-04-16 13:21:20 -0700261 enum SpaceLimit {
262 // Most restricted: if sum(groups) > super / 2, error
263 ERROR_IF_EXCEEDED_HALF_OF_SUPER,
264 // Implies ERROR_IF_EXCEEDED_SUPER; then, if sum(groups) > super / 2, warn
265 WARN_IF_EXCEEDED_HALF_OF_SUPER,
266 // Least restricted: if sum(groups) > super, error
267 ERROR_IF_EXCEEDED_SUPER,
268 };
269 // Helper of CheckSuperPartitionAllocatableSpace. Determine limit for groups
270 // and partitions.
271 SpaceLimit GetSpaceLimit(bool use_snapshot);
272
Tianjie9f4dc7f2021-03-15 16:00:50 -0700273 // Returns true if the allocatable space in super partition is larger than
274 // the size of dynamic partition groups in the manifest.
275 bool CheckSuperPartitionAllocatableSpace(
276 android::fs_mgr::MetadataBuilder* builder,
277 const DeltaArchiveManifest& manifest,
278 bool use_snapshot);
279
Yifan Hong3a1a5612019-11-05 16:34:32 -0800280 enum class DynamicPartitionDeviceStatus {
281 SUCCESS,
282 ERROR,
283 TRY_STATIC,
284 };
285
286 // Return SUCCESS and path in |device| if partition is dynamic.
287 // Return ERROR if any error.
288 // Return TRY_STATIC if caller should resolve the partition as a static
289 // partition instead.
290 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
291 const base::FilePath& device_dir,
292 const std::string& partition_name_suffix,
293 uint32_t slot,
294 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -0700295 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -0800296 std::string* device);
297
298 // Return true if |partition_name_suffix| is a block device of
299 // super partition metadata slot |slot|.
300 bool IsSuperBlockDevice(const base::FilePath& device_dir,
301 uint32_t current_slot,
302 const std::string& partition_name_suffix);
303
Yifan Hongbae27842019-10-24 16:56:12 -0700304 // If sideloading a full OTA, delete source partitions from |builder|.
305 bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder,
306 uint32_t source_slot,
307 const DeltaArchiveManifest& manifest);
308
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700309 // Returns true if metadata is expected to be mounted, false otherwise.
310 // Note that it returns false on non-Virtual A/B devices.
311 //
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500312 // Almost all functions of SnapshotManager depends on metadata being
313 // mounted.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700314 // - In Android mode for Virtual A/B devices, assume it is mounted. If not,
315 // let caller fails when calling into SnapshotManager.
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500316 // - In recovery for Virtual A/B devices, it is possible that metadata is
317 // not
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700318 // formatted, hence it cannot be mounted. Caller should not call into
319 // SnapshotManager.
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500320 // - On non-Virtual A/B devices, updates do not depend on metadata
321 // partition.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700322 // Caller should not call into SnapshotManager.
323 //
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500324 // This function does NOT mount metadata partition. Use
325 // EnsureMetadataMounted to mount metadata partition.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700326 bool ExpectMetadataMounted();
327
328 // Ensure /metadata is mounted. Returns true if successful, false otherwise.
329 //
330 // Note that this function returns true on non-Virtual A/B devices without
331 // doing anything.
332 bool EnsureMetadataMounted();
333
Yifan Hongf6f75c22020-07-31 15:20:25 -0700334 // Set boolean flags related to target build. This includes flags like
335 // target_supports_snapshot_ and is_target_dynamic_.
336 bool SetTargetBuildVars(const DeltaArchiveManifest& manifest);
337
Yifan Hong420db9b2019-07-23 20:50:33 -0700338 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700339 const FeatureFlag dynamic_partitions_;
340 const FeatureFlag virtual_ab_;
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400341 const FeatureFlag virtual_ab_compression_;
Yifan Hongf9cb4492020-04-15 13:00:20 -0700342 std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700343 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700344 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800345 // Whether the target partitions should be loaded as dynamic partitions. Set
346 // by PreparePartitionsForUpdate() per each update.
347 bool is_target_dynamic_ = false;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500348
Yifan Hong6eec9952019-12-04 13:12:01 -0800349 uint32_t source_slot_ = UINT32_MAX;
350 uint32_t target_slot_ = UINT32_MAX;
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500351 std::vector<std::vector<std::string>> dynamic_partition_list_{2UL};
Yifan Hong420db9b2019-07-23 20:50:33 -0700352
Yifan Hong537802d2018-08-15 13:15:42 -0700353 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
354};
355
356} // namespace chromeos_update_engine
357
Amin Hassaniec7bc112020-10-29 16:47:58 -0700358#endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_CONTROL_ANDROID_H_