blob: 616f7b7ce73c1efe4bafeae47347d9f606cb45de [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
23#include <string>
24#include <vector>
25
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/streams/stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action.h"
29#include "update_engine/common/hash_calculator.h"
30#include "update_engine/payload_consumer/install_plan.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070031
Sen Jiangfef85fd2016-03-25 15:32:49 -070032// This action will hash all the partitions of the target slot involved in the
33// update. The hashes are then verified against the ones in the InstallPlan.
34// If the target hash does not match, the action will fail. In case of failure,
35// the error code will depend on whether the source slot hashes are provided and
36// match.
Allie Woodeb9e6d82015-04-17 13:55:30 -070037
38namespace chromeos_update_engine {
39
Sen Jiangfef85fd2016-03-25 15:32:49 -070040// The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash
41// on the target partitions based on the already populated size and verifies it
42// matches the one in the target_hash in the InstallPlan.
43// If the hash matches, then we skip the kVerifySourceHash step, otherwise we
44// need to check if the source is the root cause of the mismatch.
45enum class VerifierStep {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070046 kVerifyTargetHash,
Sen Jiang1ad42ad2015-11-17 15:04:02 -080047 kVerifySourceHash,
Allie Woodeb9e6d82015-04-17 13:55:30 -070048};
49
50class FilesystemVerifierAction : public InstallPlanAction {
51 public:
Sen Jiange6e4bb92016-04-05 14:59:12 -070052 FilesystemVerifierAction() = default;
Allie Woodeb9e6d82015-04-17 13:55:30 -070053
54 void PerformAction() override;
55 void TerminateProcessing() override;
56
57 // Used for testing. Return true if Cleanup() has not yet been called due
58 // to a callback upon the completion or cancellation of the verifier action.
59 // A test should wait until IsCleanupPending() returns false before
Alex Deymo0b3db6b2015-08-10 15:19:37 -070060 // terminating the main loop.
Allie Woodeb9e6d82015-04-17 13:55:30 -070061 bool IsCleanupPending() const;
62
63 // Debugging/logging
64 static std::string StaticType() { return "FilesystemVerifierAction"; }
65 std::string Type() const override { return StaticType(); }
66
67 private:
Alex Deymoe5e5fe92015-10-05 09:28:19 -070068 // Starts the hashing of the current partition. If there aren't any partitions
Sen Jiangfef85fd2016-03-25 15:32:49 -070069 // remaining to be hashed, it finishes the action.
Alex Deymoe5e5fe92015-10-05 09:28:19 -070070 void StartPartitionHashing();
71
Alex Deymob9e8e262015-08-03 20:23:03 -070072 // Schedules the asynchronous read of the filesystem.
73 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070074
Alex Deymob9e8e262015-08-03 20:23:03 -070075 // Called from the main loop when a single read from |src_stream_| succeeds or
76 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
77 void OnReadDoneCallback(size_t bytes_read);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070078 void OnReadErrorCallback(const brillo::Error* error);
Alex Deymob9e8e262015-08-03 20:23:03 -070079
Alex Deymoe5e5fe92015-10-05 09:28:19 -070080 // When the read is done, finalize the hash checking of the current partition
81 // and continue checking the next one.
82 void FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070083
84 // Cleans up all the variables we use for async operations and tells the
85 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
86 // true if TerminateProcessing() was called.
87 void Cleanup(ErrorCode code);
88
Allie Woodeb9e6d82015-04-17 13:55:30 -070089 // The type of the partition that we are verifying.
Sen Jiangfef85fd2016-03-25 15:32:49 -070090 VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070091
Alex Deymoe5e5fe92015-10-05 09:28:19 -070092 // The index in the install_plan_.partitions vector of the partition currently
93 // being hashed.
94 size_t partition_index_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -070095
Alex Deymob9e8e262015-08-03 20:23:03 -070096 // If not null, the FileStream used to read from the device.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070097 brillo::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -070098
99 // Buffer for storing data we read.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100 brillo::Blob buffer_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700101
Alex Deymo20c99202015-07-09 16:14:16 -0700102 bool read_done_{false}; // true if reached EOF on the input stream.
Alex Deymo20c99202015-07-09 16:14:16 -0700103 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700104
105 // The install plan we're passed in via the input pipe.
106 InstallPlan install_plan_;
107
108 // Calculates the hash of the data.
Alex Deymo39910dc2015-11-09 17:04:30 -0800109 std::unique_ptr<HashCalculator> hasher_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700110
111 // Reads and hashes this many bytes from the head of the input stream. This
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700112 // field is initialized from the corresponding InstallPlan::Partition size,
113 // when the partition starts to be hashed.
114 int64_t remaining_size_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700115
116 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
117};
118
119} // namespace chromeos_update_engine
120
Alex Deymo39910dc2015-11-09 17:04:30 -0800121#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_