1 // 2 // Copyright (C) 2012 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_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ 18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ 19 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 23 #include <memory> 24 #include <string> 25 #include <utility> 26 #include <vector> 27 28 #include <brillo/message_loops/message_loop.h> 29 30 #include "update_engine/common/action.h" 31 #include "update_engine/common/hash_calculator.h" 32 #include "update_engine/common/scoped_task_id.h" 33 #include "update_engine/payload_consumer/file_descriptor.h" 34 #include "update_engine/payload_consumer/install_plan.h" 35 #include "update_engine/payload_consumer/verity_writer_interface.h" 36 37 // This action will hash all the partitions of the target slot involved in the 38 // update. The hashes are then verified against the ones in the InstallPlan. 39 // If the target hash does not match, the action will fail. In case of failure, 40 // the error code will depend on whether the source slot hashes are provided and 41 // match. 42 43 namespace chromeos_update_engine { 44 45 // The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash 46 // on the target partitions based on the already populated size and verifies it 47 // matches the one in the target_hash in the InstallPlan. 48 // If the hash matches, then we skip the kVerifySourceHash step, otherwise we 49 // need to check if the source is the root cause of the mismatch. 50 enum class VerifierStep { 51 kVerifyTargetHash, 52 kVerifySourceHash, 53 }; 54 55 class FilesystemVerifyDelegate { 56 public: 57 virtual ~FilesystemVerifyDelegate() = default; 58 virtual void OnVerifyProgressUpdate(double progress) = 0; 59 }; 60 61 class FilesystemVerifierAction : public InstallPlanAction { 62 public: FilesystemVerifierAction(DynamicPartitionControlInterface * dynamic_control)63 explicit FilesystemVerifierAction( 64 DynamicPartitionControlInterface* dynamic_control) 65 : verity_writer_(verity_writer::CreateVerityWriter()), 66 dynamic_control_(dynamic_control) { 67 CHECK(dynamic_control_); 68 } 69 70 ~FilesystemVerifierAction() override = default; 71 72 void PerformAction() override; 73 void TerminateProcessing() override; 74 75 // Used for listening to progress updates set_delegate(FilesystemVerifyDelegate * delegate)76 void set_delegate(FilesystemVerifyDelegate* delegate) { 77 this->delegate_ = delegate; 78 } get_delegate()79 [[nodiscard]] FilesystemVerifyDelegate* get_delegate() const { 80 return this->delegate_; 81 } 82 83 // Debugging/logging StaticType()84 static std::string StaticType() { return "FilesystemVerifierAction"; } Type()85 std::string Type() const override { return StaticType(); } 86 87 private: 88 friend class FilesystemVerifierActionTestDelegate; 89 // Wrapper function that schedules calls of EncodeFEC. Returns true on success 90 void WriteVerityData(FileDescriptor* fd, 91 void* buffer, 92 const size_t buffer_size); 93 void WriteVerityAndHashPartition(const off64_t start_offset, 94 const off64_t end_offset, 95 void* buffer, 96 const size_t buffer_size); 97 void HashPartition(const off64_t start_offset, 98 const off64_t end_offset, 99 void* buffer, 100 const size_t buffer_size); 101 102 // Return true if we need to write verity bytes. 103 bool ShouldWriteVerity(); 104 // Starts the hashing of the current partition. If there aren't any partitions 105 // remaining to be hashed, it finishes the action. 106 void StartPartitionHashing(); 107 108 const std::string& GetPartitionPath() const; 109 110 bool IsVABC(const InstallPlan::Partition& partition) const; 111 112 size_t GetPartitionSize() const; 113 114 // When the read is done, finalize the hash checking of the current partition 115 // and continue checking the next one. 116 void FinishPartitionHashing(); 117 118 // Cleans up all the variables we use for async operations and tells the 119 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be 120 // true if TerminateProcessing() was called. 121 void Cleanup(ErrorCode code); 122 123 // Invoke delegate callback to report progress, if delegate is not null 124 void UpdateProgress(double progress); 125 126 // Updates progress of current partition. |progress| should be in range [0, 127 // 1], and it will be scaled appropriately with # of partitions. 128 void UpdatePartitionProgress(double progress); 129 130 // Initialize read_fd_ and write_fd_ 131 bool InitializeFd(const std::string& part_path); 132 bool InitializeFdVABC(bool should_write_verity); 133 134 // The type of the partition that we are verifying. 135 VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash; 136 137 // The index in the install_plan_.partitions vector of the partition currently 138 // being hashed. 139 size_t partition_index_{0}; 140 141 // If not null, the FileDescriptor used to read from the device. 142 // verity writer might attempt to write to this fd, if verity is enabled. 143 std::unique_ptr<FileDescriptor> partition_fd_; 144 145 // Buffer for storing data we read. 146 brillo::Blob buffer_; 147 148 bool cancelled_{false}; // true if the action has been cancelled. 149 150 // Calculates the hash of the data. 151 std::unique_ptr<HashCalculator> hasher_; 152 153 // Write verity data of the current partition. 154 std::unique_ptr<VerityWriterInterface> verity_writer_; 155 156 // Verifies the untouched dynamic partitions for partial updates. 157 DynamicPartitionControlInterface* dynamic_control_{nullptr}; 158 159 // Reads and hashes this many bytes from the head of the input stream. When 160 // the partition starts to be hashed, this field is initialized from the 161 // corresponding InstallPlan::Partition size which is the total size 162 // update_engine is expected to write, and may be smaller than the size of the 163 // partition in gpt. 164 uint64_t partition_size_{0}; 165 166 // The byte offset that we are reading in the current partition. 167 uint64_t offset_{0}; 168 169 // The end offset of filesystem data, first byte position of hashtree. 170 uint64_t filesystem_data_end_{0}; 171 172 // An observer that observes progress updates of this action. 173 FilesystemVerifyDelegate* delegate_{}; 174 175 // Callback that should be cancelled on |TerminateProcessing|. Usually this 176 // points to pending read callbacks from async stream. 177 ScopedTaskId pending_task_id_; 178 179 // Cumulative sum of partition sizes. Used for progress report. 180 // This vector will always start with 0, and end with total size of all 181 // partitions. 182 std::vector<size_t> partition_weight_; 183 184 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction); 185 }; 186 187 } // namespace chromeos_update_engine 188 189 #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ 190