blob: 69325d77ef75aaace9a27d44ff7cd89b81b34633 [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
Alex Deymo14158572015-06-13 03:37:08 -070081 return true;
82}
83
Sen Jiangb9ef4912015-09-21 15:06:13 -070084bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
85 const PartitionConfig& new_conf,
86 const vector<AnnotatedOperation>& aops) {
Sen Jiangb9ef4912015-09-21 15:06:13 -070087 Partition part;
88 part.name = new_conf.name;
89 part.aops = aops;
Sen Jiang05feee02015-11-11 15:59:49 -080090 part.postinstall = new_conf.postinstall;
Sen Jiang3a4dfac2018-08-30 16:57:38 -070091 part.verity = new_conf.verity;
Sen Jiangb9ef4912015-09-21 15:06:13 -070092 // Initialize the PartitionInfo objects if present.
93 if (!old_conf.path.empty())
Amin Hassani232f8f92019-01-14 16:15:31 -080094 TEST_AND_RETURN_FALSE(
95 diff_utils::InitializePartitionInfo(old_conf, &part.old_info));
96 TEST_AND_RETURN_FALSE(
97 diff_utils::InitializePartitionInfo(new_conf, &part.new_info));
Sen Jiangb9ef4912015-09-21 15:06:13 -070098 part_vec_.push_back(std::move(part));
Sen Jiang70a6ab02015-08-28 13:23:27 -070099 return true;
Alex Deymo14158572015-06-13 03:37:08 -0700100}
101
102bool PayloadFile::WritePayload(const string& payload_file,
103 const string& data_blobs_path,
104 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700105 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700106 // Reorder the data blobs with the manifest_.
107 string ordered_blobs_path;
108 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
Amin Hassani232f8f92019-01-14 16:15:31 -0800109 "CrAU_temp_data.ordered.XXXXXX", &ordered_blobs_path, nullptr));
Alex Deymo14158572015-06-13 03:37:08 -0700110 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
111 TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path));
112
Sen Jiang70a6ab02015-08-28 13:23:27 -0700113 // Check that install op blobs are in order.
114 uint64_t next_blob_offset = 0;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700115 for (const auto& part : part_vec_) {
116 for (const auto& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700117 if (!aop.op.has_data_offset())
118 continue;
119 if (aop.op.data_offset() != next_blob_offset) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800120 LOG(FATAL) << "bad blob offset! " << aop.op.data_offset()
121 << " != " << next_blob_offset;
Alex Deymo14158572015-06-13 03:37:08 -0700122 }
Sen Jiang70a6ab02015-08-28 13:23:27 -0700123 next_blob_offset += aop.op.data_length();
Alex Deymo14158572015-06-13 03:37:08 -0700124 }
125 }
126
Sen Jiangb9ef4912015-09-21 15:06:13 -0700127 // Copy the operations and partition info from the part_vec_ to the manifest.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700128 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700129 for (const auto& part : part_vec_) {
Amin Hassani55c75412019-10-07 11:20:39 -0700130 PartitionUpdate* partition = manifest_.add_partitions();
131 partition->set_partition_name(part.name);
132 if (part.postinstall.run) {
133 partition->set_run_postinstall(true);
134 if (!part.postinstall.path.empty())
135 partition->set_postinstall_path(part.postinstall.path);
136 if (!part.postinstall.filesystem_type.empty())
137 partition->set_filesystem_type(part.postinstall.filesystem_type);
138 partition->set_postinstall_optional(part.postinstall.optional);
139 }
140 if (!part.verity.IsEmpty()) {
141 if (part.verity.hash_tree_extent.num_blocks() != 0) {
142 *partition->mutable_hash_tree_data_extent() =
143 part.verity.hash_tree_data_extent;
144 *partition->mutable_hash_tree_extent() = part.verity.hash_tree_extent;
145 partition->set_hash_tree_algorithm(part.verity.hash_tree_algorithm);
146 if (!part.verity.hash_tree_salt.empty())
147 partition->set_hash_tree_salt(part.verity.hash_tree_salt.data(),
148 part.verity.hash_tree_salt.size());
Sen Jiang05feee02015-11-11 15:59:49 -0800149 }
Amin Hassani55c75412019-10-07 11:20:39 -0700150 if (part.verity.fec_extent.num_blocks() != 0) {
151 *partition->mutable_fec_data_extent() = part.verity.fec_data_extent;
152 *partition->mutable_fec_extent() = part.verity.fec_extent;
153 partition->set_fec_roots(part.verity.fec_roots);
Alex Deymo14158572015-06-13 03:37:08 -0700154 }
155 }
Amin Hassani55c75412019-10-07 11:20:39 -0700156 for (const AnnotatedOperation& aop : part.aops) {
157 *partition->add_operations() = aop.op;
158 }
159 if (part.old_info.has_size() || part.old_info.has_hash())
160 *(partition->mutable_old_partition_info()) = part.old_info;
161 if (part.new_info.has_size() || part.new_info.has_hash())
162 *(partition->mutable_new_partition_info()) = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700163 }
164
165 // Signatures appear at the end of the blobs. Note the offset in the
Amin Hassani55c75412019-10-07 11:20:39 -0700166 // |manifest_|.
Sen Jiang644f6182015-10-06 16:45:57 -0700167 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700168 if (!private_key_path.empty()) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800169 TEST_AND_RETURN_FALSE(PayloadSigner::SignatureBlobLength(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800170 {private_key_path}, &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800171 PayloadSigner::AddSignatureToManifest(
Amin Hassani232f8f92019-01-14 16:15:31 -0800172 next_blob_offset,
173 signature_blob_length,
Amin Hassani232f8f92019-01-14 16:15:31 -0800174 &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700175 }
176
Sen Jiang70a6ab02015-08-28 13:23:27 -0700177 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700178 string serialized_manifest;
Sen Jiang9b2f1782019-01-24 14:27:50 -0800179 TEST_AND_RETURN_FALSE(manifest_.SerializeToString(&serialized_manifest));
Alex Deymo14158572015-06-13 03:37:08 -0700180
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700181 uint64_t metadata_size =
182 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
183
Alex Deymo14158572015-06-13 03:37:08 -0700184 LOG(INFO) << "Writing final delta file header...";
185 DirectFileWriter writer;
186 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
187 O_WRONLY | O_CREAT | O_TRUNC,
188 0644) == 0);
189 ScopedFileWriterCloser writer_closer(&writer);
190
191 // Write header
Alex Deymo95699a92016-12-08 19:43:01 -0800192 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700193
194 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700195 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700196
197 // Write protobuf length
Amin Hassani232f8f92019-01-14 16:15:31 -0800198 TEST_AND_RETURN_FALSE(
199 WriteUint64AsBigEndian(&writer, serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700200
Amin Hassani55c75412019-10-07 11:20:39 -0700201 // Metadata signature has the same size as payload signature, because they
202 // are both the same kind of signature for the same kind of hash.
203 uint32_t metadata_signature_size = htobe32(signature_blob_length);
204 TEST_AND_RETURN_FALSE_ERRNO(
205 writer.Write(&metadata_signature_size, sizeof(metadata_signature_size)));
206 metadata_size += sizeof(metadata_signature_size);
207 // Set correct size instead of big endian size.
208 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700209
Alex Deymo14158572015-06-13 03:37:08 -0700210 // Write protobuf
211 LOG(INFO) << "Writing final delta file protobuf... "
212 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800213 TEST_AND_RETURN_FALSE_ERRNO(
214 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700215
Sen Jiang644f6182015-10-06 16:45:57 -0700216 // Write metadata signature blob.
Amin Hassani55c75412019-10-07 11:20:39 -0700217 if (!private_key_path.empty()) {
Sen Jiang9b2f1782019-01-24 14:27:50 -0800218 brillo::Blob metadata_hash;
Amin Hassani232f8f92019-01-14 16:15:31 -0800219 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
220 payload_file, metadata_size, &metadata_hash));
Sen Jiang9b2f1782019-01-24 14:27:50 -0800221 string metadata_signature;
222 TEST_AND_RETURN_FALSE(PayloadSigner::SignHashWithKeys(
223 metadata_hash, {private_key_path}, &metadata_signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800224 TEST_AND_RETURN_FALSE_ERRNO(
225 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700226 }
227
Amin Hassani55c75412019-10-07 11:20:39 -0700228 // Append the data blobs.
Alex Deymo14158572015-06-13 03:37:08 -0700229 LOG(INFO) << "Writing final delta file data blobs...";
230 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
231 ScopedFdCloser blobs_fd_closer(&blobs_fd);
232 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
233 for (;;) {
234 vector<char> buf(1024 * 1024);
235 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
236 if (0 == rc) {
237 // EOF
238 break;
239 }
240 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Alex Deymo95699a92016-12-08 19:43:01 -0800241 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700242 }
243
Sen Jiang644f6182015-10-06 16:45:57 -0700244 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700245 if (!private_key_path.empty()) {
246 LOG(INFO) << "Signing the update...";
Sen Jiang9b2f1782019-01-24 14:27:50 -0800247 string signature;
Alex Deymo14158572015-06-13 03:37:08 -0700248 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
249 payload_file,
Sen Jiang9b2f1782019-01-24 14:27:50 -0800250 {private_key_path},
Sen Jiang720df3e2015-10-01 13:10:44 -0700251 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700252 metadata_signature_size,
253 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Sen Jiang9b2f1782019-01-24 14:27:50 -0800254 &signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800255 TEST_AND_RETURN_FALSE_ERRNO(
Sen Jiang9b2f1782019-01-24 14:27:50 -0800256 writer.Write(signature.data(), signature.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700257 }
258
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700259 ReportPayloadUsage(metadata_size);
260 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700261 return true;
262}
263
Amin Hassani232f8f92019-01-14 16:15:31 -0800264bool PayloadFile::ReorderDataBlobs(const string& data_blobs_path,
265 const string& new_data_blobs_path) {
Alex Deymo14158572015-06-13 03:37:08 -0700266 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
267 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
268 ScopedFdCloser in_fd_closer(&in_fd);
269
270 DirectFileWriter writer;
Alex Deymo95699a92016-12-08 19:43:01 -0800271 int rc = writer.Open(
272 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
273 if (rc != 0) {
274 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
275 return false;
276 }
Alex Deymo14158572015-06-13 03:37:08 -0700277 ScopedFileWriterCloser writer_closer(&writer);
278 uint64_t out_file_size = 0;
279
Alex Deymoa4073ef2016-03-22 23:40:53 -0700280 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700281 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700282 if (!aop.op.has_data_offset())
283 continue;
284 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700285 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700286 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
287 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
288
289 // Add the hash of the data blobs for this operation
290 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
291
292 aop.op.set_data_offset(out_file_size);
Alex Deymo95699a92016-12-08 19:43:01 -0800293 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700294 out_file_size += buf.size();
295 }
296 }
297 return true;
298}
299
Alex Deymoa12ee112015-08-12 22:19:32 -0700300bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700301 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700302 brillo::Blob hash;
303 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700304 op->set_data_sha256_hash(hash.data(), hash.size());
305 return true;
306}
307
308void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600309 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700310 off_t total_size = 0;
Sen Jiangf23bf922018-11-01 18:26:07 -0700311 int total_op = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700312
Sen Jiangb9ef4912015-09-21 15:06:13 -0700313 for (const auto& part : part_vec_) {
Sen Jianga91195d2018-10-25 15:15:38 -0700314 string part_prefix = "<" + part.name + ">:";
Sen Jiangb9ef4912015-09-21 15:06:13 -0700315 for (const AnnotatedOperation& aop : part.aops) {
Sen Jianga91195d2018-10-25 15:15:38 -0700316 DeltaObject delta(
317 part_prefix + aop.name, aop.op.type(), aop.op.data_length());
Simon Glass3d32f852017-06-28 16:38:11 -0600318 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700319 total_size += aop.op.data_length();
320 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700321 total_op += part.aops.size();
Alex Deymo14158572015-06-13 03:37:08 -0700322 }
323
Simon Glass3d32f852017-06-28 16:38:11 -0600324 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700325 total_size += metadata_size;
326
Sen Jianga91195d2018-10-25 15:15:38 -0700327 constexpr char kFormatString[] = "%6.2f%% %10jd %-13s %s %d\n";
Simon Glass3d32f852017-06-28 16:38:11 -0600328 for (const auto& object_count : object_counts) {
329 const DeltaObject& object = object_count.first;
Sen Jianga91195d2018-10-25 15:15:38 -0700330 // Use printf() instead of LOG(INFO) because timestamp makes it difficult to
331 // compare two reports.
Sen Jiangcebb6882019-02-26 16:23:28 +0800332 printf(kFormatString,
333 object.size * 100.0 / total_size,
334 object.size,
335 (object.type >= 0
336 ? InstallOperationTypeName(
337 static_cast<InstallOperation::Type>(object.type))
338 : "-"),
339 object.name.c_str(),
340 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700341 }
Sen Jiangf23bf922018-11-01 18:26:07 -0700342 printf(kFormatString, 100.0, total_size, "", "<total>", total_op);
343 fflush(stdout);
Alex Deymo14158572015-06-13 03:37:08 -0700344}
345
Alex Deymo14158572015-06-13 03:37:08 -0700346} // namespace chromeos_update_engine