Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 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/update_attempter_android.h" |
| 18 | |
| 19 | #include <algorithm> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 20 | #include <map> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 21 | #include <utility> |
| 22 | |
| 23 | #include <base/bind.h> |
| 24 | #include <base/logging.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 25 | #include <base/strings/string_number_conversions.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 26 | #include <brillo/bind_lambda.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 27 | #include <brillo/data_encoding.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 28 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 29 | #include <brillo/strings/string_utils.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 30 | |
| 31 | #include "update_engine/common/constants.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 32 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 34 | #include "update_engine/daemon_state_interface.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 35 | #include "update_engine/network_selector.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 36 | #include "update_engine/payload_consumer/download_action.h" |
| 37 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
| 38 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 39 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 40 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 41 | #ifndef _UE_SIDELOAD |
| 42 | // Do not include support for external HTTP(s) urls when building |
| 43 | // update_engine_sideload. |
| 44 | #include "update_engine/libcurl_http_fetcher.h" |
| 45 | #endif |
| 46 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 47 | using base::Bind; |
| 48 | using base::TimeDelta; |
| 49 | using base::TimeTicks; |
| 50 | using std::shared_ptr; |
| 51 | using std::string; |
| 52 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 53 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 54 | |
| 55 | namespace chromeos_update_engine { |
| 56 | |
| 57 | namespace { |
| 58 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 59 | // Minimum threshold to broadcast an status update in progress and time. |
| 60 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 61 | const int kBroadcastThresholdSeconds = 10; |
| 62 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 63 | const char* const kErrorDomain = "update_engine"; |
| 64 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 65 | // back on the service error. |
| 66 | const char* const kGenericError = "generic_error"; |
| 67 | |
| 68 | // Log and set the error on the passed ErrorPtr. |
| 69 | bool LogAndSetError(brillo::ErrorPtr* error, |
| 70 | const tracked_objects::Location& location, |
| 71 | const string& reason) { |
| 72 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 73 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 74 | << reason; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | } // namespace |
| 79 | |
| 80 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 81 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 82 | PrefsInterface* prefs, |
| 83 | BootControlInterface* boot_control, |
| 84 | HardwareInterface* hardware) |
| 85 | : daemon_state_(daemon_state), |
| 86 | prefs_(prefs), |
| 87 | boot_control_(boot_control), |
| 88 | hardware_(hardware), |
| 89 | processor_(new ActionProcessor()) { |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 90 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 94 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 95 | // re-scheduling the updates due to the processing stopped. |
| 96 | processor_->set_delegate(nullptr); |
| 97 | } |
| 98 | |
| 99 | void UpdateAttempterAndroid::Init() { |
| 100 | // In case of update_engine restart without a reboot we need to restore the |
| 101 | // reboot needed state. |
| 102 | if (UpdateCompletedOnThisBoot()) |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 103 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 104 | else |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 105 | SetStatusAndNotify(UpdateStatus::IDLE); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool UpdateAttempterAndroid::ApplyPayload( |
| 109 | const string& payload_url, |
| 110 | int64_t payload_offset, |
| 111 | int64_t payload_size, |
| 112 | const vector<string>& key_value_pair_headers, |
| 113 | brillo::ErrorPtr* error) { |
| 114 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 115 | return LogAndSetError( |
| 116 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 117 | } |
| 118 | if (ongoing_update_) { |
| 119 | return LogAndSetError( |
| 120 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 121 | } |
| 122 | DCHECK(status_ == UpdateStatus::IDLE); |
| 123 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 124 | std::map<string, string> headers; |
| 125 | for (const string& key_value_pair : key_value_pair_headers) { |
| 126 | string key; |
| 127 | string value; |
| 128 | if (!brillo::string_utils::SplitAtFirst( |
| 129 | key_value_pair, "=", &key, &value, false)) { |
| 130 | return LogAndSetError( |
| 131 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 132 | } |
| 133 | if (!headers.emplace(key, value).second) |
| 134 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 135 | } |
| 136 | |
| 137 | // Unique identifier for the payload. An empty string means that the payload |
| 138 | // can't be resumed. |
| 139 | string payload_id = (headers[kPayloadPropertyFileHash] + |
| 140 | headers[kPayloadPropertyMetadataHash]); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 141 | |
| 142 | // Setup the InstallPlan based on the request. |
| 143 | install_plan_ = InstallPlan(); |
| 144 | |
| 145 | install_plan_.download_url = payload_url; |
| 146 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 147 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 148 | InstallPlan::Payload payload; |
| 149 | payload.size = payload_size; |
| 150 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 151 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 152 | &payload.size)) { |
| 153 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 156 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 157 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 158 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 159 | << headers[kPayloadPropertyFileHash]; |
| 160 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 161 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 162 | &payload.metadata_size)) { |
| 163 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 164 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 165 | // The |payload.type| is not used anymore since minor_version 3. |
| 166 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 167 | install_plan_.payloads.push_back(payload); |
| 168 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 169 | // The |public_key_rsa| key would override the public key stored on disk. |
| 170 | install_plan_.public_key_rsa = ""; |
| 171 | |
| 172 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 173 | install_plan_.is_resume = !payload_id.empty() && |
| 174 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 175 | if (!install_plan_.is_resume) { |
| 176 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 177 | LOG(WARNING) << "Unable to reset the update progress."; |
| 178 | } |
| 179 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 180 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 181 | } |
| 182 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 183 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 184 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 185 | |
| 186 | int data_wipe = 0; |
| 187 | install_plan_.powerwash_required = |
| 188 | base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) && |
| 189 | data_wipe != 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 190 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 191 | NetworkId network_id = kDefaultNetworkId; |
| 192 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 193 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 194 | &network_id)) { |
| 195 | return LogAndSetError( |
| 196 | error, |
| 197 | FROM_HERE, |
| 198 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 199 | } |
| 200 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 201 | return LogAndSetError( |
| 202 | error, |
| 203 | FROM_HERE, |
| 204 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 208 | LOG(INFO) << "Using this install plan:"; |
| 209 | install_plan_.Dump(); |
| 210 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 211 | BuildUpdateActions(payload_url); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 212 | // Setup extra headers. |
| 213 | HttpFetcher* fetcher = download_action_->http_fetcher(); |
| 214 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 215 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 216 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 217 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 218 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 219 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 220 | ongoing_update_ = true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 221 | |
| 222 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 223 | // before any update processing starts. This will start the update process. |
| 224 | UpdateBootFlags(); |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 229 | if (!ongoing_update_) |
| 230 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 231 | processor_->SuspendProcessing(); |
| 232 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 236 | if (!ongoing_update_) |
| 237 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 238 | processor_->ResumeProcessing(); |
| 239 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 243 | if (!ongoing_update_) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 244 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 245 | processor_->StopProcessing(); |
| 246 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 249 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 250 | LOG(INFO) << "Attempting to reset state from " |
| 251 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 252 | |
| 253 | switch (status_) { |
| 254 | case UpdateStatus::IDLE: |
| 255 | return true; |
| 256 | |
| 257 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
| 258 | // Remove the reboot marker so that if the machine is rebooted |
| 259 | // after resetting to idle state, it doesn't go back to |
| 260 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 261 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
| 262 | |
| 263 | // Update the boot flags so the current slot has higher priority. |
| 264 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 265 | ret_value = false; |
| 266 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 267 | // Mark the current slot as successful again, since marking it as active |
| 268 | // may reset the successful bit. We ignore the result of whether marking |
| 269 | // the current slot as successful worked. |
| 270 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){}))) |
| 271 | ret_value = false; |
| 272 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 273 | if (!ret_value) { |
| 274 | return LogAndSetError( |
| 275 | error, |
| 276 | FROM_HERE, |
| 277 | "Failed to reset the status to "); |
| 278 | } |
| 279 | |
| 280 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 281 | LOG(INFO) << "Reset status successful"; |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | default: |
| 286 | return LogAndSetError( |
| 287 | error, |
| 288 | FROM_HERE, |
| 289 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 290 | } |
| 291 | } |
| 292 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 293 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 294 | ErrorCode code) { |
| 295 | LOG(INFO) << "Processing Done."; |
| 296 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 297 | switch (code) { |
| 298 | case ErrorCode::kSuccess: |
| 299 | // Update succeeded. |
| 300 | WriteUpdateCompletedMarker(); |
| 301 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
| 302 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 303 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 304 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 305 | break; |
| 306 | |
| 307 | case ErrorCode::kFilesystemCopierError: |
| 308 | case ErrorCode::kNewRootfsVerificationError: |
| 309 | case ErrorCode::kNewKernelVerificationError: |
| 310 | case ErrorCode::kFilesystemVerifierError: |
| 311 | case ErrorCode::kDownloadStateInitializationError: |
| 312 | // Reset the ongoing update for these errors so it starts from the |
| 313 | // beginning next time. |
| 314 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 315 | LOG(INFO) << "Resetting update progress."; |
| 316 | break; |
| 317 | |
| 318 | default: |
| 319 | // Ignore all other error codes. |
| 320 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 321 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 322 | |
| 323 | TerminateUpdateAndNotify(code); |
| 324 | } |
| 325 | |
| 326 | void UpdateAttempterAndroid::ProcessingStopped( |
| 327 | const ActionProcessor* processor) { |
| 328 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 329 | } |
| 330 | |
| 331 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 332 | AbstractAction* action, |
| 333 | ErrorCode code) { |
| 334 | // Reset download progress regardless of whether or not the download |
| 335 | // action succeeded. |
| 336 | const string type = action->Type(); |
| 337 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 338 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 339 | } |
| 340 | if (code != ErrorCode::kSuccess) { |
| 341 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 342 | return; |
| 343 | } |
| 344 | if (type == DownloadAction::StaticType()) { |
| 345 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 350 | uint64_t bytes_received, |
| 351 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 352 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 353 | if (total) |
| 354 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 355 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 356 | download_progress_ = progress; |
| 357 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 358 | } else { |
| 359 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 364 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 365 | // download. |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | void UpdateAttempterAndroid::DownloadComplete() { |
| 370 | // Nothing needs to be done when the download completes. |
| 371 | } |
| 372 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 373 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 374 | // Self throttle based on progress. Also send notifications if progress is |
| 375 | // too slow. |
| 376 | if (progress == 1.0 || |
| 377 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 378 | TimeTicks::Now() - last_notify_time_ >= |
| 379 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 380 | download_progress_ = progress; |
| 381 | SetStatusAndNotify(status_); |
| 382 | } |
| 383 | } |
| 384 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 385 | void UpdateAttempterAndroid::UpdateBootFlags() { |
| 386 | if (updated_boot_flags_) { |
| 387 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 388 | CompleteUpdateBootFlags(true); |
| 389 | return; |
| 390 | } |
| 391 | // This is purely best effort. |
| 392 | LOG(INFO) << "Marking booted slot as good."; |
| 393 | if (!boot_control_->MarkBootSuccessfulAsync( |
| 394 | Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags, |
| 395 | base::Unretained(this)))) { |
| 396 | LOG(ERROR) << "Failed to mark current boot as successful."; |
| 397 | CompleteUpdateBootFlags(false); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) { |
| 402 | updated_boot_flags_ = true; |
| 403 | ScheduleProcessingStart(); |
| 404 | } |
| 405 | |
| 406 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 407 | LOG(INFO) << "Scheduling an action processor start."; |
| 408 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 409 | FROM_HERE, |
| 410 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 411 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 415 | if (status_ == UpdateStatus::IDLE) { |
| 416 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 417 | return; |
| 418 | } |
| 419 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 420 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 421 | actions_.clear(); |
| 422 | UpdateStatus new_status = |
| 423 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 424 | : UpdateStatus::IDLE); |
| 425 | SetStatusAndNotify(new_status); |
| 426 | ongoing_update_ = false; |
| 427 | |
| 428 | for (auto observer : daemon_state_->service_observers()) |
| 429 | observer->SendPayloadApplicationComplete(error_code); |
| 430 | } |
| 431 | |
| 432 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 433 | status_ = status; |
Sen Jiang | 2d1c87b | 2017-07-14 10:46:14 -0700 | [diff] [blame] | 434 | size_t payload_size = |
| 435 | install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 436 | UpdateEngineStatus status_to_send = {.status = status_, |
| 437 | .progress = download_progress_, |
| 438 | .new_size_bytes = payload_size}; |
| 439 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 440 | for (auto observer : daemon_state_->service_observers()) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 441 | observer->SendStatusUpdate(status_to_send); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 442 | } |
| 443 | last_notify_time_ = TimeTicks::Now(); |
| 444 | } |
| 445 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 446 | void UpdateAttempterAndroid::BuildUpdateActions(const string& url) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 447 | CHECK(!processor_->IsRunning()); |
| 448 | processor_->set_delegate(this); |
| 449 | |
| 450 | // Actions: |
| 451 | shared_ptr<InstallPlanAction> install_plan_action( |
| 452 | new InstallPlanAction(install_plan_)); |
| 453 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 454 | HttpFetcher* download_fetcher = nullptr; |
| 455 | if (FileFetcher::SupportedUrl(url)) { |
| 456 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 457 | download_fetcher = new FileFetcher(); |
| 458 | } else { |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 459 | #ifdef _UE_SIDELOAD |
| 460 | LOG(FATAL) << "Unsupported sideload URI: " << url; |
| 461 | #else |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 462 | LibcurlHttpFetcher* libcurl_fetcher = |
| 463 | new LibcurlHttpFetcher(&proxy_resolver_, hardware_); |
| 464 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 465 | download_fetcher = libcurl_fetcher; |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 466 | #endif // _UE_SIDELOAD |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 467 | } |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 468 | shared_ptr<DownloadAction> download_action( |
| 469 | new DownloadAction(prefs_, |
| 470 | boot_control_, |
| 471 | hardware_, |
| 472 | nullptr, // system_state, not used. |
| 473 | download_fetcher)); // passes ownership |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 474 | shared_ptr<FilesystemVerifierAction> filesystem_verifier_action( |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 475 | new FilesystemVerifierAction()); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 476 | |
| 477 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 478 | new PostinstallRunnerAction(boot_control_, hardware_)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 479 | |
| 480 | download_action->set_delegate(this); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 481 | download_action->set_base_offset(base_offset_); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 482 | download_action_ = download_action; |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 483 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 484 | |
| 485 | actions_.push_back(shared_ptr<AbstractAction>(install_plan_action)); |
| 486 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 487 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 488 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
| 489 | |
| 490 | // Bond them together. We have to use the leaf-types when calling |
| 491 | // BondActions(). |
| 492 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 493 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 494 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 495 | postinstall_runner_action.get()); |
| 496 | |
| 497 | // Enqueue the actions. |
| 498 | for (const shared_ptr<AbstractAction>& action : actions_) |
| 499 | processor_->EnqueueAction(action.get()); |
| 500 | } |
| 501 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 502 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 503 | string boot_id; |
| 504 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 505 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() { |
| 510 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 511 | // when the update was completed by setting a pref, so we can check whether |
| 512 | // the last update was on this boot or a previous one. |
| 513 | string boot_id; |
| 514 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 515 | |
| 516 | string update_completed_on_boot_id; |
| 517 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 518 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 519 | &update_completed_on_boot_id) && |
| 520 | update_completed_on_boot_id == boot_id); |
| 521 | } |
| 522 | |
| 523 | } // namespace chromeos_update_engine |