blob: 759b45526410596eca9d08ebec42ac15d6dc5038 [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.
Alex Deymo64d98782016-02-05 18:03:48 -080062 if (install_plan_.payload_type == InstallPayloadType::kDelta &&
63 install_plan_.partitions.empty() &&
Sen Jiang1ad42ad2015-11-17 15:04:02 -080064 verifier_mode_ == VerifierMode::kComputeSourceHash &&
65 DeltaPerformer::kSupportedMinorPayloadVersion <
66 kOpSrcHashMinorPayloadVersion) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070067 LOG(INFO) << "Using legacy partition names.";
68 InstallPlan::Partition part;
69 string part_path;
70
71 part.name = kLegacyPartitionNameRoot;
72 if (!boot_control_->GetPartitionDevice(
73 part.name, install_plan_.source_slot, &part_path))
74 return;
75 int block_count = 0, block_size = 0;
76 if (utils::GetFilesystemSize(part_path, &block_count, &block_size)) {
77 part.source_size = static_cast<int64_t>(block_count) * block_size;
78 LOG(INFO) << "Partition " << part.name << " size: " << part.source_size
79 << " bytes (" << block_count << "x" << block_size << ").";
80 }
81 install_plan_.partitions.push_back(part);
82
83 part.name = kLegacyPartitionNameKernel;
84 if (!boot_control_->GetPartitionDevice(
85 part.name, install_plan_.source_slot, &part_path))
86 return;
87 off_t kernel_part_size = utils::FileSize(part_path);
88 if (kernel_part_size < 0)
89 return;
90 LOG(INFO) << "Partition " << part.name << " size: " << kernel_part_size
91 << " bytes.";
92 part.source_size = kernel_part_size;
93 install_plan_.partitions.push_back(part);
94 }
95
96 if (install_plan_.partitions.empty()) {
97 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070098 if (HasOutputPipe())
99 SetOutputObject(install_plan_);
100 abort_action_completer.set_code(ErrorCode::kSuccess);
101 return;
102 }
103
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700104 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700105 abort_action_completer.set_should_complete(false);
106}
107
108void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700109 cancelled_ = true;
110 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700111}
112
113bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -0700114 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700115}
116
117void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700118 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700119 // This memory is not used anymore.
120 buffer_.clear();
121
Allie Woodeb9e6d82015-04-17 13:55:30 -0700122 if (cancelled_)
123 return;
124 if (code == ErrorCode::kSuccess && HasOutputPipe())
125 SetOutputObject(install_plan_);
126 processor_->ActionComplete(this, code);
127}
128
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700129void FilesystemVerifierAction::StartPartitionHashing() {
130 if (partition_index_ == install_plan_.partitions.size()) {
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800131 // We never called this action with kVerifySourceHash directly, if we are in
132 // this mode, it means the target partition verification has failed, so we
133 // should set the error code to reflect the error in target.
134 if (verifier_mode_ == VerifierMode::kVerifySourceHash)
135 Cleanup(ErrorCode::kNewRootfsVerificationError);
136 else
137 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138 return;
139 }
140 InstallPlan::Partition& partition =
141 install_plan_.partitions[partition_index_];
142
143 string part_path;
144 switch (verifier_mode_) {
145 case VerifierMode::kComputeSourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800146 case VerifierMode::kVerifySourceHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700147 boot_control_->GetPartitionDevice(
148 partition.name, install_plan_.source_slot, &part_path);
149 remaining_size_ = partition.source_size;
150 break;
151 case VerifierMode::kVerifyTargetHash:
152 boot_control_->GetPartitionDevice(
153 partition.name, install_plan_.target_slot, &part_path);
154 remaining_size_ = partition.target_size;
155 break;
156 }
157 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
158 << partition.name << ") on device " << part_path;
159 if (part_path.empty())
160 return Cleanup(ErrorCode::kFilesystemVerifierError);
161
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700162 brillo::ErrorPtr error;
163 src_stream_ = brillo::FileStream::Open(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700164 base::FilePath(part_path),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700165 brillo::Stream::AccessMode::READ,
166 brillo::FileStream::Disposition::OPEN_EXISTING,
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700167 &error);
168
169 if (!src_stream_) {
170 LOG(ERROR) << "Unable to open " << part_path << " for reading";
171 return Cleanup(ErrorCode::kFilesystemVerifierError);
172 }
173
174 buffer_.resize(kReadFileBufferSize);
175 read_done_ = false;
Alex Deymo39910dc2015-11-09 17:04:30 -0800176 hasher_.reset(new HashCalculator());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700177
178 // Start the first read.
179 ScheduleRead();
180}
181
Alex Deymob9e8e262015-08-03 20:23:03 -0700182void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700183 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
184 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700185 if (!bytes_to_read) {
186 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700187 return;
188 }
189
Alex Deymob9e8e262015-08-03 20:23:03 -0700190 bool read_async_ok = src_stream_->ReadAsync(
191 buffer_.data(),
192 bytes_to_read,
193 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
194 base::Unretained(this)),
195 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
196 base::Unretained(this)),
197 nullptr);
198
199 if (!read_async_ok) {
200 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
201 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700202 }
203}
204
Alex Deymob9e8e262015-08-03 20:23:03 -0700205void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
206 if (bytes_read == 0) {
207 read_done_ = true;
208 } else {
209 remaining_size_ -= bytes_read;
210 CHECK(!read_done_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700211 if (!hasher_->Update(buffer_.data(), bytes_read)) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700212 LOG(ERROR) << "Unable to update the hash.";
213 Cleanup(ErrorCode::kError);
214 return;
215 }
216 }
217
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700218 // We either terminate the current partition or have more data to read.
219 if (cancelled_)
220 return Cleanup(ErrorCode::kError);
221
222 if (read_done_ || remaining_size_ == 0) {
223 if (remaining_size_ != 0) {
224 LOG(ERROR) << "Failed to read the remaining " << remaining_size_
225 << " bytes from partition "
226 << install_plan_.partitions[partition_index_].name;
227 return Cleanup(ErrorCode::kFilesystemVerifierError);
228 }
229 return FinishPartitionHashing();
230 }
231 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700232}
233
234void FilesystemVerifierAction::OnReadErrorCallback(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700235 const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700236 // TODO(deymo): Transform the read-error into an specific ErrorCode.
237 LOG(ERROR) << "Asynchronous read failed.";
238 Cleanup(ErrorCode::kError);
239}
240
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700241void FilesystemVerifierAction::FinishPartitionHashing() {
242 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700243 LOG(ERROR) << "Unable to finalize the hash.";
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700244 return Cleanup(ErrorCode::kError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700245 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700246 InstallPlan::Partition& partition =
247 install_plan_.partitions[partition_index_];
248 LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash();
Alex Deymob9e8e262015-08-03 20:23:03 -0700249
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700250 switch (verifier_mode_) {
251 case VerifierMode::kComputeSourceHash:
252 partition.source_hash = hasher_->raw_hash();
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800253 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700254 break;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700255 case VerifierMode::kVerifyTargetHash:
256 if (partition.target_hash != hasher_->raw_hash()) {
257 LOG(ERROR) << "New '" << partition.name
258 << "' partition verification failed.";
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800259 if (DeltaPerformer::kSupportedMinorPayloadVersion <
260 kOpSrcHashMinorPayloadVersion)
261 return Cleanup(ErrorCode::kNewRootfsVerificationError);
262 // If we support per-operation source hash, then we skipped source
263 // filesystem verification, now that the target partition does not
264 // match, we need to switch to kVerifySourceHash mode to check if it's
265 // because the source partition does not match either.
266 verifier_mode_ = VerifierMode::kVerifySourceHash;
267 partition_index_ = 0;
268 } else {
269 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700270 }
271 break;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800272 case VerifierMode::kVerifySourceHash:
273 if (partition.source_hash != hasher_->raw_hash()) {
274 LOG(ERROR) << "Old '" << partition.name
275 << "' partition verification failed.";
276 return Cleanup(ErrorCode::kDownloadStateInitializationError);
277 }
278 partition_index_++;
279 break;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700280 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700281 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700282 hasher_.reset();
283 buffer_.clear();
284 src_stream_->CloseBlocking(nullptr);
285 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700286}
287
288} // namespace chromeos_update_engine