blob: 117e8f5d99e717d5c8d0d2f5665e66c78ba664eb [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)) {
Darin Petkov3aefa862010-12-07 14:45:00 -0800177 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
178 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700179 } 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
Darin Petkov9574f7e2011-01-13 10:48:12 -0800197DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
198 const std::vector<char>& payload,
199 DeltaArchiveManifest* manifest,
200 uint64_t* metadata_size) {
201 if (payload.size() < strlen(kDeltaMagic) +
202 kDeltaVersionLength + kDeltaProtobufLengthLength) {
203 // Don't have enough bytes to know the protobuf length.
204 return kMetadataParseInsufficientData;
205 }
206 if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) {
207 LOG(ERROR) << "Bad payload format -- invalid delta magic.";
208 return kMetadataParseError;
209 }
210 uint64_t protobuf_length;
211 COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength,
212 protobuf_length_size_mismatch);
213 memcpy(&protobuf_length,
214 &payload[strlen(kDeltaMagic) + kDeltaVersionLength],
215 kDeltaProtobufLengthLength);
216 protobuf_length = be64toh(protobuf_length); // switch big endian to host
217 if (payload.size() < strlen(kDeltaMagic) + kDeltaVersionLength +
218 kDeltaProtobufLengthLength + protobuf_length) {
219 return kMetadataParseInsufficientData;
220 }
221 // We have the full proto buffer in |payload|. Parse it.
222 const int offset = strlen(kDeltaMagic) + kDeltaVersionLength +
223 kDeltaProtobufLengthLength;
224 if (!manifest->ParseFromArray(&payload[offset], protobuf_length)) {
225 LOG(ERROR) << "Unable to parse manifest in update file.";
226 return kMetadataParseError;
227 }
228 *metadata_size = strlen(kDeltaMagic) + kDeltaVersionLength +
229 kDeltaProtobufLengthLength + protobuf_length;
230 return kMetadataParseSuccess;
231}
232
233
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700234// Wrapper around write. Returns bytes written on success or
235// -errno on error.
236// This function performs as many actions as it can, given the amount of
237// data received thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -0700238ssize_t DeltaPerformer::Write(const void* bytes, size_t count) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700239 const char* c_bytes = reinterpret_cast<const char*>(bytes);
240 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
241
242 if (!manifest_valid_) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800243 MetadataParseResult result = ParsePayloadMetadata(buffer_,
244 &manifest_,
245 &manifest_metadata_size_);
246 if (result == kMetadataParseError) {
Darin Petkov934bb412010-11-18 11:21:35 -0800247 return -EINVAL;
248 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800249 if (result == kMetadataParseInsufficientData) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700250 return count;
251 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700252 // Remove protobuf and header info from buffer_, so buffer_ contains
253 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700254 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700255 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700256 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700257 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700258 manifest_valid_ = true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700259 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700260 if (!PrimeUpdateState()) {
261 LOG(ERROR) << "Unable to prime the update state.";
262 return -EINVAL;
263 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700264 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700265 ssize_t total_operations = manifest_.install_operations_size() +
266 manifest_.kernel_install_operations_size();
267 while (next_operation_num_ < total_operations) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700268 const DeltaArchiveManifest_InstallOperation &op =
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700269 next_operation_num_ < manifest_.install_operations_size() ?
270 manifest_.install_operations(next_operation_num_) :
271 manifest_.kernel_install_operations(
272 next_operation_num_ - manifest_.install_operations_size());
273 if (!CanPerformInstallOperation(op))
274 break;
Darin Petkov45580e42010-10-08 14:02:40 -0700275 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700276 ScopedTerminatorExitUnblocker exit_unblocker =
277 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700278 // Log every thousandth operation, and also the first and last ones
279 if ((next_operation_num_ % 1000 == 0) ||
280 (next_operation_num_ + 1 == total_operations)) {
281 LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/"
282 << total_operations;
283 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700284 bool is_kernel_partition =
285 (next_operation_num_ >= manifest_.install_operations_size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700286 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
287 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700288 if (!PerformReplaceOperation(op, is_kernel_partition)) {
289 LOG(ERROR) << "Failed to perform replace operation "
290 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700291 return -EINVAL;
292 }
293 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700294 if (!PerformMoveOperation(op, is_kernel_partition)) {
295 LOG(ERROR) << "Failed to perform move operation "
296 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700297 return -EINVAL;
298 }
299 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700300 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
301 LOG(ERROR) << "Failed to perform bsdiff operation "
302 << next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700303 return -EINVAL;
304 }
305 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700306 next_operation_num_++;
Darin Petkov73058b42010-10-06 16:32:19 -0700307 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700308 }
309 return count;
310}
311
312bool DeltaPerformer::CanPerformInstallOperation(
313 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
314 operation) {
315 // Move operations don't require any data blob, so they can always
316 // be performed
317 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
318 return true;
319
320 // See if we have the entire data blob in the buffer
321 if (operation.data_offset() < buffer_offset_) {
322 LOG(ERROR) << "we threw away data it seems?";
323 return false;
324 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700325
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700326 return (operation.data_offset() + operation.data_length()) <=
327 (buffer_offset_ + buffer_.size());
328}
329
330bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700331 const DeltaArchiveManifest_InstallOperation& operation,
332 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700333 CHECK(operation.type() == \
334 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
335 operation.type() == \
336 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
337
338 // Since we delete data off the beginning of the buffer as we use it,
339 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700340 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
341 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700342
Darin Petkov437adc42010-10-07 13:12:24 -0700343 // Extract the signature message if it's in this operation.
344 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700345
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700346 DirectExtentWriter direct_writer;
347 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
348 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700349
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700350 // Since bzip decompression is optional, we have a variable writer that will
351 // point to one of the ExtentWriter objects above.
352 ExtentWriter* writer = NULL;
353 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
354 writer = &zero_pad_writer;
355 } else if (operation.type() ==
356 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
357 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
358 writer = bzip_writer.get();
359 } else {
360 NOTREACHED();
361 }
362
363 // Create a vector of extents to pass to the ExtentWriter.
364 vector<Extent> extents;
365 for (int i = 0; i < operation.dst_extents_size(); i++) {
366 extents.push_back(operation.dst_extents(i));
367 }
368
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700369 int fd = is_kernel_partition ? kernel_fd_ : fd_;
370
371 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700372 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
373 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700374
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700375 // Update buffer
376 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700377 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700378 return true;
379}
380
381bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700382 const DeltaArchiveManifest_InstallOperation& operation,
383 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700384 // Calculate buffer size. Note, this function doesn't do a sliding
385 // window to copy in case the source and destination blocks overlap.
386 // If we wanted to do a sliding window, we could program the server
387 // to generate deltas that effectively did a sliding window.
388
389 uint64_t blocks_to_read = 0;
390 for (int i = 0; i < operation.src_extents_size(); i++)
391 blocks_to_read += operation.src_extents(i).num_blocks();
392
393 uint64_t blocks_to_write = 0;
394 for (int i = 0; i < operation.dst_extents_size(); i++)
395 blocks_to_write += operation.dst_extents(i).num_blocks();
396
397 DCHECK_EQ(blocks_to_write, blocks_to_read);
398 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700399
400 int fd = is_kernel_partition ? kernel_fd_ : fd_;
401
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700402 // Read in bytes.
403 ssize_t bytes_read = 0;
404 for (int i = 0; i < operation.src_extents_size(); i++) {
405 ssize_t bytes_read_this_iteration = 0;
406 const Extent& extent = operation.src_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700407 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700408 &buf[bytes_read],
409 extent.num_blocks() * block_size_,
410 extent.start_block() * block_size_,
411 &bytes_read_this_iteration));
412 TEST_AND_RETURN_FALSE(
413 bytes_read_this_iteration ==
414 static_cast<ssize_t>(extent.num_blocks() * block_size_));
415 bytes_read += bytes_read_this_iteration;
416 }
417
Darin Petkov45580e42010-10-08 14:02:40 -0700418 // If this is a non-idempotent operation, request a delayed exit and clear the
419 // update state in case the operation gets interrupted. Do this as late as
420 // possible.
421 if (!IsIdempotentOperation(operation)) {
422 Terminator::set_exit_blocked(true);
423 ResetUpdateProgress(prefs_, true);
424 }
425
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700426 // Write bytes out.
427 ssize_t bytes_written = 0;
428 for (int i = 0; i < operation.dst_extents_size(); i++) {
429 const Extent& extent = operation.dst_extents(i);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700430 TEST_AND_RETURN_FALSE(utils::PWriteAll(fd,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700431 &buf[bytes_written],
432 extent.num_blocks() * block_size_,
433 extent.start_block() * block_size_));
434 bytes_written += extent.num_blocks() * block_size_;
435 }
436 DCHECK_EQ(bytes_written, bytes_read);
437 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
438 return true;
439}
440
441bool DeltaPerformer::ExtentsToBsdiffPositionsString(
442 const RepeatedPtrField<Extent>& extents,
443 uint64_t block_size,
444 uint64_t full_length,
445 string* positions_string) {
446 string ret;
447 uint64_t length = 0;
448 for (int i = 0; i < extents.size(); i++) {
449 Extent extent = extents.Get(i);
450 int64_t start = extent.start_block();
451 uint64_t this_length = min(full_length - length,
452 extent.num_blocks() * block_size);
453 if (start == static_cast<int64_t>(kSparseHole))
454 start = -1;
455 else
456 start *= block_size;
457 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
458 length += this_length;
459 }
460 TEST_AND_RETURN_FALSE(length == full_length);
461 if (!ret.empty())
462 ret.resize(ret.size() - 1); // Strip trailing comma off
463 *positions_string = ret;
464 return true;
465}
466
467bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700468 const DeltaArchiveManifest_InstallOperation& operation,
469 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700470 // Since we delete data off the beginning of the buffer as we use it,
471 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700472 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
473 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700474
475 string input_positions;
476 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
477 block_size_,
478 operation.src_length(),
479 &input_positions));
480 string output_positions;
481 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
482 block_size_,
483 operation.dst_length(),
484 &output_positions));
485
486 string temp_filename;
487 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
488 &temp_filename,
489 NULL));
490 ScopedPathUnlinker path_unlinker(temp_filename);
491 {
492 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
493 ScopedFdCloser fd_closer(&fd);
494 TEST_AND_RETURN_FALSE(
495 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
496 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700497
498 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700499 const string& path = StringPrintf("/dev/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700500
Darin Petkov45580e42010-10-08 14:02:40 -0700501 // If this is a non-idempotent operation, request a delayed exit and clear the
502 // update state in case the operation gets interrupted. Do this as late as
503 // possible.
504 if (!IsIdempotentOperation(operation)) {
505 Terminator::set_exit_blocked(true);
506 ResetUpdateProgress(prefs_, true);
507 }
508
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700509 vector<string> cmd;
510 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700511 cmd.push_back(path);
512 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700513 cmd.push_back(temp_filename);
514 cmd.push_back(input_positions);
515 cmd.push_back(output_positions);
516 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700517 TEST_AND_RETURN_FALSE(
518 Subprocess::SynchronousExecFlags(cmd,
519 &return_code,
520 G_SPAWN_LEAVE_DESCRIPTORS_OPEN));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700521 TEST_AND_RETURN_FALSE(return_code == 0);
522
523 if (operation.dst_length() % block_size_) {
524 // Zero out rest of final block.
525 // TODO(adlr): build this into bspatch; it's more efficient that way.
526 const Extent& last_extent =
527 operation.dst_extents(operation.dst_extents_size() - 1);
528 const uint64_t end_byte =
529 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
530 const uint64_t begin_byte =
531 end_byte - (block_size_ - operation.dst_length() % block_size_);
532 vector<char> zeros(end_byte - begin_byte);
533 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700534 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700535 }
536
537 // Update buffer.
538 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700539 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700540 return true;
541}
542
Darin Petkovd7061ab2010-10-06 14:37:09 -0700543bool DeltaPerformer::ExtractSignatureMessage(
544 const DeltaArchiveManifest_InstallOperation& operation) {
545 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
546 !manifest_.has_signatures_offset() ||
547 manifest_.signatures_offset() != operation.data_offset()) {
548 return false;
549 }
550 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
551 manifest_.signatures_size() == operation.data_length());
552 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
553 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
554 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
555 signatures_message_data_.insert(
556 signatures_message_data_.begin(),
557 buffer_.begin(),
558 buffer_.begin() + manifest_.signatures_size());
Darin Petkov437adc42010-10-07 13:12:24 -0700559 // The hash of all data consumed so far should be verified against the signed
560 // hash.
561 signed_hash_context_ = hash_calculator_.GetContext();
562 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
563 signed_hash_context_))
564 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700565 LOG(INFO) << "Extracted signature data of size "
566 << manifest_.signatures_size() << " at "
567 << manifest_.signatures_offset();
568 return true;
569}
570
Darin Petkov437adc42010-10-07 13:12:24 -0700571bool DeltaPerformer::VerifyPayload(
572 const string& public_key_path,
573 const std::string& update_check_response_hash,
574 const uint64_t update_check_response_size) {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700575 string key_path = public_key_path;
576 if (key_path.empty()) {
577 key_path = kUpdatePayloadPublicKeyPath;
578 }
579 LOG(INFO) << "Verifying delta payload. Public key path: " << key_path;
Darin Petkov437adc42010-10-07 13:12:24 -0700580
581 // Verifies the download hash.
582 const string& download_hash_data = hash_calculator_.hash();
583 TEST_AND_RETURN_FALSE(!download_hash_data.empty());
584 TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash);
585
586 // Verifies the download size.
587 TEST_AND_RETURN_FALSE(update_check_response_size ==
588 manifest_metadata_size_ + buffer_offset_);
589
590 // Verifies the signed payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700591 if (!utils::FileExists(key_path.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700592 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700593 return true;
594 }
595 TEST_AND_RETURN_FALSE(!signatures_message_data_.empty());
596 vector<char> signed_hash_data;
597 TEST_AND_RETURN_FALSE(PayloadSigner::VerifySignature(signatures_message_data_,
598 key_path,
599 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700600 OmahaHashCalculator signed_hasher;
Darin Petkov437adc42010-10-07 13:12:24 -0700601 TEST_AND_RETURN_FALSE(signed_hasher.SetContext(signed_hash_context_));
602 TEST_AND_RETURN_FALSE(signed_hasher.Finalize());
603 const vector<char>& hash_data = signed_hasher.raw_hash();
Darin Petkovd7061ab2010-10-06 14:37:09 -0700604 TEST_AND_RETURN_FALSE(!hash_data.empty());
Darin Petkov437adc42010-10-07 13:12:24 -0700605 TEST_AND_RETURN_FALSE(hash_data == signed_hash_data);
606 return true;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700607}
608
Darin Petkov3aefa862010-12-07 14:45:00 -0800609bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size,
610 vector<char>* kernel_hash,
611 uint64_t* rootfs_size,
612 vector<char>* rootfs_hash) {
Darin Petkov2dd01092010-10-08 15:43:05 -0700613 TEST_AND_RETURN_FALSE(manifest_valid_ &&
614 manifest_.has_new_kernel_info() &&
615 manifest_.has_new_rootfs_info());
Darin Petkov3aefa862010-12-07 14:45:00 -0800616 *kernel_size = manifest_.new_kernel_info().size();
617 *rootfs_size = manifest_.new_rootfs_info().size();
618 vector<char> new_kernel_hash(manifest_.new_kernel_info().hash().begin(),
619 manifest_.new_kernel_info().hash().end());
620 vector<char> new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(),
621 manifest_.new_rootfs_info().hash().end());
622 kernel_hash->swap(new_kernel_hash);
623 rootfs_hash->swap(new_rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -0700624 return true;
625}
626
Darin Petkov698d0412010-10-13 10:59:44 -0700627bool DeltaPerformer::VerifySourcePartitions() {
628 LOG(INFO) << "Verifying source partitions.";
629 CHECK(manifest_valid_);
630 if (manifest_.has_old_kernel_info()) {
631 const PartitionInfo& info = manifest_.old_kernel_info();
Darin Petkov3aefa862010-12-07 14:45:00 -0800632 TEST_AND_RETURN_FALSE(!current_kernel_hash_.empty() &&
633 current_kernel_hash_.size() == info.hash().size() &&
634 memcmp(current_kernel_hash_.data(),
Darin Petkov698d0412010-10-13 10:59:44 -0700635 info.hash().data(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800636 current_kernel_hash_.size()) == 0);
Darin Petkov698d0412010-10-13 10:59:44 -0700637 }
638 if (manifest_.has_old_rootfs_info()) {
639 const PartitionInfo& info = manifest_.old_rootfs_info();
Darin Petkov3aefa862010-12-07 14:45:00 -0800640 TEST_AND_RETURN_FALSE(!current_rootfs_hash_.empty() &&
641 current_rootfs_hash_.size() == info.hash().size() &&
642 memcmp(current_rootfs_hash_.data(),
Darin Petkov698d0412010-10-13 10:59:44 -0700643 info.hash().data(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800644 current_rootfs_hash_.size()) == 0);
Darin Petkov698d0412010-10-13 10:59:44 -0700645 }
646 return true;
647}
648
Darin Petkov437adc42010-10-07 13:12:24 -0700649void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
650 hash_calculator_.Update(&buffer_[0], count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700651 buffer_.erase(buffer_.begin(), buffer_.begin() + count);
652}
653
Darin Petkov0406e402010-10-06 21:33:11 -0700654bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
655 string update_check_response_hash) {
656 int64_t next_operation = kUpdateStateOperationInvalid;
657 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
658 &next_operation) &&
659 next_operation != kUpdateStateOperationInvalid &&
660 next_operation > 0);
661
662 string interrupted_hash;
663 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
664 &interrupted_hash) &&
665 !interrupted_hash.empty() &&
666 interrupted_hash == update_check_response_hash);
667
Darin Petkov61426142010-10-08 11:04:55 -0700668 int64_t resumed_update_failures;
669 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
670 &resumed_update_failures) ||
671 resumed_update_failures <= kMaxResumedUpdateFailures);
672
Darin Petkov0406e402010-10-06 21:33:11 -0700673 // Sanity check the rest.
674 int64_t next_data_offset = -1;
675 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
676 &next_data_offset) &&
677 next_data_offset >= 0);
678
Darin Petkov437adc42010-10-07 13:12:24 -0700679 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -0700680 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700681 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
682 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -0700683
684 int64_t manifest_metadata_size = 0;
685 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
686 &manifest_metadata_size) &&
687 manifest_metadata_size > 0);
688
689 return true;
690}
691
Darin Petkov9b230572010-10-08 10:20:09 -0700692bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -0700693 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
694 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -0700695 if (!quick) {
696 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
697 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
698 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
699 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
700 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -0700701 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700702 }
Darin Petkov73058b42010-10-06 16:32:19 -0700703 return true;
704}
705
706bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700707 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -0700708 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -0700709 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -0700710 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -0700711 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -0700712 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -0700713 hash_calculator_.GetContext()));
714 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
715 buffer_offset_));
716 last_updated_buffer_offset_ = buffer_offset_;
717 }
Darin Petkov73058b42010-10-06 16:32:19 -0700718 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
719 next_operation_num_));
720 return true;
721}
722
Darin Petkov9b230572010-10-08 10:20:09 -0700723bool DeltaPerformer::PrimeUpdateState() {
724 CHECK(manifest_valid_);
725 block_size_ = manifest_.block_size();
726
727 int64_t next_operation = kUpdateStateOperationInvalid;
728 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
729 next_operation == kUpdateStateOperationInvalid ||
730 next_operation <= 0) {
731 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -0700732 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -0700733 return true;
734 }
735 next_operation_num_ = next_operation;
736
737 // Resuming an update -- load the rest of the update state.
738 int64_t next_data_offset = -1;
739 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
740 &next_data_offset) &&
741 next_data_offset >= 0);
742 buffer_offset_ = next_data_offset;
743
744 // The signed hash context may be empty if the interrupted update didn't reach
745 // the signature blob.
746 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
747 &signed_hash_context_);
748
749 string hash_context;
750 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
751 &hash_context) &&
752 hash_calculator_.SetContext(hash_context));
753
754 int64_t manifest_metadata_size = 0;
755 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
756 &manifest_metadata_size) &&
757 manifest_metadata_size > 0);
758 manifest_metadata_size_ = manifest_metadata_size;
759
Darin Petkov61426142010-10-08 11:04:55 -0700760 // Speculatively count the resume as a failure.
761 int64_t resumed_update_failures;
762 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
763 resumed_update_failures++;
764 } else {
765 resumed_update_failures = 1;
766 }
767 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -0700768 return true;
769}
770
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700771} // namespace chromeos_update_engine