blob: dd48eff74c500985a93f2bb6737c69d24cede557 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Allie Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
Allie Woodeb9e6d82015-04-17 13:55:30 -070019
20#include <sys/stat.h>
21#include <sys/types.h>
22
Amin Hassaniabe4a772018-07-26 11:19:10 -070023#include <memory>
Allie Woodeb9e6d82015-04-17 13:55:30 -070024#include <string>
Kelvin Zhang8704c832021-05-10 17:53:14 -040025#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070026#include <vector>
27
Kelvin Zhangec205cf2020-09-28 13:23:40 -040028#include <brillo/message_loops/message_loop.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/action.h"
31#include "update_engine/common/hash_calculator.h"
Kelvin Zhang8704c832021-05-10 17:53:14 -040032#include "update_engine/common/scoped_task_id.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040033#include "update_engine/payload_consumer/file_descriptor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/payload_consumer/install_plan.h"
Sen Jiang57f91802017-11-14 17:42:13 -080035#include "update_engine/payload_consumer/verity_writer_interface.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070036
Sen Jiangfef85fd2016-03-25 15:32:49 -070037// 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.
Allie Woodeb9e6d82015-04-17 13:55:30 -070042
43namespace chromeos_update_engine {
44
Sen Jiangfef85fd2016-03-25 15:32:49 -070045// 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.
50enum class VerifierStep {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070051 kVerifyTargetHash,
Sen Jiang1ad42ad2015-11-17 15:04:02 -080052 kVerifySourceHash,
Allie Woodeb9e6d82015-04-17 13:55:30 -070053};
54
Kelvin Zhang70eef232020-06-12 20:32:40 +000055class FilesystemVerifyDelegate {
56 public:
57 virtual ~FilesystemVerifyDelegate() = default;
58 virtual void OnVerifyProgressUpdate(double progress) = 0;
59};
60
Allie Woodeb9e6d82015-04-17 13:55:30 -070061class FilesystemVerifierAction : public InstallPlanAction {
62 public:
Tianjie24f96092020-06-30 12:26:25 -070063 explicit FilesystemVerifierAction(
64 DynamicPartitionControlInterface* dynamic_control)
65 : verity_writer_(verity_writer::CreateVerityWriter()),
66 dynamic_control_(dynamic_control) {
67 CHECK(dynamic_control_);
68 }
69
Sen Jiang57f91802017-11-14 17:42:13 -080070 ~FilesystemVerifierAction() override = default;
Allie Woodeb9e6d82015-04-17 13:55:30 -070071
72 void PerformAction() override;
73 void TerminateProcessing() override;
74
Kelvin Zhang70eef232020-06-12 20:32:40 +000075 // Used for listening to progress updates
76 void set_delegate(FilesystemVerifyDelegate* delegate) {
77 this->delegate_ = delegate;
78 }
79 [[nodiscard]] FilesystemVerifyDelegate* get_delegate() const {
80 return this->delegate_;
81 }
82
Allie Woodeb9e6d82015-04-17 13:55:30 -070083 // Debugging/logging
84 static std::string StaticType() { return "FilesystemVerifierAction"; }
85 std::string Type() const override { return StaticType(); }
86
87 private:
Amin Hassaniabe4a772018-07-26 11:19:10 -070088 friend class FilesystemVerifierActionTestDelegate;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -080089 void WriteVerityAndHashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -040090 const off64_t end_offset,
91 void* buffer,
92 const size_t buffer_size);
Kelvin Zhang1a0ed712022-01-26 16:09:05 -080093 void HashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -040094 const off64_t end_offset,
95 void* buffer,
96 const size_t buffer_size);
Kelvin Zhangec205cf2020-09-28 13:23:40 -040097
98 // Return true if we need to write verity bytes.
99 bool ShouldWriteVerity();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700100 // Starts the hashing of the current partition. If there aren't any partitions
Sen Jiangfef85fd2016-03-25 15:32:49 -0700101 // remaining to be hashed, it finishes the action.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700102 void StartPartitionHashing();
103
Kelvin Zhange012f652021-05-10 16:00:31 -0400104 const std::string& GetPartitionPath() const;
105
106 bool IsVABC(const InstallPlan::Partition& partition) const;
107
108 size_t GetPartitionSize() const;
109
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700110 // When the read is done, finalize the hash checking of the current partition
111 // and continue checking the next one.
112 void FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700113
114 // Cleans up all the variables we use for async operations and tells the
115 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
116 // true if TerminateProcessing() was called.
117 void Cleanup(ErrorCode code);
118
Kelvin Zhang70eef232020-06-12 20:32:40 +0000119 // Invoke delegate callback to report progress, if delegate is not null
120 void UpdateProgress(double progress);
121
Kelvin Zhang8704c832021-05-10 17:53:14 -0400122 // Updates progress of current partition. |progress| should be in range [0,
123 // 1], and it will be scaled appropriately with # of partitions.
124 void UpdatePartitionProgress(double progress);
125
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400126 // Initialize read_fd_ and write_fd_
127 bool InitializeFd(const std::string& part_path);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400128 bool InitializeFdVABC(bool should_write_verity);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400129
Allie Woodeb9e6d82015-04-17 13:55:30 -0700130 // The type of the partition that we are verifying.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700131 VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700132
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700133 // The index in the install_plan_.partitions vector of the partition currently
134 // being hashed.
135 size_t partition_index_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700136
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400137 // If not null, the FileDescriptor used to read from the device.
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500138 // verity writer might attempt to write to this fd, if verity is enabled.
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800139 std::unique_ptr<FileDescriptor> partition_fd_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700140
141 // Buffer for storing data we read.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700142 brillo::Blob buffer_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700143
Alex Deymo20c99202015-07-09 16:14:16 -0700144 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700145
Allie Woodeb9e6d82015-04-17 13:55:30 -0700146 // Calculates the hash of the data.
Alex Deymo39910dc2015-11-09 17:04:30 -0800147 std::unique_ptr<HashCalculator> hasher_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700148
Sen Jiang57f91802017-11-14 17:42:13 -0800149 // Write verity data of the current partition.
150 std::unique_ptr<VerityWriterInterface> verity_writer_;
151
Tianjie24f96092020-06-30 12:26:25 -0700152 // Verifies the untouched dynamic partitions for partial updates.
153 DynamicPartitionControlInterface* dynamic_control_{nullptr};
154
Sen Jiang57f91802017-11-14 17:42:13 -0800155 // Reads and hashes this many bytes from the head of the input stream. When
156 // the partition starts to be hashed, this field is initialized from the
157 // corresponding InstallPlan::Partition size which is the total size
158 // update_engine is expected to write, and may be smaller than the size of the
159 // partition in gpt.
160 uint64_t partition_size_{0};
161
162 // The byte offset that we are reading in the current partition.
163 uint64_t offset_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700164
Kelvin Zhang7f925672021-03-15 13:37:40 -0400165 // The end offset of filesystem data, first byte position of hashtree.
166 uint64_t filesystem_data_end_{0};
167
Kelvin Zhang70eef232020-06-12 20:32:40 +0000168 // An observer that observes progress updates of this action.
169 FilesystemVerifyDelegate* delegate_{};
170
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400171 // Callback that should be cancelled on |TerminateProcessing|. Usually this
172 // points to pending read callbacks from async stream.
Kelvin Zhang8704c832021-05-10 17:53:14 -0400173 ScopedTaskId pending_task_id_;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400174
Kelvin Zhang5b145822022-07-26 10:56:09 -0700175 // Cumulative sum of partition sizes. Used for progress report.
176 // This vector will always start with 0, and end with total size of all
177 // partitions.
178 std::vector<size_t> partition_weight_;
179
Allie Woodeb9e6d82015-04-17 13:55:30 -0700180 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
181};
182
183} // namespace chromeos_update_engine
184
Alex Deymo39910dc2015-11-09 17:04:30 -0800185#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_