blob: a5b7a729f2618fa0bee9f1fbe805140e3ae9ccec [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() {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700149 int err = 0;
150 if (close(kernel_fd_) == -1) {
151 err = errno;
152 PLOG(ERROR) << "Unable to close kernel fd:";
153 }
154 if (close(fd_) == -1) {
155 err = errno;
156 PLOG(ERROR) << "Unable to close rootfs fd:";
157 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700158 LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800159 fd_ = -2; // Set to invalid so that calls to Open() will fail.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700160 path_ = "";
Darin Petkov934bb412010-11-18 11:21:35 -0800161 if (!buffer_.empty()) {
162 LOG(ERROR) << "Called Close() while buffer not empty!";
163 if (err >= 0) {
164 err = 1;
165 }
166 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700167 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700168}
169
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700170namespace {
171
172void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
173 string sha256;
174 if (OmahaHashCalculator::Base64Encode(info.hash().data(),
175 info.hash().size(),
176 &sha256)) {
177 LOG(INFO) << "PartitionInfo " << tag << " sha256:" << sha256
178 << " length:" << tag.size();
179 } else {
180 LOG(ERROR) << "Base64Encode failed for tag: " << tag;
181 }
182}
183
184void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
185 if (manifest.has_old_kernel_info())
186 LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info");
187 if (manifest.has_old_rootfs_info())
188 LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info");
189 if (manifest.has_new_kernel_info())
190 LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info");
191 if (manifest.has_new_rootfs_info())
192 LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info");
193}
194
195} // namespace {}
196
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700197// Wrapper around write. Returns bytes written on success or
198// -errno on error.
199// This function performs as many actions as it can, given the amount of
200// data received thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -0700201ssize_t DeltaPerformer::Write(const void* bytes, size_t count) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700202 const char* c_bytes = reinterpret_cast<const char*>(bytes);
203 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
204
205 if (!manifest_valid_) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700206 if (buffer_.size() < strlen(kDeltaMagic) +
207 kDeltaVersionLength + kDeltaProtobufLengthLength) {
Darin Petkov934bb412010-11-18 11:21:35 -0800208 // Don't have enough bytes to know the protobuf length.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700209 return count;
210 }
Darin Petkov934bb412010-11-18 11:21:35 -0800211 if (memcmp(buffer_.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) {
212 LOG(ERROR) << "Bad payload format -- invalid delta magic.";
213 return -EINVAL;
214 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700215 uint64_t protobuf_length;
216 COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength,
217 protobuf_length_size_mismatch);
218 memcpy(&protobuf_length,
219 &buffer_[strlen(kDeltaMagic) + kDeltaVersionLength],
220 kDeltaProtobufLengthLength);
221 protobuf_length = be64toh(protobuf_length); // switch big endian to host
222 if (buffer_.size() < strlen(kDeltaMagic) + kDeltaVersionLength +
223 kDeltaProtobufLengthLength + protobuf_length) {
224 return count;
225 }
226 // We have the full proto buffer in buffer_. Parse it.
227 const int offset = strlen(kDeltaMagic) + kDeltaVersionLength +
228 kDeltaProtobufLengthLength;
229 if (!manifest_.ParseFromArray(&buffer_[offset], protobuf_length)) {
230 LOG(ERROR) << "Unable to parse manifest in update file.";
231 return -EINVAL;
232 }
233 // Remove protobuf and header info from buffer_, so buffer_ contains
234 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700235 manifest_metadata_size_ = strlen(kDeltaMagic) + kDeltaVersionLength +
Darin Petkov73058b42010-10-06 16:32:19 -0700236 kDeltaProtobufLengthLength + protobuf_length;
Darin Petkov437adc42010-10-07 13:12:24 -0700237 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700238 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700239 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700240 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700241 manifest_valid_ = true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700242 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700243 if (!PrimeUpdateState()) {
244 LOG(ERROR) << "Unable to prime the update state.";
245 return -EINVAL;
246 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700247 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700248 ssize_t total_operations = manifest_.install_operations_size() +
249 manifest_.kernel_install_operations_size();
250 while (next_operation_num_ < total_operations) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700251 const DeltaArchiveManifest_InstallOperation &op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700252 next_operation_num_ < manifest_.install_operations_size() ?
253 manifest_.install_operations(next_operation_num_) :
254 manifest_.kernel_install_operations(
255 next_operation_num_ - manifest_.install_operations_size());
256 if (!CanPerformInstallOperation(op))
257 break;
Darin Petkov45580e42010-10-08 14:02:40 -0700258 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700259 ScopedTerminatorExitUnblocker exit_unblocker =
260 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700261 // Log every thousandth operation, and also the first and last ones
262 if ((next_operation_num_ % 1000 == 0) ||
263 (next_operation_num_ + 1 == total_operations)) {
264 LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/"
265 << total_operations;
266 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700267 bool is_kernel_partition =
268 (next_operation_num_ >= manifest_.install_operations_size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700269 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
270 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700271 if (!PerformReplaceOperation(op, is_kernel_partition)) {
272 LOG(ERROR) << "Failed to perform replace operation "
273 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700274 return -EINVAL;
275 }
276 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700277 if (!PerformMoveOperation(op, is_kernel_partition)) {
278 LOG(ERROR) << "Failed to perform move operation "
279 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700280 return -EINVAL;
281 }
282 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700283 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
284 LOG(ERROR) << "Failed to perform bsdiff operation "
285 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700286 return -EINVAL;
287 }
288 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700289 next_operation_num_++;
Darin Petkov73058b42010-10-06 16:32:19 -0700290 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700291 }
292 return count;
293}
294
295bool DeltaPerformer::CanPerformInstallOperation(
296 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
297 operation) {
298 // Move operations don't require any data blob, so they can always
299 // be performed
300 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
301 return true;
302
303 // See if we have the entire data blob in the buffer
304 if (operation.data_offset() < buffer_offset_) {
305 LOG(ERROR) << "we threw away data it seems?";
306 return false;
307 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700308
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700309 return (operation.data_offset() + operation.data_length()) <=
310 (buffer_offset_ + buffer_.size());
311}
312
313bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700314 const DeltaArchiveManifest_InstallOperation& operation,
315 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700316 CHECK(operation.type() == \
317 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
318 operation.type() == \
319 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
320
321 // Since we delete data off the beginning of the buffer as we use it,
322 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700323 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
324 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700325
Darin Petkov437adc42010-10-07 13:12:24 -0700326 // Extract the signature message if it's in this operation.
327 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700328
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700329 DirectExtentWriter direct_writer;
330 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
331 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700332
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700333 // Since bzip decompression is optional, we have a variable writer that will
334 // point to one of the ExtentWriter objects above.
335 ExtentWriter* writer = NULL;
336 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
337 writer = &zero_pad_writer;
338 } else if (operation.type() ==
339 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
340 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
341 writer = bzip_writer.get();
342 } else {
343 NOTREACHED();
344 }
345
346 // Create a vector of extents to pass to the ExtentWriter.
347 vector<Extent> extents;
348 for (int i = 0; i < operation.dst_extents_size(); i++) {
349 extents.push_back(operation.dst_extents(i));
350 }
351
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700352 int fd = is_kernel_partition ? kernel_fd_ : fd_;
353
354 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700355 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
356 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700357
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700358 // Update buffer
359 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700360 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700361 return true;
362}
363
364bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700365 const DeltaArchiveManifest_InstallOperation& operation,
366 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700367 // Calculate buffer size. Note, this function doesn't do a sliding
368 // window to copy in case the source and destination blocks overlap.
369 // If we wanted to do a sliding window, we could program the server
370 // to generate deltas that effectively did a sliding window.
371
372 uint64_t blocks_to_read = 0;
373 for (int i = 0; i < operation.src_extents_size(); i++)
374 blocks_to_read += operation.src_extents(i).num_blocks();
375
376 uint64_t blocks_to_write = 0;
377 for (int i = 0; i < operation.dst_extents_size(); i++)
378 blocks_to_write += operation.dst_extents(i).num_blocks();
379
380 DCHECK_EQ(blocks_to_write, blocks_to_read);
381 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700382
383 int fd = is_kernel_partition ? kernel_fd_ : fd_;
384
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700385 // Read in bytes.
386 ssize_t bytes_read = 0;
387 for (int i = 0; i < operation.src_extents_size(); i++) {
388 ssize_t bytes_read_this_iteration = 0;
389 const Extent& extent = operation.src_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700390 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700391 &buf[bytes_read],
392 extent.num_blocks() * block_size_,
393 extent.start_block() * block_size_,
394 &bytes_read_this_iteration));
395 TEST_AND_RETURN_FALSE(
396 bytes_read_this_iteration ==
397 static_cast<ssize_t>(extent.num_blocks() * block_size_));
398 bytes_read += bytes_read_this_iteration;
399 }
400
Darin Petkov45580e42010-10-08 14:02:40 -0700401 // If this is a non-idempotent operation, request a delayed exit and clear the
402 // update state in case the operation gets interrupted. Do this as late as
403 // possible.
404 if (!IsIdempotentOperation(operation)) {
405 Terminator::set_exit_blocked(true);
406 ResetUpdateProgress(prefs_, true);
407 }
408
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700409 // Write bytes out.
410 ssize_t bytes_written = 0;
411 for (int i = 0; i < operation.dst_extents_size(); i++) {
412 const Extent& extent = operation.dst_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700413 TEST_AND_RETURN_FALSE(utils::PWriteAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700414 &buf[bytes_written],
415 extent.num_blocks() * block_size_,
416 extent.start_block() * block_size_));
417 bytes_written += extent.num_blocks() * block_size_;
418 }
419 DCHECK_EQ(bytes_written, bytes_read);
420 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
421 return true;
422}
423
424bool DeltaPerformer::ExtentsToBsdiffPositionsString(
425 const RepeatedPtrField<Extent>& extents,
426 uint64_t block_size,
427 uint64_t full_length,
428 string* positions_string) {
429 string ret;
430 uint64_t length = 0;
431 for (int i = 0; i < extents.size(); i++) {
432 Extent extent = extents.Get(i);
433 int64_t start = extent.start_block();
434 uint64_t this_length = min(full_length - length,
435 extent.num_blocks() * block_size);
436 if (start == static_cast<int64_t>(kSparseHole))
437 start = -1;
438 else
439 start *= block_size;
440 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
441 length += this_length;
442 }
443 TEST_AND_RETURN_FALSE(length == full_length);
444 if (!ret.empty())
445 ret.resize(ret.size() - 1); // Strip trailing comma off
446 *positions_string = ret;
447 return true;
448}
449
450bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700451 const DeltaArchiveManifest_InstallOperation& operation,
452 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700453 // Since we delete data off the beginning of the buffer as we use it,
454 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700455 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
456 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700457
458 string input_positions;
459 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
460 block_size_,
461 operation.src_length(),
462 &input_positions));
463 string output_positions;
464 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
465 block_size_,
466 operation.dst_length(),
467 &output_positions));
468
469 string temp_filename;
470 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
471 &temp_filename,
472 NULL));
473 ScopedPathUnlinker path_unlinker(temp_filename);
474 {
475 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
476 ScopedFdCloser fd_closer(&fd);
477 TEST_AND_RETURN_FALSE(
478 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
479 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700480
481 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700482 const string& path = StringPrintf("/dev/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700483
Darin Petkov45580e42010-10-08 14:02:40 -0700484 // If this is a non-idempotent operation, request a delayed exit and clear the
485 // update state in case the operation gets interrupted. Do this as late as
486 // possible.
487 if (!IsIdempotentOperation(operation)) {
488 Terminator::set_exit_blocked(true);
489 ResetUpdateProgress(prefs_, true);
490 }
491
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700492 vector<string> cmd;
493 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700494 cmd.push_back(path);
495 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700496 cmd.push_back(temp_filename);
497 cmd.push_back(input_positions);
498 cmd.push_back(output_positions);
499 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700500 TEST_AND_RETURN_FALSE(
501 Subprocess::SynchronousExecFlags(cmd,
502 &return_code,
503 G_SPAWN_LEAVE_DESCRIPTORS_OPEN));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700504 TEST_AND_RETURN_FALSE(return_code == 0);
505
506 if (operation.dst_length() % block_size_) {
507 // Zero out rest of final block.
508 // TODO(adlr): build this into bspatch; it's more efficient that way.
509 const Extent& last_extent =
510 operation.dst_extents(operation.dst_extents_size() - 1);
511 const uint64_t end_byte =
512 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
513 const uint64_t begin_byte =
514 end_byte - (block_size_ - operation.dst_length() % block_size_);
515 vector<char> zeros(end_byte - begin_byte);
516 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700517 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700518 }
519
520 // Update buffer.
521 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700522 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700523 return true;
524}
525
Darin Petkovd7061ab2010-10-06 14:37:09 -0700526bool DeltaPerformer::ExtractSignatureMessage(
527 const DeltaArchiveManifest_InstallOperation& operation) {
528 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
529 !manifest_.has_signatures_offset() ||
530 manifest_.signatures_offset() != operation.data_offset()) {
531 return false;
532 }
533 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
534 manifest_.signatures_size() == operation.data_length());
535 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
536 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
537 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
538 signatures_message_data_.insert(
539 signatures_message_data_.begin(),
540 buffer_.begin(),
541 buffer_.begin() + manifest_.signatures_size());
Darin Petkov437adc42010-10-07 13:12:24 -0700542 // The hash of all data consumed so far should be verified against the signed
543 // hash.
544 signed_hash_context_ = hash_calculator_.GetContext();
545 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
546 signed_hash_context_))
547 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700548 LOG(INFO) << "Extracted signature data of size "
549 << manifest_.signatures_size() << " at "
550 << manifest_.signatures_offset();
551 return true;
552}
553
Darin Petkov437adc42010-10-07 13:12:24 -0700554bool DeltaPerformer::VerifyPayload(
555 const string& public_key_path,
556 const std::string& update_check_response_hash,
557 const uint64_t update_check_response_size) {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700558 string key_path = public_key_path;
559 if (key_path.empty()) {
560 key_path = kUpdatePayloadPublicKeyPath;
561 }
562 LOG(INFO) << "Verifying delta payload. Public key path: " << key_path;
Darin Petkov437adc42010-10-07 13:12:24 -0700563
564 // Verifies the download hash.
565 const string& download_hash_data = hash_calculator_.hash();
566 TEST_AND_RETURN_FALSE(!download_hash_data.empty());
567 TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash);
568
569 // Verifies the download size.
570 TEST_AND_RETURN_FALSE(update_check_response_size ==
571 manifest_metadata_size_ + buffer_offset_);
572
573 // Verifies the signed payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700574 if (!utils::FileExists(key_path.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700575 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700576 return true;
577 }
578 TEST_AND_RETURN_FALSE(!signatures_message_data_.empty());
579 vector<char> signed_hash_data;
580 TEST_AND_RETURN_FALSE(PayloadSigner::VerifySignature(signatures_message_data_,
581 key_path,
582 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700583 OmahaHashCalculator signed_hasher;
Darin Petkov437adc42010-10-07 13:12:24 -0700584 TEST_AND_RETURN_FALSE(signed_hasher.SetContext(signed_hash_context_));
585 TEST_AND_RETURN_FALSE(signed_hasher.Finalize());
586 const vector<char>& hash_data = signed_hasher.raw_hash();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700587 TEST_AND_RETURN_FALSE(!hash_data.empty());
Darin Petkov437adc42010-10-07 13:12:24 -0700588 TEST_AND_RETURN_FALSE(hash_data == signed_hash_data);
589 return true;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700590}
591
Darin Petkov2dd01092010-10-08 15:43:05 -0700592bool DeltaPerformer::VerifyAppliedUpdate(const string& path,
593 const string& kernel_path) {
594 LOG(INFO) << "Verifying applied update.";
595 TEST_AND_RETURN_FALSE(manifest_valid_ &&
596 manifest_.has_new_kernel_info() &&
597 manifest_.has_new_rootfs_info());
598 const string* paths[] = { &kernel_path, &path };
599 const PartitionInfo* infos[] = {
600 &manifest_.new_kernel_info(), &manifest_.new_rootfs_info()
601 };
602 for (size_t i = 0; i < arraysize(paths); ++i) {
603 OmahaHashCalculator hasher;
604 TEST_AND_RETURN_FALSE(hasher.UpdateFile(*paths[i], infos[i]->size()));
605 TEST_AND_RETURN_FALSE(hasher.Finalize());
606 TEST_AND_RETURN_FALSE(hasher.raw_hash().size() == infos[i]->hash().size());
607 TEST_AND_RETURN_FALSE(memcmp(hasher.raw_hash().data(),
608 infos[i]->hash().data(),
609 hasher.raw_hash().size()) == 0);
610 }
611 return true;
612}
613
Darin Petkov698d0412010-10-13 10:59:44 -0700614bool DeltaPerformer::VerifySourcePartitions() {
615 LOG(INFO) << "Verifying source partitions.";
616 CHECK(manifest_valid_);
617 if (manifest_.has_old_kernel_info()) {
618 const PartitionInfo& info = manifest_.old_kernel_info();
619 TEST_AND_RETURN_FALSE(current_kernel_hash_ != NULL &&
620 current_kernel_hash_->size() == info.hash().size() &&
621 memcmp(current_kernel_hash_->data(),
622 info.hash().data(),
623 current_kernel_hash_->size()) == 0);
624 }
625 if (manifest_.has_old_rootfs_info()) {
626 const PartitionInfo& info = manifest_.old_rootfs_info();
627 TEST_AND_RETURN_FALSE(current_rootfs_hash_ != NULL &&
628 current_rootfs_hash_->size() == info.hash().size() &&
629 memcmp(current_rootfs_hash_->data(),
630 info.hash().data(),
631 current_rootfs_hash_->size()) == 0);
632 }
633 return true;
634}
635
Darin Petkov437adc42010-10-07 13:12:24 -0700636void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
637 hash_calculator_.Update(&buffer_[0], count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700638 buffer_.erase(buffer_.begin(), buffer_.begin() + count);
639}
640
Darin Petkov0406e402010-10-06 21:33:11 -0700641bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
642 string update_check_response_hash) {
643 int64_t next_operation = kUpdateStateOperationInvalid;
644 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
645 &next_operation) &&
646 next_operation != kUpdateStateOperationInvalid &&
647 next_operation > 0);
648
649 string interrupted_hash;
650 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
651 &interrupted_hash) &&
652 !interrupted_hash.empty() &&
653 interrupted_hash == update_check_response_hash);
654
Darin Petkov61426142010-10-08 11:04:55 -0700655 int64_t resumed_update_failures;
656 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
657 &resumed_update_failures) ||
658 resumed_update_failures <= kMaxResumedUpdateFailures);
659
Darin Petkov0406e402010-10-06 21:33:11 -0700660 // Sanity check the rest.
661 int64_t next_data_offset = -1;
662 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
663 &next_data_offset) &&
664 next_data_offset >= 0);
665
Darin Petkov437adc42010-10-07 13:12:24 -0700666 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -0700667 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700668 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
669 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -0700670
671 int64_t manifest_metadata_size = 0;
672 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
673 &manifest_metadata_size) &&
674 manifest_metadata_size > 0);
675
676 return true;
677}
678
Darin Petkov9b230572010-10-08 10:20:09 -0700679bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -0700680 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
681 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -0700682 if (!quick) {
683 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
684 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
685 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
686 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
687 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -0700688 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700689 }
Darin Petkov73058b42010-10-06 16:32:19 -0700690 return true;
691}
692
693bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700694 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -0700695 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700696 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -0700697 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -0700698 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700699 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -0700700 hash_calculator_.GetContext()));
701 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
702 buffer_offset_));
703 last_updated_buffer_offset_ = buffer_offset_;
704 }
Darin Petkov73058b42010-10-06 16:32:19 -0700705 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
706 next_operation_num_));
707 return true;
708}
709
Darin Petkov9b230572010-10-08 10:20:09 -0700710bool DeltaPerformer::PrimeUpdateState() {
711 CHECK(manifest_valid_);
712 block_size_ = manifest_.block_size();
713
714 int64_t next_operation = kUpdateStateOperationInvalid;
715 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
716 next_operation == kUpdateStateOperationInvalid ||
717 next_operation <= 0) {
718 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -0700719 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -0700720 return true;
721 }
722 next_operation_num_ = next_operation;
723
724 // Resuming an update -- load the rest of the update state.
725 int64_t next_data_offset = -1;
726 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
727 &next_data_offset) &&
728 next_data_offset >= 0);
729 buffer_offset_ = next_data_offset;
730
731 // The signed hash context may be empty if the interrupted update didn't reach
732 // the signature blob.
733 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
734 &signed_hash_context_);
735
736 string hash_context;
737 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
738 &hash_context) &&
739 hash_calculator_.SetContext(hash_context));
740
741 int64_t manifest_metadata_size = 0;
742 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
743 &manifest_metadata_size) &&
744 manifest_metadata_size > 0);
745 manifest_metadata_size_ = manifest_metadata_size;
746
Darin Petkov61426142010-10-08 11:04:55 -0700747 // Speculatively count the resume as a failure.
748 int64_t resumed_update_failures;
749 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
750 resumed_update_failures++;
751 } else {
752 resumed_update_failures = 1;
753 }
754 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -0700755 return true;
756}
757
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700758} // namespace chromeos_update_engine