Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2018 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_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_ |
| 19 | |
| 20 | #include <memory> |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 21 | #include <string> |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 22 | |
| 23 | #include <verity/hash_tree_builder.h> |
| 24 | |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 25 | #include "payload_consumer/file_descriptor.h" |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 26 | #include "update_engine/payload_consumer/verity_writer_interface.h" |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | class VerityWriterAndroid : public VerityWriterInterface { |
| 31 | public: |
| 32 | VerityWriterAndroid() = default; |
| 33 | ~VerityWriterAndroid() override = default; |
| 34 | |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 35 | bool Init(const InstallPlan::Partition& partition); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 36 | bool Update(uint64_t offset, const uint8_t* buffer, size_t size) override; |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 37 | bool Finalize(FileDescriptor* read_fd, FileDescriptor* write_fd) override; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 38 | |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 39 | // Read [data_offset : data_offset + data_size) from |path| and encode FEC |
| 40 | // data, if |verify_mode|, then compare the encoded FEC with the one in |
| 41 | // |path|, otherwise write the encoded FEC to |path|. We can't encode as we go |
| 42 | // in each Update() like hash tree, because for every rs block, its data are |
| 43 | // spreaded across entire |data_size|, unless we can cache all data in |
| 44 | // memory, we have to re-read them from disk. |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 45 | static bool EncodeFEC(FileDescriptor* read_fd, |
| 46 | FileDescriptor* write_fd, |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 47 | uint64_t data_offset, |
| 48 | uint64_t data_size, |
| 49 | uint64_t fec_offset, |
| 50 | uint64_t fec_size, |
| 51 | uint32_t fec_roots, |
| 52 | uint32_t block_size, |
| 53 | bool verify_mode); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 54 | static bool EncodeFEC(const std::string& path, |
| 55 | uint64_t data_offset, |
| 56 | uint64_t data_size, |
| 57 | uint64_t fec_offset, |
| 58 | uint64_t fec_size, |
| 59 | uint32_t fec_roots, |
| 60 | uint32_t block_size, |
| 61 | bool verify_mode); |
| 62 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 63 | private: |
| 64 | const InstallPlan::Partition* partition_ = nullptr; |
| 65 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 66 | std::unique_ptr<HashTreeBuilder> hash_tree_builder_; |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 67 | uint64_t total_offset_ = 0; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(VerityWriterAndroid); |
| 69 | }; |
| 70 | |
| 71 | } // namespace chromeos_update_engine |
| 72 | |
| 73 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_ |