blob: b5d1cff9c0d2058f0af2b953613dc9a69186625e [file] [log] [blame]
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/delta_performer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -07006
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07007#include <endian.h>
8#include <errno.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -07009
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <algorithm>
11#include <cstring>
12#include <string>
13#include <vector>
14
Darin Petkovd7061ab2010-10-06 14:37:09 -070015#include <base/scoped_ptr.h>
16#include <base/string_util.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017#include <google/protobuf/repeated_field.h>
18
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include "update_engine/bzip_extent_writer.h"
20#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes353777c2010-10-08 10:34:30 -070021#include "update_engine/extent_ranges.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include "update_engine/extent_writer.h"
23#include "update_engine/graph_types.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070024#include "update_engine/payload_signer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070025#include "update_engine/prefs_interface.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070026#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070027#include "update_engine/terminator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070028
29using std::min;
30using std::string;
31using std::vector;
32using google::protobuf::RepeatedPtrField;
33
34namespace chromeos_update_engine {
35
Darin Petkovabc7bc02011-02-23 14:39:43 -080036const char DeltaPerformer::kUpdatePayloadPublicKeyPath[] =
37 "/usr/share/update_engine/update-payload-key.pub.pem";
38
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070039namespace {
40
41const int kDeltaVersionLength = 8;
42const int kDeltaProtobufLengthLength = 8;
Darin Petkov73058b42010-10-06 16:32:19 -070043const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070044const int kMaxResumedUpdateFailures = 10;
Darin Petkov73058b42010-10-06 16:32:19 -070045
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070046// Converts extents to a human-readable string, for use by DumpUpdateProto().
47string ExtentsToString(const RepeatedPtrField<Extent>& extents) {
48 string ret;
49 for (int i = 0; i < extents.size(); i++) {
50 const Extent& extent = extents.Get(i);
51 if (extent.start_block() == kSparseHole) {
52 ret += StringPrintf("{kSparseHole, %" PRIu64 "}, ", extent.num_blocks());
53 } else {
54 ret += StringPrintf("{%" PRIu64 ", %" PRIu64 "}, ",
55 extent.start_block(), extent.num_blocks());
56 }
57 }
58 if (!ret.empty()) {
59 DCHECK_GT(ret.size(), static_cast<size_t>(1));
60 ret.resize(ret.size() - 2);
61 }
62 return ret;
63}
64
65// LOGs a DeltaArchiveManifest object. Useful for debugging.
66void DumpUpdateProto(const DeltaArchiveManifest& manifest) {
67 LOG(INFO) << "Update Proto:";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070068 LOG(INFO) << " block_size: " << manifest.block_size();
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070069 for (int i = 0; i < (manifest.install_operations_size() +
70 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071 const DeltaArchiveManifest_InstallOperation& op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070072 i < manifest.install_operations_size() ?
73 manifest.install_operations(i) :
74 manifest.kernel_install_operations(
75 i - manifest.install_operations_size());
76 if (i == 0)
77 LOG(INFO) << " Rootfs ops:";
78 else if (i == manifest.install_operations_size())
79 LOG(INFO) << " Kernel ops:";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070080 LOG(INFO) << " operation(" << i << ")";
81 LOG(INFO) << " type: "
82 << DeltaArchiveManifest_InstallOperation_Type_Name(op.type());
83 if (op.has_data_offset())
84 LOG(INFO) << " data_offset: " << op.data_offset();
85 if (op.has_data_length())
86 LOG(INFO) << " data_length: " << op.data_length();
87 LOG(INFO) << " src_extents: " << ExtentsToString(op.src_extents());
88 if (op.has_src_length())
89 LOG(INFO) << " src_length: " << op.src_length();
90 LOG(INFO) << " dst_extents: " << ExtentsToString(op.dst_extents());
91 if (op.has_dst_length())
92 LOG(INFO) << " dst_length: " << op.dst_length();
93 }
94}
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070095
96// Opens path for read/write, put the fd into *fd. On success returns true
97// and sets *err to 0. On failure, returns false and sets *err to errno.
98bool OpenFile(const char* path, int* fd, int* err) {
99 if (*fd != -1) {
100 LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")";
101 *err = EINVAL;
102 return false;
103 }
104 *fd = open(path, O_RDWR, 000);
105 if (*fd < 0) {
106 *err = errno;
107 PLOG(ERROR) << "Unable to open file " << path;
108 return false;
109 }
110 *err = 0;
111 return true;
112}
113
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700114} // namespace {}
115
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700116// Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat
117// it safely. Returns false otherwise.
118bool DeltaPerformer::IsIdempotentOperation(
119 const DeltaArchiveManifest_InstallOperation& op) {
120 if (op.src_extents_size() == 0) {
121 return true;
122 }
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700123 // When in doubt, it's safe to declare an op non-idempotent. Note that we
124 // could detect other types of idempotent operations here such as a MOVE that
125 // moves blocks onto themselves. However, we rely on the server to not send
126 // such operations at all.
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700127 ExtentRanges src_ranges;
128 src_ranges.AddRepeatedExtents(op.src_extents());
129 const uint64_t block_count = src_ranges.blocks();
130 src_ranges.SubtractRepeatedExtents(op.dst_extents());
131 return block_count == src_ranges.blocks();
132}
133
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700134int DeltaPerformer::Open(const char* path, int flags, mode_t mode) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700135 int err;
136 if (OpenFile(path, &fd_, &err))
137 path_ = path;
138 return -err;
139}
140
141bool DeltaPerformer::OpenKernel(const char* kernel_path) {
142 int err;
143 bool success = OpenFile(kernel_path, &kernel_fd_, &err);
144 if (success)
145 kernel_path_ = kernel_path;
146 return success;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700147}
148
149int DeltaPerformer::Close() {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700150 int err = 0;
151 if (close(kernel_fd_) == -1) {
152 err = errno;
153 PLOG(ERROR) << "Unable to close kernel fd:";
154 }
155 if (close(fd_) == -1) {
156 err = errno;
157 PLOG(ERROR) << "Unable to close rootfs fd:";
158 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700159 LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800160 fd_ = -2; // Set to invalid so that calls to Open() will fail.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700161 path_ = "";
Darin Petkov934bb412010-11-18 11:21:35 -0800162 if (!buffer_.empty()) {
163 LOG(ERROR) << "Called Close() while buffer not empty!";
164 if (err >= 0) {
165 err = 1;
166 }
167 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700168 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700169}
170
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700171namespace {
172
173void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
174 string sha256;
175 if (OmahaHashCalculator::Base64Encode(info.hash().data(),
176 info.hash().size(),
177 &sha256)) {
Darin Petkov3aefa862010-12-07 14:45:00 -0800178 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
179 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700180 } else {
181 LOG(ERROR) << "Base64Encode failed for tag: " << tag;
182 }
183}
184
185void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
186 if (manifest.has_old_kernel_info())
187 LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info");
188 if (manifest.has_old_rootfs_info())
189 LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info");
190 if (manifest.has_new_kernel_info())
191 LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info");
192 if (manifest.has_new_rootfs_info())
193 LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info");
194}
195
196} // namespace {}
197
Darin Petkov9574f7e2011-01-13 10:48:12 -0800198DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
199 const std::vector<char>& payload,
200 DeltaArchiveManifest* manifest,
201 uint64_t* metadata_size) {
202 if (payload.size() < strlen(kDeltaMagic) +
203 kDeltaVersionLength + kDeltaProtobufLengthLength) {
204 // Don't have enough bytes to know the protobuf length.
205 return kMetadataParseInsufficientData;
206 }
207 if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) {
208 LOG(ERROR) << "Bad payload format -- invalid delta magic.";
209 return kMetadataParseError;
210 }
211 uint64_t protobuf_length;
212 COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength,
213 protobuf_length_size_mismatch);
214 memcpy(&protobuf_length,
215 &payload[strlen(kDeltaMagic) + kDeltaVersionLength],
216 kDeltaProtobufLengthLength);
217 protobuf_length = be64toh(protobuf_length); // switch big endian to host
218 if (payload.size() < strlen(kDeltaMagic) + kDeltaVersionLength +
219 kDeltaProtobufLengthLength + protobuf_length) {
220 return kMetadataParseInsufficientData;
221 }
222 // We have the full proto buffer in |payload|. Parse it.
223 const int offset = strlen(kDeltaMagic) + kDeltaVersionLength +
224 kDeltaProtobufLengthLength;
225 if (!manifest->ParseFromArray(&payload[offset], protobuf_length)) {
226 LOG(ERROR) << "Unable to parse manifest in update file.";
227 return kMetadataParseError;
228 }
229 *metadata_size = strlen(kDeltaMagic) + kDeltaVersionLength +
230 kDeltaProtobufLengthLength + protobuf_length;
231 return kMetadataParseSuccess;
232}
233
234
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700235// Wrapper around write. Returns bytes written on success or
236// -errno on error.
237// This function performs as many actions as it can, given the amount of
238// data received thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -0700239ssize_t DeltaPerformer::Write(const void* bytes, size_t count) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700240 const char* c_bytes = reinterpret_cast<const char*>(bytes);
241 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
242
243 if (!manifest_valid_) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800244 MetadataParseResult result = ParsePayloadMetadata(buffer_,
245 &manifest_,
246 &manifest_metadata_size_);
247 if (result == kMetadataParseError) {
Darin Petkov934bb412010-11-18 11:21:35 -0800248 return -EINVAL;
249 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800250 if (result == kMetadataParseInsufficientData) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700251 return count;
252 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700253 // Remove protobuf and header info from buffer_, so buffer_ contains
254 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700255 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700256 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700257 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700258 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700259 manifest_valid_ = true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700260 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700261 if (!PrimeUpdateState()) {
262 LOG(ERROR) << "Unable to prime the update state.";
263 return -EINVAL;
264 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700265 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700266 ssize_t total_operations = manifest_.install_operations_size() +
267 manifest_.kernel_install_operations_size();
268 while (next_operation_num_ < total_operations) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700269 const DeltaArchiveManifest_InstallOperation &op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700270 next_operation_num_ < manifest_.install_operations_size() ?
271 manifest_.install_operations(next_operation_num_) :
272 manifest_.kernel_install_operations(
273 next_operation_num_ - manifest_.install_operations_size());
274 if (!CanPerformInstallOperation(op))
275 break;
Darin Petkov45580e42010-10-08 14:02:40 -0700276 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700277 ScopedTerminatorExitUnblocker exit_unblocker =
278 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700279 // Log every thousandth operation, and also the first and last ones
280 if ((next_operation_num_ % 1000 == 0) ||
281 (next_operation_num_ + 1 == total_operations)) {
282 LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/"
283 << total_operations;
284 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700285 bool is_kernel_partition =
286 (next_operation_num_ >= manifest_.install_operations_size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700287 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
288 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700289 if (!PerformReplaceOperation(op, is_kernel_partition)) {
290 LOG(ERROR) << "Failed to perform replace operation "
291 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700292 return -EINVAL;
293 }
294 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700295 if (!PerformMoveOperation(op, is_kernel_partition)) {
296 LOG(ERROR) << "Failed to perform move operation "
297 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700298 return -EINVAL;
299 }
300 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700301 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
302 LOG(ERROR) << "Failed to perform bsdiff operation "
303 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700304 return -EINVAL;
305 }
306 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700307 next_operation_num_++;
Darin Petkov73058b42010-10-06 16:32:19 -0700308 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700309 }
310 return count;
311}
312
313bool DeltaPerformer::CanPerformInstallOperation(
314 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
315 operation) {
316 // Move operations don't require any data blob, so they can always
317 // be performed
318 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
319 return true;
320
321 // See if we have the entire data blob in the buffer
322 if (operation.data_offset() < buffer_offset_) {
323 LOG(ERROR) << "we threw away data it seems?";
324 return false;
325 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700326
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700327 return (operation.data_offset() + operation.data_length()) <=
328 (buffer_offset_ + buffer_.size());
329}
330
331bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700332 const DeltaArchiveManifest_InstallOperation& operation,
333 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700334 CHECK(operation.type() == \
335 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
336 operation.type() == \
337 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
338
339 // Since we delete data off the beginning of the buffer as we use it,
340 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700341 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
342 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700343
Darin Petkov437adc42010-10-07 13:12:24 -0700344 // Extract the signature message if it's in this operation.
345 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700346
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700347 DirectExtentWriter direct_writer;
348 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
349 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700350
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700351 // Since bzip decompression is optional, we have a variable writer that will
352 // point to one of the ExtentWriter objects above.
353 ExtentWriter* writer = NULL;
354 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
355 writer = &zero_pad_writer;
356 } else if (operation.type() ==
357 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
358 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
359 writer = bzip_writer.get();
360 } else {
361 NOTREACHED();
362 }
363
364 // Create a vector of extents to pass to the ExtentWriter.
365 vector<Extent> extents;
366 for (int i = 0; i < operation.dst_extents_size(); i++) {
367 extents.push_back(operation.dst_extents(i));
368 }
369
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700370 int fd = is_kernel_partition ? kernel_fd_ : fd_;
371
372 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700373 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
374 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700375
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700376 // Update buffer
377 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700378 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700379 return true;
380}
381
382bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700383 const DeltaArchiveManifest_InstallOperation& operation,
384 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700385 // Calculate buffer size. Note, this function doesn't do a sliding
386 // window to copy in case the source and destination blocks overlap.
387 // If we wanted to do a sliding window, we could program the server
388 // to generate deltas that effectively did a sliding window.
389
390 uint64_t blocks_to_read = 0;
391 for (int i = 0; i < operation.src_extents_size(); i++)
392 blocks_to_read += operation.src_extents(i).num_blocks();
393
394 uint64_t blocks_to_write = 0;
395 for (int i = 0; i < operation.dst_extents_size(); i++)
396 blocks_to_write += operation.dst_extents(i).num_blocks();
397
398 DCHECK_EQ(blocks_to_write, blocks_to_read);
399 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700400
401 int fd = is_kernel_partition ? kernel_fd_ : fd_;
402
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700403 // Read in bytes.
404 ssize_t bytes_read = 0;
405 for (int i = 0; i < operation.src_extents_size(); i++) {
406 ssize_t bytes_read_this_iteration = 0;
407 const Extent& extent = operation.src_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700408 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700409 &buf[bytes_read],
410 extent.num_blocks() * block_size_,
411 extent.start_block() * block_size_,
412 &bytes_read_this_iteration));
413 TEST_AND_RETURN_FALSE(
414 bytes_read_this_iteration ==
415 static_cast<ssize_t>(extent.num_blocks() * block_size_));
416 bytes_read += bytes_read_this_iteration;
417 }
418
Darin Petkov45580e42010-10-08 14:02:40 -0700419 // If this is a non-idempotent operation, request a delayed exit and clear the
420 // update state in case the operation gets interrupted. Do this as late as
421 // possible.
422 if (!IsIdempotentOperation(operation)) {
423 Terminator::set_exit_blocked(true);
424 ResetUpdateProgress(prefs_, true);
425 }
426
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700427 // Write bytes out.
428 ssize_t bytes_written = 0;
429 for (int i = 0; i < operation.dst_extents_size(); i++) {
430 const Extent& extent = operation.dst_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700431 TEST_AND_RETURN_FALSE(utils::PWriteAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700432 &buf[bytes_written],
433 extent.num_blocks() * block_size_,
434 extent.start_block() * block_size_));
435 bytes_written += extent.num_blocks() * block_size_;
436 }
437 DCHECK_EQ(bytes_written, bytes_read);
438 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
439 return true;
440}
441
442bool DeltaPerformer::ExtentsToBsdiffPositionsString(
443 const RepeatedPtrField<Extent>& extents,
444 uint64_t block_size,
445 uint64_t full_length,
446 string* positions_string) {
447 string ret;
448 uint64_t length = 0;
449 for (int i = 0; i < extents.size(); i++) {
450 Extent extent = extents.Get(i);
451 int64_t start = extent.start_block();
452 uint64_t this_length = min(full_length - length,
453 extent.num_blocks() * block_size);
454 if (start == static_cast<int64_t>(kSparseHole))
455 start = -1;
456 else
457 start *= block_size;
458 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
459 length += this_length;
460 }
461 TEST_AND_RETURN_FALSE(length == full_length);
462 if (!ret.empty())
463 ret.resize(ret.size() - 1); // Strip trailing comma off
464 *positions_string = ret;
465 return true;
466}
467
468bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700469 const DeltaArchiveManifest_InstallOperation& operation,
470 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700471 // Since we delete data off the beginning of the buffer as we use it,
472 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700473 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
474 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700475
476 string input_positions;
477 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
478 block_size_,
479 operation.src_length(),
480 &input_positions));
481 string output_positions;
482 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
483 block_size_,
484 operation.dst_length(),
485 &output_positions));
486
487 string temp_filename;
488 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
489 &temp_filename,
490 NULL));
491 ScopedPathUnlinker path_unlinker(temp_filename);
492 {
493 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
494 ScopedFdCloser fd_closer(&fd);
495 TEST_AND_RETURN_FALSE(
496 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
497 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700498
499 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700500 const string& path = StringPrintf("/dev/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700501
Darin Petkov45580e42010-10-08 14:02:40 -0700502 // If this is a non-idempotent operation, request a delayed exit and clear the
503 // update state in case the operation gets interrupted. Do this as late as
504 // possible.
505 if (!IsIdempotentOperation(operation)) {
506 Terminator::set_exit_blocked(true);
507 ResetUpdateProgress(prefs_, true);
508 }
509
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700510 vector<string> cmd;
511 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700512 cmd.push_back(path);
513 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700514 cmd.push_back(temp_filename);
515 cmd.push_back(input_positions);
516 cmd.push_back(output_positions);
517 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700518 TEST_AND_RETURN_FALSE(
519 Subprocess::SynchronousExecFlags(cmd,
520 &return_code,
521 G_SPAWN_LEAVE_DESCRIPTORS_OPEN));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700522 TEST_AND_RETURN_FALSE(return_code == 0);
523
524 if (operation.dst_length() % block_size_) {
525 // Zero out rest of final block.
526 // TODO(adlr): build this into bspatch; it's more efficient that way.
527 const Extent& last_extent =
528 operation.dst_extents(operation.dst_extents_size() - 1);
529 const uint64_t end_byte =
530 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
531 const uint64_t begin_byte =
532 end_byte - (block_size_ - operation.dst_length() % block_size_);
533 vector<char> zeros(end_byte - begin_byte);
534 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700535 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700536 }
537
538 // Update buffer.
539 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700540 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700541 return true;
542}
543
Darin Petkovd7061ab2010-10-06 14:37:09 -0700544bool DeltaPerformer::ExtractSignatureMessage(
545 const DeltaArchiveManifest_InstallOperation& operation) {
546 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
547 !manifest_.has_signatures_offset() ||
548 manifest_.signatures_offset() != operation.data_offset()) {
549 return false;
550 }
551 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
552 manifest_.signatures_size() == operation.data_length());
553 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
554 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
555 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
556 signatures_message_data_.insert(
557 signatures_message_data_.begin(),
558 buffer_.begin(),
559 buffer_.begin() + manifest_.signatures_size());
Darin Petkov437adc42010-10-07 13:12:24 -0700560 // The hash of all data consumed so far should be verified against the signed
561 // hash.
562 signed_hash_context_ = hash_calculator_.GetContext();
563 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
564 signed_hash_context_))
565 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700566 LOG(INFO) << "Extracted signature data of size "
567 << manifest_.signatures_size() << " at "
568 << manifest_.signatures_offset();
569 return true;
570}
571
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700572#define TEST_SET_TRUE_RET_TRUE(_ptr, _condition) \
573 do { \
574 if (!(_condition)) { \
575 LOG(ERROR) << "Non fatal public key verification: " << #_condition; \
576 if (_ptr) { \
577 *(_ptr) = true; \
578 } \
579 return true; \
580 } \
581 } while(0)
582
Darin Petkov437adc42010-10-07 13:12:24 -0700583bool DeltaPerformer::VerifyPayload(
584 const string& public_key_path,
585 const std::string& update_check_response_hash,
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700586 const uint64_t update_check_response_size,
587 bool* signature_failed) {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700588 string key_path = public_key_path;
589 if (key_path.empty()) {
590 key_path = kUpdatePayloadPublicKeyPath;
591 }
592 LOG(INFO) << "Verifying delta payload. Public key path: " << key_path;
Darin Petkov437adc42010-10-07 13:12:24 -0700593
594 // Verifies the download hash.
595 const string& download_hash_data = hash_calculator_.hash();
596 TEST_AND_RETURN_FALSE(!download_hash_data.empty());
597 TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash);
598
599 // Verifies the download size.
600 TEST_AND_RETURN_FALSE(update_check_response_size ==
601 manifest_metadata_size_ + buffer_offset_);
602
603 // Verifies the signed payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700604 if (!utils::FileExists(key_path.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700605 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700606 return true;
607 }
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700608 TEST_SET_TRUE_RET_TRUE(signature_failed, !signatures_message_data_.empty());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700609 vector<char> signed_hash_data;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700610 TEST_SET_TRUE_RET_TRUE(signature_failed, PayloadSigner::VerifySignature(
611 signatures_message_data_,
612 key_path,
613 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700614 OmahaHashCalculator signed_hasher;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700615 TEST_SET_TRUE_RET_TRUE(signature_failed,
616 signed_hasher.SetContext(signed_hash_context_));
617 TEST_SET_TRUE_RET_TRUE(signature_failed,
618 signed_hasher.Finalize());
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700619 vector<char> hash_data = signed_hasher.raw_hash();
620 PayloadSigner::PadRSA2048SHA256Hash(&hash_data);
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700621 TEST_SET_TRUE_RET_TRUE(signature_failed, !hash_data.empty());
622 if (hash_data != signed_hash_data) {
623 LOG(ERROR) << "Public key verificaion failed. This is non-fatal. "
624 "Attached Signature:";
625 utils::HexDumpVector(signed_hash_data);
626 LOG(ERROR) << "Computed Signature:";
627 utils::HexDumpVector(hash_data);
628 if (signature_failed) {
629 *signature_failed = true;
630 }
631 }
Darin Petkov437adc42010-10-07 13:12:24 -0700632 return true;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700633}
634
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700635#undef TEST_SET_TRUE_RET_TRUE
636
Darin Petkov3aefa862010-12-07 14:45:00 -0800637bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size,
638 vector<char>* kernel_hash,
639 uint64_t* rootfs_size,
640 vector<char>* rootfs_hash) {
Darin Petkov2dd01092010-10-08 15:43:05 -0700641 TEST_AND_RETURN_FALSE(manifest_valid_ &&
642 manifest_.has_new_kernel_info() &&
643 manifest_.has_new_rootfs_info());
Darin Petkov3aefa862010-12-07 14:45:00 -0800644 *kernel_size = manifest_.new_kernel_info().size();
645 *rootfs_size = manifest_.new_rootfs_info().size();
646 vector<char> new_kernel_hash(manifest_.new_kernel_info().hash().begin(),
647 manifest_.new_kernel_info().hash().end());
648 vector<char> new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(),
649 manifest_.new_rootfs_info().hash().end());
650 kernel_hash->swap(new_kernel_hash);
651 rootfs_hash->swap(new_rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -0700652 return true;
653}
654
Darin Petkov698d0412010-10-13 10:59:44 -0700655bool DeltaPerformer::VerifySourcePartitions() {
656 LOG(INFO) << "Verifying source partitions.";
657 CHECK(manifest_valid_);
658 if (manifest_.has_old_kernel_info()) {
659 const PartitionInfo& info = manifest_.old_kernel_info();
Darin Petkov3aefa862010-12-07 14:45:00 -0800660 TEST_AND_RETURN_FALSE(!current_kernel_hash_.empty() &&
661 current_kernel_hash_.size() == info.hash().size() &&
662 memcmp(current_kernel_hash_.data(),
Darin Petkov698d0412010-10-13 10:59:44 -0700663 info.hash().data(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800664 current_kernel_hash_.size()) == 0);
Darin Petkov698d0412010-10-13 10:59:44 -0700665 }
666 if (manifest_.has_old_rootfs_info()) {
667 const PartitionInfo& info = manifest_.old_rootfs_info();
Darin Petkov3aefa862010-12-07 14:45:00 -0800668 TEST_AND_RETURN_FALSE(!current_rootfs_hash_.empty() &&
669 current_rootfs_hash_.size() == info.hash().size() &&
670 memcmp(current_rootfs_hash_.data(),
Darin Petkov698d0412010-10-13 10:59:44 -0700671 info.hash().data(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800672 current_rootfs_hash_.size()) == 0);
Darin Petkov698d0412010-10-13 10:59:44 -0700673 }
674 return true;
675}
676
Darin Petkov437adc42010-10-07 13:12:24 -0700677void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
678 hash_calculator_.Update(&buffer_[0], count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700679 buffer_.erase(buffer_.begin(), buffer_.begin() + count);
680}
681
Darin Petkov0406e402010-10-06 21:33:11 -0700682bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
683 string update_check_response_hash) {
684 int64_t next_operation = kUpdateStateOperationInvalid;
685 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
686 &next_operation) &&
687 next_operation != kUpdateStateOperationInvalid &&
688 next_operation > 0);
689
690 string interrupted_hash;
691 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
692 &interrupted_hash) &&
693 !interrupted_hash.empty() &&
694 interrupted_hash == update_check_response_hash);
695
Darin Petkov61426142010-10-08 11:04:55 -0700696 int64_t resumed_update_failures;
697 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
698 &resumed_update_failures) ||
699 resumed_update_failures <= kMaxResumedUpdateFailures);
700
Darin Petkov0406e402010-10-06 21:33:11 -0700701 // Sanity check the rest.
702 int64_t next_data_offset = -1;
703 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
704 &next_data_offset) &&
705 next_data_offset >= 0);
706
Darin Petkov437adc42010-10-07 13:12:24 -0700707 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -0700708 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700709 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
710 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -0700711
712 int64_t manifest_metadata_size = 0;
713 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
714 &manifest_metadata_size) &&
715 manifest_metadata_size > 0);
716
717 return true;
718}
719
Darin Petkov9b230572010-10-08 10:20:09 -0700720bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -0700721 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
722 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -0700723 if (!quick) {
724 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
725 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
726 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
727 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
728 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -0700729 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700730 }
Darin Petkov73058b42010-10-06 16:32:19 -0700731 return true;
732}
733
734bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700735 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -0700736 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700737 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -0700738 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -0700739 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700740 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -0700741 hash_calculator_.GetContext()));
742 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
743 buffer_offset_));
744 last_updated_buffer_offset_ = buffer_offset_;
745 }
Darin Petkov73058b42010-10-06 16:32:19 -0700746 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
747 next_operation_num_));
748 return true;
749}
750
Darin Petkov9b230572010-10-08 10:20:09 -0700751bool DeltaPerformer::PrimeUpdateState() {
752 CHECK(manifest_valid_);
753 block_size_ = manifest_.block_size();
754
755 int64_t next_operation = kUpdateStateOperationInvalid;
756 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
757 next_operation == kUpdateStateOperationInvalid ||
758 next_operation <= 0) {
759 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -0700760 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -0700761 return true;
762 }
763 next_operation_num_ = next_operation;
764
765 // Resuming an update -- load the rest of the update state.
766 int64_t next_data_offset = -1;
767 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
768 &next_data_offset) &&
769 next_data_offset >= 0);
770 buffer_offset_ = next_data_offset;
771
772 // The signed hash context may be empty if the interrupted update didn't reach
773 // the signature blob.
774 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
775 &signed_hash_context_);
776
777 string hash_context;
778 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
779 &hash_context) &&
780 hash_calculator_.SetContext(hash_context));
781
782 int64_t manifest_metadata_size = 0;
783 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
784 &manifest_metadata_size) &&
785 manifest_metadata_size > 0);
786 manifest_metadata_size_ = manifest_metadata_size;
787
Darin Petkov61426142010-10-08 11:04:55 -0700788 // Speculatively count the resume as a failure.
789 int64_t resumed_update_failures;
790 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
791 resumed_update_failures++;
792 } else {
793 resumed_update_failures = 1;
794 }
795 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -0700796 return true;
797}
798
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700799} // namespace chromeos_update_engine