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> |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 24 | #include <base/logging.h> |
| 25 | #include <base/posix/eintr_wrapper.h> |
| 26 | #include <fec/ecc.h> |
| 27 | extern "C" { |
| 28 | #include <fec.h> |
| 29 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 30 | |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 31 | #include "payload_consumer/file_descriptor.h" |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 32 | #include "update_engine/payload_consumer/cached_file_descriptor.h" |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/verity_writer_interface.h" |
| 34 | |
| 35 | namespace chromeos_update_engine { |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 36 | enum class EncodeFECStep { |
| 37 | kInitFDStep, |
| 38 | kEncodeRoundStep, |
| 39 | kWriteStep, |
| 40 | kComplete |
| 41 | }; |
| 42 | class IncrementalEncodeFEC { |
| 43 | public: |
| 44 | IncrementalEncodeFEC() |
| 45 | : rs_char_(nullptr, &free_rs_char), cache_fd_(nullptr, 1 * (1 << 20)) {} |
| 46 | // Initialize all member variables needed to performe FEC Computation |
| 47 | bool Init(const uint64_t _data_offset, |
| 48 | const uint64_t _data_size, |
| 49 | const uint64_t _fec_offset, |
| 50 | const uint64_t _fec_size, |
| 51 | const uint64_t _fec_roots, |
| 52 | const uint64_t _block_size, |
| 53 | const bool _verify_mode); |
| 54 | bool Compute(FileDescriptor* _read_fd, FileDescriptor* _write_fd); |
| 55 | void UpdateState(); |
| 56 | bool Finished() const; |
| 57 | void Reset(); |
| 58 | |
| 59 | private: |
| 60 | brillo::Blob rs_blocks_; |
| 61 | brillo::Blob buffer_; |
| 62 | brillo::Blob fec_; |
| 63 | brillo::Blob fec_read_; |
| 64 | EncodeFECStep current_step_; |
| 65 | size_t current_round_; |
| 66 | size_t num_rounds_; |
| 67 | FileDescriptor* read_fd_; |
| 68 | FileDescriptor* write_fd_; |
| 69 | uint64_t data_offset_; |
| 70 | uint64_t data_size_; |
| 71 | uint64_t fec_offset_; |
| 72 | uint64_t fec_size_; |
| 73 | uint64_t fec_roots_; |
| 74 | uint64_t block_size_; |
| 75 | size_t rs_n_; |
| 76 | bool verify_mode_; |
| 77 | std::unique_ptr<void, decltype(&free_rs_char)> rs_char_; |
| 78 | UnownedCachedFileDescriptor cache_fd_; |
| 79 | }; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 80 | |
| 81 | class VerityWriterAndroid : public VerityWriterInterface { |
| 82 | public: |
| 83 | VerityWriterAndroid() = default; |
| 84 | ~VerityWriterAndroid() override = default; |
| 85 | |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 86 | bool Init(const InstallPlan::Partition& partition); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 87 | 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] | 88 | bool Finalize(FileDescriptor* read_fd, FileDescriptor* write_fd) override; |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 89 | bool IncrementalFinalize(FileDescriptor* read_fd, |
| 90 | FileDescriptor* write_fd) override; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 91 | |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 92 | bool FECFinished() const override; |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 93 | // Read [data_offset : data_offset + data_size) from |path| and encode FEC |
| 94 | // data, if |verify_mode|, then compare the encoded FEC with the one in |
| 95 | // |path|, otherwise write the encoded FEC to |path|. We can't encode as we go |
| 96 | // in each Update() like hash tree, because for every rs block, its data are |
| 97 | // spreaded across entire |data_size|, unless we can cache all data in |
| 98 | // memory, we have to re-read them from disk. |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 99 | static bool EncodeFEC(FileDescriptor* read_fd, |
| 100 | FileDescriptor* write_fd, |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 101 | uint64_t data_offset, |
| 102 | uint64_t data_size, |
| 103 | uint64_t fec_offset, |
| 104 | uint64_t fec_size, |
| 105 | uint32_t fec_roots, |
| 106 | uint32_t block_size, |
| 107 | bool verify_mode); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 108 | static bool EncodeFEC(const std::string& path, |
| 109 | uint64_t data_offset, |
| 110 | uint64_t data_size, |
| 111 | uint64_t fec_offset, |
| 112 | uint64_t fec_size, |
| 113 | uint32_t fec_roots, |
| 114 | uint32_t block_size, |
| 115 | bool verify_mode); |
| 116 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 117 | private: |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 118 | // stores the state of EncodeFEC |
| 119 | IncrementalEncodeFEC encodeFEC_; |
| 120 | bool hash_tree_written_ = false; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 121 | const InstallPlan::Partition* partition_ = nullptr; |
| 122 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 123 | std::unique_ptr<HashTreeBuilder> hash_tree_builder_; |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 124 | uint64_t total_offset_ = 0; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(VerityWriterAndroid); |
| 126 | }; |
| 127 | |
| 128 | } // namespace chromeos_update_engine |
| 129 | |
| 130 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_ |