| 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" | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 32 | #include "update_engine/payload_consumer/partition_writer_interface.h" | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/verified_source_fd.h" | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 34 | #include "update_engine/update_metadata.pb.h" | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 35 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 36 | namespace chromeos_update_engine { | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 37 | class PartitionWriter : public PartitionWriterInterface { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 38 | public: | 
|  | 39 | PartitionWriter(const PartitionUpdate& partition_update, | 
|  | 40 | const InstallPlan::Partition& install_part, | 
|  | 41 | DynamicPartitionControlInterface* dynamic_control, | 
|  | 42 | size_t block_size, | 
|  | 43 | bool is_interactive); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 44 | ~PartitionWriter(); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 45 | static bool ValidateSourceHash(const brillo::Blob& calculated_hash, | 
|  | 46 | const InstallOperation& operation, | 
|  | 47 | const FileDescriptorPtr source_fd, | 
|  | 48 | ErrorCode* error); | 
| Kelvin Zhang | b5bd075 | 2022-03-10 15:45:25 -0800 | [diff] [blame] | 49 | static bool ValidateSourceHash(const InstallOperation& operation, | 
|  | 50 | const FileDescriptorPtr source_fd, | 
|  | 51 | size_t block_size, | 
|  | 52 | ErrorCode* error); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 53 |  | 
|  | 54 | // Perform necessary initialization work before InstallOperation can be | 
|  | 55 | // applied to this partition | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 56 | [[nodiscard]] bool Init(const InstallPlan* install_plan, | 
|  | 57 | bool source_may_exist, | 
|  | 58 | size_t next_op_index) override; | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 59 |  | 
|  | 60 | // |CheckpointUpdateProgress| will be called after SetNextOpIndex(), but it's | 
|  | 61 | // optional. DeltaPerformer may or may not call this everytime an operation is | 
|  | 62 | // applied. | 
|  | 63 | //   |next_op_index| is index of next operation that should be applied. | 
|  | 64 | // |next_op_index-1| is the last operation that is already applied. | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 65 | void CheckpointUpdateProgress(size_t next_op_index) override; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 66 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 67 | // Close partition writer, when calling this function there's no guarantee | 
|  | 68 | // that all |InstallOperations| are sent to |PartitionWriter|. This function | 
|  | 69 | // will be called even if we are pausing/aborting the update. | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 70 | int Close() override; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 71 |  | 
|  | 72 | // These perform a specific type of operation and return true on success. | 
|  | 73 | // |error| will be set if source hash mismatch, otherwise |error| might not be | 
|  | 74 | // set even if it fails. | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 75 | [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation, | 
|  | 76 | const void* data, | 
|  | 77 | size_t count) override; | 
|  | 78 | [[nodiscard]] bool PerformZeroOrDiscardOperation( | 
|  | 79 | const InstallOperation& operation) override; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 80 |  | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 81 | [[nodiscard]] bool PerformSourceCopyOperation( | 
|  | 82 | const InstallOperation& operation, ErrorCode* error) override; | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 83 | [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation, | 
|  | 84 | ErrorCode* error, | 
|  | 85 | const void* data, | 
|  | 86 | size_t count) override; | 
| 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. | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 91 | [[nodiscard]] bool FinishedInstallOps() override { return true; } | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 92 |  | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 93 | private: | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 94 | friend class PartitionWriterTest; | 
|  | 95 | FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest); | 
|  | 96 |  | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 97 | [[nodiscard]] bool OpenSourcePartition(uint32_t source_slot, | 
|  | 98 | bool source_may_exist); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 99 | FileDescriptorPtr ChooseSourceFD(const InstallOperation& op, | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 100 | ErrorCode* error); | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 101 |  | 
|  | 102 | [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter(); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 103 |  | 
|  | 104 | const PartitionUpdate& partition_update_; | 
|  | 105 | const InstallPlan::Partition& install_part_; | 
|  | 106 | DynamicPartitionControlInterface* dynamic_control_; | 
|  | 107 | // Path to source partition | 
|  | 108 | std::string source_path_; | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 109 | VerifiedSourceFd verified_source_fd_; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 110 | // Path to target partition | 
|  | 111 | std::string target_path_; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 112 | FileDescriptorPtr target_fd_; | 
|  | 113 | const bool interactive_; | 
|  | 114 | const size_t block_size_; | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 115 |  | 
|  | 116 | // This instance handles decompression/bsdfif/puffdiff. It's responsible for | 
|  | 117 | // constructing data which should be written to target partition, actual | 
|  | 118 | // "writing" is handled by |PartitionWriter| | 
|  | 119 | InstallOperationExecutor install_op_executor_; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 120 | }; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 121 |  | 
|  | 122 | namespace partition_writer { | 
|  | 123 | // Return a PartitionWriter instance for perform InstallOps on this partition. | 
|  | 124 | // Uses VABCPartitionWriter for Virtual AB Compression | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 125 | std::unique_ptr<PartitionWriterInterface> CreatePartitionWriter( | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 126 | const PartitionUpdate& partition_update, | 
|  | 127 | const InstallPlan::Partition& install_part, | 
|  | 128 | DynamicPartitionControlInterface* dynamic_control, | 
|  | 129 | size_t block_size, | 
|  | 130 | bool is_interactive, | 
|  | 131 | bool is_dynamic_partition); | 
|  | 132 | }  // namespace partition_writer | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 133 | }  // namespace chromeos_update_engine | 
|  | 134 |  | 
|  | 135 | #endif |