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