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