blob: 0318999cab9c2514ff75e01e4bee69811f854fd4 [file] [log] [blame]
Casey Dahlina93cd532016-01-14 16:55:11 -08001//
2// Copyright (C) 2012 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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/common_service.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080018
Casey Dahlina93cd532016-01-14 16:55:11 -080019#include <string>
20
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090021#include <base/bind.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080022#include <base/location.h>
23#include <base/logging.h>
24#include <base/strings/stringprintf.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080025#include <brillo/message_loops/message_loop.h>
26#include <brillo/strings/string_utils.h>
27#include <policy/device_policy.h>
28
29#include "update_engine/common/clock_interface.h"
30#include "update_engine/common/hardware_interface.h"
31#include "update_engine/common/prefs.h"
Amin Hassani538bd592020-11-04 20:46:08 -080032#include "update_engine/common/system_state.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080033#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070034#include "update_engine/cros/connection_manager_interface.h"
35#include "update_engine/cros/omaha_request_params.h"
36#include "update_engine/cros/omaha_utils.h"
37#include "update_engine/cros/p2p_manager.h"
38#include "update_engine/cros/payload_state_interface.h"
39#include "update_engine/cros/update_attempter.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080040
41using base::StringPrintf;
42using brillo::ErrorPtr;
43using brillo::string_utils::ToString;
Casey Dahlina93cd532016-01-14 16:55:11 -080044using std::string;
Xiaochu Liu88d90382018-08-29 16:09:11 -070045using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070046using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070047using update_engine::UpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080048
49namespace chromeos_update_engine {
50
51namespace {
52// Log and set the error on the passed ErrorPtr.
53void LogAndSetError(ErrorPtr* error,
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070054 const base::Location& location,
Casey Dahlina93cd532016-01-14 16:55:11 -080055 const string& reason) {
56 brillo::Error::AddTo(error,
57 location,
58 UpdateEngineService::kErrorDomain,
59 UpdateEngineService::kErrorFailed,
60 reason);
61 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
62 << reason;
63}
64} // namespace
65
66const char* const UpdateEngineService::kErrorDomain = "update_engine";
67const char* const UpdateEngineService::kErrorFailed =
68 "org.chromium.UpdateEngine.Error.Failed";
69
Amin Hassani538bd592020-11-04 20:46:08 -080070UpdateEngineService::UpdateEngineService() = default;
Casey Dahlina93cd532016-01-14 16:55:11 -080071
72// org::chromium::UpdateEngineInterfaceInterface methods implementation.
73
Aaron Woodbf5a2522017-10-04 10:58:36 -070074bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
75 int32_t in_flags_as_int) {
76 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
77 LOG(INFO) << "Setting Update Attempt Flags: "
78 << "flags=0x" << std::hex << flags << " "
79 << "RestrictDownload="
80 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
81 : "no");
Amin Hassani538bd592020-11-04 20:46:08 -080082 SystemState::Get()->update_attempter()->SetUpdateAttemptFlags(flags);
Aaron Woodbf5a2522017-10-04 10:58:36 -070083 return true;
84}
85
Casey Dahlina93cd532016-01-14 16:55:11 -080086bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
87 const string& in_app_version,
88 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070089 int32_t in_flags_as_int,
90 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070091 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
92 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070093 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080094
95 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
96 << "omaha_url=\"" << in_omaha_url << "\" "
97 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -070098 << "interactive=" << (interactive ? "yes " : "no ")
99 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
100
Amin Hassani538bd592020-11-04 20:46:08 -0800101 *out_result = SystemState::Get()->update_attempter()->CheckForUpdate(
Aaron Wood081c0232017-10-19 17:14:58 -0700102 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800103 return true;
104}
105
Xiaochu Liu88d90382018-08-29 16:09:11 -0700106bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
107 const string& omaha_url,
Amin Hassani2b68e6b2020-04-17 10:49:12 -0700108 const vector<string>& dlc_ids) {
Amin Hassani538bd592020-11-04 20:46:08 -0800109 if (!SystemState::Get()->update_attempter()->CheckForInstall(dlc_ids,
110 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700111 // TODO(xiaochu): support more detailed error messages.
112 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
113 return false;
114 }
115 return true;
116}
117
Casey Dahlina93cd532016-01-14 16:55:11 -0800118bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
119 LOG(INFO) << "Attempting rollback to non-active partitions.";
120
Amin Hassani538bd592020-11-04 20:46:08 -0800121 if (!SystemState::Get()->update_attempter()->Rollback(in_powerwash)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800122 // TODO(dgarrett): Give a more specific error code/reason.
123 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
124 return false;
125 }
126 return true;
127}
128
129bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
130 bool* out_can_rollback) {
Amin Hassani538bd592020-11-04 20:46:08 -0800131 bool can_rollback = SystemState::Get()->update_attempter()->CanRollback();
Casey Dahlina93cd532016-01-14 16:55:11 -0800132 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
133 *out_can_rollback = can_rollback;
134 return true;
135}
136
137bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
Amin Hassani538bd592020-11-04 20:46:08 -0800138 if (!SystemState::Get()->update_attempter()->ResetStatus()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800139 // TODO(dgarrett): Give a more specific error code/reason.
140 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
141 return false;
142 }
143 return true;
144}
145
Andrewa8d7df32020-03-15 20:10:01 -0700146bool UpdateEngineService::SetDlcActiveValue(brillo::ErrorPtr* error,
147 bool is_active,
148 const string& dlc_id) {
Amin Hassani538bd592020-11-04 20:46:08 -0800149 if (!SystemState::Get()->update_attempter()->SetDlcActiveValue(is_active,
150 dlc_id)) {
Andrewa8d7df32020-03-15 20:10:01 -0700151 LogAndSetError(error, FROM_HERE, "SetDlcActiveValue failed.");
152 return false;
153 }
154 return true;
155}
156
Casey Dahlina93cd532016-01-14 16:55:11 -0800157bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700158 UpdateEngineStatus* out_status) {
Amin Hassani538bd592020-11-04 20:46:08 -0800159 if (!SystemState::Get()->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800160 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
161 return false;
162 }
163 return true;
164}
165
166bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
Amin Hassani538bd592020-11-04 20:46:08 -0800167 if (!SystemState::Get()->update_attempter()->RebootIfNeeded()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800168 // TODO(dgarrett): Give a more specific error code/reason.
169 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
170 return false;
171 }
172 return true;
173}
174
175bool UpdateEngineService::SetChannel(ErrorPtr* error,
176 const string& in_target_channel,
177 bool in_is_powerwash_allowed) {
Amin Hassani538bd592020-11-04 20:46:08 -0800178 const policy::DevicePolicy* device_policy =
179 SystemState::Get()->device_policy();
Casey Dahlina93cd532016-01-14 16:55:11 -0800180
181 // The device_policy is loaded in a lazy way before an update check. Load it
182 // now from the libbrillo cache if it wasn't already loaded.
183 if (!device_policy) {
Amin Hassani538bd592020-11-04 20:46:08 -0800184 UpdateAttempter* update_attempter = SystemState::Get()->update_attempter();
Casey Dahlina93cd532016-01-14 16:55:11 -0800185 if (update_attempter) {
186 update_attempter->RefreshDevicePolicy();
Amin Hassani538bd592020-11-04 20:46:08 -0800187 device_policy = SystemState::Get()->device_policy();
Casey Dahlina93cd532016-01-14 16:55:11 -0800188 }
189 }
190
191 bool delegated = false;
192 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
193 !delegated) {
194 LogAndSetError(error,
195 FROM_HERE,
196 "Cannot set target channel explicitly when channel "
197 "policy/settings is not delegated");
198 return false;
199 }
200
201 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
202 string error_message;
Amin Hassani538bd592020-11-04 20:46:08 -0800203 if (!SystemState::Get()->request_params()->SetTargetChannel(
Casey Dahlina93cd532016-01-14 16:55:11 -0800204 in_target_channel, in_is_powerwash_allowed, &error_message)) {
205 LogAndSetError(error, FROM_HERE, error_message);
206 return false;
207 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800208 return true;
209}
210
211bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
212 bool in_get_current_channel,
213 string* out_channel) {
Amin Hassani538bd592020-11-04 20:46:08 -0800214 OmahaRequestParams* rp = SystemState::Get()->request_params();
Casey Dahlina93cd532016-01-14 16:55:11 -0800215 *out_channel =
216 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
217 return true;
218}
219
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700220bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
Andrew9d5a61d2020-03-26 13:40:37 -0700221 const string& in_cohort_hint) {
Amin Hassani538bd592020-11-04 20:46:08 -0800222 PrefsInterface* prefs = SystemState::Get()->prefs();
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700223
224 // It is ok to override the cohort hint with an invalid value since it is
225 // stored in stateful partition. The code reading it should sanitize it
226 // anyway.
227 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
228 LogAndSetError(
229 error,
230 FROM_HERE,
231 StringPrintf("Error setting the cohort hint value to \"%s\".",
232 in_cohort_hint.c_str()));
233 return false;
234 }
235 return true;
236}
237
238bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
239 string* out_cohort_hint) {
Amin Hassani538bd592020-11-04 20:46:08 -0800240 PrefsInterface* prefs = SystemState::Get()->prefs();
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700241
242 *out_cohort_hint = "";
243 if (prefs->Exists(kPrefsOmahaCohortHint) &&
244 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
245 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
246 return false;
247 }
248 return true;
249}
250
Casey Dahlina93cd532016-01-14 16:55:11 -0800251bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
252 bool in_enabled) {
Amin Hassani538bd592020-11-04 20:46:08 -0800253 PrefsInterface* prefs = SystemState::Get()->prefs();
Casey Dahlina93cd532016-01-14 16:55:11 -0800254
255 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
256 LogAndSetError(
257 error,
258 FROM_HERE,
259 StringPrintf("Error setting the update via p2p permission to %s.",
260 ToString(in_enabled).c_str()));
261 return false;
262 }
263 return true;
264}
265
266bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
267 bool* out_enabled) {
Amin Hassani538bd592020-11-04 20:46:08 -0800268 PrefsInterface* prefs = SystemState::Get()->prefs();
Casey Dahlina93cd532016-01-14 16:55:11 -0800269
270 bool p2p_pref = false; // Default if no setting is present.
271 if (prefs->Exists(kPrefsP2PEnabled) &&
272 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
273 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
274 return false;
275 }
276
277 *out_enabled = p2p_pref;
278 return true;
279}
280
281bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
282 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700283 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800284 SystemState::Get()->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800285
286 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700287 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700288 LogAndSetError(error,
289 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800290 "Ignoring the update over cellular setting since there's "
291 "a device policy enforcing this setting.");
292 return false;
293 }
294
295 // If the policy wasn't loaded yet, then it is still OK to change the local
296 // setting because the policy will be checked again during the update check.
297
Amin Hassani538bd592020-11-04 20:46:08 -0800298 PrefsInterface* prefs = SystemState::Get()->prefs();
Casey Dahlina93cd532016-01-14 16:55:11 -0800299
Weidong Guo421ff332017-04-17 10:08:38 -0700300 if (!prefs ||
301 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700302 LogAndSetError(error,
303 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800304 string("Error setting the update over cellular to ") +
305 (in_allowed ? "true" : "false"));
306 return false;
307 }
308 return true;
309}
310
Weidong Guo421ff332017-04-17 10:08:38 -0700311bool UpdateEngineService::SetUpdateOverCellularTarget(
312 brillo::ErrorPtr* error,
313 const std::string& target_version,
314 int64_t target_size) {
315 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800316 SystemState::Get()->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700317
Weidong Guo421ff332017-04-17 10:08:38 -0700318 // Check if this setting is allowed by the device policy.
319 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
320 LogAndSetError(error,
321 FROM_HERE,
322 "Ignoring the update over cellular setting since there's "
323 "a device policy enforcing this setting.");
324 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700325 }
Tao Bao5688d162017-06-06 13:09:06 -0700326
Weidong Guo421ff332017-04-17 10:08:38 -0700327 // If the policy wasn't loaded yet, then it is still OK to change the local
328 // setting because the policy will be checked again during the update check.
329
Amin Hassani538bd592020-11-04 20:46:08 -0800330 PrefsInterface* prefs = SystemState::Get()->prefs();
Weidong Guo421ff332017-04-17 10:08:38 -0700331
332 if (!prefs ||
333 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
334 target_version) ||
335 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
336 LogAndSetError(
337 error, FROM_HERE, "Error setting the target for update over cellular.");
338 return false;
339 }
340 return true;
341}
342
343bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
344 bool* out_allowed) {
345 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800346 SystemState::Get()->connection_manager();
Weidong Guo421ff332017-04-17 10:08:38 -0700347
348 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
349 // We have device policy, so ignore the user preferences.
350 *out_allowed = connection_manager->IsUpdateAllowedOver(
351 ConnectionType::kCellular, ConnectionTethering::kUnknown);
352 } else {
Amin Hassani538bd592020-11-04 20:46:08 -0800353 PrefsInterface* prefs = SystemState::Get()->prefs();
Weidong Guo421ff332017-04-17 10:08:38 -0700354
355 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
356 // Update is not allowed as user preference is not set or not available.
357 *out_allowed = false;
358 return true;
359 }
360
361 bool is_allowed;
362
363 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
364 LogAndSetError(error,
365 FROM_HERE,
366 "Error getting the update over cellular preference.");
367 return false;
368 }
369 *out_allowed = is_allowed;
370 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800371 return true;
372}
373
374bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
375 int64_t* out_usec_wallclock) {
376 base::Time time;
Amin Hassani538bd592020-11-04 20:46:08 -0800377 if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(&time)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800378 LogAndSetError(error, FROM_HERE, "No pending update.");
379 return false;
380 }
381
Amin Hassani538bd592020-11-04 20:46:08 -0800382 ClockInterface* clock = SystemState::Get()->clock();
Casey Dahlina93cd532016-01-14 16:55:11 -0800383 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
384 return true;
385}
386
387bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
388 string* out_prev_version) {
Amin Hassani538bd592020-11-04 20:46:08 -0800389 *out_prev_version = SystemState::Get()->update_attempter()->GetPrevVersion();
Casey Dahlina93cd532016-01-14 16:55:11 -0800390 return true;
391}
392
393bool UpdateEngineService::GetRollbackPartition(
394 ErrorPtr* /* error */, string* out_rollback_partition_name) {
395 BootControlInterface::Slot rollback_slot =
Amin Hassani538bd592020-11-04 20:46:08 -0800396 SystemState::Get()->update_attempter()->GetRollbackSlot();
Casey Dahlina93cd532016-01-14 16:55:11 -0800397
398 if (rollback_slot == BootControlInterface::kInvalidSlot) {
399 out_rollback_partition_name->clear();
400 return true;
401 }
402
403 string name;
Amin Hassani538bd592020-11-04 20:46:08 -0800404 if (!SystemState::Get()->boot_control()->GetPartitionDevice(
Casey Dahlina93cd532016-01-14 16:55:11 -0800405 "KERNEL", rollback_slot, &name)) {
406 LOG(ERROR) << "Invalid rollback device";
407 return false;
408 }
409
410 LOG(INFO) << "Getting rollback partition name. Result: " << name;
411 *out_rollback_partition_name = name;
412 return true;
413}
414
Shuqian Zhao29971732016-02-05 11:29:32 -0800415bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
416 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700417 ErrorCode error_code =
Amin Hassani538bd592020-11-04 20:46:08 -0800418 SystemState::Get()->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800419 *out_last_attempt_error = static_cast<int>(error_code);
420 return true;
421}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700422
Casey Dahlina93cd532016-01-14 16:55:11 -0800423} // namespace chromeos_update_engine