blob: 624a411b6f5dc59fd905284866c959bfdf628328 [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
17#ifndef UPDATE_ENGINE_PARTITION_WRITER_H_
18#define UPDATE_ENGINE_PARTITION_WRITER_H_
19
20#include <cstdint>
21#include <string>
22
23#include <brillo/secure_blob.h>
24#include <gtest/gtest_prod.h>
25
26#include "update_engine/common/dynamic_partition_control_interface.h"
27#include "update_engine/payload_consumer/file_descriptor.h"
28#include "update_engine/payload_consumer/install_plan.h"
29#include "update_engine/update_metadata.pb.h"
30namespace chromeos_update_engine {
31class PartitionWriter {
32 public:
33 PartitionWriter(const PartitionUpdate& partition_update,
34 const InstallPlan::Partition& install_part,
35 DynamicPartitionControlInterface* dynamic_control,
36 size_t block_size,
37 bool is_interactive);
38 ~PartitionWriter();
39 static bool ValidateSourceHash(const brillo::Blob& calculated_hash,
40 const InstallOperation& operation,
41 const FileDescriptorPtr source_fd,
42 ErrorCode* error);
43
44 // Perform necessary initialization work before InstallOperation can be
45 // applied to this partition
46 [[nodiscard]] bool Init(const InstallPlan* install_plan,
47 bool source_may_exist);
48
49 int Close();
50
51 // These perform a specific type of operation and return true on success.
52 // |error| will be set if source hash mismatch, otherwise |error| might not be
53 // set even if it fails.
54 [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation,
55 const void* data,
56 size_t count);
57 [[nodiscard]] bool PerformZeroOrDiscardOperation(
58 const InstallOperation& operation);
59
60 [[nodiscard]] bool PerformSourceCopyOperation(
61 const InstallOperation& operation, ErrorCode* error);
62 [[nodiscard]] bool PerformSourceBsdiffOperation(
63 const InstallOperation& operation,
64 ErrorCode* error,
65 const void* data,
66 size_t count);
67 [[nodiscard]] bool PerformPuffDiffOperation(const InstallOperation& operation,
68 ErrorCode* error,
69 const void* data,
70 size_t count);
71
72 private:
73 friend class PartitionWriterTest;
74 FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
75
76 bool OpenCurrentECCPartition();
77 // For a given operation, choose the source fd to be used (raw device or error
78 // correction device) based on the source operation hash.
79 // Returns nullptr if the source hash mismatch cannot be corrected, and set
80 // the |error| accordingly.
81 FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation,
82 ErrorCode* error);
83
84 const PartitionUpdate& partition_update_;
85 const InstallPlan::Partition& install_part_;
86 DynamicPartitionControlInterface* dynamic_control_;
87 // Path to source partition
88 std::string source_path_;
89 // Path to target partition
90 std::string target_path_;
91 FileDescriptorPtr source_fd_;
92 FileDescriptorPtr target_fd_;
93 const bool interactive_;
94 const size_t block_size_;
95 // File descriptor of the error corrected source partition. Only set while
96 // updating partition using a delta payload for a partition where error
97 // correction is available. The size of the error corrected device is smaller
98 // than the underlying raw device, since it doesn't include the error
99 // correction blocks.
100 FileDescriptorPtr source_ecc_fd_{nullptr};
101
102 // The total number of operations that failed source hash verification but
103 // passed after falling back to the error-corrected |source_ecc_fd_| device.
104 uint64_t source_ecc_recovered_failures_{0};
105
106 // Whether opening the current partition as an error-corrected device failed.
107 // Used to avoid re-opening the same source partition if it is not actually
108 // error corrected.
109 bool source_ecc_open_failure_{false};
110};
111} // namespace chromeos_update_engine
112
113#endif