blob: d28526fb75b0903536843e1d9fa81f9bcd430d0b [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"
Sen Jiang70a6ab02015-08-28 13:23:27 -070032#include "update_engine/payload_constants.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070033#include "update_engine/system_state.h"
34#include "update_engine/utils.h"
35
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
Alex Deymo763e7db2015-08-27 21:08:08 -070044string PartitionTypeToString(const PartitionType partition_type) {
45 // TODO(deymo): The PartitionType class should be replaced with just the
46 // string name that comes from the payload. This function should be deleted
47 // then.
48 switch (partition_type) {
49 case PartitionType::kRootfs:
50 case PartitionType::kSourceRootfs:
51 return kLegacyPartitionNameRoot;
52 case PartitionType::kKernel:
53 case PartitionType::kSourceKernel:
54 return kLegacyPartitionNameKernel;
55 }
56 return "<unknown>";
57}
58
Allie Woodeb9e6d82015-04-17 13:55:30 -070059FilesystemVerifierAction::FilesystemVerifierAction(
60 SystemState* system_state,
61 PartitionType partition_type)
62 : partition_type_(partition_type),
Allie Woodeb9e6d82015-04-17 13:55:30 -070063 remaining_size_(kint64max),
64 system_state_(system_state) {}
65
66void FilesystemVerifierAction::PerformAction() {
67 // Will tell the ActionProcessor we've failed if we return.
68 ScopedActionCompleter abort_action_completer(processor_, this);
69
70 if (!HasInputObject()) {
71 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
72 return;
73 }
74 install_plan_ = GetInputObject();
75
Alex Deymo763e7db2015-08-27 21:08:08 -070076 // TODO(deymo): Remove this from the FileSystemVerifierAction.
Allie Woodeb9e6d82015-04-17 13:55:30 -070077 if (partition_type_ == PartitionType::kKernel) {
78 LOG(INFO) << "verifying kernel, marking as unbootable";
Alex Deymo763e7db2015-08-27 21:08:08 -070079 if (!system_state_->boot_control()->MarkSlotUnbootable(
80 install_plan_.target_slot)) {
Allie Woodeb9e6d82015-04-17 13:55:30 -070081 PLOG(ERROR) << "Unable to clear kernel GPT boot flags: " <<
82 install_plan_.kernel_install_path;
83 }
84 }
85
86 if (install_plan_.is_full_update &&
87 (partition_type_ == PartitionType::kSourceRootfs ||
88 partition_type_ == PartitionType::kSourceKernel)) {
89 // No hash verification needed. Done!
90 LOG(INFO) << "filesystem verifying skipped on full update.";
91 if (HasOutputPipe())
92 SetOutputObject(install_plan_);
93 abort_action_completer.set_code(ErrorCode::kSuccess);
94 return;
95 }
96
97 string target_path;
Alex Deymo763e7db2015-08-27 21:08:08 -070098 string partition_name = PartitionTypeToString(partition_type_);
Allie Woodeb9e6d82015-04-17 13:55:30 -070099 switch (partition_type_) {
100 case PartitionType::kRootfs:
101 target_path = install_plan_.install_path;
102 if (target_path.empty()) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700103 system_state_->boot_control()->GetPartitionDevice(
104 partition_name, install_plan_.target_slot, &target_path);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700105 }
106 break;
107 case PartitionType::kKernel:
108 target_path = install_plan_.kernel_install_path;
109 if (target_path.empty()) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700110 system_state_->boot_control()->GetPartitionDevice(
111 partition_name, install_plan_.target_slot, &target_path);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700112 }
113 break;
114 case PartitionType::kSourceRootfs:
Alex Deymo763e7db2015-08-27 21:08:08 -0700115 target_path = install_plan_.source_path;
116 if (target_path.empty()) {
117 system_state_->boot_control()->GetPartitionDevice(
118 partition_name, install_plan_.source_slot, &target_path);
119 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700120 break;
121 case PartitionType::kSourceKernel:
Alex Deymo763e7db2015-08-27 21:08:08 -0700122 target_path = install_plan_.kernel_source_path;
123 if (target_path.empty()) {
124 system_state_->boot_control()->GetPartitionDevice(
125 partition_name, install_plan_.source_slot, &target_path);
126 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700127 break;
128 }
129
Alex Deymob9e8e262015-08-03 20:23:03 -0700130 chromeos::ErrorPtr error;
131 src_stream_ = chromeos::FileStream::Open(
132 base::FilePath(target_path),
133 chromeos::Stream::AccessMode::READ,
134 chromeos::FileStream::Disposition::OPEN_EXISTING,
135 &error);
136
137 if (!src_stream_) {
138 LOG(ERROR) << "Unable to open " << target_path << " for reading";
Allie Woodeb9e6d82015-04-17 13:55:30 -0700139 return;
140 }
141
Alex Deymob9e8e262015-08-03 20:23:03 -0700142 DetermineFilesystemSize(target_path);
Alex Deymo20c99202015-07-09 16:14:16 -0700143 buffer_.resize(kReadFileBufferSize);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700144
145 // Start the first read.
Alex Deymob9e8e262015-08-03 20:23:03 -0700146 ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700147
148 abort_action_completer.set_should_complete(false);
149}
150
151void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700152 cancelled_ = true;
153 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700154}
155
156bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -0700157 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700158}
159
160void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700161 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700162 // This memory is not used anymore.
163 buffer_.clear();
164
Allie Woodeb9e6d82015-04-17 13:55:30 -0700165 if (cancelled_)
166 return;
167 if (code == ErrorCode::kSuccess && HasOutputPipe())
168 SetOutputObject(install_plan_);
169 processor_->ActionComplete(this, code);
170}
171
Alex Deymob9e8e262015-08-03 20:23:03 -0700172void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700173 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
174 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700175 if (!bytes_to_read) {
176 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700177 return;
178 }
179
Alex Deymob9e8e262015-08-03 20:23:03 -0700180 bool read_async_ok = src_stream_->ReadAsync(
181 buffer_.data(),
182 bytes_to_read,
183 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
184 base::Unretained(this)),
185 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
186 base::Unretained(this)),
187 nullptr);
188
189 if (!read_async_ok) {
190 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
191 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700192 }
193}
194
Alex Deymob9e8e262015-08-03 20:23:03 -0700195void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
196 if (bytes_read == 0) {
197 read_done_ = true;
198 } else {
199 remaining_size_ -= bytes_read;
200 CHECK(!read_done_);
201 if (!hasher_.Update(buffer_.data(), bytes_read)) {
202 LOG(ERROR) << "Unable to update the hash.";
203 Cleanup(ErrorCode::kError);
204 return;
205 }
206 }
207
208 // We either terminate the action or have more data to read.
209 if (!CheckTerminationConditions())
210 ScheduleRead();
211}
212
213void FilesystemVerifierAction::OnReadErrorCallback(
214 const chromeos::Error* error) {
215 // TODO(deymo): Transform the read-error into an specific ErrorCode.
216 LOG(ERROR) << "Asynchronous read failed.";
217 Cleanup(ErrorCode::kError);
218}
219
220bool FilesystemVerifierAction::CheckTerminationConditions() {
221 if (cancelled_) {
222 Cleanup(ErrorCode::kError);
223 return true;
224 }
225
226 if (!read_done_)
227 return false;
228
229 // We're done!
230 ErrorCode code = ErrorCode::kSuccess;
231 if (hasher_.Finalize()) {
232 LOG(INFO) << "Hash: " << hasher_.hash();
233 switch (partition_type_) {
234 case PartitionType::kRootfs:
235 if (install_plan_.rootfs_hash != hasher_.raw_hash()) {
236 code = ErrorCode::kNewRootfsVerificationError;
237 LOG(ERROR) << "New rootfs verification failed.";
238 }
239 break;
240 case PartitionType::kKernel:
241 if (install_plan_.kernel_hash != hasher_.raw_hash()) {
242 code = ErrorCode::kNewKernelVerificationError;
243 LOG(ERROR) << "New kernel verification failed.";
244 }
245 break;
246 case PartitionType::kSourceRootfs:
247 install_plan_.source_rootfs_hash = hasher_.raw_hash();
248 break;
249 case PartitionType::kSourceKernel:
250 install_plan_.source_kernel_hash = hasher_.raw_hash();
251 break;
252 }
253 } else {
254 LOG(ERROR) << "Unable to finalize the hash.";
255 code = ErrorCode::kError;
256 }
257 Cleanup(code);
258 return true;
259}
260
Alex Deymo763e7db2015-08-27 21:08:08 -0700261void FilesystemVerifierAction::DetermineFilesystemSize(const string& path) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700262 switch (partition_type_) {
263 case PartitionType::kRootfs:
264 remaining_size_ = install_plan_.rootfs_size;
265 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes.";
266 break;
267 case PartitionType::kKernel:
268 remaining_size_ = install_plan_.kernel_size;
269 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes.";
270 break;
271 case PartitionType::kSourceRootfs:
272 {
273 int block_count = 0, block_size = 0;
Alex Deymob9e8e262015-08-03 20:23:03 -0700274 if (utils::GetFilesystemSize(path, &block_count, &block_size)) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700275 remaining_size_ = static_cast<int64_t>(block_count) * block_size;
276 LOG(INFO) << "Filesystem size: " << remaining_size_ << " bytes ("
277 << block_count << "x" << block_size << ").";
278 }
279 }
280 break;
281 default:
282 break;
283 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700284}
285
286} // namespace chromeos_update_engine