| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 20 | #include <memory> | 
|  | 21 | #include <set> | 
|  | 22 | #include <string> | 
| Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 23 | #include <vector> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 24 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 25 | #include <base/files/file_util.h> | 
| Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 26 | #include <libsnapshot/auto_device.h> | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 27 | #include <libsnapshot/snapshot.h> | 
|  | 28 |  | 
| Yifan Hong | 15726b9 | 2019-11-05 19:06:48 -0800 | [diff] [blame] | 29 | #include "update_engine/common/dynamic_partition_control_interface.h" | 
|  | 30 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 31 | namespace chromeos_update_engine { | 
|  | 32 |  | 
|  | 33 | class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface { | 
|  | 34 | public: | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 35 | DynamicPartitionControlAndroid(); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | ~DynamicPartitionControlAndroid(); | 
| Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 37 | FeatureFlag GetDynamicPartitionsFeatureFlag() override; | 
| Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 38 | FeatureFlag GetVirtualAbFeatureFlag() override; | 
| Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 39 | bool OptimizeOperation(const std::string& partition_name, | 
|  | 40 | const InstallOperation& operation, | 
|  | 41 | InstallOperation* optimized) override; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 42 | void Cleanup() override; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 43 |  | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 44 | bool PreparePartitionsForUpdate(uint32_t source_slot, | 
|  | 45 | uint32_t target_slot, | 
|  | 46 | const DeltaArchiveManifest& manifest, | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 47 | bool update, | 
|  | 48 | uint64_t* required_size) override; | 
| Yifan Hong | 7b3910a | 2020-03-24 17:47:32 -0700 | [diff] [blame] | 49 | bool FinishUpdate(bool powerwash_required) override; | 
| Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 50 | std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction( | 
|  | 51 | BootControlInterface* boot_control, | 
|  | 52 | PrefsInterface* prefs, | 
|  | 53 | CleanupPreviousUpdateActionDelegateInterface* delegate) override; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 54 |  | 
| Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 55 | bool ResetUpdate(PrefsInterface* prefs) override; | 
|  | 56 |  | 
| Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 57 | bool ListDynamicPartitionsForSlot( | 
|  | 58 | uint32_t current_slot, std::vector<std::string>* partitions) override; | 
|  | 59 |  | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 60 | bool VerifyExtentsForUntouchedPartitions( | 
|  | 61 | uint32_t source_slot, | 
|  | 62 | uint32_t target_slot, | 
|  | 63 | const std::vector<std::string>& partitions) override; | 
|  | 64 |  | 
| Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 65 | bool GetDeviceDir(std::string* path) override; | 
|  | 66 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 67 | // Return the device for partition |partition_name| at slot |slot|. | 
|  | 68 | // |current_slot| should be set to the current active slot. | 
|  | 69 | // Note: this function is only used by BootControl*::GetPartitionDevice. | 
|  | 70 | // Other callers should prefer BootControl*::GetPartitionDevice over | 
|  | 71 | // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice(). | 
|  | 72 | bool GetPartitionDevice(const std::string& partition_name, | 
|  | 73 | uint32_t slot, | 
|  | 74 | uint32_t current_slot, | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 75 | bool not_in_payload, | 
|  | 76 | std::string* device, | 
|  | 77 | bool* is_dynamic); | 
|  | 78 |  | 
|  | 79 | bool GetPartitionDevice(const std::string& partition_name, | 
|  | 80 | uint32_t slot, | 
|  | 81 | uint32_t current_slot, | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 82 | std::string* device); | 
|  | 83 |  | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 84 | protected: | 
|  | 85 | // These functions are exposed for testing. | 
|  | 86 |  | 
|  | 87 | // Unmap logical partition on device mapper. This is the reverse operation | 
|  | 88 | // of MapPartitionOnDeviceMapper. | 
|  | 89 | // Returns true if unmapped successfully. | 
|  | 90 | virtual bool UnmapPartitionOnDeviceMapper( | 
|  | 91 | const std::string& target_partition_name); | 
|  | 92 |  | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 93 | // Retrieves metadata from |super_device| at slot |slot|. | 
|  | 94 | virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( | 
|  | 95 | const std::string& super_device, uint32_t slot); | 
|  | 96 |  | 
|  | 97 | // Retrieves metadata from |super_device| at slot |source_slot|. And modifies | 
|  | 98 | // the metadata so that during updates, the metadata can be written to | 
|  | 99 | // |target_slot|. In particular, on retrofit devices, the returned metadata | 
|  | 100 | // automatically includes block devices at |target_slot|. | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 101 | virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 102 | const std::string& super_device, | 
|  | 103 | uint32_t source_slot, | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 104 | uint32_t target_slot); | 
|  | 105 |  | 
|  | 106 | // Write metadata |builder| to |super_device| at slot |target_slot|. | 
|  | 107 | virtual bool StoreMetadata(const std::string& super_device, | 
|  | 108 | android::fs_mgr::MetadataBuilder* builder, | 
|  | 109 | uint32_t target_slot); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 110 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 111 | // Map logical partition on device-mapper. | 
|  | 112 | // |super_device| is the device path of the physical partition ("super"). | 
|  | 113 | // |target_partition_name| is the identifier used in metadata; for example, | 
|  | 114 | // "vendor_a" | 
|  | 115 | // |slot| is the selected slot to mount; for example, 0 for "_a". | 
|  | 116 | // Returns true if mapped successfully; if so, |path| is set to the device | 
|  | 117 | // path of the mapped logical partition. | 
|  | 118 | virtual bool MapPartitionOnDeviceMapper( | 
|  | 119 | const std::string& super_device, | 
|  | 120 | const std::string& target_partition_name, | 
|  | 121 | uint32_t slot, | 
|  | 122 | bool force_writable, | 
|  | 123 | std::string* path); | 
|  | 124 |  | 
|  | 125 | // Return true if a static partition exists at device path |path|. | 
|  | 126 | virtual bool DeviceExists(const std::string& path); | 
|  | 127 |  | 
|  | 128 | // Returns the current state of the underlying device mapper device | 
|  | 129 | // with given name. | 
|  | 130 | // One of INVALID, SUSPENDED or ACTIVE. | 
|  | 131 | virtual android::dm::DmDeviceState GetState(const std::string& name); | 
|  | 132 |  | 
|  | 133 | // Returns the path to the device mapper device node in '/dev' corresponding | 
|  | 134 | // to 'name'. If the device does not exist, false is returned, and the path | 
|  | 135 | // parameter is not set. | 
|  | 136 | virtual bool GetDmDevicePathByName(const std::string& name, | 
|  | 137 | std::string* path); | 
|  | 138 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 139 | // Return the name of the super partition (which stores super partition | 
|  | 140 | // metadata) for a given slot. | 
|  | 141 | virtual std::string GetSuperPartitionName(uint32_t slot); | 
|  | 142 |  | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 143 | virtual void set_fake_mapped_devices(const std::set<std::string>& fake); | 
|  | 144 |  | 
| Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 145 | // Allow mock objects to override this to test recovery mode. | 
|  | 146 | virtual bool IsRecovery(); | 
|  | 147 |  | 
| Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 148 | // Determine path for system_other partition. | 
|  | 149 | // |source_slot| should be current slot. | 
|  | 150 | // |target_slot| should be "other" slot. | 
|  | 151 | // |partition_name_suffix| should be "system" + suffix(|target_slot|). | 
|  | 152 | // Return true and set |path| if successful. | 
|  | 153 | // Set |path| to empty if no need to erase system_other. | 
|  | 154 | // Set |should_unmap| to true if path needs to be unmapped later. | 
|  | 155 | // | 
|  | 156 | // Note: system_other cannot use GetPartitionDevice or | 
|  | 157 | // GetDynamicPartitionDevice because: | 
|  | 158 | // - super partition metadata may be loaded from the source slot | 
|  | 159 | // - UPDATED flag needs to be check to skip erasing if partition is not | 
|  | 160 | //   created by flashing tools | 
|  | 161 | // - Snapshots from previous update attempts should not be used. | 
|  | 162 | virtual bool GetSystemOtherPath(uint32_t source_slot, | 
|  | 163 | uint32_t target_slot, | 
|  | 164 | const std::string& partition_name_suffix, | 
|  | 165 | std::string* path, | 
|  | 166 | bool* should_unmap); | 
|  | 167 |  | 
|  | 168 | // Returns true if any entry in the fstab file in |path| has AVB enabled, | 
|  | 169 | // false if not enabled, and nullopt for any error. | 
|  | 170 | virtual std::optional<bool> IsAvbEnabledInFstab(const std::string& path); | 
|  | 171 |  | 
|  | 172 | // Returns true if system_other has AVB enabled, false if not enabled, and | 
|  | 173 | // nullopt for any error. | 
|  | 174 | virtual std::optional<bool> IsAvbEnabledOnSystemOther(); | 
|  | 175 |  | 
|  | 176 | // Erase system_other partition that may contain system_other.img. | 
|  | 177 | // After the update, the content of system_other may be corrupted but with | 
|  | 178 | // valid AVB footer. If the update is rolled back and factory data reset is | 
|  | 179 | // triggered, system_b fails to be mapped with verity errors (see | 
|  | 180 | // b/152444348). Erase the system_other so that mapping system_other is | 
|  | 181 | // skipped. | 
|  | 182 | virtual bool EraseSystemOtherAvbFooter(uint32_t source_slot, | 
|  | 183 | uint32_t target_slot); | 
|  | 184 |  | 
| Yifan Hong | 302fa70 | 2020-04-16 09:48:29 -0700 | [diff] [blame] | 185 | // Helper for PreparePartitionsForUpdate. Used for devices with dynamic | 
|  | 186 | // partitions updating without snapshots. | 
|  | 187 | // If |delete_source| is set, source partitions are deleted before resizing | 
|  | 188 | // target partitions (using DeleteSourcePartitions). | 
|  | 189 | virtual bool PrepareDynamicPartitionsForUpdate( | 
|  | 190 | uint32_t source_slot, | 
|  | 191 | uint32_t target_slot, | 
|  | 192 | const DeltaArchiveManifest& manifest, | 
|  | 193 | bool delete_source); | 
|  | 194 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 195 | private: | 
| Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 196 | friend class DynamicPartitionControlAndroidTest; | 
| Yifan Hong | 302fa70 | 2020-04-16 09:48:29 -0700 | [diff] [blame] | 197 | friend class SnapshotPartitionTestP; | 
| Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 198 |  | 
| Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 199 | void UnmapAllPartitions(); | 
| Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 200 | bool MapPartitionInternal(const std::string& super_device, | 
|  | 201 | const std::string& target_partition_name, | 
|  | 202 | uint32_t slot, | 
|  | 203 | bool force_writable, | 
|  | 204 | std::string* path); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 205 |  | 
| Yifan Hong | 8d6df9a | 2020-08-13 13:59:54 -0700 | [diff] [blame] | 206 | // Update |builder| according to |partition_metadata|. | 
|  | 207 | // - In Android mode, this is only called when the device | 
|  | 208 | //   does not have Virtual A/B. | 
|  | 209 | // - When sideloading, this maybe called as a fallback path if CoW cannot | 
|  | 210 | //   be created. | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 211 | bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder, | 
|  | 212 | uint32_t target_slot, | 
|  | 213 | const DeltaArchiveManifest& manifest); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 214 |  | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 215 | // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for | 
|  | 216 | // Virtual A/B update. | 
|  | 217 | bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot, | 
|  | 218 | uint32_t target_slot, | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 219 | const DeltaArchiveManifest& manifest, | 
|  | 220 | uint64_t* required_size); | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 221 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 222 | enum class DynamicPartitionDeviceStatus { | 
|  | 223 | SUCCESS, | 
|  | 224 | ERROR, | 
|  | 225 | TRY_STATIC, | 
|  | 226 | }; | 
|  | 227 |  | 
|  | 228 | // Return SUCCESS and path in |device| if partition is dynamic. | 
|  | 229 | // Return ERROR if any error. | 
|  | 230 | // Return TRY_STATIC if caller should resolve the partition as a static | 
|  | 231 | // partition instead. | 
|  | 232 | DynamicPartitionDeviceStatus GetDynamicPartitionDevice( | 
|  | 233 | const base::FilePath& device_dir, | 
|  | 234 | const std::string& partition_name_suffix, | 
|  | 235 | uint32_t slot, | 
|  | 236 | uint32_t current_slot, | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 237 | bool not_in_payload, | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 238 | std::string* device); | 
|  | 239 |  | 
|  | 240 | // Return true if |partition_name_suffix| is a block device of | 
|  | 241 | // super partition metadata slot |slot|. | 
|  | 242 | bool IsSuperBlockDevice(const base::FilePath& device_dir, | 
|  | 243 | uint32_t current_slot, | 
|  | 244 | const std::string& partition_name_suffix); | 
|  | 245 |  | 
| Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 246 | // If sideloading a full OTA, delete source partitions from |builder|. | 
|  | 247 | bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder, | 
|  | 248 | uint32_t source_slot, | 
|  | 249 | const DeltaArchiveManifest& manifest); | 
|  | 250 |  | 
| Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 251 | // Returns true if metadata is expected to be mounted, false otherwise. | 
|  | 252 | // Note that it returns false on non-Virtual A/B devices. | 
|  | 253 | // | 
|  | 254 | // Almost all functions of SnapshotManager depends on metadata being mounted. | 
|  | 255 | // - In Android mode for Virtual A/B devices, assume it is mounted. If not, | 
|  | 256 | //   let caller fails when calling into SnapshotManager. | 
|  | 257 | // - In recovery for Virtual A/B devices, it is possible that metadata is not | 
|  | 258 | //   formatted, hence it cannot be mounted. Caller should not call into | 
|  | 259 | //   SnapshotManager. | 
|  | 260 | // - On non-Virtual A/B devices, updates do not depend on metadata partition. | 
|  | 261 | //   Caller should not call into SnapshotManager. | 
|  | 262 | // | 
|  | 263 | // This function does NOT mount metadata partition. Use EnsureMetadataMounted | 
|  | 264 | // to mount metadata partition. | 
|  | 265 | bool ExpectMetadataMounted(); | 
|  | 266 |  | 
|  | 267 | // Ensure /metadata is mounted. Returns true if successful, false otherwise. | 
|  | 268 | // | 
|  | 269 | // Note that this function returns true on non-Virtual A/B devices without | 
|  | 270 | // doing anything. | 
|  | 271 | bool EnsureMetadataMounted(); | 
|  | 272 |  | 
| Yifan Hong | f6f75c2 | 2020-07-31 15:20:25 -0700 | [diff] [blame] | 273 | // Set boolean flags related to target build. This includes flags like | 
|  | 274 | // target_supports_snapshot_ and is_target_dynamic_. | 
|  | 275 | bool SetTargetBuildVars(const DeltaArchiveManifest& manifest); | 
|  | 276 |  | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 277 | std::set<std::string> mapped_devices_; | 
| Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 278 | const FeatureFlag dynamic_partitions_; | 
|  | 279 | const FeatureFlag virtual_ab_; | 
| Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 280 | std::unique_ptr<android::snapshot::ISnapshotManager> snapshot_; | 
| Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 281 | std::unique_ptr<android::snapshot::AutoDevice> metadata_device_; | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 282 | bool target_supports_snapshot_ = false; | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 283 | // Whether the target partitions should be loaded as dynamic partitions. Set | 
|  | 284 | // by PreparePartitionsForUpdate() per each update. | 
|  | 285 | bool is_target_dynamic_ = false; | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 286 | uint32_t source_slot_ = UINT32_MAX; | 
|  | 287 | uint32_t target_slot_ = UINT32_MAX; | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 288 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 289 | DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid); | 
|  | 290 | }; | 
|  | 291 |  | 
|  | 292 | }  // namespace chromeos_update_engine | 
|  | 293 |  | 
|  | 294 | #endif  // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_ |