blob: d5c59cebd77639acf126bc627e1b3bc0118239ab [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo14158572015-06-13 03:37:08 -070016
17#include "update_engine/payload_generator/payload_file.h"
18
Alex Deymo6f20dd42015-08-18 16:42:46 -070019#include <endian.h>
20
Alex Deymo14158572015-06-13 03:37:08 -070021#include <algorithm>
Simon Glass3d32f852017-06-28 16:38:11 -060022#include <map>
23
24#include <base/strings/stringprintf.h>
Alex Deymo14158572015-06-13 03:37:08 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#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 Deymo14158572015-06-13 03:37:08 -070030#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo14158572015-06-13 03:37:08 -070031#include "update_engine/payload_generator/delta_diff_utils.h"
32#include "update_engine/payload_generator/payload_signer.h"
33
34using std::string;
35using std::vector;
36
37namespace chromeos_update_engine {
38
39namespace {
40
Alex Deymo14158572015-06-13 03:37:08 -070041struct 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.
56bool 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 Deymo14158572015-06-13 03:37:08 -070064bool PayloadFile::Init(const PayloadGenerationConfig& config) {
Alex Deymoa4073ef2016-03-22 23:40:53 -070065 TEST_AND_RETURN_FALSE(config.version.Validate());
66 major_version_ = config.version.major;
67 manifest_.set_minor_version(config.version.minor);
Alex Deymo14158572015-06-13 03:37:08 -070068
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 Jiang5011df62017-06-28 17:13:19 -070076 manifest_.set_max_timestamp(config.max_timestamp);
Alex Deymo14158572015-06-13 03:37:08 -070077 return true;
78}
79
Sen Jiangb9ef4912015-09-21 15:06:13 -070080bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
81 const PartitionConfig& new_conf,
82 const vector<AnnotatedOperation>& aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -070083 // Check partitions order for Chrome OS
84 if (major_version_ == kChromeOSMajorPayloadVersion) {
Tudor Brindusdda79e22018-06-28 18:03:21 -070085 const vector<const char*> part_order = { kPartitionNameRoot,
86 kPartitionNameKernel };
Sen Jiangb9ef4912015-09-21 15:06:13 -070087 TEST_AND_RETURN_FALSE(part_vec_.size() < part_order.size());
88 TEST_AND_RETURN_FALSE(new_conf.name == part_order[part_vec_.size()]);
Sen Jiang70a6ab02015-08-28 13:23:27 -070089 }
Sen Jiangb9ef4912015-09-21 15:06:13 -070090 Partition part;
91 part.name = new_conf.name;
92 part.aops = aops;
Sen Jiang05feee02015-11-11 15:59:49 -080093 part.postinstall = new_conf.postinstall;
Sen Jiangb9ef4912015-09-21 15:06:13 -070094 // 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 Jiang70a6ab02015-08-28 13:23:27 -0700101 return true;
Alex Deymo14158572015-06-13 03:37:08 -0700102}
103
104bool PayloadFile::WritePayload(const string& payload_file,
105 const string& data_blobs_path,
106 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700107 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700108 // 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 Jiang70a6ab02015-08-28 13:23:27 -0700117 // Check that install op blobs are in order.
118 uint64_t next_blob_offset = 0;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700119 for (const auto& part : part_vec_) {
120 for (const auto& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700121 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 Deymo14158572015-06-13 03:37:08 -0700126 }
Sen Jiang70a6ab02015-08-28 13:23:27 -0700127 next_blob_offset += aop.op.data_length();
Alex Deymo14158572015-06-13 03:37:08 -0700128 }
129 }
130
Sen Jiangb9ef4912015-09-21 15:06:13 -0700131 // Copy the operations and partition info from the part_vec_ to the manifest.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700132 manifest_.clear_install_operations();
133 manifest_.clear_kernel_install_operations();
134 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700135 for (const auto& part : part_vec_) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700136 if (major_version_ == kBrilloMajorPayloadVersion) {
137 PartitionUpdate* partition = manifest_.add_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700138 partition->set_partition_name(part.name);
Sen Jiang05feee02015-11-11 15:59:49 -0800139 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 Deymo5b91c6b2016-08-04 20:33:36 -0700145 partition->set_postinstall_optional(part.postinstall.optional);
Sen Jiang05feee02015-11-11 15:59:49 -0800146 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700147 for (const AnnotatedOperation& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700148 *partition->add_operations() = aop.op;
149 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700150 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 Jiang70a6ab02015-08-28 13:23:27 -0700154 } else {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700155 // major_version_ == kChromeOSMajorPayloadVersion
Tudor Brindusdda79e22018-06-28 18:03:21 -0700156 if (part.name == kPartitionNameKernel) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700157 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700158 *manifest_.add_kernel_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700159 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 Jiang70a6ab02015-08-28 13:23:27 -0700165 *manifest_.add_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700166 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 Deymo14158572015-06-13 03:37:08 -0700170 }
171 }
172 }
173
174 // Signatures appear at the end of the blobs. Note the offset in the
175 // manifest_.
Sen Jiang644f6182015-10-06 16:45:57 -0700176 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700177 if (!private_key_path.empty()) {
Alex Deymo14158572015-06-13 03:37:08 -0700178 TEST_AND_RETURN_FALSE(
179 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
180 &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800181 PayloadSigner::AddSignatureToManifest(
182 next_blob_offset, signature_blob_length,
183 major_version_ == kChromeOSMajorPayloadVersion, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700184 }
185
Sen Jiang70a6ab02015-08-28 13:23:27 -0700186 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700187 string serialized_manifest;
188 TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest));
189
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700190 uint64_t metadata_size =
191 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
192
Alex Deymo14158572015-06-13 03:37:08 -0700193 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 Deymo95699a92016-12-08 19:43:01 -0800201 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700202
203 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700204 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700205
206 // Write protobuf length
207 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
208 serialized_manifest.size()));
209
Sen Jiang644f6182015-10-06 16:45:57 -0700210 // Write metadata signature size.
211 uint32_t metadata_signature_size = 0;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700212 if (major_version_ == kBrilloMajorPayloadVersion) {
Sen Jiang644f6182015-10-06 16:45:57 -0700213 // 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 Deymo95699a92016-12-08 19:43:01 -0800216 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(&metadata_signature_size,
217 sizeof(metadata_signature_size)));
Sen Jiang644f6182015-10-06 16:45:57 -0700218 metadata_size += sizeof(metadata_signature_size);
219 // Set correct size instead of big endian size.
220 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700221 }
222
Alex Deymo14158572015-06-13 03:37:08 -0700223 // Write protobuf
224 LOG(INFO) << "Writing final delta file protobuf... "
225 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800226 TEST_AND_RETURN_FALSE_ERRNO(
227 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700228
Sen Jiang644f6182015-10-06 16:45:57 -0700229 // Write metadata signature blob.
230 if (major_version_ == kBrilloMajorPayloadVersion &&
231 !private_key_path.empty()) {
232 brillo::Blob metadata_hash, metadata_signature;
Alex Deymo39910dc2015-11-09 17:04:30 -0800233 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(payload_file,
Sen Jiang644f6182015-10-06 16:45:57 -0700234 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 Deymo95699a92016-12-08 19:43:01 -0800240 TEST_AND_RETURN_FALSE_ERRNO(
241 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700242 }
243
Alex Deymo14158572015-06-13 03:37:08 -0700244 // 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 Deymo95699a92016-12-08 19:43:01 -0800257 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700258 }
259
Sen Jiang644f6182015-10-06 16:45:57 -0700260 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700261 if (!private_key_path.empty()) {
262 LOG(INFO) << "Signing the update...";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700263 brillo::Blob signature_blob;
Alex Deymo14158572015-06-13 03:37:08 -0700264 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
265 payload_file,
266 vector<string>(1, private_key_path),
Sen Jiang720df3e2015-10-01 13:10:44 -0700267 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700268 metadata_signature_size,
269 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Alex Deymo14158572015-06-13 03:37:08 -0700270 &signature_blob));
Alex Deymo95699a92016-12-08 19:43:01 -0800271 TEST_AND_RETURN_FALSE_ERRNO(
272 writer.Write(signature_blob.data(), signature_blob.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700273 }
274
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700275 ReportPayloadUsage(metadata_size);
276 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700277 return true;
278}
279
280bool 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 Deymo95699a92016-12-08 19:43:01 -0800288 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 Deymo14158572015-06-13 03:37:08 -0700294 ScopedFileWriterCloser writer_closer(&writer);
295 uint64_t out_file_size = 0;
296
Alex Deymoa4073ef2016-03-22 23:40:53 -0700297 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700298 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700299 if (!aop.op.has_data_offset())
300 continue;
301 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700302 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700303 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 Deymo95699a92016-12-08 19:43:01 -0800310 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700311 out_file_size += buf.size();
312 }
313 }
314 return true;
315}
316
Alex Deymoa12ee112015-08-12 22:19:32 -0700317bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700318 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700319 brillo::Blob hash;
320 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700321 op->set_data_sha256_hash(hash.data(), hash.size());
322 return true;
323}
324
325void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600326 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700327 off_t total_size = 0;
328
Sen Jiangb9ef4912015-09-21 15:06:13 -0700329 for (const auto& part : part_vec_) {
330 for (const AnnotatedOperation& aop : part.aops) {
Simon Glass3d32f852017-06-28 16:38:11 -0600331 DeltaObject delta(aop.name, aop.op.type(), aop.op.data_length());
332 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700333 total_size += aop.op.data_length();
334 }
335 }
336
Simon Glass3d32f852017-06-28 16:38:11 -0600337 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700338 total_size += metadata_size;
339
Simon Glass3d32f852017-06-28 16:38:11 -0600340 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 Deymo8241ffb2015-10-15 10:36:44 -0700345 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 Glass3d32f852017-06-28 16:38:11 -0600350 object.name.c_str(),
351 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700352 }
Simon Glass3d32f852017-06-28 16:38:11 -0600353 LOG(INFO) << base::StringPrintf(kFormatString,
354 100.0,
355 static_cast<intmax_t>(total_size),
356 "",
357 "<total>",
358 1);
Alex Deymo14158572015-06-13 03:37:08 -0700359}
360
Alex Deymo14158572015-06-13 03:37:08 -0700361} // namespace chromeos_update_engine