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