blob: 8530d379dda4b9ee891bda188c70f152078badb7 [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 // For delta updates (major version 1) we need to populate the source
61 // partition hash if not pre-populated.
62 if (!install_plan_.is_full_update && install_plan_.partitions.empty() &&
Sen Jiang1ad42ad2015-11-17 15:04:02 -080063 verifier_mode_ == VerifierMode::kComputeSourceHash &&
64 DeltaPerformer::kSupportedMinorPayloadVersion <
65 kOpSrcHashMinorPayloadVersion) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070066 LOG(INFO) << "Using legacy partition names.";
67 InstallPlan::Partition part;
68 string part_path;
69
70 part.name = kLegacyPartitionNameRoot;
71 if (!boot_control_->GetPartitionDevice(
72 part.name, install_plan_.source_slot, &part_path))
73 return;
74 int block_count = 0, block_size = 0;
75 if (utils::GetFilesystemSize(part_path, &block_count, &block_size)) {
76 part.source_size = static_cast<int64_t>(block_count) * block_size;
77 LOG(INFO) << "Partition " << part.name << " size: " << part.source_size
78 << " bytes (" << block_count << "x" << block_size << ").";
79 }
80 install_plan_.partitions.push_back(part);
81
82 part.name = kLegacyPartitionNameKernel;
83 if (!boot_control_->GetPartitionDevice(
84 part.name, install_plan_.source_slot, &part_path))
85 return;
86 off_t kernel_part_size = utils::FileSize(part_path);
87 if (kernel_part_size < 0)
88 return;
89 LOG(INFO) << "Partition " << part.name << " size: " << kernel_part_size
90 << " bytes.";
91 part.source_size = kernel_part_size;
92 install_plan_.partitions.push_back(part);
93 }
94
95 if (install_plan_.partitions.empty()) {
96 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070097 if (HasOutputPipe())
98 SetOutputObject(install_plan_);
99 abort_action_completer.set_code(ErrorCode::kSuccess);
100 return;
101 }
102
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700103 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700104 abort_action_completer.set_should_complete(false);
105}
106
107void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700108 cancelled_ = true;
109 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700110}
111
112bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -0700113 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700114}
115
116void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700117 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700118 // This memory is not used anymore.
119 buffer_.clear();
120
Allie Woodeb9e6d82015-04-17 13:55:30 -0700121 if (cancelled_)
122 return;
123 if (code == ErrorCode::kSuccess && HasOutputPipe())
124 SetOutputObject(install_plan_);
125 processor_->ActionComplete(this, code);
126}
127
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128void FilesystemVerifierAction::StartPartitionHashing() {
129 if (partition_index_ == install_plan_.partitions.size()) {
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800130 // We never called this action with kVerifySourceHash directly, if we are in
131 // this mode, it means the target partition verification has failed, so we
132 // should set the error code to reflect the error in target.
133 if (verifier_mode_ == VerifierMode::kVerifySourceHash)
134 Cleanup(ErrorCode::kNewRootfsVerificationError);
135 else
136 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700137 return;
138 }
139 InstallPlan::Partition& partition =
140 install_plan_.partitions[partition_index_];
141
142 string part_path;
143 switch (verifier_mode_) {
144 case VerifierMode::kComputeSourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800145 case VerifierMode::kVerifySourceHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700146 boot_control_->GetPartitionDevice(
147 partition.name, install_plan_.source_slot, &part_path);
148 remaining_size_ = partition.source_size;
149 break;
150 case VerifierMode::kVerifyTargetHash:
151 boot_control_->GetPartitionDevice(
152 partition.name, install_plan_.target_slot, &part_path);
153 remaining_size_ = partition.target_size;
154 break;
155 }
156 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
157 << partition.name << ") on device " << part_path;
158 if (part_path.empty())
159 return Cleanup(ErrorCode::kFilesystemVerifierError);
160
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700161 brillo::ErrorPtr error;
162 src_stream_ = brillo::FileStream::Open(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700163 base::FilePath(part_path),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700164 brillo::Stream::AccessMode::READ,
165 brillo::FileStream::Disposition::OPEN_EXISTING,
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700166 &error);
167
168 if (!src_stream_) {
169 LOG(ERROR) << "Unable to open " << part_path << " for reading";
170 return Cleanup(ErrorCode::kFilesystemVerifierError);
171 }
172
173 buffer_.resize(kReadFileBufferSize);
174 read_done_ = false;
Alex Deymo39910dc2015-11-09 17:04:30 -0800175 hasher_.reset(new HashCalculator());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700176
177 // Start the first read.
178 ScheduleRead();
179}
180
Alex Deymob9e8e262015-08-03 20:23:03 -0700181void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700182 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
183 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700184 if (!bytes_to_read) {
185 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700186 return;
187 }
188
Alex Deymob9e8e262015-08-03 20:23:03 -0700189 bool read_async_ok = src_stream_->ReadAsync(
190 buffer_.data(),
191 bytes_to_read,
192 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
193 base::Unretained(this)),
194 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
195 base::Unretained(this)),
196 nullptr);
197
198 if (!read_async_ok) {
199 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
200 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700201 }
202}
203
Alex Deymob9e8e262015-08-03 20:23:03 -0700204void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
205 if (bytes_read == 0) {
206 read_done_ = true;
207 } else {
208 remaining_size_ -= bytes_read;
209 CHECK(!read_done_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700210 if (!hasher_->Update(buffer_.data(), bytes_read)) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700211 LOG(ERROR) << "Unable to update the hash.";
212 Cleanup(ErrorCode::kError);
213 return;
214 }
215 }
216
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700217 // We either terminate the current partition or have more data to read.
218 if (cancelled_)
219 return Cleanup(ErrorCode::kError);
220
221 if (read_done_ || remaining_size_ == 0) {
222 if (remaining_size_ != 0) {
223 LOG(ERROR) << "Failed to read the remaining " << remaining_size_
224 << " bytes from partition "
225 << install_plan_.partitions[partition_index_].name;
226 return Cleanup(ErrorCode::kFilesystemVerifierError);
227 }
228 return FinishPartitionHashing();
229 }
230 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700231}
232
233void FilesystemVerifierAction::OnReadErrorCallback(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700234 const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700235 // TODO(deymo): Transform the read-error into an specific ErrorCode.
236 LOG(ERROR) << "Asynchronous read failed.";
237 Cleanup(ErrorCode::kError);
238}
239
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700240void FilesystemVerifierAction::FinishPartitionHashing() {
241 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700242 LOG(ERROR) << "Unable to finalize the hash.";
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700243 return Cleanup(ErrorCode::kError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700244 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700245 InstallPlan::Partition& partition =
246 install_plan_.partitions[partition_index_];
247 LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash();
Alex Deymob9e8e262015-08-03 20:23:03 -0700248
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700249 switch (verifier_mode_) {
250 case VerifierMode::kComputeSourceHash:
251 partition.source_hash = hasher_->raw_hash();
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800252 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700253 break;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700254 case VerifierMode::kVerifyTargetHash:
255 if (partition.target_hash != hasher_->raw_hash()) {
256 LOG(ERROR) << "New '" << partition.name
257 << "' partition verification failed.";
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800258 if (DeltaPerformer::kSupportedMinorPayloadVersion <
259 kOpSrcHashMinorPayloadVersion)
260 return Cleanup(ErrorCode::kNewRootfsVerificationError);
261 // If we support per-operation source hash, then we skipped source
262 // filesystem verification, now that the target partition does not
263 // match, we need to switch to kVerifySourceHash mode to check if it's
264 // because the source partition does not match either.
265 verifier_mode_ = VerifierMode::kVerifySourceHash;
266 partition_index_ = 0;
267 } else {
268 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700269 }
270 break;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800271 case VerifierMode::kVerifySourceHash:
272 if (partition.source_hash != hasher_->raw_hash()) {
273 LOG(ERROR) << "Old '" << partition.name
274 << "' partition verification failed.";
275 return Cleanup(ErrorCode::kDownloadStateInitializationError);
276 }
277 partition_index_++;
278 break;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700279 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700280 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700281 hasher_.reset();
282 buffer_.clear();
283 src_stream_->CloseBlocking(nullptr);
284 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700285}
286
287} // namespace chromeos_update_engine