blob: 7ee9d4fb20976e0db6a23d178a23e1da6ccf20c8 [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 }
122 // TODO(adlr): detect other types of idempotent operations. For example,
123 // a MOVE may move a block onto itself.
124
125 // When in doubt, it's safe to declare an op non-idempotent.
126 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
168// Wrapper around write. Returns bytes written on success or
169// -errno on error.
170// This function performs as many actions as it can, given the amount of
171// data received thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -0700172ssize_t DeltaPerformer::Write(const void* bytes, size_t count) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700173 const char* c_bytes = reinterpret_cast<const char*>(bytes);
174 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
175
176 if (!manifest_valid_) {
177 // See if we have enough bytes for the manifest yet
178 if (buffer_.size() < strlen(kDeltaMagic) +
179 kDeltaVersionLength + kDeltaProtobufLengthLength) {
180 // Don't have enough bytes to even know the protobuf length
181 return count;
182 }
183 uint64_t protobuf_length;
184 COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength,
185 protobuf_length_size_mismatch);
186 memcpy(&protobuf_length,
187 &buffer_[strlen(kDeltaMagic) + kDeltaVersionLength],
188 kDeltaProtobufLengthLength);
189 protobuf_length = be64toh(protobuf_length); // switch big endian to host
190 if (buffer_.size() < strlen(kDeltaMagic) + kDeltaVersionLength +
191 kDeltaProtobufLengthLength + protobuf_length) {
192 return count;
193 }
194 // We have the full proto buffer in buffer_. Parse it.
195 const int offset = strlen(kDeltaMagic) + kDeltaVersionLength +
196 kDeltaProtobufLengthLength;
197 if (!manifest_.ParseFromArray(&buffer_[offset], protobuf_length)) {
198 LOG(ERROR) << "Unable to parse manifest in update file.";
199 return -EINVAL;
200 }
201 // Remove protobuf and header info from buffer_, so buffer_ contains
202 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700203 manifest_metadata_size_ = strlen(kDeltaMagic) + kDeltaVersionLength +
Darin Petkov73058b42010-10-06 16:32:19 -0700204 kDeltaProtobufLengthLength + protobuf_length;
Darin Petkov437adc42010-10-07 13:12:24 -0700205 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700206 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700207 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700208 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700209 manifest_valid_ = true;
Darin Petkov9b230572010-10-08 10:20:09 -0700210 if (!PrimeUpdateState()) {
211 LOG(ERROR) << "Unable to prime the update state.";
212 return -EINVAL;
213 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700214 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700215 ssize_t total_operations = manifest_.install_operations_size() +
216 manifest_.kernel_install_operations_size();
217 while (next_operation_num_ < total_operations) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700218 const DeltaArchiveManifest_InstallOperation &op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700219 next_operation_num_ < manifest_.install_operations_size() ?
220 manifest_.install_operations(next_operation_num_) :
221 manifest_.kernel_install_operations(
222 next_operation_num_ - manifest_.install_operations_size());
223 if (!CanPerformInstallOperation(op))
224 break;
Darin Petkov45580e42010-10-08 14:02:40 -0700225 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700226 ScopedTerminatorExitUnblocker exit_unblocker =
227 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700228 // Log every thousandth operation, and also the first and last ones
229 if ((next_operation_num_ % 1000 == 0) ||
230 (next_operation_num_ + 1 == total_operations)) {
231 LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/"
232 << total_operations;
233 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700234 bool is_kernel_partition =
235 (next_operation_num_ >= manifest_.install_operations_size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700236 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
237 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700238 if (!PerformReplaceOperation(op, is_kernel_partition)) {
239 LOG(ERROR) << "Failed to perform replace operation "
240 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700241 return -EINVAL;
242 }
243 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700244 if (!PerformMoveOperation(op, is_kernel_partition)) {
245 LOG(ERROR) << "Failed to perform move operation "
246 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700247 return -EINVAL;
248 }
249 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700250 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
251 LOG(ERROR) << "Failed to perform bsdiff operation "
252 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700253 return -EINVAL;
254 }
255 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700256 next_operation_num_++;
Darin Petkov73058b42010-10-06 16:32:19 -0700257 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700258 }
259 return count;
260}
261
262bool DeltaPerformer::CanPerformInstallOperation(
263 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
264 operation) {
265 // Move operations don't require any data blob, so they can always
266 // be performed
267 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
268 return true;
269
270 // See if we have the entire data blob in the buffer
271 if (operation.data_offset() < buffer_offset_) {
272 LOG(ERROR) << "we threw away data it seems?";
273 return false;
274 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700275
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700276 return (operation.data_offset() + operation.data_length()) <=
277 (buffer_offset_ + buffer_.size());
278}
279
280bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700281 const DeltaArchiveManifest_InstallOperation& operation,
282 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700283 CHECK(operation.type() == \
284 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
285 operation.type() == \
286 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
287
288 // Since we delete data off the beginning of the buffer as we use it,
289 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700290 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
291 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700292
Darin Petkov437adc42010-10-07 13:12:24 -0700293 // Extract the signature message if it's in this operation.
294 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700295
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700296 DirectExtentWriter direct_writer;
297 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
298 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700299
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700300 // Since bzip decompression is optional, we have a variable writer that will
301 // point to one of the ExtentWriter objects above.
302 ExtentWriter* writer = NULL;
303 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
304 writer = &zero_pad_writer;
305 } else if (operation.type() ==
306 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
307 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
308 writer = bzip_writer.get();
309 } else {
310 NOTREACHED();
311 }
312
313 // Create a vector of extents to pass to the ExtentWriter.
314 vector<Extent> extents;
315 for (int i = 0; i < operation.dst_extents_size(); i++) {
316 extents.push_back(operation.dst_extents(i));
317 }
318
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700319 int fd = is_kernel_partition ? kernel_fd_ : fd_;
320
321 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700322 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
323 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700324
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700325 // Update buffer
326 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700327 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700328 return true;
329}
330
331bool DeltaPerformer::PerformMoveOperation(
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 // Calculate buffer size. Note, this function doesn't do a sliding
335 // window to copy in case the source and destination blocks overlap.
336 // If we wanted to do a sliding window, we could program the server
337 // to generate deltas that effectively did a sliding window.
338
339 uint64_t blocks_to_read = 0;
340 for (int i = 0; i < operation.src_extents_size(); i++)
341 blocks_to_read += operation.src_extents(i).num_blocks();
342
343 uint64_t blocks_to_write = 0;
344 for (int i = 0; i < operation.dst_extents_size(); i++)
345 blocks_to_write += operation.dst_extents(i).num_blocks();
346
347 DCHECK_EQ(blocks_to_write, blocks_to_read);
348 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700349
350 int fd = is_kernel_partition ? kernel_fd_ : fd_;
351
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700352 // Read in bytes.
353 ssize_t bytes_read = 0;
354 for (int i = 0; i < operation.src_extents_size(); i++) {
355 ssize_t bytes_read_this_iteration = 0;
356 const Extent& extent = operation.src_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700357 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700358 &buf[bytes_read],
359 extent.num_blocks() * block_size_,
360 extent.start_block() * block_size_,
361 &bytes_read_this_iteration));
362 TEST_AND_RETURN_FALSE(
363 bytes_read_this_iteration ==
364 static_cast<ssize_t>(extent.num_blocks() * block_size_));
365 bytes_read += bytes_read_this_iteration;
366 }
367
Darin Petkov45580e42010-10-08 14:02:40 -0700368 // If this is a non-idempotent operation, request a delayed exit and clear the
369 // update state in case the operation gets interrupted. Do this as late as
370 // possible.
371 if (!IsIdempotentOperation(operation)) {
372 Terminator::set_exit_blocked(true);
373 ResetUpdateProgress(prefs_, true);
374 }
375
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700376 // Write bytes out.
377 ssize_t bytes_written = 0;
378 for (int i = 0; i < operation.dst_extents_size(); i++) {
379 const Extent& extent = operation.dst_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700380 TEST_AND_RETURN_FALSE(utils::PWriteAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700381 &buf[bytes_written],
382 extent.num_blocks() * block_size_,
383 extent.start_block() * block_size_));
384 bytes_written += extent.num_blocks() * block_size_;
385 }
386 DCHECK_EQ(bytes_written, bytes_read);
387 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
388 return true;
389}
390
391bool DeltaPerformer::ExtentsToBsdiffPositionsString(
392 const RepeatedPtrField<Extent>& extents,
393 uint64_t block_size,
394 uint64_t full_length,
395 string* positions_string) {
396 string ret;
397 uint64_t length = 0;
398 for (int i = 0; i < extents.size(); i++) {
399 Extent extent = extents.Get(i);
400 int64_t start = extent.start_block();
401 uint64_t this_length = min(full_length - length,
402 extent.num_blocks() * block_size);
403 if (start == static_cast<int64_t>(kSparseHole))
404 start = -1;
405 else
406 start *= block_size;
407 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
408 length += this_length;
409 }
410 TEST_AND_RETURN_FALSE(length == full_length);
411 if (!ret.empty())
412 ret.resize(ret.size() - 1); // Strip trailing comma off
413 *positions_string = ret;
414 return true;
415}
416
417bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700418 const DeltaArchiveManifest_InstallOperation& operation,
419 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700420 // Since we delete data off the beginning of the buffer as we use it,
421 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700422 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
423 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700424
425 string input_positions;
426 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
427 block_size_,
428 operation.src_length(),
429 &input_positions));
430 string output_positions;
431 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
432 block_size_,
433 operation.dst_length(),
434 &output_positions));
435
436 string temp_filename;
437 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
438 &temp_filename,
439 NULL));
440 ScopedPathUnlinker path_unlinker(temp_filename);
441 {
442 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
443 ScopedFdCloser fd_closer(&fd);
444 TEST_AND_RETURN_FALSE(
445 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
446 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700447
448 int fd = is_kernel_partition ? kernel_fd_ : fd_;
449 const string& path = is_kernel_partition ? kernel_path_ : path_;
450
Darin Petkov45580e42010-10-08 14:02:40 -0700451 // If this is a non-idempotent operation, request a delayed exit and clear the
452 // update state in case the operation gets interrupted. Do this as late as
453 // possible.
454 if (!IsIdempotentOperation(operation)) {
455 Terminator::set_exit_blocked(true);
456 ResetUpdateProgress(prefs_, true);
457 }
458
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700459 vector<string> cmd;
460 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700461 cmd.push_back(path);
462 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700463 cmd.push_back(temp_filename);
464 cmd.push_back(input_positions);
465 cmd.push_back(output_positions);
466 int return_code = 0;
467 TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code));
468 TEST_AND_RETURN_FALSE(return_code == 0);
469
470 if (operation.dst_length() % block_size_) {
471 // Zero out rest of final block.
472 // TODO(adlr): build this into bspatch; it's more efficient that way.
473 const Extent& last_extent =
474 operation.dst_extents(operation.dst_extents_size() - 1);
475 const uint64_t end_byte =
476 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
477 const uint64_t begin_byte =
478 end_byte - (block_size_ - operation.dst_length() % block_size_);
479 vector<char> zeros(end_byte - begin_byte);
480 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700481 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700482 }
483
484 // Update buffer.
485 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700486 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700487 return true;
488}
489
Darin Petkovd7061ab2010-10-06 14:37:09 -0700490bool DeltaPerformer::ExtractSignatureMessage(
491 const DeltaArchiveManifest_InstallOperation& operation) {
492 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
493 !manifest_.has_signatures_offset() ||
494 manifest_.signatures_offset() != operation.data_offset()) {
495 return false;
496 }
497 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
498 manifest_.signatures_size() == operation.data_length());
499 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
500 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
501 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
502 signatures_message_data_.insert(
503 signatures_message_data_.begin(),
504 buffer_.begin(),
505 buffer_.begin() + manifest_.signatures_size());
Darin Petkov437adc42010-10-07 13:12:24 -0700506 // The hash of all data consumed so far should be verified against the signed
507 // hash.
508 signed_hash_context_ = hash_calculator_.GetContext();
509 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
510 signed_hash_context_))
511 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700512 LOG(INFO) << "Extracted signature data of size "
513 << manifest_.signatures_size() << " at "
514 << manifest_.signatures_offset();
515 return true;
516}
517
Darin Petkov437adc42010-10-07 13:12:24 -0700518bool DeltaPerformer::VerifyPayload(
519 const string& public_key_path,
520 const std::string& update_check_response_hash,
521 const uint64_t update_check_response_size) {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700522 string key_path = public_key_path;
523 if (key_path.empty()) {
524 key_path = kUpdatePayloadPublicKeyPath;
525 }
526 LOG(INFO) << "Verifying delta payload. Public key path: " << key_path;
Darin Petkov437adc42010-10-07 13:12:24 -0700527
528 // Verifies the download hash.
529 const string& download_hash_data = hash_calculator_.hash();
530 TEST_AND_RETURN_FALSE(!download_hash_data.empty());
531 TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash);
532
533 // Verifies the download size.
534 TEST_AND_RETURN_FALSE(update_check_response_size ==
535 manifest_metadata_size_ + buffer_offset_);
536
537 // Verifies the signed payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700538 if (!utils::FileExists(key_path.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700539 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700540 return true;
541 }
542 TEST_AND_RETURN_FALSE(!signatures_message_data_.empty());
543 vector<char> signed_hash_data;
544 TEST_AND_RETURN_FALSE(PayloadSigner::VerifySignature(signatures_message_data_,
545 key_path,
546 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700547 OmahaHashCalculator signed_hasher;
Darin Petkov437adc42010-10-07 13:12:24 -0700548 TEST_AND_RETURN_FALSE(signed_hasher.SetContext(signed_hash_context_));
549 TEST_AND_RETURN_FALSE(signed_hasher.Finalize());
550 const vector<char>& hash_data = signed_hasher.raw_hash();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700551 TEST_AND_RETURN_FALSE(!hash_data.empty());
Darin Petkov437adc42010-10-07 13:12:24 -0700552 TEST_AND_RETURN_FALSE(hash_data == signed_hash_data);
553 return true;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700554}
555
Darin Petkov2dd01092010-10-08 15:43:05 -0700556bool DeltaPerformer::VerifyAppliedUpdate(const string& path,
557 const string& kernel_path) {
558 LOG(INFO) << "Verifying applied update.";
559 TEST_AND_RETURN_FALSE(manifest_valid_ &&
560 manifest_.has_new_kernel_info() &&
561 manifest_.has_new_rootfs_info());
562 const string* paths[] = { &kernel_path, &path };
563 const PartitionInfo* infos[] = {
564 &manifest_.new_kernel_info(), &manifest_.new_rootfs_info()
565 };
566 for (size_t i = 0; i < arraysize(paths); ++i) {
567 OmahaHashCalculator hasher;
568 TEST_AND_RETURN_FALSE(hasher.UpdateFile(*paths[i], infos[i]->size()));
569 TEST_AND_RETURN_FALSE(hasher.Finalize());
570 TEST_AND_RETURN_FALSE(hasher.raw_hash().size() == infos[i]->hash().size());
571 TEST_AND_RETURN_FALSE(memcmp(hasher.raw_hash().data(),
572 infos[i]->hash().data(),
573 hasher.raw_hash().size()) == 0);
574 }
575 return true;
576}
577
Darin Petkov437adc42010-10-07 13:12:24 -0700578void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
579 hash_calculator_.Update(&buffer_[0], count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700580 buffer_.erase(buffer_.begin(), buffer_.begin() + count);
581}
582
Darin Petkov0406e402010-10-06 21:33:11 -0700583bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
584 string update_check_response_hash) {
585 int64_t next_operation = kUpdateStateOperationInvalid;
586 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
587 &next_operation) &&
588 next_operation != kUpdateStateOperationInvalid &&
589 next_operation > 0);
590
591 string interrupted_hash;
592 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
593 &interrupted_hash) &&
594 !interrupted_hash.empty() &&
595 interrupted_hash == update_check_response_hash);
596
Darin Petkov61426142010-10-08 11:04:55 -0700597 int64_t resumed_update_failures;
598 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
599 &resumed_update_failures) ||
600 resumed_update_failures <= kMaxResumedUpdateFailures);
601
Darin Petkov0406e402010-10-06 21:33:11 -0700602 // Sanity check the rest.
603 int64_t next_data_offset = -1;
604 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
605 &next_data_offset) &&
606 next_data_offset >= 0);
607
Darin Petkov437adc42010-10-07 13:12:24 -0700608 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -0700609 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700610 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
611 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -0700612
613 int64_t manifest_metadata_size = 0;
614 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
615 &manifest_metadata_size) &&
616 manifest_metadata_size > 0);
617
618 return true;
619}
620
Darin Petkov9b230572010-10-08 10:20:09 -0700621bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -0700622 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
623 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -0700624 if (!quick) {
625 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
626 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
627 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
628 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
629 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -0700630 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700631 }
Darin Petkov73058b42010-10-06 16:32:19 -0700632 return true;
633}
634
635bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700636 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -0700637 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700638 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -0700639 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -0700640 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700641 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -0700642 hash_calculator_.GetContext()));
643 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
644 buffer_offset_));
645 last_updated_buffer_offset_ = buffer_offset_;
646 }
Darin Petkov73058b42010-10-06 16:32:19 -0700647 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
648 next_operation_num_));
649 return true;
650}
651
Darin Petkov9b230572010-10-08 10:20:09 -0700652bool DeltaPerformer::PrimeUpdateState() {
653 CHECK(manifest_valid_);
654 block_size_ = manifest_.block_size();
655
656 int64_t next_operation = kUpdateStateOperationInvalid;
657 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
658 next_operation == kUpdateStateOperationInvalid ||
659 next_operation <= 0) {
660 // Initiating a new update, no more state needs to be initialized.
661 return true;
662 }
663 next_operation_num_ = next_operation;
664
665 // Resuming an update -- load the rest of the update state.
666 int64_t next_data_offset = -1;
667 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
668 &next_data_offset) &&
669 next_data_offset >= 0);
670 buffer_offset_ = next_data_offset;
671
672 // The signed hash context may be empty if the interrupted update didn't reach
673 // the signature blob.
674 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
675 &signed_hash_context_);
676
677 string hash_context;
678 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
679 &hash_context) &&
680 hash_calculator_.SetContext(hash_context));
681
682 int64_t manifest_metadata_size = 0;
683 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
684 &manifest_metadata_size) &&
685 manifest_metadata_size > 0);
686 manifest_metadata_size_ = manifest_metadata_size;
687
Darin Petkov61426142010-10-08 11:04:55 -0700688 // Speculatively count the resume as a failure.
689 int64_t resumed_update_failures;
690 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
691 resumed_update_failures++;
692 } else {
693 resumed_update_failures = 1;
694 }
695 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -0700696 return true;
697}
698
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700699} // namespace chromeos_update_engine