xref: /aosp_15_r20/system/update_engine/payload_consumer/partition_writer.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2020 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_PARTITION_WRITER_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_PARTITION_WRITER_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <cstdint>
21*5a923131SAndroid Build Coastguard Worker #include <memory>
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker #include <brillo/secure_blob.h>
25*5a923131SAndroid Build Coastguard Worker #include <gtest/gtest_prod.h>
26*5a923131SAndroid Build Coastguard Worker 
27*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/dynamic_partition_control_interface.h"
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/extent_writer.h"
29*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/file_descriptor.h"
30*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/install_operation_executor.h"
31*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/install_plan.h"
32*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/partition_writer_interface.h"
33*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/verified_source_fd.h"
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
35*5a923131SAndroid Build Coastguard Worker 
36*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
37*5a923131SAndroid Build Coastguard Worker class PartitionWriter : public PartitionWriterInterface {
38*5a923131SAndroid Build Coastguard Worker  public:
39*5a923131SAndroid Build Coastguard Worker   PartitionWriter(const PartitionUpdate& partition_update,
40*5a923131SAndroid Build Coastguard Worker                   const InstallPlan::Partition& install_part,
41*5a923131SAndroid Build Coastguard Worker                   DynamicPartitionControlInterface* dynamic_control,
42*5a923131SAndroid Build Coastguard Worker                   size_t block_size,
43*5a923131SAndroid Build Coastguard Worker                   bool is_interactive);
44*5a923131SAndroid Build Coastguard Worker   ~PartitionWriter();
45*5a923131SAndroid Build Coastguard Worker   static bool ValidateSourceHash(const brillo::Blob& calculated_hash,
46*5a923131SAndroid Build Coastguard Worker                                  const InstallOperation& operation,
47*5a923131SAndroid Build Coastguard Worker                                  const FileDescriptorPtr source_fd,
48*5a923131SAndroid Build Coastguard Worker                                  ErrorCode* error);
49*5a923131SAndroid Build Coastguard Worker   static bool ValidateSourceHash(const InstallOperation& operation,
50*5a923131SAndroid Build Coastguard Worker                                  const FileDescriptorPtr source_fd,
51*5a923131SAndroid Build Coastguard Worker                                  size_t block_size,
52*5a923131SAndroid Build Coastguard Worker                                  ErrorCode* error);
53*5a923131SAndroid Build Coastguard Worker 
54*5a923131SAndroid Build Coastguard Worker   // Perform necessary initialization work before InstallOperation can be
55*5a923131SAndroid Build Coastguard Worker   // applied to this partition
56*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool Init(const InstallPlan* install_plan,
57*5a923131SAndroid Build Coastguard Worker                           bool source_may_exist,
58*5a923131SAndroid Build Coastguard Worker                           size_t next_op_index) override;
59*5a923131SAndroid Build Coastguard Worker 
60*5a923131SAndroid Build Coastguard Worker   // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's
61*5a923131SAndroid Build Coastguard Worker   // optional. DeltaPerformer may or may not call this everytime an operation is
62*5a923131SAndroid Build Coastguard Worker   // applied.
63*5a923131SAndroid Build Coastguard Worker   //   |next_op_index| is index of next operation that should be applied.
64*5a923131SAndroid Build Coastguard Worker   // |next_op_index-1| is the last operation that is already applied.
65*5a923131SAndroid Build Coastguard Worker   void CheckpointUpdateProgress(size_t next_op_index) override;
66*5a923131SAndroid Build Coastguard Worker 
67*5a923131SAndroid Build Coastguard Worker   // Close partition writer, when calling this function there's no guarantee
68*5a923131SAndroid Build Coastguard Worker   // that all |InstallOperations| are sent to |PartitionWriter|. This function
69*5a923131SAndroid Build Coastguard Worker   // will be called even if we are pausing/aborting the update.
70*5a923131SAndroid Build Coastguard Worker   int Close() override;
71*5a923131SAndroid Build Coastguard Worker 
72*5a923131SAndroid Build Coastguard Worker   // These perform a specific type of operation and return true on success.
73*5a923131SAndroid Build Coastguard Worker   // |error| will be set if source hash mismatch, otherwise |error| might not be
74*5a923131SAndroid Build Coastguard Worker   // set even if it fails.
75*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation,
76*5a923131SAndroid Build Coastguard Worker                                              const void* data,
77*5a923131SAndroid Build Coastguard Worker                                              size_t count) override;
78*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool PerformZeroOrDiscardOperation(
79*5a923131SAndroid Build Coastguard Worker       const InstallOperation& operation) override;
80*5a923131SAndroid Build Coastguard Worker 
81*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool PerformSourceCopyOperation(
82*5a923131SAndroid Build Coastguard Worker       const InstallOperation& operation, ErrorCode* error) override;
83*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation,
84*5a923131SAndroid Build Coastguard Worker                                           ErrorCode* error,
85*5a923131SAndroid Build Coastguard Worker                                           const void* data,
86*5a923131SAndroid Build Coastguard Worker                                           size_t count) override;
87*5a923131SAndroid Build Coastguard Worker 
88*5a923131SAndroid Build Coastguard Worker   // |DeltaPerformer| calls this when all Install Ops are sent to partition
89*5a923131SAndroid Build Coastguard Worker   // writer. No |Perform*Operation| methods will be called in the future, and
90*5a923131SAndroid Build Coastguard Worker   // the partition writer is expected to be closed soon.
FinishedInstallOps()91*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool FinishedInstallOps() override { return true; }
92*5a923131SAndroid Build Coastguard Worker 
93*5a923131SAndroid Build Coastguard Worker  private:
94*5a923131SAndroid Build Coastguard Worker   friend class PartitionWriterTest;
95*5a923131SAndroid Build Coastguard Worker   FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
96*5a923131SAndroid Build Coastguard Worker 
97*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] bool OpenSourcePartition(uint32_t source_slot,
98*5a923131SAndroid Build Coastguard Worker                                          bool source_may_exist);
99*5a923131SAndroid Build Coastguard Worker   FileDescriptorPtr ChooseSourceFD(const InstallOperation& op,
100*5a923131SAndroid Build Coastguard Worker                                    ErrorCode* error);
101*5a923131SAndroid Build Coastguard Worker 
102*5a923131SAndroid Build Coastguard Worker   [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
103*5a923131SAndroid Build Coastguard Worker 
104*5a923131SAndroid Build Coastguard Worker   const PartitionUpdate& partition_update_;
105*5a923131SAndroid Build Coastguard Worker   const InstallPlan::Partition& install_part_;
106*5a923131SAndroid Build Coastguard Worker   DynamicPartitionControlInterface* dynamic_control_;
107*5a923131SAndroid Build Coastguard Worker   // Path to source partition
108*5a923131SAndroid Build Coastguard Worker   std::string source_path_;
109*5a923131SAndroid Build Coastguard Worker   VerifiedSourceFd verified_source_fd_;
110*5a923131SAndroid Build Coastguard Worker   // Path to target partition
111*5a923131SAndroid Build Coastguard Worker   std::string target_path_;
112*5a923131SAndroid Build Coastguard Worker   FileDescriptorPtr target_fd_;
113*5a923131SAndroid Build Coastguard Worker   const bool interactive_;
114*5a923131SAndroid Build Coastguard Worker   const size_t block_size_;
115*5a923131SAndroid Build Coastguard Worker 
116*5a923131SAndroid Build Coastguard Worker   // This instance handles decompression/bsdfif/puffdiff. It's responsible for
117*5a923131SAndroid Build Coastguard Worker   // constructing data which should be written to target partition, actual
118*5a923131SAndroid Build Coastguard Worker   // "writing" is handled by |PartitionWriter|
119*5a923131SAndroid Build Coastguard Worker   InstallOperationExecutor install_op_executor_;
120*5a923131SAndroid Build Coastguard Worker };
121*5a923131SAndroid Build Coastguard Worker 
122*5a923131SAndroid Build Coastguard Worker namespace partition_writer {
123*5a923131SAndroid Build Coastguard Worker // Return a PartitionWriter instance for perform InstallOps on this partition.
124*5a923131SAndroid Build Coastguard Worker // Uses VABCPartitionWriter for Virtual AB Compression
125*5a923131SAndroid Build Coastguard Worker std::unique_ptr<PartitionWriterInterface> CreatePartitionWriter(
126*5a923131SAndroid Build Coastguard Worker     const PartitionUpdate& partition_update,
127*5a923131SAndroid Build Coastguard Worker     const InstallPlan::Partition& install_part,
128*5a923131SAndroid Build Coastguard Worker     DynamicPartitionControlInterface* dynamic_control,
129*5a923131SAndroid Build Coastguard Worker     size_t block_size,
130*5a923131SAndroid Build Coastguard Worker     bool is_interactive,
131*5a923131SAndroid Build Coastguard Worker     bool is_dynamic_partition);
132*5a923131SAndroid Build Coastguard Worker }  // namespace partition_writer
133*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
134*5a923131SAndroid Build Coastguard Worker 
135*5a923131SAndroid Build Coastguard Worker #endif
136