blob: 1388f2da25e123749bc433b3d282284616483dd3 [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)
Amin Hassani232f8f92019-01-14 16:15:31 -080044 : name(in_name), type(in_type), size(in_size) {}
45 bool operator<(const DeltaObject& object) const {
Alex Deymo14158572015-06-13 03:37:08 -070046 return (size != object.size) ? (size < object.size) : (name < object.name);
47 }
48 string name;
49 int type;
50 off_t size;
51};
52
53// Writes the uint64_t passed in in host-endian to the file as big-endian.
54// Returns true on success.
55bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
56 uint64_t value_be = htobe64(value);
57 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
58 return true;
59}
60
61} // namespace
62
Alex Deymo14158572015-06-13 03:37:08 -070063bool PayloadFile::Init(const PayloadGenerationConfig& config) {
Alex Deymoa4073ef2016-03-22 23:40:53 -070064 TEST_AND_RETURN_FALSE(config.version.Validate());
65 major_version_ = config.version.major;
66 manifest_.set_minor_version(config.version.minor);
Alex Deymo14158572015-06-13 03:37:08 -070067
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);
Sen Jiang5011df62017-06-28 17:13:19 -070075 manifest_.set_max_timestamp(config.max_timestamp);
Yifan Hong398cb542018-10-18 11:29:40 -070076
Amin Hassani55c75412019-10-07 11:20:39 -070077 if (config.target.dynamic_partition_metadata != nullptr)
78 *(manifest_.mutable_dynamic_partition_metadata()) =
79 *(config.target.dynamic_partition_metadata);
Yifan Hong398cb542018-10-18 11:29:40 -070080
Tianjief5baff42020-07-17 21:43:22 -070081 if (config.is_partial_update) {
82 manifest_.set_partial_update(true);
83 }
Alex Deymo14158572015-06-13 03:37:08 -070084 return true;
85}
86
Sen Jiangb9ef4912015-09-21 15:06:13 -070087bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
88 const PartitionConfig& new_conf,
Kelvin Zhangb0b9c202020-07-24 16:02:09 -040089 vector<AnnotatedOperation> aops) {
Sen Jiangb9ef4912015-09-21 15:06:13 -070090 Partition part;
91 part.name = new_conf.name;
Kelvin Zhangb0b9c202020-07-24 16:02:09 -040092 part.aops = std::move(aops);
Sen Jiang05feee02015-11-11 15:59:49 -080093 part.postinstall = new_conf.postinstall;
Sen Jiang3a4dfac2018-08-30 16:57:38 -070094 part.verity = new_conf.verity;
Kelvin Zhang1f496422020-08-11 17:18:23 -040095 part.version = new_conf.version;
Sen Jiangb9ef4912015-09-21 15:06:13 -070096 // Initialize the PartitionInfo objects if present.
97 if (!old_conf.path.empty())
Amin Hassani232f8f92019-01-14 16:15:31 -080098 TEST_AND_RETURN_FALSE(
99 diff_utils::InitializePartitionInfo(old_conf, &part.old_info));
100 TEST_AND_RETURN_FALSE(
101 diff_utils::InitializePartitionInfo(new_conf, &part.new_info));
Sen Jiangb9ef4912015-09-21 15:06:13 -0700102 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(
Amin Hassani232f8f92019-01-14 16:15:31 -0800113 "CrAU_temp_data.ordered.XXXXXX", &ordered_blobs_path, nullptr));
Alex Deymo14158572015-06-13 03:37:08 -0700114 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) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800124 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_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700133 for (const auto& part : part_vec_) {
Amin Hassani55c75412019-10-07 11:20:39 -0700134 PartitionUpdate* partition = manifest_.add_partitions();
135 partition->set_partition_name(part.name);
Kelvin Zhang1f496422020-08-11 17:18:23 -0400136 if (!part.version.empty()) {
137 partition->set_version(part.version);
138 }
Amin Hassani55c75412019-10-07 11:20:39 -0700139 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);
145 partition->set_postinstall_optional(part.postinstall.optional);
146 }
147 if (!part.verity.IsEmpty()) {
148 if (part.verity.hash_tree_extent.num_blocks() != 0) {
149 *partition->mutable_hash_tree_data_extent() =
150 part.verity.hash_tree_data_extent;
151 *partition->mutable_hash_tree_extent() = part.verity.hash_tree_extent;
152 partition->set_hash_tree_algorithm(part.verity.hash_tree_algorithm);
153 if (!part.verity.hash_tree_salt.empty())
154 partition->set_hash_tree_salt(part.verity.hash_tree_salt.data(),
155 part.verity.hash_tree_salt.size());
Sen Jiang05feee02015-11-11 15:59:49 -0800156 }
Amin Hassani55c75412019-10-07 11:20:39 -0700157 if (part.verity.fec_extent.num_blocks() != 0) {
158 *partition->mutable_fec_data_extent() = part.verity.fec_data_extent;
159 *partition->mutable_fec_extent() = part.verity.fec_extent;
160 partition->set_fec_roots(part.verity.fec_roots);
Alex Deymo14158572015-06-13 03:37:08 -0700161 }
162 }
Amin Hassani55c75412019-10-07 11:20:39 -0700163 for (const AnnotatedOperation& aop : part.aops) {
164 *partition->add_operations() = aop.op;
165 }
166 if (part.old_info.has_size() || part.old_info.has_hash())
167 *(partition->mutable_old_partition_info()) = part.old_info;
168 if (part.new_info.has_size() || part.new_info.has_hash())
169 *(partition->mutable_new_partition_info()) = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700170 }
171
172 // Signatures appear at the end of the blobs. Note the offset in the
Amin Hassani55c75412019-10-07 11:20:39 -0700173 // |manifest_|.
Sen Jiang644f6182015-10-06 16:45:57 -0700174 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700175 if (!private_key_path.empty()) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800176 TEST_AND_RETURN_FALSE(PayloadSigner::SignatureBlobLength(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800177 {private_key_path}, &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800178 PayloadSigner::AddSignatureToManifest(
Kelvin Zhangb0b9c202020-07-24 16:02:09 -0400179 next_blob_offset, signature_blob_length, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700180 }
181
Sen Jiang70a6ab02015-08-28 13:23:27 -0700182 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700183 string serialized_manifest;
Sen Jiang9b2f1782019-01-24 14:27:50 -0800184 TEST_AND_RETURN_FALSE(manifest_.SerializeToString(&serialized_manifest));
Alex Deymo14158572015-06-13 03:37:08 -0700185
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700186 uint64_t metadata_size =
187 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
188
Alex Deymo14158572015-06-13 03:37:08 -0700189 LOG(INFO) << "Writing final delta file header...";
190 DirectFileWriter writer;
191 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
192 O_WRONLY | O_CREAT | O_TRUNC,
193 0644) == 0);
194 ScopedFileWriterCloser writer_closer(&writer);
195
196 // Write header
Alex Deymo95699a92016-12-08 19:43:01 -0800197 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700198
199 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700200 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700201
202 // Write protobuf length
Amin Hassani232f8f92019-01-14 16:15:31 -0800203 TEST_AND_RETURN_FALSE(
204 WriteUint64AsBigEndian(&writer, serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700205
Amin Hassani55c75412019-10-07 11:20:39 -0700206 // Metadata signature has the same size as payload signature, because they
207 // are both the same kind of signature for the same kind of hash.
208 uint32_t metadata_signature_size = htobe32(signature_blob_length);
209 TEST_AND_RETURN_FALSE_ERRNO(
210 writer.Write(&metadata_signature_size, sizeof(metadata_signature_size)));
211 metadata_size += sizeof(metadata_signature_size);
212 // Set correct size instead of big endian size.
213 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700214
Alex Deymo14158572015-06-13 03:37:08 -0700215 // Write protobuf
216 LOG(INFO) << "Writing final delta file protobuf... "
217 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800218 TEST_AND_RETURN_FALSE_ERRNO(
219 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700220
Sen Jiang644f6182015-10-06 16:45:57 -0700221 // Write metadata signature blob.
Amin Hassani55c75412019-10-07 11:20:39 -0700222 if (!private_key_path.empty()) {
Sen Jiang9b2f1782019-01-24 14:27:50 -0800223 brillo::Blob metadata_hash;
Amin Hassani232f8f92019-01-14 16:15:31 -0800224 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
225 payload_file, metadata_size, &metadata_hash));
Sen Jiang9b2f1782019-01-24 14:27:50 -0800226 string metadata_signature;
227 TEST_AND_RETURN_FALSE(PayloadSigner::SignHashWithKeys(
228 metadata_hash, {private_key_path}, &metadata_signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800229 TEST_AND_RETURN_FALSE_ERRNO(
230 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700231 }
232
Amin Hassani55c75412019-10-07 11:20:39 -0700233 // Append the data blobs.
Alex Deymo14158572015-06-13 03:37:08 -0700234 LOG(INFO) << "Writing final delta file data blobs...";
235 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
236 ScopedFdCloser blobs_fd_closer(&blobs_fd);
237 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
238 for (;;) {
239 vector<char> buf(1024 * 1024);
240 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
241 if (0 == rc) {
242 // EOF
243 break;
244 }
245 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Alex Deymo95699a92016-12-08 19:43:01 -0800246 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700247 }
248
Sen Jiang644f6182015-10-06 16:45:57 -0700249 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700250 if (!private_key_path.empty()) {
251 LOG(INFO) << "Signing the update...";
Sen Jiang9b2f1782019-01-24 14:27:50 -0800252 string signature;
Alex Deymo14158572015-06-13 03:37:08 -0700253 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
254 payload_file,
Sen Jiang9b2f1782019-01-24 14:27:50 -0800255 {private_key_path},
Sen Jiang720df3e2015-10-01 13:10:44 -0700256 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700257 metadata_signature_size,
258 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Sen Jiang9b2f1782019-01-24 14:27:50 -0800259 &signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800260 TEST_AND_RETURN_FALSE_ERRNO(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800261 writer.Write(signature.data(), signature.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700262 }
263
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700264 ReportPayloadUsage(metadata_size);
265 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700266 return true;
267}
268
Amin Hassani232f8f92019-01-14 16:15:31 -0800269bool PayloadFile::ReorderDataBlobs(const string& data_blobs_path,
270 const string& new_data_blobs_path) {
Alex Deymo14158572015-06-13 03:37:08 -0700271 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
272 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
273 ScopedFdCloser in_fd_closer(&in_fd);
274
275 DirectFileWriter writer;
Alex Deymo95699a92016-12-08 19:43:01 -0800276 int rc = writer.Open(
277 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
278 if (rc != 0) {
279 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
280 return false;
281 }
Alex Deymo14158572015-06-13 03:37:08 -0700282 ScopedFileWriterCloser writer_closer(&writer);
283 uint64_t out_file_size = 0;
284
Alex Deymoa4073ef2016-03-22 23:40:53 -0700285 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700286 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700287 if (!aop.op.has_data_offset())
288 continue;
289 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700290 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700291 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
292 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
293
294 // Add the hash of the data blobs for this operation
295 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
296
297 aop.op.set_data_offset(out_file_size);
Alex Deymo95699a92016-12-08 19:43:01 -0800298 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700299 out_file_size += buf.size();
300 }
301 }
302 return true;
303}
304
Alex Deymoa12ee112015-08-12 22:19:32 -0700305bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700306 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700307 brillo::Blob hash;
308 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700309 op->set_data_sha256_hash(hash.data(), hash.size());
310 return true;
311}
312
313void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600314 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700315 off_t total_size = 0;
Sen Jiangf23bf922018-11-01 18:26:07 -0700316 int total_op = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700317
Sen Jiangb9ef4912015-09-21 15:06:13 -0700318 for (const auto& part : part_vec_) {
Sen Jianga91195d2018-10-25 15:15:38 -0700319 string part_prefix = "<" + part.name + ">:";
Sen Jiangb9ef4912015-09-21 15:06:13 -0700320 for (const AnnotatedOperation& aop : part.aops) {
Sen Jianga91195d2018-10-25 15:15:38 -0700321 DeltaObject delta(
322 part_prefix + aop.name, aop.op.type(), aop.op.data_length());
Simon Glass3d32f852017-06-28 16:38:11 -0600323 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700324 total_size += aop.op.data_length();
325 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700326 total_op += part.aops.size();
Alex Deymo14158572015-06-13 03:37:08 -0700327 }
328
Simon Glass3d32f852017-06-28 16:38:11 -0600329 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700330 total_size += metadata_size;
331
Sen Jianga91195d2018-10-25 15:15:38 -0700332 constexpr char kFormatString[] = "%6.2f%% %10jd %-13s %s %d\n";
Simon Glass3d32f852017-06-28 16:38:11 -0600333 for (const auto& object_count : object_counts) {
334 const DeltaObject& object = object_count.first;
Sen Jianga91195d2018-10-25 15:15:38 -0700335 // Use printf() instead of LOG(INFO) because timestamp makes it difficult to
336 // compare two reports.
Sen Jiangcebb6882019-02-26 16:23:28 +0800337 printf(kFormatString,
338 object.size * 100.0 / total_size,
339 object.size,
340 (object.type >= 0
341 ? InstallOperationTypeName(
342 static_cast<InstallOperation::Type>(object.type))
343 : "-"),
344 object.name.c_str(),
345 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700346 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700347 printf(kFormatString, 100.0, total_size, "", "<total>", total_op);
348 fflush(stdout);
Alex Deymo14158572015-06-13 03:37:08 -0700349}
350
Alex Deymo14158572015-06-13 03:37:08 -0700351} // namespace chromeos_update_engine