Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/common_service.h" |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 18 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 21 | #include <base/bind.h> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 22 | #include <base/location.h> |
| 23 | #include <base/logging.h> |
| 24 | #include <base/strings/stringprintf.h> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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" |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 32 | #include "update_engine/common/system_state.h" |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 34 | #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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 40 | |
| 41 | using base::StringPrintf; |
| 42 | using brillo::ErrorPtr; |
| 43 | using brillo::string_utils::ToString; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 44 | using std::string; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 45 | using std::vector; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 46 | using update_engine::UpdateAttemptFlags; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 47 | using update_engine::UpdateEngineStatus; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 48 | |
| 49 | namespace chromeos_update_engine { |
| 50 | |
| 51 | namespace { |
| 52 | // Log and set the error on the passed ErrorPtr. |
| 53 | void LogAndSetError(ErrorPtr* error, |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 54 | const base::Location& location, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 55 | 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 | |
| 66 | const char* const UpdateEngineService::kErrorDomain = "update_engine"; |
| 67 | const char* const UpdateEngineService::kErrorFailed = |
| 68 | "org.chromium.UpdateEngine.Error.Failed"; |
| 69 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 70 | UpdateEngineService::UpdateEngineService() = default; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 71 | |
| 72 | // org::chromium::UpdateEngineInterfaceInterface methods implementation. |
| 73 | |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 74 | bool 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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 82 | SystemState::Get()->update_attempter()->SetUpdateAttemptFlags(flags); |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 86 | bool UpdateEngineService::AttemptUpdate(ErrorPtr* /* error */, |
| 87 | const string& in_app_version, |
| 88 | const string& in_omaha_url, |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 89 | int32_t in_flags_as_int, |
| 90 | bool* out_result) { |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 91 | auto flags = static_cast<UpdateAttemptFlags>(in_flags_as_int); |
| 92 | bool interactive = !(flags & UpdateAttemptFlags::kFlagNonInteractive); |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 93 | bool restrict_downloads = (flags & UpdateAttemptFlags::kFlagRestrictDownload); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 94 | |
| 95 | LOG(INFO) << "Attempt update: app_version=\"" << in_app_version << "\" " |
| 96 | << "omaha_url=\"" << in_omaha_url << "\" " |
| 97 | << "flags=0x" << std::hex << flags << " " |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 98 | << "interactive=" << (interactive ? "yes " : "no ") |
| 99 | << "RestrictDownload=" << (restrict_downloads ? "yes " : "no "); |
| 100 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 101 | *out_result = SystemState::Get()->update_attempter()->CheckForUpdate( |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 102 | in_app_version, in_omaha_url, flags); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 106 | bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error, |
| 107 | const string& omaha_url, |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 108 | const vector<string>& dlc_ids) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 109 | if (!SystemState::Get()->update_attempter()->CheckForInstall(dlc_ids, |
| 110 | omaha_url)) { |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 111 | // 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 118 | bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) { |
| 119 | LOG(INFO) << "Attempting rollback to non-active partitions."; |
| 120 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 121 | if (!SystemState::Get()->update_attempter()->Rollback(in_powerwash)) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
| 129 | bool UpdateEngineService::CanRollback(ErrorPtr* /* error */, |
| 130 | bool* out_can_rollback) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 131 | bool can_rollback = SystemState::Get()->update_attempter()->CanRollback(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 132 | LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback; |
| 133 | *out_can_rollback = can_rollback; |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool UpdateEngineService::ResetStatus(ErrorPtr* error) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 138 | if (!SystemState::Get()->update_attempter()->ResetStatus()) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 146 | bool UpdateEngineService::SetDlcActiveValue(brillo::ErrorPtr* error, |
| 147 | bool is_active, |
| 148 | const string& dlc_id) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 149 | if (!SystemState::Get()->update_attempter()->SetDlcActiveValue(is_active, |
| 150 | dlc_id)) { |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 151 | LogAndSetError(error, FROM_HERE, "SetDlcActiveValue failed."); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 157 | bool UpdateEngineService::GetStatus(ErrorPtr* error, |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 158 | UpdateEngineStatus* out_status) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 159 | if (!SystemState::Get()->update_attempter()->GetStatus(out_status)) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 160 | LogAndSetError(error, FROM_HERE, "GetStatus failed."); |
| 161 | return false; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 167 | if (!SystemState::Get()->update_attempter()->RebootIfNeeded()) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
| 175 | bool UpdateEngineService::SetChannel(ErrorPtr* error, |
| 176 | const string& in_target_channel, |
| 177 | bool in_is_powerwash_allowed) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 178 | const policy::DevicePolicy* device_policy = |
| 179 | SystemState::Get()->device_policy(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 180 | |
| 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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 184 | UpdateAttempter* update_attempter = SystemState::Get()->update_attempter(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 185 | if (update_attempter) { |
| 186 | update_attempter->RefreshDevicePolicy(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 187 | device_policy = SystemState::Get()->device_policy(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 188 | } |
| 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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 203 | if (!SystemState::Get()->request_params()->SetTargetChannel( |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 204 | in_target_channel, in_is_powerwash_allowed, &error_message)) { |
| 205 | LogAndSetError(error, FROM_HERE, error_message); |
| 206 | return false; |
| 207 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | bool UpdateEngineService::GetChannel(ErrorPtr* /* error */, |
| 212 | bool in_get_current_channel, |
| 213 | string* out_channel) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 214 | OmahaRequestParams* rp = SystemState::Get()->request_params(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 215 | *out_channel = |
| 216 | (in_get_current_channel ? rp->current_channel() : rp->target_channel()); |
| 217 | return true; |
| 218 | } |
| 219 | |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 220 | bool UpdateEngineService::SetCohortHint(ErrorPtr* error, |
Andrew | 9d5a61d | 2020-03-26 13:40:37 -0700 | [diff] [blame] | 221 | const string& in_cohort_hint) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 222 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 223 | |
| 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 | |
| 238 | bool UpdateEngineService::GetCohortHint(ErrorPtr* error, |
| 239 | string* out_cohort_hint) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 240 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 241 | |
| 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 251 | bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error, |
| 252 | bool in_enabled) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 253 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 254 | |
| 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 | |
| 266 | bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error, |
| 267 | bool* out_enabled) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 268 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 269 | |
| 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 | |
| 281 | bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error, |
| 282 | bool in_allowed) { |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 283 | ConnectionManagerInterface* connection_manager = |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 284 | SystemState::Get()->connection_manager(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 285 | |
| 286 | // Check if this setting is allowed by the device policy. |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 287 | if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) { |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 288 | LogAndSetError(error, |
| 289 | FROM_HERE, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 290 | "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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 298 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 299 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 300 | if (!prefs || |
| 301 | !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) { |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 302 | LogAndSetError(error, |
| 303 | FROM_HERE, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 304 | string("Error setting the update over cellular to ") + |
| 305 | (in_allowed ? "true" : "false")); |
| 306 | return false; |
| 307 | } |
| 308 | return true; |
| 309 | } |
| 310 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 311 | bool UpdateEngineService::SetUpdateOverCellularTarget( |
| 312 | brillo::ErrorPtr* error, |
| 313 | const std::string& target_version, |
| 314 | int64_t target_size) { |
| 315 | ConnectionManagerInterface* connection_manager = |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 316 | SystemState::Get()->connection_manager(); |
Weidong Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 317 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 318 | // 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 Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 325 | } |
Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 326 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 327 | // 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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 330 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 331 | |
| 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 | |
| 343 | bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error, |
| 344 | bool* out_allowed) { |
| 345 | ConnectionManagerInterface* connection_manager = |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 346 | SystemState::Get()->connection_manager(); |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 347 | |
| 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 Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 353 | PrefsInterface* prefs = SystemState::Get()->prefs(); |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 354 | |
| 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
| 374 | bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error, |
| 375 | int64_t* out_usec_wallclock) { |
| 376 | base::Time time; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 377 | if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(&time)) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 378 | LogAndSetError(error, FROM_HERE, "No pending update."); |
| 379 | return false; |
| 380 | } |
| 381 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 382 | ClockInterface* clock = SystemState::Get()->clock(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 383 | *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds(); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */, |
| 388 | string* out_prev_version) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 389 | *out_prev_version = SystemState::Get()->update_attempter()->GetPrevVersion(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | bool UpdateEngineService::GetRollbackPartition( |
| 394 | ErrorPtr* /* error */, string* out_rollback_partition_name) { |
| 395 | BootControlInterface::Slot rollback_slot = |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 396 | SystemState::Get()->update_attempter()->GetRollbackSlot(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 397 | |
| 398 | if (rollback_slot == BootControlInterface::kInvalidSlot) { |
| 399 | out_rollback_partition_name->clear(); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | string name; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 404 | if (!SystemState::Get()->boot_control()->GetPartitionDevice( |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 405 | "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 Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 415 | bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */, |
| 416 | int32_t* out_last_attempt_error) { |
Sen Jiang | 3978ddd | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 417 | ErrorCode error_code = |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 418 | SystemState::Get()->update_attempter()->GetAttemptErrorCode(); |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 419 | *out_last_attempt_error = static_cast<int>(error_code); |
| 420 | return true; |
| 421 | } |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 422 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 423 | } // namespace chromeos_update_engine |