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 | |
| 20 | #include "update_engine/dynamic_partition_control_interface.h" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <set> |
| 24 | #include <string> |
| 25 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 26 | #include <libsnapshot/snapshot.h> |
| 27 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface { |
| 31 | public: |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 32 | DynamicPartitionControlAndroid(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 33 | ~DynamicPartitionControlAndroid(); |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 34 | FeatureFlag GetDynamicPartitionsFeatureFlag() override; |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 35 | FeatureFlag GetVirtualAbFeatureFlag() override; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | bool MapPartitionOnDeviceMapper(const std::string& super_device, |
| 37 | const std::string& target_partition_name, |
| 38 | uint32_t slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 39 | bool force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 40 | std::string* path) override; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 | void Cleanup() override; |
| 42 | bool DeviceExists(const std::string& path) override; |
| 43 | android::dm::DmDeviceState GetState(const std::string& name) override; |
| 44 | bool GetDmDevicePathByName(const std::string& name, |
| 45 | std::string* path) override; |
| 46 | std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 47 | const std::string& super_device, uint32_t source_slot) override; |
| 48 | |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 49 | bool PreparePartitionsForUpdate(uint32_t source_slot, |
| 50 | uint32_t target_slot, |
| 51 | const DeltaArchiveManifest& manifest, |
| 52 | bool update) override; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 53 | bool GetDeviceDir(std::string* path) override; |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 54 | std::string GetSuperPartitionName(uint32_t slot) override; |
Yifan Hong | a33bca4 | 2019-09-03 20:29:45 -0700 | [diff] [blame] | 55 | bool FinishUpdate() override; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | // These functions are exposed for testing. |
| 59 | |
| 60 | // Unmap logical partition on device mapper. This is the reverse operation |
| 61 | // of MapPartitionOnDeviceMapper. |
| 62 | // Returns true if unmapped successfully. |
| 63 | virtual bool UnmapPartitionOnDeviceMapper( |
| 64 | const std::string& target_partition_name); |
| 65 | |
| 66 | // Retrieve metadata from |super_device| at slot |source_slot|. |
| 67 | // |
| 68 | // If |target_slot| != kInvalidSlot, before returning the metadata, this |
| 69 | // function modifies the metadata so that during updates, the metadata can be |
| 70 | // written to |target_slot|. In particular, on retrofit devices, the returned |
| 71 | // metadata automatically includes block devices at |target_slot|. |
| 72 | // |
| 73 | // If |target_slot| == kInvalidSlot, this function returns metadata at |
| 74 | // |source_slot| without modifying it. This is the same as |
| 75 | // LoadMetadataBuilder(const std::string&, uint32_t). |
| 76 | virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 77 | const std::string& super_device, |
| 78 | uint32_t source_slot, |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 79 | uint32_t target_slot); |
| 80 | |
| 81 | // Write metadata |builder| to |super_device| at slot |target_slot|. |
| 82 | virtual bool StoreMetadata(const std::string& super_device, |
| 83 | android::fs_mgr::MetadataBuilder* builder, |
| 84 | uint32_t target_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 85 | |
| 86 | private: |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 87 | friend class DynamicPartitionControlAndroidTest; |
| 88 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 89 | void CleanupInternal(bool wait); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 90 | bool MapPartitionInternal(const std::string& super_device, |
| 91 | const std::string& target_partition_name, |
| 92 | uint32_t slot, |
| 93 | bool force_writable, |
| 94 | std::string* path); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 95 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 96 | // Update |builder| according to |partition_metadata|, assuming the device |
| 97 | // does not have Virtual A/B. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 98 | bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder, |
| 99 | uint32_t target_slot, |
| 100 | const DeltaArchiveManifest& manifest); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 101 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 102 | // Helper for PreparePartitionsForUpdate. Used for dynamic partitions without |
| 103 | // Virtual A/B update. |
| 104 | bool PrepareDynamicPartitionsForUpdate(uint32_t source_slot, |
| 105 | uint32_t target_slot, |
| 106 | const DeltaArchiveManifest& manifest); |
| 107 | |
| 108 | // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for |
| 109 | // Virtual A/B update. |
| 110 | bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot, |
| 111 | uint32_t target_slot, |
| 112 | const DeltaArchiveManifest& manifest); |
| 113 | |
| 114 | std::set<std::string> mapped_devices_; |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame^] | 115 | const FeatureFlag dynamic_partitions_; |
| 116 | const FeatureFlag virtual_ab_; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 117 | std::unique_ptr<android::snapshot::SnapshotManager> snapshot_; |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 118 | bool target_supports_snapshot_ = false; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 119 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid); |
| 121 | }; |
| 122 | |
| 123 | } // namespace chromeos_update_engine |
| 124 | |
| 125 | #endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_ |