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