| 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> | 
|  | 23 |  | 
|  | 24 | #include <base/logging.h> | 
|  | 25 | #include <base/posix/eintr_wrapper.h> | 
|  | 26 |  | 
|  | 27 | #include "update_engine/common/utils.h" | 
|  | 28 |  | 
|  | 29 | namespace chromeos_update_engine { | 
|  | 30 |  | 
|  | 31 | namespace verity_writer { | 
|  | 32 | std::unique_ptr<VerityWriterInterface> CreateVerityWriter() { | 
|  | 33 | return std::make_unique<VerityWriterAndroid>(); | 
|  | 34 | } | 
|  | 35 | }  // namespace verity_writer | 
|  | 36 |  | 
|  | 37 | bool VerityWriterAndroid::Init(const InstallPlan::Partition& partition) { | 
|  | 38 | partition_ = &partition; | 
|  | 39 |  | 
|  | 40 | if (partition_->hash_tree_size != 0) { | 
|  | 41 | auto hash_function = | 
|  | 42 | HashTreeBuilder::HashFunction(partition_->hash_tree_algorithm); | 
|  | 43 | if (hash_function == nullptr) { | 
|  | 44 | LOG(ERROR) << "Verity hash algorithm not supported: " | 
|  | 45 | << partition_->hash_tree_algorithm; | 
|  | 46 | return false; | 
|  | 47 | } | 
|  | 48 | hash_tree_builder_ = std::make_unique<HashTreeBuilder>( | 
|  | 49 | partition_->block_size, hash_function); | 
|  | 50 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Initialize( | 
|  | 51 | partition_->hash_tree_data_size, partition_->hash_tree_salt)); | 
|  | 52 | if (hash_tree_builder_->CalculateSize(partition_->hash_tree_data_size) != | 
|  | 53 | partition_->hash_tree_size) { | 
|  | 54 | LOG(ERROR) << "Verity hash tree size does not match, stored: " | 
|  | 55 | << partition_->hash_tree_size << ", calculated: " | 
|  | 56 | << hash_tree_builder_->CalculateSize( | 
|  | 57 | partition_->hash_tree_data_size); | 
|  | 58 | return false; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | return true; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | bool VerityWriterAndroid::Update(uint64_t offset, | 
|  | 65 | const uint8_t* buffer, | 
|  | 66 | size_t size) { | 
|  | 67 | if (partition_->hash_tree_size != 0) { | 
|  | 68 | uint64_t hash_tree_data_end = | 
|  | 69 | partition_->hash_tree_data_offset + partition_->hash_tree_data_size; | 
|  | 70 | uint64_t start_offset = std::max(offset, partition_->hash_tree_data_offset); | 
|  | 71 | uint64_t end_offset = std::min(offset + size, hash_tree_data_end); | 
|  | 72 | if (start_offset < end_offset) { | 
|  | 73 | TEST_AND_RETURN_FALSE(hash_tree_builder_->Update( | 
|  | 74 | buffer + start_offset - offset, end_offset - start_offset)); | 
|  | 75 |  | 
|  | 76 | if (end_offset == hash_tree_data_end) { | 
|  | 77 | // All hash tree data blocks has been hashed, write hash tree to disk. | 
|  | 78 | int fd = HANDLE_EINTR(open(partition_->target_path.c_str(), O_WRONLY)); | 
|  | 79 | if (fd < 0) { | 
|  | 80 | PLOG(ERROR) << "Failed to open " << partition_->target_path | 
|  | 81 | << " to write verity data."; | 
|  | 82 | return false; | 
|  | 83 | } | 
|  | 84 | ScopedFdCloser fd_closer(&fd); | 
|  | 85 |  | 
|  | 86 | LOG(INFO) << "Writing verity hash tree to " << partition_->target_path; | 
|  | 87 | TEST_AND_RETURN_FALSE(hash_tree_builder_->BuildHashTree()); | 
|  | 88 | TEST_AND_RETURN_FALSE(hash_tree_builder_->WriteHashTreeToFd( | 
|  | 89 | fd, partition_->hash_tree_offset)); | 
|  | 90 | hash_tree_builder_.reset(); | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 | if (partition_->fec_size != 0) { | 
|  | 95 | // TODO(senj): Update FEC data. | 
|  | 96 | } | 
|  | 97 | return true; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | }  // namespace chromeos_update_engine |