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