| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2015 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 | // | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | #include "update_engine/payload_generator/payload_file.h" | 
|  | 18 |  | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 19 | #include <endian.h> | 
|  | 20 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 21 | #include <algorithm> | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 22 | #include <map> | 
| Sen Jiang | 3a4dfac | 2018-08-30 16:57:38 -0700 | [diff] [blame] | 23 | #include <utility> | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 24 |  | 
|  | 25 | #include <base/strings/stringprintf.h> | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 26 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/hash_calculator.h" | 
| Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 28 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/delta_performer.h" | 
|  | 30 | #include "update_engine/payload_consumer/file_writer.h" | 
|  | 31 | #include "update_engine/payload_consumer/payload_constants.h" | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 32 | #include "update_engine/payload_generator/annotated_operation.h" | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 33 | #include "update_engine/payload_generator/delta_diff_utils.h" | 
|  | 34 | #include "update_engine/payload_generator/payload_signer.h" | 
|  | 35 |  | 
|  | 36 | using std::string; | 
|  | 37 | using std::vector; | 
|  | 38 |  | 
|  | 39 | namespace chromeos_update_engine { | 
|  | 40 |  | 
|  | 41 | namespace { | 
|  | 42 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 43 | struct DeltaObject { | 
|  | 44 | DeltaObject(const string& in_name, const int in_type, const off_t in_size) | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 45 | : name(in_name), type(in_type), size(in_size) {} | 
|  | 46 | bool operator<(const DeltaObject& object) const { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 47 | return (size != object.size) ? (size < object.size) : (name < object.name); | 
|  | 48 | } | 
|  | 49 | string name; | 
|  | 50 | int type; | 
|  | 51 | off_t size; | 
|  | 52 | }; | 
|  | 53 |  | 
|  | 54 | // Writes the uint64_t passed in in host-endian to the file as big-endian. | 
|  | 55 | // Returns true on success. | 
|  | 56 | bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) { | 
|  | 57 | uint64_t value_be = htobe64(value); | 
|  | 58 | TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be))); | 
|  | 59 | return true; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | }  // namespace | 
|  | 63 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 64 | bool PayloadFile::Init(const PayloadGenerationConfig& config) { | 
| Alex Deymo | a4073ef | 2016-03-22 23:40:53 -0700 | [diff] [blame] | 65 | TEST_AND_RETURN_FALSE(config.version.Validate()); | 
|  | 66 | major_version_ = config.version.major; | 
|  | 67 | manifest_.set_minor_version(config.version.minor); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 68 | manifest_.set_block_size(config.block_size); | 
| Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 69 | manifest_.set_max_timestamp(config.max_timestamp); | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 70 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 71 | if (config.target.dynamic_partition_metadata != nullptr) | 
|  | 72 | *(manifest_.mutable_dynamic_partition_metadata()) = | 
|  | 73 | *(config.target.dynamic_partition_metadata); | 
| Yifan Hong | 398cb54 | 2018-10-18 11:29:40 -0700 | [diff] [blame] | 74 |  | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 75 | if (config.is_partial_update) { | 
|  | 76 | manifest_.set_partial_update(true); | 
|  | 77 | } | 
| Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 78 |  | 
|  | 79 | if (!config.apex_info_file.empty()) { | 
|  | 80 | ApexMetadata apex_metadata; | 
|  | 81 | int fd = open(config.apex_info_file.c_str(), O_RDONLY); | 
|  | 82 | if (fd < 0) { | 
|  | 83 | PLOG(FATAL) << "Failed to open " << config.apex_info_file << " for read."; | 
|  | 84 | } | 
|  | 85 | ScopedFdCloser closer{&fd}; | 
|  | 86 | CHECK(apex_metadata.ParseFromFileDescriptor(fd)); | 
|  | 87 | if (apex_metadata.apex_info_size() > 0) { | 
|  | 88 | *manifest_.mutable_apex_info() = | 
|  | 89 | std::move(*apex_metadata.mutable_apex_info()); | 
|  | 90 | } | 
|  | 91 | } | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 92 | return true; | 
|  | 93 | } | 
|  | 94 |  | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 95 | bool PayloadFile::AddPartition(const PartitionConfig& old_conf, | 
|  | 96 | const PartitionConfig& new_conf, | 
| Tianjie | e9156ec | 2020-08-11 11:13:54 -0700 | [diff] [blame] | 97 | vector<AnnotatedOperation> aops, | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 98 | vector<CowMergeOperation> merge_sequence, | 
|  | 99 | size_t cow_size) { | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 100 | Partition part; | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 101 | part.cow_size = cow_size; | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 102 | part.name = new_conf.name; | 
| Kelvin Zhang | b0b9c20 | 2020-07-24 16:02:09 -0400 | [diff] [blame] | 103 | part.aops = std::move(aops); | 
| Tianjie | e9156ec | 2020-08-11 11:13:54 -0700 | [diff] [blame] | 104 | part.cow_merge_sequence = std::move(merge_sequence); | 
| Sen Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 105 | part.postinstall = new_conf.postinstall; | 
| Sen Jiang | 3a4dfac | 2018-08-30 16:57:38 -0700 | [diff] [blame] | 106 | part.verity = new_conf.verity; | 
| Kelvin Zhang | 1f49642 | 2020-08-11 17:18:23 -0400 | [diff] [blame] | 107 | part.version = new_conf.version; | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 108 | // Initialize the PartitionInfo objects if present. | 
|  | 109 | if (!old_conf.path.empty()) | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 110 | TEST_AND_RETURN_FALSE( | 
|  | 111 | diff_utils::InitializePartitionInfo(old_conf, &part.old_info)); | 
|  | 112 | TEST_AND_RETURN_FALSE( | 
|  | 113 | diff_utils::InitializePartitionInfo(new_conf, &part.new_info)); | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 114 | part_vec_.push_back(std::move(part)); | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 115 | return true; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | bool PayloadFile::WritePayload(const string& payload_file, | 
|  | 119 | const string& data_blobs_path, | 
|  | 120 | const string& private_key_path, | 
| Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 121 | uint64_t* metadata_size_out) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 122 | // Reorder the data blobs with the manifest_. | 
| Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame] | 123 | ScopedTempFile ordered_blobs_file("CrAU_temp_data.ordered.XXXXXX"); | 
|  | 124 | TEST_AND_RETURN_FALSE( | 
|  | 125 | ReorderDataBlobs(data_blobs_path, ordered_blobs_file.path())); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 126 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 127 | // Check that install op blobs are in order. | 
|  | 128 | uint64_t next_blob_offset = 0; | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 129 | for (const auto& part : part_vec_) { | 
|  | 130 | for (const auto& aop : part.aops) { | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 131 | if (!aop.op.has_data_offset()) | 
|  | 132 | continue; | 
|  | 133 | if (aop.op.data_offset() != next_blob_offset) { | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 134 | LOG(FATAL) << "bad blob offset! " << aop.op.data_offset() | 
|  | 135 | << " != " << next_blob_offset; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 136 | } | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 137 | next_blob_offset += aop.op.data_length(); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 141 | // Copy the operations and partition info from the part_vec_ to the manifest. | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 142 | manifest_.clear_partitions(); | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 143 | for (const auto& part : part_vec_) { | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 144 | PartitionUpdate* partition = manifest_.add_partitions(); | 
|  | 145 | partition->set_partition_name(part.name); | 
| Kelvin Zhang | 1f49642 | 2020-08-11 17:18:23 -0400 | [diff] [blame] | 146 | if (!part.version.empty()) { | 
|  | 147 | partition->set_version(part.version); | 
|  | 148 | } | 
| Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 149 | if (part.cow_size > 0) { | 
|  | 150 | partition->set_estimate_cow_size(part.cow_size); | 
|  | 151 | } | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 152 | if (part.postinstall.run) { | 
|  | 153 | partition->set_run_postinstall(true); | 
|  | 154 | if (!part.postinstall.path.empty()) | 
|  | 155 | partition->set_postinstall_path(part.postinstall.path); | 
|  | 156 | if (!part.postinstall.filesystem_type.empty()) | 
|  | 157 | partition->set_filesystem_type(part.postinstall.filesystem_type); | 
|  | 158 | partition->set_postinstall_optional(part.postinstall.optional); | 
|  | 159 | } | 
|  | 160 | if (!part.verity.IsEmpty()) { | 
|  | 161 | if (part.verity.hash_tree_extent.num_blocks() != 0) { | 
|  | 162 | *partition->mutable_hash_tree_data_extent() = | 
|  | 163 | part.verity.hash_tree_data_extent; | 
|  | 164 | *partition->mutable_hash_tree_extent() = part.verity.hash_tree_extent; | 
|  | 165 | partition->set_hash_tree_algorithm(part.verity.hash_tree_algorithm); | 
|  | 166 | if (!part.verity.hash_tree_salt.empty()) | 
|  | 167 | partition->set_hash_tree_salt(part.verity.hash_tree_salt.data(), | 
|  | 168 | part.verity.hash_tree_salt.size()); | 
| Sen Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 169 | } | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 170 | if (part.verity.fec_extent.num_blocks() != 0) { | 
|  | 171 | *partition->mutable_fec_data_extent() = part.verity.fec_data_extent; | 
|  | 172 | *partition->mutable_fec_extent() = part.verity.fec_extent; | 
|  | 173 | partition->set_fec_roots(part.verity.fec_roots); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 174 | } | 
|  | 175 | } | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 176 | for (const AnnotatedOperation& aop : part.aops) { | 
|  | 177 | *partition->add_operations() = aop.op; | 
|  | 178 | } | 
| Tianjie | e9156ec | 2020-08-11 11:13:54 -0700 | [diff] [blame] | 179 | for (const auto& merge_op : part.cow_merge_sequence) { | 
|  | 180 | *partition->add_merge_operations() = merge_op; | 
|  | 181 | } | 
|  | 182 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 183 | if (part.old_info.has_size() || part.old_info.has_hash()) | 
|  | 184 | *(partition->mutable_old_partition_info()) = part.old_info; | 
|  | 185 | if (part.new_info.has_size() || part.new_info.has_hash()) | 
|  | 186 | *(partition->mutable_new_partition_info()) = part.new_info; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
|  | 189 | // Signatures appear at the end of the blobs. Note the offset in the | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 190 | // |manifest_|. | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 191 | uint64_t signature_blob_length = 0; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 192 | if (!private_key_path.empty()) { | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 193 | TEST_AND_RETURN_FALSE(PayloadSigner::SignatureBlobLength( | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 194 | {private_key_path}, &signature_blob_length)); | 
| Sen Jiang | 3e728fe | 2015-11-05 11:37:23 -0800 | [diff] [blame] | 195 | PayloadSigner::AddSignatureToManifest( | 
| Kelvin Zhang | b0b9c20 | 2020-07-24 16:02:09 -0400 | [diff] [blame] | 196 | next_blob_offset, signature_blob_length, &manifest_); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 199 | // Serialize protobuf | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 200 | string serialized_manifest; | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 201 | TEST_AND_RETURN_FALSE(manifest_.SerializeToString(&serialized_manifest)); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 202 |  | 
| Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 203 | uint64_t metadata_size = | 
|  | 204 | sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size(); | 
|  | 205 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 206 | LOG(INFO) << "Writing final delta file header..."; | 
|  | 207 | DirectFileWriter writer; | 
|  | 208 | TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(), | 
|  | 209 | O_WRONLY | O_CREAT | O_TRUNC, | 
|  | 210 | 0644) == 0); | 
|  | 211 | ScopedFileWriterCloser writer_closer(&writer); | 
|  | 212 |  | 
|  | 213 | // Write header | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 214 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic))); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 215 |  | 
|  | 216 | // Write major version number | 
| Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 217 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_)); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | // Write protobuf length | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 220 | TEST_AND_RETURN_FALSE( | 
|  | 221 | WriteUint64AsBigEndian(&writer, serialized_manifest.size())); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 222 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 223 | // Metadata signature has the same size as payload signature, because they | 
|  | 224 | // are both the same kind of signature for the same kind of hash. | 
|  | 225 | uint32_t metadata_signature_size = htobe32(signature_blob_length); | 
|  | 226 | TEST_AND_RETURN_FALSE_ERRNO( | 
|  | 227 | writer.Write(&metadata_signature_size, sizeof(metadata_signature_size))); | 
|  | 228 | metadata_size += sizeof(metadata_signature_size); | 
|  | 229 | // Set correct size instead of big endian size. | 
|  | 230 | metadata_signature_size = signature_blob_length; | 
| Sen Jiang | f4bb3e6 | 2015-09-29 11:12:09 -0700 | [diff] [blame] | 231 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 232 | // Write protobuf | 
|  | 233 | LOG(INFO) << "Writing final delta file protobuf... " | 
|  | 234 | << serialized_manifest.size(); | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 235 | TEST_AND_RETURN_FALSE_ERRNO( | 
|  | 236 | writer.Write(serialized_manifest.data(), serialized_manifest.size())); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 237 |  | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 238 | // Write metadata signature blob. | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 239 | if (!private_key_path.empty()) { | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 240 | brillo::Blob metadata_hash; | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 241 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile( | 
|  | 242 | payload_file, metadata_size, &metadata_hash)); | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 243 | string metadata_signature; | 
|  | 244 | TEST_AND_RETURN_FALSE(PayloadSigner::SignHashWithKeys( | 
|  | 245 | metadata_hash, {private_key_path}, &metadata_signature)); | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 246 | TEST_AND_RETURN_FALSE_ERRNO( | 
|  | 247 | writer.Write(metadata_signature.data(), metadata_signature.size())); | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 248 | } | 
|  | 249 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 250 | // Append the data blobs. | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 251 | LOG(INFO) << "Writing final delta file data blobs..."; | 
| Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame] | 252 | int blobs_fd = open(ordered_blobs_file.path().c_str(), O_RDONLY, 0); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 253 | ScopedFdCloser blobs_fd_closer(&blobs_fd); | 
|  | 254 | TEST_AND_RETURN_FALSE(blobs_fd >= 0); | 
|  | 255 | for (;;) { | 
|  | 256 | vector<char> buf(1024 * 1024); | 
|  | 257 | ssize_t rc = read(blobs_fd, buf.data(), buf.size()); | 
|  | 258 | if (0 == rc) { | 
|  | 259 | // EOF | 
|  | 260 | break; | 
|  | 261 | } | 
|  | 262 | TEST_AND_RETURN_FALSE_ERRNO(rc > 0); | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 263 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc)); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 266 | // Write payload signature blob. | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 267 | if (!private_key_path.empty()) { | 
|  | 268 | LOG(INFO) << "Signing the update..."; | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 269 | string signature; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 270 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload( | 
|  | 271 | payload_file, | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 272 | {private_key_path}, | 
| Sen Jiang | 720df3e | 2015-10-01 13:10:44 -0700 | [diff] [blame] | 273 | metadata_size, | 
| Sen Jiang | 644f618 | 2015-10-06 16:45:57 -0700 | [diff] [blame] | 274 | metadata_signature_size, | 
|  | 275 | metadata_size + metadata_signature_size + manifest_.signatures_offset(), | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 276 | &signature)); | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 277 | TEST_AND_RETURN_FALSE_ERRNO( | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 278 | writer.Write(signature.data(), signature.size())); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
| Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 281 | ReportPayloadUsage(metadata_size); | 
|  | 282 | *metadata_size_out = metadata_size; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 283 | return true; | 
|  | 284 | } | 
|  | 285 |  | 
| Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 286 | bool PayloadFile::ReorderDataBlobs(const string& data_blobs_path, | 
|  | 287 | const string& new_data_blobs_path) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 288 | int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0); | 
|  | 289 | TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0); | 
|  | 290 | ScopedFdCloser in_fd_closer(&in_fd); | 
|  | 291 |  | 
|  | 292 | DirectFileWriter writer; | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 293 | int rc = writer.Open( | 
|  | 294 | new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644); | 
|  | 295 | if (rc != 0) { | 
|  | 296 | PLOG(ERROR) << "Error creating " << new_data_blobs_path; | 
|  | 297 | return false; | 
|  | 298 | } | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 299 | ScopedFileWriterCloser writer_closer(&writer); | 
|  | 300 | uint64_t out_file_size = 0; | 
|  | 301 |  | 
| Alex Deymo | a4073ef | 2016-03-22 23:40:53 -0700 | [diff] [blame] | 302 | for (auto& part : part_vec_) { | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 303 | for (AnnotatedOperation& aop : part.aops) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 304 | if (!aop.op.has_data_offset()) | 
|  | 305 | continue; | 
|  | 306 | CHECK(aop.op.has_data_length()); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 307 | brillo::Blob buf(aop.op.data_length()); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 308 | ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset()); | 
|  | 309 | TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size())); | 
|  | 310 |  | 
|  | 311 | // Add the hash of the data blobs for this operation | 
|  | 312 | TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf)); | 
|  | 313 |  | 
|  | 314 | aop.op.set_data_offset(out_file_size); | 
| Alex Deymo | 95699a9 | 2016-12-08 19:43:01 -0800 | [diff] [blame] | 315 | TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size())); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 316 | out_file_size += buf.size(); | 
|  | 317 | } | 
|  | 318 | } | 
|  | 319 | return true; | 
|  | 320 | } | 
|  | 321 |  | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 322 | bool PayloadFile::AddOperationHash(InstallOperation* op, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 323 | const brillo::Blob& buf) { | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 324 | brillo::Blob hash; | 
|  | 325 | TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash)); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 326 | op->set_data_sha256_hash(hash.data(), hash.size()); | 
|  | 327 | return true; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const { | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 331 | std::map<DeltaObject, int> object_counts; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 332 | off_t total_size = 0; | 
| Sen Jiang | f23bf92 | 2018-11-01 18:26:07 -0700 | [diff] [blame] | 333 | int total_op = 0; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 334 |  | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 335 | for (const auto& part : part_vec_) { | 
| Sen Jiang | a91195d | 2018-10-25 15:15:38 -0700 | [diff] [blame] | 336 | string part_prefix = "<" + part.name + ">:"; | 
| Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 337 | for (const AnnotatedOperation& aop : part.aops) { | 
| Sen Jiang | a91195d | 2018-10-25 15:15:38 -0700 | [diff] [blame] | 338 | DeltaObject delta( | 
|  | 339 | part_prefix + aop.name, aop.op.type(), aop.op.data_length()); | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 340 | object_counts[delta]++; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 341 | total_size += aop.op.data_length(); | 
|  | 342 | } | 
| Sen Jiang | f23bf92 | 2018-11-01 18:26:07 -0700 | [diff] [blame] | 343 | total_op += part.aops.size(); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 344 | } | 
|  | 345 |  | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 346 | object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 347 | total_size += metadata_size; | 
|  | 348 |  | 
| Sen Jiang | a91195d | 2018-10-25 15:15:38 -0700 | [diff] [blame] | 349 | constexpr char kFormatString[] = "%6.2f%% %10jd %-13s %s %d\n"; | 
| Simon Glass | 3d32f85 | 2017-06-28 16:38:11 -0600 | [diff] [blame] | 350 | for (const auto& object_count : object_counts) { | 
|  | 351 | const DeltaObject& object = object_count.first; | 
| Sen Jiang | a91195d | 2018-10-25 15:15:38 -0700 | [diff] [blame] | 352 | // Use printf() instead of LOG(INFO) because timestamp makes it difficult to | 
|  | 353 | // compare two reports. | 
| Sen Jiang | cebb688 | 2019-02-26 16:23:28 +0800 | [diff] [blame] | 354 | printf(kFormatString, | 
|  | 355 | object.size * 100.0 / total_size, | 
|  | 356 | object.size, | 
|  | 357 | (object.type >= 0 | 
|  | 358 | ? InstallOperationTypeName( | 
|  | 359 | static_cast<InstallOperation::Type>(object.type)) | 
|  | 360 | : "-"), | 
|  | 361 | object.name.c_str(), | 
|  | 362 | object_count.second); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 363 | } | 
| Sen Jiang | f23bf92 | 2018-11-01 18:26:07 -0700 | [diff] [blame] | 364 | printf(kFormatString, 100.0, total_size, "", "<total>", total_op); | 
|  | 365 | fflush(stdout); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 368 | }  // namespace chromeos_update_engine |