blob: e14ad4ec03b07b87d69cc30a7ebe3ecc33a5d89d [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
36namespace {
37
38const int kDeltaVersionLength = 8;
39const int kDeltaProtobufLengthLength = 8;
Darin Petkovd7061ab2010-10-06 14:37:09 -070040const char kUpdatePayloadPublicKeyPath[] =
41 "/usr/share/update_engine/update-payload-key.pub.pem";
Darin Petkov73058b42010-10-06 16:32:19 -070042const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070043const int kMaxResumedUpdateFailures = 10;
Darin Petkov73058b42010-10-06 16:32:19 -070044
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070045// Converts extents to a human-readable string, for use by DumpUpdateProto().
46string ExtentsToString(const RepeatedPtrField<Extent>& extents) {
47 string ret;
48 for (int i = 0; i < extents.size(); i++) {
49 const Extent& extent = extents.Get(i);
50 if (extent.start_block() == kSparseHole) {
51 ret += StringPrintf("{kSparseHole, %" PRIu64 "}, ", extent.num_blocks());
52 } else {
53 ret += StringPrintf("{%" PRIu64 ", %" PRIu64 "}, ",
54 extent.start_block(), extent.num_blocks());
55 }
56 }
57 if (!ret.empty()) {
58 DCHECK_GT(ret.size(), static_cast<size_t>(1));
59 ret.resize(ret.size() - 2);
60 }
61 return ret;
62}
63
64// LOGs a DeltaArchiveManifest object. Useful for debugging.
65void DumpUpdateProto(const DeltaArchiveManifest& manifest) {
66 LOG(INFO) << "Update Proto:";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070067 LOG(INFO) << " block_size: " << manifest.block_size();
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070068 for (int i = 0; i < (manifest.install_operations_size() +
69 manifest.kernel_install_operations_size()); i++) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070070 const DeltaArchiveManifest_InstallOperation& op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070071 i < manifest.install_operations_size() ?
72 manifest.install_operations(i) :
73 manifest.kernel_install_operations(
74 i - manifest.install_operations_size());
75 if (i == 0)
76 LOG(INFO) << " Rootfs ops:";
77 else if (i == manifest.install_operations_size())
78 LOG(INFO) << " Kernel ops:";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070079 LOG(INFO) << " operation(" << i << ")";
80 LOG(INFO) << " type: "
81 << DeltaArchiveManifest_InstallOperation_Type_Name(op.type());
82 if (op.has_data_offset())
83 LOG(INFO) << " data_offset: " << op.data_offset();
84 if (op.has_data_length())
85 LOG(INFO) << " data_length: " << op.data_length();
86 LOG(INFO) << " src_extents: " << ExtentsToString(op.src_extents());
87 if (op.has_src_length())
88 LOG(INFO) << " src_length: " << op.src_length();
89 LOG(INFO) << " dst_extents: " << ExtentsToString(op.dst_extents());
90 if (op.has_dst_length())
91 LOG(INFO) << " dst_length: " << op.dst_length();
92 }
93}
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070094
95// Opens path for read/write, put the fd into *fd. On success returns true
96// and sets *err to 0. On failure, returns false and sets *err to errno.
97bool OpenFile(const char* path, int* fd, int* err) {
98 if (*fd != -1) {
99 LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")";
100 *err = EINVAL;
101 return false;
102 }
103 *fd = open(path, O_RDWR, 000);
104 if (*fd < 0) {
105 *err = errno;
106 PLOG(ERROR) << "Unable to open file " << path;
107 return false;
108 }
109 *err = 0;
110 return true;
111}
112
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700113} // namespace {}
114
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700115// Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat
116// it safely. Returns false otherwise.
117bool DeltaPerformer::IsIdempotentOperation(
118 const DeltaArchiveManifest_InstallOperation& op) {
119 if (op.src_extents_size() == 0) {
120 return true;
121 }
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700122 // When in doubt, it's safe to declare an op non-idempotent. Note that we
123 // could detect other types of idempotent operations here such as a MOVE that
124 // moves blocks onto themselves. However, we rely on the server to not send
125 // such operations at all.
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700126 ExtentRanges src_ranges;
127 src_ranges.AddRepeatedExtents(op.src_extents());
128 const uint64_t block_count = src_ranges.blocks();
129 src_ranges.SubtractRepeatedExtents(op.dst_extents());
130 return block_count == src_ranges.blocks();
131}
132
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700133int DeltaPerformer::Open(const char* path, int flags, mode_t mode) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700134 int err;
135 if (OpenFile(path, &fd_, &err))
136 path_ = path;
137 return -err;
138}
139
140bool DeltaPerformer::OpenKernel(const char* kernel_path) {
141 int err;
142 bool success = OpenFile(kernel_path, &kernel_fd_, &err);
143 if (success)
144 kernel_path_ = kernel_path;
145 return success;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700146}
147
148int DeltaPerformer::Close() {
149 if (!buffer_.empty()) {
150 LOG(ERROR) << "Called Close() while buffer not empty!";
151 return -1;
152 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700153 int err = 0;
154 if (close(kernel_fd_) == -1) {
155 err = errno;
156 PLOG(ERROR) << "Unable to close kernel fd:";
157 }
158 if (close(fd_) == -1) {
159 err = errno;
160 PLOG(ERROR) << "Unable to close rootfs fd:";
161 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700162 LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700163 fd_ = -2; // Set so that isn't not valid AND calls to Open() will fail.
164 path_ = "";
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700165 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700166}
167
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700168namespace {
169
170void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
171 string sha256;
172 if (OmahaHashCalculator::Base64Encode(info.hash().data(),
173 info.hash().size(),
174 &sha256)) {
175 LOG(INFO) << "PartitionInfo " << tag << " sha256:" << sha256
176 << " length:" << tag.size();
177 } else {
178 LOG(ERROR) << "Base64Encode failed for tag: " << tag;
179 }
180}
181
182void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
183 if (manifest.has_old_kernel_info())
184 LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info");
185 if (manifest.has_old_rootfs_info())
186 LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info");
187 if (manifest.has_new_kernel_info())
188 LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info");
189 if (manifest.has_new_rootfs_info())
190 LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info");
191}
192
193} // namespace {}
194
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700195// Wrapper around write. Returns bytes written on success or
196// -errno on error.
197// This function performs as many actions as it can, given the amount of
198// data received thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -0700199ssize_t DeltaPerformer::Write(const void* bytes, size_t count) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700200 const char* c_bytes = reinterpret_cast<const char*>(bytes);
201 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
202
203 if (!manifest_valid_) {
204 // See if we have enough bytes for the manifest yet
205 if (buffer_.size() < strlen(kDeltaMagic) +
206 kDeltaVersionLength + kDeltaProtobufLengthLength) {
207 // Don't have enough bytes to even know the protobuf length
208 return count;
209 }
210 uint64_t protobuf_length;
211 COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength,
212 protobuf_length_size_mismatch);
213 memcpy(&protobuf_length,
214 &buffer_[strlen(kDeltaMagic) + kDeltaVersionLength],
215 kDeltaProtobufLengthLength);
216 protobuf_length = be64toh(protobuf_length); // switch big endian to host
217 if (buffer_.size() < strlen(kDeltaMagic) + kDeltaVersionLength +
218 kDeltaProtobufLengthLength + protobuf_length) {
219 return count;
220 }
221 // We have the full proto buffer in buffer_. Parse it.
222 const int offset = strlen(kDeltaMagic) + kDeltaVersionLength +
223 kDeltaProtobufLengthLength;
224 if (!manifest_.ParseFromArray(&buffer_[offset], protobuf_length)) {
225 LOG(ERROR) << "Unable to parse manifest in update file.";
226 return -EINVAL;
227 }
228 // Remove protobuf and header info from buffer_, so buffer_ contains
229 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700230 manifest_metadata_size_ = strlen(kDeltaMagic) + kDeltaVersionLength +
Darin Petkov73058b42010-10-06 16:32:19 -0700231 kDeltaProtobufLengthLength + protobuf_length;
Darin Petkov437adc42010-10-07 13:12:24 -0700232 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700233 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700234 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700235 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700236 manifest_valid_ = true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700237 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700238 if (!PrimeUpdateState()) {
239 LOG(ERROR) << "Unable to prime the update state.";
240 return -EINVAL;
241 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700242 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700243 ssize_t total_operations = manifest_.install_operations_size() +
244 manifest_.kernel_install_operations_size();
245 while (next_operation_num_ < total_operations) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700246 const DeltaArchiveManifest_InstallOperation &op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700247 next_operation_num_ < manifest_.install_operations_size() ?
248 manifest_.install_operations(next_operation_num_) :
249 manifest_.kernel_install_operations(
250 next_operation_num_ - manifest_.install_operations_size());
251 if (!CanPerformInstallOperation(op))
252 break;
Darin Petkov45580e42010-10-08 14:02:40 -0700253 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700254 ScopedTerminatorExitUnblocker exit_unblocker =
255 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700256 // Log every thousandth operation, and also the first and last ones
257 if ((next_operation_num_ % 1000 == 0) ||
258 (next_operation_num_ + 1 == total_operations)) {
259 LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/"
260 << total_operations;
261 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700262 bool is_kernel_partition =
263 (next_operation_num_ >= manifest_.install_operations_size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700264 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
265 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700266 if (!PerformReplaceOperation(op, is_kernel_partition)) {
267 LOG(ERROR) << "Failed to perform replace operation "
268 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700269 return -EINVAL;
270 }
271 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700272 if (!PerformMoveOperation(op, is_kernel_partition)) {
273 LOG(ERROR) << "Failed to perform move operation "
274 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700275 return -EINVAL;
276 }
277 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700278 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
279 LOG(ERROR) << "Failed to perform bsdiff operation "
280 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700281 return -EINVAL;
282 }
283 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700284 next_operation_num_++;
Darin Petkov73058b42010-10-06 16:32:19 -0700285 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700286 }
287 return count;
288}
289
290bool DeltaPerformer::CanPerformInstallOperation(
291 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
292 operation) {
293 // Move operations don't require any data blob, so they can always
294 // be performed
295 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
296 return true;
297
298 // See if we have the entire data blob in the buffer
299 if (operation.data_offset() < buffer_offset_) {
300 LOG(ERROR) << "we threw away data it seems?";
301 return false;
302 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700303
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700304 return (operation.data_offset() + operation.data_length()) <=
305 (buffer_offset_ + buffer_.size());
306}
307
308bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700309 const DeltaArchiveManifest_InstallOperation& operation,
310 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700311 CHECK(operation.type() == \
312 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
313 operation.type() == \
314 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
315
316 // Since we delete data off the beginning of the buffer as we use it,
317 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700318 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
319 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700320
Darin Petkov437adc42010-10-07 13:12:24 -0700321 // Extract the signature message if it's in this operation.
322 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700323
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700324 DirectExtentWriter direct_writer;
325 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
326 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700327
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700328 // Since bzip decompression is optional, we have a variable writer that will
329 // point to one of the ExtentWriter objects above.
330 ExtentWriter* writer = NULL;
331 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
332 writer = &zero_pad_writer;
333 } else if (operation.type() ==
334 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
335 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
336 writer = bzip_writer.get();
337 } else {
338 NOTREACHED();
339 }
340
341 // Create a vector of extents to pass to the ExtentWriter.
342 vector<Extent> extents;
343 for (int i = 0; i < operation.dst_extents_size(); i++) {
344 extents.push_back(operation.dst_extents(i));
345 }
346
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700347 int fd = is_kernel_partition ? kernel_fd_ : fd_;
348
349 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700350 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
351 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700352
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700353 // Update buffer
354 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700355 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700356 return true;
357}
358
359bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700360 const DeltaArchiveManifest_InstallOperation& operation,
361 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700362 // Calculate buffer size. Note, this function doesn't do a sliding
363 // window to copy in case the source and destination blocks overlap.
364 // If we wanted to do a sliding window, we could program the server
365 // to generate deltas that effectively did a sliding window.
366
367 uint64_t blocks_to_read = 0;
368 for (int i = 0; i < operation.src_extents_size(); i++)
369 blocks_to_read += operation.src_extents(i).num_blocks();
370
371 uint64_t blocks_to_write = 0;
372 for (int i = 0; i < operation.dst_extents_size(); i++)
373 blocks_to_write += operation.dst_extents(i).num_blocks();
374
375 DCHECK_EQ(blocks_to_write, blocks_to_read);
376 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700377
378 int fd = is_kernel_partition ? kernel_fd_ : fd_;
379
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700380 // Read in bytes.
381 ssize_t bytes_read = 0;
382 for (int i = 0; i < operation.src_extents_size(); i++) {
383 ssize_t bytes_read_this_iteration = 0;
384 const Extent& extent = operation.src_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700385 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700386 &buf[bytes_read],
387 extent.num_blocks() * block_size_,
388 extent.start_block() * block_size_,
389 &bytes_read_this_iteration));
390 TEST_AND_RETURN_FALSE(
391 bytes_read_this_iteration ==
392 static_cast<ssize_t>(extent.num_blocks() * block_size_));
393 bytes_read += bytes_read_this_iteration;
394 }
395
Darin Petkov45580e42010-10-08 14:02:40 -0700396 // If this is a non-idempotent operation, request a delayed exit and clear the
397 // update state in case the operation gets interrupted. Do this as late as
398 // possible.
399 if (!IsIdempotentOperation(operation)) {
400 Terminator::set_exit_blocked(true);
401 ResetUpdateProgress(prefs_, true);
402 }
403
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700404 // Write bytes out.
405 ssize_t bytes_written = 0;
406 for (int i = 0; i < operation.dst_extents_size(); i++) {
407 const Extent& extent = operation.dst_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700408 TEST_AND_RETURN_FALSE(utils::PWriteAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700409 &buf[bytes_written],
410 extent.num_blocks() * block_size_,
411 extent.start_block() * block_size_));
412 bytes_written += extent.num_blocks() * block_size_;
413 }
414 DCHECK_EQ(bytes_written, bytes_read);
415 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
416 return true;
417}
418
419bool DeltaPerformer::ExtentsToBsdiffPositionsString(
420 const RepeatedPtrField<Extent>& extents,
421 uint64_t block_size,
422 uint64_t full_length,
423 string* positions_string) {
424 string ret;
425 uint64_t length = 0;
426 for (int i = 0; i < extents.size(); i++) {
427 Extent extent = extents.Get(i);
428 int64_t start = extent.start_block();
429 uint64_t this_length = min(full_length - length,
430 extent.num_blocks() * block_size);
431 if (start == static_cast<int64_t>(kSparseHole))
432 start = -1;
433 else
434 start *= block_size;
435 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
436 length += this_length;
437 }
438 TEST_AND_RETURN_FALSE(length == full_length);
439 if (!ret.empty())
440 ret.resize(ret.size() - 1); // Strip trailing comma off
441 *positions_string = ret;
442 return true;
443}
444
445bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700446 const DeltaArchiveManifest_InstallOperation& operation,
447 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700448 // Since we delete data off the beginning of the buffer as we use it,
449 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700450 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
451 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700452
453 string input_positions;
454 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
455 block_size_,
456 operation.src_length(),
457 &input_positions));
458 string output_positions;
459 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
460 block_size_,
461 operation.dst_length(),
462 &output_positions));
463
464 string temp_filename;
465 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
466 &temp_filename,
467 NULL));
468 ScopedPathUnlinker path_unlinker(temp_filename);
469 {
470 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
471 ScopedFdCloser fd_closer(&fd);
472 TEST_AND_RETURN_FALSE(
473 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
474 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700475
476 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700477 const string& path = StringPrintf("/dev/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700478
Darin Petkov45580e42010-10-08 14:02:40 -0700479 // If this is a non-idempotent operation, request a delayed exit and clear the
480 // update state in case the operation gets interrupted. Do this as late as
481 // possible.
482 if (!IsIdempotentOperation(operation)) {
483 Terminator::set_exit_blocked(true);
484 ResetUpdateProgress(prefs_, true);
485 }
486
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700487 vector<string> cmd;
488 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700489 cmd.push_back(path);
490 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700491 cmd.push_back(temp_filename);
492 cmd.push_back(input_positions);
493 cmd.push_back(output_positions);
494 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700495 TEST_AND_RETURN_FALSE(
496 Subprocess::SynchronousExecFlags(cmd,
497 &return_code,
498 G_SPAWN_LEAVE_DESCRIPTORS_OPEN));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700499 TEST_AND_RETURN_FALSE(return_code == 0);
500
501 if (operation.dst_length() % block_size_) {
502 // Zero out rest of final block.
503 // TODO(adlr): build this into bspatch; it's more efficient that way.
504 const Extent& last_extent =
505 operation.dst_extents(operation.dst_extents_size() - 1);
506 const uint64_t end_byte =
507 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
508 const uint64_t begin_byte =
509 end_byte - (block_size_ - operation.dst_length() % block_size_);
510 vector<char> zeros(end_byte - begin_byte);
511 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700512 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700513 }
514
515 // Update buffer.
516 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700517 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700518 return true;
519}
520
Darin Petkovd7061ab2010-10-06 14:37:09 -0700521bool DeltaPerformer::ExtractSignatureMessage(
522 const DeltaArchiveManifest_InstallOperation& operation) {
523 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
524 !manifest_.has_signatures_offset() ||
525 manifest_.signatures_offset() != operation.data_offset()) {
526 return false;
527 }
528 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
529 manifest_.signatures_size() == operation.data_length());
530 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
531 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
532 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
533 signatures_message_data_.insert(
534 signatures_message_data_.begin(),
535 buffer_.begin(),
536 buffer_.begin() + manifest_.signatures_size());
Darin Petkov437adc42010-10-07 13:12:24 -0700537 // The hash of all data consumed so far should be verified against the signed
538 // hash.
539 signed_hash_context_ = hash_calculator_.GetContext();
540 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
541 signed_hash_context_))
542 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700543 LOG(INFO) << "Extracted signature data of size "
544 << manifest_.signatures_size() << " at "
545 << manifest_.signatures_offset();
546 return true;
547}
548
Darin Petkov437adc42010-10-07 13:12:24 -0700549bool DeltaPerformer::VerifyPayload(
550 const string& public_key_path,
551 const std::string& update_check_response_hash,
552 const uint64_t update_check_response_size) {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700553 string key_path = public_key_path;
554 if (key_path.empty()) {
555 key_path = kUpdatePayloadPublicKeyPath;
556 }
557 LOG(INFO) << "Verifying delta payload. Public key path: " << key_path;
Darin Petkov437adc42010-10-07 13:12:24 -0700558
559 // Verifies the download hash.
560 const string& download_hash_data = hash_calculator_.hash();
561 TEST_AND_RETURN_FALSE(!download_hash_data.empty());
562 TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash);
563
564 // Verifies the download size.
565 TEST_AND_RETURN_FALSE(update_check_response_size ==
566 manifest_metadata_size_ + buffer_offset_);
567
568 // Verifies the signed payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700569 if (!utils::FileExists(key_path.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700570 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700571 return true;
572 }
573 TEST_AND_RETURN_FALSE(!signatures_message_data_.empty());
574 vector<char> signed_hash_data;
575 TEST_AND_RETURN_FALSE(PayloadSigner::VerifySignature(signatures_message_data_,
576 key_path,
577 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700578 OmahaHashCalculator signed_hasher;
Darin Petkov437adc42010-10-07 13:12:24 -0700579 TEST_AND_RETURN_FALSE(signed_hasher.SetContext(signed_hash_context_));
580 TEST_AND_RETURN_FALSE(signed_hasher.Finalize());
581 const vector<char>& hash_data = signed_hasher.raw_hash();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700582 TEST_AND_RETURN_FALSE(!hash_data.empty());
Darin Petkov437adc42010-10-07 13:12:24 -0700583 TEST_AND_RETURN_FALSE(hash_data == signed_hash_data);
584 return true;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700585}
586
Darin Petkov2dd01092010-10-08 15:43:05 -0700587bool DeltaPerformer::VerifyAppliedUpdate(const string& path,
588 const string& kernel_path) {
589 LOG(INFO) << "Verifying applied update.";
590 TEST_AND_RETURN_FALSE(manifest_valid_ &&
591 manifest_.has_new_kernel_info() &&
592 manifest_.has_new_rootfs_info());
593 const string* paths[] = { &kernel_path, &path };
594 const PartitionInfo* infos[] = {
595 &manifest_.new_kernel_info(), &manifest_.new_rootfs_info()
596 };
597 for (size_t i = 0; i < arraysize(paths); ++i) {
598 OmahaHashCalculator hasher;
599 TEST_AND_RETURN_FALSE(hasher.UpdateFile(*paths[i], infos[i]->size()));
600 TEST_AND_RETURN_FALSE(hasher.Finalize());
601 TEST_AND_RETURN_FALSE(hasher.raw_hash().size() == infos[i]->hash().size());
602 TEST_AND_RETURN_FALSE(memcmp(hasher.raw_hash().data(),
603 infos[i]->hash().data(),
604 hasher.raw_hash().size()) == 0);
605 }
606 return true;
607}
608
Darin Petkov698d0412010-10-13 10:59:44 -0700609bool DeltaPerformer::VerifySourcePartitions() {
610 LOG(INFO) << "Verifying source partitions.";
611 CHECK(manifest_valid_);
612 if (manifest_.has_old_kernel_info()) {
613 const PartitionInfo& info = manifest_.old_kernel_info();
614 TEST_AND_RETURN_FALSE(current_kernel_hash_ != NULL &&
615 current_kernel_hash_->size() == info.hash().size() &&
616 memcmp(current_kernel_hash_->data(),
617 info.hash().data(),
618 current_kernel_hash_->size()) == 0);
619 }
620 if (manifest_.has_old_rootfs_info()) {
621 const PartitionInfo& info = manifest_.old_rootfs_info();
622 TEST_AND_RETURN_FALSE(current_rootfs_hash_ != NULL &&
623 current_rootfs_hash_->size() == info.hash().size() &&
624 memcmp(current_rootfs_hash_->data(),
625 info.hash().data(),
626 current_rootfs_hash_->size()) == 0);
627 }
628 return true;
629}
630
Darin Petkov437adc42010-10-07 13:12:24 -0700631void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
632 hash_calculator_.Update(&buffer_[0], count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700633 buffer_.erase(buffer_.begin(), buffer_.begin() + count);
634}
635
Darin Petkov0406e402010-10-06 21:33:11 -0700636bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
637 string update_check_response_hash) {
638 int64_t next_operation = kUpdateStateOperationInvalid;
639 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
640 &next_operation) &&
641 next_operation != kUpdateStateOperationInvalid &&
642 next_operation > 0);
643
644 string interrupted_hash;
645 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
646 &interrupted_hash) &&
647 !interrupted_hash.empty() &&
648 interrupted_hash == update_check_response_hash);
649
Darin Petkov61426142010-10-08 11:04:55 -0700650 int64_t resumed_update_failures;
651 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
652 &resumed_update_failures) ||
653 resumed_update_failures <= kMaxResumedUpdateFailures);
654
Darin Petkov0406e402010-10-06 21:33:11 -0700655 // Sanity check the rest.
656 int64_t next_data_offset = -1;
657 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
658 &next_data_offset) &&
659 next_data_offset >= 0);
660
Darin Petkov437adc42010-10-07 13:12:24 -0700661 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -0700662 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700663 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
664 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -0700665
666 int64_t manifest_metadata_size = 0;
667 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
668 &manifest_metadata_size) &&
669 manifest_metadata_size > 0);
670
671 return true;
672}
673
Darin Petkov9b230572010-10-08 10:20:09 -0700674bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -0700675 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
676 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -0700677 if (!quick) {
678 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
679 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
680 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
681 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
682 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -0700683 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700684 }
Darin Petkov73058b42010-10-06 16:32:19 -0700685 return true;
686}
687
688bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700689 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -0700690 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700691 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -0700692 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -0700693 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700694 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -0700695 hash_calculator_.GetContext()));
696 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
697 buffer_offset_));
698 last_updated_buffer_offset_ = buffer_offset_;
699 }
Darin Petkov73058b42010-10-06 16:32:19 -0700700 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
701 next_operation_num_));
702 return true;
703}
704
Darin Petkov9b230572010-10-08 10:20:09 -0700705bool DeltaPerformer::PrimeUpdateState() {
706 CHECK(manifest_valid_);
707 block_size_ = manifest_.block_size();
708
709 int64_t next_operation = kUpdateStateOperationInvalid;
710 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
711 next_operation == kUpdateStateOperationInvalid ||
712 next_operation <= 0) {
713 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -0700714 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -0700715 return true;
716 }
717 next_operation_num_ = next_operation;
718
719 // Resuming an update -- load the rest of the update state.
720 int64_t next_data_offset = -1;
721 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
722 &next_data_offset) &&
723 next_data_offset >= 0);
724 buffer_offset_ = next_data_offset;
725
726 // The signed hash context may be empty if the interrupted update didn't reach
727 // the signature blob.
728 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
729 &signed_hash_context_);
730
731 string hash_context;
732 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
733 &hash_context) &&
734 hash_calculator_.SetContext(hash_context));
735
736 int64_t manifest_metadata_size = 0;
737 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
738 &manifest_metadata_size) &&
739 manifest_metadata_size > 0);
740 manifest_metadata_size_ = manifest_metadata_size;
741
Darin Petkov61426142010-10-08 11:04:55 -0700742 // Speculatively count the resume as a failure.
743 int64_t resumed_update_failures;
744 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
745 resumed_update_failures++;
746 } else {
747 resumed_update_failures = 1;
748 }
749 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -0700750 return true;
751}
752
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700753} // namespace chromeos_update_engine