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 | |
| 17 | #ifndef UPDATE_ENGINE_PARTITION_WRITER_H_ |
| 18 | #define UPDATE_ENGINE_PARTITION_WRITER_H_ |
| 19 | |
| 20 | #include <cstdint> |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 21 | #include <memory> |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | #include <brillo/secure_blob.h> |
| 25 | #include <gtest/gtest_prod.h> |
| 26 | |
| 27 | #include "update_engine/common/dynamic_partition_control_interface.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 28 | #include "update_engine/payload_consumer/extent_writer.h" |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/file_descriptor.h" |
Kelvin Zhang | 40d9666 | 2021-02-24 14:21:29 -0500 | [diff] [blame] | 30 | #include "update_engine/payload_consumer/install_operation_executor.h" |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 31 | #include "update_engine/payload_consumer/install_plan.h" |
| 32 | #include "update_engine/update_metadata.pb.h" |
Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 33 | |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 34 | namespace chromeos_update_engine { |
Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 35 | |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 36 | class PartitionWriter { |
| 37 | public: |
| 38 | PartitionWriter(const PartitionUpdate& partition_update, |
| 39 | const InstallPlan::Partition& install_part, |
| 40 | DynamicPartitionControlInterface* dynamic_control, |
| 41 | size_t block_size, |
| 42 | bool is_interactive); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 43 | virtual ~PartitionWriter(); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 44 | static bool ValidateSourceHash(const brillo::Blob& calculated_hash, |
| 45 | const InstallOperation& operation, |
| 46 | const FileDescriptorPtr source_fd, |
| 47 | ErrorCode* error); |
| 48 | |
| 49 | // Perform necessary initialization work before InstallOperation can be |
| 50 | // applied to this partition |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 51 | [[nodiscard]] virtual bool Init(const InstallPlan* install_plan, |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 52 | bool source_may_exist, |
| 53 | size_t next_op_index); |
| 54 | |
| 55 | // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's |
| 56 | // optional. DeltaPerformer may or may not call this everytime an operation is |
| 57 | // applied. |
| 58 | // |next_op_index| is index of next operation that should be applied. |
| 59 | // |next_op_index-1| is the last operation that is already applied. |
| 60 | virtual void CheckpointUpdateProgress(size_t next_op_index); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 61 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 62 | // Close partition writer, when calling this function there's no guarantee |
| 63 | // that all |InstallOperations| are sent to |PartitionWriter|. This function |
| 64 | // will be called even if we are pausing/aborting the update. |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 65 | int Close(); |
| 66 | |
| 67 | // These perform a specific type of operation and return true on success. |
| 68 | // |error| will be set if source hash mismatch, otherwise |error| might not be |
| 69 | // set even if it fails. |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 70 | [[nodiscard]] virtual bool PerformReplaceOperation( |
| 71 | const InstallOperation& operation, const void* data, size_t count); |
| 72 | [[nodiscard]] virtual bool PerformZeroOrDiscardOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 73 | const InstallOperation& operation); |
| 74 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 75 | [[nodiscard]] virtual bool PerformSourceCopyOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 76 | const InstallOperation& operation, ErrorCode* error); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 77 | [[nodiscard]] virtual bool PerformSourceBsdiffOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 78 | const InstallOperation& operation, |
| 79 | ErrorCode* error, |
| 80 | const void* data, |
| 81 | size_t count); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 82 | [[nodiscard]] virtual bool PerformPuffDiffOperation( |
| 83 | const InstallOperation& operation, |
| 84 | ErrorCode* error, |
| 85 | const void* data, |
| 86 | size_t count); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 87 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 88 | // |DeltaPerformer| calls this when all Install Ops are sent to partition |
| 89 | // writer. No |Perform*Operation| methods will be called in the future, and |
| 90 | // the partition writer is expected to be closed soon. |
| 91 | [[nodiscard]] virtual bool FinishedInstallOps() { return true; } |
| 92 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 93 | protected: |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 94 | friend class PartitionWriterTest; |
| 95 | FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest); |
| 96 | |
Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 97 | bool OpenSourcePartition(uint32_t source_slot, bool source_may_exist); |
| 98 | |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 99 | bool OpenCurrentECCPartition(); |
| 100 | // For a given operation, choose the source fd to be used (raw device or error |
| 101 | // correction device) based on the source operation hash. |
| 102 | // Returns nullptr if the source hash mismatch cannot be corrected, and set |
| 103 | // the |error| accordingly. |
| 104 | FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation, |
| 105 | ErrorCode* error); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 106 | [[nodiscard]] virtual std::unique_ptr<ExtentWriter> CreateBaseExtentWriter(); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 107 | |
| 108 | const PartitionUpdate& partition_update_; |
| 109 | const InstallPlan::Partition& install_part_; |
| 110 | DynamicPartitionControlInterface* dynamic_control_; |
| 111 | // Path to source partition |
| 112 | std::string source_path_; |
| 113 | // Path to target partition |
| 114 | std::string target_path_; |
| 115 | FileDescriptorPtr source_fd_; |
| 116 | FileDescriptorPtr target_fd_; |
| 117 | const bool interactive_; |
| 118 | const size_t block_size_; |
| 119 | // File descriptor of the error corrected source partition. Only set while |
| 120 | // updating partition using a delta payload for a partition where error |
| 121 | // correction is available. The size of the error corrected device is smaller |
| 122 | // than the underlying raw device, since it doesn't include the error |
| 123 | // correction blocks. |
| 124 | FileDescriptorPtr source_ecc_fd_{nullptr}; |
| 125 | |
| 126 | // The total number of operations that failed source hash verification but |
| 127 | // passed after falling back to the error-corrected |source_ecc_fd_| device. |
| 128 | uint64_t source_ecc_recovered_failures_{0}; |
| 129 | |
| 130 | // Whether opening the current partition as an error-corrected device failed. |
| 131 | // Used to avoid re-opening the same source partition if it is not actually |
| 132 | // error corrected. |
| 133 | bool source_ecc_open_failure_{false}; |
Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 134 | |
| 135 | // This instance handles decompression/bsdfif/puffdiff. It's responsible for |
| 136 | // constructing data which should be written to target partition, actual |
| 137 | // "writing" is handled by |PartitionWriter| |
| 138 | InstallOperationExecutor install_op_executor_; |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 139 | }; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 140 | |
| 141 | namespace partition_writer { |
| 142 | // Return a PartitionWriter instance for perform InstallOps on this partition. |
| 143 | // Uses VABCPartitionWriter for Virtual AB Compression |
| 144 | std::unique_ptr<PartitionWriter> CreatePartitionWriter( |
| 145 | const PartitionUpdate& partition_update, |
| 146 | const InstallPlan::Partition& install_part, |
| 147 | DynamicPartitionControlInterface* dynamic_control, |
| 148 | size_t block_size, |
| 149 | bool is_interactive, |
| 150 | bool is_dynamic_partition); |
| 151 | } // namespace partition_writer |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 152 | } // namespace chromeos_update_engine |
| 153 | |
| 154 | #endif |