Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 19 | |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 23 | #include <memory> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 27 | #include <brillo/streams/stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 28 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/action.h" |
| 30 | #include "update_engine/common/hash_calculator.h" |
| 31 | #include "update_engine/payload_consumer/install_plan.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 32 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 33 | // This action will hash all the partitions of the target slot involved in the |
| 34 | // update. The hashes are then verified against the ones in the InstallPlan. |
| 35 | // If the target hash does not match, the action will fail. In case of failure, |
| 36 | // the error code will depend on whether the source slot hashes are provided and |
| 37 | // match. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 38 | |
| 39 | namespace chromeos_update_engine { |
| 40 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 41 | // The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash |
| 42 | // on the target partitions based on the already populated size and verifies it |
| 43 | // matches the one in the target_hash in the InstallPlan. |
| 44 | // If the hash matches, then we skip the kVerifySourceHash step, otherwise we |
| 45 | // need to check if the source is the root cause of the mismatch. |
| 46 | enum class VerifierStep { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 47 | kVerifyTargetHash, |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 48 | kVerifySourceHash, |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class FilesystemVerifierAction : public InstallPlanAction { |
| 52 | public: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 53 | FilesystemVerifierAction() = default; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 54 | |
| 55 | void PerformAction() override; |
| 56 | void TerminateProcessing() override; |
| 57 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 58 | // Debugging/logging |
| 59 | static std::string StaticType() { return "FilesystemVerifierAction"; } |
| 60 | std::string Type() const override { return StaticType(); } |
| 61 | |
| 62 | private: |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 63 | friend class FilesystemVerifierActionTestDelegate; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 64 | // Starts the hashing of the current partition. If there aren't any partitions |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 65 | // remaining to be hashed, it finishes the action. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 66 | void StartPartitionHashing(); |
| 67 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 68 | // Schedules the asynchronous read of the filesystem. |
| 69 | void ScheduleRead(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 70 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 71 | // Called from the main loop when a single read from |src_stream_| succeeds or |
| 72 | // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively. |
| 73 | void OnReadDoneCallback(size_t bytes_read); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 74 | void OnReadErrorCallback(const brillo::Error* error); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 75 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 76 | // When the read is done, finalize the hash checking of the current partition |
| 77 | // and continue checking the next one. |
| 78 | void FinishPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 79 | |
| 80 | // Cleans up all the variables we use for async operations and tells the |
| 81 | // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be |
| 82 | // true if TerminateProcessing() was called. |
| 83 | void Cleanup(ErrorCode code); |
| 84 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 85 | // The type of the partition that we are verifying. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 86 | VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 87 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 88 | // The index in the install_plan_.partitions vector of the partition currently |
| 89 | // being hashed. |
| 90 | size_t partition_index_{0}; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 91 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 92 | // If not null, the FileStream used to read from the device. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 93 | brillo::StreamPtr src_stream_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 94 | |
| 95 | // Buffer for storing data we read. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 96 | brillo::Blob buffer_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 97 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 98 | bool read_done_{false}; // true if reached EOF on the input stream. |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 99 | bool cancelled_{false}; // true if the action has been cancelled. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 100 | |
| 101 | // The install plan we're passed in via the input pipe. |
| 102 | InstallPlan install_plan_; |
| 103 | |
| 104 | // Calculates the hash of the data. |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 105 | std::unique_ptr<HashCalculator> hasher_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 106 | |
| 107 | // Reads and hashes this many bytes from the head of the input stream. This |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 108 | // field is initialized from the corresponding InstallPlan::Partition size, |
| 109 | // when the partition starts to be hashed. |
| 110 | int64_t remaining_size_{0}; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction); |
| 113 | }; |
| 114 | |
| 115 | } // namespace chromeos_update_engine |
| 116 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 117 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ |