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