• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 
19 #include <string>
20 
21 #include <base/bind.h>
22 #include <base/location.h>
23 #include <base/logging.h>
24 #include <base/strings/stringprintf.h>
25 #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"
35 #include "update_engine/omaha_utils.h"
36 #include "update_engine/p2p_manager.h"
37 #include "update_engine/payload_state_interface.h"
38 #include "update_engine/update_attempter.h"
39 
40 using base::StringPrintf;
41 using brillo::ErrorPtr;
42 using brillo::string_utils::ToString;
43 using std::string;
44 using std::vector;
45 using update_engine::UpdateAttemptFlags;
46 using update_engine::UpdateEngineStatus;
47 
48 namespace chromeos_update_engine {
49 
50 namespace {
51 // Log and set the error on the passed ErrorPtr.
LogAndSetError(ErrorPtr * error,const tracked_objects::Location & location,const string & reason)52 void LogAndSetError(ErrorPtr* error,
53 #if BASE_VER < 576279
54                     const tracked_objects::Location& location,
55 #else
56                     const base::Location& location,
57 #endif
58                     const string& reason) {
59   brillo::Error::AddTo(error,
60                        location,
61                        UpdateEngineService::kErrorDomain,
62                        UpdateEngineService::kErrorFailed,
63                        reason);
64   LOG(ERROR) << "Sending Update Engine Failure: " << location.ToString() << ": "
65              << reason;
66 }
67 }  // namespace
68 
69 const char* const UpdateEngineService::kErrorDomain = "update_engine";
70 const char* const UpdateEngineService::kErrorFailed =
71     "org.chromium.UpdateEngine.Error.Failed";
72 
UpdateEngineService(SystemState * system_state)73 UpdateEngineService::UpdateEngineService(SystemState* system_state)
74     : system_state_(system_state) {}
75 
76 // org::chromium::UpdateEngineInterfaceInterface methods implementation.
77 
SetUpdateAttemptFlags(ErrorPtr *,int32_t in_flags_as_int)78 bool UpdateEngineService::SetUpdateAttemptFlags(ErrorPtr* /* error */,
79                                                 int32_t in_flags_as_int) {
80   auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
81   LOG(INFO) << "Setting Update Attempt Flags: "
82             << "flags=0x" << std::hex << flags << " "
83             << "RestrictDownload="
84             << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
85                                                                     : "no");
86   system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
87   return true;
88 }
89 
AttemptUpdate(ErrorPtr *,const string & in_app_version,const string & in_omaha_url,int32_t in_flags_as_int,bool * out_result)90 bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */,
91                                         const string& in_app_version,
92                                         const string& in_omaha_url,
93                                         int32_t in_flags_as_int,
94                                         bool* out_result) {
95   auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int);
96   bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive);
97   bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload);
98 
99   LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" "
100             << "omaha_url=\"" << in_omaha_url << "\" "
101             << "flags=0x" << std::hex << flags << " "
102             << "interactive=" << (interactive ? "yes " : "no ")
103             << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
104 
105   *out_result = system_state_->update_attempter()->CheckForUpdate(
106       in_app_version, in_omaha_url, flags);
107   return true;
108 }
109 
AttemptInstall(brillo::ErrorPtr * error,const string & omaha_url,const vector<string> & dlc_module_ids)110 bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
111                                          const string& omaha_url,
112                                          const vector<string>& dlc_module_ids) {
113   if (!system_state_->update_attempter()->CheckForInstall(dlc_module_ids,
114                                                           omaha_url)) {
115     // TODO(xiaochu): support more detailed error messages.
116     LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
117     return false;
118   }
119   return true;
120 }
121 
AttemptRollback(ErrorPtr * error,bool in_powerwash)122 bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
123   LOG(INFO) << "Attempting rollback to non-active partitions.";
124 
125   if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
126     // TODO(dgarrett): Give a more specific error code/reason.
127     LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
128     return false;
129   }
130   return true;
131 }
132 
CanRollback(ErrorPtr *,bool * out_can_rollback)133 bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
134                                       bool* out_can_rollback) {
135   bool can_rollback = system_state_->update_attempter()->CanRollback();
136   LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
137   *out_can_rollback = can_rollback;
138   return true;
139 }
140 
ResetStatus(ErrorPtr * error)141 bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
142   if (!system_state_->update_attempter()->ResetStatus()) {
143     // TODO(dgarrett): Give a more specific error code/reason.
144     LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
145     return false;
146   }
147   return true;
148 }
149 
GetStatus(ErrorPtr * error,UpdateEngineStatus * out_status)150 bool UpdateEngineService::GetStatus(ErrorPtr* error,
151                                     UpdateEngineStatus* out_status) {
152   if (!system_state_->update_attempter()->GetStatus(out_status)) {
153     LogAndSetError(error, FROM_HERE, "GetStatus failed.");
154     return false;
155   }
156   return true;
157 }
158 
RebootIfNeeded(ErrorPtr * error)159 bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
160   if (!system_state_->update_attempter()->RebootIfNeeded()) {
161     // TODO(dgarrett): Give a more specific error code/reason.
162     LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
163     return false;
164   }
165   return true;
166 }
167 
SetChannel(ErrorPtr * error,const string & in_target_channel,bool in_is_powerwash_allowed)168 bool UpdateEngineService::SetChannel(ErrorPtr* error,
169                                      const string& in_target_channel,
170                                      bool in_is_powerwash_allowed) {
171   const policy::DevicePolicy* device_policy = system_state_->device_policy();
172 
173   // The device_policy is loaded in a lazy way before an update check. Load it
174   // now from the libbrillo cache if it wasn't already loaded.
175   if (!device_policy) {
176     UpdateAttempter* update_attempter = system_state_->update_attempter();
177     if (update_attempter) {
178       update_attempter->RefreshDevicePolicy();
179       device_policy = system_state_->device_policy();
180     }
181   }
182 
183   bool delegated = false;
184   if (device_policy && device_policy->GetReleaseChannelDelegated(&delegated) &&
185       !delegated) {
186     LogAndSetError(error,
187                    FROM_HERE,
188                    "Cannot set target channel explicitly when channel "
189                    "policy/settings is not delegated");
190     return false;
191   }
192 
193   LOG(INFO) << "Setting destination channel to: " << in_target_channel;
194   string error_message;
195   if (!system_state_->request_params()->SetTargetChannel(
196           in_target_channel, in_is_powerwash_allowed, &error_message)) {
197     LogAndSetError(error, FROM_HERE, error_message);
198     return false;
199   }
200   return true;
201 }
202 
GetChannel(ErrorPtr *,bool in_get_current_channel,string * out_channel)203 bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
204                                      bool in_get_current_channel,
205                                      string* out_channel) {
206   OmahaRequestParams* rp = system_state_->request_params();
207   *out_channel =
208       (in_get_current_channel ? rp->current_channel() : rp->target_channel());
209   return true;
210 }
211 
SetCohortHint(ErrorPtr * error,string in_cohort_hint)212 bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
213                                         string in_cohort_hint) {
214   PrefsInterface* prefs = system_state_->prefs();
215 
216   // It is ok to override the cohort hint with an invalid value since it is
217   // stored in stateful partition. The code reading it should sanitize it
218   // anyway.
219   if (!prefs->SetString(kPrefsOmahaCohortHint, in_cohort_hint)) {
220     LogAndSetError(
221         error,
222         FROM_HERE,
223         StringPrintf("Error setting the cohort hint value to \"%s\".",
224                      in_cohort_hint.c_str()));
225     return false;
226   }
227   return true;
228 }
229 
GetCohortHint(ErrorPtr * error,string * out_cohort_hint)230 bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
231                                         string* out_cohort_hint) {
232   PrefsInterface* prefs = system_state_->prefs();
233 
234   *out_cohort_hint = "";
235   if (prefs->Exists(kPrefsOmahaCohortHint) &&
236       !prefs->GetString(kPrefsOmahaCohortHint, out_cohort_hint)) {
237     LogAndSetError(error, FROM_HERE, "Error getting the cohort hint.");
238     return false;
239   }
240   return true;
241 }
242 
SetP2PUpdatePermission(ErrorPtr * error,bool in_enabled)243 bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
244                                                  bool in_enabled) {
245   PrefsInterface* prefs = system_state_->prefs();
246 
247   if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
248     LogAndSetError(
249         error,
250         FROM_HERE,
251         StringPrintf("Error setting the update via p2p permission to %s.",
252                      ToString(in_enabled).c_str()));
253     return false;
254   }
255   return true;
256 }
257 
GetP2PUpdatePermission(ErrorPtr * error,bool * out_enabled)258 bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
259                                                  bool* out_enabled) {
260   PrefsInterface* prefs = system_state_->prefs();
261 
262   bool p2p_pref = false;  // Default if no setting is present.
263   if (prefs->Exists(kPrefsP2PEnabled) &&
264       !prefs->GetBoolean(kPrefsP2PEnabled, &p2p_pref)) {
265     LogAndSetError(error, FROM_HERE, "Error getting the P2PEnabled setting.");
266     return false;
267   }
268 
269   *out_enabled = p2p_pref;
270   return true;
271 }
272 
SetUpdateOverCellularPermission(ErrorPtr * error,bool in_allowed)273 bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
274                                                           bool in_allowed) {
275   ConnectionManagerInterface* connection_manager =
276       system_state_->connection_manager();
277 
278   // Check if this setting is allowed by the device policy.
279   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
280     LogAndSetError(error,
281                    FROM_HERE,
282                    "Ignoring the update over cellular setting since there's "
283                    "a device policy enforcing this setting.");
284     return false;
285   }
286 
287   // If the policy wasn't loaded yet, then it is still OK to change the local
288   // setting because the policy will be checked again during the update check.
289 
290   PrefsInterface* prefs = system_state_->prefs();
291 
292   if (!prefs ||
293       !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
294     LogAndSetError(error,
295                    FROM_HERE,
296                    string("Error setting the update over cellular to ") +
297                        (in_allowed ? "true" : "false"));
298     return false;
299   }
300   return true;
301 }
302 
SetUpdateOverCellularTarget(brillo::ErrorPtr * error,const std::string & target_version,int64_t target_size)303 bool UpdateEngineService::SetUpdateOverCellularTarget(
304     brillo::ErrorPtr* error,
305     const std::string& target_version,
306     int64_t target_size) {
307   ConnectionManagerInterface* connection_manager =
308       system_state_->connection_manager();
309 
310   // Check if this setting is allowed by the device policy.
311   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
312     LogAndSetError(error,
313                    FROM_HERE,
314                    "Ignoring the update over cellular setting since there's "
315                    "a device policy enforcing this setting.");
316     return false;
317   }
318 
319   // If the policy wasn't loaded yet, then it is still OK to change the local
320   // setting because the policy will be checked again during the update check.
321 
322   PrefsInterface* prefs = system_state_->prefs();
323 
324   if (!prefs ||
325       !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
326                         target_version) ||
327       !prefs->SetInt64(kPrefsUpdateOverCellularTargetSize, target_size)) {
328     LogAndSetError(
329         error, FROM_HERE, "Error setting the target for update over cellular.");
330     return false;
331   }
332   return true;
333 }
334 
GetUpdateOverCellularPermission(ErrorPtr * error,bool * out_allowed)335 bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
336                                                           bool* out_allowed) {
337   ConnectionManagerInterface* connection_manager =
338       system_state_->connection_manager();
339 
340   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
341     // We have device policy, so ignore the user preferences.
342     *out_allowed = connection_manager->IsUpdateAllowedOver(
343         ConnectionType::kCellular, ConnectionTethering::kUnknown);
344   } else {
345     PrefsInterface* prefs = system_state_->prefs();
346 
347     if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
348       // Update is not allowed as user preference is not set or not available.
349       *out_allowed = false;
350       return true;
351     }
352 
353     bool is_allowed;
354 
355     if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed)) {
356       LogAndSetError(error,
357                      FROM_HERE,
358                      "Error getting the update over cellular preference.");
359       return false;
360     }
361     *out_allowed = is_allowed;
362   }
363   return true;
364 }
365 
GetDurationSinceUpdate(ErrorPtr * error,int64_t * out_usec_wallclock)366 bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
367                                                  int64_t* out_usec_wallclock) {
368   base::Time time;
369   if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
370     LogAndSetError(error, FROM_HERE, "No pending update.");
371     return false;
372   }
373 
374   ClockInterface* clock = system_state_->clock();
375   *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
376   return true;
377 }
378 
GetPrevVersion(ErrorPtr *,string * out_prev_version)379 bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
380                                          string* out_prev_version) {
381   *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
382   return true;
383 }
384 
GetRollbackPartition(ErrorPtr *,string * out_rollback_partition_name)385 bool UpdateEngineService::GetRollbackPartition(
386     ErrorPtr* /* error */, string* out_rollback_partition_name) {
387   BootControlInterface::Slot rollback_slot =
388       system_state_->update_attempter()->GetRollbackSlot();
389 
390   if (rollback_slot == BootControlInterface::kInvalidSlot) {
391     out_rollback_partition_name->clear();
392     return true;
393   }
394 
395   string name;
396   if (!system_state_->boot_control()->GetPartitionDevice(
397           "KERNEL", rollback_slot, &name)) {
398     LOG(ERROR) << "Invalid rollback device";
399     return false;
400   }
401 
402   LOG(INFO) << "Getting rollback partition name. Result: " << name;
403   *out_rollback_partition_name = name;
404   return true;
405 }
406 
GetLastAttemptError(ErrorPtr *,int32_t * out_last_attempt_error)407 bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
408                                               int32_t* out_last_attempt_error) {
409   ErrorCode error_code =
410       system_state_->update_attempter()->GetAttemptErrorCode();
411   *out_last_attempt_error = static_cast<int>(error_code);
412   return true;
413 }
414 
GetEolStatus(ErrorPtr * error,int32_t * out_eol_status)415 bool UpdateEngineService::GetEolStatus(ErrorPtr* error,
416                                        int32_t* out_eol_status) {
417   PrefsInterface* prefs = system_state_->prefs();
418 
419   string str_eol_status;
420   if (prefs->Exists(kPrefsOmahaEolStatus) &&
421       !prefs->GetString(kPrefsOmahaEolStatus, &str_eol_status)) {
422     LogAndSetError(error, FROM_HERE, "Error getting the end-of-life status.");
423     return false;
424   }
425 
426   // StringToEolStatus will return kSupported for invalid values.
427   *out_eol_status = static_cast<int32_t>(StringToEolStatus(str_eol_status));
428   return true;
429 }
430 
431 }  // namespace chromeos_update_engine
432