blob: df54f80715bbe2f761886df642b08c58bb2a5f98 [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
17#include "update_engine/filesystem_verifier_action.h"
18
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 Deymob9e8e262015-08-03 20:23:03 -070029#include <chromeos/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030
Alex Deymo763e7db2015-08-27 21:08:08 -070031#include "update_engine/boot_control_interface.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070032#include "update_engine/system_state.h"
33#include "update_engine/utils.h"
34
35using std::string;
36
37namespace chromeos_update_engine {
38
39namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070040const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070041} // namespace
42
Alex Deymo763e7db2015-08-27 21:08:08 -070043string PartitionTypeToString(const PartitionType partition_type) {
44 // TODO(deymo): The PartitionType class should be replaced with just the
45 // string name that comes from the payload. This function should be deleted
46 // then.
47 switch (partition_type) {
48 case PartitionType::kRootfs:
49 case PartitionType::kSourceRootfs:
50 return kLegacyPartitionNameRoot;
51 case PartitionType::kKernel:
52 case PartitionType::kSourceKernel:
53 return kLegacyPartitionNameKernel;
54 }
55 return "<unknown>";
56}
57
Allie Woodeb9e6d82015-04-17 13:55:30 -070058FilesystemVerifierAction::FilesystemVerifierAction(
59 SystemState* system_state,
60 PartitionType partition_type)
61 : partition_type_(partition_type),
Allie Woodeb9e6d82015-04-17 13:55:30 -070062 remaining_size_(kint64max),
63 system_state_(system_state) {}
64
65void FilesystemVerifierAction::PerformAction() {
66 // Will tell the ActionProcessor we've failed if we return.
67 ScopedActionCompleter abort_action_completer(processor_, this);
68
69 if (!HasInputObject()) {
70 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
71 return;
72 }
73 install_plan_ = GetInputObject();
74
Alex Deymo763e7db2015-08-27 21:08:08 -070075 // TODO(deymo): Remove this from the FileSystemVerifierAction.
Allie Woodeb9e6d82015-04-17 13:55:30 -070076 if (partition_type_ == PartitionType::kKernel) {
77 LOG(INFO) << "verifying kernel, marking as unbootable";
Alex Deymo763e7db2015-08-27 21:08:08 -070078 if (!system_state_->boot_control()->MarkSlotUnbootable(
79 install_plan_.target_slot)) {
Allie Woodeb9e6d82015-04-17 13:55:30 -070080 PLOG(ERROR) << "Unable to clear kernel GPT boot flags: " <<
81 install_plan_.kernel_install_path;
82 }
83 }
84
85 if (install_plan_.is_full_update &&
86 (partition_type_ == PartitionType::kSourceRootfs ||
87 partition_type_ == PartitionType::kSourceKernel)) {
88 // No hash verification needed. Done!
89 LOG(INFO) << "filesystem verifying skipped on full update.";
90 if (HasOutputPipe())
91 SetOutputObject(install_plan_);
92 abort_action_completer.set_code(ErrorCode::kSuccess);
93 return;
94 }
95
96 string target_path;
Alex Deymo763e7db2015-08-27 21:08:08 -070097 string partition_name = PartitionTypeToString(partition_type_);
Allie Woodeb9e6d82015-04-17 13:55:30 -070098 switch (partition_type_) {
99 case PartitionType::kRootfs:
100 target_path = install_plan_.install_path;
101 if (target_path.empty()) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700102 system_state_->boot_control()->GetPartitionDevice(
103 partition_name, install_plan_.target_slot, &target_path);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700104 }
105 break;
106 case PartitionType::kKernel:
107 target_path = install_plan_.kernel_install_path;
108 if (target_path.empty()) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700109 system_state_->boot_control()->GetPartitionDevice(
110 partition_name, install_plan_.target_slot, &target_path);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700111 }
112 break;
113 case PartitionType::kSourceRootfs:
Alex Deymo763e7db2015-08-27 21:08:08 -0700114 target_path = install_plan_.source_path;
115 if (target_path.empty()) {
116 system_state_->boot_control()->GetPartitionDevice(
117 partition_name, install_plan_.source_slot, &target_path);
118 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700119 break;
120 case PartitionType::kSourceKernel:
Alex Deymo763e7db2015-08-27 21:08:08 -0700121 target_path = install_plan_.kernel_source_path;
122 if (target_path.empty()) {
123 system_state_->boot_control()->GetPartitionDevice(
124 partition_name, install_plan_.source_slot, &target_path);
125 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700126 break;
127 }
128
Alex Deymob9e8e262015-08-03 20:23:03 -0700129 chromeos::ErrorPtr error;
130 src_stream_ = chromeos::FileStream::Open(
131 base::FilePath(target_path),
132 chromeos::Stream::AccessMode::READ,
133 chromeos::FileStream::Disposition::OPEN_EXISTING,
134 &error);
135
136 if (!src_stream_) {
137 LOG(ERROR) << "Unable to open " << target_path << " for reading";
Allie Woodeb9e6d82015-04-17 13:55:30 -0700138 return;
139 }
140
Alex Deymob9e8e262015-08-03 20:23:03 -0700141 DetermineFilesystemSize(target_path);
Alex Deymo20c99202015-07-09 16:14:16 -0700142 buffer_.resize(kReadFileBufferSize);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700143
144 // Start the first read.
Alex Deymob9e8e262015-08-03 20:23:03 -0700145 ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700146
147 abort_action_completer.set_should_complete(false);
148}
149
150void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700151 cancelled_ = true;
152 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700153}
154
155bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -0700156 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700157}
158
159void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700160 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700161 // This memory is not used anymore.
162 buffer_.clear();
163
Allie Woodeb9e6d82015-04-17 13:55:30 -0700164 if (cancelled_)
165 return;
166 if (code == ErrorCode::kSuccess && HasOutputPipe())
167 SetOutputObject(install_plan_);
168 processor_->ActionComplete(this, code);
169}
170
Alex Deymob9e8e262015-08-03 20:23:03 -0700171void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700172 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
173 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700174 if (!bytes_to_read) {
175 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700176 return;
177 }
178
Alex Deymob9e8e262015-08-03 20:23:03 -0700179 bool read_async_ok = src_stream_->ReadAsync(
180 buffer_.data(),
181 bytes_to_read,
182 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
183 base::Unretained(this)),
184 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
185 base::Unretained(this)),
186 nullptr);
187
188 if (!read_async_ok) {
189 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
190 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700191 }
192}
193
Alex Deymob9e8e262015-08-03 20:23:03 -0700194void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
195 if (bytes_read == 0) {
196 read_done_ = true;
197 } else {
198 remaining_size_ -= bytes_read;
199 CHECK(!read_done_);
200 if (!hasher_.Update(buffer_.data(), bytes_read)) {
201 LOG(ERROR) << "Unable to update the hash.";
202 Cleanup(ErrorCode::kError);
203 return;
204 }
205 }
206
207 // We either terminate the action or have more data to read.
208 if (!CheckTerminationConditions())
209 ScheduleRead();
210}
211
212void FilesystemVerifierAction::OnReadErrorCallback(
213 const chromeos::Error* error) {
214 // TODO(deymo): Transform the read-error into an specific ErrorCode.
215 LOG(ERROR) << "Asynchronous read failed.";
216 Cleanup(ErrorCode::kError);
217}
218
219bool FilesystemVerifierAction::CheckTerminationConditions() {
220 if (cancelled_) {
221 Cleanup(ErrorCode::kError);
222 return true;
223 }
224
225 if (!read_done_)
226 return false;
227
228 // We're done!
229 ErrorCode code = ErrorCode::kSuccess;
230 if (hasher_.Finalize()) {
231 LOG(INFO) << "Hash: " << hasher_.hash();
232 switch (partition_type_) {
233 case PartitionType::kRootfs:
234 if (install_plan_.rootfs_hash != hasher_.raw_hash()) {
235 code = ErrorCode::kNewRootfsVerificationError;
236 LOG(ERROR) << "New rootfs verification failed.";
237 }
238 break;
239 case PartitionType::kKernel:
240 if (install_plan_.kernel_hash != hasher_.raw_hash()) {
241 code = ErrorCode::kNewKernelVerificationError;
242 LOG(ERROR) << "New kernel verification failed.";
243 }
244 break;
245 case PartitionType::kSourceRootfs:
246 install_plan_.source_rootfs_hash = hasher_.raw_hash();
247 break;
248 case PartitionType::kSourceKernel:
249 install_plan_.source_kernel_hash = hasher_.raw_hash();
250 break;
251 }
252 } else {
253 LOG(ERROR) << "Unable to finalize the hash.";
254 code = ErrorCode::kError;
255 }
256 Cleanup(code);
257 return true;
258}
259
Alex Deymo763e7db2015-08-27 21:08:08 -0700260void FilesystemVerifierAction::DetermineFilesystemSize(const string& path) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700261 switch (partition_type_) {
262 case PartitionType::kRootfs:
263 remaining_size_ = install_plan_.rootfs_size;
264 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes.";
265 break;
266 case PartitionType::kKernel:
267 remaining_size_ = install_plan_.kernel_size;
268 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes.";
269 break;
270 case PartitionType::kSourceRootfs:
271 {
272 int block_count = 0, block_size = 0;
Alex Deymob9e8e262015-08-03 20:23:03 -0700273 if (utils::GetFilesystemSize(path, &block_count, &block_size)) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700274 remaining_size_ = static_cast<int64_t>(block_count) * block_size;
275 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes ("
276 << block_count << "x" << block_size << ").";
277 }
278 }
279 break;
280 default:
281 break;
282 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700283}
284
285} // namespace chromeos_update_engine