blob: 61d28287de33569a2fae4533126aa5c42107e5ef [file] [log] [blame]
Alex Deymo5e3ea272016-01-28 13:42:23 -08001//
2// Copyright (C) 2016 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/update_attempter_android.h"
18
19#include <algorithm>
Alex Deymo218397f2016-02-04 23:55:10 -080020#include <map>
Alex Deymo5e3ea272016-01-28 13:42:23 -080021#include <utility>
22
23#include <base/bind.h>
24#include <base/logging.h>
Alex Deymo218397f2016-02-04 23:55:10 -080025#include <base/strings/string_number_conversions.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080026#include <brillo/bind_lambda.h>
27#include <brillo/message_loops/message_loop.h>
Alex Deymo218397f2016-02-04 23:55:10 -080028#include <brillo/strings/string_utils.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080029
30#include "update_engine/common/constants.h"
Alex Deymo2c131bb2016-05-26 16:43:13 -070031#include "update_engine/common/file_fetcher.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080032#include "update_engine/common/libcurl_http_fetcher.h"
33#include "update_engine/common/multi_range_http_fetcher.h"
34#include "update_engine/common/utils.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070035#include "update_engine/daemon_state_interface.h"
Alex Deymo87792ea2016-07-25 15:40:36 -070036#include "update_engine/network_selector.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080037#include "update_engine/payload_consumer/download_action.h"
38#include "update_engine/payload_consumer/filesystem_verifier_action.h"
39#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo3b678db2016-02-09 11:50:06 -080040#include "update_engine/update_status_utils.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080041
42using base::Bind;
43using base::TimeDelta;
44using base::TimeTicks;
45using std::shared_ptr;
46using std::string;
47using std::vector;
48
49namespace chromeos_update_engine {
50
51namespace {
52
Alex Deymo0d298542016-03-30 18:31:49 -070053// Minimum threshold to broadcast an status update in progress and time.
54const double kBroadcastThresholdProgress = 0.01; // 1%
55const int kBroadcastThresholdSeconds = 10;
56
Alex Deymo5e3ea272016-01-28 13:42:23 -080057const char* const kErrorDomain = "update_engine";
58// TODO(deymo): Convert the different errors to a numeric value to report them
59// back on the service error.
60const char* const kGenericError = "generic_error";
61
62// Log and set the error on the passed ErrorPtr.
63bool LogAndSetError(brillo::ErrorPtr* error,
64 const tracked_objects::Location& location,
65 const string& reason) {
66 brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
67 LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
68 << reason;
69 return false;
70}
71
72} // namespace
73
74UpdateAttempterAndroid::UpdateAttempterAndroid(
Alex Deymo03a4de72016-07-20 16:08:23 -070075 DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080076 PrefsInterface* prefs,
77 BootControlInterface* boot_control,
78 HardwareInterface* hardware)
79 : daemon_state_(daemon_state),
80 prefs_(prefs),
81 boot_control_(boot_control),
82 hardware_(hardware),
83 processor_(new ActionProcessor()) {
Alex Deymo87792ea2016-07-25 15:40:36 -070084 network_selector_ = network::CreateNetworkSelector();
Alex Deymo5e3ea272016-01-28 13:42:23 -080085}
86
87UpdateAttempterAndroid::~UpdateAttempterAndroid() {
88 // Release ourselves as the ActionProcessor's delegate to prevent
89 // re-scheduling the updates due to the processing stopped.
90 processor_->set_delegate(nullptr);
91}
92
93void UpdateAttempterAndroid::Init() {
94 // In case of update_engine restart without a reboot we need to restore the
95 // reboot needed state.
96 if (UpdateCompletedOnThisBoot())
Alex Deymo0e061ae2016-02-09 17:49:03 -080097 SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
Alex Deymo5e3ea272016-01-28 13:42:23 -080098 else
Alex Deymo0e061ae2016-02-09 17:49:03 -080099 SetStatusAndNotify(UpdateStatus::IDLE);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800100}
101
102bool UpdateAttempterAndroid::ApplyPayload(
103 const string& payload_url,
104 int64_t payload_offset,
105 int64_t payload_size,
106 const vector<string>& key_value_pair_headers,
107 brillo::ErrorPtr* error) {
108 if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
109 return LogAndSetError(
110 error, FROM_HERE, "An update already applied, waiting for reboot");
111 }
112 if (ongoing_update_) {
113 return LogAndSetError(
114 error, FROM_HERE, "Already processing an update, cancel it first.");
115 }
116 DCHECK(status_ == UpdateStatus::IDLE);
117
Alex Deymo218397f2016-02-04 23:55:10 -0800118 std::map<string, string> headers;
119 for (const string& key_value_pair : key_value_pair_headers) {
120 string key;
121 string value;
122 if (!brillo::string_utils::SplitAtFirst(
123 key_value_pair, "=", &key, &value, false)) {
124 return LogAndSetError(
125 error, FROM_HERE, "Passed invalid header: " + key_value_pair);
126 }
127 if (!headers.emplace(key, value).second)
128 return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
129 }
130
131 // Unique identifier for the payload. An empty string means that the payload
132 // can't be resumed.
133 string payload_id = (headers[kPayloadPropertyFileHash] +
134 headers[kPayloadPropertyMetadataHash]);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800135
136 // Setup the InstallPlan based on the request.
137 install_plan_ = InstallPlan();
138
139 install_plan_.download_url = payload_url;
140 install_plan_.version = "";
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800141 base_offset_ = payload_offset;
Alex Deymo218397f2016-02-04 23:55:10 -0800142 install_plan_.payload_size = payload_size;
143 if (!install_plan_.payload_size) {
144 if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
145 &install_plan_.payload_size)) {
146 install_plan_.payload_size = 0;
147 }
148 }
149 install_plan_.payload_hash = headers[kPayloadPropertyFileHash];
150 if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize],
151 &install_plan_.metadata_size)) {
152 install_plan_.metadata_size = 0;
153 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800154 install_plan_.metadata_signature = "";
155 // The |public_key_rsa| key would override the public key stored on disk.
156 install_plan_.public_key_rsa = "";
157
158 install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
159 install_plan_.is_resume = !payload_id.empty() &&
160 DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
161 if (!install_plan_.is_resume) {
162 if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
163 LOG(WARNING) << "Unable to reset the update progress.";
164 }
165 if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
166 LOG(WARNING) << "Unable to save the update check response hash.";
167 }
168 }
Alex Deymo64d98782016-02-05 18:03:48 -0800169 // The |payload_type| is not used anymore since minor_version 3.
170 install_plan_.payload_type = InstallPayloadType::kUnknown;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800171
172 install_plan_.source_slot = boot_control_->GetCurrentSlot();
173 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
Alex Deymofb905d92016-06-03 19:26:58 -0700174
175 int data_wipe = 0;
176 install_plan_.powerwash_required =
177 base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) &&
178 data_wipe != 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800179
Alex Deymo87792ea2016-07-25 15:40:36 -0700180 NetworkId network_id = kDefaultNetworkId;
181 if (!headers[kPayloadPropertyNetworkId].empty()) {
182 if (!base::StringToUint64(headers[kPayloadPropertyNetworkId],
183 &network_id)) {
184 return LogAndSetError(
185 error,
186 FROM_HERE,
187 "Invalid network_id: " + headers[kPayloadPropertyNetworkId]);
188 }
189 if (!network_selector_->SetProcessNetwork(network_id)) {
190 LOG(WARNING) << "Unable to set network_id, continuing with the update.";
191 }
192 }
193
Alex Deymo5e3ea272016-01-28 13:42:23 -0800194 LOG(INFO) << "Using this install plan:";
195 install_plan_.Dump();
196
Alex Deymo2c131bb2016-05-26 16:43:13 -0700197 BuildUpdateActions(payload_url);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800198 SetupDownload();
Alex Deymofdd6dec2016-03-03 22:35:43 -0800199 // Setup extra headers.
200 HttpFetcher* fetcher = download_action_->http_fetcher();
201 if (!headers[kPayloadPropertyAuthorization].empty())
202 fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
203 if (!headers[kPayloadPropertyUserAgent].empty())
204 fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
205
Alex Deymo5e3ea272016-01-28 13:42:23 -0800206 cpu_limiter_.StartLimiter();
207 SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
Alex Deymof2858572016-02-25 11:20:13 -0800208 ongoing_update_ = true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800209
210 // Just in case we didn't update boot flags yet, make sure they're updated
211 // before any update processing starts. This will start the update process.
212 UpdateBootFlags();
213 return true;
214}
215
216bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800217 if (!ongoing_update_)
218 return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
219 processor_->SuspendProcessing();
220 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800221}
222
223bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800224 if (!ongoing_update_)
225 return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
226 processor_->ResumeProcessing();
227 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800228}
229
230bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800231 if (!ongoing_update_)
Alex Deymo5e3ea272016-01-28 13:42:23 -0800232 return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
Alex Deymof2858572016-02-25 11:20:13 -0800233 processor_->StopProcessing();
234 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800235}
236
Alex Deymo3b678db2016-02-09 11:50:06 -0800237bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
238 LOG(INFO) << "Attempting to reset state from "
239 << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
240
241 switch (status_) {
242 case UpdateStatus::IDLE:
243 return true;
244
245 case UpdateStatus::UPDATED_NEED_REBOOT: {
246 // Remove the reboot marker so that if the machine is rebooted
247 // after resetting to idle state, it doesn't go back to
248 // UpdateStatus::UPDATED_NEED_REBOOT state.
249 bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
250
251 // Update the boot flags so the current slot has higher priority.
252 if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
253 ret_value = false;
254
255 if (!ret_value) {
256 return LogAndSetError(
257 error,
258 FROM_HERE,
259 "Failed to reset the status to ");
260 }
261
262 SetStatusAndNotify(UpdateStatus::IDLE);
263 LOG(INFO) << "Reset status successful";
264 return true;
265 }
266
267 default:
268 return LogAndSetError(
269 error,
270 FROM_HERE,
271 "Reset not allowed in this state. Cancel the ongoing update first");
272 }
273}
274
Alex Deymo5e3ea272016-01-28 13:42:23 -0800275void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
276 ErrorCode code) {
277 LOG(INFO) << "Processing Done.";
278
Alex Deymo5990bf32016-07-19 17:01:41 -0700279 switch (code) {
280 case ErrorCode::kSuccess:
281 // Update succeeded.
282 WriteUpdateCompletedMarker();
283 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
284 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800285
Alex Deymo5990bf32016-07-19 17:01:41 -0700286 LOG(INFO) << "Update successfully applied, waiting to reboot.";
287 break;
288
289 case ErrorCode::kFilesystemCopierError:
290 case ErrorCode::kNewRootfsVerificationError:
291 case ErrorCode::kNewKernelVerificationError:
292 case ErrorCode::kFilesystemVerifierError:
293 case ErrorCode::kDownloadStateInitializationError:
294 // Reset the ongoing update for these errors so it starts from the
295 // beginning next time.
296 DeltaPerformer::ResetUpdateProgress(prefs_, false);
297 LOG(INFO) << "Resetting update progress.";
298 break;
299
300 default:
301 // Ignore all other error codes.
302 break;
Alex Deymo03a4de72016-07-20 16:08:23 -0700303 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800304
305 TerminateUpdateAndNotify(code);
306}
307
308void UpdateAttempterAndroid::ProcessingStopped(
309 const ActionProcessor* processor) {
310 TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
311}
312
313void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
314 AbstractAction* action,
315 ErrorCode code) {
316 // Reset download progress regardless of whether or not the download
317 // action succeeded.
318 const string type = action->Type();
319 if (type == DownloadAction::StaticType()) {
Alex Deymo0d298542016-03-30 18:31:49 -0700320 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800321 }
322 if (code != ErrorCode::kSuccess) {
323 // If an action failed, the ActionProcessor will cancel the whole thing.
324 return;
325 }
326 if (type == DownloadAction::StaticType()) {
327 SetStatusAndNotify(UpdateStatus::FINALIZING);
328 }
329}
330
331void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
332 uint64_t bytes_received,
333 uint64_t total) {
Alex Deymo0d298542016-03-30 18:31:49 -0700334 double progress = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800335 if (total)
336 progress = static_cast<double>(bytes_received) / static_cast<double>(total);
Alex Deymo0d298542016-03-30 18:31:49 -0700337 if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
Alex Deymo5e3ea272016-01-28 13:42:23 -0800338 download_progress_ = progress;
339 SetStatusAndNotify(UpdateStatus::DOWNLOADING);
Alex Deymo0d298542016-03-30 18:31:49 -0700340 } else {
341 ProgressUpdate(progress);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800342 }
343}
344
345bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
346 // TODO(deymo): Notify the DownloadAction that it should cancel the update
347 // download.
348 return false;
349}
350
351void UpdateAttempterAndroid::DownloadComplete() {
352 // Nothing needs to be done when the download completes.
353}
354
Alex Deymo0d298542016-03-30 18:31:49 -0700355void UpdateAttempterAndroid::ProgressUpdate(double progress) {
356 // Self throttle based on progress. Also send notifications if progress is
357 // too slow.
358 if (progress == 1.0 ||
359 progress - download_progress_ >= kBroadcastThresholdProgress ||
360 TimeTicks::Now() - last_notify_time_ >=
361 TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
362 download_progress_ = progress;
363 SetStatusAndNotify(status_);
364 }
365}
366
Alex Deymo5e3ea272016-01-28 13:42:23 -0800367void UpdateAttempterAndroid::UpdateBootFlags() {
368 if (updated_boot_flags_) {
369 LOG(INFO) << "Already updated boot flags. Skipping.";
370 CompleteUpdateBootFlags(true);
371 return;
372 }
373 // This is purely best effort.
374 LOG(INFO) << "Marking booted slot as good.";
375 if (!boot_control_->MarkBootSuccessfulAsync(
376 Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
377 base::Unretained(this)))) {
378 LOG(ERROR) << "Failed to mark current boot as successful.";
379 CompleteUpdateBootFlags(false);
380 }
381}
382
383void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
384 updated_boot_flags_ = true;
385 ScheduleProcessingStart();
386}
387
388void UpdateAttempterAndroid::ScheduleProcessingStart() {
389 LOG(INFO) << "Scheduling an action processor start.";
390 brillo::MessageLoop::current()->PostTask(
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700391 FROM_HERE,
392 Bind([](ActionProcessor* processor) { processor->StartProcessing(); },
393 base::Unretained(processor_.get())));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800394}
395
396void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
397 if (status_ == UpdateStatus::IDLE) {
398 LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
399 return;
400 }
401
402 // Reset cpu shares back to normal.
403 cpu_limiter_.StopLimiter();
Alex Deymo0d298542016-03-30 18:31:49 -0700404 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800405 actions_.clear();
406 UpdateStatus new_status =
407 (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
408 : UpdateStatus::IDLE);
409 SetStatusAndNotify(new_status);
410 ongoing_update_ = false;
411
412 for (auto observer : daemon_state_->service_observers())
413 observer->SendPayloadApplicationComplete(error_code);
414}
415
416void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
417 status_ = status;
418 for (auto observer : daemon_state_->service_observers()) {
419 observer->SendStatusUpdate(
420 0, download_progress_, status_, "", install_plan_.payload_size);
421 }
422 last_notify_time_ = TimeTicks::Now();
423}
424
Alex Deymo2c131bb2016-05-26 16:43:13 -0700425void UpdateAttempterAndroid::BuildUpdateActions(const string& url) {
Alex Deymo5e3ea272016-01-28 13:42:23 -0800426 CHECK(!processor_->IsRunning());
427 processor_->set_delegate(this);
428
429 // Actions:
430 shared_ptr<InstallPlanAction> install_plan_action(
431 new InstallPlanAction(install_plan_));
432
Alex Deymo2c131bb2016-05-26 16:43:13 -0700433 HttpFetcher* download_fetcher = nullptr;
434 if (FileFetcher::SupportedUrl(url)) {
435 DLOG(INFO) << "Using FileFetcher for file URL.";
436 download_fetcher = new FileFetcher();
437 } else {
438 LibcurlHttpFetcher* libcurl_fetcher =
439 new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
440 libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload);
441 download_fetcher = libcurl_fetcher;
442 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800443 shared_ptr<DownloadAction> download_action(new DownloadAction(
444 prefs_,
445 boot_control_,
446 hardware_,
447 nullptr, // system_state, not used.
448 new MultiRangeHttpFetcher(download_fetcher))); // passes ownership
Sen Jiangfef85fd2016-03-25 15:32:49 -0700449 shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
Sen Jiange6e4bb92016-04-05 14:59:12 -0700450 new FilesystemVerifierAction());
Alex Deymo5e3ea272016-01-28 13:42:23 -0800451
452 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700453 new PostinstallRunnerAction(boot_control_, hardware_));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800454
455 download_action->set_delegate(this);
456 download_action_ = download_action;
Alex Deymob6eef732016-06-10 12:58:11 -0700457 postinstall_runner_action->set_delegate(this);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800458
459 actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
460 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Sen Jiangfef85fd2016-03-25 15:32:49 -0700461 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800462 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
463
464 // Bond them together. We have to use the leaf-types when calling
465 // BondActions().
466 BondActions(install_plan_action.get(), download_action.get());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700467 BondActions(download_action.get(), filesystem_verifier_action.get());
468 BondActions(filesystem_verifier_action.get(),
Alex Deymo5e3ea272016-01-28 13:42:23 -0800469 postinstall_runner_action.get());
470
471 // Enqueue the actions.
472 for (const shared_ptr<AbstractAction>& action : actions_)
473 processor_->EnqueueAction(action.get());
474}
475
476void UpdateAttempterAndroid::SetupDownload() {
477 MultiRangeHttpFetcher* fetcher =
478 static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
479 fetcher->ClearRanges();
480 if (install_plan_.is_resume) {
481 // Resuming an update so fetch the update manifest metadata first.
482 int64_t manifest_metadata_size = 0;
Alex Deymof25eb492016-02-26 00:20:08 -0800483 int64_t manifest_signature_size = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800484 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Alex Deymof25eb492016-02-26 00:20:08 -0800485 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
486 fetcher->AddRange(base_offset_,
487 manifest_metadata_size + manifest_signature_size);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800488 // If there're remaining unprocessed data blobs, fetch them. Be careful not
489 // to request data beyond the end of the payload to avoid 416 HTTP response
490 // error codes.
491 int64_t next_data_offset = 0;
492 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Alex Deymof25eb492016-02-26 00:20:08 -0800493 uint64_t resume_offset =
494 manifest_metadata_size + manifest_signature_size + next_data_offset;
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800495 if (!install_plan_.payload_size) {
496 fetcher->AddRange(base_offset_ + resume_offset);
497 } else if (resume_offset < install_plan_.payload_size) {
498 fetcher->AddRange(base_offset_ + resume_offset,
499 install_plan_.payload_size - resume_offset);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800500 }
501 } else {
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800502 if (install_plan_.payload_size) {
503 fetcher->AddRange(base_offset_, install_plan_.payload_size);
504 } else {
505 // If no payload size is passed we assume we read until the end of the
506 // stream.
507 fetcher->AddRange(base_offset_);
508 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800509 }
510}
511
512bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
513 string boot_id;
514 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
515 prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
516 return true;
517}
518
519bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
520 // In case of an update_engine restart without a reboot, we stored the boot_id
521 // when the update was completed by setting a pref, so we can check whether
522 // the last update was on this boot or a previous one.
523 string boot_id;
524 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
525
526 string update_completed_on_boot_id;
527 return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
528 prefs_->GetString(kPrefsUpdateCompletedOnBootId,
529 &update_completed_on_boot_id) &&
530 update_completed_on_boot_id == boot_id);
531}
532
533} // namespace chromeos_update_engine