blob: 69ffbc1ac3dc8b64e06e745bc4960169c6a0e0ee [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07002// 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
Chris Masoned903c3b2011-05-12 15:35:46 -070015#include <base/memory/scoped_ptr.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -070016#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040017#include <base/stringprintf.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include <google/protobuf/repeated_field.h>
19
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070020#include "update_engine/bzip_extent_writer.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070021#include "update_engine/constants.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes353777c2010-10-08 10:34:30 -070023#include "update_engine/extent_ranges.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070024#include "update_engine/extent_writer.h"
25#include "update_engine/graph_types.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070026#include "update_engine/payload_signer.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080027#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070028#include "update_engine/prefs_interface.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070029#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070030#include "update_engine/terminator.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070031#include "update_engine/update_attempter.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070032
33using std::min;
34using std::string;
35using std::vector;
36using google::protobuf::RepeatedPtrField;
37
38namespace chromeos_update_engine {
39
Jay Srinivasanf4318702012-09-24 11:56:24 -070040const uint64_t DeltaPerformer::kDeltaVersionSize = 8;
41const uint64_t DeltaPerformer::kDeltaManifestSizeSize = 8;
Don Garrett4d039442013-10-28 18:40:06 -070042const uint64_t DeltaPerformer::kSupportedMajorPayloadVersion = 1;
43
Darin Petkovabc7bc02011-02-23 14:39:43 -080044const char DeltaPerformer::kUpdatePayloadPublicKeyPath[] =
45 "/usr/share/update_engine/update-payload-key.pub.pem";
Gilad Arnold8a86fa52013-01-15 12:35:05 -080046const unsigned DeltaPerformer::kProgressLogMaxChunks = 10;
47const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30;
48const unsigned DeltaPerformer::kProgressDownloadWeight = 50;
49const unsigned DeltaPerformer::kProgressOperationsWeight = 50;
Darin Petkovabc7bc02011-02-23 14:39:43 -080050
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070051namespace {
Darin Petkov73058b42010-10-06 16:32:19 -070052const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070053const int kMaxResumedUpdateFailures = 10;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070054// Opens path for read/write, put the fd into *fd. On success returns true
55// and sets *err to 0. On failure, returns false and sets *err to errno.
56bool OpenFile(const char* path, int* fd, int* err) {
57 if (*fd != -1) {
58 LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")";
59 *err = EINVAL;
60 return false;
61 }
62 *fd = open(path, O_RDWR, 000);
63 if (*fd < 0) {
64 *err = errno;
65 PLOG(ERROR) << "Unable to open file " << path;
66 return false;
67 }
68 *err = 0;
69 return true;
70}
71
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070072} // namespace {}
73
Gilad Arnold8a86fa52013-01-15 12:35:05 -080074
75// Computes the ratio of |part| and |total|, scaled to |norm|, using integer
76// arithmetic.
77static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) {
78 return part * norm / total;
79}
80
81void DeltaPerformer::LogProgress(const char* message_prefix) {
82 // Format operations total count and percentage.
83 string total_operations_str("?");
84 string completed_percentage_str("");
85 if (num_total_operations_) {
86 total_operations_str = StringPrintf("%zu", num_total_operations_);
87 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
88 completed_percentage_str =
89 StringPrintf(" (%llu%%)",
90 IntRatio(next_operation_num_, num_total_operations_,
91 100));
92 }
93
94 // Format download total count and percentage.
95 size_t payload_size = install_plan_->payload_size;
96 string payload_size_str("?");
97 string downloaded_percentage_str("");
98 if (payload_size) {
99 payload_size_str = StringPrintf("%zu", payload_size);
100 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
101 downloaded_percentage_str =
102 StringPrintf(" (%llu%%)",
103 IntRatio(total_bytes_received_, payload_size, 100));
104 }
105
106 LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_
107 << "/" << total_operations_str << " operations"
108 << completed_percentage_str << ", " << total_bytes_received_
109 << "/" << payload_size_str << " bytes downloaded"
110 << downloaded_percentage_str << ", overall progress "
111 << overall_progress_ << "%";
112}
113
114void DeltaPerformer::UpdateOverallProgress(bool force_log,
115 const char* message_prefix) {
116 // Compute our download and overall progress.
117 unsigned new_overall_progress = 0;
118 COMPILE_ASSERT(kProgressDownloadWeight + kProgressOperationsWeight == 100,
119 progress_weight_dont_add_up);
120 // Only consider download progress if its total size is known; otherwise
121 // adjust the operations weight to compensate for the absence of download
122 // progress. Also, make sure to cap the download portion at
123 // kProgressDownloadWeight, in case we end up downloading more than we
124 // initially expected (this indicates a problem, but could generally happen).
125 // TODO(garnold) the correction of operations weight when we do not have the
126 // total payload size, as well as the conditional guard below, should both be
127 // eliminated once we ensure that the payload_size in the install plan is
128 // always given and is non-zero. This currently isn't the case during unit
129 // tests (see chromium-os:37969).
130 size_t payload_size = install_plan_->payload_size;
131 unsigned actual_operations_weight = kProgressOperationsWeight;
132 if (payload_size)
133 new_overall_progress += min(
134 static_cast<unsigned>(IntRatio(total_bytes_received_, payload_size,
135 kProgressDownloadWeight)),
136 kProgressDownloadWeight);
137 else
138 actual_operations_weight += kProgressDownloadWeight;
139
140 // Only add completed operations if their total number is known; we definitely
141 // expect an update to have at least one operation, so the expectation is that
142 // this will eventually reach |actual_operations_weight|.
143 if (num_total_operations_)
144 new_overall_progress += IntRatio(next_operation_num_, num_total_operations_,
145 actual_operations_weight);
146
147 // Progress ratio cannot recede, unless our assumptions about the total
148 // payload size, total number of operations, or the monotonicity of progress
149 // is breached.
150 if (new_overall_progress < overall_progress_) {
151 LOG(WARNING) << "progress counter receded from " << overall_progress_
152 << "% down to " << new_overall_progress << "%; this is a bug";
153 force_log = true;
154 }
155 overall_progress_ = new_overall_progress;
156
157 // Update chunk index, log as needed: if forced by called, or we completed a
158 // progress chunk, or a timeout has expired.
159 base::Time curr_time = base::Time::Now();
160 unsigned curr_progress_chunk =
161 overall_progress_ * kProgressLogMaxChunks / 100;
162 if (force_log || curr_progress_chunk > last_progress_chunk_ ||
163 curr_time > forced_progress_log_time_) {
164 forced_progress_log_time_ = curr_time + forced_progress_log_wait_;
165 LogProgress(message_prefix);
166 }
167 last_progress_chunk_ = curr_progress_chunk;
168}
169
170
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700171// Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat
172// it safely. Returns false otherwise.
173bool DeltaPerformer::IsIdempotentOperation(
174 const DeltaArchiveManifest_InstallOperation& op) {
175 if (op.src_extents_size() == 0) {
176 return true;
177 }
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700178 // When in doubt, it's safe to declare an op non-idempotent. Note that we
179 // could detect other types of idempotent operations here such as a MOVE that
180 // moves blocks onto themselves. However, we rely on the server to not send
181 // such operations at all.
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700182 ExtentRanges src_ranges;
183 src_ranges.AddRepeatedExtents(op.src_extents());
184 const uint64_t block_count = src_ranges.blocks();
185 src_ranges.SubtractRepeatedExtents(op.dst_extents());
186 return block_count == src_ranges.blocks();
187}
188
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700189int DeltaPerformer::Open(const char* path, int flags, mode_t mode) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700190 int err;
191 if (OpenFile(path, &fd_, &err))
192 path_ = path;
193 return -err;
194}
195
196bool DeltaPerformer::OpenKernel(const char* kernel_path) {
197 int err;
198 bool success = OpenFile(kernel_path, &kernel_fd_, &err);
199 if (success)
200 kernel_path_ = kernel_path;
201 return success;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700202}
203
204int DeltaPerformer::Close() {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700205 int err = 0;
206 if (close(kernel_fd_) == -1) {
207 err = errno;
208 PLOG(ERROR) << "Unable to close kernel fd:";
209 }
210 if (close(fd_) == -1) {
211 err = errno;
212 PLOG(ERROR) << "Unable to close rootfs fd:";
213 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700214 LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800215 fd_ = -2; // Set to invalid so that calls to Open() will fail.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700216 path_ = "";
Darin Petkov934bb412010-11-18 11:21:35 -0800217 if (!buffer_.empty()) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700218 LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
219 if (err >= 0)
Darin Petkov934bb412010-11-18 11:21:35 -0800220 err = 1;
Darin Petkov934bb412010-11-18 11:21:35 -0800221 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700222 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700223}
224
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700225namespace {
226
227void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
228 string sha256;
229 if (OmahaHashCalculator::Base64Encode(info.hash().data(),
230 info.hash().size(),
231 &sha256)) {
Darin Petkov3aefa862010-12-07 14:45:00 -0800232 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
233 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700234 } else {
235 LOG(ERROR) << "Base64Encode failed for tag: " << tag;
236 }
237}
238
239void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
240 if (manifest.has_old_kernel_info())
241 LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info");
242 if (manifest.has_old_rootfs_info())
243 LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info");
244 if (manifest.has_new_kernel_info())
245 LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info");
246 if (manifest.has_new_rootfs_info())
247 LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info");
248}
249
250} // namespace {}
251
Don Garrett4d039442013-10-28 18:40:06 -0700252uint64_t DeltaPerformer::GetVersionOffset() {
253 // Manifest size is stored right after the magic string and the version.
254 return strlen(kDeltaMagic);
255}
256
Jay Srinivasanf4318702012-09-24 11:56:24 -0700257uint64_t DeltaPerformer::GetManifestSizeOffset() {
258 // Manifest size is stored right after the magic string and the version.
259 return strlen(kDeltaMagic) + kDeltaVersionSize;
260}
261
262uint64_t DeltaPerformer::GetManifestOffset() {
263 // Actual manifest begins right after the manifest size field.
264 return GetManifestSizeOffset() + kDeltaManifestSizeSize;
265}
266
267
Darin Petkov9574f7e2011-01-13 10:48:12 -0800268DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
269 const std::vector<char>& payload,
270 DeltaArchiveManifest* manifest,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700271 uint64_t* metadata_size,
David Zeuthena99981f2013-04-29 13:42:47 -0700272 ErrorCode* error) {
273 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700274
Jay Srinivasanf4318702012-09-24 11:56:24 -0700275 // manifest_offset is the byte offset where the manifest protobuf begins.
276 const uint64_t manifest_offset = GetManifestOffset();
277 if (payload.size() < manifest_offset) {
278 // Don't have enough bytes to even know the manifest size.
Darin Petkov9574f7e2011-01-13 10:48:12 -0800279 return kMetadataParseInsufficientData;
280 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700281
Jay Srinivasanf4318702012-09-24 11:56:24 -0700282 // Validate the magic string.
Darin Petkov9574f7e2011-01-13 10:48:12 -0800283 if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) {
284 LOG(ERROR) << "Bad payload format -- invalid delta magic.";
David Zeuthena99981f2013-04-29 13:42:47 -0700285 *error = kErrorCodeDownloadInvalidMetadataMagicString;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800286 return kMetadataParseError;
287 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700288
Don Garrett4d039442013-10-28 18:40:06 -0700289 // Extract the payload version from the metadata.
290 uint64_t major_payload_version;
291 COMPILE_ASSERT(sizeof(major_payload_version) == kDeltaVersionSize,
292 major_payload_version_size_mismatch);
293 memcpy(&major_payload_version,
294 &payload[GetVersionOffset()],
295 kDeltaVersionSize);
296 // switch big endian to host
297 major_payload_version = be64toh(major_payload_version);
298
299 if (major_payload_version != kSupportedMajorPayloadVersion) {
300 LOG(ERROR) << "Bad payload format -- unsupported payload version: "
301 << major_payload_version;
302 *error = kErrorCodeUnsupportedMajorPayloadVersion;
303 return kMetadataParseError;
304 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700305
Jay Srinivasanf4318702012-09-24 11:56:24 -0700306 // Next, parse the manifest size.
307 uint64_t manifest_size;
308 COMPILE_ASSERT(sizeof(manifest_size) == kDeltaManifestSizeSize,
309 manifest_size_size_mismatch);
310 memcpy(&manifest_size,
311 &payload[GetManifestSizeOffset()],
312 kDeltaManifestSizeSize);
313 manifest_size = be64toh(manifest_size); // switch big endian to host
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700314
315 // Now, check if the metasize we computed matches what was passed in
316 // through Omaha Response.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700317 *metadata_size = manifest_offset + manifest_size;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700318
Jay Srinivasanf4318702012-09-24 11:56:24 -0700319 // If the metadata size is present in install plan, check for it immediately
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700320 // even before waiting for that many number of bytes to be downloaded
321 // in the payload. This will prevent any attack which relies on us downloading
Jay Srinivasanf4318702012-09-24 11:56:24 -0700322 // data beyond the expected metadata size.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800323 if (install_plan_->hash_checks_mandatory) {
324 if (install_plan_->metadata_size != *metadata_size) {
325 LOG(ERROR) << "Mandatory metadata size in Omaha response ("
326 << install_plan_->metadata_size << ") is missing/incorrect."
327 << ", Actual = " << *metadata_size;
David Zeuthena99981f2013-04-29 13:42:47 -0700328 *error = kErrorCodeDownloadInvalidMetadataSize;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800329 return kMetadataParseError;
330 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700331 }
332
333 // Now that we have validated the metadata size, we should wait for the full
334 // metadata to be read in before we can parse it.
335 if (payload.size() < *metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800336 return kMetadataParseInsufficientData;
337 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700338
339 // Log whether we validated the size or simply trusting what's in the payload
Jay Srinivasanf4318702012-09-24 11:56:24 -0700340 // here. This is logged here (after we received the full metadata data) so
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700341 // that we just log once (instead of logging n times) if it takes n
342 // DeltaPerformer::Write calls to download the full manifest.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800343 if (install_plan_->metadata_size == *metadata_size) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700344 LOG(INFO) << "Manifest size in payload matches expected value from Omaha";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800345 } else {
346 // For mandatory-cases, we'd have already returned a kMetadataParseError
347 // above. We'll be here only for non-mandatory cases. Just send a UMA stat.
348 LOG(WARNING) << "Ignoring missing/incorrect metadata size ("
349 << install_plan_->metadata_size
350 << ") in Omaha response as validation is not mandatory. "
351 << "Trusting metadata size in payload = " << *metadata_size;
David Zeuthena99981f2013-04-29 13:42:47 -0700352 SendUmaStat(kErrorCodeDownloadInvalidMetadataSize);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800353 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700354
Jay Srinivasanf4318702012-09-24 11:56:24 -0700355 // We have the full metadata in |payload|. Verify its integrity
356 // and authenticity based on the information we have in Omaha response.
357 *error = ValidateMetadataSignature(&payload[0], *metadata_size);
David Zeuthena99981f2013-04-29 13:42:47 -0700358 if (*error != kErrorCodeSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800359 if (install_plan_->hash_checks_mandatory) {
360 LOG(ERROR) << "Mandatory metadata signature validation failed";
361 return kMetadataParseError;
362 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700363
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800364 // For non-mandatory cases, just send a UMA stat.
365 LOG(WARNING) << "Ignoring metadata signature validation failures";
366 SendUmaStat(*error);
David Zeuthena99981f2013-04-29 13:42:47 -0700367 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700368 }
369
Jay Srinivasanf4318702012-09-24 11:56:24 -0700370 // The metadata in |payload| is deemed valid. So, it's now safe to
371 // parse the protobuf.
372 if (!manifest->ParseFromArray(&payload[manifest_offset], manifest_size)) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800373 LOG(ERROR) << "Unable to parse manifest in update file.";
David Zeuthena99981f2013-04-29 13:42:47 -0700374 *error = kErrorCodeDownloadManifestParseError;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800375 return kMetadataParseError;
376 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800377 return kMetadataParseSuccess;
378}
379
380
Don Garrette410e0f2011-11-10 15:39:01 -0800381// Wrapper around write. Returns true if all requested bytes
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800382// were written, or false on any error, regardless of progress
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700383// and stores an action exit code in |error|.
384bool DeltaPerformer::Write(const void* bytes, size_t count,
David Zeuthena99981f2013-04-29 13:42:47 -0700385 ErrorCode *error) {
386 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700387
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700388 const char* c_bytes = reinterpret_cast<const char*>(bytes);
389 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800390 system_state_->payload_state()->DownloadProgress(count);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800391
392 // Update the total byte downloaded count and the progress logs.
393 total_bytes_received_ += count;
394 UpdateOverallProgress(false, "Completed ");
395
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700396 if (!manifest_valid_) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800397 MetadataParseResult result = ParsePayloadMetadata(buffer_,
398 &manifest_,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700399 &manifest_metadata_size_,
400 error);
Gilad Arnold5cac5912013-05-24 17:21:17 -0700401 if (result == kMetadataParseError)
Don Garrette410e0f2011-11-10 15:39:01 -0800402 return false;
Gilad Arnold5cac5912013-05-24 17:21:17 -0700403 if (result == kMetadataParseInsufficientData)
Don Garrette410e0f2011-11-10 15:39:01 -0800404 return true;
Gilad Arnold21504f02013-05-24 08:51:22 -0700405
406 // Checks the integrity of the payload manifest.
407 if ((*error = ValidateManifest()) != kErrorCodeSuccess)
408 return false;
409
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700410 // Remove protobuf and header info from buffer_, so buffer_ contains
411 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700412 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700413 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700414 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700415 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700416 manifest_valid_ = true;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700417
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700418 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700419 if (!PrimeUpdateState()) {
David Zeuthena99981f2013-04-29 13:42:47 -0700420 *error = kErrorCodeDownloadStateInitializationError;
Darin Petkov9b230572010-10-08 10:20:09 -0700421 LOG(ERROR) << "Unable to prime the update state.";
Don Garrette410e0f2011-11-10 15:39:01 -0800422 return false;
Darin Petkov9b230572010-10-08 10:20:09 -0700423 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800424
425 num_rootfs_operations_ = manifest_.install_operations_size();
426 num_total_operations_ =
427 num_rootfs_operations_ + manifest_.kernel_install_operations_size();
428 if (next_operation_num_ > 0)
429 UpdateOverallProgress(true, "Resuming after ");
430 LOG(INFO) << "Starting to apply update payload operations";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700431 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800432
433 while (next_operation_num_ < num_total_operations_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700434 // Check if we should cancel the current attempt for any reason.
435 // In this case, *error will have already been populated with the reason
436 // why we're cancelling.
437 if (system_state_->update_attempter()->ShouldCancel(error))
438 return false;
439
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800440 const bool is_kernel_partition =
441 (next_operation_num_ >= num_rootfs_operations_);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700442 const DeltaArchiveManifest_InstallOperation &op =
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800443 is_kernel_partition ?
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700444 manifest_.kernel_install_operations(
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800445 next_operation_num_ - num_rootfs_operations_) :
446 manifest_.install_operations(next_operation_num_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700447 if (!CanPerformInstallOperation(op)) {
448 // This means we don't have enough bytes received yet to carry out the
449 // next operation.
450 return true;
451 }
452
Jay Srinivasanf4318702012-09-24 11:56:24 -0700453 // Validate the operation only if the metadata signature is present.
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700454 // Otherwise, keep the old behavior. This serves as a knob to disable
455 // the validation logic in case we find some regression after rollout.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800456 // NOTE: If hash checks are mandatory and if metadata_signature is empty,
457 // we would have already failed in ParsePayloadMetadata method and thus not
458 // even be here. So no need to handle that case again here.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700459 if (!install_plan_->metadata_signature.empty()) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700460 // Note: Validate must be called only if CanPerformInstallOperation is
461 // called. Otherwise, we might be failing operations before even if there
462 // isn't sufficient data to compute the proper hash.
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800463 *error = ValidateOperationHash(op);
David Zeuthena99981f2013-04-29 13:42:47 -0700464 if (*error != kErrorCodeSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800465 if (install_plan_->hash_checks_mandatory) {
466 LOG(ERROR) << "Mandatory operation hash check failed";
467 return false;
468 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700469
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800470 // For non-mandatory cases, just send a UMA stat.
471 LOG(WARNING) << "Ignoring operation validation errors";
Jay Srinivasanedce2832012-10-24 18:57:47 -0700472 SendUmaStat(*error);
David Zeuthena99981f2013-04-29 13:42:47 -0700473 *error = kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700474 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700475 }
476
Darin Petkov45580e42010-10-08 14:02:40 -0700477 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700478 ScopedTerminatorExitUnblocker exit_unblocker =
479 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700480 // Log every thousandth operation, and also the first and last ones
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700481 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
482 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700483 if (!PerformReplaceOperation(op, is_kernel_partition)) {
484 LOG(ERROR) << "Failed to perform replace operation "
485 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700486 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800487 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700488 }
489 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700490 if (!PerformMoveOperation(op, is_kernel_partition)) {
491 LOG(ERROR) << "Failed to perform move operation "
492 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700493 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800494 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700495 }
496 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700497 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
498 LOG(ERROR) << "Failed to perform bsdiff operation "
499 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700500 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800501 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700502 }
503 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800504
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700505 next_operation_num_++;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800506 UpdateOverallProgress(false, "Completed ");
Darin Petkov73058b42010-10-06 16:32:19 -0700507 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700508 }
Don Garrette410e0f2011-11-10 15:39:01 -0800509 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700510}
511
David Zeuthen8f191b22013-08-06 12:27:50 -0700512bool DeltaPerformer::IsManifestValid() {
513 return manifest_valid_;
514}
515
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700516bool DeltaPerformer::CanPerformInstallOperation(
517 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
518 operation) {
519 // Move operations don't require any data blob, so they can always
520 // be performed
521 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
522 return true;
523
524 // See if we have the entire data blob in the buffer
525 if (operation.data_offset() < buffer_offset_) {
526 LOG(ERROR) << "we threw away data it seems?";
527 return false;
528 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700529
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700530 return (operation.data_offset() + operation.data_length()) <=
531 (buffer_offset_ + buffer_.size());
532}
533
534bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700535 const DeltaArchiveManifest_InstallOperation& operation,
536 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700537 CHECK(operation.type() == \
538 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
539 operation.type() == \
540 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
541
542 // Since we delete data off the beginning of the buffer as we use it,
543 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700544 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
545 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700546
Darin Petkov437adc42010-10-07 13:12:24 -0700547 // Extract the signature message if it's in this operation.
548 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700549
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700550 DirectExtentWriter direct_writer;
551 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
552 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700553
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700554 // Since bzip decompression is optional, we have a variable writer that will
555 // point to one of the ExtentWriter objects above.
556 ExtentWriter* writer = NULL;
557 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
558 writer = &zero_pad_writer;
559 } else if (operation.type() ==
560 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
561 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
562 writer = bzip_writer.get();
563 } else {
564 NOTREACHED();
565 }
566
567 // Create a vector of extents to pass to the ExtentWriter.
568 vector<Extent> extents;
569 for (int i = 0; i < operation.dst_extents_size(); i++) {
570 extents.push_back(operation.dst_extents(i));
571 }
572
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700573 int fd = is_kernel_partition ? kernel_fd_ : fd_;
574
575 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700576 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
577 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700578
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700579 // Update buffer
580 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700581 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700582 return true;
583}
584
585bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700586 const DeltaArchiveManifest_InstallOperation& operation,
587 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700588 // Calculate buffer size. Note, this function doesn't do a sliding
589 // window to copy in case the source and destination blocks overlap.
590 // If we wanted to do a sliding window, we could program the server
591 // to generate deltas that effectively did a sliding window.
592
593 uint64_t blocks_to_read = 0;
594 for (int i = 0; i < operation.src_extents_size(); i++)
595 blocks_to_read += operation.src_extents(i).num_blocks();
596
597 uint64_t blocks_to_write = 0;
598 for (int i = 0; i < operation.dst_extents_size(); i++)
599 blocks_to_write += operation.dst_extents(i).num_blocks();
600
601 DCHECK_EQ(blocks_to_write, blocks_to_read);
602 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700603
604 int fd = is_kernel_partition ? kernel_fd_ : fd_;
605
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700606 // Read in bytes.
607 ssize_t bytes_read = 0;
608 for (int i = 0; i < operation.src_extents_size(); i++) {
609 ssize_t bytes_read_this_iteration = 0;
610 const Extent& extent = operation.src_extents(i);
Darin Petkov8a075a72013-04-25 14:46:09 +0200611 const size_t bytes = extent.num_blocks() * block_size_;
612 if (extent.start_block() == kSparseHole) {
613 bytes_read_this_iteration = bytes;
614 memset(&buf[bytes_read], 0, bytes);
615 } else {
616 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
617 &buf[bytes_read],
618 bytes,
619 extent.start_block() * block_size_,
620 &bytes_read_this_iteration));
621 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700622 TEST_AND_RETURN_FALSE(
Darin Petkov8a075a72013-04-25 14:46:09 +0200623 bytes_read_this_iteration == static_cast<ssize_t>(bytes));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700624 bytes_read += bytes_read_this_iteration;
625 }
626
Darin Petkov45580e42010-10-08 14:02:40 -0700627 // If this is a non-idempotent operation, request a delayed exit and clear the
628 // update state in case the operation gets interrupted. Do this as late as
629 // possible.
630 if (!IsIdempotentOperation(operation)) {
631 Terminator::set_exit_blocked(true);
632 ResetUpdateProgress(prefs_, true);
633 }
634
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700635 // Write bytes out.
636 ssize_t bytes_written = 0;
637 for (int i = 0; i < operation.dst_extents_size(); i++) {
638 const Extent& extent = operation.dst_extents(i);
Darin Petkov8a075a72013-04-25 14:46:09 +0200639 const size_t bytes = extent.num_blocks() * block_size_;
640 if (extent.start_block() == kSparseHole) {
Darin Petkov741a8222013-05-02 10:02:34 +0200641 DCHECK(buf.begin() + bytes_written ==
642 std::search_n(buf.begin() + bytes_written,
643 buf.begin() + bytes_written + bytes,
644 bytes, 0));
Darin Petkov8a075a72013-04-25 14:46:09 +0200645 } else {
646 TEST_AND_RETURN_FALSE(
647 utils::PWriteAll(fd,
648 &buf[bytes_written],
649 bytes,
650 extent.start_block() * block_size_));
651 }
652 bytes_written += bytes;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700653 }
654 DCHECK_EQ(bytes_written, bytes_read);
655 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
656 return true;
657}
658
659bool DeltaPerformer::ExtentsToBsdiffPositionsString(
660 const RepeatedPtrField<Extent>& extents,
661 uint64_t block_size,
662 uint64_t full_length,
663 string* positions_string) {
664 string ret;
665 uint64_t length = 0;
666 for (int i = 0; i < extents.size(); i++) {
667 Extent extent = extents.Get(i);
668 int64_t start = extent.start_block();
669 uint64_t this_length = min(full_length - length,
670 extent.num_blocks() * block_size);
671 if (start == static_cast<int64_t>(kSparseHole))
672 start = -1;
673 else
674 start *= block_size;
675 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
676 length += this_length;
677 }
678 TEST_AND_RETURN_FALSE(length == full_length);
679 if (!ret.empty())
680 ret.resize(ret.size() - 1); // Strip trailing comma off
681 *positions_string = ret;
682 return true;
683}
684
685bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700686 const DeltaArchiveManifest_InstallOperation& operation,
687 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700688 // Since we delete data off the beginning of the buffer as we use it,
689 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700690 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
691 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700692
693 string input_positions;
694 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
695 block_size_,
696 operation.src_length(),
697 &input_positions));
698 string output_positions;
699 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
700 block_size_,
701 operation.dst_length(),
702 &output_positions));
703
704 string temp_filename;
705 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
706 &temp_filename,
707 NULL));
708 ScopedPathUnlinker path_unlinker(temp_filename);
709 {
710 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
711 ScopedFdCloser fd_closer(&fd);
712 TEST_AND_RETURN_FALSE(
713 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
714 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700715
Darin Petkov7f2ec752013-04-03 14:45:19 +0200716 // Update the buffer to release the patch data memory as soon as the patch
717 // file is written out.
718 buffer_offset_ += operation.data_length();
719 DiscardBufferHeadBytes(operation.data_length());
720
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700721 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Darin Petkov3d1670d2013-07-12 14:37:06 +0200722 const string path = StringPrintf("/proc/self/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700723
Darin Petkov45580e42010-10-08 14:02:40 -0700724 // If this is a non-idempotent operation, request a delayed exit and clear the
725 // update state in case the operation gets interrupted. Do this as late as
726 // possible.
727 if (!IsIdempotentOperation(operation)) {
728 Terminator::set_exit_blocked(true);
729 ResetUpdateProgress(prefs_, true);
730 }
731
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700732 vector<string> cmd;
733 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700734 cmd.push_back(path);
735 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700736 cmd.push_back(temp_filename);
737 cmd.push_back(input_positions);
738 cmd.push_back(output_positions);
739 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700740 TEST_AND_RETURN_FALSE(
741 Subprocess::SynchronousExecFlags(cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -0700742 G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700743 &return_code,
Darin Petkov85d02b72011-05-17 13:25:51 -0700744 NULL));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700745 TEST_AND_RETURN_FALSE(return_code == 0);
746
747 if (operation.dst_length() % block_size_) {
748 // Zero out rest of final block.
749 // TODO(adlr): build this into bspatch; it's more efficient that way.
750 const Extent& last_extent =
751 operation.dst_extents(operation.dst_extents_size() - 1);
752 const uint64_t end_byte =
753 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
754 const uint64_t begin_byte =
755 end_byte - (block_size_ - operation.dst_length() % block_size_);
756 vector<char> zeros(end_byte - begin_byte);
757 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700758 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700759 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700760 return true;
761}
762
Darin Petkovd7061ab2010-10-06 14:37:09 -0700763bool DeltaPerformer::ExtractSignatureMessage(
764 const DeltaArchiveManifest_InstallOperation& operation) {
765 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
766 !manifest_.has_signatures_offset() ||
767 manifest_.signatures_offset() != operation.data_offset()) {
768 return false;
769 }
770 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
771 manifest_.signatures_size() == operation.data_length());
772 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
773 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
774 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700775 signatures_message_data_.assign(
Darin Petkovd7061ab2010-10-06 14:37:09 -0700776 buffer_.begin(),
777 buffer_.begin() + manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700778
779 // Save the signature blob because if the update is interrupted after the
780 // download phase we don't go through this path anymore. Some alternatives to
781 // consider:
782 //
783 // 1. On resume, re-download the signature blob from the server and re-verify
784 // it.
785 //
786 // 2. Verify the signature as soon as it's received and don't checkpoint the
787 // blob and the signed sha-256 context.
788 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignatureBlob,
789 string(&signatures_message_data_[0],
790 signatures_message_data_.size())))
791 << "Unable to store the signature blob.";
Darin Petkov437adc42010-10-07 13:12:24 -0700792 // The hash of all data consumed so far should be verified against the signed
793 // hash.
794 signed_hash_context_ = hash_calculator_.GetContext();
795 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
796 signed_hash_context_))
797 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700798 LOG(INFO) << "Extracted signature data of size "
799 << manifest_.signatures_size() << " at "
800 << manifest_.signatures_offset();
801 return true;
802}
803
David Zeuthena99981f2013-04-29 13:42:47 -0700804ErrorCode DeltaPerformer::ValidateMetadataSignature(
Jay Srinivasanf4318702012-09-24 11:56:24 -0700805 const char* metadata, uint64_t metadata_size) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700806
Jay Srinivasanf4318702012-09-24 11:56:24 -0700807 if (install_plan_->metadata_signature.empty()) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800808 if (install_plan_->hash_checks_mandatory) {
809 LOG(ERROR) << "Missing mandatory metadata signature in Omaha response";
David Zeuthena99981f2013-04-29 13:42:47 -0700810 return kErrorCodeDownloadMetadataSignatureMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800811 }
812
813 // For non-mandatory cases, just send a UMA stat.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700814 LOG(WARNING) << "Cannot validate metadata as the signature is empty";
David Zeuthena99981f2013-04-29 13:42:47 -0700815 SendUmaStat(kErrorCodeDownloadMetadataSignatureMissingError);
816 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700817 }
818
819 // Convert base64-encoded signature to raw bytes.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700820 vector<char> metadata_signature;
821 if (!OmahaHashCalculator::Base64Decode(install_plan_->metadata_signature,
822 &metadata_signature)) {
823 LOG(ERROR) << "Unable to decode base64 metadata signature: "
824 << install_plan_->metadata_signature;
David Zeuthena99981f2013-04-29 13:42:47 -0700825 return kErrorCodeDownloadMetadataSignatureError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700826 }
827
Jay Srinivasanf4318702012-09-24 11:56:24 -0700828 vector<char> expected_metadata_hash;
829 if (!PayloadSigner::GetRawHashFromSignature(metadata_signature,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700830 public_key_path_,
Jay Srinivasanf4318702012-09-24 11:56:24 -0700831 &expected_metadata_hash)) {
832 LOG(ERROR) << "Unable to compute expected hash from metadata signature";
David Zeuthena99981f2013-04-29 13:42:47 -0700833 return kErrorCodeDownloadMetadataSignatureError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700834 }
835
Jay Srinivasanf4318702012-09-24 11:56:24 -0700836 OmahaHashCalculator metadata_hasher;
837 metadata_hasher.Update(metadata, metadata_size);
838 if (!metadata_hasher.Finalize()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700839 LOG(ERROR) << "Unable to compute actual hash of manifest";
David Zeuthena99981f2013-04-29 13:42:47 -0700840 return kErrorCodeDownloadMetadataSignatureVerificationError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700841 }
842
Jay Srinivasanf4318702012-09-24 11:56:24 -0700843 vector<char> calculated_metadata_hash = metadata_hasher.raw_hash();
844 PayloadSigner::PadRSA2048SHA256Hash(&calculated_metadata_hash);
845 if (calculated_metadata_hash.empty()) {
846 LOG(ERROR) << "Computed actual hash of metadata is empty.";
David Zeuthena99981f2013-04-29 13:42:47 -0700847 return kErrorCodeDownloadMetadataSignatureVerificationError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700848 }
849
Jay Srinivasanf4318702012-09-24 11:56:24 -0700850 if (calculated_metadata_hash != expected_metadata_hash) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700851 LOG(ERROR) << "Manifest hash verification failed. Expected hash = ";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700852 utils::HexDumpVector(expected_metadata_hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700853 LOG(ERROR) << "Calculated hash = ";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700854 utils::HexDumpVector(calculated_metadata_hash);
David Zeuthena99981f2013-04-29 13:42:47 -0700855 return kErrorCodeDownloadMetadataSignatureMismatch;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700856 }
857
858 LOG(INFO) << "Manifest signature matches expected value in Omaha response";
David Zeuthena99981f2013-04-29 13:42:47 -0700859 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700860}
861
Gilad Arnold21504f02013-05-24 08:51:22 -0700862ErrorCode DeltaPerformer::ValidateManifest() {
863 // Ensure that a full update does not contain old partition hashes, which is
864 // indicative of a delta.
865 //
866 // TODO(garnold) in general, the presence of an old partition hash should be
867 // the sole indicator for a delta update, as we would generally like update
868 // payloads to be self contained and not assume an Omaha response to tell us
869 // that. However, since this requires some massive reengineering of the update
870 // flow (making filesystem copying happen conditionally only *after*
871 // downloading and parsing of the update manifest) we'll put it off for now.
872 // See chromium-os:7597 for further discussion.
873 if (install_plan_->is_full_update &&
874 (manifest_.has_old_kernel_info() || manifest_.has_old_rootfs_info())) {
875 LOG(ERROR) << "Purported full payload contains old partition "
876 "hash(es), aborting update";
877 return kErrorCodePayloadMismatchedType;
878 }
879
880 // TODO(garnold) we should be adding more and more manifest checks, such as
881 // partition boundaries etc (see chromium-os:37661).
882
883 return kErrorCodeSuccess;
884}
885
David Zeuthena99981f2013-04-29 13:42:47 -0700886ErrorCode DeltaPerformer::ValidateOperationHash(
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800887 const DeltaArchiveManifest_InstallOperation& operation) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700888
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700889 if (!operation.data_sha256_hash().size()) {
890 if (!operation.data_length()) {
891 // Operations that do not have any data blob won't have any operation hash
892 // either. So, these operations are always considered validated since the
Jay Srinivasanf4318702012-09-24 11:56:24 -0700893 // metadata that contains all the non-data-blob portions of the operation
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800894 // has already been validated. This is true for both HTTP and HTTPS cases.
David Zeuthena99981f2013-04-29 13:42:47 -0700895 return kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700896 }
897
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800898 // No hash is present for an operation that has data blobs. This shouldn't
899 // happen normally for any client that has this code, because the
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700900 // corresponding update should have been produced with the operation
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800901 // hashes. So if it happens it means either we've turned operation hash
902 // generation off in DeltaDiffGenerator or it's a regression of some sort.
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700903 // One caveat though: The last operation is a dummy signature operation
904 // that doesn't have a hash at the time the manifest is created. So we
905 // should not complaint about that operation. This operation can be
906 // recognized by the fact that it's offset is mentioned in the manifest.
907 if (manifest_.signatures_offset() &&
908 manifest_.signatures_offset() == operation.data_offset()) {
909 LOG(INFO) << "Skipping hash verification for signature operation "
910 << next_operation_num_ + 1;
911 } else {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800912 if (install_plan_->hash_checks_mandatory) {
913 LOG(ERROR) << "Missing mandatory operation hash for operation "
914 << next_operation_num_ + 1;
David Zeuthena99981f2013-04-29 13:42:47 -0700915 return kErrorCodeDownloadOperationHashMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800916 }
917
918 // For non-mandatory cases, just send a UMA stat.
919 LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1
920 << " as there's no operation hash in manifest";
David Zeuthena99981f2013-04-29 13:42:47 -0700921 SendUmaStat(kErrorCodeDownloadOperationHashMissingError);
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700922 }
David Zeuthena99981f2013-04-29 13:42:47 -0700923 return kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700924 }
925
926 vector<char> expected_op_hash;
927 expected_op_hash.assign(operation.data_sha256_hash().data(),
928 (operation.data_sha256_hash().data() +
929 operation.data_sha256_hash().size()));
930
931 OmahaHashCalculator operation_hasher;
932 operation_hasher.Update(&buffer_[0], operation.data_length());
933 if (!operation_hasher.Finalize()) {
934 LOG(ERROR) << "Unable to compute actual hash of operation "
935 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700936 return kErrorCodeDownloadOperationHashVerificationError;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700937 }
938
939 vector<char> calculated_op_hash = operation_hasher.raw_hash();
940 if (calculated_op_hash != expected_op_hash) {
941 LOG(ERROR) << "Hash verification failed for operation "
942 << next_operation_num_ << ". Expected hash = ";
943 utils::HexDumpVector(expected_op_hash);
944 LOG(ERROR) << "Calculated hash over " << operation.data_length()
945 << " bytes at offset: " << operation.data_offset() << " = ";
946 utils::HexDumpVector(calculated_op_hash);
David Zeuthena99981f2013-04-29 13:42:47 -0700947 return kErrorCodeDownloadOperationHashMismatch;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700948 }
949
David Zeuthena99981f2013-04-29 13:42:47 -0700950 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700951}
952
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700953#define TEST_AND_RETURN_VAL(_retval, _condition) \
954 do { \
955 if (!(_condition)) { \
956 LOG(ERROR) << "VerifyPayload failure: " << #_condition; \
957 return _retval; \
958 } \
959 } while (0);
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700960
David Zeuthena99981f2013-04-29 13:42:47 -0700961ErrorCode DeltaPerformer::VerifyPayload(
Darin Petkov437adc42010-10-07 13:12:24 -0700962 const std::string& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700963 const uint64_t update_check_response_size) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700964 LOG(INFO) << "Verifying delta payload using public key: " << public_key_path_;
Darin Petkov437adc42010-10-07 13:12:24 -0700965
Jay Srinivasan0d8fb402012-05-07 19:19:38 -0700966 // Verifies the download size.
David Zeuthena99981f2013-04-29 13:42:47 -0700967 TEST_AND_RETURN_VAL(kErrorCodePayloadSizeMismatchError,
Jay Srinivasan0d8fb402012-05-07 19:19:38 -0700968 update_check_response_size ==
969 manifest_metadata_size_ + buffer_offset_);
970
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700971 // Verifies the payload hash.
972 const string& payload_hash_data = hash_calculator_.hash();
David Zeuthena99981f2013-04-29 13:42:47 -0700973 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadVerificationError,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700974 !payload_hash_data.empty());
David Zeuthena99981f2013-04-29 13:42:47 -0700975 TEST_AND_RETURN_VAL(kErrorCodePayloadHashMismatchError,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700976 payload_hash_data == update_check_response_hash);
Darin Petkov437adc42010-10-07 13:12:24 -0700977
Darin Petkov437adc42010-10-07 13:12:24 -0700978 // Verifies the signed payload hash.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700979 if (!utils::FileExists(public_key_path_.c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -0700980 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
David Zeuthena99981f2013-04-29 13:42:47 -0700981 return kErrorCodeSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700982 }
David Zeuthena99981f2013-04-29 13:42:47 -0700983 TEST_AND_RETURN_VAL(kErrorCodeSignedDeltaPayloadExpectedError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700984 !signatures_message_data_.empty());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700985 vector<char> signed_hash_data;
David Zeuthena99981f2013-04-29 13:42:47 -0700986 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700987 PayloadSigner::VerifySignature(
988 signatures_message_data_,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700989 public_key_path_,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700990 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -0700991 OmahaHashCalculator signed_hasher;
David Zeuthena99981f2013-04-29 13:42:47 -0700992 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700993 signed_hasher.SetContext(signed_hash_context_));
David Zeuthena99981f2013-04-29 13:42:47 -0700994 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700995 signed_hasher.Finalize());
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700996 vector<char> hash_data = signed_hasher.raw_hash();
997 PayloadSigner::PadRSA2048SHA256Hash(&hash_data);
David Zeuthena99981f2013-04-29 13:42:47 -0700998 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700999 !hash_data.empty());
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001000 if (hash_data != signed_hash_data) {
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001001 LOG(ERROR) << "Public key verification failed, thus update failed. "
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001002 "Attached Signature:";
1003 utils::HexDumpVector(signed_hash_data);
1004 LOG(ERROR) << "Computed Signature:";
1005 utils::HexDumpVector(hash_data);
David Zeuthena99981f2013-04-29 13:42:47 -07001006 return kErrorCodeDownloadPayloadPubKeyVerificationError;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001007 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001008
1009 // At this point, we are guaranteed to have downloaded a full payload, i.e
1010 // the one whose size matches the size mentioned in Omaha response. If any
1011 // errors happen after this, it's likely a problem with the payload itself or
1012 // the state of the system and not a problem with the URL or network. So,
Jay Srinivasan08262882012-12-28 19:29:43 -08001013 // indicate that to the payload state so that AU can backoff appropriately.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001014 system_state_->payload_state()->DownloadComplete();
1015
David Zeuthena99981f2013-04-29 13:42:47 -07001016 return kErrorCodeSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -07001017}
1018
Darin Petkov3aefa862010-12-07 14:45:00 -08001019bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size,
1020 vector<char>* kernel_hash,
1021 uint64_t* rootfs_size,
1022 vector<char>* rootfs_hash) {
Darin Petkov2dd01092010-10-08 15:43:05 -07001023 TEST_AND_RETURN_FALSE(manifest_valid_ &&
1024 manifest_.has_new_kernel_info() &&
1025 manifest_.has_new_rootfs_info());
Darin Petkov3aefa862010-12-07 14:45:00 -08001026 *kernel_size = manifest_.new_kernel_info().size();
1027 *rootfs_size = manifest_.new_rootfs_info().size();
1028 vector<char> new_kernel_hash(manifest_.new_kernel_info().hash().begin(),
1029 manifest_.new_kernel_info().hash().end());
1030 vector<char> new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(),
1031 manifest_.new_rootfs_info().hash().end());
1032 kernel_hash->swap(new_kernel_hash);
1033 rootfs_hash->swap(new_rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -07001034 return true;
1035}
1036
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001037namespace {
1038void LogVerifyError(bool is_kern,
1039 const string& local_hash,
1040 const string& expected_hash) {
1041 const char* type = is_kern ? "kernel" : "rootfs";
1042 LOG(ERROR) << "This is a server-side error due to "
1043 << "mismatched delta update image!";
1044 LOG(ERROR) << "The delta I've been given contains a " << type << " delta "
1045 << "update that must be applied over a " << type << " with "
1046 << "a specific checksum, but the " << type << " we're starting "
1047 << "with doesn't have that checksum! This means that "
1048 << "the delta I've been given doesn't match my existing "
1049 << "system. The " << type << " partition I have has hash: "
1050 << local_hash << " but the update expected me to have "
1051 << expected_hash << " .";
1052 if (is_kern) {
1053 LOG(INFO) << "To get the checksum of a kernel partition on a "
1054 << "booted machine, run this command (change /dev/sda2 "
1055 << "as needed): dd if=/dev/sda2 bs=1M 2>/dev/null | "
1056 << "openssl dgst -sha256 -binary | openssl base64";
1057 } else {
1058 LOG(INFO) << "To get the checksum of a rootfs partition on a "
1059 << "booted machine, run this command (change /dev/sda3 "
1060 << "as needed): dd if=/dev/sda3 bs=1M count=$(( "
1061 << "$(dumpe2fs /dev/sda3 2>/dev/null | grep 'Block count' "
1062 << "| sed 's/[^0-9]*//') / 256 )) | "
1063 << "openssl dgst -sha256 -binary | openssl base64";
1064 }
1065 LOG(INFO) << "To get the checksum of partitions in a bin file, "
1066 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
1067}
1068
1069string StringForHashBytes(const void* bytes, size_t size) {
1070 string ret;
1071 if (!OmahaHashCalculator::Base64Encode(bytes, size, &ret)) {
1072 ret = "<unknown>";
1073 }
1074 return ret;
1075}
1076} // namespace
1077
Darin Petkov698d0412010-10-13 10:59:44 -07001078bool DeltaPerformer::VerifySourcePartitions() {
1079 LOG(INFO) << "Verifying source partitions.";
1080 CHECK(manifest_valid_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001081 CHECK(install_plan_);
Darin Petkov698d0412010-10-13 10:59:44 -07001082 if (manifest_.has_old_kernel_info()) {
1083 const PartitionInfo& info = manifest_.old_kernel_info();
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001084 bool valid =
1085 !install_plan_->kernel_hash.empty() &&
1086 install_plan_->kernel_hash.size() == info.hash().size() &&
1087 memcmp(install_plan_->kernel_hash.data(),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001088 info.hash().data(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001089 install_plan_->kernel_hash.size()) == 0;
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001090 if (!valid) {
1091 LogVerifyError(true,
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001092 StringForHashBytes(install_plan_->kernel_hash.data(),
1093 install_plan_->kernel_hash.size()),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001094 StringForHashBytes(info.hash().data(),
1095 info.hash().size()));
1096 }
1097 TEST_AND_RETURN_FALSE(valid);
Darin Petkov698d0412010-10-13 10:59:44 -07001098 }
1099 if (manifest_.has_old_rootfs_info()) {
1100 const PartitionInfo& info = manifest_.old_rootfs_info();
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001101 bool valid =
1102 !install_plan_->rootfs_hash.empty() &&
1103 install_plan_->rootfs_hash.size() == info.hash().size() &&
1104 memcmp(install_plan_->rootfs_hash.data(),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001105 info.hash().data(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001106 install_plan_->rootfs_hash.size()) == 0;
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001107 if (!valid) {
1108 LogVerifyError(false,
Chris Sosa670d6802013-03-29 14:17:45 -07001109 StringForHashBytes(install_plan_->rootfs_hash.data(),
1110 install_plan_->rootfs_hash.size()),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001111 StringForHashBytes(info.hash().data(),
1112 info.hash().size()));
1113 }
1114 TEST_AND_RETURN_FALSE(valid);
Darin Petkov698d0412010-10-13 10:59:44 -07001115 }
1116 return true;
1117}
1118
Darin Petkov437adc42010-10-07 13:12:24 -07001119void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
1120 hash_calculator_.Update(&buffer_[0], count);
Darin Petkov7f2ec752013-04-03 14:45:19 +02001121 // Copy the remainder data into a temporary vector first to ensure that any
1122 // unused memory in the updated |buffer_| will be released.
1123 vector<char> temp(buffer_.begin() + count, buffer_.end());
1124 buffer_.swap(temp);
Darin Petkovd7061ab2010-10-06 14:37:09 -07001125}
1126
Darin Petkov0406e402010-10-06 21:33:11 -07001127bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
1128 string update_check_response_hash) {
1129 int64_t next_operation = kUpdateStateOperationInvalid;
1130 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
1131 &next_operation) &&
1132 next_operation != kUpdateStateOperationInvalid &&
1133 next_operation > 0);
1134
1135 string interrupted_hash;
1136 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
1137 &interrupted_hash) &&
David Zeuthenc41c2282013-06-17 16:33:06 -07001138 !interrupted_hash.empty() &&
1139 interrupted_hash == update_check_response_hash);
Darin Petkov0406e402010-10-06 21:33:11 -07001140
Darin Petkov61426142010-10-08 11:04:55 -07001141 int64_t resumed_update_failures;
1142 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
1143 &resumed_update_failures) ||
1144 resumed_update_failures <= kMaxResumedUpdateFailures);
1145
Darin Petkov0406e402010-10-06 21:33:11 -07001146 // Sanity check the rest.
1147 int64_t next_data_offset = -1;
1148 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
1149 &next_data_offset) &&
1150 next_data_offset >= 0);
1151
Darin Petkov437adc42010-10-07 13:12:24 -07001152 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -07001153 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -07001154 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
1155 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -07001156
1157 int64_t manifest_metadata_size = 0;
1158 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
1159 &manifest_metadata_size) &&
1160 manifest_metadata_size > 0);
1161
1162 return true;
1163}
1164
Darin Petkov9b230572010-10-08 10:20:09 -07001165bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -07001166 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
1167 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -07001168 if (!quick) {
1169 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
1170 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
David Zeuthen41996ad2013-09-24 15:43:24 -07001171 prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001172 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
1173 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001174 prefs->SetString(kPrefsUpdateStateSignatureBlob, "");
Darin Petkov9b230572010-10-08 10:20:09 -07001175 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -07001176 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001177 }
Darin Petkov73058b42010-10-06 16:32:19 -07001178 return true;
1179}
1180
1181bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001182 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -07001183 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001184 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -07001185 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -07001186 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -07001187 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -07001188 hash_calculator_.GetContext()));
1189 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
1190 buffer_offset_));
1191 last_updated_buffer_offset_ = buffer_offset_;
David Zeuthen41996ad2013-09-24 15:43:24 -07001192
1193 if (next_operation_num_ < num_total_operations_) {
1194 const bool is_kernel_partition =
1195 next_operation_num_ >= num_rootfs_operations_;
1196 const DeltaArchiveManifest_InstallOperation &op =
1197 is_kernel_partition ?
1198 manifest_.kernel_install_operations(
1199 next_operation_num_ - num_rootfs_operations_) :
1200 manifest_.install_operations(next_operation_num_);
1201 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength,
1202 op.data_length()));
1203 } else {
1204 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength,
1205 0));
1206 }
Darin Petkov0406e402010-10-06 21:33:11 -07001207 }
Darin Petkov73058b42010-10-06 16:32:19 -07001208 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
1209 next_operation_num_));
1210 return true;
1211}
1212
Darin Petkov9b230572010-10-08 10:20:09 -07001213bool DeltaPerformer::PrimeUpdateState() {
1214 CHECK(manifest_valid_);
1215 block_size_ = manifest_.block_size();
1216
1217 int64_t next_operation = kUpdateStateOperationInvalid;
1218 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
1219 next_operation == kUpdateStateOperationInvalid ||
1220 next_operation <= 0) {
1221 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -07001222 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -07001223 return true;
1224 }
1225 next_operation_num_ = next_operation;
1226
1227 // Resuming an update -- load the rest of the update state.
1228 int64_t next_data_offset = -1;
1229 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
1230 &next_data_offset) &&
1231 next_data_offset >= 0);
1232 buffer_offset_ = next_data_offset;
1233
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001234 // The signed hash context and the signature blob may be empty if the
1235 // interrupted update didn't reach the signature.
Darin Petkov9b230572010-10-08 10:20:09 -07001236 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
1237 &signed_hash_context_);
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001238 string signature_blob;
1239 if (prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signature_blob)) {
1240 signatures_message_data_.assign(signature_blob.begin(),
1241 signature_blob.end());
1242 }
Darin Petkov9b230572010-10-08 10:20:09 -07001243
1244 string hash_context;
1245 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
1246 &hash_context) &&
1247 hash_calculator_.SetContext(hash_context));
1248
1249 int64_t manifest_metadata_size = 0;
1250 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
1251 &manifest_metadata_size) &&
1252 manifest_metadata_size > 0);
1253 manifest_metadata_size_ = manifest_metadata_size;
1254
Gilad Arnold8a86fa52013-01-15 12:35:05 -08001255 // Advance the download progress to reflect what doesn't need to be
1256 // re-downloaded.
1257 total_bytes_received_ += buffer_offset_;
1258
Darin Petkov61426142010-10-08 11:04:55 -07001259 // Speculatively count the resume as a failure.
1260 int64_t resumed_update_failures;
1261 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
1262 resumed_update_failures++;
1263 } else {
1264 resumed_update_failures = 1;
1265 }
1266 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -07001267 return true;
1268}
1269
David Zeuthena99981f2013-04-29 13:42:47 -07001270void DeltaPerformer::SendUmaStat(ErrorCode code) {
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001271 utils::SendErrorCodeToUma(system_state_, code);
Jay Srinivasanf0572052012-10-23 18:12:56 -07001272}
1273
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001274} // namespace chromeos_update_engine