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