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 | #include "update_engine/payload_consumer/verity_writer_android.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <memory> |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 23 | #include <utility> |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 24 | |
| 25 | #include <base/logging.h> |
| 26 | #include <base/posix/eintr_wrapper.h> |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 27 | #include <fec/ecc.h> |
| 28 | extern "C" { |
| 29 | #include <fec.h> |
| 30 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 31 | |
| 32 | #include "update_engine/common/utils.h" |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/cached_file_descriptor.h" |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/file_descriptor.h" |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 38 | bool 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 | |
| 70 | bool 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. |
| 81 | for (size_t j = 0; j < rs_n_; j++) { |
| 82 | 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 | } |
| 98 | for (size_t k = 0; k < buffer_.size(); k++) { |
| 99 | rs_blocks_[k * rs_n_ + j] = buffer_[k]; |
| 100 | } |
| 101 | } |
| 102 | for (size_t j = 0; j < block_size_; j++) { |
| 103 | // 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 |
| 138 | void 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 | |
| 149 | bool IncrementalEncodeFEC::Finished() const { |
| 150 | return current_step_ == EncodeFECStep::kComplete; |
| 151 | } |
| 152 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 153 | namespace verity_writer { |
| 154 | std::unique_ptr<VerityWriterInterface> CreateVerityWriter() { |
| 155 | return std::make_unique<VerityWriterAndroid>(); |
| 156 | } |
| 157 | } // namespace verity_writer |
| 158 | |
| 159 | bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) { |
| 160 | partition_ = &partition; |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 161 | LOG(INFO) << "Initializing Incremental EncodeFEC"; |
| 162 | TEST_AND_RETURN_FALSE(encodeFEC_.Init(partition_->fec_data_offset, |
| 163 | partition_->fec_data_size, |
| 164 | partition_->fec_offset, |
| 165 | partition_->fec_size, |
| 166 | partition_->fec_roots, |
| 167 | partition_->block_size, |
| 168 | false /* verify_mode */)); |
| 169 | hash_tree_written_ = false; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 170 | if (partition_->hash_tree_size != 0) { |
| 171 | auto hash_function = |
| 172 | HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm); |
| 173 | if (hash_function == nullptr) { |
| 174 | LOG(ERROR) << "Verity hash algorithm not supported: " |
| 175 | << partition_->hash_tree_algorithm; |
| 176 | return false; |
| 177 | } |
| 178 | hash_tree_builder_ = std::make_unique<HashTreeBuilder>( |
| 179 | partition_->block_size, hash_function); |
| 180 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize( |
| 181 | partition_->hash_tree_data_size, partition_->hash_tree_salt)); |
| 182 | if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) != |
| 183 | partition_->hash_tree_size) { |
| 184 | LOG(ERROR) << "Verity hash tree size does not match, stored: " |
| 185 | << partition_->hash_tree_size << ", calculated: " |
| 186 | << hash_tree_builder_->CalculateSize( |
| 187 | partition_->hash_tree_data_size); |
| 188 | return false; |
| 189 | } |
| 190 | } |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 191 | total_offset_ = 0; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 195 | bool VerityWriterAndroid::Update(const uint64_t offset, |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 196 | const uint8_t* buffer, |
| 197 | size_t size) { |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 198 | if (offset != total_offset_) { |
| 199 | LOG(ERROR) << "Sequential read expected, expected to read at: " |
| 200 | << total_offset_ << " actual read occurs at: " << offset; |
| 201 | return false; |
| 202 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 203 | if (partition_->hash_tree_size != 0) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 204 | const uint64_t hash_tree_data_end = |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 205 | partition_->hash_tree_data_offset + partition_->hash_tree_data_size; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 206 | const uint64_t start_offset = |
| 207 | std::max(offset, partition_->hash_tree_data_offset); |
| 208 | if (offset + size > hash_tree_data_end) { |
| 209 | LOG(WARNING) |
| 210 | << "Reading past hash_tree_data_end, something is probably " |
| 211 | "wrong, might cause incorrect hash of partitions. offset: " |
| 212 | << offset << " size: " << size |
| 213 | << " hash_tree_data_end: " << hash_tree_data_end; |
| 214 | } |
| 215 | const uint64_t end_offset = std::min(offset + size, hash_tree_data_end); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 216 | if (start_offset < end_offset) { |
| 217 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Update( |
| 218 | buffer + start_offset - offset, end_offset - start_offset)); |
| 219 | |
| 220 | if (end_offset == hash_tree_data_end) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 221 | LOG(INFO) |
| 222 | << "Read everything before hash tree. Ready to write hash tree."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 226 | total_offset_ += size; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 227 | |
| 228 | return true; |
| 229 | } |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 230 | bool VerityWriterAndroid::Finalize(FileDescriptor* read_fd, |
| 231 | FileDescriptor* write_fd) { |
Kelvin Zhang | eeec381 | 2021-03-16 14:01:47 -0400 | [diff] [blame] | 232 | const auto hash_tree_data_end = |
| 233 | partition_->hash_tree_data_offset + partition_->hash_tree_data_size; |
| 234 | if (total_offset_ < hash_tree_data_end) { |
| 235 | LOG(ERROR) << "Read up to " << total_offset_ |
| 236 | << " when we are expecting to read everything " |
| 237 | "before " |
| 238 | << hash_tree_data_end; |
| 239 | return false; |
| 240 | } |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 241 | // All hash tree data blocks has been hashed, write hash tree to disk. |
Kelvin Zhang | 91e839c | 2022-04-05 14:25:00 -0700 | [diff] [blame] | 242 | LOG(INFO) << "Writing verity hash tree to " |
| 243 | << partition_->readonly_target_path; |
Kelvin Zhang | e4bffe6 | 2022-03-07 09:08:38 -0800 | [diff] [blame] | 244 | if (hash_tree_builder_) { |
| 245 | TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree()); |
| 246 | TEST_AND_RETURN_FALSE_ERRNO( |
| 247 | write_fd->Seek(partition_->hash_tree_offset, SEEK_SET)); |
| 248 | auto success = |
| 249 | hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) { |
| 250 | return utils::WriteAll(write_fd, data, size); |
| 251 | }); |
| 252 | // hashtree builder already prints error messages. |
| 253 | TEST_AND_RETURN_FALSE(success); |
| 254 | hash_tree_builder_.reset(); |
| 255 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 256 | if (partition_->fec_size != 0) { |
Kelvin Zhang | 91e839c | 2022-04-05 14:25:00 -0700 | [diff] [blame] | 257 | LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 258 | TEST_AND_RETURN_FALSE(EncodeFEC(read_fd, |
| 259 | write_fd, |
| 260 | partition_->fec_data_offset, |
| 261 | partition_->fec_data_size, |
| 262 | partition_->fec_offset, |
| 263 | partition_->fec_size, |
| 264 | partition_->fec_roots, |
| 265 | partition_->block_size, |
| 266 | false /* verify_mode */)); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 271 | bool VerityWriterAndroid::IncrementalFinalize(FileDescriptor* read_fd, |
| 272 | FileDescriptor* write_fd) { |
| 273 | if (!hash_tree_written_) { |
| 274 | LOG(INFO) << "Completing prework in Finalize"; |
| 275 | const auto hash_tree_data_end = |
| 276 | partition_->hash_tree_data_offset + partition_->hash_tree_data_size; |
| 277 | if (total_offset_ < hash_tree_data_end) { |
| 278 | LOG(ERROR) << "Read up to " << total_offset_ |
| 279 | << " when we are expecting to read everything " |
| 280 | "before " |
| 281 | << hash_tree_data_end; |
| 282 | return false; |
| 283 | } |
| 284 | // All hash tree data blocks has been hashed, write hash tree to disk. |
| 285 | LOG(INFO) << "Writing verity hash tree to " |
| 286 | << partition_->readonly_target_path; |
| 287 | if (hash_tree_builder_) { |
| 288 | TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree()); |
| 289 | TEST_AND_RETURN_FALSE_ERRNO( |
| 290 | write_fd->Seek(partition_->hash_tree_offset, SEEK_SET)); |
| 291 | auto success = |
| 292 | hash_tree_builder_->WriteHashTree([write_fd](auto data, auto size) { |
| 293 | return utils::WriteAll(write_fd, data, size); |
| 294 | }); |
| 295 | // hashtree builder already prints error messages. |
| 296 | TEST_AND_RETURN_FALSE(success); |
| 297 | hash_tree_builder_.reset(); |
| 298 | } |
| 299 | hash_tree_written_ = true; |
| 300 | if (partition_->fec_size != 0) { |
| 301 | LOG(INFO) << "Writing verity FEC to " << partition_->readonly_target_path; |
| 302 | } |
| 303 | } |
| 304 | if (partition_->fec_size != 0) { |
| 305 | TEST_AND_RETURN_FALSE(encodeFEC_.Compute(read_fd, write_fd)); |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | bool VerityWriterAndroid::FECFinished() const { |
| 310 | if (encodeFEC_.Finished()) { |
| 311 | return true; |
| 312 | } |
| 313 | return false; |
| 314 | } |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 315 | bool VerityWriterAndroid::EncodeFEC(FileDescriptor* read_fd, |
| 316 | FileDescriptor* write_fd, |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 317 | uint64_t data_offset, |
| 318 | uint64_t data_size, |
| 319 | uint64_t fec_offset, |
| 320 | uint64_t fec_size, |
| 321 | uint32_t fec_roots, |
| 322 | uint32_t block_size, |
| 323 | bool verify_mode) { |
| 324 | TEST_AND_RETURN_FALSE(data_size % block_size == 0); |
| 325 | TEST_AND_RETURN_FALSE(fec_roots >= 0 && fec_roots < FEC_RSM); |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 326 | // This is the N in RS(M, N), which is the number of bytes for each rs |
| 327 | // block. |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 328 | size_t rs_n = FEC_RSM - fec_roots; |
| 329 | uint64_t rounds = utils::DivRoundUp(data_size / block_size, rs_n); |
| 330 | TEST_AND_RETURN_FALSE(rounds * fec_roots * block_size == fec_size); |
| 331 | |
| 332 | std::unique_ptr<void, decltype(&free_rs_char)> rs_char( |
| 333 | init_rs_char(FEC_PARAMS(fec_roots)), &free_rs_char); |
| 334 | TEST_AND_RETURN_FALSE(rs_char != nullptr); |
Kelvin Zhang | 9792f08 | 2022-01-26 16:48:24 -0800 | [diff] [blame] | 335 | // Cache at most 1MB of fec data, in VABC, we need to re-open fd if we |
| 336 | // perform a read() operation after write(). So reduce the number of writes |
| 337 | // can save unnecessary re-opens. |
| 338 | UnownedCachedFileDescriptor cache_fd(write_fd, 1 * (1 << 20)); |
| 339 | write_fd = &cache_fd; |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 340 | |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 341 | for (size_t i = 0; i < rounds; i++) { |
| 342 | // Encodes |block_size| number of rs blocks each round so that we can read |
| 343 | // one block each time instead of 1 byte to increase random read |
| 344 | // performance. This uses about 1 MiB memory for 4K block size. |
| 345 | brillo::Blob rs_blocks(block_size * rs_n); |
| 346 | for (size_t j = 0; j < rs_n; j++) { |
| 347 | brillo::Blob buffer(block_size, 0); |
| 348 | uint64_t offset = |
| 349 | fec_ecc_interleave(i * rs_n * block_size + j, rs_n, rounds); |
| 350 | // Don't read past |data_size|, treat them as 0. |
| 351 | if (offset < data_size) { |
| 352 | ssize_t bytes_read = 0; |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 353 | TEST_AND_RETURN_FALSE(utils::PReadAll(read_fd, |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 354 | buffer.data(), |
| 355 | buffer.size(), |
| 356 | data_offset + offset, |
| 357 | &bytes_read)); |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 358 | TEST_AND_RETURN_FALSE(bytes_read >= 0); |
| 359 | TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == buffer.size()); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 360 | } |
| 361 | for (size_t k = 0; k < buffer.size(); k++) { |
| 362 | rs_blocks[k * rs_n + j] = buffer[k]; |
| 363 | } |
| 364 | } |
| 365 | brillo::Blob fec(block_size * fec_roots); |
| 366 | for (size_t j = 0; j < block_size; j++) { |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame^] | 367 | // Encode [j * rs_n : (j + 1) * rs_n) in |rs_blocks| and write |
| 368 | // |fec_roots| number of parity bytes to |j * fec_roots| in |fec|. |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 369 | encode_rs_char(rs_char.get(), |
| 370 | rs_blocks.data() + j * rs_n, |
| 371 | fec.data() + j * fec_roots); |
| 372 | } |
| 373 | |
| 374 | if (verify_mode) { |
| 375 | brillo::Blob fec_read(fec.size()); |
| 376 | ssize_t bytes_read = 0; |
| 377 | TEST_AND_RETURN_FALSE(utils::PReadAll( |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 378 | read_fd, fec_read.data(), fec_read.size(), fec_offset, &bytes_read)); |
| 379 | TEST_AND_RETURN_FALSE(bytes_read >= 0); |
| 380 | TEST_AND_RETURN_FALSE(static_cast<size_t>(bytes_read) == fec_read.size()); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 381 | TEST_AND_RETURN_FALSE(fec == fec_read); |
| 382 | } else { |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 383 | CHECK(write_fd); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 384 | write_fd->Seek(fec_offset, SEEK_SET); |
| 385 | if (!utils::WriteAll(write_fd, fec.data(), fec.size())) { |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 386 | PLOG(ERROR) << "EncodeFEC write() failed"; |
| 387 | return false; |
| 388 | } |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 389 | } |
| 390 | fec_offset += fec.size(); |
| 391 | } |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 392 | write_fd->Flush(); |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 393 | return true; |
| 394 | } |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 395 | |
| 396 | bool VerityWriterAndroid::EncodeFEC(const std::string& path, |
| 397 | uint64_t data_offset, |
| 398 | uint64_t data_size, |
| 399 | uint64_t fec_offset, |
| 400 | uint64_t fec_size, |
| 401 | uint32_t fec_roots, |
| 402 | uint32_t block_size, |
| 403 | bool verify_mode) { |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 404 | EintrSafeFileDescriptor fd; |
| 405 | TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), verify_mode ? O_RDONLY : O_RDWR)); |
| 406 | return EncodeFEC(&fd, |
| 407 | &fd, |
Kelvin Zhang | b138ab5 | 2020-11-06 15:56:41 -0500 | [diff] [blame] | 408 | data_offset, |
| 409 | data_size, |
| 410 | fec_offset, |
| 411 | fec_size, |
| 412 | fec_roots, |
| 413 | block_size, |
| 414 | verify_mode); |
| 415 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 416 | } // namespace chromeos_update_engine |