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> |
| 23 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 24 | #include <base/files/file_util.h> |
Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 25 | #include <libsnapshot/auto_device.h> |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 26 | #include <libsnapshot/snapshot.h> |
| 27 | |
Yifan Hong | 15726b9 | 2019-11-05 19:06:48 -0800 | [diff] [blame] | 28 | #include "update_engine/common/dynamic_partition_control_interface.h" |
| 29 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 30 | namespace chromeos_update_engine { |
| 31 | |
| 32 | class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface { |
| 33 | public: |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 34 | DynamicPartitionControlAndroid(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 35 | ~DynamicPartitionControlAndroid(); |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 36 | FeatureFlag GetDynamicPartitionsFeatureFlag() override; |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 37 | FeatureFlag GetVirtualAbFeatureFlag() override; |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 38 | bool OptimizeOperation(const std::string& partition_name, |
| 39 | const InstallOperation& operation, |
| 40 | InstallOperation* optimized) override; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 | void Cleanup() override; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 42 | |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 43 | bool PreparePartitionsForUpdate(uint32_t source_slot, |
| 44 | uint32_t target_slot, |
| 45 | const DeltaArchiveManifest& manifest, |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 46 | bool update, |
| 47 | uint64_t* required_size) override; |
Yifan Hong | a33bca4 | 2019-09-03 20:29:45 -0700 | [diff] [blame] | 48 | bool FinishUpdate() override; |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 49 | std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction( |
| 50 | BootControlInterface* boot_control, |
| 51 | PrefsInterface* prefs, |
| 52 | CleanupPreviousUpdateActionDelegateInterface* delegate) override; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 53 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 54 | // Return the device for partition |partition_name| at slot |slot|. |
| 55 | // |current_slot| should be set to the current active slot. |
| 56 | // Note: this function is only used by BootControl*::GetPartitionDevice. |
| 57 | // Other callers should prefer BootControl*::GetPartitionDevice over |
| 58 | // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice(). |
| 59 | bool GetPartitionDevice(const std::string& partition_name, |
| 60 | uint32_t slot, |
| 61 | uint32_t current_slot, |
| 62 | std::string* device); |
| 63 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 64 | protected: |
| 65 | // These functions are exposed for testing. |
| 66 | |
| 67 | // Unmap logical partition on device mapper. This is the reverse operation |
| 68 | // of MapPartitionOnDeviceMapper. |
| 69 | // Returns true if unmapped successfully. |
| 70 | virtual bool UnmapPartitionOnDeviceMapper( |
| 71 | const std::string& target_partition_name); |
| 72 | |
| 73 | // Retrieve metadata from |super_device| at slot |source_slot|. |
| 74 | // |
| 75 | // If |target_slot| != kInvalidSlot, before returning the metadata, this |
| 76 | // function modifies the metadata so that during updates, the metadata can be |
| 77 | // written to |target_slot|. In particular, on retrofit devices, the returned |
| 78 | // metadata automatically includes block devices at |target_slot|. |
| 79 | // |
| 80 | // If |target_slot| == kInvalidSlot, this function returns metadata at |
| 81 | // |source_slot| without modifying it. This is the same as |
| 82 | // LoadMetadataBuilder(const std::string&, uint32_t). |
| 83 | virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 84 | const std::string& super_device, |
| 85 | uint32_t source_slot, |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 86 | uint32_t target_slot); |
| 87 | |
| 88 | // Write metadata |builder| to |super_device| at slot |target_slot|. |
| 89 | virtual bool StoreMetadata(const std::string& super_device, |
| 90 | android::fs_mgr::MetadataBuilder* builder, |
| 91 | uint32_t target_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 92 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 93 | // Map logical partition on device-mapper. |
| 94 | // |super_device| is the device path of the physical partition ("super"). |
| 95 | // |target_partition_name| is the identifier used in metadata; for example, |
| 96 | // "vendor_a" |
| 97 | // |slot| is the selected slot to mount; for example, 0 for "_a". |
| 98 | // Returns true if mapped successfully; if so, |path| is set to the device |
| 99 | // path of the mapped logical partition. |
| 100 | virtual bool MapPartitionOnDeviceMapper( |
| 101 | const std::string& super_device, |
| 102 | const std::string& target_partition_name, |
| 103 | uint32_t slot, |
| 104 | bool force_writable, |
| 105 | std::string* path); |
| 106 | |
| 107 | // Return true if a static partition exists at device path |path|. |
| 108 | virtual bool DeviceExists(const std::string& path); |
| 109 | |
| 110 | // Returns the current state of the underlying device mapper device |
| 111 | // with given name. |
| 112 | // One of INVALID, SUSPENDED or ACTIVE. |
| 113 | virtual android::dm::DmDeviceState GetState(const std::string& name); |
| 114 | |
| 115 | // Returns the path to the device mapper device node in '/dev' corresponding |
| 116 | // to 'name'. If the device does not exist, false is returned, and the path |
| 117 | // parameter is not set. |
| 118 | virtual bool GetDmDevicePathByName(const std::string& name, |
| 119 | std::string* path); |
| 120 | |
| 121 | // Retrieve metadata from |super_device| at slot |source_slot|. |
| 122 | virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( |
| 123 | const std::string& super_device, uint32_t source_slot); |
| 124 | |
| 125 | // Return a possible location for devices listed by name. |
| 126 | virtual bool GetDeviceDir(std::string* path); |
| 127 | |
| 128 | // Return the name of the super partition (which stores super partition |
| 129 | // metadata) for a given slot. |
| 130 | virtual std::string GetSuperPartitionName(uint32_t slot); |
| 131 | |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 132 | virtual void set_fake_mapped_devices(const std::set<std::string>& fake); |
| 133 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 134 | // Allow mock objects to override this to test recovery mode. |
| 135 | virtual bool IsRecovery(); |
| 136 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 137 | private: |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 138 | friend class DynamicPartitionControlAndroidTest; |
| 139 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 140 | void UnmapAllPartitions(); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 141 | bool MapPartitionInternal(const std::string& super_device, |
| 142 | const std::string& target_partition_name, |
| 143 | uint32_t slot, |
| 144 | bool force_writable, |
| 145 | std::string* path); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 146 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 147 | // Update |builder| according to |partition_metadata|, assuming the device |
| 148 | // does not have Virtual A/B. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 149 | bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder, |
| 150 | uint32_t target_slot, |
| 151 | const DeltaArchiveManifest& manifest); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 152 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 153 | // Helper for PreparePartitionsForUpdate. Used for devices with dynamic |
| 154 | // partitions updating without snapshots. |
| 155 | // If |delete_source| is set, source partitions are deleted before resizing |
| 156 | // target partitions (using DeleteSourcePartitions). |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 157 | bool PrepareDynamicPartitionsForUpdate(uint32_t source_slot, |
| 158 | uint32_t target_slot, |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 159 | const DeltaArchiveManifest& manifest, |
| 160 | bool delete_source); |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 161 | |
| 162 | // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for |
| 163 | // Virtual A/B update. |
| 164 | bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot, |
| 165 | uint32_t target_slot, |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 166 | const DeltaArchiveManifest& manifest, |
| 167 | uint64_t* required_size); |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 168 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 169 | enum class DynamicPartitionDeviceStatus { |
| 170 | SUCCESS, |
| 171 | ERROR, |
| 172 | TRY_STATIC, |
| 173 | }; |
| 174 | |
| 175 | // Return SUCCESS and path in |device| if partition is dynamic. |
| 176 | // Return ERROR if any error. |
| 177 | // Return TRY_STATIC if caller should resolve the partition as a static |
| 178 | // partition instead. |
| 179 | DynamicPartitionDeviceStatus GetDynamicPartitionDevice( |
| 180 | const base::FilePath& device_dir, |
| 181 | const std::string& partition_name_suffix, |
| 182 | uint32_t slot, |
| 183 | uint32_t current_slot, |
| 184 | std::string* device); |
| 185 | |
| 186 | // Return true if |partition_name_suffix| is a block device of |
| 187 | // super partition metadata slot |slot|. |
| 188 | bool IsSuperBlockDevice(const base::FilePath& device_dir, |
| 189 | uint32_t current_slot, |
| 190 | const std::string& partition_name_suffix); |
| 191 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 192 | // If sideloading a full OTA, delete source partitions from |builder|. |
| 193 | bool DeleteSourcePartitions(android::fs_mgr::MetadataBuilder* builder, |
| 194 | uint32_t source_slot, |
| 195 | const DeltaArchiveManifest& manifest); |
| 196 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 197 | std::set<std::string> mapped_devices_; |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 198 | const FeatureFlag dynamic_partitions_; |
| 199 | const FeatureFlag virtual_ab_; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 200 | std::unique_ptr<android::snapshot::SnapshotManager> snapshot_; |
Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 201 | std::unique_ptr<android::snapshot::AutoDevice> metadata_device_; |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 202 | bool target_supports_snapshot_ = false; |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 203 | // Whether the target partitions should be loaded as dynamic partitions. Set |
| 204 | // by PreparePartitionsForUpdate() per each update. |
| 205 | bool is_target_dynamic_ = false; |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 206 | uint32_t source_slot_ = UINT32_MAX; |
| 207 | uint32_t target_slot_ = UINT32_MAX; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 208 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid); |
| 210 | }; |
| 211 | |
| 212 | } // namespace chromeos_update_engine |
| 213 | |
| 214 | #endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_ |