blob: 347833b4414d1dafd51e38a6b8e6b4183bbabc55 [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
17#include "update_engine/common_service.h"
18
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"
32#include "update_engine/common/utils.h"
33#include "update_engine/connection_manager_interface.h"
34#include "update_engine/omaha_request_params.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070035#include "update_engine/omaha_utils.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080036#include "update_engine/p2p_manager.h"
Shuqian Zhao29971732016-02-05 11:29:32 -080037#include "update_engine/payload_state_interface.h"
Alex Deymob3fa53b2016-04-18 19:57:58 -070038#include "update_engine/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
69UpdateEngineService::UpdateEngineService(SystemState* system_state)
Amin Hassani7cc8bb02019-01-14 16:29:47 -080070 : system_state_(system_state) {}
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");
82 system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
83 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
101 *out_result = system_state_->update_attempter()->CheckForUpdate(
102 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,
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800108 const vector<string>& dlc_module_ids) {
109 if (!system_state_->update_attempter()->CheckForInstall(dlc_module_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
121 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
122 // 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) {
131 bool can_rollback = system_state_->update_attempter()->CanRollback();
132 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) {
138 if (!system_state_->update_attempter()->ResetStatus()) {
139 // 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) {
149 if (!system_state_->update_attempter()->SetDlcActiveValue(is_active,
150 dlc_id)) {
151 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) {
159 if (!system_state_->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) {
167 if (!system_state_->update_attempter()->RebootIfNeeded()) {
168 // 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) {
178 const policy::DevicePolicy* device_policy = system_state_->device_policy();
179
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) {
183 UpdateAttempter* update_attempter = system_state_->update_attempter();
184 if (update_attempter) {
185 update_attempter->RefreshDevicePolicy();
186 device_policy = system_state_->device_policy();
187 }
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;
202 if (!system_state_->request_params()->SetTargetChannel(
203 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) {
213 OmahaRequestParams* rp = system_state_->request_params();
214 *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 PrefsInterface* prefs = system_state_->prefs();
222
223 // It is ok to override the cohort hint with an invalid value since it is
224 // stored in stateful partition. The code reading it should sanitize it
225 // anyway.
226 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
227 LogAndSetError(
228 error,
229 FROM_HERE,
230 StringPrintf("Error setting the cohort hint value to \"%s\".",
231 in_cohort_hint.c_str()));
232 return false;
233 }
234 return true;
235}
236
237bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
238 string* out_cohort_hint) {
239 PrefsInterface* prefs = system_state_->prefs();
240
241 *out_cohort_hint = "";
242 if (prefs->Exists(kPrefsOmahaCohortHint) &&
243 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
244 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
245 return false;
246 }
247 return true;
248}
249
Casey Dahlina93cd532016-01-14 16:55:11 -0800250bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
251 bool in_enabled) {
252 PrefsInterface* prefs = system_state_->prefs();
253
254 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
255 LogAndSetError(
256 error,
257 FROM_HERE,
258 StringPrintf("Error setting the update via p2p permission to %s.",
259 ToString(in_enabled).c_str()));
260 return false;
261 }
262 return true;
263}
264
265bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
266 bool* out_enabled) {
267 PrefsInterface* prefs = system_state_->prefs();
268
269 bool p2p_pref = false; // Default if no setting is present.
270 if (prefs->Exists(kPrefsP2PEnabled) &&
271 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
272 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
273 return false;
274 }
275
276 *out_enabled = p2p_pref;
277 return true;
278}
279
280bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
281 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700282 ConnectionManagerInterface* connection_manager =
283 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800284
285 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700286 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700287 LogAndSetError(error,
288 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800289 "Ignoring the update over cellular setting since there's "
290 "a device policy enforcing this setting.");
291 return false;
292 }
293
294 // If the policy wasn't loaded yet, then it is still OK to change the local
295 // setting because the policy will be checked again during the update check.
296
297 PrefsInterface* prefs = system_state_->prefs();
298
Weidong Guo421ff332017-04-17 10:08:38 -0700299 if (!prefs ||
300 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700301 LogAndSetError(error,
302 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800303 string("Error setting the update over cellular to ") +
304 (in_allowed ? "true" : "false"));
305 return false;
306 }
307 return true;
308}
309
Weidong Guo421ff332017-04-17 10:08:38 -0700310bool UpdateEngineService::SetUpdateOverCellularTarget(
311 brillo::ErrorPtr* error,
312 const std::string& target_version,
313 int64_t target_size) {
314 ConnectionManagerInterface* connection_manager =
315 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700316
Weidong Guo421ff332017-04-17 10:08:38 -0700317 // Check if this setting is allowed by the device policy.
318 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
319 LogAndSetError(error,
320 FROM_HERE,
321 "Ignoring the update over cellular setting since there's "
322 "a device policy enforcing this setting.");
323 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700324 }
Tao Bao5688d162017-06-06 13:09:06 -0700325
Weidong Guo421ff332017-04-17 10:08:38 -0700326 // If the policy wasn't loaded yet, then it is still OK to change the local
327 // setting because the policy will be checked again during the update check.
328
329 PrefsInterface* prefs = system_state_->prefs();
330
331 if (!prefs ||
332 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
333 target_version) ||
334 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
335 LogAndSetError(
336 error, FROM_HERE, "Error setting the target for update over cellular.");
337 return false;
338 }
339 return true;
340}
341
342bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
343 bool* out_allowed) {
344 ConnectionManagerInterface* connection_manager =
345 system_state_->connection_manager();
346
347 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
348 // We have device policy, so ignore the user preferences.
349 *out_allowed = connection_manager->IsUpdateAllowedOver(
350 ConnectionType::kCellular, ConnectionTethering::kUnknown);
351 } else {
352 PrefsInterface* prefs = system_state_->prefs();
353
354 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
355 // Update is not allowed as user preference is not set or not available.
356 *out_allowed = false;
357 return true;
358 }
359
360 bool is_allowed;
361
362 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
363 LogAndSetError(error,
364 FROM_HERE,
365 "Error getting the update over cellular preference.");
366 return false;
367 }
368 *out_allowed = is_allowed;
369 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800370 return true;
371}
372
373bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
374 int64_t* out_usec_wallclock) {
375 base::Time time;
376 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
377 LogAndSetError(error, FROM_HERE, "No pending update.");
378 return false;
379 }
380
381 ClockInterface* clock = system_state_->clock();
382 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
383 return true;
384}
385
386bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
387 string* out_prev_version) {
388 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
389 return true;
390}
391
392bool UpdateEngineService::GetRollbackPartition(
393 ErrorPtr* /* error */, string* out_rollback_partition_name) {
394 BootControlInterface::Slot rollback_slot =
395 system_state_->update_attempter()->GetRollbackSlot();
396
397 if (rollback_slot == BootControlInterface::kInvalidSlot) {
398 out_rollback_partition_name->clear();
399 return true;
400 }
401
402 string name;
403 if (!system_state_->boot_control()->GetPartitionDevice(
404 "KERNEL", rollback_slot, &name)) {
405 LOG(ERROR) << "Invalid rollback device";
406 return false;
407 }
408
409 LOG(INFO) << "Getting rollback partition name. Result: " << name;
410 *out_rollback_partition_name = name;
411 return true;
412}
413
Shuqian Zhao29971732016-02-05 11:29:32 -0800414bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
415 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700416 ErrorCode error_code =
417 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800418 *out_last_attempt_error = static_cast<int>(error_code);
419 return true;
420}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700421
Casey Dahlina93cd532016-01-14 16:55:11 -0800422} // namespace chromeos_update_engine