blob: ae4896a4fb008395065a60422e5d763b13ea5de5 [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>
22
Sen Jiang46e9b172015-08-31 14:11:01 -070023#include "update_engine/delta_performer.h"
Alex Deymo14158572015-06-13 03:37:08 -070024#include "update_engine/file_writer.h"
25#include "update_engine/omaha_hash_calculator.h"
26#include "update_engine/payload_constants.h"
27#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo14158572015-06-13 03:37:08 -070028#include "update_engine/payload_generator/delta_diff_utils.h"
29#include "update_engine/payload_generator/payload_signer.h"
30
31using std::string;
32using std::vector;
33
34namespace chromeos_update_engine {
35
36namespace {
37
Alex Deymo14158572015-06-13 03:37:08 -070038struct DeltaObject {
39 DeltaObject(const string& in_name, const int in_type, const off_t in_size)
40 : name(in_name),
41 type(in_type),
42 size(in_size) {}
43 bool operator <(const DeltaObject& object) const {
44 return (size != object.size) ? (size < object.size) : (name < object.name);
45 }
46 string name;
47 int type;
48 off_t size;
49};
50
51// Writes the uint64_t passed in in host-endian to the file as big-endian.
52// Returns true on success.
53bool WriteUint64AsBigEndian(FileWriter* writer, const uint64_t value) {
54 uint64_t value_be = htobe64(value);
55 TEST_AND_RETURN_FALSE(writer->Write(&value_be, sizeof(value_be)));
56 return true;
57}
58
59} // namespace
60
Alex Deymo14158572015-06-13 03:37:08 -070061bool PayloadFile::Init(const PayloadGenerationConfig& config) {
Sen Jiang46e9b172015-08-31 14:11:01 -070062 major_version_ = config.major_version;
63 TEST_AND_RETURN_FALSE(major_version_ == kChromeOSMajorPayloadVersion ||
64 major_version_ == kBrilloMajorPayloadVersion);
Alex Deymo14158572015-06-13 03:37:08 -070065 manifest_.set_minor_version(config.minor_version);
66
67 if (!config.source.ImageInfoIsEmpty())
68 *(manifest_.mutable_old_image_info()) = config.source.image_info;
69
70 if (!config.target.ImageInfoIsEmpty())
71 *(manifest_.mutable_new_image_info()) = config.target.image_info;
72
73 manifest_.set_block_size(config.block_size);
Alex Deymo14158572015-06-13 03:37:08 -070074 return true;
75}
76
Sen Jiangb9ef4912015-09-21 15:06:13 -070077bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
78 const PartitionConfig& new_conf,
79 const vector<AnnotatedOperation>& aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -070080 // Check partitions order for Chrome OS
81 if (major_version_ == kChromeOSMajorPayloadVersion) {
82 const vector<const char*> part_order = { kLegacyPartitionNameRoot,
83 kLegacyPartitionNameKernel };
Sen Jiangb9ef4912015-09-21 15:06:13 -070084 TEST_AND_RETURN_FALSE(part_vec_.size() < part_order.size());
85 TEST_AND_RETURN_FALSE(new_conf.name == part_order[part_vec_.size()]);
Sen Jiang70a6ab02015-08-28 13:23:27 -070086 }
Sen Jiangb9ef4912015-09-21 15:06:13 -070087 Partition part;
88 part.name = new_conf.name;
89 part.aops = aops;
90 // Initialize the PartitionInfo objects if present.
91 if (!old_conf.path.empty())
92 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(old_conf,
93 &part.old_info));
94 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(new_conf,
95 &part.new_info));
96 part_vec_.push_back(std::move(part));
Sen Jiang70a6ab02015-08-28 13:23:27 -070097 return true;
Alex Deymo14158572015-06-13 03:37:08 -070098}
99
100bool PayloadFile::WritePayload(const string& payload_file,
101 const string& data_blobs_path,
102 const string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700103 uint64_t* metadata_size_out) {
Alex Deymo14158572015-06-13 03:37:08 -0700104 // Reorder the data blobs with the manifest_.
105 string ordered_blobs_path;
106 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
107 "CrAU_temp_data.ordered.XXXXXX",
108 &ordered_blobs_path,
109 nullptr));
110 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) {
120 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_install_operations();
129 manifest_.clear_kernel_install_operations();
130 manifest_.clear_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700131 for (const auto& part : part_vec_) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700132 if (major_version_ == kBrilloMajorPayloadVersion) {
133 PartitionUpdate* partition = manifest_.add_partitions();
Sen Jiangb9ef4912015-09-21 15:06:13 -0700134 partition->set_partition_name(part.name);
135 for (const AnnotatedOperation& aop : part.aops) {
Sen Jiang70a6ab02015-08-28 13:23:27 -0700136 *partition->add_operations() = aop.op;
137 }
Sen Jiangb9ef4912015-09-21 15:06:13 -0700138 if (part.old_info.has_size() || part.old_info.has_hash())
139 *(partition->mutable_old_partition_info()) = part.old_info;
140 if (part.new_info.has_size() || part.new_info.has_hash())
141 *(partition->mutable_new_partition_info()) = part.new_info;
Sen Jiang70a6ab02015-08-28 13:23:27 -0700142 } else {
Sen Jiangb9ef4912015-09-21 15:06:13 -0700143 // major_version_ == kChromeOSMajorPayloadVersion
144 if (part.name == kLegacyPartitionNameKernel) {
145 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700146 *manifest_.add_kernel_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700147 if (part.old_info.has_size() || part.old_info.has_hash())
148 *manifest_.mutable_old_kernel_info() = part.old_info;
149 if (part.new_info.has_size() || part.new_info.has_hash())
150 *manifest_.mutable_new_kernel_info() = part.new_info;
151 } else {
152 for (const AnnotatedOperation& aop : part.aops)
Sen Jiang70a6ab02015-08-28 13:23:27 -0700153 *manifest_.add_install_operations() = aop.op;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700154 if (part.old_info.has_size() || part.old_info.has_hash())
155 *manifest_.mutable_old_rootfs_info() = part.old_info;
156 if (part.new_info.has_size() || part.new_info.has_hash())
157 *manifest_.mutable_new_rootfs_info() = part.new_info;
Alex Deymo14158572015-06-13 03:37:08 -0700158 }
159 }
160 }
161
162 // Signatures appear at the end of the blobs. Note the offset in the
163 // manifest_.
Sen Jiang644f6182015-10-06 16:45:57 -0700164 uint64_t signature_blob_length = 0;
Alex Deymo14158572015-06-13 03:37:08 -0700165 if (!private_key_path.empty()) {
Alex Deymo14158572015-06-13 03:37:08 -0700166 TEST_AND_RETURN_FALSE(
167 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
168 &signature_blob_length));
Sen Jiang3e728fe2015-11-05 11:37:23 -0800169 PayloadSigner::AddSignatureToManifest(
170 next_blob_offset, signature_blob_length,
171 major_version_ == kChromeOSMajorPayloadVersion, &manifest_);
Alex Deymo14158572015-06-13 03:37:08 -0700172 }
173
Sen Jiang70a6ab02015-08-28 13:23:27 -0700174 // Serialize protobuf
Alex Deymo14158572015-06-13 03:37:08 -0700175 string serialized_manifest;
176 TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest));
177
Sen Jiangaef1c6f2015-10-07 10:05:32 -0700178 uint64_t metadata_size =
179 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
180
Alex Deymo14158572015-06-13 03:37:08 -0700181 LOG(INFO) << "Writing final delta file header...";
182 DirectFileWriter writer;
183 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
184 O_WRONLY | O_CREAT | O_TRUNC,
185 0644) == 0);
186 ScopedFileWriterCloser writer_closer(&writer);
187
188 // Write header
Sen Jiangb8060e42015-09-24 17:30:50 -0700189 TEST_AND_RETURN_FALSE(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
Alex Deymo14158572015-06-13 03:37:08 -0700190
191 // Write major version number
Sen Jiang46e9b172015-08-31 14:11:01 -0700192 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
Alex Deymo14158572015-06-13 03:37:08 -0700193
194 // Write protobuf length
195 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
196 serialized_manifest.size()));
197
Sen Jiang644f6182015-10-06 16:45:57 -0700198 // Write metadata signature size.
199 uint32_t metadata_signature_size = 0;
Sen Jiangf4bb3e62015-09-29 11:12:09 -0700200 if (major_version_ == kBrilloMajorPayloadVersion) {
Sen Jiang644f6182015-10-06 16:45:57 -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(writer.Write(&metadata_signature_size,
205 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 }
210
Alex Deymo14158572015-06-13 03:37:08 -0700211 // Write protobuf
212 LOG(INFO) << "Writing final delta file protobuf... "
213 << serialized_manifest.size();
214 TEST_AND_RETURN_FALSE(writer.Write(serialized_manifest.data(),
215 serialized_manifest.size()));
216
Sen Jiang644f6182015-10-06 16:45:57 -0700217 // Write metadata signature blob.
218 if (major_version_ == kBrilloMajorPayloadVersion &&
219 !private_key_path.empty()) {
220 brillo::Blob metadata_hash, metadata_signature;
221 TEST_AND_RETURN_FALSE(OmahaHashCalculator::RawHashOfFile(payload_file,
222 metadata_size,
223 &metadata_hash));
224 TEST_AND_RETURN_FALSE(
225 PayloadSigner::SignHashWithKeys(metadata_hash,
226 vector<string>(1, private_key_path),
227 &metadata_signature));
228 TEST_AND_RETURN_FALSE(writer.Write(metadata_signature.data(),
229 metadata_signature.size()));
230 }
231
Alex Deymo14158572015-06-13 03:37:08 -0700232 // Append the data blobs
233 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);
245 TEST_AND_RETURN_FALSE(writer.Write(buf.data(), rc));
246 }
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...";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700251 brillo::Blob signature_blob;
Alex Deymo14158572015-06-13 03:37:08 -0700252 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
253 payload_file,
254 vector<string>(1, 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(),
Alex Deymo14158572015-06-13 03:37:08 -0700258 &signature_blob));
259 TEST_AND_RETURN_FALSE(writer.Write(signature_blob.data(),
260 signature_blob.size()));
261 }
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
268bool PayloadFile::ReorderDataBlobs(
269 const string& data_blobs_path,
270 const string& new_data_blobs_path) {
271 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;
276 TEST_AND_RETURN_FALSE(
277 writer.Open(new_data_blobs_path.c_str(),
278 O_WRONLY | O_TRUNC | O_CREAT,
279 0644) == 0);
280 ScopedFileWriterCloser writer_closer(&writer);
281 uint64_t out_file_size = 0;
282
Sen Jiangb9ef4912015-09-21 15:06:13 -0700283 for (auto& part: part_vec_) {
284 for (AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700285 if (!aop.op.has_data_offset())
286 continue;
287 CHECK(aop.op.has_data_length());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700288 brillo::Blob buf(aop.op.data_length());
Alex Deymo14158572015-06-13 03:37:08 -0700289 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
290 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
291
292 // Add the hash of the data blobs for this operation
293 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
294
295 aop.op.set_data_offset(out_file_size);
296 TEST_AND_RETURN_FALSE(writer.Write(buf.data(), buf.size()));
297 out_file_size += buf.size();
298 }
299 }
300 return true;
301}
302
Alex Deymoa12ee112015-08-12 22:19:32 -0700303bool PayloadFile::AddOperationHash(InstallOperation* op,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700304 const brillo::Blob& buf) {
Alex Deymo14158572015-06-13 03:37:08 -0700305 OmahaHashCalculator hasher;
306 TEST_AND_RETURN_FALSE(hasher.Update(buf.data(), buf.size()));
307 TEST_AND_RETURN_FALSE(hasher.Finalize());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700308 const brillo::Blob& hash = hasher.raw_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 {
314 vector<DeltaObject> objects;
315 off_t total_size = 0;
316
Sen Jiangb9ef4912015-09-21 15:06:13 -0700317 for (const auto& part : part_vec_) {
318 for (const AnnotatedOperation& aop : part.aops) {
Alex Deymo14158572015-06-13 03:37:08 -0700319 objects.push_back(DeltaObject(aop.name,
320 aop.op.type(),
321 aop.op.data_length()));
322 total_size += aop.op.data_length();
323 }
324 }
325
326 objects.push_back(DeltaObject("<manifest-metadata>",
327 -1,
328 metadata_size));
329 total_size += metadata_size;
330
331 std::sort(objects.begin(), objects.end());
332
333 static const char kFormatString[] = "%6.2f%% %10jd %-10s %s\n";
334 for (const DeltaObject& object : objects) {
Alex Deymo8241ffb2015-10-15 10:36:44 -0700335 fprintf(
336 stderr, kFormatString,
337 object.size * 100.0 / total_size,
338 static_cast<intmax_t>(object.size),
339 (object.type >= 0 ? InstallOperationTypeName(
340 static_cast<InstallOperation_Type>(object.type))
341 : "-"),
342 object.name.c_str());
Alex Deymo14158572015-06-13 03:37:08 -0700343 }
344 fprintf(stderr, kFormatString,
345 100.0, static_cast<intmax_t>(total_size), "", "<total>");
346}
347
Alex Deymo14158572015-06-13 03:37:08 -0700348} // namespace chromeos_update_engine