| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2020 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 | // | 
|  | 16 | #include <update_engine/payload_consumer/partition_writer.h> | 
|  | 17 |  | 
|  | 18 | #include <fcntl.h> | 
|  | 19 | #include <linux/fs.h> | 
|  | 20 |  | 
|  | 21 | #include <algorithm> | 
|  | 22 | #include <initializer_list> | 
|  | 23 | #include <memory> | 
|  | 24 | #include <utility> | 
|  | 25 | #include <vector> | 
|  | 26 |  | 
|  | 27 | #include <base/strings/string_number_conversions.h> | 
|  | 28 | #include <bsdiff/bspatch.h> | 
|  | 29 | #include <puffin/puffpatch.h> | 
|  | 30 | #include <bsdiff/file_interface.h> | 
|  | 31 | #include <puffin/stream.h> | 
|  | 32 |  | 
|  | 33 | #include "update_engine/common/terminator.h" | 
|  | 34 | #include "update_engine/common/utils.h" | 
|  | 35 | #include "update_engine/payload_consumer/bzip_extent_writer.h" | 
|  | 36 | #include "update_engine/payload_consumer/cached_file_descriptor.h" | 
|  | 37 | #include "update_engine/payload_consumer/extent_reader.h" | 
|  | 38 | #include "update_engine/payload_consumer/extent_writer.h" | 
|  | 39 | #include "update_engine/payload_consumer/fec_file_descriptor.h" | 
|  | 40 | #include "update_engine/payload_consumer/file_descriptor_utils.h" | 
|  | 41 | #include "update_engine/payload_consumer/install_plan.h" | 
|  | 42 | #include "update_engine/payload_consumer/mount_history.h" | 
|  | 43 | #include "update_engine/payload_consumer/payload_constants.h" | 
|  | 44 | #include "update_engine/payload_consumer/xz_extent_writer.h" | 
|  | 45 |  | 
|  | 46 | namespace chromeos_update_engine { | 
|  | 47 |  | 
|  | 48 | namespace { | 
|  | 49 | constexpr uint64_t kCacheSize = 1024 * 1024;  // 1MB | 
|  | 50 |  | 
|  | 51 | // Discard the tail of the block device referenced by |fd|, from the offset | 
|  | 52 | // |data_size| until the end of the block device. Returns whether the data was | 
|  | 53 | // discarded. | 
|  | 54 |  | 
|  | 55 | bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) { | 
|  | 56 | uint64_t part_size = fd->BlockDevSize(); | 
|  | 57 | if (!part_size || part_size <= data_size) | 
|  | 58 | return false; | 
|  | 59 |  | 
|  | 60 | struct blkioctl_request { | 
|  | 61 | int number; | 
|  | 62 | const char* name; | 
|  | 63 | }; | 
|  | 64 | const std::initializer_list<blkioctl_request> blkioctl_requests = { | 
|  | 65 | {BLKDISCARD, "BLKDISCARD"}, | 
|  | 66 | {BLKSECDISCARD, "BLKSECDISCARD"}, | 
|  | 67 | #ifdef BLKZEROOUT | 
|  | 68 | {BLKZEROOUT, "BLKZEROOUT"}, | 
|  | 69 | #endif | 
|  | 70 | }; | 
|  | 71 | for (const auto& req : blkioctl_requests) { | 
|  | 72 | int error = 0; | 
|  | 73 | if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) && | 
|  | 74 | error == 0) { | 
|  | 75 | return true; | 
|  | 76 | } | 
|  | 77 | LOG(WARNING) << "Error discarding the last " | 
|  | 78 | << (part_size - data_size) / 1024 << " KiB using ioctl(" | 
|  | 79 | << req.name << ")"; | 
|  | 80 | } | 
|  | 81 | return false; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | }  // namespace | 
|  | 85 |  | 
|  | 86 | // Opens path for read/write. On success returns an open FileDescriptor | 
|  | 87 | // and sets *err to 0. On failure, sets *err to errno and returns nullptr. | 
|  | 88 | FileDescriptorPtr OpenFile(const char* path, | 
|  | 89 | int mode, | 
|  | 90 | bool cache_writes, | 
|  | 91 | int* err) { | 
|  | 92 | // Try to mark the block device read-only based on the mode. Ignore any | 
|  | 93 | // failure since this won't work when passing regular files. | 
|  | 94 | bool read_only = (mode & O_ACCMODE) == O_RDONLY; | 
|  | 95 | utils::SetBlockDeviceReadOnly(path, read_only); | 
|  | 96 |  | 
|  | 97 | FileDescriptorPtr fd(new EintrSafeFileDescriptor()); | 
|  | 98 | if (cache_writes && !read_only) { | 
|  | 99 | fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize)); | 
|  | 100 | LOG(INFO) << "Caching writes."; | 
|  | 101 | } | 
|  | 102 | if (!fd->Open(path, mode, 000)) { | 
|  | 103 | *err = errno; | 
|  | 104 | PLOG(ERROR) << "Unable to open file " << path; | 
|  | 105 | return nullptr; | 
|  | 106 | } | 
|  | 107 | *err = 0; | 
|  | 108 | return fd; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | class BsdiffExtentFile : public bsdiff::FileInterface { | 
|  | 112 | public: | 
|  | 113 | BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size) | 
|  | 114 | : BsdiffExtentFile(std::move(reader), nullptr, size) {} | 
|  | 115 | BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size) | 
|  | 116 | : BsdiffExtentFile(nullptr, std::move(writer), size) {} | 
|  | 117 |  | 
|  | 118 | ~BsdiffExtentFile() override = default; | 
|  | 119 |  | 
|  | 120 | bool Read(void* buf, size_t count, size_t* bytes_read) override { | 
|  | 121 | TEST_AND_RETURN_FALSE(reader_->Read(buf, count)); | 
|  | 122 | *bytes_read = count; | 
|  | 123 | offset_ += count; | 
|  | 124 | return true; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | bool Write(const void* buf, size_t count, size_t* bytes_written) override { | 
|  | 128 | TEST_AND_RETURN_FALSE(writer_->Write(buf, count)); | 
|  | 129 | *bytes_written = count; | 
|  | 130 | offset_ += count; | 
|  | 131 | return true; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | bool Seek(off_t pos) override { | 
|  | 135 | if (reader_ != nullptr) { | 
|  | 136 | TEST_AND_RETURN_FALSE(reader_->Seek(pos)); | 
|  | 137 | offset_ = pos; | 
|  | 138 | } else { | 
|  | 139 | // For writes technically there should be no change of position, or it | 
|  | 140 | // should be equivalent of current offset. | 
|  | 141 | TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos)); | 
|  | 142 | } | 
|  | 143 | return true; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | bool Close() override { return true; } | 
|  | 147 |  | 
|  | 148 | bool GetSize(uint64_t* size) override { | 
|  | 149 | *size = size_; | 
|  | 150 | return true; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | private: | 
|  | 154 | BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, | 
|  | 155 | std::unique_ptr<ExtentWriter> writer, | 
|  | 156 | size_t size) | 
|  | 157 | : reader_(std::move(reader)), | 
|  | 158 | writer_(std::move(writer)), | 
|  | 159 | size_(size), | 
|  | 160 | offset_(0) {} | 
|  | 161 |  | 
|  | 162 | std::unique_ptr<ExtentReader> reader_; | 
|  | 163 | std::unique_ptr<ExtentWriter> writer_; | 
|  | 164 | uint64_t size_; | 
|  | 165 | uint64_t offset_; | 
|  | 166 |  | 
|  | 167 | DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile); | 
|  | 168 | }; | 
|  | 169 | // A class to be passed to |puffpatch| for reading from |source_fd_| and writing | 
|  | 170 | // into |target_fd_|. | 
|  | 171 | class PuffinExtentStream : public puffin::StreamInterface { | 
|  | 172 | public: | 
|  | 173 | // Constructor for creating a stream for reading from an |ExtentReader|. | 
|  | 174 | PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size) | 
|  | 175 | : PuffinExtentStream(std::move(reader), nullptr, size) {} | 
|  | 176 |  | 
|  | 177 | // Constructor for creating a stream for writing to an |ExtentWriter|. | 
|  | 178 | PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size) | 
|  | 179 | : PuffinExtentStream(nullptr, std::move(writer), size) {} | 
|  | 180 |  | 
|  | 181 | ~PuffinExtentStream() override = default; | 
|  | 182 |  | 
|  | 183 | bool GetSize(uint64_t* size) const override { | 
|  | 184 | *size = size_; | 
|  | 185 | return true; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | bool GetOffset(uint64_t* offset) const override { | 
|  | 189 | *offset = offset_; | 
|  | 190 | return true; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | bool Seek(uint64_t offset) override { | 
|  | 194 | if (is_read_) { | 
|  | 195 | TEST_AND_RETURN_FALSE(reader_->Seek(offset)); | 
|  | 196 | offset_ = offset; | 
|  | 197 | } else { | 
|  | 198 | // For writes technically there should be no change of position, or it | 
|  | 199 | // should equivalent of current offset. | 
|  | 200 | TEST_AND_RETURN_FALSE(offset_ == offset); | 
|  | 201 | } | 
|  | 202 | return true; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | bool Read(void* buffer, size_t count) override { | 
|  | 206 | TEST_AND_RETURN_FALSE(is_read_); | 
|  | 207 | TEST_AND_RETURN_FALSE(reader_->Read(buffer, count)); | 
|  | 208 | offset_ += count; | 
|  | 209 | return true; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | bool Write(const void* buffer, size_t count) override { | 
|  | 213 | TEST_AND_RETURN_FALSE(!is_read_); | 
|  | 214 | TEST_AND_RETURN_FALSE(writer_->Write(buffer, count)); | 
|  | 215 | offset_ += count; | 
|  | 216 | return true; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | bool Close() override { return true; } | 
|  | 220 |  | 
|  | 221 | private: | 
|  | 222 | PuffinExtentStream(std::unique_ptr<ExtentReader> reader, | 
|  | 223 | std::unique_ptr<ExtentWriter> writer, | 
|  | 224 | uint64_t size) | 
|  | 225 | : reader_(std::move(reader)), | 
|  | 226 | writer_(std::move(writer)), | 
|  | 227 | size_(size), | 
|  | 228 | offset_(0), | 
|  | 229 | is_read_(reader_ ? true : false) {} | 
|  | 230 |  | 
|  | 231 | std::unique_ptr<ExtentReader> reader_; | 
|  | 232 | std::unique_ptr<ExtentWriter> writer_; | 
|  | 233 | uint64_t size_; | 
|  | 234 | uint64_t offset_; | 
|  | 235 | bool is_read_; | 
|  | 236 |  | 
|  | 237 | DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream); | 
|  | 238 | }; | 
|  | 239 |  | 
|  | 240 | PartitionWriter::PartitionWriter( | 
|  | 241 | const PartitionUpdate& partition_update, | 
|  | 242 | const InstallPlan::Partition& install_part, | 
|  | 243 | DynamicPartitionControlInterface* dynamic_control, | 
|  | 244 | size_t block_size, | 
|  | 245 | bool is_interactive) | 
|  | 246 | : partition_update_(partition_update), | 
|  | 247 | install_part_(install_part), | 
|  | 248 | dynamic_control_(dynamic_control), | 
|  | 249 | interactive_(is_interactive), | 
| Kelvin Zhang | 59928f1 | 2020-11-11 21:21:27 +0000 | [diff] [blame] | 250 | block_size_(block_size) {} | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 251 |  | 
|  | 252 | PartitionWriter::~PartitionWriter() { | 
|  | 253 | Close(); | 
|  | 254 | } | 
|  | 255 |  | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 256 | bool PartitionWriter::OpenSourcePartition(uint32_t source_slot, | 
|  | 257 | bool source_may_exist) { | 
|  | 258 | source_path_.clear(); | 
|  | 259 | if (!source_may_exist) { | 
|  | 260 | return true; | 
|  | 261 | } | 
|  | 262 | if (install_part_.source_size > 0 && !install_part_.source_path.empty()) { | 
|  | 263 | source_path_ = install_part_.source_path; | 
|  | 264 | int err; | 
|  | 265 | source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err); | 
|  | 266 | if (source_fd_ == nullptr) { | 
|  | 267 | LOG(ERROR) << "Unable to open source partition " << install_part_.name | 
|  | 268 | << " on slot " << BootControlInterface::SlotName(source_slot) | 
|  | 269 | << ", file " << source_path_; | 
|  | 270 | return false; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 | return true; | 
|  | 274 | } | 
|  | 275 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 276 | bool PartitionWriter::Init(const InstallPlan* install_plan, | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 277 | bool source_may_exist, | 
|  | 278 | size_t next_op_index) { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 279 | const PartitionUpdate& partition = partition_update_; | 
|  | 280 | uint32_t source_slot = install_plan->source_slot; | 
|  | 281 | uint32_t target_slot = install_plan->target_slot; | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 282 | TEST_AND_RETURN_FALSE(OpenSourcePartition(source_slot, source_may_exist)); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 283 |  | 
|  | 284 | // We shouldn't open the source partition in certain cases, e.g. some dynamic | 
|  | 285 | // partitions in delta payload, partitions included in the full payload for | 
|  | 286 | // partial updates. Use the source size as the indicator. | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 287 |  | 
|  | 288 | target_path_ = install_part_.target_path; | 
|  | 289 | int err; | 
|  | 290 |  | 
|  | 291 | int flags = O_RDWR; | 
|  | 292 | if (!interactive_) | 
|  | 293 | flags |= O_DSYNC; | 
|  | 294 |  | 
|  | 295 | LOG(INFO) << "Opening " << target_path_ << " partition with" | 
|  | 296 | << (interactive_ ? "out" : "") << " O_DSYNC"; | 
|  | 297 |  | 
|  | 298 | target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err); | 
|  | 299 | if (!target_fd_) { | 
|  | 300 | LOG(ERROR) << "Unable to open target partition " | 
|  | 301 | << partition.partition_name() << " on slot " | 
|  | 302 | << BootControlInterface::SlotName(target_slot) << ", file " | 
|  | 303 | << target_path_; | 
|  | 304 | return false; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | LOG(INFO) << "Applying " << partition.operations().size() | 
|  | 308 | << " operations to partition \"" << partition.partition_name() | 
|  | 309 | << "\""; | 
|  | 310 |  | 
|  | 311 | // Discard the end of the partition, but ignore failures. | 
|  | 312 | DiscardPartitionTail(target_fd_, install_part_.target_size); | 
|  | 313 |  | 
|  | 314 | return true; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | bool PartitionWriter::PerformReplaceOperation(const InstallOperation& operation, | 
|  | 318 | const void* data, | 
|  | 319 | size_t count) { | 
|  | 320 | // Setup the ExtentWriter stack based on the operation type. | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 321 | std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter(); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 322 |  | 
|  | 323 | if (operation.type() == InstallOperation::REPLACE_BZ) { | 
|  | 324 | writer.reset(new BzipExtentWriter(std::move(writer))); | 
|  | 325 | } else if (operation.type() == InstallOperation::REPLACE_XZ) { | 
|  | 326 | writer.reset(new XzExtentWriter(std::move(writer))); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | TEST_AND_RETURN_FALSE( | 
|  | 330 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); | 
|  | 331 | TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length())); | 
|  | 332 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 333 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 334 | } | 
|  | 335 |  | 
|  | 336 | bool PartitionWriter::PerformZeroOrDiscardOperation( | 
|  | 337 | const InstallOperation& operation) { | 
|  | 338 | #ifdef BLKZEROOUT | 
|  | 339 | bool attempt_ioctl = true; | 
|  | 340 | int request = | 
|  | 341 | (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD); | 
|  | 342 | #else   // !defined(BLKZEROOUT) | 
|  | 343 | bool attempt_ioctl = false; | 
|  | 344 | int request = 0; | 
|  | 345 | #endif  // !defined(BLKZEROOUT) | 
|  | 346 |  | 
|  | 347 | brillo::Blob zeros; | 
|  | 348 | for (const Extent& extent : operation.dst_extents()) { | 
|  | 349 | const uint64_t start = extent.start_block() * block_size_; | 
|  | 350 | const uint64_t length = extent.num_blocks() * block_size_; | 
|  | 351 | if (attempt_ioctl) { | 
|  | 352 | int result = 0; | 
|  | 353 | if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0) | 
|  | 354 | continue; | 
|  | 355 | attempt_ioctl = false; | 
|  | 356 | } | 
|  | 357 | // In case of failure, we fall back to writing 0 to the selected region. | 
|  | 358 | zeros.resize(16 * block_size_); | 
|  | 359 | for (uint64_t offset = 0; offset < length; offset += zeros.size()) { | 
|  | 360 | uint64_t chunk_length = | 
|  | 361 | std::min(length - offset, static_cast<uint64_t>(zeros.size())); | 
| Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 362 | TEST_AND_RETURN_FALSE(utils::WriteAll( | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 363 | target_fd_, zeros.data(), chunk_length, start + offset)); | 
|  | 364 | } | 
|  | 365 | } | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 366 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
|  | 369 | bool PartitionWriter::PerformSourceCopyOperation( | 
|  | 370 | const InstallOperation& operation, ErrorCode* error) { | 
|  | 371 | TEST_AND_RETURN_FALSE(source_fd_ != nullptr); | 
|  | 372 |  | 
|  | 373 | // The device may optimize the SOURCE_COPY operation. | 
|  | 374 | // Being this a device-specific optimization let DynamicPartitionController | 
|  | 375 | // decide it the operation should be skipped. | 
|  | 376 | const PartitionUpdate& partition = partition_update_; | 
|  | 377 | const auto& partition_control = dynamic_control_; | 
|  | 378 |  | 
|  | 379 | InstallOperation buf; | 
|  | 380 | bool should_optimize = partition_control->OptimizeOperation( | 
|  | 381 | partition.partition_name(), operation, &buf); | 
|  | 382 | const InstallOperation& optimized = should_optimize ? buf : operation; | 
|  | 383 |  | 
|  | 384 | if (operation.has_src_sha256_hash()) { | 
|  | 385 | bool read_ok; | 
|  | 386 | brillo::Blob source_hash; | 
|  | 387 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), | 
|  | 388 | operation.src_sha256_hash().end()); | 
|  | 389 |  | 
|  | 390 | // We fall back to use the error corrected device if the hash of the raw | 
|  | 391 | // device doesn't match or there was an error reading the source partition. | 
|  | 392 | // Note that this code will also fall back if writing the target partition | 
|  | 393 | // fails. | 
|  | 394 | if (should_optimize) { | 
|  | 395 | // Hash operation.src_extents(), then copy optimized.src_extents to | 
|  | 396 | // optimized.dst_extents. | 
|  | 397 | read_ok = | 
|  | 398 | fd_utils::ReadAndHashExtents( | 
|  | 399 | source_fd_, operation.src_extents(), block_size_, &source_hash) && | 
|  | 400 | fd_utils::CopyAndHashExtents(source_fd_, | 
|  | 401 | optimized.src_extents(), | 
|  | 402 | target_fd_, | 
|  | 403 | optimized.dst_extents(), | 
|  | 404 | block_size_, | 
|  | 405 | nullptr /* skip hashing */); | 
|  | 406 | } else { | 
|  | 407 | read_ok = fd_utils::CopyAndHashExtents(source_fd_, | 
|  | 408 | operation.src_extents(), | 
|  | 409 | target_fd_, | 
|  | 410 | operation.dst_extents(), | 
|  | 411 | block_size_, | 
|  | 412 | &source_hash); | 
|  | 413 | } | 
|  | 414 | if (read_ok && expected_source_hash == source_hash) | 
|  | 415 | return true; | 
|  | 416 | LOG(WARNING) << "Source hash from RAW device mismatched, attempting to " | 
|  | 417 | "correct using ECC"; | 
|  | 418 | if (!OpenCurrentECCPartition()) { | 
|  | 419 | // The following function call will return false since the source hash | 
|  | 420 | // mismatches, but we still want to call it so it prints the appropriate | 
|  | 421 | // log message. | 
|  | 422 | return ValidateSourceHash(source_hash, operation, source_fd_, error); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | LOG(WARNING) << "Source hash from RAW device mismatched: found " | 
|  | 426 | << base::HexEncode(source_hash.data(), source_hash.size()) | 
|  | 427 | << ", expected " | 
|  | 428 | << base::HexEncode(expected_source_hash.data(), | 
|  | 429 | expected_source_hash.size()); | 
|  | 430 | if (should_optimize) { | 
|  | 431 | TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents( | 
|  | 432 | source_ecc_fd_, operation.src_extents(), block_size_, &source_hash)); | 
|  | 433 | TEST_AND_RETURN_FALSE( | 
|  | 434 | fd_utils::CopyAndHashExtents(source_ecc_fd_, | 
|  | 435 | optimized.src_extents(), | 
|  | 436 | target_fd_, | 
|  | 437 | optimized.dst_extents(), | 
|  | 438 | block_size_, | 
|  | 439 | nullptr /* skip hashing */)); | 
|  | 440 | } else { | 
|  | 441 | TEST_AND_RETURN_FALSE( | 
|  | 442 | fd_utils::CopyAndHashExtents(source_ecc_fd_, | 
|  | 443 | operation.src_extents(), | 
|  | 444 | target_fd_, | 
|  | 445 | operation.dst_extents(), | 
|  | 446 | block_size_, | 
|  | 447 | &source_hash)); | 
|  | 448 | } | 
|  | 449 | TEST_AND_RETURN_FALSE( | 
|  | 450 | ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)); | 
|  | 451 | // At this point reading from the error corrected device worked, but | 
|  | 452 | // reading from the raw device failed, so this is considered a recovered | 
|  | 453 | // failure. | 
|  | 454 | source_ecc_recovered_failures_++; | 
|  | 455 | } else { | 
|  | 456 | // When the operation doesn't include a source hash, we attempt the error | 
|  | 457 | // corrected device first since we can't verify the block in the raw device | 
|  | 458 | // at this point, but we fall back to the raw device since the error | 
|  | 459 | // corrected device can be shorter or not available. | 
|  | 460 |  | 
|  | 461 | if (OpenCurrentECCPartition() && | 
|  | 462 | fd_utils::CopyAndHashExtents(source_ecc_fd_, | 
|  | 463 | optimized.src_extents(), | 
|  | 464 | target_fd_, | 
|  | 465 | optimized.dst_extents(), | 
|  | 466 | block_size_, | 
|  | 467 | nullptr)) { | 
|  | 468 | return true; | 
|  | 469 | } | 
|  | 470 | TEST_AND_RETURN_FALSE(fd_utils::CopyAndHashExtents(source_fd_, | 
|  | 471 | optimized.src_extents(), | 
|  | 472 | target_fd_, | 
|  | 473 | optimized.dst_extents(), | 
|  | 474 | block_size_, | 
|  | 475 | nullptr)); | 
|  | 476 | } | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 477 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 478 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 479 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 480 | bool PartitionWriter::PerformSourceBsdiffOperation( | 
|  | 481 | const InstallOperation& operation, | 
|  | 482 | ErrorCode* error, | 
|  | 483 | const void* data, | 
|  | 484 | size_t count) { | 
|  | 485 | FileDescriptorPtr source_fd = ChooseSourceFD(operation, error); | 
|  | 486 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
|  | 487 |  | 
|  | 488 | auto reader = std::make_unique<DirectExtentReader>(); | 
|  | 489 | TEST_AND_RETURN_FALSE( | 
|  | 490 | reader->Init(source_fd, operation.src_extents(), block_size_)); | 
|  | 491 | auto src_file = std::make_unique<BsdiffExtentFile>( | 
|  | 492 | std::move(reader), | 
|  | 493 | utils::BlocksInExtents(operation.src_extents()) * block_size_); | 
|  | 494 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 495 | auto writer = CreateBaseExtentWriter(); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 496 | TEST_AND_RETURN_FALSE( | 
|  | 497 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); | 
|  | 498 | auto dst_file = std::make_unique<BsdiffExtentFile>( | 
|  | 499 | std::move(writer), | 
|  | 500 | utils::BlocksInExtents(operation.dst_extents()) * block_size_); | 
|  | 501 |  | 
|  | 502 | TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file), | 
|  | 503 | std::move(dst_file), | 
|  | 504 | reinterpret_cast<const uint8_t*>(data), | 
|  | 505 | count) == 0); | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 506 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 507 | } | 
|  | 508 |  | 
|  | 509 | bool PartitionWriter::PerformPuffDiffOperation( | 
|  | 510 | const InstallOperation& operation, | 
|  | 511 | ErrorCode* error, | 
|  | 512 | const void* data, | 
|  | 513 | size_t count) { | 
|  | 514 | FileDescriptorPtr source_fd = ChooseSourceFD(operation, error); | 
|  | 515 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
|  | 516 |  | 
|  | 517 | auto reader = std::make_unique<DirectExtentReader>(); | 
|  | 518 | TEST_AND_RETURN_FALSE( | 
|  | 519 | reader->Init(source_fd, operation.src_extents(), block_size_)); | 
|  | 520 | puffin::UniqueStreamPtr src_stream(new PuffinExtentStream( | 
|  | 521 | std::move(reader), | 
|  | 522 | utils::BlocksInExtents(operation.src_extents()) * block_size_)); | 
|  | 523 |  | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 524 | auto writer = CreateBaseExtentWriter(); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 525 | TEST_AND_RETURN_FALSE( | 
|  | 526 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); | 
|  | 527 | puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream( | 
|  | 528 | std::move(writer), | 
|  | 529 | utils::BlocksInExtents(operation.dst_extents()) * block_size_)); | 
|  | 530 |  | 
|  | 531 | constexpr size_t kMaxCacheSize = 5 * 1024 * 1024;  // Total 5MB cache. | 
|  | 532 | TEST_AND_RETURN_FALSE( | 
|  | 533 | puffin::PuffPatch(std::move(src_stream), | 
|  | 534 | std::move(dst_stream), | 
|  | 535 | reinterpret_cast<const uint8_t*>(data), | 
|  | 536 | count, | 
|  | 537 | kMaxCacheSize)); | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 538 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 539 | } | 
|  | 540 |  | 
|  | 541 | FileDescriptorPtr PartitionWriter::ChooseSourceFD( | 
|  | 542 | const InstallOperation& operation, ErrorCode* error) { | 
|  | 543 | if (source_fd_ == nullptr) { | 
|  | 544 | LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr"; | 
|  | 545 | return nullptr; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | if (!operation.has_src_sha256_hash()) { | 
|  | 549 | // When the operation doesn't include a source hash, we attempt the error | 
|  | 550 | // corrected device first since we can't verify the block in the raw device | 
|  | 551 | // at this point, but we first need to make sure all extents are readable | 
|  | 552 | // since the error corrected device can be shorter or not available. | 
|  | 553 | if (OpenCurrentECCPartition() && | 
|  | 554 | fd_utils::ReadAndHashExtents( | 
|  | 555 | source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) { | 
|  | 556 | return source_ecc_fd_; | 
|  | 557 | } | 
|  | 558 | return source_fd_; | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | brillo::Blob source_hash; | 
|  | 562 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), | 
|  | 563 | operation.src_sha256_hash().end()); | 
|  | 564 | if (fd_utils::ReadAndHashExtents( | 
|  | 565 | source_fd_, operation.src_extents(), block_size_, &source_hash) && | 
|  | 566 | source_hash == expected_source_hash) { | 
|  | 567 | return source_fd_; | 
|  | 568 | } | 
|  | 569 | // We fall back to use the error corrected device if the hash of the raw | 
|  | 570 | // device doesn't match or there was an error reading the source partition. | 
|  | 571 | if (!OpenCurrentECCPartition()) { | 
|  | 572 | // The following function call will return false since the source hash | 
|  | 573 | // mismatches, but we still want to call it so it prints the appropriate | 
|  | 574 | // log message. | 
|  | 575 | ValidateSourceHash(source_hash, operation, source_fd_, error); | 
|  | 576 | return nullptr; | 
|  | 577 | } | 
|  | 578 | LOG(WARNING) << "Source hash from RAW device mismatched: found " | 
|  | 579 | << base::HexEncode(source_hash.data(), source_hash.size()) | 
|  | 580 | << ", expected " | 
|  | 581 | << base::HexEncode(expected_source_hash.data(), | 
|  | 582 | expected_source_hash.size()); | 
|  | 583 |  | 
|  | 584 | if (fd_utils::ReadAndHashExtents( | 
|  | 585 | source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) && | 
|  | 586 | ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) { | 
|  | 587 | // At this point reading from the error corrected device worked, but | 
|  | 588 | // reading from the raw device failed, so this is considered a recovered | 
|  | 589 | // failure. | 
|  | 590 | source_ecc_recovered_failures_++; | 
|  | 591 | return source_ecc_fd_; | 
|  | 592 | } | 
|  | 593 | return nullptr; | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | bool PartitionWriter::OpenCurrentECCPartition() { | 
|  | 597 | // No support for ECC for full payloads. | 
|  | 598 | // Full payload should not have any opeartion that requires ECC partitions. | 
|  | 599 | if (source_ecc_fd_) | 
|  | 600 | return true; | 
|  | 601 |  | 
|  | 602 | if (source_ecc_open_failure_) | 
|  | 603 | return false; | 
|  | 604 |  | 
|  | 605 | #if USE_FEC | 
|  | 606 | const PartitionUpdate& partition = partition_update_; | 
|  | 607 | const InstallPlan::Partition& install_part = install_part_; | 
|  | 608 | std::string path = install_part.source_path; | 
|  | 609 | FileDescriptorPtr fd(new FecFileDescriptor()); | 
|  | 610 | if (!fd->Open(path.c_str(), O_RDONLY, 0)) { | 
|  | 611 | PLOG(ERROR) << "Unable to open ECC source partition " | 
|  | 612 | << partition.partition_name() << ", file " << path; | 
|  | 613 | source_ecc_open_failure_ = true; | 
|  | 614 | return false; | 
|  | 615 | } | 
|  | 616 | source_ecc_fd_ = fd; | 
|  | 617 | #else | 
|  | 618 | // No support for ECC compiled. | 
|  | 619 | source_ecc_open_failure_ = true; | 
|  | 620 | #endif  // USE_FEC | 
|  | 621 |  | 
|  | 622 | return !source_ecc_open_failure_; | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | int PartitionWriter::Close() { | 
|  | 626 | int err = 0; | 
|  | 627 | if (source_fd_ && !source_fd_->Close()) { | 
|  | 628 | err = errno; | 
|  | 629 | PLOG(ERROR) << "Error closing source partition"; | 
|  | 630 | if (!err) | 
|  | 631 | err = 1; | 
|  | 632 | } | 
|  | 633 | source_fd_.reset(); | 
|  | 634 | source_path_.clear(); | 
|  | 635 |  | 
|  | 636 | if (target_fd_ && !target_fd_->Close()) { | 
|  | 637 | err = errno; | 
|  | 638 | PLOG(ERROR) << "Error closing target partition"; | 
|  | 639 | if (!err) | 
|  | 640 | err = 1; | 
|  | 641 | } | 
|  | 642 | target_fd_.reset(); | 
|  | 643 | target_path_.clear(); | 
|  | 644 |  | 
|  | 645 | if (source_ecc_fd_ && !source_ecc_fd_->Close()) { | 
|  | 646 | err = errno; | 
|  | 647 | PLOG(ERROR) << "Error closing ECC source partition"; | 
|  | 648 | if (!err) | 
|  | 649 | err = 1; | 
|  | 650 | } | 
|  | 651 | source_ecc_fd_.reset(); | 
|  | 652 | source_ecc_open_failure_ = false; | 
|  | 653 | return -err; | 
|  | 654 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 655 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 656 | void PartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { | 
|  | 657 | target_fd_->Flush(); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 658 | } | 
|  | 659 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 660 | std::unique_ptr<ExtentWriter> PartitionWriter::CreateBaseExtentWriter() { | 
|  | 661 | return std::make_unique<DirectExtentWriter>(); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 662 | } | 
|  | 663 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 664 | }  // namespace chromeos_update_engine |