1 //
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 //
16
17 #include "update_engine/payload_generator/payload_file.h"
18
19 #include <endian.h>
20
21 #include <algorithm>
22 #include <map>
23
24 #include <base/strings/stringprintf.h>
25
26 #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"
30 #include "update_engine/payload_generator/annotated_operation.h"
31 #include "update_engine/payload_generator/delta_diff_utils.h"
32 #include "update_engine/payload_generator/payload_signer.h"
33
34 using std::string;
35 using std::vector;
36
37 namespace chromeos_update_engine {
38
39 namespace {
40
41 struct DeltaObject {
DeltaObjectchromeos_update_engine::__anonb09f82530111::DeltaObject42 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) {}
operator <chromeos_update_engine::__anonb09f82530111::DeltaObject46 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.
WriteUint64AsBigEndian(FileWriter * writer,const uint64_t value)56 bool 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
Init(const PayloadGenerationConfig & config)64 bool PayloadFile::Init(const PayloadGenerationConfig& config) {
65 TEST_AND_RETURN_FALSE(config.version.Validate());
66 major_version_ = config.version.major;
67 manifest_.set_minor_version(config.version.minor);
68
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);
76 manifest_.set_max_timestamp(config.max_timestamp);
77 return true;
78 }
79
AddPartition(const PartitionConfig & old_conf,const PartitionConfig & new_conf,const vector<AnnotatedOperation> & aops)80 bool PayloadFile::AddPartition(const PartitionConfig& old_conf,
81 const PartitionConfig& new_conf,
82 const vector<AnnotatedOperation>& aops) {
83 // Check partitions order for Chrome OS
84 if (major_version_ == kChromeOSMajorPayloadVersion) {
85 const vector<const char*> part_order = { kLegacyPartitionNameRoot,
86 kLegacyPartitionNameKernel };
87 TEST_AND_RETURN_FALSE(part_vec_.size() < part_order.size());
88 TEST_AND_RETURN_FALSE(new_conf.name == part_order[part_vec_.size()]);
89 }
90 Partition part;
91 part.name = new_conf.name;
92 part.aops = aops;
93 part.postinstall = new_conf.postinstall;
94 // Initialize the PartitionInfo objects if present.
95 if (!old_conf.path.empty())
96 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(old_conf,
97 &part.old_info));
98 TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(new_conf,
99 &part.new_info));
100 part_vec_.push_back(std::move(part));
101 return true;
102 }
103
WritePayload(const string & payload_file,const string & data_blobs_path,const string & private_key_path,uint64_t * metadata_size_out)104 bool PayloadFile::WritePayload(const string& payload_file,
105 const string& data_blobs_path,
106 const string& private_key_path,
107 uint64_t* metadata_size_out) {
108 // Reorder the data blobs with the manifest_.
109 string ordered_blobs_path;
110 TEST_AND_RETURN_FALSE(utils::MakeTempFile(
111 "CrAU_temp_data.ordered.XXXXXX",
112 &ordered_blobs_path,
113 nullptr));
114 ScopedPathUnlinker ordered_blobs_unlinker(ordered_blobs_path);
115 TEST_AND_RETURN_FALSE(ReorderDataBlobs(data_blobs_path, ordered_blobs_path));
116
117 // Check that install op blobs are in order.
118 uint64_t next_blob_offset = 0;
119 for (const auto& part : part_vec_) {
120 for (const auto& aop : part.aops) {
121 if (!aop.op.has_data_offset())
122 continue;
123 if (aop.op.data_offset() != next_blob_offset) {
124 LOG(FATAL) << "bad blob offset! " << aop.op.data_offset() << " != "
125 << next_blob_offset;
126 }
127 next_blob_offset += aop.op.data_length();
128 }
129 }
130
131 // Copy the operations and partition info from the part_vec_ to the manifest.
132 manifest_.clear_install_operations();
133 manifest_.clear_kernel_install_operations();
134 manifest_.clear_partitions();
135 for (const auto& part : part_vec_) {
136 if (major_version_ == kBrilloMajorPayloadVersion) {
137 PartitionUpdate* partition = manifest_.add_partitions();
138 partition->set_partition_name(part.name);
139 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 for (const AnnotatedOperation& aop : part.aops) {
148 *partition->add_operations() = aop.op;
149 }
150 if (part.old_info.has_size() || part.old_info.has_hash())
151 *(partition->mutable_old_partition_info()) = part.old_info;
152 if (part.new_info.has_size() || part.new_info.has_hash())
153 *(partition->mutable_new_partition_info()) = part.new_info;
154 } else {
155 // major_version_ == kChromeOSMajorPayloadVersion
156 if (part.name == kLegacyPartitionNameKernel) {
157 for (const AnnotatedOperation& aop : part.aops)
158 *manifest_.add_kernel_install_operations() = aop.op;
159 if (part.old_info.has_size() || part.old_info.has_hash())
160 *manifest_.mutable_old_kernel_info() = part.old_info;
161 if (part.new_info.has_size() || part.new_info.has_hash())
162 *manifest_.mutable_new_kernel_info() = part.new_info;
163 } else {
164 for (const AnnotatedOperation& aop : part.aops)
165 *manifest_.add_install_operations() = aop.op;
166 if (part.old_info.has_size() || part.old_info.has_hash())
167 *manifest_.mutable_old_rootfs_info() = part.old_info;
168 if (part.new_info.has_size() || part.new_info.has_hash())
169 *manifest_.mutable_new_rootfs_info() = part.new_info;
170 }
171 }
172 }
173
174 // Signatures appear at the end of the blobs. Note the offset in the
175 // manifest_.
176 uint64_t signature_blob_length = 0;
177 if (!private_key_path.empty()) {
178 TEST_AND_RETURN_FALSE(
179 PayloadSigner::SignatureBlobLength(vector<string>(1, private_key_path),
180 &signature_blob_length));
181 PayloadSigner::AddSignatureToManifest(
182 next_blob_offset, signature_blob_length,
183 major_version_ == kChromeOSMajorPayloadVersion, &manifest_);
184 }
185
186 // Serialize protobuf
187 string serialized_manifest;
188 TEST_AND_RETURN_FALSE(manifest_.AppendToString(&serialized_manifest));
189
190 uint64_t metadata_size =
191 sizeof(kDeltaMagic) + 2 * sizeof(uint64_t) + serialized_manifest.size();
192
193 LOG(INFO) << "Writing final delta file header...";
194 DirectFileWriter writer;
195 TEST_AND_RETURN_FALSE_ERRNO(writer.Open(payload_file.c_str(),
196 O_WRONLY | O_CREAT | O_TRUNC,
197 0644) == 0);
198 ScopedFileWriterCloser writer_closer(&writer);
199
200 // Write header
201 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(kDeltaMagic, sizeof(kDeltaMagic)));
202
203 // Write major version number
204 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer, major_version_));
205
206 // Write protobuf length
207 TEST_AND_RETURN_FALSE(WriteUint64AsBigEndian(&writer,
208 serialized_manifest.size()));
209
210 // Write metadata signature size.
211 uint32_t metadata_signature_size = 0;
212 if (major_version_ == kBrilloMajorPayloadVersion) {
213 // Metadata signature has the same size as payload signature, because they
214 // are both the same kind of signature for the same kind of hash.
215 uint32_t metadata_signature_size = htobe32(signature_blob_length);
216 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(&metadata_signature_size,
217 sizeof(metadata_signature_size)));
218 metadata_size += sizeof(metadata_signature_size);
219 // Set correct size instead of big endian size.
220 metadata_signature_size = signature_blob_length;
221 }
222
223 // Write protobuf
224 LOG(INFO) << "Writing final delta file protobuf... "
225 << serialized_manifest.size();
226 TEST_AND_RETURN_FALSE_ERRNO(
227 writer.Write(serialized_manifest.data(), serialized_manifest.size()));
228
229 // Write metadata signature blob.
230 if (major_version_ == kBrilloMajorPayloadVersion &&
231 !private_key_path.empty()) {
232 brillo::Blob metadata_hash, metadata_signature;
233 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(payload_file,
234 metadata_size,
235 &metadata_hash));
236 TEST_AND_RETURN_FALSE(
237 PayloadSigner::SignHashWithKeys(metadata_hash,
238 vector<string>(1, private_key_path),
239 &metadata_signature));
240 TEST_AND_RETURN_FALSE_ERRNO(
241 writer.Write(metadata_signature.data(), metadata_signature.size()));
242 }
243
244 // Append the data blobs
245 LOG(INFO) << "Writing final delta file data blobs...";
246 int blobs_fd = open(ordered_blobs_path.c_str(), O_RDONLY, 0);
247 ScopedFdCloser blobs_fd_closer(&blobs_fd);
248 TEST_AND_RETURN_FALSE(blobs_fd >= 0);
249 for (;;) {
250 vector<char> buf(1024 * 1024);
251 ssize_t rc = read(blobs_fd, buf.data(), buf.size());
252 if (0 == rc) {
253 // EOF
254 break;
255 }
256 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
257 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), rc));
258 }
259
260 // Write payload signature blob.
261 if (!private_key_path.empty()) {
262 LOG(INFO) << "Signing the update...";
263 brillo::Blob signature_blob;
264 TEST_AND_RETURN_FALSE(PayloadSigner::SignPayload(
265 payload_file,
266 vector<string>(1, private_key_path),
267 metadata_size,
268 metadata_signature_size,
269 metadata_size + metadata_signature_size + manifest_.signatures_offset(),
270 &signature_blob));
271 TEST_AND_RETURN_FALSE_ERRNO(
272 writer.Write(signature_blob.data(), signature_blob.size()));
273 }
274
275 ReportPayloadUsage(metadata_size);
276 *metadata_size_out = metadata_size;
277 return true;
278 }
279
ReorderDataBlobs(const string & data_blobs_path,const string & new_data_blobs_path)280 bool PayloadFile::ReorderDataBlobs(
281 const string& data_blobs_path,
282 const string& new_data_blobs_path) {
283 int in_fd = open(data_blobs_path.c_str(), O_RDONLY, 0);
284 TEST_AND_RETURN_FALSE_ERRNO(in_fd >= 0);
285 ScopedFdCloser in_fd_closer(&in_fd);
286
287 DirectFileWriter writer;
288 int rc = writer.Open(
289 new_data_blobs_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
290 if (rc != 0) {
291 PLOG(ERROR) << "Error creating " << new_data_blobs_path;
292 return false;
293 }
294 ScopedFileWriterCloser writer_closer(&writer);
295 uint64_t out_file_size = 0;
296
297 for (auto& part : part_vec_) {
298 for (AnnotatedOperation& aop : part.aops) {
299 if (!aop.op.has_data_offset())
300 continue;
301 CHECK(aop.op.has_data_length());
302 brillo::Blob buf(aop.op.data_length());
303 ssize_t rc = pread(in_fd, buf.data(), buf.size(), aop.op.data_offset());
304 TEST_AND_RETURN_FALSE(rc == static_cast<ssize_t>(buf.size()));
305
306 // Add the hash of the data blobs for this operation
307 TEST_AND_RETURN_FALSE(AddOperationHash(&aop.op, buf));
308
309 aop.op.set_data_offset(out_file_size);
310 TEST_AND_RETURN_FALSE_ERRNO(writer.Write(buf.data(), buf.size()));
311 out_file_size += buf.size();
312 }
313 }
314 return true;
315 }
316
AddOperationHash(InstallOperation * op,const brillo::Blob & buf)317 bool PayloadFile::AddOperationHash(InstallOperation* op,
318 const brillo::Blob& buf) {
319 brillo::Blob hash;
320 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(buf, &hash));
321 op->set_data_sha256_hash(hash.data(), hash.size());
322 return true;
323 }
324
ReportPayloadUsage(uint64_t metadata_size) const325 void PayloadFile::ReportPayloadUsage(uint64_t metadata_size) const {
326 std::map<DeltaObject, int> object_counts;
327 off_t total_size = 0;
328
329 for (const auto& part : part_vec_) {
330 for (const AnnotatedOperation& aop : part.aops) {
331 DeltaObject delta(aop.name, aop.op.type(), aop.op.data_length());
332 object_counts[delta]++;
333 total_size += aop.op.data_length();
334 }
335 }
336
337 object_counts[DeltaObject("<manifest-metadata>", -1, metadata_size)] = 1;
338 total_size += metadata_size;
339
340 static const char kFormatString[] = "%6.2f%% %10jd %-13s %s %d";
341 for (const auto& object_count : object_counts) {
342 const DeltaObject& object = object_count.first;
343 LOG(INFO) << base::StringPrintf(
344 kFormatString,
345 object.size * 100.0 / total_size,
346 static_cast<intmax_t>(object.size),
347 (object.type >= 0 ? InstallOperationTypeName(
348 static_cast<InstallOperation_Type>(object.type))
349 : "-"),
350 object.name.c_str(),
351 object_count.second);
352 }
353 LOG(INFO) << base::StringPrintf(kFormatString,
354 100.0,
355 static_cast<intmax_t>(total_size),
356 "",
357 "<total>",
358 1);
359 }
360
361 } // namespace chromeos_update_engine
362