blob: e3bedbcbd27a026999140daa0ce2a934814e667e [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 Hongf5261562020-03-10 10:28:10 -070038 bool OptimizeOperation(const std::string& partition_name,
39 const InstallOperation& operation,
40 InstallOperation* optimized) override;
Yifan Hong537802d2018-08-15 13:15:42 -070041 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070042
Yifan Hongf0f4a912019-09-26 17:51:33 -070043 bool PreparePartitionsForUpdate(uint32_t source_slot,
44 uint32_t target_slot,
45 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -080046 bool update,
47 uint64_t* required_size) override;
Yifan Hong7b3910a2020-03-24 17:47:32 -070048 bool FinishUpdate(bool powerwash_required) override;
Yifan Hong90965502020-02-19 15:22:47 -080049 std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
50 BootControlInterface* boot_control,
51 PrefsInterface* prefs,
52 CleanupPreviousUpdateActionDelegateInterface* delegate) override;
Yifan Hong012508e2019-07-22 18:30:40 -070053
Yifan Hong6a6d0f12020-03-11 13:20:52 -070054 bool ResetUpdate(PrefsInterface* prefs) override;
55
Yifan Hong3a1a5612019-11-05 16:34:32 -080056 // Return the device for partition |partition_name| at slot |slot|.
57 // |current_slot| should be set to the current active slot.
58 // Note: this function is only used by BootControl*::GetPartitionDevice.
59 // Other callers should prefer BootControl*::GetPartitionDevice over
60 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
61 bool GetPartitionDevice(const std::string& partition_name,
62 uint32_t slot,
63 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -070064 bool not_in_payload,
65 std::string* device,
66 bool* is_dynamic);
67
68 bool GetPartitionDevice(const std::string& partition_name,
69 uint32_t slot,
70 uint32_t current_slot,
Yifan Hong3a1a5612019-11-05 16:34:32 -080071 std::string* device);
72
Yifan Hong012508e2019-07-22 18:30:40 -070073 protected:
74 // These functions are exposed for testing.
75
76 // Unmap logical partition on device mapper. This is the reverse operation
77 // of MapPartitionOnDeviceMapper.
78 // Returns true if unmapped successfully.
79 virtual bool UnmapPartitionOnDeviceMapper(
80 const std::string& target_partition_name);
81
82 // Retrieve metadata from |super_device| at slot |source_slot|.
83 //
84 // If |target_slot| != kInvalidSlot, before returning the metadata, this
85 // function modifies the metadata so that during updates, the metadata can be
86 // written to |target_slot|. In particular, on retrofit devices, the returned
87 // metadata automatically includes block devices at |target_slot|.
88 //
89 // If |target_slot| == kInvalidSlot, this function returns metadata at
90 // |source_slot| without modifying it. This is the same as
91 // LoadMetadataBuilder(const std::string&, uint32_t).
92 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080093 const std::string& super_device,
94 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070095 uint32_t target_slot);
96
97 // Write metadata |builder| to |super_device| at slot |target_slot|.
98 virtual bool StoreMetadata(const std::string& super_device,
99 android::fs_mgr::MetadataBuilder* builder,
100 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700101
Yifan Hong3a1a5612019-11-05 16:34:32 -0800102 // Map logical partition on device-mapper.
103 // |super_device| is the device path of the physical partition ("super").
104 // |target_partition_name| is the identifier used in metadata; for example,
105 // "vendor_a"
106 // |slot| is the selected slot to mount; for example, 0 for "_a".
107 // Returns true if mapped successfully; if so, |path| is set to the device
108 // path of the mapped logical partition.
109 virtual bool MapPartitionOnDeviceMapper(
110 const std::string& super_device,
111 const std::string& target_partition_name,
112 uint32_t slot,
113 bool force_writable,
114 std::string* path);
115
116 // Return true if a static partition exists at device path |path|.
117 virtual bool DeviceExists(const std::string& path);
118
119 // Returns the current state of the underlying device mapper device
120 // with given name.
121 // One of INVALID, SUSPENDED or ACTIVE.
122 virtual android::dm::DmDeviceState GetState(const std::string& name);
123
124 // Returns the path to the device mapper device node in '/dev' corresponding
125 // to 'name'. If the device does not exist, false is returned, and the path
126 // parameter is not set.
127 virtual bool GetDmDevicePathByName(const std::string& name,
128 std::string* path);
129
130 // Retrieve metadata from |super_device| at slot |source_slot|.
131 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
132 const std::string& super_device, uint32_t source_slot);
133
134 // Return a possible location for devices listed by name.
135 virtual bool GetDeviceDir(std::string* path);
136
137 // Return the name of the super partition (which stores super partition
138 // metadata) for a given slot.
139 virtual std::string GetSuperPartitionName(uint32_t slot);
140
Yifan Hong6eec9952019-12-04 13:12:01 -0800141 virtual void set_fake_mapped_devices(const std::set<std::string>& fake);
142
Yifan Hongbae27842019-10-24 16:56:12 -0700143 // Allow mock objects to override this to test recovery mode.
144 virtual bool IsRecovery();
145
Yifan Hong29692902020-03-26 12:47:05 -0700146 // Determine path for system_other partition.
147 // |source_slot| should be current slot.
148 // |target_slot| should be "other" slot.
149 // |partition_name_suffix| should be "system" + suffix(|target_slot|).
150 // Return true and set |path| if successful.
151 // Set |path| to empty if no need to erase system_other.
152 // Set |should_unmap| to true if path needs to be unmapped later.
153 //
154 // Note: system_other cannot use GetPartitionDevice or
155 // GetDynamicPartitionDevice because:
156 // - super partition metadata may be loaded from the source slot
157 // - UPDATED flag needs to be check to skip erasing if partition is not
158 // created by flashing tools
159 // - Snapshots from previous update attempts should not be used.
160 virtual bool GetSystemOtherPath(uint32_t source_slot,
161 uint32_t target_slot,
162 const std::string& partition_name_suffix,
163 std::string* path,
164 bool* should_unmap);
165
166 // Returns true if any entry in the fstab file in |path| has AVB enabled,
167 // false if not enabled, and nullopt for any error.
168 virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path);
169
170 // Returns true if system_other has AVB enabled, false if not enabled, and
171 // nullopt for any error.
172 virtual std::optional<bool> IsAvbEnabledOnSystemOther();
173
174 // Erase system_other partition that may contain system_other.img.
175 // After the update, the content of system_other may be corrupted but with
176 // valid AVB footer. If the update is rolled back and factory data reset is
177 // triggered, system_b fails to be mapped with verity errors (see
178 // b/152444348). Erase the system_other so that mapping system_other is
179 // skipped.
180 virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot,
181 uint32_t target_slot);
182
Yifan Hong302fa702020-04-16 09:48:29 -0700183 // Helper for PreparePartitionsForUpdate. Used for devices with dynamic
184 // partitions updating without snapshots.
185 // If |delete_source| is set, source partitions are deleted before resizing
186 // target partitions (using DeleteSourcePartitions).
187 virtual bool PrepareDynamicPartitionsForUpdate(
188 uint32_t source_slot,
189 uint32_t target_slot,
190 const DeltaArchiveManifest& manifest,
191 bool delete_source);
192
Yifan Hong537802d2018-08-15 13:15:42 -0700193 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700194 friend class DynamicPartitionControlAndroidTest;
Yifan Hong302fa702020-04-16 09:48:29 -0700195 friend class SnapshotPartitionTestP;
Yifan Hongc049f932019-07-23 15:06:05 -0700196
Yifan Hongbae27842019-10-24 16:56:12 -0700197 void UnmapAllPartitions();
Yifan Hong8546a712019-03-28 14:42:53 -0700198 bool MapPartitionInternal(const std::string& super_device,
199 const std::string& target_partition_name,
200 uint32_t slot,
201 bool force_writable,
202 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700203
Yifan Hong012508e2019-07-22 18:30:40 -0700204 // Update |builder| according to |partition_metadata|, assuming the device
205 // does not have Virtual A/B.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700206 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
207 uint32_t target_slot,
208 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700209
Yifan Hong420db9b2019-07-23 20:50:33 -0700210 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
211 // Virtual A/B update.
212 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
213 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800214 const DeltaArchiveManifest& manifest,
215 uint64_t* required_size);
Yifan Hong420db9b2019-07-23 20:50:33 -0700216
Yifan Hong3a1a5612019-11-05 16:34:32 -0800217 enum class DynamicPartitionDeviceStatus {
218 SUCCESS,
219 ERROR,
220 TRY_STATIC,
221 };
222
223 // Return SUCCESS and path in |device| if partition is dynamic.
224 // Return ERROR if any error.
225 // Return TRY_STATIC if caller should resolve the partition as a static
226 // partition instead.
227 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
228 const base::FilePath& device_dir,
229 const std::string& partition_name_suffix,
230 uint32_t slot,
231 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -0700232 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -0800233 std::string* device);
234
235 // Return true if |partition_name_suffix| is a block device of
236 // super partition metadata slot |slot|.
237 bool IsSuperBlockDevice(const base::FilePath& device_dir,
238 uint32_t current_slot,
239 const std::string& partition_name_suffix);
240
Yifan Hongbae27842019-10-24 16:56:12 -0700241 // If sideloading a full OTA, delete source partitions from |builder|.
242 bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder,
243 uint32_t source_slot,
244 const DeltaArchiveManifest& manifest);
245
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700246 // Returns true if metadata is expected to be mounted, false otherwise.
247 // Note that it returns false on non-Virtual A/B devices.
248 //
249 // Almost all functions of SnapshotManager depends on metadata being mounted.
250 // - In Android mode for Virtual A/B devices, assume it is mounted. If not,
251 // let caller fails when calling into SnapshotManager.
252 // - In recovery for Virtual A/B devices, it is possible that metadata is not
253 // formatted, hence it cannot be mounted. Caller should not call into
254 // SnapshotManager.
255 // - On non-Virtual A/B devices, updates do not depend on metadata partition.
256 // Caller should not call into SnapshotManager.
257 //
258 // This function does NOT mount metadata partition. Use EnsureMetadataMounted
259 // to mount metadata partition.
260 bool ExpectMetadataMounted();
261
262 // Ensure /metadata is mounted. Returns true if successful, false otherwise.
263 //
264 // Note that this function returns true on non-Virtual A/B devices without
265 // doing anything.
266 bool EnsureMetadataMounted();
267
Yifan Hong420db9b2019-07-23 20:50:33 -0700268 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700269 const FeatureFlag dynamic_partitions_;
270 const FeatureFlag virtual_ab_;
Yifan Hongf9cb4492020-04-15 13:00:20 -0700271 std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700272 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700273 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800274 // Whether the target partitions should be loaded as dynamic partitions. Set
275 // by PreparePartitionsForUpdate() per each update.
276 bool is_target_dynamic_ = false;
Yifan Hong6eec9952019-12-04 13:12:01 -0800277 uint32_t source_slot_ = UINT32_MAX;
278 uint32_t target_slot_ = UINT32_MAX;
Yifan Hong420db9b2019-07-23 20:50:33 -0700279
Yifan Hong537802d2018-08-15 13:15:42 -0700280 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
281};
282
283} // namespace chromeos_update_engine
284
285#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_