blob: b6df4b89b2ed43d07b58d94cce2920c16922fe03 [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>
25#include <vector>
26
Kelvin Zhangec205cf2020-09-28 13:23:40 -040027#include <brillo/message_loops/message_loop.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/action.h"
30#include "update_engine/common/hash_calculator.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040031#include "update_engine/payload_consumer/file_descriptor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/payload_consumer/install_plan.h"
Sen Jiang57f91802017-11-14 17:42:13 -080033#include "update_engine/payload_consumer/verity_writer_interface.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070034
Sen Jiangfef85fd2016-03-25 15:32:49 -070035// This action will hash all the partitions of the target slot involved in the
36// update. The hashes are then verified against the ones in the InstallPlan.
37// If the target hash does not match, the action will fail. In case of failure,
38// the error code will depend on whether the source slot hashes are provided and
39// match.
Allie Woodeb9e6d82015-04-17 13:55:30 -070040
41namespace chromeos_update_engine {
42
Sen Jiangfef85fd2016-03-25 15:32:49 -070043// The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash
44// on the target partitions based on the already populated size and verifies it
45// matches the one in the target_hash in the InstallPlan.
46// If the hash matches, then we skip the kVerifySourceHash step, otherwise we
47// need to check if the source is the root cause of the mismatch.
48enum class VerifierStep {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070049 kVerifyTargetHash,
Sen Jiang1ad42ad2015-11-17 15:04:02 -080050 kVerifySourceHash,
Allie Woodeb9e6d82015-04-17 13:55:30 -070051};
52
Kelvin Zhang70eef232020-06-12 20:32:40 +000053class FilesystemVerifyDelegate {
54 public:
55 virtual ~FilesystemVerifyDelegate() = default;
56 virtual void OnVerifyProgressUpdate(double progress) = 0;
57};
58
Allie Woodeb9e6d82015-04-17 13:55:30 -070059class FilesystemVerifierAction : public InstallPlanAction {
60 public:
Tianjie24f96092020-06-30 12:26:25 -070061 explicit FilesystemVerifierAction(
62 DynamicPartitionControlInterface* dynamic_control)
63 : verity_writer_(verity_writer::CreateVerityWriter()),
64 dynamic_control_(dynamic_control) {
65 CHECK(dynamic_control_);
66 }
67
Sen Jiang57f91802017-11-14 17:42:13 -080068 ~FilesystemVerifierAction() override = default;
Allie Woodeb9e6d82015-04-17 13:55:30 -070069
70 void PerformAction() override;
71 void TerminateProcessing() override;
72
Kelvin Zhang70eef232020-06-12 20:32:40 +000073 // Used for listening to progress updates
74 void set_delegate(FilesystemVerifyDelegate* delegate) {
75 this->delegate_ = delegate;
76 }
77 [[nodiscard]] FilesystemVerifyDelegate* get_delegate() const {
78 return this->delegate_;
79 }
80
Allie Woodeb9e6d82015-04-17 13:55:30 -070081 // Debugging/logging
82 static std::string StaticType() { return "FilesystemVerifierAction"; }
83 std::string Type() const override { return StaticType(); }
84
85 private:
Amin Hassaniabe4a772018-07-26 11:19:10 -070086 friend class FilesystemVerifierActionTestDelegate;
Kelvin Zhangec205cf2020-09-28 13:23:40 -040087
88 // Return true if we need to write verity bytes.
89 bool ShouldWriteVerity();
Alex Deymoe5e5fe92015-10-05 09:28:19 -070090 // Starts the hashing of the current partition. If there aren't any partitions
Sen Jiangfef85fd2016-03-25 15:32:49 -070091 // remaining to be hashed, it finishes the action.
Alex Deymoe5e5fe92015-10-05 09:28:19 -070092 void StartPartitionHashing();
93
Alex Deymob9e8e262015-08-03 20:23:03 -070094 // Schedules the asynchronous read of the filesystem.
95 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070096
Alex Deymob9e8e262015-08-03 20:23:03 -070097 // Called from the main loop when a single read from |src_stream_| succeeds or
98 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
Kelvin Zhangec205cf2020-09-28 13:23:40 -040099 void OnReadDone(size_t bytes_read);
Alex Deymob9e8e262015-08-03 20:23:03 -0700100
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700101 // When the read is done, finalize the hash checking of the current partition
102 // and continue checking the next one.
103 void FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700104
105 // Cleans up all the variables we use for async operations and tells the
106 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
107 // true if TerminateProcessing() was called.
108 void Cleanup(ErrorCode code);
109
Kelvin Zhang70eef232020-06-12 20:32:40 +0000110 // Invoke delegate callback to report progress, if delegate is not null
111 void UpdateProgress(double progress);
112
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400113 // Initialize read_fd_ and write_fd_
114 bool InitializeFd(const std::string& part_path);
115 bool InitializeFdVABC();
116
Allie Woodeb9e6d82015-04-17 13:55:30 -0700117 // The type of the partition that we are verifying.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700118 VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700119
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700120 // The index in the install_plan_.partitions vector of the partition currently
121 // being hashed.
122 size_t partition_index_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700123
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400124 // If not null, the FileDescriptor used to read from the device.
125 // |read_fd_| and |write_fd_| will be initialized when we begin hashing a
126 // partition. They will be deallocated once we encounter an error or
127 // successfully finished hashing.
128 FileDescriptorPtr read_fd_;
129 // If not null, the FileDescriptor used to write to the device.
130 // For VABC, this will be different from |read_fd_|. For other cases
131 // this can be the same as |read_fd_|.
132 FileDescriptorPtr write_fd_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700133
134 // Buffer for storing data we read.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700135 brillo::Blob buffer_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700136
Alex Deymo20c99202015-07-09 16:14:16 -0700137 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700138
Allie Woodeb9e6d82015-04-17 13:55:30 -0700139 // Calculates the hash of the data.
Alex Deymo39910dc2015-11-09 17:04:30 -0800140 std::unique_ptr<HashCalculator> hasher_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700141
Sen Jiang57f91802017-11-14 17:42:13 -0800142 // Write verity data of the current partition.
143 std::unique_ptr<VerityWriterInterface> verity_writer_;
144
Tianjie24f96092020-06-30 12:26:25 -0700145 // Verifies the untouched dynamic partitions for partial updates.
146 DynamicPartitionControlInterface* dynamic_control_{nullptr};
147
Sen Jiang57f91802017-11-14 17:42:13 -0800148 // Reads and hashes this many bytes from the head of the input stream. When
149 // the partition starts to be hashed, this field is initialized from the
150 // corresponding InstallPlan::Partition size which is the total size
151 // update_engine is expected to write, and may be smaller than the size of the
152 // partition in gpt.
153 uint64_t partition_size_{0};
154
155 // The byte offset that we are reading in the current partition.
156 uint64_t offset_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700157
Kelvin Zhang70eef232020-06-12 20:32:40 +0000158 // An observer that observes progress updates of this action.
159 FilesystemVerifyDelegate* delegate_{};
160
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400161 // Callback that should be cancelled on |TerminateProcessing|. Usually this
162 // points to pending read callbacks from async stream.
163 brillo::MessageLoop::TaskId pending_task_id_{
164 brillo::MessageLoop::kTaskIdNull};
165
Allie Woodeb9e6d82015-04-17 13:55:30 -0700166 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
167};
168
169} // namespace chromeos_update_engine
170
Alex Deymo39910dc2015-11-09 17:04:30 -0800171#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_