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