blob: 24ea7d8dc58f9ac76a7f5ef3a93764399746e218 [file] [log] [blame]
Kelvin Zhang50bac652020-09-28 15:51:41 -04001//
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>
Kelvin Zhang1d402cb2021-02-09 15:28:16 -050020#include <sys/mman.h>
Kelvin Zhang50bac652020-09-28 15:51:41 -040021
22#include <algorithm>
23#include <initializer_list>
24#include <memory>
25#include <utility>
26#include <vector>
27
Kelvin Zhang1d402cb2021-02-09 15:28:16 -050028#include <base/files/memory_mapped_file.h>
Kelvin Zhang50bac652020-09-28 15:51:41 -040029#include <base/strings/string_number_conversions.h>
30#include <bsdiff/bspatch.h>
31#include <puffin/puffpatch.h>
32#include <bsdiff/file_interface.h>
33#include <puffin/stream.h>
34
35#include "update_engine/common/terminator.h"
36#include "update_engine/common/utils.h"
37#include "update_engine/payload_consumer/bzip_extent_writer.h"
38#include "update_engine/payload_consumer/cached_file_descriptor.h"
39#include "update_engine/payload_consumer/extent_reader.h"
40#include "update_engine/payload_consumer/extent_writer.h"
41#include "update_engine/payload_consumer/fec_file_descriptor.h"
42#include "update_engine/payload_consumer/file_descriptor_utils.h"
43#include "update_engine/payload_consumer/install_plan.h"
44#include "update_engine/payload_consumer/mount_history.h"
45#include "update_engine/payload_consumer/payload_constants.h"
46#include "update_engine/payload_consumer/xz_extent_writer.h"
Kelvin Zhang37b9b702021-02-23 10:30:37 -050047#include "update_engine/payload_generator/extent_utils.h"
Kelvin Zhang50bac652020-09-28 15:51:41 -040048
49namespace chromeos_update_engine {
50
51namespace {
52constexpr uint64_t kCacheSize = 1024 * 1024; // 1MB
53
54// Discard the tail of the block device referenced by |fd|, from the offset
55// |data_size| until the end of the block device. Returns whether the data was
56// discarded.
57
58bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) {
59 uint64_t part_size = fd->BlockDevSize();
60 if (!part_size || part_size <= data_size)
61 return false;
62
63 struct blkioctl_request {
64 int number;
65 const char* name;
66 };
67 const std::initializer_list<blkioctl_request> blkioctl_requests = {
68 {BLKDISCARD, "BLKDISCARD"},
69 {BLKSECDISCARD, "BLKSECDISCARD"},
70#ifdef BLKZEROOUT
71 {BLKZEROOUT, "BLKZEROOUT"},
72#endif
73 };
74 for (const auto& req : blkioctl_requests) {
75 int error = 0;
76 if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) &&
77 error == 0) {
78 return true;
79 }
80 LOG(WARNING) << "Error discarding the last "
81 << (part_size - data_size) / 1024 << " KiB using ioctl("
82 << req.name << ")";
83 }
84 return false;
85}
86
87} // namespace
88
Kelvin Zhang37b9b702021-02-23 10:30:37 -050089using google::protobuf::RepeatedPtrField;
90
Kelvin Zhang50bac652020-09-28 15:51:41 -040091// Opens path for read/write. On success returns an open FileDescriptor
92// and sets *err to 0. On failure, sets *err to errno and returns nullptr.
93FileDescriptorPtr OpenFile(const char* path,
94 int mode,
95 bool cache_writes,
96 int* err) {
97 // Try to mark the block device read-only based on the mode. Ignore any
98 // failure since this won't work when passing regular files.
99 bool read_only = (mode & O_ACCMODE) == O_RDONLY;
100 utils::SetBlockDeviceReadOnly(path, read_only);
101
102 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
103 if (cache_writes && !read_only) {
104 fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize));
105 LOG(INFO) << "Caching writes.";
106 }
107 if (!fd->Open(path, mode, 000)) {
108 *err = errno;
109 PLOG(ERROR) << "Unable to open file " << path;
110 return nullptr;
111 }
112 *err = 0;
113 return fd;
114}
115
116class BsdiffExtentFile : public bsdiff::FileInterface {
117 public:
118 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size)
119 : BsdiffExtentFile(std::move(reader), nullptr, size) {}
120 BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size)
121 : BsdiffExtentFile(nullptr, std::move(writer), size) {}
122
123 ~BsdiffExtentFile() override = default;
124
125 bool Read(void* buf, size_t count, size_t* bytes_read) override {
126 TEST_AND_RETURN_FALSE(reader_->Read(buf, count));
127 *bytes_read = count;
128 offset_ += count;
129 return true;
130 }
131
132 bool Write(const void* buf, size_t count, size_t* bytes_written) override {
133 TEST_AND_RETURN_FALSE(writer_->Write(buf, count));
134 *bytes_written = count;
135 offset_ += count;
136 return true;
137 }
138
139 bool Seek(off_t pos) override {
140 if (reader_ != nullptr) {
141 TEST_AND_RETURN_FALSE(reader_->Seek(pos));
142 offset_ = pos;
143 } else {
144 // For writes technically there should be no change of position, or it
145 // should be equivalent of current offset.
146 TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos));
147 }
148 return true;
149 }
150
151 bool Close() override { return true; }
152
153 bool GetSize(uint64_t* size) override {
154 *size = size_;
155 return true;
156 }
157
158 private:
159 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,
160 std::unique_ptr<ExtentWriter> writer,
161 size_t size)
162 : reader_(std::move(reader)),
163 writer_(std::move(writer)),
164 size_(size),
165 offset_(0) {}
166
167 std::unique_ptr<ExtentReader> reader_;
168 std::unique_ptr<ExtentWriter> writer_;
169 uint64_t size_;
170 uint64_t offset_;
171
172 DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile);
173};
174// A class to be passed to |puffpatch| for reading from |source_fd_| and writing
175// into |target_fd_|.
176class PuffinExtentStream : public puffin::StreamInterface {
177 public:
178 // Constructor for creating a stream for reading from an |ExtentReader|.
179 PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size)
180 : PuffinExtentStream(std::move(reader), nullptr, size) {}
181
182 // Constructor for creating a stream for writing to an |ExtentWriter|.
183 PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size)
184 : PuffinExtentStream(nullptr, std::move(writer), size) {}
185
186 ~PuffinExtentStream() override = default;
187
188 bool GetSize(uint64_t* size) const override {
189 *size = size_;
190 return true;
191 }
192
193 bool GetOffset(uint64_t* offset) const override {
194 *offset = offset_;
195 return true;
196 }
197
198 bool Seek(uint64_t offset) override {
199 if (is_read_) {
200 TEST_AND_RETURN_FALSE(reader_->Seek(offset));
201 offset_ = offset;
202 } else {
203 // For writes technically there should be no change of position, or it
204 // should equivalent of current offset.
205 TEST_AND_RETURN_FALSE(offset_ == offset);
206 }
207 return true;
208 }
209
210 bool Read(void* buffer, size_t count) override {
211 TEST_AND_RETURN_FALSE(is_read_);
212 TEST_AND_RETURN_FALSE(reader_->Read(buffer, count));
213 offset_ += count;
214 return true;
215 }
216
217 bool Write(const void* buffer, size_t count) override {
218 TEST_AND_RETURN_FALSE(!is_read_);
219 TEST_AND_RETURN_FALSE(writer_->Write(buffer, count));
220 offset_ += count;
221 return true;
222 }
223
224 bool Close() override { return true; }
225
226 private:
227 PuffinExtentStream(std::unique_ptr<ExtentReader> reader,
228 std::unique_ptr<ExtentWriter> writer,
229 uint64_t size)
230 : reader_(std::move(reader)),
231 writer_(std::move(writer)),
232 size_(size),
233 offset_(0),
234 is_read_(reader_ ? true : false) {}
235
236 std::unique_ptr<ExtentReader> reader_;
237 std::unique_ptr<ExtentWriter> writer_;
238 uint64_t size_;
239 uint64_t offset_;
240 bool is_read_;
241
242 DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream);
243};
244
245PartitionWriter::PartitionWriter(
246 const PartitionUpdate& partition_update,
247 const InstallPlan::Partition& install_part,
248 DynamicPartitionControlInterface* dynamic_control,
249 size_t block_size,
250 bool is_interactive)
251 : partition_update_(partition_update),
252 install_part_(install_part),
253 dynamic_control_(dynamic_control),
254 interactive_(is_interactive),
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500255 block_size_(block_size),
256 install_op_executor_(block_size) {}
Kelvin Zhang50bac652020-09-28 15:51:41 -0400257
258PartitionWriter::~PartitionWriter() {
259 Close();
260}
261
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500262bool PartitionWriter::OpenSourcePartition(uint32_t source_slot,
263 bool source_may_exist) {
264 source_path_.clear();
265 if (!source_may_exist) {
266 return true;
267 }
268 if (install_part_.source_size > 0 && !install_part_.source_path.empty()) {
269 source_path_ = install_part_.source_path;
270 int err;
271 source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err);
272 if (source_fd_ == nullptr) {
273 LOG(ERROR) << "Unable to open source partition " << install_part_.name
274 << " on slot " << BootControlInterface::SlotName(source_slot)
275 << ", file " << source_path_;
276 return false;
277 }
278 }
279 return true;
280}
281
Kelvin Zhang50bac652020-09-28 15:51:41 -0400282bool PartitionWriter::Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400283 bool source_may_exist,
284 size_t next_op_index) {
Kelvin Zhang50bac652020-09-28 15:51:41 -0400285 const PartitionUpdate& partition = partition_update_;
286 uint32_t source_slot = install_plan->source_slot;
287 uint32_t target_slot = install_plan->target_slot;
Kelvin Zhang3f60d532020-11-09 13:33:17 -0500288 TEST_AND_RETURN_FALSE(OpenSourcePartition(source_slot, source_may_exist));
Kelvin Zhang50bac652020-09-28 15:51:41 -0400289
290 // We shouldn't open the source partition in certain cases, e.g. some dynamic
291 // partitions in delta payload, partitions included in the full payload for
292 // partial updates. Use the source size as the indicator.
Kelvin Zhang50bac652020-09-28 15:51:41 -0400293
294 target_path_ = install_part_.target_path;
295 int err;
296
297 int flags = O_RDWR;
298 if (!interactive_)
299 flags |= O_DSYNC;
300
301 LOG(INFO) << "Opening " << target_path_ << " partition with"
302 << (interactive_ ? "out" : "") << " O_DSYNC";
303
304 target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err);
305 if (!target_fd_) {
306 LOG(ERROR) << "Unable to open target partition "
307 << partition.partition_name() << " on slot "
308 << BootControlInterface::SlotName(target_slot) << ", file "
309 << target_path_;
310 return false;
311 }
312
313 LOG(INFO) << "Applying " << partition.operations().size()
314 << " operations to partition \"" << partition.partition_name()
315 << "\"";
316
317 // Discard the end of the partition, but ignore failures.
318 DiscardPartitionTail(target_fd_, install_part_.target_size);
319
320 return true;
321}
322
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500323bool InstallOperationExecutor::ExecuteReplaceOperation(
324 const InstallOperation& operation,
325 std::unique_ptr<ExtentWriter> writer,
326 const void* data,
327 size_t count) {
328 TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::REPLACE ||
329 operation.type() == InstallOperation::REPLACE_BZ ||
330 operation.type() == InstallOperation::REPLACE_XZ);
Kelvin Zhang50bac652020-09-28 15:51:41 -0400331 // Setup the ExtentWriter stack based on the operation type.
Kelvin Zhang50bac652020-09-28 15:51:41 -0400332 if (operation.type() == InstallOperation::REPLACE_BZ) {
333 writer.reset(new BzipExtentWriter(std::move(writer)));
334 } else if (operation.type() == InstallOperation::REPLACE_XZ) {
335 writer.reset(new XzExtentWriter(std::move(writer)));
336 }
Kelvin Zhang4d22ca22021-02-09 14:06:25 -0500337 TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
Kelvin Zhang50bac652020-09-28 15:51:41 -0400338 TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length()));
339
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400340 return true;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400341}
342
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500343bool PartitionWriter::PerformReplaceOperation(const InstallOperation& operation,
344 const void* data,
345 size_t count) {
346 // Setup the ExtentWriter stack based on the operation type.
347 std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter();
348 writer->Init(operation.dst_extents(), block_size_);
349 return install_op_executor_.ExecuteReplaceOperation(
350 operation, std::move(writer), data, count);
351}
352
353bool InstallOperationExecutor::ExecuteZeroOrDiscardOperation(
354 const InstallOperation& operation, ExtentWriter* writer) {
355 TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::ZERO ||
356 operation.type() == InstallOperation::DISCARD);
357 for (const auto& extent : operation.dst_extents()) {
358 // Mmap a region of /dev/zero, as we don't need any actual memory to store
359 // these 0s, so mmap a region of "free memory".
360 base::File dev_zero(base::FilePath("/dev/zero"),
361 base::File::FLAG_OPEN | base::File::FLAG_READ);
362 base::MemoryMappedFile buffer;
363 TEST_AND_RETURN_FALSE_ERRNO(buffer.Initialize(
364 std::move(dev_zero),
365 base::MemoryMappedFile::Region{
366 0, static_cast<size_t>(extent.num_blocks() * block_size_)},
367 base::MemoryMappedFile::Access::READ_ONLY));
368 TEST_AND_RETURN_FALSE(buffer.data() != nullptr);
369 writer->Write(buffer.data(), buffer.length());
370 }
371 return true;
372}
373
Kelvin Zhang50bac652020-09-28 15:51:41 -0400374bool PartitionWriter::PerformZeroOrDiscardOperation(
375 const InstallOperation& operation) {
376#ifdef BLKZEROOUT
377 bool attempt_ioctl = true;
378 int request =
379 (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD);
380#else // !defined(BLKZEROOUT)
381 bool attempt_ioctl = false;
382 int request = 0;
383#endif // !defined(BLKZEROOUT)
384
385 brillo::Blob zeros;
386 for (const Extent& extent : operation.dst_extents()) {
387 const uint64_t start = extent.start_block() * block_size_;
388 const uint64_t length = extent.num_blocks() * block_size_;
389 if (attempt_ioctl) {
390 int result = 0;
391 if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0)
392 continue;
393 attempt_ioctl = false;
394 }
395 // In case of failure, we fall back to writing 0 to the selected region.
396 zeros.resize(16 * block_size_);
397 for (uint64_t offset = 0; offset < length; offset += zeros.size()) {
398 uint64_t chunk_length =
399 std::min(length - offset, static_cast<uint64_t>(zeros.size()));
Kelvin Zhang4b280242020-11-06 16:07:45 -0500400 TEST_AND_RETURN_FALSE(utils::WriteAll(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400401 target_fd_, zeros.data(), chunk_length, start + offset));
402 }
403 }
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400404 return true;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400405}
406
Kelvin Zhang37b9b702021-02-23 10:30:37 -0500407std::ostream& operator<<(std::ostream& out,
408 const RepeatedPtrField<Extent>& extents) {
409 if (extents.size() == 0) {
410 out << "[]";
411 return out;
412 }
413 out << "[";
414 auto begin = extents.begin();
415 out << *begin;
416 for (int i = 1; i < extents.size(); i++) {
417 ++begin;
418 out << ", " << *begin;
419 }
420 out << "]";
421 return out;
422}
423
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500424bool InstallOperationExecutor::ExecuteSourceCopyOperation(
425 const InstallOperation& operation,
426 ExtentWriter* writer,
427 FileDescriptorPtr source_fd) {
428 TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::SOURCE_COPY);
429 TEST_AND_RETURN_FALSE(source_fd != nullptr);
430
431 return fd_utils::CommonHashExtents(
432 source_fd, operation.src_extents(), writer, block_size_, nullptr);
433}
434
Kelvin Zhang50bac652020-09-28 15:51:41 -0400435bool PartitionWriter::PerformSourceCopyOperation(
436 const InstallOperation& operation, ErrorCode* error) {
437 TEST_AND_RETURN_FALSE(source_fd_ != nullptr);
438
439 // The device may optimize the SOURCE_COPY operation.
440 // Being this a device-specific optimization let DynamicPartitionController
441 // decide it the operation should be skipped.
442 const PartitionUpdate& partition = partition_update_;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400443
444 InstallOperation buf;
Kelvin Zhang37b9b702021-02-23 10:30:37 -0500445 const bool should_optimize = dynamic_control_->OptimizeOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400446 partition.partition_name(), operation, &buf);
447 const InstallOperation& optimized = should_optimize ? buf : operation;
448
Kelvin Zhang37b9b702021-02-23 10:30:37 -0500449 // Invoke ChooseSourceFD with original operation, so that it can properly
450 // verify source hashes. Optimized operation might contain a smaller set of
451 // extents, or completely empty.
452 auto source_fd = ChooseSourceFD(operation, error);
453 if (source_fd == nullptr) {
454 LOG(ERROR) << "Unrecoverable source hash mismatch found on partition "
455 << partition.partition_name()
456 << " extents: " << operation.src_extents();
457 return false;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400458 }
Kelvin Zhang37b9b702021-02-23 10:30:37 -0500459
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500460 auto writer = CreateBaseExtentWriter();
461 writer->Init(optimized.dst_extents(), block_size_);
462 return install_op_executor_.ExecuteSourceCopyOperation(
463 optimized, writer.get(), source_fd);
Kelvin Zhang50bac652020-09-28 15:51:41 -0400464}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400465
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500466bool InstallOperationExecutor::ExecuteSourceBsdiffOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400467 const InstallOperation& operation,
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500468 std::unique_ptr<ExtentWriter> writer,
469 FileDescriptorPtr source_fd,
Kelvin Zhang50bac652020-09-28 15:51:41 -0400470 const void* data,
471 size_t count) {
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500472 TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::SOURCE_BSDIFF ||
473 operation.type() == InstallOperation::BROTLI_BSDIFF ||
474 operation.type() == InstallOperation::BSDIFF);
Kelvin Zhang50bac652020-09-28 15:51:41 -0400475 TEST_AND_RETURN_FALSE(source_fd != nullptr);
476
477 auto reader = std::make_unique<DirectExtentReader>();
478 TEST_AND_RETURN_FALSE(
479 reader->Init(source_fd, operation.src_extents(), block_size_));
480 auto src_file = std::make_unique<BsdiffExtentFile>(
481 std::move(reader),
482 utils::BlocksInExtents(operation.src_extents()) * block_size_);
483
Kelvin Zhang4d22ca22021-02-09 14:06:25 -0500484 TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
Kelvin Zhang50bac652020-09-28 15:51:41 -0400485 auto dst_file = std::make_unique<BsdiffExtentFile>(
486 std::move(writer),
487 utils::BlocksInExtents(operation.dst_extents()) * block_size_);
488
489 TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
490 std::move(dst_file),
491 reinterpret_cast<const uint8_t*>(data),
492 count) == 0);
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400493 return true;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400494}
495
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500496bool PartitionWriter::PerformSourceBsdiffOperation(
Kelvin Zhang50bac652020-09-28 15:51:41 -0400497 const InstallOperation& operation,
498 ErrorCode* error,
499 const void* data,
500 size_t count) {
501 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
502 TEST_AND_RETURN_FALSE(source_fd != nullptr);
503
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500504 auto writer = CreateBaseExtentWriter();
505 writer->Init(operation.dst_extents(), block_size_);
506 return install_op_executor_.ExecuteSourceBsdiffOperation(
507 operation, std::move(writer), source_fd, data, count);
508}
509
510bool InstallOperationExecutor::ExecutePuffDiffOperation(
511 const InstallOperation& operation,
512 std::unique_ptr<ExtentWriter> writer,
513 FileDescriptorPtr source_fd,
514 const void* data,
515 size_t count) {
516 TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::PUFFDIFF);
517 TEST_AND_RETURN_FALSE(source_fd != nullptr);
518
Kelvin Zhang50bac652020-09-28 15:51:41 -0400519 auto reader = std::make_unique<DirectExtentReader>();
520 TEST_AND_RETURN_FALSE(
521 reader->Init(source_fd, operation.src_extents(), block_size_));
522 puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
523 std::move(reader),
524 utils::BlocksInExtents(operation.src_extents()) * block_size_));
525
Kelvin Zhang4d22ca22021-02-09 14:06:25 -0500526 TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
Kelvin Zhang50bac652020-09-28 15:51:41 -0400527 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 Zhang52cb1d72020-10-27 13:44:25 -0400538 return true;
Kelvin Zhang50bac652020-09-28 15:51:41 -0400539}
540
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500541bool PartitionWriter::PerformPuffDiffOperation(
542 const InstallOperation& operation,
543 ErrorCode* error,
544 const void* data,
545 size_t count) {
546 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
547 TEST_AND_RETURN_FALSE(source_fd != nullptr);
548
549 auto writer = CreateBaseExtentWriter();
550 writer->Init(operation.dst_extents(), block_size_);
551 return install_op_executor_.ExecutePuffDiffOperation(
552 operation, std::move(writer), source_fd, data, count);
553}
554
Kelvin Zhang50bac652020-09-28 15:51:41 -0400555FileDescriptorPtr PartitionWriter::ChooseSourceFD(
556 const InstallOperation& operation, ErrorCode* error) {
557 if (source_fd_ == nullptr) {
558 LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr";
559 return nullptr;
560 }
561
562 if (!operation.has_src_sha256_hash()) {
563 // When the operation doesn't include a source hash, we attempt the error
564 // corrected device first since we can't verify the block in the raw device
565 // at this point, but we first need to make sure all extents are readable
566 // since the error corrected device can be shorter or not available.
567 if (OpenCurrentECCPartition() &&
568 fd_utils::ReadAndHashExtents(
569 source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) {
570 return source_ecc_fd_;
571 }
572 return source_fd_;
573 }
574
575 brillo::Blob source_hash;
576 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
577 operation.src_sha256_hash().end());
578 if (fd_utils::ReadAndHashExtents(
579 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
580 source_hash == expected_source_hash) {
581 return source_fd_;
582 }
583 // We fall back to use the error corrected device if the hash of the raw
584 // device doesn't match or there was an error reading the source partition.
585 if (!OpenCurrentECCPartition()) {
586 // The following function call will return false since the source hash
587 // mismatches, but we still want to call it so it prints the appropriate
588 // log message.
589 ValidateSourceHash(source_hash, operation, source_fd_, error);
590 return nullptr;
591 }
592 LOG(WARNING) << "Source hash from RAW device mismatched: found "
593 << base::HexEncode(source_hash.data(), source_hash.size())
594 << ", expected "
595 << base::HexEncode(expected_source_hash.data(),
596 expected_source_hash.size());
597
598 if (fd_utils::ReadAndHashExtents(
599 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) &&
600 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) {
601 // At this point reading from the error corrected device worked, but
602 // reading from the raw device failed, so this is considered a recovered
603 // failure.
604 source_ecc_recovered_failures_++;
605 return source_ecc_fd_;
606 }
607 return nullptr;
608}
609
610bool PartitionWriter::OpenCurrentECCPartition() {
611 // No support for ECC for full payloads.
612 // Full payload should not have any opeartion that requires ECC partitions.
613 if (source_ecc_fd_)
614 return true;
615
616 if (source_ecc_open_failure_)
617 return false;
618
619#if USE_FEC
620 const PartitionUpdate& partition = partition_update_;
621 const InstallPlan::Partition& install_part = install_part_;
622 std::string path = install_part.source_path;
623 FileDescriptorPtr fd(new FecFileDescriptor());
624 if (!fd->Open(path.c_str(), O_RDONLY, 0)) {
625 PLOG(ERROR) << "Unable to open ECC source partition "
626 << partition.partition_name() << ", file " << path;
627 source_ecc_open_failure_ = true;
628 return false;
629 }
630 source_ecc_fd_ = fd;
631#else
632 // No support for ECC compiled.
633 source_ecc_open_failure_ = true;
634#endif // USE_FEC
635
636 return !source_ecc_open_failure_;
637}
638
639int PartitionWriter::Close() {
640 int err = 0;
641 if (source_fd_ && !source_fd_->Close()) {
642 err = errno;
643 PLOG(ERROR) << "Error closing source partition";
644 if (!err)
645 err = 1;
646 }
647 source_fd_.reset();
648 source_path_.clear();
649
650 if (target_fd_ && !target_fd_->Close()) {
651 err = errno;
652 PLOG(ERROR) << "Error closing target partition";
653 if (!err)
654 err = 1;
655 }
656 target_fd_.reset();
657 target_path_.clear();
658
659 if (source_ecc_fd_ && !source_ecc_fd_->Close()) {
660 err = errno;
661 PLOG(ERROR) << "Error closing ECC source partition";
662 if (!err)
663 err = 1;
664 }
665 source_ecc_fd_.reset();
666 source_ecc_open_failure_ = false;
667 return -err;
668}
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400669
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400670void PartitionWriter::CheckpointUpdateProgress(size_t next_op_index) {
671 target_fd_->Flush();
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400672}
673
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400674std::unique_ptr<ExtentWriter> PartitionWriter::CreateBaseExtentWriter() {
Kelvin Zhang4d22ca22021-02-09 14:06:25 -0500675 return std::make_unique<DirectExtentWriter>(target_fd_);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400676}
677
Kelvin Zhang1d402cb2021-02-09 15:28:16 -0500678bool InstallOperationExecutor::ExecuteInstallOp(
679 const InstallOperation& op,
680 std::unique_ptr<ExtentWriter> writer,
681 FileDescriptorPtr source_fd,
682 const void* data,
683 size_t size) {
684 switch (op.type()) {
685 case InstallOperation::REPLACE:
686 case InstallOperation::REPLACE_BZ:
687 case InstallOperation::REPLACE_XZ:
688 return ExecuteReplaceOperation(op, std::move(writer), data, size);
689 case InstallOperation::ZERO:
690 case InstallOperation::DISCARD:
691 return ExecuteZeroOrDiscardOperation(op, writer.get());
692 case InstallOperation::SOURCE_COPY:
693 return ExecuteSourceCopyOperation(op, writer.get(), source_fd);
694 case InstallOperation::SOURCE_BSDIFF:
695 case InstallOperation::BROTLI_BSDIFF:
696 return ExecuteSourceBsdiffOperation(
697 op, std::move(writer), source_fd, data, size);
698 case InstallOperation::PUFFDIFF:
699 return ExecutePuffDiffOperation(
700 op, std::move(writer), source_fd, data, size);
701 break;
702 default:
703 return false;
704 }
705 return false;
706}
707
Kelvin Zhang50bac652020-09-28 15:51:41 -0400708} // namespace chromeos_update_engine