blob: 30593dc10bbb52be726cc1db1ea52723864b9515 [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"
31#include "update_engine/common/libcurl_http_fetcher.h"
32#include "update_engine/common/multi_range_http_fetcher.h"
33#include "update_engine/common/utils.h"
34#include "update_engine/daemon_state_android.h"
35#include "update_engine/payload_consumer/download_action.h"
36#include "update_engine/payload_consumer/filesystem_verifier_action.h"
37#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo3b678db2016-02-09 11:50:06 -080038#include "update_engine/update_status_utils.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080039
40using base::Bind;
41using base::TimeDelta;
42using base::TimeTicks;
43using std::shared_ptr;
44using std::string;
45using std::vector;
46
47namespace chromeos_update_engine {
48
49namespace {
50
Alex Deymo0d298542016-03-30 18:31:49 -070051// Minimum threshold to broadcast an status update in progress and time.
52const double kBroadcastThresholdProgress = 0.01; // 1%
53const int kBroadcastThresholdSeconds = 10;
54
Alex Deymo5e3ea272016-01-28 13:42:23 -080055const char* const kErrorDomain = "update_engine";
56// TODO(deymo): Convert the different errors to a numeric value to report them
57// back on the service error.
58const char* const kGenericError = "generic_error";
59
60// Log and set the error on the passed ErrorPtr.
61bool LogAndSetError(brillo::ErrorPtr* error,
62 const tracked_objects::Location& location,
63 const string& reason) {
64 brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
65 LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
66 << reason;
67 return false;
68}
69
70} // namespace
71
72UpdateAttempterAndroid::UpdateAttempterAndroid(
73 DaemonStateAndroid* daemon_state,
74 PrefsInterface* prefs,
75 BootControlInterface* boot_control,
76 HardwareInterface* hardware)
77 : daemon_state_(daemon_state),
78 prefs_(prefs),
79 boot_control_(boot_control),
80 hardware_(hardware),
81 processor_(new ActionProcessor()) {
82}
83
84UpdateAttempterAndroid::~UpdateAttempterAndroid() {
85 // Release ourselves as the ActionProcessor's delegate to prevent
86 // re-scheduling the updates due to the processing stopped.
87 processor_->set_delegate(nullptr);
88}
89
90void UpdateAttempterAndroid::Init() {
91 // In case of update_engine restart without a reboot we need to restore the
92 // reboot needed state.
93 if (UpdateCompletedOnThisBoot())
Alex Deymo0e061ae2016-02-09 17:49:03 -080094 SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
Alex Deymo5e3ea272016-01-28 13:42:23 -080095 else
Alex Deymo0e061ae2016-02-09 17:49:03 -080096 SetStatusAndNotify(UpdateStatus::IDLE);
Alex Deymo5e3ea272016-01-28 13:42:23 -080097}
98
99bool UpdateAttempterAndroid::ApplyPayload(
100 const string& payload_url,
101 int64_t payload_offset,
102 int64_t payload_size,
103 const vector<string>& key_value_pair_headers,
104 brillo::ErrorPtr* error) {
105 if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
106 return LogAndSetError(
107 error, FROM_HERE, "An update already applied, waiting for reboot");
108 }
109 if (ongoing_update_) {
110 return LogAndSetError(
111 error, FROM_HERE, "Already processing an update, cancel it first.");
112 }
113 DCHECK(status_ == UpdateStatus::IDLE);
114
Alex Deymo218397f2016-02-04 23:55:10 -0800115 std::map<string, string> headers;
116 for (const string& key_value_pair : key_value_pair_headers) {
117 string key;
118 string value;
119 if (!brillo::string_utils::SplitAtFirst(
120 key_value_pair, "=", &key, &value, false)) {
121 return LogAndSetError(
122 error, FROM_HERE, "Passed invalid header: " + key_value_pair);
123 }
124 if (!headers.emplace(key, value).second)
125 return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
126 }
127
128 // Unique identifier for the payload. An empty string means that the payload
129 // can't be resumed.
130 string payload_id = (headers[kPayloadPropertyFileHash] +
131 headers[kPayloadPropertyMetadataHash]);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800132
133 // Setup the InstallPlan based on the request.
134 install_plan_ = InstallPlan();
135
136 install_plan_.download_url = payload_url;
137 install_plan_.version = "";
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800138 base_offset_ = payload_offset;
Alex Deymo218397f2016-02-04 23:55:10 -0800139 install_plan_.payload_size = payload_size;
140 if (!install_plan_.payload_size) {
141 if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
142 &install_plan_.payload_size)) {
143 install_plan_.payload_size = 0;
144 }
145 }
146 install_plan_.payload_hash = headers[kPayloadPropertyFileHash];
147 if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize],
148 &install_plan_.metadata_size)) {
149 install_plan_.metadata_size = 0;
150 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800151 install_plan_.metadata_signature = "";
152 // The |public_key_rsa| key would override the public key stored on disk.
153 install_plan_.public_key_rsa = "";
154
155 install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
156 install_plan_.is_resume = !payload_id.empty() &&
157 DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
158 if (!install_plan_.is_resume) {
159 if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
160 LOG(WARNING) << "Unable to reset the update progress.";
161 }
162 if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
163 LOG(WARNING) << "Unable to save the update check response hash.";
164 }
165 }
Alex Deymo64d98782016-02-05 18:03:48 -0800166 // The |payload_type| is not used anymore since minor_version 3.
167 install_plan_.payload_type = InstallPayloadType::kUnknown;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800168
169 install_plan_.source_slot = boot_control_->GetCurrentSlot();
170 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
Alex Deymofb905d92016-06-03 19:26:58 -0700171
172 int data_wipe = 0;
173 install_plan_.powerwash_required =
174 base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) &&
175 data_wipe != 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800176
177 LOG(INFO) << "Using this install plan:";
178 install_plan_.Dump();
179
180 BuildUpdateActions();
181 SetupDownload();
Alex Deymofdd6dec2016-03-03 22:35:43 -0800182 // Setup extra headers.
183 HttpFetcher* fetcher = download_action_->http_fetcher();
184 if (!headers[kPayloadPropertyAuthorization].empty())
185 fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
186 if (!headers[kPayloadPropertyUserAgent].empty())
187 fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
188
Alex Deymo5e3ea272016-01-28 13:42:23 -0800189 cpu_limiter_.StartLimiter();
190 SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
Alex Deymof2858572016-02-25 11:20:13 -0800191 ongoing_update_ = true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800192
193 // Just in case we didn't update boot flags yet, make sure they're updated
194 // before any update processing starts. This will start the update process.
195 UpdateBootFlags();
196 return true;
197}
198
199bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800200 if (!ongoing_update_)
201 return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
202 processor_->SuspendProcessing();
203 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800204}
205
206bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800207 if (!ongoing_update_)
208 return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
209 processor_->ResumeProcessing();
210 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800211}
212
213bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
Alex Deymof2858572016-02-25 11:20:13 -0800214 if (!ongoing_update_)
Alex Deymo5e3ea272016-01-28 13:42:23 -0800215 return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
Alex Deymof2858572016-02-25 11:20:13 -0800216 processor_->StopProcessing();
217 return true;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800218}
219
Alex Deymo3b678db2016-02-09 11:50:06 -0800220bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
221 LOG(INFO) << "Attempting to reset state from "
222 << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
223
224 switch (status_) {
225 case UpdateStatus::IDLE:
226 return true;
227
228 case UpdateStatus::UPDATED_NEED_REBOOT: {
229 // Remove the reboot marker so that if the machine is rebooted
230 // after resetting to idle state, it doesn't go back to
231 // UpdateStatus::UPDATED_NEED_REBOOT state.
232 bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
233
234 // Update the boot flags so the current slot has higher priority.
235 if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
236 ret_value = false;
237
238 if (!ret_value) {
239 return LogAndSetError(
240 error,
241 FROM_HERE,
242 "Failed to reset the status to ");
243 }
244
245 SetStatusAndNotify(UpdateStatus::IDLE);
246 LOG(INFO) << "Reset status successful";
247 return true;
248 }
249
250 default:
251 return LogAndSetError(
252 error,
253 FROM_HERE,
254 "Reset not allowed in this state. Cancel the ongoing update first");
255 }
256}
257
Alex Deymo5e3ea272016-01-28 13:42:23 -0800258void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
259 ErrorCode code) {
260 LOG(INFO) << "Processing Done.";
261
262 if (code == ErrorCode::kSuccess) {
263 // Update succeeded.
264 WriteUpdateCompletedMarker();
265 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
266 DeltaPerformer::ResetUpdateProgress(prefs_, false);
267
268 LOG(INFO) << "Update successfully applied, waiting to reboot.";
269 }
270
271 TerminateUpdateAndNotify(code);
272}
273
274void UpdateAttempterAndroid::ProcessingStopped(
275 const ActionProcessor* processor) {
276 TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
277}
278
279void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
280 AbstractAction* action,
281 ErrorCode code) {
282 // Reset download progress regardless of whether or not the download
283 // action succeeded.
284 const string type = action->Type();
285 if (type == DownloadAction::StaticType()) {
Alex Deymo0d298542016-03-30 18:31:49 -0700286 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800287 }
288 if (code != ErrorCode::kSuccess) {
289 // If an action failed, the ActionProcessor will cancel the whole thing.
290 return;
291 }
292 if (type == DownloadAction::StaticType()) {
293 SetStatusAndNotify(UpdateStatus::FINALIZING);
294 }
295}
296
297void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
298 uint64_t bytes_received,
299 uint64_t total) {
Alex Deymo0d298542016-03-30 18:31:49 -0700300 double progress = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800301 if (total)
302 progress = static_cast<double>(bytes_received) / static_cast<double>(total);
Alex Deymo0d298542016-03-30 18:31:49 -0700303 if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
Alex Deymo5e3ea272016-01-28 13:42:23 -0800304 download_progress_ = progress;
305 SetStatusAndNotify(UpdateStatus::DOWNLOADING);
Alex Deymo0d298542016-03-30 18:31:49 -0700306 } else {
307 ProgressUpdate(progress);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800308 }
309}
310
311bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
312 // TODO(deymo): Notify the DownloadAction that it should cancel the update
313 // download.
314 return false;
315}
316
317void UpdateAttempterAndroid::DownloadComplete() {
318 // Nothing needs to be done when the download completes.
319}
320
Alex Deymo0d298542016-03-30 18:31:49 -0700321void UpdateAttempterAndroid::ProgressUpdate(double progress) {
322 // Self throttle based on progress. Also send notifications if progress is
323 // too slow.
324 if (progress == 1.0 ||
325 progress - download_progress_ >= kBroadcastThresholdProgress ||
326 TimeTicks::Now() - last_notify_time_ >=
327 TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
328 download_progress_ = progress;
329 SetStatusAndNotify(status_);
330 }
331}
332
Alex Deymo5e3ea272016-01-28 13:42:23 -0800333void UpdateAttempterAndroid::UpdateBootFlags() {
334 if (updated_boot_flags_) {
335 LOG(INFO) << "Already updated boot flags. Skipping.";
336 CompleteUpdateBootFlags(true);
337 return;
338 }
339 // This is purely best effort.
340 LOG(INFO) << "Marking booted slot as good.";
341 if (!boot_control_->MarkBootSuccessfulAsync(
342 Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
343 base::Unretained(this)))) {
344 LOG(ERROR) << "Failed to mark current boot as successful.";
345 CompleteUpdateBootFlags(false);
346 }
347}
348
349void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
350 updated_boot_flags_ = true;
351 ScheduleProcessingStart();
352}
353
354void UpdateAttempterAndroid::ScheduleProcessingStart() {
355 LOG(INFO) << "Scheduling an action processor start.";
356 brillo::MessageLoop::current()->PostTask(
357 FROM_HERE, Bind([this] { this->processor_->StartProcessing(); }));
358}
359
360void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
361 if (status_ == UpdateStatus::IDLE) {
362 LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
363 return;
364 }
365
366 // Reset cpu shares back to normal.
367 cpu_limiter_.StopLimiter();
Alex Deymo0d298542016-03-30 18:31:49 -0700368 download_progress_ = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800369 actions_.clear();
370 UpdateStatus new_status =
371 (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
372 : UpdateStatus::IDLE);
373 SetStatusAndNotify(new_status);
374 ongoing_update_ = false;
375
376 for (auto observer : daemon_state_->service_observers())
377 observer->SendPayloadApplicationComplete(error_code);
378}
379
380void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
381 status_ = status;
382 for (auto observer : daemon_state_->service_observers()) {
383 observer->SendStatusUpdate(
384 0, download_progress_, status_, "", install_plan_.payload_size);
385 }
386 last_notify_time_ = TimeTicks::Now();
387}
388
389void UpdateAttempterAndroid::BuildUpdateActions() {
390 CHECK(!processor_->IsRunning());
391 processor_->set_delegate(this);
392
393 // Actions:
394 shared_ptr<InstallPlanAction> install_plan_action(
395 new InstallPlanAction(install_plan_));
396
397 LibcurlHttpFetcher* download_fetcher =
398 new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
399 download_fetcher->set_server_to_check(ServerToCheck::kDownload);
400 shared_ptr<DownloadAction> download_action(new DownloadAction(
401 prefs_,
402 boot_control_,
403 hardware_,
404 nullptr, // system_state, not used.
405 new MultiRangeHttpFetcher(download_fetcher))); // passes ownership
Sen Jiangfef85fd2016-03-25 15:32:49 -0700406 shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
Sen Jiange6e4bb92016-04-05 14:59:12 -0700407 new FilesystemVerifierAction());
Alex Deymo5e3ea272016-01-28 13:42:23 -0800408
409 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700410 new PostinstallRunnerAction(boot_control_, hardware_));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800411
412 download_action->set_delegate(this);
413 download_action_ = download_action;
Alex Deymob6eef732016-06-10 12:58:11 -0700414 postinstall_runner_action->set_delegate(this);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800415
416 actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
417 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Sen Jiangfef85fd2016-03-25 15:32:49 -0700418 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
Alex Deymo5e3ea272016-01-28 13:42:23 -0800419 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
420
421 // Bond them together. We have to use the leaf-types when calling
422 // BondActions().
423 BondActions(install_plan_action.get(), download_action.get());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700424 BondActions(download_action.get(), filesystem_verifier_action.get());
425 BondActions(filesystem_verifier_action.get(),
Alex Deymo5e3ea272016-01-28 13:42:23 -0800426 postinstall_runner_action.get());
427
428 // Enqueue the actions.
429 for (const shared_ptr<AbstractAction>& action : actions_)
430 processor_->EnqueueAction(action.get());
431}
432
433void UpdateAttempterAndroid::SetupDownload() {
434 MultiRangeHttpFetcher* fetcher =
435 static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
436 fetcher->ClearRanges();
437 if (install_plan_.is_resume) {
438 // Resuming an update so fetch the update manifest metadata first.
439 int64_t manifest_metadata_size = 0;
Alex Deymof25eb492016-02-26 00:20:08 -0800440 int64_t manifest_signature_size = 0;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800441 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Alex Deymof25eb492016-02-26 00:20:08 -0800442 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
443 fetcher->AddRange(base_offset_,
444 manifest_metadata_size + manifest_signature_size);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800445 // If there're remaining unprocessed data blobs, fetch them. Be careful not
446 // to request data beyond the end of the payload to avoid 416 HTTP response
447 // error codes.
448 int64_t next_data_offset = 0;
449 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Alex Deymof25eb492016-02-26 00:20:08 -0800450 uint64_t resume_offset =
451 manifest_metadata_size + manifest_signature_size + next_data_offset;
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800452 if (!install_plan_.payload_size) {
453 fetcher->AddRange(base_offset_ + resume_offset);
454 } else if (resume_offset < install_plan_.payload_size) {
455 fetcher->AddRange(base_offset_ + resume_offset,
456 install_plan_.payload_size - resume_offset);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800457 }
458 } else {
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800459 if (install_plan_.payload_size) {
460 fetcher->AddRange(base_offset_, install_plan_.payload_size);
461 } else {
462 // If no payload size is passed we assume we read until the end of the
463 // stream.
464 fetcher->AddRange(base_offset_);
465 }
Alex Deymo5e3ea272016-01-28 13:42:23 -0800466 }
467}
468
469bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
470 string boot_id;
471 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
472 prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
473 return true;
474}
475
476bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
477 // In case of an update_engine restart without a reboot, we stored the boot_id
478 // when the update was completed by setting a pref, so we can check whether
479 // the last update was on this boot or a previous one.
480 string boot_id;
481 TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
482
483 string update_completed_on_boot_id;
484 return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
485 prefs_->GetString(kPrefsUpdateCompletedOnBootId,
486 &update_completed_on_boot_id) &&
487 update_completed_on_boot_id == boot_id);
488}
489
490} // namespace chromeos_update_engine