Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 | |
| 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 Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 28 | #include <base/bind.h> |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 29 | #include <chromeos/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 31 | #include "update_engine/boot_control_interface.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 32 | #include "update_engine/system_state.h" |
| 33 | #include "update_engine/utils.h" |
| 34 | |
| 35 | using std::string; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 40 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 41 | } // namespace |
| 42 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 43 | string 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 58 | FilesystemVerifierAction::FilesystemVerifierAction( |
| 59 | SystemState* system_state, |
| 60 | PartitionType partition_type) |
| 61 | : partition_type_(partition_type), |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 62 | remaining_size_(kint64max), |
| 63 | system_state_(system_state) {} |
| 64 | |
| 65 | void 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 Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 75 | // TODO(deymo): Remove this from the FileSystemVerifierAction. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 76 | if (partition_type_ == PartitionType::kKernel) { |
| 77 | LOG(INFO) << "verifying kernel, marking as unbootable"; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 78 | if (!system_state_->boot_control()->MarkSlotUnbootable( |
| 79 | install_plan_.target_slot)) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 80 | 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 Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 97 | string partition_name = PartitionTypeToString(partition_type_); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 98 | switch (partition_type_) { |
| 99 | case PartitionType::kRootfs: |
| 100 | target_path = install_plan_.install_path; |
| 101 | if (target_path.empty()) { |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 102 | system_state_->boot_control()->GetPartitionDevice( |
| 103 | partition_name, install_plan_.target_slot, &target_path); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 104 | } |
| 105 | break; |
| 106 | case PartitionType::kKernel: |
| 107 | target_path = install_plan_.kernel_install_path; |
| 108 | if (target_path.empty()) { |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 109 | system_state_->boot_control()->GetPartitionDevice( |
| 110 | partition_name, install_plan_.target_slot, &target_path); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 111 | } |
| 112 | break; |
| 113 | case PartitionType::kSourceRootfs: |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 114 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 119 | break; |
| 120 | case PartitionType::kSourceKernel: |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 121 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 129 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 141 | DetermineFilesystemSize(target_path); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 142 | buffer_.resize(kReadFileBufferSize); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 143 | |
| 144 | // Start the first read. |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 145 | ScheduleRead(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 146 | |
| 147 | abort_action_completer.set_should_complete(false); |
| 148 | } |
| 149 | |
| 150 | void FilesystemVerifierAction::TerminateProcessing() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 151 | cancelled_ = true; |
| 152 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool FilesystemVerifierAction::IsCleanupPending() const { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 156 | return src_stream_ != nullptr; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 160 | src_stream_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 161 | // This memory is not used anymore. |
| 162 | buffer_.clear(); |
| 163 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 164 | if (cancelled_) |
| 165 | return; |
| 166 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 167 | SetOutputObject(install_plan_); |
| 168 | processor_->ActionComplete(this, code); |
| 169 | } |
| 170 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 171 | void FilesystemVerifierAction::ScheduleRead() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 172 | size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()), |
| 173 | remaining_size_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 174 | if (!bytes_to_read) { |
| 175 | OnReadDoneCallback(0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 179 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 194 | void 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 | |
| 212 | void 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 | |
| 219 | bool 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 Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame^] | 260 | void FilesystemVerifierAction::DetermineFilesystemSize(const string& path) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 261 | 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 273 | if (utils::GetFilesystemSize(path, &block_count, &block_size)) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 274 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | } // namespace chromeos_update_engine |