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