blob: 89e5884068024d5fe35da450eb1d7d3283158673 [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"
Kelvin Zhang40d96662021-02-24 14:21:29 -050030#include "update_engine/payload_consumer/install_operation_executor.h"
Kelvin Zhang50bac652020-09-28 15:51:41 -040031#include "update_engine/payload_consumer/install_plan.h"
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050032#include "update_engine/payload_consumer/partition_writer_interface.h"
Kelvin Zhangab3ce602021-02-24 14:46:40 -050033#include "update_engine/payload_consumer/verified_source_fd.h"
Kelvin Zhang50bac652020-09-28 15:51:41 -040034#include "update_engine/update_metadata.pb.h"
Kelvin Zhangcfe694f2020-11-13 13:10:42 -050035
Kelvin Zhang50bac652020-09-28 15:51:41 -040036namespace chromeos_update_engine {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050037class PartitionWriter : public PartitionWriterInterface {
Kelvin Zhang50bac652020-09-28 15:51:41 -040038 public:
39 PartitionWriter(const PartitionUpdate& partition_update,
40 const InstallPlan::Partition& install_part,
41 DynamicPartitionControlInterface* dynamic_control,
42 size_t block_size,
43 bool is_interactive);
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050044 ~PartitionWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -040045 static bool ValidateSourceHash(const brillo::Blob& calculated_hash,
46 const InstallOperation& operation,
47 const FileDescriptorPtr source_fd,
48 ErrorCode* error);
49
50 // Perform necessary initialization work before InstallOperation can be
51 // applied to this partition
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050052 [[nodiscard]] bool Init(const InstallPlan* install_plan,
53 bool source_may_exist,
54 size_t next_op_index) override;
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040055
56 // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's
57 // optional. DeltaPerformer may or may not call this everytime an operation is
58 // applied.
59 // |next_op_index| is index of next operation that should be applied.
60 // |next_op_index-1| is the last operation that is already applied.
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050061 void CheckpointUpdateProgress(size_t next_op_index) override;
Kelvin Zhang50bac652020-09-28 15:51:41 -040062
Kelvin Zhangec205cf2020-09-28 13:23:40 -040063 // Close partition writer, when calling this function there's no guarantee
64 // that all |InstallOperations| are sent to |PartitionWriter|. This function
65 // will be called even if we are pausing/aborting the update.
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050066 int Close() override;
Kelvin Zhang50bac652020-09-28 15:51:41 -040067
68 // These perform a specific type of operation and return true on success.
69 // |error| will be set if source hash mismatch, otherwise |error| might not be
70 // set even if it fails.
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050071 [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation,
72 const void* data,
73 size_t count) override;
74 [[nodiscard]] bool PerformZeroOrDiscardOperation(
75 const InstallOperation& operation) override;
Kelvin Zhang50bac652020-09-28 15:51:41 -040076
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050077 [[nodiscard]] bool PerformSourceCopyOperation(
78 const InstallOperation& operation, ErrorCode* error) override;
Tianjie8e0090d2021-08-30 22:35:21 -070079 [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation,
80 ErrorCode* error,
81 const void* data,
82 size_t count) override;
Kelvin Zhang50bac652020-09-28 15:51:41 -040083
Kelvin Zhangec205cf2020-09-28 13:23:40 -040084 // |DeltaPerformer| calls this when all Install Ops are sent to partition
85 // writer. No |Perform*Operation| methods will be called in the future, and
86 // the partition writer is expected to be closed soon.
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050087 [[nodiscard]] bool FinishedInstallOps() override { return true; }
Kelvin Zhangec205cf2020-09-28 13:23:40 -040088
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050089 private:
Kelvin Zhang50bac652020-09-28 15:51:41 -040090 friend class PartitionWriterTest;
91 FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
92
Kelvin Zhangab3ce602021-02-24 14:46:40 -050093 [[nodiscard]] bool OpenSourcePartition(uint32_t source_slot,
94 bool source_may_exist);
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050095 FileDescriptorPtr ChooseSourceFD(const InstallOperation& op,
Kelvin Zhang50bac652020-09-28 15:51:41 -040096 ErrorCode* error);
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050097
98 [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
Kelvin Zhang50bac652020-09-28 15:51:41 -040099
100 const PartitionUpdate& partition_update_;
101 const InstallPlan::Partition& install_part_;
102 DynamicPartitionControlInterface* dynamic_control_;
103 // Path to source partition
104 std::string source_path_;
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500105 VerifiedSourceFd verified_source_fd_;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400106 // Path to target partition
107 std::string target_path_;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400108 FileDescriptorPtr target_fd_;
109 const bool interactive_;
110 const size_t block_size_;
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500111
112 // This instance handles decompression/bsdfif/puffdiff. It's responsible for
113 // constructing data which should be written to target partition, actual
114 // "writing" is handled by |PartitionWriter|
115 InstallOperationExecutor install_op_executor_;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400116};
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400117
118namespace partition_writer {
119// Return a PartitionWriter instance for perform InstallOps on this partition.
120// Uses VABCPartitionWriter for Virtual AB Compression
Kelvin Zhange52b6cd2021-02-09 15:28:40 -0500121std::unique_ptr<PartitionWriterInterface> CreatePartitionWriter(
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400122 const PartitionUpdate& partition_update,
123 const InstallPlan::Partition& install_part,
124 DynamicPartitionControlInterface* dynamic_control,
125 size_t block_size,
126 bool is_interactive,
127 bool is_dynamic_partition);
128} // namespace partition_writer
Kelvin Zhang50bac652020-09-28 15:51:41 -0400129} // namespace chromeos_update_engine
130
131#endif