blob: 85fb9e4f99441f21a9e4caf7f5b2492fa8cf9341 [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,
Amin Hassani2b68e6b2020-04-17 10:49:12 -0700108 const vector<string>& dlc_ids) {
109 if (!system_state_->update_attempter()->CheckForInstall(dlc_ids, 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
120 if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
121 // 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) {
130 bool can_rollback = system_state_->update_attempter()->CanRollback();
131 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) {
137 if (!system_state_->update_attempter()->ResetStatus()) {
138 // 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) {
148 if (!system_state_->update_attempter()->SetDlcActiveValue(is_active,
149 dlc_id)) {
150 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) {
158 if (!system_state_->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) {
166 if (!system_state_->update_attempter()->RebootIfNeeded()) {
167 // 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) {
177 const policy::DevicePolicy* device_policy = system_state_->device_policy();
178
179 // The device_policy is loaded in a lazy way before an update check. Load it
180 // now from the libbrillo cache if it wasn't already loaded.
181 if (!device_policy) {
182 UpdateAttempter* update_attempter = system_state_->update_attempter();
183 if (update_attempter) {
184 update_attempter->RefreshDevicePolicy();
185 device_policy = system_state_->device_policy();
186 }
187 }
188
189 bool delegated = false;
190 if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
191 !delegated) {
192 LogAndSetError(error,
193 FROM_HERE,
194 "Cannot set target channel explicitly when channel "
195 "policy/settings is not delegated");
196 return false;
197 }
198
199 LOG(INFO) << "Setting destination channel to: " << in_target_channel;
200 string error_message;
201 if (!system_state_->request_params()->SetTargetChannel(
202 in_target_channel, in_is_powerwash_allowed, &error_message)) {
203 LogAndSetError(error, FROM_HERE, error_message);
204 return false;
205 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800206 return true;
207}
208
209bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
210 bool in_get_current_channel,
211 string* out_channel) {
212 OmahaRequestParams* rp = system_state_->request_params();
213 *out_channel =
214 (in_get_current_channel ? rp->current_channel() : rp->target_channel());
215 return true;
216}
217
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700218bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
Andrew9d5a61d2020-03-26 13:40:37 -0700219 const string& in_cohort_hint) {
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700220 PrefsInterface* prefs = system_state_->prefs();
221
222 // It is ok to override the cohort hint with an invalid value since it is
223 // stored in stateful partition. The code reading it should sanitize it
224 // anyway.
225 if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
226 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) {
238 PrefsInterface* prefs = system_state_->prefs();
239
240 *out_cohort_hint = "";
241 if (prefs->Exists(kPrefsOmahaCohortHint) &&
242 !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
243 LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
244 return false;
245 }
246 return true;
247}
248
Casey Dahlina93cd532016-01-14 16:55:11 -0800249bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
250 bool in_enabled) {
251 PrefsInterface* prefs = system_state_->prefs();
252
253 if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
254 LogAndSetError(
255 error,
256 FROM_HERE,
257 StringPrintf("Error setting the update via p2p permission to %s.",
258 ToString(in_enabled).c_str()));
259 return false;
260 }
261 return true;
262}
263
264bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
265 bool* out_enabled) {
266 PrefsInterface* prefs = system_state_->prefs();
267
268 bool p2p_pref = false; // Default if no setting is present.
269 if (prefs->Exists(kPrefsP2PEnabled) &&
270 !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
271 LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
272 return false;
273 }
274
275 *out_enabled = p2p_pref;
276 return true;
277}
278
279bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
280 bool in_allowed) {
Weidong Guo421ff332017-04-17 10:08:38 -0700281 ConnectionManagerInterface* connection_manager =
282 system_state_->connection_manager();
Casey Dahlina93cd532016-01-14 16:55:11 -0800283
284 // Check if this setting is allowed by the device policy.
Weidong Guo421ff332017-04-17 10:08:38 -0700285 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
Tao Bao5688d162017-06-06 13:09:06 -0700286 LogAndSetError(error,
287 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800288 "Ignoring the update over cellular setting since there's "
289 "a device policy enforcing this setting.");
290 return false;
291 }
292
293 // If the policy wasn't loaded yet, then it is still OK to change the local
294 // setting because the policy will be checked again during the update check.
295
296 PrefsInterface* prefs = system_state_->prefs();
297
Weidong Guo421ff332017-04-17 10:08:38 -0700298 if (!prefs ||
299 !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
Tao Bao5688d162017-06-06 13:09:06 -0700300 LogAndSetError(error,
301 FROM_HERE,
Casey Dahlina93cd532016-01-14 16:55:11 -0800302 string("Error setting the update over cellular to ") +
303 (in_allowed ? "true" : "false"));
304 return false;
305 }
306 return true;
307}
308
Weidong Guo421ff332017-04-17 10:08:38 -0700309bool UpdateEngineService::SetUpdateOverCellularTarget(
310 brillo::ErrorPtr* error,
311 const std::string& target_version,
312 int64_t target_size) {
313 ConnectionManagerInterface* connection_manager =
314 system_state_->connection_manager();
Weidong Guo4b0d6032017-04-17 10:08:38 -0700315
Weidong Guo421ff332017-04-17 10:08:38 -0700316 // Check if this setting is allowed by the device policy.
317 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
318 LogAndSetError(error,
319 FROM_HERE,
320 "Ignoring the update over cellular setting since there's "
321 "a device policy enforcing this setting.");
322 return false;
Weidong Guo4b0d6032017-04-17 10:08:38 -0700323 }
Tao Bao5688d162017-06-06 13:09:06 -0700324
Weidong Guo421ff332017-04-17 10:08:38 -0700325 // If the policy wasn't loaded yet, then it is still OK to change the local
326 // setting because the policy will be checked again during the update check.
327
328 PrefsInterface* prefs = system_state_->prefs();
329
330 if (!prefs ||
331 !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
332 target_version) ||
333 !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
334 LogAndSetError(
335 error, FROM_HERE, "Error setting the target for update over cellular.");
336 return false;
337 }
338 return true;
339}
340
341bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
342 bool* out_allowed) {
343 ConnectionManagerInterface* connection_manager =
344 system_state_->connection_manager();
345
346 if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
347 // We have device policy, so ignore the user preferences.
348 *out_allowed = connection_manager->IsUpdateAllowedOver(
349 ConnectionType::kCellular, ConnectionTethering::kUnknown);
350 } else {
351 PrefsInterface* prefs = system_state_->prefs();
352
353 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
354 // Update is not allowed as user preference is not set or not available.
355 *out_allowed = false;
356 return true;
357 }
358
359 bool is_allowed;
360
361 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
362 LogAndSetError(error,
363 FROM_HERE,
364 "Error getting the update over cellular preference.");
365 return false;
366 }
367 *out_allowed = is_allowed;
368 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800369 return true;
370}
371
372bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
373 int64_t* out_usec_wallclock) {
374 base::Time time;
375 if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
376 LogAndSetError(error, FROM_HERE, "No pending update.");
377 return false;
378 }
379
380 ClockInterface* clock = system_state_->clock();
381 *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
382 return true;
383}
384
385bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
386 string* out_prev_version) {
387 *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
388 return true;
389}
390
391bool UpdateEngineService::GetRollbackPartition(
392 ErrorPtr* /* error */, string* out_rollback_partition_name) {
393 BootControlInterface::Slot rollback_slot =
394 system_state_->update_attempter()->GetRollbackSlot();
395
396 if (rollback_slot == BootControlInterface::kInvalidSlot) {
397 out_rollback_partition_name->clear();
398 return true;
399 }
400
401 string name;
402 if (!system_state_->boot_control()->GetPartitionDevice(
403 "KERNEL", rollback_slot, &name)) {
404 LOG(ERROR) << "Invalid rollback device";
405 return false;
406 }
407
408 LOG(INFO) << "Getting rollback partition name. Result: " << name;
409 *out_rollback_partition_name = name;
410 return true;
411}
412
Shuqian Zhao29971732016-02-05 11:29:32 -0800413bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
414 int32_t* out_last_attempt_error) {
Sen Jiang3978ddd2018-03-22 18:05:44 -0700415 ErrorCode error_code =
416 system_state_->update_attempter()->GetAttemptErrorCode();
Shuqian Zhao29971732016-02-05 11:29:32 -0800417 *out_last_attempt_error = static_cast<int>(error_code);
418 return true;
419}
Alex Deymob3fa53b2016-04-18 19:57:58 -0700420
Casey Dahlina93cd532016-01-14 16:55:11 -0800421} // namespace chromeos_update_engine