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