blob: 6ef3d164d4994553d901d077ee8cfc9e14d1bdf9 [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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/streams/stream.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"
31#include "update_engine/payload_consumer/install_plan.h"
Sen Jiang57f91802017-11-14 17:42:13 -080032#include "update_engine/payload_consumer/verity_writer_interface.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070033
Sen Jiangfef85fd2016-03-25 15:32:49 -070034// This action will hash all the partitions of the target slot involved in the
35// update. The hashes are then verified against the ones in the InstallPlan.
36// If the target hash does not match, the action will fail. In case of failure,
37// the error code will depend on whether the source slot hashes are provided and
38// match.
Allie Woodeb9e6d82015-04-17 13:55:30 -070039
40namespace chromeos_update_engine {
41
Sen Jiangfef85fd2016-03-25 15:32:49 -070042// The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash
43// on the target partitions based on the already populated size and verifies it
44// matches the one in the target_hash in the InstallPlan.
45// If the hash matches, then we skip the kVerifySourceHash step, otherwise we
46// need to check if the source is the root cause of the mismatch.
47enum class VerifierStep {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070048 kVerifyTargetHash,
Sen Jiang1ad42ad2015-11-17 15:04:02 -080049 kVerifySourceHash,
Allie Woodeb9e6d82015-04-17 13:55:30 -070050};
51
Kelvin Zhang70eef232020-06-12 20:32:40 +000052class FilesystemVerifyDelegate {
53 public:
54 virtual ~FilesystemVerifyDelegate() = default;
55 virtual void OnVerifyProgressUpdate(double progress) = 0;
56};
57
Allie Woodeb9e6d82015-04-17 13:55:30 -070058class FilesystemVerifierAction : public InstallPlanAction {
59 public:
Sen Jiang57f91802017-11-14 17:42:13 -080060 FilesystemVerifierAction()
61 : verity_writer_(verity_writer::CreateVerityWriter()) {}
62 ~FilesystemVerifierAction() override = default;
Allie Woodeb9e6d82015-04-17 13:55:30 -070063
64 void PerformAction() override;
65 void TerminateProcessing() override;
66
Kelvin Zhang70eef232020-06-12 20:32:40 +000067 // Used for listening to progress updates
68 void set_delegate(FilesystemVerifyDelegate* delegate) {
69 this->delegate_ = delegate;
70 }
71 [[nodiscard]] FilesystemVerifyDelegate* get_delegate() const {
72 return this->delegate_;
73 }
74
Allie Woodeb9e6d82015-04-17 13:55:30 -070075 // Debugging/logging
76 static std::string StaticType() { return "FilesystemVerifierAction"; }
77 std::string Type() const override { return StaticType(); }
78
79 private:
Amin Hassaniabe4a772018-07-26 11:19:10 -070080 friend class FilesystemVerifierActionTestDelegate;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070081 // Starts the hashing of the current partition. If there aren't any partitions
Sen Jiangfef85fd2016-03-25 15:32:49 -070082 // remaining to be hashed, it finishes the action.
Alex Deymoe5e5fe92015-10-05 09:28:19 -070083 void StartPartitionHashing();
84
Alex Deymob9e8e262015-08-03 20:23:03 -070085 // Schedules the asynchronous read of the filesystem.
86 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070087
Alex Deymob9e8e262015-08-03 20:23:03 -070088 // Called from the main loop when a single read from |src_stream_| succeeds or
89 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
90 void OnReadDoneCallback(size_t bytes_read);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070091 void OnReadErrorCallback(const brillo::Error* error);
Alex Deymob9e8e262015-08-03 20:23:03 -070092
Alex Deymoe5e5fe92015-10-05 09:28:19 -070093 // When the read is done, finalize the hash checking of the current partition
94 // and continue checking the next one.
95 void FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070096
97 // Cleans up all the variables we use for async operations and tells the
98 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
99 // true if TerminateProcessing() was called.
100 void Cleanup(ErrorCode code);
101
Kelvin Zhang70eef232020-06-12 20:32:40 +0000102 // Invoke delegate callback to report progress, if delegate is not null
103 void UpdateProgress(double progress);
104
Allie Woodeb9e6d82015-04-17 13:55:30 -0700105 // The type of the partition that we are verifying.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700106 VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700107
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700108 // The index in the install_plan_.partitions vector of the partition currently
109 // being hashed.
110 size_t partition_index_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700111
Alex Deymob9e8e262015-08-03 20:23:03 -0700112 // If not null, the FileStream used to read from the device.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700113 brillo::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700114
115 // Buffer for storing data we read.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700116 brillo::Blob buffer_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700117
Alex Deymo20c99202015-07-09 16:14:16 -0700118 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700119
120 // The install plan we're passed in via the input pipe.
121 InstallPlan install_plan_;
122
123 // Calculates the hash of the data.
Alex Deymo39910dc2015-11-09 17:04:30 -0800124 std::unique_ptr<HashCalculator> hasher_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700125
Sen Jiang57f91802017-11-14 17:42:13 -0800126 // Write verity data of the current partition.
127 std::unique_ptr<VerityWriterInterface> verity_writer_;
128
129 // Reads and hashes this many bytes from the head of the input stream. When
130 // the partition starts to be hashed, this field is initialized from the
131 // corresponding InstallPlan::Partition size which is the total size
132 // update_engine is expected to write, and may be smaller than the size of the
133 // partition in gpt.
134 uint64_t partition_size_{0};
135
136 // The byte offset that we are reading in the current partition.
137 uint64_t offset_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700138
Kelvin Zhang70eef232020-06-12 20:32:40 +0000139 // An observer that observes progress updates of this action.
140 FilesystemVerifyDelegate* delegate_{};
141
Allie Woodeb9e6d82015-04-17 13:55:30 -0700142 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
143};
144
145} // namespace chromeos_update_engine
146
Alex Deymo39910dc2015-11-09 17:04:30 -0800147#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_