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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/update_attempter_android.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 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> |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 22 | #include <ostream> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 23 | #include <utility> |
Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 24 | #include <vector> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 25 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 28 | #include <base/bind.h> |
| 29 | #include <base/logging.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 30 | #include <base/strings/string_number_conversions.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 31 | #include <brillo/data_encoding.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 32 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 33 | #include <brillo/strings/string_utils.h> |
Sen Jiang | acbdd1c | 2017-10-19 13:30:19 -0700 | [diff] [blame] | 34 | #include <log/log_safetynet.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 35 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 36 | #include "update_engine/aosp/cleanup_previous_update_action.h" |
Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 37 | #include "update_engine/common/clock.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 38 | #include "update_engine/common/constants.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 39 | #include "update_engine/common/daemon_state_interface.h" |
| 40 | #include "update_engine/common/download_action.h" |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 41 | #include "update_engine/common/error_code.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 42 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 43 | #include "update_engine/common/file_fetcher.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 44 | #include "update_engine/common/metrics_reporter_interface.h" |
| 45 | #include "update_engine/common/network_selector.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 46 | #include "update_engine/common/utils.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 47 | #include "update_engine/metrics_utils.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 48 | #include "update_engine/payload_consumer/delta_performer.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 49 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 50 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 51 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 52 | #include "update_engine/payload_consumer/partition_writer.h" |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 53 | #include "update_engine/payload_consumer/payload_constants.h" |
| 54 | #include "update_engine/payload_consumer/payload_metadata.h" |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 55 | #include "update_engine/payload_consumer/payload_verifier.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 56 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 57 | #include "update_engine/update_boot_flags_action.h" |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 58 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 59 | |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 60 | #ifndef _UE_SIDELOAD |
| 61 | // Do not include support for external HTTP(s) urls when building |
| 62 | // update_engine_sideload. |
| 63 | #include "update_engine/libcurl_http_fetcher.h" |
| 64 | #endif |
| 65 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 66 | using android::base::unique_fd; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 67 | using base::Bind; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 68 | using base::Time; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 69 | using base::TimeDelta; |
| 70 | using base::TimeTicks; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 71 | using std::string; |
| 72 | using std::vector; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 73 | using update_engine::UpdateEngineStatus; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 74 | |
| 75 | namespace chromeos_update_engine { |
| 76 | |
| 77 | namespace { |
| 78 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 79 | // Minimum threshold to broadcast an status update in progress and time. |
| 80 | const double kBroadcastThresholdProgress = 0.01; // 1% |
| 81 | const int kBroadcastThresholdSeconds = 10; |
| 82 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 83 | const char* const kErrorDomain = "update_engine"; |
| 84 | // TODO(deymo): Convert the different errors to a numeric value to report them |
| 85 | // back on the service error. |
| 86 | const char* const kGenericError = "generic_error"; |
| 87 | |
| 88 | // Log and set the error on the passed ErrorPtr. |
| 89 | bool LogAndSetError(brillo::ErrorPtr* error, |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 90 | const base::Location& location, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 91 | const string& reason) { |
| 92 | brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason); |
| 93 | LOG(ERROR) << "Replying with failure: " << location.ToString() << ": " |
| 94 | << reason; |
| 95 | return false; |
| 96 | } |
| 97 | |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 98 | bool GetHeaderAsBool(const string& header, bool default_value) { |
| 99 | int value = 0; |
| 100 | if (base::StringToInt(header, &value) && (value == 0 || value == 1)) |
| 101 | return value == 1; |
| 102 | return default_value; |
| 103 | } |
| 104 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 105 | bool ParseKeyValuePairHeaders(const vector<string>& key_value_pair_headers, |
| 106 | std::map<string, string>* headers, |
| 107 | brillo::ErrorPtr* error) { |
| 108 | for (const string& key_value_pair : key_value_pair_headers) { |
| 109 | string key; |
| 110 | string value; |
| 111 | if (!brillo::string_utils::SplitAtFirst( |
| 112 | key_value_pair, "=", &key, &value, false)) { |
| 113 | return LogAndSetError( |
| 114 | error, FROM_HERE, "Passed invalid header: " + key_value_pair); |
| 115 | } |
| 116 | if (!headers->emplace(key, value).second) |
| 117 | return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key); |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // Unique identifier for the payload. An empty string means that the payload |
| 123 | // can't be resumed. |
| 124 | string GetPayloadId(const std::map<string, string>& headers) { |
| 125 | return (headers.count(kPayloadPropertyFileHash) |
| 126 | ? headers.at(kPayloadPropertyFileHash) |
| 127 | : "") + |
| 128 | (headers.count(kPayloadPropertyMetadataHash) |
| 129 | ? headers.at(kPayloadPropertyMetadataHash) |
| 130 | : ""); |
| 131 | } |
| 132 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 133 | } // namespace |
| 134 | |
| 135 | UpdateAttempterAndroid::UpdateAttempterAndroid( |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 136 | DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 137 | PrefsInterface* prefs, |
| 138 | BootControlInterface* boot_control, |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 139 | HardwareInterface* hardware, |
| 140 | std::unique_ptr<ApexHandlerInterface> apex_handler) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 141 | : daemon_state_(daemon_state), |
| 142 | prefs_(prefs), |
| 143 | boot_control_(boot_control), |
| 144 | hardware_(hardware), |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 145 | apex_handler_android_(std::move(apex_handler)), |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 146 | processor_(new ActionProcessor()), |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 147 | clock_(new Clock()), |
| 148 | metric_bytes_downloaded_(kPrefsCurrentBytesDownloaded, prefs_), |
| 149 | metric_total_bytes_downloaded_(kPrefsTotalBytesDownloaded, prefs_) { |
Yifan Hong | c514f66 | 2021-02-04 11:18:43 -0800 | [diff] [blame] | 150 | metrics_reporter_ = metrics::CreateMetricsReporter( |
Kelvin Zhang | 9a5e368 | 2021-03-29 12:58:48 -0400 | [diff] [blame] | 151 | boot_control_->GetDynamicPartitionControl(), &install_plan_); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 152 | network_selector_ = network::CreateNetworkSelector(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | UpdateAttempterAndroid::~UpdateAttempterAndroid() { |
| 156 | // Release ourselves as the ActionProcessor's delegate to prevent |
| 157 | // re-scheduling the updates due to the processing stopped. |
| 158 | processor_->set_delegate(nullptr); |
| 159 | } |
| 160 | |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 161 | [[nodiscard]] static bool DidSystemReboot(PrefsInterface* prefs) { |
| 162 | string boot_id; |
| 163 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 164 | string old_boot_id; |
| 165 | // If no previous boot id found, treat as a reboot and write boot ID. |
| 166 | if (!prefs->GetString(kPrefsBootId, &old_boot_id)) { |
| 167 | return true; |
| 168 | } |
| 169 | return old_boot_id != boot_id; |
| 170 | } |
| 171 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 172 | std::ostream& operator<<(std::ostream& out, OTAResult result) { |
| 173 | switch (result) { |
| 174 | case OTAResult::NOT_ATTEMPTED: |
| 175 | out << "OTAResult::NOT_ATTEMPTED"; |
| 176 | break; |
| 177 | case OTAResult::ROLLED_BACK: |
| 178 | out << "OTAResult::ROLLED_BACK"; |
| 179 | break; |
| 180 | case OTAResult::UPDATED_NEED_REBOOT: |
| 181 | out << "OTAResult::UPDATED_NEED_REBOOT"; |
| 182 | break; |
| 183 | case OTAResult::OTA_SUCCESSFUL: |
| 184 | out << "OTAResult::OTA_SUCCESSFUL"; |
| 185 | break; |
| 186 | } |
| 187 | return out; |
| 188 | } |
| 189 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 190 | void UpdateAttempterAndroid::Init() { |
| 191 | // In case of update_engine restart without a reboot we need to restore the |
| 192 | // reboot needed state. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 193 | if (UpdateCompletedOnThisBoot()) { |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 194 | LOG(INFO) << "Updated installed but update_engine is restarted without " |
| 195 | "device reboot. Resuming old state."; |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 196 | SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 197 | } else { |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 198 | const auto result = GetOTAUpdateResult(); |
| 199 | LOG(INFO) << result; |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 200 | SetStatusAndNotify(UpdateStatus::IDLE); |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 201 | if (DidSystemReboot(prefs_)) { |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 202 | UpdateStateAfterReboot(result); |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 205 | #ifdef _UE_SIDELOAD |
| 206 | LOG(INFO) << "Skip ScheduleCleanupPreviousUpdate in sideload because " |
| 207 | << "ApplyPayload will call it later."; |
| 208 | #else |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 209 | ScheduleCleanupPreviousUpdate(); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 210 | #endif |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 211 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | bool UpdateAttempterAndroid::ApplyPayload( |
| 215 | const string& payload_url, |
| 216 | int64_t payload_offset, |
| 217 | int64_t payload_size, |
| 218 | const vector<string>& key_value_pair_headers, |
| 219 | brillo::ErrorPtr* error) { |
| 220 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 221 | return LogAndSetError( |
| 222 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 223 | } |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 224 | if (processor_->IsRunning()) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 225 | return LogAndSetError( |
| 226 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 227 | } |
HÃ¥kan Kvist | b00089b | 2021-01-29 14:09:49 +0100 | [diff] [blame] | 228 | DCHECK_EQ(status_, UpdateStatus::IDLE); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 229 | |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 230 | std::map<string, string> headers; |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 231 | if (!ParseKeyValuePairHeaders(key_value_pair_headers, &headers, error)) { |
| 232 | return false; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 235 | string payload_id = GetPayloadId(headers); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 236 | |
| 237 | // Setup the InstallPlan based on the request. |
| 238 | install_plan_ = InstallPlan(); |
| 239 | |
| 240 | install_plan_.download_url = payload_url; |
| 241 | install_plan_.version = ""; |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 242 | base_offset_ = payload_offset; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 243 | InstallPlan::Payload payload; |
| 244 | payload.size = payload_size; |
| 245 | if (!payload.size) { |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 246 | if (!base::StringToUint64(headers[kPayloadPropertyFileSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 247 | &payload.size)) { |
| 248 | payload.size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 251 | if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 252 | &payload.hash)) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 253 | LOG(WARNING) << "Unable to decode base64 file hash: " |
| 254 | << headers[kPayloadPropertyFileHash]; |
| 255 | } |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 256 | if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize], |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 257 | &payload.metadata_size)) { |
| 258 | payload.metadata_size = 0; |
Alex Deymo | 218397f | 2016-02-04 23:55:10 -0800 | [diff] [blame] | 259 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 260 | // The |payload.type| is not used anymore since minor_version 3. |
| 261 | payload.type = InstallPayloadType::kUnknown; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 262 | install_plan_.payloads.push_back(payload); |
| 263 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 264 | // The |public_key_rsa| key would override the public key stored on disk. |
| 265 | install_plan_.public_key_rsa = ""; |
| 266 | |
| 267 | install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild(); |
| 268 | install_plan_.is_resume = !payload_id.empty() && |
| 269 | DeltaPerformer::CanResumeUpdate(prefs_, payload_id); |
| 270 | if (!install_plan_.is_resume) { |
Kelvin Zhang | 42c2d55 | 2022-07-25 11:04:00 -0700 | [diff] [blame] | 271 | boot_control_->GetDynamicPartitionControl()->Cleanup(); |
Kelvin Zhang | c54c996 | 2022-10-20 13:44:59 -0700 | [diff] [blame] | 272 | boot_control_->GetDynamicPartitionControl()->ResetUpdate(prefs_); |
| 273 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 274 | if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) { |
| 275 | LOG(WARNING) << "Unable to save the update check response hash."; |
| 276 | } |
| 277 | } |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 278 | install_plan_.source_slot = GetCurrentSlot(); |
| 279 | install_plan_.target_slot = GetTargetSlot(); |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 280 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 281 | install_plan_.powerwash_required = |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 282 | GetHeaderAsBool(headers[kPayloadPropertyPowerwash], false); |
| 283 | |
| 284 | install_plan_.switch_slot_on_reboot = |
| 285 | GetHeaderAsBool(headers[kPayloadPropertySwitchSlotOnReboot], true); |
| 286 | |
Tianjie Xu | 087de9d | 2019-11-01 17:11:22 -0700 | [diff] [blame] | 287 | install_plan_.run_post_install = |
| 288 | GetHeaderAsBool(headers[kPayloadPropertyRunPostInstall], true); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 289 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 290 | // Skip writing verity if we're resuming and verity has already been written. |
| 291 | install_plan_.write_verity = true; |
| 292 | if (install_plan_.is_resume && prefs_->Exists(kPrefsVerityWritten)) { |
| 293 | bool verity_written = false; |
| 294 | if (prefs_->GetBoolean(kPrefsVerityWritten, &verity_written) && |
| 295 | verity_written) { |
| 296 | install_plan_.write_verity = false; |
| 297 | } |
| 298 | } |
| 299 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 300 | NetworkId network_id = kDefaultNetworkId; |
| 301 | if (!headers[kPayloadPropertyNetworkId].empty()) { |
| 302 | if (!base::StringToUint64(headers[kPayloadPropertyNetworkId], |
| 303 | &network_id)) { |
| 304 | return LogAndSetError( |
| 305 | error, |
| 306 | FROM_HERE, |
| 307 | "Invalid network_id: " + headers[kPayloadPropertyNetworkId]); |
| 308 | } |
| 309 | if (!network_selector_->SetProcessNetwork(network_id)) { |
Sen Jiang | cbd37c6 | 2017-09-12 15:04:35 -0700 | [diff] [blame] | 310 | return LogAndSetError( |
| 311 | error, |
| 312 | FROM_HERE, |
| 313 | "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]); |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 317 | LOG(INFO) << "Using this install plan:"; |
| 318 | install_plan_.Dump(); |
| 319 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 320 | HttpFetcher* fetcher = nullptr; |
| 321 | if (FileFetcher::SupportedUrl(payload_url)) { |
| 322 | DLOG(INFO) << "Using FileFetcher for file URL."; |
| 323 | fetcher = new FileFetcher(); |
| 324 | } else { |
| 325 | #ifdef _UE_SIDELOAD |
| 326 | LOG(FATAL) << "Unsupported sideload URI: " << payload_url; |
Chih-Hung Hsieh | 6e4f345 | 2022-12-21 12:20:45 -0800 | [diff] [blame] | 327 | return false; // NOLINT, unreached but analyzer might not know. |
| 328 | // Suppress warnings about null 'fetcher' after this. |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 329 | #else |
Kelvin Zhang | c7a1d1f | 2022-07-29 13:36:29 -0700 | [diff] [blame] | 330 | LibcurlHttpFetcher* libcurl_fetcher = new LibcurlHttpFetcher(hardware_); |
Kelvin Zhang | 3cc4fa3 | 2022-09-27 16:15:10 -0700 | [diff] [blame] | 331 | if (!headers[kPayloadDownloadRetry].empty()) { |
| 332 | libcurl_fetcher->set_max_retry_count( |
| 333 | atoi(headers[kPayloadDownloadRetry].c_str())); |
| 334 | } |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 335 | libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload); |
| 336 | fetcher = libcurl_fetcher; |
| 337 | #endif // _UE_SIDELOAD |
| 338 | } |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 339 | // Setup extra headers. |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 340 | if (!headers[kPayloadPropertyAuthorization].empty()) |
| 341 | fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]); |
| 342 | if (!headers[kPayloadPropertyUserAgent].empty()) |
| 343 | fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]); |
| 344 | |
Kelvin Zhang | 98cb8f7 | 2022-08-03 12:42:02 -0700 | [diff] [blame] | 345 | if (!headers[kPayloadPropertyNetworkProxy].empty()) { |
| 346 | LOG(INFO) << "Using proxy url from payload headers: " |
| 347 | << headers[kPayloadPropertyNetworkProxy]; |
| 348 | fetcher->SetProxies({headers[kPayloadPropertyNetworkProxy]}); |
| 349 | } |
Daniel Zheng | 730ae9b | 2022-08-25 22:37:22 +0000 | [diff] [blame] | 350 | if (!headers[kPayloadDisableVABC].empty()) { |
| 351 | install_plan_.disable_vabc = true; |
| 352 | } |
Kelvin Zhang | 6bef490 | 2023-02-22 12:43:27 -0800 | [diff] [blame] | 353 | if (!headers[kPayloadEnableThreading].empty()) { |
| 354 | install_plan_.enable_threading = true; |
| 355 | } |
| 356 | if (!headers[kPayloadBatchedWrites].empty()) { |
| 357 | install_plan_.batched_writes = true; |
| 358 | } |
Kelvin Zhang | 98cb8f7 | 2022-08-03 12:42:02 -0700 | [diff] [blame] | 359 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 360 | BuildUpdateActions(fetcher); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 361 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 362 | SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 363 | |
| 364 | UpdatePrefsOnUpdateStart(install_plan_.is_resume); |
| 365 | // TODO(xunchang) report the metrics for unresumable updates |
| 366 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 367 | ScheduleProcessingStart(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 371 | bool UpdateAttempterAndroid::ApplyPayload( |
| 372 | int fd, |
| 373 | int64_t payload_offset, |
| 374 | int64_t payload_size, |
| 375 | const vector<string>& key_value_pair_headers, |
| 376 | brillo::ErrorPtr* error) { |
HÃ¥kan Kvist | b00089b | 2021-01-29 14:09:49 +0100 | [diff] [blame] | 377 | // update_engine state must be checked before modifying payload_fd_ otherwise |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 378 | // already running update will be terminated (existing file descriptor will be |
| 379 | // closed) |
HÃ¥kan Kvist | b00089b | 2021-01-29 14:09:49 +0100 | [diff] [blame] | 380 | if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) { |
| 381 | return LogAndSetError( |
| 382 | error, FROM_HERE, "An update already applied, waiting for reboot"); |
| 383 | } |
| 384 | if (processor_->IsRunning()) { |
| 385 | return LogAndSetError( |
| 386 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 387 | } |
| 388 | DCHECK_EQ(status_, UpdateStatus::IDLE); |
| 389 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 390 | payload_fd_.reset(dup(fd)); |
| 391 | const string payload_url = "fd://" + std::to_string(payload_fd_.get()); |
| 392 | |
| 393 | return ApplyPayload( |
| 394 | payload_url, payload_offset, payload_size, key_value_pair_headers, error); |
| 395 | } |
| 396 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 397 | bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 398 | if (!processor_->IsRunning()) |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 399 | return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend."); |
| 400 | processor_->SuspendProcessing(); |
| 401 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 405 | if (!processor_->IsRunning()) |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 406 | return LogAndSetError(error, FROM_HERE, "No ongoing update to resume."); |
| 407 | processor_->ResumeProcessing(); |
| 408 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) { |
Sen Jiang | 91f8d2a | 2018-07-12 14:27:04 -0700 | [diff] [blame] | 412 | if (!processor_->IsRunning()) |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 413 | return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel."); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 414 | processor_->StopProcessing(); |
| 415 | return true; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 418 | bool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) { |
| 419 | LOG(INFO) << "Attempting to reset state from " |
| 420 | << UpdateStatusToString(status_) << " to UpdateStatus::IDLE"; |
Kelvin Zhang | ff5380b | 2022-03-16 13:35:04 -0700 | [diff] [blame] | 421 | if (processor_->IsRunning()) { |
| 422 | return LogAndSetError( |
| 423 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 424 | } |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 425 | |
Mohammad Samiul Islam | b5c07bf | 2021-03-05 10:02:46 +0000 | [diff] [blame] | 426 | if (apex_handler_android_ != nullptr) { |
| 427 | LOG(INFO) << "Cleaning up reserved space for compressed APEX (if any)"; |
| 428 | std::vector<ApexInfo> apex_infos_blank; |
| 429 | apex_handler_android_->AllocateSpace(apex_infos_blank); |
| 430 | } |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 431 | // Remove the reboot marker so that if the machine is rebooted |
| 432 | // after resetting to idle state, it doesn't go back to |
| 433 | // UpdateStatus::UPDATED_NEED_REBOOT state. |
| 434 | if (!ClearUpdateCompletedMarker()) { |
| 435 | return LogAndSetError(error, |
| 436 | FROM_HERE, |
| 437 | "Failed to reset the status because " |
| 438 | "ClearUpdateCompletedMarker() failed"); |
| 439 | } |
Mohammad Samiul Islam | b5c07bf | 2021-03-05 10:02:46 +0000 | [diff] [blame] | 440 | |
Kelvin Zhang | ff5380b | 2022-03-16 13:35:04 -0700 | [diff] [blame] | 441 | if (!boot_control_->GetDynamicPartitionControl()->ResetUpdate(prefs_)) { |
| 442 | LOG(WARNING) << "Failed to reset snapshots. UpdateStatus is IDLE but" |
Daniel Zheng | 730ae9b | 2022-08-25 22:37:22 +0000 | [diff] [blame] | 443 | << "space might not be freed."; |
Kelvin Zhang | ff5380b | 2022-03-16 13:35:04 -0700 | [diff] [blame] | 444 | } |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 445 | switch (status_) { |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 446 | case UpdateStatus::IDLE: { |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 447 | return true; |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 448 | } |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 449 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 450 | case UpdateStatus::UPDATED_NEED_REBOOT: { |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 451 | const bool ret_value = resetShouldSwitchSlotOnReboot(error); |
| 452 | if (ret_value) { |
| 453 | LOG(INFO) << "Reset status successful"; |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 454 | } |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 455 | return ret_value; |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | default: |
| 459 | return LogAndSetError( |
| 460 | error, |
| 461 | FROM_HERE, |
| 462 | "Reset not allowed in this state. Cancel the ongoing update first"); |
| 463 | } |
| 464 | } |
| 465 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 466 | bool UpdateAttempterAndroid::VerifyPayloadParseManifest( |
| 467 | const std::string& metadata_filename, |
| 468 | DeltaArchiveManifest* manifest, |
| 469 | brillo::ErrorPtr* error) { |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 470 | FileDescriptorPtr fd(new EintrSafeFileDescriptor); |
| 471 | if (!fd->Open(metadata_filename.c_str(), O_RDONLY)) { |
| 472 | return LogAndSetError( |
| 473 | error, FROM_HERE, "Failed to open " + metadata_filename); |
| 474 | } |
| 475 | brillo::Blob metadata(kMaxPayloadHeaderSize); |
| 476 | if (!fd->Read(metadata.data(), metadata.size())) { |
| 477 | return LogAndSetError( |
| 478 | error, |
| 479 | FROM_HERE, |
| 480 | "Failed to read payload header from " + metadata_filename); |
| 481 | } |
Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 482 | ErrorCode errorcode{}; |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 483 | PayloadMetadata payload_metadata; |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 484 | if (payload_metadata.ParsePayloadHeader(metadata, &errorcode) != |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 485 | MetadataParseResult::kSuccess) { |
| 486 | return LogAndSetError(error, |
| 487 | FROM_HERE, |
| 488 | "Failed to parse payload header: " + |
| 489 | utils::ErrorCodeToString(errorcode)); |
| 490 | } |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 491 | uint64_t metadata_size = payload_metadata.GetMetadataSize() + |
| 492 | payload_metadata.GetMetadataSignatureSize(); |
| 493 | if (metadata_size < kMaxPayloadHeaderSize || |
| 494 | metadata_size > |
| 495 | static_cast<uint64_t>(utils::FileSize(metadata_filename))) { |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 496 | return LogAndSetError( |
| 497 | error, |
| 498 | FROM_HERE, |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 499 | "Invalid metadata size: " + std::to_string(metadata_size)); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 500 | } |
Sen Jiang | 840a7ea | 2018-09-19 14:29:44 -0700 | [diff] [blame] | 501 | metadata.resize(metadata_size); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 502 | if (!fd->Read(metadata.data() + kMaxPayloadHeaderSize, |
| 503 | metadata.size() - kMaxPayloadHeaderSize)) { |
| 504 | return LogAndSetError( |
| 505 | error, |
| 506 | FROM_HERE, |
| 507 | "Failed to read metadata and signature from " + metadata_filename); |
| 508 | } |
| 509 | fd->Close(); |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 510 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 511 | auto payload_verifier = PayloadVerifier::CreateInstanceFromZipPath( |
| 512 | constants::kUpdateCertificatesPath); |
| 513 | if (!payload_verifier) { |
| 514 | return LogAndSetError(error, |
| 515 | FROM_HERE, |
| 516 | "Failed to create the payload verifier from " + |
| 517 | std::string(constants::kUpdateCertificatesPath)); |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 518 | } |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 519 | errorcode = payload_metadata.ValidateMetadataSignature( |
| 520 | metadata, "", *payload_verifier); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 521 | if (errorcode != ErrorCode::kSuccess) { |
| 522 | return LogAndSetError(error, |
| 523 | FROM_HERE, |
| 524 | "Failed to validate metadata signature: " + |
| 525 | utils::ErrorCodeToString(errorcode)); |
| 526 | } |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 527 | if (!payload_metadata.GetManifest(metadata, manifest)) { |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 528 | return LogAndSetError(error, FROM_HERE, "Failed to parse manifest."); |
| 529 | } |
| 530 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 531 | return true; |
| 532 | } |
| 533 | |
| 534 | bool UpdateAttempterAndroid::VerifyPayloadApplicable( |
| 535 | const std::string& metadata_filename, brillo::ErrorPtr* error) { |
| 536 | DeltaArchiveManifest manifest; |
| 537 | TEST_AND_RETURN_FALSE( |
| 538 | VerifyPayloadParseManifest(metadata_filename, &manifest, error)); |
| 539 | |
| 540 | FileDescriptorPtr fd(new EintrSafeFileDescriptor); |
Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 541 | ErrorCode errorcode{}; |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 542 | |
| 543 | BootControlInterface::Slot current_slot = GetCurrentSlot(); |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 544 | for (const PartitionUpdate& partition : manifest.partitions()) { |
| 545 | if (!partition.has_old_partition_info()) |
| 546 | continue; |
| 547 | string partition_path; |
| 548 | if (!boot_control_->GetPartitionDevice( |
| 549 | partition.partition_name(), current_slot, &partition_path)) { |
| 550 | return LogAndSetError( |
| 551 | error, |
| 552 | FROM_HERE, |
| 553 | "Failed to get partition device for " + partition.partition_name()); |
| 554 | } |
| 555 | if (!fd->Open(partition_path.c_str(), O_RDONLY)) { |
| 556 | return LogAndSetError( |
| 557 | error, FROM_HERE, "Failed to open " + partition_path); |
| 558 | } |
| 559 | for (const InstallOperation& operation : partition.operations()) { |
| 560 | if (!operation.has_src_sha256_hash()) |
| 561 | continue; |
| 562 | brillo::Blob source_hash; |
| 563 | if (!fd_utils::ReadAndHashExtents(fd, |
| 564 | operation.src_extents(), |
| 565 | manifest.block_size(), |
| 566 | &source_hash)) { |
| 567 | return LogAndSetError( |
| 568 | error, FROM_HERE, "Failed to hash " + partition_path); |
| 569 | } |
Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 570 | if (!PartitionWriter::ValidateSourceHash( |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 571 | source_hash, operation, fd, &errorcode)) { |
| 572 | return false; |
| 573 | } |
| 574 | } |
| 575 | fd->Close(); |
| 576 | } |
| 577 | return true; |
| 578 | } |
| 579 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 580 | void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor, |
| 581 | ErrorCode code) { |
| 582 | LOG(INFO) << "Processing Done."; |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 583 | metric_bytes_downloaded_.Flush(true); |
| 584 | metric_total_bytes_downloaded_.Flush(true); |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 585 | last_error_ = code; |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 586 | if (status_ == UpdateStatus::CLEANUP_PREVIOUS_UPDATE) { |
| 587 | TerminateUpdateAndNotify(code); |
| 588 | return; |
| 589 | } |
| 590 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 591 | switch (code) { |
| 592 | case ErrorCode::kSuccess: |
| 593 | // Update succeeded. |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 594 | if (!WriteUpdateCompletedMarker()) { |
| 595 | LOG(ERROR) << "Failed to write update completion marker"; |
| 596 | } |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 597 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 598 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 599 | LOG(INFO) << "Update successfully applied, waiting to reboot."; |
| 600 | break; |
| 601 | |
| 602 | case ErrorCode::kFilesystemCopierError: |
| 603 | case ErrorCode::kNewRootfsVerificationError: |
| 604 | case ErrorCode::kNewKernelVerificationError: |
| 605 | case ErrorCode::kFilesystemVerifierError: |
| 606 | case ErrorCode::kDownloadStateInitializationError: |
| 607 | // Reset the ongoing update for these errors so it starts from the |
| 608 | // beginning next time. |
| 609 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 610 | LOG(INFO) << "Resetting update progress."; |
| 611 | break; |
| 612 | |
Sen Jiang | acbdd1c | 2017-10-19 13:30:19 -0700 | [diff] [blame] | 613 | case ErrorCode::kPayloadTimestampError: |
| 614 | // SafetyNet logging, b/36232423 |
| 615 | android_errorWriteLog(0x534e4554, "36232423"); |
| 616 | break; |
| 617 | |
Alex Deymo | 5990bf3 | 2016-07-19 17:01:41 -0700 | [diff] [blame] | 618 | default: |
| 619 | // Ignore all other error codes. |
| 620 | break; |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 621 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 622 | |
| 623 | TerminateUpdateAndNotify(code); |
| 624 | } |
| 625 | |
| 626 | void UpdateAttempterAndroid::ProcessingStopped( |
| 627 | const ActionProcessor* processor) { |
| 628 | TerminateUpdateAndNotify(ErrorCode::kUserCanceled); |
| 629 | } |
| 630 | |
| 631 | void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor, |
| 632 | AbstractAction* action, |
| 633 | ErrorCode code) { |
| 634 | // Reset download progress regardless of whether or not the download |
| 635 | // action succeeded. |
| 636 | const string type = action->Type(); |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 637 | if (type == CleanupPreviousUpdateAction::StaticType() || |
| 638 | (type == NoOpAction::StaticType() && |
| 639 | status_ == UpdateStatus::CLEANUP_PREVIOUS_UPDATE)) { |
| 640 | cleanup_previous_update_code_ = code; |
| 641 | NotifyCleanupPreviousUpdateCallbacksAndClear(); |
| 642 | } |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 643 | // download_progress_ is actually used by other actions, such as |
| 644 | // filesystem_verify_action. Therefore we always clear it. |
| 645 | download_progress_ = 0; |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 646 | if (type == PostinstallRunnerAction::StaticType()) { |
| 647 | bool succeeded = |
| 648 | code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive; |
| 649 | prefs_->SetBoolean(kPrefsPostInstallSucceeded, succeeded); |
| 650 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 651 | if (code != ErrorCode::kSuccess) { |
| 652 | // If an action failed, the ActionProcessor will cancel the whole thing. |
| 653 | return; |
| 654 | } |
Yifan Hong | 8351750 | 2020-02-19 15:34:13 -0800 | [diff] [blame] | 655 | if (type == UpdateBootFlagsAction::StaticType()) { |
| 656 | SetStatusAndNotify(UpdateStatus::CLEANUP_PREVIOUS_UPDATE); |
| 657 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 658 | if (type == DownloadAction::StaticType()) { |
Kelvin Zhang | d2da7b1 | 2020-07-07 17:19:21 -0400 | [diff] [blame] | 659 | auto download_action = static_cast<DownloadAction*>(action); |
| 660 | install_plan_ = *download_action->install_plan(); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 661 | SetStatusAndNotify(UpdateStatus::VERIFYING); |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 662 | } else if (type == FilesystemVerifierAction::StaticType()) { |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 663 | SetStatusAndNotify(UpdateStatus::FINALIZING); |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 664 | prefs_->SetBoolean(kPrefsVerityWritten, true); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed, |
| 669 | uint64_t bytes_received, |
| 670 | uint64_t total) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 671 | double progress = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 672 | if (total) |
| 673 | progress = static_cast<double>(bytes_received) / static_cast<double>(total); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 674 | if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 675 | download_progress_ = progress; |
| 676 | SetStatusAndNotify(UpdateStatus::DOWNLOADING); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 677 | } else { |
| 678 | ProgressUpdate(progress); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 679 | } |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 680 | |
| 681 | // Update the bytes downloaded in prefs. |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 682 | metric_bytes_downloaded_ += bytes_progressed; |
| 683 | metric_total_bytes_downloaded_ += bytes_progressed; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) { |
| 687 | // TODO(deymo): Notify the DownloadAction that it should cancel the update |
| 688 | // download. |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | void UpdateAttempterAndroid::DownloadComplete() { |
| 693 | // Nothing needs to be done when the download completes. |
| 694 | } |
| 695 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 696 | void UpdateAttempterAndroid::ProgressUpdate(double progress) { |
| 697 | // Self throttle based on progress. Also send notifications if progress is |
| 698 | // too slow. |
| 699 | if (progress == 1.0 || |
| 700 | progress - download_progress_ >= kBroadcastThresholdProgress || |
| 701 | TimeTicks::Now() - last_notify_time_ >= |
| 702 | TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) { |
| 703 | download_progress_ = progress; |
| 704 | SetStatusAndNotify(status_); |
| 705 | } |
| 706 | } |
| 707 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 708 | void UpdateAttempterAndroid::OnVerifyProgressUpdate(double progress) { |
| 709 | assert(status_ == UpdateStatus::VERIFYING); |
| 710 | ProgressUpdate(progress); |
| 711 | } |
| 712 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 713 | void UpdateAttempterAndroid::ScheduleProcessingStart() { |
| 714 | LOG(INFO) << "Scheduling an action processor start."; |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 715 | processor_->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 716 | brillo::MessageLoop::current()->PostTask( |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 717 | FROM_HERE, |
| 718 | Bind([](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 719 | base::Unretained(processor_.get()))); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) { |
| 723 | if (status_ == UpdateStatus::IDLE) { |
| 724 | LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called."; |
| 725 | return; |
| 726 | } |
| 727 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 728 | if (status_ == UpdateStatus::CLEANUP_PREVIOUS_UPDATE) { |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 729 | ClearUpdateCompletedMarker(); |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 730 | LOG(INFO) << "Terminating cleanup previous update."; |
| 731 | SetStatusAndNotify(UpdateStatus::IDLE); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 732 | for (auto observer : daemon_state_->service_observers()) |
| 733 | observer->SendPayloadApplicationComplete(error_code); |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
Yifan Hong | 9194ce8 | 2019-11-07 11:03:42 -0800 | [diff] [blame] | 737 | boot_control_->GetDynamicPartitionControl()->Cleanup(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 738 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 739 | download_progress_ = 0; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 740 | UpdateStatus new_status = |
| 741 | (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT |
| 742 | : UpdateStatus::IDLE); |
| 743 | SetStatusAndNotify(new_status); |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 744 | payload_fd_.reset(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 745 | |
Sen Jiang | b19c3ec | 2017-10-06 15:18:46 -0700 | [diff] [blame] | 746 | // The network id is only applicable to one download attempt and once it's |
| 747 | // done the network id should not be re-used anymore. |
| 748 | if (!network_selector_->SetProcessNetwork(kDefaultNetworkId)) { |
| 749 | LOG(WARNING) << "Unable to unbind network."; |
| 750 | } |
| 751 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 752 | for (auto observer : daemon_state_->service_observers()) |
| 753 | observer->SendPayloadApplicationComplete(error_code); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 754 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 755 | CollectAndReportUpdateMetricsOnUpdateFinished(error_code); |
| 756 | ClearMetricsPrefs(); |
| 757 | if (error_code == ErrorCode::kSuccess) { |
xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 758 | // We should only reset the PayloadAttemptNumber if the update succeeds, or |
| 759 | // we switch to a different payload. |
| 760 | prefs_->Delete(kPrefsPayloadAttemptNumber); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 761 | metrics_utils::SetSystemUpdatedMarker(clock_.get(), prefs_); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 762 | // Clear the total bytes downloaded if and only if the update succeeds. |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 763 | metric_total_bytes_downloaded_.Delete(); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 764 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) { |
| 768 | status_ = status; |
Sen Jiang | 2d1c87b | 2017-07-14 10:46:14 -0700 | [diff] [blame] | 769 | size_t payload_size = |
| 770 | install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 771 | UpdateEngineStatus status_to_send = {.status = status_, |
| 772 | .progress = download_progress_, |
| 773 | .new_size_bytes = payload_size}; |
| 774 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 775 | for (auto observer : daemon_state_->service_observers()) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 776 | observer->SendStatusUpdate(status_to_send); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 777 | } |
| 778 | last_notify_time_ = TimeTicks::Now(); |
| 779 | } |
| 780 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 781 | void UpdateAttempterAndroid::BuildUpdateActions(HttpFetcher* fetcher) { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 782 | CHECK(!processor_->IsRunning()); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 783 | |
| 784 | // Actions: |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 785 | auto update_boot_flags_action = |
| 786 | std::make_unique<UpdateBootFlagsAction>(boot_control_); |
Yifan Hong | 8351750 | 2020-02-19 15:34:13 -0800 | [diff] [blame] | 787 | auto cleanup_previous_update_action = |
| 788 | boot_control_->GetDynamicPartitionControl() |
| 789 | ->GetCleanupPreviousUpdateAction(boot_control_, prefs_, this); |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 790 | auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan_); |
| 791 | auto download_action = |
| 792 | std::make_unique<DownloadAction>(prefs_, |
| 793 | boot_control_, |
| 794 | hardware_, |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 795 | fetcher, // passes ownership |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 796 | true /* interactive */, |
| 797 | update_certificates_path_); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 798 | download_action->set_delegate(this); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 799 | download_action->set_base_offset(base_offset_); |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 800 | auto filesystem_verifier_action = std::make_unique<FilesystemVerifierAction>( |
| 801 | boot_control_->GetDynamicPartitionControl()); |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 802 | auto postinstall_runner_action = |
| 803 | std::make_unique<PostinstallRunnerAction>(boot_control_, hardware_); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 804 | filesystem_verifier_action->set_delegate(this); |
Alex Deymo | b6eef73 | 2016-06-10 12:58:11 -0700 | [diff] [blame] | 805 | postinstall_runner_action->set_delegate(this); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 806 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 807 | // Bond them together. We have to use the leaf-types when calling |
| 808 | // BondActions(). |
| 809 | BondActions(install_plan_action.get(), download_action.get()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 810 | BondActions(download_action.get(), filesystem_verifier_action.get()); |
| 811 | BondActions(filesystem_verifier_action.get(), |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 812 | postinstall_runner_action.get()); |
| 813 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 814 | processor_->EnqueueAction(std::move(update_boot_flags_action)); |
Yifan Hong | 8351750 | 2020-02-19 15:34:13 -0800 | [diff] [blame] | 815 | processor_->EnqueueAction(std::move(cleanup_previous_update_action)); |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 816 | processor_->EnqueueAction(std::move(install_plan_action)); |
| 817 | processor_->EnqueueAction(std::move(download_action)); |
| 818 | processor_->EnqueueAction(std::move(filesystem_verifier_action)); |
| 819 | processor_->EnqueueAction(std::move(postinstall_runner_action)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 822 | bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() { |
| 823 | string boot_id; |
| 824 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
Kelvin Zhang | 1bcd7d7 | 2023-02-14 16:43:34 -0800 | [diff] [blame^] | 825 | LOG(INFO) << "Writing update complete marker, slot " |
| 826 | << boot_control_->GetCurrentSlot() << ", boot id: " << boot_id; |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 827 | TEST_AND_RETURN_FALSE( |
| 828 | prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id)); |
| 829 | TEST_AND_RETURN_FALSE( |
| 830 | prefs_->SetInt64(kPrefsPreviousSlot, boot_control_->GetCurrentSlot())); |
| 831 | return true; |
| 832 | } |
| 833 | |
| 834 | bool UpdateAttempterAndroid::ClearUpdateCompletedMarker() { |
| 835 | LOG(INFO) << "Clearing update complete marker."; |
| 836 | TEST_AND_RETURN_FALSE(prefs_->Delete(kPrefsUpdateCompletedOnBootId)); |
| 837 | TEST_AND_RETURN_FALSE(prefs_->Delete(kPrefsPreviousSlot)); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 838 | return true; |
| 839 | } |
| 840 | |
Kelvin Zhang | 1bcd7d7 | 2023-02-14 16:43:34 -0800 | [diff] [blame^] | 841 | bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() const { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 842 | // In case of an update_engine restart without a reboot, we stored the boot_id |
| 843 | // when the update was completed by setting a pref, so we can check whether |
| 844 | // the last update was on this boot or a previous one. |
| 845 | string boot_id; |
| 846 | TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id)); |
| 847 | |
| 848 | string update_completed_on_boot_id; |
| 849 | return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) && |
| 850 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, |
| 851 | &update_completed_on_boot_id) && |
| 852 | update_completed_on_boot_id == boot_id); |
| 853 | } |
| 854 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 855 | // Collect and report the android metrics when we terminate the update. |
| 856 | void UpdateAttempterAndroid::CollectAndReportUpdateMetricsOnUpdateFinished( |
| 857 | ErrorCode error_code) { |
| 858 | int64_t attempt_number = |
| 859 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 860 | PayloadType payload_type = kPayloadTypeFull; |
| 861 | int64_t payload_size = 0; |
| 862 | for (const auto& p : install_plan_.payloads) { |
| 863 | if (p.type == InstallPayloadType::kDelta) |
| 864 | payload_type = kPayloadTypeDelta; |
| 865 | payload_size += p.size; |
| 866 | } |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 867 | // In some cases, e.g. after calling |setShouldSwitchSlotOnReboot()|, this |
| 868 | // function will be triggered, but payload_size in this case might be 0, if so |
| 869 | // skip reporting any metrics. |
| 870 | if (payload_size == 0) { |
| 871 | return; |
| 872 | } |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 873 | |
| 874 | metrics::AttemptResult attempt_result = |
| 875 | metrics_utils::GetAttemptResult(error_code); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 876 | Time boot_time_start = Time::FromInternalValue( |
| 877 | metrics_utils::GetPersistedValue(kPrefsUpdateBootTimestampStart, prefs_)); |
| 878 | Time monotonic_time_start = Time::FromInternalValue( |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 879 | metrics_utils::GetPersistedValue(kPrefsUpdateTimestampStart, prefs_)); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 880 | TimeDelta duration = clock_->GetBootTime() - boot_time_start; |
| 881 | TimeDelta duration_uptime = clock_->GetMonotonicTime() - monotonic_time_start; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 882 | |
| 883 | metrics_reporter_->ReportUpdateAttemptMetrics( |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 884 | static_cast<int>(attempt_number), |
| 885 | payload_type, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 886 | duration, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 887 | duration_uptime, |
| 888 | payload_size, |
| 889 | attempt_result, |
| 890 | error_code); |
| 891 | |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 892 | int64_t current_bytes_downloaded = metric_bytes_downloaded_.get(); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 893 | metrics_reporter_->ReportUpdateAttemptDownloadMetrics( |
| 894 | current_bytes_downloaded, |
| 895 | 0, |
| 896 | DownloadSource::kNumDownloadSources, |
| 897 | metrics::DownloadErrorCode::kUnset, |
| 898 | metrics::ConnectionType::kUnset); |
| 899 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 900 | if (error_code == ErrorCode::kSuccess) { |
| 901 | int64_t reboot_count = |
| 902 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 903 | string build_version; |
| 904 | prefs_->GetString(kPrefsPreviousVersion, &build_version); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 905 | |
| 906 | // For android metrics, we only care about the total bytes downloaded |
| 907 | // for all sources; for now we assume the only download source is |
| 908 | // HttpsServer. |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 909 | int64_t total_bytes_downloaded = metric_total_bytes_downloaded_.get(); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 910 | int64_t num_bytes_downloaded[kNumDownloadSources] = {}; |
| 911 | num_bytes_downloaded[DownloadSource::kDownloadSourceHttpsServer] = |
| 912 | total_bytes_downloaded; |
| 913 | |
| 914 | int download_overhead_percentage = 0; |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 915 | if (total_bytes_downloaded >= payload_size) { |
| 916 | CHECK_GT(payload_size, 0); |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 917 | download_overhead_percentage = |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 918 | (total_bytes_downloaded - payload_size) * 100ull / payload_size; |
| 919 | } else { |
| 920 | LOG(WARNING) << "Downloaded bytes " << total_bytes_downloaded |
| 921 | << " is smaller than the payload size " << payload_size; |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 922 | } |
Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 923 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 924 | metrics_reporter_->ReportSuccessfulUpdateMetrics( |
| 925 | static_cast<int>(attempt_number), |
| 926 | 0, // update abandoned count |
| 927 | payload_type, |
| 928 | payload_size, |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 929 | num_bytes_downloaded, |
| 930 | download_overhead_percentage, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 931 | duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 932 | duration_uptime, |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 933 | static_cast<int>(reboot_count), |
| 934 | 0); // url_switch_count |
| 935 | } |
| 936 | } |
| 937 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 938 | bool UpdateAttempterAndroid::OTARebootSucceeded() const { |
| 939 | const auto current_slot = boot_control_->GetCurrentSlot(); |
| 940 | const string current_version = |
| 941 | android::base::GetProperty("ro.build.version.incremental", ""); |
| 942 | int64_t previous_slot = -1; |
| 943 | TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsPreviousSlot, &previous_slot)); |
| 944 | string previous_version; |
| 945 | TEST_AND_RETURN_FALSE( |
| 946 | prefs_->GetString(kPrefsPreviousVersion, &previous_version)); |
| 947 | if (previous_slot != current_slot) { |
| 948 | LOG(INFO) << "Detected a slot switch, OTA succeeded, device updated from " |
Kelvin Zhang | 1bcd7d7 | 2023-02-14 16:43:34 -0800 | [diff] [blame^] | 949 | << previous_version << " to " << current_version |
| 950 | << ", previous slot: " << previous_slot |
| 951 | << " current slot: " << current_slot; |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 952 | if (previous_version == current_version) { |
| 953 | LOG(INFO) << "Previous version is the same as current version, this is " |
| 954 | "possibly a self-OTA."; |
| 955 | } |
| 956 | return true; |
| 957 | } else { |
| 958 | LOG(INFO) << "Slot didn't switch, either the OTA is rolled back, or slot " |
| 959 | "switch never happened, or system not rebooted at all."; |
| 960 | if (previous_version != current_version) { |
| 961 | LOG(INFO) << "Slot didn't change, but version changed from " |
| 962 | << previous_version << " to " << current_version |
| 963 | << " device could be flashed."; |
| 964 | } |
| 965 | return false; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | OTAResult UpdateAttempterAndroid::GetOTAUpdateResult() const { |
| 970 | // We only set |kPrefsSystemUpdatedMarker| if slot is actually switched, so |
| 971 | // existence of this pref is sufficient indicator. Given that we have to |
| 972 | // delete this pref after checking it. This is done in |
Kelvin Zhang | 1bcd7d7 | 2023-02-14 16:43:34 -0800 | [diff] [blame^] | 973 | // |DeltaPerformer::ResetUpdateProgress| and |
| 974 | // |UpdateAttempterAndroid::UpdateStateAfterReboot| |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 975 | auto slot_switch_attempted = prefs_->Exists(kPrefsUpdateCompletedOnBootId); |
| 976 | auto system_rebooted = DidSystemReboot(prefs_); |
| 977 | auto ota_successful = OTARebootSucceeded(); |
| 978 | if (ota_successful) { |
| 979 | return OTAResult::OTA_SUCCESSFUL; |
| 980 | } |
| 981 | if (slot_switch_attempted) { |
| 982 | if (system_rebooted) { |
| 983 | // If we attempted slot switch, but still end up on the same slot, we |
| 984 | // probably rolled back. |
| 985 | return OTAResult::ROLLED_BACK; |
| 986 | } else { |
| 987 | return OTAResult::UPDATED_NEED_REBOOT; |
| 988 | } |
| 989 | } |
| 990 | return OTAResult::NOT_ATTEMPTED; |
| 991 | } |
| 992 | |
| 993 | void UpdateAttempterAndroid::UpdateStateAfterReboot(const OTAResult result) { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 994 | // Example: [ro.build.version.incremental]: [4292972] |
| 995 | string current_version = |
| 996 | android::base::GetProperty("ro.build.version.incremental", ""); |
| 997 | TEST_AND_RETURN(!current_version.empty()); |
| 998 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 999 | // |UpdateStateAfterReboot()| is only called after system reboot, so record |
| 1000 | // boot id unconditionally |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 1001 | string current_boot_id; |
| 1002 | TEST_AND_RETURN(utils::GetBootId(¤t_boot_id)); |
| 1003 | prefs_->SetString(kPrefsBootId, current_boot_id); |
Kelvin Zhang | 1bcd7d7 | 2023-02-14 16:43:34 -0800 | [diff] [blame^] | 1004 | std::string slot_switch_indicator; |
| 1005 | prefs_->GetString(kPrefsUpdateCompletedOnBootId, &slot_switch_indicator); |
| 1006 | if (slot_switch_indicator != current_boot_id) { |
| 1007 | ClearUpdateCompletedMarker(); |
| 1008 | } |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 1009 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1010 | // If there's no record of previous version (e.g. due to a data wipe), we |
| 1011 | // save the info of current boot and skip the metrics report. |
| 1012 | if (!prefs_->Exists(kPrefsPreviousVersion)) { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1013 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 1014 | prefs_->SetInt64(kPrefsPreviousSlot, boot_control_->GetCurrentSlot()); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1015 | ClearMetricsPrefs(); |
| 1016 | return; |
| 1017 | } |
Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 1018 | // update_engine restarted under the same build and same slot. |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 1019 | if (result != OTAResult::OTA_SUCCESSFUL) { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1020 | // Increment the reboot number if |kPrefsNumReboots| exists. That pref is |
| 1021 | // set when we start a new update. |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 1022 | if (prefs_->Exists(kPrefsNumReboots)) { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1023 | int64_t reboot_count = |
| 1024 | metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_); |
| 1025 | metrics_utils::SetNumReboots(reboot_count + 1, prefs_); |
| 1026 | } |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 1027 | |
| 1028 | if (result == OTAResult::ROLLED_BACK) { |
| 1029 | // This will release all space previously allocated for apex |
| 1030 | // decompression. If we detect a rollback, we should release space and |
| 1031 | // return the space to user. Any subsequent attempt to install OTA will |
| 1032 | // allocate space again anyway. |
| 1033 | LOG(INFO) << "Detected a rollback, releasing space allocated for apex " |
| 1034 | "deompression."; |
| 1035 | apex_handler_android_->AllocateSpace({}); |
| 1036 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 1037 | } |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | // Now that the build version changes, report the update metrics. |
| 1042 | // TODO(xunchang) check the build version is larger than the previous one. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1043 | prefs_->SetString(kPrefsPreviousVersion, current_version); |
Kelvin Zhang | d6fd682 | 2021-05-26 09:50:56 -0400 | [diff] [blame] | 1044 | prefs_->SetInt64(kPrefsPreviousSlot, boot_control_->GetCurrentSlot()); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1045 | |
| 1046 | bool previous_attempt_exists = prefs_->Exists(kPrefsPayloadAttemptNumber); |
| 1047 | // |kPrefsPayloadAttemptNumber| should be cleared upon successful update. |
| 1048 | if (previous_attempt_exists) { |
| 1049 | metrics_reporter_->ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
| 1050 | } |
| 1051 | |
| 1052 | metrics_utils::LoadAndReportTimeToReboot( |
| 1053 | metrics_reporter_.get(), prefs_, clock_.get()); |
| 1054 | ClearMetricsPrefs(); |
Sen Jiang | 1bafff8 | 2019-01-02 15:54:40 -0800 | [diff] [blame] | 1055 | |
| 1056 | // Also reset the update progress if the build version has changed. |
| 1057 | if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) { |
| 1058 | LOG(WARNING) << "Unable to reset the update progress."; |
| 1059 | } |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | // Save the update start time. Reset the reboot count and attempt number if the |
| 1063 | // update isn't a resume; otherwise increment the attempt number. |
| 1064 | void UpdateAttempterAndroid::UpdatePrefsOnUpdateStart(bool is_resume) { |
| 1065 | if (!is_resume) { |
| 1066 | metrics_utils::SetNumReboots(0, prefs_); |
| 1067 | metrics_utils::SetPayloadAttemptNumber(1, prefs_); |
| 1068 | } else { |
| 1069 | int64_t attempt_number = |
| 1070 | metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_); |
| 1071 | metrics_utils::SetPayloadAttemptNumber(attempt_number + 1, prefs_); |
| 1072 | } |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 1073 | metrics_utils::SetUpdateTimestampStart(clock_->GetMonotonicTime(), prefs_); |
| 1074 | metrics_utils::SetUpdateBootTimestampStart(clock_->GetBootTime(), prefs_); |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 1075 | ClearUpdateCompletedMarker(); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | void UpdateAttempterAndroid::ClearMetricsPrefs() { |
| 1079 | CHECK(prefs_); |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 1080 | metric_bytes_downloaded_.Delete(); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1081 | prefs_->Delete(kPrefsNumReboots); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1082 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 1083 | prefs_->Delete(kPrefsUpdateTimestampStart); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 1084 | prefs_->Delete(kPrefsUpdateBootTimestampStart); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 1087 | BootControlInterface::Slot UpdateAttempterAndroid::GetCurrentSlot() const { |
| 1088 | return boot_control_->GetCurrentSlot(); |
| 1089 | } |
| 1090 | |
| 1091 | BootControlInterface::Slot UpdateAttempterAndroid::GetTargetSlot() const { |
| 1092 | return GetCurrentSlot() == 0 ? 1 : 0; |
| 1093 | } |
| 1094 | |
Yifan Hong | 6f7e29f | 2019-12-13 14:41:06 -0800 | [diff] [blame] | 1095 | uint64_t UpdateAttempterAndroid::AllocateSpaceForPayload( |
| 1096 | const std::string& metadata_filename, |
| 1097 | const vector<string>& key_value_pair_headers, |
| 1098 | brillo::ErrorPtr* error) { |
Yifan Hong | 6561303 | 2020-01-09 17:52:13 -0800 | [diff] [blame] | 1099 | DeltaArchiveManifest manifest; |
| 1100 | if (!VerifyPayloadParseManifest(metadata_filename, &manifest, error)) { |
| 1101 | return 0; |
| 1102 | } |
| 1103 | std::map<string, string> headers; |
| 1104 | if (!ParseKeyValuePairHeaders(key_value_pair_headers, &headers, error)) { |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1108 | std::vector<ApexInfo> apex_infos(manifest.apex_info().begin(), |
| 1109 | manifest.apex_info().end()); |
| 1110 | uint64_t apex_size_required = 0; |
| 1111 | if (apex_handler_android_ != nullptr) { |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 1112 | auto result = apex_handler_android_->CalculateSize(apex_infos); |
| 1113 | if (!result.ok()) { |
| 1114 | LogAndSetError(error, |
| 1115 | FROM_HERE, |
| 1116 | "Failed to calculate size required for compressed APEX"); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | apex_size_required = *result; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Yifan Hong | 6561303 | 2020-01-09 17:52:13 -0800 | [diff] [blame] | 1122 | string payload_id = GetPayloadId(headers); |
| 1123 | uint64_t required_size = 0; |
| 1124 | if (!DeltaPerformer::PreparePartitionsForUpdate(prefs_, |
| 1125 | boot_control_, |
| 1126 | GetTargetSlot(), |
| 1127 | manifest, |
| 1128 | payload_id, |
| 1129 | &required_size)) { |
| 1130 | if (required_size == 0) { |
| 1131 | LogAndSetError(error, FROM_HERE, "Failed to allocate space for payload."); |
| 1132 | return 0; |
| 1133 | } else { |
| 1134 | LOG(ERROR) << "Insufficient space for payload: " << required_size |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1135 | << " bytes, apex decompression: " << apex_size_required |
Yifan Hong | 6561303 | 2020-01-09 17:52:13 -0800 | [diff] [blame] | 1136 | << " bytes"; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1137 | return required_size + apex_size_required; |
Yifan Hong | 6561303 | 2020-01-09 17:52:13 -0800 | [diff] [blame] | 1138 | } |
| 1139 | } |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (apex_size_required > 0 && apex_handler_android_ != nullptr && |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 1142 | !apex_handler_android_->AllocateSpace(apex_infos)) { |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1143 | LOG(ERROR) << "Insufficient space for apex decompression: " |
| 1144 | << apex_size_required << " bytes"; |
| 1145 | return apex_size_required; |
Kelvin Zhang | 8933d57 | 2021-01-28 10:17:34 -0500 | [diff] [blame] | 1146 | } |
Yifan Hong | 6561303 | 2020-01-09 17:52:13 -0800 | [diff] [blame] | 1147 | |
| 1148 | LOG(INFO) << "Successfully allocated space for payload."; |
Yifan Hong | 6f7e29f | 2019-12-13 14:41:06 -0800 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 1152 | void UpdateAttempterAndroid::CleanupSuccessfulUpdate( |
| 1153 | std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback, |
Yifan Hong | 2236ea0 | 2019-12-13 16:11:22 -0800 | [diff] [blame] | 1154 | brillo::ErrorPtr* error) { |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 1155 | if (cleanup_previous_update_code_.has_value()) { |
| 1156 | LOG(INFO) << "CleanupSuccessfulUpdate has previously completed with " |
| 1157 | << utils::ErrorCodeToString(*cleanup_previous_update_code_); |
| 1158 | if (callback) { |
| 1159 | callback->OnCleanupComplete( |
| 1160 | static_cast<int32_t>(*cleanup_previous_update_code_)); |
| 1161 | } |
| 1162 | return; |
Yifan Hong | 4f61156 | 2020-01-15 23:41:33 -0800 | [diff] [blame] | 1163 | } |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 1164 | if (callback) { |
| 1165 | auto callback_ptr = callback.get(); |
| 1166 | cleanup_previous_update_callbacks_.emplace_back(std::move(callback)); |
| 1167 | callback_ptr->RegisterForDeathNotifications( |
| 1168 | base::Bind(&UpdateAttempterAndroid::RemoveCleanupPreviousUpdateCallback, |
| 1169 | base::Unretained(this), |
| 1170 | base::Unretained(callback_ptr))); |
| 1171 | } |
| 1172 | ScheduleCleanupPreviousUpdate(); |
Yifan Hong | 2236ea0 | 2019-12-13 16:11:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Tianjie | 7f8f2ab | 2021-07-23 17:08:50 -0700 | [diff] [blame] | 1175 | bool UpdateAttempterAndroid::setShouldSwitchSlotOnReboot( |
| 1176 | const std::string& metadata_filename, brillo::ErrorPtr* error) { |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1177 | LOG(INFO) << "setShouldSwitchSlotOnReboot(" << metadata_filename << ")"; |
Tianjie | 7f8f2ab | 2021-07-23 17:08:50 -0700 | [diff] [blame] | 1178 | if (processor_->IsRunning()) { |
| 1179 | return LogAndSetError( |
| 1180 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 1181 | } |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1182 | DeltaArchiveManifest manifest; |
| 1183 | TEST_AND_RETURN_FALSE( |
| 1184 | VerifyPayloadParseManifest(metadata_filename, &manifest, error)); |
| 1185 | |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1186 | InstallPlan install_plan_; |
| 1187 | install_plan_.source_slot = GetCurrentSlot(); |
| 1188 | install_plan_.target_slot = GetTargetSlot(); |
| 1189 | // Don't do verity computation, just hash the partitions |
| 1190 | install_plan_.write_verity = false; |
| 1191 | // Don't run postinstall, we just need PostinstallAction to switch the slots. |
| 1192 | install_plan_.run_post_install = false; |
| 1193 | install_plan_.is_resume = true; |
| 1194 | |
| 1195 | CHECK_NE(install_plan_.source_slot, UINT32_MAX); |
| 1196 | CHECK_NE(install_plan_.target_slot, UINT32_MAX); |
| 1197 | |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1198 | auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan_); |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1199 | auto postinstall_runner_action = |
| 1200 | std::make_unique<PostinstallRunnerAction>(boot_control_, hardware_); |
| 1201 | SetStatusAndNotify(UpdateStatus::VERIFYING); |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1202 | postinstall_runner_action->set_delegate(this); |
| 1203 | |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 1204 | // If last error code is kUpdatedButNotActive, we know that we reached this |
| 1205 | // state by calling applyPayload() with switch_slot=false. That applyPayload() |
| 1206 | // call would have already performed filesystem verification, therefore, we |
| 1207 | // can safely skip the verification to save time. |
| 1208 | if (last_error_ == ErrorCode::kUpdatedButNotActive) { |
| 1209 | BondActions(install_plan_action.get(), postinstall_runner_action.get()); |
| 1210 | processor_->EnqueueAction(std::move(install_plan_action)); |
| 1211 | } else { |
Kelvin Zhang | 263a540 | 2022-12-08 23:03:32 -0800 | [diff] [blame] | 1212 | if (!boot_control_->GetDynamicPartitionControl() |
| 1213 | ->PreparePartitionsForUpdate(GetCurrentSlot(), |
| 1214 | GetTargetSlot(), |
| 1215 | manifest, |
| 1216 | false /* should update */, |
| 1217 | nullptr)) { |
| 1218 | return LogAndSetError( |
| 1219 | error, FROM_HERE, "Failed to PreparePartitionsForUpdate"); |
| 1220 | } |
| 1221 | ErrorCode error_code{}; |
| 1222 | if (!install_plan_.ParsePartitions(manifest.partitions(), |
| 1223 | boot_control_, |
| 1224 | manifest.block_size(), |
| 1225 | &error_code)) { |
| 1226 | return LogAndSetError(error, |
| 1227 | FROM_HERE, |
| 1228 | "Failed to LoadPartitionsFromSlots " + |
| 1229 | utils::ErrorCodeToString(error_code)); |
| 1230 | } |
| 1231 | |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 1232 | auto filesystem_verifier_action = |
| 1233 | std::make_unique<FilesystemVerifierAction>( |
| 1234 | boot_control_->GetDynamicPartitionControl()); |
| 1235 | filesystem_verifier_action->set_delegate(this); |
| 1236 | BondActions(install_plan_action.get(), filesystem_verifier_action.get()); |
| 1237 | BondActions(filesystem_verifier_action.get(), |
| 1238 | postinstall_runner_action.get()); |
| 1239 | processor_->EnqueueAction(std::move(install_plan_action)); |
| 1240 | processor_->EnqueueAction(std::move(filesystem_verifier_action)); |
| 1241 | } |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1242 | |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1243 | processor_->EnqueueAction(std::move(postinstall_runner_action)); |
| 1244 | ScheduleProcessingStart(); |
| 1245 | return true; |
Tianjie | 7f8f2ab | 2021-07-23 17:08:50 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | bool UpdateAttempterAndroid::resetShouldSwitchSlotOnReboot( |
| 1249 | brillo::ErrorPtr* error) { |
| 1250 | if (processor_->IsRunning()) { |
| 1251 | return LogAndSetError( |
| 1252 | error, FROM_HERE, "Already processing an update, cancel it first."); |
| 1253 | } |
Daniel Zheng | e76cf56 | 2022-11-08 17:32:10 +0000 | [diff] [blame] | 1254 | TEST_AND_RETURN_FALSE(ClearUpdateCompletedMarker()); |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1255 | // Update the boot flags so the current slot has higher priority. |
| 1256 | if (!boot_control_->SetActiveBootSlot(GetCurrentSlot())) { |
| 1257 | return LogAndSetError(error, FROM_HERE, "Failed to SetActiveBootSlot"); |
| 1258 | } |
| 1259 | |
| 1260 | // Mark the current slot as successful again, since marking it as active |
| 1261 | // may reset the successful bit. We ignore the result of whether marking |
| 1262 | // the current slot as successful worked. |
| 1263 | if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful) {}))) { |
| 1264 | return LogAndSetError( |
| 1265 | error, FROM_HERE, "Failed to MarkBootSuccessfulAsync"); |
| 1266 | } |
| 1267 | |
| 1268 | // Resets the warm reset property since we won't switch the slot. |
| 1269 | hardware_->SetWarmReset(false); |
| 1270 | |
| 1271 | // Resets the vbmeta digest. |
| 1272 | hardware_->SetVbmetaDigestForInactiveSlot(true /* reset */); |
| 1273 | LOG(INFO) << "Slot switch cancelled."; |
| 1274 | SetStatusAndNotify(UpdateStatus::IDLE); |
| 1275 | return true; |
Tianjie | 7f8f2ab | 2021-07-23 17:08:50 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 1278 | void UpdateAttempterAndroid::ScheduleCleanupPreviousUpdate() { |
| 1279 | // If a previous CleanupSuccessfulUpdate call has not finished, or an update |
| 1280 | // is in progress, skip enqueueing the action. |
| 1281 | if (processor_->IsRunning()) { |
| 1282 | LOG(INFO) << "Already processing an update. CleanupPreviousUpdate should " |
| 1283 | << "be done when the current update finishes."; |
| 1284 | return; |
| 1285 | } |
| 1286 | LOG(INFO) << "Scheduling CleanupPreviousUpdateAction."; |
| 1287 | auto action = |
| 1288 | boot_control_->GetDynamicPartitionControl() |
| 1289 | ->GetCleanupPreviousUpdateAction(boot_control_, prefs_, this); |
| 1290 | processor_->EnqueueAction(std::move(action)); |
| 1291 | processor_->set_delegate(this); |
| 1292 | SetStatusAndNotify(UpdateStatus::CLEANUP_PREVIOUS_UPDATE); |
| 1293 | processor_->StartProcessing(); |
| 1294 | } |
| 1295 | |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 1296 | void UpdateAttempterAndroid::OnCleanupProgressUpdate(double progress) { |
| 1297 | for (auto&& callback : cleanup_previous_update_callbacks_) { |
| 1298 | callback->OnCleanupProgressUpdate(progress); |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | void UpdateAttempterAndroid::NotifyCleanupPreviousUpdateCallbacksAndClear() { |
| 1303 | CHECK(cleanup_previous_update_code_.has_value()); |
| 1304 | for (auto&& callback : cleanup_previous_update_callbacks_) { |
| 1305 | callback->OnCleanupComplete( |
| 1306 | static_cast<int32_t>(*cleanup_previous_update_code_)); |
| 1307 | } |
| 1308 | cleanup_previous_update_callbacks_.clear(); |
| 1309 | } |
| 1310 | |
| 1311 | void UpdateAttempterAndroid::RemoveCleanupPreviousUpdateCallback( |
| 1312 | CleanupSuccessfulUpdateCallbackInterface* callback) { |
| 1313 | auto end_it = |
| 1314 | std::remove_if(cleanup_previous_update_callbacks_.begin(), |
| 1315 | cleanup_previous_update_callbacks_.end(), |
| 1316 | [&](const auto& e) { return e.get() == callback; }); |
| 1317 | cleanup_previous_update_callbacks_.erase( |
| 1318 | end_it, cleanup_previous_update_callbacks_.end()); |
| 1319 | } |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 1320 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 1321 | } // namespace chromeos_update_engine |