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 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 | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 31 | #include "update_engine/boot_control_interface.h" |
Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 32 | #include "update_engine/payload_constants.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 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 | |
| 43 | FilesystemVerifierAction::FilesystemVerifierAction( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 44 | const BootControlInterface* boot_control, |
| 45 | VerifierMode verifier_mode) |
| 46 | : verifier_mode_(verifier_mode), |
| 47 | boot_control_(boot_control) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 48 | |
| 49 | void FilesystemVerifierAction::PerformAction() { |
| 50 | // Will tell the ActionProcessor we've failed if we return. |
| 51 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 52 | |
| 53 | if (!HasInputObject()) { |
| 54 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 55 | return; |
| 56 | } |
| 57 | install_plan_ = GetInputObject(); |
| 58 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 59 | // For delta updates (major version 1) we need to populate the source |
| 60 | // partition hash if not pre-populated. |
| 61 | if (!install_plan_.is_full_update && install_plan_.partitions.empty() && |
| 62 | verifier_mode_ == VerifierMode::kComputeSourceHash) { |
| 63 | LOG(INFO) << "Using legacy partition names."; |
| 64 | InstallPlan::Partition part; |
| 65 | string part_path; |
| 66 | |
| 67 | part.name = kLegacyPartitionNameRoot; |
| 68 | if (!boot_control_->GetPartitionDevice( |
| 69 | part.name, install_plan_.source_slot, &part_path)) |
| 70 | return; |
| 71 | int block_count = 0, block_size = 0; |
| 72 | if (utils::GetFilesystemSize(part_path, &block_count, &block_size)) { |
| 73 | part.source_size = static_cast<int64_t>(block_count) * block_size; |
| 74 | LOG(INFO) << "Partition " << part.name << " size: " << part.source_size |
| 75 | << " bytes (" << block_count << "x" << block_size << ")."; |
| 76 | } |
| 77 | install_plan_.partitions.push_back(part); |
| 78 | |
| 79 | part.name = kLegacyPartitionNameKernel; |
| 80 | if (!boot_control_->GetPartitionDevice( |
| 81 | part.name, install_plan_.source_slot, &part_path)) |
| 82 | return; |
| 83 | off_t kernel_part_size = utils::FileSize(part_path); |
| 84 | if (kernel_part_size < 0) |
| 85 | return; |
| 86 | LOG(INFO) << "Partition " << part.name << " size: " << kernel_part_size |
| 87 | << " bytes."; |
| 88 | part.source_size = kernel_part_size; |
| 89 | install_plan_.partitions.push_back(part); |
| 90 | } |
| 91 | |
| 92 | if (install_plan_.partitions.empty()) { |
| 93 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 94 | if (HasOutputPipe()) |
| 95 | SetOutputObject(install_plan_); |
| 96 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 97 | return; |
| 98 | } |
| 99 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 100 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 101 | abort_action_completer.set_should_complete(false); |
| 102 | } |
| 103 | |
| 104 | void FilesystemVerifierAction::TerminateProcessing() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 105 | cancelled_ = true; |
| 106 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool FilesystemVerifierAction::IsCleanupPending() const { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 110 | return src_stream_ != nullptr; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 114 | src_stream_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 115 | // This memory is not used anymore. |
| 116 | buffer_.clear(); |
| 117 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 118 | if (cancelled_) |
| 119 | return; |
| 120 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 121 | SetOutputObject(install_plan_); |
| 122 | processor_->ActionComplete(this, code); |
| 123 | } |
| 124 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 125 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 126 | if (partition_index_ == install_plan_.partitions.size()) { |
| 127 | Cleanup(ErrorCode::kSuccess); |
| 128 | return; |
| 129 | } |
| 130 | InstallPlan::Partition& partition = |
| 131 | install_plan_.partitions[partition_index_]; |
| 132 | |
| 133 | string part_path; |
| 134 | switch (verifier_mode_) { |
| 135 | case VerifierMode::kComputeSourceHash: |
| 136 | boot_control_->GetPartitionDevice( |
| 137 | partition.name, install_plan_.source_slot, &part_path); |
| 138 | remaining_size_ = partition.source_size; |
| 139 | break; |
| 140 | case VerifierMode::kVerifyTargetHash: |
| 141 | boot_control_->GetPartitionDevice( |
| 142 | partition.name, install_plan_.target_slot, &part_path); |
| 143 | remaining_size_ = partition.target_size; |
| 144 | break; |
| 145 | } |
| 146 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 147 | << partition.name << ") on device " << part_path; |
| 148 | if (part_path.empty()) |
| 149 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 150 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 151 | brillo::ErrorPtr error; |
| 152 | src_stream_ = brillo::FileStream::Open( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 153 | base::FilePath(part_path), |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 154 | brillo::Stream::AccessMode::READ, |
| 155 | brillo::FileStream::Disposition::OPEN_EXISTING, |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 156 | &error); |
| 157 | |
| 158 | if (!src_stream_) { |
| 159 | LOG(ERROR) << "Unable to open " << part_path << " for reading"; |
| 160 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 161 | } |
| 162 | |
| 163 | buffer_.resize(kReadFileBufferSize); |
| 164 | read_done_ = false; |
| 165 | hasher_.reset(new OmahaHashCalculator()); |
| 166 | |
| 167 | // Start the first read. |
| 168 | ScheduleRead(); |
| 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_); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 200 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 201 | LOG(ERROR) << "Unable to update the hash."; |
| 202 | Cleanup(ErrorCode::kError); |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 207 | // We either terminate the current partition or have more data to read. |
| 208 | if (cancelled_) |
| 209 | return Cleanup(ErrorCode::kError); |
| 210 | |
| 211 | if (read_done_ || remaining_size_ == 0) { |
| 212 | if (remaining_size_ != 0) { |
| 213 | LOG(ERROR) << "Failed to read the remaining " << remaining_size_ |
| 214 | << " bytes from partition " |
| 215 | << install_plan_.partitions[partition_index_].name; |
| 216 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 217 | } |
| 218 | return FinishPartitionHashing(); |
| 219 | } |
| 220 | ScheduleRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void FilesystemVerifierAction::OnReadErrorCallback( |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 224 | const brillo::Error* error) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 225 | // TODO(deymo): Transform the read-error into an specific ErrorCode. |
| 226 | LOG(ERROR) << "Asynchronous read failed."; |
| 227 | Cleanup(ErrorCode::kError); |
| 228 | } |
| 229 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 230 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 231 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 232 | LOG(ERROR) << "Unable to finalize the hash."; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 233 | return Cleanup(ErrorCode::kError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 234 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 235 | InstallPlan::Partition& partition = |
| 236 | install_plan_.partitions[partition_index_]; |
| 237 | LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 238 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 239 | switch (verifier_mode_) { |
| 240 | case VerifierMode::kComputeSourceHash: |
| 241 | partition.source_hash = hasher_->raw_hash(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 242 | break; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 243 | case VerifierMode::kVerifyTargetHash: |
| 244 | if (partition.target_hash != hasher_->raw_hash()) { |
| 245 | LOG(ERROR) << "New '" << partition.name |
| 246 | << "' partition verification failed."; |
| 247 | return Cleanup(ErrorCode::kNewRootfsVerificationError); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 248 | } |
| 249 | break; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 250 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 251 | // Start hashing the next partition, if any. |
| 252 | partition_index_++; |
| 253 | hasher_.reset(); |
| 254 | buffer_.clear(); |
| 255 | src_stream_->CloseBlocking(nullptr); |
| 256 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | } // namespace chromeos_update_engine |