blob: a9e32a3a5e14814c1f2e5d1e2b95947313b9146c [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);
Alex Deymo14158572015-06-13 03:37:08 -070076 return true;
77}
78
Sen Jiangb9ef4912015-09-21 15:06:13 -070079bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
80 const PartitionConfig& new_conf,
81 const vector<AnnotatedOperation>& aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -070082 // Check partitions order for Chrome OS
83 if (major_version_ == kChromeOSMajorPayloadVersion) {
Tudor Brindusdda79e22018-06-28 18:03:21 -070084 const vector<const char*> part_order = { kPartitionNameRoot,
85 kPartitionNameKernel };
Sen Jiangb9ef4912015-09-21 15:06:13 -070086 TEST_AND_RETURN_FALSE(part_vec_.size() < part_order.size());
87 TEST_AND_RETURN_FALSE(new_conf.name == part_order[part_vec_.size()]);
Sen Jiang70a6ab02015-08-28 13:23:27 -070088 }
Sen Jiangb9ef4912015-09-21 15:06:13 -070089 Partition part;
90 part.name = new_conf.name;
91 part.aops = aops;
Sen Jiang05feee02015-11-11 15:59:49 -080092 part.postinstall = new_conf.postinstall;
Sen Jiangb9ef4912015-09-21 15:06:13 -070093 // Initialize the PartitionInfo objects if present.
94 if (!old_conf.path.empty())
95 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(old_conf,
96 &part.old_info));
97 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(new_conf,
98 &part.new_info));
99 part_vec_.push_back(std::move(part));
Sen Jiang70a6ab02015-08-28 13:23:27 -0700100 return true;
Alex Deymo14158572015-06-13 03:37:08 -0700101}
102
103bool PayloadFile::WritePayload(const string& payload_file,
104 const string& data_blobs_path,
105 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700106 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700107 // Reorder the data blobs with the manifest_.
108 string ordered_blobs_path;
109 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
110 "CrAU_temp_data.ordered.XXXXXX",
111 &ordered_blobs_path,
112 nullptr));
113 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
114 TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path));
115
Sen Jiang70a6ab02015-08-28 13:23:27 -0700116 // Check that install op blobs are in order.
117 uint64_t next_blob_offset = 0;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700118 for (const auto& part : part_vec_) {
119 for (const auto& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700120 if (!aop.op.has_data_offset())
121 continue;
122 if (aop.op.data_offset() != next_blob_offset) {
123 LOG(FATAL) << "bad blob offset! " << aop.op.data_offset() << " != "
124 << next_blob_offset;
Alex Deymo14158572015-06-13 03:37:08 -0700125 }
Sen Jiang70a6ab02015-08-28 13:23:27 -0700126 next_blob_offset += aop.op.data_length();
Alex Deymo14158572015-06-13 03:37:08 -0700127 }
128 }
129
Sen Jiangb9ef4912015-09-21 15:06:13 -0700130 // Copy the operations and partition info from the part_vec_ to the manifest.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700131 manifest_.clear_install_operations();
132 manifest_.clear_kernel_install_operations();
133 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700134 for (const auto& part : part_vec_) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700135 if (major_version_ == kBrilloMajorPayloadVersion) {
136 PartitionUpdate* partition = manifest_.add_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700137 partition->set_partition_name(part.name);
Sen Jiang05feee02015-11-11 15:59:49 -0800138 if (part.postinstall.run) {
139 partition->set_run_postinstall(true);
140 if (!part.postinstall.path.empty())
141 partition->set_postinstall_path(part.postinstall.path);
142 if (!part.postinstall.filesystem_type.empty())
143 partition->set_filesystem_type(part.postinstall.filesystem_type);
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700144 partition->set_postinstall_optional(part.postinstall.optional);
Sen Jiang05feee02015-11-11 15:59:49 -0800145 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700146 for (const AnnotatedOperation& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700147 *partition->add_operations() = aop.op;
148 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700149 if (part.old_info.has_size() || part.old_info.has_hash())
150 *(partition->mutable_old_partition_info()) = part.old_info;
151 if (part.new_info.has_size() || part.new_info.has_hash())
152 *(partition->mutable_new_partition_info()) = part.new_info;
Sen Jiang70a6ab02015-08-28 13:23:27 -0700153 } else {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700154 // major_version_ == kChromeOSMajorPayloadVersion
Tudor Brindusdda79e22018-06-28 18:03:21 -0700155 if (part.name == kPartitionNameKernel) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700156 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700157 *manifest_.add_kernel_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700158 if (part.old_info.has_size() || part.old_info.has_hash())
159 *manifest_.mutable_old_kernel_info() = part.old_info;
160 if (part.new_info.has_size() || part.new_info.has_hash())
161 *manifest_.mutable_new_kernel_info() = part.new_info;
162 } else {
163 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700164 *manifest_.add_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700165 if (part.old_info.has_size() || part.old_info.has_hash())
166 *manifest_.mutable_old_rootfs_info() = part.old_info;
167 if (part.new_info.has_size() || part.new_info.has_hash())
168 *manifest_.mutable_new_rootfs_info() = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700169 }
170 }
171 }
172
173 // Signatures appear at the end of the blobs. Note the offset in the
174 // manifest_.
Sen Jiang644f6182015-10-06 16:45:57 -0700175 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700176 if (!private_key_path.empty()) {
Alex Deymo14158572015-06-13 03:37:08 -0700177 TEST_AND_RETURN_FALSE(
178 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
179 &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800180 PayloadSigner::AddSignatureToManifest(
181 next_blob_offset, signature_blob_length,
182 major_version_ == kChromeOSMajorPayloadVersion, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700183 }
184
Sen Jiang70a6ab02015-08-28 13:23:27 -0700185 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700186 string serialized_manifest;
187 TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest));
188
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700189 uint64_t metadata_size =
190 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
191
Alex Deymo14158572015-06-13 03:37:08 -0700192 LOG(INFO) << "Writing final delta file header...";
193 DirectFileWriter writer;
194 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
195 O_WRONLY | O_CREAT | O_TRUNC,
196 0644) == 0);
197 ScopedFileWriterCloser writer_closer(&writer);
198
199 // Write header
Alex Deymo95699a92016-12-08 19:43:01 -0800200 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700201
202 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700203 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700204
205 // Write protobuf length
206 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
207 serialized_manifest.size()));
208
Sen Jiang644f6182015-10-06 16:45:57 -0700209 // Write metadata signature size.
210 uint32_t metadata_signature_size = 0;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700211 if (major_version_ == kBrilloMajorPayloadVersion) {
Sen Jiang644f6182015-10-06 16:45:57 -0700212 // Metadata signature has the same size as payload signature, because they
213 // are both the same kind of signature for the same kind of hash.
214 uint32_t metadata_signature_size = htobe32(signature_blob_length);
Alex Deymo95699a92016-12-08 19:43:01 -0800215 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(&metadata_signature_size,
216 sizeof(metadata_signature_size)));
Sen Jiang644f6182015-10-06 16:45:57 -0700217 metadata_size += sizeof(metadata_signature_size);
218 // Set correct size instead of big endian size.
219 metadata_signature_size = signature_blob_length;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700220 }
221
Alex Deymo14158572015-06-13 03:37:08 -0700222 // Write protobuf
223 LOG(INFO) << "Writing final delta file protobuf... "
224 << serialized_manifest.size();
Alex Deymo95699a92016-12-08 19:43:01 -0800225 TEST_AND_RETURN_FALSE_ERRNO(
226 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700227
Sen Jiang644f6182015-10-06 16:45:57 -0700228 // Write metadata signature blob.
229 if (major_version_ == kBrilloMajorPayloadVersion &&
230 !private_key_path.empty()) {
231 brillo::Blob metadata_hash, metadata_signature;
Alex Deymo39910dc2015-11-09 17:04:30 -0800232 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(payload_file,
Sen Jiang644f6182015-10-06 16:45:57 -0700233 metadata_size,
234 &metadata_hash));
235 TEST_AND_RETURN_FALSE(
236 PayloadSigner::SignHashWithKeys(metadata_hash,
237 vector<string>(1, private_key_path),
238 &metadata_signature));
Alex Deymo95699a92016-12-08 19:43:01 -0800239 TEST_AND_RETURN_FALSE_ERRNO(
240 writer.Write(metadata_signature.data(), metadata_signature.size()));
Sen Jiang644f6182015-10-06 16:45:57 -0700241 }
242
Alex Deymo14158572015-06-13 03:37:08 -0700243 // Append the data blobs
244 LOG(INFO) << "Writing final delta file data blobs...";
245 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
246 ScopedFdCloser blobs_fd_closer(&blobs_fd);
247 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
248 for (;;) {
249 vector<char> buf(1024 * 1024);
250 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
251 if (0 == rc) {
252 // EOF
253 break;
254 }
255 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
Alex Deymo95699a92016-12-08 19:43:01 -0800256 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
Alex Deymo14158572015-06-13 03:37:08 -0700257 }
258
Sen Jiang644f6182015-10-06 16:45:57 -0700259 // Write payload signature blob.
Alex Deymo14158572015-06-13 03:37:08 -0700260 if (!private_key_path.empty()) {
261 LOG(INFO) << "Signing the update...";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700262 brillo::Blob signature_blob;
Alex Deymo14158572015-06-13 03:37:08 -0700263 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
264 payload_file,
265 vector<string>(1, private_key_path),
Sen Jiang720df3e2015-10-01 13:10:44 -0700266 metadata_size,
Sen Jiang644f6182015-10-06 16:45:57 -0700267 metadata_signature_size,
268 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
Alex Deymo14158572015-06-13 03:37:08 -0700269 &signature_blob));
Alex Deymo95699a92016-12-08 19:43:01 -0800270 TEST_AND_RETURN_FALSE_ERRNO(
271 writer.Write(signature_blob.data(), signature_blob.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700272 }
273
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700274 ReportPayloadUsage(metadata_size);
275 *metadata_size_out = metadata_size;
Alex Deymo14158572015-06-13 03:37:08 -0700276 return true;
277}
278
279bool PayloadFile::ReorderDataBlobs(
280 const string& data_blobs_path,
281 const string& new_data_blobs_path) {
282 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
283 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
284 ScopedFdCloser in_fd_closer(&in_fd);
285
286 DirectFileWriter writer;
Alex Deymo95699a92016-12-08 19:43:01 -0800287 int rc = writer.Open(
288 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
289 if (rc != 0) {
290 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
291 return false;
292 }
Alex Deymo14158572015-06-13 03:37:08 -0700293 ScopedFileWriterCloser writer_closer(&writer);
294 uint64_t out_file_size = 0;
295
Alex Deymoa4073ef2016-03-22 23:40:53 -0700296 for (auto& part : part_vec_) {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700297 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700298 if (!aop.op.has_data_offset())
299 continue;
300 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700301 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700302 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
303 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
304
305 // Add the hash of the data blobs for this operation
306 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
307
308 aop.op.set_data_offset(out_file_size);
Alex Deymo95699a92016-12-08 19:43:01 -0800309 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
Alex Deymo14158572015-06-13 03:37:08 -0700310 out_file_size += buf.size();
311 }
312 }
313 return true;
314}
315
Alex Deymoa12ee112015-08-12 22:19:32 -0700316bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700317 const brillo::Blob& buf) {
Sen Jiang2703ef42017-03-16 13:36:21 -0700318 brillo::Blob hash;
319 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
Alex Deymo14158572015-06-13 03:37:08 -0700320 op->set_data_sha256_hash(hash.data(), hash.size());
321 return true;
322}
323
324void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
Simon Glass3d32f852017-06-28 16:38:11 -0600325 std::map<DeltaObject, int> object_counts;
Alex Deymo14158572015-06-13 03:37:08 -0700326 off_t total_size = 0;
327
Sen Jiangb9ef4912015-09-21 15:06:13 -0700328 for (const auto& part : part_vec_) {
329 for (const AnnotatedOperation& aop : part.aops) {
Simon Glass3d32f852017-06-28 16:38:11 -0600330 DeltaObject delta(aop.name, aop.op.type(), aop.op.data_length());
331 object_counts[delta]++;
Alex Deymo14158572015-06-13 03:37:08 -0700332 total_size += aop.op.data_length();
333 }
334 }
335
Simon Glass3d32f852017-06-28 16:38:11 -0600336 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
Alex Deymo14158572015-06-13 03:37:08 -0700337 total_size += metadata_size;
338
Simon Glass3d32f852017-06-28 16:38:11 -0600339 static const char kFormatString[] = "%6.2f%% %10jd %-13s %s %d";
340 for (const auto& object_count : object_counts) {
341 const DeltaObject& object = object_count.first;
342 LOG(INFO) << base::StringPrintf(
343 kFormatString,
Alex Deymo8241ffb2015-10-15 10:36:44 -0700344 object.size * 100.0 / total_size,
345 static_cast<intmax_t>(object.size),
346 (object.type >= 0 ? InstallOperationTypeName(
347 static_cast<InstallOperation_Type>(object.type))
348 : "-"),
Simon Glass3d32f852017-06-28 16:38:11 -0600349 object.name.c_str(),
350 object_count.second);
Alex Deymo14158572015-06-13 03:37:08 -0700351 }
Simon Glass3d32f852017-06-28 16:38:11 -0600352 LOG(INFO) << base::StringPrintf(kFormatString,
353 100.0,
354 static_cast<intmax_t>(total_size),
355 "",
356 "<total>",
357 1);
Alex Deymo14158572015-06-13 03:37:08 -0700358}
359
Alex Deymo14158572015-06-13 03:37:08 -0700360} // namespace chromeos_update_engine