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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/boot_control_interface.h" |
| 32 | #include "update_engine/common/utils.h" |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 33 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/payload_constants.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 35 | |
| 36 | using std::string; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 41 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | FilesystemVerifierAction::FilesystemVerifierAction( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 45 | const BootControlInterface* boot_control, |
| 46 | VerifierMode verifier_mode) |
| 47 | : verifier_mode_(verifier_mode), |
| 48 | boot_control_(boot_control) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 49 | |
| 50 | void 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 Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 60 | // 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 Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 63 | verifier_mode_ == VerifierMode::kComputeSourceHash && |
| 64 | DeltaPerformer::kSupportedMinorPayloadVersion < |
| 65 | kOpSrcHashMinorPayloadVersion) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 66 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 97 | if (HasOutputPipe()) |
| 98 | SetOutputObject(install_plan_); |
| 99 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 100 | return; |
| 101 | } |
| 102 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 103 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 104 | abort_action_completer.set_should_complete(false); |
| 105 | } |
| 106 | |
| 107 | void FilesystemVerifierAction::TerminateProcessing() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 108 | cancelled_ = true; |
| 109 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool FilesystemVerifierAction::IsCleanupPending() const { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 113 | return src_stream_ != nullptr; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 117 | src_stream_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 118 | // This memory is not used anymore. |
| 119 | buffer_.clear(); |
| 120 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 121 | if (cancelled_) |
| 122 | return; |
| 123 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 124 | SetOutputObject(install_plan_); |
| 125 | processor_->ActionComplete(this, code); |
| 126 | } |
| 127 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 128 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 129 | if (partition_index_ == install_plan_.partitions.size()) { |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 130 | // 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 Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 137 | 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 Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 145 | case VerifierMode::kVerifySourceHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 146 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 161 | brillo::ErrorPtr error; |
| 162 | src_stream_ = brillo::FileStream::Open( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 163 | base::FilePath(part_path), |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 164 | brillo::Stream::AccessMode::READ, |
| 165 | brillo::FileStream::Disposition::OPEN_EXISTING, |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 166 | &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 Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 175 | hasher_.reset(new HashCalculator()); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 176 | |
| 177 | // Start the first read. |
| 178 | ScheduleRead(); |
| 179 | } |
| 180 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 181 | void FilesystemVerifierAction::ScheduleRead() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 182 | size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()), |
| 183 | remaining_size_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 184 | if (!bytes_to_read) { |
| 185 | OnReadDoneCallback(0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 189 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 204 | void 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 Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 210 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 211 | LOG(ERROR) << "Unable to update the hash."; |
| 212 | Cleanup(ErrorCode::kError); |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 217 | // 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void FilesystemVerifierAction::OnReadErrorCallback( |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 234 | const brillo::Error* error) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 235 | // TODO(deymo): Transform the read-error into an specific ErrorCode. |
| 236 | LOG(ERROR) << "Asynchronous read failed."; |
| 237 | Cleanup(ErrorCode::kError); |
| 238 | } |
| 239 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 240 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 241 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 242 | LOG(ERROR) << "Unable to finalize the hash."; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 243 | return Cleanup(ErrorCode::kError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 244 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 245 | InstallPlan::Partition& partition = |
| 246 | install_plan_.partitions[partition_index_]; |
| 247 | LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 248 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 249 | switch (verifier_mode_) { |
| 250 | case VerifierMode::kComputeSourceHash: |
| 251 | partition.source_hash = hasher_->raw_hash(); |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 252 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 253 | break; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 254 | case VerifierMode::kVerifyTargetHash: |
| 255 | if (partition.target_hash != hasher_->raw_hash()) { |
| 256 | LOG(ERROR) << "New '" << partition.name |
| 257 | << "' partition verification failed."; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 258 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 269 | } |
| 270 | break; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame^] | 271 | 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 279 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 280 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 281 | hasher_.reset(); |
| 282 | buffer_.clear(); |
| 283 | src_stream_->CloseBlocking(nullptr); |
| 284 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | } // namespace chromeos_update_engine |