| 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> | 
|  | 22 |  | 
| Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 23 | #include "update_engine/delta_performer.h" | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 24 | #include "update_engine/file_writer.h" | 
|  | 25 | #include "update_engine/omaha_hash_calculator.h" | 
|  | 26 | #include "update_engine/payload_constants.h" | 
|  | 27 | #include "update_engine/payload_generator/annotated_operation.h" | 
|  | 28 | #include "update_engine/payload_generator/delta_diff_generator.h" | 
|  | 29 | #include "update_engine/payload_generator/delta_diff_utils.h" | 
|  | 30 | #include "update_engine/payload_generator/payload_signer.h" | 
|  | 31 |  | 
|  | 32 | using std::string; | 
|  | 33 | using std::vector; | 
|  | 34 |  | 
|  | 35 | namespace chromeos_update_engine { | 
|  | 36 |  | 
|  | 37 | namespace { | 
|  | 38 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 39 | static const char* kInstallOperationTypes[] = { | 
|  | 40 | "REPLACE", | 
|  | 41 | "REPLACE_BZ", | 
|  | 42 | "MOVE", | 
|  | 43 | "BSDIFF", | 
|  | 44 | "SOURCE_COPY", | 
|  | 45 | "SOURCE_BSDIFF" | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 | struct DeltaObject { | 
|  | 49 | DeltaObject(const string& in_name, const int in_type, const off_t in_size) | 
|  | 50 | : name(in_name), | 
|  | 51 | type(in_type), | 
|  | 52 | size(in_size) {} | 
|  | 53 | bool operator <(const DeltaObject& object) const { | 
|  | 54 | return (size != object.size) ? (size < object.size) : (name < object.name); | 
|  | 55 | } | 
|  | 56 | string name; | 
|  | 57 | int type; | 
|  | 58 | off_t size; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | // Writes the uint64_t passed in in host-endian to the file as big-endian. | 
|  | 62 | // Returns true on success. | 
|  | 63 | bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) { | 
|  | 64 | uint64_t value_be = htobe64(value); | 
|  | 65 | TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be))); | 
|  | 66 | return true; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | }  // namespace | 
|  | 70 |  | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 71 | bool PayloadFile::Init(const PayloadGenerationConfig& config) { | 
| Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 72 | major_version_ = config.major_version; | 
|  | 73 | TEST_AND_RETURN_FALSE(major_version_ == kChromeOSMajorPayloadVersion || | 
|  | 74 | major_version_ == kBrilloMajorPayloadVersion); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 75 | manifest_.set_minor_version(config.minor_version); | 
|  | 76 |  | 
|  | 77 | if (!config.source.ImageInfoIsEmpty()) | 
|  | 78 | *(manifest_.mutable_old_image_info()) = config.source.image_info; | 
|  | 79 |  | 
|  | 80 | if (!config.target.ImageInfoIsEmpty()) | 
|  | 81 | *(manifest_.mutable_new_image_info()) = config.target.image_info; | 
|  | 82 |  | 
|  | 83 | manifest_.set_block_size(config.block_size); | 
|  | 84 |  | 
|  | 85 | // Initialize the PartitionInfo objects if present. | 
|  | 86 | if (!config.source.kernel.path.empty()) { | 
|  | 87 | TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo( | 
|  | 88 | config.source.kernel, | 
|  | 89 | manifest_.mutable_old_kernel_info())); | 
|  | 90 | } | 
|  | 91 | TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo( | 
|  | 92 | config.target.kernel, | 
|  | 93 | manifest_.mutable_new_kernel_info())); | 
|  | 94 | if (!config.source.rootfs.path.empty()) { | 
|  | 95 | TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo( | 
|  | 96 | config.source.rootfs, | 
|  | 97 | manifest_.mutable_old_rootfs_info())); | 
|  | 98 | } | 
|  | 99 | TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo( | 
|  | 100 | config.target.rootfs, | 
|  | 101 | manifest_.mutable_new_rootfs_info())); | 
|  | 102 | return true; | 
|  | 103 | } | 
|  | 104 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 105 | bool PayloadFile::AddPartitionOperations( | 
|  | 106 | const std::string& partition_name, | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 107 | const vector<AnnotatedOperation>& aops) { | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 108 | // Check partitions order for Chrome OS | 
|  | 109 | if (major_version_ == kChromeOSMajorPayloadVersion) { | 
|  | 110 | const vector<const char*> part_order = { kLegacyPartitionNameRoot, | 
|  | 111 | kLegacyPartitionNameKernel }; | 
|  | 112 | TEST_AND_RETURN_FALSE(aops_vec_.size() < part_order.size()); | 
|  | 113 | TEST_AND_RETURN_FALSE(partition_name == part_order[aops_vec_.size()]); | 
|  | 114 | } | 
|  | 115 | aops_vec_.emplace_back(partition_name, aops); | 
|  | 116 | return true; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | bool PayloadFile::WritePayload(const string& payload_file, | 
|  | 120 | const string& data_blobs_path, | 
|  | 121 | const string& private_key_path, | 
|  | 122 | uint64_t* medatata_size_out) { | 
|  | 123 | // Reorder the data blobs with the manifest_. | 
|  | 124 | string ordered_blobs_path; | 
|  | 125 | TEST_AND_RETURN_FALSE(utils::MakeTempFile( | 
|  | 126 | "CrAU_temp_data.ordered.XXXXXX", | 
|  | 127 | &ordered_blobs_path, | 
|  | 128 | nullptr)); | 
|  | 129 | ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path); | 
|  | 130 | TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path)); | 
|  | 131 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 132 | // Check that install op blobs are in order. | 
|  | 133 | uint64_t next_blob_offset = 0; | 
|  | 134 | for (const auto& part_name_aops_pair : aops_vec_) { | 
|  | 135 | for (const auto& aop : part_name_aops_pair.second) { | 
|  | 136 | if (!aop.op.has_data_offset()) | 
|  | 137 | continue; | 
|  | 138 | if (aop.op.data_offset() != next_blob_offset) { | 
|  | 139 | LOG(FATAL) << "bad blob offset! " << aop.op.data_offset() << " != " | 
|  | 140 | << next_blob_offset; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 141 | } | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 142 | next_blob_offset += aop.op.data_length(); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 146 | // Copy the operations from the aops_vec_ to the manifest. | 
|  | 147 | manifest_.clear_install_operations(); | 
|  | 148 | manifest_.clear_kernel_install_operations(); | 
|  | 149 | manifest_.clear_partitions(); | 
|  | 150 | for (const auto& part_name_aops_pair : aops_vec_) { | 
|  | 151 | if (major_version_ == kBrilloMajorPayloadVersion) { | 
|  | 152 | PartitionUpdate* partition = manifest_.add_partitions(); | 
|  | 153 | partition->set_partition_name(part_name_aops_pair.first); | 
|  | 154 | for (const AnnotatedOperation& aop : part_name_aops_pair.second) { | 
|  | 155 | *partition->add_operations() = aop.op; | 
|  | 156 | } | 
|  | 157 | } else { | 
|  | 158 | for (const AnnotatedOperation& aop : part_name_aops_pair.second) { | 
|  | 159 | if (part_name_aops_pair.first == kLegacyPartitionNameKernel) { | 
|  | 160 | *manifest_.add_kernel_install_operations() = aop.op; | 
|  | 161 | } else { | 
|  | 162 | *manifest_.add_install_operations() = aop.op; | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 163 | } | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 164 | } | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | // Signatures appear at the end of the blobs. Note the offset in the | 
|  | 169 | // manifest_. | 
|  | 170 | if (!private_key_path.empty()) { | 
|  | 171 | uint64_t signature_blob_length = 0; | 
|  | 172 | TEST_AND_RETURN_FALSE( | 
|  | 173 | PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path), | 
|  | 174 | &signature_blob_length)); | 
|  | 175 | AddSignatureOp(next_blob_offset, signature_blob_length, &manifest_); | 
|  | 176 | } | 
|  | 177 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 178 | // Serialize protobuf | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 179 | string serialized_manifest; | 
|  | 180 | TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest)); | 
|  | 181 |  | 
|  | 182 | LOG(INFO) << "Writing final delta file header..."; | 
|  | 183 | DirectFileWriter writer; | 
|  | 184 | TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(), | 
|  | 185 | O_WRONLY | O_CREAT | O_TRUNC, | 
|  | 186 | 0644) == 0); | 
|  | 187 | ScopedFileWriterCloser writer_closer(&writer); | 
|  | 188 |  | 
|  | 189 | // Write header | 
|  | 190 | TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, strlen(kDeltaMagic))); | 
|  | 191 |  | 
|  | 192 | // Write major version number | 
| Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 193 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_)); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 194 |  | 
|  | 195 | // Write protobuf length | 
|  | 196 | TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, | 
|  | 197 | serialized_manifest.size())); | 
|  | 198 |  | 
|  | 199 | // Write protobuf | 
|  | 200 | LOG(INFO) << "Writing final delta file protobuf... " | 
|  | 201 | << serialized_manifest.size(); | 
|  | 202 | TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(), | 
|  | 203 | serialized_manifest.size())); | 
|  | 204 |  | 
|  | 205 | // Append the data blobs | 
|  | 206 | LOG(INFO) << "Writing final delta file data blobs..."; | 
|  | 207 | int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0); | 
|  | 208 | ScopedFdCloser blobs_fd_closer(&blobs_fd); | 
|  | 209 | TEST_AND_RETURN_FALSE(blobs_fd >= 0); | 
|  | 210 | for (;;) { | 
|  | 211 | vector<char> buf(1024 * 1024); | 
|  | 212 | ssize_t rc = read(blobs_fd, buf.data(), buf.size()); | 
|  | 213 | if (0 == rc) { | 
|  | 214 | // EOF | 
|  | 215 | break; | 
|  | 216 | } | 
|  | 217 | TEST_AND_RETURN_FALSE_ERRNO(rc > 0); | 
|  | 218 | TEST_AND_RETURN_FALSE(writer.Write(buf.data(), rc)); | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | // Write signature blob. | 
|  | 222 | if (!private_key_path.empty()) { | 
|  | 223 | LOG(INFO) << "Signing the update..."; | 
|  | 224 | chromeos::Blob signature_blob; | 
|  | 225 | TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload( | 
|  | 226 | payload_file, | 
|  | 227 | vector<string>(1, private_key_path), | 
|  | 228 | &signature_blob)); | 
|  | 229 | TEST_AND_RETURN_FALSE(writer.Write(signature_blob.data(), | 
|  | 230 | signature_blob.size())); | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | *medatata_size_out = | 
|  | 234 | strlen(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size(); | 
|  | 235 | ReportPayloadUsage(*medatata_size_out); | 
|  | 236 | return true; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | bool PayloadFile::ReorderDataBlobs( | 
|  | 240 | const string& data_blobs_path, | 
|  | 241 | const string& new_data_blobs_path) { | 
|  | 242 | int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0); | 
|  | 243 | TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0); | 
|  | 244 | ScopedFdCloser in_fd_closer(&in_fd); | 
|  | 245 |  | 
|  | 246 | DirectFileWriter writer; | 
|  | 247 | TEST_AND_RETURN_FALSE( | 
|  | 248 | writer.Open(new_data_blobs_path.c_str(), | 
|  | 249 | O_WRONLY | O_TRUNC | O_CREAT, | 
|  | 250 | 0644) == 0); | 
|  | 251 | ScopedFileWriterCloser writer_closer(&writer); | 
|  | 252 | uint64_t out_file_size = 0; | 
|  | 253 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 254 | for (auto& part_name_aops_pair : aops_vec_) { | 
|  | 255 | for (AnnotatedOperation& aop : part_name_aops_pair.second) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 256 | if (!aop.op.has_data_offset()) | 
|  | 257 | continue; | 
|  | 258 | CHECK(aop.op.has_data_length()); | 
|  | 259 | chromeos::Blob buf(aop.op.data_length()); | 
|  | 260 | ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset()); | 
|  | 261 | TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size())); | 
|  | 262 |  | 
|  | 263 | // Add the hash of the data blobs for this operation | 
|  | 264 | TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf)); | 
|  | 265 |  | 
|  | 266 | aop.op.set_data_offset(out_file_size); | 
|  | 267 | TEST_AND_RETURN_FALSE(writer.Write(buf.data(), buf.size())); | 
|  | 268 | out_file_size += buf.size(); | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 | return true; | 
|  | 272 | } | 
|  | 273 |  | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 274 | bool PayloadFile::AddOperationHash(InstallOperation* op, | 
|  | 275 | const chromeos::Blob& buf) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 276 | OmahaHashCalculator hasher; | 
|  | 277 | TEST_AND_RETURN_FALSE(hasher.Update(buf.data(), buf.size())); | 
|  | 278 | TEST_AND_RETURN_FALSE(hasher.Finalize()); | 
|  | 279 | const chromeos::Blob& hash = hasher.raw_hash(); | 
|  | 280 | op->set_data_sha256_hash(hash.data(), hash.size()); | 
|  | 281 | return true; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const { | 
|  | 285 | vector<DeltaObject> objects; | 
|  | 286 | off_t total_size = 0; | 
|  | 287 |  | 
| Sen Jiang | 70a6ab0 | 2015-08-28 13:23:27 -0700 | [diff] [blame] | 288 | for (const auto& part_name_aops_pair : aops_vec_) { | 
|  | 289 | for (const AnnotatedOperation& aop : part_name_aops_pair.second) { | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 290 | objects.push_back(DeltaObject(aop.name, | 
|  | 291 | aop.op.type(), | 
|  | 292 | aop.op.data_length())); | 
|  | 293 | total_size += aop.op.data_length(); | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | objects.push_back(DeltaObject("<manifest-metadata>", | 
|  | 298 | -1, | 
|  | 299 | metadata_size)); | 
|  | 300 | total_size += metadata_size; | 
|  | 301 |  | 
|  | 302 | std::sort(objects.begin(), objects.end()); | 
|  | 303 |  | 
|  | 304 | static const char kFormatString[] = "%6.2f%% %10jd %-10s %s\n"; | 
|  | 305 | for (const DeltaObject& object : objects) { | 
|  | 306 | fprintf(stderr, kFormatString, | 
|  | 307 | object.size * 100.0 / total_size, | 
|  | 308 | static_cast<intmax_t>(object.size), | 
|  | 309 | object.type >= 0 ? kInstallOperationTypes[object.type] : "-", | 
|  | 310 | object.name.c_str()); | 
|  | 311 | } | 
|  | 312 | fprintf(stderr, kFormatString, | 
|  | 313 | 100.0, static_cast<intmax_t>(total_size), "", "<total>"); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | void AddSignatureOp(uint64_t signature_blob_offset, | 
|  | 317 | uint64_t signature_blob_length, | 
|  | 318 | DeltaArchiveManifest* manifest) { | 
|  | 319 | LOG(INFO) << "Making room for signature in file"; | 
|  | 320 | manifest->set_signatures_offset(signature_blob_offset); | 
|  | 321 | LOG(INFO) << "set? " << manifest->has_signatures_offset(); | 
|  | 322 | // Add a dummy op at the end to appease older clients | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 323 | InstallOperation* dummy_op = manifest->add_kernel_install_operations(); | 
|  | 324 | dummy_op->set_type(InstallOperation::REPLACE); | 
| Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 325 | dummy_op->set_data_offset(signature_blob_offset); | 
|  | 326 | manifest->set_signatures_offset(signature_blob_offset); | 
|  | 327 | dummy_op->set_data_length(signature_blob_length); | 
|  | 328 | manifest->set_signatures_size(signature_blob_length); | 
|  | 329 | Extent* dummy_extent = dummy_op->add_dst_extents(); | 
|  | 330 | // Tell the dummy op to write this data to a big sparse hole | 
|  | 331 | dummy_extent->set_start_block(kSparseHole); | 
|  | 332 | dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) / | 
|  | 333 | kBlockSize); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | }  // namespace chromeos_update_engine |