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 | |
Yifan Hong | 15726b9 | 2019-11-05 19:06:48 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_ |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 24 | #include <vector> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 25 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 26 | #include "update_engine/common/action.h" |
| 27 | #include "update_engine/common/cleanup_previous_update_action_delegate.h" |
Yifan Hong | 2257ee1 | 2020-01-13 18:33:00 -0800 | [diff] [blame] | 28 | #include "update_engine/common/error_code.h" |
Amin Hassani | 9ed2cee | 2020-11-13 18:40:35 -0800 | [diff] [blame] | 29 | #include "update_engine/common/prefs_interface.h" |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 30 | #include "update_engine/payload_consumer/file_descriptor.h" |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 31 | #include "update_engine/update_metadata.pb.h" |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 32 | |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 33 | // Forware declare for libsnapshot/snapshot_writer.h |
| 34 | namespace android::snapshot { |
| 35 | class ISnapshotWriter; |
| 36 | } |
| 37 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 38 | namespace chromeos_update_engine { |
| 39 | |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 40 | struct PartitionDevice { |
| 41 | std::string rw_device_path; |
Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 42 | std::string readonly_device_path; |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 43 | bool is_dynamic; |
| 44 | }; |
| 45 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 46 | struct FeatureFlag { |
| 47 | enum class Value { NONE = 0, RETROFIT, LAUNCH }; |
| 48 | constexpr explicit FeatureFlag(Value value) : value_(value) {} |
| 49 | constexpr bool IsEnabled() const { return value_ != Value::NONE; } |
| 50 | constexpr bool IsRetrofit() const { return value_ == Value::RETROFIT; } |
Yifan Hong | 6e0d0ef | 2019-10-17 14:34:22 -0700 | [diff] [blame] | 51 | constexpr bool IsLaunch() const { return value_ == Value::LAUNCH; } |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | Value value_; |
| 55 | }; |
| 56 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 57 | class BootControlInterface; |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 58 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 59 | class DynamicPartitionControlInterface { |
| 60 | public: |
| 61 | virtual ~DynamicPartitionControlInterface() = default; |
| 62 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 63 | // Return the feature flags of dynamic partitions on this device. |
| 64 | // Return RETROFIT iff dynamic partitions is retrofitted on this device, |
| 65 | // LAUNCH iff this device is launched with dynamic partitions, |
| 66 | // NONE iff dynamic partitions is disabled on this device. |
| 67 | virtual FeatureFlag GetDynamicPartitionsFeatureFlag() = 0; |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 68 | |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 69 | // Return the feature flags of Virtual A/B on this device. |
| 70 | virtual FeatureFlag GetVirtualAbFeatureFlag() = 0; |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 71 | // Return the feature flags of Virtual A/B Compression on this device. |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 72 | // This function will tell you if current device supports VABC. However, it |
| 73 | // DOES NOT tell you if VABC is used for current OTA update. For that, use |
| 74 | // UpdateUsesSnapshotCompression. |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 75 | virtual FeatureFlag GetVirtualAbCompressionFeatureFlag() = 0; |
Kelvin Zhang | 1c4b981 | 2022-04-06 17:29:00 -0700 | [diff] [blame] | 76 | // Return the feature flag for Virtual AB Compression XOR |
| 77 | virtual FeatureFlag GetVirtualAbCompressionXorFeatureFlag() = 0; |
David Anderson | e35b438 | 2022-03-08 23:18:29 -0800 | [diff] [blame^] | 78 | // Returns whether userspace snapshots are enabled on the device, but not |
| 79 | // whether they're enabled for the update. |
| 80 | virtual FeatureFlag GetVirtualAbUserspaceSnapshotsFeatureFlag() = 0; |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 81 | |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 82 | // Attempt to optimize |operation|. |
| 83 | // If successful, |optimized| contains an operation with extents that |
| 84 | // needs to be written. |
| 85 | // If failed, no optimization is available, and caller should perform |
| 86 | // |operation| directly. |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 87 | // |partition_name| should not have the slot suffix; implementation of |
| 88 | // DynamicPartitionControlInterface checks partition at the target slot |
| 89 | // previously set with PreparePartitionsForUpdate(). |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 90 | virtual bool OptimizeOperation(const std::string& partition_name, |
| 91 | const InstallOperation& operation, |
| 92 | InstallOperation* optimized) = 0; |
Alessio Balsini | 14980e2 | 2019-11-26 11:46:06 +0000 | [diff] [blame] | 93 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 94 | // Do necessary cleanups before destroying the object. |
| 95 | virtual void Cleanup() = 0; |
| 96 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 97 | // Prepare all partitions for an update specified in |manifest|. |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 98 | // This is needed before calling MapPartitionOnDeviceMapper(), otherwise the |
| 99 | // device would be mapped in an inconsistent way. |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 100 | // If |update| is set, create snapshots and writes super partition metadata. |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 101 | // If |required_size| is not null and call fails due to insufficient space, |
| 102 | // |required_size| will be set to total free space required on userdata |
| 103 | // partition to apply the update. Otherwise (call succeeds, or fails |
| 104 | // due to other errors), |required_size| is set to zero. |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 105 | virtual bool PreparePartitionsForUpdate(uint32_t source_slot, |
| 106 | uint32_t target_slot, |
| 107 | const DeltaArchiveManifest& manifest, |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 108 | bool update, |
| 109 | uint64_t* required_size) = 0; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 110 | |
Yifan Hong | 0b664d1 | 2020-01-13 18:06:54 -0800 | [diff] [blame] | 111 | // After writing to new partitions, before rebooting into the new slot, call |
| 112 | // this function to indicate writes to new partitions are done. |
Yifan Hong | 7b3910a | 2020-03-24 17:47:32 -0700 | [diff] [blame] | 113 | virtual bool FinishUpdate(bool powerwash_required) = 0; |
Yifan Hong | 2257ee1 | 2020-01-13 18:33:00 -0800 | [diff] [blame] | 114 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 115 | // Get an action to clean up previous update. |
| 116 | // Return NoOpAction on non-Virtual A/B devices. |
| 117 | // Before applying the next update, run this action to clean up previous |
| 118 | // update files. This function blocks until delta files are merged into |
| 119 | // current OS partitions and finished cleaning up. |
| 120 | // - If successful, action completes with kSuccess. |
| 121 | // - If any error, but caller should retry after reboot, action completes with |
| 122 | // kError. |
| 123 | // - If any irrecoverable failures, action completes with kDeviceCorrupted. |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 124 | // |
| 125 | // See ResetUpdate for differences between CleanuPreviousUpdateAction and |
| 126 | // ResetUpdate. |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 127 | virtual std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction( |
| 128 | BootControlInterface* boot_control, |
| 129 | PrefsInterface* prefs, |
| 130 | CleanupPreviousUpdateActionDelegateInterface* delegate) = 0; |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 131 | |
| 132 | // Called after an unwanted payload has been successfully applied and the |
| 133 | // device has not yet been rebooted. |
| 134 | // |
| 135 | // For snapshot updates (Virtual A/B), it calls |
| 136 | // DeltaPerformer::ResetUpdateProgress(false /* quick */) and |
| 137 | // frees previously allocated space; the next update will need to be |
| 138 | // started over. |
| 139 | // |
| 140 | // Note: CleanupPreviousUpdateAction does not do anything if an update is in |
| 141 | // progress, while ResetUpdate() forcefully free previously |
| 142 | // allocated space for snapshot updates. |
| 143 | virtual bool ResetUpdate(PrefsInterface* prefs) = 0; |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 144 | |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 145 | // Reads the dynamic partitions metadata from the given slot, and puts the |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 146 | // name of the dynamic partitions with the current suffix to |partitions|. |
| 147 | // Returns true on success. |
| 148 | virtual bool ListDynamicPartitionsForSlot( |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 149 | uint32_t slot, |
| 150 | uint32_t current_slot, |
| 151 | std::vector<std::string>* partitions) = 0; |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 152 | |
| 153 | // Finds a possible location that list all block devices by name; and puts |
| 154 | // the result in |path|. Returns true on success. |
| 155 | // Sample result: /dev/block/by-name/ |
| 156 | virtual bool GetDeviceDir(std::string* path) = 0; |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 157 | |
| 158 | // Verifies that the untouched dynamic partitions in the target metadata have |
| 159 | // the same extents as the source metadata. |
| 160 | virtual bool VerifyExtentsForUntouchedPartitions( |
| 161 | uint32_t source_slot, |
| 162 | uint32_t target_slot, |
| 163 | const std::vector<std::string>& partitions) = 0; |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 164 | // Partition name is expected to be unsuffixed. e.g. system, vendor |
| 165 | // Return an interface to write to a snapshoted partition. |
| 166 | // If `is_append` is false, then existing COW data will be overwritten. |
| 167 | // Otherwise the cow writer will be opened on APPEND mode, existing COW data |
| 168 | // is preserved. |
| 169 | virtual std::unique_ptr<android::snapshot::ISnapshotWriter> OpenCowWriter( |
| 170 | const std::string& unsuffixed_partition_name, |
| 171 | const std::optional<std::string>&, |
| 172 | bool is_append = false) = 0; |
Kelvin Zhang | 21a4991 | 2021-03-12 14:28:33 -0500 | [diff] [blame] | 173 | // Open a general purpose FD capable to reading and writing to COW. Note that |
| 174 | // writes must be block aligned. |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 175 | virtual std::unique_ptr<FileDescriptor> OpenCowFd( |
Kelvin Zhang | c82511c | 2020-11-06 16:01:24 -0500 | [diff] [blame] | 176 | const std::string& unsuffixed_partition_name, |
| 177 | const std::optional<std::string>&, |
| 178 | bool is_append = false) = 0; |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 179 | |
Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 180 | virtual bool IsDynamicPartition(const std::string& part_name, |
| 181 | uint32_t slot) = 0; |
Kelvin Zhang | eb9de16 | 2020-11-16 15:47:28 -0500 | [diff] [blame] | 182 | |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 183 | // Create virtual block devices for all partitions. |
| 184 | virtual bool MapAllPartitions() = 0; |
| 185 | // Unmap virtual block devices for all partitions. |
| 186 | virtual bool UnmapAllPartitions() = 0; |
Yifan Hong | b0cbd39 | 2021-02-04 11:11:45 -0800 | [diff] [blame] | 187 | |
| 188 | // Return if snapshot compression is enabled for this update. |
| 189 | // This function should only be called after preparing for an update |
| 190 | // (PreparePartitionsForUpdate), and before merging |
| 191 | // (see GetCleanupPreviousUpdateAction and CleanupPreviousUpdateAction) or |
| 192 | // resetting it (ResetUpdate). |
| 193 | // |
| 194 | // To know if the device supports snapshot compression by itself, use |
| 195 | // GetVirtualAbCompressionFeatureFlag |
| 196 | virtual bool UpdateUsesSnapshotCompression() = 0; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace chromeos_update_engine |
| 200 | |
Yifan Hong | 15726b9 | 2019-11-05 19:06:48 -0800 | [diff] [blame] | 201 | #endif // UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_ |