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 | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame^] | 28 | #include "update_engine/common/prefs_interface.h" |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/extent_writer.h" |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 30 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 31 | #include "update_engine/payload_consumer/install_plan.h" |
| 32 | #include "update_engine/update_metadata.pb.h" |
| 33 | namespace chromeos_update_engine { |
| 34 | class PartitionWriter { |
| 35 | public: |
| 36 | PartitionWriter(const PartitionUpdate& partition_update, |
| 37 | const InstallPlan::Partition& install_part, |
| 38 | DynamicPartitionControlInterface* dynamic_control, |
| 39 | size_t block_size, |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame^] | 40 | PrefsInterface* prefs, |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 41 | bool is_interactive); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 42 | virtual ~PartitionWriter(); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 43 | static bool ValidateSourceHash(const brillo::Blob& calculated_hash, |
| 44 | const InstallOperation& operation, |
| 45 | const FileDescriptorPtr source_fd, |
| 46 | ErrorCode* error); |
| 47 | |
| 48 | // Perform necessary initialization work before InstallOperation can be |
| 49 | // applied to this partition |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 50 | [[nodiscard]] virtual bool Init(const InstallPlan* install_plan, |
| 51 | bool source_may_exist); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 52 | |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame^] | 53 | // This will be called by DeltaPerformer after applying an InstallOp. |
| 54 | // |next_op_index| is index of next operation that should be applied. |
| 55 | // |next_op_index-1| is the last operation that is already applied. |
| 56 | virtual void CheckpointUpdateProgress(size_t next_op_index) {} |
| 57 | |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 58 | int Close(); |
| 59 | |
| 60 | // These perform a specific type of operation and return true on success. |
| 61 | // |error| will be set if source hash mismatch, otherwise |error| might not be |
| 62 | // set even if it fails. |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 63 | [[nodiscard]] virtual bool PerformReplaceOperation( |
| 64 | const InstallOperation& operation, const void* data, size_t count); |
| 65 | [[nodiscard]] virtual bool PerformZeroOrDiscardOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 66 | const InstallOperation& operation); |
| 67 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 68 | [[nodiscard]] virtual bool PerformSourceCopyOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 69 | const InstallOperation& operation, ErrorCode* error); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 70 | [[nodiscard]] virtual bool PerformSourceBsdiffOperation( |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 71 | const InstallOperation& operation, |
| 72 | ErrorCode* error, |
| 73 | const void* data, |
| 74 | size_t count); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 75 | [[nodiscard]] virtual bool PerformPuffDiffOperation( |
| 76 | const InstallOperation& operation, |
| 77 | ErrorCode* error, |
| 78 | const void* data, |
| 79 | size_t count); |
| 80 | [[nodiscard]] virtual bool Flush(); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 81 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 82 | protected: |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 83 | friend class PartitionWriterTest; |
| 84 | FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest); |
| 85 | |
| 86 | bool OpenCurrentECCPartition(); |
| 87 | // For a given operation, choose the source fd to be used (raw device or error |
| 88 | // correction device) based on the source operation hash. |
| 89 | // Returns nullptr if the source hash mismatch cannot be corrected, and set |
| 90 | // the |error| accordingly. |
| 91 | FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation, |
| 92 | ErrorCode* error); |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 93 | [[nodiscard]] virtual std::unique_ptr<ExtentWriter> CreateBaseExtentWriter(); |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 94 | |
| 95 | const PartitionUpdate& partition_update_; |
| 96 | const InstallPlan::Partition& install_part_; |
| 97 | DynamicPartitionControlInterface* dynamic_control_; |
| 98 | // Path to source partition |
| 99 | std::string source_path_; |
| 100 | // Path to target partition |
| 101 | std::string target_path_; |
| 102 | FileDescriptorPtr source_fd_; |
| 103 | FileDescriptorPtr target_fd_; |
| 104 | const bool interactive_; |
| 105 | const size_t block_size_; |
| 106 | // File descriptor of the error corrected source partition. Only set while |
| 107 | // updating partition using a delta payload for a partition where error |
| 108 | // correction is available. The size of the error corrected device is smaller |
| 109 | // than the underlying raw device, since it doesn't include the error |
| 110 | // correction blocks. |
| 111 | FileDescriptorPtr source_ecc_fd_{nullptr}; |
| 112 | |
| 113 | // The total number of operations that failed source hash verification but |
| 114 | // passed after falling back to the error-corrected |source_ecc_fd_| device. |
| 115 | uint64_t source_ecc_recovered_failures_{0}; |
| 116 | |
| 117 | // Whether opening the current partition as an error-corrected device failed. |
| 118 | // Used to avoid re-opening the same source partition if it is not actually |
| 119 | // error corrected. |
| 120 | bool source_ecc_open_failure_{false}; |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame^] | 121 | |
| 122 | PrefsInterface* prefs_; |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 123 | }; |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 124 | |
| 125 | namespace partition_writer { |
| 126 | // Return a PartitionWriter instance for perform InstallOps on this partition. |
| 127 | // Uses VABCPartitionWriter for Virtual AB Compression |
| 128 | std::unique_ptr<PartitionWriter> CreatePartitionWriter( |
| 129 | const PartitionUpdate& partition_update, |
| 130 | const InstallPlan::Partition& install_part, |
| 131 | DynamicPartitionControlInterface* dynamic_control, |
| 132 | size_t block_size, |
Kelvin Zhang | 24599af | 2020-10-27 13:44:25 -0400 | [diff] [blame^] | 133 | PrefsInterface* prefs, |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 134 | bool is_interactive, |
| 135 | bool is_dynamic_partition); |
| 136 | } // namespace partition_writer |
Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 137 | } // namespace chromeos_update_engine |
| 138 | |
| 139 | #endif |