blob: dee30e8ec291ac12f6341b937ab70b541ea3eb5e [file] [log] [blame]
Kelvin Zhang50bac652020-09-28 15:51:41 -04001//
2// Copyright (C) 2020 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_PARTITION_WRITER_H_
18#define UPDATE_ENGINE_PARTITION_WRITER_H_
19
20#include <cstdint>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040021#include <memory>
Kelvin Zhang50bac652020-09-28 15:51:41 -040022#include <string>
23
24#include <brillo/secure_blob.h>
25#include <gtest/gtest_prod.h>
26
27#include "update_engine/common/dynamic_partition_control_interface.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040028#include "update_engine/payload_consumer/extent_writer.h"
Kelvin Zhang50bac652020-09-28 15:51:41 -040029#include "update_engine/payload_consumer/file_descriptor.h"
30#include "update_engine/payload_consumer/install_plan.h"
31#include "update_engine/update_metadata.pb.h"
Kelvin Zhangcfe694f2020-11-13 13:10:42 -050032
Kelvin Zhang50bac652020-09-28 15:51:41 -040033namespace chromeos_update_engine {
Kelvin Zhang1d402cb2021-02-09 15:28:16 -050034
35// A reference class for interpretation of different OTA ops.
36// Different partition writers(VABCPartitionWriter and the regular
37// PartitionWriter) will use this class via composition, and optionally optimize
38// for some specific operations. For example, in VABC, copy operations are
39// handled with special care, but other operations are defaulted to this class.
40class InstallOperationExecutor {
41 public:
42 explicit InstallOperationExecutor(size_t block_size)
43 : block_size_(block_size) {}
44
45 bool ExecuteInstallOp(const InstallOperation& op,
46 std::unique_ptr<ExtentWriter> writer,
47 FileDescriptorPtr source_fd,
48 const void* data,
49 size_t size);
50 bool ExecuteReplaceOperation(const InstallOperation& operation,
51 std::unique_ptr<ExtentWriter> writer,
52 const void* data,
53 size_t count);
54 bool ExecuteZeroOrDiscardOperation(const InstallOperation& operation,
55 ExtentWriter* writer);
56 bool ExecuteSourceCopyOperation(const InstallOperation& operation,
57 ExtentWriter* writer,
58 FileDescriptorPtr source_fd);
59 bool ExecuteSourceBsdiffOperation(const InstallOperation& operation,
60 std::unique_ptr<ExtentWriter> writer,
61 FileDescriptorPtr source_fd,
62 const void* data,
63 size_t count);
64 bool ExecutePuffDiffOperation(const InstallOperation& operation,
65 std::unique_ptr<ExtentWriter> writer,
66 FileDescriptorPtr source_fd,
67 const void* data,
68 size_t count);
69
70 private:
71 size_t block_size_;
72};
73
Kelvin Zhang50bac652020-09-28 15:51:41 -040074class PartitionWriter {
75 public:
76 PartitionWriter(const PartitionUpdate& partition_update,
77 const InstallPlan::Partition& install_part,
78 DynamicPartitionControlInterface* dynamic_control,
79 size_t block_size,
80 bool is_interactive);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040081 virtual ~PartitionWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -040082 static bool ValidateSourceHash(const brillo::Blob& calculated_hash,
83 const InstallOperation& operation,
84 const FileDescriptorPtr source_fd,
85 ErrorCode* error);
86
87 // Perform necessary initialization work before InstallOperation can be
88 // applied to this partition
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040089 [[nodiscard]] virtual bool Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040090 bool source_may_exist,
91 size_t next_op_index);
92
93 // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's
94 // optional. DeltaPerformer may or may not call this everytime an operation is
95 // applied.
96 // |next_op_index| is index of next operation that should be applied.
97 // |next_op_index-1| is the last operation that is already applied.
98 virtual void CheckpointUpdateProgress(size_t next_op_index);
Kelvin Zhang50bac652020-09-28 15:51:41 -040099
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400100 // Close partition writer, when calling this function there's no guarantee
101 // that all |InstallOperations| are sent to |PartitionWriter|. This function
102 // will be called even if we are pausing/aborting the update.
Kelvin Zhang50bac652020-09-28 15:51:41 -0400103 int Close();
104
105 // These perform a specific type of operation and return true on success.
106 // |error| will be set if source hash mismatch, otherwise |error| might not be
107 // set even if it fails.
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400108 [[nodiscard]] virtual bool PerformReplaceOperation(
109 const InstallOperation& operation, const void* data, size_t count);
110 [[nodiscard]] virtual bool PerformZeroOrDiscardOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400111 const InstallOperation& operation);
112
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400113 [[nodiscard]] virtual bool PerformSourceCopyOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400114 const InstallOperation& operation, ErrorCode* error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400115 [[nodiscard]] virtual bool PerformSourceBsdiffOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400116 const InstallOperation& operation,
117 ErrorCode* error,
118 const void* data,
119 size_t count);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400120 [[nodiscard]] virtual bool PerformPuffDiffOperation(
121 const InstallOperation& operation,
122 ErrorCode* error,
123 const void* data,
124 size_t count);
Kelvin Zhang50bac652020-09-28 15:51:41 -0400125
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400126 // |DeltaPerformer| calls this when all Install Ops are sent to partition
127 // writer. No |Perform*Operation| methods will be called in the future, and
128 // the partition writer is expected to be closed soon.
129 [[nodiscard]] virtual bool FinishedInstallOps() { return true; }
130
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400131 protected:
Kelvin Zhang50bac652020-09-28 15:51:41 -0400132 friend class PartitionWriterTest;
133 FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
134
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500135 bool OpenSourcePartition(uint32_t source_slot, bool source_may_exist);
136
Kelvin Zhang50bac652020-09-28 15:51:41 -0400137 bool OpenCurrentECCPartition();
138 // For a given operation, choose the source fd to be used (raw device or error
139 // correction device) based on the source operation hash.
140 // Returns nullptr if the source hash mismatch cannot be corrected, and set
141 // the |error| accordingly.
142 FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation,
143 ErrorCode* error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400144 [[nodiscard]] virtual std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400145
146 const PartitionUpdate& partition_update_;
147 const InstallPlan::Partition& install_part_;
148 DynamicPartitionControlInterface* dynamic_control_;
149 // Path to source partition
150 std::string source_path_;
151 // Path to target partition
152 std::string target_path_;
153 FileDescriptorPtr source_fd_;
154 FileDescriptorPtr target_fd_;
155 const bool interactive_;
156 const size_t block_size_;
157 // File descriptor of the error corrected source partition. Only set while
158 // updating partition using a delta payload for a partition where error
159 // correction is available. The size of the error corrected device is smaller
160 // than the underlying raw device, since it doesn't include the error
161 // correction blocks.
162 FileDescriptorPtr source_ecc_fd_{nullptr};
163
164 // The total number of operations that failed source hash verification but
165 // passed after falling back to the error-corrected |source_ecc_fd_| device.
166 uint64_t source_ecc_recovered_failures_{0};
167
168 // Whether opening the current partition as an error-corrected device failed.
169 // Used to avoid re-opening the same source partition if it is not actually
170 // error corrected.
171 bool source_ecc_open_failure_{false};
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500172
173 // This instance handles decompression/bsdfif/puffdiff. It's responsible for
174 // constructing data which should be written to target partition, actual
175 // "writing" is handled by |PartitionWriter|
176 InstallOperationExecutor install_op_executor_;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400177};
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400178
179namespace partition_writer {
180// Return a PartitionWriter instance for perform InstallOps on this partition.
181// Uses VABCPartitionWriter for Virtual AB Compression
182std::unique_ptr<PartitionWriter> CreatePartitionWriter(
183 const PartitionUpdate& partition_update,
184 const InstallPlan::Partition& install_part,
185 DynamicPartitionControlInterface* dynamic_control,
186 size_t block_size,
187 bool is_interactive,
188 bool is_dynamic_partition);
189} // namespace partition_writer
Kelvin Zhang50bac652020-09-28 15:51:41 -0400190} // namespace chromeos_update_engine
191
192#endif