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