blob: d808dd547b92ec9d5aa5ebc8ad8f49a77561fd11 [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#include "update_engine/payload_consumer/verity_writer_android.h"
18
19#include <fcntl.h>
20
21#include <algorithm>
22#include <memory>
Daniel Zhengbd16b012022-09-28 23:40:05 +000023#include <utility>
Sen Jiang57f91802017-11-14 17:42:13 -080024
25#include <base/logging.h>
26#include <base/posix/eintr_wrapper.h>
Sen Jianga778e5b2018-09-13 11:42:56 -070027#include <fec/ecc.h>
28extern "C" {
29#include <fec.h>
30}
Sen Jiang57f91802017-11-14 17:42:13 -080031
32#include "update_engine/common/utils.h"
Kelvin Zhang7f925672021-03-15 13:37:40 -040033#include "update_engine/payload_consumer/cached_file_descriptor.h"
Kelvin Zhangb138ab52020-11-06 15:56:41 -050034#include "update_engine/payload_consumer/file_descriptor.h"
Sen Jiang57f91802017-11-14 17:42:13 -080035
36namespace chromeos_update_engine {
37
Daniel Zhengbd16b012022-09-28 23:40:05 +000038bool IncrementalEncodeFEC::Init(const uint64_t _data_offset,
39 const uint64_t _data_size,
40 const uint64_t _fec_offset,
41 const uint64_t _fec_size,
42 const uint64_t _fec_roots,
43 const uint64_t _block_size,
44 const bool _verify_mode) {
45 current_step_ = EncodeFECStep::kInitFDStep;
46 data_offset_ = _data_offset;
47 data_size_ = _data_size;
48 fec_offset_ = _fec_offset;
49 fec_size_ = _fec_size;
50 fec_roots_ = _fec_roots;
51 block_size_ = _block_size;
52 verify_mode_ = _verify_mode;
53 current_round_ = 0;
54 // This is the N in RS(M, N), which is the number of bytes for each rs block.
55 rs_n_ = FEC_RSM - fec_roots_;
56 rs_char_.reset(init_rs_char(FEC_PARAMS(fec_roots_)));
57 rs_blocks_.resize(block_size_ * rs_n_);
58 buffer_.resize(block_size_, 0);
59 fec_.resize(block_size_ * fec_roots_);
60 fec_read_.resize(fec_.size());
61 TEST_AND_RETURN_FALSE(data_size_ % block_size_ == 0);
62 TEST_AND_RETURN_FALSE(fec_roots_ >= 0 && fec_roots_ < FEC_RSM);
63
64 num_rounds_ = utils::DivRoundUp(data_size_ / block_size_, rs_n_);
65 TEST_AND_RETURN_FALSE(num_rounds_ * fec_roots_ * block_size_ == fec_size_);
66 TEST_AND_RETURN_FALSE(rs_char_ != nullptr);
67 return true;
68}
69
70bool IncrementalEncodeFEC::Compute(FileDescriptor* _read_fd,
71 FileDescriptor* _write_fd) {
72 if (current_step_ == EncodeFECStep::kInitFDStep) {
73 read_fd_ = _read_fd;
74 write_fd_ = _write_fd;
75 cache_fd_.SetFD(write_fd_);
76 write_fd_ = &cache_fd_;
77 } else if (current_step_ == EncodeFECStep::kEncodeRoundStep) {
78 // Encodes |block_size| number of rs blocks each round so that we can read
79 // one block each time instead of 1 byte to increase random read
80 // performance. This uses about 1 MiB memory for 4K block size.
Daniel Zhenge0a95432024-08-29 23:07:02 +000081 for (uint64_t j = 0; j < rs_n_; j++) {
Daniel Zhengbd16b012022-09-28 23:40:05 +000082 uint64_t offset = fec_ecc_interleave(
83 current_round_ * rs_n_ * block_size_ + j, rs_n_, num_rounds_);
84 // Don't read past |data_size|, treat them as 0.
85 if (offset >= data_size_) {
86 std::fill(buffer_.begin(), buffer_.end(), 0);
87 } else {
88 ssize_t bytes_read = 0;
89 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd_,
90 buffer_.data(),
91 buffer_.size(),
92 data_offset_ + offset,
93 &bytes_read));
94 TEST_AND_RETURN_FALSE(bytes_read >= 0);
95 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) ==
96 buffer_.size());
97 }
Daniel Zhenge0a95432024-08-29 23:07:02 +000098 for (uint64_t k = 0; k < buffer_.size(); k++) {
Daniel Zhengbd16b012022-09-28 23:40:05 +000099 rs_blocks_[k * rs_n_ + j] = buffer_[k];
100 }
101 }
Daniel Zhenge0a95432024-08-29 23:07:02 +0000102 for (uint64_t j = 0; j < block_size_; j++) {
Daniel Zhengbd16b012022-09-28 23:40:05 +0000103 // Encode [j * rs_n_ : (j + 1) * rs_n_) in |rs_blocks| and write
104 // |fec_roots| number of parity bytes to |j * fec_roots| in |fec|.
105 encode_rs_char(rs_char_.get(),
106 rs_blocks_.data() + j * rs_n_,
107 fec_.data() + j * fec_roots_);
108 }
109
110 if (verify_mode_) {
111 ssize_t bytes_read = 0;
112 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd_,
113 fec_read_.data(),
114 fec_read_.size(),
115 fec_offset_,
116 &bytes_read));
117 TEST_AND_RETURN_FALSE(bytes_read >= 0);
118 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) ==
119 fec_read_.size());
120 TEST_AND_RETURN_FALSE(fec_ == fec_read_);
121 } else {
122 CHECK(write_fd_);
123 write_fd_->Seek(fec_offset_, SEEK_SET);
124 if (!utils::WriteAll(write_fd_, fec_.data(), fec_.size())) {
125 PLOG(ERROR) << "EncodeFEC write() failed";
126 return false;
127 }
128 }
129 fec_offset_ += fec_.size();
130 current_round_++;
131 } else if (current_step_ == EncodeFECStep::kWriteStep) {
132 write_fd_->Flush();
133 }
134 UpdateState();
135 return true;
136}
137// update the current state of EncodeFEC. Can be changed to have smaller steps
138void IncrementalEncodeFEC::UpdateState() {
139 if (current_step_ == EncodeFECStep::kInitFDStep) {
140 current_step_ = EncodeFECStep::kEncodeRoundStep;
141 } else if (current_step_ == EncodeFECStep::kEncodeRoundStep &&
142 current_round_ == num_rounds_) {
143 current_step_ = EncodeFECStep::kWriteStep;
144 } else if (current_step_ == EncodeFECStep::kWriteStep) {
145 current_step_ = EncodeFECStep::kComplete;
146 }
147}
148
149bool IncrementalEncodeFEC::Finished() const {
150 return current_step_ == EncodeFECStep::kComplete;
151}
152
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000153double IncrementalEncodeFEC::ReportProgress() const {
Daniel Zheng701cb6f2023-03-02 13:12:31 -0800154 if (num_rounds_ == 0) {
155 return 1.0;
156 }
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000157 return static_cast<double>(current_round_) / num_rounds_;
158}
159
Sen Jiang57f91802017-11-14 17:42:13 -0800160namespace verity_writer {
161std::unique_ptr<VerityWriterInterface> CreateVerityWriter() {
162 return std::make_unique<VerityWriterAndroid>();
163}
164} // namespace verity_writer
165
166bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) {
167 partition_ = &partition;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000168 LOG(INFO) << "Initializing Incremental EncodeFEC";
169 TEST_AND_RETURN_FALSE(encodeFEC_.Init(partition_->fec_data_offset,
170 partition_->fec_data_size,
171 partition_->fec_offset,
172 partition_->fec_size,
173 partition_->fec_roots,
174 partition_->block_size,
175 false /* verify_mode */));
176 hash_tree_written_ = false;
Sen Jiang57f91802017-11-14 17:42:13 -0800177 if (partition_->hash_tree_size != 0) {
178 auto hash_function =
179 HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm);
180 if (hash_function == nullptr) {
181 LOG(ERROR) << "Verity hash algorithm not supported: "
182 << partition_->hash_tree_algorithm;
183 return false;
184 }
185 hash_tree_builder_ = std::make_unique<HashTreeBuilder>(
186 partition_->block_size, hash_function);
187 TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize(
188 partition_->hash_tree_data_size, partition_->hash_tree_salt));
189 if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) !=
190 partition_->hash_tree_size) {
191 LOG(ERROR) << "Verity hash tree size does not match, stored: "
192 << partition_->hash_tree_size << ", calculated: "
193 << hash_tree_builder_->CalculateSize(
194 partition_->hash_tree_data_size);
195 return false;
196 }
197 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400198 total_offset_ = 0;
Sen Jiang57f91802017-11-14 17:42:13 -0800199 return true;
200}
201
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400202bool VerityWriterAndroid::Update(const uint64_t offset,
Sen Jiang57f91802017-11-14 17:42:13 -0800203 const uint8_t* buffer,
204 size_t size) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400205 if (offset != total_offset_) {
206 LOG(ERROR) << "Sequential read expected, expected to read at: "
207 << total_offset_ << " actual read occurs at: " << offset;
208 return false;
209 }
Sen Jiang57f91802017-11-14 17:42:13 -0800210 if (partition_->hash_tree_size != 0) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400211 const uint64_t hash_tree_data_end =
Sen Jiang57f91802017-11-14 17:42:13 -0800212 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400213 const uint64_t start_offset =
214 std::max(offset, partition_->hash_tree_data_offset);
215 if (offset + size > hash_tree_data_end) {
216 LOG(WARNING)
217 << "Reading past hash_tree_data_end, something is probably "
218 "wrong, might cause incorrect hash of partitions. offset: "
219 << offset << " size: " << size
220 << " hash_tree_data_end: " << hash_tree_data_end;
221 }
222 const uint64_t end_offset = std::min(offset + size, hash_tree_data_end);
Sen Jiang57f91802017-11-14 17:42:13 -0800223 if (start_offset < end_offset) {
224 TEST_AND_RETURN_FALSE(hash_tree_builder_->Update(
225 buffer + start_offset - offset, end_offset - start_offset));
226
227 if (end_offset == hash_tree_data_end) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400228 LOG(INFO)
229 << "Read everything before hash tree. Ready to write hash tree.";
Sen Jiang57f91802017-11-14 17:42:13 -0800230 }
231 }
232 }
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400233 total_offset_ += size;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400234
235 return true;
236}
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800237bool VerityWriterAndroid::Finalize(FileDescriptor* read_fd,
238 FileDescriptor* write_fd) {
Kelvin Zhangeeec3812021-03-16 14:01:47 -0400239 const auto hash_tree_data_end =
240 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
241 if (total_offset_ < hash_tree_data_end) {
242 LOG(ERROR) << "Read up to " << total_offset_
243 << " when we are expecting to read everything "
244 "before "
245 << hash_tree_data_end;
246 return false;
247 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400248 // All hash tree data blocks has been hashed, write hash tree to disk.
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700249 LOG(INFO) << "Writing verity hash tree to "
250 << partition_->readonly_target_path;
Kelvin Zhange4bffe62022-03-07 09:08:38 -0800251 if (hash_tree_builder_) {
252 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
253 TEST_AND_RETURN_FALSE_ERRNO(
254 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
255 auto success =
256 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
257 return utils::WriteAll(write_fd, data, size);
258 });
259 // hashtree builder already prints error messages.
260 TEST_AND_RETURN_FALSE(success);
261 hash_tree_builder_.reset();
262 }
Sen Jiang57f91802017-11-14 17:42:13 -0800263 if (partition_->fec_size != 0) {
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700264 LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400265 TEST_AND_RETURN_FALSE(EncodeFEC(read_fd,
266 write_fd,
267 partition_->fec_data_offset,
268 partition_->fec_data_size,
269 partition_->fec_offset,
270 partition_->fec_size,
271 partition_->fec_roots,
272 partition_->block_size,
273 false /* verify_mode */));
Sen Jiang57f91802017-11-14 17:42:13 -0800274 }
275 return true;
276}
277
Daniel Zhengbd16b012022-09-28 23:40:05 +0000278bool VerityWriterAndroid::IncrementalFinalize(FileDescriptor* read_fd,
279 FileDescriptor* write_fd) {
280 if (!hash_tree_written_) {
281 LOG(INFO) << "Completing prework in Finalize";
282 const auto hash_tree_data_end =
283 partition_->hash_tree_data_offset + partition_->hash_tree_data_size;
284 if (total_offset_ < hash_tree_data_end) {
285 LOG(ERROR) << "Read up to " << total_offset_
286 << " when we are expecting to read everything "
287 "before "
288 << hash_tree_data_end;
289 return false;
290 }
291 // All hash tree data blocks has been hashed, write hash tree to disk.
292 LOG(INFO) << "Writing verity hash tree to "
293 << partition_->readonly_target_path;
294 if (hash_tree_builder_) {
295 TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree());
296 TEST_AND_RETURN_FALSE_ERRNO(
297 write_fd->Seek(partition_->hash_tree_offset, SEEK_SET));
298 auto success =
299 hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) {
300 return utils::WriteAll(write_fd, data, size);
301 });
302 // hashtree builder already prints error messages.
303 TEST_AND_RETURN_FALSE(success);
304 hash_tree_builder_.reset();
305 }
306 hash_tree_written_ = true;
307 if (partition_->fec_size != 0) {
308 LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path;
309 }
310 }
311 if (partition_->fec_size != 0) {
312 TEST_AND_RETURN_FALSE(encodeFEC_.Compute(read_fd, write_fd));
313 }
314 return true;
315}
316bool VerityWriterAndroid::FECFinished() const {
Daniel Zheng701cb6f2023-03-02 13:12:31 -0800317 if ((encodeFEC_.Finished() || partition_->fec_size == 0) &&
318 hash_tree_written_) {
Daniel Zhengbd16b012022-09-28 23:40:05 +0000319 return true;
320 }
321 return false;
322}
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000323
324double VerityWriterAndroid::GetProgress() {
325 return encodeFEC_.ReportProgress();
326}
327
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800328bool VerityWriterAndroid::EncodeFEC(FileDescriptor* read_fd,
329 FileDescriptor* write_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700330 uint64_t data_offset,
331 uint64_t data_size,
332 uint64_t fec_offset,
333 uint64_t fec_size,
334 uint32_t fec_roots,
335 uint32_t block_size,
336 bool verify_mode) {
337 TEST_AND_RETURN_FALSE(data_size % block_size == 0);
338 TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM);
Daniel Zhengbd16b012022-09-28 23:40:05 +0000339 // This is the N in RS(M, N), which is the number of bytes for each rs
340 // block.
Sen Jianga778e5b2018-09-13 11:42:56 -0700341 size_t rs_n = FEC_RSM - fec_roots;
342 uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n);
343 TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size);
344
345 std::unique_ptr<void, decltype(&free_rs_char)> rs_char(
346 init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char);
347 TEST_AND_RETURN_FALSE(rs_char != nullptr);
Kelvin Zhang9792f082022-01-26 16:48:24 -0800348 // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we
349 // perform a read() operation after write(). So reduce the number of writes
350 // can save unnecessary re-opens.
351 UnownedCachedFileDescriptor cache_fd(write_fd, 1 * (1 << 20));
352 write_fd = &cache_fd;
Sen Jianga778e5b2018-09-13 11:42:56 -0700353
Sen Jianga778e5b2018-09-13 11:42:56 -0700354 for (size_t i = 0; i < rounds; i++) {
355 // Encodes |block_size| number of rs blocks each round so that we can read
356 // one block each time instead of 1 byte to increase random read
357 // performance. This uses about 1 MiB memory for 4K block size.
358 brillo::Blob rs_blocks(block_size * rs_n);
359 for (size_t j = 0; j < rs_n; j++) {
360 brillo::Blob buffer(block_size, 0);
361 uint64_t offset =
362 fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds);
363 // Don't read past |data_size|, treat them as 0.
364 if (offset < data_size) {
365 ssize_t bytes_read = 0;
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500366 TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd,
Sen Jianga778e5b2018-09-13 11:42:56 -0700367 buffer.data(),
368 buffer.size(),
369 data_offset + offset,
370 &bytes_read));
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500371 TEST_AND_RETURN_FALSE(bytes_read >= 0);
372 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700373 }
374 for (size_t k = 0; k < buffer.size(); k++) {
375 rs_blocks[k * rs_n + j] = buffer[k];
376 }
377 }
378 brillo::Blob fec(block_size * fec_roots);
379 for (size_t j = 0; j < block_size; j++) {
Daniel Zhengbd16b012022-09-28 23:40:05 +0000380 // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write
381 // |fec_roots| number of parity bytes to |j * fec_roots| in |fec|.
Sen Jianga778e5b2018-09-13 11:42:56 -0700382 encode_rs_char(rs_char.get(),
383 rs_blocks.data() + j * rs_n,
384 fec.data() + j * fec_roots);
385 }
386
387 if (verify_mode) {
388 brillo::Blob fec_read(fec.size());
389 ssize_t bytes_read = 0;
390 TEST_AND_RETURN_FALSE(utils::PReadAll(
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500391 read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read));
392 TEST_AND_RETURN_FALSE(bytes_read >= 0);
393 TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size());
Sen Jianga778e5b2018-09-13 11:42:56 -0700394 TEST_AND_RETURN_FALSE(fec == fec_read);
395 } else {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500396 CHECK(write_fd);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400397 write_fd->Seek(fec_offset, SEEK_SET);
398 if (!utils::WriteAll(write_fd, fec.data(), fec.size())) {
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500399 PLOG(ERROR) << "EncodeFEC write() failed";
400 return false;
401 }
Sen Jianga778e5b2018-09-13 11:42:56 -0700402 }
403 fec_offset += fec.size();
404 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400405 write_fd->Flush();
Sen Jianga778e5b2018-09-13 11:42:56 -0700406 return true;
407}
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500408
409bool VerityWriterAndroid::EncodeFEC(const std::string& path,
410 uint64_t data_offset,
411 uint64_t data_size,
412 uint64_t fec_offset,
413 uint64_t fec_size,
414 uint32_t fec_roots,
415 uint32_t block_size,
416 bool verify_mode) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800417 EintrSafeFileDescriptor fd;
418 TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR));
419 return EncodeFEC(&fd,
420 &fd,
Kelvin Zhangb138ab52020-11-06 15:56:41 -0500421 data_offset,
422 data_size,
423 fec_offset,
424 fec_size,
425 fec_roots,
426 block_size,
427 verify_mode);
428}
Sen Jiang57f91802017-11-14 17:42:13 -0800429} // namespace chromeos_update_engine