blob: 5c0a03b82e05971fa1d8ebe7b0638d31c3c49e3c [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
Yifan Hong15726b92019-11-05 19:06:48 -080017#ifndef UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_
18#define UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_
Yifan Hong537802d2018-08-15 13:15:42 -070019
20#include <stdint.h>
21
22#include <memory>
23#include <string>
Tianjie99d570d2020-06-04 14:57:19 -070024#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070025
Yifan Hong90965502020-02-19 15:22:47 -080026#include "update_engine/common/action.h"
27#include "update_engine/common/cleanup_previous_update_action_delegate.h"
Yifan Hong2257ee12020-01-13 18:33:00 -080028#include "update_engine/common/error_code.h"
Amin Hassani9ed2cee2020-11-13 18:40:35 -080029#include "update_engine/common/prefs_interface.h"
Kelvin Zhang34618522020-09-28 09:21:02 -040030#include "update_engine/payload_consumer/file_descriptor.h"
Yifan Hong13d41cb2019-09-16 13:18:22 -070031#include "update_engine/update_metadata.pb.h"
Yifan Hong012508e2019-07-22 18:30:40 -070032
Kelvin Zhang34618522020-09-28 09:21:02 -040033// Forware declare for libsnapshot/snapshot_writer.h
34namespace android::snapshot {
David Andersona4b7ba62023-05-10 21:41:37 -070035class ICowWriter;
Kelvin Zhang34618522020-09-28 09:21:02 -040036}
37
Yifan Hong537802d2018-08-15 13:15:42 -070038namespace chromeos_update_engine {
39
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050040struct PartitionDevice {
41 std::string rw_device_path;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -040042 std::string readonly_device_path;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050043 bool is_dynamic;
44};
45
Yifan Hong186bb682019-07-23 14:04:39 -070046struct 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 Hong6e0d0ef2019-10-17 14:34:22 -070051 constexpr bool IsLaunch() const { return value_ == Value::LAUNCH; }
Yifan Hong186bb682019-07-23 14:04:39 -070052
53 private:
54 Value value_;
55};
56
Yifan Hong90965502020-02-19 15:22:47 -080057class BootControlInterface;
Yifan Hong90965502020-02-19 15:22:47 -080058
Yifan Hong537802d2018-08-15 13:15:42 -070059class DynamicPartitionControlInterface {
60 public:
61 virtual ~DynamicPartitionControlInterface() = default;
62
Yifan Hong186bb682019-07-23 14:04:39 -070063 // 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 Hong6e38b352018-11-19 14:12:37 -080068
Yifan Hong413d5722019-07-23 14:21:09 -070069 // Return the feature flags of Virtual A/B on this device.
70 virtual FeatureFlag GetVirtualAbFeatureFlag() = 0;
Kelvin Zhangda1b3142020-09-24 17:09:02 -040071 // Return the feature flags of Virtual A/B Compression on this device.
Kelvin Zhang06188352021-02-10 13:21:47 -050072 // 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 Zhangda1b3142020-09-24 17:09:02 -040075 virtual FeatureFlag GetVirtualAbCompressionFeatureFlag() = 0;
Kelvin Zhang1c4b9812022-04-06 17:29:00 -070076 // Return the feature flag for Virtual AB Compression XOR
77 virtual FeatureFlag GetVirtualAbCompressionXorFeatureFlag() = 0;
David Andersone35b4382022-03-08 23:18:29 -080078 // 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 Hong413d5722019-07-23 14:21:09 -070081
Yifan Hongf5261562020-03-10 10:28:10 -070082 // 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 Hong6eec9952019-12-04 13:12:01 -080087 // |partition_name| should not have the slot suffix; implementation of
88 // DynamicPartitionControlInterface checks partition at the target slot
89 // previously set with PreparePartitionsForUpdate().
Yifan Hongf5261562020-03-10 10:28:10 -070090 virtual bool OptimizeOperation(const std::string& partition_name,
91 const InstallOperation& operation,
92 InstallOperation* optimized) = 0;
Alessio Balsini14980e22019-11-26 11:46:06 +000093
Yifan Hong537802d2018-08-15 13:15:42 -070094 // Do necessary cleanups before destroying the object.
95 virtual void Cleanup() = 0;
96
Yifan Hong13d41cb2019-09-16 13:18:22 -070097 // Prepare all partitions for an update specified in |manifest|.
Yifan Hong012508e2019-07-22 18:30:40 -070098 // This is needed before calling MapPartitionOnDeviceMapper(), otherwise the
99 // device would be mapped in an inconsistent way.
Yifan Hongf0f4a912019-09-26 17:51:33 -0700100 // If |update| is set, create snapshots and writes super partition metadata.
Yifan Hongf033ecb2020-01-07 18:13:56 -0800101 // 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 Hongf0f4a912019-09-26 17:51:33 -0700105 virtual bool PreparePartitionsForUpdate(uint32_t source_slot,
106 uint32_t target_slot,
107 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800108 bool update,
Daniel Zhengeede4c82023-06-13 11:21:06 -0700109 uint64_t* required_size,
110 ErrorCode* error = nullptr) = 0;
Yifan Hong537802d2018-08-15 13:15:42 -0700111
Yifan Hong0b664d12020-01-13 18:06:54 -0800112 // After writing to new partitions, before rebooting into the new slot, call
113 // this function to indicate writes to new partitions are done.
Yifan Hong7b3910a2020-03-24 17:47:32 -0700114 virtual bool FinishUpdate(bool powerwash_required) = 0;
Yifan Hong2257ee12020-01-13 18:33:00 -0800115
Yifan Hong90965502020-02-19 15:22:47 -0800116 // Get an action to clean up previous update.
117 // Return NoOpAction on non-Virtual A/B devices.
118 // Before applying the next update, run this action to clean up previous
119 // update files. This function blocks until delta files are merged into
120 // current OS partitions and finished cleaning up.
121 // - If successful, action completes with kSuccess.
122 // - If any error, but caller should retry after reboot, action completes with
123 // kError.
124 // - If any irrecoverable failures, action completes with kDeviceCorrupted.
Yifan Hong6a6d0f12020-03-11 13:20:52 -0700125 //
126 // See ResetUpdate for differences between CleanuPreviousUpdateAction and
127 // ResetUpdate.
Yifan Hong90965502020-02-19 15:22:47 -0800128 virtual std::unique_ptr<AbstractAction> GetCleanupPreviousUpdateAction(
129 BootControlInterface* boot_control,
130 PrefsInterface* prefs,
131 CleanupPreviousUpdateActionDelegateInterface* delegate) = 0;
Yifan Hong6a6d0f12020-03-11 13:20:52 -0700132
133 // Called after an unwanted payload has been successfully applied and the
134 // device has not yet been rebooted.
135 //
136 // For snapshot updates (Virtual A/B), it calls
137 // DeltaPerformer::ResetUpdateProgress(false /* quick */) and
138 // frees previously allocated space; the next update will need to be
139 // started over.
140 //
141 // Note: CleanupPreviousUpdateAction does not do anything if an update is in
142 // progress, while ResetUpdate() forcefully free previously
143 // allocated space for snapshot updates.
144 virtual bool ResetUpdate(PrefsInterface* prefs) = 0;
Tianjie99d570d2020-06-04 14:57:19 -0700145
Tianjie3a55fc22021-02-13 16:02:22 -0800146 // Reads the dynamic partitions metadata from the given slot, and puts the
Tianjie99d570d2020-06-04 14:57:19 -0700147 // name of the dynamic partitions with the current suffix to |partitions|.
148 // Returns true on success.
149 virtual bool ListDynamicPartitionsForSlot(
Tianjie3a55fc22021-02-13 16:02:22 -0800150 uint32_t slot,
151 uint32_t current_slot,
152 std::vector<std::string>* partitions) = 0;
Tianjie99d570d2020-06-04 14:57:19 -0700153
154 // Finds a possible location that list all block devices by name; and puts
155 // the result in |path|. Returns true on success.
156 // Sample result: /dev/block/by-name/
157 virtual bool GetDeviceDir(std::string* path) = 0;
Tianjie24f96092020-06-30 12:26:25 -0700158
159 // Verifies that the untouched dynamic partitions in the target metadata have
160 // the same extents as the source metadata.
161 virtual bool VerifyExtentsForUntouchedPartitions(
162 uint32_t source_slot,
163 uint32_t target_slot,
164 const std::vector<std::string>& partitions) = 0;
Kelvin Zhang34618522020-09-28 09:21:02 -0400165 // Partition name is expected to be unsuffixed. e.g. system, vendor
166 // Return an interface to write to a snapshoted partition.
167 // If `is_append` is false, then existing COW data will be overwritten.
168 // Otherwise the cow writer will be opened on APPEND mode, existing COW data
169 // is preserved.
David Andersona4b7ba62023-05-10 21:41:37 -0700170 virtual std::unique_ptr<android::snapshot::ICowWriter> OpenCowWriter(
Kelvin Zhang34618522020-09-28 09:21:02 -0400171 const std::string& unsuffixed_partition_name,
172 const std::optional<std::string>&,
David Andersona4b7ba62023-05-10 21:41:37 -0700173 std::optional<uint64_t> label) = 0;
Kelvin Zhang21a49912021-03-12 14:28:33 -0500174 // Open a general purpose FD capable to reading and writing to COW. Note that
175 // writes must be block aligned.
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800176 virtual std::unique_ptr<FileDescriptor> OpenCowFd(
Kelvin Zhangc82511c2020-11-06 16:01:24 -0500177 const std::string& unsuffixed_partition_name,
178 const std::optional<std::string>&,
179 bool is_append = false) = 0;
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400180
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500181 virtual bool IsDynamicPartition(const std::string& part_name,
182 uint32_t slot) = 0;
Kelvin Zhangeb9de162020-11-16 15:47:28 -0500183
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400184 // Create virtual block devices for all partitions.
185 virtual bool MapAllPartitions() = 0;
186 // Unmap virtual block devices for all partitions.
187 virtual bool UnmapAllPartitions() = 0;
Yifan Hongb0cbd392021-02-04 11:11:45 -0800188
189 // Return if snapshot compression is enabled for this update.
190 // This function should only be called after preparing for an update
191 // (PreparePartitionsForUpdate), and before merging
192 // (see GetCleanupPreviousUpdateAction and CleanupPreviousUpdateAction) or
193 // resetting it (ResetUpdate).
194 //
195 // To know if the device supports snapshot compression by itself, use
196 // GetVirtualAbCompressionFeatureFlag
197 virtual bool UpdateUsesSnapshotCompression() = 0;
Yifan Hong537802d2018-08-15 13:15:42 -0700198};
199
200} // namespace chromeos_update_engine
201
Yifan Hong15726b92019-11-05 19:06:48 -0800202#endif // UPDATE_ENGINE_COMMON_DYNAMIC_PARTITION_CONTROL_INTERFACE_H_