blob: 4b420d2dec1a72e18e49b9aa40dbac87e3a17269 [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"
32namespace chromeos_update_engine {
33class PartitionWriter {
34 public:
35 PartitionWriter(const PartitionUpdate& partition_update,
36 const InstallPlan::Partition& install_part,
37 DynamicPartitionControlInterface* dynamic_control,
38 size_t block_size,
39 bool is_interactive);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040040 virtual ~PartitionWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -040041 static bool ValidateSourceHash(const brillo::Blob& calculated_hash,
42 const InstallOperation& operation,
43 const FileDescriptorPtr source_fd,
44 ErrorCode* error);
45
46 // Perform necessary initialization work before InstallOperation can be
47 // applied to this partition
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040048 [[nodiscard]] virtual bool Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040049 bool source_may_exist,
50 size_t next_op_index);
51
52 // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's
53 // optional. DeltaPerformer may or may not call this everytime an operation is
54 // applied.
55 // |next_op_index| is index of next operation that should be applied.
56 // |next_op_index-1| is the last operation that is already applied.
57 virtual void CheckpointUpdateProgress(size_t next_op_index);
Kelvin Zhang50bac652020-09-28 15:51:41 -040058
Kelvin Zhangec205cf2020-09-28 13:23:40 -040059 // Close partition writer, when calling this function there's no guarantee
60 // that all |InstallOperations| are sent to |PartitionWriter|. This function
61 // will be called even if we are pausing/aborting the update.
Kelvin Zhang50bac652020-09-28 15:51:41 -040062 int Close();
63
64 // These perform a specific type of operation and return true on success.
65 // |error| will be set if source hash mismatch, otherwise |error| might not be
66 // set even if it fails.
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040067 [[nodiscard]] virtual bool PerformReplaceOperation(
68 const InstallOperation& operation, const void* data, size_t count);
69 [[nodiscard]] virtual bool PerformZeroOrDiscardOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -040070 const InstallOperation& operation);
71
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040072 [[nodiscard]] virtual bool PerformSourceCopyOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -040073 const InstallOperation& operation, ErrorCode* error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040074 [[nodiscard]] virtual bool PerformSourceBsdiffOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -040075 const InstallOperation& operation,
76 ErrorCode* error,
77 const void* data,
78 size_t count);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040079 [[nodiscard]] virtual bool PerformPuffDiffOperation(
80 const InstallOperation& operation,
81 ErrorCode* error,
82 const void* data,
83 size_t count);
Kelvin Zhang50bac652020-09-28 15:51:41 -040084
Kelvin Zhangec205cf2020-09-28 13:23:40 -040085 // |DeltaPerformer| calls this when all Install Ops are sent to partition
86 // writer. No |Perform*Operation| methods will be called in the future, and
87 // the partition writer is expected to be closed soon.
88 [[nodiscard]] virtual bool FinishedInstallOps() { return true; }
89
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040090 protected:
Kelvin Zhang50bac652020-09-28 15:51:41 -040091 friend class PartitionWriterTest;
92 FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
93
Kelvin Zhang3f60d532020-11-09 13:33:17 -050094 bool OpenSourcePartition(uint32_t source_slot, bool source_may_exist);
95
Kelvin Zhang50bac652020-09-28 15:51:41 -040096 bool OpenCurrentECCPartition();
97 // For a given operation, choose the source fd to be used (raw device or error
98 // correction device) based on the source operation hash.
99 // Returns nullptr if the source hash mismatch cannot be corrected, and set
100 // the |error| accordingly.
101 FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation,
102 ErrorCode* error);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400103 [[nodiscard]] virtual std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -0400104
105 const PartitionUpdate& partition_update_;
106 const InstallPlan::Partition& install_part_;
107 DynamicPartitionControlInterface* dynamic_control_;
108 // Path to source partition
109 std::string source_path_;
110 // Path to target partition
111 std::string target_path_;
112 FileDescriptorPtr source_fd_;
113 FileDescriptorPtr target_fd_;
114 const bool interactive_;
115 const size_t block_size_;
116 // File descriptor of the error corrected source partition. Only set while
117 // updating partition using a delta payload for a partition where error
118 // correction is available. The size of the error corrected device is smaller
119 // than the underlying raw device, since it doesn't include the error
120 // correction blocks.
121 FileDescriptorPtr source_ecc_fd_{nullptr};
122
123 // The total number of operations that failed source hash verification but
124 // passed after falling back to the error-corrected |source_ecc_fd_| device.
125 uint64_t source_ecc_recovered_failures_{0};
126
127 // Whether opening the current partition as an error-corrected device failed.
128 // Used to avoid re-opening the same source partition if it is not actually
129 // error corrected.
130 bool source_ecc_open_failure_{false};
131};
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400132
133namespace partition_writer {
134// Return a PartitionWriter instance for perform InstallOps on this partition.
135// Uses VABCPartitionWriter for Virtual AB Compression
136std::unique_ptr<PartitionWriter> CreatePartitionWriter(
137 const PartitionUpdate& partition_update,
138 const InstallPlan::Partition& install_part,
139 DynamicPartitionControlInterface* dynamic_control,
140 size_t block_size,
141 bool is_interactive,
142 bool is_dynamic_partition);
143} // namespace partition_writer
Kelvin Zhang50bac652020-09-28 15:51:41 -0400144} // namespace chromeos_update_engine
145
146#endif