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> |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 25 | #include <android-base/unique_fd.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 26 | #include <base/bind.h> |
| 27 | #include <base/logging.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 28 | #include <base/strings/string_number_conversions.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 29 | #include <brillo/data_encoding.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 30 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 31 | #include <brillo/strings/string_utils.h> |
Sen Jiang | acbdd1c | 2017-10-19 13:30:19 -0700 | [diff] [blame] | 32 | #include <log/log_safetynet.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 33 | |
| 34 | #include "update_engine/common/constants.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 35 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 36 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 37 | #include "update_engine/common/utils.h" |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 38 | #include "update_engine/daemon_state_interface.h" |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 39 | #include "update_engine/metrics_reporter_interface.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 40 | #include "update_engine/metrics_utils.h" |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 41 | #include "update_engine/network_selector.h" |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame^] | 42 | #include "update_engine/payload_consumer/certificate_parser_interface.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 43 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 44 | #include "update_engine/payload_consumer/download_action.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 45 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 46 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 47 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 48 | #include "update_engine/payload_consumer/payload_constants.h" |
| 49 | #include "update_engine/payload_consumer/payload_metadata.h" |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame^] | 50 | #include "update_engine/payload_consumer/payload_verifier.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 51 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 52 | #include "update_engine/update_boot_flags_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 53 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 54 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 55 | #ifndef _UE_SIDELOAD |
| 56 | // Do not include support for external HTTP(s) urls when building |
| 57 | // update_engine_sideload. |
| 58 | #include "update_engine/libcurl_http_fetcher.h" |
| 59 | #endif |
| 60 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 61 | using android::base::unique_fd; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 62 | using base::Bind; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 63 | using base::Time; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 64 | using base::TimeDelta; |
| 65 | using base::TimeTicks; |
| 66 | using std::shared_ptr; |
| 67 | using std::string; |
| 68 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 69 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 70 | |
| 71 | namespace chromeos_update_engine { |
| 72 | |
| 73 | namespace { |
| 74 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 75 | // Minimum threshold to broadcast an status update in progress and time. |
| 76 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 77 | const int kBroadcastThresholdSeconds = 10; |
| 78 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 79 | const char* const kErrorDomain = "update_engine"; |
| 80 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 81 | // back on the service error. |
| 82 | const char* const kGenericError = "generic_error"; |
| 83 | |
| 84 | // Log and set the error on the passed ErrorPtr. |
| 85 | bool LogAndSetError(brillo::ErrorPtr* error, |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 86 | const base::Location& location, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 87 | const string& reason) { |
| 88 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 89 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 90 | << reason; |
| 91 | return false; |
| 92 | } |
| 93 | |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 94 | bool GetHeaderAsBool(const string& header, bool default_value) { |
| 95 | int value = 0; |
| 96 | if (base::StringToInt(header, &value) && (value == 0 || value == 1)) |
| 97 | return value == 1; |
| 98 | return default_value; |
| 99 | } |
| 100 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 101 | } // namespace |
| 102 | |
| 103 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 104 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 105 | PrefsInterface* prefs, |
| 106 | BootControlInterface* boot_control, |
| 107 | HardwareInterface* hardware) |
| 108 | : daemon_state_(daemon_state), |
| 109 | prefs_(prefs), |
| 110 | boot_control_(boot_control), |
| 111 | hardware_(hardware), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 112 | processor_(new ActionProcessor()), |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 113 | clock_(new Clock()) { |
| 114 | metrics_reporter_ = metrics::CreateMetricsReporter(); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 115 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 119 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 120 | // re-scheduling the updates due to the processing stopped. |
| 121 | processor_->set_delegate(nullptr); |
| 122 | } |
| 123 | |
| 124 | void UpdateAttempterAndroid::Init() { |
| 125 | // In case of update_engine restart without a reboot we need to restore the |
| 126 | // reboot needed state. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 127 | if (UpdateCompletedOnThisBoot()) { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 128 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 129 | } else { |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 130 | SetStatusAndNotify(UpdateStatus::IDLE); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 131 | UpdatePrefsAndReportUpdateMetricsOnReboot(); |
| 132 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool UpdateAttempterAndroid::ApplyPayload( |
| 136 | const string& payload_url, |
| 137 | int64_t payload_offset, |
| 138 | int64_t payload_size, |
| 139 | const vector<string>& key_value_pair_headers, |
| 140 | brillo::ErrorPtr* error) { |
| 141 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 142 | return LogAndSetError( |
| 143 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 144 | } |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 145 | if (processor_->IsRunning()) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 146 | return LogAndSetError( |
| 147 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 148 | } |
| 149 | DCHECK(status_ == UpdateStatus::IDLE); |
| 150 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 151 | std::map<string, string> headers; |
| 152 | for (const string& key_value_pair : key_value_pair_headers) { |
| 153 | string key; |
| 154 | string value; |
| 155 | if (!brillo::string_utils::SplitAtFirst( |
| 156 | key_value_pair, "=", &key, &value, false)) { |
| 157 | return LogAndSetError( |
| 158 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 159 | } |
| 160 | if (!headers.emplace(key, value).second) |
| 161 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 162 | } |
| 163 | |
| 164 | // Unique identifier for the payload. An empty string means that the payload |
| 165 | // can't be resumed. |
| 166 | string payload_id = (headers[kPayloadPropertyFileHash] + |
| 167 | headers[kPayloadPropertyMetadataHash]); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 168 | |
| 169 | // Setup the InstallPlan based on the request. |
| 170 | install_plan_ = InstallPlan(); |
| 171 | |
| 172 | install_plan_.download_url = payload_url; |
| 173 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 174 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 175 | InstallPlan::Payload payload; |
| 176 | payload.size = payload_size; |
| 177 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 178 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 179 | &payload.size)) { |
| 180 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 183 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 184 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 185 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 186 | << headers[kPayloadPropertyFileHash]; |
| 187 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 188 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 189 | &payload.metadata_size)) { |
| 190 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 191 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 192 | // The |payload.type| is not used anymore since minor_version 3. |
| 193 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 194 | install_plan_.payloads.push_back(payload); |
| 195 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 196 | // The |public_key_rsa| key would override the public key stored on disk. |
| 197 | install_plan_.public_key_rsa = ""; |
| 198 | |
| 199 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 200 | install_plan_.is_resume = !payload_id.empty() && |
| 201 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 202 | if (!install_plan_.is_resume) { |
| 203 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 204 | LOG(WARNING) << "Unable to reset the update progress."; |
| 205 | } |
| 206 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 207 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 208 | } |
| 209 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 210 | install_plan_.source_slot = boot_control_->GetCurrentSlot(); |
| 211 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 212 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 213 | install_plan_.powerwash_required = |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 214 | GetHeaderAsBool(headers[kPayloadPropertyPowerwash], false); |
| 215 | |
| 216 | install_plan_.switch_slot_on_reboot = |
| 217 | GetHeaderAsBool(headers[kPayloadPropertySwitchSlotOnReboot], true); |
| 218 | |
| 219 | install_plan_.run_post_install = true; |
| 220 | // Optionally skip post install if and only if: |
| 221 | // a) we're resuming |
| 222 | // b) post install has already succeeded before |
| 223 | // c) RUN_POST_INSTALL is set to 0. |
| 224 | if (install_plan_.is_resume && prefs_->Exists(kPrefsPostInstallSucceeded)) { |
| 225 | bool post_install_succeeded = false; |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 226 | if (prefs_->GetBoolean(kPrefsPostInstallSucceeded, |
| 227 | &post_install_succeeded) && |
| 228 | post_install_succeeded) { |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 229 | install_plan_.run_post_install = |
| 230 | GetHeaderAsBool(headers[kPayloadPropertyRunPostInstall], true); |
| 231 | } |
| 232 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 233 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 234 | // Skip writing verity if we're resuming and verity has already been written. |
| 235 | install_plan_.write_verity = true; |
| 236 | if (install_plan_.is_resume && prefs_->Exists(kPrefsVerityWritten)) { |
| 237 | bool verity_written = false; |
| 238 | if (prefs_->GetBoolean(kPrefsVerityWritten, &verity_written) && |
| 239 | verity_written) { |
| 240 | install_plan_.write_verity = false; |
| 241 | } |
| 242 | } |
| 243 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 244 | NetworkId network_id = kDefaultNetworkId; |
| 245 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 246 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 247 | &network_id)) { |
| 248 | return LogAndSetError( |
| 249 | error, |
| 250 | FROM_HERE, |
| 251 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 252 | } |
| 253 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 254 | return LogAndSetError( |
| 255 | error, |
| 256 | FROM_HERE, |
| 257 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 261 | LOG(INFO) << "Using this install plan:"; |
| 262 | install_plan_.Dump(); |
| 263 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 264 | HttpFetcher* fetcher = nullptr; |
| 265 | if (FileFetcher::SupportedUrl(payload_url)) { |
| 266 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 267 | fetcher = new FileFetcher(); |
| 268 | } else { |
| 269 | #ifdef _UE_SIDELOAD |
| 270 | LOG(FATAL) << "Unsupported sideload URI: " << payload_url; |
| 271 | #else |
| 272 | LibcurlHttpFetcher* libcurl_fetcher = |
| 273 | new LibcurlHttpFetcher(&proxy_resolver_, hardware_); |
| 274 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 275 | fetcher = libcurl_fetcher; |
| 276 | #endif // _UE_SIDELOAD |
| 277 | } |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 278 | // Setup extra headers. |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 279 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 280 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 281 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 282 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 283 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 284 | BuildUpdateActions(fetcher); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 285 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 286 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 287 | |
| 288 | UpdatePrefsOnUpdateStart(install_plan_.is_resume); |
| 289 | // TODO(xunchang) report the metrics for unresumable updates |
| 290 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 291 | ScheduleProcessingStart(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 295 | bool UpdateAttempterAndroid::ApplyPayload( |
| 296 | int fd, |
| 297 | int64_t payload_offset, |
| 298 | int64_t payload_size, |
| 299 | const vector<string>& key_value_pair_headers, |
| 300 | brillo::ErrorPtr* error) { |
| 301 | payload_fd_.reset(dup(fd)); |
| 302 | const string payload_url = "fd://" + std::to_string(payload_fd_.get()); |
| 303 | |
| 304 | return ApplyPayload( |
| 305 | payload_url, payload_offset, payload_size, key_value_pair_headers, error); |
| 306 | } |
| 307 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 308 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 309 | if (!processor_->IsRunning()) |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 310 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 311 | processor_->SuspendProcessing(); |
| 312 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 316 | if (!processor_->IsRunning()) |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 317 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 318 | processor_->ResumeProcessing(); |
| 319 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 323 | if (!processor_->IsRunning()) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 324 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 325 | processor_->StopProcessing(); |
| 326 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 329 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 330 | LOG(INFO) << "Attempting to reset state from " |
| 331 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
| 332 | |
| 333 | switch (status_) { |
| 334 | case UpdateStatus::IDLE: |
| 335 | return true; |
| 336 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 337 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 338 | // Remove the reboot marker so that if the machine is rebooted |
| 339 | // after resetting to idle state, it doesn't go back to |
| 340 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 341 | bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 342 | ClearMetricsPrefs(); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 343 | |
| 344 | // Update the boot flags so the current slot has higher priority. |
| 345 | if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot())) |
| 346 | ret_value = false; |
| 347 | |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 348 | // Mark the current slot as successful again, since marking it as active |
| 349 | // may reset the successful bit. We ignore the result of whether marking |
| 350 | // the current slot as successful worked. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 351 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful) {}))) |
Alex Deymo | 5259033 | 2016-11-29 18:29:13 -0800 | [diff] [blame] | 352 | ret_value = false; |
| 353 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 354 | if (!ret_value) { |
| 355 | return LogAndSetError( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 356 | error, FROM_HERE, "Failed to reset the status to "); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 360 | LOG(INFO) << "Reset status successful"; |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | default: |
| 365 | return LogAndSetError( |
| 366 | error, |
| 367 | FROM_HERE, |
| 368 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 369 | } |
| 370 | } |
| 371 | |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 372 | bool UpdateAttempterAndroid::VerifyPayloadApplicable( |
| 373 | const std::string& metadata_filename, brillo::ErrorPtr* error) { |
| 374 | FileDescriptorPtr fd(new EintrSafeFileDescriptor); |
| 375 | if (!fd->Open(metadata_filename.c_str(), O_RDONLY)) { |
| 376 | return LogAndSetError( |
| 377 | error, FROM_HERE, "Failed to open " + metadata_filename); |
| 378 | } |
| 379 | brillo::Blob metadata(kMaxPayloadHeaderSize); |
| 380 | if (!fd->Read(metadata.data(), metadata.size())) { |
| 381 | return LogAndSetError( |
| 382 | error, |
| 383 | FROM_HERE, |
| 384 | "Failed to read payload header from " + metadata_filename); |
| 385 | } |
| 386 | ErrorCode errorcode; |
| 387 | PayloadMetadata payload_metadata; |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 388 | if (payload_metadata.ParsePayloadHeader(metadata, &errorcode) != |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 389 | MetadataParseResult::kSuccess) { |
| 390 | return LogAndSetError(error, |
| 391 | FROM_HERE, |
| 392 | "Failed to parse payload header: " + |
| 393 | utils::ErrorCodeToString(errorcode)); |
| 394 | } |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 395 | uint64_t metadata_size = payload_metadata.GetMetadataSize() + |
| 396 | payload_metadata.GetMetadataSignatureSize(); |
| 397 | if (metadata_size < kMaxPayloadHeaderSize || |
| 398 | metadata_size > |
| 399 | static_cast<uint64_t>(utils::FileSize(metadata_filename))) { |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 400 | return LogAndSetError( |
| 401 | error, |
| 402 | FROM_HERE, |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 403 | "Invalid metadata size: " + std::to_string(metadata_size)); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 404 | } |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 405 | metadata.resize(metadata_size); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 406 | if (!fd->Read(metadata.data() + kMaxPayloadHeaderSize, |
| 407 | metadata.size() - kMaxPayloadHeaderSize)) { |
| 408 | return LogAndSetError( |
| 409 | error, |
| 410 | FROM_HERE, |
| 411 | "Failed to read metadata and signature from " + metadata_filename); |
| 412 | } |
| 413 | fd->Close(); |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 414 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame^] | 415 | auto payload_verifier = PayloadVerifier::CreateInstanceFromZipPath( |
| 416 | constants::kUpdateCertificatesPath); |
| 417 | if (!payload_verifier) { |
| 418 | return LogAndSetError(error, |
| 419 | FROM_HERE, |
| 420 | "Failed to create the payload verifier from " + |
| 421 | std::string(constants::kUpdateCertificatesPath)); |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 422 | } |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame^] | 423 | errorcode = payload_metadata.ValidateMetadataSignature( |
| 424 | metadata, "", *payload_verifier); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 425 | if (errorcode != ErrorCode::kSuccess) { |
| 426 | return LogAndSetError(error, |
| 427 | FROM_HERE, |
| 428 | "Failed to validate metadata signature: " + |
| 429 | utils::ErrorCodeToString(errorcode)); |
| 430 | } |
| 431 | DeltaArchiveManifest manifest; |
| 432 | if (!payload_metadata.GetManifest(metadata, &manifest)) { |
| 433 | return LogAndSetError(error, FROM_HERE, "Failed to parse manifest."); |
| 434 | } |
| 435 | |
| 436 | BootControlInterface::Slot current_slot = boot_control_->GetCurrentSlot(); |
| 437 | for (const PartitionUpdate& partition : manifest.partitions()) { |
| 438 | if (!partition.has_old_partition_info()) |
| 439 | continue; |
| 440 | string partition_path; |
| 441 | if (!boot_control_->GetPartitionDevice( |
| 442 | partition.partition_name(), current_slot, &partition_path)) { |
| 443 | return LogAndSetError( |
| 444 | error, |
| 445 | FROM_HERE, |
| 446 | "Failed to get partition device for " + partition.partition_name()); |
| 447 | } |
| 448 | if (!fd->Open(partition_path.c_str(), O_RDONLY)) { |
| 449 | return LogAndSetError( |
| 450 | error, FROM_HERE, "Failed to open " + partition_path); |
| 451 | } |
| 452 | for (const InstallOperation& operation : partition.operations()) { |
| 453 | if (!operation.has_src_sha256_hash()) |
| 454 | continue; |
| 455 | brillo::Blob source_hash; |
| 456 | if (!fd_utils::ReadAndHashExtents(fd, |
| 457 | operation.src_extents(), |
| 458 | manifest.block_size(), |
| 459 | &source_hash)) { |
| 460 | return LogAndSetError( |
| 461 | error, FROM_HERE, "Failed to hash " + partition_path); |
| 462 | } |
| 463 | if (!DeltaPerformer::ValidateSourceHash( |
| 464 | source_hash, operation, fd, &errorcode)) { |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | fd->Close(); |
| 469 | } |
| 470 | return true; |
| 471 | } |
| 472 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 473 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 474 | ErrorCode code) { |
| 475 | LOG(INFO) << "Processing Done."; |
| 476 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 477 | switch (code) { |
| 478 | case ErrorCode::kSuccess: |
| 479 | // Update succeeded. |
| 480 | WriteUpdateCompletedMarker(); |
| 481 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 482 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 483 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 484 | break; |
| 485 | |
| 486 | case ErrorCode::kFilesystemCopierError: |
| 487 | case ErrorCode::kNewRootfsVerificationError: |
| 488 | case ErrorCode::kNewKernelVerificationError: |
| 489 | case ErrorCode::kFilesystemVerifierError: |
| 490 | case ErrorCode::kDownloadStateInitializationError: |
| 491 | // Reset the ongoing update for these errors so it starts from the |
| 492 | // beginning next time. |
| 493 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 494 | LOG(INFO) << "Resetting update progress."; |
| 495 | break; |
| 496 | |
Sen Jiang | acbdd1c | 2017-10-19 13:30:19 -0700 | [diff] [blame] | 497 | case ErrorCode::kPayloadTimestampError: |
| 498 | // SafetyNet logging, b/36232423 |
| 499 | android_errorWriteLog(0x534e4554, "36232423"); |
| 500 | break; |
| 501 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 502 | default: |
| 503 | // Ignore all other error codes. |
| 504 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 505 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 506 | |
| 507 | TerminateUpdateAndNotify(code); |
| 508 | } |
| 509 | |
| 510 | void UpdateAttempterAndroid::ProcessingStopped( |
| 511 | const ActionProcessor* processor) { |
| 512 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 513 | } |
| 514 | |
| 515 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 516 | AbstractAction* action, |
| 517 | ErrorCode code) { |
| 518 | // Reset download progress regardless of whether or not the download |
| 519 | // action succeeded. |
| 520 | const string type = action->Type(); |
| 521 | if (type == DownloadAction::StaticType()) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 522 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 523 | } |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 524 | if (type == PostinstallRunnerAction::StaticType()) { |
| 525 | bool succeeded = |
| 526 | code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive; |
| 527 | prefs_->SetBoolean(kPrefsPostInstallSucceeded, succeeded); |
| 528 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 529 | if (code != ErrorCode::kSuccess) { |
| 530 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 531 | return; |
| 532 | } |
| 533 | if (type == DownloadAction::StaticType()) { |
| 534 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 535 | } else if (type == FilesystemVerifierAction::StaticType()) { |
| 536 | prefs_->SetBoolean(kPrefsVerityWritten, true); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 541 | uint64_t bytes_received, |
| 542 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 543 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 544 | if (total) |
| 545 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 546 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 547 | download_progress_ = progress; |
| 548 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 549 | } else { |
| 550 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 551 | } |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 552 | |
| 553 | // Update the bytes downloaded in prefs. |
| 554 | int64_t current_bytes_downloaded = |
| 555 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, prefs_); |
| 556 | int64_t total_bytes_downloaded = |
| 557 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, prefs_); |
| 558 | prefs_->SetInt64(kPrefsCurrentBytesDownloaded, |
| 559 | current_bytes_downloaded + bytes_progressed); |
| 560 | prefs_->SetInt64(kPrefsTotalBytesDownloaded, |
| 561 | total_bytes_downloaded + bytes_progressed); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 565 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 566 | // download. |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | void UpdateAttempterAndroid::DownloadComplete() { |
| 571 | // Nothing needs to be done when the download completes. |
| 572 | } |
| 573 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 574 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 575 | // Self throttle based on progress. Also send notifications if progress is |
| 576 | // too slow. |
| 577 | if (progress == 1.0 || |
| 578 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 579 | TimeTicks::Now() - last_notify_time_ >= |
| 580 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 581 | download_progress_ = progress; |
| 582 | SetStatusAndNotify(status_); |
| 583 | } |
| 584 | } |
| 585 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 586 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 587 | LOG(INFO) << "Scheduling an action processor start."; |
| 588 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 589 | FROM_HERE, |
| 590 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 591 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 595 | if (status_ == UpdateStatus::IDLE) { |
| 596 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 597 | return; |
| 598 | } |
| 599 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 600 | boot_control_->Cleanup(); |
| 601 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 602 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 603 | UpdateStatus new_status = |
| 604 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 605 | : UpdateStatus::IDLE); |
| 606 | SetStatusAndNotify(new_status); |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 607 | payload_fd_.reset(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 608 | |
Sen Jiang | b19c3ec | 2017-10-06 15:18:46 -0700 | [diff] [blame] | 609 | // The network id is only applicable to one download attempt and once it's |
| 610 | // done the network id should not be re-used anymore. |
| 611 | if (!network_selector_->SetProcessNetwork(kDefaultNetworkId)) { |
| 612 | LOG(WARNING) << "Unable to unbind network."; |
| 613 | } |
| 614 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 615 | for (auto observer : daemon_state_->service_observers()) |
| 616 | observer->SendPayloadApplicationComplete(error_code); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 617 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 618 | CollectAndReportUpdateMetricsOnUpdateFinished(error_code); |
| 619 | ClearMetricsPrefs(); |
| 620 | if (error_code == ErrorCode::kSuccess) { |
xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 621 | // We should only reset the PayloadAttemptNumber if the update succeeds, or |
| 622 | // we switch to a different payload. |
| 623 | prefs_->Delete(kPrefsPayloadAttemptNumber); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 624 | metrics_utils::SetSystemUpdatedMarker(clock_.get(), prefs_); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 625 | // Clear the total bytes downloaded if and only if the update succeeds. |
| 626 | prefs_->SetInt64(kPrefsTotalBytesDownloaded, 0); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 627 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 631 | status_ = status; |
Sen Jiang | 2d1c87b | 2017-07-14 10:46:14 -0700 | [diff] [blame] | 632 | size_t payload_size = |
| 633 | install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 634 | UpdateEngineStatus status_to_send = {.status = status_, |
| 635 | .progress = download_progress_, |
| 636 | .new_size_bytes = payload_size}; |
| 637 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 638 | for (auto observer : daemon_state_->service_observers()) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 639 | observer->SendStatusUpdate(status_to_send); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 640 | } |
| 641 | last_notify_time_ = TimeTicks::Now(); |
| 642 | } |
| 643 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 644 | void UpdateAttempterAndroid::BuildUpdateActions(HttpFetcher* fetcher) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 645 | CHECK(!processor_->IsRunning()); |
| 646 | processor_->set_delegate(this); |
| 647 | |
| 648 | // Actions: |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 649 | auto update_boot_flags_action = |
| 650 | std::make_unique<UpdateBootFlagsAction>(boot_control_); |
| 651 | auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan_); |
| 652 | auto download_action = |
| 653 | std::make_unique<DownloadAction>(prefs_, |
| 654 | boot_control_, |
| 655 | hardware_, |
| 656 | nullptr, // system_state, not used. |
| 657 | fetcher, // passes ownership |
| 658 | true /* interactive */); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 659 | download_action->set_delegate(this); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 660 | download_action->set_base_offset(base_offset_); |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 661 | auto filesystem_verifier_action = |
| 662 | std::make_unique<FilesystemVerifierAction>(); |
| 663 | auto postinstall_runner_action = |
| 664 | std::make_unique<PostinstallRunnerAction>(boot_control_, hardware_); |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 665 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 666 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 667 | // Bond them together. We have to use the leaf-types when calling |
| 668 | // BondActions(). |
| 669 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 670 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 671 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 672 | postinstall_runner_action.get()); |
| 673 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 674 | processor_->EnqueueAction(std::move(update_boot_flags_action)); |
| 675 | processor_->EnqueueAction(std::move(install_plan_action)); |
| 676 | processor_->EnqueueAction(std::move(download_action)); |
| 677 | processor_->EnqueueAction(std::move(filesystem_verifier_action)); |
| 678 | processor_->EnqueueAction(std::move(postinstall_runner_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 681 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 682 | string boot_id; |
| 683 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 684 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id); |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() { |
| 689 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 690 | // when the update was completed by setting a pref, so we can check whether |
| 691 | // the last update was on this boot or a previous one. |
| 692 | string boot_id; |
| 693 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 694 | |
| 695 | string update_completed_on_boot_id; |
| 696 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 697 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 698 | &update_completed_on_boot_id) && |
| 699 | update_completed_on_boot_id == boot_id); |
| 700 | } |
| 701 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 702 | // Collect and report the android metrics when we terminate the update. |
| 703 | void UpdateAttempterAndroid::CollectAndReportUpdateMetricsOnUpdateFinished( |
| 704 | ErrorCode error_code) { |
| 705 | int64_t attempt_number = |
| 706 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 707 | PayloadType payload_type = kPayloadTypeFull; |
| 708 | int64_t payload_size = 0; |
| 709 | for (const auto& p : install_plan_.payloads) { |
| 710 | if (p.type == InstallPayloadType::kDelta) |
| 711 | payload_type = kPayloadTypeDelta; |
| 712 | payload_size += p.size; |
| 713 | } |
| 714 | |
| 715 | metrics::AttemptResult attempt_result = |
| 716 | metrics_utils::GetAttemptResult(error_code); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 717 | Time boot_time_start = Time::FromInternalValue( |
| 718 | metrics_utils::GetPersistedValue(kPrefsUpdateBootTimestampStart, prefs_)); |
| 719 | Time monotonic_time_start = Time::FromInternalValue( |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 720 | metrics_utils::GetPersistedValue(kPrefsUpdateTimestampStart, prefs_)); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 721 | TimeDelta duration = clock_->GetBootTime() - boot_time_start; |
| 722 | TimeDelta duration_uptime = clock_->GetMonotonicTime() - monotonic_time_start; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 723 | |
| 724 | metrics_reporter_->ReportUpdateAttemptMetrics( |
| 725 | nullptr, // system_state |
| 726 | static_cast<int>(attempt_number), |
| 727 | payload_type, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 728 | duration, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 729 | duration_uptime, |
| 730 | payload_size, |
| 731 | attempt_result, |
| 732 | error_code); |
| 733 | |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 734 | int64_t current_bytes_downloaded = |
| 735 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, prefs_); |
| 736 | metrics_reporter_->ReportUpdateAttemptDownloadMetrics( |
| 737 | current_bytes_downloaded, |
| 738 | 0, |
| 739 | DownloadSource::kNumDownloadSources, |
| 740 | metrics::DownloadErrorCode::kUnset, |
| 741 | metrics::ConnectionType::kUnset); |
| 742 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 743 | if (error_code == ErrorCode::kSuccess) { |
| 744 | int64_t reboot_count = |
| 745 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 746 | string build_version; |
| 747 | prefs_->GetString(kPrefsPreviousVersion, &build_version); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 748 | |
| 749 | // For android metrics, we only care about the total bytes downloaded |
| 750 | // for all sources; for now we assume the only download source is |
| 751 | // HttpsServer. |
| 752 | int64_t total_bytes_downloaded = |
| 753 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, prefs_); |
| 754 | int64_t num_bytes_downloaded[kNumDownloadSources] = {}; |
| 755 | num_bytes_downloaded[DownloadSource::kDownloadSourceHttpsServer] = |
| 756 | total_bytes_downloaded; |
| 757 | |
| 758 | int download_overhead_percentage = 0; |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 759 | if (total_bytes_downloaded >= payload_size) { |
| 760 | CHECK_GT(payload_size, 0); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 761 | download_overhead_percentage = |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 762 | (total_bytes_downloaded - payload_size) * 100ull / payload_size; |
| 763 | } else { |
| 764 | LOG(WARNING) << "Downloaded bytes " << total_bytes_downloaded |
| 765 | << " is smaller than the payload size " << payload_size; |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 766 | } |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 767 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 768 | metrics_reporter_->ReportSuccessfulUpdateMetrics( |
| 769 | static_cast<int>(attempt_number), |
| 770 | 0, // update abandoned count |
| 771 | payload_type, |
| 772 | payload_size, |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 773 | num_bytes_downloaded, |
| 774 | download_overhead_percentage, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 775 | duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 776 | duration_uptime, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 777 | static_cast<int>(reboot_count), |
| 778 | 0); // url_switch_count |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void UpdateAttempterAndroid::UpdatePrefsAndReportUpdateMetricsOnReboot() { |
| 783 | string current_boot_id; |
| 784 | TEST_AND_RETURN(utils::GetBootId(¤t_boot_id)); |
| 785 | // Example: [ro.build.version.incremental]: [4292972] |
| 786 | string current_version = |
| 787 | android::base::GetProperty("ro.build.version.incremental", ""); |
| 788 | TEST_AND_RETURN(!current_version.empty()); |
| 789 | |
| 790 | // If there's no record of previous version (e.g. due to a data wipe), we |
| 791 | // save the info of current boot and skip the metrics report. |
| 792 | if (!prefs_->Exists(kPrefsPreviousVersion)) { |
| 793 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 794 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 795 | ClearMetricsPrefs(); |
| 796 | return; |
| 797 | } |
| 798 | string previous_version; |
| 799 | // update_engine restarted under the same build. |
| 800 | // TODO(xunchang) identify and report rollback by checking UpdateMarker. |
| 801 | if (prefs_->GetString(kPrefsPreviousVersion, &previous_version) && |
| 802 | previous_version == current_version) { |
| 803 | string last_boot_id; |
| 804 | bool is_reboot = prefs_->Exists(kPrefsBootId) && |
| 805 | (prefs_->GetString(kPrefsBootId, &last_boot_id) && |
| 806 | last_boot_id != current_boot_id); |
| 807 | // Increment the reboot number if |kPrefsNumReboots| exists. That pref is |
| 808 | // set when we start a new update. |
| 809 | if (is_reboot && prefs_->Exists(kPrefsNumReboots)) { |
| 810 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 811 | int64_t reboot_count = |
| 812 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 813 | metrics_utils::SetNumReboots(reboot_count + 1, prefs_); |
| 814 | } |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | // Now that the build version changes, report the update metrics. |
| 819 | // TODO(xunchang) check the build version is larger than the previous one. |
| 820 | prefs_->SetString(kPrefsBootId, current_boot_id); |
| 821 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
| 822 | |
| 823 | bool previous_attempt_exists = prefs_->Exists(kPrefsPayloadAttemptNumber); |
| 824 | // |kPrefsPayloadAttemptNumber| should be cleared upon successful update. |
| 825 | if (previous_attempt_exists) { |
| 826 | metrics_reporter_->ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
| 827 | } |
| 828 | |
| 829 | metrics_utils::LoadAndReportTimeToReboot( |
| 830 | metrics_reporter_.get(), prefs_, clock_.get()); |
| 831 | ClearMetricsPrefs(); |
Sen Jiang | 1bafff8 | 2019-01-02 15:54:40 -0800 | [diff] [blame] | 832 | |
| 833 | // Also reset the update progress if the build version has changed. |
| 834 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 835 | LOG(WARNING) << "Unable to reset the update progress."; |
| 836 | } |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // Save the update start time. Reset the reboot count and attempt number if the |
| 840 | // update isn't a resume; otherwise increment the attempt number. |
| 841 | void UpdateAttempterAndroid::UpdatePrefsOnUpdateStart(bool is_resume) { |
| 842 | if (!is_resume) { |
| 843 | metrics_utils::SetNumReboots(0, prefs_); |
| 844 | metrics_utils::SetPayloadAttemptNumber(1, prefs_); |
| 845 | } else { |
| 846 | int64_t attempt_number = |
| 847 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 848 | metrics_utils::SetPayloadAttemptNumber(attempt_number + 1, prefs_); |
| 849 | } |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 850 | metrics_utils::SetUpdateTimestampStart(clock_->GetMonotonicTime(), prefs_); |
| 851 | metrics_utils::SetUpdateBootTimestampStart(clock_->GetBootTime(), prefs_); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void UpdateAttempterAndroid::ClearMetricsPrefs() { |
| 855 | CHECK(prefs_); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 856 | prefs_->Delete(kPrefsCurrentBytesDownloaded); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 857 | prefs_->Delete(kPrefsNumReboots); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 858 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 859 | prefs_->Delete(kPrefsUpdateTimestampStart); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 860 | prefs_->Delete(kPrefsUpdateBootTimestampStart); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 863 | } // namespace chromeos_update_engine |