blob: 4d7d3dc147b1b54c2f47b04315b2dfd1affcb4c3 [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#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070018
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <algorithm>
25#include <cstdlib>
26#include <string>
27
Alex Deymo20c99202015-07-09 16:14:16 -070028#include <base/bind.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/boot_control_interface.h"
32#include "update_engine/common/utils.h"
Sen Jiang1ad42ad2015-11-17 15:04:02 -080033#include "update_engine/payload_consumer/delta_performer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/payload_consumer/payload_constants.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070035
36using std::string;
37
38namespace chromeos_update_engine {
39
40namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070041const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070042} // namespace
43
44FilesystemVerifierAction::FilesystemVerifierAction(
Alex Deymoe5e5fe92015-10-05 09:28:19 -070045 const BootControlInterface* boot_control,
46 VerifierMode verifier_mode)
47 : verifier_mode_(verifier_mode),
48 boot_control_(boot_control) {}
Allie Woodeb9e6d82015-04-17 13:55:30 -070049
50void FilesystemVerifierAction::PerformAction() {
51 // Will tell the ActionProcessor we've failed if we return.
52 ScopedActionCompleter abort_action_completer(processor_, this);
53
54 if (!HasInputObject()) {
55 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
56 return;
57 }
58 install_plan_ = GetInputObject();
59
Alex Deymoe5e5fe92015-10-05 09:28:19 -070060 if (install_plan_.partitions.empty()) {
61 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070062 if (HasOutputPipe())
63 SetOutputObject(install_plan_);
64 abort_action_completer.set_code(ErrorCode::kSuccess);
65 return;
66 }
67
Alex Deymoe5e5fe92015-10-05 09:28:19 -070068 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070069 abort_action_completer.set_should_complete(false);
70}
71
72void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -070073 cancelled_ = true;
74 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -070075}
76
77bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -070078 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -070079}
80
81void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -070082 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -070083 // This memory is not used anymore.
84 buffer_.clear();
85
Allie Woodeb9e6d82015-04-17 13:55:30 -070086 if (cancelled_)
87 return;
88 if (code == ErrorCode::kSuccess && HasOutputPipe())
89 SetOutputObject(install_plan_);
90 processor_->ActionComplete(this, code);
91}
92
Alex Deymoe5e5fe92015-10-05 09:28:19 -070093void FilesystemVerifierAction::StartPartitionHashing() {
94 if (partition_index_ == install_plan_.partitions.size()) {
Sen Jiang1ad42ad2015-11-17 15:04:02 -080095 // We never called this action with kVerifySourceHash directly, if we are in
96 // this mode, it means the target partition verification has failed, so we
97 // should set the error code to reflect the error in target.
98 if (verifier_mode_ == VerifierMode::kVerifySourceHash)
99 Cleanup(ErrorCode::kNewRootfsVerificationError);
100 else
101 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700102 return;
103 }
104 InstallPlan::Partition& partition =
105 install_plan_.partitions[partition_index_];
106
107 string part_path;
108 switch (verifier_mode_) {
109 case VerifierMode::kComputeSourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800110 case VerifierMode::kVerifySourceHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700111 boot_control_->GetPartitionDevice(
112 partition.name, install_plan_.source_slot, &part_path);
113 remaining_size_ = partition.source_size;
114 break;
115 case VerifierMode::kVerifyTargetHash:
116 boot_control_->GetPartitionDevice(
117 partition.name, install_plan_.target_slot, &part_path);
118 remaining_size_ = partition.target_size;
119 break;
120 }
121 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
122 << partition.name << ") on device " << part_path;
123 if (part_path.empty())
124 return Cleanup(ErrorCode::kFilesystemVerifierError);
125
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700126 brillo::ErrorPtr error;
127 src_stream_ = brillo::FileStream::Open(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128 base::FilePath(part_path),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700129 brillo::Stream::AccessMode::READ,
130 brillo::FileStream::Disposition::OPEN_EXISTING,
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131 &error);
132
133 if (!src_stream_) {
134 LOG(ERROR) << "Unable to open " << part_path << " for reading";
135 return Cleanup(ErrorCode::kFilesystemVerifierError);
136 }
137
138 buffer_.resize(kReadFileBufferSize);
139 read_done_ = false;
Alex Deymo39910dc2015-11-09 17:04:30 -0800140 hasher_.reset(new HashCalculator());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700141
142 // Start the first read.
143 ScheduleRead();
144}
145
Alex Deymob9e8e262015-08-03 20:23:03 -0700146void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700147 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
148 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700149 if (!bytes_to_read) {
150 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700151 return;
152 }
153
Alex Deymob9e8e262015-08-03 20:23:03 -0700154 bool read_async_ok = src_stream_->ReadAsync(
155 buffer_.data(),
156 bytes_to_read,
157 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
158 base::Unretained(this)),
159 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
160 base::Unretained(this)),
161 nullptr);
162
163 if (!read_async_ok) {
164 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
165 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700166 }
167}
168
Alex Deymob9e8e262015-08-03 20:23:03 -0700169void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
170 if (bytes_read == 0) {
171 read_done_ = true;
172 } else {
173 remaining_size_ -= bytes_read;
174 CHECK(!read_done_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700175 if (!hasher_->Update(buffer_.data(), bytes_read)) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700176 LOG(ERROR) << "Unable to update the hash.";
177 Cleanup(ErrorCode::kError);
178 return;
179 }
180 }
181
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700182 // We either terminate the current partition or have more data to read.
183 if (cancelled_)
184 return Cleanup(ErrorCode::kError);
185
186 if (read_done_ || remaining_size_ == 0) {
187 if (remaining_size_ != 0) {
188 LOG(ERROR) << "Failed to read the remaining " << remaining_size_
189 << " bytes from partition "
190 << install_plan_.partitions[partition_index_].name;
191 return Cleanup(ErrorCode::kFilesystemVerifierError);
192 }
193 return FinishPartitionHashing();
194 }
195 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700196}
197
198void FilesystemVerifierAction::OnReadErrorCallback(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700199 const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700200 // TODO(deymo): Transform the read-error into an specific ErrorCode.
201 LOG(ERROR) << "Asynchronous read failed.";
202 Cleanup(ErrorCode::kError);
203}
204
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700205void FilesystemVerifierAction::FinishPartitionHashing() {
206 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700207 LOG(ERROR) << "Unable to finalize the hash.";
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700208 return Cleanup(ErrorCode::kError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700209 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700210 InstallPlan::Partition& partition =
211 install_plan_.partitions[partition_index_];
212 LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash();
Alex Deymob9e8e262015-08-03 20:23:03 -0700213
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700214 switch (verifier_mode_) {
215 case VerifierMode::kComputeSourceHash:
216 partition.source_hash = hasher_->raw_hash();
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800217 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700218 break;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700219 case VerifierMode::kVerifyTargetHash:
220 if (partition.target_hash != hasher_->raw_hash()) {
221 LOG(ERROR) << "New '" << partition.name
222 << "' partition verification failed.";
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800223 if (DeltaPerformer::kSupportedMinorPayloadVersion <
224 kOpSrcHashMinorPayloadVersion)
225 return Cleanup(ErrorCode::kNewRootfsVerificationError);
226 // If we support per-operation source hash, then we skipped source
227 // filesystem verification, now that the target partition does not
228 // match, we need to switch to kVerifySourceHash mode to check if it's
229 // because the source partition does not match either.
230 verifier_mode_ = VerifierMode::kVerifySourceHash;
231 partition_index_ = 0;
232 } else {
233 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700234 }
235 break;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800236 case VerifierMode::kVerifySourceHash:
237 if (partition.source_hash != hasher_->raw_hash()) {
238 LOG(ERROR) << "Old '" << partition.name
239 << "' partition verification failed.";
240 return Cleanup(ErrorCode::kDownloadStateInitializationError);
241 }
242 partition_index_++;
243 break;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700244 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700245 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700246 hasher_.reset();
247 buffer_.clear();
248 src_stream_->CloseBlocking(nullptr);
249 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700250}
251
252} // namespace chromeos_update_engine