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/multi_range_http_fetcher.h" |
| 34 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 35 | #include "update_engine/daemon_state_interface.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 36 | #include "update_engine/network_selector.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 37 | #include "update_engine/payload_consumer/download_action.h" |
| 38 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
| 39 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 40 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 41 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 42 | #ifndef _UE_SIDELOAD |
| 43 | // Do not include support for external HTTP(s) urls when building |
| 44 | // update_engine_sideload. |
| 45 | #include "update_engine/libcurl_http_fetcher.h" |
| 46 | #endif |
| 47 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 48 | using base::Bind; |
| 49 | using base::TimeDelta; |
| 50 | using base::TimeTicks; |
| 51 | using std::shared_ptr; |
| 52 | using std::string; |
| 53 | using std::vector; |
| 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; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 148 | install_plan_.payload_size = payload_size; |
| 149 | if (!install_plan_.payload_size) { |
| 150 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
| 151 | &install_plan_.payload_size)) { |
| 152 | install_plan_.payload_size = 0; |
| 153 | } |
| 154 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame^] | 155 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
| 156 | &install_plan_.payload_hash)) { |
| 157 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 158 | << headers[kPayloadPropertyFileHash]; |
| 159 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 160 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
| 161 | &install_plan_.metadata_size)) { |
| 162 | install_plan_.metadata_size = 0; |
| 163 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 164 | install_plan_.metadata_signature = ""; |
| 165 | // The |public_key_rsa| key would override the public key stored on disk. |
| 166 | install_plan_.public_key_rsa = ""; |
| 167 | |
| 168 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 169 | install_plan_.is_resume = !payload_id.empty() && |
| 170 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 171 | if (!install_plan_.is_resume) { |
| 172 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 173 | LOG(WARNING) << "Unable to reset the update progress."; |
| 174 | } |
| 175 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 176 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 177 | } |
| 178 | } |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 179 | // The |payload_type| is not used anymore since minor_version 3. |
| 180 | install_plan_.payload_type = InstallPayloadType::kUnknown; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 181 | |
| 182 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 183 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 184 | |
| 185 | int data_wipe = 0; |
| 186 | install_plan_.powerwash_required = |
| 187 | base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) && |
| 188 | data_wipe != 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 189 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 190 | NetworkId network_id = kDefaultNetworkId; |
| 191 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 192 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 193 | &network_id)) { |
| 194 | return LogAndSetError( |
| 195 | error, |
| 196 | FROM_HERE, |
| 197 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 198 | } |
| 199 | if (!network_selector_->SetProcessNetwork(network_id)) { |
| 200 | LOG(WARNING) << "Unable to set network_id, continuing with the update."; |
| 201 | } |
| 202 | } |
| 203 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 204 | LOG(INFO) << "Using this install plan:"; |
| 205 | install_plan_.Dump(); |
| 206 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 207 | BuildUpdateActions(payload_url); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 208 | SetupDownload(); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 209 | // Setup extra headers. |
| 210 | HttpFetcher* fetcher = download_action_->http_fetcher(); |
| 211 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 212 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 213 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 214 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 215 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 216 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 217 | ongoing_update_ = true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 218 | |
| 219 | // Just in case we didn't update boot flags yet, make sure they're updated |
| 220 | // before any update processing starts. This will start the update process. |
| 221 | UpdateBootFlags(); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 226 | if (!ongoing_update_) |
| 227 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 228 | processor_->SuspendProcessing(); |
| 229 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 233 | if (!ongoing_update_) |
| 234 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 235 | processor_->ResumeProcessing(); |
| 236 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 240 | if (!ongoing_update_) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 241 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 242 | processor_->StopProcessing(); |
| 243 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 246 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 247 | LOG(INFO) << "Attempting to reset state from " |
| 248 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 249 | |
| 250 | switch (status_) { |
| 251 | case UpdateStatus::IDLE: |
| 252 | return true; |
| 253 | |
| 254 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
| 255 | // Remove the reboot marker so that if the machine is rebooted |
| 256 | // after resetting to idle state, it doesn't go back to |
| 257 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 258 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
| 259 | |
| 260 | // Update the boot flags so the current slot has higher priority. |
| 261 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 262 | ret_value = false; |
| 263 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 264 | // Mark the current slot as successful again, since marking it as active |
| 265 | // may reset the successful bit. We ignore the result of whether marking |
| 266 | // the current slot as successful worked. |
| 267 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){}))) |
| 268 | ret_value = false; |
| 269 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 270 | if (!ret_value) { |
| 271 | return LogAndSetError( |
| 272 | error, |
| 273 | FROM_HERE, |
| 274 | "Failed to reset the status to "); |
| 275 | } |
| 276 | |
| 277 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 278 | LOG(INFO) << "Reset status successful"; |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | default: |
| 283 | return LogAndSetError( |
| 284 | error, |
| 285 | FROM_HERE, |
| 286 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 287 | } |
| 288 | } |
| 289 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 290 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 291 | ErrorCode code) { |
| 292 | LOG(INFO) << "Processing Done."; |
| 293 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 294 | switch (code) { |
| 295 | case ErrorCode::kSuccess: |
| 296 | // Update succeeded. |
| 297 | WriteUpdateCompletedMarker(); |
| 298 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
| 299 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 300 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 301 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 302 | break; |
| 303 | |
| 304 | case ErrorCode::kFilesystemCopierError: |
| 305 | case ErrorCode::kNewRootfsVerificationError: |
| 306 | case ErrorCode::kNewKernelVerificationError: |
| 307 | case ErrorCode::kFilesystemVerifierError: |
| 308 | case ErrorCode::kDownloadStateInitializationError: |
| 309 | // Reset the ongoing update for these errors so it starts from the |
| 310 | // beginning next time. |
| 311 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 312 | LOG(INFO) << "Resetting update progress."; |
| 313 | break; |
| 314 | |
| 315 | default: |
| 316 | // Ignore all other error codes. |
| 317 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 318 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 319 | |
| 320 | TerminateUpdateAndNotify(code); |
| 321 | } |
| 322 | |
| 323 | void UpdateAttempterAndroid::ProcessingStopped( |
| 324 | const ActionProcessor* processor) { |
| 325 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 326 | } |
| 327 | |
| 328 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 329 | AbstractAction* action, |
| 330 | ErrorCode code) { |
| 331 | // Reset download progress regardless of whether or not the download |
| 332 | // action succeeded. |
| 333 | const string type = action->Type(); |
| 334 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 335 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 336 | } |
| 337 | if (code != ErrorCode::kSuccess) { |
| 338 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 339 | return; |
| 340 | } |
| 341 | if (type == DownloadAction::StaticType()) { |
| 342 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 347 | uint64_t bytes_received, |
| 348 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 349 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 350 | if (total) |
| 351 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 352 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 353 | download_progress_ = progress; |
| 354 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 355 | } else { |
| 356 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 361 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 362 | // download. |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | void UpdateAttempterAndroid::DownloadComplete() { |
| 367 | // Nothing needs to be done when the download completes. |
| 368 | } |
| 369 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 370 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 371 | // Self throttle based on progress. Also send notifications if progress is |
| 372 | // too slow. |
| 373 | if (progress == 1.0 || |
| 374 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 375 | TimeTicks::Now() - last_notify_time_ >= |
| 376 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 377 | download_progress_ = progress; |
| 378 | SetStatusAndNotify(status_); |
| 379 | } |
| 380 | } |
| 381 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 382 | void UpdateAttempterAndroid::UpdateBootFlags() { |
| 383 | if (updated_boot_flags_) { |
| 384 | LOG(INFO) << "Already updated boot flags. Skipping."; |
| 385 | CompleteUpdateBootFlags(true); |
| 386 | return; |
| 387 | } |
| 388 | // This is purely best effort. |
| 389 | LOG(INFO) << "Marking booted slot as good."; |
| 390 | if (!boot_control_->MarkBootSuccessfulAsync( |
| 391 | Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags, |
| 392 | base::Unretained(this)))) { |
| 393 | LOG(ERROR) << "Failed to mark current boot as successful."; |
| 394 | CompleteUpdateBootFlags(false); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) { |
| 399 | updated_boot_flags_ = true; |
| 400 | ScheduleProcessingStart(); |
| 401 | } |
| 402 | |
| 403 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 404 | LOG(INFO) << "Scheduling an action processor start."; |
| 405 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 406 | FROM_HERE, |
| 407 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 408 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 412 | if (status_ == UpdateStatus::IDLE) { |
| 413 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 414 | return; |
| 415 | } |
| 416 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 417 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 418 | actions_.clear(); |
| 419 | UpdateStatus new_status = |
| 420 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 421 | : UpdateStatus::IDLE); |
| 422 | SetStatusAndNotify(new_status); |
| 423 | ongoing_update_ = false; |
| 424 | |
| 425 | for (auto observer : daemon_state_->service_observers()) |
| 426 | observer->SendPayloadApplicationComplete(error_code); |
| 427 | } |
| 428 | |
| 429 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 430 | status_ = status; |
| 431 | for (auto observer : daemon_state_->service_observers()) { |
| 432 | observer->SendStatusUpdate( |
| 433 | 0, download_progress_, status_, "", install_plan_.payload_size); |
| 434 | } |
| 435 | last_notify_time_ = TimeTicks::Now(); |
| 436 | } |
| 437 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 438 | void UpdateAttempterAndroid::BuildUpdateActions(const string& url) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 439 | CHECK(!processor_->IsRunning()); |
| 440 | processor_->set_delegate(this); |
| 441 | |
| 442 | // Actions: |
| 443 | shared_ptr<InstallPlanAction> install_plan_action( |
| 444 | new InstallPlanAction(install_plan_)); |
| 445 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 446 | HttpFetcher* download_fetcher = nullptr; |
| 447 | if (FileFetcher::SupportedUrl(url)) { |
| 448 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 449 | download_fetcher = new FileFetcher(); |
| 450 | } else { |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 451 | #ifdef _UE_SIDELOAD |
| 452 | LOG(FATAL) << "Unsupported sideload URI: " << url; |
| 453 | #else |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 454 | LibcurlHttpFetcher* libcurl_fetcher = |
| 455 | new LibcurlHttpFetcher(&proxy_resolver_, hardware_); |
| 456 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 457 | download_fetcher = libcurl_fetcher; |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 458 | #endif // _UE_SIDELOAD |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 459 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 460 | shared_ptr<DownloadAction> download_action(new DownloadAction( |
| 461 | prefs_, |
| 462 | boot_control_, |
| 463 | hardware_, |
| 464 | nullptr, // system_state, not used. |
| 465 | new MultiRangeHttpFetcher(download_fetcher))); // passes ownership |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 466 | shared_ptr<FilesystemVerifierAction> filesystem_verifier_action( |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 467 | new FilesystemVerifierAction()); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 468 | |
| 469 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action( |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 470 | new PostinstallRunnerAction(boot_control_, hardware_)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 471 | |
| 472 | download_action->set_delegate(this); |
| 473 | download_action_ = download_action; |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 474 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 475 | |
| 476 | actions_.push_back(shared_ptr<AbstractAction>(install_plan_action)); |
| 477 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 478 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 479 | actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); |
| 480 | |
| 481 | // Bond them together. We have to use the leaf-types when calling |
| 482 | // BondActions(). |
| 483 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 484 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 485 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 486 | postinstall_runner_action.get()); |
| 487 | |
| 488 | // Enqueue the actions. |
| 489 | for (const shared_ptr<AbstractAction>& action : actions_) |
| 490 | processor_->EnqueueAction(action.get()); |
| 491 | } |
| 492 | |
| 493 | void UpdateAttempterAndroid::SetupDownload() { |
| 494 | MultiRangeHttpFetcher* fetcher = |
| 495 | static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher()); |
| 496 | fetcher->ClearRanges(); |
| 497 | if (install_plan_.is_resume) { |
| 498 | // Resuming an update so fetch the update manifest metadata first. |
| 499 | int64_t manifest_metadata_size = 0; |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 500 | int64_t manifest_signature_size = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 501 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 502 | prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size); |
| 503 | fetcher->AddRange(base_offset_, |
| 504 | manifest_metadata_size + manifest_signature_size); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 505 | // If there're remaining unprocessed data blobs, fetch them. Be careful not |
| 506 | // to request data beyond the end of the payload to avoid 416 HTTP response |
| 507 | // error codes. |
| 508 | int64_t next_data_offset = 0; |
| 509 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 510 | uint64_t resume_offset = |
| 511 | manifest_metadata_size + manifest_signature_size + next_data_offset; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 512 | if (!install_plan_.payload_size) { |
| 513 | fetcher->AddRange(base_offset_ + resume_offset); |
| 514 | } else if (resume_offset < install_plan_.payload_size) { |
| 515 | fetcher->AddRange(base_offset_ + resume_offset, |
| 516 | install_plan_.payload_size - resume_offset); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 517 | } |
| 518 | } else { |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 519 | if (install_plan_.payload_size) { |
| 520 | fetcher->AddRange(base_offset_, install_plan_.payload_size); |
| 521 | } else { |
| 522 | // If no payload size is passed we assume we read until the end of the |
| 523 | // stream. |
| 524 | fetcher->AddRange(base_offset_); |
| 525 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 530 | string boot_id; |
| 531 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 532 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() { |
| 537 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 538 | // when the update was completed by setting a pref, so we can check whether |
| 539 | // the last update was on this boot or a previous one. |
| 540 | string boot_id; |
| 541 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 542 | |
| 543 | string update_completed_on_boot_id; |
| 544 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 545 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 546 | &update_completed_on_boot_id) && |
| 547 | update_completed_on_boot_id == boot_id); |
| 548 | } |
| 549 | |
| 550 | } // namespace chromeos_update_engine |