blob: 13cf4895b5b4635d076663bfb34fbda38cd72799 [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>
Sen Jiang3a4dfac2018-08-30 16:57:38 -070023#include <utility>
Simon Glass3d32f852017-06-28 16:38:11 -060024
25#include <base/strings/stringprintf.h>
Alex Deymo14158572015-06-13 03:37:08 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#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 Deymo14158572015-06-13 03:37:08 -070031#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo14158572015-06-13 03:37:08 -070032#include "update_engine/payload_generator/delta_diff_utils.h"
33#include "update_engine/payload_generator/payload_signer.h"
34
35using std::string;
36using std::vector;
37
38namespace chromeos_update_engine {
39
40namespace {
41
Alex Deymo14158572015-06-13 03:37:08 -070042struct DeltaObject {
43 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
44 : name(in_name),
45 type(in_type),
46 size(in_size) {}
47 bool operator <(const DeltaObject& object) const {
48 return (size != object.size) ? (size < object.size) : (name < object.name);
49 }
50 string name;
51 int type;
52 off_t size;
53};
54
55// Writes the uint64_t passed in in host-endian to the file as big-endian.
56// Returns true on success.
57bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
58 uint64_t value_be = htobe64(value);
59 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
60 return true;
61}
62
63} // namespace
64
Alex Deymo14158572015-06-13 03:37:08 -070065bool PayloadFile::Init(const PayloadGenerationConfig& config) {
Alex Deymoa4073ef2016-03-22 23:40:53 -070066 TEST_AND_RETURN_FALSE(config.version.Validate());
67 major_version_ = config.version.major;
68 manifest_.set_minor_version(config.version.minor);
Alex Deymo14158572015-06-13 03:37:08 -070069
70 if (!config.source.ImageInfoIsEmpty())
71 *(manifest_.mutable_old_image_info()) = config.source.image_info;
72
73 if (!config.target.ImageInfoIsEmpty())
74 *(manifest_.mutable_new_image_info()) = config.target.image_info;
75
76 manifest_.set_block_size(config.block_size);
Sen Jiang5011df62017-06-28 17:13:19 -070077 manifest_.set_max_timestamp(config.max_timestamp);
Alex Deymo14158572015-06-13 03:37:08 -070078 return true;
79}
80
Sen Jiangb9ef4912015-09-21 15:06:13 -070081bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
82 const PartitionConfig& new_conf,
83 const vector<AnnotatedOperation>& aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -070084 // Check partitions order for Chrome OS
85 if (major_version_ == kChromeOSMajorPayloadVersion) {
Tudor Brindusdda79e22018-06-28 18:03:21 -070086 const vector<const char*> part_order = { kPartitionNameRoot,
87 kPartitionNameKernel };
Sen Jiangb9ef4912015-09-21 15:06:13 -070088 TEST_AND_RETURN_FALSE(part_vec_.size() < part_order.size());
89 TEST_AND_RETURN_FALSE(new_conf.name == part_order[part_vec_.size()]);
Sen Jiang70a6ab02015-08-28 13:23:27 -070090 }
Sen Jiangb9ef4912015-09-21 15:06:13 -070091 Partition part;
92 part.name = new_conf.name;
93 part.aops = aops;
Sen Jiang05feee02015-11-11 15:59:49 -080094 part.postinstall = new_conf.postinstall;
Sen Jiang3a4dfac2018-08-30 16:57:38 -070095 part.verity = new_conf.verity;
Sen Jiangb9ef4912015-09-21 15:06:13 -070096 // Initialize the PartitionInfo objects if present.
97 if (!old_conf.path.empty())
98 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(old_conf,
99 &part.old_info));
100 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(new_conf,
101 &part.new_info));
102 part_vec_.push_back(std::move(part));
Sen Jiang70a6ab02015-08-28 13:23:27 -0700103 return true;
Alex Deymo14158572015-06-13 03:37:08 -0700104}
105
106bool PayloadFile::WritePayload(const string& payload_file,
107 const string& data_blobs_path,
108 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700109 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700110 // Reorder the data blobs with the manifest_.
111 string ordered_blobs_path;
112 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
113 "CrAU_temp_data.ordered.XXXXXX",
114 &ordered_blobs_path,
115 nullptr));
116 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
117 TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path));
118
Sen Jiang70a6ab02015-08-28 13:23:27 -0700119 // Check that install op blobs are in order.
120 uint64_t next_blob_offset = 0;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700121 for (const auto& part : part_vec_) {
122 for (const auto& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700123 if (!aop.op.has_data_offset())
124 continue;
125 if (aop.op.data_offset() != next_blob_offset) {
126 LOG(FATAL) << "bad blob offset! " << aop.op.data_offset() << " != "
127 << next_blob_offset;
Alex Deymo14158572015-06-13 03:37:08 -0700128 }
Sen Jiang70a6ab02015-08-28 13:23:27 -0700129 next_blob_offset += aop.op.data_length();
Alex Deymo14158572015-06-13 03:37:08 -0700130 }
131 }
132
Sen Jiangb9ef4912015-09-21 15:06:13 -0700133 // Copy the operations and partition info from the part_vec_ to the manifest.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700134 manifest_.clear_install_operations();
135 manifest_.clear_kernel_install_operations();
136 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700137 for (const auto& part : part_vec_) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700138 if (major_version_ == kBrilloMajorPayloadVersion) {
139 PartitionUpdate* partition = manifest_.add_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700140 partition->set_partition_name(part.name);
Sen Jiang05feee02015-11-11 15:59:49 -0800141 if (part.postinstall.run) {
142 partition->set_run_postinstall(true);
143 if (!part.postinstall.path.empty())
144 partition->set_postinstall_path(part.postinstall.path);
145 if (!part.postinstall.filesystem_type.empty())
146 partition->set_filesystem_type(part.postinstall.filesystem_type);
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700147 partition->set_postinstall_optional(part.postinstall.optional);
Sen Jiang05feee02015-11-11 15:59:49 -0800148 }
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700149 if (!part.verity.IsEmpty()) {
150 if (part.verity.hash_tree_extent.num_blocks() != 0) {
151 *partition->mutable_hash_tree_data_extent() =
152 part.verity.hash_tree_data_extent;
153 *partition->mutable_hash_tree_extent() = part.verity.hash_tree_extent;
154 partition->set_hash_tree_algorithm(part.verity.hash_tree_algorithm);
155 if (!part.verity.hash_tree_salt.empty())
156 partition->set_hash_tree_salt(part.verity.hash_tree_salt.data(),
157 part.verity.hash_tree_salt.size());
158 }
159 if (part.verity.fec_extent.num_blocks() != 0) {
160 *partition->mutable_fec_data_extent() = part.verity.fec_data_extent;
161 *partition->mutable_fec_extent() = part.verity.fec_extent;
162 partition->set_fec_roots(part.verity.fec_roots);
163 }
164 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700165 for (const AnnotatedOperation& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700166 *partition->add_operations() = aop.op;
167 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700168 if (part.old_info.has_size() || part.old_info.has_hash())
169 *(partition->mutable_old_partition_info()) = part.old_info;
170 if (part.new_info.has_size() || part.new_info.has_hash())
171 *(partition->mutable_new_partition_info()) = part.new_info;
Sen Jiang70a6ab02015-08-28 13:23:27 -0700172 } else {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700173 // major_version_ == kChromeOSMajorPayloadVersion
Tudor Brindusdda79e22018-06-28 18:03:21 -0700174 if (part.name == kPartitionNameKernel) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700175 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700176 *manifest_.add_kernel_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700177 if (part.old_info.has_size() || part.old_info.has_hash())
178 *manifest_.mutable_old_kernel_info() = part.old_info;
179 if (part.new_info.has_size() || part.new_info.has_hash())
180 *manifest_.mutable_new_kernel_info() = part.new_info;
181 } else {
182 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700183 *manifest_.add_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700184 if (part.old_info.has_size() || part.old_info.has_hash())
185 *manifest_.mutable_old_rootfs_info() = part.old_info;
186 if (part.new_info.has_size() || part.new_info.has_hash())
187 *manifest_.mutable_new_rootfs_info() = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700188 }
189 }
190 }
191
192 // Signatures appear at the end of the blobs. Note the offset in the
193 // manifest_.
Sen Jiang644f6182015-10-06 16:45:57 -0700194 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700195 if (!private_key_path.empty()) {
Alex Deymo14158572015-06-13 03:37:08 -0700196 TEST_AND_RETURN_FALSE(
197 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
198 &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800199 PayloadSigner::AddSignatureToManifest(
200 next_blob_offset, signature_blob_length,
201 major_version_ == kChromeOSMajorPayloadVersion, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700202 }
203
Sen Jiang70a6ab02015-08-28 13:23:27 -0700204 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700205 string serialized_manifest;
206 TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest));
207
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700208 uint64_t metadata_size =
209 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
210
Alex Deymo14158572015-06-13 03:37:08 -0700211 LOG(INFO) << "Writing final delta file header...";
212 DirectFileWriter writer;
213 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
214 O_WRONLY | O_CREAT | O_TRUNC,
215 0644) == 0);
216 ScopedFileWriterCloser writer_closer(&writer);
217
218 // Write header
Alex Deymo95699a92016-12-08 19:43:01 -0800219 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700220
221 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700222 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700223
224 // Write protobuf length
225 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
226 serialized_manifest.size()));
227
Sen Jiang644f6182015-10-06 16:45:57 -0700228 // Write metadata signature size.
229 uint32_t metadata_signature_size = 0;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700230 if (major_version_ == kBrilloMajorPayloadVersion) {
Sen Jiang644f6182015-10-06 16:45:57 -0700231 // Metadata signature has the same size as payload signature, because they
232 // are both the same kind of signature for the same kind of hash.
233 uint32_t metadata_signature_size = htobe32(signature_blob_length);
Alex Deymo95699a92016-12-08 19:43:01 -0800234 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(&metadata_signature_size,
235 sizeof(metadata_signature_size)));
Sen Jiang644f6182015-10-06 16:45:57 -0700236 metadata_size += sizeof(metadata_signature_size);
237 // Set correct size instead of big endian size.
238 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700239 }
240
Alex Deymo14158572015-06-13 03:37:08 -0700241 // Write protobuf
242 LOG(INFO) << "Writing final delta file protobuf... "
243 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800244 TEST_AND_RETURN_FALSE_ERRNO(
245 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700246
Sen Jiang644f6182015-10-06 16:45:57 -0700247 // Write metadata signature blob.
248 if (major_version_ == kBrilloMajorPayloadVersion &&
249 !private_key_path.empty()) {
250 brillo::Blob metadata_hash, metadata_signature;
Alex Deymo39910dc2015-11-09 17:04:30 -0800251 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(payload_file,
Sen Jiang644f6182015-10-06 16:45:57 -0700252 metadata_size,
253 &metadata_hash));
254 TEST_AND_RETURN_FALSE(
255 PayloadSigner::SignHashWithKeys(metadata_hash,
256 vector<string>(1, private_key_path),
257 &metadata_signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800258 TEST_AND_RETURN_FALSE_ERRNO(
259 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700260 }
261
Alex Deymo14158572015-06-13 03:37:08 -0700262 // Append the data blobs
263 LOG(INFO) << "Writing final delta file data blobs...";
264 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
265 ScopedFdCloser blobs_fd_closer(&blobs_fd);
266 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
267 for (;;) {
268 vector<char> buf(1024 * 1024);
269 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
270 if (0 == rc) {
271 // EOF
272 break;
273 }
274 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Alex Deymo95699a92016-12-08 19:43:01 -0800275 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700276 }
277
Sen Jiang644f6182015-10-06 16:45:57 -0700278 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700279 if (!private_key_path.empty()) {
280 LOG(INFO) << "Signing the update...";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700281 brillo::Blob signature_blob;
Alex Deymo14158572015-06-13 03:37:08 -0700282 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
283 payload_file,
284 vector<string>(1, private_key_path),
Sen Jiang720df3e2015-10-01 13:10:44 -0700285 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700286 metadata_signature_size,
287 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Alex Deymo14158572015-06-13 03:37:08 -0700288 &signature_blob));
Alex Deymo95699a92016-12-08 19:43:01 -0800289 TEST_AND_RETURN_FALSE_ERRNO(
290 writer.Write(signature_blob.data(), signature_blob.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700291 }
292
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700293 ReportPayloadUsage(metadata_size);
294 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700295 return true;
296}
297
298bool PayloadFile::ReorderDataBlobs(
299 const string& data_blobs_path,
300 const string& new_data_blobs_path) {
301 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
302 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
303 ScopedFdCloser in_fd_closer(&in_fd);
304
305 DirectFileWriter writer;
Alex Deymo95699a92016-12-08 19:43:01 -0800306 int rc = writer.Open(
307 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
308 if (rc != 0) {
309 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
310 return false;
311 }
Alex Deymo14158572015-06-13 03:37:08 -0700312 ScopedFileWriterCloser writer_closer(&writer);
313 uint64_t out_file_size = 0;
314
Alex Deymoa4073ef2016-03-22 23:40:53 -0700315 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700316 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700317 if (!aop.op.has_data_offset())
318 continue;
319 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700320 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700321 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
322 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
323
324 // Add the hash of the data blobs for this operation
325 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
326
327 aop.op.set_data_offset(out_file_size);
Alex Deymo95699a92016-12-08 19:43:01 -0800328 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700329 out_file_size += buf.size();
330 }
331 }
332 return true;
333}
334
Alex Deymoa12ee112015-08-12 22:19:32 -0700335bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700336 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700337 brillo::Blob hash;
338 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700339 op->set_data_sha256_hash(hash.data(), hash.size());
340 return true;
341}
342
343void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600344 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700345 off_t total_size = 0;
346
Sen Jiangb9ef4912015-09-21 15:06:13 -0700347 for (const auto& part : part_vec_) {
Sen Jianga91195d2018-10-25 15:15:38 -0700348 string part_prefix = "<" + part.name + ">:";
Sen Jiangb9ef4912015-09-21 15:06:13 -0700349 for (const AnnotatedOperation& aop : part.aops) {
Sen Jianga91195d2018-10-25 15:15:38 -0700350 DeltaObject delta(
351 part_prefix + aop.name, aop.op.type(), aop.op.data_length());
Simon Glass3d32f852017-06-28 16:38:11 -0600352 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700353 total_size += aop.op.data_length();
354 }
355 }
356
Simon Glass3d32f852017-06-28 16:38:11 -0600357 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700358 total_size += metadata_size;
359
Sen Jianga91195d2018-10-25 15:15:38 -0700360 constexpr char kFormatString[] = "%6.2f%% %10jd %-13s %s %d\n";
Simon Glass3d32f852017-06-28 16:38:11 -0600361 for (const auto& object_count : object_counts) {
362 const DeltaObject& object = object_count.first;
Sen Jianga91195d2018-10-25 15:15:38 -0700363 // Use printf() instead of LOG(INFO) because timestamp makes it difficult to
364 // compare two reports.
365 printf(
Simon Glass3d32f852017-06-28 16:38:11 -0600366 kFormatString,
Alex Deymo8241ffb2015-10-15 10:36:44 -0700367 object.size * 100.0 / total_size,
Sen Jianga91195d2018-10-25 15:15:38 -0700368 object.size,
Alex Deymo8241ffb2015-10-15 10:36:44 -0700369 (object.type >= 0 ? InstallOperationTypeName(
370 static_cast<InstallOperation_Type>(object.type))
371 : "-"),
Simon Glass3d32f852017-06-28 16:38:11 -0600372 object.name.c_str(),
373 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700374 }
Sen Jianga91195d2018-10-25 15:15:38 -0700375 printf(kFormatString, 100.0, total_size, "", "<total>", 1);
Alex Deymo14158572015-06-13 03:37:08 -0700376}
377
Alex Deymo14158572015-06-13 03:37:08 -0700378} // namespace chromeos_update_engine