blob: 2688e9afa2de3c5d91cb91a7baf2e8db05b6e008 [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 manifest_.set_block_size(config.block_size);
Sen Jiang5011df62017-06-28 17:13:19 -070068 manifest_.set_max_timestamp(config.max_timestamp);
Yifan Hong398cb542018-10-18 11:29:40 -070069
Amin Hassani55c75412019-10-07 11:20:39 -070070 if (config.target.dynamic_partition_metadata != nullptr)
71 *(manifest_.mutable_dynamic_partition_metadata()) =
72 *(config.target.dynamic_partition_metadata);
Yifan Hong398cb542018-10-18 11:29:40 -070073
Tianjief5baff42020-07-17 21:43:22 -070074 if (config.is_partial_update) {
75 manifest_.set_partial_update(true);
76 }
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,
Tianjiee9156ec2020-08-11 11:13:54 -070082 vector<AnnotatedOperation> aops,
83 vector<CowMergeOperation> merge_sequence) {
Sen Jiangb9ef4912015-09-21 15:06:13 -070084 Partition part;
85 part.name = new_conf.name;
Kelvin Zhangb0b9c202020-07-24 16:02:09 -040086 part.aops = std::move(aops);
Tianjiee9156ec2020-08-11 11:13:54 -070087 part.cow_merge_sequence = std::move(merge_sequence);
Sen Jiang05feee02015-11-11 15:59:49 -080088 part.postinstall = new_conf.postinstall;
Sen Jiang3a4dfac2018-08-30 16:57:38 -070089 part.verity = new_conf.verity;
Kelvin Zhang1f496422020-08-11 17:18:23 -040090 part.version = new_conf.version;
Sen Jiangb9ef4912015-09-21 15:06:13 -070091 // Initialize the PartitionInfo objects if present.
92 if (!old_conf.path.empty())
Amin Hassani232f8f92019-01-14 16:15:31 -080093 TEST_AND_RETURN_FALSE(
94 diff_utils::InitializePartitionInfo(old_conf, &part.old_info));
95 TEST_AND_RETURN_FALSE(
96 diff_utils::InitializePartitionInfo(new_conf, &part.new_info));
Sen Jiangb9ef4912015-09-21 15:06:13 -070097 part_vec_.push_back(std::move(part));
Sen Jiang70a6ab02015-08-28 13:23:27 -070098 return true;
Alex Deymo14158572015-06-13 03:37:08 -070099}
100
101bool PayloadFile::WritePayload(const string& payload_file,
102 const string& data_blobs_path,
103 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700104 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700105 // Reorder the data blobs with the manifest_.
106 string ordered_blobs_path;
107 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
Amin Hassani232f8f92019-01-14 16:15:31 -0800108 "CrAU_temp_data.ordered.XXXXXX", &ordered_blobs_path, nullptr));
Alex Deymo14158572015-06-13 03:37:08 -0700109 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
110 TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path));
111
Sen Jiang70a6ab02015-08-28 13:23:27 -0700112 // Check that install op blobs are in order.
113 uint64_t next_blob_offset = 0;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700114 for (const auto& part : part_vec_) {
115 for (const auto& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700116 if (!aop.op.has_data_offset())
117 continue;
118 if (aop.op.data_offset() != next_blob_offset) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800119 LOG(FATAL) << "bad blob offset! " << aop.op.data_offset()
120 << " != " << next_blob_offset;
Alex Deymo14158572015-06-13 03:37:08 -0700121 }
Sen Jiang70a6ab02015-08-28 13:23:27 -0700122 next_blob_offset += aop.op.data_length();
Alex Deymo14158572015-06-13 03:37:08 -0700123 }
124 }
125
Sen Jiangb9ef4912015-09-21 15:06:13 -0700126 // Copy the operations and partition info from the part_vec_ to the manifest.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700127 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700128 for (const auto& part : part_vec_) {
Amin Hassani55c75412019-10-07 11:20:39 -0700129 PartitionUpdate* partition = manifest_.add_partitions();
130 partition->set_partition_name(part.name);
Kelvin Zhang1f496422020-08-11 17:18:23 -0400131 if (!part.version.empty()) {
132 partition->set_version(part.version);
133 }
Amin Hassani55c75412019-10-07 11:20:39 -0700134 if (part.postinstall.run) {
135 partition->set_run_postinstall(true);
136 if (!part.postinstall.path.empty())
137 partition->set_postinstall_path(part.postinstall.path);
138 if (!part.postinstall.filesystem_type.empty())
139 partition->set_filesystem_type(part.postinstall.filesystem_type);
140 partition->set_postinstall_optional(part.postinstall.optional);
141 }
142 if (!part.verity.IsEmpty()) {
143 if (part.verity.hash_tree_extent.num_blocks() != 0) {
144 *partition->mutable_hash_tree_data_extent() =
145 part.verity.hash_tree_data_extent;
146 *partition->mutable_hash_tree_extent() = part.verity.hash_tree_extent;
147 partition->set_hash_tree_algorithm(part.verity.hash_tree_algorithm);
148 if (!part.verity.hash_tree_salt.empty())
149 partition->set_hash_tree_salt(part.verity.hash_tree_salt.data(),
150 part.verity.hash_tree_salt.size());
Sen Jiang05feee02015-11-11 15:59:49 -0800151 }
Amin Hassani55c75412019-10-07 11:20:39 -0700152 if (part.verity.fec_extent.num_blocks() != 0) {
153 *partition->mutable_fec_data_extent() = part.verity.fec_data_extent;
154 *partition->mutable_fec_extent() = part.verity.fec_extent;
155 partition->set_fec_roots(part.verity.fec_roots);
Alex Deymo14158572015-06-13 03:37:08 -0700156 }
157 }
Amin Hassani55c75412019-10-07 11:20:39 -0700158 for (const AnnotatedOperation& aop : part.aops) {
159 *partition->add_operations() = aop.op;
160 }
Tianjiee9156ec2020-08-11 11:13:54 -0700161 for (const auto& merge_op : part.cow_merge_sequence) {
162 *partition->add_merge_operations() = merge_op;
163 }
164
Amin Hassani55c75412019-10-07 11:20:39 -0700165 if (part.old_info.has_size() || part.old_info.has_hash())
166 *(partition->mutable_old_partition_info()) = part.old_info;
167 if (part.new_info.has_size() || part.new_info.has_hash())
168 *(partition->mutable_new_partition_info()) = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700169 }
170
171 // Signatures appear at the end of the blobs. Note the offset in the
Amin Hassani55c75412019-10-07 11:20:39 -0700172 // |manifest_|.
Sen Jiang644f6182015-10-06 16:45:57 -0700173 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700174 if (!private_key_path.empty()) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800175 TEST_AND_RETURN_FALSE(PayloadSigner::SignatureBlobLength(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800176 {private_key_path}, &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800177 PayloadSigner::AddSignatureToManifest(
Kelvin Zhangb0b9c202020-07-24 16:02:09 -0400178 next_blob_offset, signature_blob_length, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700179 }
180
Sen Jiang70a6ab02015-08-28 13:23:27 -0700181 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700182 string serialized_manifest;
Sen Jiang9b2f1782019-01-24 14:27:50 -0800183 TEST_AND_RETURN_FALSE(manifest_.SerializeToString(&serialized_manifest));
Alex Deymo14158572015-06-13 03:37:08 -0700184
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700185 uint64_t metadata_size =
186 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
187
Alex Deymo14158572015-06-13 03:37:08 -0700188 LOG(INFO) << "Writing final delta file header...";
189 DirectFileWriter writer;
190 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
191 O_WRONLY | O_CREAT | O_TRUNC,
192 0644) == 0);
193 ScopedFileWriterCloser writer_closer(&writer);
194
195 // Write header
Alex Deymo95699a92016-12-08 19:43:01 -0800196 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700197
198 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700199 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700200
201 // Write protobuf length
Amin Hassani232f8f92019-01-14 16:15:31 -0800202 TEST_AND_RETURN_FALSE(
203 WriteUint64AsBigEndian(&writer, serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700204
Amin Hassani55c75412019-10-07 11:20:39 -0700205 // Metadata signature has the same size as payload signature, because they
206 // are both the same kind of signature for the same kind of hash.
207 uint32_t metadata_signature_size = htobe32(signature_blob_length);
208 TEST_AND_RETURN_FALSE_ERRNO(
209 writer.Write(&metadata_signature_size, sizeof(metadata_signature_size)));
210 metadata_size += sizeof(metadata_signature_size);
211 // Set correct size instead of big endian size.
212 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700213
Alex Deymo14158572015-06-13 03:37:08 -0700214 // Write protobuf
215 LOG(INFO) << "Writing final delta file protobuf... "
216 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800217 TEST_AND_RETURN_FALSE_ERRNO(
218 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700219
Sen Jiang644f6182015-10-06 16:45:57 -0700220 // Write metadata signature blob.
Amin Hassani55c75412019-10-07 11:20:39 -0700221 if (!private_key_path.empty()) {
Sen Jiang9b2f1782019-01-24 14:27:50 -0800222 brillo::Blob metadata_hash;
Amin Hassani232f8f92019-01-14 16:15:31 -0800223 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
224 payload_file, metadata_size, &metadata_hash));
Sen Jiang9b2f1782019-01-24 14:27:50 -0800225 string metadata_signature;
226 TEST_AND_RETURN_FALSE(PayloadSigner::SignHashWithKeys(
227 metadata_hash, {private_key_path}, &metadata_signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800228 TEST_AND_RETURN_FALSE_ERRNO(
229 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700230 }
231
Amin Hassani55c75412019-10-07 11:20:39 -0700232 // Append the data blobs.
Alex Deymo14158572015-06-13 03:37:08 -0700233 LOG(INFO) << "Writing final delta file data blobs...";
234 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
235 ScopedFdCloser blobs_fd_closer(&blobs_fd);
236 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
237 for (;;) {
238 vector<char> buf(1024 * 1024);
239 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
240 if (0 == rc) {
241 // EOF
242 break;
243 }
244 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Alex Deymo95699a92016-12-08 19:43:01 -0800245 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700246 }
247
Sen Jiang644f6182015-10-06 16:45:57 -0700248 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700249 if (!private_key_path.empty()) {
250 LOG(INFO) << "Signing the update...";
Sen Jiang9b2f1782019-01-24 14:27:50 -0800251 string signature;
Alex Deymo14158572015-06-13 03:37:08 -0700252 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
253 payload_file,
Sen Jiang9b2f1782019-01-24 14:27:50 -0800254 {private_key_path},
Sen Jiang720df3e2015-10-01 13:10:44 -0700255 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700256 metadata_signature_size,
257 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Sen Jiang9b2f1782019-01-24 14:27:50 -0800258 &signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800259 TEST_AND_RETURN_FALSE_ERRNO(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800260 writer.Write(signature.data(), signature.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700261 }
262
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700263 ReportPayloadUsage(metadata_size);
264 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700265 return true;
266}
267
Amin Hassani232f8f92019-01-14 16:15:31 -0800268bool PayloadFile::ReorderDataBlobs(const string& data_blobs_path,
269 const string& new_data_blobs_path) {
Alex Deymo14158572015-06-13 03:37:08 -0700270 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
271 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
272 ScopedFdCloser in_fd_closer(&in_fd);
273
274 DirectFileWriter writer;
Alex Deymo95699a92016-12-08 19:43:01 -0800275 int rc = writer.Open(
276 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
277 if (rc != 0) {
278 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
279 return false;
280 }
Alex Deymo14158572015-06-13 03:37:08 -0700281 ScopedFileWriterCloser writer_closer(&writer);
282 uint64_t out_file_size = 0;
283
Alex Deymoa4073ef2016-03-22 23:40:53 -0700284 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700285 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700286 if (!aop.op.has_data_offset())
287 continue;
288 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700289 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700290 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
291 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
292
293 // Add the hash of the data blobs for this operation
294 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
295
296 aop.op.set_data_offset(out_file_size);
Alex Deymo95699a92016-12-08 19:43:01 -0800297 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700298 out_file_size += buf.size();
299 }
300 }
301 return true;
302}
303
Alex Deymoa12ee112015-08-12 22:19:32 -0700304bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700305 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700306 brillo::Blob hash;
307 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700308 op->set_data_sha256_hash(hash.data(), hash.size());
309 return true;
310}
311
312void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600313 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700314 off_t total_size = 0;
Sen Jiangf23bf922018-11-01 18:26:07 -0700315 int total_op = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700316
Sen Jiangb9ef4912015-09-21 15:06:13 -0700317 for (const auto& part : part_vec_) {
Sen Jianga91195d2018-10-25 15:15:38 -0700318 string part_prefix = "<" + part.name + ">:";
Sen Jiangb9ef4912015-09-21 15:06:13 -0700319 for (const AnnotatedOperation& aop : part.aops) {
Sen Jianga91195d2018-10-25 15:15:38 -0700320 DeltaObject delta(
321 part_prefix + aop.name, aop.op.type(), aop.op.data_length());
Simon Glass3d32f852017-06-28 16:38:11 -0600322 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700323 total_size += aop.op.data_length();
324 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700325 total_op += part.aops.size();
Alex Deymo14158572015-06-13 03:37:08 -0700326 }
327
Simon Glass3d32f852017-06-28 16:38:11 -0600328 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700329 total_size += metadata_size;
330
Sen Jianga91195d2018-10-25 15:15:38 -0700331 constexpr char kFormatString[] = "%6.2f%% %10jd %-13s %s %d\n";
Simon Glass3d32f852017-06-28 16:38:11 -0600332 for (const auto& object_count : object_counts) {
333 const DeltaObject& object = object_count.first;
Sen Jianga91195d2018-10-25 15:15:38 -0700334 // Use printf() instead of LOG(INFO) because timestamp makes it difficult to
335 // compare two reports.
Sen Jiangcebb6882019-02-26 16:23:28 +0800336 printf(kFormatString,
337 object.size * 100.0 / total_size,
338 object.size,
339 (object.type >= 0
340 ? InstallOperationTypeName(
341 static_cast<InstallOperation::Type>(object.type))
342 : "-"),
343 object.name.c_str(),
344 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700345 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700346 printf(kFormatString, 100.0, total_size, "", "<total>", total_op);
347 fflush(stdout);
Alex Deymo14158572015-06-13 03:37:08 -0700348}
349
Alex Deymo14158572015-06-13 03:37:08 -0700350} // namespace chromeos_update_engine