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>
21 #include <string>
22 
23 #include <verity/hash_tree_builder.h>
24 
25 #include "payload_consumer/file_descriptor.h"
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 
35   bool Init(const InstallPlan::Partition& partition);
36   bool Update(uint64_t offset, const uint8_t* buffer, size_t size) override;
37   bool Finalize(FileDescriptorPtr read_fd, FileDescriptorPtr write_fd) override;
38 
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.
45   static bool EncodeFEC(FileDescriptorPtr read_fd,
46                         FileDescriptorPtr write_fd,
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);
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 
63  private:
64   const InstallPlan::Partition* partition_ = nullptr;
65 
66   std::unique_ptr<HashTreeBuilder> hash_tree_builder_;
67   uint64_t total_offset_ = 0;
68   DISALLOW_COPY_AND_ASSIGN(VerityWriterAndroid);
69 };
70 
71 }  // namespace chromeos_update_engine
72 
73 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_
74