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