blob: a6a49209cc1ad050961b24e3a55026b198c05c5d [file] [log] [blame]
Sen Jiang57f91802017-11-14 17:42:13 -08001//
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 Jianga778e5b2018-09-13 11:42:56 -070021#include <string>
Sen Jiang57f91802017-11-14 17:42:13 -080022
23#include <verity/hash_tree_builder.h>
24
Kelvin Zhangb138ab52020-11-06 15:56:41 -050025#include "payload_consumer/file_descriptor.h"
Sen Jiang57f91802017-11-14 17:42:13 -080026#include "update_engine/payload_consumer/verity_writer_interface.h"
27
28namespace chromeos_update_engine {
29
30class VerityWriterAndroid : public VerityWriterInterface {
31 public:
32 VerityWriterAndroid() = default;
33 ~VerityWriterAndroid() override = default;
34
Kelvin Zhangb138ab52020-11-06 15:56:41 -050035 bool Init(const InstallPlan::Partition& partition);
Sen Jiang57f91802017-11-14 17:42:13 -080036 bool Update(uint64_t offset, const uint8_t* buffer, size_t size) override;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -080037 bool Finalize(FileDescriptor* read_fd, FileDescriptor* write_fd) override;
Sen Jiang57f91802017-11-14 17:42:13 -080038
Sen Jianga778e5b2018-09-13 11:42:56 -070039 // 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 Zhang1a0ed712022-01-26 16:09:05 -080045 static bool EncodeFEC(FileDescriptor* read_fd,
46 FileDescriptor* write_fd,
Kelvin Zhangb138ab52020-11-06 15:56:41 -050047 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 Jianga778e5b2018-09-13 11:42:56 -070054 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 Jiang57f91802017-11-14 17:42:13 -080063 private:
64 const InstallPlan::Partition* partition_ = nullptr;
65
Sen Jiang57f91802017-11-14 17:42:13 -080066 std::unique_ptr<HashTreeBuilder> hash_tree_builder_;
Kelvin Zhangeeec3812021-03-16 14:01:47 -040067 uint64_t total_offset_ = 0;
Sen Jiang57f91802017-11-14 17:42:13 -080068 DISALLOW_COPY_AND_ASSIGN(VerityWriterAndroid);
69};
70
71} // namespace chromeos_update_engine
72
73#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_VERITY_WRITER_ANDROID_H_