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