blob: e5ee828c4d63f93adab9e8d0cae489fe77a9ee02 [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
Casey Dahlina93cd532016-01-14 16:55:11 -080029#include "update_engine/common/hardware_interface.h"
30#include "update_engine/common/prefs.h"
Amin Hassani538bd592020-11-04 20:46:08 -080031#include "update_engine/common/system_state.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080032#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070033#include "update_engine/cros/connection_manager_interface.h"
34#include "update_engine/cros/omaha_request_params.h"
35#include "update_engine/cros/omaha_utils.h"
36#include "update_engine/cros/p2p_manager.h"
37#include "update_engine/cros/payload_state_interface.h"
38#include "update_engine/cros/update_attempter.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080039
40using base::StringPrintf;
41using brillo::ErrorPtr;
42using brillo::string_utils::ToString;
Casey Dahlina93cd532016-01-14 16:55:11 -080043using std::string;
Xiaochu Liu88d90382018-08-29 16:09:11 -070044using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070045using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070046using update_engine::UpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080047
48namespace chromeos_update_engine {
49
50namespace {
51// Log and set the error on the passed ErrorPtr.
52void LogAndSetError(ErrorPtr* error,
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070053 const base::Location& location,
Casey Dahlina93cd532016-01-14 16:55:11 -080054 const string& reason) {
55 brillo::Error::AddTo(error,
56 location,
57 UpdateEngineService::kErrorDomain,
58 UpdateEngineService::kErrorFailed,
59 reason);
60 LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
61 << reason;
62}
63} // namespace
64
65const char* const UpdateEngineService::kErrorDomain = "update_engine";
66const char* const UpdateEngineService::kErrorFailed =
67 "org.chromium.UpdateEngine.Error.Failed";
68
Amin Hassani538bd592020-11-04 20:46:08 -080069UpdateEngineService::UpdateEngineService() = default;
Casey Dahlina93cd532016-01-14 16:55:11 -080070
71// org::chromium::UpdateEngineInterfaceInterface methods implementation.
72
Aaron Woodbf5a2522017-10-04 10:58:36 -070073bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
74 int32_t in_flags_as_int) {
75 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
76 LOG(INFO) << "Setting Update Attempt Flags: "
77 << "flags=0x" << std::hex << flags << " "
78 << "RestrictDownload="
79 << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
80 : "no");
Amin Hassani538bd592020-11-04 20:46:08 -080081 SystemState::Get()->update_attempter()->SetUpdateAttemptFlags(flags);
Aaron Woodbf5a2522017-10-04 10:58:36 -070082 return true;
83}
84
Casey Dahlina93cd532016-01-14 16:55:11 -080085bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
86 const string& in_app_version,
87 const string& in_omaha_url,
Aaron Wood081c0232017-10-19 17:14:58 -070088 int32_t in_flags_as_int,
89 bool* out_result) {
Aaron Woodbf5a2522017-10-04 10:58:36 -070090 auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
91 bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
Aaron Wood081c0232017-10-19 17:14:58 -070092 bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
Casey Dahlina93cd532016-01-14 16:55:11 -080093
94 LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
95 << "omaha_url=\"" << in_omaha_url << "\" "
96 << "flags=0x" << std::hex << flags << " "
Aaron Wood081c0232017-10-19 17:14:58 -070097 << "interactive=" << (interactive ? "yes " : "no ")
98 << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
99
Amin Hassani538bd592020-11-04 20:46:08 -0800100 *out_result = SystemState::Get()->update_attempter()->CheckForUpdate(
Aaron Wood081c0232017-10-19 17:14:58 -0700101 in_app_version, in_omaha_url, flags);
Casey Dahlina93cd532016-01-14 16:55:11 -0800102 return true;
103}
104
Xiaochu Liu88d90382018-08-29 16:09:11 -0700105bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
106 const string& omaha_url,
Amin Hassani2b68e6b2020-04-17 10:49:12 -0700107 const vector<string>& dlc_ids) {
Amin Hassani538bd592020-11-04 20:46:08 -0800108 if (!SystemState::Get()->update_attempter()->CheckForInstall(dlc_ids,
109 omaha_url)) {
Xiaochu Liu88d90382018-08-29 16:09:11 -0700110 // TODO(xiaochu): support more detailed error messages.
111 LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
112 return false;
113 }
114 return true;
115}
116
Casey Dahlina93cd532016-01-14 16:55:11 -0800117bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
118 LOG(INFO) << "Attempting rollback to non-active partitions.";
119
Amin Hassani538bd592020-11-04 20:46:08 -0800120 if (!SystemState::Get()->update_attempter()->Rollback(in_powerwash)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800121 // TODO(dgarrett): Give a more specific error code/reason.
122 LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
123 return false;
124 }
125 return true;
126}
127
128bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
129 bool* out_can_rollback) {
Amin Hassani538bd592020-11-04 20:46:08 -0800130 bool can_rollback = SystemState::Get()->update_attempter()->CanRollback();
Casey Dahlina93cd532016-01-14 16:55:11 -0800131 LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
132 *out_can_rollback = can_rollback;
133 return true;
134}
135
136bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
Amin Hassani538bd592020-11-04 20:46:08 -0800137 if (!SystemState::Get()->update_attempter()->ResetStatus()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800138 // TODO(dgarrett): Give a more specific error code/reason.
139 LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
140 return false;
141 }
142 return true;
143}
144
Andrewa8d7df32020-03-15 20:10:01 -0700145bool UpdateEngineService::SetDlcActiveValue(brillo::ErrorPtr* error,
146 bool is_active,
147 const string& dlc_id) {
Amin Hassani538bd592020-11-04 20:46:08 -0800148 if (!SystemState::Get()->update_attempter()->SetDlcActiveValue(is_active,
149 dlc_id)) {
Andrewa8d7df32020-03-15 20:10:01 -0700150 LogAndSetError(error, FROM_HERE, "SetDlcActiveValue failed.");
151 return false;
152 }
153 return true;
154}
155
Casey Dahlina93cd532016-01-14 16:55:11 -0800156bool UpdateEngineService::GetStatus(ErrorPtr* error,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700157 UpdateEngineStatus* out_status) {
Amin Hassani538bd592020-11-04 20:46:08 -0800158 if (!SystemState::Get()->update_attempter()->GetStatus(out_status)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800159 LogAndSetError(error, FROM_HERE, "GetStatus failed.");
160 return false;
161 }
162 return true;
163}
164
165bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
Amin Hassani538bd592020-11-04 20:46:08 -0800166 if (!SystemState::Get()->update_attempter()->RebootIfNeeded()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800167 // TODO(dgarrett): Give a more specific error code/reason.
168 LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
169 return false;
170 }
171 return true;
172}
173
174bool UpdateEngineService::SetChannel(ErrorPtr* error,
175 const string& in_target_channel,
176 bool in_is_powerwash_allowed) {
Amin Hassani538bd592020-11-04 20:46:08 -0800177 const policy::DevicePolicy* device_policy =
178 SystemState::Get()->device_policy();
Casey Dahlina93cd532016-01-14 16:55:11 -0800179
180 // The device_policy is loaded in a lazy way before an update check. Load it
181 // now from the libbrillo cache if it wasn't already loaded.
182 if (!device_policy) {
Amin Hassani538bd592020-11-04 20:46:08 -0800183 UpdateAttempter* update_attempter = SystemState::Get()->update_attempter();
Casey Dahlina93cd532016-01-14 16:55:11 -0800184 if (update_attempter) {
185 update_attempter->RefreshDevicePolicy();
Amin Hassani538bd592020-11-04 20:46:08 -0800186 device_policy = SystemState::Get()->device_policy();
Casey Dahlina93cd532016-01-14 16:55:11 -0800187 }
188 }
189
190 bool delegated = false;
191 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
192 !delegated) {
193 LogAndSetError(error,
194 FROM_HERE,
195 "Cannot set target channel explicitly when channel "
196 "policy/settings is not delegated");
197 return false;
198 }
199
200 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
201 string error_message;
Amin Hassani538bd592020-11-04 20:46:08 -0800202 if (!SystemState::Get()->request_params()->SetTargetChannel(
Casey Dahlina93cd532016-01-14 16:55:11 -0800203 in_target_channel, in_is_powerwash_allowed, &error_message)) {
204 LogAndSetError(error, FROM_HERE, error_message);
205 return false;
206 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800207 return true;
208}
209
210bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
211 bool in_get_current_channel,
212 string* out_channel) {
Amin Hassani538bd592020-11-04 20:46:08 -0800213 OmahaRequestParams* rp = SystemState::Get()->request_params();
Casey Dahlina93cd532016-01-14 16:55:11 -0800214 *out_channel =
215 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
216 return true;
217}
218
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700219bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
Andrew9d5a61d2020-03-26 13:40:37 -0700220 const string& in_cohort_hint) {
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700221 // It is ok to override the cohort hint with an invalid value since it is
222 // stored in stateful partition. The code reading it should sanitize it
223 // anyway.
Amin Hassani90e9f192020-11-18 14:20:56 -0800224 if (!SystemState::Get()->prefs()->SetString(kPrefsOmahaCohortHint,
225 in_cohort_hint)) {
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700226 LogAndSetError(
227 error,
228 FROM_HERE,
229 StringPrintf("Error setting the cohort hint value to \"%s\".",
230 in_cohort_hint.c_str()));
231 return false;
232 }
233 return true;
234}
235
236bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
237 string* out_cohort_hint) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800238 const auto* prefs = SystemState::Get()->prefs();
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700239 *out_cohort_hint = "";
240 if (prefs->Exists(kPrefsOmahaCohortHint) &&
241 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
242 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
243 return false;
244 }
245 return true;
246}
247
Casey Dahlina93cd532016-01-14 16:55:11 -0800248bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
249 bool in_enabled) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800250 if (!SystemState::Get()->prefs()->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800251 LogAndSetError(
252 error,
253 FROM_HERE,
254 StringPrintf("Error setting the update via p2p permission to %s.",
255 ToString(in_enabled).c_str()));
256 return false;
257 }
258 return true;
259}
260
261bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
262 bool* out_enabled) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800263 const auto* prefs = SystemState::Get()->prefs();
Casey Dahlina93cd532016-01-14 16:55:11 -0800264 bool p2p_pref = false; // Default if no setting is present.
265 if (prefs->Exists(kPrefsP2PEnabled) &&
266 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
267 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
268 return false;
269 }
270
271 *out_enabled = p2p_pref;
272 return true;
273}
274
275bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
276 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700277 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800278 SystemState::Get()->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800279
280 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700281 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700282 LogAndSetError(error,
283 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800284 "Ignoring the update over cellular setting since there's "
285 "a device policy enforcing this setting.");
286 return false;
287 }
288
289 // If the policy wasn't loaded yet, then it is still OK to change the local
290 // setting because the policy will be checked again during the update check.
Amin Hassani90e9f192020-11-18 14:20:56 -0800291 if (!SystemState::Get()->prefs()->SetBoolean(
292 kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700293 LogAndSetError(error,
294 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800295 string("Error setting the update over cellular to ") +
296 (in_allowed ? "true" : "false"));
297 return false;
298 }
299 return true;
300}
301
Weidong Guo421ff332017-04-17 10:08:38 -0700302bool UpdateEngineService::SetUpdateOverCellularTarget(
303 brillo::ErrorPtr* error,
304 const std::string& target_version,
305 int64_t target_size) {
306 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800307 SystemState::Get()->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700308
Weidong Guo421ff332017-04-17 10:08:38 -0700309 // Check if this setting is allowed by the device policy.
310 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
311 LogAndSetError(error,
312 FROM_HERE,
313 "Ignoring the update over cellular setting since there's "
314 "a device policy enforcing this setting.");
315 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700316 }
Tao Bao5688d162017-06-06 13:09:06 -0700317
Weidong Guo421ff332017-04-17 10:08:38 -0700318 // If the policy wasn't loaded yet, then it is still OK to change the local
319 // setting because the policy will be checked again during the update check.
320
Amin Hassani90e9f192020-11-18 14:20:56 -0800321 auto* prefs = SystemState::Get()->prefs();
322 if (!prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
Weidong Guo421ff332017-04-17 10:08:38 -0700323 target_version) ||
324 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
325 LogAndSetError(
326 error, FROM_HERE, "Error setting the target for update over cellular.");
327 return false;
328 }
329 return true;
330}
331
332bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
333 bool* out_allowed) {
334 ConnectionManagerInterface* connection_manager =
Amin Hassani538bd592020-11-04 20:46:08 -0800335 SystemState::Get()->connection_manager();
Weidong Guo421ff332017-04-17 10:08:38 -0700336
337 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
338 // We have device policy, so ignore the user preferences.
339 *out_allowed = connection_manager->IsUpdateAllowedOver(
340 ConnectionType::kCellular, ConnectionTethering::kUnknown);
341 } else {
Amin Hassani90e9f192020-11-18 14:20:56 -0800342 const auto* prefs = SystemState::Get()->prefs();
343 if (!prefs->Exists(kPrefsUpdateOverCellularPermission)) {
Weidong Guo421ff332017-04-17 10:08:38 -0700344 // Update is not allowed as user preference is not set or not available.
345 *out_allowed = false;
346 return true;
347 }
348
349 bool is_allowed;
350
351 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
352 LogAndSetError(error,
353 FROM_HERE,
354 "Error getting the update over cellular preference.");
355 return false;
356 }
357 *out_allowed = is_allowed;
358 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800359 return true;
360}
361
362bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
363 int64_t* out_usec_wallclock) {
364 base::Time time;
Amin Hassani538bd592020-11-04 20:46:08 -0800365 if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(&time)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800366 LogAndSetError(error, FROM_HERE, "No pending update.");
367 return false;
368 }
369
Amin Hassani0468a762020-11-17 23:53:48 -0800370 const auto* clock = SystemState::Get()->clock();
Casey Dahlina93cd532016-01-14 16:55:11 -0800371 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
372 return true;
373}
374
375bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
376 string* out_prev_version) {
Amin Hassani538bd592020-11-04 20:46:08 -0800377 *out_prev_version = SystemState::Get()->update_attempter()->GetPrevVersion();
Casey Dahlina93cd532016-01-14 16:55:11 -0800378 return true;
379}
380
381bool UpdateEngineService::GetRollbackPartition(
382 ErrorPtr* /* error */, string* out_rollback_partition_name) {
383 BootControlInterface::Slot rollback_slot =
Amin Hassani538bd592020-11-04 20:46:08 -0800384 SystemState::Get()->update_attempter()->GetRollbackSlot();
Casey Dahlina93cd532016-01-14 16:55:11 -0800385
386 if (rollback_slot == BootControlInterface::kInvalidSlot) {
387 out_rollback_partition_name->clear();
388 return true;
389 }
390
391 string name;
Amin Hassani538bd592020-11-04 20:46:08 -0800392 if (!SystemState::Get()->boot_control()->GetPartitionDevice(
Casey Dahlina93cd532016-01-14 16:55:11 -0800393 "KERNEL", rollback_slot, &name)) {
394 LOG(ERROR) << "Invalid rollback device";
395 return false;
396 }
397
398 LOG(INFO) << "Getting rollback partition name. Result: " << name;
399 *out_rollback_partition_name = name;
400 return true;
401}
402
Shuqian Zhao29971732016-02-05 11:29:32 -0800403bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
404 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700405 ErrorCode error_code =
Amin Hassani538bd592020-11-04 20:46:08 -0800406 SystemState::Get()->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800407 *out_last_attempt_error = static_cast<int>(error_code);
408 return true;
409}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700410
Casey Dahlina93cd532016-01-14 16:55:11 -0800411} // namespace chromeos_update_engine